PageRenderTime 55ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/hphp/runtime/ext/gd/libgd/gd_gif_in.cpp

https://gitlab.com/Blueprint-Marketing/hhvm
C++ | 636 lines | 490 code | 105 blank | 41 comment | 110 complexity | a2ad385ec8471f94d8989f87d71f68ea MD5 | raw file
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include "gd.h"
  6. #include "php.h"
  7. /* Used only when debugging GIF compression code */
  8. /* #define DEBUGGING_ENVARS */
  9. #ifdef DEBUGGING_ENVARS
  10. static int verbose_set = 0;
  11. static int verbose;
  12. #define VERBOSE (verbose_set?verbose:set_verbose())
  13. static int set_verbose(void)
  14. {
  15. verbose = !!getenv("GIF_VERBOSE");
  16. verbose_set = 1;
  17. return(verbose);
  18. }
  19. #else
  20. #define VERBOSE 0
  21. #endif
  22. #define MAXCOLORMAPSIZE 256
  23. #define TRUE 1
  24. #define FALSE 0
  25. #define CM_RED 0
  26. #define CM_GREEN 1
  27. #define CM_BLUE 2
  28. #define MAX_LWZ_BITS 12
  29. #define INTERLACE 0x40
  30. #define LOCALCOLORMAP 0x80
  31. #define BitSet(byte, bit) (((byte) & (bit)) == (bit))
  32. #define ReadOK(file,buffer,len) (gdGetBuf(buffer, len, file) > 0)
  33. #define LM_to_uint(a,b) (((b)<<8)|(a))
  34. /* We may eventually want to use this information, but def it out for now */
  35. #if 0
  36. static struct {
  37. unsigned int Width;
  38. unsigned int Height;
  39. unsigned char ColorMap[3][MAXCOLORMAPSIZE];
  40. unsigned int BitPixel;
  41. unsigned int ColorResolution;
  42. unsigned int Background;
  43. unsigned int AspectRatio;
  44. } GifScreen;
  45. #endif
  46. #if 0
  47. static struct {
  48. int transparent;
  49. int delayTime;
  50. int inputFlag;
  51. int disposal;
  52. } Gif89 = { -1, -1, -1, 0 };
  53. #endif
  54. #define STACK_SIZE ((1<<(MAX_LWZ_BITS))*2)
  55. typedef struct {
  56. unsigned char buf[280];
  57. int curbit, lastbit, done, last_byte;
  58. } CODE_STATIC_DATA;
  59. typedef struct {
  60. int fresh;
  61. int code_size, set_code_size;
  62. int max_code, max_code_size;
  63. int firstcode, oldcode;
  64. int clear_code, end_code;
  65. int table[2][(1<< MAX_LWZ_BITS)];
  66. int stack[STACK_SIZE], *sp;
  67. CODE_STATIC_DATA scd;
  68. } LZW_STATIC_DATA;
  69. static int ReadColorMap (gdIOCtx *fd, int number, unsigned char (*buffer)[256]);
  70. static int DoExtension (gdIOCtx *fd, int label, int *Transparent, int *ZeroDataBlockP);
  71. static int GetDataBlock (gdIOCtx *fd, unsigned char *buf, int *ZeroDataBlockP);
  72. static int GetCode (gdIOCtx *fd, CODE_STATIC_DATA *scd, int code_size, int flag, int *ZeroDataBlockP);
  73. static int LWZReadByte (gdIOCtx *fd, LZW_STATIC_DATA *sd, char flag, int input_code_size, int *ZeroDataBlockP);
  74. static void ReadImage (gdImagePtr im, gdIOCtx *fd, int len, int height, unsigned char (*cmap)[256], int interlace, int *ZeroDataBlockP); /*1.4//, int ignore); */
  75. gdImagePtr gdImageCreateFromGifSource(gdSourcePtr inSource) /* {{{ */
  76. {
  77. gdIOCtx *in = gdNewSSCtx(inSource, NULL);
  78. gdImagePtr im;
  79. im = gdImageCreateFromGifCtx(in);
  80. in->gd_free(in);
  81. return im;
  82. }
  83. /* }}} */
  84. gdImagePtr gdImageCreateFromGif(FILE *fdFile) /* {{{ */
  85. {
  86. gdIOCtx *fd = gdNewFileCtx(fdFile);
  87. gdImagePtr im = 0;
  88. im = gdImageCreateFromGifCtx(fd);
  89. fd->gd_free(fd);
  90. return im;
  91. }
  92. /* }}} */
  93. gdImagePtr gdImageCreateFromGifCtx(gdIOCtxPtr fd) /* {{{ */
  94. {
  95. int BitPixel;
  96. #if 0
  97. int ColorResolution;
  98. int Background;
  99. int AspectRatio;
  100. #endif
  101. int Transparent = (-1);
  102. unsigned char buf[16];
  103. unsigned char c;
  104. unsigned char ColorMap[3][MAXCOLORMAPSIZE];
  105. unsigned char localColorMap[3][MAXCOLORMAPSIZE];
  106. int imw, imh, screen_width, screen_height;
  107. int useGlobalColormap;
  108. int bitPixel;
  109. int i;
  110. /*1.4//int imageCount = 0; */
  111. int ZeroDataBlock = FALSE;
  112. int haveGlobalColormap;
  113. gdImagePtr im = 0;
  114. /*1.4//imageNumber = 1; */
  115. if (! ReadOK(fd,buf,6)) {
  116. return 0;
  117. }
  118. if (strncmp((char *)buf,"GIF",3) != 0) {
  119. return 0;
  120. }
  121. if (memcmp((char *)buf+3, "87a", 3) == 0) {
  122. } else if (memcmp((char *)buf+3, "89a", 3) == 0) {
  123. } else {
  124. return 0;
  125. }
  126. if (! ReadOK(fd,buf,7)) {
  127. return 0;
  128. }
  129. BitPixel = 2<<(buf[4]&0x07);
  130. #if 0
  131. ColorResolution = (int) (((buf[4]&0x70)>>3)+1);
  132. Background = buf[5];
  133. AspectRatio = buf[6];
  134. #endif
  135. screen_width = imw = LM_to_uint(buf[0],buf[1]);
  136. screen_height = imh = LM_to_uint(buf[2],buf[3]);
  137. haveGlobalColormap = BitSet(buf[4], LOCALCOLORMAP); /* Global Colormap */
  138. if (haveGlobalColormap) {
  139. if (ReadColorMap(fd, BitPixel, ColorMap)) {
  140. return 0;
  141. }
  142. }
  143. for (;;) {
  144. int top, left;
  145. int width, height;
  146. if (! ReadOK(fd,&c,1)) {
  147. return 0;
  148. }
  149. if (c == ';') { /* GIF terminator */
  150. goto terminated;
  151. }
  152. if (c == '!') { /* Extension */
  153. if (! ReadOK(fd,&c,1)) {
  154. return 0;
  155. }
  156. DoExtension(fd, c, &Transparent, &ZeroDataBlock);
  157. continue;
  158. }
  159. if (c != ',') { /* Not a valid start character */
  160. continue;
  161. }
  162. /*1.4//++imageCount; */
  163. if (! ReadOK(fd,buf,9)) {
  164. return 0;
  165. }
  166. useGlobalColormap = ! BitSet(buf[8], LOCALCOLORMAP);
  167. bitPixel = 1<<((buf[8]&0x07)+1);
  168. left = LM_to_uint(buf[0], buf[1]);
  169. top = LM_to_uint(buf[2], buf[3]);
  170. width = LM_to_uint(buf[4], buf[5]);
  171. height = LM_to_uint(buf[6], buf[7]);
  172. if (left + width > screen_width || top + height > screen_height) {
  173. if (VERBOSE) {
  174. printf("Frame is not confined to screen dimension.\n");
  175. }
  176. return 0;
  177. }
  178. if (!(im = gdImageCreate(width, height))) {
  179. return 0;
  180. }
  181. im->interlace = BitSet(buf[8], INTERLACE);
  182. if (!useGlobalColormap) {
  183. if (ReadColorMap(fd, bitPixel, localColorMap)) {
  184. gdImageDestroy(im);
  185. return 0;
  186. }
  187. ReadImage(im, fd, width, height, localColorMap,
  188. BitSet(buf[8], INTERLACE), &ZeroDataBlock);
  189. } else {
  190. if (!haveGlobalColormap) {
  191. gdImageDestroy(im);
  192. return 0;
  193. }
  194. ReadImage(im, fd, width, height,
  195. ColorMap,
  196. BitSet(buf[8], INTERLACE), &ZeroDataBlock);
  197. }
  198. if (Transparent != (-1)) {
  199. gdImageColorTransparent(im, Transparent);
  200. }
  201. goto terminated;
  202. }
  203. terminated:
  204. /* Terminator before any image was declared! */
  205. if (!im) {
  206. return 0;
  207. }
  208. if (!im->colorsTotal) {
  209. gdImageDestroy(im);
  210. return 0;
  211. }
  212. /* Check for open colors at the end, so
  213. we can reduce colorsTotal and ultimately
  214. BitsPerPixel */
  215. for (i=((im->colorsTotal-1)); (i>=0); i--) {
  216. if (im->open[i]) {
  217. im->colorsTotal--;
  218. } else {
  219. break;
  220. }
  221. }
  222. return im;
  223. }
  224. /* }}} */
  225. static int ReadColorMap(gdIOCtx *fd, int number, unsigned char (*buffer)[256]) /* {{{ */
  226. {
  227. int i;
  228. unsigned char rgb[3];
  229. for (i = 0; i < number; ++i) {
  230. if (! ReadOK(fd, rgb, sizeof(rgb))) {
  231. return TRUE;
  232. }
  233. buffer[CM_RED][i] = rgb[0] ;
  234. buffer[CM_GREEN][i] = rgb[1] ;
  235. buffer[CM_BLUE][i] = rgb[2] ;
  236. }
  237. return FALSE;
  238. }
  239. /* }}} */
  240. static int
  241. DoExtension(gdIOCtx *fd, int label, int *Transparent, int *ZeroDataBlockP)
  242. {
  243. unsigned char buf[256];
  244. switch (label) {
  245. case 0xf9: /* Graphic Control Extension */
  246. memset(buf, 0, 4); /* initialize a few bytes in the case the next function fails */
  247. (void) GetDataBlock(fd, (unsigned char*) buf, ZeroDataBlockP);
  248. #if 0
  249. Gif89.disposal = (buf[0] >> 2) & 0x7;
  250. Gif89.inputFlag = (buf[0] >> 1) & 0x1;
  251. Gif89.delayTime = LM_to_uint(buf[1],buf[2]);
  252. #endif
  253. if ((buf[0] & 0x1) != 0)
  254. *Transparent = buf[3];
  255. while (GetDataBlock(fd, (unsigned char*) buf, ZeroDataBlockP) > 0);
  256. return FALSE;
  257. default:
  258. break;
  259. }
  260. while (GetDataBlock(fd, (unsigned char*) buf, ZeroDataBlockP) > 0)
  261. ;
  262. return FALSE;
  263. }
  264. /* }}} */
  265. static int
  266. GetDataBlock_(gdIOCtx *fd, unsigned char *buf, int *ZeroDataBlockP)
  267. {
  268. unsigned char count;
  269. if (! ReadOK(fd,&count,1)) {
  270. return -1;
  271. }
  272. *ZeroDataBlockP = count == 0;
  273. if ((count != 0) && (! ReadOK(fd, buf, count))) {
  274. return -1;
  275. }
  276. return count;
  277. }
  278. /* }}} */
  279. static int
  280. GetDataBlock(gdIOCtx *fd, unsigned char *buf, int *ZeroDataBlockP)
  281. {
  282. int rv;
  283. int i;
  284. rv = GetDataBlock_(fd,buf, ZeroDataBlockP);
  285. if (VERBOSE) {
  286. char *tmp = NULL;
  287. if (rv > 0) {
  288. tmp = (char*) safe_emalloc(3 * rv, sizeof(char), 1);
  289. for (i=0;i<rv;i++) {
  290. sprintf(&tmp[3*sizeof(char)*i], " %02x", buf[i]);
  291. }
  292. } else {
  293. tmp = estrdup("");
  294. }
  295. php_gd_error_ex(E_NOTICE, "[GetDataBlock returning %d: %s]", rv, tmp);
  296. efree(tmp);
  297. }
  298. return(rv);
  299. }
  300. /* }}} */
  301. static int
  302. GetCode_(gdIOCtx *fd, CODE_STATIC_DATA *scd, int code_size, int flag, int *ZeroDataBlockP)
  303. {
  304. int i, j, ret;
  305. unsigned char count;
  306. if (flag) {
  307. scd->curbit = 0;
  308. scd->lastbit = 0;
  309. scd->last_byte = 0;
  310. scd->done = FALSE;
  311. return 0;
  312. }
  313. if ( (scd->curbit + code_size) >= scd->lastbit) {
  314. if (scd->done) {
  315. if (scd->curbit >= scd->lastbit) {
  316. /* Oh well */
  317. }
  318. return -1;
  319. }
  320. scd->buf[0] = scd->buf[scd->last_byte-2];
  321. scd->buf[1] = scd->buf[scd->last_byte-1];
  322. if ((count = GetDataBlock(fd, &scd->buf[2], ZeroDataBlockP)) <= 0)
  323. scd->done = TRUE;
  324. scd->last_byte = 2 + count;
  325. scd->curbit = (scd->curbit - scd->lastbit) + 16;
  326. scd->lastbit = (2+count)*8 ;
  327. }
  328. ret = 0;
  329. for (i = scd->curbit, j = 0; j < code_size; ++i, ++j)
  330. ret |= ((scd->buf[ i / 8 ] & (1 << (i % 8))) != 0) << j;
  331. scd->curbit += code_size;
  332. return ret;
  333. }
  334. static int
  335. GetCode(gdIOCtx *fd, CODE_STATIC_DATA *scd, int code_size, int flag, int *ZeroDataBlockP)
  336. {
  337. int rv;
  338. rv = GetCode_(fd, scd, code_size,flag, ZeroDataBlockP);
  339. if (VERBOSE) printf("[GetCode(,%d,%d) returning %d]\n",code_size,flag,rv);
  340. return(rv);
  341. }
  342. /* }}} */
  343. static int
  344. LWZReadByte_(gdIOCtx *fd, LZW_STATIC_DATA *sd, char flag, int input_code_size, int *ZeroDataBlockP)
  345. {
  346. int code, incode, i;
  347. if (flag) {
  348. sd->set_code_size = input_code_size;
  349. sd->code_size = sd->set_code_size+1;
  350. sd->clear_code = 1 << sd->set_code_size ;
  351. sd->end_code = sd->clear_code + 1;
  352. sd->max_code_size = 2*sd->clear_code;
  353. sd->max_code = sd->clear_code+2;
  354. GetCode(fd, &sd->scd, 0, TRUE, ZeroDataBlockP);
  355. sd->fresh = TRUE;
  356. for (i = 0; i < sd->clear_code; ++i) {
  357. sd->table[0][i] = 0;
  358. sd->table[1][i] = i;
  359. }
  360. for (; i < (1<<MAX_LWZ_BITS); ++i)
  361. sd->table[0][i] = sd->table[1][0] = 0;
  362. sd->sp = sd->stack;
  363. return 0;
  364. } else if (sd->fresh) {
  365. sd->fresh = FALSE;
  366. do {
  367. sd->firstcode = sd->oldcode =
  368. GetCode(fd, &sd->scd, sd->code_size, FALSE, ZeroDataBlockP);
  369. } while (sd->firstcode == sd->clear_code);
  370. return sd->firstcode;
  371. }
  372. if (sd->sp > sd->stack)
  373. return *--sd->sp;
  374. while ((code = GetCode(fd, &sd->scd, sd->code_size, FALSE, ZeroDataBlockP)) >= 0) {
  375. if (code == sd->clear_code) {
  376. for (i = 0; i < sd->clear_code; ++i) {
  377. sd->table[0][i] = 0;
  378. sd->table[1][i] = i;
  379. }
  380. for (; i < (1<<MAX_LWZ_BITS); ++i)
  381. sd->table[0][i] = sd->table[1][i] = 0;
  382. sd->code_size = sd->set_code_size+1;
  383. sd->max_code_size = 2*sd->clear_code;
  384. sd->max_code = sd->clear_code+2;
  385. sd->sp = sd->stack;
  386. sd->firstcode = sd->oldcode =
  387. GetCode(fd, &sd->scd, sd->code_size, FALSE, ZeroDataBlockP);
  388. return sd->firstcode;
  389. } else if (code == sd->end_code) {
  390. int count;
  391. unsigned char buf[260];
  392. if (*ZeroDataBlockP)
  393. return -2;
  394. while ((count = GetDataBlock(fd, buf, ZeroDataBlockP)) > 0)
  395. ;
  396. if (count != 0)
  397. return -2;
  398. }
  399. incode = code;
  400. if (sd->sp == (sd->stack + STACK_SIZE)) {
  401. /* Bad compressed data stream */
  402. return -1;
  403. }
  404. if (code >= sd->max_code) {
  405. *sd->sp++ = sd->firstcode;
  406. code = sd->oldcode;
  407. }
  408. while (code >= sd->clear_code) {
  409. if (sd->sp == (sd->stack + STACK_SIZE)) {
  410. /* Bad compressed data stream */
  411. return -1;
  412. }
  413. *sd->sp++ = sd->table[1][code];
  414. if (code == sd->table[0][code]) {
  415. /* Oh well */
  416. }
  417. code = sd->table[0][code];
  418. }
  419. *sd->sp++ = sd->firstcode = sd->table[1][code];
  420. if ((code = sd->max_code) <(1<<MAX_LWZ_BITS)) {
  421. sd->table[0][code] = sd->oldcode;
  422. sd->table[1][code] = sd->firstcode;
  423. ++sd->max_code;
  424. if ((sd->max_code >= sd->max_code_size) &&
  425. (sd->max_code_size < (1<<MAX_LWZ_BITS))) {
  426. sd->max_code_size *= 2;
  427. ++sd->code_size;
  428. }
  429. }
  430. sd->oldcode = incode;
  431. if (sd->sp > sd->stack)
  432. return *--sd->sp;
  433. }
  434. return code;
  435. }
  436. /* }}} */
  437. static int
  438. LWZReadByte(gdIOCtx *fd, LZW_STATIC_DATA *sd, char flag, int input_code_size, int *ZeroDataBlockP)
  439. {
  440. int rv;
  441. rv = LWZReadByte_(fd, sd, flag, input_code_size, ZeroDataBlockP);
  442. if (VERBOSE) printf("[LWZReadByte(,%d,%d) returning %d]\n",flag,input_code_size,rv);
  443. return(rv);
  444. }
  445. /* }}} */
  446. static void
  447. ReadImage(gdImagePtr im, gdIOCtx *fd, int len, int height, unsigned char (*cmap)[256], int interlace, int *ZeroDataBlockP) /*1.4//, int ignore) */
  448. {
  449. unsigned char c;
  450. int v;
  451. int xpos = 0, ypos = 0, pass = 0;
  452. int i;
  453. LZW_STATIC_DATA sd;
  454. /*
  455. ** Initialize the Compression routines
  456. */
  457. if (! ReadOK(fd,&c,1)) {
  458. return;
  459. }
  460. if (c > MAX_LWZ_BITS) {
  461. return;
  462. }
  463. /* Stash the color map into the image */
  464. for (i=0; (i<gdMaxColors); i++) {
  465. im->red[i] = cmap[CM_RED][i];
  466. im->green[i] = cmap[CM_GREEN][i];
  467. im->blue[i] = cmap[CM_BLUE][i];
  468. im->open[i] = 1;
  469. }
  470. /* Many (perhaps most) of these colors will remain marked open. */
  471. im->colorsTotal = gdMaxColors;
  472. if (LWZReadByte(fd, &sd, TRUE, c, ZeroDataBlockP) < 0) {
  473. return;
  474. }
  475. /*
  476. ** If this is an "uninteresting picture" ignore it.
  477. ** REMOVED For 1.4
  478. */
  479. /*if (ignore) { */
  480. /* while (LWZReadByte(fd, &sd, FALSE, c) >= 0) */
  481. /* ; */
  482. /* return; */
  483. /*} */
  484. while ((v = LWZReadByte(fd, &sd, FALSE, c, ZeroDataBlockP)) >= 0) {
  485. if (v >= gdMaxColors) {
  486. v = 0;
  487. }
  488. /* This how we recognize which colors are actually used. */
  489. if (im->open[v]) {
  490. im->open[v] = 0;
  491. }
  492. gdImageSetPixel(im, xpos, ypos, v);
  493. ++xpos;
  494. if (xpos == len) {
  495. xpos = 0;
  496. if (interlace) {
  497. switch (pass) {
  498. case 0:
  499. case 1:
  500. ypos += 8; break;
  501. case 2:
  502. ypos += 4; break;
  503. case 3:
  504. ypos += 2; break;
  505. }
  506. if (ypos >= height) {
  507. ++pass;
  508. switch (pass) {
  509. case 1:
  510. ypos = 4; break;
  511. case 2:
  512. ypos = 2; break;
  513. case 3:
  514. ypos = 1; break;
  515. default:
  516. goto fini;
  517. }
  518. }
  519. } else {
  520. ++ypos;
  521. }
  522. }
  523. if (ypos >= height)
  524. break;
  525. }
  526. fini:
  527. if (LWZReadByte(fd, &sd, FALSE, c, ZeroDataBlockP) >=0) {
  528. /* Ignore extra */
  529. }
  530. }
  531. /* }}} */