1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
|
/*
* Copyright © 2008 Chris Wilson <chris@chris-wilson.co.uk>
*
* This library is free software; you can redistribute it and/or
* modify it either under the terms of the GNU Lesser General Public
* License version 2.1 as published by the Free Software Foundation
* (the "LGPL") or, at your option, under the terms of the Mozilla
* Public License Version 1.1 (the "MPL"). If you do not alter this
* notice, a recipient may use your version of this file under either
* the MPL or the LGPL.
*
* You should have received a copy of the LGPL along with this library
* in the file COPYING-LGPL-2.1; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA
* You should have received a copy of the MPL along with this library
* in the file COPYING-MPL-1.1
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
* OF ANY KIND, either express or implied. See the LGPL or the MPL for
* the specific language governing rights and limitations.
*
* The Original Code is the cairo graphics library.
*
* The Initial Developer of the Original Code is Chris Wilson.
*
* Contributor(s):
* Chris Wilson <chris@chris-wilson.co.uk>
*/
#include "config.h"
#include "cairo-script-private.h"
#include <stdio.h>
#include <limits.h> /* INT_MAX */
#include <string.h>
#include <zlib.h>
#if HAVE_LZO
#include <lzo/lzo2a.h>
#endif
#define CHUNK_SIZE 32768
#define OWN_STREAM 0x1
csi_status_t
csi_file_new (csi_t *ctx,
csi_object_t *obj,
const char *path, const char *mode)
{
csi_file_t *file;
file = _csi_slab_alloc (ctx, sizeof (csi_file_t));
if (file == NULL)
return _csi_error (CAIRO_STATUS_NO_MEMORY);
file->base.type = CSI_OBJECT_TYPE_FILE;
file->base.ref = 1;
file->data = NULL;
file->type = STDIO;
file->flags = OWN_STREAM;
file->src = fopen (path, mode);
if (file->src == NULL) {
_csi_slab_free (ctx, file, sizeof (csi_file_t));
return _csi_error (CAIRO_STATUS_FILE_NOT_FOUND);
}
file->data = _csi_alloc (ctx, CHUNK_SIZE);
if (file->data == NULL) {
_csi_slab_free (ctx, file, sizeof (csi_file_t));
return _csi_error (CAIRO_STATUS_NO_MEMORY);
}
file->bp = file->data;
file->rem = 0;
obj->type = CSI_OBJECT_TYPE_FILE;
obj->datum.file = file;
return CAIRO_STATUS_SUCCESS;
}
csi_status_t
csi_file_new_for_stream (csi_t *ctx,
csi_object_t *obj,
FILE *stream)
{
csi_file_t *file;
file = _csi_slab_alloc (ctx, sizeof (csi_file_t));
if (file == NULL)
return _csi_error (CAIRO_STATUS_NO_MEMORY);
file->base.type = CSI_OBJECT_TYPE_FILE;
file->base.ref = 1;
file->data = NULL;
file->type = STDIO;
file->flags = 0;
file->src = stream;
if (file->src == NULL) {
_csi_slab_free (ctx, file, sizeof (csi_file_t));
return _csi_error (CAIRO_STATUS_FILE_NOT_FOUND);
}
file->data = _csi_alloc (ctx, CHUNK_SIZE);
if (file->data == NULL) {
_csi_slab_free (ctx, file, sizeof (csi_file_t));
return _csi_error (CAIRO_STATUS_NO_MEMORY);
}
file->bp = file->data;
file->rem = 0;
obj->type = CSI_OBJECT_TYPE_FILE;
obj->datum.file = file;
return CAIRO_STATUS_SUCCESS;
}
csi_status_t
csi_file_new_for_bytes (csi_t *ctx,
csi_object_t *obj,
const char *bytes,
unsigned int length)
{
csi_file_t *file;
file = _csi_slab_alloc (ctx, sizeof (csi_file_t));
if (file == NULL)
return _csi_error (CAIRO_STATUS_NO_MEMORY);
file->base.type = CSI_OBJECT_TYPE_FILE;
file->base.ref = 1;
file->type = BYTES;
file->src = (uint8_t *) bytes;
file->data = (uint8_t *) bytes;
file->bp = (uint8_t *) bytes;
file->rem = length;
obj->type = CSI_OBJECT_TYPE_FILE;
obj->datum.file = file;
return CAIRO_STATUS_SUCCESS;
}
csi_status_t
csi_file_new_from_string (csi_t *ctx,
csi_object_t *obj,
csi_string_t *src)
{
csi_file_t *file;
file = _csi_slab_alloc (ctx, sizeof (csi_file_t));
if (_csi_unlikely (file == NULL))
return _csi_error (CAIRO_STATUS_NO_MEMORY);
file->base.type = CSI_OBJECT_TYPE_FILE;
file->base.ref = 1;
if (src->deflate) {
uLongf len = src->deflate;
csi_object_t tmp_obj;
csi_string_t *tmp_str;
csi_status_t status;
status = csi_string_new (ctx, &tmp_obj, NULL, src->deflate);
if (_csi_unlikely (status))
return status;
tmp_str = tmp_obj.datum.string;
switch (src->method) {
case NONE:
default:
status = _csi_error (CAIRO_STATUS_NO_MEMORY);
break;
case ZLIB:
#if HAVE_ZLIB
if (uncompress ((Bytef *) tmp_str->string, &len,
(Bytef *) src->string, src->len) != Z_OK)
#endif
status = _csi_error (CAIRO_STATUS_NO_MEMORY);
break;
case LZO:
#if HAVE_LZO
if (lzo2a_decompress ((lzo_bytep) src->string, src->len,
(lzo_bytep) tmp_str->string, &len,
NULL))
#endif
status = _csi_error (CAIRO_STATUS_NO_MEMORY);
break;
}
if (_csi_unlikely (status)) {
csi_string_free (ctx, tmp_str);
_csi_slab_free (ctx, file, sizeof (csi_file_t));
return status;
}
file->src = tmp_str;
file->data = tmp_str->string;
file->rem = tmp_str->len;
} else {
file->src = src; src->base.ref++;
file->data = src->string;
file->rem = src->len;
}
file->type = BYTES;
file->bp = file->data;
obj->type = CSI_OBJECT_TYPE_FILE;
obj->datum.file = file;
return CAIRO_STATUS_SUCCESS;
}
static csi_status_t
_csi_file_new_filter (csi_t *ctx,
csi_object_t *obj,
csi_object_t *src,
const csi_filter_funcs_t *funcs,
void *data)
{
csi_file_t *file;
csi_object_t src_file;
csi_status_t status;
file = _csi_slab_alloc (ctx, sizeof (csi_file_t));
if (file == NULL)
return _csi_error (CAIRO_STATUS_NO_MEMORY);
obj->type = CSI_OBJECT_TYPE_FILE;
obj->datum.file = file;
file->base.type = CSI_OBJECT_TYPE_FILE;
file->base.ref = 1;
file->type = FILTER;
file->data = data;
file->filter = funcs;
status = csi_object_as_file (ctx, src, &src_file);
if (status) {
csi_object_free (ctx, obj);
return status;
}
file->src = src_file.datum.file;
return CAIRO_STATUS_SUCCESS;
}
#if 0
csi_status_t
csi_file_new_from_stream (csi_t *ctx,
FILE *file,
csi_object_t **out)
{
csi_file_t *obj;
obj = (csi_file_t *) _csi_object_new (ctx, CSI_OBJECT_TYPE_FILE);
if (obj == NULL)
return _csi_error (CAIRO_STATUS_NO_MEMORY);
obj->type = STDIO;
obj->src = file;
obj->data = _csi_alloc (ctx, CHUNK_SIZE);
if (obj->data == NULL) {
csi_object_free (&obj->base);
return _csi_error (CAIRO_STATUS_UNDEFINED_FILENAME_ERROR);
}
obj->bp = obj->data;
obj->rem = 0;
*out = &obj->base;
return CAIRO_STATUS_SUCCESS;
}
static csi_object_t *
_csi_file_new_from_procedure (csi_t *ctx, csi_object_t *src)
{
csi_file_t *obj;
obj = (csi_file_t *) _csi_object_new (ctx, CSI_OBJECT_TYPE_FILE);
if (obj == NULL)
return NULL;
obj->type = PROCEDURE;
obj->src = csi_object_reference (src);
obj->data = NULL;
return &obj->base;
}
#endif
typedef struct _ascii85_decode_data {
uint8_t buf[CHUNK_SIZE];
uint8_t *bp;
short bytes_available;
short eod;
} _ascii85_decode_data_t;
static int
_getc_skip_whitespace (csi_file_t *src)
{
int c;
do switch ((c = csi_file_getc (src))) {
case 0x0:
case 0x9:
case 0xa:
case 0xc:
case 0xd:
case 0x20:
continue;
default:
return c;
} while (TRUE);
return c;
}
static void
_ascii85_decode (csi_file_t *file)
{
_ascii85_decode_data_t *data = file->data;
unsigned int n;
if (data->eod)
return;
data->bp = data->buf;
n = 0;
do {
unsigned int v = _getc_skip_whitespace (file->src);
if (v == 'z') {
data->buf[n+0] = 0;
data->buf[n+1] = 0;
data->buf[n+2] = 0;
data->buf[n+3] = 0;
} else if (v == '~') {
_getc_skip_whitespace (file->src); /* == '>' || IO_ERROR */
data->eod = TRUE;
break;
} else if (v < '!' || v > 'u') {
/* IO_ERROR */
data->eod = TRUE;
break;
} else {
unsigned int i;
v -= '!';
for (i = 1; i < 5; i++) {
int c = _getc_skip_whitespace (file->src);
if (c == '~') { /* short tuple */
_getc_skip_whitespace (file->src); /* == '>' || IO_ERROR */
data->eod = TRUE;
switch (i) {
case 0:
case 1:
/* IO_ERROR */
break;
case 2:
v = v * (85*85*85) + 85*85*85 -1;
goto odd1;
case 3:
v = v * (85*85) + 85*85 -1;
goto odd2;
case 4:
v = v * 85 + 84;
data->buf[n+2] = v >> 8 & 0xff;
odd2:
data->buf[n+1] = v >> 16 & 0xff;
odd1:
data->buf[n+0] = v >> 24 & 0xff;
data->bytes_available = n + i - 1;
return;
}
break;
}
v = 85*v + c-'!';
}
data->buf[n+0] = v >> 24 & 0xff;
data->buf[n+1] = v >> 16 & 0xff;
data->buf[n+2] = v >> 8 & 0xff;
data->buf[n+3] = v >> 0 & 0xff;
}
n += 4;
} while (n < sizeof (data->buf) && data->eod == FALSE);
data->bytes_available = n;
}
static int
_ascii85_decode_getc (csi_file_t *file)
{
_ascii85_decode_data_t *data = file->data;
if (data->bytes_available == 0) {
_ascii85_decode (file);
if (data->bytes_available == 0)
return EOF;
}
data->bytes_available--;
return *data->bp++;
}
static void
_ascii85_decode_putc (csi_file_t *file, int c)
{
_ascii85_decode_data_t *data = file->data;
data->bytes_available++;
data->bp--;
}
static int
_ascii85_decode_read (csi_file_t *file, uint8_t *buf, int len)
{
_ascii85_decode_data_t *data = file->data;
if (data->bytes_available == 0) {
_ascii85_decode (file);
if (data->bytes_available == 0)
return 0;
}
if (len > data->bytes_available)
len = data->bytes_available;
memcpy (buf, data->bp, len);
data->bp += len;
data->bytes_available -= len;
return len;
}
csi_status_t
csi_file_new_ascii85_decode (csi_t *ctx,
csi_object_t *obj,
csi_dictionary_t *dict,
csi_object_t *src)
{
static const csi_filter_funcs_t funcs = {
_ascii85_decode_getc,
_ascii85_decode_putc,
_ascii85_decode_read,
_csi_free,
};
_ascii85_decode_data_t *data;
data = _csi_alloc0 (ctx, sizeof (_ascii85_decode_data_t));
if (data == NULL)
return _csi_error (CAIRO_STATUS_NO_MEMORY);
return _csi_file_new_filter (ctx, obj, src, &funcs, data);
}
#if HAVE_ZLIB
#include <zlib.h>
typedef struct _deflate_decode_data {
z_stream zlib_stream;
uint8_t in[CHUNK_SIZE];
uint8_t out[CHUNK_SIZE];
int bytes_available;
uint8_t *bp;
} _deflate_decode_data_t;
static void
_deflate_decode (csi_file_t *file)
{
_deflate_decode_data_t *data = file->data;
uint8_t *bp;
int len;
data->zlib_stream.next_out = data->out;
data->zlib_stream.avail_out = sizeof (data->out);
bp = data->in;
len = sizeof (data->in);
if (data->zlib_stream.avail_in) {
memmove (data->in,
data->zlib_stream.next_in,
data->zlib_stream.avail_in);
len -= data->zlib_stream.avail_in;
bp += data->zlib_stream.avail_in;
}
len = csi_file_read (file->src, bp, len);
data->zlib_stream.next_in = data->in;
data->zlib_stream.avail_in += len;
inflate (&data->zlib_stream, len == 0 ? Z_FINISH : Z_NO_FLUSH);
data->bytes_available = data->zlib_stream.next_out - data->out;
data->bp = data->out;
}
static int
_deflate_decode_getc (csi_file_t *file)
{
_deflate_decode_data_t *data = file->data;
if (data->bytes_available == 0) {
_deflate_decode (file);
if (data->bytes_available == 0)
return EOF;
}
data->bytes_available--;
return *data->bp++;
}
static void
_deflate_decode_putc (csi_file_t *file, int c)
{
_deflate_decode_data_t *data = file->data;
data->bytes_available++;
data->bp--;
}
static int
_deflate_decode_read (csi_file_t *file, uint8_t *buf, int len)
{
_deflate_decode_data_t *data = file->data;
if (data->bytes_available == 0) {
_deflate_decode (file);
if (data->bytes_available == 0)
return 0;
}
if (len > (int) data->bytes_available)
len = data->bytes_available;
memcpy (buf, data->bp, len);
data->bp += len;
data->bytes_available -= len;
return len;
}
static void
_deflate_destroy (csi_t *ctx, void *closure)
{
_deflate_decode_data_t *data;
data = closure;
inflateEnd (&data->zlib_stream);
_csi_free (ctx, data);
}
csi_status_t
csi_file_new_deflate_decode (csi_t *ctx,
csi_object_t *obj,
csi_dictionary_t *dict,
csi_object_t *src)
{
static const csi_filter_funcs_t funcs = {
_deflate_decode_getc,
_deflate_decode_putc,
_deflate_decode_read,
_deflate_destroy,
};
_deflate_decode_data_t *data;
data = _csi_alloc (ctx, sizeof (_deflate_decode_data_t));
if (data == NULL)
return _csi_error (CAIRO_STATUS_NO_MEMORY);
data->zlib_stream.zalloc = Z_NULL;
data->zlib_stream.zfree = Z_NULL;
data->zlib_stream.opaque = Z_NULL;
data->zlib_stream.next_in = data->in;
data->zlib_stream.avail_in = 0;
data->zlib_stream.next_out = data->out;
data->zlib_stream.avail_out = sizeof (data->out);
data->bytes_available = 0;
if (inflateInit (&data->zlib_stream) != Z_OK) {
_csi_free (ctx, data);
return _csi_error (CAIRO_STATUS_NO_MEMORY);
}
return _csi_file_new_filter (ctx, obj, src, &funcs, data);
}
#endif
#if 0
static int
hex_value (int c)
{
if (c < '0')
return EOF;
if (c <= '9')
return c - '0';
c |= 32;
if (c < 'a')
return EOF;
if (c <= 'f')
return c - 'a' + 0xa;
return EOF;
}
/* Adobe Type 1 Font Format book: p63 */
typedef struct _decrypt_data {
uint8_t putback[32];
uint8_t nputback;
csi_bool_t is_hexadecimal;
unsigned short R;
int eod;
} _decrypt_data_t;
static uint8_t
_decrypt (unsigned short *R, uint8_t cypher)
{
#define c1 52845
#define c2 22719
uint8_t plain;
plain = cypher ^ (*R >> 8);
*R = (cypher + *R) * c1 + c2;
return plain;
#undef c1
#undef c2
}
int
csi_decrypt (uint8_t *in, int length,
unsigned short salt, int binary,
uint8_t *out)
{
const uint8_t * const end = in + length;
uint8_t *base = out;
while (in < end) {
int c;
if (! binary) {
int c_hi = -1, c_lo = 0;
while (in < end && (c_hi = *in++)) {
switch (c_hi) {
case 0x0:
case 0x9:
case 0xa:
case 0xc:
case 0xd:
case 0x20:
continue;
default:
break;
}
}
if (c_hi < 0)
break;
while (in < end && (c_lo = *in++)) {
switch (c_lo) {
case 0x0:
case 0x9:
case 0xa:
case 0xc:
case 0xd:
case 0x20:
continue;
default:
break;
}
}
c = (hex_value (c_hi) << 4) | hex_value (c_lo);
} else
c = *in++;
*out++ = _decrypt (&salt, c);
}
return out - base;
}
static uint8_t
_encrypt (unsigned short *R, uint8_t plain)
{
#define c1 52845
#define c2 22719
uint8_t cypher;
cypher = plain ^ (*R >> 8);
*R = (cypher + *R) * c1 + c2;
return cypher;
#undef c1
#undef c2
}
int
csi_encrypt (uint8_t *in, int length,
unsigned short salt, int discard, int binary,
uint8_t *out)
{
const char hex[]="0123456789abcdef";
const uint8_t * const end = in + length;
uint8_t *base = out;
int col = 0;
while (discard--) {
if (! binary) {
int c = _encrypt (&salt, ' ');
*out++ = hex[(c >> 4) & 0xf];
*out++ = hex[(c >> 0) & 0xf];
} else
*out++ = _encrypt (&salt, 0);
}
while (in < end) {
int c;
c = _encrypt (&salt, *in++);
if (! binary) {
if (col == 78) {
*out++ = '\n';
col = 0;
}
*out++ = hex[(c >> 4) & 0xf];
*out++ = hex[(c >> 0) & 0xf];
col += 2;
} else
*out++ = c;
}
return out - base;
}
static int
_decrypt_getc (csi_file_t *file)
{
_decrypt_data_t *data = file->data;
int c;
if (data->nputback)
return data->putback[--data->nputback];
if (data->is_hexadecimal) {
int c_hi, c_lo;
c_hi = _getc_skip_whitespace (file->src);
c_lo = _getc_skip_whitespace (file->src);
c = (hex_value (c_hi) << 4) | hex_value (c_lo);
} else
c = csi_file_getc (file->src);
if (c == EOF)
return EOF;
return _decrypt (&data->R, c);
}
static void
_decrypt_putc (csi_file_t *file, int c)
{
_decrypt_data_t *data;
data = file->data;
data->putback[data->nputback++] = c;
}
csi_object_t *
csi_file_new_decrypt (csi_t *ctx, csi_object_t *src, int salt, int discard)
{
csi_object_t *obj;
_decrypt_data_t *data;
int n;
data = _csi_alloc0 (ctx, sizeof (_decrypt_data_t));
if (data == NULL)
return NULL;
data->R = salt;
obj = _csi_file_new_filter (ctx, src,
_decrypt_getc,
_decrypt_putc,
NULL,
_csi_free,
data);
if (obj == NULL)
return NULL;
/* XXX determine encoding, eexec only? */
data->is_hexadecimal = salt != 4330;
for (n = 0; n < discard; n++) {
int c;
c = csi_file_getc (obj);
if (c == EOF) {
return obj;
}
}
return obj;
}
#endif
csi_status_t
_csi_file_execute (csi_t *ctx, csi_file_t *obj)
{
return _csi_scan_file (ctx, obj);
}
int
csi_file_getc (csi_file_t *file)
{
int c;
if (_csi_unlikely (file->src == NULL))
return EOF;
switch (file->type) {
case STDIO:
if (_csi_likely (file->rem)) {
c = *file->bp++;
file->rem--;
} else {
file->rem = fread (file->bp = file->data, 1, CHUNK_SIZE, file->src);
case BYTES:
if (_csi_likely (file->rem)) {
c = *file->bp++;
file->rem--;
} else
c = EOF;
}
break;
case PROCEDURE:
#if 0
if (file->data == NULL) {
csi_status_t status;
csi_object_t *string;
RERUN_PROCEDURE:
status = csi_object_execute (file->src);
if (status)
return EOF;
string = csi_pop_operand (file->base.ctx);
if (string == NULL)
return EOF;
file->data = csi_object_as_file (file->base.ctx, string);
csi_object_free (string);
if (file->data == NULL)
return EOF;
}
c = csi_file_getc (file->data);
if (c == EOF) {
csi_object_free (file->data);
file->data = NULL;
goto RERUN_PROCEDURE;
}
#else
c = EOF;
#endif
break;
case FILTER:
c = file->filter->filter_getc (file);
break;
default:
c = EOF;
break;
}
return c;
}
int
csi_file_read (csi_file_t *file, void *buf, int len)
{
int ret;
if (file->src == NULL)
return 0;
switch (file->type) {
case STDIO:
if (file->rem > 0) {
ret = len;
if (file->rem < ret)
ret = file->rem;
memcpy (buf, file->bp, ret);
file->bp += ret;
file->rem -= ret;
} else
ret = fread (buf, 1, len, file->src);
break;
case BYTES:
if (file->rem > 0) {
ret = len;
if (file->rem < ret)
ret = file->rem;
memcpy (buf, file->bp, ret);
file->bp += ret;
file->rem -= ret;
} else
ret = 0;
break;
case PROCEDURE:
#if 0
if (file->data == NULL) {
csi_status_t status;
csi_object_t *string;
RERUN_PROCEDURE:
status = csi_object_execute (file->src);
if (status)
return 0;
string = csi_pop_operand (file->base.ctx);
if (string == NULL)
return 0;
file->data = csi_object_as_file (file->base.ctx, string);
csi_object_free (string);
if (file->data == NULL)
return 0;
}
ret = csi_file_read (file->data, buf, len);
if (ret == 0) {
csi_object_free (file->data);
file->data = NULL;
goto RERUN_PROCEDURE;
}
#else
ret = 0;
#endif
break;
case FILTER:
ret = file->filter->filter_read (file, buf, len);
break;
default:
ret = 0;
break;
}
return ret;
}
void
csi_file_putc (csi_file_t *file, int c)
{
if (file->src == NULL)
return;
switch ((int) file->type) {
case STDIO:
case BYTES:
file->bp--;
file->rem++;
break;
case FILTER:
file->filter->filter_putc (file, c);
break;
default:
break;
}
}
void
csi_file_flush (csi_file_t *file)
{
if (file->src == NULL)
return;
switch ((int) file->type) {
case FILTER: /* need to eat EOD */
while (csi_file_getc (file) != EOF)
;
break;
default:
break;
}
}
void
csi_file_close (csi_t *ctx, csi_file_t *file)
{
if (file->src == NULL)
return;
switch (file->type) {
case STDIO:
if (file->flags & OWN_STREAM)
fclose (file->src);
break;
case BYTES:
if (file->src != file->data) {
csi_string_t *src = file->src;
if (src != NULL && --src->base.ref == 0)
csi_string_free (ctx, src);
}
break;
case FILTER:
{
csi_file_t *src = file->src;
if (src != NULL && --src->base.ref == 0)
_csi_file_free (ctx, src);
}
break;
case PROCEDURE:
default:
break;
}
file->src = NULL;
}
void
_csi_file_free (csi_t *ctx, csi_file_t *file)
{
csi_file_flush (file);
/* XXX putback */
csi_file_close (ctx, file);
switch (file->type) {
case BYTES:
break;
case PROCEDURE:
#if 0
csi_object_free (ctx, file->data);
#endif
break;
case STDIO:
_csi_free (ctx, file->data);
break;
case FILTER:
file->filter->filter_destroy (ctx, file->data);
break;
default:
break;
}
_csi_slab_free (ctx, file, sizeof (csi_file_t));
}
csi_status_t
_csi_file_as_string (csi_t *ctx,
csi_file_t *file,
csi_object_t *obj)
{
char *bytes;
unsigned int len;
unsigned int allocated;
csi_status_t status;
allocated = 16384;
bytes = _csi_alloc (ctx, allocated);
if (bytes == NULL)
return _csi_error (CAIRO_STATUS_NO_MEMORY);
len = 0;
do {
int ret;
ret = csi_file_read (file, bytes + len, allocated - len);
if (ret == 0)
break;
len += ret;
if (len + 1 > allocated / 2) {
char *newbytes;
int newlen;
if (_csi_unlikely (allocated > INT_MAX / 2))
return _csi_error (CAIRO_STATUS_NO_MEMORY);
newlen = allocated * 2;
newbytes = _csi_realloc (ctx, bytes, newlen);
if (_csi_unlikely (newbytes == NULL)) {
_csi_free (ctx, bytes);
return _csi_error (CAIRO_STATUS_NO_MEMORY);
}
bytes = newbytes;
allocated = newlen;
}
} while (TRUE);
bytes[len] = '\0'; /* better safe than sorry! */
status = csi_string_new_from_bytes (ctx, obj, bytes, len);
if (status) {
_csi_free (ctx, bytes);
return status;
}
return CAIRO_STATUS_SUCCESS;
}
|