- Timestamp:
- Jan 23, 2009, 1:22:47 AM (16 years ago)
- Location:
- trunk/src
- Files:
-
- 1 added
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/kmk/Makefile.am
r2249 r2263 69 69 kmkbuiltin/install.c \ 70 70 kmkbuiltin/kDepIDB.c \ 71 kmkbuiltin/kDepObj.c \ 71 72 kmkbuiltin/ln.c \ 72 73 kmkbuiltin/md5sum.c \ -
trunk/src/kmk/Makefile.kmk
r2243 r2263 240 240 kmkbuiltin/install.c \ 241 241 kmkbuiltin/kDepIDB.c \ 242 kmkbuiltin/kDepObj.c \ 242 243 kmkbuiltin/md5sum.c \ 243 244 kmkbuiltin/mkdir.c \ … … 281 282 kmk_test \ 282 283 kDepIDB \ 284 kDepObj \ 283 285 284 286 kmk_append_TEMPLATE = BIN-KMK … … 383 385 kDepIDB_SOURCES = \ 384 386 kmkbuiltin/kDepIDB.c 387 388 kDepObj_TEMPLATE = BIN-KMK 389 kDepObj_DEFS = kmk_builtin_kDepObj=main 390 kDepObj_INCS = . 391 kDepObj_LIBS = $(LIB_KDEP) 392 kDepObj_SOURCES = \ 393 kmkbuiltin/kDepObj.c 385 394 386 395 -
trunk/src/kmk/kmkbuiltin.c
r2243 r2263 211 211 rc = kmk_builtin_test(argc, argv, environ, ppapszArgvToSpawn); 212 212 /* rarely used commands: */ 213 else if (!strcmp(pszCmd, "kDepObj")) 214 rc = kmk_builtin_kDepObj(argc, argv, environ); 213 215 else if (!strcmp(pszCmd, "chmod")) 214 216 rc = kmk_builtin_chmod(argc, argv, environ); -
trunk/src/kmk/kmkbuiltin.h
r2243 r2263 53 53 extern int kmk_builtin_test(int argc, char **argv, char **envp, char ***ppapszArgvSpawn); 54 54 extern int kmk_builtin_kDepIDB(int argc, char **argv, char **envp); 55 extern int kmk_builtin_kDepObj(int argc, char **argv, char **envp); 55 56 56 57 extern char *kmk_builtin_func_printf(char *o, char **argv, const char *funcname); -
trunk/src/kmk/kmkbuiltin/kDepIDB.c
r2243 r2263 38 38 #endif 39 39 #if !defined(_MSC_VER) 40 # include <stdint.h>41 40 # include <unistd.h> 42 41 #else 43 # define USE_WIN_MMAP44 42 # include <io.h> 45 # include <Windows.h>46 typedef unsigned char uint8_t;47 typedef unsigned short uint16_t;48 typedef unsigned int uint32_t;49 43 #endif 50 /*#include "kDep.h"*/ 44 #include "../../lib/k/kDefs.h" 45 #include "../../lib/k/kTypes.h" 51 46 #include "../../lib/kDep.h" 52 47 #include "kmkbuiltin.h" 53 48 54 #define OFFSETOF(type, member) ( (int)(size_t)(void *)&( ((type *)(void *)0)->member) ) 55 49 50 /******************************************************************************* 51 * Defined Constants And Macros * 52 *******************************************************************************/ 56 53 /*#define DEBUG*/ 57 54 #ifdef DEBUG 58 55 # define dprintf(a) printf a 59 # define dump(pb, cb, offBase) hexdump(pb,cb,offBase)56 # define dump(pb, cb, offBase) depHexDump(pb,cb,offBase) 60 57 #else 61 58 # define dprintf(a) do {} while (0) … … 70 67 static const char *argv0 = ""; 71 68 72 #ifdef DEBUG73 /**74 * Performs a hexdump.75 */76 static void hexdump(const uint8_t *pb, size_t cb, size_t offBase)77 {78 static const char szHex[16] = "0123456789abcdef";79 80 const unsigned cchWidth = 16;81 size_t off = 0;82 while (off < cb)83 {84 unsigned i;85 printf("%s%0*x %04x:", off ? "\n" : "", sizeof(pb) * 2, offBase + off, off);86 for (i = 0; i < cchWidth && off + i < cb ; i++)87 printf(off + i < cb ? !(i & 7) && i ? "-%02x" : " %02x" : " ", pb[i]);88 89 while (i++ < cchWidth)90 printf(" ");91 printf(" ");92 93 for (i = 0; i < cchWidth && off + i < cb; i++)94 {95 const uint8_t u8 = pb[i];96 printf("%c", u8 < 127 && u8 >= 32 ? u8 : '.', 1);97 }98 off += cchWidth;99 pb += cchWidth;100 }101 printf("\n");102 }103 #endif104 69 105 70 /** … … 113 78 * @param cchPrefix The size of the prefix. 114 79 */ 115 static int ScanStream( uint8_t*pbStream, size_t cbStream, const char *pszPrefix, size_t cchPrefix)116 { 117 const uint8_t*pbCur = pbStream;80 static int ScanStream(KU8 *pbStream, size_t cbStream, const char *pszPrefix, size_t cchPrefix) 81 { 82 const KU8 *pbCur = pbStream; 118 83 size_t cbLeft = cbStream; 119 84 register char chFirst = *pszPrefix; … … 140 105 141 106 return 0; 142 }143 144 145 #ifdef USE_WIN_MMAP146 /** Handle to the current file mapping object. */147 static HANDLE g_hMapObj = NULL;148 #endif149 150 151 /**152 * Reads the file specified by the pInput file stream into memory.153 * The size of the file is returned in *pcbFile if specified.154 * The returned pointer should be freed by FreeFileMemory().155 */156 void *ReadFileIntoMemory(FILE *pInput, size_t *pcbFile)157 {158 void *pvFile;159 long cbFile;160 161 /*162 * Figure out file size.163 */164 #if defined(_MSC_VER)165 cbFile = _filelength(fileno(pInput));166 if (cbFile < 0)167 #else168 if ( fseek(pInput, 0, SEEK_END) < 0169 || (cbFile = ftell(pInput)) < 0170 || fseek(pInput, 0, SEEK_SET))171 #endif172 {173 fprintf(stderr, "%s: error: Failed to determin file size.\n", argv0);174 return NULL;175 }176 if (pcbFile)177 *pcbFile = cbFile;178 179 /*180 * Try mmap first.181 */182 #ifdef USE_WIN_MMAP183 {184 HANDLE hMapObj = CreateFileMapping((HANDLE)_get_osfhandle(fileno(pInput)),185 NULL, PAGE_READONLY, 0, cbFile, NULL);186 if (hMapObj != NULL)187 {188 pvFile = MapViewOfFile(hMapObj, FILE_MAP_READ, 0, 0, cbFile);189 if (pvFile)190 {191 g_hMapObj = hMapObj;192 return pvFile;193 }194 fprintf(stderr, "%s: warning: MapViewOfFile failed, %d.\n", argv0, GetLastError());195 CloseHandle(hMapObj);196 }197 else198 fprintf(stderr, "%s: warning: CreateFileMapping failed, %d.\n", argv0, GetLastError());199 }200 201 #endif202 203 /*204 * Allocate memory and read the file.205 */206 pvFile = malloc(cbFile + 1);207 if (pvFile)208 {209 if (fread(pvFile, cbFile, 1, pInput))210 {211 ((uint8_t *)pvFile)[cbFile] = '\0';212 return pvFile;213 }214 fprintf(stderr, "%s: error: Failed to read %ld bytes.\n", argv0, cbFile);215 free(pvFile);216 }217 else218 fprintf(stderr, "%s: error: Failed to allocate %ld bytes (file mapping).\n", argv0, cbFile);219 return NULL;220 }221 222 223 static void FreeFileMemory(void *pvFile)224 {225 #if defined(USE_WIN_MMAP)226 if (g_hMapObj)227 {228 UnmapViewOfFile(pvFile);229 CloseHandle(g_hMapObj);230 return;231 }232 #endif233 free(pvFile);234 107 } 235 108 … … 244 117 245 118 /** A PDB 7.0 Page number. */ 246 typedef uint32_tPDB70PAGE;119 typedef KU32 PDB70PAGE; 247 120 /** Pointer to a PDB 7.0 Page number. */ 248 121 typedef PDB70PAGE *PPDB70PAGE; … … 254 127 { 255 128 /** The size of the stream. */ 256 uint32_tcbStream;129 KU32 cbStream; 257 130 } PDB70STREAM, *PPDB70STREAM; 258 131 … … 266 139 { 267 140 /** The signature string. */ 268 uint8_tszSignature[sizeof(PDB_SIGNATURE_700)];141 KU8 szSignature[sizeof(PDB_SIGNATURE_700)]; 269 142 /** The page size. */ 270 uint32_tcbPage;143 KU32 cbPage; 271 144 /** The start page. */ 272 145 PDB70PAGE iStartPage; … … 274 147 PDB70PAGE cPages; 275 148 /** The root stream directory. */ 276 uint32_tcbRoot;149 KU32 cbRoot; 277 150 /** Unknown function, always 0. */ 278 uint32_tu32Reserved;151 KU32 u32Reserved; 279 152 /** The page index of the root page table. */ 280 153 PDB70PAGE iRootPages; … … 287 160 { 288 161 /** The number of streams */ 289 uint32_tcStreams;162 KU32 cStreams; 290 163 /** Array of streams. */ 291 164 PDB70STREAM aStreams[1]; 292 /* uint32_taiPages[] */165 /* KU32 aiPages[] */ 293 166 } PDB70ROOT, *PPDB70ROOT; 294 167 … … 299 172 { 300 173 /** The structure version. */ 301 uint32_tVersion;174 KU32 Version; 302 175 /** Timestamp. */ 303 uint32_tTimeStamp;176 KU32 TimeStamp; 304 177 /** Unknown. */ 305 uint32_tUnknown1;178 KU32 Unknown1; 306 179 /** GUID. */ 307 uint32_tu32Guid[4];180 KU32 u32Guid[4]; 308 181 /** The size of the following name table. */ 309 uint32_tcbNames;182 KU32 cbNames; 310 183 /** The name table. */ 311 184 char szzNames[1]; … … 341 214 static size_t Pdb70Align(PPDB70HDR pHdr, size_t cb) 342 215 { 343 if (cb == ~( uint32_t)0 || !cb)216 if (cb == ~(KU32)0 || !cb) 344 217 return 0; 345 218 return ((cb + pHdr->cbPage - 1) / pHdr->cbPage) * pHdr->cbPage; … … 349 222 static size_t Pdb70Pages(PPDB70HDR pHdr, size_t cb) 350 223 { 351 if (cb == ~( uint32_t)0 || !cb)224 if (cb == ~(KU32)0 || !cb) 352 225 return 0; 353 226 return (cb + pHdr->cbPage - 1) / pHdr->cbPage; … … 358 231 const size_t cbPage = pHdr->cbPage; 359 232 size_t cPages = Pdb70Pages(pHdr, cb); 360 uint8_t*pbBuf = malloc(cPages * cbPage + 1);233 KU8 *pbBuf = malloc(cPages * cbPage + 1); 361 234 if (pbBuf) 362 235 { … … 368 241 { 369 242 off *= cbPage; 370 memcpy(pbBuf + iPage * cbPage, ( uint8_t*)pHdr + off, cbPage);243 memcpy(pbBuf + iPage * cbPage, (KU8 *)pHdr + off, cbPage); 371 244 dump(pbBuf + iPage * cbPage, iPage + 1 < cPages ? cbPage : cb % cbPage, off); 372 245 } … … 393 266 * (Todo: Check if we can just use the stream #0 size..) 394 267 */ 395 PPDB70PAGE piPageMap = ( uint32_t *)((uint8_t*)pHdr + pHdr->iRootPages * pHdr->cbPage);268 PPDB70PAGE piPageMap = (KU32 *)((KU8 *)pHdr + pHdr->iRootPages * pHdr->cbPage); 396 269 PPDB70ROOT pRoot = Pdb70AllocAndRead(pHdr, pHdr->cbRoot, piPageMap); 397 270 if (pRoot) … … 400 273 /* This stuff is probably unnecessary: */ 401 274 /* size = stream header + array of stream. */ 402 size_t cb = OFFSETOF(PDB70ROOT, aStreams[pRoot->cStreams]);275 size_t cb = K_OFFSETOF(PDB70ROOT, aStreams[pRoot->cStreams]); 403 276 free(pRoot); 404 277 pRoot = Pdb70AllocAndRead(pHdr, cb, piPageMap); … … 408 281 unsigned iStream = pRoot->cStreams; 409 282 while (iStream-- > 0) 410 if (pRoot->aStreams[iStream].cbStream != ~( uint32_t)0)283 if (pRoot->aStreams[iStream].cbStream != ~(KU32)0) 411 284 cb += Pdb70Pages(pHdr, pRoot->aStreams[iStream].cbStream) * sizeof(PDB70PAGE); 412 285 free(pRoot); … … 431 304 PPDB70PAGE paiPageMap; 432 305 if ( iStream >= pRoot->cStreams 433 || cbStream == ~( uint32_t)0)306 || cbStream == ~(KU32)0) 434 307 { 435 308 fprintf(stderr, "%s: error: Invalid stream %d\n", argv0, iStream); … … 439 312 paiPageMap = (PPDB70PAGE)&pRoot->aStreams[pRoot->cStreams]; 440 313 while (iStream-- > 0) 441 if (pRoot->aStreams[iStream].cbStream != ~( uint32_t)0)314 if (pRoot->aStreams[iStream].cbStream != ~(KU32)0) 442 315 paiPageMap += Pdb70Pages(pHdr, pRoot->aStreams[iStream].cbStream); 443 316 … … 447 320 } 448 321 449 static int Pdb70Process( uint8_t*pbFile, size_t cbFile)322 static int Pdb70Process(KU8 *pbFile, size_t cbFile) 450 323 { 451 324 PPDB70HDR pHdr = (PPDB70HDR)pbFile; … … 477 350 if ( pNames->Version == PDB70NAMES_VERSION 478 351 && pNames->cbNames > 32 479 && pNames->cbNames + offsetof(PDB70NAMES, szzNames) <= pRoot->aStreams[1].cbStream)352 && pNames->cbNames + K_OFFSETOF(PDB70NAMES, szzNames) <= pRoot->aStreams[1].cbStream) 480 353 { 481 354 /* … … 529 402 for (iStream = 0; iStream < pRoot->cStreams && !rc; iStream++) 530 403 { 531 uint8_t*pbStream;532 if ( pRoot->aStreams[iStream].cbStream == ~( uint32_t)0404 KU8 *pbStream; 405 if ( pRoot->aStreams[iStream].cbStream == ~(KU32)0 533 406 || !pRoot->aStreams[iStream].cbStream) 534 407 continue; 535 408 dprintf(("Stream #%d: %#x bytes (%#x aligned)\n", iStream, pRoot->aStreams[iStream].cbStream, 536 409 Pdb70Align(pHdr, pRoot->aStreams[iStream].cbStream))); 537 pbStream = ( uint8_t*)Pdb70AllocAndReadStream(pHdr, pRoot, iStream, &cbStream);410 pbStream = (KU8 *)Pdb70AllocAndReadStream(pHdr, pRoot, iStream, &cbStream); 538 411 if (pbStream) 539 412 { … … 562 435 563 436 /** A PDB 2.0 Page number. */ 564 typedef uint16_tPDB20PAGE;437 typedef KU16 PDB20PAGE; 565 438 /** Pointer to a PDB 2.0 Page number. */ 566 439 typedef PDB20PAGE *PPDB20PAGE; … … 572 445 { 573 446 /** The size of the stream. */ 574 uint32_tcbStream;447 KU32 cbStream; 575 448 /** Some unknown value. */ 576 uint32_tu32Unknown;449 KU32 u32Unknown; 577 450 } PDB20STREAM, *PPDB20STREAM; 578 451 … … 585 458 { 586 459 /** The signature string. */ 587 uint8_tszSignature[sizeof(PDB_SIGNATURE_200)];460 KU8 szSignature[sizeof(PDB_SIGNATURE_200)]; 588 461 /** The page size. */ 589 uint32_tcbPage;462 KU32 cbPage; 590 463 /** The start page - whatever that is... */ 591 464 PDB20PAGE iStartPage; … … 604 477 { 605 478 /** The number of streams */ 606 uint16_tcStreams;479 KU16 cStreams; 607 480 /** Reserved or high part of cStreams. */ 608 uint16_tu16Reserved;481 KU16 u16Reserved; 609 482 /** Array of streams. */ 610 483 PDB20STREAM aStreams[1]; … … 629 502 static size_t Pdb20Pages(PPDB20HDR pHdr, size_t cb) 630 503 { 631 if (cb == ~( uint32_t)0 || !cb)504 if (cb == ~(KU32)0 || !cb) 632 505 return 0; 633 506 return (cb + pHdr->cbPage - 1) / pHdr->cbPage; … … 637 510 { 638 511 size_t cPages = Pdb20Pages(pHdr, cb); 639 uint8_t*pbBuf = malloc(cPages * pHdr->cbPage + 1);512 KU8 *pbBuf = malloc(cPages * pHdr->cbPage + 1); 640 513 if (pbBuf) 641 514 { … … 645 518 size_t off = paiPageMap[iPage]; 646 519 off *= pHdr->cbPage; 647 memcpy(pbBuf + iPage * pHdr->cbPage, ( uint8_t*)pHdr + off, pHdr->cbPage);520 memcpy(pbBuf + iPage * pHdr->cbPage, (KU8 *)pHdr + off, pHdr->cbPage); 648 521 iPage++; 649 522 } … … 665 538 { 666 539 /* size = stream header + array of stream. */ 667 size_t cb = OFFSETOF(PDB20ROOT, aStreams[pRoot->cStreams]);540 size_t cb = K_OFFSETOF(PDB20ROOT, aStreams[pRoot->cStreams]); 668 541 free(pRoot); 669 542 pRoot = Pdb20AllocAndRead(pHdr, cb, &pHdr->aiRootPageMap[0]); … … 673 546 unsigned iStream = pRoot->cStreams; 674 547 while (iStream-- > 0) 675 if (pRoot->aStreams[iStream].cbStream != ~( uint32_t)0)548 if (pRoot->aStreams[iStream].cbStream != ~(KU32)0) 676 549 cb += Pdb20Pages(pHdr, pRoot->aStreams[iStream].cbStream) * sizeof(PDB20PAGE); 677 550 free(pRoot); … … 693 566 PPDB20PAGE paiPageMap; 694 567 if ( iStream >= pRoot->cStreams 695 || cbStream == ~( uint32_t)0)568 || cbStream == ~(KU32)0) 696 569 { 697 570 fprintf(stderr, "%s: error: Invalid stream %d\n", argv0, iStream); … … 701 574 paiPageMap = (PPDB20PAGE)&pRoot->aStreams[pRoot->cStreams]; 702 575 while (iStream-- > 0) 703 if (pRoot->aStreams[iStream].cbStream != ~( uint32_t)0)576 if (pRoot->aStreams[iStream].cbStream != ~(KU32)0) 704 577 paiPageMap += Pdb20Pages(pHdr, pRoot->aStreams[iStream].cbStream); 705 578 … … 709 582 } 710 583 711 static int Pdb20Process( uint8_t*pbFile, size_t cbFile)584 static int Pdb20Process(KU8 *pbFile, size_t cbFile) 712 585 { 713 586 PPDB20HDR pHdr = (PPDB20HDR)pbFile; … … 732 605 for (iStream = 0; iStream < pRoot->cStreams && !rc; iStream++) 733 606 { 734 uint8_t*pbStream;735 if (pRoot->aStreams[iStream].cbStream == ~( uint32_t)0)607 KU8 *pbStream; 608 if (pRoot->aStreams[iStream].cbStream == ~(KU32)0) 736 609 continue; 737 pbStream = ( uint8_t*)Pdb20AllocAndReadStream(pHdr, pRoot, iStream, NULL);610 pbStream = (KU8 *)Pdb20AllocAndReadStream(pHdr, pRoot, iStream, NULL); 738 611 if (pbStream) 739 612 { … … 756 629 { 757 630 size_t cbFile; 758 uint8_t *pbFile; 631 KU8 *pbFile; 632 void *pvOpaque; 759 633 int rc = 0; 760 634 … … 762 636 * Read the file into memory. 763 637 */ 764 pbFile = ( uint8_t *)ReadFileIntoMemory(pInput, &cbFile);638 pbFile = (KU8 *)depReadFileIntoMemory(pInput, &cbFile, &pvOpaque); 765 639 if (!pbFile) 766 640 return 1; … … 779 653 } 780 654 781 FreeFileMemory(pbFile);655 depFreeFileMemory(pbFile, pvOpaque); 782 656 return rc; 783 657 } -
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 -
trunk/src/lib/kDep.h
r2243 r2263 46 46 extern void depPrintStubs(FILE *pOutput); 47 47 extern void depCleanup(void); 48 extern void *depReadFileIntoMemory(FILE *pInput, size_t *pcbFile, void **ppvOpaque); 49 extern void depFreeFileMemory(void *pvFile, void *pvOpaque); 50 #ifdef ___k_kTypes_h___ 51 extern void depHexDump(const KU8 *pb, size_t cb, size_t offBase); 52 #endif 48 53 49 54 #endif
Note:
See TracChangeset
for help on using the changeset viewer.