-
Notifications
You must be signed in to change notification settings - Fork 2
/
lgpng_chunks.c
811 lines (760 loc) · 20 KB
/
lgpng_chunks.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
/*
* Copyright (c) 2018,2022-2023 Tristan Le Guern <tleguern@bouledef.eu>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "config.h"
#include <ctype.h>
#include COMPAT_ENDIAN_H
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "lgpng.h"
uint8_t png_sig[8] = {137, 80, 78, 71, 13, 10, 26, 10};
const char *colourtypemap[COLOUR_TYPE__MAX] = {
"greyscale",
"error",
"truecolour",
"indexed",
"greyscale + alpha",
"error",
"truecolour + alpha",
};
const char *compressiontypemap[COMPRESSION_TYPE__MAX] = {
"deflate",
};
const char *filtermethodmap[FILTER_METHOD__MAX] = {
"adaptive",
};
const char *interlacemap[INTERLACE_METHOD__MAX] = {
"standard",
"adam7",
};
const char *rendering_intentmap[RENDERING_INTENT__MAX] = {
"perceptual",
"relative colorimetric",
"saturation",
"absolute colorimetric",
};
const char *unitspecifiermap[UNITSPECIFIER__MAX] = {
"unknown",
"metre",
};
const char *dispose_opmap[DISPOSE_OP__MAX] = {
"none",
"background",
"previous",
};
const char *blend_opmap[BLEND_OP__MAX] = {
"source",
"over",
};
const char *offsunitspecifiermap[OFFS_UNITSPECIFIER__MAX] = {
"pixel",
"micrometer",
};
const char *ster_mode_map[STER_MODE__MAX] = {
"cross-fuse layout",
"diverging-fuse layout",
};
const char *disposal_methodmap[DISPOSAL_METHOD__MAX] = {
"none",
"do not dispose",
"restore to background color",
"restore to previous",
"reserved",
"reserved",
"reserved",
"reserved",
};
const char *user_inputmap[USER_INPUT__MAX] = {
"user input is not expected",
"user input is expected",
};
bool
lgpng_validate_keyword(uint8_t *keyword, size_t keywordz)
{
if (' ' == keyword[0]) {
return(false);
}
if (' ' == keyword[keywordz - 1]) {
return(false);
}
if (NULL != memmem(keyword, keywordz," ", 2)) {
return(false);
}
for (size_t i = 0; i < keywordz; i++) {
if (keyword[i] < 32
|| (keyword[i] > 126 && keyword[i] < 161)) {
return(false);
}
}
return(true);
}
bool
lgpng_is_official_keyword(uint8_t *keyword, size_t keywordz)
{
/* From WD-png-3-20221025/ */
if (0 != memcmp(keyword, "Title", keywordz)
&& 0 != memcmp(keyword, "Author", keywordz)
&& 0 != memcmp(keyword, "Description", keywordz)
&& 0 != memcmp(keyword, "Copyright", keywordz)
&& 0 != memcmp(keyword, "Creation Time", keywordz)
&& 0 != memcmp(keyword, "Software", keywordz)
&& 0 != memcmp(keyword, "Disclaimer", keywordz)
&& 0 != memcmp(keyword, "Warning", keywordz)
&& 0 != memcmp(keyword, "Source", keywordz)
&& 0 != memcmp(keyword, "Comment", keywordz)
&& 0 != memcmp(keyword, "XML:com.adobe.xmp", keywordz)
/* From DNOTE-pngext-20221024 */
&& 0 != memcmp(keyword, "Collection", keywordz)) {
return(false);
}
return(true);
}
int
lgpng_create_IHDR_from_data(struct IHDR *ihdr, uint8_t *data, uint32_t length)
{
if (13 != length) {
return(-1);
}
ihdr->length = length;
(void)memcpy(&(ihdr->type), "IHDR", 4);
(void)memcpy(&(ihdr->data.width), data, 4);
(void)memcpy(&(ihdr->data.height), data + 4, 4);
ihdr->data.bitdepth = data[8];
ihdr->data.colourtype = data[9];
ihdr->data.compression = data[10];
ihdr->data.filter = data[11];
ihdr->data.interlace = data[12];
ihdr->data.width = be32toh(ihdr->data.width);
ihdr->data.height = be32toh(ihdr->data.height);
return(0);
}
int
lgpng_create_PLTE_from_data(struct PLTE *plte, uint8_t *data, uint32_t length)
{
size_t elemz;
plte->length = length;
elemz = length / 3;
if (0 != length % 3 || 256 < elemz) {
return(-1);
}
(void)memcpy(&(plte->type), "PLTE", 4);
plte->data.entries = elemz;
for (size_t i = 0, j = 0; i < elemz; i++, j += 3) {
plte->data.entry[i].red = data[j];
plte->data.entry[i].green = data[j + 1];
plte->data.entry[i].blue = data[j + 2];
}
return(0);
}
int
lgpng_create_IDAT_from_data(struct IDAT *idat, uint8_t *data, uint32_t length)
{
idat->length = length;
(void)memcpy(&(idat->type), "IDAT", 4);
idat->data.data = data;
return(0);
}
int
lgpng_create_tRNS_from_data(struct tRNS *trns, struct IHDR *ihdr, uint8_t *data, uint32_t length)
{
if (NULL == ihdr) {
return(-1);
}
trns->length = length;
(void)memcpy(&(trns->type), "tRNS", 4);
trns->data.gray = 0;
trns->data.red = 0;
trns->data.green = 0;
trns->data.blue = 0;
trns->data.entries = 0;
(void)memset(&(trns->data.palette), 0, sizeof(trns->data.palette));
switch (ihdr->data.colourtype) {
case COLOUR_TYPE_GREYSCALE:
if (2 != length) {
return(-1);
}
(void)memcpy(&(trns->data.gray), data, 2);
trns->data.gray = be16toh(trns->data.gray);
break;
case COLOUR_TYPE_TRUECOLOUR:
if (6 != length) {
return(-1);
}
(void)memcpy(&(trns->data.red), data, 2);
trns->data.red = be16toh(trns->data.red);
(void)memcpy(&(trns->data.green), data, 2);
trns->data.green = be16toh(trns->data.green);
(void)memcpy(&(trns->data.blue), data, 2);
trns->data.blue = be16toh(trns->data.blue);
break;
case COLOUR_TYPE_INDEXED:
if (length > 256) {
return(-1);
}
trns->data.entries = length;
(void)memcpy(&(trns->data.palette), data, length);
break;
default:
return(-1);
}
return(0);
}
int
lgpng_create_sBIT_from_data(struct sBIT *sbit, struct IHDR *ihdr, uint8_t *data, uint32_t length)
{
if (NULL == ihdr) {
return(-1);
}
sbit->length = length;
(void)memcpy(&(sbit->type), "sBIT", 4);
sbit->data.sgreyscale = 0;
sbit->data.sred = 0;
sbit->data.sgreen = 0;
sbit->data.sblue = 0;
sbit->data.salpha = 0;
/* Reject invalid data size */
switch(ihdr->data.colourtype) {
case COLOUR_TYPE_GREYSCALE:
if (1 != length) {
return(-1);
}
break;
case COLOUR_TYPE_TRUECOLOUR:
/* FALLTHROUGH */
case COLOUR_TYPE_INDEXED:
if (3 != length) {
return(-1);
}
break;
case COLOUR_TYPE_GREYSCALE_ALPHA:
if (2 != length) {
return(-1);
}
break;
case COLOUR_TYPE_TRUECOLOUR_ALPHA:
if (4 != length) {
return(-1);
}
break;
default:
break;
}
/* Unpack data according to IHDR */
if (COLOUR_TYPE_GREYSCALE == ihdr->data.colourtype
|| COLOUR_TYPE_GREYSCALE_ALPHA == ihdr->data.colourtype) {
sbit->data.sgreyscale = (uint8_t)data[0];
} else if (COLOUR_TYPE_TRUECOLOUR == ihdr->data.colourtype
|| COLOUR_TYPE_INDEXED == ihdr->data.colourtype
|| COLOUR_TYPE_TRUECOLOUR_ALPHA == ihdr->data.colourtype) {
sbit->data.sred = (uint8_t)data[0];
sbit->data.sgreen = (uint8_t)data[1];
sbit->data.sblue = (uint8_t)data[2];
}
if (COLOUR_TYPE_GREYSCALE_ALPHA == ihdr->data.colourtype) {
sbit->data.salpha = (uint8_t)data[1];
} else if (COLOUR_TYPE_TRUECOLOUR_ALPHA == ihdr->data.colourtype) {
sbit->data.salpha = (uint8_t)data[3];
}
/* Reject invalid values */
if (COLOUR_TYPE_GREYSCALE == ihdr->data.colourtype
|| COLOUR_TYPE_GREYSCALE_ALPHA == ihdr->data.colourtype) {
if (sbit->data.sgreyscale <= 0) {
return(-1);
}
if (sbit->data.sgreyscale > ihdr->data.bitdepth) {
return(-1);
}
}
if (COLOUR_TYPE_GREYSCALE_ALPHA == ihdr->data.colourtype
|| COLOUR_TYPE_TRUECOLOUR_ALPHA == ihdr->data.colourtype) {
if (sbit->data.salpha <= 0) {
return(-1);
}
if (sbit->data.salpha > ihdr->data.bitdepth) {
return(-1);
}
}
if (COLOUR_TYPE_TRUECOLOUR == ihdr->data.colourtype
|| COLOUR_TYPE_TRUECOLOUR_ALPHA == ihdr->data.colourtype) {
if (sbit->data.sred <= 0 || sbit->data.sgreen <= 0
|| sbit->data.sblue <= 0) {
return(-1);
}
if (sbit->data.sred > ihdr->data.bitdepth
|| sbit->data.sgreen > ihdr->data.bitdepth
|| sbit->data.sblue > ihdr->data.bitdepth) {
return(-1);
}
}
/* Palette based image are restricted to 8-bits */
if (COLOUR_TYPE_INDEXED == ihdr->data.colourtype) {
if (sbit->data.sred <= 0 || sbit->data.sgreen <= 0
|| sbit->data.sblue <= 0) {
return(-1);
}
if (sbit->data.sred > 8
|| sbit->data.sgreen > 8
|| sbit->data.sblue > 8) {
return(-1);
}
}
return(0);
}
int
lgpng_create_cHRM_from_data(struct cHRM *chrm, uint8_t *data, uint32_t length)
{
(void)memcpy(&(chrm->type), "cHRM", 4);
if (32 != length) {
return(-1);
}
(void)memcpy(&(chrm->data), data, length);
chrm->data.whitex = be32toh(chrm->data.whitex);
chrm->data.whitey = be32toh(chrm->data.whitey);
chrm->data.redx = be32toh(chrm->data.redx);
chrm->data.redy = be32toh(chrm->data.redy);
chrm->data.greenx = be32toh(chrm->data.greenx);
chrm->data.greeny = be32toh(chrm->data.greeny);
chrm->data.bluex = be32toh(chrm->data.bluex);
chrm->data.bluey = be32toh(chrm->data.bluey);
return(0);
}
int
lgpng_create_gAMA_from_data(struct gAMA *gama, uint8_t *data, uint32_t length)
{
if (4 != length) {
return(-1);
}
gama->length = 4;
(void)memcpy(&(gama->type), "gAMA", 4);
(void)memcpy(&(gama->data.gamma), data, 4);
gama->data.gamma = be32toh(gama->data.gamma);
return(0);
}
int
lgpng_create_iCCP_from_data(struct iCCP *iccp, uint8_t *data, uint32_t length)
{
size_t offset;
uint8_t *nul;
iccp->length = length;
(void)memcpy(&(iccp->type), "iCCP", 4);
if (NULL == (nul = memchr(data, '\0', 80))) {
return(-1);
}
offset = (size_t)(nul - data);
if (1 > offset || 79 < offset) {
return(-1);
}
(void)memset(iccp->data.name, 0, sizeof(iccp->data.name));
(void)memcpy(iccp->data.name, data, offset);
iccp->data.namez = offset;
if (!lgpng_validate_keyword(iccp->data.name, offset)) {
return(-1);
}
/*
* Only one compression type allowed here too but check anyway
*/
if (offset + 1 > length) {
return(-1);
}
iccp->data.compression = data[offset + 1];
if (COMPRESSION_TYPE_DEFLATE != iccp->data.compression) {
return(-1);
}
if (offset + 2 > length) {
return(-1);
}
iccp->data.profile = data + offset + 2;
iccp->data.profilez = length - offset - 2;
return(0);
}
int
lgpng_create_sRGB_from_data(struct sRGB *srgb, uint8_t *data, uint32_t length)
{
if (1 != length) {
return(-1);
}
srgb->length = length;
(void)memcpy(&(srgb->type), "sRGB", 4);
srgb->data.intent = data[0];
return(0);
}
int
lgpng_create_cICP_from_data(struct cICP *cicp, uint8_t *data, uint32_t length)
{
if (4 != length) {
return(-1);
}
cicp->length = length;
(void)memcpy(&(cicp->type), "cICP", 4);
cicp->data.colour_primaries = data[0];
cicp->data.transfer_function = data[1];
cicp->data.matrix_coefficients = data[2];
cicp->data.video_full_range = data[3];
if (0 != cicp->data.matrix_coefficients) {
return(-1);
}
if (0 != cicp->data.video_full_range && 1 != cicp->data.video_full_range) {
return(-1);
}
/*
* TODO: find the name of the transfer functions
* -> Tables 2, 3 & 4 of ITU-T H.273 ?
* */
return(0);
}
int
lgpng_create_tEXt_from_data(struct tEXt *text, uint8_t *data, uint32_t length)
{
size_t offset;
uint8_t *nul;
text->length = length;
(void)memcpy(&(text->type), "tEXt", 4);
if (NULL == (nul = memchr(data, '\0', 80))) {
return(-1);
}
offset = (size_t)(nul - data);
(void)memset(text->data.keyword, 0, sizeof(text->data.keyword));
(void)memcpy(text->data.keyword, data, offset);
if (1 > offset || 79 < offset) {
return(-1);
}
if (!lgpng_validate_keyword(text->data.keyword, offset)) {
return(-1);
}
text->data.text = data + offset + 1;
return(0);
}
int
lgpng_create_zTXt_from_data(struct zTXt *ztxt, uint8_t *data, uint32_t length)
{
size_t offset;
uint8_t *nul;
ztxt->length = length;
(void)memcpy(&(ztxt->type), "zTXt", 4);
if (NULL == (nul = memchr(data, '\0', 80))) {
return(-1);
}
offset = (size_t)(nul - data);
(void)memset(ztxt->data.keyword, 0, sizeof(ztxt->data.keyword));
(void)memcpy(ztxt->data.keyword, data, offset);
ztxt->data.keywordz = offset;
if (1 > offset || 79 < offset) {
return(-1);
}
if (!lgpng_validate_keyword(ztxt->data.keyword, offset)) {
return(-1);
}
/*
* Only one compression type allowed here but check anyway
*/
if (offset + 1 > length) {
return(-1);
}
ztxt->data.compression = data[offset + 1];
if (COMPRESSION_TYPE_DEFLATE != ztxt->data.compression) {
return(-1);
}
if (offset + 2 > length) {
return(-1);
}
ztxt->data.text = data + offset + 2;
ztxt->data.textz = length - offset - 2;
return(0);
}
int
lgpng_create_bKGD_from_data(struct bKGD *bkgd, struct IHDR *ihdr, struct PLTE *plte, uint8_t *data, uint32_t length)
{
struct rgb16 *rgb;
/* Detect uninitialized IHDR chunk */
if (NULL == ihdr) {
return(-1);
}
/* Same with PLTE */
if ((NULL == plte)
&& COLOUR_TYPE_INDEXED == ihdr->data.colourtype) {
return(-1);
}
bkgd->length = length;
(void)memcpy(&(bkgd->type), "bKGD", 4);
switch (ihdr->data.colourtype) {
case COLOUR_TYPE_GREYSCALE:
/* FALLTHROUGH */
case COLOUR_TYPE_GREYSCALE_ALPHA:
if (2 != length) {
return(-1);
}
bkgd->data.greyscale = *(uint16_t *)data;
bkgd->data.greyscale = be16toh(bkgd->data.greyscale);
break;
case COLOUR_TYPE_TRUECOLOUR:
/* FALLTHROUGH */
case COLOUR_TYPE_TRUECOLOUR_ALPHA:
if (6 != length) {
return(-1);
}
rgb = (struct rgb16 *)data;
bkgd->data.rgb.red = be16toh(rgb->red);
bkgd->data.rgb.green = be16toh(rgb->green);
bkgd->data.rgb.blue = be16toh(rgb->blue);
break;
case COLOUR_TYPE_INDEXED:
if (1 != length) {
return(-1);
}
/* Colour out of PLTE index */
if (data[0] >= plte->data.entries) {
return(-1);
}
bkgd->data.paletteindex = data[0];
break;
}
return(0);
}
int
lgpng_create_hIST_from_data(struct hIST *hist, struct PLTE *plte, uint8_t *data, uint32_t length)
{
size_t elemz;
uint16_t *frequency;
/* Detect uninitialized PLTE chunk */
if (0 == plte->data.entries) {
return(-1);
}
(void)memcpy(&(hist->type), "hIST", 4);
hist->length = length;
/* length is a series of 16-bit integers */
elemz = length / 2;
/* hIST mirrors the PLTE chunk so it inherits the same restrictions */
if (0 != length % 2 || 256 < elemz) {
return(-1);
}
/* hIST should have the exact same number of entries than PLTE */
if (elemz != plte->data.entries) {
return(-1);
}
frequency = (uint16_t *)data;
(void)memset(hist->data.frequency, 0, sizeof(hist->data.frequency));
for (size_t i = 0; i < elemz; i++) {
hist->data.frequency[i] = frequency[i];
}
return(0);
}
int
lgpng_create_pHYs_from_data(struct pHYs *phys, uint8_t *data, uint32_t length)
{
phys->length = length;
(void)memcpy(&(phys->type), "pHYs", 4);
(void)memcpy(&(phys->data.ppux), data, 4);
(void)memcpy(&(phys->data.ppuy), data + 4, 4);
phys->data.ppux = be32toh(phys->data.ppux);
phys->data.ppuy = be32toh(phys->data.ppuy);
phys->data.unitspecifier = data[9];
return(0);
}
int
lgpng_create_sPLT_from_data(struct sPLT *splt, uint8_t *data, uint32_t length)
{
size_t offset;
uint8_t *nul;
splt->length = length;
(void)memcpy(&(splt->type), "sPLT", 4);
if (NULL == (nul = memchr(data, '\0', 80))) {
return(-1);
}
offset = (size_t)(nul - data);
(void)memset(splt->data.palettename, 0, sizeof(splt->data.palettename));
(void)memcpy(splt->data.palettename, data, offset);
if (1 > offset || 79 < offset) {
return(-1);
}
if (!lgpng_validate_keyword((uint8_t *)splt->data.palettename, offset)) {
return(-1);
}
offset += 1;
splt->data.sampledepth = data[offset];
if (8 != splt->data.sampledepth && 16 != splt->data.sampledepth) {
return(-1);
}
offset += 1;
if (8 == splt->data.sampledepth) {
splt->data.entries = (length - offset) / 6;
if ((length - offset) % 6 != 0) {
return(-1);
}
}
if (16 == splt->data.sampledepth) {
splt->data.entries = (length - offset) / 10;
if ((length - offset) % 10 != 0) {
return(-1);
}
}
/* TODO: Can't do the rest without allocations */
return(0);
}
int
lgpng_create_eXIf_from_data(struct eXIf *exif, uint8_t *data, uint32_t length)
{
exif->length = length;
(void)memcpy(&(exif->type), "eXIf", 4);
exif->data.profile = data;
return(0);
}
int
lgpng_create_tIME_from_data(struct tIME *time, uint8_t *data, uint32_t length)
{
if (7 != length) {
return(-1);
}
time->length = length;
(void)memcpy(&(time->type), "tIME", 4);
(void)memcpy(&(time->data.year), data, 2);
time->data.year = be16toh(time->data.year);
time->data.month = data[2];
time->data.day = data[3];
time->data.hour = data[4];
time->data.minute = data[5];
time->data.second = data[6];
return(0);
}
int
lgpng_create_acTL_from_data(struct acTL *actl, uint8_t *data, uint32_t length)
{
if (8 != length) {
return(-1);
}
actl->length = length;
(void)memcpy(&(actl->type), "acTL", 4);
(void)memcpy(&(actl->data.num_frames), data, 4);
(void)memcpy(&(actl->data.num_plays), data + 4, 4);
actl->data.num_frames = be32toh(actl->data.num_frames);
actl->data.num_plays = be32toh(actl->data.num_plays);
if (0 == actl->data.num_frames) {
return(-1);
}
return(0);
}
int
lgpng_create_fcTL_from_data(struct fcTL *fctl, uint8_t *data, uint32_t length)
{
if (26 != length) {
return(-1);
}
fctl->length = length;
(void)memcpy(&(fctl->type), "fcTL", 4);
(void)memcpy(&(fctl->data.sequence_number), data, 4);
(void)memcpy(&(fctl->data.width), data + 4, 4);
(void)memcpy(&(fctl->data.height), data + 8, 4);
(void)memcpy(&(fctl->data.x_offset), data + 12, 4);
(void)memcpy(&(fctl->data.y_offset), data + 16, 4);
(void)memcpy(&(fctl->data.delay_num), data + 20, 2);
(void)memcpy(&(fctl->data.delay_den), data + 22, 2);
fctl->data.sequence_number = be32toh(fctl->data.sequence_number);
fctl->data.width = be32toh(fctl->data.width);
fctl->data.height = be32toh(fctl->data.height);
fctl->data.x_offset = be32toh(fctl->data.x_offset);
fctl->data.y_offset = be32toh(fctl->data.y_offset);
fctl->data.delay_num = be16toh(fctl->data.delay_num);
fctl->data.delay_den = be16toh(fctl->data.delay_den);
fctl->data.dispose_op = data[24];
fctl->data.blend_op = data[25];
if (fctl->data.width == 0 || fctl->data.height == 0) {
return(-1);
}
if (fctl->data.dispose_op >= DISPOSE_OP__MAX) {
return(-1);
}
if (fctl->data.blend_op >= BLEND_OP__MAX) {
return(-1);
}
return(0);
}
int
lgpng_create_fdAT_from_data(struct fdAT *fdat, uint8_t *data, uint32_t length)
{
if (5 > length) {
return(-1);
}
fdat->length = length;
(void)memcpy(&(fdat->type), "fdAT", 4);
(void)memcpy(&(fdat->data.sequence_number), data, 4);
fdat->data.sequence_number = be32toh(fdat->data.sequence_number);
fdat->data.frame_data = data + 4;
return(0);
}
int
lgpng_create_oFFs_from_data(struct oFFs *offs, uint8_t *data, uint32_t length)
{
if (5 > length) {
return(-1);
}
offs->length = length;
(void)memcpy(&(offs->type), "oFFs", 4);
(void)memcpy(&(offs->data.x_position), data, 4);
(void)memcpy(&(offs->data.y_position), data, 4);
offs->data.x_position = (int32_t)be32toh(offs->data.x_position);
offs->data.y_position = (int32_t)be32toh(offs->data.y_position);
offs->data.unitspecifier = data[8];
if (offs->data.unitspecifier >= OFFS_UNITSPECIFIER__MAX) {
return(-1);
}
return(0);
}
int
lgpng_create_gIFg_from_data(struct gIFg *gifg, uint8_t *data, uint32_t length)
{
if (4 > length) {
return(-1);
}
gifg->length = length;
(void)memcpy(&(gifg->type), "gIFg", 4);
gifg->data.disposal_method = data[0];
gifg->data.user_input = data[1];
(void)memcpy(&(gifg->data.delay_time), data, 2);
gifg->data.delay_time = be16toh(gifg->data.delay_time);
if (gifg->data.disposal_method >= 8) {
return(-1);
}
if (gifg->data.user_input >= 2) {
return(-1);
}
return(0);
}
int
lgpng_create_gIFx_from_data(struct gIFx *gifx, uint8_t *data, uint32_t length)
{
if (length < 11) {
return(-1);
}
gifx->length = length;
(void)memcpy(&(gifx->type), "gIFx", 4);
(void)memcpy(gifx->data.identifier, data, 8);
(void)memcpy(gifx->data.code, data + 8, 3);
gifx->data.data = data + 11;
return(0);
}
int
lgpng_create_sTER_from_data(struct sTER *ster, uint8_t *data, uint32_t length)
{
if (length != 1) {
return(-1);
}
ster->length = length;
(void)memcpy(&(ster->type), "sTER", 4);
if (data[0] >= STER_MODE__MAX) {
return(-1);
}
ster->data.mode = data[0];
return(0);
}