home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cenvi23.zip / MEMSRCH.LIB < prev    next >
Text File  |  1996-02-12  |  4KB  |  129 lines

  1. // MemSrch.lib - Provide MemSearch routine to use assembly
  2. // ver.1         to greatly speed-up search for sub-text
  3. //               within a bigger buffer
  4.  
  5. if !defined(_DOS_) && !define(_WIN16_) {
  6.    puts("MEMSRCH.LIB requires 16-bit DOS or Windows version of CEnvi.\a\n");
  7.    exit(EXIT_FAILURE);
  8. }
  9.  
  10. MemSearchForMem(pBuffer,pBufferLen,pSubBuffer,pSubBufferLen,pCaseSensitive)
  11. {
  12.    //   public _thing
  13.    //
  14.    //ToUpperToLower PROC NEAR
  15.    //Upper_ah:
  16.    //   cmp   ah, 'a'
  17.    //   jb    Lower_al
  18.    //   cmp   ah, 'z'
  19.    //   ja    Lower_al
  20.    //   sub   ah, 'a' - 'A'
  21.    //Lower_al:
  22.    //   cmp   al, 'A'
  23.    //   jb    UpLowDone
  24.    //   cmp   al, 'Z'
  25.    //   ja    UpLowDone
  26.    //   add   ah, 'a' - 'A'
  27.    //UpLowDone:
  28.    //   ret
  29.    //ToUpperToLower ENDP
  30.    //
  31.    //
  32.    //_thing PROC FAR
  33.    //
  34.    //   ; assume ax is segment of big buffer
  35.    //   ; assume bx is segment of little buffer
  36.    //   ; assume cx is length to search first character in big buffer
  37.    //   ; assume dx is length of sub-buffer
  38.    //
  39.    //   ; set di to location of big buffer so ax:di is big buffer
  40.    //   mov   di, 0
  41.    //   ; set si to location of sub-buffer so bx:si is sub-buffer
  42.    //   mov   si, 0
  43.    //
  44.    //   ; get ds:di to big buffer, and es:si to sub-buffer
  45.    //   mov   ds, ax
  46.    //   mov   es, bx
  47.    //
  48.    //   ; set al and ah to the byte may starts the sub-buffer, assume
  49.    //   ; case insensitive, but CEnvi may overwrite that
  50.    //   mov   al, es:[si]
  51.    //   mov   ah, al
  52.    //   call  ToUpperToLower
  53.    //
  54.    //IsThisTheFirstByte:
  55.    //   cmp   al, [di]
  56.    //   je    FirstByteFound
  57.    //   cmp   ah, [di]
  58.    //   jne   NotFoundThisByte
  59.    //
  60.    //FirstByteFound:
  61.    //   ; found first byte, and so check all the other bytes for a match
  62.    //   push  di
  63.    //   push  ax
  64.    //   xor   bx, bx
  65.    //KeepLookingInSubBuffer:
  66.    //   inc   bx
  67.    //   inc   di
  68.    //   cmp   bx, dx
  69.    //   je    FoundIt
  70.    //   mov   al, es:[si+bx]
  71.    //   mov   ah, al
  72.    //   call  ToUpperToLower
  73.    //   cmp   al, [di]
  74.    //   je    KeepLookingInSubBuffer
  75.    //   cmp   ah, [di]
  76.    //   je    KeepLookingInSubBuffer
  77.    //
  78.    //NotThisSubBuffer:
  79.    //   pop   ax
  80.    //   pop   di
  81.    //
  82.    //NotFoundThisByte:
  83.    //   inc   di
  84.    //   loop  IsThisTheFirstByte
  85.    //
  86.    //   ; if here then buffer was never found; return NULL
  87.    //   xor   ax, ax
  88.    //   xor   dx, dx
  89.    //   jmp   ByeBye
  90.    //
  91.    //FoundIt:
  92.    //   ; Yea! it was found; return address where it was found
  93.    //   pop   ax
  94.    //   pop   ax       ; ax = di becaue had pushed di
  95.    //   mov   dx, ds
  96.    //
  97.    //ByeBye:
  98.    //   ret
  99.    //
  100.    //_thing ENDP
  101.    lAsmCode =
  102.       "\x80\xFC\x61\x72\x08\x80\xFC\x7A\x77\x03\x80\xEC\x20\x3C\x41\x72"
  103.       "\x07\x3C\x5A\x77\x03\x80\xC4\x20\xC3\xBF\x00\x00\xBE\x00\x00\x8E"
  104.       "\xD8\x8E\xC3\x26\x8A\x04\x88\xC4\xE8\xD5\xFF\x3A\x05\x74\x04\x3A"
  105.       "\x25\x75\x1C\x57\x50\x31\xDB\x43\x47\x39\xD3\x74\x1B\x26\x8A\x00"
  106.       "\x88\xC4\xE8\xBB\xFF\x3A\x05\x74\xEE\x3A\x25\x74\xEA\x58\x5F\x47"
  107.       "\xE2\xD9\x31\xC0\x31\xD2\xEB\x04\x58\x58\x8C\xDA\xCB";
  108.    // only search first byte up to first one that can fit entire SubBuffer
  109.    lSearchLen = pBufferLen - pSubBufferLen + 1;
  110.    if ( lSearchLen < 0 )
  111.       return NULL;
  112.    // make local copy of assembly so we can change it
  113.    memcpy(lAsm,lAsmCode,1+GetArraySpan(lAsmCode));
  114.    // if case-sensitive then do have case-sensitive sub-routine do nothing
  115.    if ( pCaseSensitive )
  116.       lAsm[0] = '\xC3';
  117.    // set di in code to offst of Buffer, and si to offset of SubBuffer
  118.    BLObPut(lAsm,0x1A,offset(pBuffer),UWORD16);
  119.    BLObPut(lAsm,0x1D,offset(pSubBuffer),UWORD16);
  120.    // call the routine with ax, bx, cx, and dx as assebmly likes it
  121.    lFound = asm(lAsm+0x19,segment(pBuffer),segment(pSubBuffer),
  122.                 lSearchLen,pSubBufferLen);
  123.    if ( !lFound )
  124.       return NULL;
  125.    // return variable relative to pBuffer
  126.    return ( pBuffer + (lFound-pointer(pBuffer)) );
  127. }
  128.  
  129.