zlib_helper.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * Copyright (c) 2012 Andrey Kemka
  3. *
  4. * This software is provided 'as-is', without any express or
  5. * implied warranty. In no event will the authors be held
  6. * liable for any damages arising from the use of this software.
  7. *
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute
  10. * it freely, subject to the following restrictions:
  11. *
  12. * 1. The origin of this software must not be misrepresented;
  13. * you must not claim that you wrote the original software.
  14. * If you use this software in a product, an acknowledgment
  15. * in the product documentation would be appreciated but
  16. * is not required.
  17. *
  18. * 2. Altered source versions must be plainly marked as such,
  19. * and must not be misrepresented as being the original software.
  20. *
  21. * 3. This notice may not be removed or altered from any
  22. * source distribution.
  23. */
  24. #include <stdio.h>
  25. #include <math.h>
  26. #include <zlib.h>
  27. #ifndef __CEGCC__
  28. #include <memory.h>
  29. #endif
  30. typedef struct
  31. {
  32. void *Memory;
  33. unsigned int Size;
  34. unsigned int Position;
  35. } zglTMemory, *zglPMemory;
  36. void zlib_Init( z_stream *strm )
  37. {
  38. memset( strm, 0, sizeof( z_stream ) );
  39. inflateInit_( strm, ZLIB_VERSION, sizeof( z_stream ) );
  40. }
  41. void zlib_Free( z_stream *strm )
  42. {
  43. inflateEnd( strm );
  44. }
  45. #ifdef __CEGCC__
  46. unsigned long udivmodsi4( unsigned long num, unsigned long den, int modwanted )
  47. {
  48. unsigned long bit = 1;
  49. unsigned long res = 0;
  50. while ( den < num && bit && !( den & ( 1L << 31 ) ) )
  51. {
  52. den <<= 1;
  53. bit <<= 1;
  54. }
  55. while ( bit )
  56. {
  57. if (num >= den)
  58. {
  59. num -= den;
  60. res |= bit;
  61. }
  62. bit >>=1;
  63. den >>=1;
  64. }
  65. if ( modwanted ) return num;
  66. return res;
  67. }
  68. long __umodsi3 ( long a, long b )
  69. {
  70. return udivmodsi4( a, b, 1 );
  71. }
  72. #endif
  73. int png_DecodeIDAT( zglTMemory *pngMem, z_stream *pngZStream, unsigned int *pngIDATEnd, void *Buffer, int Bytes )
  74. {
  75. unsigned char *b;
  76. char *IDATHeader;
  77. int result = -1;
  78. pngZStream->next_out = Buffer;
  79. pngZStream->avail_out = Bytes;
  80. while ( pngZStream->avail_out > 0 )
  81. {
  82. if ( ( pngMem->Position == *pngIDATEnd ) && ( pngZStream->avail_out > 0 ) && ( pngZStream->avail_in == 0 ) )
  83. {
  84. pngMem->Position += 4;
  85. b = (unsigned char*)( (unsigned char*)pngMem->Memory + pngMem->Position );
  86. *pngIDATEnd = b[ 3 ] + ( b[ 2 ] << 8 ) + ( b[ 1 ] << 16 ) + ( b[ 0 ] << 24 );
  87. pngMem->Position += 4;
  88. IDATHeader = (char*)( (char*)pngMem->Memory + pngMem->Position );
  89. if ( ( IDATHeader[ 0 ] != 'I' ) && ( IDATHeader[ 1 ] != 'D' ) && ( IDATHeader[ 2 ] != 'A' ) && ( IDATHeader[ 3 ] != 'T' ) )
  90. return -1;
  91. pngMem->Position += 4;
  92. *pngIDATEnd += pngMem->Position;
  93. }
  94. if ( pngZStream->avail_in == 0 )
  95. {
  96. if ( pngMem->Size - pngMem->Position > 0 )
  97. {
  98. if ( pngMem->Position + 65535 > *pngIDATEnd )
  99. {
  100. if ( pngMem->Position + ( *pngIDATEnd - pngMem->Position ) > pngMem->Size )
  101. pngZStream->avail_in = pngMem->Size - pngMem->Position;
  102. else
  103. pngZStream->avail_in = *pngIDATEnd - pngMem->Position;
  104. }
  105. else
  106. {
  107. if ( pngMem->Position + 65535 > pngMem->Size )
  108. pngZStream->avail_in = pngMem->Size - pngMem->Position;
  109. else
  110. pngZStream->avail_in = 65535;
  111. }
  112. pngMem->Position += pngZStream->avail_in;
  113. }
  114. else
  115. pngZStream->avail_in = 0;
  116. if ( pngZStream->avail_in == 0 )
  117. return Bytes - pngZStream->avail_out;
  118. pngZStream->next_in = (void*)( (char*)pngMem->Memory + pngMem->Position - pngZStream->avail_in );
  119. }
  120. result = inflate( pngZStream, 0 );
  121. if ( result < 0 )
  122. return -1;
  123. else
  124. result = pngZStream->avail_in;
  125. }
  126. return result;
  127. }