Coverage Report

Created: 2025-11-16 09:57

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/workdir/UnpackedTarball/cairo/src/cairo-output-stream.c
Line
Count
Source
1
/* -*- Mode: c; tab-width: 8; c-basic-offset: 4; indent-tabs-mode: t; -*- */
2
/* cairo-output-stream.c: Output stream abstraction
3
 *
4
 * Copyright © 2005 Red Hat, Inc
5
 *
6
 * This library is free software; you can redistribute it and/or
7
 * modify it either under the terms of the GNU Lesser General Public
8
 * License version 2.1 as published by the Free Software Foundation
9
 * (the "LGPL") or, at your option, under the terms of the Mozilla
10
 * Public License Version 1.1 (the "MPL"). If you do not alter this
11
 * notice, a recipient may use your version of this file under either
12
 * the MPL or the LGPL.
13
 *
14
 * You should have received a copy of the LGPL along with this library
15
 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
16
 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA
17
 * You should have received a copy of the MPL along with this library
18
 * in the file COPYING-MPL-1.1
19
 *
20
 * The contents of this file are subject to the Mozilla Public License
21
 * Version 1.1 (the "License"); you may not use this file except in
22
 * compliance with the License. You may obtain a copy of the License at
23
 * http://www.mozilla.org/MPL/
24
 *
25
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
26
 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
27
 * the specific language governing rights and limitations.
28
 *
29
 * The Original Code is the cairo graphics library.
30
 *
31
 * The Initial Developer of the Original Code is Red Hat, Inc.
32
 *
33
 * Author(s):
34
 *  Kristian Høgsberg <krh@redhat.com>
35
 */
36
37
#define _DEFAULT_SOURCE /* for snprintf() */
38
#include "cairoint.h"
39
40
#include "cairo-output-stream-private.h"
41
42
#include "cairo-array-private.h"
43
#include "cairo-error-private.h"
44
#include "cairo-compiler-private.h"
45
46
#include <stdio.h>
47
#include <errno.h>
48
49
/* Numbers printed with %f are printed with this number of significant
50
 * digits after the decimal.
51
 */
52
0
#define SIGNIFICANT_DIGITS_AFTER_DECIMAL 6
53
54
/* Numbers printed with %g are assumed to only have %CAIRO_FIXED_FRAC_BITS
55
 * bits of precision available after the decimal point.
56
 *
57
 * FIXED_POINT_DECIMAL_DIGITS specifies the minimum number of decimal
58
 * digits after the decimal point required to preserve the available
59
 * precision.
60
 *
61
 * The conversion is:
62
 *
63
 * <programlisting>
64
 * FIXED_POINT_DECIMAL_DIGITS = ceil( CAIRO_FIXED_FRAC_BITS * ln(2)/ln(10) )
65
 * </programlisting>
66
 *
67
 * We can replace ceil(x) with (int)(x+1) since x will never be an
68
 * integer for any likely value of %CAIRO_FIXED_FRAC_BITS.
69
 */
70
0
#define FIXED_POINT_DECIMAL_DIGITS ((int)(CAIRO_FIXED_FRAC_BITS*0.301029996 + 1))
71
72
void
73
_cairo_output_stream_init (cairo_output_stream_t            *stream,
74
         cairo_output_stream_write_func_t  write_func,
75
         cairo_output_stream_flush_func_t  flush_func,
76
         cairo_output_stream_close_func_t  close_func)
