Example #1
0
/*-------------------------------------------------------------------------
 * Function:	test_noconv
 *
 * Purpose:	Tests creation of datasets when no conversion is present.
 *
 * Return:	Success:	0
 *
 *		Failure:	number of errors
 *
 * Programmer:	Robb Matzke
 *              Monday, January  4, 1999
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static int
test_noconv(hid_t file)
{
    hid_t	cwg=-1, type=-1, space=-1, dset=-1;
    c_e1	val;
    static c_e1	data1[]={E1_RED,   E1_GREEN, E1_BLUE,  E1_GREEN, E1_WHITE,
			 E1_WHITE, E1_BLACK, E1_GREEN, E1_BLUE,  E1_RED,
			 E1_RED,   E1_BLUE,  E1_GREEN, E1_BLACK, E1_WHITE,
			 E1_RED,   E1_WHITE, E1_GREEN, E1_GREEN, E1_BLUE};
    c_e1	data2[NELMTS(data1)];
    hsize_t	ds_size[1]={NELMTS(data1)};
    size_t	i;

    TESTING("no-conversion datasets");
    if ((cwg=H5Gcreate(file, "test_noconv", 0))<0) goto error;

    if ((type = H5Tcreate(H5T_ENUM, sizeof(c_e1)))<0) goto error;
    if (H5Tenum_insert(type, "RED",   CPTR(val, E1_RED  ))<0) goto error;
    if (H5Tenum_insert(type, "GREEN", CPTR(val, E1_GREEN))<0) goto error;
    if (H5Tenum_insert(type, "BLUE",  CPTR(val, E1_BLUE ))<0) goto error;
    if (H5Tenum_insert(type, "WHITE", CPTR(val, E1_WHITE))<0) goto error;
    if (H5Tenum_insert(type, "BLACK", CPTR(val, E1_BLACK))<0) goto error;

    if ((space=H5Screate_simple(1, ds_size, NULL))<0) goto error;
    if ((dset=H5Dcreate(cwg, "color_table", type, space, H5P_DEFAULT))<0)
	goto error;
    if (H5Dwrite(dset, type, space, space, H5P_DEFAULT, data1)<0) goto error;
    if (H5Dread(dset, type, space, space, H5P_DEFAULT, data2)<0) goto error;

    for (i=0; i<ds_size[0]; i++) {
	if (data1[i]!=data2[i]) {
	    H5_FAILED();
	    printf("    data1[%lu]=%d, data2[%lu]=%d (should be same)\n",
		   (unsigned long)i, (int)(data1[i]),
		   (unsigned long)i, (int)(data2[i]));
	    goto error;
	}
    }

    if (H5Dclose(dset)<0) goto error;
    if (H5Sclose(space)<0) goto error;
    if (H5Tclose(type)<0) goto error;
    if (H5Gclose(cwg)<0) goto error;
    PASSED();
    return 0;

 error:
    H5E_BEGIN_TRY {
	H5Dclose(dset);
	H5Sclose(space);
	H5Tclose(type);
	H5Gclose(cwg);
    } H5E_END_TRY;
    return 1;
}
Example #2
0
/*-------------------------------------------------------------------------
 * Function:	H5MF_alloc_open
 *
 * Purpose:	Open an existing free space manager of TYPE for file by
 *		creating a free-space structure
 *
 * Return:	Success:	non-negative
 *		Failure:	negative
 *
 * Programmer:	Quincey Koziol
 *		[email protected]
 *		Jan  8 2008
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5MF_alloc_open(H5F_t *f, hid_t dxpl_id, H5FD_mem_t type)
{
    const H5FS_section_class_t *classes[] = { /* Free space section classes implemented for file */
        H5MF_FSPACE_SECT_CLS_SIMPLE};
    herr_t ret_value = SUCCEED;         /* Return value */

    FUNC_ENTER_NOAPI_NOINIT

    /*
     * Check arguments.
     */
    HDassert(f);
    HDassert(f->shared);
    HDassert(type != H5FD_MEM_NOLIST);
    HDassert(H5F_addr_defined(f->shared->fs_addr[type]));
    HDassert(f->shared->fs_state[type] == H5F_FS_STATE_CLOSED);

    /* Open an existing free space structure for the file */
    if(NULL == (f->shared->fs_man[type] = H5FS_open(f, dxpl_id, f->shared->fs_addr[type],
	    NELMTS(classes), classes, f, f->shared->alignment, f->shared->threshold)))
	HGOTO_ERROR(H5E_RESOURCE, H5E_CANTINIT, FAIL, "can't initialize free space info")

    /* Set the state for the free space manager to "open", if it is now */
    if(f->shared->fs_man[type])
        f->shared->fs_state[type] = H5F_FS_STATE_OPEN;

done:
    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5MF_alloc_open() */
