bool DocDir::LoadLinks(const String& package) { String f = LoadFile(DocFile(package, "links")); CParser p(f); ArrayMap<DocKey, Entry>& pk = dir.GetAdd(package); try { while(!p.IsEof()) { if(p.Id("LINK")) { DocKey key; p.PassChar('('); if(!ReadCode(p, key)) return false; p.PassChar(','); String link = p.ReadString(); p.PassChar(')'); Entry& e = pk.GetAdd(key); e.text = link; e.type = LINK; } if(p.Id("IGNORED")) { DocKey key; p.PassChar('('); if(!ReadCode(p, key)) return false; p.PassChar(')'); Entry& e = pk.GetAdd(key); e.text.Clear(); e.type = IGNORED; } } return true; } catch(CParser::Error) { return false; } }
void Scan(void) { char code[20]; char name[20]; FILE *file; time_t mytime; mytime = time(NULL); while(1) { ReadCode(code); GetName(name, code); time(&mytime); // printf("FILE OPENED\n"); printf("\n%s\t\t%s", name, ctime(&mytime)); file = fopen("LOG", "a+"); fprintf(file, "%s\t\t%s", name, ctime(&mytime)); fclose(file); } }
int HVS_B0307::ReadVoltage(__u16* v) { __u16 code; int rc=ReadCode(&code); if ( rc&CAMAC_CC_ERRORS ) return rc; *v = Voltage(code); return rc; }
int HVS_B0307::ReadOverloadValue(int &overload_code) { __u16 code; int rc=ReadCode(&code); if ( rc&CAMAC_CC_ERRORS ) return rc; overload_code=(code&OverloadMask)>>12; return rc; }
int HVS_B0307::ReadOverload(bool* over) { __u16 code; int rc=ReadCode(&code); if ( rc&CAMAC_CC_ERRORS ) return rc; *over = Overload(code); return rc; }
int HVS_B0307::ReadState(bool* s, bool* over) { __u16 code; int rc=ReadCode(&code); if ( rc&CAMAC_CC_ERRORS ) return rc; *s = On(code); if ( over ) *over = Overload(code); return rc; }
int DocDir::ReadDocHeader(const char *filename, DocKey& key) { FileIn in(filename); if(!in) return -1; String l = in.GetLine(); CParser p(l); if(!p.Id("ITEM") || !p.Char('(') || !ReadCode(p, key)) return -1; p.Char(')'); if(p.Id("EXTERNAL")) return EXTERNAL; return NORMAL; }
void NewUser(void) { FILE *file; char name[100]; char code[100]; if(users != NULL) free(users); FillUsers(); do { printf("USERNAME: "******"%s", name); }while(CheckName(name)); do { ReadCode(code); printf("CODE: %s\n", code); if(CheckCode(code)) printf("Code is already in use!\n"); else { printf("Done.\n"); break; } }while(1); if((file = fopen("USERS", "a+")) == NULL) { perror("Failed to open file!\n"); exit(1); } else { fprintf(file, "%s\t\t%s\r\n", name, code); fclose(file); } }
String DocDir::GetText(const DocKey& key) const { String s = LoadFile(GetFilePath(key)); if(s.IsEmpty()) return Null; CParser p(s); DocKey dummy; if(!p.Id("ITEM") || !p.Char('(') || !ReadCode(p, dummy)) return "Invalid file"; p.Char(')'); p.Id("EXTERNAL"); String text; while(p.Id("TEXT")) { p.Char('('); if(p.IsString()) text.Cat(p.ReadString()); else return "Invalid file"; p.Char(')'); } return p.Id("END_ITEM") ? text : "Invalid file"; }
int Shader::CreateShaderObj(const char* filename, int shaderType, GLuint* shaderId){ char *code = NULL; int rc = 0; // create a shader handle *shaderId = glCreateShader(shaderType); // read the code code = ReadCode(filename); if (code == NULL) return(-1); // attach the source to the shaders const char* _code = code; glShaderSource(*shaderId, 1, &_code, NULL); // compile the code glCompileShader(*shaderId); // check for errors glGetShaderiv(*shaderId, GL_COMPILE_STATUS, &rc); if (rc != GL_TRUE) { GLsizei length; GLsizei bufSize = 0; char *error = NULL; fprintf(stderr, "Error when creating a shader \n"); glGetShaderiv(*shaderId, GL_INFO_LOG_LENGTH, &bufSize); error = (char *)malloc(bufSize*sizeof(char) + 1); if (error != NULL) { glGetShaderInfoLog(*shaderId, bufSize, &length, error); fprintf(stderr, "%s \n", error); } if (error != NULL) free(error); rc = -1; } else rc = 0; if (code != NULL) free(code); // free the source code of the shader return(rc); }
void Interpret(int argc, char *argv[]) { printf(" Loading and Resolving References ...\n"); InitializeInterpreter(argc, argv); CodeFile = fopen(CodeFileName,"r"); if (CodeFile == NULL) printf("%s NOT FOUND. \n",CodeFileName); else { ReadCode(CodeFile); if (ResolveReferences()) /*Mini Loader Here*/ { printf(" Begin Execution:\n"); Execute(); printf(" End Execution.\n"); } else printf("Execution Inhibited.\n"); } }
/*************************************************************** * * * Name: GIFdecode Date: 06.10.92 * * * * Function: Decode image from GIF array * * * * Input: GIFarr[] - compressed image in GIF format * * * * Output: PIXarr[] - image (byte per pixel) * * Width - image width * * Height - image height * * Ncols - number of colors * * R[] - red components * * G[] - green components * * B[] - blue components * * return - 0 - if O.K. * * 1 - if error * * * ***************************************************************/ int GIFdecode(byte *GIFarr, byte *PIXarr, int *Width, int *Height, int *Ncols, byte *R, byte *G, byte *B) { byte b, /* working variable */ FinChar; /* final character */ int i, /* working variable for loops */ BitsPixel, /* number of bits per pixel */ IniCodeSize, /* initial number of bits per code */ ClearCode, /* reset code */ EOFCode, /* end of file code */ FreeCode, /* first unused entry */ CurCode, /* current code */ InCode, /* input code */ OldCode, /* previous code */ PixMask, /* mask for pixel */ OutCount; /* output stack counter */ long Npix; /* number of pixels */ ptr1 = GIFarr; ptr2 = PIXarr; OldCode = 0; FinChar = 0; /* R E A D H E A D E R */ if (strncmp((char *)GIFarr,"GIF87a",6) && strncmp((char *)GIFarr,"GIF89a",6)) { fprintf(stderr,"\nGIFinfo: not a GIF\n"); return 1; } ptr1 += 6; ptr1 += 2; /* screen width ... ignore */ ptr1 += 2; /* screen height ... ignore */ b = *ptr1++; BitsPixel = (b & 7) + 1; /* # of bits per pixel */ *Ncols = 1 << BitsPixel; PixMask = (*Ncols) - 1; /* mask for pixel code */ if ((b & 0x80) == 0) { /* is there color map? */ fprintf(stderr,"\nGIFdecode: warning! no color map\n"); *Ncols = 0; } ++ptr1; /* background color ... ignore */ b = *ptr1++; /* supposed to be NULL */ if (b) { fprintf(stderr,"\nGIFdecode: bad screen descriptor\n"); return 1; } for (i=0; i<(*Ncols); i++) { /* global color map */ R[i] = *ptr1++; G[i] = *ptr1++; B[i] = *ptr1++; } b = *ptr1++; /* image separator */ if (b != ',') { fprintf(stderr,"\nGIFdecode: no image separator\n"); return 1; } ptr1 += 2; /* left offset ... ignore */ ptr1 += 2; /* top offset ... ignore */ b = *ptr1++; /* image width */ *Width = b + 0x100*(*ptr1++); b = *ptr1++; /* image height */ *Height = b + 0x100*(*ptr1++); b = *ptr1++; /* local colors, interlace */ if ((b & 0xc0) != 0) { fprintf(stderr, "\nGIFdecode: unexpected item (local colors or interlace)\n"); return 1; } IniCodeSize = *ptr1++; CurCodeSize = ++IniCodeSize; CurMaxCode = (1 << IniCodeSize); ClearCode = (1 << (IniCodeSize - 1)); EOFCode = ClearCode + 1; FreeCode = ClearCode + 2; /* D E C O D E I M A G E */ Npix =(long) (*Width) * (*Height); OutCount = 0; CurBit = -1; CurCode = ReadCode(); while (Npix > 0) { if (CurCode < 0) { fprintf(stderr,"\nGIFdecode: corrupted GIF (zero block length)\n"); return 1; } if (CurCode == EOFCode) { fprintf(stderr,"\nGIFdecode: corrupted GIF (unexpected EOF)\n"); return 1; } if (CurCode == ClearCode) { /* clear code ... reset */ CurCodeSize = IniCodeSize; CurMaxCode = (1 << IniCodeSize); FreeCode = ClearCode + 2; OldCode = CurCode = ReadCode(); FinChar = CurCode; OutPixel(FinChar); Npix--; } else { /* image code */ InCode = CurCode; if (CurCode >= FreeCode) { CurCode = OldCode; OutCode[OutCount++] = FinChar; } while (CurCode > PixMask) { /* build output pixel chain */ if (OutCount >= TSIZE) { fprintf(stderr,"\nGIFdecode: corrupted GIF (big output count)\n"); return 1; } OutCode[OutCount++] = Suffix[CurCode]; CurCode = Prefix[CurCode]; } FinChar = CurCode; OutCode[OutCount++] = FinChar; for (i=OutCount-1; i>=0; i--) { /* put out pixel chain */ OutPixel(OutCode[i]); Npix--; } OutCount = 0; Prefix[FreeCode] = OldCode; /* build the tables */ Suffix[FreeCode] = FinChar; OldCode = InCode; FreeCode++; /* move pointer */ if (FreeCode >= CurMaxCode) { if (CurCodeSize < BITS) { CurCodeSize++; CurMaxCode *= 2; } } } CurCode = ReadCode(); } return 0; }
Texture * LoadGIF(FILE *fp, char *fname ) { Texture *texture; int filesize, numcols; register unsigned char ch, ch1; register byte *ptr, *ptr1; register int i; BitOffset=0; XC=0; YC=0; Pass=0; /* find the size of the file */ fseek(fp, 0L, 2); filesize = ftell(fp); fseek(fp, 0L, 0); if (!(ptr = RawGIF = (byte *) malloc(filesize))) fatal_error("not enough memory to read gif file"); if (!(Raster = (byte *) malloc(filesize))) { free( ptr ); fatal_error("not enough memory to read gif file"); } if (fread(ptr, filesize, 1, fp) != 1) fatal_error("GIF data read failed"); if (strncmp(ptr, id, 6)) fatal_error("not a GIF file"); ptr += 6; /* Get variables from the GIF screen descriptor */ ch = NEXTBYTE; RWidth = ch + 0x100 * NEXTBYTE; /* screen dimensions... not used. */ ch = NEXTBYTE; RHeight = ch + 0x100 * NEXTBYTE; if (Verbose) fprintf(stderr, "screen dims: %dx%d.\n", RWidth, RHeight); ch = NEXTBYTE; HasColormap = ((ch & COLORMAPMASK) ? True : False); BitsPerPixel = (ch & 7) + 1; numcols = ColorMapSize = 1 << BitsPerPixel; BitMask = ColorMapSize - 1; printf("ColorMapSize = %d\n",ColorMapSize); Background = NEXTBYTE; /* background color... not used. */ if (NEXTBYTE) /* supposed to be NULL */ fatal_error("corrupt GIF file (bad screen descriptor)"); /* Read in global colormap. */ if (HasColormap) { if (Verbose) fprintf(stderr, "%s is %dx%d, %d bits per pixel, (%d colors).\n", fname, RWidth,RHeight,BitsPerPixel, ColorMapSize); for (i = 0; i < ColorMapSize; i++) { Red[i] = NEXTBYTE; Green[i] = NEXTBYTE; Blue[i] = NEXTBYTE; used[i] = 0; } numused = 0; } /* else no colormap in GIF file */ /* Check for image seperator */ if (NEXTBYTE != IMAGESEP) fatal_error("corrupt GIF file (no image separator)"); /* Now read in values from the image descriptor */ ch = NEXTBYTE; LeftOfs = ch + 0x100 * NEXTBYTE; ch = NEXTBYTE; TopOfs = ch + 0x100 * NEXTBYTE; ch = NEXTBYTE; Width = ch + 0x100 * NEXTBYTE; ch = NEXTBYTE; Height = ch + 0x100 * NEXTBYTE; Interlace = ((NEXTBYTE & INTERLACEMASK) ? True : False); if (Verbose) fprintf(stderr, "Reading a %d by %d %sinterlaced image...", Width, Height, (Interlace) ? "" : "non-"); texture = new_texture( Width, Height ); /* Note that I ignore the possible existence of a local color map. * I'm told there aren't many files around that use them, and the spec * says it's defined for future use. This could lead to an error * reading some files. */ /* Start reading the raster data. First we get the intial code size * and compute decompressor constant values, based on this code size. */ CodeSize = NEXTBYTE; ClearCode = (1 << CodeSize); EOFCode = ClearCode + 1; FreeCode = FirstFree = ClearCode + 2; /* The GIF spec has it that the code size is the code size used to * compute the above values is the code size given in the file, but the * code size used in compression/decompression is the code size given in * the file plus one. (thus the ++). */ CodeSize++; InitCodeSize = CodeSize; MaxCode = (1 << CodeSize); ReadMask = MaxCode - 1; /* Read the raster data. Here we just transpose it from the GIF array * to the Raster array, turning it from a series of blocks into one long * data stream, which makes life much easier for ReadCode(). */ ptr1 = Raster; do { ch = ch1 = NEXTBYTE; while (ch--) *ptr1++ = NEXTBYTE; if ((ptr - Raster) > filesize) fatal_error("corrupt GIF file (unblock)"); } while(ch1); free(RawGIF); /* We're done with the raw data now... */ if (Verbose) { fprintf(stderr, "done.\n"); fprintf(stderr, "Decompressing..."); } Image = texture->texels; BytesPerScanline = Width; /* Decompress the file, continuing until you see the GIF EOF code. * One obvious enhancement is to add checking for corrupt files here. */ Code = ReadCode(); while (Code != EOFCode) { /* Clear code sets everything back to its initial value, then reads the * immediately subsequent code as uncompressed data. */ if (Code == ClearCode) { CodeSize = InitCodeSize; MaxCode = (1 << CodeSize); ReadMask = MaxCode - 1; FreeCode = FirstFree; CurCode = OldCode = Code = ReadCode(); FinChar = CurCode & BitMask; AddToPixel(FinChar); } else { /* If not a clear code, then must be data: save same as CurCode and InCode */ CurCode = InCode = Code; /* If greater or equal to FreeCode, not in the hash table yet; * repeat the last character decoded */ if (CurCode >= FreeCode) { CurCode = OldCode; OutCode[OutCount++] = FinChar; } /* Unless this code is raw data, pursue the chain pointed to by CurCode * through the hash table to its end; each code in the chain puts its * associated output code on the output queue. */ while (CurCode > BitMask) { if (OutCount > 1024) { fprintf(stderr,"\nCorrupt GIF file (OutCount)!\n"); exit(1); /* calling 'exit(-1)' dumps core, so I don't */ } OutCode[OutCount++] = Suffix[CurCode]; CurCode = Prefix[CurCode]; } /* The last code in the chain is treated as raw data. */ FinChar = CurCode & BitMask; OutCode[OutCount++] = FinChar; /* Now we put the data out to the Output routine. * It's been stacked LIFO, so deal with it that way... */ for (i = OutCount - 1; i >= 0; i--) AddToPixel(OutCode[i]); OutCount = 0; /* Build the hash table on-the-fly. No table is stored in the file. */ Prefix[FreeCode] = OldCode; Suffix[FreeCode] = FinChar; OldCode = InCode; /* Point to the next slot in the table. If we exceed the current * MaxCode value, increment the code size unless it's already 12. If it * is, do nothing: the next code decompressed better be CLEAR */ FreeCode++; if (FreeCode >= MaxCode) { if (CodeSize < 12) { CodeSize++; MaxCode *= 2; ReadMask = (1 << CodeSize) - 1; } } } Code = ReadCode(); } free(Raster); if (Verbose) fprintf(stderr, "done.\n"); else fprintf(stderr,"(of which %d are used)\n",numused); remap(texture); return texture; }
int readGifImage(const char *name,_image *im) { register byte ch, ch1; register byte *ptr, *ptr1; register int i, block; int npixels, maxpixels, aspect, filesize; float normaspect; int OutCount = 0, /* Decompressor output 'stack count' */ RWidth, RHeight, /* screen dimensions */ /*LeftOfs, TopOfs, image offset */ BitsPerPixel, /* Bits per pixel, read from GIF header */ ColorMapSize, /* number of colors */ Background, /* background color */ InitCodeSize, /* Starting code size, used during Clear */ Code, /* Value returned by ReadCode */ MaxCode, /* limiting value for current code size */ ClearCode, /* GIF clear code */ EOFCode, /* GIF end-of-information code */ CurCode, OldCode=0, InCode, /* Decompressor variables */ FirstFree, /* First free code, generated per GIF spec */ FreeCode, /* Decompressor,next free slot in hash table */ FinChar=0, /* Decompressor variable */ BitMask, /* AND mask for data size */ Misc; /* miscellaneous bits (interlace, local cmap)*/ int Interlace, HasColormap; /* not used */ /* char header[10]; */ /* The hash table used by the decompressor */ int Prefix[4096]; int Suffix[4096]; /* An output array used by the decompressor */ int OutCode[4097]; /* initialize variables */ BitOffset = XC = YC = Pass = OutCount = npixels = maxpixels = 0; RawGIF = Raster = NULL; gif89 = 0; #ifdef WIN32 fp = fopen(name,"rb"); #else fp = fopen(name,"r"); #endif fp = fopen(name,"rb"); if (!fp) { return(GifError("could not open a GIF file")); } /* find the size of the file */ fseek(fp, 0L, 2); filesize = ftell(fp); fseek(fp, 0L, 0); /* the +256's are so we can read truncated GIF files without fear of segmentation violation */ if (!(ptr = RawGIF = (byte *) ImageIO_alloc(filesize+256))) return( GifError("not enough memory to read gif file") ); if (!(Raster = (byte *) ImageIO_alloc(filesize+256))) return( GifError("not enough memory to read gif file") ); if (fread(ptr, filesize, 1, fp) != 1) return( GifError("GIF data read failed") ); if (strncmp((char *) ptr, id87, 6)==0) gif89 = 0; else if (strncmp((char *) ptr, id89, 6)==0) gif89 = 1; else return( GifError("not a GIF file")); ptr += 6; /* Get variables from the GIF screen descriptor */ ch = NEXTBYTE; RWidth = ch + 0x100 * NEXTBYTE; /* screen dimensions... not used. */ ch = NEXTBYTE; RHeight = ch + 0x100 * NEXTBYTE; use(RWidth); use(RHeight); ch = NEXTBYTE; HasColormap = ((ch & COLORMAPMASK) ? TRUE : FALSE); BitsPerPixel = (ch & 7) + 1; ColorMapSize = 1 << BitsPerPixel; BitMask = ColorMapSize - 1; Background = NEXTBYTE; /* background color... not used. */ use(Background); aspect = NEXTBYTE; if (aspect) { if (!gif89) return(GifError("corrupt GIF file (screen descriptor)")); else normaspect = (float) (aspect + 15) / 64.0f; /* gif89 aspect ratio */ if (DEBUG) fprintf(stderr,"GIF89 aspect = %f\n", normaspect); } /* Read in global colormap. */ if (HasColormap) { r = (byte *) ImageIO_alloc(ColorMapSize * sizeof(byte)); g = (byte *) ImageIO_alloc(ColorMapSize * sizeof(byte)); b = (byte *) ImageIO_alloc(ColorMapSize * sizeof(byte)); for (i = 0; i < ColorMapSize; i++) { r[i] = NEXTBYTE; g[i] = NEXTBYTE; b[i] = NEXTBYTE; } } else { /* no colormap in GIF file */ /* put std EGA palette (repeated 16 times) into colormap, for lack of anything better to do */ ColorMapSize = 256; r = (byte *) ImageIO_alloc(256 * sizeof(byte)); g = (byte *) ImageIO_alloc(256 * sizeof(byte)); b = (byte *) ImageIO_alloc(256 * sizeof(byte)); for (i = 0; i < 256; i++) { r[i] = EGApalette[i&15][0]; g[i] = EGApalette[i&15][1]; b[i] = EGApalette[i&15][2]; } } /* possible things at this point are: * an application extension block * a comment extension block * an (optional) graphic control extension block * followed by either an image * or a plaintext extension */ while (1) { block = NEXTBYTE; if (block == EXTENSION) { /* parse extension blocks */ int i, fn, blocksize, aspnum, aspden; /* read extension block */ fn = NEXTBYTE; if (DEBUG) fprintf(stderr,"GIF extension type 0x%02x\n", fn); if (fn == 'R') { /* GIF87 aspect extension */ blocksize = NEXTBYTE; if (blocksize == 2) { aspnum = NEXTBYTE; aspden = NEXTBYTE; if (aspden>0 && aspnum>0) normaspect = (float) aspnum / (float) aspden; else { normaspect = 1.0; aspnum = aspden = 1; } if (DEBUG) fprintf(stderr,"GIF87 aspect extension: %d:%d = %f\n\n", aspnum, aspden,normaspect); } else { for (i=0; i<blocksize; i++) (void)NEXTBYTE; } } else if (fn == 0xFE) { /* Comment Extension. just eat it */ int ch, j, sbsize; if (DEBUG) fprintf(stderr,"Comment extension: "); /* read (and ignore) data sub-blocks */ do { j = 0; sbsize = NEXTBYTE; while (j<sbsize) { ch = NEXTBYTE; j++; if (DEBUG) fprintf(stderr,"%c", ch); } } while (sbsize); if (DEBUG) fprintf(stderr,"\n\n"); } else if (fn == 0x01) { /* PlainText Extension */ int j,sbsize,ch; int tgLeft, tgTop, tgWidth, tgHeight, cWidth, cHeight, fg, bg; /* SetISTR(ISTR_WARNING, "PlainText extension found in GIF file. Ignored.");*/ sbsize = NEXTBYTE; tgLeft = NEXTBYTE; tgLeft += (NEXTBYTE)<<8; tgTop = NEXTBYTE; tgTop += (NEXTBYTE)<<8; tgWidth = NEXTBYTE; tgWidth += (NEXTBYTE)<<8; tgHeight = NEXTBYTE; tgHeight += (NEXTBYTE)<<8; cWidth = NEXTBYTE; cHeight = NEXTBYTE; fg = NEXTBYTE; bg = NEXTBYTE; i=12; for ( ; i<sbsize; i++) (void)NEXTBYTE; /* read rest of first subblock */ if (DEBUG) fprintf(stderr, "PlainText: tgrid=%d,%d %dx%d cell=%dx%d col=%d,%d\n", tgLeft, tgTop, tgWidth, tgHeight, cWidth, cHeight, fg, bg); /* read (and ignore) data sub-blocks */ do { j = 0; sbsize = NEXTBYTE; while (j<sbsize) { ch = NEXTBYTE; j++; if (DEBUG) fprintf(stderr,"%c", ch); } } while (sbsize); if (DEBUG) fprintf(stderr,"\n\n"); } else if (fn == 0xF9) { /* Graphic Control Extension */ int j, sbsize; if (DEBUG) fprintf(stderr,"Graphic Control extension\n\n"); /* SetISTR(ISTR_WARNING, "Graphic Control Extension in GIF file. Ignored.");*/ /* read (and ignore) data sub-blocks */ do { j = 0; sbsize = NEXTBYTE; while (j<sbsize) { (void)NEXTBYTE; j++; } } while (sbsize); } else { /* unknown extension */ int j, sbsize; if (DEBUG) fprintf(stderr,"unknown GIF extension 0x%02x\n\n", fn); /* SetISTR(ISTR_WARNING, "Unknown extension 0x%02x in GIF file. Ignored.",fn);*/ /* read (and ignore) data sub-blocks */ do { j = 0; sbsize = NEXTBYTE; while (j<sbsize) { (void)NEXTBYTE; j++; } } while (sbsize); } } else if (block == IMAGESEP) break; /* read an image */ else if (block == TRAILER) { return( GifError("no image data found in GIF file") ); } else return (GifError("Unknown block type found in file.")); } /* read in values from the image descriptor */ ch = NEXTBYTE; /* LeftOfs = ch + 0x100 * NEXTBYTE;*/ ch = NEXTBYTE; /* TopOfs = ch + 0x100 * NEXTBYTE; */ ch = NEXTBYTE; ch = NEXTBYTE; ch = NEXTBYTE; Width = ch + 0x100 * NEXTBYTE; ch = NEXTBYTE; Height = ch + 0x100 * NEXTBYTE; Misc = NEXTBYTE; Interlace = ((Misc & INTERLACEMASK) ? TRUE : FALSE); if (Misc & 0x80) { for (i=0; i< 1 << ((Misc&7)+1); i++) { r[i] = NEXTBYTE; g[i] = NEXTBYTE; b[i] = NEXTBYTE; } } if (!HasColormap && !(Misc&0x80)) { /* no global or local colormap */ /* SetISTR(ISTR_WARNING, "No colormap in this GIF file. Assuming EGA colors.");*/ } /* Start reading the raster data. First we get the intial code size * and compute decompressor constant values, based on this code size. */ /* SetISTR(ISTR_FORMAT, "GIF%s, %d bits per pixel, %sinterlaced. (%d bytes)", (gif89) ? "89" : "87", BitsPerPixel, Interlace ? "" : "non-", filesize);*/ CodeSize = NEXTBYTE; ClearCode = (1 << CodeSize); EOFCode = ClearCode + 1; FreeCode = FirstFree = ClearCode + 2; /* The GIF spec has it that the code size is the code size used to * compute the above values is the code size given in the file, but the * code size used in compression/decompression is the code size given in * the file plus one. (thus the ++). */ CodeSize++; InitCodeSize = CodeSize; MaxCode = (1 << CodeSize); ReadMask = MaxCode - 1; /* UNBLOCK: * Read the raster data. Here we just transpose it from the GIF array * to the Raster array, turning it from a series of blocks into one long * data stream, which makes life much easier for ReadCode(). */ ptr1 = Raster; do { ch = ch1 = NEXTBYTE; while (ch--) { *ptr1 = NEXTBYTE; ptr1++; } if ((ptr - RawGIF) > filesize) { /* SetISTR(ISTR_WARNING, "This GIF file seems to be truncated. Winging it.");*/ break; } } while(ch1); ImageIO_free(RawGIF); RawGIF = NULL; if (DEBUG) { fprintf(stderr,"xv: LoadGIF() - picture is %dx%d, %d bits, %sinterlaced\n", Width, Height, BitsPerPixel, Interlace ? "" : "non-"); } /* Allocate the 'pic' */ maxpixels = Width*Height; im->xdim = Width; im->ydim = Height; im->zdim = 1; im->vdim = 3; im->wdim = 1; im->wordKind = WK_FIXED; im->sign = SGN_UNSIGNED; im->data = ImageIO_alloc(Width * Height * 3); org = buf = (unsigned char *) im->data; if (!org) return( GifError("not enough memory for image buffer") ); /* Decompress the file, continuing until you see the GIF EOF code. * One obvious enhancement is to add checking for corrupt files here. */ Code = ReadCode(); while (Code != EOFCode) { /* Clear code sets everything back to its initial value, then reads the * immediately subsequent code as uncompressed data. */ if (Code == ClearCode) { CodeSize = InitCodeSize; MaxCode = (1 << CodeSize); ReadMask = MaxCode - 1; FreeCode = FirstFree; Code = ReadCode(); CurCode = OldCode = Code; FinChar = CurCode & BitMask; if (!Interlace) { *buf++ = r[FinChar]; *buf++ = g[FinChar]; *buf++ = b[FinChar]; } else DoInterlace((byte)FinChar); npixels++; } else { /* If not a clear code, must be data: save same as CurCode and InCode */ /* if we're at maxcode and didn't get a clear, stop loading */ if (FreeCode>=4096) { printf("freecode blew up\n"); break; } CurCode = InCode = Code; /* If greater or equal to FreeCode, not in the hash table yet; * repeat the last character decoded */ if (CurCode >= FreeCode) { CurCode = OldCode; if (OutCount > 4096) { printf("outcount1 blew up\n"); break; } OutCode[OutCount++] = FinChar; } /* Unless this code is raw data, pursue the chain pointed to by CurCode * through the hash table to its end; each code in the chain puts its * associated output code on the output queue. */ while (CurCode > BitMask) { if (OutCount > 4096) { fprintf(stderr,"outcount2 blew up\n"); break;} /* corrupt file */ OutCode[OutCount++] = Suffix[CurCode]; CurCode = Prefix[CurCode]; } if (OutCount > 4096) { printf("outcount blew up\n"); break; } /* The last code in the chain is treated as raw data. */ FinChar = CurCode & BitMask; OutCode[OutCount++] = FinChar; /* Now we put the data out to the Output routine. * It's been stacked LIFO, so deal with it that way... */ /* safety thing: prevent exceeding range of 'pic' */ if (npixels + OutCount > maxpixels) OutCount = maxpixels-npixels; npixels += OutCount; if (!Interlace) for (i=OutCount-1; i>=0; i--) { *buf++ = r[OutCode[i]]; *buf++ = g[OutCode[i]]; *buf++ = b[OutCode[i]]; } else for (i=OutCount-1; i>=0; i--) DoInterlace((byte)OutCode[i]); OutCount = 0; /* Build the hash table on-the-fly. No table is stored in the file. */ Prefix[FreeCode] = OldCode; Suffix[FreeCode] = FinChar; OldCode = InCode; /* Point to the next slot in the table. If we exceed the current * MaxCode value, increment the code size unless it's already 12. If it * is, do nothing: the next code decompressed better be CLEAR */ FreeCode++; if (FreeCode >= MaxCode) { if (CodeSize < 12) { CodeSize++; MaxCode *= 2; ReadMask = (1 << CodeSize) - 1; } } } Code = ReadCode(); if (npixels >= maxpixels) break; } ImageIO_free(Raster); Raster = NULL; if (npixels != maxpixels) { /* SetISTR(ISTR_WARNING,"This GIF file seems to be truncated. Winging it.");*/ if (!Interlace) memset(buf, 0, 3*(maxpixels-npixels)); /* clear to EOBuffer */ } /* SetDirRButt(F_FORMAT, F_GIF); SetDirRButt(F_COLORS, F_FULLCOLOR);*/ return 1; }
BYTE * Decompress(GIFIMAGEDESC *GifImageDesc, GIFHEAD *GifHead) { int i; XC = 0; YC = 0; Pass = 0; OutCount = 0; BitOffset = 0; DataMask = (1 << ((GifHead->PackedField & 0x07) +1)) -1; Raster = GifImageDesc->GIFImage; /* Check for image seperator */ /* Now read in values from the image descriptor */ IWidth = GifImageDesc->ImageWidth; IHeight = GifImageDesc->ImageHeight; Interlace = GifImageDesc->PackedField & 0x40; /* * Note that I ignore the possible existence of a local color map. I'm * told there aren't many files around that use them, and the spec says * it's defined for future use. This could lead to an error reading some * files. */ /* * Start reading the raster data. First we get the WORDial code size and * compute decompressor constant values, based on this code size. */ CodeSize = GifImageDesc->CodeSize; ClearCode = (1 << CodeSize); EOFCode = ClearCode + 1; FreeCode = FirstFree = ClearCode + 2; /* * The GIF spec has it that the code size is the code size used to compute * the above values is the code size given in the file, but the code size * used in compression/decompression is the code size given in the file * plus one. (thus the ++). */ CodeSize++; InitCodeSize = CodeSize; MaxCode = (1 << CodeSize); ReadMask = MaxCode - 1; /* * Read the raster data. Here we just transpose it from the GIF array to * the Raster array, turning it from a series of blocks WORDo one long * data stream, which makes life much easier for ReadCode(). */ /* Allocate the Image */ if (!(Image = (BYTE *)malloc((size_t)IWidth*(size_t)IHeight))) { printf("Out of memory"); exit(EXIT_FAILURE); } BytesPerScanline = IWidth; /* * Decompress the file, continuing until you see the GIF EOF code. One * obvious enhancement is to add checking for corrupt files here. */ Code = ReadCode(); while (Code != EOFCode) { /* * Clear code sets everything back to its initial value, then reads * the immediately subsequent code as uncompressed data. */ if (Code == ClearCode) { CodeSize = InitCodeSize; MaxCode = (1 << CodeSize); ReadMask = MaxCode - 1; FreeCode = FirstFree; CurCode = OldCode = Code = ReadCode(); FinChar = CurCode & DataMask; AddToPixel((BYTE)FinChar); } else { /* * If not a clear code, then must be data: save same as CurCode * and InCode */ CurCode = InCode = Code; /* * If greater or equal to FreeCode, not in the hash table yet; * repeat the last character decoded */ if (CurCode >= FreeCode) { CurCode = OldCode; OutCode[OutCount++] = FinChar; } /* * Unless this code is raw data, pursue the chain poWORDed to by * CurCode through the hash table to its end; each code in the * chain puts its associated output code on the output queue. */ while (CurCode > DataMask) { if (OutCount > 1024) { /*return error message*/ } OutCode[OutCount++] = Suffix[CurCode]; CurCode = Prefix[CurCode]; } /* The last code in the chain is treated as raw data. */ FinChar = CurCode & DataMask; OutCode[OutCount++] = FinChar; /* * Now we put the data out to the Output routine. It's been * stacked LIFO, so deal with it that way... */ for (i = OutCount - 1; i >= 0; i--) AddToPixel((BYTE)OutCode[i]); OutCount = 0; /* * Build the hash table on-the-fly. No table is stored in the * file. */ Prefix[FreeCode] = OldCode; Suffix[FreeCode] = FinChar; OldCode = InCode; /* * PoWORD to the next slot in the table. If we exceed the current * MaxCode value, increment the code size unless it's already 12. * If it is, do nothing: the next code decompressed better be * CLEAR */ FreeCode++; if (FreeCode >= MaxCode) if (CodeSize < 12) { CodeSize++; MaxCode *= 2; ReadMask = (1 << CodeSize) - 1; } } Code = ReadCode(); } return Image; }
/*========================================================================= * FUNCTION: tVM_Execute * TYPE: public interface * OVERVIEW: execute a basic function * INTERFACE: * parameters: * returns: * the result of the basic function *=======================================================================*/ int tVM_Execute() { int running = 1; u8 bytecode; u8 type; u8 ac_flag; s32 integer; s32 stackindex,index; tVMValue value1,value2,value3; tVMValue retValue;// = (tVMValue*)mem_alloc(sizeof(tVMValue)); /* initialize the running Stack FP */ setFP(FirstFP); /* seek to the entry function */ setFI(0); /* initialize the code reader */ tVM_InitializeCodeReader(); /* execute the byte codes in loop */ while(running) { bytecode = ReadCode(); switch(bytecode) { case C_NOP: break; case C_CONST: { ReadVMValue(&value1); PushVMValue(&value1); break; } case C_LOAD: { ac_flag =ReadAccessFlag(); if(ac_flag == ACCESS_FLAG_GLOBAL) stackindex = ReadIndex(); else stackindex = ReadIndex() + getFP(); LoadVMValue(stackindex,&value1); PushVMValue(&value1); break; } case C_STORE: { ac_flag =ReadAccessFlag(); if(ac_flag == ACCESS_FLAG_GLOBAL) stackindex = ReadIndex(); else stackindex = ReadIndex() + getFP(); PopVMValue(&value1); /* pop the source value */ StoreVMValue(stackindex,&value1); break; } case C_HEAP_LOAD: { type = ReadDataType(); PopVMValue(&value2); /* Pop Addr */ PopVMValue(&value1); /* Pop Base */ tVMValue_HeapLoad(value1.value.ptr_val+value2.value.int_val,type,&value3); /* load the heap memory */ PushVMValue(&value3); /* push the loaded value */ break; } case C_HEAP_STORE: { ptr32 addr; type = ReadDataType(); PopVMValue(&value3); /* Pop Addr */ PopVMValue(&value2); /* Pop Base */ PopVMValue(&value1); /* Pop Value */ addr = (ptr32)(value2.value.ptr_val + value3.value.int_val); if(value1.type != type) { tVMValue_ConvertType(&value1,type); } tVMValue_HeapStore(addr,&value1); break; } case C_FORCE_LOAD: { ac_flag =ReadAccessFlag(); if(ac_flag == ACCESS_FLAG_GLOBAL) stackindex = ReadIndex(); else stackindex = ReadIndex() + getFP(); type = ReadDataType(); ForceLoadVMValue(stackindex,type,&value1); PushVMValue(&value1); break; } case C_ALLOC: { PopVMValue(&value1); value2.type = PtrType; value2.value.ptr_val = (ptr32)mem_alloc(value1.value.int_val); memset(value2.value.ptr_val,0,value1.value.int_val); PushVMValue(&value2); break; } case C_ALLOC_ARRAY: { s32 i; s32 dimension; s32* index_ranges; dimension = ReadInteger(); if(dimension < 1) break; index_ranges = (s32*)mem_alloc(sizeof(s32)*dimension); for(i=0;i<dimension;i++) { PopVMValue(&value1); index_ranges[dimension-i-1] = value1.value.int_val; } value1.type = PtrType; value1.value.ptr_val = tVMValue_HeapAllocMultiArray(dimension,index_ranges,0); PushVMValue(&value1); mem_free(index_ranges); break; } case C_FREE: { PopVMValue(&value1); if(value1.value.ptr_val != NULL) mem_free(value1.value.ptr_val); break; } case C_FREE_ARRAY: { break; } case C_PUSH: { value1.type = ReadDataType(); value1.value.int_val = 0; PushVMValue(&value1); break; } case C_POP: { s32 i; integer = ReadInteger(); for(i=0;i<integer;i++) { PopVMValue(&value1); tVMValue_FreeSelf(&value1); } break; } case C_POP_RESTOP: { s32 i; integer = ReadInteger(); PopVMValue(&value2); /* reserve top value */ for(i=0;i<integer;i++) { PopVMValue(&value1); tVMValue_FreeSelf(&value1); } PushVMValue(&value2); /* push back top value */ break; } case C_CONVERT: { u8 type = (u8)ReadDataType(); PopVMValue(&value1); tVMValue_ConvertType(&value1,type); PushVMValue(&value1); break; } case C_ADD: { PopVMValue(&value2); PopVMValue(&value1); tVMValue_Add(&value1,&value2,&value3); PushVMValue(&value3); tVMValue_FreeSelf(&value1); tVMValue_FreeSelf(&value2); break; } case C_SUB: { PopVMValue(&value2); PopVMValue(&value1); tVMValue_Sub(&value1,&value2,&value3); PushVMValue(&value3); tVMValue_FreeSelf(&value1); tVMValue_FreeSelf(&value2); break; } case C_MUL: { PopVMValue(&value2); PopVMValue(&value1); tVMValue_Mul(&value1,&value2,&value3); PushVMValue(&value3); tVMValue_FreeSelf(&value1); tVMValue_FreeSelf(&value2); break; } case C_DIV: { PopVMValue(&value2); PopVMValue(&value1); tVMValue_Div(&value1,&value2,&value3); PushVMValue(&value3); tVMValue_FreeSelf(&value1); tVMValue_FreeSelf(&value2); break; } case C_MOD: { PopVMValue(&value2); PopVMValue(&value1); tVMValue_Mod(&value1,&value2,&value3); PushVMValue(&value3); tVMValue_FreeSelf(&value1); tVMValue_FreeSelf(&value2); break; } case C_OPP: { PopVMValue(&value1); tVMValue_Opp(&value1,&value2); PushVMValue(&value2); tVMValue_FreeSelf(&value1); break; } case C_AND: { PopVMValue(&value2); PopVMValue(&value1); tVMValue_AND(&value1,&value2,&value3); PushVMValue(&value3); tVMValue_FreeSelf(&value1); tVMValue_FreeSelf(&value2); break; } case C_OR: { PopVMValue(&value2); PopVMValue(&value1); tVMValue_OR(&value1,&value2,&value3); PushVMValue(&value3); tVMValue_FreeSelf(&value1); tVMValue_FreeSelf(&value2); break; } case C_EQ: { PopVMValue(&value2); PopVMValue(&value1); tVMValue_EQ(&value1,&value2,&value3); PushVMValue(&value3); tVMValue_FreeSelf(&value1); tVMValue_FreeSelf(&value2); break; } case C_NOT_EQ: { PopVMValue(&value2); PopVMValue(&value1); tVMValue_NOTEQ(&value1,&value2,&value3); PushVMValue(&value3); tVMValue_FreeSelf(&value1); tVMValue_FreeSelf(&value2); break; } case C_LT: { PopVMValue(&value2); PopVMValue(&value1); tVMValue_LT(&value1,&value2,&value3); PushVMValue(&value3); tVMValue_FreeSelf(&value1); tVMValue_FreeSelf(&value2); break; } case C_LG: { PopVMValue(&value2); PopVMValue(&value1); tVMValue_LG(&value1,&value2,&value3); PushVMValue(&value3); tVMValue_FreeSelf(&value1); tVMValue_FreeSelf(&value2); break; } case C_LT_EQ: { PopVMValue(&value2); PopVMValue(&value1); tVMValue_LTEQ(&value1,&value2,&value3); PushVMValue(&value3); tVMValue_FreeSelf(&value1); tVMValue_FreeSelf(&value2); break; } case C_LG_EQ: { PopVMValue(&value2); PopVMValue(&value1); tVMValue_LGEQ(&value1,&value2,&value3); PushVMValue(&value3); tVMValue_FreeSelf(&value1); tVMValue_FreeSelf(&value2); break; } case C_FJP: { s32 size = ReadIndex(); PopVMValue(&value1); if(value1.value.int_val == 0) /* if it is false */ addIP(size); break; } case C_TJP: { s32 size = ReadIndex(); PopVMValue(&value1); if(value1.value.int_val != 0) /* if it is true */ addIP(size); break; } case C_JMP: { s32 size = ReadIndex(); addIP(size); break; } case C_CALL: { /* read function name */ integer = ReadIndex(); /* push the stack frame */ PushInteger(getIP()); PushInteger(getFI()); PushInteger(getFP()); /* goto the call function code */ tVM_ReleaseCodeReader(); setFI(integer); tVM_InitializeCodeReader(); /* set new FP,RP */ setFP(getSP()); break; } case C_INVOKE: { /* read function name */ index = ReadIndex(); /* execute the native function */ tNativeFunction_Invoke(index); break; } case C_RET: { u32 param_bytes = ReadIndex(); /* get the result of the function */ retValue.type = NullType; PopVMValue(&retValue); /* if this is the start function,then exit the loop */ if(getFP() == FirstFP) { running = 0; /* set flag to stop while */ break; } /* restore last stack frame and return to last function code */ tVM_ReleaseCodeReader(); PopInteger(integer); setFP(integer); PopInteger(integer); setFI(integer); tVM_InitializeCodeReader(); PopInteger(integer); setIP(integer); /* pop the old parameters */ PopBytes(param_bytes); /* push back result of last function */ PushVMValue(&retValue); break; } } } /* close the code reader */ tVM_ReleaseCodeReader(); return 1; }
bool DefFile::Read() { stream = new std::fstream(fileName.c_str(), std::ios::in); if (stream != NULL) { try { lineno = 0; NextToken(); while (!stream->eof()) { if (token->IsEnd()) { NextToken(); } else if (!token->IsKeyword()) { throw new std::runtime_error("Invalid directive"); } else { switch(token->GetKeyword()) { case edt_name: ReadName(); break; case edt_library: ReadLibrary(); break; case edt_exports: ReadExports(); break; case edt_imports: ReadImports(); break; case edt_description: ReadDescription(); break; case edt_stacksize: ReadStacksize(); break; case edt_heapsize: ReadHeapsize(); break; case edt_code: ReadCode(); break; case edt_data: ReadData(); break; case edt_sections: ReadSections(); break; default: throw new std::runtime_error("Invalid directive"); } } } } catch (std::runtime_error *e) { std::cout << fileName << "(" << lineno << "): " << e->what() << std::endl ; delete e; } delete stream; } else { std::cout << "File '" << name << "' not found." << std::endl; } return true; }