Coverage Report

Created: 2025-12-31 06:34

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