-
Notifications
You must be signed in to change notification settings - Fork 7.7k
/
image.c
1542 lines (1369 loc) · 42 KB
/
image.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
+----------------------------------------------------------------------+
| PHP Version 7 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2018 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Rasmus Lerdorf <rasmus@php.net> |
| Marcus Boerger <helly@php.net> |
+----------------------------------------------------------------------+
*/
#include "php.h"
#include <stdio.h>
#if HAVE_FCNTL_H
#include <fcntl.h>
#endif
#include "fopen_wrappers.h"
#include "ext/standard/fsock.h"
#if HAVE_UNISTD_H
#include <unistd.h>
#endif
#include "php_image.h"
#ifdef PHP_WIN32
#include "win32/php_stdint.h"
#endif
#if HAVE_ZLIB && !defined(COMPILE_DL_ZLIB)
#include "zlib.h"
#endif
/* file type markers */
PHPAPI const char php_sig_gif[3] = {'G', 'I', 'F'};
PHPAPI const char php_sig_psd[4] = {'8', 'B', 'P', 'S'};
PHPAPI const char php_sig_bmp[2] = {'B', 'M'};
PHPAPI const char php_sig_swf[3] = {'F', 'W', 'S'};
PHPAPI const char php_sig_swc[3] = {'C', 'W', 'S'};
PHPAPI const char php_sig_jpg[3] = {(char) 0xff, (char) 0xd8, (char) 0xff};
PHPAPI const char php_sig_png[8] = {(char) 0x89, (char) 0x50, (char) 0x4e, (char) 0x47,
(char) 0x0d, (char) 0x0a, (char) 0x1a, (char) 0x0a};
PHPAPI const char php_sig_tif_ii[4] = {'I','I', (char)0x2A, (char)0x00};
PHPAPI const char php_sig_tif_mm[4] = {'M','M', (char)0x00, (char)0x2A};
PHPAPI const char php_sig_jpc[3] = {(char)0xff, (char)0x4f, (char)0xff};
PHPAPI const char php_sig_jp2[12] = {(char)0x00, (char)0x00, (char)0x00, (char)0x0c,
(char)0x6a, (char)0x50, (char)0x20, (char)0x20,
(char)0x0d, (char)0x0a, (char)0x87, (char)0x0a};
PHPAPI const char php_sig_iff[4] = {'F','O','R','M'};
PHPAPI const char php_sig_ico[4] = {(char)0x00, (char)0x00, (char)0x01, (char)0x00};
PHPAPI const char php_sig_riff[4] = {'R', 'I', 'F', 'F'};
PHPAPI const char php_sig_webp[4] = {'W', 'E', 'B', 'P'};
/* REMEMBER TO ADD MIME-TYPE TO FUNCTION php_image_type_to_mime_type */
/* PCX must check first 64bytes and byte 0=0x0a and byte2 < 0x06 */
/* return info as a struct, to make expansion easier */
struct gfxinfo {
unsigned int width;
unsigned int height;
unsigned int bits;
unsigned int channels;
};
/* {{{ PHP_MINIT_FUNCTION(imagetypes)
* Register IMAGETYPE_<xxx> constants used by GetImageSize(), image_type_to_mime_type, ext/exif */
PHP_MINIT_FUNCTION(imagetypes)
{
REGISTER_LONG_CONSTANT("IMAGETYPE_GIF", IMAGE_FILETYPE_GIF, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("IMAGETYPE_JPEG", IMAGE_FILETYPE_JPEG, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("IMAGETYPE_PNG", IMAGE_FILETYPE_PNG, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("IMAGETYPE_SWF", IMAGE_FILETYPE_SWF, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("IMAGETYPE_PSD", IMAGE_FILETYPE_PSD, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("IMAGETYPE_BMP", IMAGE_FILETYPE_BMP, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("IMAGETYPE_TIFF_II", IMAGE_FILETYPE_TIFF_II, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("IMAGETYPE_TIFF_MM", IMAGE_FILETYPE_TIFF_MM, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("IMAGETYPE_JPC", IMAGE_FILETYPE_JPC, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("IMAGETYPE_JP2", IMAGE_FILETYPE_JP2, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("IMAGETYPE_JPX", IMAGE_FILETYPE_JPX, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("IMAGETYPE_JB2", IMAGE_FILETYPE_JB2, CONST_CS | CONST_PERSISTENT);
#if HAVE_ZLIB && !defined(COMPILE_DL_ZLIB)
REGISTER_LONG_CONSTANT("IMAGETYPE_SWC", IMAGE_FILETYPE_SWC, CONST_CS | CONST_PERSISTENT);
#endif
REGISTER_LONG_CONSTANT("IMAGETYPE_IFF", IMAGE_FILETYPE_IFF, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("IMAGETYPE_WBMP", IMAGE_FILETYPE_WBMP, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("IMAGETYPE_JPEG2000",IMAGE_FILETYPE_JPC, CONST_CS | CONST_PERSISTENT); /* keep alias */
REGISTER_LONG_CONSTANT("IMAGETYPE_XBM", IMAGE_FILETYPE_XBM, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("IMAGETYPE_ICO", IMAGE_FILETYPE_ICO, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("IMAGETYPE_WEBP", IMAGE_FILETYPE_WEBP, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("IMAGETYPE_UNKNOWN", IMAGE_FILETYPE_UNKNOWN, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("IMAGETYPE_COUNT", IMAGE_FILETYPE_COUNT, CONST_CS | CONST_PERSISTENT);
return SUCCESS;
}
/* }}} */
/* {{{ php_handle_gif
* routine to handle GIF files. If only everything were that easy... ;} */
static struct gfxinfo *php_handle_gif (php_stream * stream)
{
struct gfxinfo *result = NULL;
unsigned char dim[5];
if (php_stream_seek(stream, 3, SEEK_CUR))
return NULL;
if (php_stream_read(stream, (char*)dim, sizeof(dim)) != sizeof(dim))
return NULL;
result = (struct gfxinfo *) ecalloc(1, sizeof(struct gfxinfo));
result->width = (unsigned int)dim[0] | (((unsigned int)dim[1])<<8);
result->height = (unsigned int)dim[2] | (((unsigned int)dim[3])<<8);
result->bits = dim[4]&0x80 ? ((((unsigned int)dim[4])&0x07) + 1) : 0;
result->channels = 3; /* always */
return result;
}
/* }}} */
/* {{{ php_handle_psd
*/
static struct gfxinfo *php_handle_psd (php_stream * stream)
{
struct gfxinfo *result = NULL;
unsigned char dim[8];
if (php_stream_seek(stream, 11, SEEK_CUR))
return NULL;
if (php_stream_read(stream, (char*)dim, sizeof(dim)) != sizeof(dim))
return NULL;
result = (struct gfxinfo *) ecalloc(1, sizeof(struct gfxinfo));
result->height = (((unsigned int)dim[0]) << 24) + (((unsigned int)dim[1]) << 16) + (((unsigned int)dim[2]) << 8) + ((unsigned int)dim[3]);
result->width = (((unsigned int)dim[4]) << 24) + (((unsigned int)dim[5]) << 16) + (((unsigned int)dim[6]) << 8) + ((unsigned int)dim[7]);
return result;
}
/* }}} */
/* {{{ php_handle_bmp
*/
static struct gfxinfo *php_handle_bmp (php_stream * stream)
{
struct gfxinfo *result = NULL;
unsigned char dim[16];
int size;
if (php_stream_seek(stream, 11, SEEK_CUR))
return NULL;
if (php_stream_read(stream, (char*)dim, sizeof(dim)) != sizeof(dim))
return NULL;
size = (((unsigned int)dim[ 3]) << 24) + (((unsigned int)dim[ 2]) << 16) + (((unsigned int)dim[ 1]) << 8) + ((unsigned int) dim[ 0]);
if (size == 12) {
result = (struct gfxinfo *) ecalloc (1, sizeof(struct gfxinfo));
result->width = (((unsigned int)dim[ 5]) << 8) + ((unsigned int) dim[ 4]);
result->height = (((unsigned int)dim[ 7]) << 8) + ((unsigned int) dim[ 6]);
result->bits = ((unsigned int)dim[11]);
} else if (size > 12 && (size <= 64 || size == 108 || size == 124)) {
result = (struct gfxinfo *) ecalloc (1, sizeof(struct gfxinfo));
result->width = (((unsigned int)dim[ 7]) << 24) + (((unsigned int)dim[ 6]) << 16) + (((unsigned int)dim[ 5]) << 8) + ((unsigned int) dim[ 4]);
result->height = (((unsigned int)dim[11]) << 24) + (((unsigned int)dim[10]) << 16) + (((unsigned int)dim[ 9]) << 8) + ((unsigned int) dim[ 8]);
result->height = abs((int32_t)result->height);
result->bits = (((unsigned int)dim[15]) << 8) + ((unsigned int)dim[14]);
} else {
return NULL;
}
return result;
}
/* }}} */
/* {{{ php_swf_get_bits
* routines to handle SWF files. */
static unsigned long int php_swf_get_bits (unsigned char* buffer, unsigned int pos, unsigned int count)
{
unsigned int loop;
unsigned long int result = 0;
for (loop = pos; loop < pos + count; loop++)
{
result = result +
((((buffer[loop / 8]) >> (7 - (loop % 8))) & 0x01) << (count - (loop - pos) - 1));
}
return result;
}
/* }}} */
#if HAVE_ZLIB && !defined(COMPILE_DL_ZLIB)
/* {{{ php_handle_swc
*/
static struct gfxinfo *php_handle_swc(php_stream * stream)
{
struct gfxinfo *result = NULL;
long bits;
unsigned char a[64];
unsigned long len=64, szlength;
int factor = 1,maxfactor = 16;
int status = 0;
unsigned char *b, *buf = NULL;
zend_string *bufz;
if (php_stream_seek(stream, 5, SEEK_CUR)) {
return NULL;
}
if (php_stream_read(stream, (char *) a, sizeof(a)) != sizeof(a)) {
return NULL;
}
b = ecalloc(1, len + 1);
if (uncompress(b, &len, a, sizeof(a)) != Z_OK) {
/* failed to decompress the file, will try reading the rest of the file */
if (php_stream_seek(stream, 8, SEEK_SET)) {
efree(b);
return NULL;
}
bufz = php_stream_copy_to_mem(stream, PHP_STREAM_COPY_ALL, 0);
if (!bufz) {
efree(b);
return NULL;
}
/*
* zlib::uncompress() wants to know the output data length
* if none was given as a parameter
* we try from input length * 2 up to input length * 2^8
* doubling it whenever it wasn't big enough
* that should be eneugh for all real life cases
*/
do {
szlength = ZSTR_LEN(bufz) * (1<<factor++);
buf = erealloc(buf, szlength);
status = uncompress(buf, &szlength, (unsigned char *) ZSTR_VAL(bufz), ZSTR_LEN(bufz));
} while ((status==Z_BUF_ERROR)&&(factor<maxfactor));
if (bufz) {
zend_string_release_ex(bufz, 0);
}
if (status == Z_OK) {
memcpy(b, buf, len);
}
if (buf) {
efree(buf);
}
}
if (!status) {
result = (struct gfxinfo *) ecalloc (1, sizeof (struct gfxinfo));
bits = php_swf_get_bits (b, 0, 5);
result->width = (php_swf_get_bits (b, 5 + bits, bits) -
php_swf_get_bits (b, 5, bits)) / 20;
result->height = (php_swf_get_bits (b, 5 + (3 * bits), bits) -
php_swf_get_bits (b, 5 + (2 * bits), bits)) / 20;
} else {
result = NULL;
}
efree (b);
return result;
}
/* }}} */
#endif
/* {{{ php_handle_swf
*/
static struct gfxinfo *php_handle_swf (php_stream * stream)
{
struct gfxinfo *result = NULL;
long bits;
unsigned char a[32];
if (php_stream_seek(stream, 5, SEEK_CUR))
return NULL;
if (php_stream_read(stream, (char*)a, sizeof(a)) != sizeof(a))
return NULL;
result = (struct gfxinfo *) ecalloc (1, sizeof (struct gfxinfo));
bits = php_swf_get_bits (a, 0, 5);
result->width = (php_swf_get_bits (a, 5 + bits, bits) -
php_swf_get_bits (a, 5, bits)) / 20;
result->height = (php_swf_get_bits (a, 5 + (3 * bits), bits) -
php_swf_get_bits (a, 5 + (2 * bits), bits)) / 20;
result->bits = 0;
result->channels = 0;
return result;
}
/* }}} */
/* {{{ php_handle_png
* routine to handle PNG files */
static struct gfxinfo *php_handle_png (php_stream * stream)
{
struct gfxinfo *result = NULL;
unsigned char dim[9];
/* Width: 4 bytes
* Height: 4 bytes
* Bit depth: 1 byte
* Color type: 1 byte
* Compression method: 1 byte
* Filter method: 1 byte
* Interlace method: 1 byte
*/
if (php_stream_seek(stream, 8, SEEK_CUR))
return NULL;
if((php_stream_read(stream, (char*)dim, sizeof(dim))) < sizeof(dim))
return NULL;
result = (struct gfxinfo *) ecalloc(1, sizeof(struct gfxinfo));
result->width = (((unsigned int)dim[0]) << 24) + (((unsigned int)dim[1]) << 16) + (((unsigned int)dim[2]) << 8) + ((unsigned int)dim[3]);
result->height = (((unsigned int)dim[4]) << 24) + (((unsigned int)dim[5]) << 16) + (((unsigned int)dim[6]) << 8) + ((unsigned int)dim[7]);
result->bits = (unsigned int)dim[8];
return result;
}
/* }}} */
/* routines to handle JPEG data */
/* some defines for the different JPEG block types */
#define M_SOF0 0xC0 /* Start Of Frame N */
#define M_SOF1 0xC1 /* N indicates which compression process */
#define M_SOF2 0xC2 /* Only SOF0-SOF2 are now in common use */
#define M_SOF3 0xC3
#define M_SOF5 0xC5 /* NB: codes C4 and CC are NOT SOF markers */
#define M_SOF6 0xC6
#define M_SOF7 0xC7
#define M_SOF9 0xC9
#define M_SOF10 0xCA
#define M_SOF11 0xCB
#define M_SOF13 0xCD
#define M_SOF14 0xCE
#define M_SOF15 0xCF
#define M_SOI 0xD8
#define M_EOI 0xD9 /* End Of Image (end of datastream) */
#define M_SOS 0xDA /* Start Of Scan (begins compressed data) */
#define M_APP0 0xe0
#define M_APP1 0xe1
#define M_APP2 0xe2
#define M_APP3 0xe3
#define M_APP4 0xe4
#define M_APP5 0xe5
#define M_APP6 0xe6
#define M_APP7 0xe7
#define M_APP8 0xe8
#define M_APP9 0xe9
#define M_APP10 0xea
#define M_APP11 0xeb
#define M_APP12 0xec
#define M_APP13 0xed
#define M_APP14 0xee
#define M_APP15 0xef
#define M_COM 0xFE /* COMment */
#define M_PSEUDO 0xFFD8 /* pseudo marker for start of image(byte 0) */
/* {{{ php_read2
*/
static unsigned short php_read2(php_stream * stream)
{
unsigned char a[2];
/* return 0 if we couldn't read enough data */
if((php_stream_read(stream, (char *) a, sizeof(a))) < sizeof(a)) return 0;
return (((unsigned short)a[0]) << 8) + ((unsigned short)a[1]);
}
/* }}} */
/* {{{ php_next_marker
* get next marker byte from file */
static unsigned int php_next_marker(php_stream * stream, int last_marker, int ff_read)
{
int a=0, marker;
/* get marker byte, swallowing possible padding */
if (!ff_read) {
size_t extraneous = 0;
while ((marker = php_stream_getc(stream)) != 0xff) {
if (marker == EOF) {
return M_EOI;/* we hit EOF */
}
extraneous++;
}
if (extraneous) {
php_error_docref(NULL, E_WARNING, "corrupt JPEG data: %zu extraneous bytes before marker", extraneous);
}
}
a = 1;
do {
if ((marker = php_stream_getc(stream)) == EOF)
{
return M_EOI;/* we hit EOF */
}
a++;
} while (marker == 0xff);
if (a < 2)
{
return M_EOI; /* at least one 0xff is needed before marker code */
}
return (unsigned int)marker;
}
/* }}} */
/* {{{ php_skip_variable
* skip over a variable-length block; assumes proper length marker */
static int php_skip_variable(php_stream * stream)
{
zend_off_t length = ((unsigned int)php_read2(stream));
if (length < 2) {
return 0;
}
length = length - 2;
php_stream_seek(stream, (zend_long)length, SEEK_CUR);
return 1;
}
/* }}} */
/* {{{ php_read_APP
*/
static int php_read_APP(php_stream * stream, unsigned int marker, zval *info)
{
unsigned short length;
char *buffer;
char markername[16];
zval *tmp;
length = php_read2(stream);
if (length < 2) {
return 0;
}
length -= 2; /* length includes itself */
buffer = emalloc(length);
if (php_stream_read(stream, buffer, (zend_long) length) != length) {
efree(buffer);
return 0;
}
snprintf(markername, sizeof(markername), "APP%d", marker - M_APP0);
if ((tmp = zend_hash_str_find(Z_ARRVAL_P(info), markername, strlen(markername))) == NULL) {
/* XXX we only catch the 1st tag of it's kind! */
add_assoc_stringl(info, markername, buffer, length);
}
efree(buffer);
return 1;
}
/* }}} */
/* {{{ php_handle_jpeg
main loop to parse JPEG structure */
static struct gfxinfo *php_handle_jpeg (php_stream * stream, zval *info)
{
struct gfxinfo *result = NULL;
unsigned int marker = M_PSEUDO;
unsigned short length, ff_read=1;
for (;;) {
marker = php_next_marker(stream, marker, ff_read);
ff_read = 0;
switch (marker) {
case M_SOF0:
case M_SOF1:
case M_SOF2:
case M_SOF3:
case M_SOF5:
case M_SOF6:
case M_SOF7:
case M_SOF9:
case M_SOF10:
case M_SOF11:
case M_SOF13:
case M_SOF14:
case M_SOF15:
if (result == NULL) {
/* handle SOFn block */
result = (struct gfxinfo *) ecalloc(1, sizeof(struct gfxinfo));
length = php_read2(stream);
result->bits = php_stream_getc(stream);
result->height = php_read2(stream);
result->width = php_read2(stream);
result->channels = php_stream_getc(stream);
if (!info || length < 8) { /* if we don't want an extanded info -> return */
return result;
}
if (php_stream_seek(stream, length - 8, SEEK_CUR)) { /* file error after info */
return result;
}
} else {
if (!php_skip_variable(stream)) {
return result;
}
}
break;
case M_APP0:
case M_APP1:
case M_APP2:
case M_APP3:
case M_APP4:
case M_APP5:
case M_APP6:
case M_APP7:
case M_APP8:
case M_APP9:
case M_APP10:
case M_APP11:
case M_APP12:
case M_APP13:
case M_APP14:
case M_APP15:
if (info) {
if (!php_read_APP(stream, marker, info)) { /* read all the app marks... */
return result;
}
} else {
if (!php_skip_variable(stream)) {
return result;
}
}
break;
case M_SOS:
case M_EOI:
return result; /* we're about to hit image data, or are at EOF. stop processing. */
default:
if (!php_skip_variable(stream)) { /* anything else isn't interesting */
return result;
}
break;
}
}
return result; /* perhaps image broken -> no info but size */
}
/* }}} */
/* {{{ php_read4
*/
static unsigned int php_read4(php_stream * stream)
{
unsigned char a[4];
/* just return 0 if we hit the end-of-file */
if ((php_stream_read(stream, (char*)a, sizeof(a))) != sizeof(a)) return 0;
return (((unsigned int)a[0]) << 24)
+ (((unsigned int)a[1]) << 16)
+ (((unsigned int)a[2]) << 8)
+ (((unsigned int)a[3]));
}
/* }}} */
/* {{{ JPEG 2000 Marker Codes */
#define JPEG2000_MARKER_PREFIX 0xFF /* All marker codes start with this */
#define JPEG2000_MARKER_SOC 0x4F /* Start of Codestream */
#define JPEG2000_MARKER_SOT 0x90 /* Start of Tile part */
#define JPEG2000_MARKER_SOD 0x93 /* Start of Data */
#define JPEG2000_MARKER_EOC 0xD9 /* End of Codestream */
#define JPEG2000_MARKER_SIZ 0x51 /* Image and tile size */
#define JPEG2000_MARKER_COD 0x52 /* Coding style default */
#define JPEG2000_MARKER_COC 0x53 /* Coding style component */
#define JPEG2000_MARKER_RGN 0x5E /* Region of interest */
#define JPEG2000_MARKER_QCD 0x5C /* Quantization default */
#define JPEG2000_MARKER_QCC 0x5D /* Quantization component */
#define JPEG2000_MARKER_POC 0x5F /* Progression order change */
#define JPEG2000_MARKER_TLM 0x55 /* Tile-part lengths */
#define JPEG2000_MARKER_PLM 0x57 /* Packet length, main header */
#define JPEG2000_MARKER_PLT 0x58 /* Packet length, tile-part header */
#define JPEG2000_MARKER_PPM 0x60 /* Packed packet headers, main header */
#define JPEG2000_MARKER_PPT 0x61 /* Packed packet headers, tile part header */
#define JPEG2000_MARKER_SOP 0x91 /* Start of packet */
#define JPEG2000_MARKER_EPH 0x92 /* End of packet header */
#define JPEG2000_MARKER_CRG 0x63 /* Component registration */
#define JPEG2000_MARKER_COM 0x64 /* Comment */
/* }}} */
/* {{{ php_handle_jpc
Main loop to parse JPEG2000 raw codestream structure */
static struct gfxinfo *php_handle_jpc(php_stream * stream)
{
struct gfxinfo *result = NULL;
int highest_bit_depth, bit_depth;
unsigned char first_marker_id;
unsigned int i;
/* JPEG 2000 components can be vastly different from one another.
Each component can be sampled at a different resolution, use
a different colour space, have a separate colour depth, and
be compressed totally differently! This makes giving a single
"bit depth" answer somewhat problematic. For this implementation
we'll use the highest depth encountered. */
/* Get the single byte that remains after the file type indentification */
first_marker_id = php_stream_getc(stream);
/* Ensure that this marker is SIZ (as is mandated by the standard) */
if (first_marker_id != JPEG2000_MARKER_SIZ) {
php_error_docref(NULL, E_WARNING, "JPEG2000 codestream corrupt(Expected SIZ marker not found after SOC)");
return NULL;
}
result = (struct gfxinfo *)ecalloc(1, sizeof(struct gfxinfo));
php_read2(stream); /* Lsiz */
php_read2(stream); /* Rsiz */
result->width = php_read4(stream); /* Xsiz */
result->height = php_read4(stream); /* Ysiz */
#if MBO_0
php_read4(stream); /* XOsiz */
php_read4(stream); /* YOsiz */
php_read4(stream); /* XTsiz */
php_read4(stream); /* YTsiz */
php_read4(stream); /* XTOsiz */
php_read4(stream); /* YTOsiz */
#else
if (php_stream_seek(stream, 24, SEEK_CUR)) {
efree(result);
return NULL;
}
#endif
result->channels = php_read2(stream); /* Csiz */
if ((result->channels == 0 && php_stream_eof(stream)) || result->channels > 256) {
efree(result);
return NULL;
}
/* Collect bit depth info */
highest_bit_depth = 0;
for (i = 0; i < result->channels; i++) {
bit_depth = php_stream_getc(stream); /* Ssiz[i] */
bit_depth++;
if (bit_depth > highest_bit_depth) {
highest_bit_depth = bit_depth;
}
php_stream_getc(stream); /* XRsiz[i] */
php_stream_getc(stream); /* YRsiz[i] */
}
result->bits = highest_bit_depth;
return result;
}
/* }}} */
/* {{{ php_handle_jp2
main loop to parse JPEG 2000 JP2 wrapper format structure */
static struct gfxinfo *php_handle_jp2(php_stream *stream)
{
struct gfxinfo *result = NULL;
unsigned int box_length;
unsigned int box_type;
char jp2c_box_id[] = {(char)0x6a, (char)0x70, (char)0x32, (char)0x63};
/* JP2 is a wrapper format for JPEG 2000. Data is contained within "boxes".
Boxes themselves can be contained within "super-boxes". Super-Boxes can
contain super-boxes which provides us with a hierarchical storage system.
It is valid for a JP2 file to contain multiple individual codestreams.
We'll just look for the first codestream at the root of the box structure
and handle that.
*/
for (;;)
{
box_length = php_read4(stream); /* LBox */
/* TBox */
if (php_stream_read(stream, (void *)&box_type, sizeof(box_type)) != sizeof(box_type)) {
/* Use this as a general "out of stream" error */
break;
}
if (box_length == 1) {
/* We won't handle XLBoxes */
return NULL;
}
if (!memcmp(&box_type, jp2c_box_id, 4))
{
/* Skip the first 3 bytes to emulate the file type examination */
php_stream_seek(stream, 3, SEEK_CUR);
result = php_handle_jpc(stream);
break;
}
/* Stop if this was the last box */
if ((int)box_length <= 0) {
break;
}
/* Skip over LBox (Which includes both TBox and LBox itself */
if (php_stream_seek(stream, box_length - 8, SEEK_CUR)) {
break;
}
}
if (result == NULL) {
php_error_docref(NULL, E_WARNING, "JP2 file has no codestreams at root level");
}
return result;
}
/* }}} */
/* {{{ tiff constants
*/
PHPAPI const int php_tiff_bytes_per_format[] = {0, 1, 1, 2, 4, 8, 1, 1, 2, 4, 8, 4, 8};
/* uncompressed only */
#define TAG_IMAGEWIDTH 0x0100
#define TAG_IMAGEHEIGHT 0x0101
/* compressed images only */
#define TAG_COMP_IMAGEWIDTH 0xA002
#define TAG_COMP_IMAGEHEIGHT 0xA003
#define TAG_FMT_BYTE 1
#define TAG_FMT_STRING 2
#define TAG_FMT_USHORT 3
#define TAG_FMT_ULONG 4
#define TAG_FMT_URATIONAL 5
#define TAG_FMT_SBYTE 6
#define TAG_FMT_UNDEFINED 7
#define TAG_FMT_SSHORT 8
#define TAG_FMT_SLONG 9
#define TAG_FMT_SRATIONAL 10
#define TAG_FMT_SINGLE 11
#define TAG_FMT_DOUBLE 12
/* }}} */
/* {{{ php_ifd_get16u
* Convert a 16 bit unsigned value from file's native byte order */
static int php_ifd_get16u(void *Short, int motorola_intel)
{
if (motorola_intel) {
return (((unsigned char *)Short)[0] << 8) | ((unsigned char *)Short)[1];
} else {
return (((unsigned char *)Short)[1] << 8) | ((unsigned char *)Short)[0];
}
}
/* }}} */
/* {{{ php_ifd_get16s
* Convert a 16 bit signed value from file's native byte order */
static signed short php_ifd_get16s(void *Short, int motorola_intel)
{
return (signed short)php_ifd_get16u(Short, motorola_intel);
}
/* }}} */
/* {{{ php_ifd_get32s
* Convert a 32 bit signed value from file's native byte order */
static int php_ifd_get32s(void *Long, int motorola_intel)
{
if (motorola_intel) {
return ((( char *)Long)[0] << 24) | (((unsigned char *)Long)[1] << 16)
| (((unsigned char *)Long)[2] << 8 ) | (((unsigned char *)Long)[3] << 0 );
} else {
return ((( char *)Long)[3] << 24) | (((unsigned char *)Long)[2] << 16)
| (((unsigned char *)Long)[1] << 8 ) | (((unsigned char *)Long)[0] << 0 );
}
}
/* }}} */
/* {{{ php_ifd_get32u
* Convert a 32 bit unsigned value from file's native byte order */
static unsigned php_ifd_get32u(void *Long, int motorola_intel)
{
return (unsigned)php_ifd_get32s(Long, motorola_intel) & 0xffffffff;
}
/* }}} */
/* {{{ php_handle_tiff
main loop to parse TIFF structure */
static struct gfxinfo *php_handle_tiff (php_stream * stream, zval *info, int motorola_intel)
{
struct gfxinfo *result = NULL;
int i, num_entries;
unsigned char *dir_entry;
size_t ifd_size, dir_size, entry_value, width=0, height=0, ifd_addr;
int entry_tag , entry_type;
char *ifd_data, ifd_ptr[4];
if (php_stream_read(stream, ifd_ptr, 4) != 4)
return NULL;
ifd_addr = php_ifd_get32u(ifd_ptr, motorola_intel);
if (php_stream_seek(stream, ifd_addr-8, SEEK_CUR))
return NULL;
ifd_size = 2;
ifd_data = emalloc(ifd_size);
if (php_stream_read(stream, ifd_data, 2) != 2) {
efree(ifd_data);
return NULL;
}
num_entries = php_ifd_get16u(ifd_data, motorola_intel);
dir_size = 2/*num dir entries*/ +12/*length of entry*/*num_entries +4/* offset to next ifd (points to thumbnail or NULL)*/;
ifd_size = dir_size;
ifd_data = erealloc(ifd_data,ifd_size);
if (php_stream_read(stream, ifd_data+2, dir_size-2) != dir_size-2) {
efree(ifd_data);
return NULL;
}
/* now we have the directory we can look how long it should be */
ifd_size = dir_size;
for(i=0;i<num_entries;i++) {
dir_entry = (unsigned char *) ifd_data+2+i*12;
entry_tag = php_ifd_get16u(dir_entry+0, motorola_intel);
entry_type = php_ifd_get16u(dir_entry+2, motorola_intel);
switch(entry_type) {
case TAG_FMT_BYTE:
case TAG_FMT_SBYTE:
entry_value = (size_t)(dir_entry[8]);
break;
case TAG_FMT_USHORT:
entry_value = php_ifd_get16u(dir_entry+8, motorola_intel);
break;
case TAG_FMT_SSHORT:
entry_value = php_ifd_get16s(dir_entry+8, motorola_intel);
break;
case TAG_FMT_ULONG:
entry_value = php_ifd_get32u(dir_entry+8, motorola_intel);
break;
case TAG_FMT_SLONG:
entry_value = php_ifd_get32s(dir_entry+8, motorola_intel);
break;
default:
continue;
}
switch(entry_tag) {
case TAG_IMAGEWIDTH:
case TAG_COMP_IMAGEWIDTH:
width = entry_value;
break;
case TAG_IMAGEHEIGHT:
case TAG_COMP_IMAGEHEIGHT:
height = entry_value;
break;
}
}
efree(ifd_data);
if ( width && height) {
/* not the same when in for-loop */
result = (struct gfxinfo *) ecalloc(1, sizeof(struct gfxinfo));
result->height = height;
result->width = width;
result->bits = 0;
result->channels = 0;
return result;
}
return NULL;
}
/* }}} */
/* {{{ php_handle_psd
*/
static struct gfxinfo *php_handle_iff(php_stream * stream)
{
struct gfxinfo * result;
unsigned char a[10];
int chunkId;
int size;
short width, height, bits;
if (php_stream_read(stream, (char *) a, 8) != 8) {
return NULL;
}
if (strncmp((char *) a+4, "ILBM", 4) && strncmp((char *) a+4, "PBM ", 4)) {
return NULL;
}
/* loop chunks to find BMHD chunk */
do {
if (php_stream_read(stream, (char*)a, 8) != 8) {
return NULL;
}
chunkId = php_ifd_get32s(a+0, 1);
size = php_ifd_get32s(a+4, 1);
if (size < 0) {
return NULL;
}
if ((size & 1) == 1) {
size++;
}
if (chunkId == 0x424d4844) { /* BMHD chunk */
if (size < 9 || php_stream_read(stream, (char*)a, 9) != 9) {
return NULL;
}
width = php_ifd_get16s(a+0, 1);
height = php_ifd_get16s(a+2, 1);
bits = a[8] & 0xff;
if (width > 0 && height > 0 && bits > 0 && bits < 33) {
result = (struct gfxinfo *) ecalloc(1, sizeof(struct gfxinfo));
result->width = width;
result->height = height;
result->bits = bits;
result->channels = 0;
return result;
}
} else {
if (php_stream_seek(stream, size, SEEK_CUR)) {
return NULL;
}
}
} while (1);
}
/* }}} */
/* {{{ php_get_wbmp
* int WBMP file format type
* byte Header Type
* byte Extended Header
* byte Header Data (type 00 = multibyte)
* byte Header Data (type 11 = name/pairs)
* int Number of columns
* int Number of rows
*/
static int php_get_wbmp(php_stream *stream, struct gfxinfo **result, int check)
{
int i, width = 0, height = 0;
if (php_stream_rewind(stream)) {
return 0;
}
/* get type */
if (php_stream_getc(stream) != 0) {
return 0;
}
/* skip header */
do {
i = php_stream_getc(stream);
if (i < 0) {
return 0;
}
} while (i & 0x80);
/* get width */
do {
i = php_stream_getc(stream);
if (i < 0) {
return 0;
}
width = (width << 7) | (i & 0x7f);
/* maximum valid width for wbmp (although 127 may be a more accurate one) */
if (width > 2048) {
return 0;
}
} while (i & 0x80);
/* get height */
do {
i = php_stream_getc(stream);
if (i < 0) {
return 0;
}
height = (height << 7) | (i & 0x7f);
/* maximum valid height for wbmp (although 127 may be a more accurate one) */
if (height > 2048) {
return 0;
}
} while (i & 0x80);
if (!height || !width) {
return 0;
}
if (!check) {
(*result)->width = width;
(*result)->height = height;
}
return IMAGE_FILETYPE_WBMP;
}
/* }}} */