77
2
{
78
2
    stream->write_func = write_func;
79
2
    stream->flush_func = flush_func;
80
2
    stream->close_func = close_func;
81
2
    stream->position = 0;
82
2
    stream->status = CAIRO_STATUS_SUCCESS;
83
2
    stream->closed = FALSE;
84
2
}
85
86
cairo_status_t
87
_cairo_output_stream_fini (cairo_output_stream_t *stream)
88
2
{
89
2
    return _cairo_output_stream_close (stream);
90
2
}
91
92
const cairo_output_stream_t _cairo_output_stream_nil = {
93
    NULL, /* write_func */
94
    NULL, /* flush_func */
95
    NULL, /* close_func */
96
    0,    /* position */
97
    CAIRO_STATUS_NO_MEMORY,
98
    FALSE /* closed */
99
};
100
101
static const cairo_output_stream_t _cairo_output_stream_nil_write_error = {
102
    NULL, /* write_func */
103
    NULL, /* flush_func */
104
    NULL, /* close_func */
105
    0,    /* position */
106
    CAIRO_STATUS_WRITE_ERROR,
107
    FALSE /* closed */
108
};
109
110
typedef struct _cairo_output_stream_with_closure {
111
    cairo_output_stream_t  base;
112
    cairo_write_func_t     write_func;
113
    cairo_close_func_t     close_func;
114
    void      *closure;
115
} cairo_output_stream_with_closure_t;
116
117
118
static cairo_status_t
119
closure_write (cairo_output_stream_t *stream,
120
         const unsigned char *data, unsigned int length)
121
0
{
122
0
    cairo_output_stream_with_closure_t *stream_with_closure =
123
0
  (cairo_output_stream_with_closure_t *) stream;
124
125
0
    if (stream_with_closure->write_func == NULL)
126
0
  return CAIRO_STATUS_SUCCESS;
127
128
0
    return stream_with_closure->write_func (stream_with_closure->closure,
129
0
              data, length);
130
0
}
131
132
static cairo_status_t
133
closure_close (cairo_output_stream_t *stream)
134
0
{
135
0
    cairo_output_stream_with_closure_t *stream_with_closure =
136
0
  (cairo_output_stream_with_closure_t *) stream;
137
138
0
    if (stream_with_closure->close_func != NULL)
139
0
  return stream_with_closure->close_func (stream_with_closure->closure);
140
0
    else
141
0
  return CAIRO_STATUS_SUCCESS;
142
0
}
143
144
cairo_output_stream_t *
145
_cairo_output_stream_create (cairo_write_func_t   write_func,
146
           cairo_close_func_t   close_func,
147
           void     *closure)
