Exemple #1
0
void SG_vfile__reread(
	SG_context* pCtx,
	SG_vfile * pvf,
	SG_vhash** ppvh)
{
	SG_vhash* pvh = NULL;
	SG_uint64 len64;
	SG_uint32 len32;
	SG_byte* p = NULL;

	SG_ERR_CHECK(  SG_file__seek_end(pCtx, pvf->pFile, &len64)  );
	SG_ERR_CHECK(  SG_file__seek(pCtx, pvf->pFile, 0)  );
	
	// TODO "len64" is uint64 because we can have huge files, but
	// TODO our buffer is limited to uint32 (on 32bit systems).
	// TODO verify that len will fit in uint32.
	len32 = (SG_uint32)len64;
	if (len32 > 0)
	{
		SG_ERR_CHECK(  SG_alloc(pCtx, 1,len32+1,&p)  );
		SG_ERR_CHECK(  SG_file__read(pCtx, pvf->pFile, len32, p, NULL)  );

		p[len32] = 0;

		SG_ERR_CHECK(  SG_VHASH__ALLOC__FROM_JSON(pCtx, &pvh, (const char*) p)  );
		*ppvh = pvh;
	}

fail:
	SG_NULLFREE(pCtx, p);
}
static void sg_unzip__locate_central_dir(SG_context* pCtx, SG_file* pFile, SG_uint64* piPosition)
{
    unsigned char* buf = NULL;
    SG_uint64 uSizeFile;
    SG_uint32 uBackRead;
    SG_uint32 uMaxBack=0xffff; /* maximum size of global comment */
    SG_uint64 uPosFound=0;

    SG_ERR_CHECK(  SG_file__seek_end(pCtx, pFile, &uSizeFile)  );

    if (uMaxBack > uSizeFile)
    {
        uMaxBack = (SG_uint32) uSizeFile;
    }

	SG_ERR_CHECK(  SG_malloc(pCtx, BUFREADCOMMENT+4, &buf)  );

    uBackRead = 4;
    while (uBackRead<uMaxBack)
    {
        SG_uint32 uReadSize;
        SG_uint64 uReadPos;
        int i;

        if (uBackRead+BUFREADCOMMENT>uMaxBack)
        {
            uBackRead = uMaxBack;
        }
        else
        {
            uBackRead += BUFREADCOMMENT;
        }
        uReadPos = uSizeFile-uBackRead ;

        uReadSize = (SG_uint32) (((BUFREADCOMMENT+4) < (uSizeFile-uReadPos)) ?
                     (BUFREADCOMMENT+4) : (uSizeFile-uReadPos));
        SG_ERR_CHECK(  SG_file__seek(pCtx, pFile, uReadPos)  );

        SG_ERR_CHECK(  SG_file__read(pCtx, pFile, uReadSize, buf, NULL)  );

        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;
        }
    }

    *piPosition = uPosFound;

fail:
    SG_NULLFREE(pCtx, buf);
}