Coverage Report

Created: 2026-11-30 06:23

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/cairo/util/cairo-script/cairo-script-interpreter.c
Line
Count
Source
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
35
#include "config.h"
36
37
#include "cairo.h"
38
39
#include "cairo-script-private.h"
40
41
#include <stdio.h>
42
#include <stdlib.h>
43
#include <string.h>
44
#include <math.h>
45
#include <assert.h>
46
47
#ifndef MAX
48
37.0k
#define MAX(a,b) (((a)>=(b))?(a):(b))
49
#endif
50
51
csi_status_t
52
_csi_error (csi_status_t status)
53
4.17k
{
54
4.17k
    return status;
55
4.17k
}
56
57
/* XXX track global/local memory, cap etc, mark/sweep GC */
58
void *
59
_csi_alloc (csi_t *ctx, int size)
60
3.83M
{
61
3.83M
    return malloc (size);
62
3.83M
}
63
64
void *
65
_csi_alloc0 (csi_t *ctx, int size)
66
0
{
67
0
    void *ptr;
68
69
0
    ptr = _csi_alloc (ctx, size);
70
0
    if (_csi_likely (ptr != NULL))
71
0
  memset (ptr, 0, size);
72
73
0
    return ptr;
74
0
}
75
76
void *
77
_csi_realloc (csi_t *ctx, void *ptr, int size)
78
2.74k
{
79
2.74k
    return realloc (ptr, size);
80
2.74k
}
81
82
void
83
_csi_free (csi_t *ctx, void *ptr)
84
3.80M
{
85
3.80M
    if (_csi_unlikely (ptr == NULL))
86
1
  return;
87
88
3.80M
    free (ptr);
89
3.80M
}
90
91
void *
92
_csi_perm_alloc (csi_t *ctx, int size)
93
1.87M
{
94
1.87M
    csi_chunk_t *chunk;
95
1.87M
    void *ptr;
96
97
1.87M
    size = (size + sizeof (void *)-1) & -sizeof (void *);
98
99
1.87M
    chunk = ctx->perm_chunk;
100
1.87M
    if (chunk == NULL || chunk->rem < size) {
101
13.2k
  int chunk_size = (size + 8191) & -8192;
102
13.2k
  chunk = _csi_alloc (ctx, sizeof (csi_chunk_t) + chunk_size);
103
13.2k
  if (_csi_unlikely (chunk == NULL))
104
0
      return NULL;
105
106
13.2k
  chunk->rem = chunk_size;
107
13.2k
  chunk->ptr = (char *) (chunk + 1);
108
13.2k
  chunk->next = ctx->perm_chunk;
109
13.2k
  ctx->perm_chunk = chunk;
110
13.2k
    }
111
112
1.87M
    ptr = chunk->ptr;
113
1.87M
    chunk->ptr += size;
114
1.87M
    chunk->rem -= size;
115
116
1.87M
    return ptr;
117
1.87M
}
118
119
void *
120
_csi_slab_alloc (csi_t *ctx, int size)
121
6.71M
{
122
#if CSI_DEBUG_MALLOC
123
    return malloc (size);
124
#else
125
6.71M
    int chunk_size;
126
6.71M
    csi_chunk_t *chunk;
127
6.71M
    void *ptr;
128
129
6.71M
    chunk_size = 2 * sizeof (void *);
130
6.71M
    chunk_size = (size + chunk_size - 1) / chunk_size;
131
132
6.71M
    if (ctx->slabs[chunk_size].free_list) {
133
625k
  ptr = ctx->slabs[chunk_size].free_list;
134
625k
  ctx->slabs[chunk_size].free_list = *(void **) ptr;
135
625k
  return ptr;
136
625k
    }
137
138
6.08M
    chunk = ctx->slabs[chunk_size].chunk;
139
6.08M
    if (chunk == NULL || ! chunk->rem) {
140
37.0k
  int cnt = MAX (128, 8192 / (chunk_size * 2 * sizeof (void *)));
141
142
37.0k
  chunk = _csi_alloc (ctx,
143
37.0k
          sizeof (csi_chunk_t) +
144
37.0k
          cnt * chunk_size * 2 * sizeof (void *));
145
37.0k
  if (_csi_unlikely (chunk == NULL))
146
0
      return NULL;
147
148
37.0k
  chunk->rem = cnt;
149
37.0k
  chunk->ptr = (char *) (chunk + 1);
150
37.0k
  chunk->next = ctx->slabs[chunk_size].chunk;
151
37.0k
  ctx->slabs[chunk_size].chunk = chunk;
152
37.0k
    }
153
154
6.08M
    ptr = chunk->ptr;
155
6.08M
    chunk->ptr += chunk_size * 2 * sizeof (void *);
156
6.08M
    chunk->rem--;
157
158
6.08M
    return ptr;
159
6.08M
#endif
160
6.08M
}
161
162
void
163
_csi_slab_free (csi_t *ctx, void *ptr, int size)
164
6.68M
{
165
6.68M
    int chunk_size;
166
6.68M
    void **free_list;
167
168
6.68M
    if (_csi_unlikely (ptr == NULL))
169
0
  return;
170
171
#if CSI_DEBUG_MALLOC
172
    free (ptr);
173
#else
174
6.68M
    chunk_size = 2 * sizeof (void *);
175
6.68M
    chunk_size = (size + chunk_size - 1) / chunk_size;
176
177
6.68M
    free_list = ptr;
178
6.68M
    *free_list = ctx->slabs[chunk_size].free_list;
179
6.68M
    ctx->slabs[chunk_size].free_list = ptr;
180
6.68M
#endif
181
6.68M
}
182
183
csi_status_t
184
_csi_stack_push (csi_t *ctx, csi_stack_t *stack,
185
     const csi_object_t *obj)
