| /* | |
| * Copyright © 2026 Behdad Esfahbod | |
| * | |
| * This is part of HarfBuzz, a text shaping library. | |
| * | |
| * Permission is hereby granted, without written agreement and without | |
| * license or royalty fees, to use, copy, modify, and distribute this | |
| * software and its documentation for any purpose, provided that the | |
| * above copyright notice and the following two paragraphs appear in | |
| * all copies of this software. | |
| * | |
| * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR | |
| * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES | |
| * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN | |
| * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH | |
| * DAMAGE. | |
| * | |
| * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, | |
| * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND | |
| * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS | |
| * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO | |
| * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. | |
| */ | |
| #ifdef NDEBUG | |
| #undef NDEBUG | |
| #endif | |
| #include <assert.h> | |
| #include <limits.h> | |
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <string.h> | |
| #include <zlib.h> | |
| #include <hb.h> | |
| #include <hb-vector.h> | |
| /* Three pixels per row; rows exercise PNG filters 0 through 4. */ | |
| static const unsigned char rgba_png[] = { | |
| 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d, | |
| 0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x05, | |
| 0x08, 0x06, 0x00, 0x00, 0x00, 0x80, 0x71, 0x56, 0xa2, 0x00, 0x00, 0x00, | |
| 0x40, 0x49, 0x44, 0x41, 0x54, 0x78, 0x9c, 0x63, 0xe0, 0x12, 0x39, 0xc1, | |
| 0xa0, 0x21, 0xb2, 0xaf, 0xc1, 0x4d, 0x64, 0xcb, 0x7f, 0x46, 0x2e, 0xb9, | |
| 0x13, 0x0d, 0x72, 0x0c, 0xdf, 0xea, 0x81, 0x98, 0x91, 0x89, 0x81, 0x8b, | |
| 0xa1, 0x1e, 0x88, 0x19, 0x81, 0xb8, 0x81, 0x99, 0x55, 0x2e, 0xa5, 0x91, | |
| 0x9f, 0xf5, 0x77, 0x03, 0x10, 0xd7, 0xb3, 0x80, 0x44, 0x18, 0x18, 0x80, | |
| 0xb2, 0x0c, 0x0c, 0x8c, 0x00, 0x2c, 0x4d, 0x0e, 0xdd, 0xe3, 0xfd, 0xc3, | |
| 0xd2, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4e, 0x44, 0xae, 0x42, 0x60, | |
| 0x82, | |
| }; | |
| static const unsigned char rgba_color[] = { | |
| 0x0a, 0x14, 0xc8, 0x28, 0x14, 0xbe, 0x46, 0x14, 0xb4, 0x0a, 0x1e, 0xc8, | |
| 0x28, 0x1e, 0xbe, 0x46, 0x1e, 0xb4, 0x0a, 0x28, 0xc8, 0x28, 0x28, 0xbe, | |
| 0x46, 0x28, 0xb4, 0x0a, 0x32, 0xc8, 0x28, 0x32, 0xbe, 0x46, 0x32, 0xb4, | |
| 0x0a, 0x3c, 0xc8, 0x28, 0x3c, 0xbe, 0x46, 0x3c, 0xb4, | |
| }; | |
| static const unsigned char rgba_alpha[] = { | |
| 0x00, 0x80, 0xff, 0x80, 0xff, 0x00, 0xff, 0x00, 0x80, 0x00, 0x80, 0xff, | |
| 0x80, 0xff, 0x00, | |
| }; | |
| static const unsigned char gray_alpha_png[] = { | |
| 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d, | |
| 0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x05, | |
| 0x08, 0x04, 0x00, 0x00, 0x00, 0x2a, 0x78, 0x9e, 0x29, 0x00, 0x00, 0x00, | |
| 0x29, 0x49, 0x44, 0x41, 0x54, 0x78, 0x9c, 0x63, 0x90, 0x63, 0x30, 0x6a, | |
| 0x70, 0xfb, 0xcf, 0xa8, 0xd1, 0x20, 0x52, 0x2f, 0xc2, 0xc8, 0xc4, 0x55, | |
| 0xcf, 0xc5, 0xc8, 0xd5, 0xc0, 0xac, 0xdc, 0xc8, 0xdf, 0xc0, 0x5f, 0xcf, | |
| 0xc2, 0xd5, 0x00, 0xe2, 0x03, 0x00, 0x89, 0xff, 0x07, 0x6d, 0x48, 0x18, | |
| 0x52, 0xb3, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4e, 0x44, 0xae, 0x42, | |
| 0x60, 0x82, | |
| }; | |
| static const unsigned char gray_alpha_color[] = { | |
| 0x1e, 0x32, 0x46, 0x28, 0x3c, 0x50, 0x32, 0x46, 0x5a, 0x3c, 0x50, 0x64, | |
| 0x46, 0x5a, 0x6e, | |
| }; | |
| static const unsigned char gray_alpha_alpha[] = { | |
| 0x00, 0x80, 0xff, 0x80, 0xff, 0x00, 0xff, 0x00, 0x80, 0x00, 0x80, 0xff, | |
| 0x80, 0xff, 0x00, | |
| }; | |
| static const unsigned char indexed_png[] = { | |
| 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d, | |
| 0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x05, | |
| 0x08, 0x03, 0x00, 0x00, 0x00, 0xb7, 0xaf, 0xa6, 0x90, 0x00, 0x00, 0x00, | |
| 0x09, 0x50, 0x4c, 0x54, 0x45, 0xc8, 0x14, 0x0a, 0x0a, 0xc8, 0x14, 0x14, | |
| 0x0a, 0xc8, 0x0f, 0x07, 0xc1, 0x1f, 0x00, 0x00, 0x00, 0x03, 0x74, 0x52, | |
| 0x4e, 0x53, 0x00, 0x80, 0xff, 0xec, 0xf7, 0xb3, 0x18, 0x00, 0x00, 0x00, | |
| 0x1b, 0x49, 0x44, 0x41, 0x54, 0x78, 0x9c, 0x63, 0x60, 0x60, 0x64, 0x62, | |
| 0x64, 0x64, 0xfc, 0xc7, 0xc4, 0xf8, 0x8f, 0x91, 0xf9, 0x3f, 0x23, 0x23, | |
| 0x0b, 0x90, 0x0d, 0x00, 0x1f, 0xa2, 0x04, 0x0f, 0xd9, 0x90, 0xb3, 0x2e, | |
| 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4e, 0x44, 0xae, 0x42, 0x60, 0x82, | |
| }; | |
| static const unsigned char indexed_color[] = { | |
| 0x78, 0x9c, 0x63, 0x60, 0x60, 0x64, 0x62, 0x64, 0x64, 0xfc, 0xc7, 0xc4, | |
| 0xf8, 0x8f, 0x91, 0xf9, 0x3f, 0x23, 0x23, 0x0b, 0x90, 0x0d, 0x00, 0x1f, | |
| 0xa2, 0x04, 0x0f, | |
| }; | |
| static const unsigned char indexed_alpha[] = { | |
| 0x00, 0x80, 0xff, 0x80, 0xff, 0x00, 0xff, 0x00, 0x80, 0x00, 0x80, 0xff, | |
| 0x80, 0xff, 0x00, | |
| }; | |
| /* The indexed image above with 2 bits a pixel: a row of it is one byte. | |
| * Its compressed rows are embedded as they are. */ | |
| static const unsigned char indexed_2bit_png[] = { | |
| 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d, | |
| 0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x05, | |
| 0x02, 0x03, 0x00, 0x00, 0x00, 0xfd, 0x1f, 0xbe, 0x31, 0x00, 0x00, 0x00, | |
| 0x09, 0x50, 0x4c, 0x54, 0x45, 0xc8, 0x14, 0x0a, 0x0a, 0xc8, 0x14, 0x14, | |
| 0x0a, 0xc8, 0x0f, 0x07, 0xc1, 0x1f, 0x00, 0x00, 0x00, 0x03, 0x74, 0x52, | |
| 0x4e, 0x53, 0x00, 0x80, 0xff, 0xec, 0xf7, 0xb3, 0x18, 0x00, 0x00, 0x00, | |
| 0x15, 0x49, 0x44, 0x41, 0x54, 0x78, 0x01, 0x01, 0x0a, 0x00, 0xf5, 0xff, | |
| 0x00, 0x18, 0x00, 0x60, 0x00, 0x84, 0x00, 0x18, 0x00, 0x60, 0x06, 0xbe, | |
| 0x01, 0x75, 0xe6, 0x02, 0xb3, 0x08, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, | |
| 0x4e, 0x44, 0xae, 0x42, 0x60, 0x82, | |
| }; | |
| static const unsigned char indexed_2bit_color[] = { | |
| 0x78, 0x01, 0x01, 0x0a, 0x00, 0xf5, 0xff, 0x00, 0x18, 0x00, 0x60, 0x00, | |
| 0x84, 0x00, 0x18, 0x00, 0x60, 0x06, 0xbe, 0x01, 0x75, | |
| }; | |
| /* Images with no alpha that name one transparent sample in tRNS, two | |
| * bytes a color. Their rows are not filtered, and are stored, not | |
| * compressed. | |
| * | |
| * Gray, a bit a pixel, in a checkerboard, with white transparent. The | |
| * bits of tRNS that are not of a sample are set, and do not count. */ | |
| static const unsigned char gray_1bit_key_png[] = { | |
| 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d, | |
| 0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x05, | |
| 0x01, 0x00, 0x00, 0x00, 0x00, 0xa8, 0x0a, 0x6b, 0x0f, 0x00, 0x00, 0x00, | |
| 0x02, 0x74, 0x52, 0x4e, 0x53, 0xff, 0x01, 0x92, 0xb0, 0x00, 0xdc, 0x00, | |
| 0x00, 0x00, 0x15, 0x49, 0x44, 0x41, 0x54, 0x78, 0x01, 0x01, 0x0a, 0x00, | |
| 0xf5, 0xff, 0x00, 0x40, 0x00, 0xa0, 0x00, 0x40, 0x00, 0xa0, 0x00, 0x40, | |
| 0x0a, 0x0a, 0x02, 0x01, 0xd7, 0x96, 0x08, 0x71, 0x00, 0x00, 0x00, 0x00, | |
| 0x49, 0x45, 0x4e, 0x44, 0xae, 0x42, 0x60, 0x82, | |
| }; | |
| static const unsigned char gray_1bit_key_color[] = { | |
| 0x78, 0x01, 0x01, 0x0a, 0x00, 0xf5, 0xff, 0x00, 0x40, 0x00, 0xa0, 0x00, | |
| 0x40, 0x00, 0xa0, 0x00, 0x40, 0x0a, 0x0a, 0x02, 0x01, | |
| }; | |
| /* Gray, 8 bits a pixel, with 0x32 transparent; the other byte of tRNS | |
| * is set as well. */ | |
| static const unsigned char gray_key_png[] = { | |
| 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d, | |
| 0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x05, | |
| 0x08, 0x00, 0x00, 0x00, 0x00, 0xa5, 0x1a, 0x09, 0x7e, 0x00, 0x00, 0x00, | |
| 0x02, 0x74, 0x52, 0x4e, 0x53, 0xab, 0x32, 0xf3, 0xb7, 0xf9, 0x9a, 0x00, | |
| 0x00, 0x00, 0x1f, 0x49, 0x44, 0x41, 0x54, 0x78, 0x01, 0x01, 0x14, 0x00, | |
| 0xeb, 0xff, 0x00, 0x1e, 0x28, 0x32, 0x00, 0x28, 0x32, 0x3c, 0x00, 0x32, | |
| 0x3c, 0x46, 0x00, 0x3c, 0x46, 0x50, 0x00, 0x46, 0x50, 0x5a, 0x1e, 0x28, | |
| 0x03, 0x85, 0x21, 0xbd, 0xf0, 0x4d, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, | |
| 0x4e, 0x44, 0xae, 0x42, 0x60, 0x82, | |
| }; | |
| static const unsigned char gray_key_color[] = { | |
| 0x78, 0x01, 0x01, 0x14, 0x00, 0xeb, 0xff, 0x00, 0x1e, 0x28, 0x32, 0x00, | |
| 0x28, 0x32, 0x3c, 0x00, 0x32, 0x3c, 0x46, 0x00, 0x3c, 0x46, 0x50, 0x00, | |
| 0x46, 0x50, 0x5a, 0x1e, 0x28, 0x03, 0x85, | |
| }; | |
| /* RGB, with (10, 30, 200) transparent. */ | |
| static const unsigned char rgb_key_png[] = { | |
| 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d, | |
| 0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x05, | |
| 0x08, 0x02, 0x00, 0x00, 0x00, 0x0f, 0x13, 0xc1, 0xf5, 0x00, 0x00, 0x00, | |
| 0x06, 0x74, 0x52, 0x4e, 0x53, 0x00, 0x0a, 0x00, 0x1e, 0x00, 0xc8, 0xa7, | |
| 0x11, 0xdb, 0xc8, 0x00, 0x00, 0x00, 0x3d, 0x49, 0x44, 0x41, 0x54, 0x78, | |
| 0x01, 0x01, 0x32, 0x00, 0xcd, 0xff, 0x00, 0x0a, 0x14, 0xc8, 0x28, 0x14, | |
| 0xbe, 0x46, 0x14, 0xb4, 0x00, 0x0a, 0x1e, 0xc8, 0x28, 0x1e, 0xbe, 0x46, | |
| 0x1e, 0xb4, 0x00, 0x0a, 0x28, 0xc8, 0x28, 0x28, 0xbe, 0x46, 0x28, 0xb4, | |
| 0x00, 0x0a, 0x32, 0xc8, 0x28, 0x32, 0xbe, 0x46, 0x32, 0xb4, 0x00, 0x0a, | |
| 0x3c, 0xc8, 0x28, 0x3c, 0xbe, 0x46, 0x3c, 0xb4, 0x74, 0xe9, 0x0f, 0xd3, | |
| 0xf6, 0x01, 0x0f, 0xdf, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4e, 0x44, | |
| 0xae, 0x42, 0x60, 0x82, | |
| }; | |
| static const unsigned char rgb_key_color[] = { | |
| 0x78, 0x01, 0x01, 0x32, 0x00, 0xcd, 0xff, 0x00, 0x0a, 0x14, 0xc8, 0x28, | |
| 0x14, 0xbe, 0x46, 0x14, 0xb4, 0x00, 0x0a, 0x1e, 0xc8, 0x28, 0x1e, 0xbe, | |
| 0x46, 0x1e, 0xb4, 0x00, 0x0a, 0x28, 0xc8, 0x28, 0x28, 0xbe, 0x46, 0x28, | |
| 0xb4, 0x00, 0x0a, 0x32, 0xc8, 0x28, 0x32, 0xbe, 0x46, 0x32, 0xb4, 0x00, | |
| 0x0a, 0x3c, 0xc8, 0x28, 0x3c, 0xbe, 0x46, 0x3c, 0xb4, 0x74, 0xe9, 0x0f, | |
| 0xd3, | |
| }; | |
| /* Three pixels per row, RGB, interlaced: its rows are in seven passes. */ | |
| static const unsigned char interlaced_png[] = { | |
| 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d, | |
| 0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x05, | |
| 0x08, 0x02, 0x00, 0x00, 0x01, 0x78, 0x14, 0xf1, 0x63, 0x00, 0x00, 0x00, | |
| 0x42, 0x49, 0x44, 0x41, 0x54, 0x78, 0x01, 0x01, 0x37, 0x00, 0xc8, 0xff, | |
| 0x00, 0x0a, 0x14, 0xc8, 0x00, 0x0a, 0xb4, 0xc8, 0x00, 0x5a, 0x14, 0xc8, | |
| 0x00, 0x5a, 0xb4, 0xc8, 0x00, 0x0a, 0x64, 0xc8, 0x5a, 0x64, 0xc8, 0x00, | |
| 0x32, 0x14, 0xc8, 0x00, 0x32, 0x64, 0xc8, 0x00, 0x32, 0xb4, 0xc8, 0x00, | |
| 0x0a, 0x3c, 0xc8, 0x32, 0x3c, 0xc8, 0x5a, 0x3c, 0xc8, 0x00, 0x0a, 0x8c, | |
| 0xc8, 0x32, 0x8c, 0xc8, 0x5a, 0x8c, 0xc8, 0x07, 0xc9, 0x14, 0x83, 0xd2, | |
| 0x9e, 0x51, 0xdb, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4e, 0x44, 0xae, | |
| 0x42, 0x60, 0x82, | |
| }; | |
| static const unsigned char invalid_filter_png[] = { | |
| 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d, | |
| 0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x05, | |
| 0x08, 0x06, 0x00, 0x00, 0x00, 0x80, 0x71, 0x56, 0xa2, 0x00, 0x00, 0x00, | |
| 0x40, 0x49, 0x44, 0x41, 0x54, 0x78, 0x9c, 0x63, 0xe5, 0x12, 0x39, 0xc1, | |
| 0xa0, 0x21, 0xb2, 0xaf, 0xc1, 0x4d, 0x64, 0xcb, 0x7f, 0x46, 0x2e, 0xb9, | |
| 0x13, 0x0d, 0x72, 0x0c, 0xdf, 0xea, 0x81, 0x98, 0x91, 0x89, 0x81, 0x8b, | |
| 0xa1, 0x1e, 0x88, 0x19, 0x81, 0xb8, 0x81, 0x99, 0x55, 0x2e, 0xa5, 0x91, | |
| 0x9f, 0xf5, 0x77, 0x03, 0x10, 0xd7, 0xb3, 0x80, 0x44, 0x18, 0x18, 0x80, | |
| 0xb2, 0x0c, 0x0c, 0x8c, 0x00, 0x2d, 0x92, 0x0e, 0xe2, 0x3a, 0x7b, 0x84, | |
| 0x05, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4e, 0x44, 0xae, 0x42, 0x60, | |
| 0x82, | |
| }; | |
| static const unsigned char uniform_png[] = { | |
| 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d, | |
| 0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x14, | |
| 0x08, 0x06, 0x00, 0x00, 0x00, 0x8d, 0x89, 0x1d, 0x0d, 0x00, 0x00, 0x00, | |
| 0x1e, 0x49, 0x44, 0x41, 0x54, 0x78, 0x9c, 0x63, 0x10, 0x49, 0x39, 0xd1, | |
| 0x40, 0x4d, 0xcc, 0x30, 0x6a, 0xe0, 0xa8, 0x81, 0xa3, 0x06, 0x8e, 0x1a, | |
| 0x38, 0x6a, 0xe0, 0x48, 0x35, 0x10, 0x00, 0x14, 0xce, 0xbc, 0x1f, 0x68, | |
| 0xfa, 0x0f, 0x65, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4e, 0x44, 0xae, | |
| 0x42, 0x60, 0x82, | |
| }; | |
| static const unsigned char rgba_palette_png[] = { | |
| 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d, | |
| 0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x05, | |
| 0x08, 0x06, 0x00, 0x00, 0x00, 0x80, 0x71, 0x56, 0xa2, 0x00, 0x00, 0x00, | |
| 0x03, 0x50, 0x4c, 0x54, 0x45, 0x00, 0x00, 0x00, 0xa7, 0x7a, 0x3d, 0xda, | |
| 0x00, 0x00, 0x00, 0x40, 0x49, 0x44, 0x41, 0x54, 0x78, 0x9c, 0x63, 0xe0, | |
| 0x12, 0x39, 0xc1, 0xa0, 0x21, 0xb2, 0xaf, 0xc1, 0x4d, 0x64, 0xcb, 0x7f, | |
| 0x46, 0x2e, 0xb9, 0x13, 0x0d, 0x72, 0x0c, 0xdf, 0xea, 0x81, 0x98, 0x91, | |
| 0x89, 0x81, 0x8b, 0xa1, 0x1e, 0x88, 0x19, 0x81, 0xb8, 0x81, 0x99, 0x55, | |
| 0x2e, 0xa5, 0x91, 0x9f, 0xf5, 0x77, 0x03, 0x10, 0xd7, 0xb3, 0x80, 0x44, | |
| 0x18, 0x18, 0x80, 0xb2, 0x0c, 0x0c, 0x8c, 0x00, 0x2c, 0x4d, 0x0e, 0xdd, | |
| 0xe3, 0xfd, 0xc3, 0xd2, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4e, 0x44, | |
| 0xae, 0x42, 0x60, 0x82, | |
| }; | |
| static unsigned | |
| find_bytes (const char *data, unsigned len, unsigned start, const char *text) | |
| { | |
| unsigned n = strlen (text); | |
| if (n > len) return UINT_MAX; | |
| for (unsigned i = start; i <= len - n; i++) | |
| if (!memcmp (data + i, text, n)) return i; | |
| return UINT_MAX; | |
| } | |
| static unsigned | |
| read_unsigned (const char *data, unsigned len, unsigned pos) | |
| { | |
| unsigned value = 0; | |
| assert (pos < len && data[pos] >= '0' && data[pos] <= '9'); | |
| while (pos < len && data[pos] >= '0' && data[pos] <= '9') | |
| value = value * 10 + data[pos++] - '0'; | |
| return value; | |
| } | |
| static void | |
| check_stream (const char *data, unsigned len, unsigned object, | |
| const unsigned char *expected, unsigned expected_len, hb_bool_t pixels) | |
| { | |
| unsigned length = find_bytes (data, len, object, "/Length "); | |
| unsigned stream = find_bytes (data, len, object, "stream\n"); | |
| assert (length < stream && stream < len); | |
| unsigned stream_len = read_unsigned (data, len, length + 8); | |
| stream += 7; | |
| assert (stream_len <= len - stream); | |
| if (pixels && find_bytes (data, len, object, "/Filter /FlateDecode") < stream) | |
| { | |
| unsigned char *decoded = malloc (expected_len); | |
| assert (decoded); | |
| uLongf decoded_len = expected_len; | |
| assert (uncompress (decoded, &decoded_len, (const Bytef *) data + stream, stream_len) == Z_OK); | |
| assert (decoded_len == expected_len && !memcmp (decoded, expected, expected_len)); | |
| free (decoded); | |
| } | |
| else | |
| { | |
| assert (stream_len == expected_len); | |
| assert (!memcmp (data + stream, expected, expected_len)); | |
| } | |
| } | |
| static hb_blob_t * | |
| render_png (const unsigned char *png, unsigned len, int64_t budget, | |
| hb_vector_paint_t **paint_out) | |
| { | |
| hb_vector_paint_t *paint = hb_vector_paint_create_or_fail (HB_VECTOR_FORMAT_PDF); | |
| assert (paint); | |
| hb_paint_funcs_t *funcs = hb_vector_paint_get_funcs (paint); | |
| assert (hb_paint_set_budget (funcs, paint, budget)); | |
| hb_glyph_extents_t ext = {0, 50, 30, -50}; | |
| hb_vector_paint_set_glyph_extents (paint, &ext); | |
| hb_blob_t *image = hb_blob_create ((const char *) png, len, | |
| HB_MEMORY_MODE_READONLY, NULL, NULL); | |
| hb_paint_image (funcs, paint, image, 3, 5, HB_PAINT_IMAGE_FORMAT_PNG, 0, &ext); | |
| hb_blob_destroy (image); | |
| *paint_out = paint; | |
| return hb_vector_paint_render (paint); | |
| } | |
| static void | |
| check_alpha_png (const unsigned char *png, unsigned png_len, | |
| const unsigned char *color, unsigned color_len, | |
| const unsigned char *alpha, unsigned alpha_len, | |
| const char *color_space) | |
| { | |
| hb_vector_paint_t *paint; | |
| hb_blob_t *pdf = render_png (png, png_len, HB_BUDGET_DEFAULT, &paint); | |
| assert (pdf); | |
| unsigned len; | |
| const char *data = hb_blob_get_data (pdf, &len); | |
| unsigned smask = find_bytes (data, len, 0, "/SMask "); | |
| assert (smask != UINT_MAX); | |
| unsigned mask_id = read_unsigned (data, len, smask + 7); | |
| char object[32]; | |
| snprintf (object, sizeof (object), "%u 0 obj\n", mask_id); | |
| unsigned mask = find_bytes (data, len, 0, object); | |
| assert (mask != UINT_MAX); | |
| unsigned primary = 0, cursor = 0; | |
| while (cursor < smask) | |
| { | |
| unsigned next = find_bytes (data, len, cursor, "/Subtype /Image"); | |
| if (next >= smask) break; | |
| primary = next; | |
| cursor = next + 1; | |
| } | |
| check_stream (data, len, primary, color, color_len, strcmp (color_space, "/Indexed /DeviceRGB") != 0); | |
| check_stream (data, len, mask, alpha, alpha_len, 1); | |
| unsigned color_pos = find_bytes (data, len, primary, color_space); | |
| assert (color_pos < find_bytes (data, len, primary, "stream\n")); | |
| hb_blob_destroy (pdf); | |
| hb_vector_paint_destroy (paint); | |
| } | |
| /* The transparent sample of an image with no alpha is a color key | |
| * mask: the image's own rows are embedded as they are, and no soft | |
| * mask is made. */ | |
| static void | |
| check_key_png (const unsigned char *png, unsigned png_len, | |
| const unsigned char *color, unsigned color_len, | |
| const char *color_space, const char *mask) | |
| { | |
| hb_vector_paint_t *paint; | |
| hb_blob_t *pdf = render_png (png, png_len, HB_BUDGET_DEFAULT, &paint); | |
| assert (pdf); | |
| unsigned len; | |
| const char *data = hb_blob_get_data (pdf, &len); | |
| unsigned image = find_bytes (data, len, 0, "/Subtype /Image"); | |
| assert (image != UINT_MAX); | |
| unsigned stream = find_bytes (data, len, image, "stream\n"); | |
| assert (stream != UINT_MAX); | |
| assert (find_bytes (data, len, image, color_space) < stream); | |
| assert (find_bytes (data, len, image, mask) < stream); | |
| assert (find_bytes (data, len, 0, "/SMask") == UINT_MAX); | |
| check_stream (data, len, image, color, color_len, 0); | |
| hb_blob_destroy (pdf); | |
| hb_vector_paint_destroy (paint); | |
| } | |
| static void | |
| check_rejected_png (const unsigned char *png, unsigned png_len, int64_t budget) | |
| { | |
| hb_vector_paint_t *paint; | |
| hb_blob_t *pdf = render_png (png, png_len, budget, &paint); | |
| if (pdf) | |
| { | |
| unsigned len; | |
| const char *data = hb_blob_get_data (pdf, &len); | |
| assert (find_bytes (data, len, 0, "/Subtype /Image") == UINT_MAX); | |
| hb_blob_destroy (pdf); | |
| } | |
| hb_vector_paint_destroy (paint); | |
| } | |
| static void | |
| write_u32 (unsigned char *p, unsigned value) | |
| { | |
| p[0] = value >> 24; | |
| p[1] = value >> 16; | |
| p[2] = value >> 8; | |
| p[3] = value; | |
| } | |
| static void | |
| check_compressed_input_budget (void) | |
| { | |
| /* One RGBA pixel preceded by 128 empty, nonfinal stored DEFLATE blocks. | |
| * Decoding must charge for those blocks as well as the five output bytes. */ | |
| unsigned char png[73 + 128 * 5] = {0}; | |
| unsigned idat_len = sizeof (png) - 57; | |
| memcpy (png, rgba_png, 33); | |
| write_u32 (png + 16, 1); | |
| write_u32 (png + 20, 1); | |
| write_u32 (png + 29, crc32 (0, png + 12, 17)); | |
| write_u32 (png + 33, idat_len); | |
| memcpy (png + 37, "IDAT", 4); | |
| unsigned char *p = png + 41; | |
| *p++ = 0x78; *p++ = 0x01; /* zlib header */ | |
| for (unsigned i = 0; i < 128; i++) | |
| { | |
| *p++ = 0; *p++ = 0; *p++ = 0; *p++ = 255; *p++ = 255; | |
| } | |
| *p++ = 1; *p++ = 5; *p++ = 0; *p++ = 250; *p++ = 255; | |
| const unsigned char pixel[] = {0, 20, 100, 200, 128}; | |
| memcpy (p, pixel, sizeof (pixel)); p += sizeof (pixel); | |
| write_u32 (p, adler32 (1, pixel, sizeof (pixel))); p += 4; | |
| write_u32 (p, crc32 (0, png + 37, idat_len + 4)); p += 4; | |
| memcpy (p, rgba_png + sizeof (rgba_png) - 12, 12); | |
| check_alpha_png (png, sizeof (png), pixel + 1, 3, pixel + 4, 1, "/DeviceRGB"); | |
| check_rejected_png (png, sizeof (png), 10); | |
| } | |
| int | |
| main (void) | |
| { | |
| check_compressed_input_budget (); | |
| check_alpha_png (rgba_png, sizeof (rgba_png), rgba_color, sizeof (rgba_color), | |
| rgba_alpha, sizeof (rgba_alpha), "/DeviceRGB"); | |
| check_alpha_png (rgba_palette_png, sizeof (rgba_palette_png), rgba_color, sizeof (rgba_color), | |
| rgba_alpha, sizeof (rgba_alpha), "/DeviceRGB"); | |
| check_alpha_png (gray_alpha_png, sizeof (gray_alpha_png), gray_alpha_color, sizeof (gray_alpha_color), | |
| gray_alpha_alpha, sizeof (gray_alpha_alpha), "/DeviceGray"); | |
| check_alpha_png (indexed_png, sizeof (indexed_png), indexed_color, sizeof (indexed_color), | |
| indexed_alpha, sizeof (indexed_alpha), "/Indexed /DeviceRGB"); | |
| check_alpha_png (indexed_2bit_png, sizeof (indexed_2bit_png), | |
| indexed_2bit_color, sizeof (indexed_2bit_color), | |
| indexed_alpha, sizeof (indexed_alpha), "/Indexed /DeviceRGB"); | |
| check_key_png (gray_1bit_key_png, sizeof (gray_1bit_key_png), | |
| gray_1bit_key_color, sizeof (gray_1bit_key_color), | |
| "/ColorSpace /DeviceGray\n", "/Mask [1 1]\n"); | |
| check_key_png (gray_key_png, sizeof (gray_key_png), | |
| gray_key_color, sizeof (gray_key_color), | |
| "/ColorSpace /DeviceGray\n", "/Mask [50 50]\n"); | |
| check_key_png (rgb_key_png, sizeof (rgb_key_png), | |
| rgb_key_color, sizeof (rgb_key_color), | |
| "/ColorSpace /DeviceRGB\n", "/Mask [10 10 30 30 200 200]\n"); | |
| unsigned char uniform_color[20 * 20 * 3], uniform_alpha[20 * 20]; | |
| for (unsigned i = 0; i < 20 * 20; i++) | |
| { | |
| uniform_color[i * 3] = 20; | |
| uniform_color[i * 3 + 1] = 100; | |
| uniform_color[i * 3 + 2] = 200; | |
| uniform_alpha[i] = 128; | |
| } | |
| check_alpha_png (uniform_png, sizeof (uniform_png), uniform_color, sizeof (uniform_color), | |
| uniform_alpha, sizeof (uniform_alpha), "/DeviceRGB"); | |
| check_rejected_png (invalid_filter_png, sizeof (invalid_filter_png), HB_BUDGET_DEFAULT); | |
| check_rejected_png (interlaced_png, sizeof (interlaced_png), HB_BUDGET_DEFAULT); | |
| check_rejected_png (rgba_png, sizeof (rgba_png), 1); | |
| unsigned char oversized[sizeof (rgba_png)]; | |
| memcpy (oversized, rgba_png, sizeof (oversized)); | |
| memset (oversized + 16, 0xFF, 8); /* IHDR dimensions cannot wrap allocation sizes. */ | |
| check_rejected_png (oversized, sizeof (oversized), HB_BUDGET_DEFAULT); | |
| return 0; | |
| } |