148
0
{
149
0
    cairo_output_stream_with_closure_t *stream;
150
151
0
    stream = _cairo_calloc (sizeof (cairo_output_stream_with_closure_t));
152
0
    if (unlikely (stream == NULL)) {
153
0
  _cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
154
0
  return (cairo_output_stream_t *) &_cairo_output_stream_nil;
155
0
    }
156
157
0
    _cairo_output_stream_init (&stream->base,
158
0
             closure_write, NULL, closure_close);
159
0
    stream->write_func = write_func;
160
0
    stream->close_func = close_func;
161
0
    stream->closure = closure;
162
163
0
    return &stream->base;
164
0
}
165
166
cairo_output_stream_t *
167
_cairo_output_stream_create_in_error (cairo_status_t status)
168
0
{
169
0
    cairo_output_stream_t *stream;
170
171
    /* check for the common ones */
172
0
    if (status == CAIRO_STATUS_NO_MEMORY)
173
0
  return (cairo_output_stream_t *) &_cairo_output_stream_nil;
174
0
    if (status == CAIRO_STATUS_WRITE_ERROR)
175
0
  return (cairo_output_stream_t *) &_cairo_output_stream_nil_write_error;
176
177
0
    stream = _cairo_calloc (sizeof (cairo_output_stream_t));
178
0
    if (unlikely (stream == NULL)) {
179
0
  _cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
180
0
  return (cairo_output_stream_t *) &_cairo_output_stream_nil;
181
0
    }
182
183
0
    _cairo_output_stream_init (stream, NULL, NULL, NULL);
184
0
    stream->status = status;
185
186
0
    return stream;
187
0
}
188
189
cairo_status_t
190
_cairo_output_stream_flush (cairo_output_stream_t *stream)
191
0
{
192
0
    cairo_status_t status;
193
194
0
    if (stream->closed)
195
0
  return stream->status;
196
197
0
    if (stream == &_cairo_output_stream_nil ||
198
0
  stream == &_cairo_output_stream_nil_write_error)
199
0
    {
200
0
  return stream->status;
201
0
    }
202
203
0
    if (stream->flush_func) {
204
0
  status = stream->flush_func (stream);
205
  /* Don't overwrite a pre-existing status failure. */
206
0
  if (stream->status == CAIRO_STATUS_SUCCESS)
207
0
      stream->status = status;
208
0
    }
209
210
0
    return stream->status;
211
0
}
212
213
cairo_status_t
214
_cairo_output_stream_close (cairo_output_stream_t *stream)
215
2
{
216
2
    cairo_status_t status;
217
218
2
    if (stream->closed)
219
0
  return stream->status;
220
221
2
    if (stream == &_cairo_output_stream_nil ||
222
2
  stream == &_cairo_output_stream_nil_write_error)
223
0
    {
224
0
  return stream->status;
225
0
    }
226
227
2
    if (stream->close_func) {
228
0
  status = stream->close_func (stream);
229
  /* Don't overwrite a pre-existing status failure. */
230
0
  if (stream->status == CAIRO_STATUS_SUCCESS)
231
0
      stream->status = status;
232
0
    }
233
234
2
    stream->closed = TRUE;
235
236
2
    return stream->status;
237
2
}
238
239
cairo_status_t
240
_cairo_output_stream_destroy (cairo_output_stream_t *stream)
241
2
{
242
2
    cairo_status_t status;
243
244
2
    assert (stream != NULL);
245
246
2
    if (stream == &_cairo_output_stream_nil ||
247
2
  stream == &_cairo_output_stream_nil_write_error)
248
0
    {
249
0
  return stream->status;
250
0
    }
251
252
2
    status = _cairo_output_stream_fini (stream);
253
2
    free (stream);
254
255
2
    return status;
256
2
}
257
258
void
259
_cairo_output_stream_write (cairo_output_stream_t *stream,
260
          const void *data, size_t length)
261
20
{
262
20
    if (length == 0 || stream->status)
263
4
  return;
264
265
16
    if (stream->closed) {
266
0
  stream->status = CAIRO_STATUS_WRITE_ERROR;
267
0
  return;
268
0
    }
269
270
16
    stream->status = stream->write_func (stream, data, length);
271
16
    stream->position += length;
272
16
}
273
274
void
275
_cairo_output_stream_write_hex_string (cairo_output_stream_t *stream,
276
               const unsigned char *data,
277
               size_t length)
278
0
{
279
0
    const char hex_chars[] = "0123456789abcdef";
280
0
    char buffer[2];
281
0
    unsigned int i, column;
282
283
0
    for (i = 0, column = 0; i < length; i++, column++) {
284
0
  if (column == 38) {
285
0
      _cairo_output_stream_write (stream, "\n", 1);
286
0
      column = 0;
287
0
  }
288
0
  buffer[0] = hex_chars[(data[i] >> 4) & 0x0f];
289
0
  buffer[1] = hex_chars[data[i] & 0x0f];
290
0
  _cairo_output_stream_write (stream, buffer, 2);
291
0
    }
292
0
}
293
294
/* Format a double in a locale independent way and trim trailing
295
 * zeros.  Based on code from Alex Larson <alexl@redhat.com>.
296
 * https://mail.gnome.org/archives/gtk-devel-list/2001-October/msg00087.html
297
 *
298
 * The code in the patch is copyright Red Hat, Inc under the LGPL, but
299
 * has been relicensed under the LGPL/MPL dual license for inclusion
300
 * into cairo (see COPYING). -- Kristian Høgsberg <krh@redhat.com>
301
 */
