OLD | NEW |
1 // Copyright 2013 Google Inc. All Rights Reserved. | 1 // Copyright 2013 Google Inc. All Rights Reserved. |
2 // | 2 // |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
4 // you may not use this file except in compliance with the License. | 4 // you may not use this file except in compliance with the License. |
5 // You may obtain a copy of the License at | 5 // You may obtain a copy of the License at |
6 // | 6 // |
7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
8 // | 8 // |
9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 // See the License for the specific language governing permissions and | 12 // See the License for the specific language governing permissions and |
13 // limitations under the License. | 13 // limitations under the License. |
14 // | 14 // |
15 // Helpers common across multiple parts of woff2 | 15 // Helpers common across multiple parts of woff2 |
16 | 16 |
17 #include <algorithm> | 17 #include <algorithm> |
18 | 18 |
19 #include "./woff2_common.h" | 19 #include "./woff2_common.h" |
20 | 20 |
21 namespace woff2 { | 21 namespace woff2 { |
22 | 22 |
23 | 23 |
24 uint32_t ComputeULongSum(const uint8_t* buf, size_t size) { | 24 uint32_t ComputeULongSum(const uint8_t* buf, size_t size) { |
25 uint32_t checksum = 0; | 25 uint32_t checksum = 0; |
26 for (size_t i = 0; i < size; i += 4) { | 26 size_t aligned_size = size & ~3; |
| 27 for (size_t i = 0; i < aligned_size; i += 4) { |
27 #if (defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)) | 28 #if (defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)) |
28 uint32_t v = *reinterpret_cast<const uint32_t*>(buf + i); | 29 uint32_t v = *reinterpret_cast<const uint32_t*>(buf + i); |
29 checksum += (((v & 0xFF) << 24) | ((v & 0xFF00) << 8) | | 30 checksum += (((v & 0xFF) << 24) | ((v & 0xFF00) << 8) | |
30 ((v & 0xFF0000) >> 8) | ((v & 0xFF000000) >> 24)); | 31 ((v & 0xFF0000) >> 8) | ((v & 0xFF000000) >> 24)); |
31 #elif (defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)) | 32 #elif (defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)) |
32 checksum += *reinterpret_cast<const uint32_t*>(buf + i); | 33 checksum += *reinterpret_cast<const uint32_t*>(buf + i); |
33 #else | 34 #else |
34 checksum += (buf[i] << 24) | (buf[i + 1] << 16) | | 35 checksum += (buf[i] << 24) | (buf[i + 1] << 16) | |
35 (buf[i + 2] << 8) | buf[i + 3]; | 36 (buf[i + 2] << 8) | buf[i + 3]; |
36 #endif | 37 #endif |
37 } | 38 } |
| 39 |
| 40 // treat size not aligned on 4 as if it were padded to 4 with 0's |
| 41 if (size != aligned_size) { |
| 42 uint32_t v = 0; |
| 43 for (size_t i = aligned_size; i < size; ++i) { |
| 44 v |= buf[i] << (24 - 8 * (i & 3)); |
| 45 } |
| 46 checksum += v; |
| 47 } |
| 48 |
38 return checksum; | 49 return checksum; |
39 } | 50 } |
40 | 51 |
41 size_t CollectionHeaderSize(uint32_t header_version, uint32_t num_fonts) { | 52 size_t CollectionHeaderSize(uint32_t header_version, uint32_t num_fonts) { |
42 size_t size = 0; | 53 size_t size = 0; |
43 if (header_version == 0x00020000) { | 54 if (header_version == 0x00020000) { |
44 size += 12; // ulDsig{Tag,Length,Offset} | 55 size += 12; // ulDsig{Tag,Length,Offset} |
45 } | 56 } |
46 if (header_version == 0x00010000 || header_version == 0x00020000) { | 57 if (header_version == 0x00010000 || header_version == 0x00020000) { |
47 size += 12 // TTCTag, Version, numFonts | 58 size += 12 // TTCTag, Version, numFonts |
48 + 4 * num_fonts; // OffsetTable[numFonts] | 59 + 4 * num_fonts; // OffsetTable[numFonts] |
49 } | 60 } |
50 return size; | 61 return size; |
51 } | 62 } |
52 | 63 |
53 } // namespace woff2 | 64 } // namespace woff2 |
OLD | NEW |