vli_encoder.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. /// \file vli_encoder.c
  4. /// \brief Encodes 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_encode(lzma_vli vli, size_t *vli_pos,
  15. uint8_t *restrict out, size_t *restrict out_pos,
  16. size_t out_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. // In single-call mode, we expect that the caller has
  23. // reserved enough output space.
  24. if (*out_pos >= out_size)
  25. return LZMA_PROG_ERROR;
  26. } else {
  27. // This never happens when we are called by liblzma, but
  28. // may happen if called directly from an application.
  29. if (*out_pos >= out_size)
  30. return LZMA_BUF_ERROR;
  31. }
  32. // Validate the arguments.
  33. if (*vli_pos >= LZMA_VLI_BYTES_MAX || vli > LZMA_VLI_MAX)
  34. return LZMA_PROG_ERROR;
  35. // Shift vli so that the next bits to encode are the lowest. In
  36. // single-call mode this never changes vli since *vli_pos is zero.
  37. vli >>= *vli_pos * 7;
  38. // Write the non-last bytes in a loop.
  39. while (vli >= 0x80) {
  40. // We don't need *vli_pos during this function call anymore,
  41. // but update it here so that it is ready if we need to
  42. // return before the whole integer has been decoded.
  43. ++*vli_pos;
  44. assert(*vli_pos < LZMA_VLI_BYTES_MAX);
  45. // Write the next byte.
  46. out[*out_pos] = (uint8_t)(vli) | 0x80;
  47. vli >>= 7;
  48. if (++*out_pos == out_size)
  49. return vli_pos == &vli_pos_internal
  50. ? LZMA_PROG_ERROR : LZMA_OK;
  51. }
  52. // Write the last byte.
  53. out[*out_pos] = (uint8_t)(vli);
  54. ++*out_pos;
  55. ++*vli_pos;
  56. return vli_pos == &vli_pos_internal ? LZMA_OK : LZMA_STREAM_END;
  57. }