19 #ifndef _XDELTA3_DECODE_H_ 20 #define _XDELTA3_DECODE_H_ 22 #define SRCORTGT(x) ((((x) & VCD_SRCORTGT) == VCD_SOURCE) ? \ 23 VCD_SOURCE : ((((x) & VCD_SRCORTGT) == \ 24 VCD_TARGET) ? VCD_TARGET : 0)) 58 stream->
msg =
"unsupported VCD_TARGET offset";
86 (uint8_t*) xd3_alloc (stream, stream->
space_out, 1)) == NULL)
109 if (*buf_ptr != NULL && *buf_alloc < size)
111 xd3_free (stream, *buf_ptr);
115 if (*buf_ptr == NULL)
119 if ((*buf_ptr = (uint8_t*) xd3_alloc (stream, *buf_alloc, 1)) == NULL)
137 if (section->
pos < section->
size)
146 if ((copy == 0) && (section->
pos == 0))
150 sect_take = section->
size;
159 if (section->
pos == 0)
179 section->
pos += sect_take;
183 DECODE_INPUT (sect_take);
186 if (section->
pos < section->
size)
188 stream->
msg =
"further input required";
211 if ((inst->
size == 0) &&
212 (ret = xd3_read_size (stream,
221 if (inst->
type >= XD3_CPY)
225 DP(
RINT "DECODE:%u: COPY at %"Q"u (winoffset %u) size %u winaddr %u\n",
234 if ((ret = xd3_decode_address (stream,
236 inst->
type - XD3_CPY,
247 stream->
msg =
"address too large";
256 stream->
msg =
"size too large";
263 if (inst->
type == XD3_ADD)
266 DP(RINT
"DECODE:%d: ADD at %"Q
"u (winoffset %u) size %u\n",
268 (stream->total_out + stream->dec_position - stream->dec_cpylen),
269 stream->dec_position - stream->dec_cpylen,
275 XD3_ASSERT (inst->type == XD3_RUN);
276 DP(RINT
"DECODE:%d: RUN at %"Q
"u (winoffset %u) size %u\n",
278 stream->total_out + stream->dec_position - stream->dec_cpylen,
279 stream->dec_position - stream->dec_cpylen,
288 stream->
msg =
"size too large";
305 stream->
msg =
"instruction underflow";
320 if (inst->
type1 != XD3_NOOP &&
325 if (inst->
type2 != XD3_NOOP &&
353 stream->
msg =
"data underflow";
363 inst->
type = XD3_NOOP;
371 stream->
msg =
"data underflow";
381 inst->
type = XD3_NOOP;
417 inst->
type = XD3_NOOP;
418 stream->
msg =
"VCD_TARGET not implemented";
436 if ((ret = xd3_getblk (stream, block)))
441 stream->
msg =
"non-seekable source in decode";
447 src = source->
curblk + blkoff;
451 if ((source->
onblk != blksize) &&
452 (blkoff + take > source->
onblk))
454 IF_DEBUG1(
DP(
RINT "[srcfile] short at blkno %"Q"u onblk " 455 "%u blksize %u blkoff %u take %u\n",
461 stream->
msg =
"source file too short";
469 if (blkoff + take <= blksize)
471 inst->
type = XD3_NOOP;
476 take = blksize - blkoff;
498 inst->
type = XD3_NOOP;
509 for (i = take; i != 0; i -= 1)
516 memcpy (dst, src, take);
542 #define DECODE_SECONDARY_SECTION(UPPER,LOWER) \ 543 ((secondary_stream->dec_del_ind & VCD_ ## UPPER ## COMP) && \ 544 (ret = xd3_decode_secondary (secondary_stream, \ 545 & secondary_stream-> LOWER ## _sect, \ 546 & xd3_sec_ ## LOWER (secondary_stream)))) 548 if (DECODE_SECONDARY_SECTION (DATA, data) ||
549 DECODE_SECONDARY_SECTION (INST, inst) ||
550 DECODE_SECONDARY_SECTION (ADDR, addr))
554 #undef DECODE_SECONDARY_SECTION 586 copy = (take != more);
598 stream->
msg =
"further input required";
609 stream->
msg =
"internal error";
687 IF_DEBUG2 (
DP(
RINT "AVAIL_OUT(%d) != DEC_TGTLEN(%d)\n",
689 stream->
msg =
"wrong window length";
695 stream->
msg =
"extra data section";
701 stream->
msg =
"extra address section";
713 stream->
msg =
"target window checksum mismatch";
729 stream->
msg =
"encoder/decoder transition";
733 #define BYTE_CASE(expr,x,nstate) \ 736 ((ret = xd3_decode_byte (stream, & (x))) != 0) ) { return ret; } \ 737 stream->dec_state = (nstate); \ 740 #define OFFSET_CASE(expr,x,nstate) \ 743 ((ret = xd3_decode_offset (stream, & (x))) != 0) ) { return ret; } \ 744 stream->dec_state = (nstate); \ 747 #define SIZE_CASE(expr,x,nstate) \ 750 ((ret = xd3_decode_size (stream, & (x))) != 0) ) { return ret; } \ 751 stream->dec_state = (nstate); \ 758 if ((ret = xd3_decode_bytes (stream, stream->
dec_magic,
764 if (stream->
dec_magic[0] != VCDIFF_MAGIC1 ||
768 stream->
msg =
"not a VCDIFF input";
774 stream->
msg =
"VCDIFF input version > 0 is not supported";
782 if ((ret = xd3_decode_byte (stream, & stream->
dec_hdr_ind)))
789 stream->
msg =
"unrecognized header indicator bits set";
809 stream->
msg =
"unknown secondary compressor ID";
822 stream->
msg =
"invalid code table size";
843 (uint8_t*) xd3_alloc (stream,
849 if ((ret = xd3_decode_bytes (stream, stream->
dec_codetbl,
856 if ((ret = xd3_apply_table_encoding (stream, stream->
dec_codetbl,
865 stream->
acache.
s_near = __rfc3284_code_table_desc.near_modes;
866 stream->
acache.
s_same = __rfc3284_code_table_desc.same_modes;
867 stream->
code_table = xd3_rfc3284_code_table ();
870 if ((ret = xd3_alloc_cache (stream))) {
return ret; }
887 (uint8_t*) xd3_alloc (stream,
910 if ((ret = xd3_decode_byte (stream, & stream->
dec_win_ind)))
919 stream->
msg =
"decoder file offset overflow";
927 stream->
msg =
"unrecognized window indicator bits set";
935 IF_DEBUG2 (
DP(
RINT "--------- TARGET WINDOW %"Q"u -----------\n",
958 stream->
msg =
"decoder copy window overflows a file offset";
968 stream->
msg =
"VCD_TARGET window out of bounds";
984 stream->
msg =
"decoder target window overflows a usize_t";
991 stream->
msg =
"hard window size exceeded";
1003 stream->
msg =
"unrecognized delta indicator bits set";
1010 stream->
msg =
"invalid delta indicator bits set";
1028 if ((ret = xd3_decode_bytes (stream, stream->
dec_cksum,
1034 for (i = 0; i < 4; i += 1)
1057 stream->
msg =
"incorrect encoding length (redundent)";
1083 stream->
msg =
"source input required";
1092 "decode cpyoff %"Q"u " 1139 stream->
msg =
"invalid state";
1144 #endif // _XDELTA3_DECODE_H_
static int xd3_decode_instruction(xd3_stream *stream)
#define XD3_HARDMAXWINSIZE
const uint8_t * dec_tgtaddrbase
const xd3_sec_type * sec_type
static int xd3_decode_setup_buffers(xd3_stream *stream)
static int xd3_decode_parse_halfinst(xd3_stream *stream, xd3_hinst *inst)
static int xd3_decode_section(xd3_stream *stream, xd3_desect *section, xd3_decode_state nstate, int copy)
static int xd3_decode_allocate(xd3_stream *stream, usize_t size, uint8_t **buf_ptr, usize_t *buf_alloc)
void xd3_init_cache(xd3_addr_cache *acache)
int xd3_decode_input(xd3_stream *stream)
xd3_decode_state dec_state
static int xd3_decode_sections(xd3_stream *stream)
const xd3_dinst * code_table
static int xd3_decode_init_window(xd3_stream *stream)
static void xd3_blksize_div(const xoff_t offset, const xd3_source *source, xoff_t *blkno, usize_t *blkoff)
static void xd3_blksize_add(xoff_t *blkno, usize_t *blkoff, const xd3_source *source, const usize_t add)
xd3_encode_state enc_state
#define OFFSET_CASE(expr, x, nstate)
static int xd3_decode_output_halfinst(xd3_stream *stream, xd3_hinst *inst)
static int xd3_decode_finish_window(xd3_stream *stream)
static int xd3_decode_emit(xd3_stream *stream)
#define BYTE_CASE(expr, x, nstate)
const uint8_t * dec_cpyaddrbase
static int xd3_decode_secondary_sections(xd3_stream *secondary_stream)
#define SIZE_CASE(expr, x, nstate)