Exemplo n.º 1
0
/****************************************************************
**
**  test_metadata(): Main meta-data encode/decode testing routine.
**
****************************************************************/
void
test_metadata(void)
{
    int16_t     ei16 = TEST_INT16_VALUE;    /* variables to hold the values to encode */
    uint16_t    eu16 = TEST_UINT16_VALUE;
    int32_t     ei32 = TEST_INT32_VALUE;
    uint32_t    eu32 = TEST_UINT32_VALUE;
    int16_t     di16;       /* variables to hold the decoded values */
    uint16_t    du16;
    int32_t     di32;
    uint32_t    du32;
    uint8_t	*p;  /* pointer into the buffer being en/de-coded */

    /* Output message about test being performed */
    MESSAGE(5, ("Testing Metadata Encoding/decoding\n"));

    /* Start by encoding the values above */
    p = encode_buffer;
    INT16ENCODE(p, ei16);       /* Encode the int16 value */
    UINT16ENCODE(p, eu16);      /* Encode the uint16 value */
    INT32ENCODE(p, ei32);       /* Encode the int32 value */
    UINT32ENCODE(p, eu32);      /* Encode the uint32 value */

    /* Check if we got what we asked for */
    if (HDmemcmp(encode_buffer, compar_buffer, sizeof(compar_buffer)) != 0) {
        unsigned                   u;      /* local counting variable */

        for (u = 0; u < sizeof(compar_buffer); u++) {
            if (compar_buffer[u] != encode_buffer[u])
                TestErrPrintf("Error encoding meta-data at offset %u, wanted: %u, got: %u\n", (unsigned) u, (unsigned) compar_buffer[u], (unsigned) encode_buffer[u]);
        }                       /* end for */
    }                           /* end if */
    /* Test decoding macros */
    p = encode_buffer;
    INT16DECODE(p, di16);       /* Decode the int16 value */
    UINT16DECODE(p, du16);      /* Decode the uint16 value */
    INT32DECODE(p, di32);       /* Decode the int32 value */
    UINT32DECODE(p, du32);      /* Decode the uint32 value */

    /* Check the values decoded */
    if (di16 != TEST_INT16_VALUE)
        TestErrPrintf("Error decoding int16 meta-data wanted: %d, got: %d "
                   "at %s:%d\n", (int) TEST_INT16_VALUE, (int) di16,
                   __FILE__, __LINE__);
    if (du16 != TEST_UINT16_VALUE)
        TestErrPrintf("Error decoding uint16 meta-data wanted: %u, got: %u "
                   "at %s:%d\n", (unsigned) TEST_UINT16_VALUE, (unsigned) du16,
                   __FILE__, __LINE__);
    if (di32 != TEST_INT32_VALUE)
        TestErrPrintf("Error decoding int32 meta-data wanted: %ld, got: %ld "
                   "at %s:%d\n", (long) TEST_INT32_VALUE, (long) di32,
                   __FILE__, __LINE__);
    if (du32 != TEST_UINT32_VALUE)
        TestErrPrintf("Error decoding uint32 meta-data wanted: %lu, got: %lu "
                   "at %s:%d\n", (unsigned long) TEST_UINT32_VALUE, (unsigned long) du32,
                   __FILE__, __LINE__);
}                               /* test_metadata() */
Exemplo n.º 2
0
/*-------------------------------------------------------------------------
 * Function:	H5O_fill_new_decode
 *
 * Purpose:	Decode a new fill value message.  The new fill value
 * 		message is fill value plus space allocation time and
 * 		fill value writing time and whether fill value is defined.
 *
 * Return:	Success:	Ptr to new message in native struct.
 *
 *		Failure:	NULL
 *
 * Programmer:  Raymond Lu
 *              Feb 26, 2002
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static void *
H5O_fill_new_decode(H5F_t UNUSED *f, hid_t UNUSED dxpl_id, const uint8_t *p)
{
    H5O_fill_new_t	*mesg=NULL;
    int			version;
    void		*ret_value;

    FUNC_ENTER_NOAPI_NOINIT(H5O_fill_new_decode);

    assert(f);
    assert(p);

    if (NULL==(mesg=H5FL_CALLOC(H5O_fill_new_t)))
	HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, NULL, "memory allocation failed for fill value message");

    /* Version */
    version = *p++;
    if( version != H5O_FILL_VERSION && version !=H5O_FILL_VERSION_2)
        HGOTO_ERROR(H5E_OHDR, H5E_CANTLOAD, NULL, "bad version number for fill value message");

    /* Space allocation time */
    mesg->alloc_time = (H5D_alloc_time_t)*p++;

    /* Fill value write time */
    mesg->fill_time = (H5D_fill_time_t)*p++;

    /* Whether fill value is defined */
    mesg->fill_defined = *p++;

    /* Only decode fill value information if one is defined */
    if(mesg->fill_defined) {
        INT32DECODE(p, mesg->size);
        if (mesg->size>0) {
            H5_CHECK_OVERFLOW(mesg->size,ssize_t,size_t);
            if (NULL==(mesg->buf=H5MM_malloc((size_t)mesg->size)))
                HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, NULL, "memory allocation failed for fill value");
            HDmemcpy(mesg->buf, p, (size_t)mesg->size);
        }
    } /* end if */
    else
        mesg->size=(-1);

    /* Set return value */
    ret_value = (void*)mesg;

