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.

93 lines
2.5KB

  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. p = *buf + len;
  50. }
  51. bytestream2_get_buffer(gb, p, s);
  52. p += s;
  53. len += s;
  54. } else {
  55. int l = 2 + (s >> 5);
  56. int off = ((s & 0x1f) << 8) + 1;
  57. if (l == LZF_LONG_BACKREF)
  58. l += bytestream2_get_byte(gb);
  59. off += bytestream2_get_byte(gb);
  60. if (off > len)
  61. return AVERROR_INVALIDDATA;
  62. if (l > *size - len) {
  63. *size += *size / 2;
  64. ret = av_reallocp(buf, *size);
  65. if (ret < 0)
  66. return ret;
  67. p = *buf + len;
  68. }
  69. av_memcpy_backptr(p, off, l);
  70. p += l;
  71. len += l;
  72. }
  73. }
  74. *size = len;
  75. return 0;
  76. }