302
static void
303
_cairo_dtostr (char *buffer, size_t size, double d, cairo_bool_t limited_precision)
304
8
{
305
8
    const char *decimal_point;
306
8
    int decimal_point_len;
307
8
    char *p;
308
8
    int decimal_len;
309
8
    int num_zeros, decimal_digits;
310
311
    /* Omit the minus sign from negative zero. */
312
8
    if (d == 0.0)
313
0
  d = 0.0;
314
315
8
    decimal_point = _cairo_get_locale_decimal_point ();
316
8
    decimal_point_len = strlen (decimal_point);
317
318
8
    assert (decimal_point_len != 0);
319
320
8
    if (limited_precision) {
321
0
  snprintf (buffer, size, "%.*f", FIXED_POINT_DECIMAL_DIGITS, d);
322
8
    } else {
323
  /* Using "%f" to print numbers less than 0.1 will result in
324
   * reduced precision due to the default 6 digits after the
325
   * decimal point.
326
   *
327
   * For numbers is < 0.1, we print with maximum precision and count
328
   * the number of zeros between the decimal point and the first
329
   * significant digit. We then print the number again with the
330
   * number of decimal places that gives us the required number of
331
   * significant digits. This ensures the number is correctly
332
   * rounded.
333
   */
334
8
  if (fabs (d) >= 0.1) {
335
8
      snprintf (buffer, size, "%f", d);
336
8
  } else {
337
0
      snprintf (buffer, size, "%.18f", d);
338
0
      p = buffer;
339
340
0
      if (*p == '+' || *p == '-')
341
0
    p++;
342
343
0
      while (_cairo_isdigit (*p))
344
0
    p++;
345
346
0
      if (strncmp (p, decimal_point, decimal_point_len) == 0)
347
0
    p += decimal_point_len;
348
349
0
      num_zeros = 0;
350
0
      while (*p++ == '0')
351
0
    num_zeros++;
352
353
0
      decimal_digits = num_zeros + SIGNIFICANT_DIGITS_AFTER_DECIMAL;
354
355
0
      if (decimal_digits < 18)
356
0
    snprintf (buffer, size, "%.*f", decimal_digits, d);
357
0
  }
358
8
    }
359
8
    p = buffer;
360
361
8
    if (*p == '+' || *p == '-')
362
0
  p++;
363
364
16
    while (_cairo_isdigit (*p))
365
8
  p++;
366
367
8
    if (strncmp (p, decimal_point, decimal_point_len) == 0) {
368
8
  *p = '.';
369
8
  decimal_len = strlen (p + decimal_point_len);
370
8
  memmove (p + 1, p + decimal_point_len, decimal_len);
371
8
  p[1 + decimal_len] = 0;
372
373
  /* Remove trailing zeros and decimal point if possible. */
374
56
  for (p = p + decimal_len; *p == '0'; p--)
375
48
      *p = 0;
376
377
8
  if (*p == '.') {
378
8
      *p = 0;
379
8
      p--;
380
8
  }
381
8
    }
382
8
}
383
384
enum {
385
    LENGTH_MODIFIER_LONG = 0x100,
386
    LENGTH_MODIFIER_LONG_LONG = 0x200
387
};
388
389
/* Here's a limited reimplementation of printf.  The reason for doing
390
 * this is primarily to special case handling of doubles.  We want
391
 * locale independent formatting of doubles and we want to trim
392
 * trailing zeros.  This is handled by dtostr() above, and the code
393
 * below handles everything else by calling snprintf() to do the
394
 * formatting.  This functionality is only for internal use and we
395
 * only implement the formats we actually use.
396
 */
397
void
398
_cairo_output_stream_vprintf (cairo_output_stream_t *stream,
399
            const char *fmt, va_list ap)
400
4
{
401
4
#define SINGLE_FMT_BUFFER_SIZE 32
402
4
    char buffer[512], single_fmt[SINGLE_FMT_BUFFER_SIZE];
403
4
    int single_fmt_length;
404
4
    char *p;
405
4
    const char *f, *start;
406
4
    int length_modifier, width;
407
4
    cairo_bool_t var_width;
408
409
4
    f = fmt;
410
4
    p = buffer;
411
346
    while (*f != '\0') {
412
342
  if (p == buffer + sizeof (buffer)) {
413
0
      _cairo_output_stream_write (stream, buffer, sizeof (buffer));
414
0
      p = buffer;
415
0
  }
416
417
342
  if (*f != '%') {
418
330
      *p++ = *f++;
419
330
      continue;
420
330
  }
421
422
12
  start = f;
423
12
  f++;
424
425
12
  if (*f == '0')
426
0
      f++;
427
428
12
        var_width = FALSE;
429
12
        if (*f == '*') {
430
0
            var_width = TRUE;
431
0
      f++;
432
0
        }
433
434
12
  while (_cairo_isdigit (*f))
435
0
      f++;
436
437
12
  length_modifier = 0;
438
12
  if (*f == 'l') {
439
0
      length_modifier = LENGTH_MODIFIER_LONG;
440
0
      f++;
441
0
      if (*f == 'l') {
442
0
    length_modifier = LENGTH_MODIFIER_LONG_LONG;
443
0
    f++;
444
0
      }
445
0
  }
446
447
  /* The only format strings exist in the cairo implementation
448
   * itself. So there's an internal consistency problem if any
449
   * of them is larger than our format buffer size. */
450
12
  single_fmt_length = f - start + 1;
451
12
  assert (single_fmt_length + 1 <= SINGLE_FMT_BUFFER_SIZE);
452
453
  /* Reuse the format string for this conversion. */
454
12
  memcpy (single_fmt, start, single_fmt_length);
455
12
  single_fmt[single_fmt_length] = '\0';
456
457
  /* Flush contents of buffer before snprintf()'ing into it. */
458
12
  _cairo_output_stream_write (stream, buffer, p - buffer);
459
460
  /* We group signed and unsigned together in this switch, the
461
   * only thing that matters here is the size of the arguments,
462
   * since we're just passing the data through to sprintf(). */
463
12
  switch (*f | length_modifier) {
464
0
  case '%':
465
0
      buffer[0] = *f;
466
0
      buffer[1] = 0;
467
0
      break;
468
0
  case 'd':
469
0
  case 'u':
470
0
  case 'o':
471
0
  case 'x':
472
0
  case 'X':
473
0
            if (var_width) {
474
0
                width = va_arg (ap, int);
475
0
                snprintf (buffer, sizeof buffer,
476
0
                          single_fmt, width, va_arg (ap, int));
477
0
            } else {
478
0
                snprintf (buffer, sizeof buffer, single_fmt, va_arg (ap, int));
479
0
            }
480
0
      break;
481
0
  case 'd' | LENGTH_MODIFIER_LONG:
482
0
  case 'u' | LENGTH_MODIFIER_LONG:
483
0
  case 'o' | LENGTH_MODIFIER_LONG:
484
0
  case 'x' | LENGTH_MODIFIER_LONG:
485
0
  case 'X' | LENGTH_MODIFIER_LONG:
486
0
            if (var_width) {
487
0
                width = va_arg (ap, int);
488
0
                snprintf (buffer, sizeof buffer,
489
0
                          single_fmt, width, va_arg (ap, long int));
490
0
            } else {
491
0
                snprintf (buffer, sizeof buffer,
492
0
                          single_fmt, va_arg (ap, long int));
493
0
            }
494
0
      break;
495
0
  case 'd' | LENGTH_MODIFIER_LONG_LONG:
496
0
  case 'u' | LENGTH_MODIFIER_LONG_LONG:
497
0
  case 'o' | LENGTH_MODIFIER_LONG_LONG:
498
0
  case 'x' | LENGTH_MODIFIER_LONG_LONG:
499
0
  case 'X' | LENGTH_MODIFIER_LONG_LONG:
500
0
      if (var_width) {
501
0
    width = va_arg (ap, int);
502
0
    snprintf (buffer, sizeof buffer,
503
0
        single_fmt, width, va_arg (ap, long long int));
504
0
      } else {
505
0
    snprintf (buffer, sizeof buffer,
506
0
        single_fmt, va_arg (ap, long long int));
507
0
      }
508
0
      break;
509
4
  case 's': {
510
      /* Write out strings as they may be larger than the buffer. */
511
4
      const char *s = va_arg (ap, const char *);
512
4
      int len = strlen(s);
513
4
      _cairo_output_stream_write (stream, s, len);
514
4
      buffer[0] = 0;
515
4
      }
516
4
      break;
517
8
  case 'f':
518
8
      _cairo_dtostr (buffer, sizeof buffer, va_arg (ap, double), FALSE);
519
8
      break;
520
0
  case 'g':
521
0
      _cairo_dtostr (buffer, sizeof buffer, va_arg (ap, double), TRUE);
522
0
      break;
523
0
  case 'c':
524
0
      buffer[0] = va_arg (ap, int);
525
0
      buffer[1] = 0;
526
0
      break;
527
0
  default:
528
0
      ASSERT_NOT_REACHED;
529
12
  }
530
12
  p = buffer + strlen (buffer);
531
12
  f++;
532
12
    }
533
534
4
    _cairo_output_stream_write (stream, buffer, p - buffer);
535
4
}
536
537
void
538
_cairo_output_stream_printf (cairo_output_stream_t *stream,
539
           const char *fmt, ...)
540
4
{
541
4
    va_list ap;
542
543
4
    va_start (ap, fmt);
544
545
4
    _cairo_output_stream_vprintf (stream, fmt, ap);
546
547
4
    va_end (ap);
548
4
}
549
550
/* Matrix elements that are smaller than the value of the largest element * MATRIX_ROUNDING_TOLERANCE
551
 * are rounded down to zero. */
552
0
#define MATRIX_ROUNDING_TOLERANCE 1e-12
553
554
void
555
_cairo_output_stream_print_matrix (cairo_output_stream_t *stream,
556
           const cairo_matrix_t  *matrix)
557
0
{
558
0
    cairo_matrix_t m;
559
0
    double s, e;
560
561
0
    m = *matrix;
562
0
    s = fabs (m.xx);
563
0
    if (fabs (m.xy) > s)
564
0
  s = fabs (m.xy);
565
0
    if (fabs (m.yx) > s)
566
0
  s = fabs (m.yx);
567
0
    if (fabs (m.yy) > s)
568
0
  s = fabs (m.yy);
569
570
0
    e = s * MATRIX_ROUNDING_TOLERANCE;
571
0
    if (fabs(m.xx) < e)
572
0
  m.xx = 0;
573
0
    if (fabs(m.xy) < e)
574
0
  m.xy = 0;
575
0
    if (fabs(m.yx) < e)
576
0
  m.yx = 0;
577
0
    if (fabs(m.yy) < e)
578
0
  m.yy = 0;
579
0
    if (fabs(m.x0) < e)
580
0
  m.x0 = 0;
581
0
    if (fabs(m.y0) < e)
582
0
  m.y0 = 0;
583
584
0
    _cairo_output_stream_printf (stream,
585
0
         "%f %f %f %f %f %f",
586
0
         m.xx, m.yx, m.xy, m.yy, m.x0, m.y0);
587
0
}
588
589
long long
590
_cairo_output_stream_get_position (cairo_output_stream_t *stream)
591
0
{
592
0
    return stream->position;
593
0
}
594
595
cairo_status_t
596
_cairo_output_stream_get_status (cairo_output_stream_t *stream)
597
2
{
598
2
    return stream->status;
599
2
}
600
601
/* Maybe this should be a configure time option, so embedded targets
602
 * don't have to pull in stdio. */