186
18.0M
{
187
18.0M
    if (_csi_unlikely (stack->len == stack->size))
188
2.30k
  return _csi_stack_push_internal (ctx, stack, obj);
189
190
18.0M
    stack->objects[stack->len++] = *obj;
191
18.0M
    return CSI_STATUS_SUCCESS;
192
18.0M
}
193
194
static void
195
_csi_perm_fini (csi_t *ctx)
196
6.55k
{
197
19.8k
    while (ctx->perm_chunk != NULL) {
198
13.2k
  csi_chunk_t *chunk = ctx->perm_chunk;
199
13.2k
  ctx->perm_chunk = chunk->next;
200
13.2k
  _csi_free (ctx, chunk);
201
13.2k
    }
202
6.55k
}
203
204
static void
205
_csi_slab_fini (csi_t *ctx)
206
6.55k
{
207
6.55k
    unsigned int i;
208
209
111k
    for (i = 0; i < sizeof (ctx->slabs) / sizeof (ctx->slabs[0]); i++) {
210
141k
  while (ctx->slabs[i].chunk != NULL) {
211
37.0k
      csi_chunk_t *chunk = ctx->slabs[i].chunk;
212
37.0k
      ctx->slabs[i].chunk = chunk->next;
213
37.0k
      _csi_free (ctx, chunk);
214
37.0k
  }
215
104k
    }
216
6.55k
}
217
218
static csi_status_t
219
_add_operator (csi_t *ctx,
220
         csi_dictionary_t *dict,
221
         const csi_operator_def_t *def)
222
1.27M
{
223
1.27M
    csi_object_t name;
224
1.27M
    csi_object_t operator;
225
1.27M
    csi_status_t status;
226
227
1.27M
    status = csi_name_new_static (ctx, &name, def->name);
228
1.27M
    if (status)
229
0
  return status;
230
231
1.27M
    csi_operator_new (&operator, def->op);
232
233
1.27M
    return csi_dictionary_put (ctx, dict, name.datum.name, &operator);
234
1.27M
}
235
236
static csi_status_t
237
_add_integer_constant (csi_t *ctx,
238
           csi_dictionary_t *dict,
239
           const csi_integer_constant_def_t *def)
240
563k
{
241
563k
    csi_object_t name;
242
563k
    csi_object_t constant;
243
563k
    csi_status_t status;
244
245
563k
    status = csi_name_new_static (ctx, &name, def->name);
246
563k
    if (status)
247
0
  return status;
248
249
563k
    csi_integer_new (&constant, def->value);
250
251
563k
    return csi_dictionary_put (ctx, dict, name.datum.name, &constant);
252
563k
}
253
254
static csi_status_t
255
_add_real_constant (csi_t *ctx,
256
        csi_dictionary_t *dict,
257
        const csi_real_constant_def_t *def)
