/work/workdir/UnpackedTarball/libeot/src/writeFontFile.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* Copyright (c) 2013 Brennan T. Vincent <brennanv@email.arizona.edu> |
2 | | * This file is a part of libeot, which is licensed under the MPL license, version 2.0. |
3 | | * For full details, see the file LICENSE |
4 | | */ |
5 | | |
6 | | #include <stdio.h> |
7 | | #include <stdbool.h> |
8 | | #include <stdint.h> |
9 | | #include <stdlib.h> |
10 | | |
11 | | #include <libeot/libeot.h> |
12 | | |
13 | | #include "util/stream.h" |
14 | | #include "lzcomp/liblzcomp.h" |
15 | | #include "ctf/parseCTF.h" |
16 | | const uint8_t ENCRYPTION_KEY = 0x50; |
17 | | |
18 | | enum EOTError writeFontBuffer(const uint8_t *font, unsigned fontSize, bool compressed, bool encrypted, uint8_t **finalOutBuffer, unsigned *finalFontSize) |
19 | 0 | { |
20 | 0 | enum EOTError result; |
21 | 0 | uint8_t *buf = (uint8_t *)malloc(fontSize); |
22 | 0 | for (unsigned i = 0; i < fontSize; ++i) |
23 | 0 | { |
24 | 0 | if (encrypted) |
25 | 0 | { |
26 | 0 | buf[i] = font[i] ^ ENCRYPTION_KEY; |
27 | 0 | } |
28 | 0 | else |
29 | 0 | { |
30 | 0 | buf[i] = font[i]; |
31 | 0 | } |
32 | 0 | } |
33 | 0 | uint8_t *ctfs[3] = {NULL, NULL, NULL}; |
34 | 0 | struct SFNTContainer *ctr = NULL; |
35 | 0 | if (compressed) |
36 | 0 | { |
37 | 0 | #ifndef DONT_UNCOMPRESS |
38 | 0 | unsigned sizes[3]; |
39 | 0 | struct Stream sBuf = constructStream(buf, fontSize); |
40 | 0 | result = unpackMtx(&sBuf, fontSize, ctfs, sizes); |
41 | 0 | if (result != EOT_SUCCESS) |
42 | 0 | { |
43 | 0 | goto CLEANUP; |
44 | 0 | } |
45 | 0 | struct Stream streams[3]; |
46 | 0 | for (unsigned i = 0; i < 3; ++i) |
47 | 0 | { |
48 | 0 | streams[i] = constructStream(ctfs[i], sizes[i]); |
49 | 0 | } |
50 | 0 | struct Stream *streamPtrs[3] = {streams, streams + 1, streams + 2}; /* ugh */ |
51 | 0 | result = parseCTF(streamPtrs, &ctr); |
52 | 0 | if (result != EOT_SUCCESS) |
53 | 0 | { |
54 | 0 | goto CLEANUP; |
55 | 0 | } |
56 | 0 | result = dumpContainer(ctr, finalOutBuffer, finalFontSize); |
57 | 0 | if (result != EOT_SUCCESS) |
58 | 0 | { |
59 | 0 | goto CLEANUP; |
60 | 0 | } |
61 | | #else |
62 | | *finalOutBuffer = buf; |
63 | | *finalFontSize = fontSize; |
64 | | #endif |
65 | 0 | } |
66 | 0 | else |
67 | 0 | { |
68 | 0 | *finalOutBuffer = buf; |
69 | 0 | *finalFontSize = fontSize; |
70 | 0 | } |
71 | 0 | result = EOT_SUCCESS; |
72 | 0 | CLEANUP: |
73 | 0 | if (*finalOutBuffer != buf) |
74 | 0 | { |
75 | 0 | free(buf); |
76 | 0 | } |
77 | 0 | for (unsigned i = 0; i < 3; ++i) |
78 | 0 | { |
79 | 0 | free(ctfs[i]); |
80 | 0 | } |
81 | 0 | if (ctr) |
82 | 0 | { |
83 | 0 | freeContainer(ctr); |
84 | 0 | } |
85 | 0 | return result; |
86 | 0 | } |
87 | | |
88 | | enum EOTError writeFontFile(const uint8_t *font, unsigned fontSize, bool compressed, bool encrypted, FILE *outFile) |
89 | 0 | { |
90 | 0 | enum EOTError result; |
91 | 0 | uint8_t *finalBuf; |
92 | 0 | unsigned finalFontSize; |
93 | 0 | result = writeFontBuffer(font, fontSize, compressed, encrypted, &finalBuf, &finalFontSize); |
94 | 0 | int itemsWritten = fwrite(finalBuf, 1, (long)finalFontSize, outFile); |
95 | 0 | if (itemsWritten == finalFontSize) |
96 | 0 | { |
97 | 0 | result = EOT_SUCCESS; |
98 | 0 | } |
99 | 0 | else |
100 | 0 | { |
101 | 0 | result = EOT_FWRITE_ERROR; |
102 | 0 | } |
103 | 0 | free(finalBuf); |
104 | 0 | return result; |
105 | 0 | } |
106 | | |
107 | | /* vim:set shiftwidth=2 softtabstop=2 expandtab: */ |