1 #define PERL_NO_GET_CONTEXT
7 /* Something arbitary for a buffer size */
8 #define BYTELOADER_BUFFER 8096
11 bl_getc(struct byteloader_fdata *data)
14 if (SvCUR(data->datasv) <= data->next_out) {
16 /* Run out of buffered data, so attempt to read some more */
17 *(SvPV_nolen (data->datasv)) = '\0';
18 SvCUR_set (data->datasv, 0);
20 result = FILTER_READ (data->idx + 1, data->datasv, BYTELOADER_BUFFER);
22 /* Filter returned error, or we got EOF and no data, then return EOF.
23 Not sure if filter is allowed to return EOF and add data simultaneously
24 Think not, but will bullet proof against it. */
25 if (result < 0 || SvCUR(data->datasv) == 0)
27 /* Else there must be at least one byte present, which is good enough */
30 return *((char *) SvPV_nolen (data->datasv) + data->next_out++);
34 bl_read(struct byteloader_fdata *data, char *buf, size_t size, size_t n)
39 size_t wanted = size * n;
41 start = SvPV (data->datasv, len);
42 if (len < (data->next_out + wanted)) {
45 /* Shuffle data to start of buffer */
46 len -= data->next_out;
48 memmove (start, start + data->next_out, len + 1);
49 SvCUR_set (data->datasv, len);
51 *start = '\0'; /* Avoid call to memmove. */
52 SvCUR_set (data->datasv, 0);
56 /* Attempt to read more data. */
58 result = FILTER_READ (data->idx + 1, data->datasv, BYTELOADER_BUFFER);
60 start = SvPV (data->datasv, len);
61 } while (result > 0 && len < wanted);
62 /* Loop while not (EOF || error) and short reads */
64 /* If not enough data read, truncate copy */
70 memcpy (buf, start + data->next_out, wanted);
71 data->next_out += wanted;
78 byteloader_filter(pTHX_ int idx, SV *buf_sv, int maxlen)
80 OP *saveroot = PL_main_root;
81 OP *savestart = PL_main_start;
82 struct byteloader_state bstate;
83 struct byteloader_fdata data;
86 data.datasv = FILTER_DATA(idx);
89 bstate.bs_fdata = &data;
90 bstate.bs_obj_list = Null(void**);
91 bstate.bs_obj_list_fill = -1;
92 bstate.bs_sv = Nullsv;
93 bstate.bs_iv_overflows = 0;
95 byterun(aTHX_ &bstate);
100 PL_eval_start = PL_main_start;
102 o = newSVOP(OP_CONST, 0, newSViv(1));
103 PL_eval_root = newLISTOP(OP_LINESEQ, 0, PL_main_root, o);
104 PL_main_root->op_next = o;
105 PL_eval_root = newUNOP(OP_LEAVEEVAL, 0, PL_eval_root);
106 o->op_next = PL_eval_root;
108 PL_main_root = saveroot;
109 PL_main_start = savestart;
115 MODULE = ByteLoader PACKAGE = ByteLoader
120 import(package="ByteLoader", ...)
123 SV *sv = newSVpvn ("", 0);
126 croak ("Could not allocate ByteLoader buffers");
127 filter_add(byteloader_filter, sv);
130 unimport(package="ByteLoader", ...)
133 filter_del(byteloader_filter);