static int getVarFromStream(IOSTREAM *s, char *name, char *value) { char *q; int l; int c; again: for(l=MAXVARNAME, q=name; --l > 0; ) { switch(c = Sgetc(s)) { case EOF: return FALSE; case '=': *q = EOS; goto do_value; case '\n': goto again; default: *q++ = c; } } return FALSE; do_value: for(l=MAXVARVAL, q=value; --l > 0; ) { switch(c = Sgetc(s)) { case EOF: case '\n': *q = EOS; return TRUE; default: *q++ = c; } } return FALSE; }
static int getNum(IOSTREAM *fd) { int c; int v; for(;;) { do { c = Sgetc(fd); } while(isspace(c)); if ( isdigit(c) ) { v = valdigit(c); for(;;) { c = Sgetc(fd); if ( isdigit(c) ) v = v*10 + valdigit(c); else break; } if ( !isspace(c) ) Sungetc(c, fd); return v; } if ( c == '#' ) { do { c = Sgetc(fd); } while( c != '\n' && c != EOF ); } else return -1; } }
static int getVarFromStream(IOSTREAM *s, tmp_buffer *name, tmp_buffer *value) { int c; again: initBuffer(name); initBuffer(value); for(;;) { switch(c = Sgetc(s)) { case EOF: return FALSE; case '=': addBuffer(name, EOS, char); goto do_value; case '\n': discardBuffer(name); goto again; default: addBuffer(name, c, char); } } do_value: for(;;) { switch(c = Sgetc(s)) { case EOF: case '\n': addBuffer(value, EOS, char); return TRUE; default: addBuffer(value, c, char); } } }
static ssize_t /* decode */ chunked_read(void *handle, char *buf, size_t size) { chunked_context *ctx = handle; for(;;) { if ( ctx->avail > 0 ) /* data waiting */ { size_t max_rd = ctx->avail < size ? ctx->avail : size; ssize_t rc; if ( (rc = Sfread(buf, sizeof(char), max_rd, ctx->stream)) > 0 ) { ctx->avail -= rc; if ( ctx->avail == 0 ) { if ( Sgetc(ctx->stream) != '\r' || Sgetc(ctx->stream) != '\n' ) { Sseterr(ctx->chunked_stream, 0, "Chunk not followed by \\r\\n"); return -1; } } return rc; } else if ( rc == 0 ) { Sseterr(ctx->chunked_stream, 0, "Unexpected EOF in chunked data"); return -1; } else { return -1; } } else { char hdr[MAXHDR]; char *s; if ( (s = Sfgets(hdr, sizeof(hdr), ctx->stream)) ) { char *ehdr; long len; errno = 0; len = strtol(hdr, &ehdr, 16); if ( errno || len < 0 ) { Sseterr(ctx->chunked_stream, 0, "Bad chunk length"); return -1; } if ( len == 0 ) { do { s = Sfgets(hdr, sizeof(hdr), ctx->stream); } while ( s && strcmp(s, "\r\n") != 0 ); if ( s ) return 0; Sseterr(ctx->chunked_stream, 0, "Bad end-of-stream"); return -1; } ctx->avail = len; /*continue*/ } } } }
static int NextInt(IOSTREAM *fstream) { int value = 0; int gotone = 0; int done = 0; /* loop, accumulate hex value until find delimiter */ /* skip any initial delimiters found in read stream */ while (!done) { int ch = Sgetc(fstream); if ( ch == EOF ) { return -1; } else if ( ch == '\r' ) { continue; } else { int dvalue = hexTable[ch]; if ( dvalue >= 0 ) { value = (value << 4) + dvalue; gotone++; } else if ((hexTable[ch]) == -1 && gotone) done++; } } return value; }
main() { int c ; while ( ( c = Sgetc( 0 ) ) != SIO_EOF ) putchar( c ) ; exit( 0 ) ; }
static unsigned char * read_bitmap_data(IOSTREAM *fd, int *w, int *h) { long offset = Stell(fd); unsigned char *rval; int c0; c0 = Sgetc(fd); Sungetc(c0, fd); switch(c0) { case '#': if ( (rval = read_x11_bitmap_file(fd, w, h)) != NULL ) return rval; Sseek(fd, offset, SIO_SEEK_SET); break; case '/': if ( (rval = read_sun_icon_file(fd, w, h)) != NULL ) return rval; Sseek(fd, offset, SIO_SEEK_SET); break; } return NULL; }
XImage * read_ppm_file(Display *disp, Colormap cmap, int depth, IOSTREAM *fd) { XImage *img; long here = Stell(fd); int c; int fmt, encoding; int width, height, bytes_per_line, scale=0; char *data; int allocdepth; int pad = XBitmapPad(disp); Visual *v = DefaultVisual(disp, DefaultScreen(disp)); ncolours = nmapped = nfailed = 0; /* statistics */ assert(pad%8 == 0); if ( (c=Sgetc(fd)) != 'P' ) { Sungetc(c, fd); return NULL; } if ( !cmap ) cmap = DefaultColormap(disp, DefaultScreen(disp)); c = Sgetc(fd); if ( c < '1' || c > '9' ) goto errout; c -= '0'; fmt = ((c - 1) % 3) + 1; encoding = c - fmt; width = getNum(fd); height = getNum(fd); if ( fmt == PNM_PBM ) { depth = 1; } else { scale = getNum(fd); if ( !depth ) depth = DefaultDepth(disp, DefaultScreen(disp)); } if ( width < 0 || height < 0 || scale < 0 ) goto errout; allocdepth = (depth >= 24 ? 32 : depth); bytes_per_line = roundup((width*allocdepth+7)/8, pad/8); data = (char *)pceMalloc(height * bytes_per_line); img = XCreateImage(disp, v, depth, fmt == PNM_PBM ? XYBitmap : ZPixmap, 0, data, width, height, pad, bytes_per_line); if ( !img ) { perror("XCreateImage"); pceFree(data); goto errout; } img->bits_per_pixel = depth; switch(encoding) { int x, y; case PNM_ASCII: { switch(fmt) { case PNM_PBM: for(y=0; y<height; y++) { for(x=0; x<width; x++) { int value = getNum(fd); if ( value < 0 || value > 1 ) goto errout; XPutPixel(img, x, y, value); } } break; case PNM_PGM: { Table t = newTable(64); for(y=0; y<height; y++) { for(x=0; x<width; x++) { int g = getNum(fd); unsigned long pixel; if ( g < 0 || g > scale ) goto errout; if ( scale != 255 ) g = rescale(g, scale, 255); pixel = colourPixel(disp, depth, cmap, t, g, g, g); XPutPixel(img, x, y, pixel); } } freeTable(t); break; } case PNM_PPM: { Table t = newTable(64); for(y=0; y<height; y++) { for(x=0; x<width; x++) { int r = getNum(fd); int g = getNum(fd); int b = getNum(fd); unsigned long pixel; if ( r < 0 || r > scale || g < 0 || g > scale || b < 0 || b > scale ) goto errout; if ( scale != 255 ) { r = rescale(r, scale, 255); g = rescale(g, scale, 255); b = rescale(b, scale, 255); } pixel = colourPixel(disp, depth, cmap, t, r, g, b); XPutPixel(img, x, y, pixel); } } freeTable(t); break; } break; } break; } case PNM_RAWBITS: { switch(fmt) { case PNM_PBM: { int byte = 0; int bit = 0; for(y=0; y<height; y++) { for(x=0; x<width; x++) { if ( !bit ) { byte = Sgetc(fd); bit = 8; } bit--; XPutPixel(img, x, y, (byte & (1<<bit)) ? 1 : 0); } bit = 0; /* scanlines are byte-aligned */ } break; } case PNM_PGM: { Table t = newTable(64); for(y=0; y<height; y++) { for(x=0; x<width; x++) { int g; unsigned long pixel; if ( Sfeof(fd) || (g=Sgetc(fd)) > scale ) goto errout; if ( scale != 255 ) g = rescale(g, scale, 255); pixel = colourPixel(disp, depth, cmap, t, g, g, g); XPutPixel(img, x, y, pixel); } } freeTable(t); break; } case PNM_PPM: { Table t = newTable(64); for(y=0; y<height; y++) { for(x=0; x<width; x++) { int r, g, b; unsigned long pixel; if ( Sfeof(fd) || (r=Sgetc(fd)) > scale || (g=Sgetc(fd)) > scale || (b=Sgetc(fd)) > scale ) goto errout; if ( scale != 255 ) { r = rescale(r, scale, 255); g = rescale(g, scale, 255); b = rescale(b, scale, 255); } pixel = colourPixel(disp, depth, cmap, t, r, g, b); XPutPixel(img, x, y, pixel); } } freeTable(t); break; } break; } break; } case PNM_RUNLEN: { int rlen = 0; unsigned long cpixel = NOPIXEL; switch(fmt) { case PNM_PGM: { Table t = newTable(64); DEBUG(NAME_pnm, Cprintf("Reading runlength encoded graymap\n")); for(y=0; y<height; y++) { for(x=0; x<width; x++) { if ( rlen-- > 0 ) { XPutPixel(img, x, y, cpixel); } else { int g; if ( (g=Sgetc(fd)) > scale || (rlen = Sgetc(fd)) == EOF ) goto errout; rlen &= 0xff; if ( scale != 255 ) g = rescale(g, scale, 255); cpixel = colourPixel(disp, depth, cmap, t, g, g, g); XPutPixel(img, x, y, cpixel); rlen--; } } } freeTable(t); break; } case PNM_PPM: { Table t = newTable(64); for(y=0; y<height; y++) { for(x=0; x<width; x++) { if ( rlen-- > 0 ) { XPutPixel(img, x, y, cpixel); } else { int r, g, b; if ( (r=Sgetc(fd)) > scale || (g=Sgetc(fd)) > scale || (b=Sgetc(fd)) > scale || (rlen = Sgetc(fd)) == EOF ) goto errout; rlen &= 0xff; if ( scale != 255 ) { r = rescale(r, scale, 255); g = rescale(g, scale, 255); b = rescale(b, scale, 255); } cpixel = colourPixel(disp, depth, cmap, t, r, g, b); XPutPixel(img, x, y, cpixel); rlen--; } } } freeTable(t); break; } } } } DEBUG(NAME_ppm, Cprintf("PNM: Converted %dx%dx%d image, %d colours (%d mapped, %d failed)\n", width, height, depth, ncolours, nmapped, nfailed)); return img; errout: DEBUG(NAME_ppm, Cprintf("PNM: Format error, index = %d\n", Stell(fd))); Sseek(fd, here, SEEK_SET); return NULL; }