| 1 |
/* Copyright (C) 2001-2012 by George Williams */ |
| 2 |
/* Copyright (C) 2016 by Belleve Invis */ |
| 3 |
|
| 4 |
/* |
| 5 |
* Redistribution and use in source and binary forms, with or without |
| 6 |
* modification, are permitted provided that the following conditions are met: |
| 7 |
* |
| 8 |
* Redistributions of source code must retain the above copyright notice, this |
| 9 |
* list of conditions and the following disclaimer. |
| 10 |
* |
| 11 |
* Redistributions in binary form must reproduce the above copyright notice, |
| 12 |
* this list of conditions and the following disclaimer in the documentation |
| 13 |
* and/or other materials provided with the distribution. |
| 14 |
* |
| 15 |
* The name of the author may not be used to endorse or promote products |
| 16 |
* derived from this software without specific prior written permission. |
| 17 |
* |
| 18 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 19 |
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 20 |
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
| 21 |
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 22 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 23 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 24 |
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 25 |
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 26 |
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 27 |
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 |
*/ |
| 29 |
|
| 30 |
#include "ttinstr.h" |
| 31 |
#include <ctype.h> |
| 32 |
|
| 33 |
enum ttf_instructions { |
| 34 |
ttf_npushb = 0x40, |
| 35 |
ttf_npushw = 0x41, |
| 36 |
ttf_pushb = 0xb0, |
| 37 |
ttf_pushw = 0xb8, |
| 38 |
ttf_aa = 0x7f, |
| 39 |
ttf_abs = 0x64, |
| 40 |
ttf_add = 0x60, |
| 41 |
ttf_alignpts = 0x27, |
| 42 |
ttf_alignrp = 0x3c, |
| 43 |
ttf_and = 0x5a, |
| 44 |
ttf_call = 0x2b, |
| 45 |
ttf_ceiling = 0x67, |
| 46 |
ttf_cindex = 0x25, |
| 47 |
ttf_clear = 0x22, |
| 48 |
ttf_debug = 0x4f, |
| 49 |
ttf_deltac1 = 0x73, |
| 50 |
ttf_deltac2 = 0x74, |
| 51 |
ttf_deltac3 = 0x75, |
| 52 |
ttf_deltap1 = 0x5d, |
| 53 |
ttf_deltap2 = 0x71, |
| 54 |
ttf_deltap3 = 0x72, |
| 55 |
ttf_depth = 0x24, |
| 56 |
ttf_div = 0x62, |
| 57 |
ttf_dup = 0x20, |
| 58 |
ttf_eif = 0x59, |
| 59 |
ttf_else = 0x1b, |
| 60 |
ttf_endf = 0x2d, |
| 61 |
ttf_eq = 0x54, |
| 62 |
ttf_even = 0x57, |
| 63 |
ttf_fdef = 0x2c, |
| 64 |
ttf_flipoff = 0x4e, |
| 65 |
ttf_flipon = 0x4d, |
| 66 |
ttf_flippt = 0x80, |
| 67 |
ttf_fliprgoff = 0x82, |
| 68 |
ttf_fliprgon = 0x81, |
| 69 |
ttf_floor = 0x66, |
| 70 |
ttf_gc = 0x46, |
| 71 |
ttf_getinfo = 0x88, |
| 72 |
ttf_gfv = 0x0d, |
| 73 |
ttf_gpv = 0x0c, |
| 74 |
ttf_gt = 0x52, |
| 75 |
ttf_gteq = 0x53, |
| 76 |
ttf_idef = 0x89, |
| 77 |
ttf_if = 0x58, |
| 78 |
ttf_instctrl = 0x8e, |
| 79 |
ttf_ip = 0x39, |
| 80 |
ttf_isect = 0x0f, |
| 81 |
ttf_iup = 0x30, |
| 82 |
ttf_jmpr = 0x1c, |
| 83 |
ttf_jrof = 0x79, |
| 84 |
ttf_jrot = 0x78, |
| 85 |
ttf_loopcall = 0x2a, |
| 86 |
ttf_lt = 0x50, |
| 87 |
ttf_lteq = 0x51, |
| 88 |
ttf_max = 0x8b, |
| 89 |
ttf_md = 0x49, |
| 90 |
ttf_mdap = 0x2e, |
| 91 |
ttf_mdrp = 0xc0, |
| 92 |
ttf_miap = 0x3e, |
| 93 |
ttf_min = 0x8c, |
| 94 |
ttf_mindex = 0x26, |
| 95 |
ttf_mirp = 0xe0, |
| 96 |
ttf_mppem = 0x4b, |
| 97 |
ttf_mps = 0x4c, |
| 98 |
ttf_msirp = 0x3a, |
| 99 |
ttf_mul = 0x63, |
| 100 |
ttf_neg = 0x65, |
| 101 |
ttf_neq = 0x55, |
| 102 |
ttf_not = 0x5c, |
| 103 |
ttf_nround = 0x6c, |
| 104 |
ttf_odd = 0x56, |
| 105 |
ttf_or = 0x5b, |
| 106 |
ttf_pop = 0x21, |
| 107 |
ttf_rcvt = 0x45, |
| 108 |
ttf_rdtg = 0x7d, |
| 109 |
ttf_roff = 0x7a, |
| 110 |
ttf_roll = 0x8a, |
| 111 |
ttf_round = 0x68, |
| 112 |
ttf_rs = 0x43, |
| 113 |
ttf_rtdg = 0x3d, |
| 114 |
ttf_rtg = 0x18, |
| 115 |
ttf_rthg = 0x19, |
| 116 |
ttf_rutg = 0x7c, |
| 117 |
ttf_s45round = 0x77, |
| 118 |
ttf_sangw = 0x7e, |
| 119 |
ttf_scanctrl = 0x85, |
| 120 |
ttf_scantype = 0x8d, |
| 121 |
ttf_scfs = 0x48, |
| 122 |
ttf_scvtci = 0x1d, |
| 123 |
ttf_sdb = 0x5e, |
| 124 |
ttf_sdpvtl = 0x86, |
| 125 |
ttf_sds = 0x5f, |
| 126 |
ttf_sfvfs = 0x0b, |
| 127 |
ttf_sfvtca = 0x04, |
| 128 |
ttf_sfvtl = 0x08, |
| 129 |
ttf_sfvtpv = 0x0e, |
| 130 |
ttf_shc = 0x34, |
| 131 |
ttf_shp = 0x32, |
| 132 |
ttf_shpix = 0x38, |
| 133 |
ttf_shz = 0x36, |
| 134 |
ttf_sloop = 0x17, |
| 135 |
ttf_smd = 0x1a, |
| 136 |
ttf_spvfs = 0x0a, |
| 137 |
ttf_spvtca = 0x02, |
| 138 |
ttf_spvtl = 0x06, |
| 139 |
ttf_sround = 0x76, |
| 140 |
ttf_srp0 = 0x10, |
| 141 |
ttf_srp1 = 0x11, |
| 142 |
ttf_srp2 = 0x12, |
| 143 |
ttf_ssw = 0x1f, |
| 144 |
ttf_sswci = 0x1e, |
| 145 |
ttf_sub = 0x61, |
| 146 |
ttf_svtca = 0x00, |
| 147 |
ttf_swap = 0x23, |
| 148 |
ttf_szp0 = 0x13, |
| 149 |
ttf_szp1 = 0x14, |
| 150 |
ttf_szp2 = 0x15, |
| 151 |
ttf_szps = 0x16, |
| 152 |
ttf_utp = 0x29, |
| 153 |
ttf_wcvtf = 0x70, |
| 154 |
ttf_wcvtp = 0x44, |
| 155 |
ttf_ws = 0x42, |
| 156 |
ttf_getvariation = 0x91 |
| 157 |
}; |
| 158 |
enum byte_types { bt_instr, bt_cnt, bt_byte, bt_wordhi, bt_wordlo, bt_impliedreturn }; |
| 159 |
|
| 160 |
struct instrdata { |
| 161 |
uint8_t *instrs; |
| 162 |
uint32_t instr_cnt; |
| 163 |
uint8_t *bts; |
| 164 |
}; |
| 165 |
|
| 166 |
const char *ff_ttf_instrnames[] = {"SVTCA[y-axis]", |
| 167 |
"SVTCA[x-axis]", |
| 168 |
"SPVTCA[y-axis]", |
| 169 |
"SPVTCA[x-axis]", |
| 170 |
"SFVTCA[y-axis]", |
| 171 |
"SFVTCA[x-axis]", |
| 172 |
"SPVTL[parallel]", |
| 173 |
"SPVTL[orthog]", |
| 174 |
"SFVTL[parallel]", |
| 175 |
"SFVTL[orthog]", |
| 176 |
"SPVFS", |
| 177 |
"SFVFS", |
| 178 |
"GPV", |
| 179 |
"GFV", |
| 180 |
"SFVTPV", |
| 181 |
"ISECT", |
| 182 |
"SRP0", |
| 183 |
"SRP1", |
| 184 |
"SRP2", |
| 185 |
"SZP0", |
| 186 |
"SZP1", |
| 187 |
"SZP2", |
| 188 |
"SZPS", |
| 189 |
"SLOOP", |
| 190 |
"RTG", |
| 191 |
"RTHG", |
| 192 |
"SMD", |
| 193 |
"ELSE", |
| 194 |
"JMPR", |
| 195 |
"SCVTCI", |
| 196 |
"SSWCI", |
| 197 |
"SSW", |
| 198 |
"DUP", |
| 199 |
"POP", |
| 200 |
"CLEAR", |
| 201 |
"SWAP", |
| 202 |
"DEPTH", |
| 203 |
"CINDEX", |
| 204 |
"MINDEX", |
| 205 |
"ALIGNPTS", |
| 206 |
"Unknown28", |
| 207 |
"UTP", |
| 208 |
"LOOPCALL", |
| 209 |
"CALL", |
| 210 |
"FDEF", |
| 211 |
"ENDF", |
| 212 |
"MDAP[no-rnd]", |
| 213 |
"MDAP[rnd]", |
| 214 |
"IUP[y]", |
| 215 |
"IUP[x]", |
| 216 |
"SHP[rp2]", |
| 217 |
"SHP[rp1]", |
| 218 |
"SHC[rp2]", |
| 219 |
"SHC[rp1]", |
| 220 |
"SHZ[rp2]", |
| 221 |
"SHZ[rp1]", |
| 222 |
"SHPIX", |
| 223 |
"IP", |
| 224 |
"MSIRP[no-rp0]", |
| 225 |
"MSIRP[rp0]", |
| 226 |
"ALIGNRP", |
| 227 |
"RTDG", |
| 228 |
"MIAP[no-rnd]", |
| 229 |
"MIAP[rnd]", |
| 230 |
"NPUSHB", |
| 231 |
"NPUSHW", |
| 232 |
"WS", |
| 233 |
"RS", |
| 234 |
"WCVTP", |
| 235 |
"RCVT", |
| 236 |
"GC[cur]", |
| 237 |
"GC[orig]", |
| 238 |
"SCFS", |
| 239 |
"MD[grid]", |
| 240 |
"MD[orig]", |
| 241 |
"MPPEM", |
| 242 |
"MPS", |
| 243 |
"FLIPON", |
| 244 |
"FLIPOFF", |
| 245 |
"DEBUG", |
| 246 |
"LT", |
| 247 |
"LTEQ", |
| 248 |
"GT", |
| 249 |
"GTEQ", |
| 250 |
"EQ", |
| 251 |
"NEQ", |
| 252 |
"ODD", |
| 253 |
"EVEN", |
| 254 |
"IF", |
| 255 |
"EIF", |
| 256 |
"AND", |
| 257 |
"OR", |
| 258 |
"NOT", |
| 259 |
"DELTAP1", |
| 260 |
"SDB", |
| 261 |
"SDS", |
| 262 |
"ADD", |
| 263 |
"SUB", |
| 264 |
"DIV", |
| 265 |
"MUL", |
| 266 |
"ABS", |
| 267 |
"NEG", |
| 268 |
"FLOOR", |
| 269 |
"CEILING", |
| 270 |
"ROUND[Grey]", |
| 271 |
"ROUND[Black]", |
| 272 |
"ROUND[White]", |
| 273 |
"ROUND[Undef4]", |
| 274 |
"NROUND[Grey]", |
| 275 |
"NROUND[Black]", |
| 276 |
"NROUND[White]", |
| 277 |
"NROUND[Undef4]", |
| 278 |
"WCVTF", |
| 279 |
"DELTAP2", |
| 280 |
"DELTAP3", |
| 281 |
"DELTAC1", |
| 282 |
"DELTAC2", |
| 283 |
"DELTAC3", |
| 284 |
"SROUND", |
| 285 |
"S45ROUND", |
| 286 |
"JROT", |
| 287 |
"JROF", |
| 288 |
"ROFF", |
| 289 |
"Unknown7B", |
| 290 |
"RUTG", |
| 291 |
"RDTG", |
| 292 |
"SANGW", |
| 293 |
"AA", |
| 294 |
"FLIPPT", |
| 295 |
"FLIPRGON", |
| 296 |
"FLIPRGOFF", |
| 297 |
"Unknown83", |
| 298 |
"Unknown84", |
| 299 |
"SCANCTRL", |
| 300 |
"SDPVTL[parallel]", |
| 301 |
"SDPVTL[orthog]", |
| 302 |
"GETINFO", |
| 303 |
"IDEF", |
| 304 |
"ROLL", |
| 305 |
"MAX", |
| 306 |
"MIN", |
| 307 |
"SCANTYPE", |
| 308 |
"INSTCTRL", |
| 309 |
"Unknown8F", |
| 310 |
"Unknown90", |
| 311 |
"GETVARIATION", |
| 312 |
"Unknown92", |
| 313 |
"Unknown93", |
| 314 |
"Unknown94", |
| 315 |
"Unknown95", |
| 316 |
"Unknown96", |
| 317 |
"Unknown97", |
| 318 |
"Unknown98", |
| 319 |
"Unknown99", |
| 320 |
"Unknown9A", |
| 321 |
"Unknown9B", |
| 322 |
"Unknown9C", |
| 323 |
"Unknown9D", |
| 324 |
"Unknown9E", |
| 325 |
"Unknown9F", |
| 326 |
"UnknownA0", |
| 327 |
"UnknownA1", |
| 328 |
"UnknownA2", |
| 329 |
"UnknownA3", |
| 330 |
"UnknownA4", |
| 331 |
"UnknownA5", |
| 332 |
"UnknownA6", |
| 333 |
"UnknownA7", |
| 334 |
"UnknownA8", |
| 335 |
"UnknownA9", |
| 336 |
"UnknownAA", |
| 337 |
"UnknownAB", |
| 338 |
"UnknownAC", |
| 339 |
"UnknownAD", |
| 340 |
"UnknownAE", |
| 341 |
"UnknownAF", |
| 342 |
"PUSHB_1", |
| 343 |
"PUSHB_2", |
| 344 |
"PUSHB_3", |
| 345 |
"PUSHB_4", |
| 346 |
"PUSHB_5", |
| 347 |
"PUSHB_6", |
| 348 |
"PUSHB_7", |
| 349 |
"PUSHB_8", |
| 350 |
"PUSHW_1", |
| 351 |
"PUSHW_2", |
| 352 |
"PUSHW_3", |
| 353 |
"PUSHW_4", |
| 354 |
"PUSHW_5", |
| 355 |
"PUSHW_6", |
| 356 |
"PUSHW_7", |
| 357 |
"PUSHW_8", |
| 358 |
"MDRP[grey]", |
| 359 |
"MDRP[black]", |
| 360 |
"MDRP[white]", |
| 361 |
"MDRP03", |
| 362 |
"MDRP[rnd,grey]", |
| 363 |
"MDRP[rnd,black]", |
| 364 |
"MDRP[rnd,white]", |
| 365 |
"MDRP07", |
| 366 |
"MDRP[min,grey]", |
| 367 |
"MDRP[min,black]", |
| 368 |
"MDRP[min,white]", |
| 369 |
"MDRP0b", |
| 370 |
"MDRP[min,rnd,grey]", |
| 371 |
"MDRP[min,rnd,black]", |
| 372 |
"MDRP[min,rnd,white]", |
| 373 |
"MDRP0f", |
| 374 |
"MDRP[rp0,grey]", |
| 375 |
"MDRP[rp0,black]", |
| 376 |
"MDRP[rp0,white]", |
| 377 |
"MDRP13", |
| 378 |
"MDRP[rp0,rnd,grey]", |
| 379 |
"MDRP[rp0,rnd,black]", |
| 380 |
"MDRP[rp0,rnd,white]", |
| 381 |
"MDRP17", |
| 382 |
"MDRP[rp0,min,grey]", |
| 383 |
"MDRP[rp0,min,black]", |
| 384 |
"MDRP[rp0,min,white]", |
| 385 |
"MDRP1b", |
| 386 |
"MDRP[rp0,min,rnd,grey]", |
| 387 |
"MDRP[rp0,min,rnd,black]", |
| 388 |
"MDRP[rp0,min,rnd,white]", |
| 389 |
"MDRP1f", |
| 390 |
"MIRP[grey]", |
| 391 |
"MIRP[black]", |
| 392 |
"MIRP[white]", |
| 393 |
"MIRP03", |
| 394 |
"MIRP[rnd,grey]", |
| 395 |
"MIRP[rnd,black]", |
| 396 |
"MIRP[rnd,white]", |
| 397 |
"MIRP07", |
| 398 |
"MIRP[min,grey]", |
| 399 |
"MIRP[min,black]", |
| 400 |
"MIRP[min,white]", |
| 401 |
"MIRP0b", |
| 402 |
"MIRP[min,rnd,grey]", |
| 403 |
"MIRP[min,rnd,black]", |
| 404 |
"MIRP[min,rnd,white]", |
| 405 |
"MIRP0f", |
| 406 |
"MIRP[rp0,grey]", |
| 407 |
"MIRP[rp0,black]", |
| 408 |
"MIRP[rp0,white]", |
| 409 |
"MIRP13", |
| 410 |
"MIRP[rp0,rnd,grey]", |
| 411 |
"MIRP[rp0,rnd,black]", |
| 412 |
"MIRP[rp0,rnd,white]", |
| 413 |
"MIRP17", |
| 414 |
"MIRP[rp0,min,grey]", |
| 415 |
"MIRP[rp0,min,black]", |
| 416 |
"MIRP[rp0,min,white]", |
| 417 |
"MIRP1b", |
| 418 |
"MIRP[rp0,min,rnd,grey]", |
| 419 |
"MIRP[rp0,min,rnd,black]", |
| 420 |
"MIRP[rp0,min,rnd,white]", |
| 421 |
"MIRP1f"}; |
| 422 |
|
| 423 |
static int strnmatch(const char *str1, const char *str2, int n) { |
| 424 |
int ch1, ch2; |
| 425 |
for (; n-- > 0;) { |
| 426 |
ch1 = *str1++; |
| 427 |
ch2 = *str2++; |
| 428 |
ch1 = tolower(ch1); |
| 429 |
ch2 = tolower(ch2); |
| 430 |
if (ch1 != ch2 || ch1 == '\0') return (ch1 - ch2); |
| 431 |
} |
| 432 |
return (0); |
| 433 |
} |
| 434 |
|
| 435 |
static uint8_t *parse_instrs(char *text, int *len, void *context, |
| 436 |
void (*IVError)(void *context, const char *, int)) { |
| 437 |
short numberstack[256]; |
| 438 |
int npos = 0, nread, i; |
| 439 |
int push_left = 0, push_size = 0; |
| 440 |
char *pt; |
| 441 |
char *end, *bend, *brack; |
| 442 |
int icnt = 0, imax = (int)(strlen(text)), val; |
| 443 |
uint8_t *instrs; |
| 444 |
NEW(instrs, imax); |
| 445 |
|
| 446 |
for (pt = text; *pt; ++pt) { |
| 447 |
npos = 0; |
| 448 |
while (npos < 256) { |
| 449 |
while (*pt == ' ' || *pt == '\t') |
| 450 |
++pt; |
| 451 |
if (isdigit(*pt) || *pt == '-') { |
| 452 |
val = strtol(pt, &end, 0); |
| 453 |
if (val > 32767 || val < -32768) { |
| 454 |
IVError(context, "A value must be between [-32768,32767]", (int)(pt - text)); |
| 455 |
return (NULL); |
| 456 |
} |
| 457 |
pt = end; |
| 458 |
numberstack[npos++] = val; |
| 459 |
} else |
| 460 |
break; |
| 461 |
} |
| 462 |
while (*pt == ' ' || *pt == '\t') |
| 463 |
++pt; |
| 464 |
if (npos == 0 && (*pt == '\r' || *pt == '\n' || *pt == '\0')) continue; |
| 465 |
nread = 0; |
| 466 |
if (push_left == -1) { |
| 467 |
/* we need a push count */ |
| 468 |
if (npos == 0) |
| 469 |
IVError(context, "Expected a number for a push count", (int)(pt - text)); |
| 470 |
else if (numberstack[0] > 255 || numberstack[0] <= 0) { |
| 471 |
IVError(context, "The push count must be a number between 0 and 255", |
| 472 |
(int)(pt - text)); |
| 473 |
return (NULL); |
| 474 |
} else { |
| 475 |
nread = 1; |
| 476 |
instrs[icnt++] = numberstack[0]; |
| 477 |
push_left = numberstack[0]; |
| 478 |
} |
| 479 |
} |
| 480 |
if (push_left != 0 && push_left < npos - nread && |
| 481 |
(*pt == '\r' || *pt == '\n' || *pt == '\0')) { |
| 482 |
IVError(context, "More pushes specified than needed", (int)(pt - text)); |
| 483 |
return (NULL); |
| 484 |
} |
| 485 |
while (push_left > 0 && nread < npos) { |
| 486 |
if (push_size == 2) { |
| 487 |
instrs[icnt++] = numberstack[nread] >> 8; |
| 488 |
instrs[icnt++] = numberstack[nread++] & 0xff; |
| 489 |
} else if (numberstack[0] > 255 || numberstack[0] < 0) { |
| 490 |
IVError(context, "A value to be pushed by a byte push must be between 0 and 255", |
| 491 |
(int)(pt - text)); |
| 492 |
return (NULL); |
| 493 |
} else |
| 494 |
instrs[icnt++] = numberstack[nread++]; |
| 495 |
--push_left; |
| 496 |
} |
| 497 |
if (nread < npos && push_left == 0 && (*pt == '\r' || *pt == '\n' || *pt == '\0')) { |
| 498 |
IVError(context, "Unexpected number", (int)(pt - text)); |
| 499 |
return (NULL); |
| 500 |
} |
| 501 |
if (*pt == '\r' || *pt == '\n' || *pt == '\0') continue; |
| 502 |
if (push_left > 0) { |
| 503 |
IVError(context, "Missing pushes", (int)(pt - text)); |
| 504 |
return (NULL); |
| 505 |
} |
| 506 |
while (nread < npos) { |
| 507 |
i = nread; |
| 508 |
if (numberstack[nread] >= 0 && numberstack[nread] <= 255) { |
| 509 |
while (i < npos && numberstack[i] >= 0 && numberstack[i] <= 255) |
| 510 |
++i; |
| 511 |
if (i - nread <= 8) |
| 512 |
instrs[icnt++] = ttf_pushb + (i - nread) - 1; |
| 513 |
else { |
| 514 |
instrs[icnt++] = ttf_npushb; |
| 515 |
instrs[icnt++] = i - nread; |
| 516 |
} |
| 517 |
while (nread < i) |
| 518 |
instrs[icnt++] = numberstack[nread++]; |
| 519 |
} else { |
| 520 |
while (i < npos && (numberstack[i] < 0 || numberstack[i] > 255)) |
| 521 |
++i; |
| 522 |
if (i - nread <= 8) |
| 523 |
instrs[icnt++] = ttf_pushw + (i - nread) - 1; |
| 524 |
else { |
| 525 |
instrs[icnt++] = ttf_npushw; |
| 526 |
instrs[icnt++] = i - nread; |
| 527 |
} |
| 528 |
while (nread < i) { |
| 529 |
instrs[icnt++] = numberstack[nread] >> 8; |
| 530 |
instrs[icnt++] = numberstack[nread++] & 0xff; |
| 531 |
} |
| 532 |
} |
| 533 |
} |
| 534 |
brack = NULL; |
| 535 |
for (end = pt; *end != '\r' && *end != '\n' && *end != ' ' && *end != '\0'; ++end) |
| 536 |
if (*end == '[' || *end == '_') brack = end; |
| 537 |
for (i = 0; i < 256; ++i) |
| 538 |
if (strnmatch(pt, ff_ttf_instrnames[i], (int)(end - pt)) == 0 && |
| 539 |
sizeof(char) * (end - pt) == strlen(ff_ttf_instrnames[i])) |
| 540 |
break; |
| 541 |
if (i == 256 && brack != NULL) { |
| 542 |
for (i = 0; i < 256; ++i) |
| 543 |
if (strnmatch(pt, ff_ttf_instrnames[i], (int)(brack - pt + 1)) == 0) break; |
| 544 |
val = strtol(brack + 1, &bend, 2); /* Stuff in brackets should be in binary */ |
| 545 |
while (*bend == ' ' || *bend == '\t') |
| 546 |
++bend; |
| 547 |
if (*bend != ']') { |
| 548 |
IVError(context, |
| 549 |
"Missing right bracket in command (or bad binary value in bracket)", |
| 550 |
(int)(pt - text)); |
| 551 |
return (NULL); |
| 552 |
} |
| 553 |
if (val >= 32) { |
| 554 |
IVError(context, "Bracketted value is too large", (int)(pt - text)); |
| 555 |
return (NULL); |
| 556 |
} |
| 557 |
i += val; |
| 558 |
} |
| 559 |
pt = end; |
| 560 |
instrs[icnt++] = i; |
| 561 |
if (i == ttf_npushb || i == ttf_npushw || (i >= ttf_pushb && i <= ttf_pushw + 7)) { |
| 562 |
push_size = (i == ttf_npushb || (i >= ttf_pushb && i <= ttf_pushb + 7)) ? 1 : 2; |
| 563 |
if (i == ttf_npushb || i == ttf_npushw) |
| 564 |
push_left = -1; |
| 565 |
else if (i >= ttf_pushb && i <= ttf_pushb + 7) |
| 566 |
push_left = i - ttf_pushb + 1; |
| 567 |
else |
| 568 |
push_left = i - ttf_pushw + 1; |
| 569 |
} |
| 570 |
if (*pt == '\0') break; |
| 571 |
} |
| 572 |
*len = icnt; |
| 573 |
RESIZE(instrs, icnt == 0 ? 1 : icnt); |
| 574 |
return instrs; /* some versions of realloc abort on 0 */ |
| 575 |
} |
| 576 |
|
| 577 |
static int instr_typify(struct instrdata *id) { |
| 578 |
int i, len = id->instr_cnt, cnt, j, lh; |
| 579 |
uint8_t *instrs = id->instrs; |
| 580 |
uint8_t *bts; |
| 581 |
|
| 582 |
if (id->bts == NULL) NEW(id->bts, len + 1); |
| 583 |
bts = id->bts; |
| 584 |
for (i = lh = 0; i < len; ++i) { |
| 585 |
bts[i] = bt_instr; |
| 586 |
++lh; |
| 587 |
if (instrs[i] == ttf_npushb) { |
| 588 |
/* NPUSHB */ |
| 589 |
bts[++i] = bt_cnt; |
| 590 |
cnt = instrs[i]; |
| 591 |
for (j = 0; j < cnt; ++j) |
| 592 |
bts[++i] = bt_byte; |
| 593 |
lh += 1 + cnt; |
| 594 |
} else if (instrs[i] == ttf_npushw) { |
| 595 |
/* NPUSHW */ |
| 596 |
bts[++i] = bt_cnt; |
| 597 |
++lh; |
| 598 |
cnt = instrs[i]; |
| 599 |
for (j = 0; j < cnt; ++j) { |
| 600 |
bts[++i] = bt_wordhi; |
| 601 |
bts[++i] = bt_wordlo; |
| 602 |
} |
| 603 |
lh += 1 + cnt; |
| 604 |
} else if ((instrs[i] & 0xf8) == 0xb0) { |
| 605 |
/* PUSHB[n] */ |
| 606 |
cnt = (instrs[i] & 7) + 1; |
| 607 |
for (j = 0; j < cnt; ++j) |
| 608 |
bts[++i] = bt_byte; |
| 609 |
lh += cnt; |
| 610 |
} else if ((instrs[i] & 0xf8) == 0xb8) { |
| 611 |
/* PUSHW[n] */ |
| 612 |
cnt = (instrs[i] & 7) + 1; |
| 613 |
for (j = 0; j < cnt; ++j) { |
| 614 |
bts[++i] = bt_wordhi; |
| 615 |
bts[++i] = bt_wordlo; |
| 616 |
} |
| 617 |
lh += cnt; |
| 618 |
} |
| 619 |
} |
| 620 |
bts[i] = bt_impliedreturn; |
| 621 |
return (lh); |
| 622 |
} |
| 623 |
|
| 624 |
json_value *dump_ttinstr(uint8_t *instructions, uint32_t length, const otfcc_Options *options) { |
| 625 |
if (options->instr_as_bytes) { |
| 626 |
size_t len = 0; |
| 627 |
uint8_t *buf = base64_encode(instructions, length, &len); |
| 628 |
return json_string_new_length((uint32_t)len, (char *)buf); |
| 629 |
FREE(buf); |
| 630 |
} else { |
| 631 |
struct instrdata id; |
| 632 |
|
| 633 |
memset(&id, 0, sizeof(id)); |
| 634 |
id.instr_cnt = length; |
| 635 |
id.instrs = instructions; |
| 636 |
instr_typify(&id); |
| 637 |
json_value *ret = json_array_new(id.instr_cnt); |
| 638 |
for (uint32_t i = 0; i < id.instr_cnt; ++i) { |
| 639 |
if (id.bts[i] == bt_wordhi) { |
| 640 |
json_array_push( |
| 641 |
ret, json_integer_new((int16_t)((id.instrs[i] << 8) | id.instrs[i + 1]))); |
| 642 |
++i; |
| 643 |
} else if (id.bts[i] == bt_cnt || id.bts[i] == bt_byte) { |
| 644 |
json_array_push(ret, json_integer_new(id.instrs[i])); |
| 645 |
} else { |
| 646 |
json_array_push(ret, json_string_new(ff_ttf_instrnames[id.instrs[i]])); |
| 647 |
} |
| 648 |
} |
| 649 |
FREE(id.bts); |
| 650 |
return preserialize(ret); |
| 651 |
} |
| 652 |
} |
| 653 |
|
| 654 |
void parse_ttinstr(json_value *col, void *context, void (*Make)(void *, uint8_t *, uint32_t), |
| 655 |
void (*Wrong)(void *, const char *, int)) { |
| 656 |
if (!col) { |
| 657 |
Make(context, NULL, 0); |
| 658 |
} else if (col->type == json_string) { |
| 659 |
size_t instrlen; |
| 660 |
uint8_t *instructions = |
| 661 |
base64_decode((uint8_t *)col->u.string.ptr, col->u.string.length, &instrlen); |
| 662 |
Make(context, instructions, (uint32_t)instrlen); |
| 663 |
} else if (col->type == json_array) { |
| 664 |
size_t istrlen = 0; |
| 665 |
for (uint32_t j = 0; j < col->u.array.length; j++) { |
| 666 |
json_value *record = col->u.array.values[j]; |
| 667 |
if (record->type == json_string) { |
| 668 |
istrlen += record->u.string.length + 1; |
| 669 |
} else if (record->type == json_integer) { |
| 670 |
istrlen += 1 + 20; |
| 671 |
} else { |
| 672 |
Make(context, NULL, 0); |
| 673 |
return; |
| 674 |
} |
| 675 |
} |
| 676 |
sds instrString = sdsnewlen(NULL, istrlen + 1); |
| 677 |
char *head = instrString; |
| 678 |
for (uint32_t j = 0; j < col->u.array.length; j++) { |
| 679 |
json_value *record = col->u.array.values[j]; |
| 680 |
if (record->type == json_string) { |
| 681 |
memcpy(head, record->u.string.ptr, sizeof(char) * record->u.string.length); |
| 682 |
head += record->u.string.length; |
| 683 |
} else if (record->type == json_integer) { |
| 684 |
int n = snprintf(head, 20, "%d", (int)record->u.integer); |
| 685 |
head += n; |
| 686 |
} |
| 687 |
*head = '\n'; |
| 688 |
head++; |
| 689 |
} |
| 690 |
int instrLength = 0; |
| 691 |
uint8_t *instructions = parse_instrs(instrString, &instrLength, context, Wrong); |
| 692 |
sdsfree(instrString); |
| 693 |
if (instructions && instrLength) { |
| 694 |
Make(context, instructions, instrLength); |
| 695 |
} else { |
| 696 |
Make(context, NULL, 0); |
| 697 |
} |
| 698 |
} else { |
| 699 |
Make(context, NULL, 0); |
| 700 |
} |
| 701 |
} |