local uLong unzlocal_SearchCentralDir OF()

in util/src/minizip/unzip.c [340:402]


local uLong unzlocal_SearchCentralDir OF(
     (const zlib_filefunc_def * pzlib_filefunc_def,
      voidpf filestream));

local uLong
unzlocal_SearchCentralDir(
    const zlib_filefunc_def *pzlib_filefunc_def,
    voidpf filestream)
{
    unsigned char *buf;
    uLong uSizeFile;
    uLong uBackRead;
    uLong uMaxBack = 0xffff;    /* maximum size of global comment */
    uLong uPosFound = 0;

    if (ZSEEK(*pzlib_filefunc_def, filestream, 0, ZLIB_FILEFUNC_SEEK_END) != 0)
        return 0;

    uSizeFile = ZTELL(*pzlib_filefunc_def, filestream);

    if (uMaxBack > uSizeFile)
        uMaxBack = uSizeFile;

    buf = (unsigned char *) ALLOC(BUFREADCOMMENT + 4);
    if (buf == NULL)
        return 0;

    uBackRead = 4;
    while (uBackRead < uMaxBack)
    {
        uLong uReadSize,
         uReadPos;
        int i = 0;
        if (uBackRead + BUFREADCOMMENT > uMaxBack)
            uBackRead = uMaxBack;
        else
            uBackRead += BUFREADCOMMENT;
        uReadPos = uSizeFile - uBackRead;

        uReadSize = ((BUFREADCOMMENT + 4) < (uSizeFile - uReadPos)) ?
            (BUFREADCOMMENT + 4) : (uSizeFile - uReadPos);
        if (ZSEEK
            (*pzlib_filefunc_def, filestream, uReadPos,
             ZLIB_FILEFUNC_SEEK_SET) != 0)
            break;

        if (ZREAD(*pzlib_filefunc_def, filestream, buf, uReadSize) != uReadSize)
            break;

        for (i = (int) uReadSize - 3; (i--) > 0;)
            if (((*(buf + i)) == 0x50) && ((*(buf + i + 1)) == 0x4b) &&
                ((*(buf + i + 2)) == 0x05) && ((*(buf + i + 3)) == 0x06))
            {
                uPosFound = uReadPos + i;
                break;
            }

        if (uPosFound != 0)
            break;
    }
    TRYFREE(buf);
    return uPosFound;
}