Fix OTS warning about `maxp.maxSizeOfInstructions`.
[ttfautohint.git] / lib / tafile.c
blobdbe521d552240367a74efb5c8d6a360d2895d218
1 /* tafile.c */
3 /*
4 * Copyright (C) 2011-2022 by Werner Lemberg.
6 * This file is part of the ttfautohint library, and may only be used,
7 * modified, and distributed under the terms given in `COPYING'. By
8 * continuing to use, modify, or distribute this file you indicate that you
9 * have read `COPYING' and understand and accept it fully.
11 * The file `COPYING' mentioned in the previous paragraph is distributed
12 * with the ttfautohint library.
16 #include "ta.h"
19 #define BUF_SIZE 0x10000
21 FT_Error
22 TA_font_file_read(FILE* file,
23 FT_Byte** buffer,
24 size_t* length)
26 FT_Byte buf[BUF_SIZE];
27 size_t len = 0;
28 size_t read_bytes;
31 *buffer = (FT_Byte*)malloc(BUF_SIZE);
32 if (!*buffer)
33 return FT_Err_Out_Of_Memory;
35 while ((read_bytes = fread(buf, 1, BUF_SIZE, file)) > 0)
37 FT_Byte* buf_new;
40 buf_new = (FT_Byte*)realloc(*buffer, len + read_bytes);
41 if (!buf_new)
42 return FT_Err_Out_Of_Memory;
43 else
44 *buffer = buf_new;
46 memcpy(*buffer + len, buf, read_bytes);
48 len += read_bytes;
51 if (ferror(file))
52 return FT_Err_Invalid_Stream_Read;
54 /* a valid TTF can never be that small */
55 if (len < 100)
56 return TA_Err_Invalid_Font_Type;
58 *length = len;
60 return TA_Err_Ok;
64 FT_Error
65 TA_font_file_write(FONT* font,
66 FILE* out_file)
68 if (fwrite(font->out_buf, 1, font->out_len, out_file) != font->out_len)
69 return TA_Err_Invalid_Stream_Write;
71 return TA_Err_Ok;
75 FT_Error
76 TA_control_file_read(FONT* font,
77 FILE* control_file)
79 char* buf[BUF_SIZE];
80 size_t control_len = 0;
81 size_t read_bytes;
84 font->control_buf = (char*)malloc(BUF_SIZE);
85 if (!font->control_buf)
86 return FT_Err_Out_Of_Memory;
88 while ((read_bytes = fread(buf, 1, BUF_SIZE, control_file)) > 0)
90 char* control_buf_new;
93 /* we store the data as a C string, allocating one more byte */
94 control_buf_new = (char*)realloc(font->control_buf,
95 control_len + read_bytes + 1);
96 if (!control_buf_new)
97 return FT_Err_Out_Of_Memory;
98 else
99 font->control_buf = control_buf_new;
101 memcpy(font->control_buf + control_len, buf, read_bytes);
103 control_len += read_bytes;
106 if (ferror(control_file))
107 return FT_Err_Invalid_Stream_Read;
109 font->control_len = control_len;
110 font->control_buf[control_len] = '\0';
112 return TA_Err_Ok;
115 /* end of tafile.c */