| 1 | #include "stdafx.h" |
| 2 | #include "KmpSearch.h" |
| 3 | #include <stdio.h> |
| 4 | #include <stdlib.h> |
| 5 | #include <string.h> |
| 6 | |
| 7 | #ifdef _MSC_VER |
| 8 | |
| 9 | __declspec(naked) void __fastcall KmpSearch::init(const char *subStr) { |
| 10 | __asm { |
| 11 | // kmp initialization, make jump table for mismatches |
| 12 | push esi |
| 13 | push edi |
| 14 | push ebp |
| 15 | push ebx |
| 16 | push ecx |
| 17 | |
| 18 | mov esi, edx // edx: subStr argument |
| 19 | lea edi, [ecx + KmpSearch::_subStr] // this + 0x100 |
| 20 | lea ebx, [ecx + KmpSearch::_retarget] // this |
| 21 | lea ebp, [ecx + 1] |
| 22 | |
| 23 | mov byte ptr [ebx], -1 |
| 24 | |
| 25 | xor eax, eax |
| 26 | |
| 27 | loopStart: |
| 28 | shr ecx, 8 |
| 29 | test al, 3 |
| 30 | jnz short dontLoad |
| 31 | mov ecx, dword ptr [esi + eax] // load next 4 bytes of subStr |
| 32 | mov dword ptr [edi], ecx // and copy them to this._subStr while we're at it |
| 33 | lea edi, [edi + 4] |
| 34 | dontLoad: |
| 35 | |
| 36 | or cl, cl // end of subStr? |
| 37 | jz short endOfString |
| 38 | |
| 39 | mov edx, eax // save counter (i) in edx |
| 40 | |
| 41 | xlat // al = retarget[i] |
| 42 | inc al |
| 43 | mov byte ptr [ebp + edx], al // retarget[i + 1] = retarget[i] + 1 |
| 44 | jz short decLoopEnd |
| 45 | decrementLoop: |
| 46 | dec al |
| 47 | mov ah, byte ptr [esi + eax] // ah = sub[retarget[i + 1] - 1] |
| 48 | cmp ah, cl // if (ah == sub[i]) |
| 49 | jz short decLoopEnd // goto decLoopEnd |
| 50 | |
| 51 | xlat // al = retarget[retarget[i + 1] - 1] |
| 52 | xor ah, ah |
| 53 | inc al // al = retarget[retarget[i + 1] - 1] + 1 |
| 54 | mov byte ptr [ebp + edx], al // retarget[i + 1] = al |
| 55 | jnz short decrementLoop |
| 56 | decLoopEnd: |
| 57 | lea eax, [edx + 1] // i = i + 1 |
| 58 | jmp short loopStart |
| 59 | |
| 60 | endOfString: |
| 61 | |
| 62 | pop ecx // this |
| 63 | mov [ecx + KmpSearch::_strLen], eax // length of substring (without '\0') |
| 64 | |
| 65 | pop ebx |
| 66 | pop ebp |
| 67 | pop edi |
| 68 | pop esi |
| 69 | ret |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | __declspec(naked) char * __fastcall KmpSearch::search(const char *str) { |
| 74 | __asm { |
| 75 | push esi |
| 76 | push edi |
| 77 | push ebx |
| 78 | |
| 79 | mov esi, edx // edx: str argument |
| 80 | lea edi, [ecx + KmpSearch::_subStr] |
| 81 | lea ebx, [ecx + KmpSearch::_retarget] |
| 82 | mov ch, byte ptr [ecx + KmpSearch::_strLen] |
| 83 | |
| 84 | or ch, ch // if (_strLen == 0) |
| 85 | jz short endOfString // goto endOfString |
| 86 | |
| 87 | xor edx, edx // index |
| 88 | |
| 89 | mov cl, 3 |
| 90 | searchLoop: |
| 91 | shr eax, 8 |
| 92 | inc cl |
| 93 | test cl, 4 |
| 94 | jz short skipRead |
| 95 | lodsd |
| 96 | xor cl, cl |
| 97 | skipRead: |
| 98 | |
| 99 | test al, al |
| 100 | jz short endOfString |
| 101 | |
| 102 | newIndexLoop: |
| 103 | cmp al, byte ptr [edi + edx] // while (c != sub[index]) { |
| 104 | jz short gotNewIndex |
| 105 | or edx, edx // if (index == 0) |
| 106 | jz short searchLoop // goto searchLoop |
| 107 | movzx edx, byte ptr [ebx + edx] // index = retarget[index] |
| 108 | jmp short newIndexLoop // } |
| 109 | |
| 110 | gotNewIndex: |
| 111 | inc edx |
| 112 | cmp dl, ch // if (index != _strLen) |
| 113 | jne short searchLoop // goto searchLoop |
| 114 | |
| 115 | movzx ebx, cl // bytes of eax used |
| 116 | movzx ecx, ch // length of subStr |
| 117 | |
| 118 | lea eax, [esi + ebx - 3] |
| 119 | sub eax, ecx |
| 120 | |
| 121 | pop ebx |
| 122 | pop edi |
| 123 | pop esi |
| 124 | ret |
| 125 | |
| 126 | endOfString: |
| 127 | xor eax, eax // return NULL |
| 128 | pop ebx |
| 129 | pop edi |
| 130 | pop esi |
| 131 | ret |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | #else |
| 136 | |
| 137 | void __fastcall KmpSearch::init(const char *subStr) { |
| 138 | strcpy(_subStr, subStr); |
| 139 | } |
| 140 | |
| 141 | char * __fastcall KmpSearch::search(const char *str) { |
| 142 | return strstr(str, _subStr); |
| 143 | } |
| 144 | |
| 145 | #endif |
| 146 |