tinfgzip.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * tinfgzip - tiny gzip decompressor
  3. *
  4. * Copyright (c) 2003 by Joergen Ibsen / Jibz
  5. * All Rights Reserved
  6. *
  7. * http://www.ibsensoftware.com/
  8. *
  9. * Copyright (c) 2014-2016 by Paul Sokolovsky
  10. *
  11. * This software is provided 'as-is', without any express
  12. * or implied warranty. In no event will the authors be
  13. * held liable for any damages arising from the use of
  14. * this software.
  15. *
  16. * Permission is granted to anyone to use this software
  17. * for any purpose, including commercial applications,
  18. * and to alter it and redistribute it freely, subject to
  19. * the following restrictions:
  20. *
  21. * 1. The origin of this software must not be
  22. * misrepresented; you must not claim that you
  23. * wrote the original software. If you use this
  24. * software in a product, an acknowledgment in
  25. * the product documentation would be appreciated
  26. * but is not required.
  27. *
  28. * 2. Altered source versions must be plainly marked
  29. * as such, and must not be misrepresented as
  30. * being the original software.
  31. *
  32. * 3. This notice may not be removed or altered from
  33. * any source distribution.
  34. */
  35. #include "tinf.h"
  36. #define FTEXT 1
  37. #define FHCRC 2
  38. #define FEXTRA 4
  39. #define FNAME 8
  40. #define FCOMMENT 16
  41. void tinf_skip_bytes(TINF_DATA *d, int num);
  42. uint16_t tinf_get_uint16(TINF_DATA *d);
  43. void tinf_skip_bytes(TINF_DATA *d, int num)
  44. {
  45. while (num--) uzlib_get_byte(d);
  46. }
  47. uint16_t tinf_get_uint16(TINF_DATA *d)
  48. {
  49. unsigned int v = uzlib_get_byte(d);
  50. v = (uzlib_get_byte(d) << 8) | v;
  51. return v;
  52. }
  53. int uzlib_gzip_parse_header(TINF_DATA *d)
  54. {
  55. unsigned char flg;
  56. /* -- check format -- */
  57. /* check id bytes */
  58. if (uzlib_get_byte(d) != 0x1f || uzlib_get_byte(d) != 0x8b) return TINF_DATA_ERROR;
  59. /* check method is deflate */
  60. if (uzlib_get_byte(d) != 8) return TINF_DATA_ERROR;
  61. /* get flag byte */
  62. flg = uzlib_get_byte(d);
  63. /* check that reserved bits are zero */
  64. if (flg & 0xe0) return TINF_DATA_ERROR;
  65. /* -- find start of compressed data -- */
  66. /* skip rest of base header of 10 bytes */
  67. tinf_skip_bytes(d, 6);
  68. /* skip extra data if present */
  69. if (flg & FEXTRA)
  70. {
  71. unsigned int xlen = tinf_get_uint16(d);
  72. tinf_skip_bytes(d, xlen);
  73. }
  74. /* skip file name if present */
  75. if (flg & FNAME) { while (uzlib_get_byte(d)); }
  76. /* skip file comment if present */
  77. if (flg & FCOMMENT) { while (uzlib_get_byte(d)); }
  78. /* check header crc if present */
  79. if (flg & FHCRC)
  80. {
  81. /*unsigned int hcrc =*/ tinf_get_uint16(d);
  82. // TODO: Check!
  83. // if (hcrc != (tinf_crc32(src, start - src) & 0x0000ffff))
  84. // return TINF_DATA_ERROR;
  85. }
  86. /* initialize for crc32 checksum */
  87. d->checksum_type = TINF_CHKSUM_CRC;
  88. d->checksum = ~0;
  89. return TINF_OK;
  90. }