Пример #1
0
int tr_general(uint8_t *src_data, uint16_t* offset, uint16_t* length, uint8_t* buffer, struct ipfix_lnf_map* item_info)
{
	switch(*length)
	{
	case 1:
		readui8(buffer) = readui8(src_data + *offset);
	break;
	case 2:
		readui16(buffer) = ntohs(readui16(src_data + *offset));
	break;
	case 4:
		readui32(buffer) = ntohl(readui32(src_data + *offset));
	break;
	case 8:
		readui64(buffer) = be64toh(readui64(src_data + *offset));
	break;
	case VAR_IE_LENGTH:
		*length = real_length(src_data, offset, *length);
	default:
		//Assume data type to be octetArray -> no endian conversion
		memcpy(buffer, src_data + *offset, *length);
	}
	if(/*length check comes here*/0){
		//invalid length
		return 1;
	}
	return 0;
}
Пример #2
0
static int ZIP_isArchive(const char *filename, int forWriting)
{
    PHYSFS_uint32 sig;
    int retval = 0;
    void *in;

    in = __PHYSFS_platformOpenRead(filename);
    BAIL_IF_MACRO(in == NULL, NULL, 0);

    /*
     * The first thing in a zip file might be the signature of the
     *  first local file record, so it makes for a quick determination.
     */
    if (readui32(in, &sig))
    {
        retval = (sig == ZIP_LOCAL_FILE_SIG);
        if (!retval)
        {
            /*
             * No sig...might be a ZIP with data at the start
             *  (a self-extracting executable, etc), so we'll have to do
             *  it the hard way...
             */
            retval = (zip_find_end_of_central_dir(in, NULL) != -1);
        } /* if */
    } /* if */

    __PHYSFS_platformClose(in);
    return(retval);
} /* ZIP_isArchive */
Пример #3
0
int tr_datetime(uint8_t *src_data, uint16_t* offset, uint16_t *length, uint8_t* buffer, struct ipfix_lnf_map* item_info)
{
	//lnf requires timestamp in milliseconds
	uint16_t ie = item_info->ie;
	switch(ie)
	{
		//FlowEnd/StartSysUpTime
	case 21: case 22:
			readui64(buffer) = ntohl(readui32(src_data + *offset));
	break;
		//iana ipfix flowStart/EndSeconds
	case 150: case 151:
			readui64(buffer) = be64toh(readui64(src_data + *offset))*1000;
	break;
		//flowStart/EndMilliseconds
	case 152: case 153:
			readui64(buffer) = be64toh(readui64(src_data + *offset));
	break;
		//flowStart/EndMicroseconds
	case 154: case 155:
			readui64(buffer) = be64toh(readui64(src_data + *offset))/1000;
	break;
		//flowStart/EndNanoseconds
	case 156: case 157:
			readui64(buffer) = be64toh(readui64(src_data + *offset))/1000000;
	break;
	default:
		return 1;
	}
	return 0;
	
}
Пример #4
0
int tr_address(uint8_t *src_data, uint16_t* offset, uint16_t *length, uint8_t* buffer, struct ipfix_lnf_map* item_info)
{
	switch(*length)
	{
	case 4: 
		memset(buffer, 0x0, sizeof(lnf_ip_t));
		((lnf_ip_t*)buffer)->data[3] = readui32(src_data + *offset);
	break;
	case 16:
		memcpy(buffer, src_data + *offset, 16);
	break;
	default:
		return 1;
	}
	return 0;
}
Пример #5
0
// !!! FIXME: this is sort of a big, ugly function.
const MOJOSHADER_effect *MOJOSHADER_parseEffect(const char *profile,
                                                const unsigned char *buf,
                                                const unsigned int _len,
                                                const MOJOSHADER_swizzle *swiz,
                                                const unsigned int swizcount,
                                                const MOJOSHADER_samplerMap *smap,
                                                const unsigned int smapcount,
                                                MOJOSHADER_malloc m,
                                                MOJOSHADER_free f,
                                                void *d)
{
    if ( ((m == NULL) && (f != NULL)) || ((m != NULL) && (f == NULL)) )
        return &MOJOSHADER_out_of_mem_effect;  // supply both or neither.

    if (m == NULL) m = MOJOSHADER_internal_malloc;
    if (f == NULL) f = MOJOSHADER_internal_free;

    MOJOSHADER_effect *retval = (MOJOSHADER_effect*)m(sizeof (MOJOSHADER_effect), d);
    if (retval == NULL)
        return &MOJOSHADER_out_of_mem_effect;  // supply both or neither.
    memset(retval, '\0', sizeof (*retval));

    retval->malloc = m;
    retval->free = f;
    retval->malloc_data = d;

    const uint8 *ptr = (const uint8 *) buf;
    uint32 len = (uint32) _len;
    size_t siz = 0;
    int i, j, k;

    if (len < 8)
        goto parseEffect_unexpectedEOF;

    const uint8 *base = NULL;
    if (readui32(&ptr, &len) != 0xFEFF0901) // !!! FIXME: is this always magic?
        goto parseEffect_notAnEffectsFile;
    else
    {
        const uint32 offset = readui32(&ptr, &len);
        base = ptr;
//printf("base offset == %u\n", offset);
        if (offset > len)
            goto parseEffect_unexpectedEOF;
        ptr += offset;
        len -= offset;
    } // else

    // params...

    if (len < 16)
        goto parseEffect_unexpectedEOF;

    const uint32 numparams = readui32(&ptr, &len);
    const uint32 numtechniques = readui32(&ptr, &len);

    readui32(&ptr, &len); // !!! FIXME: there are 8 unknown bytes here. Annotations?
    /*const uint32 numobjects = */ readui32(&ptr, &len);

    if (numparams > 0)
    {
        siz = sizeof (MOJOSHADER_effectParam) * numparams;
        retval->params = (MOJOSHADER_effectParam *) m(siz, d);
        if (retval->params == NULL)
            goto parseEffect_outOfMemory;
        memset(retval->params, '\0', siz);

        retval->param_count = numparams;

        for (i = 0; i < numparams; i++)
        {
            if (len < 16)
                goto parseEffect_unexpectedEOF;

            const uint32 typeoffset = readui32(&ptr, &len);
            /*const uint32 valoffset =*/ readui32(&ptr, &len);
            /*const uint32 flags =*/ readui32(&ptr, &len);
            const uint32 numannos = readui32(&ptr, &len);
            for (j = 0; j < numannos; j++)
            {
                if (len < 8)
                    goto parseEffect_unexpectedEOF;
                // !!! FIXME: parse annotations.
                readui32(&ptr, &len);
                readui32(&ptr, &len);
            } // for

            const uint8 *typeptr = base + typeoffset;
            unsigned int typelen = 9999999;  // !!! FIXME
            /*const uint32 paramtype =*/ readui32(&typeptr, &typelen);
            /*const uint32 paramclass =*/ readui32(&typeptr, &typelen);
            const uint32 paramname = readui32(&typeptr, &typelen);
            const uint32 paramsemantic = readui32(&typeptr, &typelen);

            // !!! FIXME: sanity checks!
            const char *namestr = ((const char *) base) + paramname;
            const char *semstr = ((const char *) base) + paramsemantic;
            uint32 len;
            char *strptr;
            len = *((const uint32 *) namestr);
            strptr = (char *) m(len + 1, d);
            memcpy(strptr, namestr + 4, len);
            strptr[len] = '\0';
            retval->params[i].name = strptr;
            len = *((const uint32 *) semstr);
            strptr = (char *) m(len + 1, d);
            memcpy(strptr, semstr + 4, len);
            strptr[len] = '\0';
            retval->params[i].semantic = strptr;
        } // for
    } // if

    uint32 numshaders = 0;  // we'll calculate this later.

    // techniques...

    if (numtechniques > 0)
    {
        siz = sizeof (MOJOSHADER_effectTechnique) * numtechniques;
        retval->techniques = (MOJOSHADER_effectTechnique *) m(siz, d);
        if (retval->techniques == NULL)
            goto parseEffect_outOfMemory;
        memset(retval->techniques, '\0', siz);

        retval->technique_count = numtechniques;

        for (i = 0; i < numtechniques; i++)
        {
            if (len < 12)
                goto parseEffect_unexpectedEOF;
            
            MOJOSHADER_effectTechnique *technique = &retval->techniques[i];

            const uint32 nameoffset = readui32(&ptr, &len);
            const uint32 numannos = readui32(&ptr, &len);
            const uint32 numpasses = readui32(&ptr, &len);

            if (nameoffset >= _len)
                goto parseEffect_unexpectedEOF;

            if (numannos > 0)
            {
                // !!! FIXME: expose these to the caller?
                for (j = 0; j < numannos; j++)
                {
                    if (len < 8)
                        goto parseEffect_unexpectedEOF;
                    readui32(&ptr, &len);  // typedef offset
                    readui32(&ptr, &len);  // value offset
                } // for
            } // if

            // !!! FIXME: verify this doesn't go past EOF looking for a null.
            {
                const char *namestr = ((char *) base) + nameoffset;
                uint32 len = *((const uint32 *) namestr);
                char *strptr = (char *) m(len + 1, d);
                memcpy(strptr, namestr + 4, len);
                strptr[len] = '\0';
                technique->name = strptr;
            }

            if (numpasses > 0)
            {
                technique->pass_count = numpasses;

                siz = sizeof (MOJOSHADER_effectPass) * numpasses;
                technique->passes = (MOJOSHADER_effectPass *) m(siz, d);
                if (technique->passes == NULL)
                    goto parseEffect_outOfMemory;
                memset(technique->passes, '\0', siz);

                for (j = 0; j < numpasses; j++)
                {
                    if (len < 12)
                        goto parseEffect_unexpectedEOF;

                    MOJOSHADER_effectPass *pass = &technique->passes[j];

                    const uint32 passnameoffset = readui32(&ptr, &len);
                    const uint32 numannos = readui32(&ptr, &len);
                    const uint32 numstates = readui32(&ptr, &len);

                    if (passnameoffset >= _len)
                        goto parseEffect_unexpectedEOF;

                    // !!! FIXME: verify this doesn't go past EOF looking for a null.
                    {
                        const char *namestr = ((char *) base) + passnameoffset;
                        uint32 len = *((const uint32 *) namestr);
                        char *strptr = (char *) m(len + 1, d);
                        memcpy(strptr, namestr + 4, len);
                        strptr[len] = '\0';
                        pass->name = strptr;
                    }

                    if (numannos > 0)
                    {
                        for (k = 0; k < numannos; k++)
                        {
                            if (len < 8)
                                goto parseEffect_unexpectedEOF;
                            // !!! FIXME: do something with this.
                            readui32(&ptr, &len);
                            readui32(&ptr, &len);
                        } // for
                    } // if

                    if (numstates > 0)
                    {
                        pass->state_count = numstates;

                        siz = sizeof (MOJOSHADER_effectState) * numstates;
                        pass->states = (MOJOSHADER_effectState *) m(siz, d);
                        if (pass->states == NULL)
                            goto parseEffect_outOfMemory;
                        memset(pass->states, '\0', siz);

                        for (k = 0; k < numstates; k++)
                        {
                            if (len < 16)
                                goto parseEffect_unexpectedEOF;

                            MOJOSHADER_effectState *state = &pass->states[k];
                            const uint32 type = readui32(&ptr, &len);
                            readui32(&ptr, &len);  // !!! FIXME: don't know what this field does.
                            /*const uint32 offsetend = */ readui32(&ptr, &len);
                            /*const uint32 offsetstart = */ readui32(&ptr, &len);
                            state->type = type;

                            if ((type == 0x92) || (type == 0x93))
                                numshaders++;
                        } // for
                    } // if
                } // for
            } // if
        } // for
    } // if

    // textures...

    if (len < 8)
        goto parseEffect_unexpectedEOF;

    const int numtextures = readui32(&ptr, &len);
    const int numobjects = readui32(&ptr, &len);  // !!! FIXME: "objects" for lack of a better word.

    if (numtextures > 0)
    {
        siz = sizeof (MOJOSHADER_effectTexture) * numtextures;
        retval->textures = (MOJOSHADER_effectTexture*)m(siz, d);
        if (retval->textures == NULL)
            goto parseEffect_outOfMemory;
        memset(retval->textures, '\0', siz);

        for (i = 0; i < numtextures; i++)
        {
            if (len < 8)
                goto parseEffect_unexpectedEOF;

            MOJOSHADER_effectTexture *texture = &retval->textures[i];
            const uint32 texparam = readui32(&ptr, &len);
            const uint32 texsize = readui32(&ptr, &len);
            // apparently texsize will pad out to 32 bits.
            const uint32 readsize = (((texsize + 3) / 4) * 4);
            if (len < readsize)
                goto parseEffect_unexpectedEOF;

            texture->param = texparam;
            char *str = (char*)m(texsize + 1, d);
            if (str == NULL)
                goto parseEffect_outOfMemory;
            memcpy(str, ptr, texsize);
            str[texsize] = '\0';
            texture->name = str;

            ptr += readsize;
            len -= readsize;
        } // for
    } // if

    // shaders...

    if (numshaders > 0)
    {
        siz = sizeof (MOJOSHADER_effectShader) * numshaders;
        retval->shaders = (MOJOSHADER_effectShader *) m(siz, d);
        if (retval->shaders == NULL)
            goto parseEffect_outOfMemory;
        memset(retval->shaders, '\0', siz);

        retval->shader_count = numshaders;

        // !!! FIXME: I wonder if we should pull these from offsets and not
        // !!! FIXME:  count on them all being in a line like this.
        for (i = 0; i < numshaders; i++)
        {
            if (len < 24)
                goto parseEffect_unexpectedEOF;

            MOJOSHADER_effectShader *shader = &retval->shaders[i];
            const uint32 technique = readui32(&ptr, &len);
            const uint32 pass = readui32(&ptr, &len);
            readui32(&ptr, &len);  // !!! FIXME: don't know what this does.
            readui32(&ptr, &len);  // !!! FIXME: don't know what this does (vertex/pixel/geometry?)
            readui32(&ptr, &len);  // !!! FIXME: don't know what this does.
            const uint32 shadersize = readui32(&ptr, &len);

            if (len < shadersize)
                goto parseEffect_unexpectedEOF;

            shader->technique = technique;
            shader->pass = pass;
            shader->shader = MOJOSHADER_parse(profile, ptr, shadersize,
                                              swiz, swizcount, smap, smapcount,
                                              m, f, d);

            // !!! FIXME: check for errors.

            ptr += shadersize;
            len -= shadersize;
        } // for
    } // if

    // !!! FIXME: we parse this, but don't expose the data, yet.
    // mappings ...
    assert(numshaders <= numobjects);
    const uint32 nummappings = numobjects - numshaders;
    if (nummappings > 0)
    {
        for (i = 0; i < nummappings; i++)
        {
            if (len < 24)
                goto parseEffect_unexpectedEOF;

            /*const uint32 magic = */ readui32(&ptr, &len);
            /*const uint32 index = */ readui32(&ptr, &len);
            readui32(&ptr, &len);  // !!! FIXME: what is this field?
            readui32(&ptr, &len);  // !!! FIXME: what is this field?
            /*const uint32 type = */ readui32(&ptr, &len);
            const uint32 mapsize = readui32(&ptr, &len);
            if (mapsize > 0)
            {
                const uint32 readsize = (((mapsize + 3) / 4) * 4);
                if (len < readsize)
                    goto parseEffect_unexpectedEOF;
            } // if
        } // for
    } // if

    retval->profile = (char *) m(strlen(profile) + 1, d);
    if (retval->profile == NULL)
        goto parseEffect_outOfMemory;
    strcpy((char *) retval->profile, profile);

    return retval;


// !!! FIXME: do something with this.
parseEffect_notAnEffectsFile:
parseEffect_unexpectedEOF:
parseEffect_outOfMemory:
    MOJOSHADER_freeEffect(retval);
    return &MOJOSHADER_out_of_mem_effect;
} // MOJOSHADER_parseEffect
Пример #6
0
// !!! FIXME: improve the crap out of this API later.
int isDDS(const void *_ptr, const unsigned long _len)
{
    size_t len = (size_t) _len;
    const uint8 *ptr = (const uint8 *) _ptr;
    return (readui32(ptr, len) == DDS_MAGIC);
} // isDDS
Пример #7
0
static int parse_dds(
	DDSHeader &header, const uint8 *&ptr, size_t &len,
	unsigned int &_glfmt, unsigned int &_miplevels)
{
    const uint32 pitchAndLinear = (DDSD_PITCH | DDSD_LINEARSIZE);
    uint32 width = 0;
    uint32 height = 0;
    uint32 calcSize = 0;
    uint32 calcSizeFlag = DDSD_LINEARSIZE;

    // Files start with magic value...
    if (readui32(ptr, len) != DDS_MAGIC)
        return 0;  // not a DDS file.

    // Then comes the DDS header...
    if (len < DDS_HEADERSIZE)
        return 0;

    header.dwSize = readui32(ptr, len);
    header.dwFlags = readui32(ptr, len);
    header.dwHeight = readui32(ptr, len);
    header.dwWidth = readui32(ptr, len);
    header.dwPitchOrLinearSize = readui32(ptr, len);
    header.dwDepth = readui32(ptr, len);
    header.dwMipMapCount = readui32(ptr, len);
    for (unsigned int i = 0; i < STATICARRAYLEN(header.dwReserved1); i++)
        header.dwReserved1[i] = readui32(ptr, len);
    header.ddspf.dwSize = readui32(ptr, len);
    header.ddspf.dwFlags = readui32(ptr, len);
    header.ddspf.dwFourCC = readui32(ptr, len);
    header.ddspf.dwRGBBitCount = readui32(ptr, len);
    header.ddspf.dwRBitMask = readui32(ptr, len);
    header.ddspf.dwGBitMask = readui32(ptr, len);
    header.ddspf.dwBBitMask = readui32(ptr, len);
    header.ddspf.dwABitMask = readui32(ptr, len);
    header.dwCaps = readui32(ptr, len);
    header.dwCaps2 = readui32(ptr, len);
    header.dwCaps3 = readui32(ptr, len);
    header.dwCaps4 = readui32(ptr, len);
    header.dwReserved2 = readui32(ptr, len);

    width = header.dwWidth;
    height = header.dwHeight;

    header.dwCaps &= ~DDSCAPS_ALPHA;  // we'll get this from the pixel format.

    if (header.dwSize != DDS_HEADERSIZE)   // header size must be 124.
        return 0;
    else if (header.ddspf.dwSize != DDS_PIXFMTSIZE)   // size must be 32.
        return 0;
    else if ((header.dwFlags & DDSD_REQ) != DDSD_REQ)  // must have these bits.
        return 0;
    else if ((header.dwCaps & DDSCAPS_TEXTURE) == 0)
        return 0;
    else if (header.dwCaps2 != 0)  // !!! FIXME (non-zero with other bits in dwCaps set)
        return 0;
    else if ((header.dwFlags & pitchAndLinear) == pitchAndLinear)
        return 0;  // can't specify both.

    _miplevels = (header.dwCaps & DDSCAPS_MIPMAP) ? header.dwMipMapCount : 1;

    if (header.ddspf.dwFlags & DDPF_FOURCC)
    {
        switch (header.ddspf.dwFourCC)
        {
            case FOURCC_DXT1:
                _glfmt = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
                calcSize = ((width ? ((width + 3) / 4) : 1) * 8) *
                           (height ? ((height + 3) / 4) : 1);
                break;
            case FOURCC_DXT3:
                _glfmt = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
                calcSize = ((width ? ((width + 3) / 4) : 1) * 16) *
                           (height ? ((height + 3) / 4) : 1);
                break;
            case FOURCC_DXT5:
                _glfmt = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
                calcSize = ((width ? ((width + 3) / 4) : 1) * 16) *
                           (height ? ((height + 3) / 4) : 1);
                break;

            // !!! FIXME: DX10 is an extended header, introduced by DirectX 10.
            //case FOURCC_DX10: do_something(); break;

            //case FOURCC_DXT2:  // premultiplied alpha unsupported.
            //case FOURCC_DXT4:  // premultiplied alpha unsupported.
            default:
                return 0;  // unsupported data format.
        } // switch
    } // if

    // no FourCC...uncompressed data.
    else if (header.ddspf.dwFlags & DDPF_RGB)
    {
        if ( (header.ddspf.dwRBitMask != 0x00FF0000) ||
             (header.ddspf.dwGBitMask != 0x0000FF00) ||
             (header.ddspf.dwBBitMask != 0x000000FF) )
            return 0;  // !!! FIXME: deal with this.

        if (header.ddspf.dwFlags & DDPF_ALPHAPIXELS)
        {
            if ( (header.ddspf.dwRGBBitCount != 32) ||
                 (header.ddspf.dwABitMask != 0xFF000000) )
                return 0;  // unsupported.
            _glfmt = GL_BGRA;
        } // if
        else
        {
            if (header.ddspf.dwRGBBitCount != 24)
                return 0;  // unsupported.
            _glfmt = GL_BGR;
        } // else

        calcSizeFlag = DDSD_PITCH;
        calcSize = ((width * header.ddspf.dwRGBBitCount) + 7) / 8;
    } // else if

    //else if (header.ddspf.dwFlags & DDPF_LUMINANCE)  // !!! FIXME
    //else if (header.ddspf.dwFlags & DDPF_YUV)  // !!! FIXME
    //else if (header.ddspf.dwFlags & DDPF_ALPHA)  // !!! FIXME
    else
    {
        return 0;  // unsupported data format.
    } // else if

    // no pitch or linear size? Calculate it.
    if ((header.dwFlags & pitchAndLinear) == 0)
    {
        if (!calcSizeFlag)
        {
            assert(0 && "should have caught this up above");
            return 0;  // uh oh.
        } // if

        header.dwPitchOrLinearSize = calcSize;
        header.dwFlags |= calcSizeFlag;
    } // if

    return 1;
} // parse_dds