Changeset 2263 for trunk/src/lib/kDep.c
- Timestamp:
- Jan 23, 2009, 1:22:47 AM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/lib/kDep.c
r2243 r2263 35 35 #include <sys/stat.h> 36 36 #include "k/kDefs.h" 37 #include "k/kTypes.h" 37 38 #if K_OS == K_OS_WINDOWS 38 # include <windows.h> 39 # define USE_WIN_MMAP 40 # include <io.h> 41 # include <Windows.h> 39 42 extern void nt_fullpath(const char *pszPath, char *pszFull, size_t cchFull); /* nt_fullpath.c */ 40 43 #else … … 294 297 this is one of the algorithms used in berkeley db (see sleepycat) and 295 298 elsewhere. */ 296 static unsigned sdbm(const char *str )299 static unsigned sdbm(const char *str, size_t size) 297 300 { 298 301 unsigned hash = 0; 299 302 int c; 300 303 301 while ( (c = *(unsigned const char *)str++))304 while (size-- > 0 && (c = *(unsigned const char *)str++)) 302 305 hash = c + (hash << 6) + (hash << 16) - hash; 303 306 … … 310 313 * 311 314 * @returns Pointer to the allocated dependency. 312 * @param pszFilename The filename. 315 * @param pszFilename The filename. Does not need to be terminated. 313 316 * @param cchFilename The length of the filename. 314 317 */ 315 318 PDEP depAdd(const char *pszFilename, size_t cchFilename) 316 319 { 317 unsigned uHash = sdbm(pszFilename);318 PDEP pDep;319 PDEP pDepPrev;320 unsigned uHash = sdbm(pszFilename, cchFilename); 321 PDEP pDep; 322 PDEP pDepPrev; 320 323 321 324 /* … … 341 344 342 345 pDep->cchFilename = cchFilename; 343 memcpy(pDep->szFilename, pszFilename, cchFilename + 1); 346 memcpy(pDep->szFilename, pszFilename, cchFilename); 347 pDep->szFilename[cchFilename] = '\0'; 344 348 pDep->uHash = uHash; 345 349 … … 373 377 } 374 378 379 380 /** 381 * Performs a hexdump. 382 */ 383 void depHexDump(const KU8 *pb, size_t cb, size_t offBase) 384 { 385 const unsigned cchWidth = 16; 386 size_t off = 0; 387 while (off < cb) 388 { 389 unsigned i; 390 printf("%s%0*x %04x:", off ? "\n" : "", sizeof(pb) * 2, offBase + off, off); 391 for (i = 0; i < cchWidth && off + i < cb ; i++) 392 printf(off + i < cb ? !(i & 7) && i ? "-%02x" : " %02x" : " ", pb[i]); 393 394 while (i++ < cchWidth) 395 printf(" "); 396 printf(" "); 397 398 for (i = 0; i < cchWidth && off + i < cb; i++) 399 { 400 const KU8 u8 = pb[i]; 401 printf("%c", u8 < 127 && u8 >= 32 ? u8 : '.', 1); 402 } 403 off += cchWidth; 404 pb += cchWidth; 405 } 406 printf("\n"); 407 } 408 409 410 /** 411 * Reads the file specified by the pInput file stream into memory. 412 * 413 * @returns The address of the memory mapping on success. This must be 414 * freed by calling depFreeFileMemory. 415 * 416 * @param pInput The file stream to load or map into memory. 417 * @param pcbFile Where to return the mapping (file) size. 418 * @param ppvOpaque Opaque data when mapping, otherwise NULL. 419 */ 420 void *depReadFileIntoMemory(FILE *pInput, size_t *pcbFile, void **ppvOpaque) 421 { 422 void *pvFile; 423 long cbFile; 424 425 /* 426 * Figure out file size. 427 */ 428 #if defined(_MSC_VER) 429 cbFile = _filelength(fileno(pInput)); 430 if (cbFile < 0) 431 #else 432 if ( fseek(pInput, 0, SEEK_END) < 0 433 || (cbFile = ftell(pInput)) < 0 434 || fseek(pInput, 0, SEEK_SET)) 435 #endif 436 { 437 fprintf(stderr, "kDep: error: Failed to determin file size.\n"); 438 return NULL; 439 } 440 if (pcbFile) 441 *pcbFile = cbFile; 442 443 /* 444 * Try mmap first. 445 */ 446 #ifdef USE_WIN_MMAP 447 { 448 HANDLE hMapObj = CreateFileMapping((HANDLE)_get_osfhandle(fileno(pInput)), 449 NULL, PAGE_READONLY, 0, cbFile, NULL); 450 if (hMapObj != NULL) 451 { 452 pvFile = MapViewOfFile(hMapObj, FILE_MAP_READ, 0, 0, cbFile); 453 if (pvFile) 454 { 455 *ppvOpaque = hMapObj; 456 return pvFile; 457 } 458 fprintf(stderr, "kDep: warning: MapViewOfFile failed, %d.\n", GetLastError()); 459 CloseHandle(hMapObj); 460 } 461 else 462 fprintf(stderr, "kDep: warning: CreateFileMapping failed, %d.\n", GetLastError()); 463 } 464 465 #endif 466 467 /* 468 * Allocate memory and read the file. 469 */ 470 pvFile = malloc(cbFile + 1); 471 if (pvFile) 472 { 473 if (fread(pvFile, cbFile, 1, pInput)) 474 { 475 ((KU8 *)pvFile)[cbFile] = '\0'; 476 *ppvOpaque = NULL; 477 return pvFile; 478 } 479 fprintf(stderr, "kDep: error: Failed to read %ld bytes.\n", cbFile); 480 free(pvFile); 481 } 482 else 483 fprintf(stderr, "kDep: error: Failed to allocate %ld bytes (file mapping).\n", cbFile); 484 return NULL; 485 } 486 487 488 /** 489 * Free resources allocated by depReadFileIntoMemory. 490 * 491 * @param pvFile The address of the memory mapping. 492 * @param pvOpaque The opaque value returned together with the mapping. 493 */ 494 void depFreeFileMemory(void *pvFile, void *pvOpaque) 495 { 496 #if defined(USE_WIN_MMAP) 497 if (pvOpaque) 498 { 499 UnmapViewOfFile(pvFile); 500 CloseHandle(pvOpaque); 501 return; 502 } 503 #endif 504 free(pvFile); 505 } 506
Note:
See TracChangeset
for help on using the changeset viewer.