vli_decoder.c 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. /// \file vli_decoder.c
  4. /// \brief Decodes variable-length integers
  5. //
  6. // Author: Lasse Collin
  7. //
  8. // This file has been put into the public domain.
  9. // You can do whatever you want with this file.
  10. //
  11. ///////////////////////////////////////////////////////////////////////////////
  12. #include "common.h"
  13. extern LZMA_API(lzma_ret)
  14. lzma_vli_decode(lzma_vli *restrict vli, size_t *vli_pos,
  15. const uint8_t *restrict in, size_t *restrict in_pos,
  16. size_t in_size)
  17. {
  18. // If we haven't been given vli_pos, work in single-call mode.
  19. size_t vli_pos_internal = 0;
  20. if (vli_pos == NULL) {
  21. vli_pos = &vli_pos_internal;
  22. *vli = 0;
  23. // If there's no input, use LZMA_DATA_ERROR. This way it is
  24. // easy to decode VLIs from buffers that have known size,
  25. // and get the correct error code in case the buffer is
  26. // too short.
  27. if (*in_pos >= in_size)
  28. return LZMA_DATA_ERROR;
  29. } else {
  30. // Initialize *vli when starting to decode a new integer.
  31. if (*vli_pos == 0)
  32. *vli = 0;
  33. // Validate the arguments.
  34. if (*vli_pos >= LZMA_VLI_BYTES_MAX
  35. || (*vli >> (*vli_pos * 7)) != 0)
  36. return LZMA_PROG_ERROR;;
  37. if (*in_pos >= in_size)
  38. return LZMA_BUF_ERROR;
  39. }
  40. do {
  41. // Read the next byte. Use a temporary variable so that we
  42. // can update *in_pos immediately.
  43. const uint8_t byte = in[*in_pos];
  44. ++*in_pos;
  45. // Add the newly read byte to *vli.
  46. *vli += (lzma_vli)(byte & 0x7F) << (*vli_pos * 7);
  47. ++*vli_pos;
  48. // Check if this is the last byte of a multibyte integer.
  49. if ((byte & 0x80) == 0) {
  50. // We don't allow using variable-length integers as
  51. // padding i.e. the encoding must use the most the
  52. // compact form.
  53. if (byte == 0x00 && *vli_pos > 1)
  54. return LZMA_DATA_ERROR;
  55. return vli_pos == &vli_pos_internal
  56. ? LZMA_OK : LZMA_STREAM_END;
  57. }
  58. // There is at least one more byte coming. If we have already
  59. // read maximum number of bytes, the integer is considered
  60. // corrupt.
  61. //
  62. // If we need bigger integers in future, old versions liblzma
  63. // will confusingly indicate the file being corrupt instead of
  64. // unsupported. I suppose it's still better this way, because
  65. // in the foreseeable future (writing this in 2008) the only
  66. // reason why files would appear having over 63-bit integers
  67. // is that the files are simply corrupt.
  68. if (*vli_pos == LZMA_VLI_BYTES_MAX)
  69. return LZMA_DATA_ERROR;
  70. } while (*in_pos < in_size);
  71. return vli_pos == &vli_pos_internal ? LZMA_DATA_ERROR : LZMA_OK;
  72. }