| 1 |
#include "cff-index.h" |
| 2 |
// INDEX util functions |
| 3 |
|
| 4 |
static INLINE void disposeCffIndex(cff_Index *in) { |
| 5 |
if (in->offset) FREE(in->offset); |
| 6 |
if (in->data) FREE(in->data); |
| 7 |
} |
| 8 |
|
| 9 |
caryll_standardRefTypeFn(cff_Index, disposeCffIndex); |
| 10 |
|
| 11 |
static uint32_t getIndexLength(const cff_Index *i) { |
| 12 |
if (i->count != 0) |
| 13 |
return 3 + (i->offset[i->count] - 1) + ((i->count + 1) * i->offSize); |
| 14 |
else |
| 15 |
return 3; |
| 16 |
} |
| 17 |
static void emptyIndex(cff_Index *i) { |
| 18 |
cff_iIndex.dispose(i); |
| 19 |
memset(i, 0, sizeof(*i)); |
| 20 |
} |
| 21 |
|
| 22 |
static void extractIndex(uint8_t *data, uint32_t pos, cff_Index *in) { |
| 23 |
in->count = gu2(data, pos); |
| 24 |
in->offSize = gu1(data, pos + 2); |
| 25 |
|
| 26 |
if (in->count > 0) { |
| 27 |
NEW(in->offset, in->count + 1); |
| 28 |
|
| 29 |
for (arity_t i = 0; i <= in->count; i++) { |
| 30 |
switch (in->offSize) { |
| 31 |
case 1: |
| 32 |
in->offset[i] = gu1(data, pos + 3 + (i * in->offSize)); |
| 33 |
break; |
| 34 |
case 2: |
| 35 |
in->offset[i] = gu2(data, pos + 3 + (i * in->offSize)); |
| 36 |
break; |
| 37 |
case 3: |
| 38 |
in->offset[i] = gu3(data, pos + 3 + (i * in->offSize)); |
| 39 |
break; |
| 40 |
case 4: |
| 41 |
in->offset[i] = gu4(data, pos + 3 + (i * in->offSize)); |
| 42 |
break; |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
NEW(in->data, in->offset[in->count] - 1); |
| 47 |
memcpy(in->data, data + pos + 3 + (in->count + 1) * in->offSize, in->offset[in->count] - 1); |
| 48 |
} else { |
| 49 |
in->offset = NULL; |
| 50 |
in->data = NULL; |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
static cff_Index *newIndexByCallback(void *context, uint32_t length, caryll_Buffer *(*fn)(void *, uint32_t)) { |
| 55 |
cff_Index *idx = cff_iIndex.create(); |
| 56 |
idx->count = length; |
| 57 |
NEW(idx->offset, idx->count + 1); |
| 58 |
idx->offset[0] = 1; |
| 59 |
idx->data = NULL; |
| 60 |
|
| 61 |
size_t used = 0; |
| 62 |
size_t blank = 0; |
| 63 |
for (arity_t i = 0; i < length; i++) { |
| 64 |
caryll_Buffer *blob = fn(context, i); |
| 65 |
if (blank < blob->size) { |
| 66 |
used += blob->size; |
| 67 |
blank = (used >> 1) & 0xFFFFFF; |
| 68 |
RESIZE(idx->data, used + blank); |
| 69 |
} else { |
| 70 |
used += blob->size; |
| 71 |
blank -= blob->size; |
| 72 |
} |
| 73 |
idx->offset[i + 1] = (uint32_t)(blob->size + idx->offset[i]); |
| 74 |
memcpy(idx->data + idx->offset[i] - 1, blob->data, blob->size); |
| 75 |
buffree(blob); |
| 76 |
} |
| 77 |
idx->offSize = 4; |
| 78 |
return idx; |
| 79 |
} |
| 80 |
|
| 81 |
static caryll_Buffer *buildIndex(const cff_Index *index) { |
| 82 |
caryll_Buffer *blob = bufnew(); |
| 83 |
if (!index->count) { |
| 84 |
bufwrite8(blob, 0); |
| 85 |
bufwrite8(blob, 0); |
| 86 |
bufwrite8(blob, 0); |
| 87 |
return blob; |
| 88 |
} |
| 89 |
|
| 90 |
uint32_t lastOffset = index->offset[index->count]; |
| 91 |
uint8_t offSize = 4; |
| 92 |
if (lastOffset < 0x100) { |
| 93 |
offSize = 1; |
| 94 |
} else if (lastOffset < 0x10000) { |
| 95 |
offSize = 2; |
| 96 |
} else if (lastOffset < 0x1000000) { |
| 97 |
offSize = 3; |
| 98 |
} else { |
| 99 |
offSize = 4; |
| 100 |
} |
| 101 |
|
| 102 |
if (index->count != 0) |
| 103 |
blob->size = 3 + (index->offset[index->count] - 1) + ((index->count + 1) * offSize); |
| 104 |
else |
| 105 |
blob->size = 3; |
| 106 |
|
| 107 |
NEW(blob->data, blob->size); |
| 108 |
blob->data[0] = index->count / 256; |
| 109 |
blob->data[1] = index->count % 256; |
| 110 |
blob->data[2] = offSize; |
| 111 |
|
| 112 |
if (index->count > 0) { |
| 113 |
for (arity_t i = 0; i <= index->count; i++) { |
| 114 |
switch (offSize) { |
| 115 |
case 1: |
| 116 |
blob->data[3 + i] = index->offset[i]; |
| 117 |
break; |
| 118 |
case 2: |
| 119 |
blob->data[3 + i * 2] = index->offset[i] / 256; |
| 120 |
blob->data[4 + i * 2] = index->offset[i] % 256; |
| 121 |
break; |
| 122 |
case 3: |
| 123 |
blob->data[3 + i * 3] = index->offset[i] / 65536; |
| 124 |
blob->data[4 + i * 3] = (index->offset[i] % 65536) / 256; |
| 125 |
blob->data[5 + i * 3] = (index->offset[i] % 65536) % 256; |
| 126 |
break; |
| 127 |
case 4: |
| 128 |
blob->data[3 + i * 4] = (index->offset[i] / 65536) / 256; |
| 129 |
blob->data[4 + i * 4] = (index->offset[i] / 65536) % 256; |
| 130 |
blob->data[5 + i * 4] = (index->offset[i] % 65536) / 256; |
| 131 |
blob->data[6 + i * 4] = (index->offset[i] % 65536) % 256; |
| 132 |
break; |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
if (index->data != NULL) |
| 137 |
memcpy(blob->data + 3 + ((index->count + 1) * offSize), index->data, index->offset[index->count] - 1); |
| 138 |
} |
| 139 |
blob->cursor = blob->size; |
| 140 |
return blob; |
| 141 |
} |
| 142 |
|
| 143 |
caryll_ElementInterfaceOf(cff_Index) cff_iIndex = { |
| 144 |
caryll_standardRefTypeMethods(cff_Index), .getLength = getIndexLength, .empty = emptyIndex, .parse = extractIndex, |
| 145 |
.fromCallback = newIndexByCallback, .build = buildIndex, |
| 146 |
}; |