258
26.2k
{
259
26.2k
    csi_object_t name;
260
26.2k
    csi_object_t constant;
261
26.2k
    csi_status_t status;
262
263
26.2k
    status = csi_name_new_static (ctx, &name, def->name);
264
26.2k
    if (status)
265
0
  return status;
266
267
26.2k
    csi_real_new (&constant, def->value);
268
269
26.2k
    return csi_dictionary_put (ctx, dict, name.datum.name, &constant);
270
26.2k
}
271
272
static csi_status_t
273
_init_dictionaries (csi_t *ctx)
274
6.55k
{
275
6.55k
    csi_status_t status;
276
6.55k
    csi_stack_t *stack;
277
6.55k
    csi_object_t obj;
278
6.55k
    csi_dictionary_t *dict, *opcodes;
279
6.55k
    const csi_operator_def_t *odef;
280
6.55k
    const csi_integer_constant_def_t *idef;
281
6.55k
    const csi_real_constant_def_t *rdef;
282
6.55k
    unsigned n;
283
284
6.55k
    stack = &ctx->dstack;
285
286
6.55k
    status = _csi_stack_init (ctx, stack, 4);
287
6.55k
    if (_csi_unlikely (status))
288
0
  return status;
289
290
    /* systemdict */
291
6.55k
    status = csi_dictionary_new (ctx, &obj);
292
6.55k
    if (_csi_unlikely (status))
293
0
  return status;
294
295
6.55k
    status = _csi_stack_push (ctx, stack, &obj);
296
6.55k
    if (_csi_unlikely (status))
297
0
  return status;
298
299
6.55k
    dict = obj.datum.dictionary;
300
301
6.55k
    status = csi_dictionary_new (ctx, &obj);
302
6.55k
    if (_csi_unlikely (status))
303
0
  return status;
304
305
6.55k
    opcodes = obj.datum.dictionary;
306
307
6.55k
    n = 0;
308
6.55k
    csi_integer_new (&obj, n);
309
6.55k
    status = csi_dictionary_put (ctx, opcodes, 0, &obj);
310
6.55k
    if (_csi_unlikely (status))
311
0
  return status;
312
6.55k
    ctx->opcode[n++] = NULL;
313
314
    /* fill systemdict with operators */
315
1.28M
    for (odef = _csi_operators (); odef->name != NULL; odef++) {
316
1.27M
  status = _add_operator (ctx, dict, odef);
317
1.27M
  if (_csi_unlikely (status))
318
0
      return status;
319
320
1.27M
  if (! csi_dictionary_has (opcodes, (csi_name_t) odef->op)) {
321
911k
      csi_integer_new (&obj, n);
322
911k
      status = csi_dictionary_put (ctx,
323
911k
                             opcodes, (csi_name_t) odef->op, &obj);
324
911k
      if (_csi_unlikely (status))
325
0
    return status;
326
327
911k
      assert (n < sizeof (ctx->opcode) / sizeof (ctx->opcode[0]));
328
911k
      ctx->opcode[n++] = odef->op;
329
911k
  }
330
1.27M
    }
331
6.55k
    csi_dictionary_free (ctx, opcodes);
332
333
    /* add constants */
334
570k
    for (idef = _csi_integer_constants (); idef->name != NULL; idef++) {
335
563k
  status = _add_integer_constant (ctx, dict, idef);
336
563k
  if (_csi_unlikely (status))
337
0
      return status;
338
563k
    }
339
32.7k
    for (rdef = _csi_real_constants (); rdef->name != NULL; rdef++) {
340
26.2k
  status = _add_real_constant (ctx, dict, rdef);
341
26.2k
  if (_csi_unlikely (status))
342
0
      return status;
343
26.2k
    }
344
345
    /* and seal */
346
    //dict.type &= ~CSI_OBJECT_ATTR_WRITABLE;
347
348
349
    /* globaldict */
350
6.55k
    status = csi_dictionary_new (ctx, &obj);
351
6.55k
    if (_csi_unlikely (status))
352
0
  return status;
353
6.55k
    status = _csi_stack_push (ctx, stack, &obj);
354
6.55k
    if (_csi_unlikely (status))
355
0
  return status;
356
357
    /* userdict */
358
6.55k
    status = csi_dictionary_new (ctx, &obj);
359
6.55k
    if (_csi_unlikely (status))
360
0
  return status;
361
6.55k
    status = _csi_stack_push (ctx, stack, &obj);
362
6.55k
    if (_csi_unlikely (status))
363
0
  return status;
364
365
6.55k
    return CSI_STATUS_SUCCESS;
366
6.55k
}
367
368
/* intern string */
369
370
typedef struct _cairo_intern_string {
371
    csi_hash_entry_t hash_entry;
372
    int len;
373
    char *string;
374
} csi_intern_string_t;
375
376
static unsigned long
377
_intern_string_hash (const char *str, int len)
378
14.0M
{
379
14.0M
    const signed char *p = (const signed char *) str;
380
14.0M
    if (len > 0) {
381
14.0M
  unsigned int h = *p;
382
383
46.9M
  while (--len)
384
32.9M
      h = (h << 5) - h + *++p;
385
386
14.0M
  return h;
387
14.0M
    }
388
1.18k
    return 0;
389
14.0M
}
390
391
static cairo_bool_t
392
_intern_string_equal (const void *_a, const void *_b)
393
12.2M
{
394
12.2M
    const csi_intern_string_t *a = _a;
395
12.2M
    const csi_intern_string_t *b = _b;
396
397
12.2M
    if (a->len != b->len)
398
342
  return FALSE;
399
400
12.2M
    return memcmp (a->string, b->string, a->len) == 0;
401
12.2M
}
402
403
static void
404
_csi_init (csi_t *ctx)
405
6.55k
{
406
6.55k
    csi_status_t status;
407
408
6.55k
    memset (ctx, 0, sizeof (*ctx));
409
410
6.55k
    ctx->status = CSI_STATUS_SUCCESS;
411
6.55k
    ctx->ref_count = 1;
412
6.55k
    ctx->scanner.line_number = -1;
413
414
6.55k
    status = _csi_hash_table_init (&ctx->strings, _intern_string_equal);
415
6.55k
    if (status)
416
0
  goto FAIL;
417
418
6.55k
    status = _csi_stack_init (ctx, &ctx->ostack, 2048);
419
6.55k
    if (status)
420
0
  goto FAIL;
421
6.55k
    status = _init_dictionaries (ctx);
422
6.55k
    if (status)
423
0
  goto FAIL;
424
425
6.55k
    status = _csi_scanner_init (ctx, &ctx->scanner);
426
6.55k
    if (status)
427
0
  goto FAIL;
428
429
6.55k
    return;
430
431
6.55k
FAIL:
432
0
    if (ctx->status == CSI_STATUS_SUCCESS)
433
0
  ctx->status = status;
434
0
}
435
436
static void
437
_csi_finish (csi_t *ctx)
438
6.55k
{
439
6.55k
    _csi_stack_fini (ctx, &ctx->ostack);
440
6.55k
    _csi_stack_fini (ctx, &ctx->dstack);
441
6.55k
    _csi_scanner_fini (ctx, &ctx->scanner);
442
443
6.55k
    _csi_hash_table_fini (&ctx->strings);
444
6.55k
}
445
446
csi_status_t
447
_csi_name_define (csi_t *ctx, csi_name_t name, csi_object_t *obj)
448
5.36k
{
449
5.36k
    return csi_dictionary_put (ctx,
450
5.36k
      ctx->dstack.objects[ctx->dstack.len-1].datum.dictionary,
451
5.36k
      name,
452
5.36k
      obj);
453
5.36k
}
454
455
csi_status_t
456
_csi_name_lookup (csi_t *ctx, csi_name_t name, csi_object_t *obj)
457
10.7M
{
458
10.7M
    int i;
459
460
32.2M
    for (i = ctx->dstack.len; i--; ) {
461
32.2M
  csi_dictionary_t *dict;
462
32.2M
  csi_dictionary_entry_t *entry;
463
464
32.2M
  dict = ctx->dstack.objects[i].datum.dictionary;
465
32.2M
  entry = _csi_hash_table_lookup (&dict->hash_table,
466
32.2M
          (csi_hash_entry_t *) &name);
467
32.2M
  if (entry != NULL) {
468
10.7M
      *obj = entry->value;
469
10.7M
      return CSI_STATUS_SUCCESS;
470
10.7M
  }
471
32.2M
    }
472
473
1.11k
    return _csi_error (CSI_STATUS_INVALID_SCRIPT);
474
10.7M
}
475
476
csi_status_t
477
_csi_name_undefine (csi_t *ctx, csi_name_t name)
478
1
{
479
1
    unsigned int i;
480
481
3
    for (i = ctx->dstack.len; --i; ) {
482
2
  if (csi_dictionary_has (ctx->dstack.objects[i].datum.dictionary,
483
2
        name))
484
0
  {
485
0
      csi_dictionary_remove (ctx,
486
0
           ctx->dstack.objects[i].datum.dictionary,
487
0
           name);
488
0
      return CSI_STATUS_SUCCESS;
489
0
  }
490
2
    }
491
492
1
    return _csi_error (CSI_STATUS_INVALID_SCRIPT);
493
1
}
494
495
csi_status_t
496
_csi_intern_string (csi_t *ctx, const char **str_inout, int len)
497
14.0M
{
498
14.0M
    char *str = (char *) *str_inout;
499
14.0M
    csi_intern_string_t tmpl, *istring;
500
14.0M
    csi_status_t status = CSI_STATUS_SUCCESS;
501
502
14.0M
    tmpl.hash_entry.hash = _intern_string_hash (str, len);
503
14.0M
    tmpl.len = len;
504
14.0M
    tmpl.string = (char *) str;
505
506
14.0M
    istring = _csi_hash_table_lookup (&ctx->strings, &tmpl.hash_entry);
507
14.0M
    if (istring == NULL) {
508
1.87M
  istring = _csi_perm_alloc (ctx,
509
1.87M
           sizeof (csi_intern_string_t) + len + 1);
510
1.87M
  if (istring != NULL) {
511
1.87M
      istring->hash_entry.hash = tmpl.hash_entry.hash;
512
1.87M
      istring->len = tmpl.len;
513
1.87M
      istring->string = (char *) (istring + 1);
514
1.87M
      memcpy (istring->string, str, len);
515
1.87M
      istring->string[len] = '\0';
516
517
1.87M
      status = _csi_hash_table_insert (&ctx->strings,
518
1.87M
               &istring->hash_entry);
519
1.87M
      if (_csi_unlikely (status)) {
520
0
    _csi_free (ctx, istring);
521
0
    return status;
522
0
      }
523
1.87M
  } else
524
0
      return _csi_error (CSI_STATUS_NO_MEMORY);
525
1.87M
    }
526
527
14.0M
    *str_inout = istring->string;
528
14.0M
    return CSI_STATUS_SUCCESS;
529
14.0M
}
530
531
/* Public */
532
533
static csi_t _csi_nil = { -1, CSI_STATUS_NO_MEMORY };
534
535
csi_t *
536
cairo_script_interpreter_create (void)
537
6.55k
{
538
6.55k
    csi_t *ctx;
539
540
6.55k
    ctx = malloc (sizeof (csi_t));
541
6.55k
    if (ctx == NULL)
542
0
  return (csi_t *) &_csi_nil;
543
544
6.55k
    _csi_init (ctx);
545
546
6.55k
    return ctx;
547
6.55k
}
548
549
void
550
cairo_script_interpreter_install_hooks (csi_t *ctx,
551
          const csi_hooks_t *hooks)