done:
    if (!ret_value && mesg) {
        if(mesg->buf)
            H5MM_xfree(mesg->buf);
	H5FL_FREE(H5O_fill_new_t,mesg);
    }

    FUNC_LEAVE_NOAPI(ret_value);
}
Exemplo n.º 3
0
/*--------------------------------------------------------------------------
 NAME
    HCIcszip_decode -- Decode SZIP compressed data into a buffer.

 USAGE
    int32 HCIcszip_decode(info,length,buf)
    compinfo_t *info;   IN: the info about the compressed element
    int32 length;       IN: number of bytes to read into the buffer
    uint8 *buf;         OUT: buffer to store the bytes read

 RETURNS
    Returns SUCCEED or FAIL

 DESCRIPTION
    Common code called to decode SZIP data from the file.

 GLOBAL VARIABLES
 COMMENTS, BUGS, ASSUMPTIONS
 EXAMPLES
 REVISION LOG
--------------------------------------------------------------------------*/
PRIVATE int32
HCIcszip_decode(compinfo_t * info, int32 length, uint8 *buf)
{
    CONSTR(FUNC, "HCIcszip_decode");
#ifdef H4_HAVE_LIBSZ
    accrec_t *access_rec;
    comp_coder_szip_info_t *szip_info;    /* ptr to SZIP info */
    uint8 *in_buffer;
    uint8 *out_buffer;
    int32 in_length;
    int32 out_length;
    int bytes_per_pixel;
    int32 rbytes;
    uint16 tag,ref;
    int32 len1;
    int32 aid;
    int32 status;
    size_t size_out;
    uint8 *cp;
    int32 good_bytes;
    int32 old_way;
    SZ_com_t sz_param;
#endif

#ifdef H4_HAVE_LIBSZ

    szip_info = &(info->cinfo.coder_info.szip_info);
    if (szip_info->szip_state == SZIP_INIT) {
	/*  Load from disk, decode the data */

	if ((access_rec = HAatom_object(info->aid)) == NULL)    /* get the access_rec pointer */
	    HRETURN_ERROR(DFE_ARGS, FAIL);

	/* Discover how much data must be read */
	if(HTPinquire(access_rec->ddid,&tag,&ref,NULL,&in_length)==FAIL)
		HRETURN_ERROR(DFE_INTERNAL, FAIL);

        if (in_length == -1)
		HRETURN_ERROR(DFE_INTERNAL, FAIL);

        if (tag & 0x4000) {
	    /* this is linked list -- get the length of the data */
            aid = Hstartread(access_rec->file_id, tag, ref);
            if (HDinqblockinfo(aid, &len1, NULL, NULL, NULL) == FAIL) {
	       Hendaccess(aid);
	       HRETURN_ERROR(DFE_INTERNAL, FAIL);
            }
            in_length = len1; 
	    Hendaccess(aid);
        }

	old_way = (int)(szip_info->options_mask & SZ_H4_REV_2);
	if (old_way == 0) {
		/* special case: read data encoded in V4.2r0 */
		old_way = 1;
		good_bytes = in_length;
                in_length = in_length+5;
	        if ((in_buffer = (uint8 *) HDmalloc(in_length)) == NULL)
    	           HRETURN_ERROR(DFE_NOSPACE, FAIL);
		cp = in_buffer;
		*cp = 0;
		cp++;
		INT32ENCODE(cp, good_bytes);
	} else {
		/*  V4.2r1: in_length is correct */
		old_way = 0;
	        if ((in_buffer = (uint8 *) HDmalloc(in_length)) == NULL)
    	           HRETURN_ERROR(DFE_NOSPACE, FAIL);
	}

        /* Allocate memory for the uncompressed data */
	bytes_per_pixel = (szip_info->bits_per_pixel + 7) >> 3;
	if (bytes_per_pixel == 3)
		bytes_per_pixel++;

        out_length = szip_info->pixels * bytes_per_pixel;
	if ((out_buffer = (uint8 *) HDmalloc(out_length)) == NULL)
		HRETURN_ERROR(DFE_NOSPACE, FAIL);

	/* Read the unompressed data */
	if (old_way == 1) {
		/* this is encoded in V4.2r0 */
		/* the preamble isn't in the file, so read only the data */
		if ((rbytes = Hread(info->aid, in_length-5, in_buffer+5)) == FAIL)
		{
			HDfree(out_buffer);
			HDfree(in_buffer);
			HRETURN_ERROR(DFE_READERROR, FAIL);
		}
		if (rbytes == 0 || rbytes != (in_length - 5)) {
			/* is this possible? */
			HDfree(out_buffer);
			HDfree(in_buffer);
			HRETURN_ERROR(DFE_READERROR, FAIL);
		}
	} else {
		/* HDF4.2R1: read the data plus preamble */
		if ((rbytes = Hread(info->aid, in_length, in_buffer)) == FAIL)
		{
			HDfree(out_buffer);
			HDfree(in_buffer);
			HRETURN_ERROR(DFE_READERROR, FAIL);
		}
		if (rbytes == 0 || rbytes != in_length) {
			/* is this possible? */
			HDfree(out_buffer);
			HDfree(in_buffer);
			HRETURN_ERROR(DFE_READERROR, FAIL);
		}
	}
        cp = in_buffer;
        cp++;
        INT32DECODE(cp, good_bytes);
	if (in_buffer[0] == 1) {
           /* This byte means the data was not compressed -- just copy out */
	    szip_info->szip_state = SZIP_RUN;
	    HDmemcpy(out_buffer, in_buffer+5, good_bytes);
	    szip_info->buffer = out_buffer;
	    szip_info->buffer_pos = 0;
	    szip_info->buffer_size = good_bytes;
	    szip_info->offset = 0;
	    if (good_bytes > length) {
		/* partial read */
		HDmemcpy(buf, in_buffer+5, length);
	        szip_info->buffer_pos += length;
	        szip_info->buffer_size -= length;
	    } else {
		/* read the whole data block to the user buffer */
		HDmemcpy(buf, in_buffer+5, good_bytes);
	        szip_info->buffer_pos += good_bytes;
	        szip_info->buffer_size -= good_bytes;
	    }
	    szip_info->offset = szip_info->buffer_pos;
	    HDfree(in_buffer);
	    if (szip_info->buffer_size == 0) {
		if (szip_info->buffer != NULL) {
		   HDfree(szip_info->buffer);
		   szip_info->buffer = NULL;
		}
	    }
	    return (SUCCEED);
        }

	/* Decompress the data */
     
        /* set up the parameters */
	sz_param.options_mask = (szip_info->options_mask & ~SZ_H4_REV_2);
	sz_param.bits_per_pixel = szip_info->bits_per_pixel;
	sz_param.pixels_per_block = szip_info->pixels_per_block;
	sz_param.pixels_per_scanline = szip_info->pixels_per_scanline;
	size_out = out_length;
	if(SZ_OK!= (status = SZ_BufftoBuffDecompress(out_buffer, &size_out, (in_buffer+5), good_bytes, &sz_param)))
         {
		HDfree(out_buffer);
		HDfree(in_buffer);
		HRETURN_ERROR(DFE_CDECODE, FAIL);
        }

	if ((int32)size_out != out_length) {
	   /* This should never happen?? */
	   printf("status: %d ??bytes != out_length %d != %d\n",status,size_out,out_length);
	}

        /* The data is successfully decompressed. Put into the szip struct */
	 HDfree(in_buffer);
	 szip_info->szip_state = SZIP_RUN;
	 szip_info->buffer = out_buffer;
	 szip_info->buffer_pos = 0;
	 szip_info->buffer_size = out_length;
	 szip_info->offset = 0;
    }

   /* copy the data into the return buffer */
    if (length > szip_info->buffer_size)
    {	
        /*  can't happen?? panic?? */
	if (szip_info->buffer != NULL) {
            HDfree(szip_info->buffer);
	    szip_info->buffer = NULL;
        } 
        return (FAIL);
    }

    HDmemcpy(buf, szip_info->buffer + szip_info->buffer_pos, length);
    szip_info->buffer_pos += length;
    szip_info->buffer_size -= length;
    szip_info->offset = szip_info->buffer_pos;

    if (szip_info->buffer_size == 0) {
       if (szip_info->buffer != NULL) {
  	    HDfree(szip_info->buffer);
	    szip_info->buffer = NULL;
       }
    }

    return (SUCCEED);

#else /* ifdef H4_HAVE_LIBSZ */

    HRETURN_ERROR(DFE_CANTCOMP, FAIL);

#endif /* H4_HAVE_LIBSZ */

}   /* end HCIcszip_decode() */