-
Notifications
You must be signed in to change notification settings - Fork 19
/
memfile.c
1504 lines (1225 loc) · 33.4 KB
/
memfile.c
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
/* Part of SWI-Prolog
Author: Jan Wielemaker
E-mail: [email protected]
WWW: http://www.swi-prolog.org
Copyright (c) 2001-2015, University of Amsterdam
VU University Amsterdam
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
#include <config.h>
#include <SWI-Stream.h>
#include <SWI-Prolog.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <errno.h>
#ifdef O_PLMT
#include <pthread.h>
#endif
#include "error.h"
#ifdef O_PLMT
#define LOCK(mf) pthread_mutex_lock(&(mf)->mutex)
#define UNLOCK(mf) pthread_mutex_unlock(&(mf)->mutex)
#else
#define LOCK(mf)
#define UNLOCK(mf)
#endif
static atom_t ATOM_encoding;
static atom_t ATOM_unknown;
static atom_t ATOM_octet;
static atom_t ATOM_ascii;
static atom_t ATOM_iso_latin_1;
static atom_t ATOM_text;
static atom_t ATOM_utf8;
static atom_t ATOM_unicode_be;
static atom_t ATOM_unicode_le;
static atom_t ATOM_wchar_t;
static atom_t ATOM_read;
static atom_t ATOM_write;
static atom_t ATOM_append;
static atom_t ATOM_update;
static atom_t ATOM_insert;
static atom_t ATOM_free_on_close;
#define MEMFILE_MAGIC 0x5624a6b3L
#define MEMFILE_CMAGIC 0x5624a6b7L
#define NOSIZE ((size_t)-1)
#define V_CHARCOUNT 0x01
#define V_LINENO 0x02
#define V_LINEPOS 0x04
#define V_ALL 0x07
typedef struct
{ size_t byte_count; /* Byte position in MF */
size_t char_count; /* Corresponding logical char */
size_t line_no; /* Line */
size_t line_pos; /* Line position */
unsigned int valid; /* Valid mask */
} pos_cache;
typedef struct
{ char *data; /* data of the file */
size_t end; /* End of buffer */
size_t gap_start; /* Insertion point */
size_t gap_size; /* Insertion hole */
size_t char_count; /* size in characters */
pos_cache pcache; /* Cached position */
size_t here; /* read pointer */
IOSTREAM *stream; /* Stream hanging onto it */
atom_t symbol; /* <memory_file>(%p) */
atom_t atom; /* Created from atom */
atom_t mode; /* current open mode */
#ifdef O_PLMT
pthread_mutex_t mutex; /* Our lock */
#endif
int magic; /* MEMFILE_MAGIC */
int free_on_close; /* free if it is closed */
IOENC encoding; /* encoding of the data */
} memfile;
static int destroy_memory_file(memfile *m);
/*******************************
* SYMBOL *
*******************************/
static void
acquire_memfile_symbol(atom_t symbol)
{ memfile *mf = PL_blob_data(symbol, NULL, NULL);
mf->symbol = symbol;
}
static int
release_memfile_symbol(atom_t symbol)
{ memfile *mf = PL_blob_data(symbol, NULL, NULL);
destroy_memory_file(mf);
return TRUE;
}
static int
compare_memfile_symbols(atom_t a, atom_t b)
{ memfile *mfa = PL_blob_data(a, NULL, NULL);
memfile *mfb = PL_blob_data(b, NULL, NULL);
return ( mfa > mfb ? 1 :
mfa < mfb ? -1 : 0
);
}
static int
write_memfile_symbol(IOSTREAM *s, atom_t symbol, int flags)
{ memfile *mf = PL_blob_data(symbol, NULL, NULL);
Sfprintf(s, "<memory_file>(%p)", mf);
return TRUE;
}
static PL_blob_t memfile_blob =
{ PL_BLOB_MAGIC,
PL_BLOB_NOCOPY,
"memory_file",
release_memfile_symbol,
compare_memfile_symbols,
write_memfile_symbol,
acquire_memfile_symbol
};
static int
unify_memfile(term_t handle, memfile *mf)
{ if ( PL_unify_blob(handle, mf, sizeof(*mf), &memfile_blob) )
return TRUE;
if ( !PL_is_variable(handle) )
return PL_uninstantiation_error(handle);
return FALSE; /* (resource) error */
}
static int
get_memfile(term_t handle, memfile **mfp)
{ PL_blob_t *type;
void *data;
if ( PL_get_blob(handle, &data, NULL, &type) && type == &memfile_blob)
{ memfile *mf = data;
assert(mf->magic == MEMFILE_MAGIC);
LOCK(mf);
if ( mf->symbol )
{ *mfp = mf;
return TRUE;
}
UNLOCK(mf);
PL_permission_error("access", "freed_memory_file", handle);
return FALSE;
}
return PL_type_error("memory_file", handle);
}
static void
release_memfile(memfile *mf)
{ UNLOCK(mf);
}
static void
empty_memory_file(memfile *m)
{ if ( m->data )
free(m->data);
m->encoding = ENC_UTF8;
m->data = NULL;
m->end = 0;
m->gap_start = 0;
m->gap_size = 0;
m->char_count = NOSIZE;
m->pcache.valid = 0;
m->here = 0;
}
static foreign_t
new_memory_file(term_t handle)
{ memfile *m = calloc(1, sizeof(*m));
if ( !m )
return PL_resource_error("memory");
m->magic = MEMFILE_MAGIC;
m->encoding = ENC_UTF8;
m->data = NULL;
m->atom = 0;
m->symbol = 0;
m->stream = NULL;
#ifdef O_PLMT
pthread_mutex_init(&m->mutex, NULL);
#endif
if ( unify_memfile(handle, m) )
return TRUE;
destroy_memory_file(m);
return FALSE;
}
static void
clean_memory_file(memfile *m)
{ if ( m->stream )
{ Sclose(m->stream);
m->stream = NULL;
}
if ( m->atom )
{ PL_unregister_atom(m->atom);
m->atom = 0;
m->data = NULL;
} else if ( m->data )
{ free(m->data);
m->data = NULL;
}
}
static int
destroy_memory_file(memfile *m)
{ clean_memory_file(m);
#ifdef O_PLMT
pthread_mutex_destroy(&m->mutex);
#endif
m->magic = MEMFILE_CMAGIC;
free(m);
return TRUE;
}
static foreign_t
free_memory_file(term_t handle)
{ memfile *m;
if ( get_memfile(handle, &m) )
{ m->symbol = 0;
clean_memory_file(m);
release_memfile(m);
return TRUE;
}
return FALSE;
}
/*******************************
* CHECKING *
*******************************/
#define ISUTF8_CB(c) (((c)&0xc0) == 0x80) /* Is continuation byte */
#ifdef O_SECURE
#define CONT(in,i) (assert(ISUTF8_CB(in[i])),1)
#define IS_UTF8_2BYTE(in) \
((in[0]&0xe0) == 0xc0 && CONT(in,1))
#define IS_UTF8_3BYTE(in) \
((in[0]&0xf0) == 0xe0 && CONT(in,1)&&CONT(in,2))
#define IS_UTF8_4BYTE(in) \
((in[0]&0xf8) == 0xf0 && CONT(in,1)&&CONT(in,2)&&CONT(in,3))
#define IS_UTF8_5BYTE(in) \
((in[0]&0xfc) == 0xf8 && CONT(in,1)&&CONT(in,2)&&CONT(in,3)&&CONT(in,4))
#define IS_UTF8_6BYTE(in) \
((in[0]&0xfe) == 0xfc && CONT(in,1)&&CONT(in,2)&&CONT(in,3)&&CONT(in,4)&&CONT(in,5))
static size_t
check_utf8_seq(char *s, size_t len)
{ size_t count = 0;
size_t skip;
while(len > 0)
{ if ( (*s&0x80) )
{ if ( IS_UTF8_2BYTE(s) ) skip = 2;
else if ( IS_UTF8_3BYTE(s) ) skip = 3;
else if ( IS_UTF8_4BYTE(s) ) skip = 4;
else if ( IS_UTF8_5BYTE(s) ) skip = 5;
else if ( IS_UTF8_6BYTE(s) ) skip = 6;
else assert(0);
} else
skip = 1;
assert(len >= skip);
len -= skip;
s += skip;
count++;
}
return count;
}
static void
check_memfile(memfile *mf)
{ size_t count = 0;
count += check_utf8_seq(&mf->data[0], mf->gap_start);
count += check_utf8_seq(&mf->data[mf->gap_start+mf->gap_size],
mf->end-(mf->gap_size+mf->gap_start));
assert(mf->char_count == NOSIZE || mf->char_count == count);
}
#else /*O_SECURE*/
#define check_memfile(mf) (void)0
#endif /*O_SECURE*/
/*******************************
* STREAM FUNCTIONS *
*******************************/
#define CHECK_MEMFILE(m) \
if ( m->magic != MEMFILE_MAGIC ) \
{ errno = EINVAL; \
return -1; \
}
static ssize_t
read_memfile(void *handle, char *buf, size_t size)
{ memfile *m = handle;
size_t done = 0;
CHECK_MEMFILE(m);
if ( m->here < m->gap_start )
{ size_t before_gap = m->gap_start - m->here;
if ( size <= before_gap )
{ memcpy(buf, &m->data[m->here], size);
m->here += size;
return size;
} else
{ memcpy(buf, &m->data[m->here], before_gap);
m->here += before_gap;
done = before_gap;
}
}
/* we are now after the gap */
{ size_t left = size - done;
size_t start = m->here + m->gap_size;
size_t avail = m->end - start;
if ( avail < left )
{ left = avail;
size = done + avail;
}
m->here += left;
memcpy(&buf[done], &m->data[start], left);
return size;
}
}
static size_t
memfile_nextsize(size_t needed)
{ size_t size = 512;
while ( size < needed )
size *= 2;
return size;
}
static int
ensure_gap_size(memfile *m, size_t size)
{ if ( m->gap_size < size )
{ size_t nextsize = memfile_nextsize(m->end+(size-m->gap_size));
void *ptr;
if ( m->data )
ptr = realloc(m->data, nextsize);
else
ptr = malloc(nextsize);
if ( ptr != NULL )
{ size_t after_gap = m->end - (m->gap_start + m->gap_size);
m->data = ptr;
memmove(&m->data[nextsize-after_gap], &m->data[m->end-after_gap], after_gap);
m->gap_size += nextsize - m->end;
m->end = nextsize;
} else
{ return -1;
}
}
return 0;
}
static void
move_gap_to(memfile *m, size_t to)
{ assert(to <= m->end - m->gap_size);
if ( to != m->gap_start )
{ if ( to > m->gap_start ) /* move forwards */
{ memmove(&m->data[m->gap_start],
&m->data[m->gap_start+m->gap_size],
to - m->gap_start);
m->gap_start = to;
} else /* move backwards */
{ memmove(&m->data[to+m->gap_size],
&m->data[to],
m->gap_start - to);
m->gap_start = to;
}
}
}
static ssize_t
write_memfile(void *handle, char *buf, size_t size)
{ memfile *m = handle;
int rc;
CHECK_MEMFILE(m);
if ( size > 0 )
{ m->char_count = NOSIZE; /* TBD: Dynamically update? */
if ( m->gap_start < m->pcache.byte_count )
m->pcache.valid = 0;
if ( m->mode == ATOM_update )
{ size_t start = m->gap_start + m->gap_size;
size_t after = m->end - start;
if ( size > after )
{ if ( (rc=ensure_gap_size(m, size-after)) != 0 )
return rc;
m->gap_size -= size-after;
}
memmove(&m->data[m->gap_start], buf, size);
m->gap_start += size;
} else
{ if ( (rc=ensure_gap_size(m, size)) != 0 )
return rc;
memcpy(&m->data[m->gap_start], buf, size);
m->gap_start += size;
m->gap_size -= size;
}
}
return size;
}
static int64_t
seek64_memfile(void *handle, int64_t offset, int whence)
{ memfile *m = handle;
CHECK_MEMFILE(m);
switch(whence)
{ case SIO_SEEK_SET:
break;
case SIO_SEEK_CUR:
offset += m->here;
break;
case SIO_SEEK_END:
offset = (m->end-m->gap_size) - offset;
break;
default:
errno = EINVAL;
return -1;
}
if ( offset < 0 || offset > (int64_t)(m->end - m->gap_size) )
{ errno = EINVAL;
return -1;
}
if ( (m->stream->flags & SIO_INPUT) ) /* reading */
{ m->here = offset;
} else
{ move_gap_to(m, offset);
}
return offset;
}
static long
seek_memfile(void *handle, long offset, int whence)
{ return (long)seek64_memfile(handle, (int64_t)offset, whence);
}
static int
close_memfile(void *handle)
{ memfile *m = handle;
CHECK_MEMFILE(m);
m->stream = NULL;
m->mode = 0;
if ( m->free_on_close )
clean_memory_file(m);
PL_unregister_atom(m->symbol);
return 0;
}
IOFUNCTIONS memfile_functions =
{ read_memfile,
write_memfile,
seek_memfile,
close_memfile,
NULL, /* control */
seek64_memfile
};
static foreign_t
alreadyOpen(term_t handle, const char *op)
{ return pl_error(NULL, 0, "already open",
ERR_PERMISSION, handle, op, "memory_file");
}
static struct encname
{ IOENC code;
atom_t *name;
} encoding_names[] =
{ { ENC_UNKNOWN, &ATOM_unknown },
{ ENC_OCTET, &ATOM_octet },
{ ENC_ASCII, &ATOM_ascii },
{ ENC_ISO_LATIN_1, &ATOM_iso_latin_1 },
{ ENC_ANSI, &ATOM_text },
{ ENC_UTF8, &ATOM_utf8 },
{ ENC_UNICODE_BE, &ATOM_unicode_be },
{ ENC_UNICODE_LE, &ATOM_unicode_le },
{ ENC_WCHAR, &ATOM_wchar_t },
{ ENC_UNKNOWN, NULL },
};
static IOENC
atom_to_encoding(atom_t a)
{ struct encname *en;
for(en=encoding_names; en->name; en++)
{ if ( *en->name == a )
return en->code;
}
return ENC_UNKNOWN;
}
static int
get_encoding(term_t t, IOENC *enc)
{ atom_t en;
if ( PL_get_atom(t, &en) )
{ IOENC encoding;
if ( (encoding = atom_to_encoding(en)) == ENC_UNKNOWN )
return pl_error(NULL, 0, NULL, ERR_DOMAIN, t, "encoding");
*enc = encoding;
return TRUE;
}
return pl_error(NULL, 0, NULL, ERR_TYPE, t, "encoding");
}
static foreign_t
open_memory_file4(term_t handle, term_t mode, term_t stream, term_t options)
{ memfile *m;
foreign_t rc;
if ( get_memfile(handle, &m) )
{ int flags = SIO_FBUF|SIO_RECORDPOS|SIO_NOMUTEX;
atom_t iom;
IOSTREAM *fd;
IOENC encoding;
int free_on_close = FALSE;
if ( m->stream )
{ rc = alreadyOpen(handle, "open");
goto out;
}
if ( !PL_get_atom(mode, &iom) )
{ rc = pl_error("open_memory_file", 3, NULL, ERR_ARGTYPE, 2,
mode, "io_mode");
goto out;
}
encoding = m->encoding;
if ( options )
{ term_t tail = PL_copy_term_ref(options);
term_t head = PL_new_term_ref();
while(PL_get_list(tail, head, tail))
{ size_t arity;
atom_t name;
if ( PL_get_name_arity(head, &name, &arity) && arity == 1 )
{ term_t arg = PL_new_term_ref();
_PL_get_arg(1, head, arg);
if ( name == ATOM_encoding )
{ if ( !get_encoding(arg, &encoding) )
{ rc = FALSE;
goto out;
}
} else if ( name == ATOM_free_on_close )
{ if ( !PL_get_bool(arg, &free_on_close) )
{ rc = pl_error("open_memory_file", 4, NULL, ERR_TYPE,
arg, "boolean");
goto out;
}
}
} else
{ rc = pl_error("open_memory_file", 4, NULL, ERR_TYPE, head, "option");
goto out;
}
}
if ( !PL_get_nil(tail) )
{ rc = pl_error("open_memory_file", 4, NULL, ERR_TYPE, tail, "list");
goto out;
}
}
if ( iom == ATOM_write || iom == ATOM_append ||
iom == ATOM_update || iom == ATOM_insert )
{ flags |= SIO_OUTPUT;
if ( m->atom )
{ rc = pl_error("open_memory_file", 3, "read only",
ERR_PERMISSION, handle, "modify", "memory_file");
goto out;
}
if ( iom == ATOM_write )
{ empty_memory_file(m);
m->encoding = encoding;
} else
{ if ( m->encoding != encoding )
{ rc = pl_error("open_memory_file", 3, "inconsistent encoding",
ERR_PERMISSION, handle, PL_atom_chars(iom), "memory_file");
goto out;
}
if ( iom == ATOM_append )
{ move_gap_to(m, m->end - m->gap_size);
} else
{ move_gap_to(m, 0);
}
}
} else if ( iom == ATOM_read )
{ flags |= SIO_INPUT;
m->free_on_close = free_on_close;
m->here = 0;
} else
{ rc = pl_error("open_memory_file", 3, NULL, ERR_DOMAIN,
mode, "io_mode");
goto out;
}
if ( encoding != ENC_OCTET )
flags |= SIO_TEXT;
if ( !(fd = Snew(m, flags, &memfile_functions)) )
{ rc = pl_error("open_memory_file", 3, NULL, ERR_ERRNO, errno,
"create", "memory_file", handle);
goto out;
}
if ( (rc=PL_unify_stream(stream, fd)) )
{ fd->encoding = encoding;
fd->newline = SIO_NL_POSIX;
m->stream = fd;
m->mode = iom;
PL_register_atom(m->symbol);
} else
{ Sclose(fd);
}
out:
release_memfile(m);
} else
rc = FALSE;
return rc;
}
static foreign_t
open_memory_file(term_t handle, term_t mode, term_t stream)
{ return open_memory_file4(handle, mode, stream, 0);
}
static int
get_size_mf(memfile *m, IOENC encoding, size_t *sizep)
{ size_t size;
if ( m->char_count != NOSIZE && encoding == m->encoding )
{ size = m->char_count;
} else
{ size = m->end - m->gap_size;
switch( encoding )
{ case ENC_ISO_LATIN_1:
case ENC_OCTET:
case ENC_ASCII:
break;
case ENC_UNICODE_BE:
case ENC_UNICODE_LE:
size /= 2;
break;
case ENC_WCHAR:
size /= sizeof(wchar_t);
break;
case ENC_UTF8:
{ size_t gap_end = m->gap_start+m->gap_size;
/* assumes UTF-8 sequences are not broken over the gap */
size = PL_utf8_strlen(m->data, m->gap_start);
size += PL_utf8_strlen(&m->data[gap_end], m->end-gap_end);
break;
}
default:
assert(0);
return FALSE;
}
if ( encoding == m->encoding )
m->char_count = size;
}
*sizep = size;
return TRUE;
}
static foreign_t
size_memory_file(term_t handle, term_t sizeh, term_t encoding)
{ memfile *m;
foreign_t rc;
if ( get_memfile(handle, &m) )
{ size_t size;
IOENC size_enc;
if ( m->stream && !m->atom )
{ rc = alreadyOpen(handle, "size");
goto out;
}
if ( encoding )
{ if ( !get_encoding(encoding, &size_enc) )
{ rc = FALSE;
goto out;
}
} else
size_enc = m->encoding;
rc = ( get_size_mf(m, size_enc, &size) &&
PL_unify_int64(sizeh, size)
);
out:
release_memfile(m);
} else
rc = FALSE;
return rc;
}
static foreign_t
size_memory_file2(term_t handle, term_t size)
{ return size_memory_file(handle, size, 0);
}
static foreign_t
size_memory_file3(term_t handle, term_t size, term_t encoding)
{ return size_memory_file(handle, size, encoding);
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
utf8_position_memory_file(+MF, -Here, -Size)
Given MF is a UTF-8 encoded memory file, unify here with the
byte-position of the read-pointer and Size with the total size of the
memory file in bytes. This is a bit hacky predicate, but the information
is easily available at low cost, while it is very valuable for producing
answers in content-length computation of the HTTP server. See
http_wrapper.pl
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
static foreign_t
utf8_position(term_t handle, term_t here, term_t size)
{ memfile *m;
int rc;
if ( get_memfile(handle, &m) )
{ if ( m->encoding != ENC_UTF8 )
{ rc = pl_error(NULL, 0, "no UTF-8 encoding",
ERR_PERMISSION, handle, "utf8_position", "memory_file");
goto out;
}
if ( !PL_unify_integer(size, m->end - m->gap_size) )
{ rc = FALSE;
goto out;
}
if ( m->stream )
{ IOPOS *op = m->stream->position;
long p;
m->stream->position = NULL;
p = Stell(m->stream);
m->stream->position = op;
rc = PL_unify_integer(here, p);
} else
rc = PL_unify_integer(here, 0);
out:
release_memfile(m);
} else
rc = FALSE;
return rc;
}
static foreign_t
atom_to_memory_file(term_t atom, term_t handle)
{ atom_t a;
if ( PL_get_atom(atom, &a) )
{ memfile *m = calloc(1, sizeof(*m));
if ( !m )
return pl_error(NULL, 0, NULL, ERR_ERRNO, errno,
"create", "memory_file", handle);
m->atom = a;
PL_register_atom(m->atom);
m->magic = MEMFILE_MAGIC;
if ( (m->data = (char *)PL_atom_nchars(a, &m->char_count)) )
{ m->encoding = ENC_ISO_LATIN_1;
m->end = m->char_count;
m->gap_start = m->end;
} else if ( (m->data = (char *)PL_atom_wchars(a, &m->char_count)) )
{ m->encoding = ENC_WCHAR;
m->end = m->char_count * sizeof(wchar_t);
m->gap_start = m->end;
} else if ( PL_blob_data(a, &m->char_count, NULL) )
{ m->data = PL_blob_data(a, &m->end, NULL);
m->encoding = ENC_OCTET;
m->char_count = m->end;
m->gap_start = m->end;
}
#ifdef O_PLMT
pthread_mutex_init(&m->mutex, NULL);
#endif
if ( unify_memfile(handle, m) )
{ return TRUE;
} else
{ destroy_memory_file(m);
return FALSE;
}
} else
{ return pl_error(NULL, 0, NULL, ERR_ARGTYPE, 1,
atom, "atom");
}
}
/*******************************
* DIRECT EXCHANGE *
*******************************/
static foreign_t
can_modify_memory_file(term_t handle, memfile *mf)
{ if ( mf->atom )
return pl_error(NULL, 0, "read only",
ERR_PERMISSION, handle, "modify", "memory_file");
if ( mf->stream )
return alreadyOpen(handle, "modify");
return TRUE;
}
/* Get the byte offset for a character
*/
static char *
utf8_skip_char(const char *in, const char *e)
{ if ( !(in[0]&0x80) )
{ return (char*)in+1;
} else
{ in++;
while ( in < e && ISUTF8_CB(in[0]) )
in++;
return (char*)in;
}
}
/* Skip chars forward from a byte position, returning the new
byte position in the memory file or NOSIZE if we would skip
outside the limits of the memory file. Returns:
OUTOFRANGE if memfile is too small
FALSE if the encoding is not supported
TRUE if ok
*/
#define OUTOFRANGE -1
static int
mf_skip(memfile *mf, IOENC encoding, size_t from, size_t chars, size_t *end)
{ size_t to;
switch(encoding)
{ case ENC_OCTET:
case ENC_ASCII:
case ENC_ISO_LATIN_1:
to = from+chars;
break;
case ENC_UTF8:
{ const char *start, *s, *e;
const size_t chars0 = (from == 0 ? chars : NOSIZE);
if ( from == 0 &&
(mf->pcache.valid & V_CHARCOUNT) &&
chars >= mf->pcache.char_count )
{ from = mf->pcache.byte_count;
chars -= mf->pcache.char_count;
}
if ( from < mf->gap_start )
{ start = s = &mf->data[from];
e = &mf->data[mf->gap_start];
while(chars>0 && s<e)
{ chars--;
s = utf8_skip_char(s, e);
}
from += s - start;
if ( chars == 0 )
{ utf8_out:
if ( chars0 != NOSIZE )
{ mf->pcache.char_count = chars0;
mf->pcache.byte_count = from;
mf->pcache.valid |= V_CHARCOUNT;