/src/brunsli/c/tests/test_utils.cc
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright (c) Google LLC 2019 |
2 | | // |
3 | | // Use of this source code is governed by an MIT-style |
4 | | // license that can be found in the LICENSE file or at |
5 | | // https://opensource.org/licenses/MIT. |
6 | | |
7 | | #include <cstddef> |
8 | | #include <cstdint> |
9 | | #include <cstdlib> |
10 | | #include <fstream> |
11 | | #include <ios> |
12 | | #include <iterator> |
13 | | #include <memory> |
14 | | #include <string> |
15 | | #include <tuple> |
16 | | #include <vector> |
17 | | |
18 | | #include "./test_utils.h" |
19 | | |
20 | | #if !defined(TEST_DATA_PATH) |
21 | | #include "tools/cpp/runfiles/runfiles.h" |
22 | | #endif |
23 | | |
24 | | namespace brunsli { |
25 | | |
26 | 0 | size_t StringOutputFunction(void* data, const uint8_t* buf, size_t count) { |
27 | 0 | std::string* output = reinterpret_cast<std::string*>(data); |
28 | 0 | output->append(reinterpret_cast<const char*>(buf), count); |
29 | 0 | return count; |
30 | 0 | } |
31 | | |
32 | | static const uint8_t kSmallBrunsliFile[] = { |
33 | | /* Signature */ |
34 | | 0x0a, 0x04, |
35 | | 0x42, 0xd2, 0xd5, 0x4e, |
36 | | |
37 | | /* Header */ |
38 | | 0x12, 0x08, |
39 | | 0x08, 0x10, 0x10, 0x10, 0x18, 0x02, 0x20, 0x00, |
40 | | |
41 | | /* Internal */ |
42 | | 0x22, 0xa8, 0x00, |
43 | | 0xe0, 0xb6, 0x01, 0x04, 0x41, 0x10, 0x5a, 0x06, |
44 | | 0xc5, 0x3a, 0xac, 0x18, 0xdd, 0x4c, 0x55, 0x67, |
45 | | 0xb5, 0x4f, 0xe9, 0x91, 0xe4, 0xbe, 0x6f, 0x19, |
46 | | 0x69, 0x37, 0x53, 0x35, 0xe3, 0x3a, 0x6e, 0x06, // 020 |
47 | | 0xf8, 0x01, 0x04, 0x2a, 0x8b, 0x08, 0x01, 0x00, |
48 | | |
49 | | /* Metadata */ |
50 | | 0x1a, 0x06, |
51 | | 0x01, |
52 | | 0x03, 0x00, 0x80, 0x3a, 0x03, |
53 | | |
54 | | /* Quant */ |
55 | | 0x2a, 0x82, 0x00, |
56 | | 0x29, 0x53, |
57 | | |
58 | | /* Histo */ |
59 | | 0x32, 0xb8, 0x80, 0x00, |
60 | | 0x00, 0x0e, 0x0b, 0xf4, 0x56, 0xcd, 0x01, 0x90, |
61 | | 0x00, 0x09, 0xca, 0x8f, 0xd6, 0x18, 0x12, 0x9b, |
62 | | 0x76, 0x27, 0xfd, 0x70, 0x2c, 0xd7, 0xb3, 0x6d, |
63 | | 0xdc, 0xf7, 0x1e, 0x8f, 0x80, 0xa1, 0x17, 0x92, // 020 |
64 | | 0xfe, 0x14, 0xa0, 0x7e, 0x79, 0x2f, 0xef, 0xf5, |
65 | | 0xbd, 0x62, 0x32, 0x9c, 0xa0, 0xef, 0x33, 0xf0, |
66 | | 0x1e, 0xcf, 0x02, 0xdb, 0x56, 0x5f, 0x0d, 0x2a, |
67 | | |
68 | | /* Unknown */ |
69 | | 0x7a, 0x15, |
70 | | 'k', 'o', 't', ' ', 'l', 'o', 'm', |
71 | | 'o', 'm', ' ', 'k', 'o', 'l', 'o', |
72 | | 'l', ' ', 's', 'l', 'o', 'n', 'a', |
73 | | |
74 | | /* DC */ |
75 | | 0x3a, 0x8e, 0x80, 0x00, |
76 | | 0x13, 0x00, 0x00, 0x00, 0x3a, 0x40, 0x97, 0xa7, |
77 | | 0xcf, 0xf5, 0x00, 0x00, 0xff, 0xff, |
78 | | |
79 | | /* AC */ |
80 | | 0x42, 0xc2, 0x82, 0x00, |
81 | | 0xf3, 0x4c, 0xc4, 0xdc, 0x00, 0xd0, 0x5d, 0xfe, |
82 | | 0xf9, 0x3e, 0xc1, 0x15, 0xec, 0x84, 0xad, 0xdf, |
83 | | 0x0e, 0x04, 0x59, 0xdb, 0x81, 0x2a, 0x43, 0x3d, |
84 | | 0x95, 0xc1, 0x2a, 0x94, 0x57, 0x8d, 0x67, 0xea, // 020 |
85 | | 0x85, 0x0f, 0x73, 0xb8, 0x9d, 0x0a, 0xa8, 0x26, |
86 | | 0xad, 0x7a, 0xa3, 0x55, 0xa4, 0x53, 0x29, 0xa9, |
87 | | 0xf0, 0x7b, 0x40, 0xe8, 0x32, 0x77, 0x05, 0x48, |
88 | | 0xf2, 0x51, 0x4f, 0x05, 0x0a, 0xa5, 0xb9, 0xc5, // 040 |
89 | | 0x2b, 0x5e, 0x0c, 0xce, 0xe2, 0x0a, 0x6b, 0x6d, |
90 | | 0x37, 0x93, 0x33, 0x75, 0x43, 0x98, 0x5d, 0xe6, |
91 | | 0x54, 0x93, 0x2f, 0x5f, 0x11, 0x97, 0x94, 0x54, |
92 | | 0xed, 0xff, 0x9b, 0x5d, 0x20, 0x28, 0x47, 0xe4, // 060 |
93 | | 0x77, 0x9d, 0x14, 0x81, 0x66, 0x04, 0xb6, 0x72, |
94 | | 0x05, 0xe0, 0x54, 0x10, 0x4c, 0x2f, 0x51, 0x72, |
95 | | 0x9e, 0x4b, 0xa3, 0x3c, 0x27, 0x1f, 0x14, 0x20, |
96 | | 0x5e, 0xb7, 0x87, 0x34, 0xa7, 0xc8, 0x90, 0xc3, // 080 |
97 | | 0x2f, 0x80, 0x41, 0x0f, 0xd0, 0xa9, 0x8e, 0x72, |
98 | | 0xf7, 0x09, 0x85, 0x67, 0xd2, 0xa0, 0xad, 0x3a, |
99 | | 0xf4, 0x28, 0x47, 0x31, 0x36, 0xe4, 0xbe, 0xa3, |
100 | | 0x73, 0x85, 0x55, 0xc8, 0x16, 0x98, 0x0e, 0xb3, // 0A0 |
101 | | 0xd0, 0xcd, 0x00, 0x54, 0x93, 0x8f, 0x35, 0xf3, |
102 | | 0xe9, 0x6b, 0xac, 0xde, 0xd6, 0x1f, 0x64, 0x61, |
103 | | 0x18, 0xf7, 0x6d, 0x9c, 0x2b, 0xc8, 0x7e, 0xc0, |
104 | | 0xa3, 0xa1, 0x87, 0xa2, 0x02, 0x06, 0x71, 0xf5, // 0C0 |
105 | | 0x9b, 0xd4, 0xec, 0xdd, 0x6b, 0xef, 0x99, 0x3a, |
106 | | 0x9c, 0xa2, 0xaa, 0x49, 0xc6, 0x46, 0x66, 0xa5, |
107 | | 0xb8, 0x1f, 0x4a, 0x2a, 0xf5, 0xdb, 0x10, 0xfa, |
108 | | 0x17, 0x29, 0x27, 0x50, 0xe5, 0xf8, 0x34, 0x4d, // 0E0 |
109 | | 0xc2, 0x95, 0x9c, 0x15, 0xa9, 0x71, 0xea, 0xda, |
110 | | 0x6a, 0xf4, 0x31, 0x93, 0x4c, 0x1d, 0x52, 0x98, |
111 | | 0x09, 0xc7, 0xd5, 0x24, 0x11, 0x63, 0x0a, 0x2f, |
112 | | 0x25, 0x15, 0xfb, 0x2f, 0x08, 0x4a, 0x56, 0xed, // 100 |
113 | | 0x27, 0x93, 0x08, 0x5c, 0x3a, 0x6d, 0x79, 0x78, |
114 | | 0x94, 0xdc, 0xe3, 0xbf, 0x4a, 0xc0, 0x06, 0xcd, |
115 | | 0x46, 0xb4, 0x12, 0x17, 0x20, 0x58, 0x21, 0x1a, |
116 | | 0xa7, 0x52, 0xb9, 0x66, 0x08, 0x80, 0x9d, 0xcf, // 120 |
117 | | 0x5d, 0x9b, 0xcd, 0x05, 0xc9, 0x6f, 0x51, 0x8c, |
118 | | 0x62, 0xe9, 0xef, 0x28, 0xdb, 0xec, 0x4b, 0x61, |
119 | | 0x04, 0x15, 0x9f, 0xf5, 0xfc, 0x52, 0x9b, 0xdb, |
120 | | 0xaf, 0xde, 0x0a, 0x94, 0xc0, 0x7e, 0x84, 0x52, // 140 |
121 | | 0xc6, 0x00 |
122 | | }; |
123 | | |
124 | | static const uint8_t kFallbackBrunsliFile[] = { |
125 | | /* Signature */ |
126 | | 0x0a, 0x04, |
127 | | 0x42, 0xd2, 0xd5, 0x4e, |
128 | | |
129 | | /* Header */ |
130 | | 0x12, 0x02, |
131 | | 0x18, 0x04, |
132 | | |
133 | | /* Internal */ |
134 | | 0x4a, 0x04, |
135 | | 0xde, 0xad, 0xbe, 0xef |
136 | | }; |
137 | | |
138 | 0 | std::vector<uint8_t> GetSmallBrunsliFile() { |
139 | 0 | return std::vector<uint8_t>(kSmallBrunsliFile, |
140 | 0 | kSmallBrunsliFile + sizeof(kSmallBrunsliFile)); |
141 | 0 | } |
142 | | |
143 | 0 | std::vector<uint8_t> GetFallbackBrunsliFile() { |
144 | 0 | return std::vector<uint8_t>( |
145 | 0 | kFallbackBrunsliFile, |
146 | 0 | kFallbackBrunsliFile + sizeof(kFallbackBrunsliFile)); |
147 | 0 | } |
148 | | |
149 | | namespace { |
150 | 0 | uint32_t readU32(const uint8_t* data) { |
151 | 0 | return data[3] | (data[2] << 8) | (data[1] << 16) | (data[0] << 24); |
152 | 0 | } |
153 | | |
154 | | #if defined(TEST_DATA_PATH) |
155 | 0 | std::string GetTestDataPath(const std::string& filename) { |
156 | 0 | return std::string(TEST_DATA_PATH "/") + filename; |
157 | 0 | } |
158 | | #else |
159 | | using bazel::tools::cpp::runfiles::Runfiles; |
160 | | const std::unique_ptr<Runfiles> kRunfiles(Runfiles::Create("")); |
161 | | std::string GetTestDataPath(const std::string& filename) { |
162 | | std::string root(BRUNSLI_ROOT_PACKAGE "/tests/testdata/"); |
163 | | return kRunfiles->Rlocation(root + filename); |
164 | | } |
165 | | #endif |
166 | | } // namespace |
167 | | |
168 | | std::vector<std::tuple<std::vector<uint8_t>>> ParseMar(const void* data, |
169 | 0 | size_t size) { |
170 | 0 | std::vector<std::tuple<std::vector<uint8_t>>> result; |
171 | 0 | const uint8_t* bytes = reinterpret_cast<const uint8_t*>(data); |
172 | 0 | if (size < 8) std::abort(); |
173 | 0 | uint32_t sig = readU32(bytes); |
174 | 0 | if (sig != 0x4D415231) std::abort(); |
175 | 0 | uint32_t index = readU32(bytes + 4); |
176 | 0 | if ((index + 4 < index) || (index + 4 > size)) std::abort(); |
177 | 0 | uint32_t index_size = readU32(bytes + index); |
178 | 0 | index += 4; |
179 | 0 | if (index + index_size < index) std::abort(); |
180 | 0 | uint32_t index_end = index + index_size; |
181 | 0 | if (index_end > size) std::abort(); |
182 | 0 | while (index < index_end) { |
183 | 0 | if ((index + 13 < index) || (index + 13 > index_end)) std::abort(); |
184 | 0 | uint32_t offset = readU32(bytes + index); |
185 | 0 | uint32_t len = readU32(bytes + index + 4); |
186 | 0 | if (offset + len < offset || offset + len > size) std::abort(); |
187 | 0 | index += 12; |
188 | 0 | while (bytes[index++]) { |
189 | 0 | if (index == index_end) std::abort(); |
190 | 0 | } |
191 | 0 | const uint8_t* start = bytes + offset; |
192 | 0 | result.emplace_back(std::vector<uint8_t>(start, start + len)); |
193 | 0 | } |
194 | 0 | return result; |
195 | 0 | } |
196 | | |
197 | 0 | std::vector<uint8_t> ReadTestData(const std::string& filename) { |
198 | 0 | std::string full_path = GetTestDataPath(filename); |
199 | 0 | std::ifstream file(full_path, std::ios::binary); |
200 | 0 | std::vector<char> str((std::istreambuf_iterator<char>(file)), |
201 | 0 | std::istreambuf_iterator<char>()); |
202 | 0 | if (!file.good()) std::abort(); |
203 | 0 | const uint8_t* raw = reinterpret_cast<const uint8_t*>(str.data()); |
204 | 0 | std::vector<uint8_t> data(raw, raw + str.size()); |
205 | 0 | return data; |
206 | 0 | } |
207 | | |
208 | | } // namespace brunsli |