Example #3
0
/*-------------------------------------------------------------------------
 * Function:	H5HF_space_start
 *
 * Purpose:	"Start up" free space for heap - open existing free space
 *              structure if one exists, otherwise create a new free space
 *              structure
 *
 * Return:	Success:	non-negative
 *
 *		Failure:	negative
 *
 * Programmer:	Quincey Koziol
 *		[email protected]
 *		May  2 2006
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5HF_space_start(H5HF_hdr_t *hdr, hid_t dxpl_id, hbool_t may_create)
{
    const H5FS_section_class_t *classes[] = { /* Free space section classes implemented for fractal heap */
        H5HF_FSPACE_SECT_CLS_SINGLE,
        H5HF_FSPACE_SECT_CLS_FIRST_ROW,
        H5HF_FSPACE_SECT_CLS_NORMAL_ROW,
        H5HF_FSPACE_SECT_CLS_INDIRECT};
    herr_t ret_value = SUCCEED;         /* Return value */

    FUNC_ENTER_NOAPI_NOINIT(H5HF_space_start)

    /*
     * Check arguments.
     */
    HDassert(hdr);

    /* Check for creating free space info for the heap */
    if(H5F_addr_defined(hdr->fs_addr)) {
        /* Open an existing free space structure for the heap */
        if(NULL == (hdr->fspace = H5FS_open(hdr->f, dxpl_id, hdr->fs_addr,
                NELMTS(classes), classes, hdr)))
            HGOTO_ERROR(H5E_HEAP, H5E_CANTINIT, FAIL, "can't initialize free space info")
    } /* end if */
    else {
        /* Check if we are allowed to create the free space manager */
        if(may_create) {
Example #4
0
/** Encode Elf header disk structure */
void *
SgAsmElfFileHeader::encode(ByteOrder::Endianness sex, Elf32FileHeader_disk *disk) const
{
    ROSE_ASSERT(p_magic.size() == NELMTS(disk->e_ident_magic));
    for (size_t i=0; i<NELMTS(disk->e_ident_magic); i++)
        disk->e_ident_magic[i] = p_magic[i];
    ByteOrder::host_to_disk(sex, p_e_ident_file_class, &(disk->e_ident_file_class));
    ByteOrder::host_to_disk(sex, p_e_ident_data_encoding, &(disk->e_ident_data_encoding));
    ByteOrder::host_to_disk(sex, p_e_ident_file_version, &(disk->e_ident_file_version));
    ROSE_ASSERT(p_e_ident_padding.size() == NELMTS(disk->e_ident_padding));
    for (size_t i=0; i<NELMTS(disk->e_ident_padding); i++)
        disk->e_ident_padding[i] = p_e_ident_padding[i];
    ByteOrder::host_to_disk(sex, p_e_type, &(disk->e_type));
    ByteOrder::host_to_disk(sex, p_e_machine, &(disk->e_machine));
    ByteOrder::host_to_disk(sex, p_exec_format->get_version(), &(disk->e_version));
    ByteOrder::host_to_disk(sex, get_entry_rva(), &(disk->e_entry));
    if (get_segment_table()) {
        ByteOrder::host_to_disk(sex, get_segment_table()->get_offset(), &(disk->e_phoff));
    } else {
        ByteOrder::host_to_disk(sex, 0, &(disk->e_phoff));
    }
    if (get_section_table()) {
        ByteOrder::host_to_disk(sex, get_section_table()->get_offset(), &(disk->e_shoff));
    } else {
        ByteOrder::host_to_disk(sex, 0, &(disk->e_shoff));
    }
    ByteOrder::host_to_disk(sex, p_e_flags,               &(disk->e_flags));
    ByteOrder::host_to_disk(sex, p_e_ehsize,              &(disk->e_ehsize));

    if (p_e_phnum>0) {
        ByteOrder::host_to_disk(sex, p_phextrasz+sizeof(SgAsmElfSegmentTableEntry::Elf32SegmentTableEntry_disk),
                                &(disk->e_phentsize));
    } else {
        ByteOrder::host_to_disk(sex, 0, &(disk->e_phentsize));
    }
    if (p_e_shnum>0) {
        ByteOrder::host_to_disk(sex, p_shextrasz+sizeof(SgAsmElfSectionTableEntry::Elf32SectionTableEntry_disk),
                                &(disk->e_shentsize));
    } else {
        ByteOrder::host_to_disk(sex, 0, &(disk->e_shentsize));
    }
    ByteOrder::host_to_disk(sex, p_e_phnum,               &(disk->e_phnum));
    ByteOrder::host_to_disk(sex, p_e_shnum,               &(disk->e_shnum));
    ByteOrder::host_to_disk(sex, p_e_shstrndx,            &(disk->e_shstrndx));

    return disk;
}
Example #5
0
/*-----------------------------------------------------------------------------
 * Purpose:     Push a new input item onto the stack.
 *
 * Programmer:  Robb Matzke
 *              Monday, July 10, 2000
 *
 * Modifications:
 *-----------------------------------------------------------------------------
 */
void
lex_push(lex_t *f, lex_t *item)
{
    if (f->nstack+1>=NELMTS(f->stack)) {
        out_errorn("file inclusion nested too deeply");
    } else {
        f->stack[f->nstack++] = item;
    }
}
/*-------------------------------------------------------------------------
 * Function:	enough_room
 *
 * Purpose:	Tries to create a bunch of sparse files to see if quotas will
 *		get in the way.  Some systems also have problems opening
 *		enough files and we'll check that too.
 *
 * Return:	Success:	Non-zero
 *
 *		Failure:	zero
 *
 * Programmer:	Robb Matzke
 *              Thursday, August  6, 1998
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static int
enough_room(hid_t fapl)
{
    int		ret_value=0;
    int		fd[68];
    size_t	i, size = (size_t)1 << 30;
    char	filename[1024], name[1024];

    /* Initialize file descriptors */
    for (i=0; i<NELMTS(fd); i++) fd[i] = -1;

    /* Get file name template */
    HDassert(H5FD_FAMILY==H5Pget_driver(fapl));
    h5_fixname(FILENAME[0], fapl, filename, sizeof(filename));

    /* Create files */
    for (i=0; i<NELMTS(fd); i++) {
        HDsnprintf(name, sizeof(name), filename, i);
        if ((fd[i]=HDopen(name, O_RDWR|O_CREAT|O_TRUNC, 0666)) < 0) {
            goto done;
        }
        if ((off_t)size != HDlseek(fd[i], (off_t)size, SEEK_SET)) {
            goto done;
        }
        if (1!=HDwrite(fd[i], "X", (size_t)1)) {
            goto done;
        }
    }
    ret_value = 1;

done:
    for (i=0; i<NELMTS(fd) && fd[i]>=0; i++) {
        HDsnprintf(name, sizeof(name), filename, i);
        if(HDclose(fd[i]) < 0)
            ret_value=0;
        HDremove(name);
    }

    return ret_value;
}
Example #7
0
/*-------------------------------------------------------------------------
 * Function:    sym_eval
 *
 * Purpose:     Returns the variable value of a symbol if it has one.
 *
 * Return:      Success:        Ptr to a copy of the variable value.
 *
 *              Failure:        NIL
 *
 * Programmer:  Robb Matzke
 *              [email protected]
 *              Dec  4 1996
 *
 * Modifications:
 *      Robb Matzke, 4 Feb 1997
 *      Fixed the arguments for the obj_deref() call.
 *
 *      Robb Matzke, 2000-07-03
 *      The symbol `$*' evaluates to a list of all $N files for
 *      consecutive N beginning at 1.
 *-------------------------------------------------------------------------
 */
static obj_t
sym_eval (obj_t _self)
{
    obj_t       name_1=NIL, file_1=NIL, retval=NIL;
    obj_sym_t   *self = MYCLASS(_self);

    /* If the symbol has a variable value then return it. */    
    if (MYCLASS(_self)->sym->var) {
        return obj_copy (MYCLASS(_self)->sym->var, SHALLOW);
    }

    /* The symbol `$*' evaluates to a list of the first consecutive files
     * bound to the $N symbols. */
    if (!strcmp(self->sym->name, "$*")) {
        obj_t   opands[1024], retval;
        int     nopands, i;
        
        for (nopands=0; nopands<NELMTS(opands); nopands++) {
            obj_t symbol;
            char tmp[32];
            sprintf(tmp, "$%d", nopands+1);
            symbol = obj_new(C_SYM, tmp);
            opands[nopands] = sym_vboundp(symbol);
            obj_dest(symbol);
            if (!opands[nopands] || C_FILE!=opands[nopands]->pub.cls) {
                /* We reached the last file or something isn't a file */
                obj_dest(opands[nopands]);
                break;
            }
        }
        retval = V_make_list(nopands, opands);
        for (i=0; i<nopands; i++) {
            obj_dest(opands[i]);
        }
        return retval;
    }

    /* If the symbol exists in the first data file, then return
     * that SDO. */
    name_1 = obj_new (C_SYM, "$1");
    file_1 = MYCLASS(name_1)->sym->var;
    name_1 = obj_dest (name_1);

    if (file_1 && C_FILE==file_1->pub.cls) {
        retval = obj_deref (file_1, 1, &_self);
        return retval;
    }

    /* Symbol has no value. */    
    out_errorn ("eval: variable `%s' has no value", obj_name(_self));
    return NIL;
}
Example #8
0
/* Encode the PE header into disk format */
void *
SgAsmPEFileHeader::encode(PEFileHeader_disk *disk) const
{
    for (size_t i=0; i<NELMTS(disk->e_magic); i++)
        disk->e_magic[i] = get_magic()[i];
    host_to_le(p_e_cpu_type,           &(disk->e_cpu_type));
    host_to_le(p_e_nsections,          &(disk->e_nsections));
    host_to_le(p_e_time,               &(disk->e_time));
    host_to_le(p_e_coff_symtab,        &(disk->e_coff_symtab));
    host_to_le(p_e_coff_nsyms,         &(disk->e_coff_nsyms));
    host_to_le(p_e_nt_hdr_size,        &(disk->e_nt_hdr_size));
    host_to_le(p_e_flags,              &(disk->e_flags));

    return disk;
}
Example #9
0
int print_filters(hid_t dcpl_id)
{
 int          nfilters;       /* number of filters */
 unsigned     filt_flags;     /* filter flags */
 H5Z_filter_t filtn;          /* filter identification number */
 unsigned     cd_values[20];  /* filter client data values */
 size_t       cd_nelmts;      /* filter client number of values */
 size_t       cd_num;         /* filter client data counter */
 char         f_name[256];    /* filter name */
 char         s[64];          /* temporary string buffer */
 int          i;

 /* get information about filters */
 if ((nfilters = H5Pget_nfilters(dcpl_id))<0)
  return -1;

 for (i=0; i<nfilters; i++)
 {
  cd_nelmts = NELMTS(cd_values);

  filtn = H5Pget_filter(dcpl_id,
   (unsigned)i,
   &filt_flags,
   &cd_nelmts,
   cd_values,
   sizeof(f_name),
   f_name);


  f_name[sizeof(f_name)-1] = '\0';
  sprintf(s, "Filter-%d:", i);
  printf("    %-10s %s-%u %s {", s,
   f_name[0]?f_name:"method",
   (unsigned)filtn,
   filt_flags & H5Z_FLAG_OPTIONAL?"OPT":"");
  for (cd_num=0; cd_num<cd_nelmts; cd_num++) {
   printf("%s%u", cd_num?", ":"", cd_values[cd_num]);
  }
  printf("}\n");
 }

 return 0;


}
int has_filter(hid_t dcpl_id,
               H5Z_filter_t filtnin)
{

 int          nfilters;       /* number of filters */
 unsigned     filt_flags;     /* filter flags */
 H5Z_filter_t filtn;          /* filter identification number */
 unsigned     cd_values[20];  /* filter client data values */
 size_t       cd_nelmts;      /* filter client number of values */
 char         f_name[256];    /* filter name */
 int          have=0;         /* flag, filter is present */
 int          i;              /* index */

 /* if no information about the input filter is requested return exit */
 if (filtnin==-1)
  return 1;

 /* get information about filters */
 if ((nfilters = H5Pget_nfilters(dcpl_id))<0)
  return -1;

 /* if we do not have filters and the requested filter is NONE, return 1 */
 if (!nfilters && filtnin==H5Z_FILTER_NONE)
  return 1;

 for (i=0; i<nfilters; i++)
 {
  cd_nelmts = NELMTS(cd_values);

  filtn = H5Pget_filter(dcpl_id,
   (unsigned)i,
   &filt_flags,
   &cd_nelmts,
   cd_values,
   sizeof(f_name),
   f_name);


  if (filtnin==filtn)
   have=1;

 }

 return have;
}
int has_layout(hid_t dcpl_id,
               pack_info_t *obj)
{
 hsize_t      chsize[64];     /* chunk size in elements */
 H5D_layout_t layout;         /* layout */
 int          nfilters;       /* number of filters */
 int          rank;           /* rank */
 int          i;              /* index */

 /* if no information about the input layout is requested return exit */
 if (obj==NULL)
  return 1;

 /* check if we have filters in the input object */
 if ((nfilters = H5Pget_nfilters(dcpl_id))<0)
  return -1;

 /* a non chunked layout was requested on a filtered object; avoid the test */
 if (nfilters && obj->layout!=H5D_CHUNKED)
  return 1;

 /* get layout */
 if ((layout = H5Pget_layout(dcpl_id))<0)
  return -1;

 if (obj->layout != layout)
  return 0;

 if (layout==H5D_CHUNKED)
 {
  if ((rank = H5Pget_chunk(dcpl_id,NELMTS(chsize),chsize/*out*/))<0)
   return -1;
  if (obj->chunk.rank != rank)
   return 0;
  for ( i=0; i<rank; i++)
   if (chsize[i] != obj->chunk.chunk_lengths[i])
    return 0;
 }

 return 1;
}
Example #12
0
/*-------------------------------------------------------------------------
 * Function:	test_curve
 *
 * Purpose:	Tests reading and writing DBcurve objects.
 *
 * Return:	Success:	0
 *
 *		Failure:	number of errors
 *
 * Programmer:	Robb Matzke
 *              Tuesday, March 23, 1999
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static int
test_curve(DBfile *dbfile)
{
    int			nerrors = 0;
    static double	xvals_d[] = { 1.1,   2.2,   3.3,   4.4,   5.5 };
    static double	yvals_d[] = {10.01, 20.02, 30.03, 40.04, 50.05};
    
    puts("=== Curves ===");

    DBMkDir(dbfile, "/curves");
    DBSetDir(dbfile, "/curves");

    /* Write an initial curve along with its X and Y arrays */
    if (DBPutCurve(dbfile, "curve1", xvals_d, yvals_d, DB_DOUBLE,
		   NELMTS(xvals_d), NULL)<0) {
	puts("DBPutCurve(curve1) failed");
	nerrors++;
    }

    return nerrors;
}
Example #13
0
/*-------------------------------------------------------------------------
 * Function:	H5MF_alloc_create
 *
 * Purpose:	Create free space manager of TYPE for the file by creating
 *		a free-space structure
 *
 * Return:	Success:	non-negative
 *		Failure:	negative
 *
 * Programmer:	Quincey Koziol
 *		[email protected]
 *		Jan  8 2008
 *
 *-------------------------------------------------------------------------
 */
