xref: /dflybsd-src/contrib/xz/src/liblzma/common/vli_decoder.c (revision b5feb3da7c498482b19d14ac6f2b1901005f7d94)
12940b44dSPeter Avalos ///////////////////////////////////////////////////////////////////////////////
22940b44dSPeter Avalos //
32940b44dSPeter Avalos /// \file       vli_decoder.c
42940b44dSPeter Avalos /// \brief      Decodes variable-length integers
52940b44dSPeter Avalos //
62940b44dSPeter Avalos //  Author:     Lasse Collin
72940b44dSPeter Avalos //
82940b44dSPeter Avalos //  This file has been put into the public domain.
92940b44dSPeter Avalos //  You can do whatever you want with this file.
102940b44dSPeter Avalos //
112940b44dSPeter Avalos ///////////////////////////////////////////////////////////////////////////////
122940b44dSPeter Avalos 
132940b44dSPeter Avalos #include "common.h"
142940b44dSPeter Avalos 
152940b44dSPeter Avalos 
162940b44dSPeter Avalos extern LZMA_API(lzma_ret)
lzma_vli_decode(lzma_vli * restrict vli,size_t * vli_pos,const uint8_t * restrict in,size_t * restrict in_pos,size_t in_size)172940b44dSPeter Avalos lzma_vli_decode(lzma_vli *restrict vli, size_t *vli_pos,
182940b44dSPeter Avalos 		const uint8_t *restrict in, size_t *restrict in_pos,
192940b44dSPeter Avalos 		size_t in_size)
202940b44dSPeter Avalos {
212940b44dSPeter Avalos 	// If we haven't been given vli_pos, work in single-call mode.
222940b44dSPeter Avalos 	size_t vli_pos_internal = 0;
232940b44dSPeter Avalos 	if (vli_pos == NULL) {
242940b44dSPeter Avalos 		vli_pos = &vli_pos_internal;
252940b44dSPeter Avalos 		*vli = 0;
262940b44dSPeter Avalos 
272940b44dSPeter Avalos 		// If there's no input, use LZMA_DATA_ERROR. This way it is
282940b44dSPeter Avalos 		// easy to decode VLIs from buffers that have known size,
292940b44dSPeter Avalos 		// and get the correct error code in case the buffer is
302940b44dSPeter Avalos 		// too short.
312940b44dSPeter Avalos 		if (*in_pos >= in_size)
322940b44dSPeter Avalos 			return LZMA_DATA_ERROR;
332940b44dSPeter Avalos 
342940b44dSPeter Avalos 	} else {
352940b44dSPeter Avalos 		// Initialize *vli when starting to decode a new integer.
362940b44dSPeter Avalos 		if (*vli_pos == 0)
372940b44dSPeter Avalos 			*vli = 0;
382940b44dSPeter Avalos 
392940b44dSPeter Avalos 		// Validate the arguments.
402940b44dSPeter Avalos 		if (*vli_pos >= LZMA_VLI_BYTES_MAX
412940b44dSPeter Avalos 				|| (*vli >> (*vli_pos * 7)) != 0)
422940b44dSPeter Avalos 			return LZMA_PROG_ERROR;;
432940b44dSPeter Avalos 
442940b44dSPeter Avalos 		if (*in_pos >= in_size)
452940b44dSPeter Avalos 			return LZMA_BUF_ERROR;
462940b44dSPeter Avalos 	}
472940b44dSPeter Avalos 
482940b44dSPeter Avalos 	do {
492940b44dSPeter Avalos 		// Read the next byte. Use a temporary variable so that we
502940b44dSPeter Avalos 		// can update *in_pos immediately.
512940b44dSPeter Avalos 		const uint8_t byte = in[*in_pos];
522940b44dSPeter Avalos 		++*in_pos;
532940b44dSPeter Avalos 
542940b44dSPeter Avalos 		// Add the newly read byte to *vli.
552940b44dSPeter Avalos 		*vli += (lzma_vli)(byte & 0x7F) << (*vli_pos * 7);
562940b44dSPeter Avalos 		++*vli_pos;
572940b44dSPeter Avalos 
582940b44dSPeter Avalos 		// Check if this is the last byte of a multibyte integer.
592940b44dSPeter Avalos 		if ((byte & 0x80) == 0) {
602940b44dSPeter Avalos 			// We don't allow using variable-length integers as
612940b44dSPeter Avalos 			// padding i.e. the encoding must use the most the
622940b44dSPeter Avalos 			// compact form.
632940b44dSPeter Avalos 			if (byte == 0x00 && *vli_pos > 1)
642940b44dSPeter Avalos 				return LZMA_DATA_ERROR;
652940b44dSPeter Avalos 
662940b44dSPeter Avalos 			return vli_pos == &vli_pos_internal
672940b44dSPeter Avalos 					? LZMA_OK : LZMA_STREAM_END;
682940b44dSPeter Avalos 		}
692940b44dSPeter Avalos 
702940b44dSPeter Avalos 		// There is at least one more byte coming. If we have already
712940b44dSPeter Avalos 		// read maximum number of bytes, the integer is considered
722940b44dSPeter Avalos 		// corrupt.
732940b44dSPeter Avalos 		//
742940b44dSPeter Avalos 		// If we need bigger integers in future, old versions liblzma
75*e151908bSDaniel Fojt 		// will confusingly indicate the file being corrupt instead of
762940b44dSPeter Avalos 		// unsupported. I suppose it's still better this way, because
772940b44dSPeter Avalos 		// in the foreseeable future (writing this in 2008) the only
782940b44dSPeter Avalos 		// reason why files would appear having over 63-bit integers
792940b44dSPeter Avalos 		// is that the files are simply corrupt.
802940b44dSPeter Avalos 		if (*vli_pos == LZMA_VLI_BYTES_MAX)
812940b44dSPeter Avalos 			return LZMA_DATA_ERROR;
822940b44dSPeter Avalos 
832940b44dSPeter Avalos 	} while (*in_pos < in_size);
842940b44dSPeter Avalos 
852940b44dSPeter Avalos 	return vli_pos == &vli_pos_internal ? LZMA_DATA_ERROR : LZMA_OK;
862940b44dSPeter Avalos }
87