Primarily formatting changes, but I also plugged a couple of potential
return value problems, memory leaks, and descriptor leaks.
This commit is contained in:
parent
4bc9d39560
commit
2dc8c2c26b
|
@ -106,6 +106,7 @@ static int reverse_order;
|
|||
* this is used to extract image data from core dumps.
|
||||
*
|
||||
*/
|
||||
static void
|
||||
addlongimgtag(dptr, xsize, ysize)
|
||||
unsigned long *dptr;
|
||||
int xsize, ysize;
|
||||
|
@ -123,7 +124,8 @@ 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];
|
||||
|
@ -132,7 +134,8 @@ static unsigned short getshort(inf)
|
|||
return (buf[0] << 8) + (buf[1] << 0);
|
||||
}
|
||||
|
||||
static unsigned long getlong(inf)
|
||||
static unsigned long
|
||||
getlong(inf)
|
||||
FILE *inf;
|
||||
{
|
||||
unsigned char buf[4];
|
||||
|
@ -141,7 +144,8 @@ static unsigned long getlong(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;
|
||||
{
|
||||
|
@ -152,7 +156,8 @@ unsigned short val;
|
|||
fwrite(buf, 2, 1, outf);
|
||||
}
|
||||
|
||||
static int putlong(outf,val)
|
||||
static int
|
||||
putlong(outf, val)
|
||||
FILE *outf;
|
||||
unsigned long val;
|
||||
{
|
||||
|
@ -165,7 +170,8 @@ unsigned long val;
|
|||
return fwrite(buf, 4, 1, outf);
|
||||
}
|
||||
|
||||
static void readheader(inf,image)
|
||||
static void
|
||||
readheader(inf, image)
|
||||
FILE *inf;
|
||||
IMAGE *image;
|
||||
{
|
||||
|
@ -178,7 +184,8 @@ IMAGE *image;
|
|||
image->zsize = getshort(inf);
|
||||
}
|
||||
|
||||
static int writeheader(outf,image)
|
||||
static int
|
||||
writeheader(outf, image)
|
||||
FILE *outf;
|
||||
IMAGE *image;
|
||||
{
|
||||
|
@ -199,7 +206,8 @@ IMAGE *image;
|
|||
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;
|
||||
|
@ -213,7 +221,8 @@ int len;
|
|||
return r;
|
||||
}
|
||||
|
||||
static void readtab(inf,tab,len)
|
||||
static void
|
||||
readtab(inf, tab, len)
|
||||
FILE *inf;
|
||||
/*unsigned*/ long *tab;
|
||||
int len;
|
||||
|
@ -267,15 +276,15 @@ 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;
|
||||
|
@ -287,17 +296,16 @@ longimagedata(self, args)
|
|||
}
|
||||
readheader(inf,&image);
|
||||
if (image.imagic != IMAGIC) {
|
||||
PyErr_SetString(ImgfileError,"bad magic number in image file");
|
||||
fclose(inf);
|
||||
return NULL;
|
||||
PyErr_SetString(ImgfileError,
|
||||
"bad magic number in image file");
|
||||
goto finally;
|
||||
}
|
||||
rle = ISRLE(image.type);
|
||||
bpp = BPP(image.type);
|
||||
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;
|
||||
|
@ -308,6 +316,11 @@ longimagedata(self, args)
|
|||
lengthtab = (long *)malloc(tablen);
|
||||
rlebuflen = 1.05 * xsize +10;
|
||||
rledat = (unsigned char *)malloc(rlebuflen);
|
||||
if (!starttab || !lengthtab || !rledat) {
|
||||
PyErr_NoMemory();
|
||||
goto finally;
|
||||
}
|
||||
|
||||
fseek(inf, 512, SEEK_SET);
|
||||
readtab(inf, starttab, tablen);
|
||||
readtab(inf, lengthtab, tablen);
|
||||
|
@ -329,15 +342,11 @@ longimagedata(self, args)
|
|||
|
||||
fseek(inf, 512 + 2 * tablen, SEEK_SET);
|
||||
cur = 512 + 2 * tablen;
|
||||
rv = PyString_FromStringAndSize((char *) 0,
|
||||
rv = PyString_FromStringAndSize((char *)NULL,
|
||||
(xsize * ysize + TAGLEN) * sizeof(long));
|
||||
if (rv == NULL) {
|
||||
fclose(inf);
|
||||
free(lengthtab);
|
||||
free(starttab);
|
||||
free(rledat);
|
||||
return NULL;
|
||||
}
|
||||
if (rv == NULL)
|
||||
goto finally;
|
||||
|
||||
base = (unsigned char *) PyString_AsString(rv);
|
||||
#ifdef ADD_TAGS
|
||||
addlongimgtag(base,xsize,ysize);
|
||||
|
@ -349,24 +358,21 @@ longimagedata(self, args)
|
|||
lptr += (ysize - 1) * xsize
|
||||
* sizeof(unsigned long);
|
||||
for (y = 0; y < ysize; y++) {
|
||||
if(cur != starttab[y+z*ysize]) {
|
||||
fseek(inf,starttab[y+z*ysize],
|
||||
int idx = y + z * ysize;
|
||||
if (cur != starttab[idx]) {
|
||||
fseek(inf,starttab[idx],
|
||||
SEEK_SET);
|
||||
cur = starttab[y+z*ysize];
|
||||
cur = starttab[idx];
|
||||
}
|
||||
if(lengthtab[y+z*ysize]>rlebuflen) {
|
||||
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];
|
||||
fread(rledat, lengthtab[idx], 1, inf);
|
||||
cur += lengthtab[idx];
|
||||
expandrow(lptr, rledat, 3-z);
|
||||
if (reverse_order)
|
||||
lptr -= xsize
|
||||
|
@ -383,14 +389,14 @@ longimagedata(self, args)
|
|||
* sizeof(unsigned long);
|
||||
for (y = 0; y < ysize; y++) {
|
||||
for(z = 0; z < zsize; z++) {
|
||||
if(cur != starttab[y+z*ysize]) {
|
||||
fseek(inf,starttab[y+z*ysize],
|
||||
int idx = y + z * ysize;
|
||||
if (cur != starttab[idx]) {
|
||||
fseek(inf, starttab[idx],
|
||||
SEEK_SET);
|
||||
cur = starttab[y+z*ysize];
|
||||
cur = starttab[idx];
|
||||
}
|
||||
fread(rledat,lengthtab[y+z*ysize],
|
||||
1,inf);
|
||||
cur += lengthtab[y+z*ysize];
|
||||
fread(rledat, lengthtab[idx], 1, inf);
|
||||
cur += lengthtab[idx];
|
||||
expandrow(lptr, rledat, 3-z);
|
||||
}
|
||||
if (reverse_order)
|
||||
|
@ -403,18 +409,13 @@ longimagedata(self, args)
|
|||
setalpha(base, xsize * ysize);
|
||||
else if (zsize < 3)
|
||||
copybw((long *) base, xsize * ysize);
|
||||
fclose(inf);
|
||||
free(starttab);
|
||||
free(lengthtab);
|
||||
free(rledat);
|
||||
return rv;
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
rv = PyString_FromStringAndSize((char *) 0,
|
||||
(xsize*ysize+TAGLEN)*sizeof(long));
|
||||
if (rv == NULL) {
|
||||
fclose(inf);
|
||||
return NULL;
|
||||
}
|
||||
if (rv == NULL)
|
||||
goto finally;
|
||||
|
||||
base = (unsigned char *) PyString_AsString(rv);
|
||||
#ifdef ADD_TAGS
|
||||
addlongimgtag(base, xsize, ysize);
|
||||
|
@ -439,15 +440,20 @@ longimagedata(self, args)
|
|||
setalpha(base, xsize * ysize);
|
||||
else if (zsize < 3)
|
||||
copybw((long *) base, xsize * ysize);
|
||||
fclose(inf);
|
||||
free(verdat);
|
||||
return rv;
|
||||
}
|
||||
finally:
|
||||
free(starttab);
|
||||
free(lengthtab);
|
||||
free(rledat);
|
||||
free(verdat);
|
||||
fclose(inf);
|
||||
return rv;
|
||||
}
|
||||
|
||||
/* static utility functions for longimagedata */
|
||||
|
||||
static void interleaverow(lptr,cptr,z,n)
|
||||
static void
|
||||
interleaverow(lptr, cptr, z, n)
|
||||
unsigned char *lptr, *cptr;
|
||||
int z, n;
|
||||
{
|
||||
|
@ -458,7 +464,8 @@ int z, n;
|
|||
}
|
||||
}
|
||||
|
||||
static void copybw(lptr,n)
|
||||
static void
|
||||
copybw(lptr, n)
|
||||
long *lptr;
|
||||
int n;
|
||||
{
|
||||
|
@ -480,7 +487,8 @@ int n;
|
|||
}
|
||||
}
|
||||
|
||||
static void setalpha(lptr,n)
|
||||
static void
|
||||
setalpha(lptr, n)
|
||||
unsigned char *lptr;
|
||||
{
|
||||
while (n >= 8) {
|
||||
|
@ -501,7 +509,8 @@ static void setalpha(lptr,n)
|
|||
}
|
||||
}
|
||||
|
||||
static void expandrow(optr,iptr,z)
|
||||
static void
|
||||
expandrow(optr, iptr, z)
|
||||
unsigned char *optr, *iptr;
|
||||
int z;
|
||||
{
|
||||
|
@ -530,7 +539,8 @@ int z;
|
|||
*optr = *iptr++;
|
||||
optr += 4;
|
||||
}
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
pixel = *iptr++;
|
||||
while (count >= 8) {
|
||||
optr[0 * 4] = pixel;
|
||||
|
@ -570,13 +580,14 @@ 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))
|
||||
|
@ -595,6 +606,10 @@ longstoimage(self, args)
|
|||
rlebuflen = 1.05 * xsize + 10;
|
||||
rlebuf = (unsigned char *)malloc(rlebuflen);
|
||||
lumbuf = (unsigned char *)malloc(xsize * sizeof(long));
|
||||
if (!starttab || !lengthtab || !rlebuf || !lumbuf) {
|
||||
PyErr_NoMemory();
|
||||
goto finally;
|
||||
}
|
||||
|
||||
memset(&image, 0, sizeof(IMAGE));
|
||||
image.imagic = IMAGIC;
|
||||
|
@ -609,29 +624,24 @@ longstoimage(self, args)
|
|||
image.min = 0;
|
||||
image.max = 255;
|
||||
goodwrite *= writeheader(outf, &image);
|
||||
fseek(outf,512+2*tablen,SEEK_SET);
|
||||
pos = 512 + 2 * tablen;
|
||||
fseek(outf, pos, SEEK_SET);
|
||||
if (reverse_order)
|
||||
lptr += (ysize - 1) * xsize * sizeof(unsigned long);
|
||||
for (y = 0; y < ysize; y++) {
|
||||
for (z = 0; z < zsize; z++) {
|
||||
if (zsize == 1) {
|
||||
lumrow(lptr, lumbuf, xsize);
|
||||
len = compressrow(lumbuf,rlebuf,CHANOFFSET(z),
|
||||
xsize);
|
||||
len = compressrow(lumbuf, rlebuf,
|
||||
CHANOFFSET(z), xsize);
|
||||
} else {
|
||||
len = compressrow(lptr,rlebuf,CHANOFFSET(z),
|
||||
xsize);
|
||||
len = compressrow(lptr, rlebuf,
|
||||
CHANOFFSET(z), xsize);
|
||||
}
|
||||
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;
|
||||
|
@ -647,36 +657,40 @@ longstoimage(self, args)
|
|||
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;
|
||||
{
|
||||
lumptr += CHANOFFSET(0);
|
||||
while (n--) {
|
||||
*lumptr = ILUM(rgbptr[OFFSET_R],rgbptr[OFFSET_G],
|
||||
*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;
|
||||
{
|
||||
|
@ -692,9 +706,11 @@ int z, cnt;
|
|||
while(iptr < ibufend) {
|
||||
sptr = iptr;
|
||||
iptr += 8;
|
||||
while((iptr<ibufend)&& ((iptr[-8]!=iptr[-4])
|
||||
||(iptr[-4]!=iptr[0])))
|
||||
while ((iptr<ibufend) &&
|
||||
((iptr[-8]!=iptr[-4]) ||(iptr[-4]!=iptr[0])))
|
||||
{
|
||||
iptr += 4;
|
||||
}
|
||||
iptr -= 8;
|
||||
count = (iptr - sptr) / 4;
|
||||
while (count) {
|
||||
|
@ -750,7 +766,8 @@ PyObject *args;
|
|||
return PyInt_FromLong(oldorder);
|
||||
}
|
||||
|
||||
static PyMethodDef rgbimg_methods[] = {
|
||||
static PyMethodDef
|
||||
rgbimg_methods[] = {
|
||||
{"sizeofimage", sizeofimage},
|
||||
{"longimagedata", longimagedata},
|
||||
{"longstoimage", longstoimage},
|
||||
|
@ -758,6 +775,7 @@ static PyMethodDef rgbimg_methods[] = {
|
|||
{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");
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue