Changeset 914 for trunk/src


Ignore:
Timestamp:
Sep 12, 1999, 11:51:03 PM (26 years ago)
Author:
davidr
Message:

Partially updated ReadTypelib.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/oleaut32/typelib.cpp

    r848 r914  
    1 /* $Id: typelib.cpp,v 1.3 1999-09-06 20:13:23 davidr Exp $ */
     1/* $Id: typelib.cpp,v 1.4 1999-09-12 21:51:03 davidr Exp $ */
    22/*
    33 *      TYPELIB
     
    933933}
    934934
    935 
     935// ----------------------------------------------------------------------
     936// TLB_FindMagic
     937// ----------------------------------------------------------------------
     938static  int     TLB_FindMagic(TLBContext *pcx, char * pMagic)
     939{
     940    long        lBlkSize = 1024;        // Block size
     941    long        lCurrentPos = 0;        // Current search pos in file.
     942    DWORD       lFileSize;              // Size of file to be searched.
     943    long        lDataSize;              // Bytes last read.
     944    long        lNoBlocks;              // Remaining blocks to scan.
     945    long        ll;
     946    int         fFound;                 // Found flag
     947    char *      pBuf;                   // Allow room for a trailing '\0'
     948    HANDLE      hHeap;                  // Heap Handle
     949    int         iLen = strlen(pMagic);  // Length of search string
     950
     951    // Get a search buffer...
     952    hHeap = GetProcessHeap();
     953    pBuf = (char *)HeapAlloc(hHeap, 0, lBlkSize * 2);
     954
     955    // Calc search params...
     956    lFileSize = GetFileSize(pcx->hFile, NULL);
     957    lNoBlocks = lFileSize / lBlkSize;
     958    fFound = FALSE;
     959
     960    // Read first block...
     961    lDataSize = TLB_Read(pBuf, lBlkSize, pcx, 0);
     962
     963    // Loop through blocks searching for target string...
     964    // Two blocks are stored to simplify boundary checking.
     965    // NB lNoBlocks is zero where the file fits into one block,
     966    //    so this is skipped
     967    while((lNoBlocks > 0) && !fFound)
     968    {
     969        lNoBlocks -= 1;
     970        lDataSize = TLB_Read(pBuf + lBlkSize, lBlkSize, pcx, DO_NOT_SEEK);
     971
     972        for (ll = 0; ll < lBlkSize; ll++)
     973        {
     974            if (strncmp(pBuf + ll, pMagic, iLen) == 0)
     975            {
     976                fFound = TRUE;
     977                break;
     978            }
     979            lCurrentPos += 1;   // Keep track of posn.
     980        }
     981        // Copy block2 to block1
     982        memcpy(pBuf, pBuf + lBlkSize, lDataSize);
     983    }
     984
     985    // Now search last block...
     986    if (!fFound)
     987    {
     988        for (ll = 0; ll < (lDataSize - iLen); ll++)
     989        {
     990            if (strncmp(pBuf + ll, pMagic, iLen) == 0)
     991            {
     992                fFound = TRUE;
     993                break;
     994            }
     995            lCurrentPos += 1;   // Keep track of posn.
     996        }
     997    }
     998
     999    // Loose buffer.
     1000    HeapFree(hHeap, 0, pBuf);
     1001
     1002    if (fFound)
     1003        return lCurrentPos;
     1004
     1005   return 0;
     1006}
     1007
     1008// ----------------------------------------------------------------------
     1009// TLB_FindTlb
     1010// ----------------------------------------------------------------------
    9361011long TLB_FindTlb(TLBContext *pcx)
    937 {/* FIXME: should parse the file properly
    938   * hack to find our tlb data
    939   */
    940 #define TLBBUFSZ 1024
    941     char buff[TLBBUFSZ+1]; /* room for a trailing '\0' */
    942     long ret=0,found=0;
    943     int count;
    944     char *pChr;
    945 
    946 #define LOOK_FOR_MAGIC(magic)                           \
    947     count=TLB_Read(buff, TLBBUFSZ, pcx, 0);             \
    948     do {                                                \
    949         buff[count]='\0';                               \
    950         pChr = buff;                                    \
    951         while (pChr) {                                  \
    952             pChr = (char *)memchr(pChr,magic[0],count-(pChr-buff));\
    953             if (pChr) {                                 \
    954                 if (!memcmp(pChr,magic,4)) {            \
    955                     ret+= pChr-buff;                    \
    956                     found = 1;                          \
    957                     break;                              \
    958                 }                                       \
    959                 pChr++;                                 \
    960             }                                           \
    961         }                                               \
    962         if (found) break;                               \
    963         ret+=count;                                     \
    964         count=TLB_Read(buff, TLBBUFSZ, pcx, DO_NOT_SEEK);\
    965     } while(count>0);
    966 
    967 
    968     LOOK_FOR_MAGIC(TLBMAGIC2);
    969     if(count)
    970         return ret;
    971 
    972     LOOK_FOR_MAGIC(TLBMAGIC1);
    973     if(count)
    974         ERR_(ole)("type library format not (yet) implemented\n");
    975     else
    976         ERR_(ole)("not type library found in this file\n");
     1012{
     1013    long        lPosn;
     1014
     1015    lPosn = TLB_FindMagic(pcx, TLBMAGIC2 );
     1016    if (lPosn != 0)
     1017    {
     1018        dprintf(("OLEAUT32: TLB_FindTlb - Located MAGIC2 @%lx", lPosn));
     1019        return lPosn;
     1020    }
     1021
     1022    lPosn = TLB_FindMagic(pcx, TLBMAGIC1 );
     1023    if (lPosn != 0)
     1024    {
     1025        dprintf(("OLEAUT32: TLB_FindTlb - Located MAGIC1 @%lx", lPosn));
     1026        dprintf(("OLEAUT32: ERROR UNSUPPORTED TYPELIB!"));
     1027        return -1;
     1028    }
     1029
     1030    dprintf(("OLEAUT32: TLB_FindTlb ERROR: NO TYPELIB FOUND"));
    9771031    return -1;
    9781032}
    979 #undef LOOK_FOR_MAGIC
    980 
     1033
     1034// ----------------------------------------------------------------------
     1035// TLB_ReadTypeLib
     1036// ----------------------------------------------------------------------
    9811037int TLB_ReadTypeLib(PCHAR file, ITypeLib **ppTypeLib)
    9821038{
    983     TLBContext cx;
    984     OFSTRUCT ofStruct;
    985     long oStart,lPSegDir;
    986     TLBLibInfo* pLibInfo=NULL;
    987     TLB2Header tlbHeader;
    988     TLBSegDir tlbSegDir;
    989     if((cx.hFile=OpenFile(file, &ofStruct, OF_READ))==HFILE_ERROR) {
    990         ERR_(typelib)("cannot open %s error 0x%lx\n",file, GetLastError());
     1039    TLBContext          cx;
     1040    OFSTRUCT            ofStruct;
     1041    long                oStart , lPSegDir;
     1042    TLBLibInfo *        pLibInfo = NULL;
     1043    TLB2Header          tlbHeader;
     1044    TLBSegDir           tlbSegDir;
     1045
     1046    if((cx.hFile = OpenFile(file, &ofStruct, OF_READ))==HFILE_ERROR)
     1047    {
     1048        ERR_(typelib)("cannot open %s error0x%lx\n",file, GetLastError());
    9911049        return E_FAIL;
    9921050    }
    9931051    /* get pointer to beginning of typelib data */
    994     if((oStart=TLB_FindTlb(&cx))<0){
    995         if(oStart==-1)
    996             ERR_(typelib)("cannot locate typelib in  %s\n",file);
    997         else
    998             ERR_(typelib)("unsupported typelib format in  %s\n",file);
    999         return E_FAIL;
    1000     }
    1001     cx.oStart=oStart;
     1052    oStart = TLB_FindTlb( & cx);
     1053    if(oStart < 0)
     1054    {
     1055        ERR_(typelib)("cannot locate typelib in  %s\n",file);
     1056        return E_FAIL;
     1057    }
     1058    cx.oStart = oStart;
    10021059    pLibInfo=(TLBLibInfo*)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(TLBLibInfo));
    10031060    if (!pLibInfo){
     
    10091066    cx.pLibInfo=pLibInfo;
    10101067    /* read header */
    1011     TLB_Read((void*)&tlbHeader, sizeof(tlbHeader), &cx, 0);
     1068    TLB_Read((void*)&tlbHeader, sizeof(tlbHeader), &cx, oStart);
    10121069    /* there is a small number of information here until the next important
    10131070     * part:
     
    10171074            (tlbHeader.varflags & HELPDLLFLAG? 4 :0);
    10181075    /* now read the segment directory */
    1019     TLB_Read((void*)&tlbSegDir, sizeof(tlbSegDir), &cx, lPSegDir); 
     1076    TLB_Read((void*)&tlbSegDir, sizeof(tlbSegDir), &cx, oStart + lPSegDir); 
    10201077    cx.pTblDir=&tlbSegDir;
    10211078    /* just check two entries */
Note: See TracChangeset for help on using the changeset viewer.