static herr_t
H5MF_alloc_create(H5F_t *f, hid_t dxpl_id, H5FD_mem_t type)
{
    const H5FS_section_class_t *classes[] = { /* Free space section classes implemented for file */
        H5MF_FSPACE_SECT_CLS_SIMPLE};
    herr_t ret_value = SUCCEED;         /* Return value */
    H5FS_create_t fs_create; 		/* Free space creation parameters */

    FUNC_ENTER_NOAPI_NOINIT

    /*
     * Check arguments.
     */
    HDassert(f);
    HDassert(f->shared);
    HDassert(type != H5FD_MEM_NOLIST);
    HDassert(!H5F_addr_defined(f->shared->fs_addr[type]));
    HDassert(f->shared->fs_state[type] == H5F_FS_STATE_CLOSED);

    /* Set the free space creation parameters */
    fs_create.client = H5FS_CLIENT_FILE_ID;
    fs_create.shrink_percent = H5MF_FSPACE_SHRINK;
    fs_create.expand_percent = H5MF_FSPACE_EXPAND;
    fs_create.max_sect_addr = 1 + H5VM_log2_gen((uint64_t)f->shared->maxaddr);
    fs_create.max_sect_size = f->shared->maxaddr;

    if(NULL == (f->shared->fs_man[type] = H5FS_create(f, dxpl_id, NULL,
	    &fs_create, NELMTS(classes), classes, f, f->shared->alignment, f->shared->threshold)))
	HGOTO_ERROR(H5E_RESOURCE, H5E_CANTINIT, FAIL, "can't initialize free space info")


    /* Set the state for the free space manager to "open", if it is now */
    if(f->shared->fs_man[type])
        f->shared->fs_state[type] = H5F_FS_STATE_OPEN;

done:
    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5MF_alloc_create() */
static
int verify_filters(hid_t pid, hid_t tid, int nfilters, filter_info_t *filter)
{
    int           nfilters_dcpl;  /* number of filters in DCPL*/
    unsigned      filt_flags;     /* filter flags */
    H5Z_filter_t  filtn;          /* filter identification number */
    unsigned      cd_values[20];  /* filter client data values */
    size_t        cd_nelmts;      /* filter client number of values */
    char          f_name[256];    /* filter name */
    size_t        size;           /* type size */
    int           i;              /* index */
    unsigned      j;              /* index */

    /* get information about filters */
    if((nfilters_dcpl = H5Pget_nfilters(pid)) < 0)
        return -1;

    /* if we do not have filters and the requested filter is NONE, return 1 */
    if(!nfilters_dcpl &&
        nfilters == 1 &&
        filter[0].filtn == H5Z_FILTER_NONE )
        return 1;

    /* else the numbers of filters must match */
    if (nfilters_dcpl != nfilters )
        return 0;

    /*-------------------------------------------------------------------------
     * build a list with DCPL filters
     *-------------------------------------------------------------------------
     */

    for( i = 0; i < nfilters_dcpl; i++)
    {
        cd_nelmts = NELMTS(cd_values);
        filtn = H5Pget_filter2(pid, (unsigned)i, &filt_flags, &cd_nelmts,
            cd_values, sizeof(f_name), f_name, NULL);

        /* filter ID */
        if (filtn != filter[i].filtn)
            return 0;

        /* compare client data values. some filters do return local values */
        switch (filtn)
        {

            case H5Z_FILTER_NONE:
            break;

            case H5Z_FILTER_SHUFFLE:
                /* 1 private client value is returned by DCPL */
                if ( cd_nelmts != H5Z_SHUFFLE_TOTAL_NPARMS && filter[i].cd_nelmts != H5Z_SHUFFLE_USER_NPARMS )
                    return 0;

                /* get dataset's type size */
                if((size = H5Tget_size(tid)) <= 0)
                    return -1;

                /* the private client value holds the dataset's type size */
                if ( size != cd_values[0] )
                    return 0;

                break;

            case H5Z_FILTER_SZIP:
                /* 4 private client values are returned by DCPL */
                if ( cd_nelmts != H5Z_SZIP_TOTAL_NPARMS && filter[i].cd_nelmts != H5Z_SZIP_USER_NPARMS )
                    return 0;

                /* "User" parameter for pixels-per-block (index 1) */
                if ( cd_values[H5Z_SZIP_PARM_PPB] != filter[i].cd_values[H5Z_SZIP_PARM_PPB] )
                    return 0;

                break;

            case H5Z_FILTER_NBIT:
                /* only client data values number of values checked */
                if ( H5Z_NBIT_USER_NPARMS != filter[i].cd_nelmts)
                    return 0;
                break;

            case H5Z_FILTER_SCALEOFFSET:
                /* only client data values checked */
                for( j = 0; j < H5Z_SCALEOFFSET_USER_NPARMS; j++)
                    if (cd_values[j] != filter[i].cd_values[j])
                        return 0;
                break;

            /* for these filters values must match, no local values set in DCPL */
            case H5Z_FILTER_FLETCHER32:
            case H5Z_FILTER_DEFLATE:

                if ( cd_nelmts != filter[i].cd_nelmts)
                    return 0;

                for( j = 0; j < cd_nelmts; j++)
                    if (cd_values[j] != filter[i].cd_values[j])
                        return 0;

                break;

            default:
                if ( cd_nelmts != filter[i].cd_nelmts)
                    return 0;

                for( j = 0; j < cd_nelmts; j++)
                    if (cd_values[j] != filter[i].cd_values[j])
                        return 0;
                break;

        } /* switch */
    }

    return 1;
}
Example #15
0
    H5EA_t *ea = NULL;          /* Pointer to new extensible array */
    H5EA_hdr_t *hdr = NULL;     /* The extensible array header information */
    haddr_t ea_addr;            /* Array header address */

#ifdef QAK
HDfprintf(stderr, "%s: Called\n", FUNC);
#endif /* QAK */

    /*
     * Check arguments.
     */
    HDassert(f);
    HDassert(cparam);

    /* H5EA interface sanity check */
    HDcompile_assert(H5EA_NUM_CLS_ID == NELMTS(H5EA_client_class_g));

    /* Create extensible array header */
    if(HADDR_UNDEF == (ea_addr = H5EA__hdr_create(f, dxpl_id, cparam, ctx_udata)))
	H5E_THROW(H5E_CANTINIT, "can't create extensible array header")

    /* Allocate extensible array wrapper */
    if(NULL == (ea = H5FL_MALLOC(H5EA_t)))
	H5E_THROW(H5E_CANTALLOC, "memory allocation failed for extensible array info")

    /* Lock the array header into memory */
    if(NULL == (hdr = H5EA__hdr_protect(f, dxpl_id, ea_addr, ctx_udata, H5AC__NO_FLAGS_SET)))
	H5E_THROW(H5E_CANTPROTECT, "unable to load extensible array header")

    /* Point extensible array wrapper at header and bump it's ref count */
    ea->hdr = hdr;
int
main() 
{
    /* The SgAsmGenericFile is the root of a tree describing a binary file (executable, shared lib, object, core dump).
     * Usually this node appears in a much larger AST containing instructions contexts and other information, but for the
     * purposes of this example, we'll just be generating the ELF Container and not any instructions.  This root will
     * eventually be passed to unparse() to generate the executable file. */
    SgAsmGenericFile *ef = new SgAsmGenericFile;
    
    /* The ELF File Header is the first thing in the file, always at offset zero. The constructors generally take arguments to
     * describe the new object's relationship with existing objects, i.e., its location in the AST. In this case, the
     * SgAsmElfFileHeader is a child of the SgAsmGenericFile (file headers are always children of their respective file).
     *
     * Section constructors (SgAsmElfFileHeader derives from SgAsmGenericSection) always place the new section at the current
     * end-of-file and give it an initial size of one byte.  This ensures that each section has a unique starting address,
     * which is important when file memory is actually allocated for a section and other sections need to be moved out of the
     * way--the allocator needs to know the relative positions of the sections in order to correctly relocate them.
     *
     * If we were parsing an ELF file we would usually use ROSE's frontend() method. However, one can also parse the file by
     * first constructing the SgAsmElfFileHeader and then invoking its parse() method, which parses the ELF File Header and
     * everything that can be reached from that header.
     * 
     * We use 0x400000 as the virtual address of the main LOAD segment, which occupies the first part of the file, up through
     * the end of the .text section (see below). ELF Headers don't actually store a base address, so instead of assigning one
     * to the SgAsmElfFileHeader we'll leave the headers's base address at zero and add base_va explicitly whenever we need
     * it. */
    SgAsmElfFileHeader *fhdr = new SgAsmElfFileHeader(ef);
    fhdr->get_exec_format()->set_word_size(8);                  /* default is 32-bit; we want 64-bit */
    fhdr->set_isa(SgAsmExecutableFileFormat::ISA_X8664_Family); /* instruction set architecture; default is ISA_IA32_386 */
    rose_addr_t base_va = 0x400000;                             /* base virtual address */

    /* ELF executable files always have an ELF Segment Table (also called the ELF Program Header Table) usually appears
     * immediately after the ELF File Header.  The ELF Segment Table describes contiguous regions of the file that should be
     * memory mapped by the loader. ELF Segments don't have names--names are imparted to the segment by virtue of a segment
     * also being described by the ELF Section Table which we'll create later.
     *
     * Note that an SgAsmGenericSection (which represents both ELF Segments and ELF Sections) does not have the ELF Segment
     * Table or ELF Section Table as a parent in the AST, but rather has the ELF File Header as a parent. This allows a single
     * SgAsmGenericSection to represent an ELF Segment, an ELF Section, both, or neither. It also allows us to define ELF
     * Segments and ELF Sections without needing to first define the ELF Segment Table or ELF Section Table, which is
     * particularly useful when one wants one of those tables to appear at the end of the file. */
    SgAsmElfSegmentTable *segtab = new SgAsmElfSegmentTable(fhdr);

#if 1
    /* Create the .text section to hold machine instructions. We'll eventually add it to the ELF Section Table (which will be
     * created at the end of the file) and store its name in an ELF String Section associated with the ELF Section Table. The
     * fact that we haven't created the string table yet doesn't prevent us from giving the section a name.
     *
     * A note about names: Names are represented by SgAsmGenericString objects. The SgAsmGenericString class is virtual and
     * subclasses SgAsmBasicString and SgAsmStoredString represent strings that have no file storage and strings that are
     * stored in the file, respectively.  The stored-string classes are further specialized to handle string tables for
     * various file formats and understand how to map the string into a file, how to allocate and reallocate space, etc.  In
     * general, you want to change the string characters (with set_string()) stored in an SgAsmGenericString that's already
     * present in the AST.
     * 
     * The section constructors and reallocators don't worry about alignment issues--they always allocate from the next
     * available byte. However, instructions typically need to satisfy some alignment constraints. We can easily adjust the
     * file offset chosen by the constructor, but we also need to tell the reallocator about the alignment constraint.
     * 
     * NOTE: There's a slight nuance of the reallocator that must be accounted for if you want to be very precise about where
     *       things are located in the file. When the reallocator shifts sections to make room for a section that needs to
     *       grow, it shifts all affected sections by a uniform amount. This preserves overlaps (like when a segment overlaps
     *       with a bunch of sections) but it also means that the alignment constraint we'll set for ".text" will cause the
     *       ELF Segment Table constructed above at a lower offset to move in tandem with ".text" when the ELF File Header (at
     *       an even lower address) needs to grow to its final size.  This isn't a problem--you just end up with a couple unused
     *       bytes between the header and the segment table that will show up as a "hole" when subsequently parsed. If you're
     *       picky, the correct solution is to give the header a chance to allocate its file space before we construct the
     *       ".text" segment.
     *
     * In order to write the instructions out during the unparse phase, we create a new class from SgAsmElfSection and
     * override the virtual unparse() method.  The reallocate() method is called just before unparsing and we augment that in
     * order to set an ELF-specific bit in the ELF Section Table flags (alternatively, we could have set that bit after the
     * add_section() call below that adds the .text section to the ELF Section Table, but we do it here for demonstration).
     * The calculate_sizes() function is used by SgAsmElfSection::reallocate() to figure out how much space is needed for the
     * section.
     *
     * The instructions correspond to the assembly:
     *     .section .text
     *     .global _start
     * _start:
     *     movl    $1, %eax    # _exit(86)
     *     movl    $86, %ebx
     *     int     $0x80 */
    class TextSection : public SgAsmElfSection {
    public:
        TextSection(SgAsmElfFileHeader *fhdr, size_t ins_size, const unsigned char *ins_bytes)
            : SgAsmElfSection(fhdr), ins_size(ins_size), ins_bytes(ins_bytes)
            {}
        virtual rose_addr_t calculate_sizes(size_t *entsize, size_t *required, size_t *optional, size_t *entcount) const {
            if (entsize)  *entsize  = 1;                        /* an "entry" is one byte of instruction */
            if (required) *required = 1;                        /* each "entry" is also stored in one byte of the file */
            if (optional) *optional = 0;                        /* there is no extra data per instruction byte */
            if (entcount) *entcount = ins_size;                 /* number of "entries" is the total instruction bytes */
            return ins_size;                                    /* return value is section size required */
        }
        virtual bool reallocate() {
            bool retval = SgAsmElfSection::reallocate();        /* returns true if size or position of any section changed */
            SgAsmElfSectionTableEntry *ste = get_section_entry();
            ste->set_sh_flags(ste->get_sh_flags() | SgAsmElfSectionTableEntry::SHF_ALLOC); /* set the SHF_ALLOC bit */
            return retval;
        }
        virtual void unparse(std::ostream &f) const {
            write(f, 0, ins_size, ins_bytes);                   /* Write the instructions at offset zero in section */
        }
        size_t ins_size;
        const unsigned char *ins_bytes;
    };
    ef->reallocate();                                           /* Give existing sections a chance to allocate file space */
    static const unsigned char instructions[] = {0xb8, 0x01, 0x00, 0x00, 0x00, 0xbb, 0x56, 0x00, 0x00, 0x00, 0xcd, 0x80};
    SgAsmElfSection *text = new TextSection(fhdr, NELMTS(instructions), instructions);
    text->set_purpose(SgAsmGenericSection::SP_PROGRAM);         /* Program-supplied text/data/etc. */
    text->set_file_alignment(4);                                /* Tell reallocator about alignment constraint */
    text->set_mapped_alignment(4);                              /* Alignment constraint for memory mapping */
    text->set_mapped_preferred_rva(base_va+text->get_offset()); /* Mapped address is based on file offset */
    text->set_mapped_size(text->get_size());                    /* Mapped size is same as file size */
    text->set_mapped_rperm(true);                               /* Readable */
    text->set_mapped_wperm(false);                              /* Not writable */
    text->set_mapped_xperm(true);                               /* Executable */
    text->get_name()->set_string(".text");                      /* Give section the standard name */
    text->align();                                              /* Because we changed alignment constraints */
#else
    /* Another way to do the same thing without a temporary class. */
    ef->reallocate();
    static const unsigned char instructions[] = {0xb8, 0x01, 0x00, 0x00, 0x00, 0xbb, 0x56, 0x00, 0x00, 0x00, 0xcd, 0x80};
    SgAsmElfSection *text = new SgAsmElfSection(fhdr);
    text->set_purpose(SgAsmGenericSection::SP_PROGRAM);         /* Program-supplied text/data/etc. */
    text->set_file_alignment(4);                                /* Tell reallocator about alignment constraint */
    text->set_size(sizeof instructions);                        /* Give section a specific size */
    text->set_mapped_alignment(4);                              /* Alignment constraint for memory mapping */
    text->set_mapped_preferred_rva(base_va+text->get_offset()); /* Mapped address is based on file offset */
    text->set_mapped_size(text->get_size());                    /* Mapped size is same as file size */
    text->set_mapped_rperm(true);                               /* Readable */
    text->set_mapped_wperm(false);                              /* Not writable */
    text->set_mapped_xperm(true);                               /* Executable */
    text->get_name()->set_string(".text");                      /* Give section the standard name */
    text->align();                                              /* Because we changed alignment constraints */
    unsigned char *content = text->writable_content(sizeof instructions);
    memcpy(content, instructions, sizeof instructions);
#endif


    /* Set the main entry point to be the first virtual address in the text section. A rose_rva_t address is associated
     * with a section (.text in this case) so that if the starting address of the section changes (such as via reallocate())
     * then the rose_rva_t address will also change so as to continue to refer to the same byte of the section. */
    rose_rva_t entry_rva(text->get_mapped_preferred_rva(), text);
    fhdr->add_entry_rva(entry_rva);

    /* We now add an entry to the ELF Segment Table. ELF Segments often overlap multiple sections, and in this case we're
     * going to create a segment that overlaps with the ELF File Header, the ELF Segment Table, and the ".text" section. The
     * functions that reallocate space for sections (since most are born with a size of one byte) understand that things may
     * overlap and make appropriate adjustments. In this case all we need to do is set the starting offset and size. */
    SgAsmElfSection *seg1 = new SgAsmElfSection(fhdr);          /* ELF Segments are represented by SgAsmElfSection */
    seg1->get_name()->set_string("LOAD");                       /* Segment names aren't saved (but useful for debugging) */
    seg1->set_offset(0);                                        /* Starts at beginning of file */
    seg1->set_size(text->get_offset() + text->get_size());      /* Extends to end of .text section */
    seg1->set_mapped_preferred_rva(base_va);                    /* Typically mapped by loader to this memory address */
    seg1->set_mapped_size(seg1->get_size());                    /* Make mapped size match size in the file */
    seg1->set_mapped_rperm(true);                               /* Readable */
    seg1->set_mapped_wperm(false);                              /* Not writable */
    seg1->set_mapped_xperm(true);                               /* Executable */
    segtab->add_section(seg1);                                  /* Add definition to ELF Segment Table */

    /* Add an empty PAX segment as further demonstration of adding an ELF Segment. We need to tell the loader that this
     * section holds PAX Flags. There's no interface (currently) to do this at a generic level, so we make an adjustment
     * directly to the ELF Segment Table entry. */
    SgAsmElfSection *pax = new SgAsmElfSection(fhdr);
    pax->get_name()->set_string("PAX Flags");                   /* Name just for debugging */
    pax->set_offset(0);                                         /* Override address to be at zero rather than EOF */
    pax->set_size(0);                                           /* Override size to be zero rather than one byte */
    segtab->add_section(pax);                                   /* Add definition to ELF Segment Table */
    pax->get_segment_entry()->set_type(SgAsmElfSegmentTableEntry::PT_PAX_FLAGS);
    
    /* In order to give sections names there has to be an ELF String Section where the names are stored. An ELF String Section
     * is a section itself and will appear in the ELF Section Table. The name of this string section will be stored in a
     * string section, possibly this same string section. */
    SgAsmElfStringSection *shstrtab = new SgAsmElfStringSection(fhdr);
    shstrtab->get_name()->set_string(".shstrtab");

    /* Most files have an ELF Section Table that describes the various sections in the file (the code, data, dynamic linking,
     * symbol tables, string tables, etc). The ELF Section Table is a child of the ELF File Header, just as all other sections
     * reachable from the ELF File Header are also children of the header.  The class SgAsmGenericSection is used to represent
     * all types of contiguous file regions, whether they're ELF Sections, ELF Segments, PE Objects, etc.
     * 
     * The first entry of an ELF Section Table should always be an all-zero entry. It is not necessary to explicitly create
     * the zero entry since the unparse() method will do so. When parsing an ELF Section Table, the parser will read the
     * all-zero entry and actually create an empty, no-name, section with ID==0 (this is to handle cases that violate the ELF
     * specification).
     * 
     * Section names are stored in an ELF String Section that's associated with the ELF Section Table. We make that
     * association by adding the ELF String Section to the ELF Section Table. The first string table that's added will be the
     * one that stores section names. */
    SgAsmElfSectionTable *sectab = new SgAsmElfSectionTable(fhdr);
    sectab->add_section(text);                                  /* Add the .text section */
    sectab->add_section(shstrtab);                              /* Add the string table to store section names. */

#if 1 /* This stuff is here for demonstration. It's not necessary for the generated a.out to function properly. */

    /* Create a symbol table. Symbol tables should always be added to the ELF Section Table, and thus have a name. ELF Symbol
     * Tables need to know the section that serves as storage for the symbol names, an ELF String Section. The string table
     * for the section name (set above) does not need to be the same as the string table used for symbol names, although we
     * use the same one here just for brevity. */
    SgAsmElfSymbolSection *symtab = new SgAsmElfSymbolSection(fhdr, shstrtab);
    symtab->get_name()->set_string(".dynsym-test");
    symtab->set_is_dynamic(true);                               /* This is a symbol table for dynamic linking */
    sectab->add_section(symtab);                                /* Make the entry in the ELF Section Table */

    /* Add some symbols to the symbol table. Symbols are born with an empty name pointing into the symbol string table and the
     * names should be adjusted by calling get_name()->set_string("xxx"); there is no need to construct new SgAsmStoredString
     * objects and assign them with set_name(). */
    SgAsmElfSymbol *symbol;

    symbol = new SgAsmElfSymbol(symtab);                        /* New symbol is a child of the symbol table */
    symbol->set_value(0xaabbccdd);                              /* Arbitrary symbol value, often an RVA */
    symbol->set_size(512);                                      /* Arbitrary size of the thing to which the symbol refers */
    symbol->get_name()->set_string("symbolA");                  /* Symbol name */

    symbol = new SgAsmElfSymbol(symtab);
    symbol->set_value(0x11223344);
    symbol->set_size(4);
    symbol->get_name()->set_string("symbolB");

    /* Create a .dynamic section. Dynamic sections are generally added to both the section table and the segment table and are
     * memory mapped with read/write permission. They need an ELF String Table in order to store the library names. In real
     * executables the string table is usually separate from the others, but we'll just use the one we already created. */
    SgAsmElfDynamicSection *dynamic = new SgAsmElfDynamicSection(fhdr, shstrtab);
    dynamic->get_name()->set_string(".dynamic-test");           /* Give section a name */
    dynamic->set_mapped_preferred_rva(0x800000);                /* RVA where loader will map section */
    dynamic->set_mapped_size(dynamic->get_size());              /* Make mapped size same as file size */
    dynamic->set_mapped_rperm(true);                            /* Readable */
    dynamic->set_mapped_wperm(true);                            /* Writable */
    dynamic->set_mapped_xperm(false);                           /* Not executable */
    sectab->add_section(dynamic);                               /* Make an ELF Section Table entry */
    segtab->add_section(dynamic);                               /* Make an ELF Segment Table entry */

    /* Create some entries for the .dynamic section. Entries are born with a null name rather than an empty string since most
     * entries don't have a string value. So an entry that requires a string value (like DT_NEEDED) will need to have a new
     * string constructed. You don't have to worry about placing the new string in a particular string table since
     * reallocate() will eventually take care of that, so you can even use the very simple SgAsmBasicString interface. */
    SgAsmElfDynamicEntry *dynent;

    /* Add some DT_NULL entries */
    dynent = new SgAsmElfDynamicEntry(dynamic);
    dynent = new SgAsmElfDynamicEntry(dynamic);
    dynent = new SgAsmElfDynamicEntry(dynamic);

    /* Add a library name */
    dynent = new SgAsmElfDynamicEntry(dynamic);
    dynent->set_d_tag(SgAsmElfDynamicEntry::DT_NEEDED);
    dynent->set_name(new SgAsmBasicString("testlib"));

    /* Create a relocation table. Relocation tables depend on a symbol table and, indirectly, a string table. */
    SgAsmElfRelocSection *reladyn = new SgAsmElfRelocSection(fhdr, symtab,NULL);
    reladyn->get_name()->set_string(".rela.dyn-test");
    reladyn->set_uses_addend(true);                             /* This is a "rela" table rather than a "rel" table */
    sectab->add_section(reladyn);                               /* Make an ELF Section Table entry */

    /* Add a relocation entry to the relocation table. We create just one here, but in practice you would create as many as
     * you need.  Even REL tabels have a set_r_addend() method, but you should leave the addend at zero for them. The
     * relocation type determines how many bits of the offset are significant (and other details) and are described in the
     * system <elf.h> file (we may eventually change this interface to unify the various file formats). */
    SgAsmElfRelocEntry *relent = new SgAsmElfRelocEntry(reladyn);
    relent->set_r_offset(0x080495ac);                           /* An arbitrary offset */
    relent->set_r_addend(0xaa);                                 /* Arbitrary value to add to relocation address */
    relent->set_sym(symtab->index_of(symbol));                  /* Index of symbol in its symbol table */
    relent->set_type(SgAsmElfRelocEntry::R_386_JMP_SLOT);       /*  */

    /* We can add notes to an executable */
    SgAsmElfNoteSection *notes = new SgAsmElfNoteSection(fhdr);
    notes->set_name(new SgAsmBasicString("ELF Notes"));
    notes->set_mapped_preferred_rva(0x900000);                /* RVA where loader will map section */
    notes->set_mapped_size(notes->get_size());              /* Make mapped size same as file size */
    notes->set_mapped_rperm(true);                            /* Readable */
    notes->set_mapped_wperm(true);                            /* Writable */
    notes->set_mapped_xperm(false);                           /* Not executable */
    SgAsmElfNoteEntry *note = new SgAsmElfNoteEntry(notes);
    note->set_name(new SgAsmBasicString("ROSE"));
    const char *payload = "This is a test note.";
    for (size_t i=0; payload[i]; i++) {
        note->get_payload().push_back(payload[i]);
    }
    segtab->add_section(notes);


    /* Add a Symbol Version section.  This is a gnu extension to elf to support versioning of individual symbols 
       the size of the symver section should match the size of the dynamic symbol section (nominally .dynsym).
       there should also be an entry in the .dynamic section [DT_VERDEFNUM] that matches this same number.
     */
    SgAsmElfSymverSection* symver = new SgAsmElfSymverSection(fhdr);
    symver->get_name()->set_string(".gnu-version-test");       /* Give section a name */
    symver->set_mapped_preferred_rva(0xA00000);                /* RVA where loader will map section */
    symver->set_mapped_size(symver->get_size());               /* Make mapped size same as file size */
    symver->set_mapped_rperm(true);                            /* Readable */
    symver->set_mapped_wperm(false);                           /* Writable */
    symver->set_mapped_xperm(false);                           /* Not executable */
    sectab->add_section(symver);                               /* Make an ELF Section Table entry */

    for(size_t i=0; i < symtab->get_symbols()->get_symbols().size(); ++i){
      SgAsmElfSymverEntry* symver_entry = new SgAsmElfSymverEntry(symver);
      symver_entry->set_value(i);
    }
    
    SgAsmElfSymverDefinedSection* symver_def = new SgAsmElfSymverDefinedSection(fhdr,shstrtab);
    symver_def->get_name()->set_string(".gnu.version_d-test");
    symver_def->set_mapped_preferred_rva(0xB00000);                /* RVA where loader will map section */
    symver_def->set_mapped_size(symver_def->get_size());               /* Make mapped size same as file size */
    symver_def->set_mapped_rperm(true);                            /* Readable */
    symver_def->set_mapped_wperm(false);                           /* Writable */
    symver_def->set_mapped_xperm(false);                           /* Not executable */

    sectab->add_section(symver_def);                               /* Make an ELF Section Table entry */

    /** normal case - one Aux structure*/
    SgAsmElfSymverDefinedEntry* symdefEntry1 = new SgAsmElfSymverDefinedEntry(symver_def);
    symdefEntry1->set_version(1);
    symdefEntry1->set_hash(0xdeadbeef);// need to implement real hash support?
    symdefEntry1->set_flags(0x0);
    
    SgAsmElfSymverDefinedAux* symdefAux1a = new SgAsmElfSymverDefinedAux(symdefEntry1, symver_def);
    symdefAux1a->get_name()->set_string("version-1");

    SgAsmElfSymverDefinedEntry* symdefEntry2 = new SgAsmElfSymverDefinedEntry(symver_def);
    new SgAsmElfSymverDefinedAux(symdefEntry2, symver_def);
    new SgAsmElfSymverDefinedAux(symdefEntry2, symver_def);

    /* Very strange case, but technically legal - zero Aux */
    new SgAsmElfSymverDefinedEntry(symver_def);

    SgAsmElfSymverNeededSection* symver_need = new SgAsmElfSymverNeededSection(fhdr,shstrtab);
    symver_need->get_name()->set_string(".gnu.version_d-test");
    symver_need->set_mapped_preferred_rva(0xC00000);                /* RVA where loader will map section */
    symver_need->set_mapped_size(symver_need->get_size());               /* Make mapped size same as file size */
    symver_need->set_mapped_rperm(true);                            /* Readable */
    symver_need->set_mapped_wperm(false);                           /* Writable */
    symver_need->set_mapped_xperm(false);                           /* Not executable */
    sectab->add_section(symver_need);                               /* Make an ELF Section Table entry */


#endif

    /* Some of the sections we created above have default sizes of one byte because there's no way to determine their true
     * size until we're all done creating sections.  The SgAsmGenericFile::reallocate() traverses the AST and allocates the
     * actual file storage for each section, adjusting other sections to make room and updating various tables and
     * file-based data structures. You can call this function at any time, but it must be called before unparse(). */
    ef->reallocate();

    /* The reallocate() has a shortcoming [as of 2008-12-19] in that it might not correctly update memory mappings in the case
     * where the mapping for a section is inferred from the mapping of a containing segment when the section is shifted in the
     * file to make room for an earlier section that's also in the segment.
     * 
     * We can work around that here by explicitly setting the memory mapping to be relative to the segment. However, we'll
     * need to call reallocate() again because besides moving things around, reallocate() also updates some ELF-specific data
     * structures from values stored in the more generic structures. */
    text->set_mapped_preferred_rva(seg1->get_mapped_preferred_rva()+(text->get_offset()-seg1->get_offset()));
    ef->reallocate(); /*won't resize or move things this time since we didn't modify much since the last call to reallocate()*/

    /* Show some results. This is largely the same output as would be found in the *.dump file produced by many of the other
     * binary analysis tools. */
    ef->dump(stdout);
    SgAsmGenericSectionPtrList all = ef->get_sections(true);
    for (size_t i=0; i<all.size(); i++) {
        fprintf(stdout, "Section %zu:\n", i);
        all[i]->dump(stdout, "    ", -1);
    }

    /* Unparse the AST to generate an executable. */
    std::ofstream f("a.out");
    ef->unparse(f);

    return 0;
}
Example #17
0
int apply_filters(const char* name,    /* object name from traverse list */
                  int rank,            /* rank of dataset */
                  hsize_t *dims,       /* dimensions of dataset */
                  size_t msize,        /* size of type */
                  hid_t dcpl_id,       /* dataset creation property list */
                  pack_opt_t *options, /* repack options */
                  int *has_filter)     /* (OUT) object NAME has a filter */


{
    int          nfilters;       /* number of filters in DCPL */
    hsize_t      chsize[64];     /* chunk size in elements */
    H5D_layout_t layout;
    int          i;
    pack_info_t  obj;

    *has_filter = 0;

    if (rank==0) /* scalar dataset, do not apply */
        return 0;

   /*-------------------------------------------------------------------------
    * initialize the assigment object
    *-------------------------------------------------------------------------
    */
    init_packobject(&obj);

   /*-------------------------------------------------------------------------
    * find options
    *-------------------------------------------------------------------------
    */
    if (aux_assign_obj(name,options,&obj)==0)
        return 0;

    /* get information about input filters */
    if ((nfilters = H5Pget_nfilters(dcpl_id))<0)
        return -1;

   /*-------------------------------------------------------------------------
    * check if we have filters in the pipeline
    * we want to replace them with the input filters
    * only remove if we are inserting new ones
    *-------------------------------------------------------------------------
    */
    if (nfilters && obj.nfilters )
    {
        *has_filter = 1;
        if (H5Premove_filter(dcpl_id,H5Z_FILTER_ALL)<0)
            return -1;
    }

   /*-------------------------------------------------------------------------
    * check if there is an existent chunk
    * read it only if there is not a requested layout
    *-------------------------------------------------------------------------
    */
    if (obj.layout == -1 )
    {
        if ((layout = H5Pget_layout(dcpl_id))<0)
            return -1;

        if (layout == H5D_CHUNKED)
        {
            if ((rank = H5Pget_chunk(dcpl_id,NELMTS(chsize),chsize/*out*/))<0)
                return -1;
            obj.layout = H5D_CHUNKED;
            obj.chunk.rank = rank;
            for ( i = 0; i < rank; i++)
                obj.chunk.chunk_lengths[i] = chsize[i];
        }
    }

    /*-------------------------------------------------------------------------
    * the type of filter and additional parameter
    * type can be one of the filters
    * H5Z_FILTER_NONE        0 , uncompress if compressed
    * H5Z_FILTER_DEFLATE     1 , deflation like gzip
    * H5Z_FILTER_SHUFFLE     2 , shuffle the data
    * H5Z_FILTER_FLETCHER32  3 , fletcher32 checksum of EDC
    * H5Z_FILTER_SZIP        4 , szip compression
    * H5Z_FILTER_NBIT        5 , nbit compression
    * H5Z_FILTER_SCALEOFFSET 6 , scaleoffset compression
    *-------------------------------------------------------------------------
    */

    if (obj.nfilters)
    {

   /*-------------------------------------------------------------------------
    * filters require CHUNK layout; if we do not have one define a default
    *-------------------------------------------------------------------------
    */
        if (obj.layout==-1)
        {

            /* stripmine info */
            hsize_t sm_size[H5S_MAX_RANK]; /*stripmine size */
            hsize_t sm_nbytes;             /*bytes per stripmine */

            obj.chunk.rank = rank;

            /*
            * determine the strip mine size. The strip mine is
            * a hyperslab whose size is manageable.
            */



            sm_nbytes = msize;
            for ( i = rank; i > 0; --i)
            {
                hsize_t size = H5TOOLS_BUFSIZE / sm_nbytes;
                if ( size == 0) /* datum size > H5TOOLS_BUFSIZE */
                    size = 1;
                sm_size[i - 1] = MIN(dims[i - 1], size);
                sm_nbytes *= sm_size[i - 1];
                assert(sm_nbytes > 0);

            }

            for ( i = 0; i < rank; i++)
            {
                obj.chunk.chunk_lengths[i] = sm_size[i];
            }

        }

        for ( i=0; i<obj.nfilters; i++)
        {
            switch (obj.filter[i].filtn)
            {
            default:
                break;

           /*-------------------------------------------------------------------------
            * H5Z_FILTER_DEFLATE       1 , deflation like gzip
            *-------------------------------------------------------------------------
            */
            case H5Z_FILTER_DEFLATE:
                {
                    unsigned     aggression;     /* the deflate level */

                    aggression = obj.filter[i].cd_values[0];
                    /* set up for deflated data */
                    if(H5Pset_chunk(dcpl_id, obj.chunk.rank, obj.chunk.chunk_lengths)<0)
                        return -1;
                    if(H5Pset_deflate(dcpl_id,aggression)<0)
                        return -1;
                }
                break;

           /*-------------------------------------------------------------------------
            * H5Z_FILTER_SZIP       4 , szip compression
            *-------------------------------------------------------------------------
            */
            case H5Z_FILTER_SZIP:
                {
                    unsigned  options_mask;
                    unsigned  pixels_per_block;

                    options_mask     = obj.filter[i].cd_values[0];
                    pixels_per_block = obj.filter[i].cd_values[1];

                    /* set up for szip data */
                    if(H5Pset_chunk(dcpl_id,obj.chunk.rank,obj.chunk.chunk_lengths)<0)
                        return -1;
                    if (H5Pset_szip(dcpl_id,options_mask,pixels_per_block)<0)
                        return -1;

                }
                break;

           /*-------------------------------------------------------------------------
            * H5Z_FILTER_SHUFFLE    2 , shuffle the data
            *-------------------------------------------------------------------------
            */
            case H5Z_FILTER_SHUFFLE:
                if(H5Pset_chunk(dcpl_id, obj.chunk.rank, obj.chunk.chunk_lengths)<0)
                    return -1;
                if (H5Pset_shuffle(dcpl_id)<0)
                    return -1;
                break;

           /*-------------------------------------------------------------------------
            * H5Z_FILTER_FLETCHER32 3 , fletcher32 checksum of EDC
            *-------------------------------------------------------------------------
            */
            case H5Z_FILTER_FLETCHER32:
                if(H5Pset_chunk(dcpl_id, obj.chunk.rank, obj.chunk.chunk_lengths)<0)
                    return -1;
                if (H5Pset_fletcher32(dcpl_id)<0)
                    return -1;
                break;
           /*----------- -------------------------------------------------------------
            * H5Z_FILTER_NBIT , NBIT compression
            *-------------------------------------------------------------------------
            */
            case H5Z_FILTER_NBIT:
                if(H5Pset_chunk(dcpl_id, obj.chunk.rank, obj.chunk.chunk_lengths)<0)
                    return -1;
                if (H5Pset_nbit(dcpl_id)<0)
                    return -1;
                break;
            /*----------- -------------------------------------------------------------
             * H5Z_FILTER_SCALEOFFSET , scale+offset compression
             *-------------------------------------------------------------------------
             */

            case H5Z_FILTER_SCALEOFFSET:
                {
                    H5Z_SO_scale_type_t scale_type;
                    int                 scale_factor;

                    scale_type   = (H5Z_SO_scale_type_t)obj.filter[i].cd_values[0];
                    scale_factor = obj.filter[i].cd_values[1];

                    if(H5Pset_chunk(dcpl_id, obj.chunk.rank, obj.chunk.chunk_lengths)<0)
                        return -1;
                    if (H5Pset_scaleoffset(dcpl_id,scale_type,scale_factor)<0)
                        return -1;
                }
                break;
            } /* switch */
        }/*i*/

    }
    /*obj.nfilters*/

    /*-------------------------------------------------------------------------
    * layout
    *-------------------------------------------------------------------------
    */

    if (obj.layout>=0)
    {
        /* a layout was defined */
        if (H5Pset_layout(dcpl_id, obj.layout)<0)
            return -1;

        if (H5D_CHUNKED == obj.layout)
        {
            if(H5Pset_chunk(dcpl_id, obj.chunk.rank, obj.chunk.chunk_lengths)<0)
                return -1;
        }
        else if (H5D_COMPACT == obj.layout)
        {
            if (H5Pset_alloc_time(dcpl_id, H5D_ALLOC_TIME_EARLY)<0)
                return -1;
        }
        /* remove filters for the H5D_CONTIGUOUS case */
        else if (H5D_CONTIGUOUS == obj.layout)
        {
            if (H5Premove_filter(dcpl_id,H5Z_FILTER_ALL)<0)
                return -1;
        }

    }

 return 0;
}
Example #18
0
/*-------------------------------------------------------------------------
 * Function:	test_ucdmesh
 *
 * Purpose:	Test unstructured mesh functions.
 *
 * Return:	Success:	0
 *
 *		Failure:	number of errors
 *
 * Programmer:	Robb Matzke
 *              Thursday, April  1, 1999
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static int
test_ucdmesh(DBfile *dbfile)
{
    int			nerrors = 0;
    static double	coordA[] = {101, 102, 103, 104, 105};
    static double	coordB[] = {201, 202, 203, 204, 205};
    static double	coordC[] = {301, 302, 303, 304, 305};
    static double	*coords[] = {coordA, coordB, coordC};
    static char		*coordnames[] = {"coordA", "coordB", "coordC"};
    static int		nnodes = NELMTS(coordA);
    static int		ndims = NELMTS(coords);
    static int		nzones = 3;
    static float	varA[] = {1,  2,  3,  4,  5, 6, 7, 8};
    static float	*vars[] = {varA};
    static char		*varnames[] = {"varA"};
    static int		nels = NELMTS(varA);
    static int		nvars = NELMTS(vars);
    static float	mixA[] = {0.1, 0.2, 0.3};
    static float	*mixvars[] = {mixA};
    static int		mixlen = NELMTS(mixA);
    static int		nodelist[] = {500, 501, 502, 503, 504, 505, 506, 507};
    static int		lnodelist = NELMTS(nodelist);
    static int		shapecnt[] = {1,  1,  2,  2,  3,  3,  4,  4,  5,  5};
    static int		shapesize[]= {5,  5,  4,  4,  3,  3,  2,  2,  1,  1};
    static int		nshapes = NELMTS(shapecnt);
    static int		typelist[] = {99, 98, 97, 96, 95, 94};
    static int		ntypes = NELMTS(typelist);
    static int		types[]    = {193, 192, 191, 190, 189, 187, 186};
    static int		zoneno[]   = {185, 184, 183, 182, 181, 180, 179};
    static int		nfaces = NELMTS(types);
    static int		origin = 1;

    puts("=== Ucdmesh ===");

    DBMkDir(dbfile, "/ucd");
    DBSetDir(dbfile, "/ucd");

    /* Mesh */
    if (DBPutUcdmesh(dbfile, "um1", ndims, coordnames, (float**)coords, nnodes,
		     nzones, "zl1", "fl1", DB_DOUBLE,
		     NULL)<0) {
	puts("DBPutUcdmesh(um1) failed");
	nerrors++;
    }
    if (DBPutUcdsubmesh(dbfile, "um2", "um1", nzones, "zl1", "fl1", NULL)<0) {
	puts("DBPutUcdsubmesh(um2) failed");
	nerrors++;
    }
    
    /* Variable */
    if (DBPutUcdvar(dbfile, "uv1", "um1", nvars, varnames, vars, nels, mixvars,
		    mixlen, DB_FLOAT, DB_NODECENT, NULL)<0) {
	puts("DBPutUcdvar(uv1) failed");
	nerrors++;
    }

    /* Facelist */
    if (DBPutFacelist(dbfile, "fl1", nfaces, ndims, nodelist, lnodelist,
		      origin, zoneno, shapesize, shapecnt, nshapes, types,
		      typelist, ntypes)<0) {
	puts("DBPutFacelist(fl1) failed");
	nerrors++;
    }

    /* Zonelist */
    if (DBPutZonelist(dbfile, "zl1", nzones, ndims, nodelist, lnodelist,
		      origin, shapesize, shapecnt, nshapes)<0) {
	puts("DBPutZonelist(zl1) failed");
	nerrors++;
    }

    
    return nerrors;
}