CVS update: php3/functions

From: Date: Fri, 09 Oct 1998 16:16:06 +0000
Subject: CVS update: php3/functions
Groups: php.dev 
Request: Send a blank email to php-dev+get-1766@lists.php.net to get a copy of this message
Date: Friday October 9, 1998 @ 12:16 Author: ssb Update of /repository/php3/functions In directory asf:/u2/tmp/cvs-serv3019 Modified Files: image.c Log Message: add Thies's GetImageSize() optimization. Index: php3/functions/image.c diff -c php3/functions/image.c:1.35 php3/functions/image.c:1.36 *** php3/functions/image.c:1.35 Fri Aug 14 19:47:16 1998 --- php3/functions/image.c Fri Oct 9 12:16:05 1998 *************** *** 26,32 **** | Authors: Rasmus Lerdorf | +----------------------------------------------------------------------+ */ ! /* $Id: image.c,v 1.35 1998/08/14 23:47:16 steffann Exp $ */ /* * Based on Daniel Schmitt's imageinfo.c which carried the following * Copyright notice. --- 26,32 ---- | Authors: Rasmus Lerdorf | +----------------------------------------------------------------------+ */ ! /* $Id: image.c,v 1.36 1998/10/09 16:16:05 ssb Exp $ */ /* * Based on Daniel Schmitt's imageinfo.c which carried the following * Copyright notice. *************** *** 76,117 **** }; /* routine to handle GIF files. If only everything were that easy... ;} */ ! static struct gfxinfo *php3_handle_gif (int infile) { struct gfxinfo *result = NULL; unsigned char a[2]; result = (struct gfxinfo *) emalloc(sizeof(struct gfxinfo)); ! lseek(infile, 6L, 0); ! read(infile, a, 2); result->width = (unsigned short)a[0] | (((unsigned short)a[1])<<8); ! read(infile, a, 2); result->height = (unsigned short)a[0] | (((unsigned short)a[1])<<8); return result; } ! static unsigned long php3_read4(int infile) { ! unsigned char c1, c2, c3, c4; /* just return 0 if we hit the end-of-file */ ! if (!read(infile, &c1, 1)) return 0; ! if (!read(infile, &c2, 1)) return 0; ! if (!read(infile, &c3, 1)) return 0; ! if (!read(infile, &c4, 1)) return 0; ! return (((unsigned long) c1) << 24) + (((unsigned long) c2) << 16) + (((unsigned long) c3) << 8) + ((unsigned long) c4); } /* routine to handle PNG files. - even easier */ ! static struct gfxinfo *php3_handle_png(int infile) { struct gfxinfo *result = NULL; unsigned long in_width, in_height; result = (struct gfxinfo *) emalloc(sizeof(struct gfxinfo)); ! lseek(infile, 16L, 0); ! in_width = php3_read4(infile); ! in_height = php3_read4(infile); result->width = (unsigned int) in_width; result->height = (unsigned int) in_height; return result; --- 76,116 ---- }; /* routine to handle GIF files. If only everything were that easy... ;} */ ! static struct gfxinfo *php3_handle_gif (FILE *fp) { struct gfxinfo *result = NULL; unsigned char a[2]; result = (struct gfxinfo *) emalloc(sizeof(struct gfxinfo)); ! fseek(fp, 6L, SEEK_SET); ! fread(a,sizeof(a),1,fp); result->width = (unsigned short)a[0] | (((unsigned short)a[1])<<8); ! fread(a,sizeof(a),1,fp); result->height = (unsigned short)a[0] | (((unsigned short)a[1])<<8); return result; } ! static unsigned long php3_read4(FILE *fp) { ! unsigned char a[ 4 ]; /* just return 0 if we hit the end-of-file */ ! if (fread(a,sizeof(a),1,fp) != 1) return 0; ! ! return (((unsigned long) a[ 0 ]) << 24) + (((unsigned long) a[ 1 ]) << 16) + (((unsigned long) a[ 2 ]) << 8) + ((unsigned long) a[ 3 ]); ! } /* routine to handle PNG files. - even easier */ ! static struct gfxinfo *php3_handle_png(FILE *fp) { struct gfxinfo *result = NULL; unsigned long in_width, in_height; result = (struct gfxinfo *) emalloc(sizeof(struct gfxinfo)); ! fseek(fp, 16L, SEEK_SET); ! in_width = php3_read4(fp); ! in_height = php3_read4(fp); result->width = (unsigned int) in_width; result->height = (unsigned int) in_height; return result; *************** *** 136,192 **** #define M_EOI 0xD9 /* End Of Image (end of datastream) */ #define M_SOS 0xDA /* Start Of Scan (begins compressed data) */ ! static unsigned short php3_read2(int infile) { ! unsigned char c1, c2; /* just return 0 if we hit the end-of-file */ ! if (!read(infile, &c1, 1)) return 0; ! if (!read(infile, &c2, 1)) return 0; ! return (((unsigned short) c1) << 8) + ((unsigned short) c2); } ! static unsigned int php3_next_marker(int infile) /* get next marker byte from file */ { ! unsigned char c; /* skip unimportant stuff */ ! read(infile, &c, 1); ! while (c != (unsigned char) 0xff) ! if (read(infile, &c, 1) != 1) ! return M_EOI; /* we hit EOF */ /* get marker byte, swallowing possible padding */ do { ! if (read(infile, &c, 1) != 1) return M_EOI; /* we hit EOF */ ! } while (c == (unsigned char) 0xff); return (unsigned int) c; } ! static void php3_skip_variable(int infile) /* skip over a variable-length block; assumes proper length marker */ { unsigned short length; ! length = php3_read2(infile); length -= 2; /* length includes itself */ ! lseek(infile, (long) length, 1); /* skip the header */ } ! static struct gfxinfo *php3_handle_jpeg(int infile) /* main loop to parse JPEG structure */ { struct gfxinfo *result = NULL; unsigned int marker; unsigned short in_width, in_height; ! lseek(infile, 2L, 0); /* position file pointer on first marker */ for (;;) { ! marker = php3_next_marker(infile); switch (marker) { case M_SOF0: case M_SOF1: --- 135,194 ---- #define M_EOI 0xD9 /* End Of Image (end of datastream) */ #define M_SOS 0xDA /* Start Of Scan (begins compressed data) */ ! static unsigned short php3_read2(FILE *fp) { ! unsigned char a[ 2 ]; /* just return 0 if we hit the end-of-file */ ! if (fread(a,sizeof(a),1,fp) != 1) return 0; ! ! return (((unsigned short) a[ 0 ]) << 8) + ((unsigned short) a[ 1 ]); } ! static unsigned int php3_next_marker(FILE *fp) /* get next marker byte from file */ { ! int c; /* skip unimportant stuff */ ! ! c = getc(fp); ! ! while (c != 0xff) { ! if ((c = getc(fp)) == EOF) ! return M_EOI; /* we hit EOF */ ! } /* get marker byte, swallowing possible padding */ do { ! if ((c = getc(fp)) == EOF) return M_EOI; /* we hit EOF */ ! } while (c == 0xff); return (unsigned int) c; } ! static void php3_skip_variable(FILE *fp) /* skip over a variable-length block; assumes proper length marker */ { unsigned short length; ! length = php3_read2(fp); length -= 2; /* length includes itself */ ! fseek(fp, (long) length, SEEK_CUR); /* skip the header */ } ! static struct gfxinfo *php3_handle_jpeg(FILE *fp) /* main loop to parse JPEG structure */ { struct gfxinfo *result = NULL; unsigned int marker; unsigned short in_width, in_height; ! fseek(fp, 2L, SEEK_CUR); /* position file pointer on first marker */ for (;;) { ! marker = php3_next_marker(fp); switch (marker) { case M_SOF0: case M_SOF1: *************** *** 202,210 **** case M_SOF14: case M_SOF15: /* handle SOFn block */ ! lseek(infile, 3L, 1); /* skip length and precision bytes */ ! in_height = php3_read2(infile); ! in_width = php3_read2(infile); /* fill a gfxinfo struct to return the data */ result = (struct gfxinfo *) emalloc(sizeof(struct gfxinfo)); result->width = (unsigned int) in_width; --- 204,212 ---- case M_SOF14: case M_SOF15: /* handle SOFn block */ ! fseek(fp, 3L, SEEK_CUR); /* skip length and precision bytes */ ! in_height = php3_read2(fp); ! in_width = php3_read2(fp); /* fill a gfxinfo struct to return the data */ result = (struct gfxinfo *) emalloc(sizeof(struct gfxinfo)); result->width = (unsigned int) in_width; *************** *** 216,222 **** return NULL; /* we're about to hit image data, or are at EOF. Error. */ break; default: ! php3_skip_variable(infile); /* anything else isn't interesting */ break; } } --- 218,224 ---- return NULL; /* we're about to hit image data, or are at EOF. Error. */ break; default: ! php3_skip_variable(fp); /* anything else isn't interesting */ break; } } *************** *** 226,232 **** void php3_getimagesize(INTERNAL_FUNCTION_PARAMETERS) { pval *arg1; ! int filehandle, itype = 0; char filetype[3]; char pngtype[8]; char temp[64]; --- 228,235 ---- void php3_getimagesize(INTERNAL_FUNCTION_PARAMETERS) { pval *arg1; ! FILE *fp; ! int itype = 0; char filetype[3]; char pngtype[8]; char temp[64]; *************** *** 240,267 **** /* Check open_basedir */ if (_php3_check_open_basedir(arg1->value.str.val)) return; ! if ((filehandle = open(arg1->value.str.val, O_RDONLY)) < 0) { php3_error(E_WARNING, "Unable to open %s", arg1->value.str.val); return; } ! read(filehandle, filetype, sizeof(filetype)); if (!memcmp(filetype, php3_sig_gif, 3)) { ! result = php3_handle_gif (filehandle); itype = 1; } else if (!memcmp(filetype, php3_sig_jpg, 3)) { ! result = php3_handle_jpeg(filehandle); itype = 2; } else if (!memcmp(filetype, php3_sig_png, 3)) { ! lseek(filehandle, 0L, 0); ! read(filehandle, pngtype, sizeof(pngtype)); if (!memcmp(pngtype, php3_sig_png, 8)) { ! result = php3_handle_png(filehandle); itype = 3; } else { php3_error(E_WARNING, "PNG file corrupted by ASCII conversion"); } } ! close(filehandle); if (result) { if (array_init(return_value) == FAILURE) { php3_error(E_ERROR, "Unable to initialize array"); --- 243,270 ---- /* Check open_basedir */ if (_php3_check_open_basedir(arg1->value.str.val)) return; ! if ((fp = fopen(arg1->value.str.val,"rb")) == 0) { php3_error(E_WARNING, "Unable to open %s", arg1->value.str.val); return; } ! fread(filetype,sizeof(filetype),1,fp); if (!memcmp(filetype, php3_sig_gif, 3)) { ! result = php3_handle_gif (fp); itype = 1; } else if (!memcmp(filetype, php3_sig_jpg, 3)) { ! result = php3_handle_jpeg(fp); itype = 2; } else if (!memcmp(filetype, php3_sig_png, 3)) { ! fseek(fp, 0L, SEEK_SET); ! fread(pngtype, sizeof(pngtype), 1, fp); if (!memcmp(pngtype, php3_sig_png, 8)) { ! result = php3_handle_png(fp); itype = 3; } else { php3_error(E_WARNING, "PNG file corrupted by ASCII conversion"); } } ! fclose(fp); if (result) { if (array_init(return_value) == FAILURE) { php3_error(E_ERROR, "Unable to initialize array"); -- PHP Development Mailing List http://www.php.net/ To unsubscribe send an empty message to php-dev-unsubscribe@lists.php.net For help: php-dev-help@lists.php.net

« previous php.dev (#1766) next »