Hash_table* smp_load(const char *project, const char *skel) { const char *file = NULL; FILE *f_skl = NULL; unsigned long nstr; const char *tab, *tabp; Hash_table *htab; int i; struct stat sbuf; char *cbdid; if (NULL == (file = skl_file_e(project,skel,"smp"))) return NULL; if (NULL == (f_skl = fopen(file,"r"))) { if (strcmp(project,"cdli")) { free((char*)file); if (NULL == (file = skl_file_e("cdli",skel,"smp"))) return NULL; if (NULL == (f_skl = fopen(file,"r"))) { free((char*)file); return NULL; } } else { free((char*)file); return NULL; } } stat(file,&sbuf); xfread(file,1,&nstr,4,1,f_skl); tab = tabp = malloc(sbuf.st_size - 4); xfseek(file,f_skl,4,SEEK_SET); xfread(file,1,tab,1,sbuf.st_size - 4,f_skl); xfclose(file,f_skl); htab = hash_create(nstr); hash_add(htab,(unsigned char *)"#cbd",cbdid = cbd_ident(project,skel)); for (i = 0; i < nstr; ++i) { const char *data = tabp+strlen(tabp)+1; hash_add(htab,(unsigned char *)tabp,(void*)data); tabp = data + strlen(data) + 1; } skels = realloc(skels,(nskels+1)*sizeof(struct loaded_skl)); skels[nskels].hash = htab; skels[nskels].tab = (unsigned char*)tab; skels[nskels].file = (char*)file; skels[nskels].cbd = cbdid; ++nskels; return htab; }
int load_map_header(t_engine *e, FILE *fd) { // regarder si ya soucis de BIG/LITTLE endian avec les shorts et les fread if (xfread((void*)&e->map_data.w, sizeof(unsigned short), 1, fd)) return (1); if (xfread((void*)&e->map_data.h, sizeof(unsigned short), 1, fd)) return (1); if (xfread((void*)&e->map_data.nb_players, sizeof(unsigned short), 1, fd)) return (1); return (0); }
static void readStringTable(unsigned long offset) { // Read string table size unsigned long ssize; fseek(sym, offset, 0); xfread(&ssize, sizeof(ssize), 1, sym); if (ssize > MAXINT) fatalError("String table is too big -- sorry."); stringTable = new char[ssize+1]; fseek(sym, offset, 0); xfread(stringTable, sizeof(char), (int)ssize, sym); stringTable[ssize] = NULL; }
/* build the new symbol and string tables */ static void buildNewTables(const HDR *x, long nsyms) { long remaining = nsyms; // total number of symbol table entries remaining to process #if FAST_NONPORTABLE_STRUCT_ARRAY_READ const int bunch = 1024; // number of symbol table entries to process at a time #else const int bunch = 1; #endif char *syms = new char[bunch*SYMSZ]; // buffer of old symbol table entries fseek(sym, N_SYMOFF(*x), 0); // go to old symbol table // fill the buffer int numInBuffer = xfread(syms, SYMSZ, min(bunch, remaining), sym); int symsi = 0; // index in buffer char *thisSym = syms; // pointer to entry symsi in buffer int auxEntries = 0; // number of aux entries still to skip over while (remaining) { if (auxEntries) // skip over them { if (auxEntries < 0) fatalError("Bad symbol table"); int skip = min(numInBuffer-symsi, auxEntries); if (remaining < skip) fatalError("Bad symbol table (missing auxiliary entries)"); auxEntries -= skip; remaining -= skip; symsi += skip; thisSym += skip*SYMSZ; } else { transformSymEntry((SYM*)thisSym); auxEntries = N_AUX(*(SYM*)thisSym); remaining--; symsi++; thisSym += SYMSZ; } assert (symsi <= bunch); if (symsi == bunch) // write and then refill the buffer { xfwrite(syms, SYMSZ, numInBuffer, newSym); numInBuffer = xfread(syms, SYMSZ, min(bunch, remaining), sym); symsi = 0; thisSym = syms; } } xfwrite(syms, SYMSZ, numInBuffer, newSym); delete syms; }
void Squeak_Image_Reader::read_image() { /* readImageFromFile: f HeapSize: desiredHeapSize StartingAt: imageOffset "Read an image from the given file stream, allocating the given amount of memory to its object heap. Fail if the image has an unknown format or requires more than the given amount of memory." "Details: This method detects when the image was stored on a machine with the opposite byte ordering from this machine and swaps the bytes automatically. Furthermore, it allows the header information to start 512 bytes into the file, since some file transfer programs for the Macintosh apparently prepend a Mac-specific header of this size. Note that this same 512 bytes of prefix area could also be used to store an exec command on Unix systems, allowing one to launch Smalltalk by invoking the image name as a command." "This code is based on C code by Ian Piumarta and Smalltalk code by Tim Rowledge. Many thanks to both of you!!" */ Object::verify_constants(); read_header(); fprintf(stdout, "allocating memory for snapshot\n"); // "allocate a contiguous block of memory for the Squeak heap" memory = (char*)Memory_Semantics::shared_malloc(dataSize); assert_always(memory != NULL); /* memStart := self startOfMemory. memoryLimit := (memStart + heapSize) - 24. "decrease memoryLimit a tad for safety" endOfMemory := memStart + dataSize. */ // "position file after the header" fprintf(stdout, "reading objects in snapshot\n"); if ( fseek(image_file, headerStart + headerSize, SEEK_SET)) perror("seek"), fatal(); // "read in the image in bulk, then swap the bytes if necessary" xfread(memory, 1, dataSize, image_file); // "First, byte-swap every word in the image. This fixes objects headers." if (swap_bytes) reverseBytes((int32*)&memory[0], (int32*)&memory[dataSize]); // "Second, return the bytes of bytes-type objects to their orginal order." if (swap_bytes) byteSwapByteObjects(); Safepoint_Ability sa(false); // for distributing objects and putting image name distribute_objects(); imageNamePut_on_all_cores(file_name, strlen(file_name)); // we need to reoder floats if the image was a Cog image if (is_cog_image_with_reodered_floats()) { normalize_float_ordering_in_image(); } }
/* NOTE: not reentrant */ char *xfread_name( FILE *fp, char *filename, size_t len_bytes ) { static size_t alloc_size = 0; size_t len; /* get length */ len = xfread_value( fp, filename, len_bytes ) & 0xFFFF; /* first time */ if ( alloc_size == 0 ) atexit( free_last_name ); /* reallocate string if needed */ if ( alloc_size < len + 1 ) { alloc_size = len + 1; last_name = realloc( last_name, alloc_size ); } /* read bytes */ xfread( fp, filename, last_name, len ); last_name[len] = '\0'; return last_name; }
/*----------------------------------------------------------------------------- * Read file signature *----------------------------------------------------------------------------*/ enum file_type read_signature( FILE *fp, char *filename ) { enum file_type type = is_none; file_version = -1; memset( file_signature, 0, sizeof(file_signature) ); /* read signature */ xfread( fp, filename, file_signature, 8 ); if ( strncmp( file_signature, "Z80RMF", 6 ) == 0 ) type = is_object; else if ( strncmp( file_signature, "Z80LMF", 6 ) == 0 ) type = is_library; else die("File %s: not object nor library file\n", filename ); /* read version */ if ( sscanf( file_signature + 6, "%d", &file_version ) < 1 ) die("File %s: not object nor library file\n", filename ); if ( file_version < MIN_VERSION || file_version > MAX_VERSION ) die("File %s: not object or library file version %d not supported\n", filename, file_version ); printf("\nFile %s at $%04X: %s\n", filename, ftell( fp ) - 8, file_signature ); return type; }
static bta_cell_t *_bta_read_cell(FILE *fp) { bta_cell_t *rval; uint32_t size; uint32_t compsize; rval = (bta_cell_t *)xzalloc(sizeof(bta_cell_t)); rval->x = fp_read16le(fp); rval->y = fp_read16le(fp); rval->width = fp_read16le(fp); rval->height = fp_read16le(fp); rval->delay = fp_read16le(fp); compsize = fp_read32le(fp); debug("x = %d\n", rval->x); debug("y = %d\n", rval->y); debug("width = %d\n", rval->width); debug("height = %d\n", rval->height); debug("compsize = %d\n", compsize); debug("size = %d\n", (rval->height * rval->width) * 4); size = (rval->height * rval->width) * 4; rval->gfx = bts_new(compsize); xfread(rval->gfx->buf, sizeof(uint8_t), compsize, fp); rval->gfx = zlib_uncompress(rval->gfx, size); return rval; }
bool CDlgFileConv::ConvertFileToCharset( FILE* fps, FILE* fpd, const std::string& src_charset, const std::string& dst_charset) { char *src_text = NULL, *dst_text = NULL; size_t src_len = 0, dst_len = 0; int ret; if (!xfread(fps, -1, 0, &src_text, &src_len)) return false; ret = convert_to_charset( src_charset.c_str(), dst_charset.c_str(), src_text, src_len, &dst_text, &dst_len, 0); if (!ret) { xfree(src_text); return false; } ret = fwrite(dst_text, dst_len, 1, fpd); xfree(src_text); xfree(dst_text); return ret == 1; }
/* Parse and store the ACL data from a directory vnode */ static afs_uint32 parse_acl(XFILE * X, unsigned char *tag, tagged_field * field, afs_uint32 value, tag_parse_info * pi, void *g_refcon, void *l_refcon) { struct acl_accessList *acl; dump_parser *p = (dump_parser *) g_refcon; afs_vnode *v = (afs_vnode *) l_refcon; afs_uint32 r, i, n; if ((r = xfread(X, v->acl, SIZEOF_LARGEDISKVNODE - SIZEOF_SMALLDISKVNODE))) return r; v->field_mask |= F_VNODE_ACL; if (p->print_flags & DSPRINT_ACL) { acl = (struct acl_accessList *)(v->acl); n = ntohl(acl->positive); if (n) { printf(" Positive ACL: %d entries\n", n); for (i = 0; i < n; i++) printf(" %9d %s\n", ntohl(acl->entries[i].id), rights2str(ntohl(acl->entries[i].rights))); } n = ntohl(acl->negative); if (n) { printf(" Negative ACL: %d entries\n", n); for (i = ntohl(acl->positive); i < ntohl(acl->total); i++) printf(" %9d %s\n", ntohl(acl->entries[i].id), rights2str(ntohl(acl->entries[i].rights))); } } return ReadByte(X, tag); }
void sky_dump(const char *project, const char *skel, const char *values, const char *type) { struct skl *skltab = NULL,*s; const char *file = NULL; FILE *f_skl = NULL; unsigned long ntab; const unsigned char *tab, *tabp; int i; int skyext = 1; if (NULL == (file = skl_file(project,skel,skyext))) { fprintf(stderr,"sklshow: %s/%s.sky not found\n",project,skel); return; } if (NULL == (f_skl = fopen(file,"r"))) { fprintf(stderr, "sklshow: %s found but not openable\n", file); return; } xfread(file,1,&ntab,4,1,f_skl); skltab = malloc(ntab * sizeof(struct skl)); xfread(file,1,skltab,sizeof(struct skl),ntab,f_skl); for (s = skltab,i=0; i < ntab; ++i,++s) if (!strcmp(s->values,values) && !strcmp(s->type,type)) break; if (i == ntab) { fprintf(stderr, "sklshow: values/type %s/%s not found in %s/%s.sky\n", values, type, project, skel); return; } /*FIXME: should keep a list of these so they can be free'd */ tab = tabp = malloc(s->length); xfseek(file,f_skl,s->offset,SEEK_SET); xfread(file,1,tab,1,s->length,f_skl); xfclose(file,f_skl); for (i = 0; i < s->count; ++i) { printf("%s\n",tabp); tabp += strlen((const char *)tabp) + 1; } }
/* do_read for profiled xfiles */ static afs_uint32 xf_PROFILE_do_read(XFILE * X, void *buf, afs_uint32 count) { PFILE *PF = X->refcon; afs_uint32 err; err = xfread(&PF->content, buf, count); xfprintf(&PF->profile, "R %ld =%ld\n", (long)count, (long)err); return err; }
afs_uint32 ReadInt16(XFILE * X, afs_uint16 * val) { afs_uint32 r; if ((r = xfread(X, val, 2))) return r; *val = ntohs(*val); return 0; }
afs_uint32 ReadInt32(XFILE * X, afs_uint32 * val) { afs_uint32 r; if ((r = xfread(X, val, 4))) return r; *val = ntohl(*val); return 0; }
static void local_readParticle(FILE *f, float *partData, bool doByteswap) { xfread(partData, sizeof(float), 6, f); if (doByteswap) { for (int i = 0; i < 6; i++) byteswap(partData + i, sizeof(float)); } }
void get_pwd_pop(char *pwd) { FILE *stream; size_t octet; stream = popen_result("pwd"); octet = xfread(pwd, 1, 2048, stream); if (pwd[octet - 1] == '\n') pwd[octet - 1] = '\0'; pwd[octet] = '\0'; xfpurge(stream); xpclose(stream); }
static void local_readHeaderActual(artHeader_t header, FILE *f) { uint32_t b1, b2; endian_t systemEndianess = endian_getSystemEndianess(); xfread(&b1, sizeof(uint32_t), 1, f); xfread(header->headerString, sizeof(char), ARTHEADER_HEADERSTRING_LENGTH, f); header->headerString[ARTHEADER_HEADERSTRING_LENGTH] = '\0'; xfread(&(header->aexpn), sizeof(float), 121, f); xfread(&b2, sizeof(uint32_t), 1, f); if (b1 != b2) { diediedie(EXIT_FAILURE); } if (systemEndianess != header->fileEndianess) local_byteswapHeader(header); local_calcFactorWeight(header); local_calcFactorPosition(header); local_calFactorVelocity(header); }
static int copyfile(XFILE * in, XFILE * out, int size) { static char buf[COPYBUFSIZE]; int nr, r; while (size) { nr = (size > COPYBUFSIZE) ? COPYBUFSIZE : size; if ((r = xfread(in, buf, nr))) return r; if ((r = xfwrite(out, buf, nr))) return r; size -= nr; } return 0; }
/* * This function will generate a table of how often each character in a file occurs. * The result is stored in pOutFrequencies. */ eFileCode GenerateFrequenciesForGeneric(FILE* pFile, unsigned long long pOutFrequencies[256]) { assert(pFile != NULL); while (true) { char byte; size_t read = xfread(&byte, 1, 1, pFile); if (read == 0) return FILE_SUCCESS; unsigned index = (unsigned char)byte; assert(index < 256); pOutFrequencies[index]++; } }
struct pic_string * pic_get_output_string(pic_state *pic, struct pic_port *port) { size_t size; char *buf; /* get endpos */ xfflush(port->file); size = (size_t)xftell(port->file); xrewind(port->file); /* copy to buf */ buf = (char *)pic_alloc(pic, size + 1); buf[size] = 0; xfread(buf, size, 1, port->file); return pic_make_str(pic, buf, size); }
afs_uint32 xfskip(XFILE * X, afs_uint32 count) { afs_uint32 code; u_int64 tmp64; /* Use the skip method, if there is one */ if (X->do_skip && !X->passthru) { code = (X->do_skip) (X, count); if (code) return code; add64_32(tmp64, X->filepos, count); cp64(X->filepos, tmp64); return 0; } /* Simulate using absolute seek, if available */ if (X->do_seek && !X->passthru) { if ((code = xftell(X, &tmp64))) return code; add64_32(X->filepos, tmp64, count); cp64(tmp64, X->filepos); return xfseek(X, &tmp64); } /* Do it the hard/slow way - read all the data to be skipped. * This is done if no other method is available, or if we are * supposed to be copying all the data to another XFILE */ { char buf[SKIP_SIZE]; afs_uint32 n; while (count) { n = (count > SKIP_SIZE) ? SKIP_SIZE : count; if ((code = xfread(X, buf, n))) return code; count -= n; } return 0; } }
int load_map_data_scale(t_engine *e, FILE *fd) { int y; int x; e->map_data.data = (t_map_data_scale**)xmalloc(sizeof(*e->map_data.data) * e->map_data.h); for (y = 0; y < e->map_data.h; y++) { e->map_data.data[y] = (t_map_data_scale*)xmalloc(sizeof(**e->map_data.data) * e->map_data.w); for (x = 0; x < e->map_data.w; x++) { if (xfread((void*)&(e->map_data.data[y][x]), sizeof(t_map_data_scale), 1, fd)) return (1); } } return (0); }
void se_pcre_init(const char *project, const char *index) { static int initialized = 0; static char *iproject = NULL, *iindex = NULL; FILE *fp; static char glist_fname[_MAX_PATH]; if (!project || !*project) project = "cdli"; if (initialized && !strcmp(iproject,project) && !strcmp(iindex,index)) return; if (iproject) free(iproject); if (iindex) free(iindex); iproject = strdup(project); iindex = strdup(index); initialized = 1; strcpy(glist_fname, se_file(project, index, "key.lst")); xaccess (glist_fname, R_OK, TRUE); glist_buf_len = fsize_t (glist_fname, NULL); glist_buf = malloc(glist_buf_len+3); *glist_buf = '\n'; fp = xfopen (glist_fname, "rb"); xfread (glist_fname, TRUE, &glist_buf[1], 1, glist_buf_len, fp); xfclose (glist_fname, fp); ++glist_buf_len; glist_buf[glist_buf_len++] = '\n'; glist_buf[glist_buf_len++] = '\0'; for (glist_begin = glist_buf+1; '#' == *glist_begin; ++glist_begin) { while ('\n' != *glist_begin) ++glist_begin; } glist_len = glist_buf_len - (glist_begin - glist_buf); }
static pic_value pic_port_read_blob_ip(pic_state *pic) { struct pic_port *port; struct pic_blob *bv; int n; char *buf; size_t start, end, i, len; n = pic_get_args(pic, "b|pkk", &bv, &port, &start, &end); switch (n) { case 1: port = pic_stdin(pic); case 2: start = 0; case 3: end = bv->len; } assert_port_profile(port, PIC_PORT_IN | PIC_PORT_BINARY, PIC_PORT_OPEN, "read-bytevector!"); if (end < start) { pic_errorf(pic, "read-bytevector!: end index must be greater than or equal to start index"); } len = end - start; buf = pic_calloc(pic, len, sizeof(char)); i = xfread(buf, sizeof(char), len, port->file); memcpy(bv->data + start, buf, i); pic_free(pic, buf); if (i == 0) { return pic_eof_object(); } else { return pic_size_value(i); } }
static pic_value pic_port_get_output_bytevector(pic_state *pic) { struct pic_port *port = pic_stdout(pic); pic_blob *blob; size_t size; pic_get_args(pic, "|p", &port); assert_port_profile(port, PIC_PORT_OUT | PIC_PORT_BINARY, PIC_PORT_OPEN, "get-output-bytevector"); /* get endpos */ xfflush(port->file); size = (size_t)xftell(port->file); xrewind(port->file); /* copy to buf */ blob = pic_make_blob(pic, size); xfread(blob->data, 1, size, port->file); return pic_obj_value(blob); }
static pic_value pic_port_read_blob(pic_state *pic) { struct pic_port *port = pic_stdin(pic); pic_blob *blob; size_t k, i; pic_get_args(pic, "k|p", &k, &port); assert_port_profile(port, PIC_PORT_IN | PIC_PORT_BINARY, PIC_PORT_OPEN, "read-bytevector"); blob = pic_make_blob(pic, k); i = xfread(blob->data, sizeof(char), k, port->file); if (i == 0) { return pic_eof_object(); } else { pic_realloc(pic, blob->data, i); blob->len = i; return pic_obj_value(blob); } }
static void local_readComponent(art_t art, uint64_t pSkip, uint64_t pRead, stai_t component, bool doByteswap) { if ((component == NULL) || (pSkip == art->numParticlesInPage)) { xfseek(art->f, art->numParticlesInPage * sizeof(float), SEEK_CUR); return; } float *buffer; bool bufferIsAllocated; if (stai_isLinear(component) && (stai_getSizeOfElementInBytes(component) == sizeof(float))) { buffer = stai_getBase(component); } else { buffer = xmalloc(sizeof(float) * pRead); bufferIsAllocated = true; } xfseek(art->f, (long)pSkip * sizeof(float), SEEK_CUR); xfread(buffer, sizeof(float), pRead, art->f); xfseek(art->f, (long)(art->numParticlesInPage - pSkip - pRead) * sizeof(float), SEEK_CUR); if (doByteswap) { for (int i = 0; i < pRead; i++) byteswap(buffer + i, sizeof(float)); } if (bufferIsAllocated) { local_copyBufferToStai(buffer, component, pRead); xfree(buffer); } }
/* Parse or skip over the vnode data */ static afs_uint32 parse_vdata(XFILE * X, unsigned char *tag, tagged_field * field, afs_uint32 value, tag_parse_info * pi, void *g_refcon, void *l_refcon) { dump_parser *p = (dump_parser *) g_refcon; afs_vnode *v = (afs_vnode *) l_refcon; static char *symlink_buf = 0; static int symlink_size = 0; afs_uint32 r; if ((r = ReadInt32(X, &v->size))) return r; v->field_mask |= F_VNODE_SIZE; if (v->size) { v->field_mask |= F_VNODE_DATA; if ((r = xftell(X, &v->d_offset))) return r; if (p->print_flags & DSPRINT_VNODE) printf("%s%d (0x%08x) bytes at %s (0x%s)\n", field->label, v->size, v->size, decimate_int64(&v->d_offset, 0), hexify_int64(&v->d_offset, 0)); switch (v->type) { case vSymlink: if (v->size > symlink_size) { if (symlink_buf) symlink_buf = realloc(symlink_buf, v->size + 1); else symlink_buf = (char *)malloc(v->size + 1); symlink_size = symlink_buf ? v->size : 0; } if (symlink_buf) { if ((r = xfread(X, symlink_buf, v->size))) return r; symlink_buf[v->size] = 0; if (p->print_flags & DSPRINT_VNODE) printf(" Target: %s\n", symlink_buf); } else { /* Call the callback here, because it's non-fatal */ if (p->cb_error) (p->cb_error) (ENOMEM, 0, p->err_refcon, "Out of memory reading symlink"); if ((r = xfskip(X, v->size))) return r; } break; case vDirectory: if (p->cb_dirent || (p->print_flags & DSPRINT_DIR)) { if ((r = parse_directory(X, p, v, v->size, 0))) return r; break; } default: if ((r = xfskip(X, v->size))) return r; } } else if (p->print_flags & DSPRINT_VNODE) { printf("%sEmpty\n", field->label); } if (p->repair_flags & DSFIX_VDSYNC) { r = resync_vnode(X, p, v, 10, 15); if (r) return r; } return ReadByte(X, tag); }
int32 Squeak_Image_Reader::get_long() { int32 x; xfread(&x, sizeof(x), 1, image_file); if (swap_bytes) swap_bytes_long(&x); return x; }
static afs_uint32 symlink_cb(afs_vnode * v, XFILE * X, void *refcon) { char *vnodepath, *linktarget, vnpx[30]; dt_uint64 where; int r, use = 0; if (!dirs_done) { dirs_done = 1; if (verbose) printf("* Extracting files...\n"); } /* Should we even use this? */ if (!use_vnum) { if ((r = Path_Build(X, &phi, v->vnode, &vnodepath, !use_realpath))) return r; if (!(use = usevnode(X, v->vnode, vnodepath))) { free(vnodepath); return 0; } if (use == 2) { free(vnodepath); sprintf(vnpx, "#%d:%d", v->vnode, v->vuniq); vnodepath = vnpx; } } else { sprintf(vnpx, "#%d:%d", v->vnode, v->vuniq); vnodepath = vnpx; } if (!(linktarget = malloc(v->size + 1))) { if (vnodepath != vnpx) free(vnodepath); return DSERR_MEM; } if ((r = xftell(X, &where)) || (r = xfseek(X, &v->d_offset)) || (r = xfread(X, linktarget, v->size))) { if (vnodepath != vnpx) free(vnodepath); free(linktarget); return r; } xfseek(X, &where); linktarget[v->size] = 0; /* Print it out */ if (verbose) printf("l%s %3d %-11d %11d %s %s -> %s\n", modestr(v->mode), v->nlinks, v->owner, v->size, datestr(v->server_date), vnodepath, linktarget); else if (!quiet) printf("%s\n", vnodepath); r = 0; if (!nomode) { if (symlink(linktarget, vnodepath + 1)) r = errno; } free(linktarget); if (vnodepath != vnpx) free(vnodepath); return r; }