cairo-script-scanner.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904
  1. /*
  2. * Copyright © 2008 Chris Wilson <chris@chris-wilson.co.uk>
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it either under the terms of the GNU Lesser General Public
  6. * License version 2.1 as published by the Free Software Foundation
  7. * (the "LGPL") or, at your option, under the terms of the Mozilla
  8. * Public License Version 1.1 (the "MPL"). If you do not alter this
  9. * notice, a recipient may use your version of this file under either
  10. * the MPL or the LGPL.
  11. *
  12. * You should have received a copy of the LGPL along with this library
  13. * in the file COPYING-LGPL-2.1; if not, write to the Free Software
  14. * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA
  15. * You should have received a copy of the MPL along with this library
  16. * in the file COPYING-MPL-1.1
  17. *
  18. * The contents of this file are subject to the Mozilla Public License
  19. * Version 1.1 (the "License"); you may not use this file except in
  20. * compliance with the License. You may obtain a copy of the License at
  21. * http://www.mozilla.org/MPL/
  22. *
  23. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
  24. * OF ANY KIND, either express or implied. See the LGPL or the MPL for
  25. * the specific language governing rights and limitations.
  26. *
  27. * The Original Code is the cairo graphics library.
  28. *
  29. * The Initial Developer of the Original Code is Chris Wilson.
  30. *
  31. * Contributor(s):
  32. * Chris Wilson <chris@chris-wilson.co.uk>
  33. */
  34. #include "config.h"
  35. #include "cairo-script-private.h"
  36. #include <limits.h> /* INT_MAX */
  37. #include <math.h> /* pow */
  38. #include <stdio.h> /* EOF */
  39. #include <stdint.h> /* for {INT,UINT}*_{MIN,MAX} */
  40. #include <stdlib.h> /* malloc/free */
  41. #include <string.h> /* memset */
  42. #include <assert.h>
  43. #include <zlib.h>
  44. #if HAVE_LZO
  45. #include <lzo/lzo2a.h>
  46. #endif
  47. #define DEBUG_SCAN 0
  48. #if WORDS_BIGENDIAN
  49. #define le16(x) bswap_16 (x)
  50. #define le32(x) bswap_32 (x)
  51. #define be16(x) x
  52. #define be32(x) x
  53. #define to_be32(x) x
  54. #else
  55. #define le16(x) x
  56. #define le32(x) x
  57. #define be16(x) bswap_16 (x)
  58. #define be32(x) bswap_32 (x)
  59. #define to_be32(x) bswap_32 (x)
  60. #endif
  61. /*
  62. * whitespace:
  63. * 0 - nul
  64. * 9 - tab
  65. * A - LF
  66. * C - FF
  67. * D - CR
  68. *
  69. * syntax delimiters
  70. * ( = 28, ) = 29 - literal strings
  71. * < = 3C, > = 3E - hex/base85 strings, dictionary name
  72. * [ = 5B, ] = 5D - array
  73. * { = 7B, } = 7C - procedure
  74. * / = 5C - literal marker
  75. * % = 25 - comment
  76. */
  77. static void
  78. fprintf_obj (FILE *stream, csi_t *ctx, const csi_object_t *obj)
  79. {
  80. switch (csi_object_get_type (obj)) {
  81. case CSI_OBJECT_TYPE_NULL:
  82. fprintf (stream, "NULL\n");
  83. break;
  84. /* atomics */
  85. case CSI_OBJECT_TYPE_BOOLEAN:
  86. fprintf (stream, "boolean: %s\n",
  87. obj->datum.boolean ? "true" : "false");
  88. break;
  89. case CSI_OBJECT_TYPE_INTEGER:
  90. fprintf (stream, "integer: %ld\n", obj->datum.integer);
  91. break;
  92. case CSI_OBJECT_TYPE_MARK:
  93. fprintf (stream, "mark\n");
  94. break;
  95. case CSI_OBJECT_TYPE_NAME:
  96. fprintf (stream, "name: %s\n", (char *) obj->datum.name);
  97. break;
  98. case CSI_OBJECT_TYPE_OPERATOR:
  99. fprintf (stream, "operator: %p\n", obj->datum.ptr);
  100. break;
  101. case CSI_OBJECT_TYPE_REAL:
  102. fprintf (stream, "real: %g\n", obj->datum.real);
  103. break;
  104. /* compound */
  105. case CSI_OBJECT_TYPE_ARRAY:
  106. fprintf (stream, "array\n");
  107. break;
  108. case CSI_OBJECT_TYPE_DICTIONARY:
  109. fprintf (stream, "dictionary\n");
  110. break;
  111. case CSI_OBJECT_TYPE_FILE:
  112. fprintf (stream, "file\n");
  113. break;
  114. case CSI_OBJECT_TYPE_MATRIX:
  115. fprintf (stream, "matrix: [%g %g %g %g %g %g]\n",
  116. obj->datum.matrix->matrix.xx,
  117. obj->datum.matrix->matrix.yx,
  118. obj->datum.matrix->matrix.xy,
  119. obj->datum.matrix->matrix.yy,
  120. obj->datum.matrix->matrix.x0,
  121. obj->datum.matrix->matrix.y0);
  122. break;
  123. case CSI_OBJECT_TYPE_STRING:
  124. fprintf (stream, "string: len=%ld, defate=%ld, method=%d\n",
  125. obj->datum.string->len, obj->datum.string->deflate, obj->datum.string->method);
  126. break;
  127. /* cairo */
  128. case CSI_OBJECT_TYPE_CONTEXT:
  129. fprintf (stream, "context\n");
  130. break;
  131. case CSI_OBJECT_TYPE_FONT:
  132. fprintf (stream, "font\n");
  133. break;
  134. case CSI_OBJECT_TYPE_PATTERN:
  135. fprintf (stream, "pattern\n");
  136. break;
  137. case CSI_OBJECT_TYPE_SCALED_FONT:
  138. fprintf (stream, "scaled-font\n");
  139. break;
  140. case CSI_OBJECT_TYPE_SURFACE:
  141. fprintf (stream, "surface\n");
  142. break;
  143. }
  144. }
  145. /* takes ownership of obj */
  146. static inline csi_status_t
  147. scan_push (csi_t *ctx, csi_object_t *obj)
  148. {
  149. return ctx->scanner.push (ctx, obj);
  150. }
  151. static inline csi_status_t
  152. scan_execute (csi_t *ctx, csi_object_t *obj)
  153. {
  154. return ctx->scanner.execute (ctx, obj);
  155. }
  156. static cairo_status_t
  157. buffer_init (csi_t *ctx, csi_buffer_t *buffer)
  158. {
  159. cairo_status_t status = CSI_STATUS_SUCCESS;
  160. buffer->size = 16384;
  161. buffer->base = _csi_alloc (ctx, buffer->size);
  162. if (_csi_unlikely (buffer->base == NULL)) {
  163. status = _csi_error (CSI_STATUS_NO_MEMORY);
  164. buffer->size = 0;
  165. }
  166. buffer->ptr = buffer->base;
  167. buffer->end = buffer->base + buffer->size;
  168. return status;
  169. }
  170. static void
  171. buffer_fini (csi_t *ctx, csi_buffer_t *buffer)
  172. {
  173. _csi_free (ctx, buffer->base);
  174. }
  175. static void
  176. _buffer_grow (csi_t *ctx, csi_scanner_t *scan)
  177. {
  178. int newsize;
  179. int offset;
  180. char *base;
  181. if (_csi_unlikely (scan->buffer.size > INT_MAX / 2))
  182. longjmp (scan->jump_buffer, _csi_error (CSI_STATUS_NO_MEMORY));
  183. offset = scan->buffer.ptr - scan->buffer.base;
  184. newsize = scan->buffer.size * 2;
  185. base = _csi_realloc (ctx, scan->buffer.base, newsize);
  186. if (_csi_unlikely (base == NULL))
  187. longjmp (scan->jump_buffer, _csi_error (CSI_STATUS_NO_MEMORY));
  188. scan->buffer.base = base;
  189. scan->buffer.ptr = base + offset;
  190. scan->buffer.end = base + newsize;
  191. scan->buffer.size = newsize;
  192. }
  193. static inline void
  194. buffer_check (csi_t *ctx, csi_scanner_t *scan, int count)
  195. {
  196. if (_csi_unlikely (scan->buffer.ptr + count > scan->buffer.end))
  197. _buffer_grow (ctx, scan);
  198. }
  199. static inline void
  200. buffer_add (csi_buffer_t *buffer, int c)
  201. {
  202. *buffer->ptr++ = c;
  203. }
  204. static inline void
  205. buffer_reset (csi_buffer_t *buffer)
  206. {
  207. buffer->ptr = buffer->base;
  208. }
  209. static void
  210. token_start (csi_scanner_t *scan)
  211. {
  212. buffer_reset (&scan->buffer);
  213. }
  214. static void
  215. token_add (csi_t *ctx, csi_scanner_t *scan, int c)
  216. {
  217. buffer_check (ctx, scan, 1);
  218. buffer_add (&scan->buffer, c);
  219. }
  220. static void
  221. token_add_unchecked (csi_scanner_t *scan, int c)
  222. {
  223. buffer_add (&scan->buffer, c);
  224. }
  225. csi_boolean_t
  226. _csi_parse_number (csi_object_t *obj, const char *s, int len)
  227. {
  228. int radix = 0;
  229. long long mantissa = 0;
  230. int exponent = 0;
  231. int sign = 1;
  232. int decimal = -1;
  233. int exponent_sign = 0;
  234. const char * const end = s + len;
  235. switch (*s) {
  236. case '0':
  237. case '1':
  238. case '2':
  239. case '3':
  240. case '4':
  241. case '5':
  242. case '6':
  243. case '7':
  244. case '8':
  245. case '9':
  246. mantissa = *s - '0';
  247. case '+':
  248. break;
  249. case '-':
  250. sign = -1;
  251. break;
  252. case '.':
  253. decimal = 0;
  254. break;
  255. default:
  256. return FALSE;
  257. }
  258. while (++s < end) {
  259. if (*s < '0') {
  260. if (*s == '.') {
  261. if (_csi_unlikely (radix))
  262. return FALSE;
  263. if (_csi_unlikely (decimal != -1))
  264. return FALSE;
  265. if (_csi_unlikely (exponent_sign))
  266. return FALSE;
  267. decimal = 0;
  268. } else if (*s == '!') {
  269. if (_csi_unlikely (radix))
  270. return FALSE;
  271. if (_csi_unlikely (decimal != -1))
  272. return FALSE;
  273. if (_csi_unlikely (exponent_sign))
  274. return FALSE;
  275. radix = mantissa;
  276. mantissa = 0;
  277. if (_csi_unlikely (radix < 2 || radix > 36))
  278. return FALSE;
  279. } else
  280. return FALSE;
  281. } else if (*s <= '9') {
  282. int v = *s - '0';
  283. if (_csi_unlikely (radix && v >= radix))
  284. return FALSE;
  285. if (exponent_sign) {
  286. exponent = 10 * exponent + v;
  287. } else {
  288. if (radix)
  289. mantissa = radix * mantissa + v;
  290. else
  291. mantissa = 10 * mantissa + v;
  292. if (decimal != -1)
  293. decimal++;
  294. }
  295. } else if (*s == 'E' || * s== 'e') {
  296. if (radix == 0) {
  297. if (_csi_unlikely (s + 1 == end))
  298. return FALSE;
  299. exponent_sign = 1;
  300. if (s[1] == '-') {
  301. exponent_sign = -1;
  302. s++;
  303. } else if (s[1] == '+')
  304. s++;
  305. } else {
  306. int v = 0xe;
  307. if (_csi_unlikely (v >= radix))
  308. return FALSE;
  309. mantissa = radix * mantissa + v;
  310. }
  311. } else if (*s < 'A') {
  312. return FALSE;
  313. } else if (*s <= 'Z') {
  314. int v = *s - 'A' + 0xA;
  315. if (_csi_unlikely (v >= radix))
  316. return FALSE;
  317. mantissa = radix * mantissa + v;
  318. } else if (*s < 'a') {
  319. return FALSE;
  320. } else if (*s <= 'z') {
  321. int v = *s - 'a' + 0xa;
  322. if (_csi_unlikely (v >= radix))
  323. return FALSE;
  324. mantissa = radix * mantissa + v;
  325. } else
  326. return FALSE;
  327. }
  328. if (exponent_sign || decimal != -1) {
  329. if (mantissa == 0) {
  330. obj->type = CSI_OBJECT_TYPE_REAL;
  331. obj->datum.real = 0.;
  332. return TRUE;
  333. } else {
  334. int e;
  335. double v;
  336. v = mantissa;
  337. e = exponent * exponent_sign;
  338. if (decimal != -1)
  339. e -= decimal;
  340. switch (e) {
  341. case -7: v *= 0.0000001; break;
  342. case -6: v *= 0.000001; break;
  343. case -5: v *= 0.00001; break;
  344. case -4: v *= 0.0001; break;
  345. case -3: v *= 0.001; break;
  346. case -2: v *= 0.01; break;
  347. case -1: v *= 0.1; break;
  348. case 0: break;
  349. case 1: v *= 10; break;
  350. case 2: v *= 100; break;
  351. case 3: v *= 1000; break;
  352. case 4: v *= 10000; break;
  353. case 5: v *= 100000; break;
  354. case 6: v *= 1000000; break;
  355. default:
  356. v *= pow (10, e); /* XXX */
  357. break;
  358. }
  359. obj->type = CSI_OBJECT_TYPE_REAL;
  360. obj->datum.real = sign * v;
  361. return TRUE;
  362. }
  363. } else {
  364. obj->type = CSI_OBJECT_TYPE_INTEGER;
  365. obj->datum.integer = sign * mantissa;
  366. return TRUE;
  367. }
  368. }
  369. static void
  370. token_end (csi_t *ctx, csi_scanner_t *scan, csi_file_t *src)
  371. {
  372. cairo_status_t status;
  373. char *s;
  374. csi_object_t obj;
  375. int len;
  376. /*
  377. * Any token that consists entirely of regular characters and
  378. * cannot be interpreted as a number is treated as a name object
  379. * (more precisely, an executable name). All characters except
  380. * delimiters and white-space characters can appear in names,
  381. * including characters ordinarily considered to be punctuation.
  382. */
  383. if (_csi_unlikely (scan->buffer.ptr == scan->buffer.base))
  384. return;
  385. s = scan->buffer.base;
  386. len = scan->buffer.ptr - scan->buffer.base;
  387. if (_csi_likely (! scan->bind)) {
  388. if (s[0] == '{') { /* special case procedures */
  389. if (scan->build_procedure.type != CSI_OBJECT_TYPE_NULL) {
  390. status = _csi_stack_push (ctx,
  391. &scan->procedure_stack,
  392. &scan->build_procedure);
  393. if (_csi_unlikely (status))
  394. longjmp (scan->jump_buffer, status);
  395. }
  396. status = csi_array_new (ctx, 0, &scan->build_procedure);
  397. if (_csi_unlikely (status))
  398. longjmp (scan->jump_buffer, status);
  399. scan->build_procedure.type |= CSI_OBJECT_ATTR_EXECUTABLE;
  400. return;
  401. } else if (s[0] == '}') {
  402. if (_csi_unlikely
  403. (scan->build_procedure.type == CSI_OBJECT_TYPE_NULL))
  404. {
  405. longjmp (scan->jump_buffer, _csi_error (CSI_STATUS_INVALID_SCRIPT));
  406. }
  407. if (scan->procedure_stack.len) {
  408. csi_object_t *next;
  409. next = _csi_stack_peek (&scan->procedure_stack, 0);
  410. status = csi_array_append (ctx, next->datum.array,
  411. &scan->build_procedure);
  412. scan->build_procedure = *next;
  413. scan->procedure_stack.len--;
  414. } else {
  415. status = scan_push (ctx, &scan->build_procedure);
  416. scan->build_procedure.type = CSI_OBJECT_TYPE_NULL;
  417. }
  418. if (_csi_unlikely (status))
  419. longjmp (scan->jump_buffer, status);
  420. return;
  421. }
  422. }
  423. if (s[0] == '/') {
  424. if (len >= 2 && s[1] == '/') { /* substituted name */
  425. status = csi_name_new (ctx, &obj, s + 2, len - 2);
  426. if (_csi_unlikely (status))
  427. longjmp (scan->jump_buffer, status);
  428. status = _csi_name_lookup (ctx, obj.datum.name, &obj);
  429. } else { /* literal name */
  430. status = csi_name_new (ctx, &obj, s + 1, len - 1);
  431. }
  432. if (_csi_unlikely (status))
  433. longjmp (scan->jump_buffer, status);
  434. } else {
  435. if (! _csi_parse_number (&obj, s, len)) {
  436. status = csi_name_new (ctx, &obj, s, len);
  437. if (_csi_unlikely (status))
  438. longjmp (scan->jump_buffer, status);
  439. obj.type |= CSI_OBJECT_ATTR_EXECUTABLE;
  440. }
  441. }
  442. /* consume whitespace after token, before calling the interpreter */
  443. if (scan->build_procedure.type != CSI_OBJECT_TYPE_NULL) {
  444. status = csi_array_append (ctx,
  445. scan->build_procedure.datum.array,
  446. &obj);
  447. } else if (obj.type & CSI_OBJECT_ATTR_EXECUTABLE) {
  448. status = scan_execute (ctx, &obj);
  449. csi_object_free (ctx, &obj);
  450. } else {
  451. status = scan_push (ctx, &obj);
  452. }
  453. if (_csi_unlikely (status))
  454. longjmp (scan->jump_buffer, status);
  455. }
  456. static void
  457. string_add (csi_t *ctx, csi_scanner_t *scan, int c)
  458. {
  459. buffer_check (ctx, scan, 1);
  460. buffer_add (&scan->buffer, c);
  461. }
  462. static void
  463. string_end (csi_t *ctx, csi_scanner_t *scan)
  464. {
  465. csi_object_t obj;
  466. cairo_status_t status;
  467. status = csi_string_new (ctx,
  468. &obj,
  469. scan->buffer.base,
  470. scan->buffer.ptr - scan->buffer.base);
  471. if (_csi_unlikely (status))
  472. longjmp (scan->jump_buffer, status);
  473. if (scan->build_procedure.type != CSI_OBJECT_TYPE_NULL)
  474. status = csi_array_append (ctx,
  475. scan->build_procedure.datum.array,
  476. &obj);
  477. else
  478. status = scan_push (ctx, &obj);
  479. if (_csi_unlikely (status))
  480. longjmp (scan->jump_buffer, status);
  481. }
  482. static int
  483. hex_value (int c)
  484. {
  485. if (c < '0')
  486. return EOF;
  487. if (c <= '9')
  488. return c - '0';
  489. c |= 32;
  490. if (c < 'a')
  491. return EOF;
  492. if (c <= 'f')
  493. return c - 'a' + 0xa;
  494. return EOF;
  495. }
  496. static void
  497. hex_add (csi_t *ctx, csi_scanner_t *scan, int c)
  498. {
  499. if (scan->accumulator_count == 0) {
  500. scan->accumulator |= hex_value (c) << 4;
  501. scan->accumulator_count = 1;
  502. } else {
  503. scan->accumulator |= hex_value (c) << 0;
  504. buffer_check (ctx, scan, 1);
  505. buffer_add (&scan->buffer, scan->accumulator);
  506. scan->accumulator = 0;
  507. scan->accumulator_count = 0;
  508. }
  509. }
  510. static void
  511. hex_end (csi_t *ctx, csi_scanner_t *scan)
  512. {
  513. csi_object_t obj;
  514. cairo_status_t status;
  515. if (scan->accumulator_count)
  516. hex_add (ctx, scan, '0');
  517. status = csi_string_new (ctx,
  518. &obj,
  519. scan->buffer.base,
  520. scan->buffer.ptr - scan->buffer.base);
  521. if (_csi_unlikely (status))
  522. longjmp (scan->jump_buffer, status);
  523. if (scan->build_procedure.type != CSI_OBJECT_TYPE_NULL)
  524. status = csi_array_append (ctx,
  525. scan->build_procedure.datum.array,
  526. &obj);
  527. else
  528. status = scan_push (ctx, &obj);
  529. if (_csi_unlikely (status))
  530. longjmp (scan->jump_buffer, status);
  531. }
  532. static void
  533. base85_add (csi_t *ctx, csi_scanner_t *scan, int c)
  534. {
  535. if (c == 'z') {
  536. if (_csi_unlikely (scan->accumulator_count != 0))
  537. longjmp (scan->jump_buffer, _csi_error (CSI_STATUS_INVALID_SCRIPT));
  538. buffer_check (ctx, scan, 4);
  539. buffer_add (&scan->buffer, 0);
  540. buffer_add (&scan->buffer, 0);
  541. buffer_add (&scan->buffer, 0);
  542. buffer_add (&scan->buffer, 0);
  543. } else if (_csi_unlikely (c < '!' || c > 'u')) {
  544. longjmp (scan->jump_buffer, _csi_error (CSI_STATUS_INVALID_SCRIPT));
  545. } else {
  546. scan->accumulator = scan->accumulator*85 + c - '!';
  547. if (++scan->accumulator_count == 5) {
  548. buffer_check (ctx, scan, 4);
  549. buffer_add (&scan->buffer, (scan->accumulator >> 24) & 0xff);
  550. buffer_add (&scan->buffer, (scan->accumulator >> 16) & 0xff);
  551. buffer_add (&scan->buffer, (scan->accumulator >> 8) & 0xff);
  552. buffer_add (&scan->buffer, (scan->accumulator >> 0) & 0xff);
  553. scan->accumulator = 0;
  554. scan->accumulator_count = 0;
  555. }
  556. }
  557. }
  558. static void
  559. base85_end (csi_t *ctx, csi_scanner_t *scan, cairo_bool_t deflate)
  560. {
  561. csi_object_t obj;
  562. cairo_status_t status;
  563. buffer_check (ctx, scan, 4);
  564. switch (scan->accumulator_count) {
  565. case 0:
  566. break;
  567. case 1:
  568. longjmp (scan->jump_buffer, _csi_error (CSI_STATUS_INVALID_SCRIPT));
  569. break;
  570. case 2:
  571. scan->accumulator = scan->accumulator * (85*85*85) + 85*85*85 -1;
  572. buffer_add (&scan->buffer, (scan->accumulator >> 24) & 0xff);
  573. break;
  574. case 3:
  575. scan->accumulator = scan->accumulator * (85*85) + 85*85 -1;
  576. buffer_add (&scan->buffer, (scan->accumulator >> 24) & 0xff);
  577. buffer_add (&scan->buffer, (scan->accumulator >> 16) & 0xff);
  578. break;
  579. case 4:
  580. scan->accumulator = scan->accumulator * 85 + 84;
  581. buffer_add (&scan->buffer, (scan->accumulator >> 24) & 0xff);
  582. buffer_add (&scan->buffer, (scan->accumulator >> 16) & 0xff);
  583. buffer_add (&scan->buffer, (scan->accumulator >> 8) & 0xff);
  584. break;
  585. }
  586. if (deflate) {
  587. uLongf len = be32 (*(uint32_t *) scan->buffer.base);
  588. Bytef *source = (Bytef *) (scan->buffer.base + sizeof (uint32_t));
  589. status = csi_string_deflate_new (ctx, &obj,
  590. source,
  591. (Bytef *) scan->buffer.ptr - source,
  592. len);
  593. if (_csi_unlikely (status))
  594. longjmp (scan->jump_buffer, status);
  595. } else {
  596. status = csi_string_new (ctx,
  597. &obj,
  598. scan->buffer.base,
  599. scan->buffer.ptr - scan->buffer.base);
  600. if (_csi_unlikely (status))
  601. longjmp (scan->jump_buffer, status);
  602. }
  603. if (scan->build_procedure.type != CSI_OBJECT_TYPE_NULL)
  604. status = csi_array_append (ctx,
  605. scan->build_procedure.datum.array,
  606. &obj);
  607. else
  608. status = scan_push (ctx, &obj);
  609. if (_csi_unlikely (status))
  610. longjmp (scan->jump_buffer, status);
  611. }
  612. static void
  613. base64_add (csi_t *ctx, csi_scanner_t *scan, int c)
  614. {
  615. int val;
  616. /* Convert Base64 character to its 6 bit nibble */
  617. val = scan->accumulator;
  618. if (c =='/') {
  619. val = (val << 6) | 63;
  620. } else if (c =='+') {
  621. val = (val << 6) | 62;
  622. } else if (c >='A' && c <='Z') {
  623. val = (val << 6) | (c -'A');
  624. } else if (c >='a' && c <='z') {
  625. val = (val << 6) | (c -'a' + 26);
  626. } else if (c >='0' && c <='9') {
  627. val = (val << 6) | (c -'0' + 52);
  628. }
  629. buffer_check (ctx, scan, 1);
  630. switch (scan->accumulator_count++) {
  631. case 0:
  632. break;
  633. case 1:
  634. buffer_add (&scan->buffer, (val >> 4) & 0xFF);
  635. val &= 0xF;
  636. break;
  637. case 2:
  638. buffer_add (&scan->buffer, (val >> 2) & 0xFF);
  639. val &= 0x3;
  640. break;
  641. case 3:
  642. buffer_add (&scan->buffer, (val >> 0) & 0xFF);
  643. scan->accumulator_count = 0;
  644. val = 0;
  645. break;
  646. }
  647. if (c == '=') {
  648. scan->accumulator_count = 0;
  649. scan->accumulator = 0;
  650. } else {
  651. scan->accumulator = val;
  652. }
  653. }
  654. static void
  655. base64_end (csi_t *ctx, csi_scanner_t *scan)
  656. {
  657. csi_object_t obj;
  658. cairo_status_t status;
  659. switch (scan->accumulator_count) {
  660. case 0:
  661. break;
  662. case 2:
  663. base64_add (ctx, scan, (scan->accumulator << 2) & 0x3f);
  664. base64_add (ctx, scan, '=');
  665. break;
  666. case 1:
  667. base64_add (ctx, scan, (scan->accumulator << 4) & 0x3f);
  668. base64_add (ctx, scan, '=');
  669. base64_add (ctx, scan, '=');
  670. break;
  671. }
  672. status = csi_string_new (ctx,
  673. &obj,
  674. scan->buffer.base,
  675. scan->buffer.ptr - scan->buffer.base);
  676. if (_csi_unlikely (status))
  677. longjmp (scan->jump_buffer, status);
  678. if (scan->build_procedure.type != CSI_OBJECT_TYPE_NULL)
  679. status = csi_array_append (ctx,
  680. scan->build_procedure.datum.array,
  681. &obj);
  682. else
  683. status = scan_push (ctx, &obj);
  684. if (_csi_unlikely (status))
  685. longjmp (scan->jump_buffer, status);
  686. }
  687. static void
  688. scan_read (csi_scanner_t *scan, csi_file_t *src, void *ptr, int len)
  689. {
  690. uint8_t *data = ptr;
  691. do {
  692. int ret = csi_file_read (src, data, len);
  693. if (_csi_unlikely (ret == 0))
  694. longjmp (scan->jump_buffer, _csi_error (CSI_STATUS_READ_ERROR));
  695. data += ret;
  696. len -= ret;
  697. } while (_csi_unlikely (len));
  698. }
  699. static void
  700. string_read (csi_t *ctx,
  701. csi_scanner_t *scan,
  702. csi_file_t *src,
  703. int len,
  704. int compressed,
  705. csi_object_t *obj)
  706. {
  707. csi_status_t status;
  708. status = csi_string_new (ctx, obj, NULL, len);
  709. if (_csi_unlikely (status))
  710. longjmp (scan->jump_buffer, status);
  711. if (compressed) {
  712. uint32_t u32;
  713. scan_read (scan, src, &u32, 4);
  714. obj->datum.string->deflate = be32 (u32);
  715. obj->datum.string->method = compressed;
  716. }
  717. if (_csi_likely (len))
  718. scan_read (scan, src, obj->datum.string->string, len);
  719. obj->datum.string->string[len] = '\0';
  720. }
  721. static void
  722. _scan_file (csi_t *ctx, csi_file_t *src)
  723. {
  724. csi_scanner_t *scan = &ctx->scanner;
  725. int c, next;
  726. union {
  727. int8_t i8;
  728. uint8_t u8;
  729. int16_t i16;
  730. uint16_t u16;
  731. int32_t i32;
  732. uint32_t u32;
  733. float f;
  734. } u;
  735. int deflate = 0;
  736. int string_p;
  737. scan_none:
  738. while ((c = csi_file_getc (src)) != EOF) {
  739. csi_object_t obj = { CSI_OBJECT_TYPE_NULL };
  740. switch (c) {
  741. case 0xa:
  742. scan->line_number++;
  743. case 0x0:
  744. case 0x9:
  745. case 0xc:
  746. case 0xd:
  747. case 0x20: /* ignore whitespace */
  748. break;
  749. case '%':
  750. goto scan_comment;
  751. case '(':
  752. goto scan_string;
  753. case '[': /* needs special case */
  754. case ']':
  755. case '{':
  756. case '}':
  757. token_start (scan);
  758. token_add_unchecked (scan, c);
  759. token_end (ctx, scan, src);
  760. goto scan_none;
  761. case '<':
  762. next = csi_file_getc (src);
  763. switch (next) {
  764. case EOF:
  765. csi_file_putc (src, '<');
  766. return;
  767. case '<':
  768. /* dictionary name */
  769. token_start (scan);
  770. token_add_unchecked (scan, '<');
  771. token_add_unchecked (scan, '<');
  772. token_end (ctx, scan, src);
  773. goto scan_none;
  774. case '|':
  775. deflate = 1;
  776. case '~':
  777. goto scan_base85;
  778. case '{':
  779. goto scan_base64;
  780. default:
  781. csi_file_putc (src, next);
  782. goto scan_hex;
  783. }
  784. break;
  785. /* binary token */
  786. #define MSB_INT8 128
  787. #define MSB_UINT8 129
  788. #define MSB_INT16 130
  789. #define MSB_UINT16 131
  790. #define MSB_INT32 132
  791. #define LSB_INT8 MSB_INT8
  792. #define LSB_UINT8 MSB_UINT8
  793. #define LSB_INT16 133
  794. #define LSB_UINT16 134
  795. #define LSB_INT32 135
  796. #define MSB_FLOAT32 140
  797. #define LSB_FLOAT32 141
  798. case MSB_INT8:
  799. scan_read (scan, src, &u.i8, 1);
  800. csi_integer_new (&obj, u.i8);
  801. break;
  802. case MSB_UINT8:
  803. scan_read (scan, src, &u.u8, 1);
  804. csi_integer_new (&obj, u.u8);
  805. break;
  806. case MSB_INT16:
  807. scan_read (scan, src, &u.i16, 2);
  808. csi_integer_new (&obj, be16 (u.i16));
  809. break;
  810. case LSB_INT16:
  811. scan_read (scan, src, &u.i16, 2);
  812. csi_integer_new (&obj, le16 (u.i16));
  813. break;
  814. case MSB_UINT16:
  815. scan_read (scan, src, &u.u16, 2);
  816. csi_integer_new (&obj, be16 (u.u16));
  817. break;
  818. case LSB_UINT16:
  819. scan_read (scan, src, &u.u16, 2);
  820. csi_integer_new (&obj, le16 (u.u16));
  821. break;
  822. case MSB_INT32:
  823. scan_read (scan, src, &u.i32, 4);
  824. csi_integer_new (&obj, be32 (u.i32));
  825. break;
  826. case LSB_INT32:
  827. scan_read (scan, src, &u.i32, 4);
  828. csi_integer_new (&obj, le32 (u.i32));
  829. break;
  830. case 136: /* 16.16 msb */
  831. scan_read (scan, src, &u.i32, 4);
  832. csi_real_new (&obj, be32 (u.i32) / 65536.);
  833. break;
  834. case 137: /* 16.16 lsb */
  835. scan_read (scan, src, &u.i32, 4);
  836. csi_real_new (&obj, le32 (u.i32) / 65536.);
  837. break;
  838. case 138: /* 24.8 msb */
  839. scan_read (scan, src, &u.i32, 4);
  840. csi_real_new (&obj, be32 (u.i32) / 256.);
  841. break;
  842. case 139: /* 24.8 lsb */
  843. scan_read (scan, src, &u.i32, 4);
  844. csi_real_new (&obj, le32 (u.i32) / 256.);
  845. break;
  846. case MSB_FLOAT32:
  847. scan_read (scan, src, &u.i32, 4);
  848. u.i32 = be32 (u.i32);
  849. csi_real_new (&obj, u.f);
  850. break;
  851. case LSB_FLOAT32:
  852. scan_read (scan, src, &u.i32, 4);
  853. u.i32 = le32 (u.i32);
  854. csi_real_new (&obj, u.f);
  855. break;
  856. #define STRING_1 142
  857. #define STRING_2_MSB 144
  858. #define STRING_2_LSB 146
  859. #define STRING_4_MSB 148
  860. #define STRING_4_LSB 150
  861. #define STRING_DEFLATE 1
  862. case STRING_1:
  863. case STRING_1 | STRING_DEFLATE:
  864. scan_read (scan, src, &u.u8, 1);
  865. string_read (ctx, scan, src, u.u8, c & STRING_DEFLATE, &obj);
  866. break;
  867. case STRING_2_MSB:
  868. case STRING_2_MSB | STRING_DEFLATE:
  869. scan_read (scan, src, &u.u16, 2);
  870. string_read (ctx, scan, src, be16 (u.u16), c & STRING_DEFLATE, &obj);
  871. break;
  872. case STRING_2_LSB:
  873. case STRING_2_LSB | STRING_DEFLATE:
  874. scan_read (scan, src, &u.u16, 2);
  875. string_read (ctx, scan, src, le16 (u.u16), c & STRING_DEFLATE, &obj);
  876. break;
  877. case STRING_4_MSB:
  878. case STRING_4_MSB | STRING_DEFLATE:
  879. scan_read (scan, src, &u.u32, 4);
  880. string_read (ctx, scan, src, be32 (u.u32), c & STRING_DEFLATE, &obj);
  881. break;
  882. case STRING_4_LSB:
  883. case STRING_4_LSB | STRING_DEFLATE:
  884. scan_read (scan, src, &u.u32, 4);
  885. string_read (ctx, scan, src, le32 (u.u32), c & STRING_DEFLATE, &obj);
  886. break;
  887. #define OPCODE 152
  888. case OPCODE:
  889. scan_read (scan, src, &u.u8, 1);
  890. csi_operator_new (&obj, ctx->opcode[u.u8]);
  891. break;
  892. case OPCODE | 1:
  893. scan_read (scan, src, &u.u8, 1);
  894. csi_operator_new (&obj, ctx->opcode[u.u8]);
  895. obj.type &= ~CSI_OBJECT_ATTR_EXECUTABLE;
  896. break;
  897. #define STRING_LZO 154
  898. case STRING_LZO:
  899. scan_read (scan, src, &u.u32, 4);
  900. string_read (ctx, scan, src, be32 (u.u32), LZO, &obj);
  901. break;
  902. /* unassigned */
  903. case 155:
  904. case 156:
  905. case 157:
  906. case 158:
  907. case 159:
  908. longjmp (scan->jump_buffer, _csi_error (CSI_STATUS_INVALID_SCRIPT));
  909. case '#': /* PDF 1.2 escape code */
  910. {
  911. int c_hi = csi_file_getc (src);
  912. int c_lo = csi_file_getc (src);
  913. c = (hex_value (c_hi) << 4) | hex_value (c_lo);
  914. }
  915. /* fall-through */
  916. default:
  917. token_start (scan);
  918. token_add_unchecked (scan, c);
  919. goto scan_token;
  920. }
  921. if (obj.type != CSI_OBJECT_TYPE_NULL) {
  922. cairo_status_t status;
  923. if (scan->build_procedure.type != CSI_OBJECT_TYPE_NULL) {
  924. status = csi_array_append (ctx,
  925. scan->build_procedure.datum.array,
  926. &obj);
  927. } else if (obj.type & CSI_OBJECT_ATTR_EXECUTABLE) {
  928. status = scan_execute (ctx, &obj);
  929. csi_object_free (ctx, &obj);
  930. } else {
  931. status = scan_push (ctx, &obj);
  932. }
  933. if (_csi_unlikely (status))
  934. longjmp (scan->jump_buffer, status);
  935. }
  936. }
  937. return;
  938. scan_token:
  939. while ((c = csi_file_getc (src)) != EOF) {
  940. switch (c) {
  941. case 0xa:
  942. scan->line_number++;
  943. case 0x0:
  944. case 0x9:
  945. case 0xc:
  946. case 0xd:
  947. case 0x20:
  948. token_end (ctx, scan, src);
  949. goto scan_none;
  950. /* syntax delimiters */
  951. case '%':
  952. token_end (ctx, scan, src);
  953. goto scan_comment;
  954. /* syntax error? */
  955. case '(':
  956. token_end (ctx, scan, src);
  957. goto scan_string;
  958. /* XXX syntax error? */
  959. case ')':
  960. token_end (ctx, scan, src);
  961. goto scan_none;
  962. case '/':
  963. /* need to special case '^//?' */
  964. if (scan->buffer.ptr > scan->buffer.base+1 ||
  965. scan->buffer.base[0] != '/')
  966. {
  967. token_end (ctx, scan, src);
  968. token_start (scan);
  969. }
  970. token_add_unchecked (scan, '/');
  971. goto scan_token;
  972. case '{':
  973. case '}':
  974. case ']':
  975. token_end (ctx, scan, src);
  976. token_start (scan);
  977. token_add_unchecked (scan, c);
  978. token_end (ctx, scan, src);
  979. goto scan_none;
  980. case '<':
  981. csi_file_putc (src, '<');
  982. token_end (ctx, scan, src);
  983. goto scan_none;
  984. case '#': /* PDF 1.2 escape code */
  985. {
  986. int c_hi = csi_file_getc (src);
  987. int c_lo = csi_file_getc (src);
  988. c = (hex_value (c_hi) << 4) | hex_value (c_lo);
  989. }
  990. /* fall-through */
  991. default:
  992. token_add (ctx, scan, c);
  993. break;
  994. }
  995. }
  996. token_end (ctx, scan, src);
  997. return;
  998. scan_comment:
  999. /* discard until newline */
  1000. while ((c = csi_file_getc (src)) != EOF) {
  1001. switch (c) {
  1002. case 0xa:
  1003. scan->line_number++;
  1004. case 0xc:
  1005. goto scan_none;
  1006. }
  1007. }
  1008. return;
  1009. scan_string:
  1010. buffer_reset (&scan->buffer);
  1011. string_p = 1;
  1012. while ((c = csi_file_getc (src)) != EOF) {
  1013. switch (c) {
  1014. case '\\': /* escape */
  1015. next = csi_file_getc (src);
  1016. switch (next) {
  1017. case EOF:
  1018. longjmp (scan->jump_buffer, _csi_error (CSI_STATUS_INVALID_SCRIPT));
  1019. case 'n':
  1020. string_add (ctx, scan, '\n');
  1021. break;
  1022. case 'r':
  1023. string_add (ctx, scan, '\r');
  1024. break;
  1025. case 't':
  1026. string_add (ctx, scan, '\t');
  1027. break;
  1028. case 'b':
  1029. string_add (ctx, scan, '\b');
  1030. break;
  1031. case 'f':
  1032. string_add (ctx, scan, '\f');
  1033. break;
  1034. case '\\':
  1035. string_add (ctx, scan, '\\');
  1036. break;
  1037. case '(':
  1038. string_add (ctx, scan, '(');
  1039. break;
  1040. case ')':
  1041. string_add (ctx, scan, ')');
  1042. break;
  1043. case '0': case '1': case '2': case '3':
  1044. case '4': case '5': case '6': case '7':
  1045. { /* octal code: \d{1,3} */
  1046. int i;
  1047. c = next - '0';
  1048. for (i = 0; i < 2; i++) {
  1049. next = csi_file_getc (src);
  1050. switch (next) {
  1051. case EOF:
  1052. return;
  1053. case '0': case '1': case '2': case '3':
  1054. case '4': case '5': case '6': case '7':
  1055. c = 8*c + next-'0';
  1056. break;
  1057. default:
  1058. csi_file_putc (src, next);
  1059. goto octal_code_done;
  1060. }
  1061. }
  1062. octal_code_done:
  1063. string_add (ctx, scan, c);
  1064. }
  1065. break;
  1066. case 0xa:
  1067. /* skip the newline */
  1068. next = csi_file_getc (src); /* might be compound LFCR */
  1069. switch (next) {
  1070. case EOF:
  1071. return;
  1072. case 0xc:
  1073. break;
  1074. default:
  1075. csi_file_putc (src, next);
  1076. break;
  1077. }
  1078. scan->line_number++;
  1079. break;
  1080. case 0xc:
  1081. break;
  1082. default:
  1083. /* ignore the '\' */
  1084. break;
  1085. }
  1086. break;
  1087. case '(':
  1088. string_p++;
  1089. string_add (ctx, scan, c);
  1090. break;
  1091. case ')':
  1092. if (--string_p == 0) {
  1093. string_end (ctx, scan);
  1094. goto scan_none;
  1095. }
  1096. /* fall through */
  1097. default:
  1098. string_add (ctx, scan, c);
  1099. break;
  1100. }
  1101. }
  1102. longjmp (scan->jump_buffer, _csi_error (CSI_STATUS_INVALID_SCRIPT));
  1103. scan_hex:
  1104. buffer_reset (&scan->buffer);
  1105. scan->accumulator_count = 0;
  1106. scan->accumulator = 0;
  1107. while ((c = csi_file_getc (src)) != EOF) {
  1108. switch (c) {
  1109. case 0xa:
  1110. scan->line_number++;
  1111. case 0x0:
  1112. case 0x9:
  1113. case 0xc:
  1114. case 0xd:
  1115. case 0x20: /* ignore whitespace */
  1116. break;
  1117. case '>':
  1118. hex_end (ctx, scan); /* fixup odd digit with '0' */
  1119. goto scan_none;
  1120. case '0':
  1121. case '1':
  1122. case '2':
  1123. case '3':
  1124. case '4':
  1125. case '5':
  1126. case '6':
  1127. case '7':
  1128. case '8':
  1129. case '9':
  1130. case 'a':
  1131. case 'b':
  1132. case 'c':
  1133. case 'd':
  1134. case 'e':
  1135. case 'f':
  1136. case 'A':
  1137. case 'B':
  1138. case 'C':
  1139. case 'D':
  1140. case 'E':
  1141. case 'F':
  1142. hex_add (ctx, scan, c);
  1143. break;
  1144. default:
  1145. longjmp (scan->jump_buffer, _csi_error (CSI_STATUS_INVALID_SCRIPT));
  1146. }
  1147. }
  1148. longjmp (scan->jump_buffer, _csi_error (CSI_STATUS_INVALID_SCRIPT));
  1149. scan_base85:
  1150. buffer_reset (&scan->buffer);
  1151. scan->accumulator = 0;
  1152. scan->accumulator_count = 0;
  1153. while ((c = csi_file_getc (src)) != EOF) {
  1154. switch (c) {
  1155. case '~':
  1156. next = csi_file_getc (src);
  1157. switch (next) {
  1158. case EOF:
  1159. return;
  1160. case '>':
  1161. base85_end (ctx, scan, deflate);
  1162. deflate = 0;
  1163. goto scan_none;
  1164. }
  1165. csi_file_putc (src, next);
  1166. /* fall-through */
  1167. default:
  1168. base85_add (ctx, scan, c);
  1169. break;
  1170. }
  1171. }
  1172. longjmp (scan->jump_buffer, _csi_error (CSI_STATUS_INVALID_SCRIPT));
  1173. scan_base64:
  1174. buffer_reset (&scan->buffer);
  1175. scan->accumulator = 0;
  1176. scan->accumulator_count = 0;
  1177. while ((c = csi_file_getc (src)) != EOF) {
  1178. switch (c) {
  1179. case '}':
  1180. next = csi_file_getc (src);
  1181. switch (next) {
  1182. case EOF:
  1183. return;
  1184. case '>':
  1185. base64_end (ctx, scan);
  1186. goto scan_none;
  1187. }
  1188. longjmp (scan->jump_buffer, _csi_error (CSI_STATUS_INVALID_SCRIPT));
  1189. default:
  1190. base64_add (ctx, scan, c);
  1191. break;
  1192. }
  1193. }
  1194. longjmp (scan->jump_buffer, _csi_error (CSI_STATUS_INVALID_SCRIPT));
  1195. }
  1196. static csi_status_t
  1197. _scan_push (csi_t *ctx, csi_object_t *obj)
  1198. {
  1199. if (DEBUG_SCAN) {
  1200. fprintf (stderr, "push ");
  1201. fprintf_obj (stderr, ctx, obj);
  1202. }
  1203. return _csi_push_ostack (ctx, obj);
  1204. }
  1205. static csi_status_t
  1206. _scan_execute (csi_t *ctx, csi_object_t *obj)
  1207. {
  1208. if (DEBUG_SCAN) {
  1209. fprintf (stderr, "exec ");
  1210. fprintf_obj (stderr, ctx, obj);
  1211. }
  1212. return csi_object_execute (ctx, obj);
  1213. }
  1214. csi_status_t
  1215. _csi_scanner_init (csi_t *ctx, csi_scanner_t *scanner)
  1216. {
  1217. csi_status_t status;
  1218. memset (scanner, 0, sizeof (csi_scanner_t));
  1219. status = buffer_init (ctx, &scanner->buffer);
  1220. if (status)
  1221. return status;
  1222. status = _csi_stack_init (ctx, &scanner->procedure_stack, 4);
  1223. if (status)
  1224. return status;
  1225. scanner->bind = 0;
  1226. scanner->push = _scan_push;
  1227. scanner->execute = _scan_execute;
  1228. return CSI_STATUS_SUCCESS;
  1229. }
  1230. void
  1231. _csi_scanner_fini (csi_t *ctx, csi_scanner_t *scanner)
  1232. {
  1233. buffer_fini (ctx, &scanner->buffer);
  1234. _csi_stack_fini (ctx, &scanner->procedure_stack);
  1235. if (scanner->build_procedure.type != CSI_OBJECT_TYPE_NULL)
  1236. csi_object_free (ctx, &scanner->build_procedure);
  1237. }
  1238. csi_status_t
  1239. _csi_scan_file (csi_t *ctx, csi_file_t *src)
  1240. {
  1241. csi_status_t status;
  1242. int old_line_number;
  1243. /* This function needs to be reentrant to handle recursive scanners.
  1244. * i.e. one script executes a second.
  1245. */
  1246. if (ctx->scanner.depth++ == 0) {
  1247. if ((status = setjmp (ctx->scanner.jump_buffer))) {
  1248. ctx->scanner.depth = 0;
  1249. return status;
  1250. }
  1251. }
  1252. old_line_number = ctx->scanner.line_number;
  1253. ctx->scanner.line_number = 0;
  1254. _scan_file (ctx, src);
  1255. ctx->scanner.line_number = old_line_number;
  1256. --ctx->scanner.depth;
  1257. return CSI_STATUS_SUCCESS;
  1258. }
  1259. struct _translate_closure {
  1260. csi_dictionary_t *opcodes;
  1261. cairo_write_func_t write_func;
  1262. void *closure;
  1263. };
  1264. static csi_status_t
  1265. _translate_name (csi_t *ctx,
  1266. csi_name_t name,
  1267. csi_boolean_t executable,
  1268. struct _translate_closure *closure)
  1269. {
  1270. if (executable) {
  1271. csi_dictionary_entry_t *entry;
  1272. uint16_t u16;
  1273. /* Bind executable names.
  1274. * XXX This may break some scripts that overload system operators.
  1275. */
  1276. entry = _csi_hash_table_lookup (&closure->opcodes->hash_table,
  1277. (csi_hash_entry_t *) &name);
  1278. if (entry == NULL)
  1279. goto STRING;
  1280. u16 = entry->value.datum.integer;
  1281. u16 = be16 (u16);
  1282. closure->write_func (closure->closure, (unsigned char *) &u16, 2);
  1283. } else {
  1284. closure->write_func (closure->closure, (unsigned char *) "/", 1);
  1285. STRING:
  1286. closure->write_func (closure->closure,
  1287. (unsigned char *) name,
  1288. strlen ((char *) name));
  1289. closure->write_func (closure->closure, (unsigned char *) "\n", 1);
  1290. }
  1291. return CSI_STATUS_SUCCESS;
  1292. }
  1293. static csi_status_t
  1294. _translate_operator (csi_t *ctx,
  1295. csi_operator_t op,
  1296. csi_boolean_t executable,
  1297. struct _translate_closure *closure)
  1298. {
  1299. csi_dictionary_entry_t *entry;
  1300. uint16_t u16;
  1301. entry = _csi_hash_table_lookup (&closure->opcodes->hash_table,
  1302. (csi_hash_entry_t *) &op);
  1303. if (entry == NULL)
  1304. return _csi_error (CSI_STATUS_INVALID_SCRIPT);
  1305. u16 = entry->value.datum.integer;
  1306. if (! executable)
  1307. u16 += 1 << 8;
  1308. u16 = be16 (u16);
  1309. closure->write_func (closure->closure, (unsigned char *) &u16, 2);
  1310. return CSI_STATUS_SUCCESS;
  1311. }
  1312. static csi_status_t
  1313. _translate_integer (csi_t *ctx,
  1314. csi_integer_t i,
  1315. struct _translate_closure *closure)
  1316. {
  1317. uint8_t hdr;
  1318. union {
  1319. int8_t i8;
  1320. uint8_t u8;
  1321. int16_t i16;
  1322. uint16_t u16;
  1323. int32_t i32;
  1324. uint32_t u32;
  1325. } u;
  1326. int len;
  1327. #if WORDS_BIGENDIAN
  1328. if (i < INT16_MIN) {
  1329. hdr = MSB_INT32;
  1330. len = 4;
  1331. u.i32 = i;
  1332. } else if (i < INT8_MIN) {
  1333. hdr = MSB_INT16;
  1334. len = 2;
  1335. u.i16 = i;
  1336. } else if (i < 0) {
  1337. hdr = MSB_INT8;
  1338. len = 1;
  1339. u.i8 = i;
  1340. } else if (i <= UINT8_MAX) {
  1341. hdr = MSB_UINT8;
  1342. len = 1;
  1343. u.u8 = i;
  1344. } else if (i <= UINT16_MAX) {
  1345. hdr = MSB_UINT16;
  1346. len = 2;
  1347. u.u16 = i;
  1348. } else {
  1349. hdr = MSB_INT32;
  1350. len = 4;
  1351. u.u32 = i;
  1352. }
  1353. #else
  1354. if (i < INT16_MIN) {
  1355. hdr = LSB_INT32;
  1356. len = 4;
  1357. u.i32 = i;
  1358. } else if (i < INT8_MIN) {
  1359. hdr = LSB_INT16;
  1360. len = 2;
  1361. u.i16 = i;
  1362. } else if (i < 0) {
  1363. hdr = LSB_INT8;
  1364. len = 1;
  1365. u.i8 = i;
  1366. } else if (i <= UINT8_MAX) {
  1367. hdr = LSB_UINT8;
  1368. len = 1;
  1369. u.u8 = i;
  1370. } else if (i <= UINT16_MAX) {
  1371. hdr = LSB_UINT16;
  1372. len = 2;
  1373. u.u16 = i;
  1374. } else {
  1375. hdr = LSB_INT32;
  1376. len = 4;
  1377. u.u32 = i;
  1378. }
  1379. #endif
  1380. closure->write_func (closure->closure, (unsigned char *) &hdr, 1);
  1381. closure->write_func (closure->closure, (unsigned char *) &u, len);
  1382. return CSI_STATUS_SUCCESS;
  1383. }
  1384. static csi_status_t
  1385. _translate_real (csi_t *ctx,
  1386. csi_real_t real,
  1387. struct _translate_closure *closure)
  1388. {
  1389. uint8_t hdr;
  1390. if (real >= INT32_MIN && real <= INT32_MAX && (int) real == real)
  1391. return _translate_integer (ctx, real, closure);
  1392. #if WORDS_BIGENDIAN
  1393. hdr = MSB_FLOAT32;
  1394. #else
  1395. hdr = LSB_FLOAT32;
  1396. #endif
  1397. closure->write_func (closure->closure, (unsigned char *) &hdr, 1);
  1398. closure->write_func (closure->closure, (unsigned char *) &real, 4);
  1399. return CSI_STATUS_SUCCESS;
  1400. }
  1401. static csi_status_t
  1402. _translate_string (csi_t *ctx,
  1403. csi_string_t *string,
  1404. struct _translate_closure *closure)
  1405. {
  1406. uint8_t hdr;
  1407. union {
  1408. uint8_t u8;
  1409. uint16_t u16;
  1410. uint32_t u32;
  1411. } u;
  1412. void *buf;
  1413. unsigned long hdr_len, buf_len, deflate;
  1414. int method;
  1415. buf = string->string;
  1416. buf_len = string->len;
  1417. deflate = string->deflate;
  1418. method = string->method;
  1419. #if HAVE_LZO
  1420. if (method == NONE && buf_len > 16) {
  1421. unsigned long mem_len = 2*string->len > LZO2A_999_MEM_COMPRESS ? 2*string->len : LZO2A_999_MEM_COMPRESS;
  1422. void *mem = malloc (mem_len);
  1423. void *work = malloc(LZO2A_999_MEM_COMPRESS);
  1424. if (lzo2a_999_compress ((lzo_bytep) buf, buf_len,
  1425. (lzo_bytep) mem, &mem_len,
  1426. work) == 0 &&
  1427. 8+2*mem_len < buf_len)
  1428. {
  1429. method = LZO;
  1430. deflate = buf_len;
  1431. buf_len = mem_len;
  1432. buf = mem;
  1433. }
  1434. else
  1435. {
  1436. free (mem);
  1437. }
  1438. free (work);
  1439. }
  1440. #if HAVE_ZLIB
  1441. if (method == ZLIB) {
  1442. buf_len = string->deflate;
  1443. buf = malloc (string->deflate);
  1444. if (uncompress ((Bytef *) buf, &buf_len,
  1445. (Bytef *) string->string, string->len) == Z_OK)
  1446. {
  1447. assert(string->len > 0);
  1448. if (buf_len <= 8 + 2*((unsigned long)string->len)) {
  1449. method = NONE;
  1450. deflate = 0;
  1451. } else {
  1452. unsigned long mem_len = 2*string->deflate;
  1453. void *mem = malloc (mem_len);
  1454. void *work = malloc(LZO2A_999_MEM_COMPRESS);
  1455. if (lzo2a_999_compress ((lzo_bytep) buf, buf_len,
  1456. (lzo_bytep) mem, &mem_len,
  1457. work) == 0)
  1458. {
  1459. if (8 + mem_len > buf_len) {
  1460. method = NONE;
  1461. deflate = 0;
  1462. } else {
  1463. free (buf);
  1464. method = LZO;
  1465. deflate = buf_len;
  1466. buf_len = mem_len;
  1467. buf = mem;
  1468. assert(deflate);
  1469. }
  1470. }
  1471. else
  1472. {
  1473. free (buf);
  1474. buf = string->string;
  1475. buf_len = string->len;
  1476. }
  1477. free (work);
  1478. }
  1479. }
  1480. else
  1481. {
  1482. free (buf);
  1483. buf = string->string;
  1484. buf_len = string->len;
  1485. }
  1486. }
  1487. #endif
  1488. #endif
  1489. if (method == LZO) {
  1490. hdr = STRING_LZO;
  1491. u.u32 = to_be32 (buf_len);
  1492. hdr_len = 4;
  1493. } else {
  1494. #if WORDS_BIGENDIAN
  1495. if (buf_len <= UINT8_MAX) {
  1496. hdr = STRING_1;
  1497. u.u8 = buf_len;
  1498. hdr_len = 1;
  1499. } else if (buf_len <= UINT16_MAX) {
  1500. hdr = STRING_2_MSB;
  1501. u.u16 = buf_len;
  1502. hdr_len = 2;
  1503. } else {
  1504. hdr = STRING_4_MSB;
  1505. u.u32 = buf_len;
  1506. hdr_len = 4;
  1507. }
  1508. #else
  1509. if (buf_len <= UINT8_MAX) {
  1510. hdr = STRING_1;
  1511. u.u8 = buf_len;
  1512. hdr_len = 1;
  1513. } else if (buf_len <= UINT16_MAX) {
  1514. hdr = STRING_2_LSB;
  1515. u.u16 = buf_len;
  1516. hdr_len = 2;
  1517. } else {
  1518. hdr = STRING_4_LSB;
  1519. u.u32 = buf_len;
  1520. hdr_len = 4;
  1521. }
  1522. #endif
  1523. if (deflate) {
  1524. assert (method == ZLIB);
  1525. hdr |= STRING_DEFLATE;
  1526. }
  1527. }
  1528. closure->write_func (closure->closure, (unsigned char *) &hdr, 1);
  1529. closure->write_func (closure->closure, (unsigned char *) &u, hdr_len);
  1530. if (deflate) {
  1531. uint32_t u32 = to_be32 (deflate);
  1532. closure->write_func (closure->closure, (unsigned char *) &u32, 4);
  1533. }
  1534. closure->write_func (closure->closure, (unsigned char *) buf, buf_len);
  1535. if (buf != string->string)
  1536. free (buf);
  1537. return CSI_STATUS_SUCCESS;
  1538. }
  1539. static csi_status_t
  1540. _translate_push (csi_t *ctx, csi_object_t *obj)
  1541. {
  1542. struct _translate_closure *closure = ctx->scanner.closure;
  1543. if (0) {
  1544. fprintf (stderr, "push ");
  1545. fprintf_obj (stderr, ctx, obj);
  1546. }
  1547. switch (csi_object_get_type (obj)) {
  1548. case CSI_OBJECT_TYPE_NAME:
  1549. return _translate_name (ctx, obj->datum.name, FALSE, closure);
  1550. case CSI_OBJECT_TYPE_OPERATOR:
  1551. return _translate_operator (ctx, obj->datum.op, FALSE, closure);
  1552. case CSI_OBJECT_TYPE_INTEGER:
  1553. return _translate_integer (ctx, obj->datum.integer, closure);
  1554. case CSI_OBJECT_TYPE_REAL:
  1555. return _translate_real (ctx, obj->datum.real, closure);
  1556. case CSI_OBJECT_TYPE_STRING:
  1557. return _translate_string (ctx, obj->datum.string, closure);
  1558. case CSI_OBJECT_TYPE_NULL:
  1559. case CSI_OBJECT_TYPE_BOOLEAN:
  1560. case CSI_OBJECT_TYPE_MARK:
  1561. case CSI_OBJECT_TYPE_ARRAY:
  1562. case CSI_OBJECT_TYPE_DICTIONARY:
  1563. case CSI_OBJECT_TYPE_FILE:
  1564. case CSI_OBJECT_TYPE_MATRIX:
  1565. case CSI_OBJECT_TYPE_CONTEXT:
  1566. case CSI_OBJECT_TYPE_FONT:
  1567. case CSI_OBJECT_TYPE_PATTERN:
  1568. case CSI_OBJECT_TYPE_SCALED_FONT:
  1569. case CSI_OBJECT_TYPE_SURFACE:
  1570. longjmp (ctx->scanner.jump_buffer, _csi_error (CSI_STATUS_INVALID_SCRIPT));
  1571. break;
  1572. }
  1573. csi_object_free (ctx, obj);
  1574. return CSI_STATUS_SUCCESS;
  1575. }
  1576. static csi_status_t
  1577. _translate_execute (csi_t *ctx, csi_object_t *obj)
  1578. {
  1579. struct _translate_closure *closure = ctx->scanner.closure;
  1580. if (0) {
  1581. fprintf (stderr, "exec ");
  1582. fprintf_obj (stderr, ctx, obj);
  1583. }
  1584. switch (csi_object_get_type (obj)) {
  1585. case CSI_OBJECT_TYPE_NAME:
  1586. return _translate_name (ctx, obj->datum.name, TRUE, closure);
  1587. case CSI_OBJECT_TYPE_OPERATOR:
  1588. return _translate_operator (ctx, obj->datum.op, TRUE, closure);
  1589. case CSI_OBJECT_TYPE_INTEGER:
  1590. return _translate_integer (ctx, obj->datum.integer, closure);
  1591. case CSI_OBJECT_TYPE_REAL:
  1592. return _translate_real (ctx, obj->datum.real, closure);
  1593. case CSI_OBJECT_TYPE_STRING:
  1594. return _translate_string (ctx, obj->datum.string, closure);
  1595. case CSI_OBJECT_TYPE_NULL:
  1596. case CSI_OBJECT_TYPE_BOOLEAN:
  1597. case CSI_OBJECT_TYPE_MARK:
  1598. case CSI_OBJECT_TYPE_ARRAY:
  1599. case CSI_OBJECT_TYPE_DICTIONARY:
  1600. case CSI_OBJECT_TYPE_FILE:
  1601. case CSI_OBJECT_TYPE_MATRIX:
  1602. case CSI_OBJECT_TYPE_CONTEXT:
  1603. case CSI_OBJECT_TYPE_FONT:
  1604. case CSI_OBJECT_TYPE_PATTERN:
  1605. case CSI_OBJECT_TYPE_SCALED_FONT:
  1606. case CSI_OBJECT_TYPE_SURFACE:
  1607. longjmp (ctx->scanner.jump_buffer, _csi_error (CSI_STATUS_INVALID_SCRIPT));
  1608. break;
  1609. }
  1610. return CSI_STATUS_SUCCESS;
  1611. }
  1612. static csi_status_t
  1613. build_opcodes (csi_t *ctx, csi_dictionary_t **out)
  1614. {
  1615. csi_object_t obj;
  1616. csi_dictionary_t *dict;
  1617. const csi_operator_def_t *def;
  1618. csi_status_t status;
  1619. int opcode = OPCODE << 8;
  1620. status = csi_dictionary_new (ctx, &obj);
  1621. if (_csi_unlikely (status))
  1622. return status;
  1623. dict = obj.datum.dictionary;
  1624. csi_integer_new (&obj, opcode++);
  1625. status = csi_dictionary_put (ctx, dict, 0, &obj);
  1626. if (_csi_unlikely (status))
  1627. goto FAIL;
  1628. for (def = _csi_operators (); def->name != NULL; def++) {
  1629. csi_object_t name;
  1630. csi_dictionary_entry_t *entry;
  1631. int code;
  1632. entry = _csi_hash_table_lookup (&dict->hash_table,
  1633. (csi_hash_entry_t *) &def->op);
  1634. if (entry == NULL) {
  1635. code = opcode++;
  1636. csi_integer_new (&obj, code);
  1637. status = csi_dictionary_put (ctx, dict, (csi_name_t) def->op, &obj);
  1638. if (_csi_unlikely (status))
  1639. goto FAIL;
  1640. } else {
  1641. code = entry->value.datum.integer;
  1642. csi_integer_new (&obj, code);
  1643. }
  1644. assert (ctx->opcode[code & 0xff] == def->op);
  1645. status = csi_name_new_static (ctx, &name, def->name);
  1646. if (_csi_unlikely (status))
  1647. goto FAIL;
  1648. status = csi_dictionary_put (ctx, dict, name.datum.name, &obj);
  1649. if (_csi_unlikely (status))
  1650. goto FAIL;
  1651. }
  1652. *out = dict;
  1653. return CSI_STATUS_SUCCESS;
  1654. FAIL:
  1655. csi_dictionary_free (ctx, dict);
  1656. return status;
  1657. }
  1658. csi_status_t
  1659. _csi_translate_file (csi_t *ctx,
  1660. csi_file_t *file,
  1661. cairo_write_func_t write_func,
  1662. void *closure)
  1663. {
  1664. csi_status_t status;
  1665. struct _translate_closure translator;
  1666. if ((status = setjmp (ctx->scanner.jump_buffer)))
  1667. return status;
  1668. status = build_opcodes (ctx, &translator.opcodes);
  1669. if (_csi_unlikely (status))
  1670. return status;
  1671. translator.write_func = write_func;
  1672. translator.closure = closure;
  1673. ctx->scanner.closure = &translator;
  1674. ctx->scanner.bind = 1;
  1675. ctx->scanner.push = _translate_push;
  1676. ctx->scanner.execute = _translate_execute;
  1677. _scan_file (ctx, file);
  1678. ctx->scanner.bind = 0;
  1679. ctx->scanner.push = _scan_push;
  1680. ctx->scanner.execute = _scan_execute;
  1681. csi_dictionary_free (ctx, translator.opcodes);
  1682. return CSI_STATUS_SUCCESS;
  1683. }