Coverage Report

Created: 2025-06-24 07:01

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