| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
#include "glk.h" |
| 8 |
#include "git.h" |
| 9 |
#include "opcodes.h" |
| 10 |
|
| 11 |
#ifndef TRUE |
| 12 |
#define TRUE 1 |
| 13 |
#endif |
| 14 |
#ifndef FALSE |
| 15 |
#define FALSE 0 |
| 16 |
#endif |
| 17 |
|
| 18 |
#define serop_KeyIndirect (0x01) |
| 19 |
#define serop_ZeroKeyTerminates (0x02) |
| 20 |
#define serop_ReturnIndex (0x04) |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
static void fetchkey(unsigned char *keybuf, glui32 key, glui32 keysize, |
| 50 |
glui32 options); |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
glui32 git_linear_search(glui32 key, glui32 keysize, |
| 68 |
glui32 start, glui32 structsize, glui32 numstructs, |
| 69 |
glui32 keyoffset, glui32 options) |
| 70 |
{ |
| 71 |
unsigned char keybuf[4]; |
| 72 |
glui32 count; |
| 73 |
int ix; |
| 74 |
int retindex = ((options & serop_ReturnIndex) != 0); |
| 75 |
int zeroterm = ((options & serop_ZeroKeyTerminates) != 0); |
| 76 |
|
| 77 |
fetchkey(keybuf, key, keysize, options); |
| 78 |
|
| 79 |
for (count=0; count<numstructs; count++, start+=structsize) { |
| 80 |
int match = TRUE; |
| 81 |
if (keysize <= 4) { |
| 82 |
for (ix=0; match && ix<keysize; ix++) { |
| 83 |
if (memRead8(start + keyoffset + ix) != keybuf[ix]) |
| 84 |
match = FALSE; |
| 85 |
} |
| 86 |
} |
| 87 |
else { |
| 88 |
for (ix=0; match && ix<keysize; ix++) { |
| 89 |
if (memRead8(start + keyoffset + ix) != memRead8(key + ix)) |
| 90 |
match = FALSE; |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
if (match) { |
| 95 |
if (retindex) |
| 96 |
return count; |
| 97 |
else |
| 98 |
return start; |
| 99 |
} |
| 100 |
|
| 101 |
if (zeroterm) { |
| 102 |
match = TRUE; |
| 103 |
for (ix=0; match && ix<keysize; ix++) { |
| 104 |
if (memRead8(start + keyoffset + ix) != 0) |
| 105 |
match = FALSE; |
| 106 |
} |
| 107 |
if (match) { |
| 108 |
break; |
| 109 |
} |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
if (retindex) |
| 114 |
return -1; |
| 115 |
else |
| 116 |
return 0; |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
static glui32 binary_search_generic(glui32 key, glui32 keysize, |
| 130 |
glui32 start, glui32 structsize, glui32 numstructs, |
| 131 |
glui32 keyoffset, glui32 options) |
| 132 |
{ |
| 133 |
unsigned char keybuf[4]; |
| 134 |
unsigned char byte, byte2; |
| 135 |
glui32 top, bot, val, addr; |
| 136 |
int ix; |
| 137 |
int retindex = ((options & serop_ReturnIndex) != 0); |
| 138 |
|
| 139 |
fetchkey(keybuf, key, keysize, options); |
| 140 |
|
| 141 |
bot = 0; |
| 142 |
top = numstructs; |
| 143 |
while (bot < top) { |
| 144 |
int cmp = 0; |
| 145 |
val = (top+bot) / 2; |
| 146 |
addr = start + val * structsize; |
| 147 |
|
| 148 |
if (keysize <= 4) { |
| 149 |
for (ix=0; (!cmp) && ix<keysize; ix++) { |
| 150 |
byte = memRead8(addr + keyoffset + ix); |
| 151 |
byte2 = keybuf[ix]; |
| 152 |
if (byte < byte2) |
| 153 |
cmp = -1; |
| 154 |
else if (byte > byte2) |
| 155 |
cmp = 1; |
| 156 |
} |
| 157 |
} |
| 158 |
else { |
| 159 |
for (ix=0; (!cmp) && ix<keysize; ix++) { |
| 160 |
byte = memRead8(addr + keyoffset + ix); |
| 161 |
byte2 = memRead8(key + ix); |
| 162 |
if (byte < byte2) |
| 163 |
cmp = -1; |
| 164 |
else if (byte > byte2) |
| 165 |
cmp = 1; |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
if (!cmp) { |
| 170 |
if (retindex) |
| 171 |
return val; |
| 172 |
else |
| 173 |
return addr; |
| 174 |
} |
| 175 |
|
| 176 |
if (cmp < 0) { |
| 177 |
bot = val+1; |
| 178 |
} |
| 179 |
else { |
| 180 |
top = val; |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
if (retindex) |
| 185 |
return -1; |
| 186 |
else |
| 187 |
return 0; |
| 188 |
} |
| 189 |
|
| 190 |
static glui32 binary_search_16bit(git_uint16 key, glui32 start, |
| 191 |
glui32 structsize, glui32 numstructs, |
| 192 |
glui32 keyoffset, glui32 options) |
| 193 |
{ |
| 194 |
glui32 addr, top, bot, val; |
| 195 |
git_uint16 m; |
| 196 |
int retindex = ((options & serop_ReturnIndex) != 0); |
| 197 |
|
| 198 |
bot = 0; |
| 199 |
top = numstructs; |
| 200 |
while (bot < top) { |
| 201 |
val = (top+bot) / 2; |
| 202 |
addr = start + val * structsize; |
| 203 |
|
| 204 |
m = memRead16(addr + keyoffset); |
| 205 |
if (m == key) { |
| 206 |
if (retindex) |
| 207 |
return val; |
| 208 |
else |
| 209 |
return addr; |
| 210 |
} else if (m < key) { |
| 211 |
bot = val + 1; |
| 212 |
} else { |
| 213 |
top = val; |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
if (retindex) |
| 218 |
return -1; |
| 219 |
else |
| 220 |
return 0; |
| 221 |
} |
| 222 |
|
| 223 |
static glui32 binary_search_32bit(git_uint32 key, glui32 start, |
| 224 |
glui32 structsize, glui32 numstructs, |
| 225 |
glui32 keyoffset, glui32 options) |
| 226 |
{ |
| 227 |
glui32 addr, top, bot, val, m; |
| 228 |
int retindex = ((options & serop_ReturnIndex) != 0); |
| 229 |
|
| 230 |
bot = 0; |
| 231 |
top = numstructs; |
| 232 |
while (bot < top) { |
| 233 |
val = (top+bot) / 2; |
| 234 |
addr = start + val * structsize; |
| 235 |
|
| 236 |
m = memRead32(addr + keyoffset); |
| 237 |
if (m == key) { |
| 238 |
if (retindex) |
| 239 |
return val; |
| 240 |
else |
| 241 |
return addr; |
| 242 |
} else if (m < key) { |
| 243 |
bot = val + 1; |
| 244 |
} else { |
| 245 |
top = val; |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
if (retindex) |
| 250 |
return -1; |
| 251 |
else |
| 252 |
return 0; |
| 253 |
} |
| 254 |
|
| 255 |
glui32 git_binary_search(glui32 key, glui32 keysize, |
| 256 |
glui32 start, glui32 structsize, glui32 numstructs, |
| 257 |
glui32 keyoffset, glui32 options) |
| 258 |
{ |
| 259 |
if (keysize == 2) { |
| 260 |
git_uint16 key16; |
| 261 |
if ((options & serop_KeyIndirect) != 0) { |
| 262 |
key16 = memRead16(key); |
| 263 |
} else { |
| 264 |
key16 = key; |
| 265 |
} |
| 266 |
return binary_search_16bit(key16, start, structsize, numstructs, keyoffset, options); |
| 267 |
} |
| 268 |
if (keysize == 4) { |
| 269 |
git_uint32 key32; |
| 270 |
if ((options & serop_KeyIndirect) != 0) { |
| 271 |
key32 = memRead32(key); |
| 272 |
} else { |
| 273 |
key32 = key; |
| 274 |
} |
| 275 |
return binary_search_32bit(key32, start, structsize, numstructs, keyoffset, options); |
| 276 |
} |
| 277 |
return binary_search_generic(key, keysize, start, structsize, numstructs, keyoffset, options); |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
glui32 git_linked_search(glui32 key, glui32 keysize, |
| 289 |
glui32 start, glui32 keyoffset, glui32 nextoffset, glui32 options) |
| 290 |
{ |
| 291 |
unsigned char keybuf[4]; |
| 292 |
int ix; |
| 293 |
glui32 val; |
| 294 |
int zeroterm = ((options & serop_ZeroKeyTerminates) != 0); |
| 295 |
|
| 296 |
fetchkey(keybuf, key, keysize, options); |
| 297 |
|
| 298 |
while (start != 0) { |
| 299 |
int match = TRUE; |
| 300 |
if (keysize <= 4) { |
| 301 |
for (ix=0; match && ix<keysize; ix++) { |
| 302 |
if (memRead8(start + keyoffset + ix) != keybuf[ix]) |
| 303 |
match = FALSE; |
| 304 |
} |
| 305 |
} |
| 306 |
else { |
| 307 |
for (ix=0; match && ix<keysize; ix++) { |
| 308 |
if (memRead8(start + keyoffset + ix) != memRead8(key + ix)) |
| 309 |
match = FALSE; |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
if (match) { |
| 314 |
return start; |
| 315 |
} |
| 316 |
|
| 317 |
if (zeroterm) { |
| 318 |
match = TRUE; |
| 319 |
for (ix=0; match && ix<keysize; ix++) { |
| 320 |
if (memRead8(start + keyoffset + ix) != 0) |
| 321 |
match = FALSE; |
| 322 |
} |
| 323 |
if (match) { |
| 324 |
break; |
| 325 |
} |
| 326 |
} |
| 327 |
|
| 328 |
val = start + nextoffset; |
| 329 |
start = memRead32(val); |
| 330 |
} |
| 331 |
|
| 332 |
return 0; |
| 333 |
} |
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
static void fetchkey(unsigned char *keybuf, glui32 key, glui32 keysize, |
| 341 |
glui32 options) |
| 342 |
{ |
| 343 |
int ix; |
| 344 |
|
| 345 |
if (options & serop_KeyIndirect) { |
| 346 |
if (keysize <= 4) { |
| 347 |
for (ix=0; ix<keysize; ix++) |
| 348 |
keybuf[ix] = memRead8(key+ix); |
| 349 |
} |
| 350 |
} |
| 351 |
else { |
| 352 |
switch (keysize) { |
| 353 |
case 4: |
| 354 |
write32(keybuf, key); |
| 355 |
break; |
| 356 |
case 2: |
| 357 |
write16(keybuf, key); |
| 358 |
break; |
| 359 |
case 1: |
| 360 |
write8(keybuf, key); |
| 361 |
break; |
| 362 |
default: |
| 363 |
fatalError("Direct search key must hold one, two, or four bytes."); |
| 364 |
} |
| 365 |
} |
| 366 |
} |