From 2dc8c2c26b14a59385672e7fe4f2c8aa0eff6e3f Mon Sep 17 00:00:00 2001 From: Barry Warsaw Date: Thu, 9 Jan 1997 22:29:12 +0000 Subject: [PATCH] Primarily formatting changes, but I also plugged a couple of potential return value problems, memory leaks, and descriptor leaks. --- Modules/rgbimgmodule.c | 610 +++++++++++++++++++++-------------------- 1 file changed, 308 insertions(+), 302 deletions(-) diff --git a/Modules/rgbimgmodule.c b/Modules/rgbimgmodule.c index 4ece3cc3a08..02005bb7dd6 100644 --- a/Modules/rgbimgmodule.c +++ b/Modules/rgbimgmodule.c @@ -106,11 +106,12 @@ static int reverse_order; * this is used to extract image data from core dumps. * */ -addlongimgtag(dptr,xsize,ysize) +static void +addlongimgtag(dptr, xsize, ysize) unsigned long *dptr; -int xsize, ysize; + int xsize, ysize; { - dptr = dptr+(xsize*ysize); + dptr = dptr + (xsize * ysize); dptr[0] = 0x12345678; dptr[1] = 0x59493333; dptr[2] = 0x69434222; @@ -123,100 +124,108 @@ int xsize, ysize; * byte order independent read/write of shorts and longs. * */ -static unsigned short getshort(inf) +static unsigned short +getshort(inf) FILE *inf; { unsigned char buf[2]; - fread(buf,2,1,inf); - return (buf[0]<<8)+(buf[1]<<0); + fread(buf, 2, 1, inf); + return (buf[0] << 8) + (buf[1] << 0); } -static unsigned long getlong(inf) +static unsigned long +getlong(inf) FILE *inf; { unsigned char buf[4]; - fread(buf,4,1,inf); - return (buf[0]<<24)+(buf[1]<<16)+(buf[2]<<8)+(buf[3]<<0); + fread(buf, 4, 1, inf); + return (buf[0] << 24) + (buf[1] << 16) + (buf[2] << 8) + (buf[3] << 0); } -static void putshort(outf,val) +static void +putshort(outf, val) FILE *outf; -unsigned short val; + unsigned short val; { unsigned char buf[2]; - buf[0] = (val>>8); - buf[1] = (val>>0); - fwrite(buf,2,1,outf); + buf[0] = (val >> 8); + buf[1] = (val >> 0); + fwrite(buf, 2, 1, outf); } -static int putlong(outf,val) +static int +putlong(outf, val) FILE *outf; -unsigned long val; + unsigned long val; { unsigned char buf[4]; - buf[0] = (val>>24); - buf[1] = (val>>16); - buf[2] = (val>>8); - buf[3] = (val>>0); - return fwrite(buf,4,1,outf); + buf[0] = (val >> 24); + buf[1] = (val >> 16); + buf[2] = (val >> 8); + buf[3] = (val >> 0); + return fwrite(buf, 4, 1, outf); } -static void readheader(inf,image) +static void +readheader(inf, image) FILE *inf; -IMAGE *image; + IMAGE *image; { - memset(image,0,sizeof(IMAGE)); + memset(image ,0, sizeof(IMAGE)); image->imagic = getshort(inf); - image->type = getshort(inf); - image->dim = getshort(inf); - image->xsize = getshort(inf); - image->ysize = getshort(inf); - image->zsize = getshort(inf); + image->type = getshort(inf); + image->dim = getshort(inf); + image->xsize = getshort(inf); + image->ysize = getshort(inf); + image->zsize = getshort(inf); } -static int writeheader(outf,image) +static int +writeheader(outf, image) FILE *outf; -IMAGE *image; + IMAGE *image; { IMAGE t; - memset(&t,0,sizeof(IMAGE)); - fwrite(&t,sizeof(IMAGE),1,outf); - fseek(outf,0,SEEK_SET); - putshort(outf,image->imagic); - putshort(outf,image->type); - putshort(outf,image->dim); - putshort(outf,image->xsize); - putshort(outf,image->ysize); - putshort(outf,image->zsize); - putlong(outf,image->min); - putlong(outf,image->max); - putlong(outf,0); - return fwrite("no name",8,1,outf); + memset(&t, 0, sizeof(IMAGE)); + fwrite(&t, sizeof(IMAGE), 1, outf); + fseek(outf, 0, SEEK_SET); + putshort(outf, image->imagic); + putshort(outf, image->type); + putshort(outf, image->dim); + putshort(outf, image->xsize); + putshort(outf, image->ysize); + putshort(outf, image->zsize); + putlong(outf, image->min); + putlong(outf, image->max); + putlong(outf, 0); + return fwrite("no name", 8, 1, outf); } -static int writetab(outf,tab,len) +static int +writetab(outf, tab, len) FILE *outf; -/*unsigned*/ long *tab; -int len; + /*unsigned*/ long *tab; + int len; { int r = 0; while(len) { - r = putlong(outf,*tab++); + r = putlong(outf, *tab++); len -= 4; } return r; } -static void readtab(inf,tab,len) +static void +readtab(inf, tab, len) FILE *inf; -/*unsigned*/ long *tab; -int len; + /*unsigned*/ long *tab; + int len; { while(len) { *tab++ = getlong(inf); @@ -240,14 +249,14 @@ sizeofimage(self, args) if (!PyArg_Parse(args, "s", &name)) return NULL; - inf = fopen(name,"r"); - if(!inf) { + inf = fopen(name, "r"); + if (!inf) { PyErr_SetString(ImgfileError, "can't open image file"); return NULL; } - readheader(inf,&image); + readheader(inf, &image); fclose(inf); - if(image.imagic != IMAGIC) { + if (image.imagic != IMAGIC) { PyErr_SetString(ImgfileError, "bad magic number in image file"); return NULL; @@ -267,107 +276,104 @@ longimagedata(self, args) { char *name; unsigned char *base, *lptr; - unsigned char *rledat, *verdat; - long *starttab, *lengthtab; - FILE *inf; + unsigned char *rledat = NULL, *verdat = NULL; + long *starttab = NULL, *lengthtab = NULL; + FILE *inf = NULL; IMAGE image; int y, z, tablen; int xsize, ysize, zsize; int bpp, rle, cur, badorder; int rlebuflen; - PyObject *rv; + PyObject *rv = NULL; if (!PyArg_Parse(args, "s", &name)) return NULL; inf = fopen(name,"r"); - if(!inf) { - PyErr_SetString(ImgfileError,"can't open image file"); + if (!inf) { + PyErr_SetString(ImgfileError, "can't open image file"); return NULL; } readheader(inf,&image); - if(image.imagic != IMAGIC) { - PyErr_SetString(ImgfileError,"bad magic number in image file"); - fclose(inf); - return NULL; + if (image.imagic != IMAGIC) { + PyErr_SetString(ImgfileError, + "bad magic number in image file"); + goto finally; } rle = ISRLE(image.type); bpp = BPP(image.type); - if(bpp != 1 ) { + if (bpp != 1) { PyErr_SetString(ImgfileError, "image must have 1 byte per pix chan"); - fclose(inf); - return NULL; + goto finally; } xsize = image.xsize; ysize = image.ysize; zsize = image.zsize; - if(rle) { - tablen = ysize*zsize*sizeof(long); + if (rle) { + tablen = ysize * zsize * sizeof(long); starttab = (long *)malloc(tablen); lengthtab = (long *)malloc(tablen); - rlebuflen = 1.05*xsize+10; + rlebuflen = 1.05 * xsize +10; rledat = (unsigned char *)malloc(rlebuflen); - fseek(inf,512,SEEK_SET); - readtab(inf,starttab,tablen); - readtab(inf,lengthtab,tablen); + if (!starttab || !lengthtab || !rledat) { + PyErr_NoMemory(); + goto finally; + } + + fseek(inf, 512, SEEK_SET); + readtab(inf, starttab, tablen); + readtab(inf, lengthtab, tablen); -/* check data order */ + /* check data order */ cur = 0; badorder = 0; - for(y=0; yrlebuflen) { + if (lengthtab[idx] > rlebuflen) { PyErr_SetString(ImgfileError, - "rlebuf is too small - bad poop"); - fclose(inf); + "rlebuf is too small"); Py_DECREF(rv); - free(rledat); - free(starttab); - free(lengthtab); - return NULL; + rv = NULL; + goto finally; } - fread(rledat,lengthtab[y+z*ysize], - 1,inf); - cur += lengthtab[y+z*ysize]; - expandrow(lptr,rledat,3-z); + fread(rledat, lengthtab[idx], 1, inf); + cur += lengthtab[idx]; + expandrow(lptr, rledat, 3-z); if (reverse_order) lptr -= xsize * sizeof(unsigned long); @@ -381,17 +387,17 @@ longimagedata(self, args) if (reverse_order) lptr += (ysize - 1) * xsize * sizeof(unsigned long); - for(y=0; y=8) { - lptr[0] = 0xff000000+(0x010101*(lptr[0]&0xff)); - lptr[1] = 0xff000000+(0x010101*(lptr[1]&0xff)); - lptr[2] = 0xff000000+(0x010101*(lptr[2]&0xff)); - lptr[3] = 0xff000000+(0x010101*(lptr[3]&0xff)); - lptr[4] = 0xff000000+(0x010101*(lptr[4]&0xff)); - lptr[5] = 0xff000000+(0x010101*(lptr[5]&0xff)); - lptr[6] = 0xff000000+(0x010101*(lptr[6]&0xff)); - lptr[7] = 0xff000000+(0x010101*(lptr[7]&0xff)); + while (n >= 8) { + lptr[0] = 0xff000000 + (0x010101 * (lptr[0] & 0xff)); + lptr[1] = 0xff000000 + (0x010101 * (lptr[1] & 0xff)); + lptr[2] = 0xff000000 + (0x010101 * (lptr[2] & 0xff)); + lptr[3] = 0xff000000 + (0x010101 * (lptr[3] & 0xff)); + lptr[4] = 0xff000000 + (0x010101 * (lptr[4] & 0xff)); + lptr[5] = 0xff000000 + (0x010101 * (lptr[5] & 0xff)); + lptr[6] = 0xff000000 + (0x010101 * (lptr[6] & 0xff)); + lptr[7] = 0xff000000 + (0x010101 * (lptr[7] & 0xff)); lptr += 8; - n-=8; + n -= 8; } - while(n--) { - *lptr = 0xff000000+(0x010101*(*lptr&0xff)); + while (n--) { + *lptr = 0xff000000 + (0x010101 * (*lptr&0xff)); lptr++; } } -static void setalpha(lptr,n) +static void +setalpha(lptr, n) unsigned char *lptr; { - while(n>=8) { - lptr[0*4] = 0xff; - lptr[1*4] = 0xff; - lptr[2*4] = 0xff; - lptr[3*4] = 0xff; - lptr[4*4] = 0xff; - lptr[5*4] = 0xff; - lptr[6*4] = 0xff; - lptr[7*4] = 0xff; - lptr += 4*8; + while (n >= 8) { + lptr[0 * 4] = 0xff; + lptr[1 * 4] = 0xff; + lptr[2 * 4] = 0xff; + lptr[3 * 4] = 0xff; + lptr[4 * 4] = 0xff; + lptr[5 * 4] = 0xff; + lptr[6 * 4] = 0xff; + lptr[7 * 4] = 0xff; + lptr += 4 * 8; n -= 8; } - while(n--) { + while (n--) { *lptr = 0xff; lptr += 4; } } -static void expandrow(optr,iptr,z) +static void +expandrow(optr, iptr, z) unsigned char *optr, *iptr; -int z; + int z; { unsigned char pixel, count; optr += z; - while(1) { + while (1) { pixel = *iptr++; - if ( !(count = (pixel & 0x7f)) ) + if (!(count = (pixel & 0x7f))) return; - if(pixel & 0x80) { - while(count>=8) { - optr[0*4] = iptr[0]; - optr[1*4] = iptr[1]; - optr[2*4] = iptr[2]; - optr[3*4] = iptr[3]; - optr[4*4] = iptr[4]; - optr[5*4] = iptr[5]; - optr[6*4] = iptr[6]; - optr[7*4] = iptr[7]; - optr += 8*4; + if (pixel & 0x80) { + while (count >= 8) { + optr[0 * 4] = iptr[0]; + optr[1 * 4] = iptr[1]; + optr[2 * 4] = iptr[2]; + optr[3 * 4] = iptr[3]; + optr[4 * 4] = iptr[4]; + optr[5 * 4] = iptr[5]; + optr[6 * 4] = iptr[6]; + optr[7 * 4] = iptr[7]; + optr += 8 * 4; iptr += 8; count -= 8; } - while(count--) { + while (count--) { *optr = *iptr++; - optr+=4; + optr += 4; } - } else { + } + else { pixel = *iptr++; - while(count>=8) { - optr[0*4] = pixel; - optr[1*4] = pixel; - optr[2*4] = pixel; - optr[3*4] = pixel; - optr[4*4] = pixel; - optr[5*4] = pixel; - optr[6*4] = pixel; - optr[7*4] = pixel; - optr += 8*4; + while (count >= 8) { + optr[0 * 4] = pixel; + optr[1 * 4] = pixel; + optr[2 * 4] = pixel; + optr[3 * 4] = pixel; + optr[4 * 4] = pixel; + optr[5 * 4] = pixel; + optr[6 * 4] = pixel; + optr[7 * 4] = pixel; + optr += 8 * 4; count -= 8; } - while(count--) { + while (count--) { *optr = pixel; - optr+=4; + optr += 4; } } } @@ -570,36 +580,41 @@ longstoimage(self, args) unsigned char *lptr; char *name; int xsize, ysize, zsize; - FILE *outf; + FILE *outf = NULL; IMAGE image; int tablen, y, z, pos, len; - long *starttab, *lengthtab; - unsigned char *rlebuf; - unsigned char *lumbuf; + long *starttab = NULL, *lengthtab = NULL; + unsigned char *rlebuf = NULL; + unsigned char *lumbuf = NULL; int rlebuflen, goodwrite; + PyObject *retval = NULL; if (!PyArg_Parse(args, "(s#iiis)", &lptr, &len, &xsize, &ysize, &zsize, &name)) return NULL; goodwrite = 1; - outf = fopen(name,"w"); - if(!outf) { - PyErr_SetString(ImgfileError,"can't open output file"); + outf = fopen(name, "w"); + if (!outf) { + PyErr_SetString(ImgfileError, "can't open output file"); return NULL; } - tablen = ysize*zsize*sizeof(long); + tablen = ysize * zsize * sizeof(long); starttab = (long *)malloc(tablen); lengthtab = (long *)malloc(tablen); - rlebuflen = 1.05*xsize+10; + rlebuflen = 1.05 * xsize + 10; rlebuf = (unsigned char *)malloc(rlebuflen); - lumbuf = (unsigned char *)malloc(xsize*sizeof(long)); - - memset(&image,0,sizeof(IMAGE)); + lumbuf = (unsigned char *)malloc(xsize * sizeof(long)); + if (!starttab || !lengthtab || !rlebuf || !lumbuf) { + PyErr_NoMemory(); + goto finally; + } + + memset(&image, 0, sizeof(IMAGE)); image.imagic = IMAGIC; image.type = RLE(1); - if(zsize>1) + if (zsize>1) image.dim = 3; else image.dim = 2; @@ -608,34 +623,29 @@ longstoimage(self, args) image.zsize = zsize; image.min = 0; image.max = 255; - goodwrite *= writeheader(outf,&image); - fseek(outf,512+2*tablen,SEEK_SET); - pos = 512+2*tablen; + goodwrite *= writeheader(outf, &image); + pos = 512 + 2 * tablen; + fseek(outf, pos, SEEK_SET); if (reverse_order) lptr += (ysize - 1) * xsize * sizeof(unsigned long); - for(y=0; yrlebuflen) { + if(len > rlebuflen) { PyErr_SetString(ImgfileError, - "rlebuf is too small - bad poop"); - free(starttab); - free(lengthtab); - free(rlebuf); - free(lumbuf); - fclose(outf); - return NULL; + "rlebuf is too small"); + goto finally; } - goodwrite *= fwrite(rlebuf,len,1,outf); - starttab[y+z*ysize] = pos; - lengthtab[y+z*ysize] = len; + goodwrite *= fwrite(rlebuf, len, 1, outf); + starttab[y + z * ysize] = pos; + lengthtab[y + z * ysize] = len; pos += len; } if (reverse_order) @@ -644,41 +654,45 @@ longstoimage(self, args) lptr += xsize * sizeof(unsigned long); } - fseek(outf,512,SEEK_SET); - goodwrite *= writetab(outf,starttab,tablen); - goodwrite *= writetab(outf,lengthtab,tablen); + fseek(outf, 512, SEEK_SET); + goodwrite *= writetab(outf, starttab, tablen); + goodwrite *= writetab(outf, lengthtab, tablen); + if (goodwrite) { + Py_INCREF(Py_None); + retval = Py_None; + } else + PyErr_SetString(ImgfileError, "not enough space for image"); + + finally: + fclose(outf); free(starttab); free(lengthtab); free(rlebuf); free(lumbuf); - fclose(outf); - if(goodwrite) { - Py_INCREF(Py_None); - return Py_None; - } else { - PyErr_SetString(ImgfileError,"not enough space for image!!"); - return NULL; - } + return retval; } /* static utility functions for longstoimage */ -static void lumrow(rgbptr,lumptr,n) +static void +lumrow(rgbptr, lumptr, n) unsigned char *rgbptr, *lumptr; -int n; + int n; { lumptr += CHANOFFSET(0); - while(n--) { - *lumptr = ILUM(rgbptr[OFFSET_R],rgbptr[OFFSET_G], + while (n--) { + *lumptr = ILUM(rgbptr[OFFSET_R], + rgbptr[OFFSET_G], rgbptr[OFFSET_B]); lumptr += 4; rgbptr += 4; } } -static int compressrow(lbuf,rlebuf,z,cnt) +static int +compressrow(lbuf, rlebuf, z, cnt) unsigned char *lbuf, *rlebuf; -int z, cnt; + int z, cnt; { unsigned char *iptr, *ibufend, *sptr, *optr; short todo, cc; @@ -686,35 +700,37 @@ int z, cnt; lbuf += z; iptr = lbuf; - ibufend = iptr+cnt*4; + ibufend = iptr + cnt * 4; optr = rlebuf; - while(iptr126 ? 126:count; + count = (iptr - sptr) / 4; + while (count) { + todo = count > 126 ? 126 : count; count -= todo; - *optr++ = 0x80|todo; - while(todo>8) { - optr[0] = sptr[0*4]; - optr[1] = sptr[1*4]; - optr[2] = sptr[2*4]; - optr[3] = sptr[3*4]; - optr[4] = sptr[4*4]; - optr[5] = sptr[5*4]; - optr[6] = sptr[6*4]; - optr[7] = sptr[7*4]; + *optr++ = 0x80 | todo; + while (todo > 8) { + optr[0] = sptr[0 * 4]; + optr[1] = sptr[1 * 4]; + optr[2] = sptr[2 * 4]; + optr[3] = sptr[3 * 4]; + optr[4] = sptr[4 * 4]; + optr[5] = sptr[5 * 4]; + optr[6] = sptr[6 * 4]; + optr[7] = sptr[7 * 4]; optr += 8; - sptr += 8*4; + sptr += 8 * 4; todo -= 8; } - while(todo--) { + while (todo--) { *optr++ = *sptr; sptr += 4; } @@ -722,11 +738,11 @@ int z, cnt; sptr = iptr; cc = *iptr; iptr += 4; - while( (iptr126 ? 126:count; + count = (iptr - sptr) / 4; + while (count) { + todo = count > 126 ? 126 : count; count -= todo; *optr++ = todo; *optr++ = cc; @@ -739,7 +755,7 @@ int z, cnt; static PyObject * ttob(self, args) PyObject *self; -PyObject *args; + PyObject *args; { int order, oldorder; @@ -750,14 +766,16 @@ PyObject *args; return PyInt_FromLong(oldorder); } -static PyMethodDef rgbimg_methods[] = { - {"sizeofimage", sizeofimage}, - {"longimagedata", longimagedata}, - {"longstoimage", longstoimage}, - {"ttob", ttob}, - {NULL, NULL} /* sentinel */ +static PyMethodDef +rgbimg_methods[] = { + {"sizeofimage", sizeofimage}, + {"longimagedata", longimagedata}, + {"longstoimage", longstoimage}, + {"ttob", ttob}, + {NULL, NULL} /* sentinel */ }; + void initrgbimg() { @@ -765,20 +783,8 @@ initrgbimg() m = Py_InitModule("rgbimg", rgbimg_methods); d = PyModule_GetDict(m); ImgfileError = PyString_FromString("rgbimg.error"); - if (ImgfileError == NULL - || PyDict_SetItemString(d, "error", ImgfileError)) - Py_FatalError("can't define rgbimg.error"); + if (ImgfileError) + PyDict_SetItemString(d, "error", ImgfileError); + if (PyErr_Occurred()) + Py_FatalError("can't initialize rgbimg module"); } - - - - - - - - - - - - -