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