diff options
-rw-r--r-- | firmware/font.c | 106 |
1 files changed, 51 insertions, 55 deletions
diff --git a/firmware/font.c b/firmware/font.c index 6b1f51a24e..1635441b7a 100644 --- a/firmware/font.c +++ b/firmware/font.c | |||
@@ -72,16 +72,24 @@ void font_init(void) | |||
72 | memset(&font_ui, 0, sizeof(struct font)); | 72 | memset(&font_ui, 0, sizeof(struct font)); |
73 | } | 73 | } |
74 | 74 | ||
75 | static int readshort(unsigned short *sp) | 75 | /* Check if we have x bytes left in the file buffer */ |
76 | #define HAVEBYTES(x) (fileptr + (x) <= eofptr) | ||
77 | |||
78 | /* Helper functions to read big-endian unaligned short or long from | ||
79 | the file buffer. Bounds-checking must be done in the calling | ||
80 | function. | ||
81 | */ | ||
82 | |||
83 | static short readshort(void) | ||
76 | { | 84 | { |
77 | unsigned short s; | 85 | unsigned short s; |
78 | 86 | ||
79 | s = *fileptr++ & 0xff; | 87 | s = *fileptr++ & 0xff; |
80 | *sp = (*fileptr++ << 8) | s; | 88 | s |= (*fileptr++ << 8); |
81 | return (fileptr <= eofptr); | 89 | return s; |
82 | } | 90 | } |
83 | 91 | ||
84 | static long readlong(unsigned long *lp) | 92 | static long readlong(void) |
85 | { | 93 | { |
86 | unsigned long l; | 94 | unsigned long l; |
87 | 95 | ||
@@ -89,18 +97,14 @@ static long readlong(unsigned long *lp) | |||
89 | l |= *fileptr++ << 8; | 97 | l |= *fileptr++ << 8; |
90 | l |= ((unsigned long)(*fileptr++)) << 16; | 98 | l |= ((unsigned long)(*fileptr++)) << 16; |
91 | l |= ((unsigned long)(*fileptr++)) << 24; | 99 | l |= ((unsigned long)(*fileptr++)) << 24; |
92 | *lp = l; | 100 | return l; |
93 | return (fileptr <= eofptr); | ||
94 | } | 101 | } |
95 | 102 | ||
96 | /* read count bytes*/ | 103 | /* read count bytes*/ |
97 | static int readstr(char *buf, int count) | 104 | static void readstr(char *buf, int count) |
98 | { | 105 | { |
99 | int n = count; | 106 | while (count--) |
100 | |||
101 | while (--n >= 0) | ||
102 | *buf++ = *fileptr++; | 107 | *buf++ = *fileptr++; |
103 | return (fileptr <= eofptr)? count: 0; | ||
104 | } | 108 | } |
105 | 109 | ||
106 | void font_reset(void) | 110 | void font_reset(void) |
@@ -111,44 +115,30 @@ void font_reset(void) | |||
111 | static struct font* font_load_header(struct font *pf) | 115 | static struct font* font_load_header(struct font *pf) |
112 | { | 116 | { |
113 | char version[4+1]; | 117 | char version[4+1]; |
114 | unsigned short maxwidth, height, ascent, pad; | 118 | |
115 | unsigned long firstchar, defaultchar, size; | 119 | /* Check we have enough data */ |
116 | unsigned long nbits; | 120 | if (!HAVEBYTES(28)) |
121 | return NULL; | ||
117 | 122 | ||
118 | /* read magic and version #*/ | 123 | /* read magic and version #*/ |
119 | memset(version, 0, sizeof(version)); | 124 | memset(version, 0, sizeof(version)); |
120 | if (readstr(version, 4) != 4) | 125 | readstr(version, 4); |
121 | return NULL; | 126 | |
122 | if (strcmp(version, VERSION) != 0) | 127 | if (strcmp(version, VERSION) != 0) |
123 | return NULL; | 128 | return NULL; |
124 | 129 | ||
125 | /* font info*/ | 130 | /* font info*/ |
126 | if (!readshort(&maxwidth)) | 131 | pf->maxwidth = readshort(); |
127 | return NULL; | 132 | pf->height = readshort(); |
128 | pf->maxwidth = maxwidth; | 133 | pf->ascent = readshort(); |
129 | if (!readshort(&height)) | 134 | fileptr += 2; /* Skip padding */ |
130 | return NULL; | 135 | pf->firstchar = readlong(); |
131 | pf->height = height; | 136 | pf->defaultchar = readlong(); |
132 | if (!readshort(&ascent)) | 137 | pf->size = readlong(); |
133 | return NULL; | ||
134 | pf->ascent = ascent; | ||
135 | if (!readshort(&pad)) | ||
136 | return NULL; | ||
137 | if (!readlong(&firstchar)) | ||
138 | return NULL; | ||
139 | pf->firstchar = firstchar; | ||
140 | if (!readlong(&defaultchar)) | ||
141 | return NULL; | ||
142 | pf->defaultchar = defaultchar; | ||
143 | if (!readlong(&size)) | ||
144 | return NULL; | ||
145 | pf->size = size; | ||
146 | 138 | ||
147 | /* get variable font data sizes*/ | 139 | /* get variable font data sizes*/ |
148 | /* # words of bitmap_t*/ | 140 | /* # words of bitmap_t*/ |
149 | if (!readlong(&nbits)) | 141 | pf->bits_size = readlong(); |
150 | return NULL; | ||
151 | pf->bits_size = nbits; | ||
152 | 142 | ||
153 | return pf; | 143 | return pf; |
154 | } | 144 | } |
@@ -157,13 +147,14 @@ struct font* font_load_in_memory(struct font* pf) | |||
157 | { | 147 | { |
158 | long i, noffset, nwidth; | 148 | long i, noffset, nwidth; |
159 | 149 | ||
160 | /* # longs of offset*/ | 150 | if (!HAVEBYTES(4)) |
161 | if (!readlong(&noffset)) | ||
162 | return NULL; | 151 | return NULL; |
163 | 152 | ||
153 | /* # longs of offset*/ | ||
154 | noffset = readlong(); | ||
155 | |||
164 | /* # bytes of width*/ | 156 | /* # bytes of width*/ |
165 | if (!readlong(&nwidth)) | 157 | nwidth = readlong(); |
166 | return NULL; | ||
167 | 158 | ||
168 | /* variable font data*/ | 159 | /* variable font data*/ |
169 | pf->bits = (unsigned char *)fileptr; | 160 | pf->bits = (unsigned char *)fileptr; |
@@ -186,24 +177,28 @@ struct font* font_load_in_memory(struct font* pf) | |||
186 | { | 177 | { |
187 | long_offset = 0; | 178 | long_offset = 0; |
188 | pf->offset = (unsigned short *)fileptr; | 179 | pf->offset = (unsigned short *)fileptr; |
180 | |||
181 | /* Check we have sufficient buffer */ | ||
182 | if (!HAVEBYTES(noffset * sizeof(short))) | ||
183 | return NULL; | ||
184 | |||
189 | for (i=0; i<noffset; ++i) | 185 | for (i=0; i<noffset; ++i) |
190 | { | 186 | { |
191 | unsigned short offset; | 187 | ((unsigned short*)(pf->offset))[i] = (unsigned short)readshort(); |
192 | if (!readshort(&offset)) | ||
193 | return NULL; | ||
194 | ((unsigned short*)(pf->offset))[i] = (unsigned short)offset; | ||
195 | } | 188 | } |
196 | } | 189 | } |
197 | else | 190 | else |
198 | { | 191 | { |
199 | long_offset = 1; | 192 | long_offset = 1; |
200 | pf->offset = (unsigned short *)fileptr; | 193 | pf->offset = (unsigned short *)fileptr; |
194 | |||
195 | /* Check we have sufficient buffer */ | ||
196 | if (!HAVEBYTES(noffset * sizeof(long))) | ||
197 | return NULL; | ||
198 | |||
201 | for (i=0; i<noffset; ++i) | 199 | for (i=0; i<noffset; ++i) |
202 | { | 200 | { |
203 | unsigned long offset; | 201 | ((unsigned long*)(pf->offset))[i] = (unsigned long)readlong(); |
204 | if (!readlong(&offset)) | ||
205 | return NULL; | ||
206 | ((unsigned long*)(pf->offset))[i] = (unsigned long)offset; | ||
207 | } | 202 | } |
208 | } | 203 | } |
209 | } | 204 | } |
@@ -229,13 +224,14 @@ struct font* font_load_cached(struct font* pf) | |||
229 | unsigned long noffset, nwidth; | 224 | unsigned long noffset, nwidth; |
230 | unsigned char* oldfileptr = fileptr; | 225 | unsigned char* oldfileptr = fileptr; |
231 | 226 | ||
232 | /* # longs of offset*/ | 227 | if (!HAVEBYTES(2 * sizeof(long))) |
233 | if (!readlong(&noffset)) | ||
234 | return NULL; | 228 | return NULL; |
235 | 229 | ||
230 | /* # longs of offset*/ | ||
231 | noffset = readlong(); | ||
232 | |||
236 | /* # bytes of width*/ | 233 | /* # bytes of width*/ |
237 | if (!readlong(&nwidth)) | 234 | nwidth = readlong(); |
238 | return NULL; | ||
239 | 235 | ||
240 | /* We are now at the bitmap data, this is fixed at 36.. */ | 236 | /* We are now at the bitmap data, this is fixed at 36.. */ |
241 | pf->bits = NULL; | 237 | pf->bits = NULL; |