603
604
605
typedef struct _stdio_stream {
606
    cairo_output_stream_t  base;
607
    FILE      *file;
608
} stdio_stream_t;
609
610
static cairo_status_t
611
stdio_write (cairo_output_stream_t *base,
612
       const unsigned char *data, unsigned int length)
613
0
{
614
0
    stdio_stream_t *stream = (stdio_stream_t *) base;
615
616
0
    if (fwrite (data, 1, length, stream->file) != length)
617
0
  return _cairo_error (CAIRO_STATUS_WRITE_ERROR);
618
619
0
    return CAIRO_STATUS_SUCCESS;
620
0
}
621
622
static cairo_status_t
623
stdio_flush (cairo_output_stream_t *base)
624
0
{
625
0
    stdio_stream_t *stream = (stdio_stream_t *) base;
626
627
0
    fflush (stream->file);
628
629
0
    if (ferror (stream->file))
630
0
  return _cairo_error (CAIRO_STATUS_WRITE_ERROR);
631
0
    else
632
0
  return CAIRO_STATUS_SUCCESS;
633
0
}
634
635
static cairo_status_t
636
stdio_close (cairo_output_stream_t *base)
637
0
{
638
0
    cairo_status_t status;
639
0
    stdio_stream_t *stream = (stdio_stream_t *) base;
640
641
0
    status = stdio_flush (base);
642
643
0
    fclose (stream->file);
644
645
0
    return status;
646
0
}
647
648
cairo_output_stream_t *
649
_cairo_output_stream_create_for_file (FILE *file)
650
0
{
651
0
    stdio_stream_t *stream;
652
653
0
    if (file == NULL) {
654
0
  _cairo_error_throw (CAIRO_STATUS_WRITE_ERROR);
655
0
  return (cairo_output_stream_t *) &_cairo_output_stream_nil_write_error;
656
0
    }
657
658
0
    stream = _cairo_calloc (sizeof *stream);
659
0
    if (unlikely (stream == NULL)) {
660
0
  _cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
661
0
  return (cairo_output_stream_t *) &_cairo_output_stream_nil;
662
0
    }
663
664
0
    _cairo_output_stream_init (&stream->base,
665
0
             stdio_write, stdio_flush, stdio_flush);
666
0
    stream->file = file;
667
668
0
    return &stream->base;
669
0
}
670
671
cairo_output_stream_t *
672
_cairo_output_stream_create_for_filename (const char *filename)
673
2
{
674
2
    stdio_stream_t *stream;
675
2
    FILE *file;
676
2
    cairo_status_t status;
677
678
2
    if (filename == NULL)
679
2
  return _cairo_null_stream_create ();
680
681
0
    status = _cairo_fopen (filename, "wb", &file);
682
683
0
    if (status != CAIRO_STATUS_SUCCESS)
684
0
  return _cairo_output_stream_create_in_error (status);
685
686
0
    if (file == NULL) {
687
0
  switch (errno) {
688
0
  case ENOMEM:
689
0
      _cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
690
0
      return (cairo_output_stream_t *) &_cairo_output_stream_nil;
691
0
  default:
692
0
      _cairo_error_throw (CAIRO_STATUS_WRITE_ERROR);
693
0
      return (cairo_output_stream_t *) &_cairo_output_stream_nil_write_error;
694
0
  }
695
0
    }
696
697
0
    stream = _cairo_calloc (sizeof *stream);
698
0
    if (unlikely (stream == NULL)) {
699
0
  fclose (file);
700
0
  _cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
701
0
  return (cairo_output_stream_t *) &_cairo_output_stream_nil;
702
0
    }
703
704
0
    _cairo_output_stream_init (&stream->base,
705
0
             stdio_write, stdio_flush, stdio_close);
706
0
    stream->file = file;
707
708
0
    return &stream->base;
709
0
}
710
711
712
typedef struct _memory_stream {
713
    cairo_output_stream_t base;
714
    cairo_array_t   array;
715
} memory_stream_t;
716
717
static cairo_status_t
718
memory_write (cairo_output_stream_t *base,
719
        const unsigned char *data, unsigned int length)
