~ubuntu-branches/ubuntu/intrepid/libpng/intrepid-security

« back to all changes in this revision

Viewing changes to pngwutil.c

  • Committer: Bazaar Package Importer
  • Author(s): Anibal Monsalve Salazar
  • Date: 2006-11-21 19:07:43 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20061121190743-wkt0yzs5uq2xoq10
Tags: 1.2.13-4
Removed drop_pass_width patch. Closes: #399499.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
 
2
2
/* pngwutil.c - utilities to write a PNG file
3
3
 *
4
 
 * libpng version 1.2.8 - December 3, 2004
 
4
 * Last changed in libpng 1.2.13 November 13, 2006
5
5
 * For conditions of distribution and use, see copyright notice in png.h
6
 
 * Copyright (c) 1998-2004 Glenn Randers-Pehrson
 
6
 * Copyright (c) 1998-2006 Glenn Randers-Pehrson
7
7
 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
8
8
 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
9
9
 */
16
16
 * with unsigned numbers for convenience, although one supported
17
17
 * ancillary chunk uses signed (two's complement) numbers.
18
18
 */
19
 
void /* PRIVATE */
 
19
void PNGAPI
20
20
png_save_uint_32(png_bytep buf, png_uint_32 i)
21
21
{
22
22
   buf[0] = (png_byte)((i >> 24) & 0xff);
25
25
   buf[3] = (png_byte)(i & 0xff);
26
26
}
27
27
 
28
 
#if defined(PNG_WRITE_pCAL_SUPPORTED) || defined(PNG_WRITE_oFFs_SUPPORTED)
29
28
/* The png_save_int_32 function assumes integers are stored in two's
30
29
 * complement format.  If this isn't the case, then this routine needs to
31
30
 * be modified to write data in two's complement format.
32
31
 */
33
 
void /* PRIVATE */
 
32
void PNGAPI
34
33
png_save_int_32(png_bytep buf, png_int_32 i)
35
34
{
36
35
   buf[0] = (png_byte)((i >> 24) & 0xff);
38
37
   buf[2] = (png_byte)((i >> 8) & 0xff);
39
38
   buf[3] = (png_byte)(i & 0xff);
40
39
}
41
 
#endif
42
40
 
43
41
/* Place a 16-bit number into a buffer in PNG byte order.
44
42
 * The parameter is declared unsigned int, not png_uint_16,
45
43
 * just to avoid potential problems on pre-ANSI C compilers.
46
44
 */
47
 
void /* PRIVATE */
 
45
void PNGAPI
48
46
png_save_uint_16(png_bytep buf, unsigned int i)
49
47
{
50
48
   buf[0] = (png_byte)((i >> 8) & 0xff);
64
62
png_write_chunk(png_structp png_ptr, png_bytep chunk_name,
65
63
   png_bytep data, png_size_t length)
66
64
{
 
65
   if(png_ptr == NULL) return;
67
66
   png_write_chunk_start(png_ptr, chunk_name, (png_uint_32)length);
68
67
   png_write_chunk_data(png_ptr, data, length);
69
68
   png_write_chunk_end(png_ptr);
79
78
{
80
79
   png_byte buf[4];
81
80
   png_debug2(0, "Writing %s chunk (%lu bytes)\n", chunk_name, length);
 
81
   if(png_ptr == NULL) return;
82
82
 
83
83
   /* write the length */
84
84
   png_save_uint_32(buf, length);
100
100
png_write_chunk_data(png_structp png_ptr, png_bytep data, png_size_t length)
101
101
{
102
102
   /* write the data, and run the CRC over it */
 
103
   if(png_ptr == NULL) return;
103
104
   if (data != NULL && length > 0)
104
105
   {
105
106
      png_calculate_crc(png_ptr, data, length);
113
114
{
114
115
   png_byte buf[4];
115
116
 
 
117
   if(png_ptr == NULL) return;
 
118
 
116
119
   /* write the crc */
117
120
   png_save_uint_32(buf, png_ptr->crc);
118
121
 
161
164
{
162
165
   int ret;
163
166
 
164
 
   comp->num_output_ptr = comp->max_output_ptr = 0;
 
167
   comp->num_output_ptr = 0;
 
168
   comp->max_output_ptr = 0;
165
169
   comp->output_ptr = NULL;
166
170
   comp->input = NULL;
 
171
   comp->input_len = 0;
167
172
 
168
173
   /* we may just want to pass the text right through */
169
174
   if (compression == PNG_TEXT_COMPRESSION_NONE)
728
733
   png_size_t name_len;
729
734
   png_charp new_name;
730
735
   compression_state comp;
 
736
   int embedded_profile_len = 0;
731
737
 
732
738
   png_debug(1, "in png_write_iCCP\n");
 
739
 
 
740
   comp.num_output_ptr = 0;
 
741
   comp.max_output_ptr = 0;
 
742
   comp.output_ptr = NULL;
 
743
   comp.input = NULL;
 
744
   comp.input_len = 0;
 
745
 
733
746
   if (name == NULL || (name_len = png_check_keyword(png_ptr, name,
734
747
      &new_name)) == 0)
735
748
   {
743
756
   if (profile == NULL)
744
757
      profile_len = 0;
745
758
 
 
759
   if (profile_len > 3)
 
760
      embedded_profile_len = ((*(profile  ))<<24) | ((*(profile+1))<<16) |
 
761
          ((*(profile+2))<< 8) | ((*(profile+3))    );
 
762
 
 
763
   if (profile_len < embedded_profile_len)
 
764
     {
 
765
        png_warning(png_ptr,
 
766
          "Embedded profile length too large in iCCP chunk");
 
767
        return;
 
768
     }
 
769
 
 
770
   if (profile_len > embedded_profile_len)
 
771
     {
 
772
        png_warning(png_ptr,
 
773
          "Truncating profile to actual length in iCCP chunk");
 
774
        profile_len = embedded_profile_len;
 
775
     }
 
776
 
746
777
   if (profile_len)
747
778
       profile_len = png_text_compress(png_ptr, profile, (png_size_t)profile_len,
748
779
          PNG_COMPRESSION_TYPE_BASE, &comp);
930
961
   itemp = (png_uint_32)(white_y * 100000.0 + 0.5);
931
962
   png_save_uint_32(buf + 4, itemp);
932
963
 
933
 
   if (red_x < 0 || red_x > 0.8 || red_y < 0 || red_y > 0.8 ||
934
 
       red_x + red_y > 1.0)
 
964
   if (red_x < 0 ||  red_y < 0 || red_x + red_y > 1.0)
935
965
   {
936
966
      png_warning(png_ptr, "Invalid cHRM red point specified");
937
967
      return;
941
971
   itemp = (png_uint_32)(red_y * 100000.0 + 0.5);
942
972
   png_save_uint_32(buf + 12, itemp);
943
973
 
944
 
   if (green_x < 0 || green_x > 0.8 || green_y < 0 || green_y > 0.8 ||
945
 
       green_x + green_y > 1.0)
 
974
   if (green_x < 0 || green_y < 0 || green_x + green_y > 1.0)
946
975
   {
947
976
      png_warning(png_ptr, "Invalid cHRM green point specified");
948
977
      return;
952
981
   itemp = (png_uint_32)(green_y * 100000.0 + 0.5);
953
982
   png_save_uint_32(buf + 20, itemp);
954
983
 
955
 
   if (blue_x < 0 || blue_x > 0.8 || blue_y < 0 || blue_y > 0.8 ||
956
 
       blue_x + blue_y > 1.0)
 
984
   if (blue_x < 0 || blue_y < 0 || blue_x + blue_y > 1.0)
957
985
   {
958
986
      png_warning(png_ptr, "Invalid cHRM blue point specified");
959
987
      return;
991
1019
   png_save_uint_32(buf, (png_uint_32)white_x);
992
1020
   png_save_uint_32(buf + 4, (png_uint_32)white_y);
993
1021
 
994
 
   if (red_x > 80000L || red_y > 80000L || red_x + red_y > 100000L)
 
1022
   if (red_x + red_y > 100000L)
995
1023
   {
996
1024
      png_warning(png_ptr, "Invalid cHRM fixed red point specified");
997
1025
      return;
999
1027
   png_save_uint_32(buf + 8, (png_uint_32)red_x);
1000
1028
   png_save_uint_32(buf + 12, (png_uint_32)red_y);
1001
1029
 
1002
 
   if (green_x > 80000L || green_y > 80000L || green_x + green_y > 100000L)
 
1030
   if (green_x + green_y > 100000L)
1003
1031
   {
1004
1032
      png_warning(png_ptr, "Invalid fixed cHRM green point specified");
1005
1033
      return;
1007
1035
   png_save_uint_32(buf + 16, (png_uint_32)green_x);
1008
1036
   png_save_uint_32(buf + 20, (png_uint_32)green_y);
1009
1037
 
1010
 
   if (blue_x > 80000L || blue_y > 80000L || blue_x + blue_y > 100000L)
 
1038
   if (blue_x + blue_y > 100000L)
1011
1039
   {
1012
1040
      png_warning(png_ptr, "Invalid fixed cHRM blue point specified");
1013
1041
      return;
1344
1372
 
1345
1373
   png_debug(1, "in png_write_zTXt\n");
1346
1374
 
 
1375
   comp.num_output_ptr = 0;
 
1376
   comp.max_output_ptr = 0;
 
1377
   comp.output_ptr = NULL;
 
1378
   comp.input = NULL;
 
1379
   comp.input_len = 0;
 
1380
 
1347
1381
   if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
1348
1382
   {
1349
1383
      png_warning(png_ptr, "Empty keyword in zTXt chunk");
1397
1431
 
1398
1432
   png_debug(1, "in png_write_iTXt\n");
1399
1433
 
 
1434
   comp.num_output_ptr = 0;
 
1435
   comp.max_output_ptr = 0;
 
1436
   comp.output_ptr = NULL;
 
1437
   comp.input = NULL;
 
1438
 
1400
1439
   if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
1401
1440
   {
1402
1441
      png_warning(png_ptr, "Empty keyword in iTXt chunk");
1552
1591
/* write the sCAL chunk */
1553
1592
#if defined(PNG_FLOATING_POINT_SUPPORTED) && !defined(PNG_NO_STDIO)
1554
1593
void /* PRIVATE */
1555
 
png_write_sCAL(png_structp png_ptr, int unit, double width,double height)
 
1594
png_write_sCAL(png_structp png_ptr, int unit, double width, double height)
1556
1595
{
1557
1596
#ifdef PNG_USE_LOCAL_ARRAYS
1558
1597
   PNG_sCAL;
1559
1598
#endif
 
1599
   char buf[64];
1560
1600
   png_size_t total_len;
1561
 
   char wbuf[32], hbuf[32];
1562
 
   png_byte bunit = unit;
1563
1601
 
1564
1602
   png_debug(1, "in png_write_sCAL\n");
1565
1603
 
 
1604
   buf[0] = (char)unit;
1566
1605
#if defined(_WIN32_WCE)
1567
1606
/* sprintf() function is not supported on WindowsCE */
1568
1607
   {
1569
1608
      wchar_t wc_buf[32];
 
1609
      size_t wc_len;
1570
1610
      swprintf(wc_buf, TEXT("%12.12e"), width);
1571
 
      WideCharToMultiByte(CP_ACP, 0, wc_buf, -1, wbuf, 32, NULL, NULL);
 
1611
      wc_len = wcslen(wc_buf);
 
1612
      WideCharToMultiByte(CP_ACP, 0, wc_buf, -1, buf + 1, wc_len, NULL, NULL);
 
1613
      total_len = wc_len + 2;
1572
1614
      swprintf(wc_buf, TEXT("%12.12e"), height);
1573
 
      WideCharToMultiByte(CP_ACP, 0, wc_buf, -1, hbuf, 32, NULL, NULL);
 
1615
      wc_len = wcslen(wc_buf);
 
1616
      WideCharToMultiByte(CP_ACP, 0, wc_buf, -1, buf + total_len, wc_len,
 
1617
         NULL, NULL);
 
1618
      total_len += wc_len;
1574
1619
   }
1575
1620
#else
1576
 
   sprintf(wbuf, "%12.12e", width);
1577
 
   sprintf(hbuf, "%12.12e", height);
 
1621
   sprintf(buf + 1, "%12.12e", width);
 
1622
   total_len = 1 + png_strlen(buf + 1) + 1;
 
1623
   sprintf(buf + total_len, "%12.12e", height);
 
1624
   total_len += png_strlen(buf + total_len);
1578
1625
#endif
1579
 
   total_len = 1 + png_strlen(wbuf)+1 + png_strlen(hbuf);
1580
 
 
1581
 
   png_debug1(3, "sCAL total length = %d\n", (int)total_len);
1582
 
   png_write_chunk_start(png_ptr, (png_bytep)png_sCAL, (png_uint_32)total_len);
1583
 
   png_write_chunk_data(png_ptr, (png_bytep)&bunit, 1);
1584
 
   png_write_chunk_data(png_ptr, (png_bytep)wbuf, png_strlen(wbuf)+1);
1585
 
   png_write_chunk_data(png_ptr, (png_bytep)hbuf, png_strlen(hbuf));
1586
 
 
1587
 
   png_write_chunk_end(png_ptr);
 
1626
 
 
1627
   png_debug1(3, "sCAL total length = %u\n", (unsigned int)total_len);
 
1628
   png_write_chunk(png_ptr, (png_bytep)png_sCAL, (png_bytep)buf, total_len);
1588
1629
}
1589
1630
#else
1590
1631
#ifdef PNG_FIXED_POINT_SUPPORTED
1595
1636
#ifdef PNG_USE_LOCAL_ARRAYS
1596
1637
   PNG_sCAL;
1597
1638
#endif
1598
 
   png_size_t total_len;
1599
 
   char wbuf[32], hbuf[32];
1600
 
   png_byte bunit = unit;
 
1639
   png_byte buf[64];
 
1640
   png_size_t wlen, hlen, total_len;
1601
1641
 
1602
1642
   png_debug(1, "in png_write_sCAL_s\n");
1603
1643
 
1604
 
   png_strcpy(wbuf,(const char *)width);
1605
 
   png_strcpy(hbuf,(const char *)height);
1606
 
   total_len = 1 + png_strlen(wbuf)+1 + png_strlen(hbuf);
1607
 
 
1608
 
   png_debug1(3, "sCAL total length = %d\n", total_len);
1609
 
   png_write_chunk_start(png_ptr, (png_bytep)png_sCAL, (png_uint_32)total_len);
1610
 
   png_write_chunk_data(png_ptr, (png_bytep)&bunit, 1);
1611
 
   png_write_chunk_data(png_ptr, (png_bytep)wbuf, png_strlen(wbuf)+1);
1612
 
   png_write_chunk_data(png_ptr, (png_bytep)hbuf, png_strlen(hbuf));
1613
 
 
1614
 
   png_write_chunk_end(png_ptr);
 
1644
   wlen = png_strlen(width);
 
1645
   hlen = png_strlen(height);
 
1646
   total_len = wlen + hlen + 2;
 
1647
   if (total_len > 64)
 
1648
   {
 
1649
      png_warning(png_ptr, "Can't write sCAL (buffer too small)");
 
1650
      return;
 
1651
   }
 
1652
 
 
1653
   buf[0] = (png_byte)unit;
 
1654
   png_memcpy(buf + 1, width, wlen + 1);      /* append the '\0' here */
 
1655
   png_memcpy(buf + wlen + 2, height, hlen);  /* do NOT append the '\0' here */
 
1656
 
 
1657
   png_debug1(3, "sCAL total length = %u\n", (unsigned int)total_len);
 
1658
   png_write_chunk(png_ptr, (png_bytep)png_sCAL, buf, total_len);
1615
1659
}
1616
1660
#endif
1617
1661
#endif