Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/thirdparty/libpng/pngset.c
9833 views
1
/* pngset.c - storage of image information into info struct
2
*
3
* Copyright (c) 2018-2025 Cosmin Truta
4
* Copyright (c) 1998-2018 Glenn Randers-Pehrson
5
* Copyright (c) 1996-1997 Andreas Dilger
6
* Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc.
7
*
8
* This code is released under the libpng license.
9
* For conditions of distribution and use, see the disclaimer
10
* and license in png.h
11
*
12
* The functions here are used during reads to store data from the file
13
* into the info struct, and during writes to store application data
14
* into the info struct for writing into the file. This abstracts the
15
* info struct and allows us to change the structure in the future.
16
*/
17
18
#include "pngpriv.h"
19
20
#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
21
22
#ifdef PNG_bKGD_SUPPORTED
23
void PNGAPI
24
png_set_bKGD(png_const_structrp png_ptr, png_inforp info_ptr,
25
png_const_color_16p background)
26
{
27
png_debug1(1, "in %s storage function", "bKGD");
28
29
if (png_ptr == NULL || info_ptr == NULL || background == NULL)
30
return;
31
32
info_ptr->background = *background;
33
info_ptr->valid |= PNG_INFO_bKGD;
34
}
35
#endif
36
37
#ifdef PNG_cHRM_SUPPORTED
38
void PNGFAPI
39
png_set_cHRM_fixed(png_const_structrp png_ptr, png_inforp info_ptr,
40
png_fixed_point white_x, png_fixed_point white_y, png_fixed_point red_x,
41
png_fixed_point red_y, png_fixed_point green_x, png_fixed_point green_y,
42
png_fixed_point blue_x, png_fixed_point blue_y)
43
{
44
png_debug1(1, "in %s storage function", "cHRM fixed");
45
46
if (png_ptr == NULL || info_ptr == NULL)
47
return;
48
49
info_ptr->cHRM.redx = red_x;
50
info_ptr->cHRM.redy = red_y;
51
info_ptr->cHRM.greenx = green_x;
52
info_ptr->cHRM.greeny = green_y;
53
info_ptr->cHRM.bluex = blue_x;
54
info_ptr->cHRM.bluey = blue_y;
55
info_ptr->cHRM.whitex = white_x;
56
info_ptr->cHRM.whitey = white_y;
57
58
info_ptr->valid |= PNG_INFO_cHRM;
59
}
60
61
void PNGFAPI
62
png_set_cHRM_XYZ_fixed(png_const_structrp png_ptr, png_inforp info_ptr,
63
png_fixed_point int_red_X, png_fixed_point int_red_Y,
64
png_fixed_point int_red_Z, png_fixed_point int_green_X,
65
png_fixed_point int_green_Y, png_fixed_point int_green_Z,
66
png_fixed_point int_blue_X, png_fixed_point int_blue_Y,
67
png_fixed_point int_blue_Z)
68
{
69
png_XYZ XYZ;
70
png_xy xy;
71
72
png_debug1(1, "in %s storage function", "cHRM XYZ fixed");
73
74
if (png_ptr == NULL || info_ptr == NULL)
75
return;
76
77
XYZ.red_X = int_red_X;
78
XYZ.red_Y = int_red_Y;
79
XYZ.red_Z = int_red_Z;
80
XYZ.green_X = int_green_X;
81
XYZ.green_Y = int_green_Y;
82
XYZ.green_Z = int_green_Z;
83
XYZ.blue_X = int_blue_X;
84
XYZ.blue_Y = int_blue_Y;
85
XYZ.blue_Z = int_blue_Z;
86
87
if (png_xy_from_XYZ(&xy, &XYZ) == 0)
88
{
89
info_ptr->cHRM = xy;
90
info_ptr->valid |= PNG_INFO_cHRM;
91
}
92
93
else
94
png_app_error(png_ptr, "invalid cHRM XYZ");
95
}
96
97
# ifdef PNG_FLOATING_POINT_SUPPORTED
98
void PNGAPI
99
png_set_cHRM(png_const_structrp png_ptr, png_inforp info_ptr,
100
double white_x, double white_y, double red_x, double red_y,
101
double green_x, double green_y, double blue_x, double blue_y)
102
{
103
png_set_cHRM_fixed(png_ptr, info_ptr,
104
png_fixed(png_ptr, white_x, "cHRM White X"),
105
png_fixed(png_ptr, white_y, "cHRM White Y"),
106
png_fixed(png_ptr, red_x, "cHRM Red X"),
107
png_fixed(png_ptr, red_y, "cHRM Red Y"),
108
png_fixed(png_ptr, green_x, "cHRM Green X"),
109
png_fixed(png_ptr, green_y, "cHRM Green Y"),
110
png_fixed(png_ptr, blue_x, "cHRM Blue X"),
111
png_fixed(png_ptr, blue_y, "cHRM Blue Y"));
112
}
113
114
void PNGAPI
115
png_set_cHRM_XYZ(png_const_structrp png_ptr, png_inforp info_ptr, double red_X,
116
double red_Y, double red_Z, double green_X, double green_Y, double green_Z,
117
double blue_X, double blue_Y, double blue_Z)
118
{
119
png_set_cHRM_XYZ_fixed(png_ptr, info_ptr,
120
png_fixed(png_ptr, red_X, "cHRM Red X"),
121
png_fixed(png_ptr, red_Y, "cHRM Red Y"),
122
png_fixed(png_ptr, red_Z, "cHRM Red Z"),
123
png_fixed(png_ptr, green_X, "cHRM Green X"),
124
png_fixed(png_ptr, green_Y, "cHRM Green Y"),
125
png_fixed(png_ptr, green_Z, "cHRM Green Z"),
126
png_fixed(png_ptr, blue_X, "cHRM Blue X"),
127
png_fixed(png_ptr, blue_Y, "cHRM Blue Y"),
128
png_fixed(png_ptr, blue_Z, "cHRM Blue Z"));
129
}
130
# endif /* FLOATING_POINT */
131
132
#endif /* cHRM */
133
134
#ifdef PNG_cICP_SUPPORTED
135
void PNGAPI
136
png_set_cICP(png_const_structrp png_ptr, png_inforp info_ptr,
137
png_byte colour_primaries, png_byte transfer_function,
138
png_byte matrix_coefficients, png_byte video_full_range_flag)
139
{
140
png_debug1(1, "in %s storage function", "cICP");
141
142
if (png_ptr == NULL || info_ptr == NULL)
143
return;
144
145
info_ptr->cicp_colour_primaries = colour_primaries;
146
info_ptr->cicp_transfer_function = transfer_function;
147
info_ptr->cicp_matrix_coefficients = matrix_coefficients;
148
info_ptr->cicp_video_full_range_flag = video_full_range_flag;
149
150
if (info_ptr->cicp_matrix_coefficients != 0)
151
{
152
png_warning(png_ptr, "Invalid cICP matrix coefficients");
153
return;
154
}
155
156
info_ptr->valid |= PNG_INFO_cICP;
157
}
158
#endif /* cICP */
159
160
#ifdef PNG_cLLI_SUPPORTED
161
void PNGFAPI
162
png_set_cLLI_fixed(png_const_structrp png_ptr, png_inforp info_ptr,
163
/* The values below are in cd/m2 (nits) and are scaled by 10,000; not
164
* 100,000 as in the case of png_fixed_point.
165
*/
166
png_uint_32 maxCLL, png_uint_32 maxFALL)
167
{
168
png_debug1(1, "in %s storage function", "cLLI");
169
170
if (png_ptr == NULL || info_ptr == NULL)
171
return;
172
173
/* Check the light level range: */
174
if (maxCLL > 0x7FFFFFFFU || maxFALL > 0x7FFFFFFFU)
175
{
176
/* The limit is 200kcd/m2; somewhat bright but not inconceivable because
177
* human vision is said to run up to 100Mcd/m2. The sun is about 2Gcd/m2.
178
*
179
* The reference sRGB monitor is 80cd/m2 and the limit of PQ encoding is
180
* 2kcd/m2.
181
*/
182
png_chunk_report(png_ptr, "cLLI light level exceeds PNG limit",
183
PNG_CHUNK_WRITE_ERROR);
184
return;
185
}
186
187
info_ptr->maxCLL = maxCLL;
188
info_ptr->maxFALL = maxFALL;
189
info_ptr->valid |= PNG_INFO_cLLI;
190
}
191
192
# ifdef PNG_FLOATING_POINT_SUPPORTED
193
void PNGAPI
194
png_set_cLLI(png_const_structrp png_ptr, png_inforp info_ptr,
195
double maxCLL, double maxFALL)
196
{
197
png_set_cLLI_fixed(png_ptr, info_ptr,
198
png_fixed_ITU(png_ptr, maxCLL, "png_set_cLLI(maxCLL)"),
199
png_fixed_ITU(png_ptr, maxFALL, "png_set_cLLI(maxFALL)"));
200
}
201
# endif /* FLOATING_POINT */
202
#endif /* cLLI */
203
204
#ifdef PNG_mDCV_SUPPORTED
205
static png_uint_16
206
png_ITU_fixed_16(int *error, png_fixed_point v)
207
{
208
/* Return a safe uint16_t value scaled according to the ITU H273 rules for
209
* 16-bit display chromaticities. Functions like the corresponding
210
* png_fixed() internal function with regard to errors: it's an error on
211
* write, a chunk_benign_error on read: See the definition of
212
* png_chunk_report in pngpriv.h.
213
*/
214
v /= 2; /* rounds to 0 in C: avoids insignificant arithmetic errors */
215
if (v > 65535 || v < 0)
216
{
217
*error = 1;
218
return 0;
219
}
220
221
return (png_uint_16)/*SAFE*/v;
222
}
223
224
void PNGAPI
225
png_set_mDCV_fixed(png_const_structrp png_ptr, png_inforp info_ptr,
226
png_fixed_point white_x, png_fixed_point white_y,
227
png_fixed_point red_x, png_fixed_point red_y,
228
png_fixed_point green_x, png_fixed_point green_y,
229
png_fixed_point blue_x, png_fixed_point blue_y,
230
png_uint_32 maxDL,
231
png_uint_32 minDL)
232
{
233
png_uint_16 rx, ry, gx, gy, bx, by, wx, wy;
234
int error;
235
236
png_debug1(1, "in %s storage function", "mDCV");
237
238
if (png_ptr == NULL || info_ptr == NULL)
239
return;
240
241
/* Check the input values to ensure they are in the expected range: */
242
error = 0;
243
rx = png_ITU_fixed_16(&error, red_x);
244
ry = png_ITU_fixed_16(&error, red_y);
245
gx = png_ITU_fixed_16(&error, green_x);
246
gy = png_ITU_fixed_16(&error, green_y);
247
bx = png_ITU_fixed_16(&error, blue_x);
248
by = png_ITU_fixed_16(&error, blue_y);
249
wx = png_ITU_fixed_16(&error, white_x);
250
wy = png_ITU_fixed_16(&error, white_y);
251
252
if (error)
253
{
254
png_chunk_report(png_ptr,
255
"mDCV chromaticities outside representable range",
256
PNG_CHUNK_WRITE_ERROR);
257
return;
258
}
259
260
/* Check the light level range: */
261
if (maxDL > 0x7FFFFFFFU || minDL > 0x7FFFFFFFU)
262
{
263
/* The limit is 200kcd/m2; somewhat bright but not inconceivable because
264
* human vision is said to run up to 100Mcd/m2. The sun is about 2Gcd/m2.
265
*
266
* The reference sRGB monitor is 80cd/m2 and the limit of PQ encoding is
267
* 2kcd/m2.
268
*/
269
png_chunk_report(png_ptr, "mDCV display light level exceeds PNG limit",
270
PNG_CHUNK_WRITE_ERROR);
271
return;
272
}
273
274
/* All values are safe, the settings are accepted.
275
*
276
* IMPLEMENTATION NOTE: in practice the values can be checked and assigned
277
* but the result is confusing if a writing app calls png_set_mDCV more than
278
* once, the second time with an invalid value. This approach is more
279
* obviously correct at the cost of typing and a very slight machine
280
* overhead.
281
*/
282
info_ptr->mastering_red_x = rx;
283
info_ptr->mastering_red_y = ry;
284
info_ptr->mastering_green_x = gx;
285
info_ptr->mastering_green_y = gy;
286
info_ptr->mastering_blue_x = bx;
287
info_ptr->mastering_blue_y = by;
288
info_ptr->mastering_white_x = wx;
289
info_ptr->mastering_white_y = wy;
290
info_ptr->mastering_maxDL = maxDL;
291
info_ptr->mastering_minDL = minDL;
292
info_ptr->valid |= PNG_INFO_mDCV;
293
}
294
295
# ifdef PNG_FLOATING_POINT_SUPPORTED
296
void PNGAPI
297
png_set_mDCV(png_const_structrp png_ptr, png_inforp info_ptr,
298
double white_x, double white_y, double red_x, double red_y, double green_x,
299
double green_y, double blue_x, double blue_y,
300
double maxDL, double minDL)
301
{
302
png_set_mDCV_fixed(png_ptr, info_ptr,
303
png_fixed(png_ptr, white_x, "png_set_mDCV(white(x))"),
304
png_fixed(png_ptr, white_y, "png_set_mDCV(white(y))"),
305
png_fixed(png_ptr, red_x, "png_set_mDCV(red(x))"),
306
png_fixed(png_ptr, red_y, "png_set_mDCV(red(y))"),
307
png_fixed(png_ptr, green_x, "png_set_mDCV(green(x))"),
308
png_fixed(png_ptr, green_y, "png_set_mDCV(green(y))"),
309
png_fixed(png_ptr, blue_x, "png_set_mDCV(blue(x))"),
310
png_fixed(png_ptr, blue_y, "png_set_mDCV(blue(y))"),
311
png_fixed_ITU(png_ptr, maxDL, "png_set_mDCV(maxDL)"),
312
png_fixed_ITU(png_ptr, minDL, "png_set_mDCV(minDL)"));
313
}
314
# endif /* FLOATING_POINT */
315
#endif /* mDCV */
316
317
#ifdef PNG_eXIf_SUPPORTED
318
void PNGAPI
319
png_set_eXIf(png_const_structrp png_ptr, png_inforp info_ptr,
320
png_bytep exif)
321
{
322
png_warning(png_ptr, "png_set_eXIf does not work; use png_set_eXIf_1");
323
PNG_UNUSED(info_ptr)
324
PNG_UNUSED(exif)
325
}
326
327
void PNGAPI
328
png_set_eXIf_1(png_const_structrp png_ptr, png_inforp info_ptr,
329
png_uint_32 num_exif, png_bytep exif)
330
{
331
png_bytep new_exif;
332
333
png_debug1(1, "in %s storage function", "eXIf");
334
335
if (png_ptr == NULL || info_ptr == NULL ||
336
(png_ptr->mode & PNG_WROTE_eXIf) != 0)
337
return;
338
339
new_exif = png_voidcast(png_bytep, png_malloc_warn(png_ptr, num_exif));
340
341
if (new_exif == NULL)
342
{
343
png_warning(png_ptr, "Insufficient memory for eXIf chunk data");
344
return;
345
}
346
347
memcpy(new_exif, exif, (size_t)num_exif);
348
349
png_free_data(png_ptr, info_ptr, PNG_FREE_EXIF, 0);
350
351
info_ptr->num_exif = num_exif;
352
info_ptr->exif = new_exif;
353
info_ptr->free_me |= PNG_FREE_EXIF;
354
info_ptr->valid |= PNG_INFO_eXIf;
355
}
356
#endif /* eXIf */
357
358
#ifdef PNG_gAMA_SUPPORTED
359
void PNGFAPI
360
png_set_gAMA_fixed(png_const_structrp png_ptr, png_inforp info_ptr,
361
png_fixed_point file_gamma)
362
{
363
png_debug1(1, "in %s storage function", "gAMA");
364
365
if (png_ptr == NULL || info_ptr == NULL)
366
return;
367
368
info_ptr->gamma = file_gamma;
369
info_ptr->valid |= PNG_INFO_gAMA;
370
}
371
372
# ifdef PNG_FLOATING_POINT_SUPPORTED
373
void PNGAPI
374
png_set_gAMA(png_const_structrp png_ptr, png_inforp info_ptr, double file_gamma)
375
{
376
png_set_gAMA_fixed(png_ptr, info_ptr, png_fixed(png_ptr, file_gamma,
377
"png_set_gAMA"));
378
}
379
# endif
380
#endif
381
382
#ifdef PNG_hIST_SUPPORTED
383
void PNGAPI
384
png_set_hIST(png_const_structrp png_ptr, png_inforp info_ptr,
385
png_const_uint_16p hist)
386
{
387
int i;
388
389
png_debug1(1, "in %s storage function", "hIST");
390
391
if (png_ptr == NULL || info_ptr == NULL)
392
return;
393
394
if (info_ptr->num_palette == 0 || info_ptr->num_palette
395
> PNG_MAX_PALETTE_LENGTH)
396
{
397
png_warning(png_ptr,
398
"Invalid palette size, hIST allocation skipped");
399
400
return;
401
}
402
403
png_free_data(png_ptr, info_ptr, PNG_FREE_HIST, 0);
404
405
/* Changed from info->num_palette to PNG_MAX_PALETTE_LENGTH in
406
* version 1.2.1
407
*/
408
info_ptr->hist = png_voidcast(png_uint_16p, png_malloc_warn(png_ptr,
409
PNG_MAX_PALETTE_LENGTH * (sizeof (png_uint_16))));
410
411
if (info_ptr->hist == NULL)
412
{
413
png_warning(png_ptr, "Insufficient memory for hIST chunk data");
414
return;
415
}
416
417
for (i = 0; i < info_ptr->num_palette; i++)
418
info_ptr->hist[i] = hist[i];
419
420
info_ptr->free_me |= PNG_FREE_HIST;
421
info_ptr->valid |= PNG_INFO_hIST;
422
}
423
#endif
424
425
void PNGAPI
426
png_set_IHDR(png_const_structrp png_ptr, png_inforp info_ptr,
427
png_uint_32 width, png_uint_32 height, int bit_depth,
428
int color_type, int interlace_type, int compression_type,
429
int filter_type)
430
{
431
png_debug1(1, "in %s storage function", "IHDR");
432
433
if (png_ptr == NULL || info_ptr == NULL)
434
return;
435
436
info_ptr->width = width;
437
info_ptr->height = height;
438
info_ptr->bit_depth = (png_byte)bit_depth;
439
info_ptr->color_type = (png_byte)color_type;
440
info_ptr->compression_type = (png_byte)compression_type;
441
info_ptr->filter_type = (png_byte)filter_type;
442
info_ptr->interlace_type = (png_byte)interlace_type;
443
444
png_check_IHDR (png_ptr, info_ptr->width, info_ptr->height,
445
info_ptr->bit_depth, info_ptr->color_type, info_ptr->interlace_type,
446
info_ptr->compression_type, info_ptr->filter_type);
447
448
if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
449
info_ptr->channels = 1;
450
451
else if ((info_ptr->color_type & PNG_COLOR_MASK_COLOR) != 0)
452
info_ptr->channels = 3;
453
454
else
455
info_ptr->channels = 1;
456
457
if ((info_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0)
458
info_ptr->channels++;
459
460
info_ptr->pixel_depth = (png_byte)(info_ptr->channels * info_ptr->bit_depth);
461
462
info_ptr->rowbytes = PNG_ROWBYTES(info_ptr->pixel_depth, width);
463
}
464
465
#ifdef PNG_oFFs_SUPPORTED
466
void PNGAPI
467
png_set_oFFs(png_const_structrp png_ptr, png_inforp info_ptr,
468
png_int_32 offset_x, png_int_32 offset_y, int unit_type)
469
{
470
png_debug1(1, "in %s storage function", "oFFs");
471
472
if (png_ptr == NULL || info_ptr == NULL)
473
return;
474
475
info_ptr->x_offset = offset_x;
476
info_ptr->y_offset = offset_y;
477
info_ptr->offset_unit_type = (png_byte)unit_type;
478
info_ptr->valid |= PNG_INFO_oFFs;
479
}
480
#endif
481
482
#ifdef PNG_pCAL_SUPPORTED
483
void PNGAPI
484
png_set_pCAL(png_const_structrp png_ptr, png_inforp info_ptr,
485
png_const_charp purpose, png_int_32 X0, png_int_32 X1, int type,
486
int nparams, png_const_charp units, png_charpp params)
487
{
488
size_t length;
489
int i;
490
491
png_debug1(1, "in %s storage function", "pCAL");
492
493
if (png_ptr == NULL || info_ptr == NULL || purpose == NULL || units == NULL
494
|| (nparams > 0 && params == NULL))
495
return;
496
497
length = strlen(purpose) + 1;
498
png_debug1(3, "allocating purpose for info (%lu bytes)",
499
(unsigned long)length);
500
501
/* TODO: validate format of calibration name and unit name */
502
503
/* Check that the type matches the specification. */
504
if (type < 0 || type > 3)
505
{
506
png_chunk_report(png_ptr, "Invalid pCAL equation type",
507
PNG_CHUNK_WRITE_ERROR);
508
return;
509
}
510
511
if (nparams < 0 || nparams > 255)
512
{
513
png_chunk_report(png_ptr, "Invalid pCAL parameter count",
514
PNG_CHUNK_WRITE_ERROR);
515
return;
516
}
517
518
/* Validate params[nparams] */
519
for (i=0; i<nparams; ++i)
520
{
521
if (params[i] == NULL ||
522
!png_check_fp_string(params[i], strlen(params[i])))
523
{
524
png_chunk_report(png_ptr, "Invalid format for pCAL parameter",
525
PNG_CHUNK_WRITE_ERROR);
526
return;
527
}
528
}
529
530
info_ptr->pcal_purpose = png_voidcast(png_charp,
531
png_malloc_warn(png_ptr, length));
532
533
if (info_ptr->pcal_purpose == NULL)
534
{
535
png_chunk_report(png_ptr, "Insufficient memory for pCAL purpose",
536
PNG_CHUNK_WRITE_ERROR);
537
return;
538
}
539
540
memcpy(info_ptr->pcal_purpose, purpose, length);
541
542
info_ptr->free_me |= PNG_FREE_PCAL;
543
544
png_debug(3, "storing X0, X1, type, and nparams in info");
545
info_ptr->pcal_X0 = X0;
546
info_ptr->pcal_X1 = X1;
547
info_ptr->pcal_type = (png_byte)type;
548
info_ptr->pcal_nparams = (png_byte)nparams;
549
550
length = strlen(units) + 1;
551
png_debug1(3, "allocating units for info (%lu bytes)",
552
(unsigned long)length);
553
554
info_ptr->pcal_units = png_voidcast(png_charp,
555
png_malloc_warn(png_ptr, length));
556
557
if (info_ptr->pcal_units == NULL)
558
{
559
png_warning(png_ptr, "Insufficient memory for pCAL units");
560
return;
561
}
562
563
memcpy(info_ptr->pcal_units, units, length);
564
565
info_ptr->pcal_params = png_voidcast(png_charpp, png_malloc_warn(png_ptr,
566
(size_t)(((unsigned int)nparams + 1) * (sizeof (png_charp)))));
567
568
if (info_ptr->pcal_params == NULL)
569
{
570
png_warning(png_ptr, "Insufficient memory for pCAL params");
571
return;
572
}
573
574
memset(info_ptr->pcal_params, 0, ((unsigned int)nparams + 1) *
575
(sizeof (png_charp)));
576
577
for (i = 0; i < nparams; i++)
578
{
579
length = strlen(params[i]) + 1;
580
png_debug2(3, "allocating parameter %d for info (%lu bytes)", i,
581
(unsigned long)length);
582
583
info_ptr->pcal_params[i] = (png_charp)png_malloc_warn(png_ptr, length);
584
585
if (info_ptr->pcal_params[i] == NULL)
586
{
587
png_warning(png_ptr, "Insufficient memory for pCAL parameter");
588
return;
589
}
590
591
memcpy(info_ptr->pcal_params[i], params[i], length);
592
}
593
594
info_ptr->valid |= PNG_INFO_pCAL;
595
}
596
#endif
597
598
#ifdef PNG_sCAL_SUPPORTED
599
void PNGAPI
600
png_set_sCAL_s(png_const_structrp png_ptr, png_inforp info_ptr,
601
int unit, png_const_charp swidth, png_const_charp sheight)
602
{
603
size_t lengthw = 0, lengthh = 0;
604
605
png_debug1(1, "in %s storage function", "sCAL");
606
607
if (png_ptr == NULL || info_ptr == NULL)
608
return;
609
610
/* Double check the unit (should never get here with an invalid
611
* unit unless this is an API call.)
612
*/
613
if (unit != 1 && unit != 2)
614
png_error(png_ptr, "Invalid sCAL unit");
615
616
if (swidth == NULL || (lengthw = strlen(swidth)) == 0 ||
617
swidth[0] == 45 /* '-' */ || !png_check_fp_string(swidth, lengthw))
618
png_error(png_ptr, "Invalid sCAL width");
619
620
if (sheight == NULL || (lengthh = strlen(sheight)) == 0 ||
621
sheight[0] == 45 /* '-' */ || !png_check_fp_string(sheight, lengthh))
622
png_error(png_ptr, "Invalid sCAL height");
623
624
info_ptr->scal_unit = (png_byte)unit;
625
626
++lengthw;
627
628
png_debug1(3, "allocating unit for info (%u bytes)", (unsigned int)lengthw);
629
630
info_ptr->scal_s_width = png_voidcast(png_charp,
631
png_malloc_warn(png_ptr, lengthw));
632
633
if (info_ptr->scal_s_width == NULL)
634
{
635
png_warning(png_ptr, "Memory allocation failed while processing sCAL");
636
637
return;
638
}
639
640
memcpy(info_ptr->scal_s_width, swidth, lengthw);
641
642
++lengthh;
643
644
png_debug1(3, "allocating unit for info (%u bytes)", (unsigned int)lengthh);
645
646
info_ptr->scal_s_height = png_voidcast(png_charp,
647
png_malloc_warn(png_ptr, lengthh));
648
649
if (info_ptr->scal_s_height == NULL)
650
{
651
png_free(png_ptr, info_ptr->scal_s_width);
652
info_ptr->scal_s_width = NULL;
653
654
png_warning(png_ptr, "Memory allocation failed while processing sCAL");
655
return;
656
}
657
658
memcpy(info_ptr->scal_s_height, sheight, lengthh);
659
660
info_ptr->free_me |= PNG_FREE_SCAL;
661
info_ptr->valid |= PNG_INFO_sCAL;
662
}
663
664
# ifdef PNG_FLOATING_POINT_SUPPORTED
665
void PNGAPI
666
png_set_sCAL(png_const_structrp png_ptr, png_inforp info_ptr, int unit,
667
double width, double height)
668
{
669
png_debug1(1, "in %s storage function", "sCAL");
670
671
/* Check the arguments. */
672
if (width <= 0)
673
png_warning(png_ptr, "Invalid sCAL width ignored");
674
675
else if (height <= 0)
676
png_warning(png_ptr, "Invalid sCAL height ignored");
677
678
else
679
{
680
/* Convert 'width' and 'height' to ASCII. */
681
char swidth[PNG_sCAL_MAX_DIGITS+1];
682
char sheight[PNG_sCAL_MAX_DIGITS+1];
683
684
png_ascii_from_fp(png_ptr, swidth, (sizeof swidth), width,
685
PNG_sCAL_PRECISION);
686
png_ascii_from_fp(png_ptr, sheight, (sizeof sheight), height,
687
PNG_sCAL_PRECISION);
688
689
png_set_sCAL_s(png_ptr, info_ptr, unit, swidth, sheight);
690
}
691
}
692
# endif
693
694
# ifdef PNG_FIXED_POINT_SUPPORTED
695
void PNGAPI
696
png_set_sCAL_fixed(png_const_structrp png_ptr, png_inforp info_ptr, int unit,
697
png_fixed_point width, png_fixed_point height)
698
{
699
png_debug1(1, "in %s storage function", "sCAL");
700
701
/* Check the arguments. */
702
if (width <= 0)
703
png_warning(png_ptr, "Invalid sCAL width ignored");
704
705
else if (height <= 0)
706
png_warning(png_ptr, "Invalid sCAL height ignored");
707
708
else
709
{
710
/* Convert 'width' and 'height' to ASCII. */
711
char swidth[PNG_sCAL_MAX_DIGITS+1];
712
char sheight[PNG_sCAL_MAX_DIGITS+1];
713
714
png_ascii_from_fixed(png_ptr, swidth, (sizeof swidth), width);
715
png_ascii_from_fixed(png_ptr, sheight, (sizeof sheight), height);
716
717
png_set_sCAL_s(png_ptr, info_ptr, unit, swidth, sheight);
718
}
719
}
720
# endif
721
#endif
722
723
#ifdef PNG_pHYs_SUPPORTED
724
void PNGAPI
725
png_set_pHYs(png_const_structrp png_ptr, png_inforp info_ptr,
726
png_uint_32 res_x, png_uint_32 res_y, int unit_type)
727
{
728
png_debug1(1, "in %s storage function", "pHYs");
729
730
if (png_ptr == NULL || info_ptr == NULL)
731
return;
732
733
info_ptr->x_pixels_per_unit = res_x;
734
info_ptr->y_pixels_per_unit = res_y;
735
info_ptr->phys_unit_type = (png_byte)unit_type;
736
info_ptr->valid |= PNG_INFO_pHYs;
737
}
738
#endif
739
740
void PNGAPI
741
png_set_PLTE(png_structrp png_ptr, png_inforp info_ptr,
742
png_const_colorp palette, int num_palette)
743
{
744
745
png_uint_32 max_palette_length;
746
747
png_debug1(1, "in %s storage function", "PLTE");
748
749
if (png_ptr == NULL || info_ptr == NULL)
750
return;
751
752
max_palette_length = (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) ?
753
(1 << info_ptr->bit_depth) : PNG_MAX_PALETTE_LENGTH;
754
755
if (num_palette < 0 || num_palette > (int) max_palette_length)
756
{
757
if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
758
png_error(png_ptr, "Invalid palette length");
759
760
else
761
{
762
png_warning(png_ptr, "Invalid palette length");
763
764
return;
765
}
766
}
767
768
if ((num_palette > 0 && palette == NULL) ||
769
(num_palette == 0
770
# ifdef PNG_MNG_FEATURES_SUPPORTED
771
&& (png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE) == 0
772
# endif
773
))
774
{
775
png_error(png_ptr, "Invalid palette");
776
}
777
778
/* It may not actually be necessary to set png_ptr->palette here;
779
* we do it for backward compatibility with the way the png_handle_tRNS
780
* function used to do the allocation.
781
*
782
* 1.6.0: the above statement appears to be incorrect; something has to set
783
* the palette inside png_struct on read.
784
*/
785
png_free_data(png_ptr, info_ptr, PNG_FREE_PLTE, 0);
786
787
/* Changed in libpng-1.2.1 to allocate PNG_MAX_PALETTE_LENGTH instead
788
* of num_palette entries, in case of an invalid PNG file or incorrect
789
* call to png_set_PLTE() with too-large sample values.
790
*/
791
png_ptr->palette = png_voidcast(png_colorp, png_calloc(png_ptr,
792
PNG_MAX_PALETTE_LENGTH * (sizeof (png_color))));
793
794
if (num_palette > 0)
795
memcpy(png_ptr->palette, palette, (unsigned int)num_palette *
796
(sizeof (png_color)));
797
798
info_ptr->palette = png_ptr->palette;
799
info_ptr->num_palette = png_ptr->num_palette = (png_uint_16)num_palette;
800
info_ptr->free_me |= PNG_FREE_PLTE;
801
info_ptr->valid |= PNG_INFO_PLTE;
802
}
803
804
#ifdef PNG_sBIT_SUPPORTED
805
void PNGAPI
806
png_set_sBIT(png_const_structrp png_ptr, png_inforp info_ptr,
807
png_const_color_8p sig_bit)
808
{
809
png_debug1(1, "in %s storage function", "sBIT");
810
811
if (png_ptr == NULL || info_ptr == NULL || sig_bit == NULL)
812
return;
813
814
info_ptr->sig_bit = *sig_bit;
815
info_ptr->valid |= PNG_INFO_sBIT;
816
}
817
#endif
818
819
#ifdef PNG_sRGB_SUPPORTED
820
void PNGAPI
821
png_set_sRGB(png_const_structrp png_ptr, png_inforp info_ptr, int srgb_intent)
822
{
823
png_debug1(1, "in %s storage function", "sRGB");
824
825
if (png_ptr == NULL || info_ptr == NULL)
826
return;
827
828
info_ptr->rendering_intent = srgb_intent;
829
info_ptr->valid |= PNG_INFO_sRGB;
830
}
831
832
void PNGAPI
833
png_set_sRGB_gAMA_and_cHRM(png_const_structrp png_ptr, png_inforp info_ptr,
834
int srgb_intent)
835
{
836
png_debug1(1, "in %s storage function", "sRGB_gAMA_and_cHRM");
837
838
if (png_ptr == NULL || info_ptr == NULL)
839
return;
840
841
png_set_sRGB(png_ptr, info_ptr, srgb_intent);
842
843
# ifdef PNG_gAMA_SUPPORTED
844
png_set_gAMA_fixed(png_ptr, info_ptr, PNG_GAMMA_sRGB_INVERSE);
845
# endif /* gAMA */
846
847
# ifdef PNG_cHRM_SUPPORTED
848
png_set_cHRM_fixed(png_ptr, info_ptr,
849
/* color x y */
850
/* white */ 31270, 32900,
851
/* red */ 64000, 33000,
852
/* green */ 30000, 60000,
853
/* blue */ 15000, 6000);
854
# endif /* cHRM */
855
}
856
#endif /* sRGB */
857
858
859
#ifdef PNG_iCCP_SUPPORTED
860
void PNGAPI
861
png_set_iCCP(png_const_structrp png_ptr, png_inforp info_ptr,
862
png_const_charp name, int compression_type,
863
png_const_bytep profile, png_uint_32 proflen)
864
{
865
png_charp new_iccp_name;
866
png_bytep new_iccp_profile;
867
size_t length;
868
869
png_debug1(1, "in %s storage function", "iCCP");
870
871
if (png_ptr == NULL || info_ptr == NULL || name == NULL || profile == NULL)
872
return;
873
874
if (compression_type != PNG_COMPRESSION_TYPE_BASE)
875
png_app_error(png_ptr, "Invalid iCCP compression method");
876
877
length = strlen(name)+1;
878
new_iccp_name = png_voidcast(png_charp, png_malloc_warn(png_ptr, length));
879
880
if (new_iccp_name == NULL)
881
{
882
png_benign_error(png_ptr, "Insufficient memory to process iCCP chunk");
883
884
return;
885
}
886
887
memcpy(new_iccp_name, name, length);
888
new_iccp_profile = png_voidcast(png_bytep,
889
png_malloc_warn(png_ptr, proflen));
890
891
if (new_iccp_profile == NULL)
892
{
893
png_free(png_ptr, new_iccp_name);
894
png_benign_error(png_ptr,
895
"Insufficient memory to process iCCP profile");
896
897
return;
898
}
899
900
memcpy(new_iccp_profile, profile, proflen);
901
902
png_free_data(png_ptr, info_ptr, PNG_FREE_ICCP, 0);
903
904
info_ptr->iccp_proflen = proflen;
905
info_ptr->iccp_name = new_iccp_name;
906
info_ptr->iccp_profile = new_iccp_profile;
907
info_ptr->free_me |= PNG_FREE_ICCP;
908
info_ptr->valid |= PNG_INFO_iCCP;
909
}
910
#endif
911
912
#ifdef PNG_TEXT_SUPPORTED
913
void PNGAPI
914
png_set_text(png_const_structrp png_ptr, png_inforp info_ptr,
915
png_const_textp text_ptr, int num_text)
916
{
917
int ret;
918
ret = png_set_text_2(png_ptr, info_ptr, text_ptr, num_text);
919
920
if (ret != 0)
921
png_error(png_ptr, "Insufficient memory to store text");
922
}
923
924
int /* PRIVATE */
925
png_set_text_2(png_const_structrp png_ptr, png_inforp info_ptr,
926
png_const_textp text_ptr, int num_text)
927
{
928
int i;
929
930
png_debug1(1, "in text storage function, chunk typeid = 0x%lx",
931
png_ptr == NULL ? 0xabadca11UL : (unsigned long)png_ptr->chunk_name);
932
933
if (png_ptr == NULL || info_ptr == NULL || num_text <= 0 || text_ptr == NULL)
934
return 0;
935
936
/* Make sure we have enough space in the "text" array in info_struct
937
* to hold all of the incoming text_ptr objects. This compare can't overflow
938
* because max_text >= num_text (anyway, subtract of two positive integers
939
* can't overflow in any case.)
940
*/
941
if (num_text > info_ptr->max_text - info_ptr->num_text)
942
{
943
int old_num_text = info_ptr->num_text;
944
int max_text;
945
png_textp new_text = NULL;
946
947
/* Calculate an appropriate max_text, checking for overflow. */
948
max_text = old_num_text;
949
if (num_text <= INT_MAX - max_text)
950
{
951
max_text += num_text;
952
953
/* Round up to a multiple of 8 */
954
if (max_text < INT_MAX-8)
955
max_text = (max_text + 8) & ~0x7;
956
957
else
958
max_text = INT_MAX;
959
960
/* Now allocate a new array and copy the old members in; this does all
961
* the overflow checks.
962
*/
963
new_text = png_voidcast(png_textp,png_realloc_array(png_ptr,
964
info_ptr->text, old_num_text, max_text-old_num_text,
965
sizeof *new_text));
966
}
967
968
if (new_text == NULL)
969
{
970
png_chunk_report(png_ptr, "too many text chunks",
971
PNG_CHUNK_WRITE_ERROR);
972
973
return 1;
974
}
975
976
png_free(png_ptr, info_ptr->text);
977
978
info_ptr->text = new_text;
979
info_ptr->free_me |= PNG_FREE_TEXT;
980
info_ptr->max_text = max_text;
981
/* num_text is adjusted below as the entries are copied in */
982
983
png_debug1(3, "allocated %d entries for info_ptr->text", max_text);
984
}
985
986
for (i = 0; i < num_text; i++)
987
{
988
size_t text_length, key_len;
989
size_t lang_len, lang_key_len;
990
png_textp textp = &(info_ptr->text[info_ptr->num_text]);
991
992
if (text_ptr[i].key == NULL)
993
continue;
994
995
if (text_ptr[i].compression < PNG_TEXT_COMPRESSION_NONE ||
996
text_ptr[i].compression >= PNG_TEXT_COMPRESSION_LAST)
997
{
998
png_chunk_report(png_ptr, "text compression mode is out of range",
999
PNG_CHUNK_WRITE_ERROR);
1000
continue;
1001
}
1002
1003
key_len = strlen(text_ptr[i].key);
1004
1005
if (text_ptr[i].compression <= 0)
1006
{
1007
lang_len = 0;
1008
lang_key_len = 0;
1009
}
1010
1011
else
1012
# ifdef PNG_iTXt_SUPPORTED
1013
{
1014
/* Set iTXt data */
1015
1016
if (text_ptr[i].lang != NULL)
1017
lang_len = strlen(text_ptr[i].lang);
1018
1019
else
1020
lang_len = 0;
1021
1022
if (text_ptr[i].lang_key != NULL)
1023
lang_key_len = strlen(text_ptr[i].lang_key);
1024
1025
else
1026
lang_key_len = 0;
1027
}
1028
# else /* iTXt */
1029
{
1030
png_chunk_report(png_ptr, "iTXt chunk not supported",
1031
PNG_CHUNK_WRITE_ERROR);
1032
continue;
1033
}
1034
# endif
1035
1036
if (text_ptr[i].text == NULL || text_ptr[i].text[0] == '\0')
1037
{
1038
text_length = 0;
1039
# ifdef PNG_iTXt_SUPPORTED
1040
if (text_ptr[i].compression > 0)
1041
textp->compression = PNG_ITXT_COMPRESSION_NONE;
1042
1043
else
1044
# endif
1045
textp->compression = PNG_TEXT_COMPRESSION_NONE;
1046
}
1047
1048
else
1049
{
1050
text_length = strlen(text_ptr[i].text);
1051
textp->compression = text_ptr[i].compression;
1052
}
1053
1054
textp->key = png_voidcast(png_charp,png_malloc_base(png_ptr,
1055
key_len + text_length + lang_len + lang_key_len + 4));
1056
1057
if (textp->key == NULL)
1058
{
1059
png_chunk_report(png_ptr, "text chunk: out of memory",
1060
PNG_CHUNK_WRITE_ERROR);
1061
1062
return 1;
1063
}
1064
1065
png_debug2(2, "Allocated %lu bytes at %p in png_set_text",
1066
(unsigned long)(png_uint_32)
1067
(key_len + lang_len + lang_key_len + text_length + 4),
1068
textp->key);
1069
1070
memcpy(textp->key, text_ptr[i].key, key_len);
1071
*(textp->key + key_len) = '\0';
1072
1073
if (text_ptr[i].compression > 0)
1074
{
1075
textp->lang = textp->key + key_len + 1;
1076
memcpy(textp->lang, text_ptr[i].lang, lang_len);
1077
*(textp->lang + lang_len) = '\0';
1078
textp->lang_key = textp->lang + lang_len + 1;
1079
memcpy(textp->lang_key, text_ptr[i].lang_key, lang_key_len);
1080
*(textp->lang_key + lang_key_len) = '\0';
1081
textp->text = textp->lang_key + lang_key_len + 1;
1082
}
1083
1084
else
1085
{
1086
textp->lang=NULL;
1087
textp->lang_key=NULL;
1088
textp->text = textp->key + key_len + 1;
1089
}
1090
1091
if (text_length != 0)
1092
memcpy(textp->text, text_ptr[i].text, text_length);
1093
1094
*(textp->text + text_length) = '\0';
1095
1096
# ifdef PNG_iTXt_SUPPORTED
1097
if (textp->compression > 0)
1098
{
1099
textp->text_length = 0;
1100
textp->itxt_length = text_length;
1101
}
1102
1103
else
1104
# endif
1105
{
1106
textp->text_length = text_length;
1107
textp->itxt_length = 0;
1108
}
1109
1110
info_ptr->num_text++;
1111
png_debug1(3, "transferred text chunk %d", info_ptr->num_text);
1112
}
1113
1114
return 0;
1115
}
1116
#endif
1117
1118
#ifdef PNG_tIME_SUPPORTED
1119
void PNGAPI
1120
png_set_tIME(png_const_structrp png_ptr, png_inforp info_ptr,
1121
png_const_timep mod_time)
1122
{
1123
png_debug1(1, "in %s storage function", "tIME");
1124
1125
if (png_ptr == NULL || info_ptr == NULL || mod_time == NULL ||
1126
(png_ptr->mode & PNG_WROTE_tIME) != 0)
1127
return;
1128
1129
if (mod_time->month == 0 || mod_time->month > 12 ||
1130
mod_time->day == 0 || mod_time->day > 31 ||
1131
mod_time->hour > 23 || mod_time->minute > 59 ||
1132
mod_time->second > 60)
1133
{
1134
png_warning(png_ptr, "Ignoring invalid time value");
1135
1136
return;
1137
}
1138
1139
info_ptr->mod_time = *mod_time;
1140
info_ptr->valid |= PNG_INFO_tIME;
1141
}
1142
#endif
1143
1144
#ifdef PNG_tRNS_SUPPORTED
1145
void PNGAPI
1146
png_set_tRNS(png_structrp png_ptr, png_inforp info_ptr,
1147
png_const_bytep trans_alpha, int num_trans, png_const_color_16p trans_color)
1148
{
1149
png_debug1(1, "in %s storage function", "tRNS");
1150
1151
if (png_ptr == NULL || info_ptr == NULL)
1152
1153
return;
1154
1155
if (trans_alpha != NULL)
1156
{
1157
/* It may not actually be necessary to set png_ptr->trans_alpha here;
1158
* we do it for backward compatibility with the way the png_handle_tRNS
1159
* function used to do the allocation.
1160
*
1161
* 1.6.0: The above statement is incorrect; png_handle_tRNS effectively
1162
* relies on png_set_tRNS storing the information in png_struct
1163
* (otherwise it won't be there for the code in pngrtran.c).
1164
*/
1165
1166
png_free_data(png_ptr, info_ptr, PNG_FREE_TRNS, 0);
1167
1168
if (num_trans > 0 && num_trans <= PNG_MAX_PALETTE_LENGTH)
1169
{
1170
/* Changed from num_trans to PNG_MAX_PALETTE_LENGTH in version 1.2.1 */
1171
info_ptr->trans_alpha = png_voidcast(png_bytep,
1172
png_malloc(png_ptr, PNG_MAX_PALETTE_LENGTH));
1173
memcpy(info_ptr->trans_alpha, trans_alpha, (size_t)num_trans);
1174
1175
info_ptr->free_me |= PNG_FREE_TRNS;
1176
info_ptr->valid |= PNG_INFO_tRNS;
1177
}
1178
png_ptr->trans_alpha = info_ptr->trans_alpha;
1179
}
1180
1181
if (trans_color != NULL)
1182
{
1183
#ifdef PNG_WARNINGS_SUPPORTED
1184
if (info_ptr->bit_depth < 16)
1185
{
1186
int sample_max = (1 << info_ptr->bit_depth) - 1;
1187
1188
if ((info_ptr->color_type == PNG_COLOR_TYPE_GRAY &&
1189
trans_color->gray > sample_max) ||
1190
(info_ptr->color_type == PNG_COLOR_TYPE_RGB &&
1191
(trans_color->red > sample_max ||
1192
trans_color->green > sample_max ||
1193
trans_color->blue > sample_max)))
1194
png_warning(png_ptr,
1195
"tRNS chunk has out-of-range samples for bit_depth");
1196
}
1197
#endif
1198
1199
info_ptr->trans_color = *trans_color;
1200
1201
if (num_trans == 0)
1202
num_trans = 1;
1203
}
1204
1205
info_ptr->num_trans = (png_uint_16)num_trans;
1206
1207
if (num_trans != 0)
1208
{
1209
info_ptr->free_me |= PNG_FREE_TRNS;
1210
info_ptr->valid |= PNG_INFO_tRNS;
1211
}
1212
}
1213
#endif
1214
1215
#ifdef PNG_sPLT_SUPPORTED
1216
void PNGAPI
1217
png_set_sPLT(png_const_structrp png_ptr,
1218
png_inforp info_ptr, png_const_sPLT_tp entries, int nentries)
1219
/*
1220
* entries - array of png_sPLT_t structures
1221
* to be added to the list of palettes
1222
* in the info structure.
1223
*
1224
* nentries - number of palette structures to be
1225
* added.
1226
*/
1227
{
1228
png_sPLT_tp np;
1229
1230
png_debug1(1, "in %s storage function", "sPLT");
1231
1232
if (png_ptr == NULL || info_ptr == NULL || nentries <= 0 || entries == NULL)
1233
return;
1234
1235
/* Use the internal realloc function, which checks for all the possible
1236
* overflows. Notice that the parameters are (int) and (size_t)
1237
*/
1238
np = png_voidcast(png_sPLT_tp,png_realloc_array(png_ptr,
1239
info_ptr->splt_palettes, info_ptr->splt_palettes_num, nentries,
1240
sizeof *np));
1241
1242
if (np == NULL)
1243
{
1244
/* Out of memory or too many chunks */
1245
png_chunk_report(png_ptr, "too many sPLT chunks", PNG_CHUNK_WRITE_ERROR);
1246
return;
1247
}
1248
1249
png_free(png_ptr, info_ptr->splt_palettes);
1250
1251
info_ptr->splt_palettes = np;
1252
info_ptr->free_me |= PNG_FREE_SPLT;
1253
1254
np += info_ptr->splt_palettes_num;
1255
1256
do
1257
{
1258
size_t length;
1259
1260
/* Skip invalid input entries */
1261
if (entries->name == NULL || entries->entries == NULL)
1262
{
1263
/* png_handle_sPLT doesn't do this, so this is an app error */
1264
png_app_error(png_ptr, "png_set_sPLT: invalid sPLT");
1265
/* Just skip the invalid entry */
1266
continue;
1267
}
1268
1269
np->depth = entries->depth;
1270
1271
/* In the event of out-of-memory just return - there's no point keeping
1272
* on trying to add sPLT chunks.
1273
*/
1274
length = strlen(entries->name) + 1;
1275
np->name = png_voidcast(png_charp, png_malloc_base(png_ptr, length));
1276
1277
if (np->name == NULL)
1278
break;
1279
1280
memcpy(np->name, entries->name, length);
1281
1282
/* IMPORTANT: we have memory now that won't get freed if something else
1283
* goes wrong; this code must free it. png_malloc_array produces no
1284
* warnings; use a png_chunk_report (below) if there is an error.
1285
*/
1286
np->entries = png_voidcast(png_sPLT_entryp, png_malloc_array(png_ptr,
1287
entries->nentries, sizeof (png_sPLT_entry)));
1288
1289
if (np->entries == NULL)
1290
{
1291
png_free(png_ptr, np->name);
1292
np->name = NULL;
1293
break;
1294
}
1295
1296
np->nentries = entries->nentries;
1297
/* This multiply can't overflow because png_malloc_array has already
1298
* checked it when doing the allocation.
1299
*/
1300
memcpy(np->entries, entries->entries,
1301
(unsigned int)entries->nentries * sizeof (png_sPLT_entry));
1302
1303
/* Note that 'continue' skips the advance of the out pointer and out
1304
* count, so an invalid entry is not added.
1305
*/
1306
info_ptr->valid |= PNG_INFO_sPLT;
1307
++(info_ptr->splt_palettes_num);
1308
++np;
1309
++entries;
1310
}
1311
while (--nentries);
1312
1313
if (nentries > 0)
1314
png_chunk_report(png_ptr, "sPLT out of memory", PNG_CHUNK_WRITE_ERROR);
1315
}
1316
#endif /* sPLT */
1317
1318
#ifdef PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED
1319
static png_byte
1320
check_location(png_const_structrp png_ptr, int location)
1321
{
1322
location &= (PNG_HAVE_IHDR|PNG_HAVE_PLTE|PNG_AFTER_IDAT);
1323
1324
/* New in 1.6.0; copy the location and check it. This is an API
1325
* change; previously the app had to use the
1326
* png_set_unknown_chunk_location API below for each chunk.
1327
*/
1328
if (location == 0 && (png_ptr->mode & PNG_IS_READ_STRUCT) == 0)
1329
{
1330
/* Write struct, so unknown chunks come from the app */
1331
png_app_warning(png_ptr,
1332
"png_set_unknown_chunks now expects a valid location");
1333
/* Use the old behavior */
1334
location = (png_byte)(png_ptr->mode &
1335
(PNG_HAVE_IHDR|PNG_HAVE_PLTE|PNG_AFTER_IDAT));
1336
}
1337
1338
/* This need not be an internal error - if the app calls
1339
* png_set_unknown_chunks on a read pointer it must get the location right.
1340
*/
1341
if (location == 0)
1342
png_error(png_ptr, "invalid location in png_set_unknown_chunks");
1343
1344
/* Now reduce the location to the top-most set bit by removing each least
1345
* significant bit in turn.
1346
*/
1347
while (location != (location & -location))
1348
location &= ~(location & -location);
1349
1350
/* The cast is safe because 'location' is a bit mask and only the low four
1351
* bits are significant.
1352
*/
1353
return (png_byte)location;
1354
}
1355
1356
void PNGAPI
1357
png_set_unknown_chunks(png_const_structrp png_ptr,
1358
png_inforp info_ptr, png_const_unknown_chunkp unknowns, int num_unknowns)
1359
{
1360
png_unknown_chunkp np;
1361
1362
if (png_ptr == NULL || info_ptr == NULL || num_unknowns <= 0 ||
1363
unknowns == NULL)
1364
return;
1365
1366
/* Check for the failure cases where support has been disabled at compile
1367
* time. This code is hardly ever compiled - it's here because
1368
* STORE_UNKNOWN_CHUNKS is set by both read and write code (compiling in this
1369
* code) but may be meaningless if the read or write handling of unknown
1370
* chunks is not compiled in.
1371
*/
1372
# if !defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED) && \
1373
defined(PNG_READ_SUPPORTED)
1374
if ((png_ptr->mode & PNG_IS_READ_STRUCT) != 0)
1375
{
1376
png_app_error(png_ptr, "no unknown chunk support on read");
1377
1378
return;
1379
}
1380
# endif
1381
# if !defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED) && \
1382
defined(PNG_WRITE_SUPPORTED)
1383
if ((png_ptr->mode & PNG_IS_READ_STRUCT) == 0)
1384
{
1385
png_app_error(png_ptr, "no unknown chunk support on write");
1386
1387
return;
1388
}
1389
# endif
1390
1391
/* Prior to 1.6.0 this code used png_malloc_warn; however, this meant that
1392
* unknown critical chunks could be lost with just a warning resulting in
1393
* undefined behavior. Now png_chunk_report is used to provide behavior
1394
* appropriate to read or write.
1395
*/
1396
np = png_voidcast(png_unknown_chunkp, png_realloc_array(png_ptr,
1397
info_ptr->unknown_chunks, info_ptr->unknown_chunks_num, num_unknowns,
1398
sizeof *np));
1399
1400
if (np == NULL)
1401
{
1402
png_chunk_report(png_ptr, "too many unknown chunks",
1403
PNG_CHUNK_WRITE_ERROR);
1404
return;
1405
}
1406
1407
png_free(png_ptr, info_ptr->unknown_chunks);
1408
1409
info_ptr->unknown_chunks = np; /* safe because it is initialized */
1410
info_ptr->free_me |= PNG_FREE_UNKN;
1411
1412
np += info_ptr->unknown_chunks_num;
1413
1414
/* Increment unknown_chunks_num each time round the loop to protect the
1415
* just-allocated chunk data.
1416
*/
1417
for (; num_unknowns > 0; --num_unknowns, ++unknowns)
1418
{
1419
memcpy(np->name, unknowns->name, (sizeof np->name));
1420
np->name[(sizeof np->name)-1] = '\0';
1421
np->location = check_location(png_ptr, unknowns->location);
1422
1423
if (unknowns->size == 0)
1424
{
1425
np->data = NULL;
1426
np->size = 0;
1427
}
1428
1429
else
1430
{
1431
np->data = png_voidcast(png_bytep,
1432
png_malloc_base(png_ptr, unknowns->size));
1433
1434
if (np->data == NULL)
1435
{
1436
png_chunk_report(png_ptr, "unknown chunk: out of memory",
1437
PNG_CHUNK_WRITE_ERROR);
1438
/* But just skip storing the unknown chunk */
1439
continue;
1440
}
1441
1442
memcpy(np->data, unknowns->data, unknowns->size);
1443
np->size = unknowns->size;
1444
}
1445
1446
/* These increments are skipped on out-of-memory for the data - the
1447
* unknown chunk entry gets overwritten if the png_chunk_report returns.
1448
* This is correct in the read case (the chunk is just dropped.)
1449
*/
1450
++np;
1451
++(info_ptr->unknown_chunks_num);
1452
}
1453
}
1454
1455
void PNGAPI
1456
png_set_unknown_chunk_location(png_const_structrp png_ptr, png_inforp info_ptr,
1457
int chunk, int location)
1458
{
1459
/* This API is pretty pointless in 1.6.0 because the location can be set
1460
* before the call to png_set_unknown_chunks.
1461
*
1462
* TODO: add a png_app_warning in 1.7
1463
*/
1464
if (png_ptr != NULL && info_ptr != NULL && chunk >= 0 &&
1465
chunk < info_ptr->unknown_chunks_num)
1466
{
1467
if ((location & (PNG_HAVE_IHDR|PNG_HAVE_PLTE|PNG_AFTER_IDAT)) == 0)
1468
{
1469
png_app_error(png_ptr, "invalid unknown chunk location");
1470
/* Fake out the pre 1.6.0 behavior: */
1471
if (((unsigned int)location & PNG_HAVE_IDAT) != 0) /* undocumented! */
1472
location = PNG_AFTER_IDAT;
1473
1474
else
1475
location = PNG_HAVE_IHDR; /* also undocumented */
1476
}
1477
1478
info_ptr->unknown_chunks[chunk].location =
1479
check_location(png_ptr, location);
1480
}
1481
}
1482
#endif /* STORE_UNKNOWN_CHUNKS */
1483
1484
#ifdef PNG_MNG_FEATURES_SUPPORTED
1485
png_uint_32 PNGAPI
1486
png_permit_mng_features(png_structrp png_ptr, png_uint_32 mng_features)
1487
{
1488
png_debug(1, "in png_permit_mng_features");
1489
1490
if (png_ptr == NULL)
1491
return 0;
1492
1493
png_ptr->mng_features_permitted = mng_features & PNG_ALL_MNG_FEATURES;
1494
1495
return png_ptr->mng_features_permitted;
1496
}
1497
#endif
1498
1499
#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
1500
static unsigned int
1501
add_one_chunk(png_bytep list, unsigned int count, png_const_bytep add, int keep)
1502
{
1503
unsigned int i;
1504
1505
/* Utility function: update the 'keep' state of a chunk if it is already in
1506
* the list, otherwise add it to the list.
1507
*/
1508
for (i=0; i<count; ++i, list += 5)
1509
{
1510
if (memcmp(list, add, 4) == 0)
1511
{
1512
list[4] = (png_byte)keep;
1513
1514
return count;
1515
}
1516
}
1517
1518
if (keep != PNG_HANDLE_CHUNK_AS_DEFAULT)
1519
{
1520
++count;
1521
memcpy(list, add, 4);
1522
list[4] = (png_byte)keep;
1523
}
1524
1525
return count;
1526
}
1527
1528
void PNGAPI
1529
png_set_keep_unknown_chunks(png_structrp png_ptr, int keep,
1530
png_const_bytep chunk_list, int num_chunks_in)
1531
{
1532
png_bytep new_list;
1533
unsigned int num_chunks, old_num_chunks;
1534
1535
if (png_ptr == NULL)
1536
return;
1537
1538
if (keep < 0 || keep >= PNG_HANDLE_CHUNK_LAST)
1539
{
1540
png_app_error(png_ptr, "png_set_keep_unknown_chunks: invalid keep");
1541
1542
return;
1543
}
1544
1545
if (num_chunks_in <= 0)
1546
{
1547
png_ptr->unknown_default = keep;
1548
1549
/* '0' means just set the flags, so stop here */
1550
if (num_chunks_in == 0)
1551
return;
1552
}
1553
1554
if (num_chunks_in < 0)
1555
{
1556
/* Ignore all unknown chunks and all chunks recognized by
1557
* libpng except for IHDR, PLTE, tRNS, IDAT, and IEND
1558
*/
1559
static const png_byte chunks_to_ignore[] = {
1560
98, 75, 71, 68, '\0', /* bKGD */
1561
99, 72, 82, 77, '\0', /* cHRM */
1562
99, 73, 67, 80, '\0', /* cICP */
1563
99, 76, 76, 73, '\0', /* cLLI */
1564
101, 88, 73, 102, '\0', /* eXIf */
1565
103, 65, 77, 65, '\0', /* gAMA */
1566
104, 73, 83, 84, '\0', /* hIST */
1567
105, 67, 67, 80, '\0', /* iCCP */
1568
105, 84, 88, 116, '\0', /* iTXt */
1569
109, 68, 67, 86, '\0', /* mDCV */
1570
111, 70, 70, 115, '\0', /* oFFs */
1571
112, 67, 65, 76, '\0', /* pCAL */
1572
112, 72, 89, 115, '\0', /* pHYs */
1573
115, 66, 73, 84, '\0', /* sBIT */
1574
115, 67, 65, 76, '\0', /* sCAL */
1575
115, 80, 76, 84, '\0', /* sPLT */
1576
115, 84, 69, 82, '\0', /* sTER */
1577
115, 82, 71, 66, '\0', /* sRGB */
1578
116, 69, 88, 116, '\0', /* tEXt */
1579
116, 73, 77, 69, '\0', /* tIME */
1580
122, 84, 88, 116, '\0' /* zTXt */
1581
};
1582
1583
chunk_list = chunks_to_ignore;
1584
num_chunks = (unsigned int)/*SAFE*/(sizeof chunks_to_ignore)/5U;
1585
}
1586
1587
else /* num_chunks_in > 0 */
1588
{
1589
if (chunk_list == NULL)
1590
{
1591
/* Prior to 1.6.0 this was silently ignored, now it is an app_error
1592
* which can be switched off.
1593
*/
1594
png_app_error(png_ptr, "png_set_keep_unknown_chunks: no chunk list");
1595
1596
return;
1597
}
1598
1599
num_chunks = (unsigned int)num_chunks_in;
1600
}
1601
1602
old_num_chunks = png_ptr->num_chunk_list;
1603
if (png_ptr->chunk_list == NULL)
1604
old_num_chunks = 0;
1605
1606
/* Since num_chunks is always restricted to UINT_MAX/5 this can't overflow.
1607
*/
1608
if (num_chunks + old_num_chunks > UINT_MAX/5)
1609
{
1610
png_app_error(png_ptr, "png_set_keep_unknown_chunks: too many chunks");
1611
1612
return;
1613
}
1614
1615
/* If these chunks are being reset to the default then no more memory is
1616
* required because add_one_chunk above doesn't extend the list if the 'keep'
1617
* parameter is the default.
1618
*/
1619
if (keep != 0)
1620
{
1621
new_list = png_voidcast(png_bytep, png_malloc(png_ptr,
1622
5 * (num_chunks + old_num_chunks)));
1623
1624
if (old_num_chunks > 0)
1625
memcpy(new_list, png_ptr->chunk_list, 5*old_num_chunks);
1626
}
1627
1628
else if (old_num_chunks > 0)
1629
new_list = png_ptr->chunk_list;
1630
1631
else
1632
new_list = NULL;
1633
1634
/* Add the new chunks together with each one's handling code. If the chunk
1635
* already exists the code is updated, otherwise the chunk is added to the
1636
* end. (In libpng 1.6.0 order no longer matters because this code enforces
1637
* the earlier convention that the last setting is the one that is used.)
1638
*/
1639
if (new_list != NULL)
1640
{
1641
png_const_bytep inlist;
1642
png_bytep outlist;
1643
unsigned int i;
1644
1645
for (i=0; i<num_chunks; ++i)
1646
{
1647
old_num_chunks = add_one_chunk(new_list, old_num_chunks,
1648
chunk_list+5*i, keep);
1649
}
1650
1651
/* Now remove any spurious 'default' entries. */
1652
num_chunks = 0;
1653
for (i=0, inlist=outlist=new_list; i<old_num_chunks; ++i, inlist += 5)
1654
{
1655
if (inlist[4])
1656
{
1657
if (outlist != inlist)
1658
memcpy(outlist, inlist, 5);
1659
outlist += 5;
1660
++num_chunks;
1661
}
1662
}
1663
1664
/* This means the application has removed all the specialized handling. */
1665
if (num_chunks == 0)
1666
{
1667
if (png_ptr->chunk_list != new_list)
1668
png_free(png_ptr, new_list);
1669
1670
new_list = NULL;
1671
}
1672
}
1673
1674
else
1675
num_chunks = 0;
1676
1677
png_ptr->num_chunk_list = num_chunks;
1678
1679
if (png_ptr->chunk_list != new_list)
1680
{
1681
if (png_ptr->chunk_list != NULL)
1682
png_free(png_ptr, png_ptr->chunk_list);
1683
1684
png_ptr->chunk_list = new_list;
1685
}
1686
}
1687
#endif
1688
1689
#ifdef PNG_READ_USER_CHUNKS_SUPPORTED
1690
void PNGAPI
1691
png_set_read_user_chunk_fn(png_structrp png_ptr, png_voidp user_chunk_ptr,
1692
png_user_chunk_ptr read_user_chunk_fn)
1693
{
1694
png_debug(1, "in png_set_read_user_chunk_fn");
1695
1696
if (png_ptr == NULL)
1697
return;
1698
1699
png_ptr->read_user_chunk_fn = read_user_chunk_fn;
1700
png_ptr->user_chunk_ptr = user_chunk_ptr;
1701
}
1702
#endif
1703
1704
#ifdef PNG_INFO_IMAGE_SUPPORTED
1705
void PNGAPI
1706
png_set_rows(png_const_structrp png_ptr, png_inforp info_ptr,
1707
png_bytepp row_pointers)
1708
{
1709
png_debug(1, "in png_set_rows");
1710
1711
if (png_ptr == NULL || info_ptr == NULL)
1712
return;
1713
1714
if (info_ptr->row_pointers != NULL &&
1715
(info_ptr->row_pointers != row_pointers))
1716
png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0);
1717
1718
info_ptr->row_pointers = row_pointers;
1719
1720
if (row_pointers != NULL)
1721
info_ptr->valid |= PNG_INFO_IDAT;
1722
}
1723
#endif
1724
1725
void PNGAPI
1726
png_set_compression_buffer_size(png_structrp png_ptr, size_t size)
1727
{
1728
png_debug(1, "in png_set_compression_buffer_size");
1729
1730
if (png_ptr == NULL)
1731
return;
1732
1733
if (size == 0 || size > PNG_UINT_31_MAX)
1734
png_error(png_ptr, "invalid compression buffer size");
1735
1736
# ifdef PNG_SEQUENTIAL_READ_SUPPORTED
1737
if ((png_ptr->mode & PNG_IS_READ_STRUCT) != 0)
1738
{
1739
png_ptr->IDAT_read_size = (png_uint_32)size; /* checked above */
1740
return;
1741
}
1742
# endif
1743
1744
# ifdef PNG_WRITE_SUPPORTED
1745
if ((png_ptr->mode & PNG_IS_READ_STRUCT) == 0)
1746
{
1747
if (png_ptr->zowner != 0)
1748
{
1749
png_warning(png_ptr,
1750
"Compression buffer size cannot be changed because it is in use");
1751
1752
return;
1753
}
1754
1755
#ifndef __COVERITY__
1756
/* Some compilers complain that this is always false. However, it
1757
* can be true when integer overflow happens.
1758
*/
1759
if (size > ZLIB_IO_MAX)
1760
{
1761
png_warning(png_ptr,
1762
"Compression buffer size limited to system maximum");
1763
size = ZLIB_IO_MAX; /* must fit */
1764
}
1765
#endif
1766
1767
if (size < 6)
1768
{
1769
/* Deflate will potentially go into an infinite loop on a SYNC_FLUSH
1770
* if this is permitted.
1771
*/
1772
png_warning(png_ptr,
1773
"Compression buffer size cannot be reduced below 6");
1774
1775
return;
1776
}
1777
1778
if (png_ptr->zbuffer_size != size)
1779
{
1780
png_free_buffer_list(png_ptr, &png_ptr->zbuffer_list);
1781
png_ptr->zbuffer_size = (uInt)size;
1782
}
1783
}
1784
# endif
1785
}
1786
1787
void PNGAPI
1788
png_set_invalid(png_const_structrp png_ptr, png_inforp info_ptr, int mask)
1789
{
1790
if (png_ptr != NULL && info_ptr != NULL)
1791
info_ptr->valid &= (unsigned int)(~mask);
1792
}
1793
1794
1795
#ifdef PNG_SET_USER_LIMITS_SUPPORTED
1796
/* This function was added to libpng 1.2.6 */
1797
void PNGAPI
1798
png_set_user_limits(png_structrp png_ptr, png_uint_32 user_width_max,
1799
png_uint_32 user_height_max)
1800
{
1801
png_debug(1, "in png_set_user_limits");
1802
1803
/* Images with dimensions larger than these limits will be
1804
* rejected by png_set_IHDR(). To accept any PNG datastream
1805
* regardless of dimensions, set both limits to 0x7fffffff.
1806
*/
1807
if (png_ptr == NULL)
1808
return;
1809
1810
png_ptr->user_width_max = user_width_max;
1811
png_ptr->user_height_max = user_height_max;
1812
}
1813
1814
/* This function was added to libpng 1.4.0 */
1815
void PNGAPI
1816
png_set_chunk_cache_max(png_structrp png_ptr, png_uint_32 user_chunk_cache_max)
1817
{
1818
png_debug(1, "in png_set_chunk_cache_max");
1819
1820
if (png_ptr != NULL)
1821
png_ptr->user_chunk_cache_max = user_chunk_cache_max;
1822
}
1823
1824
/* This function was added to libpng 1.4.1 */
1825
void PNGAPI
1826
png_set_chunk_malloc_max(png_structrp png_ptr,
1827
png_alloc_size_t user_chunk_malloc_max)
1828
{
1829
png_debug(1, "in png_set_chunk_malloc_max");
1830
1831
/* pngstruct::user_chunk_malloc_max is initialized to a non-zero value in
1832
* png.c. This API supports '0' for unlimited, make sure the correct
1833
* (unlimited) value is set here to avoid a need to check for 0 everywhere
1834
* the parameter is used.
1835
*/
1836
if (png_ptr != NULL)
1837
{
1838
if (user_chunk_malloc_max == 0U) /* unlimited */
1839
{
1840
# ifdef PNG_MAX_MALLOC_64K
1841
png_ptr->user_chunk_malloc_max = 65536U;
1842
# else
1843
png_ptr->user_chunk_malloc_max = PNG_SIZE_MAX;
1844
# endif
1845
}
1846
else
1847
png_ptr->user_chunk_malloc_max = user_chunk_malloc_max;
1848
}
1849
}
1850
#endif /* ?SET_USER_LIMITS */
1851
1852
1853
#ifdef PNG_BENIGN_ERRORS_SUPPORTED
1854
void PNGAPI
1855
png_set_benign_errors(png_structrp png_ptr, int allowed)
1856
{
1857
png_debug(1, "in png_set_benign_errors");
1858
1859
/* If allowed is 1, png_benign_error() is treated as a warning.
1860
*
1861
* If allowed is 0, png_benign_error() is treated as an error (which
1862
* is the default behavior if png_set_benign_errors() is not called).
1863
*/
1864
1865
if (allowed != 0)
1866
png_ptr->flags |= PNG_FLAG_BENIGN_ERRORS_WARN |
1867
PNG_FLAG_APP_WARNINGS_WARN | PNG_FLAG_APP_ERRORS_WARN;
1868
1869
else
1870
png_ptr->flags &= ~(PNG_FLAG_BENIGN_ERRORS_WARN |
1871
PNG_FLAG_APP_WARNINGS_WARN | PNG_FLAG_APP_ERRORS_WARN);
1872
}
1873
#endif /* BENIGN_ERRORS */
1874
1875
#ifdef PNG_CHECK_FOR_INVALID_INDEX_SUPPORTED
1876
/* Whether to report invalid palette index; added at libng-1.5.10.
1877
* It is possible for an indexed (color-type==3) PNG file to contain
1878
* pixels with invalid (out-of-range) indexes if the PLTE chunk has
1879
* fewer entries than the image's bit-depth would allow. We recover
1880
* from this gracefully by filling any incomplete palette with zeros
1881
* (opaque black). By default, when this occurs libpng will issue
1882
* a benign error. This API can be used to override that behavior.
1883
*/
1884
void PNGAPI
1885
png_set_check_for_invalid_index(png_structrp png_ptr, int allowed)
1886
{
1887
png_debug(1, "in png_set_check_for_invalid_index");
1888
1889
if (allowed > 0)
1890
png_ptr->num_palette_max = 0;
1891
1892
else
1893
png_ptr->num_palette_max = -1;
1894
}
1895
#endif
1896
1897
#if defined(PNG_TEXT_SUPPORTED) || defined(PNG_pCAL_SUPPORTED) || \
1898
defined(PNG_iCCP_SUPPORTED) || defined(PNG_sPLT_SUPPORTED)
1899
/* Check that the tEXt or zTXt keyword is valid per PNG 1.0 specification,
1900
* and if invalid, correct the keyword rather than discarding the entire
1901
* chunk. The PNG 1.0 specification requires keywords 1-79 characters in
1902
* length, forbids leading or trailing whitespace, multiple internal spaces,
1903
* and the non-break space (0x80) from ISO 8859-1. Returns keyword length.
1904
*
1905
* The 'new_key' buffer must be 80 characters in size (for the keyword plus a
1906
* trailing '\0'). If this routine returns 0 then there was no keyword, or a
1907
* valid one could not be generated, and the caller must png_error.
1908
*/
1909
png_uint_32 /* PRIVATE */
1910
png_check_keyword(png_structrp png_ptr, png_const_charp key, png_bytep new_key)
1911
{
1912
#ifdef PNG_WARNINGS_SUPPORTED
1913
png_const_charp orig_key = key;
1914
#endif
1915
png_uint_32 key_len = 0;
1916
int bad_character = 0;
1917
int space = 1;
1918
1919
png_debug(1, "in png_check_keyword");
1920
1921
if (key == NULL)
1922
{
1923
*new_key = 0;
1924
return 0;
1925
}
1926
1927
while (*key && key_len < 79)
1928
{
1929
png_byte ch = (png_byte)*key++;
1930
1931
if ((ch > 32 && ch <= 126) || (ch >= 161 /*&& ch <= 255*/))
1932
{
1933
*new_key++ = ch; ++key_len; space = 0;
1934
}
1935
1936
else if (space == 0)
1937
{
1938
/* A space or an invalid character when one wasn't seen immediately
1939
* before; output just a space.
1940
*/
1941
*new_key++ = 32; ++key_len; space = 1;
1942
1943
/* If the character was not a space then it is invalid. */
1944
if (ch != 32)
1945
bad_character = ch;
1946
}
1947
1948
else if (bad_character == 0)
1949
bad_character = ch; /* just skip it, record the first error */
1950
}
1951
1952
if (key_len > 0 && space != 0) /* trailing space */
1953
{
1954
--key_len; --new_key;
1955
if (bad_character == 0)
1956
bad_character = 32;
1957
}
1958
1959
/* Terminate the keyword */
1960
*new_key = 0;
1961
1962
if (key_len == 0)
1963
return 0;
1964
1965
#ifdef PNG_WARNINGS_SUPPORTED
1966
/* Try to only output one warning per keyword: */
1967
if (*key != 0) /* keyword too long */
1968
png_warning(png_ptr, "keyword truncated");
1969
1970
else if (bad_character != 0)
1971
{
1972
PNG_WARNING_PARAMETERS(p)
1973
1974
png_warning_parameter(p, 1, orig_key);
1975
png_warning_parameter_signed(p, 2, PNG_NUMBER_FORMAT_02x, bad_character);
1976
1977
png_formatted_warning(png_ptr, p, "keyword \"@1\": bad character '0x@2'");
1978
}
1979
#else /* !WARNINGS */
1980
PNG_UNUSED(png_ptr)
1981
#endif /* !WARNINGS */
1982
1983
return key_len;
1984
}
1985
#endif /* TEXT || pCAL || iCCP || sPLT */
1986
#endif /* READ || WRITE */
1987
1988