tinfzlib.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. * tinfzlib - tiny zlib 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. int uzlib_zlib_parse_header(TINF_DATA *d)
  37. {
  38. unsigned char cmf, flg;
  39. /* -- get header bytes -- */
  40. cmf = uzlib_get_byte(d);
  41. flg = uzlib_get_byte(d);
  42. /* -- check format -- */
  43. /* check checksum */
  44. if ((256*cmf + flg) % 31) return TINF_DATA_ERROR;
  45. /* check method is deflate */
  46. if ((cmf & 0x0f) != 8) return TINF_DATA_ERROR;
  47. /* check window size is valid */
  48. if ((cmf >> 4) > 7) return TINF_DATA_ERROR;
  49. /* check there is no preset dictionary */
  50. if (flg & 0x20) return TINF_DATA_ERROR;
  51. /* initialize for adler32 checksum */
  52. d->checksum_type = TINF_CHKSUM_ADLER;
  53. d->checksum = 1;
  54. return cmf >> 4;
  55. }