720
0
{
721
0
    memory_stream_t *stream = (memory_stream_t *) base;
722
723
0
    return _cairo_array_append_multiple (&stream->array, data, length);
724
0
}
725
726
static cairo_status_t
727
memory_close (cairo_output_stream_t *base)
728
0
{
729
0
    memory_stream_t *stream = (memory_stream_t *) base;
730
731
0
    _cairo_array_fini (&stream->array);
732
733
0
    return CAIRO_STATUS_SUCCESS;
734
0
}
735
736
cairo_output_stream_t *
737
_cairo_memory_stream_create (void)
738
0
{
739
0
    memory_stream_t *stream;
740
741
0
    stream = _cairo_calloc (sizeof *stream);
742
0
    if (unlikely (stream == NULL)) {
743
0
  _cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
744
0
  return (cairo_output_stream_t *) &_cairo_output_stream_nil;
745
0
    }
746
747
0
    _cairo_output_stream_init (&stream->base, memory_write, NULL, memory_close);
748
0
    _cairo_array_init (&stream->array, 1);
749
750
0
    return &stream->base;
751
0
}
752
753
cairo_status_t
754
_cairo_memory_stream_destroy (cairo_output_stream_t *abstract_stream,
755
            unsigned char **data_out,
756
            unsigned long *length_out)
757
0
{
758
0
    memory_stream_t *stream;
759
0
    cairo_status_t status;
760
761
0
    status = abstract_stream->status;
762
0
    if (unlikely (status))
763
0
  return _cairo_output_stream_destroy (abstract_stream);
764
765
0
    stream = (memory_stream_t *) abstract_stream;
766
767
0
    *length_out = _cairo_array_num_elements (&stream->array);
768
0
    *data_out = _cairo_calloc (*length_out);
769
0
    if (unlikely (*data_out == NULL)) {
770
0
  status = _cairo_output_stream_destroy (abstract_stream);
771
0
  assert (status == CAIRO_STATUS_SUCCESS);
772
0
  return _cairo_error (CAIRO_STATUS_NO_MEMORY);
773
0
    }
774
0
    memcpy (*data_out, _cairo_array_index (&stream->array, 0), *length_out);
775
776
0
    return _cairo_output_stream_destroy (abstract_stream);
777
0
}
778
779
void
780
_cairo_memory_stream_copy (cairo_output_stream_t *base,
781
         cairo_output_stream_t *dest)
782
0
{
783
0
    memory_stream_t *stream = (memory_stream_t *) base;
784
785
0
    if (base->status) {
786
0
  dest->status = base->status;
787
0
  return;
788
0
    }
789
790
0
    _cairo_output_stream_write (dest,
791
0
        _cairo_array_index (&stream->array, 0),
792
0
        _cairo_array_num_elements (&stream->array));
793
0
}
794
795
int
796
_cairo_memory_stream_length (cairo_output_stream_t *base)
797
0
{
798
0
    memory_stream_t *stream = (memory_stream_t *) base;
799
800
0
    return _cairo_array_num_elements (&stream->array);
801
0
}
802
803
static cairo_status_t
804
null_write (cairo_output_stream_t *base,
805
      const unsigned char *data, unsigned int length)
806
16
{
807
16
    return CAIRO_STATUS_SUCCESS;
808
16
}
809
810
cairo_output_stream_t *
811
_cairo_null_stream_create (void)
812
2
{
813
2
    cairo_output_stream_t *stream;
814
815
2
    stream = _cairo_calloc (sizeof *stream);
816
2
    if (unlikely (stream == NULL)) {
817
0
  _cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
818
0
  return (cairo_output_stream_t *) &_cairo_output_stream_nil;
819
0
    }
820
821
2
    _cairo_output_stream_init (stream, null_write, NULL, NULL);
822
823
2
    return stream;
824
2
}