void getx3dslaves() { /* */ /* Get the slave/master data. */ /* */ int i, j, *flag_data, ntypes; /* Read the header and loop through the data. */ if (x3dread_slave_header() > 0) readx3d_error(filnam); if (x3d_header.numslaves > 0) { /* Save slaves as a node flag, slave_type, */ /* with none, slave and master types. */ flag_data = (int *)malloc((nnodes+1) * sizeof(int)); for (i = 0; i < nnodes+1; i++) flag_data[i] = 4; /* Read a slave lines from the x3d file and set flag_data. */ for (i = 0; i < x3d_header.numslaves; i++) { if (x3dread_slaves() > 0) readx3d_error(filnam); flag_data[x3d_slave.node_no] = 2; for (j = 0; j < x3d_slave.nmasters; j++) { flag_data[x3d_slave.masters[j]] = 1; } } /* Save node flag name. */ flagname_node[numflags_node] = (char *)malloc(33*sizeof(char)); if (flagname_node[numflags_node] == NULL) memerr(); strcpy(flagname_node[numflags_node],"slave_types"); ntypes = 4; flagtypes_node[numflags_node] = (short) ntypes; for (j = 0; j < ntypes; j++) { flagnames_node[numflags_node][j] = (char *)malloc(33*sizeof(char)); if (j == 0) strcpy(flagnames_node[numflags_node][j],"master"); if (j == 1) strcpy(flagnames_node[numflags_node][j],"slave"); if (j == 2) strcpy(flagnames_node[numflags_node][j],"-"); if (j == 3) strcpy(flagnames_node[numflags_node][j],"none"); } /* Save node flag data. */ flags[numflags_node] = (short *)malloc((nnodes+1)*sizeof(short)); if (flags[numflags_node] == NULL) memerr(); for (i = 0; i < nnodes+1; i++) flags[numflags_node][i] = (short)flag_data[i]; numflags_node++; free(flag_data); } /* Read the slave trailer. */ if (x3dread_slave_end() > 0) readx3d_error(filnam); }
int main(int argc, char *argv[]) { int i=0; char *endp; stor[i++] = 0; if (argc < 2) { puts("\aUsage: PI <number_of_digits>"); return(1); } n = strtol(argv[1], &endp, 10); if (NULL == (mf = Fcalloc((Size_T)(n + 3L), (Size_T)sizeof(long)))) memerr(1); if (NULL == (ms = Fcalloc((Size_T)(n + 3L), (Size_T)sizeof(long)))) memerr(2); printf("\nApproximation of PI to %ld digits\n", (long)n); cnt = 0; kf = 25; ks = 57121L; mf[1] = 1L; for (i = 2; i <= (int)n; i += 2) { mf[i] = -16L; mf[i+1] = 16L; } for (i = 1; i <= (int)n; i += 2) { ms[i] = -4L; ms[i+1] = 4L; } printf("\n 3."); while (cnt < n) { for (i = 0; ++i <= (int)n - (int)cnt; ) { mf[i] *= 10L; ms[i] *= 10L; } for (i =(int)(n - cnt + 1); --i >= 2; ) { temp = 2 * i - 1; shift(&mf[i - 1], &mf[i], temp - 2, temp * kf); shift(&ms[i - 1], &ms[i], temp - 2, temp * ks); } nd = 0; shift((long FAR *)&nd, &mf[1], 1L, 5L); shift((long FAR *)&nd, &ms[1], 1L, 239L); xprint(nd); } printf("\n\nCalculations Completed!\n"); Ffree(ms); Ffree(mf); return(0); }
static struct srcspan * splitspan( /* divide source span at point */ register struct srcspan *sso, double h, double v, double m ) { register struct srcspan *ssn; double d; int hs; d = h - m*(sso->v - v); hs = d < 0. ? d-.5 : d+.5; if (sso->l >= hs) return(NULL); if (sso->r <= hs) return(sso); /* need to split it */ ssn = (struct srcspan *)malloc(sizeof(struct srcspan)); if (ssn == NULL) memerr("source spans in splitspan"); ssn->brsum = (double)(hs - sso->l)/(sso->r - sso->l) * sso->brsum; sso->brsum -= ssn->brsum; ssn->v = sso->v; ssn->l = sso->l; ssn->r = sso->l = hs; return(ssn); }
void bldtop() { /* */ /* Build cell level list. */ /* */ int l, ntop; ltop = (int *)malloc((numcell+1)*sizeof(int)); if (ltop == NULL) memerr(); for (l = 0; l <= numcell; l++) ltop[l] = 0; ntop = 0; for (l = 1; l <= numcell; l++) { if (daughter[l] == 0) { ntop++; ltop[ntop] = l; } } /* Check that calculated numtop is same as input. */ if (numtop != ntop) { fprintf(stderr,"Error, calculated numtop different from input.\n"); gmvexit(); } }
void getx3dnodedata() { /* */ /* Get and skip node field data. */ /* */ int i, j, ii; char name[40]; /* Read the header and loop through the number of data fields. */ if (x3dread_node_data_header() > 0) readx3d_error(filnam); for (ii = 0; ii < x3d_header.num_node_fields; ii++) { /* Read the node field, process nodeid, if any. */ if (x3d_read_node_data() > 0) readx3d_error(filnam); if (strcmp(x3d_node_data.name,"nodeid") == 0) { nodeids = (int *)malloc((nnodes+1)*sizeof(int)); if (nodeids == NULL) memerr(); nodeids[0] = 0; for (i = 0; i < nnodes; i++) nodeids[i+1] = (short)x3d_node_data.int_data[i]; nodeidflag = 1; } /* Free node field data. */ if (x3d_node_data.typedata == 0) free(x3d_node_data.int_data); else free(x3d_node_data.double_data); } /* Read the trailer. */ if (x3dread_node_data_header_end() > 0) readx3d_error(filnam); }
void bldmthr() { /* */ /* Build amr mother list from daughter list. */ /* */ int i, i1, n, pwr2; mother = (int *)malloc((numcell+1)*sizeof(int)); if (mother == NULL) memerr(); for (i = 0; i <= numcell; i++) mother[i] = 0; pwr2 = 2; for (i = 0; i < numdim - 1; i++) pwr2 *= 2; for (i = 1; i <= numcell; i++) { if (daughter[i] > 0) { i1 = daughter[i]; for (n = 0; n < pwr2; n++) mother[i1+n] = i; } } }
//TODO finish function lol_stmt* new_ifstmt(lol_expr* cond, lol_block* then, lol_expr** elsifs, lol_block** elsifthens, lol_block* elsestmts) { lol_if_stmt* if_stmt = malloc(sizeof(lol_if_stmt)); if (!if_stmt) { free(if_stmt); memerr(); } if_stmt->cond = cond; lol_stmt* stmt = malloc(sizeof(lol_stmt)); if (!stmt) { free(stmt); memerr(); } }
void getx3dghosts() { /* */ /* Get the ghost node. */ /* */ int i, j, *ghosts, nghosts; /* Read the header and loop through the data. */ if (x3dread_ghost_header() > 0) readx3d_error(filnam); if (x3d_header.numghosts > 0) { /* Save ghosts as a node group. */ nghosts = x3d_header.numghosts; ghosts = (int *)malloc(nghosts * sizeof(int)); /* Read a ghostline lines from the x3d file and add to ghosts. */ for (i = 0; i < x3d_header.numghosts; i++) { if (x3dread_ghosts() > 0) readx3d_error(filnam); ghosts[i] = x3d_ghost.node_no; } /* Save node group name. */ nodegrpname[nnodegrps] = (char *)malloc(33*sizeof(char)); if (nodegrpname[nnodegrps] == NULL) memerr(); strcpy(nodegrpname[nnodegrps],"ghosts"); /* Save the number and data for this node group. */ numnodegrp[nnodegrps] = nghosts; nodegrps[nnodegrps] = (int *)malloc(nghosts * sizeof(int)); if (nodegrps[nnodegrps] == NULL) memerr(); for (i = 0; i < nghosts; i++) nodegrps[nnodegrps][i] = ghosts[i]; nnodegrps++; free(ghosts); } /* Read the ghost trailer. */ if (x3dread_ghost_end() > 0) readx3d_error(filnam); }
static char* _elf_item(Elf *elf, Elf_Type type, size_t n, size_t off, int *flag) { Elf_Data src, dst; *flag = 0; elf_assert(n); elf_assert(valid_type(type)); if (off > elf->e_size) { seterr(ERROR_OUTSIDE); return NULL; } src.d_type = type; src.d_version = elf->e_version; src.d_size = n * _fsize(elf->e_class, src.d_version, type); elf_assert(src.d_size); if ((elf->e_size - off) < src.d_size) { seterr(truncerr(type)); return NULL; } dst.d_version = _elf_version; dst.d_size = n * _msize(elf->e_class, dst.d_version, type); elf_assert(dst.d_size); elf_assert(elf->e_data); if (elf->e_rawdata != elf->e_data && dst.d_size <= src.d_size) { dst.d_buf = elf->e_data + off; } else if (!(dst.d_buf = malloc(dst.d_size))) { seterr(memerr(type)); return NULL; } else { *flag = 1; } if (elf->e_rawdata) { src.d_buf = elf->e_rawdata + off; } else { src.d_buf = elf->e_data + off; } if (_elf_xlatetom(elf, &dst, &src)) { if (!*flag) { elf->e_cooked = 1; } return (char*)dst.d_buf; } if (*flag) { free(dst.d_buf); *flag = 0; } return NULL; }
lol_expr* new_dexpr(double d) { lol_expr* e = malloc(sizeof(lol_expr)); if (!e) { free(e); memerr(); } e->d = d; return e; }
lol_expr* new_lexpr(long l) { lol_expr* e = malloc(sizeof(lol_expr)); if (!e) { free(e); memerr(); } e->l = l; return e; }
lol_expr* new_sexpr(char* s) { lol_expr* e = malloc(sizeof(lol_expr)); if (!e) { free(e); memerr(); } e->s = s; return e; }
lol_expr* new_bexpr(bool b) { lol_expr* e = malloc(sizeof(lol_expr)); if (!e) { free(e); memerr(); } e->b = b; return e; }
lol_expr* new_biexpr(int op, lol_expr* left, lol_expr* right) { lol_expr* e = malloc(sizeof(lol_expr)); if (!e) { free(e); memerr(); } lol_binop_expr* binop = malloc(sizeof(lol_binop_expr)); if (!binop) { free(binop); free(e); memerr(); } binop->lhs = left; binop->rhs = right; binop->op = op; e->binop = binop; return e; }
void getx3dcelldata() { /* */ /* Get cell data, only save material ids. */ /* */ int i, j, ii; char name[40]; /* Read the header and loop through the number of data fields. */ if (x3dread_cell_data_header() > 0) readx3d_error(filnam); for (ii = 0; ii < x3d_header.num_cell_fields; ii++) { /* Read the cell field, process only the mat_ids. */ if (x3d_read_cell_data() > 0) readx3d_error(filnam); if (strcmp(x3d_cell_data.name,"matid") == 0) { cellmats=(short *)malloc((ncells)*sizeof(short)); if (cellmats == NULL) memerr(); for (i = 0; i < ncells; i++) cellmats[i] = (short)x3d_cell_data.int_data[i]; /* Check for invalid material data. */ matvalchk(ncells,mmats,0,cellmats); /* Calculate node materials. */ imat=(short *)malloc((nnodes+1)*sizeof(short)); if (imat == NULL) memerr(); bldnodemats(); } /* Free cell field data. */ if (x3d_cell_data.typedata == 0) free(x3d_cell_data.int_data); else free(x3d_cell_data.double_data); } /* Read the trailer. */ if (x3dread_cell_data_header_end() > 0) readx3d_error(filnam); }
static struct source * splitsource( /* divide source in two if it's big and long */ struct source *so ) { LRSUM lr; LRLIN fit; register struct srcspan *ss, *ssn; struct srcspan *ssl, *ssnl, head; int h; double mh, mv; struct source *sn; lrclear(&lr); for (ss = so->first; ss != NULL; ss = ss->next) for (h = ss->l; h < ss->r; h++) lrpoint(h, ss->v, &lr); if ((double)lr.n/(sampdens*sampdens) < SABIG) return(NULL); /* too small */ if (lrfit(&fit, &lr) < 0) return(NULL); /* can't fit a line */ if (fit.correlation < LCORR && fit.correlation > -LCORR) return(NULL); if (verbose) fprintf(stderr, "%s: splitting large source\n", progname); mh = lrxavg(&lr); mv = lryavg(&lr); sn = (struct source *)malloc(sizeof(struct source)); if (sn == NULL) memerr("source records in splitsource"); sn->dom = 0.0; sn->first = NULL; ssnl = NULL; head.next = so->first; ssl = &head; for (ss = so->first; ss != NULL; ssl = ss, ss = ss->next) if ((ssn = splitspan(ss, mh, mv, fit.slope)) != NULL) { if (ssn == ss) { /* remove from old */ ssl->next = ss->next; ss = ssl; } if (ssnl == NULL) /* add to new */ sn->first = ssn; else ssnl->next = ssn; ssn->next = NULL; ssnl = ssn; } so->first = head.next; return(sn); }
extern void analyze(void) /* analyze our scene */ { int h, v; int left; float *spanbr; spanbr = (float *)malloc((2*hsize+1)*sizeof(float)); if (spanbr == NULL) memerr("view span brightness buffer"); for (v = vsize; v >= -vsize; v--) { close_sources(v); #ifndef DEBUG if (verbose) { fprintf(stderr, "%s: analyzing... %3ld%%\r", progname, 100L*(vsize-v)/(2*vsize)); fflush(stderr); } #endif getviewspan(v, spanbr); left = hsize + 1; for (h = -hsize; h <= hsize; h++) { if (spanbr[h+hsize] < 0.0) { /* off view */ if (left < h) { addsrcspan(newspan(left,h,v,spanbr)); left = hsize + 1; } continue; } if (spanbr[h+hsize] > threshold) { /* in source */ if (left > h) left = h; } else { /* out of source */ if (left < h) { addsrcspan(newspan(left,h,v,spanbr)); left = hsize + 1; } addindirect(h, v, spanbr[h+hsize]); } } if (left < h) addsrcspan(newspan(left,h,v,spanbr)); } free((void *)spanbr); close_allsrcs(); }
static char* _elf_item(void *buf, Elf *elf, Elf_Type type, size_t off) { Elf_Data src, dst; elf_assert(valid_type(type)); if (off < 0 || off > elf->e_size) { seterr(ERROR_OUTSIDE); return NULL; } src.d_type = type; src.d_version = elf->e_version; src.d_size = _fsize(elf->e_class, src.d_version, type); elf_assert(src.d_size); if ((elf->e_size - off) < src.d_size) { seterr(truncerr(type)); return NULL; } dst.d_version = _elf_version; dst.d_size = _msize(elf->e_class, dst.d_version, type); elf_assert(dst.d_size); if (!(dst.d_buf = buf) && !(dst.d_buf = malloc(dst.d_size))) { seterr(memerr(type)); return NULL; } elf_assert(elf->e_data); if (elf->e_rawdata) { src.d_buf = elf->e_rawdata + off; } else { src.d_buf = elf->e_data + off; } if (_elf_xlatetom(elf, &dst, &src)) { return (char*)dst.d_buf; } if (dst.d_buf != buf) { free(dst.d_buf); } return NULL; }
void bldlvl() { /* */ /* Build cell level list. */ /* */ int level, lp, l; levcell = (int *)malloc((numcell+1)*sizeof(int)); if (levcell == NULL) memerr(); for (l = 0; l <= numcell; l++) levcell[l] = 0; for (level = 1; level <= numpack; level++) { for (lp = lopack[level]; lp <= hipack[level]; lp++) { l = lpoint[lp]; levcell[l] = level; } } }
static struct srcspan * newspan( /* allocate a new source span */ int l, int r, int v, float *sb ) { register struct srcspan *ss; register int i; ss = (struct srcspan *)malloc(sizeof(struct srcspan)); if (ss == NULL) memerr("source spans"); ss->l = l; ss->r = r; ss->v = v; ss->brsum = 0.0; for (i = l; i < r; i++) ss->brsum += sb[i+hsize]; return(ss); }
void aUpdateErrDisplay(Widget w, XEvent *event, String *params, Cardinal *count) { if (!XtIsRealized(errViewport)) return; XtVaGetValues(errViewport, XtNwidth, &width, NULL); XtVaGetValues(errViewport, XtNheight, &height, NULL); if (errPixmap) XFreePixmap(appDisplay, errPixmap); errPixmap= 0; errPixmap= XCreatePixmap(appDisplay, errWindow, width, height, appDepth); if (errPixmap == 0) { CloseErrDisplay(0, 0, 0); memerr("error Pixmap"); return; } XDefineCursor(appDisplay, errWindow, waitCursor); XFillRectangle(appDisplay, errPixmap, revGC, 0, 0, width, height); draw_error_display(); aPaintErrDisplay(0, 0, 0, 0); XUndefineCursor(appDisplay, errWindow); }
static void addsrcspan( /* add new source span to our list */ struct srcspan *nss ) { struct source *last, *cs, *this; register struct srcspan *ss; cs = NULL; for (this = curlist; this != NULL; this = this->next) { for (ss = this->first; ss != NULL; ss = ss->next) { if (!vcont(nss->v, ss->v)) break; if (hcont(ss, nss)) { if (cs == NULL) cs = this; else { last->next = this->next; mergesource(cs, this); this = last; } break; } } last = this; } if (cs == NULL) { cs = (struct source *)malloc(sizeof(struct source)); if (cs == NULL) memerr("source records"); cs->dom = 0.0; cs->first = NULL; cs->next = curlist; curlist = cs; } nss->next = cs->first; cs->first = nss; }
//prida barvu nebo ji najde na obrazovce; vraci index // tr.: adds colour or finds it on the screen; returns index //. Translate RGB to integers for x_setcolor(.) int RGB256(unsigned char r,unsigned char g,unsigned char b) { char *Palin[2]; int Npalin[2]; int *Mmapio[2]; int *mapio=NULL; char *Savecols=NULL; int npalout; char pal[3]; int ret; //nepamatuju si uz tuhle barvu nahodou ? (99% pripadu - jo!) // tr.: have'nt I seen this colour already before? // (in 99 percent of cases - yes) // if(!cgamode) // { ret=0; while(ret<16) { if(cacher[ret]==r && cacheg[ret]==g && cacheb[ret]==b ) return coloridx[ret]; ret++; } // } mapio=farmalloc(1024); Savecols=farmalloc(512); if(mapio==NULL || Savecols==NULL)memerr(); if(egamode) { pal[0]=egafilter(r>>2); pal[1]=egafilter(g>>2); pal[2]=egafilter(b>>2); } else if(vga16mode && !vgamono)
void getx3dBdyfiles() { /* */ /* Get data from Bdy files and save as node groups. */ /* */ int i, j, ii, ngrp, return_code, ifound, len, foundTag; //char dir_buf[sizeof(struct dirent) + 1024]; struct dirent *entry; DIR *dir; FILE *bdyfile; char bdyfilename[256], *tok, tagname[200], *ptr, tmpname[256]; /* Open the current directory. */ if ((dir = opendir(save_dir)) == NULL) { printf("Boundary file error, could not open directory %s\n",save_dir); return; } /* Loop through the files in the directory and get relevant Bdy files. */ while ((entry = readdir(dir)) != NULL) { /* Skip . and .. . */ ifound = 0; if ((strcmp(entry->d_name, ".")== 0) || (strcmp(entry->d_name, "..") == 0)) continue; /* Check that this file is a good Bdy file. */ ptr = strstr(entry->d_name,"Bdy"); if (ptr == NULL) continue; /* Check that prefix matches. */ len = strlen(x3dprefix); if (strncmp(entry->d_name,x3dprefix,len) != 0) continue; /* Check suffix, if any. */ ifound = 1; if (has_suffix) { ptr = (strstr(entry->d_name,x3dsuffix)); if (ptr == NULL) ifound = 0;; } if (ifound == 0) continue; /* Found a matching Bdy file. */ sprintf(bdyfilename,"%s%s",save_dir,entry->d_name); /* Extract the group name from the file name. */ sprintf(tmpname,"%s",entry->d_name); tok = strtok(entry->d_name,"."); foundTag = 0; while (!foundTag) { tok = strtok(NULL,"."); if (tok == NULL) break; if (strcmp(tok,"Bdy") == 0) foundTag = 1; else strcpy(tagname,tok); } /* Read the boundary data and save. */ if (x3d_read_Bdy_file(bdyfilename) > 0) readx3d_error(filnam); /* Save node group name and data, if any. */ if (x3d_Bdy_data.num_entries > 0) { printf("GMV read %d node group entries from Bdy file %s\n", x3d_Bdy_data.num_entries,tmpname); nodegrpname[nnodegrps] = (char *)malloc(33*sizeof(char)); if (nodegrpname[nnodegrps] == NULL) memerr(); strcpy(nodegrpname[nnodegrps],tagname); /* Save the number and data for this node group. */ ngrp = x3d_Bdy_data.num_entries; numnodegrp[nnodegrps] = ngrp; nodegrps[nnodegrps] = (int *)malloc((ngrp)*sizeof(int)); if (nodegrps[nnodegrps] == NULL) memerr(); for (i = 0; i < ngrp; i++) nodegrps[nnodegrps][i] = x3d_Bdy_data.int_data[i]; FREE(x3d_Bdy_data.int_data); nnodegrps++; } } closedir(dir); }
void getx3dRegfiles() { /* */ /* Get data from Reg files and save as cell groups */ /* or as material volume fraction subvar data. */ /* */ int i, j, ii, ngrp, nelem, ifound, len, matid, foundTag; double fmin, fmax, fillval, *vfield; struct dirent *entry; DIR *dir; FILE *regfile; char regfilename[256], *tok, tagname[200], *ptr, tmpname[256]; /* Open the current directory. */ if ((dir = opendir(save_dir)) == NULL) { printf("Reg file error, could not open directory %s\n",save_dir); return; } /* Loop through the files in the directory and get relevant Reg files. */ vfield = (double *)malloc(ncells * sizeof(double)); while ((entry = readdir(dir)) != NULL) { /* Skip . and .. . */ ifound = 0; if ((strcmp(entry->d_name, ".")== 0) || (strcmp(entry->d_name, "..") == 0)) continue; /* Check that this file is a good Reg file. */ ptr = strstr(entry->d_name,"Reg"); if (ptr == NULL) continue; /* Check that prefix matches. */ len = strlen(x3dprefix); if (strncmp(entry->d_name,x3dprefix,len) != 0) continue; /* Check suffix, if any. */ ifound = 1; if (has_suffix) { ptr = (strstr(entry->d_name,x3dsuffix)); if (ptr == NULL) ifound = 0;; } if (ifound == 0) continue; /* Found a matching Reg file. */ sprintf(regfilename,"%s%s",save_dir,entry->d_name); /* Extract the name from the file name. */ sprintf(tmpname,"%s",entry->d_name); tok = strtok(entry->d_name,"."); foundTag = 0; while (!foundTag) { tok = strtok(NULL,"."); if (tok == NULL) break; if (strcmp(tok,"Reg") == 0) foundTag = 1; else strcpy(tagname,tok); } /* Read the region data and save. */ if (x3d_read_Reg_file(regfilename) > 0) readx3d_error(filnam); /* Save cell group name or subvar name and data, if any. */ if (x3d_Reg_data.num_entries > 0) { if (x3d_Reg_data.type == 0) // Tag to group. { printf("GMV read %d cell group entries from Reg file %s\n", x3d_Reg_data.num_entries,tmpname); /* Save tagname. */ cellgrpname[ncellgrps] = (char *)malloc(33*sizeof(char)); if (cellgrpname[ncellgrps] == NULL) memerr(); strcpy(cellgrpname[ncellgrps],tagname); /* Save the number and data for this cell group. */ ngrp = x3d_Reg_data.num_entries; numcellgrp[ncellgrps] = ngrp; cellgrps[ncellgrps] = (int *)malloc((ngrp)*sizeof(int)); if (cellgrps[ncellgrps] == NULL) memerr(); for (i = 0; i < ngrp; i++) cellgrps[ncellgrps][i] = x3d_Reg_data.int_data[i]; FREE(x3d_Reg_data.int_data); ncellgrps++; } if (x3d_Reg_data.type == 1) // Mat vol. fraction to subvar. { printf("GMV read %d cell subvar entries from Reg file %s\n", x3d_Reg_data.num_entries,tmpname); /* Save subvar name. */ cfieldname[cnumvars]=(char *)malloc(33*sizeof(char)); if (cfieldname[cnumvars] == NULL) memerr(); strcpy(cfieldname[cnumvars],tagname); cvars_to_write[cnumvars] = 0; csubvarsin[cnumvars] = 1; /* If the material name does not exist, add it. */ matid = 0; for (i = 0; i < mmats; i++) { if (strcmp(tagname,mmatnames[i]) == 0) matid = i + 1; } if (matid == 0) { mmatnames[mmats] = (char *)malloc(mmats*33*sizeof(char)); strncpy(mmatnames[i],tagname,32); matid = mmats; mmats++; } /* Calculate min. and max. */ nelem = x3d_Reg_data.num_entries; fmin = MAXVAL; fmax = -MAXVAL; for (i = 0; i < nelem; i++) { fmin = (x3d_Reg_data.double_data[i] < fmin) ? x3d_Reg_data.double_data[i] : fmin; fmax = (x3d_Reg_data.double_data[i] > fmax) ? x3d_Reg_data.double_data[i] : fmax; } if (fmin >= 0.) fillval = 0; else fillval = -MAXVAL; /* Fill array and write. */ for (i = 0; i < ncells; i++) vfield[i] = fillval; for (i = 0; i < nelem; i++) { j = x3d_Reg_data.int_data[i] - 1; if (j >= 0 && j < ncells) vfield[j] = x3d_Reg_data.double_data[i]; else printf("Warning, subvars - cell id %d is out of range.\n", j+1); } writecellfld(vfield); cfieldmin[cnumvars] = fmin; cfieldmax[cnumvars] = fmax; cnumvars++; /* Replace cell materials with the current material */ /* if the volume fraction is greater than .5. */ /* Get the material id from the name. */ matid = 0; for (i = 0; i < mmats; i++) { if (strcmp(tagname,mmatnames[i]) == 0) matid = i + 1; } if (matid > 0) { for (i = 0; i < nelem; i++) { if (x3d_Reg_data.double_data[i] > .5) { j = x3d_Reg_data.int_data[i] - 1; cellmats[j] = matid; } } } FREE(x3d_Reg_data.int_data); FREE(x3d_Reg_data.double_data); } } } closedir(dir); free(vfield); }
//. Mix only the paletes on screen (visible) void MixVisiblePaletes(char writepal) { int pocet=0,i,celkpocet=0; int secidx[MAXPALMIX+1]; XSWAP atomadr[MAXPALMIX+1]; struct picinfo *dataptr; struct picinfo *obrazky; struct Url url; int maxpocet;//,frameID; XSWAP dummy1; unsigned dummy2; unsigned currentHTMLatom=p->firstHTMLatom,nextHTMLatom; struct HTMLframe *frame; struct HTMLrecord *imgatomptr; struct HTTPrecord HTTPdoc; // struct TMPframedata *htmldata; #ifdef POSIX maxpocet=MAXPALMIX; #else maxpocet=(int)((farcoreleft()-2000l)/(5500+sizeof(struct picinfo))); if(maxpocet>MAXPALMIX) maxpocet=MAXPALMIX; else if(maxpocet<1) maxpocet=1; #endif obrazky=farmalloc(sizeof(struct picinfo)*maxpocet); if(!obrazky)memerr(); // while(HTMLdoc.cur<HTMLdoc.len) while(currentHTMLatom!=IE_NULL) { kbhit(); imgatomptr=(struct HTMLrecord *)ie_getswap(currentHTMLatom); if(!imgatomptr) MALLOCERR(); nextHTMLatom=imgatomptr->next; frame=&(p->htmlframe[imgatomptr->frameID]); if( (imgatomptr->type==IMG || imgatomptr->type==TD_BACKGROUND) && (imgatomptr->y>=frame->posY || imgatomptr->yy>=frame->posY) && (imgatomptr->y<frame->posY+frame->scroll.ysize || imgatomptr->yy<frame->posY+frame->scroll.ysize) && (imgatomptr->x>=frame->posX || imgatomptr->xx>=frame->posX) && (imgatomptr->x<frame->posX+frame->scroll.xsize || imgatomptr->xx<frame->posX+frame->scroll.xsize) || imgatomptr->type==BACKGROUND) { // idx[celkpocet]=HTMLdoc.cur; atomadr[celkpocet]=currentHTMLatom; dataptr=(struct picinfo *)ie_getswap(imgatomptr->ptr); if(dataptr) { dataptr->palismap=0; swapmod=1; if(celkpocet<MAXPALMIX) { memcpy(&obrazky[pocet],dataptr,sizeof(struct picinfo)); AnalyseURL(obrazky[pocet].URL,&url,IGNORE_PARENT_FRAME); if(SearchInCache(&url,&HTTPdoc,&dummy1,&dummy2)) { i=0; while(i<pocet) { if(!strcmp(obrazky[i].filename,HTTPdoc.locname)) { secidx[celkpocet++]=i; goto nextobrazek; } i++; } //pocitaji se jenom GIFy a bitmapy: // tr.: only GIFs and bitmaps are counted/calculated if(HTTPdoc.locname[0] && (strstr(HTTPdoc.locname,".GIF") || strstr(HTTPdoc.locname,".BMP"))) { strcpy(obrazky[pocet].filename,HTTPdoc.locname); secidx[celkpocet++]=pocet; //u prvniho obrazku 0, u druheho 1; // tr.: at the first picture 0, at the second 1 pocet++; } if(pocet==maxpocet) goto allobrazky; } } else goto allobrazky; }//endif nasel sem neco (tr.: endif I found something) else MALLOCERR(); } nextobrazek: // HTMLdoc.cur++; currentHTMLatom=nextHTMLatom; } allobrazky: i=0; if(celkpocet>0) { if(!MixPal(obrazky,pocet,writepal)) //zkusim smichat palety... // tr.: I try to mix the palette goto ven; while(i<celkpocet) { //zrychlena verze (tr.: accelerated version) imgatomptr=(struct HTMLrecord *)ie_getswap(atomadr[i]); if(imgatomptr && (imgatomptr->type==IMG || imgatomptr->type==BACKGROUND || imgatomptr->type==TD_BACKGROUND)) { dataptr=(struct picinfo *)ie_getswap(imgatomptr->ptr); if(dataptr) { int resize_x=dataptr->resize_x; int resize_y=dataptr->resize_y; memcpy(dataptr,&obrazky[secidx[i]],sizeof(struct picinfo)); dataptr->resize_x=resize_x; dataptr->resize_y=resize_y; swapmod=1; } else MALLOCERR(); } else MALLOCERR(); i++; } } ven: if(obrazky) farfree(obrazky); }
int MixPal(struct picinfo *o, int n, char writepal) { char *Palin[MAXPALMIX+1]; int Npalin[MAXPALMIX+1]; int Idxtolist[MAXPALMIX+1]; int *Mmapio[MAXPALMIX+1]; int *mapio=NULL; char *Savecols=NULL; int npalout=0,npic=1,i=0; char str[80]; int rv=1; //int Uloz; (tr.: Save it) sprintf(str,MSG_LDPAL,n ); outs(str); while(npic<MAXPALMIX && i<n) { o[i].palonly=1; o[i].sizeonly=0; if(drawanyimage(&o[i])==1) { npalout+=o[i].npal; Palin[npic]=(char *)o[i].pal; Npalin[npic]=o[i].npal; Idxtolist[npic]=i; npic++; } i++; } if(npic<2) return 0; sprintf(str,MSG_MIXPAL,npalout ); outs(str); mapio=farmalloc(512+2*npalout); Savecols=farmalloc(256+npalout); if(mapio==NULL || Savecols==NULL)memerr(); if(x_getmaxcol()==1)IiNpal=2; else IiNpal=16; Palin[0]= Iipal; Npalin[0]= IiNpal; memset( Savecols, 0, IiNpal+npalout); memset( Savecols, 1, IiNpal); npalout=x_getmaxcol()+1;//256 i=PresspalO (npic, Palin, Npalin, Iipal, &npalout, mapio, Mmapio, 2, 1, 2, Savecols ); if(i!=1) { Piip(); rv=0; goto out; } if(writepal) safemappal(npalout); else IiNpal=npalout; i=1; while(i<npic) { memcpy(o[Idxtolist[i]].pal,Mmapio[i],Npalin[i]*2); o[Idxtolist[i]].palismap=1; i++; } out: if(Savecols)farfree(Savecols); if(mapio)farfree(mapio); return rv; }
// ------------- haro ----------------------- int drawGIF(struct picinfo *gif) { //picinfo (in/out) - jmeno gifu a kam nakreslit //hPicinfo(in) - handle picinfo (jen pro anim.gify) //ModPic (out)- zda uchovat zmeny v picinfo // char drive[8], dir[80], name[16], ext[8]; // char *ext; struct GIFGLB GifGlb; struct GIFIMG GifImg; int ist, mark, interlac, NumImg = 0; int *mapio=NULL, *Mmapio[2]; int i, ire=1, TraCol, Prepni; unsigned int backg,code_start; int Transp = 0, GrCtrl = 0; short int tAnim = 0; // cas animace v setinach vteriny int disp = 0; // dispozice int ReaPal = 0; // zda se cetla local paleta int GlbPal = 0; // zda se cetla globalni paleta #ifdef XANIMGIF int vdx = 0, vdy = 0; #endif long CurPos, ReaBytes, BckAdr; char *pAktpal; unsigned char *pLocPal=NULL; // lolalni gif paleta int nLocPal=0; // delka loc. palety int bSaveBck1=0,bSaveBck2=0;// zda ukladat background pro animovane //*ModPic = 0; nahrazeno swapmod !! //basebackg=-1; //mp!! // fnsplit(gif->filename, drive, dir, name, ext); /* //no longer needed - drawGIF is always called from drawanyimage() ext=strrchr(gif->filename,'.'); if(ext && strcmpi(ext,".bmp") == 0) { // Kresli bmp ist = XCHdrawBMP(gif); return( ist ); } */ /* #ifdef XANIMGIF if(gif->palonly==0 && gif->sizeonly==0 && gif->IsInXms) { // Uz je v xms -> krelit z ni ist = XGifFromXms(gif, xg_video_XMS, vdx, vdy, &tAnim); return( ist ); } #endif */ New_read_gif: NumImg = 0; ist = ReadGHeader(gif, &GifGlb, &GlbPal); if((ist&1) == 0) //failure { /*ire = ist*/ a_close (filx); if(ist==6) //mp: kdyz to neni GIF, zkusit BMP return XCHdrawBMP(gif); else return 0; } pLocPal = farmalloc(768); // Buf. na lokalni palety if(pLocPal==NULL) memerr(); g_DrawFce = DrawLine; // fce volana uvnitr dekomprese /** #ifdef XANIMGIF if(ReaPal && gif->palonly==0 && gif->sizeonly==0) { if(pGifPal==NULL) { pGifPal = farmalloc(768); if(pGifPal==NULL) memerr(); } memcpy(pGifPal, gif->pal, 3*gif->npal); // jen pro anim nGifPal = gif->npal; // schovat glb. paletu gifu for(i=0; i<3*nGifPal; i++) pGifPal[i]>>=2; } #endif **/ // Hlavni cykl pres jednotlive bloky v GIFu Next_marker: mark = ReadGMarker(); if(mark < 0) { /*ire = 8*/; //ERRGIF if(NumImg == 0) goto Only_sp; else if(NumImg == 1) { ire = 1; //mp! goto Err_frame; } else { ire = 1; goto Add_to_anim; } //ERRGIF } else if(mark == 1) // Ext. blok { ist = ReadExtBlok(&Transp, &backg, &tAnim, &disp, &GrCtrl); if((ist&1)==0) { /*ire = 10*/ ire=1; goto Err_frame; } //mp! if(GrCtrl) // Cetlo se grf. ext. { //if(backg >= gif->npal) backg = gif->npal-1; // Toto se vyskytuje ?? gif->bgindex = backg; // back, nebo transp index swapmod = 1; // mark XSWAP as modified //RGB001 if(Transp && !gif->palismap && GlbPal) { gif->pal[3*backg ] = g_rT; gif->pal[3*backg+1] = g_gT; gif->pal[3*backg+2] = g_bT; } } } else if(mark == 2) // Img. blok { ist = ReadImgBlok(gif, &GifImg, &interlac, &code_start, GlbPal, &ReaPal, &nLocPal, pLocPal); if((ist&1)==0) { /*ire = 12*/ ire=1; goto Err_frame; } //mp! if(gif->sizeonly){ ire = 1; goto Only_sp; } //SaveBck if( Transp ) { //RGB001 if(ReaPal==0 && GlbPal) //!!mp ReaPal=1; //!!mp if(ReaPal ==1) { gif->pal[3*backg ] = g_rT; gif->pal[3*backg+1] = g_gT; gif->pal[3*backg+2] = g_bT; } else if(ReaPal == 2) { pLocPal[3*backg ] = g_rT; pLocPal[3*backg+1] = g_gT; pLocPal[3*backg+2] = g_bT; } //if(GifImg.screex2==GifGlb.screenwide && GifImg.screey2==GifGlb.screendeep) if(disp >= 2) bSaveBck1++; // max. velikost s transp else bSaveBck2++; // mensi nez. cele s transp } if(!gif->palismap || gif->palonly || xg_256==MM_Hic) { gif->palismap=0; if(gif->palonly) { if(GlbPal == 0) // Zadna globalni->vezmu lokalni { //mp!!begin //specialni filtry pro 16ti barevne mody if(egamode) for(i=0; i<3*gif->npal; i++) gif->pal[i]=egafilter(gif->pal[i]); else if(vga16mode && !vgamono) for(i=0; i<3*gif->npal; i++) gif->pal[i]=vgafilter(gif->pal[i]); else //mp!!end memcpy(gif->pal, pLocPal, 3*nLocPal); gif->npal = nLocPal; } ire = 1; goto Only_sp; } } CurPos = a_lseek(filx, 0, SEEK_CUR); // Pripravit scaling (bakground NE, pripadne vzdy zakazat),nebo oriznuti //if(NumImg == 0) { ist = PrepareScaleAndX(gif, NumImg, &Prepni); if(ist == 0) { ire = 1; goto Err_frame; } // gif mimo scr //if(Prepni) goto Prepni; -> kreslit prvni frame gifu -> zacykleni } if(xg_256 != MM_Hic) // * * * * * * * * * * * * * 16 color, 256 color { // Kde vezmu Mmapio ?? if(gif->palismap && ReaPal != 2) // uz mam mapu a img mema loc.paletu { Mmapio[1]=(int *)(gif->pal); } else // musim ji vyrobit { mapio=farmalloc(1024*sizeof(int)); if(!mapio) return(2); ist = PalForPaleteModes(gif, mapio, Mmapio, ReaPal, nLocPal, pLocPal); if(ist != 1) { /*ire=ist*/ ire=1; goto Err_frame;} //mp! } #ifdef XANIMGIF if(g_gifDrawXms == 0) { TraCol = IndexToTranspCol((int)gif->bgindex, (char*)Mmapio[1]); ist = XInitImgXms(gif, NumImg, Transp, TraCol, tAnim, disp); if(ist != 1) // Neni misto v XMS : prepnout na screen a znovu { goto Prepni; } } #endif ire = draw_gif(gif,code_start,interlac,Transp,Mmapio[1],NULL, &ReaBytes); if(mapio) { farfree(mapio); mapio = 0; } } else // * * * * * * * * * * * * * * * * * * * * * * * * * * * Hicolor... { if( ReaPal==2 ) pAktpal = (char *) pLocPal; else pAktpal = (char *)gif->pal; #ifdef XANIMGIF if(g_gifDrawXms == 0) { TraCol = IndexToTranspCol((int)gif->bgindex, pAktpal); ist = XInitImgXms(gif, NumImg, Transp, TraCol, tAnim, disp); if(ist != 1) // neni XMS ? { Prepni: a_close(filx); goto New_read_gif; } } #endif ire = draw_gif(gif,code_start,interlac,Transp,NULL,pAktpal, &ReaBytes); } ReaPal = 0; if(ire != 1) // chyby pri dekompresi gifu { ire = 1; // v NumImg je pocet OK framu //mp! // if(NumImg > 0) { goto Err_frame; } // else // { goto Only_sp; // } } NumImg++; CurPos = a_lseek(filx, CurPos+ReaBytes, SEEK_SET); disp = 0; //i=getch(); if(g_gifDrawXms == 1 && NumImg > 0) { // Nakreslit vzdy jen prvni ire = 1; goto Only_sp; } } else if(mark == 0) // OK end file { //char Msg[80]; //sprintf(Msg,"EndGIFN: DrawXMS=%d NumImg=%d",g_gifDrawXms,NumImg); //outs(Msg); //getch(); //ERRGIF Add_to_anim: #ifdef XANIMGIF if(NumImg > 1 && g_gifDrawXms == 0) // pridat k animovanym { if(g_NumAnim < MAX_ANIMATEGIF) { g_TableAnim[g_NumAnim].hPicInf = gif->hPicInfo; // ??? g_TableAnim[g_NumAnim].NextAnim = 0; // Cas pristi animace // SaveBck : Ulozit background ? if(bSaveBck1 /*&& bSaveBck2*/) { ist = XSaveBackToXMS(gif, GifGlb.screenwide, GifGlb.screendeep, &BckAdr); if(ist==0) { if(BckAdr) XGifFreeXMS(); // uvolnit alloc. XMS pro pozadi } else { gif->BckImg = BckAdr; swapmod = 1; } } g_NumAnim++; //outs("Add To Anim"); } } Err_frame: if(NumImg == 0) goto Only_sp; if(g_gifDrawXms == 0 && gif->IsInXms) // prvni obrazek z gifu v XMS { ist = XGifFromXms(gif, xg_video_XMS, vdx, vdy, &tAnim); // Je-li jeden frame, pak ho uvolnit if(NumImg == 1) { XGifFreeXMS(); } } #else Err_frame: ire = 1; #endif goto Only_sp; } goto Next_marker; Only_sp: if(pLocPal) farfree(pLocPal); a_close(filx); return( ire ); }
void generateprt(void) { unsigned currentHTMLatom; struct ib_editor txt; int x,y,l,k; char radka[IE_MAXLEN+2], *ptr; struct HTMLrecord *atomptr; /* printf("Console width is: %d\n\n",CONSOLEWIDTH); #ifndef CLEMENTINE fflush(stdout); #endif*/ ie_openf_lim(&txt,CONTEXT_TMP,MAXLINES2PRINT); strcpy(txt.filename,PRINTFNAME); currentHTMLatom=p->firstHTMLatom; while(currentHTMLatom!=IE_NULL) { // kbhit(); atomptr=(struct HTMLrecord *)ie_getswap(currentHTMLatom); if(!atomptr) MALLOCERR(); currentHTMLatom=atomptr->next; if(atomptr->frameID==p->activeframe && atomptr->type==TEXT || atomptr->type==HR || atomptr->type==LI || atomptr->type==TD_BACKGROUND || (atomptr->type==TD) && (atomptr->data1 || atomptr->data2) || (atomptr->type==TABLE) && (atomptr->data1) || atomptr->type==INPUT && (atomptr->data1==TEXT || atomptr->data1==SUBMIT || atomptr->data1==BUTTON)) { x=atomptr->x/FIXEDFONTX; y=(int)((atomptr->y+FIXEDFONTY/2-1)/FIXEDFONTY); if(x>=0 && x<IE_MAXLEN && y>=0 && y<MAXLINES2PRINT) { if(atomptr->type==LI) { if(x>0)x--; virtualtextdraw(&txt,x,y,"*",1); } else if(atomptr->type==HR || atomptr->type==TD || atomptr->type==TD_BACKGROUND || atomptr->type==TABLE) { char z='-'; int yy; if(atomptr->type==HR) z='='; l=atomptr->xx/FIXEDFONTX-x; if(l<0)l=0; if(l>IE_MAXLEN)l=IE_MAXLEN; radka[l]='\0'; k=l; while(--k>=0) radka[k]=z; if(atomptr->type==TD || atomptr->type==TD_BACKGROUND) { strcat(radka,"+-"); yy=(int)((atomptr->yy+FIXEDFONTY/2)/FIXEDFONTY); while(y<yy) virtualtextdraw(&txt,x+l,y++,"|",1); l++; //+- } else if(atomptr->type==TABLE) { radka[0]=','; yy=(int)(atomptr->yy/FIXEDFONTY)-1; while(yy>y) virtualtextdraw(&txt,x,yy--,"|",1); l--; } virtualtextdraw(&txt,x,y,radka,l); } else { if(atomptr->type==INPUT) { editorptr=(struct ib_editor *)ie_getswap(atomptr->ptr); if(editorptr) { memcpy(&tmpeditor,editorptr,sizeof(struct ib_editor)); ptr=ie_getline(&tmpeditor,0); } else ptr=NULL; } else ptr=ie_getswap(atomptr->ptr); if(!ptr) memerr(); l=strlen(ptr); if(l+x>IE_MAXLEN)l=IE_MAXLEN-x; if(l<0)l=0; makestr(radka,ptr,l); virtualtextdraw(&txt,x,y,radka,l); } } } } if(txt.lines+4<txt.maxlines) { int l; ie_insline(&txt,0,""); memset(radka,'_',CONSOLEWIDTH); radka[CONSOLEWIDTH]='\0'; ie_insline(&txt,0,radka); memset(radka,' ',CONSOLEWIDTH); l=strlen(p->htmlframe[p->activeframe].cacheitem.URL); if(l>CONSOLEWIDTH-28) { l=CONSOLEWIDTH-28; strncpy(&radka[CONSOLEWIDTH-28],"...",3); } strncpy(radka,p->htmlframe[p->activeframe].cacheitem.URL,l); inettime(&radka[CONSOLEWIDTH-25]); ie_insline(&txt,0,radka); memset(radka,' ',CONSOLEWIDTH); l=strlen(arachne.title); if(l>CONSOLEWIDTH-16) { l=CONSOLEWIDTH-16; strncpy(&radka[CONSOLEWIDTH-16],"...",3); } strncpy(radka,arachne.title,l); //!!glennmcc: Jan 26, 2005 -- keep aligned regardless of changes to VER //also added lowercase 'v' in front of version number sprintf(&radka[CONSOLEWIDTH-(9+strlen(VER))],"Arachne v%s",VER); // sprintf(&radka[CONSOLEWIDTH-17],"Arachne %s",VER); //!!glennmcc: end ie_insline(&txt,0,radka); } { char ffeed[2]=" "; ffeed[0]=12; //form feed? ie_insline(&txt,txt.lines,ffeed); } ie_savef(&txt); ie_killcontext(CONTEXT_TMP); //clear temporary file // ie_closef(&txt); }
static int _elf_cook_phdr(Elf *elf) { size_t num, off, entsz; if (elf->e_class == ELFCLASS32) { num = ((Elf32_Ehdr*)elf->e_ehdr)->e_phnum; off = ((Elf32_Ehdr*)elf->e_ehdr)->e_phoff; entsz = ((Elf32_Ehdr*)elf->e_ehdr)->e_phentsize; } #if __LIBELF64 else if (elf->e_class == ELFCLASS64) { num = ((Elf64_Ehdr*)elf->e_ehdr)->e_phnum; off = ((Elf64_Ehdr*)elf->e_ehdr)->e_phoff; entsz = ((Elf64_Ehdr*)elf->e_ehdr)->e_phentsize; /* * Check for overflow on 32-bit systems */ if (overflow(off, ((Elf64_Ehdr*)elf->e_ehdr)->e_phoff, Elf64_Off)) { seterr(ERROR_OUTSIDE); return 0; } } #endif /* __LIBELF64 */ else { seterr(ERROR_UNIMPLEMENTED); return 0; } if (off) { Elf_Scn *scn; size_t size; unsigned i; char *p; if (num == PN_XNUM) { /* * Overflow in ehdr->e_phnum. * Get real value from first SHDR. */ if (!(scn = elf->e_scn_1)) { seterr(ERROR_NOSUCHSCN); return 0; } if (elf->e_class == ELFCLASS32) { num = scn->s_shdr32.sh_info; } #if __LIBELF64 else if (elf->e_class == ELFCLASS64) { num = scn->s_shdr64.sh_info; } #endif /* __LIBELF64 */ /* we already had this else { seterr(ERROR_UNIMPLEMENTED); return 0; } */ } size = _fsize(elf->e_class, elf->e_version, ELF_T_PHDR); elf_assert(size); #if ENABLE_EXTENDED_FORMAT if (entsz < size) { #else /* ENABLE_EXTENDED_FORMAT */ if (entsz != size) { #endif /* ENABLE_EXTENDED_FORMAT */ seterr(ERROR_EHDR_PHENTSIZE); return 0; } size = _msize(elf->e_class, _elf_version, ELF_T_PHDR); elf_assert(size); if (!(p = malloc(num * size))) { seterr(memerr(ELF_T_PHDR)); return 0; } for (i = 0; i < num; i++) { if (!_elf_item(p + i * size, elf, ELF_T_PHDR, off + i * entsz)) { free(p); return 0; } } elf->e_phdr = p; elf->e_phnum = num; } return 1; } static int _elf_cook_shdr(Elf *elf) { size_t num, off, entsz; if (elf->e_class == ELFCLASS32) { num = ((Elf32_Ehdr*)elf->e_ehdr)->e_shnum; off = ((Elf32_Ehdr*)elf->e_ehdr)->e_shoff; entsz = ((Elf32_Ehdr*)elf->e_ehdr)->e_shentsize; } #if __LIBELF64 else if (elf->e_class == ELFCLASS64) { num = ((Elf64_Ehdr*)elf->e_ehdr)->e_shnum; off = ((Elf64_Ehdr*)elf->e_ehdr)->e_shoff; entsz = ((Elf64_Ehdr*)elf->e_ehdr)->e_shentsize; /* * Check for overflow on 32-bit systems */ if (overflow(off, ((Elf64_Ehdr*)elf->e_ehdr)->e_shoff, Elf64_Off)) { seterr(ERROR_OUTSIDE); return 0; } } #endif /* __LIBELF64 */ else { seterr(ERROR_UNIMPLEMENTED); return 0; } if (off) { struct tmp { Elf_Scn scn; Scn_Data data; } *head; Elf_Data src, dst; Elf_Scn *scn; Scn_Data *sd; unsigned i; if (off < 0 || off > elf->e_size) { seterr(ERROR_OUTSIDE); return 0; } src.d_type = ELF_T_SHDR; src.d_version = elf->e_version; src.d_size = _fsize(elf->e_class, src.d_version, ELF_T_SHDR); elf_assert(src.d_size); #if ENABLE_EXTENDED_FORMAT if (entsz < src.d_size) { #else /* ENABLE_EXTENDED_FORMAT */ if (entsz != src.d_size) { #endif /* ENABLE_EXTENDED_FORMAT */ seterr(ERROR_EHDR_SHENTSIZE); return 0; } dst.d_version = EV_CURRENT; if (num == 0) { union { Elf32_Shdr sh32; #if __LIBELF64 Elf64_Shdr sh64; #endif /* __LIBELF64 */ } u; /* * Overflow in ehdr->e_shnum. * Get real value from first SHDR. */ if (elf->e_size - off < entsz) { seterr(ERROR_TRUNC_SHDR); return 0; } if (elf->e_rawdata) { src.d_buf = elf->e_rawdata + off; } else { src.d_buf = elf->e_data + off; } dst.d_buf = &u; dst.d_size = sizeof(u); if (!_elf_xlatetom(elf, &dst, &src)) { return 0; } elf_assert(dst.d_size == _msize(elf->e_class, EV_CURRENT, ELF_T_SHDR)); elf_assert(dst.d_type == ELF_T_SHDR); if (elf->e_class == ELFCLASS32) { num = u.sh32.sh_size; } #if __LIBELF64 else if (elf->e_class == ELFCLASS64) { num = u.sh64.sh_size; /* * Check for overflow on 32-bit systems */ if (overflow(num, u.sh64.sh_size, Elf64_Xword)) { seterr(ERROR_OUTSIDE); return 0; } } #endif /* __LIBELF64 */ } if ((elf->e_size - off) / entsz < num) { seterr(ERROR_TRUNC_SHDR); return 0; } if (!(head = (struct tmp*)malloc(num * sizeof(struct tmp)))) { seterr(ERROR_MEM_SCN); return 0; } for (scn = NULL, i = num; i-- > 0; ) { head[i].scn = _elf_scn_init; head[i].data = _elf_data_init; head[i].scn.s_link = scn; if (!scn) { elf->e_scn_n = &head[i].scn; } scn = &head[i].scn; sd = &head[i].data; if (elf->e_rawdata) { src.d_buf = elf->e_rawdata + off + i * entsz; } else { src.d_buf = elf->e_data + off + i * entsz; } dst.d_buf = &scn->s_uhdr; dst.d_size = sizeof(scn->s_uhdr); if (!_elf_xlatetom(elf, &dst, &src)) { elf->e_scn_n = NULL; free(head); return 0; } elf_assert(dst.d_size == _msize(elf->e_class, EV_CURRENT, ELF_T_SHDR)); elf_assert(dst.d_type == ELF_T_SHDR); scn->s_elf = elf; scn->s_index = i; scn->s_data_1 = sd; scn->s_data_n = sd; sd->sd_scn = scn; if (elf->e_class == ELFCLASS32) { Elf32_Shdr *shdr = &scn->s_shdr32; scn->s_type = shdr->sh_type; scn->s_size = shdr->sh_size; scn->s_offset = shdr->sh_offset; sd->sd_data.d_align = shdr->sh_addralign; sd->sd_data.d_type = _elf_scn_type(scn->s_type); } #if __LIBELF64 else if (elf->e_class == ELFCLASS64) { Elf64_Shdr *shdr = &scn->s_shdr64; scn->s_type = shdr->sh_type; scn->s_size = shdr->sh_size; scn->s_offset = shdr->sh_offset; sd->sd_data.d_align = shdr->sh_addralign; /* * Check for overflow on 32-bit systems */ if (overflow(scn->s_size, shdr->sh_size, Elf64_Xword) || overflow(scn->s_offset, shdr->sh_offset, Elf64_Off) || overflow(sd->sd_data.d_align, shdr->sh_addralign, Elf64_Xword)) { seterr(ERROR_OUTSIDE); return 0; } sd->sd_data.d_type = _elf_scn_type(scn->s_type); /* * QUIRKS MODE: * * Some 64-bit architectures use 64-bit entries in the * .hash section. This violates the ELF standard, and * should be fixed. It's mostly harmless as long as the * binary and the machine running your program have the * same byte order, but you're in trouble if they don't, * and if the entry size is wrong. * * As a workaround, I let libelf guess the right size * for the binary. This relies pretty much on the fact * that the binary provides correct data in the section * headers. If it doesn't, it's probably broken anyway. * Therefore, libelf uses a standard conforming value * when it's not absolutely sure. */ if (scn->s_type == SHT_HASH) { int override = 0; /* * sh_entsize must reflect the entry size */ if (shdr->sh_entsize == ELF64_FSZ_ADDR) { override++; } /* * sh_size must be a multiple of sh_entsize */ if (shdr->sh_size % ELF64_FSZ_ADDR == 0) { override++; } /* * There must be room for at least 2 entries */ if (shdr->sh_size >= 2 * ELF64_FSZ_ADDR) { override++; } /* * sh_addralign must be correctly set */ if (shdr->sh_addralign == ELF64_FSZ_ADDR) { override++; } /* * The section must be properly aligned */ if (shdr->sh_offset % ELF64_FSZ_ADDR == 0) { override++; } /* XXX: also look at the data? */ /* * Make a conservative decision... */ if (override >= 5) { sd->sd_data.d_type = ELF_T_ADDR; } } /* * END QUIRKS MODE. */ } #endif /* __LIBELF64 */ /* we already had this else { seterr(ERROR_UNIMPLEMENTED); return 0; } */ sd->sd_data.d_size = scn->s_size; sd->sd_data.d_version = _elf_version; } elf_assert(scn == &head[0].scn); elf->e_scn_1 = &head[0].scn; head[0].scn.s_freeme = 1; }