552
6.55k
{
553
6.55k
    if (ctx->status)
554
0
  return;
555
556
6.55k
    ctx->hooks = *hooks;
557
6.55k
}
558
559
cairo_status_t
560
cairo_script_interpreter_run (csi_t *ctx, const char *filename)
561
0
{
562
0
    csi_object_t file;
563
564
0
    if (ctx->status)
565
0
  return ctx->status;
566
0
    if (ctx->finished)
567
0
  return ctx->status = CSI_STATUS_INTERPRETER_FINISHED;
568
569
0
    ctx->status = csi_file_new (ctx, &file, filename, "r");
570
0
    if (ctx->status)
571
0
  return ctx->status;
572
573
0
    file.type |= CSI_OBJECT_ATTR_EXECUTABLE;
574
575
0
    ctx->status = csi_object_execute (ctx, &file);
576
0
    csi_object_free (ctx, &file);
577
578
0
    return ctx->status;
579
0
}
580
581
cairo_status_t
582
cairo_script_interpreter_feed_stream (csi_t *ctx, FILE *stream)
583
0
{
584
0
    csi_object_t file;
585
586
0
    if (ctx->status)
587
0
  return ctx->status;
588
0
    if (ctx->finished)
589
0
  return ctx->status = CSI_STATUS_INTERPRETER_FINISHED;
590
591
0
    ctx->status = csi_file_new_for_stream (ctx, &file, stream);
592
0
    if (ctx->status)
593
0
  return ctx->status;
594
595
0
    file.type |= CSI_OBJECT_ATTR_EXECUTABLE;
596
597
0
    ctx->status = csi_object_execute (ctx, &file);
598
0
    csi_object_free (ctx, &file);
599
600
0
    return ctx->status;
601
0
}
602
603
cairo_status_t
604
cairo_script_interpreter_feed_string (csi_t *ctx, const char *line, int len)
605
6.55k
{
606
6.55k
    csi_object_t file;
607
608
6.55k
    if (ctx->status)
609
0
  return ctx->status;
610
6.55k
    if (ctx->finished)
611
0
  return ctx->status = CSI_STATUS_INTERPRETER_FINISHED;
612
613
6.55k
    if (len < 0)
614
0
  len = strlen (line);
615
6.55k
    ctx->status = csi_file_new_for_bytes (ctx, &file, line, len);
616
6.55k
    if (ctx->status)
617
0
  return ctx->status;
618
619
6.55k
    file.type |= CSI_OBJECT_ATTR_EXECUTABLE;
620
621
6.55k
    ctx->status = csi_object_execute (ctx, &file);
622
6.55k
    csi_object_free (ctx, &file);
623
624
6.55k
    return ctx->status;
625
6.55k
}
626
627
unsigned int
628
cairo_script_interpreter_get_line_number (csi_t *ctx)
629
0
{
630
0
    return ctx->scanner.line_number + 1; /* 1 index based */
631
0
}
632
633
csi_t *
634
cairo_script_interpreter_reference (csi_t *ctx)
635
0
{
636
0
    ctx->ref_count++;
637
0
    return ctx;
638
0
}
639
640
cairo_status_t
641
cairo_script_interpreter_finish (csi_t *ctx)
642
6.55k
{
643
6.55k
    csi_status_t status;
644
645
6.55k
    status = ctx->status;
646
6.55k
    if (! ctx->finished) {
647
6.55k
  _csi_finish (ctx);
648
6.55k
  ctx->finished = 1;
649
6.55k
    } else if (status == CSI_STATUS_SUCCESS) {
650
0
  status = ctx->status = CSI_STATUS_INTERPRETER_FINISHED;
651
0
    }
652
653
6.55k
    return status;
654
6.55k
}
655
656
static void
657
_csi_fini (csi_t *ctx)
658
6.55k
{
659
6.55k
    if (! ctx->finished)
660
0
  _csi_finish (ctx);
661
662
6.55k
    if (ctx->free_array != NULL)
663
848
  csi_array_free (ctx, ctx->free_array);
664
6.55k
    if (ctx->free_dictionary != NULL)
665
6.55k
  csi_dictionary_free (ctx, ctx->free_dictionary);
666
6.55k
    if (ctx->free_string != NULL)
667
610
  csi_string_free (ctx, ctx->free_string);
668
669
6.55k
    _csi_slab_fini (ctx);
670
6.55k
    _csi_perm_fini (ctx);
671
6.55k
}
672
673
cairo_status_t
674
cairo_script_interpreter_destroy (csi_t *ctx)
675
6.55k
{
676
6.55k
    csi_status_t status;
677
678
6.55k
    status = ctx->status;
679
6.55k
    if (--ctx->ref_count)
680
0
  return status;
681
682
6.55k
    _csi_fini (ctx);
683
6.55k
    free (ctx);
684
685
6.55k
    return status;
686
6.55k
}
687
688
cairo_status_t
689
cairo_script_interpreter_translate_stream (FILE *stream,
690
                                     cairo_write_func_t write_func,
691
             void *closure)
692
0
{
693
0
    csi_t ctx;
694
0
    csi_object_t src;
695
0
    csi_status_t status;
696
697
0
    _csi_init (&ctx);
698
699
0
    status = csi_file_new_for_stream (&ctx, &src, stream);
700
0
    if (status)
701
0
  goto BAIL;
702
703
0
    status = _csi_translate_file (&ctx, src.datum.file, write_func, closure);
704
705
0
BAIL:
706
0
    csi_object_free (&ctx, &src);
707
0
    _csi_fini (&ctx);
708
709
0
    return status;
710
0
}