/src/woff2/src/woff2_common.cc
Line | Count | Source |
1 | | /* Copyright 2013 Google Inc. All Rights Reserved. |
2 | | |
3 | | Distributed under MIT license. |
4 | | See file LICENSE for detail or copy at https://opensource.org/licenses/MIT |
5 | | */ |
6 | | |
7 | | /* Helpers common across multiple parts of woff2 */ |
8 | | |
9 | | #include <algorithm> |
10 | | |
11 | | #include "./woff2_common.h" |
12 | | |
13 | | #include "./port.h" |
14 | | |
15 | | namespace woff2 { |
16 | | |
17 | | |
18 | 123k | uint32_t ComputeULongSum(const uint8_t* buf, size_t size) { |
19 | 123k | uint32_t checksum = 0; |
20 | 123k | size_t aligned_size = size & ~3; |
21 | 7.73M | for (size_t i = 0; i < aligned_size; i += 4) { |
22 | 7.61M | #if defined(WOFF_LITTLE_ENDIAN) |
23 | 7.61M | uint32_t v = *reinterpret_cast<const uint32_t*>(buf + i); |
24 | 7.61M | checksum += (((v & 0xFF) << 24) | ((v & 0xFF00) << 8) | |
25 | 7.61M | ((v & 0xFF0000) >> 8) | ((v & 0xFF000000) >> 24)); |
26 | | #elif defined(WOFF_BIG_ENDIAN) |
27 | | checksum += *reinterpret_cast<const uint32_t*>(buf + i); |
28 | | #else |
29 | | checksum += (buf[i] << 24) | (buf[i + 1] << 16) | |
30 | | (buf[i + 2] << 8) | buf[i + 3]; |
31 | | #endif |
32 | 7.61M | } |
33 | | |
34 | | // treat size not aligned on 4 as if it were padded to 4 with 0's |
35 | 123k | if (size != aligned_size) { |
36 | 62.9k | uint32_t v = 0; |
37 | 189k | for (size_t i = aligned_size; i < size; ++i) { |
38 | 126k | v |= buf[i] << (24 - 8 * (i & 3)); |
39 | 126k | } |
40 | 62.9k | checksum += v; |
41 | 62.9k | } |
42 | | |
43 | 123k | return checksum; |
44 | 123k | } |
45 | | |
46 | 671 | size_t CollectionHeaderSize(uint32_t header_version, uint32_t num_fonts) { |
47 | 671 | size_t size = 0; |
48 | 671 | if (header_version == 0x00020000) { |
49 | 495 | size += 12; // ulDsig{Tag,Length,Offset} |
50 | 495 | } |
51 | 671 | if (header_version == 0x00010000 || header_version == 0x00020000) { |
52 | 671 | size += 12 // TTCTag, Version, numFonts |
53 | 671 | + 4 * num_fonts; // OffsetTable[numFonts] |
54 | 671 | } |
55 | 671 | return size; |
56 | 671 | } |
57 | | |
58 | | } // namespace woff2 |