Changeset 2263 for trunk/src/lib/kDep.c


Ignore:
Timestamp:
Jan 23, 2009, 1:22:47 AM (16 years ago)
Author:
bird
Message:

kDepObj: Initial code that deals with Watcom and MASM OMF files.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/lib/kDep.c

    r2243 r2263  
    3535#include <sys/stat.h>
    3636#include "k/kDefs.h"
     37#include "k/kTypes.h"
    3738#if K_OS == K_OS_WINDOWS
    38 # include <windows.h>
     39# define USE_WIN_MMAP
     40# include <io.h>
     41# include <Windows.h>
    3942 extern void nt_fullpath(const char *pszPath, char *pszFull, size_t cchFull); /* nt_fullpath.c */
    4043#else
     
    294297   this is one of the algorithms used in berkeley db (see sleepycat) and
    295298   elsewhere. */
    296 static unsigned sdbm(const char *str)
     299static unsigned sdbm(const char *str, size_t size)
    297300{
    298301    unsigned hash = 0;
    299302    int c;
    300303
    301     while ((c = *(unsigned const char *)str++))
     304    while (size-- > 0 && (c = *(unsigned const char *)str++))
    302305        hash = c + (hash << 6) + (hash << 16) - hash;
    303306
     
    310313 *
    311314 * @returns Pointer to the allocated dependency.
    312  * @param   pszFilename     The filename.
     315 * @param   pszFilename     The filename. Does not need to be terminated.
    313316 * @param   cchFilename     The length of the filename.
    314317 */
    315318PDEP depAdd(const char *pszFilename, size_t cchFilename)
    316319{
    317     unsigned uHash = sdbm(pszFilename);
    318     PDEP    pDep;
    319     PDEP    pDepPrev;
     320    unsigned    uHash = sdbm(pszFilename, cchFilename);
     321    PDEP        pDep;
     322    PDEP        pDepPrev;
    320323
    321324    /*
     
    341344
    342345    pDep->cchFilename = cchFilename;
    343     memcpy(pDep->szFilename, pszFilename, cchFilename + 1);
     346    memcpy(pDep->szFilename, pszFilename, cchFilename);
     347    pDep->szFilename[cchFilename] = '\0';
    344348    pDep->uHash = uHash;
    345349
     
    373377}
    374378
     379
     380/**
     381 * Performs a hexdump.
     382 */
     383void 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 */
     420void *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 */
     494void 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.