You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

91 lines
2.4KB

  1. /*
  2. * lzf decompression algorithm
  3. * Copyright (c) 2015 Luca Barbato
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * Libav is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * lzf decompression
  24. *
  25. * LZF is a fast compression/decompression algorithm that takes very little
  26. * code space and working memory, ideal for real-time and block compression.
  27. *
  28. * https://en.wikibooks.org/wiki/Data_Compression/Dictionary_compression#LZF
  29. */
  30. #include "libavutil/mem.h"
  31. #include "bytestream.h"
  32. #include "lzf.h"
  33. #define LZF_LITERAL_MAX (1 << 5)
  34. #define LZF_LONG_BACKREF 7 + 2
  35. int ff_lzf_uncompress(GetByteContext *gb, uint8_t **buf, int64_t *size)
  36. {
  37. int ret = 0;
  38. uint8_t *p = *buf;
  39. int64_t len = 0;
  40. while (bytestream2_get_bytes_left(gb) > 2) {
  41. uint8_t s = bytestream2_get_byte(gb);
  42. if (s < LZF_LITERAL_MAX) {
  43. s++;
  44. if (s > *size - len) {
  45. *size += *size /2;
  46. ret = av_reallocp(buf, *size);
  47. if (ret < 0)
  48. return ret;
  49. }
  50. bytestream2_get_buffer(gb, p, s);
  51. p += s;
  52. len += s;
  53. } else {
  54. int l = 2 + (s >> 5);
  55. int off = ((s & 0x1f) << 8) + 1;
  56. if (l == LZF_LONG_BACKREF)
  57. l += bytestream2_get_byte(gb);
  58. off += bytestream2_get_byte(gb);
  59. if (off > len)
  60. return AVERROR_INVALIDDATA;
  61. if (l > *size - len) {
  62. *size += *size / 2;
  63. ret = av_reallocp(buf, *size);
  64. if (ret < 0)
  65. return ret;
  66. }
  67. av_memcpy_backptr(p, off, l);
  68. p += l;
  69. len += l;
  70. }
  71. }
  72. *size = len;
  73. return 0;
  74. }