UINT16 put_opl(imgtool_stream *instream, imgtool_stream *outstream) { UINT16 out_size = 0; UINT32 rec_start = stream_tell(outstream); char *line; // reset stream stream_seek(instream, 0, SEEK_SET); stream_fill(outstream, 0x00, 4); // replace all eol with 0x00 while ((line = stream_getline(instream, 256))) { // replace tab with space for (int i=0; i<strlen(line); i++) if (line[i] == '\t') line[i] = ' '; stream_write(outstream, line, strlen(line)); stream_putc(outstream, 0x00); out_size += strlen(line) + 1; free(line); } // end of pack stream_fill(outstream, 0xff, 2); // update the size in the head stream_seek(outstream, rec_start + 2, SEEK_SET); stream_putc(outstream, (out_size>>8) & 0xff); stream_putc(outstream, out_size & 0xff); return out_size + 4; }
int stream_seek(imgtool_stream *s, INT64 pos, int where) { UINT64 size; size = stream_size(s); switch(where) { case SEEK_CUR: pos += s->position; break; case SEEK_END: pos += size; break; } if (pos < 0) s->position = 0; else s->position = MIN(size, pos); if (s->position < pos) stream_fill(s, '\0', pos - s->position); return 0; }
UINT16 put_odb(imgtool_stream *instream, imgtool_stream *outstream, UINT8 file_id) { char *line; UINT16 out_size = 0; // reset stream stream_seek(instream, 0, SEEK_SET); while ((line = stream_getline(instream, 256))) { UINT16 len = strlen(line); stream_putc(outstream, (UINT8)len); stream_putc(outstream, file_id); stream_write(outstream, line, len); out_size += (len + 1); free(line); } // end of pack stream_fill(outstream, 0xff, 2); return out_size + 4; }
static imgtoolerr_t write_fork(imgtool_partition *partition, const char *filename, const char *fork, imgtool_stream *sourcef, UINT64 pos, UINT64 fork_len, option_resolution *opts) { imgtoolerr_t err = IMGTOOLERR_SUCCESS; imgtool_stream *mem_stream = NULL; size_t len; if (fork_len > 0) { mem_stream = stream_open_mem(NULL, 0); if (!mem_stream) { err = IMGTOOLERR_OUTOFMEMORY; goto done; } stream_seek(sourcef, pos, SEEK_SET); len = stream_transfer(mem_stream, sourcef, fork_len); if (len < fork_len) stream_fill(mem_stream, 0, fork_len); stream_seek(mem_stream, 0, SEEK_SET); err = imgtool_partition_write_file(partition, filename, fork, mem_stream, opts, NULL); if (err) goto done; } done: if (mem_stream) stream_close(mem_stream); return err; }
/* returns -1 on error, 0 if last lot of data, >0 if more remaining */ int camel_pop3_stream_gets(CamelPOP3Stream *is, unsigned char **start, unsigned int *len) { int max; unsigned char *end; *len = 0; max = is->end - is->ptr; if (max == 0) { max = stream_fill(is); if (max <= 0) return max; } *start = is->ptr; end = memchr(is->ptr, '\n', max); if (end) max = (end - is->ptr) + 1; *start = is->ptr; *len = max; is->ptr += max; dd(printf("POP3_STREAM_GETS(%s,%d): '%.*s'\n", end==NULL?"more":"last", *len, (int)*len, *start)); return end == NULL?1:0; }
static imgtoolerr_t datapack_create( imgtool_image *image, imgtool_stream *stream, util::option_resolution *opts) { psion_pack *pack = (psion_pack*)imgtool_image_extra_bytes(image); static const UINT8 opk_magic[4] = {'O', 'P', 'K', 0x00}; UINT8 pack_head[8] = {0x40, 0x00, 0x59, 0x01, 0x01, 0x01, 0x00, 0x00}; UINT16 checksum; pack_head[0] |= (opts->lookup_int('R')) ? 0x00 : 0x02; pack_head[0] |= (opts->lookup_int('P')) ? 0x04 : 0x00; pack_head[0] |= (opts->lookup_int('W')) ? 0x00 : 0x08; pack_head[0] |= (opts->lookup_int('B')) ? 0x00 : 0x10; pack_head[0] |= (opts->lookup_int('C')) ? 0x20 : 0x00; pack_head[1] = opts->lookup_int('S'); checksum = head_checksum(pack_head); stream_write(stream, opk_magic, 4); stream_fill(stream, 0x00, 2); stream_write(stream, pack_head, 8); stream_putc(stream, (checksum>>8) & 0xff); stream_putc(stream, checksum & 0xff); put_name_record(stream, "MAIN", 0x81, 0x90); stream_fill(stream, 0xff, 2); update_opk_head(stream); pack->stream = stream; if (update_pack_index(pack)) return IMGTOOLERR_SUCCESS; else return IMGTOOLERR_CORRUPTIMAGE; }
UINT16 put_ob3(imgtool_stream *instream, imgtool_stream *outstream) { UINT16 size = stream_size(instream) - 6; dynamic_buffer buffer(size); stream_seek(instream, 6, SEEK_SET); stream_read(instream, &buffer[0], size); stream_write(outstream, &buffer[0], size); // end of pack stream_fill(outstream, 0xff, 2); return size; }
void* streamset8(void* dest, int8_t value, size_t n) { char* dst = dest; // memset up to 15 bytes until SSE-aligned while (((intptr_t) dst & (SSE_SIZE-1)) && n) { *dst++ = value; n--; } // memset SSE-aligned const __m128i data = _mm_set1_epi8(value); dst = stream_fill(dst, &n, data); // memset remainder while (n--) { *dst++ = value; } return dst; }
static gssize stream_read (CamelStream *stream, gchar *buffer, gsize n, GCancellable *cancellable, GError **error) { CamelPOP3Stream *is = (CamelPOP3Stream *) stream; gchar *o, *oe; guchar *p, *e, c; gint state; if (is->mode != CAMEL_POP3_STREAM_DATA || n == 0) return 0; o = buffer; oe = buffer + n; state = is->state; /* Need to copy/strip '.'s and whatnot */ p = is->ptr; e = is->end; switch (state) { state_0: case 0: /* start of line, always read at least 3 chars */ while (e - p < 3) { is->ptr = p; if (stream_fill (is, cancellable, error) == -1) return -1; p = is->ptr; e = is->end; } if (p[0] == '.') { if (p[1] == '\r' && p[2] == '\n') { is->ptr = p + 3; is->mode = CAMEL_POP3_STREAM_EOD; is->state = 0; dd (printf ("POP3_STREAM_READ (%d):\n%.*s\n", (gint)(o-buffer), (gint)(o-buffer), buffer)); return o - buffer; } p++; } state = 1; /* FALLS THROUGH */ case 1: /* looking for next sol */ while (o < oe) { c = *p++; if (c == '\n') { /* end of input sentinal check */ if (p > e) { is->ptr = e; if (stream_fill (is, cancellable, error) == -1) return -1; p = is->ptr; e = is->end; } else { *o++ = '\n'; state = 0; goto state_0; } } else if (c != '\r') { *o++ = c; } } break; } is->ptr = p; is->state = state; dd (printf ("POP3_STREAM_READ (%d):\n%.*s\n", (gint)(o-buffer), (gint)(o-buffer), buffer)); return o - buffer; }
static imgtoolerr_t macbinary_readfile(imgtool_partition *partition, const char *filename, const char *fork, imgtool_stream *destf) { static const UINT32 attrs[] = { IMGTOOLATTR_TIME_CREATED, IMGTOOLATTR_TIME_LASTMODIFIED, IMGTOOLATTR_INT_MAC_TYPE, IMGTOOLATTR_INT_MAC_CREATOR, IMGTOOLATTR_INT_MAC_FINDERFLAGS, IMGTOOLATTR_INT_MAC_COORDX, IMGTOOLATTR_INT_MAC_COORDY, IMGTOOLATTR_INT_MAC_FINDERFOLDER, IMGTOOLATTR_INT_MAC_SCRIPTCODE, IMGTOOLATTR_INT_MAC_EXTENDEDFLAGS, 0 }; imgtoolerr_t err; UINT8 header[128]; const char *basename; int i; UINT32 type_code = 0x3F3F3F3F; UINT32 creator_code = 0x3F3F3F3F; UINT16 finder_flags = 0; UINT16 coord_x = 0; UINT16 coord_y = 0; UINT16 finder_folder = 0; UINT8 script_code = 0; UINT8 extended_flags = 0; imgtool_forkent fork_entries[4]; const imgtool_forkent *data_fork = NULL; const imgtool_forkent *resource_fork = NULL; UINT32 creation_time = 0; UINT32 lastmodified_time = 0; imgtool_attribute attr_values[10]; /* get the forks */ err = imgtool_partition_list_file_forks(partition, filename, fork_entries, sizeof(fork_entries)); if (err) return err; for (i = 0; fork_entries[i].type != FORK_END; i++) { if (fork_entries[i].type == FORK_DATA) data_fork = &fork_entries[i]; else if (fork_entries[i].type == FORK_RESOURCE) resource_fork = &fork_entries[i]; } /* get the attributes */ err = imgtool_partition_get_file_attributes(partition, filename, attrs, attr_values); if (err && (ERRORCODE(err) != IMGTOOLERR_UNIMPLEMENTED)) return err; if (err == IMGTOOLERR_SUCCESS) { creation_time = mac_setup_time(attr_values[0].t); lastmodified_time = mac_setup_time(attr_values[1].t); type_code = attr_values[2].i; creator_code = attr_values[3].i; finder_flags = attr_values[4].i; coord_x = attr_values[5].i; coord_y = attr_values[6].i; finder_folder = attr_values[7].i; script_code = attr_values[8].i; extended_flags = attr_values[9].i; } memset(header, 0, sizeof(header)); /* place filename */ basename = filename; while(basename[strlen(basename) + 1]) basename += strlen(basename) + 1; pascal_from_c_string((unsigned char *) &header[1], 64, basename); place_integer_be(header, 65, 4, type_code); place_integer_be(header, 69, 4, creator_code); place_integer_be(header, 73, 1, (finder_flags >> 8) & 0xFF); place_integer_be(header, 75, 2, coord_x); place_integer_be(header, 77, 2, coord_y); place_integer_be(header, 79, 2, finder_folder); place_integer_be(header, 83, 4, data_fork ? data_fork->size : 0); place_integer_be(header, 87, 4, resource_fork ? resource_fork->size : 0); place_integer_be(header, 91, 4, creation_time); place_integer_be(header, 95, 4, lastmodified_time); place_integer_be(header, 101, 1, (finder_flags >> 0) & 0xFF); place_integer_be(header, 102, 4, 0x6D42494E); place_integer_be(header, 106, 1, script_code); place_integer_be(header, 107, 1, extended_flags); place_integer_be(header, 122, 1, 0x82); place_integer_be(header, 123, 1, 0x81); place_integer_be(header, 124, 2, ccitt_crc16(0, header, 124)); stream_write(destf, header, sizeof(header)); if (data_fork) { err = imgtool_partition_read_file(partition, filename, "", destf, NULL); if (err) return err; stream_fill(destf, 0, pad128(data_fork->size)); } if (resource_fork) { err = imgtool_partition_read_file(partition, filename, "RESOURCE_FORK", destf, NULL); if (err) return err; stream_fill(destf, 0, pad128(resource_fork->size)); } return IMGTOOLERR_SUCCESS; }
/* returns -1 on erorr, 0 if last data, >0 if more data left */ int camel_pop3_stream_getd(CamelPOP3Stream *is, unsigned char **start, unsigned int *len) { unsigned char *p, *e, *s; int state; *len = 0; if (is->mode == CAMEL_POP3_STREAM_EOD) return 0; if (is->mode == CAMEL_POP3_STREAM_LINE) { g_warning("pop3_stream reading data in line mode\n"); return 0; } state = is->state; p = is->ptr; e = is->end; while (e - p < 3) { is->ptr = p; if (stream_fill(is) == -1) return -1; p = is->ptr; e = is->end; } s = p; do { switch(state) { case 0: /* check leading '.', ... */ if (p[0] == '.') { if (p[1] == '\r' && p[2] == '\n') { is->ptr = p+3; *len = p-s; *start = s; is->mode = CAMEL_POP3_STREAM_EOD; is->state = 0; dd(printf("POP3_STREAM_GETD(%s,%d): '%.*s'\n", "last", *len, (int)*len, *start)); return 0; } /* If at start, just skip '.', else return data upto '.' but skip it */ if (p == s) { s++; p++; } else { is->ptr = p+1; *len = p-s; *start = s; is->state = 1; dd(printf("POP3_STREAM_GETD(%s,%d): '%.*s'\n", "more", *len, (int)*len, *start)); return 1; } } state = 1; case 1: /* Scan for sentinal */ while ((*p++)!='\n') ; if (p > e) { p = e; } else { state = 0; } break; } } while ((e-p) >= 3); is->state = state; is->ptr = p; *len = p-s; *start = s; dd(printf("POP3_STREAM_GETD(%s,%d): '%.*s'\n", "more", *len, (int)*len, *start)); return 1; }
/* Get one line from the pop3 stream */ int camel_pop3_stream_line(CamelPOP3Stream *is, unsigned char **data, unsigned int *len) { register unsigned char c, *p, *o, *oe; int newlen, oldlen; unsigned char *e; if (is->mode == CAMEL_POP3_STREAM_EOD) { *data = is->linebuf; *len = 0; return 0; } o = is->linebuf; oe = is->lineend - 1; p = is->ptr; e = is->end; /* Data mode, convert leading '..' to '.', and stop when we reach a solitary '.' */ if (is->mode == CAMEL_POP3_STREAM_DATA) { /* need at least 3 chars in buffer */ while (e-p < 3) { is->ptr = p; if (stream_fill(is) == -1) return -1; p = is->ptr; e = is->end; } /* check for isolated '.\r\n' or begging of line '.' */ if (p[0] == '.') { if (p[1] == '\r' && p[2] == '\n') { is->ptr = p+3; is->mode = CAMEL_POP3_STREAM_EOD; *data = is->linebuf; *len = 0; is->linebuf[0] = 0; dd(printf("POP3_STREAM_LINE(END)\n")); return 0; } p++; } } while (1) { while (o < oe) { c = *p++; if (c == '\n') { /* sentinal? */ if (p> e) { is->ptr = e; if (stream_fill(is) == -1) return -1; p = is->ptr; e = is->end; } else { is->ptr = p; *data = is->linebuf; *len = o - is->linebuf; *o = 0; dd(printf("POP3_STREAM_LINE(%d): '%s'\n", *len, *data)); return 1; } } else if (c != '\r') { *o++ = c; } } /* limit this for bad server data? */ oldlen = o - is->linebuf; newlen = (is->lineend - is->linebuf) * 3 / 2; is->lineptr = is->linebuf = g_realloc(is->linebuf, newlen); is->lineend = is->linebuf + newlen; oe = is->lineend - 1; o = is->linebuf + oldlen; } return -1; }
void* streamset32(void* dest, int32_t value, size_t n) { // memset SSE-aligned const __m128i data = _mm_set1_epi32(value); return stream_fill((char*) dest, &n, data); }