| 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 |
uint32_t ComputeULongSum(const uint8_t* buf, size_t size) { |
| 19 |
uint32_t checksum = 0; |
| 20 |
size_t aligned_size = size & ~3; |
| 21 |
for (size_t i = 0; i < aligned_size; i += 4) { |
| 22 |
checksum += |
| 23 |
(buf[i] << 24) | (buf[i + 1] << 16) | (buf[i + 2] << 8) | buf[i + 3]; |
| 24 |
} |
| 25 |
|
| 26 |
// treat size not aligned on 4 as if it were padded to 4 with 0's |
| 27 |
if (size != aligned_size) { |
| 28 |
uint32_t v = 0; |
| 29 |
for (size_t i = aligned_size; i < size; ++i) { |
| 30 |
v |= buf[i] << (24 - 8 * (i & 3)); |
| 31 |
} |
| 32 |
checksum += v; |
| 33 |
} |
| 34 |
|
| 35 |
return checksum; |
| 36 |
} |
| 37 |
|
| 38 |
size_t CollectionHeaderSize(uint32_t header_version, uint32_t num_fonts) { |
| 39 |
size_t size = 0; |
| 40 |
if (header_version == 0x00020000) { |
| 41 |
size += 12; // ulDsig{Tag,Length,Offset} |
| 42 |
} |
| 43 |
if (header_version == 0x00010000 || header_version == 0x00020000) { |
| 44 |
size += 12 // TTCTag, Version, numFonts |
| 45 |
+ 4 * num_fonts; // OffsetTable[numFonts] |
| 46 |
} |
| 47 |
return size; |
| 48 |
} |
| 49 |
|
| 50 |
} // namespace woff2 |