Stream.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /*
  2. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  3. Permission is hereby granted, free of charge, to any person
  4. obtaining a copy of this software and associated documentation
  5. files (the "Software"), to deal in the Software without
  6. restriction, including without limitation the rights to use,
  7. copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the
  9. Software is furnished to do so, subject to the following
  10. conditions:
  11. The above copyright notice and this permission notice shall be
  12. included in all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  15. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  17. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  18. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. OTHER DEALINGS IN THE SOFTWARE.
  21. */
  22. #include "Stream.h"
  23. #include "Types.h"
  24. #include "zlib.h"
  25. #include "MathUtils.h"
  26. namespace Crown
  27. {
  28. bool Stream::ZipTo(Stream* stream, size_t size, size_t& zippedSize)
  29. {
  30. const size_t CHUNK_SIZE = 16384;
  31. int ret, flush;
  32. unsigned have;
  33. z_stream strm;
  34. unsigned char in[CHUNK_SIZE];
  35. unsigned char out[CHUNK_SIZE];
  36. strm.zalloc = Z_NULL;
  37. strm.zfree = Z_NULL;
  38. strm.opaque = Z_NULL;
  39. ret = deflateInit(&strm, 6);
  40. if (ret != Z_OK)
  41. return false;
  42. size_t bytes_read = 0;
  43. do
  44. {
  45. size_t this_step_bytes = math::min(CHUNK_SIZE, size - bytes_read);
  46. ReadDataBlock(in, this_step_bytes);
  47. strm.avail_in = this_step_bytes;
  48. strm.next_in = in;
  49. flush = (size - bytes_read) <= CHUNK_SIZE ? Z_FINISH : Z_NO_FLUSH;
  50. do
  51. {
  52. strm.avail_out = CHUNK_SIZE;
  53. strm.next_out = out;
  54. ret = deflate(&strm, flush); /* no bad return value */
  55. assert(ret != Z_STREAM_ERROR); /* state not clobbered */
  56. have = CHUNK_SIZE - strm.avail_out;
  57. if (have > 0)
  58. stream->WriteDataBlock(out, have);
  59. } while (strm.avail_out == 0);
  60. assert(strm.avail_in == 0); /* all input will be used */
  61. bytes_read += this_step_bytes;
  62. /* done when last data in file processed */
  63. } while (flush != Z_FINISH);
  64. assert(ret == Z_STREAM_END); /* stream will be complete */
  65. /* clean up and return */
  66. (void)deflateEnd(&strm);
  67. zippedSize = strm.total_out;
  68. return true;
  69. }
  70. bool Stream::UnzipTo(Stream* stream, size_t& /*unzippedSize*/)
  71. {
  72. const size_t CHUNK_SIZE = 16384;
  73. int ret;
  74. unsigned have;
  75. z_stream strm;
  76. unsigned char in[CHUNK_SIZE];
  77. unsigned char out[CHUNK_SIZE];
  78. /* allocate inflate state */
  79. strm.zalloc = Z_NULL;
  80. strm.zfree = Z_NULL;
  81. strm.opaque = Z_NULL;
  82. strm.avail_in = 0;
  83. strm.next_in = Z_NULL;
  84. ret = inflateInit(&strm);
  85. if (ret != Z_OK)
  86. return false;
  87. size_t size = GetSize();
  88. size_t bytes_read = 0;
  89. /* decompress until deflate stream ends or end of file */
  90. do
  91. {
  92. size_t this_step_bytes = math::min(CHUNK_SIZE, size - bytes_read);
  93. ReadDataBlock(in, this_step_bytes);
  94. strm.avail_in = this_step_bytes;
  95. strm.next_in = in;
  96. if (strm.avail_in == 0)
  97. break;
  98. /* run inflate() on input until output buffer not full */
  99. do {
  100. strm.avail_out = CHUNK_SIZE;
  101. strm.next_out = out;
  102. ret = inflate(&strm, Z_NO_FLUSH);
  103. assert(ret != Z_STREAM_ERROR); /* state not clobbered */
  104. switch (ret) {
  105. case Z_NEED_DICT:
  106. ret = Z_DATA_ERROR; /* and fall through */
  107. case Z_DATA_ERROR:
  108. case Z_MEM_ERROR:
  109. (void)inflateEnd(&strm);
  110. return false;
  111. }
  112. have = CHUNK_SIZE - strm.avail_out;
  113. if (have > 0)
  114. stream->WriteDataBlock(out, have);
  115. } while (strm.avail_out == 0);
  116. bytes_read += this_step_bytes;
  117. /* done when inflate() says it's done */
  118. } while (ret != Z_STREAM_END);
  119. /* clean up and return */
  120. (void)inflateEnd(&strm);
  121. return ret == Z_STREAM_END;
  122. }
  123. BinaryReader::BinaryReader(Stream* s)
  124. {
  125. //BinaryReader takes the ownership of the stream.
  126. mStream = s;
  127. }
  128. BinaryReader::~BinaryReader()
  129. {
  130. }
  131. uchar BinaryReader::ReadByte()
  132. {
  133. uchar buffer;
  134. mStream->ReadDataBlock(&buffer, sizeof(uchar));
  135. return buffer;
  136. }
  137. short BinaryReader::ReadInt16()
  138. {
  139. short buffer;
  140. mStream->ReadDataBlock(&buffer, sizeof(short));
  141. return buffer;
  142. }
  143. ushort BinaryReader::ReadUint16()
  144. {
  145. ushort buffer;
  146. mStream->ReadDataBlock(&buffer, sizeof(ushort));
  147. return buffer;
  148. }
  149. int BinaryReader::ReadInt32()
  150. {
  151. int buffer;
  152. mStream->ReadDataBlock(&buffer, sizeof(int));
  153. return buffer;
  154. }
  155. uint BinaryReader::ReadUint32()
  156. {
  157. uint buffer;
  158. mStream->ReadDataBlock(&buffer, sizeof(uint));
  159. return buffer;
  160. }
  161. long long BinaryReader::ReadInt64()
  162. {
  163. long long buffer;
  164. mStream->ReadDataBlock(&buffer, sizeof(long long));
  165. return buffer;
  166. }
  167. double BinaryReader::ReadDouble()
  168. {
  169. double buffer;
  170. mStream->ReadDataBlock(&buffer, sizeof(double));
  171. return buffer;
  172. }
  173. float BinaryReader::ReadFloat()
  174. {
  175. float buffer;
  176. mStream->ReadDataBlock(&buffer, sizeof(float));
  177. return buffer;
  178. }
  179. BinaryWriter::BinaryWriter(Stream* s)
  180. {
  181. //BinaryWriter takes the ownership of the stream.
  182. mStream = s;
  183. }
  184. BinaryWriter::~BinaryWriter()
  185. {
  186. }
  187. void BinaryWriter::WriteByte(uchar buffer)
  188. {
  189. mStream->WriteDataBlock(&buffer, sizeof(uchar));
  190. }
  191. void BinaryWriter::WriteInt16(short buffer)
  192. {
  193. mStream->WriteDataBlock(&buffer, sizeof(short));
  194. }
  195. void BinaryWriter::WriteUint16(ushort buffer)
  196. {
  197. mStream->WriteDataBlock(&buffer, sizeof(ushort));
  198. }
  199. void BinaryWriter::WriteInt32(int buffer)
  200. {
  201. mStream->WriteDataBlock(&buffer, sizeof(int));
  202. }
  203. void BinaryWriter::WriteUint32(uint buffer)
  204. {
  205. mStream->WriteDataBlock(&buffer, sizeof(uint));
  206. }
  207. void BinaryWriter::WriteInt64(long long buffer)
  208. {
  209. mStream->WriteDataBlock(&buffer, sizeof(long long));
  210. }
  211. void BinaryWriter::WriteDouble(double buffer)
  212. {
  213. mStream->WriteDataBlock(&buffer, sizeof(double));
  214. }
  215. void BinaryWriter::WriteFloat(float buffer)
  216. {
  217. mStream->WriteDataBlock(&buffer, sizeof(float));
  218. }
  219. void BinaryWriter::InsertByte(uchar val, size_t offset)
  220. {
  221. size_t tmpSize = mStream->GetSize() - offset;
  222. uchar* tmp = new uchar[tmpSize];
  223. mStream->Seek(offset, SM_SeekFromBegin);
  224. mStream->ReadDataBlock(tmp, tmpSize);
  225. mStream->Seek(offset, SM_SeekFromBegin);
  226. mStream->WriteByte(val);
  227. mStream->WriteDataBlock(tmp, tmpSize);
  228. delete[] tmp;
  229. }
  230. TextReader::TextReader(Stream* s)
  231. {
  232. mStream = s;
  233. }
  234. TextReader::~TextReader()
  235. {
  236. }
  237. char TextReader::ReadChar()
  238. {
  239. return mStream->ReadByte();
  240. }
  241. char* TextReader::ReadString(char* string, int count)
  242. {
  243. char currentChar;
  244. int i = 0;
  245. while(!mStream->EndOfStream() && i < count - 1)
  246. {
  247. currentChar = mStream->ReadByte();
  248. string[i] = currentChar;
  249. i++;
  250. if (currentChar == '\n')
  251. {
  252. break;
  253. }
  254. }
  255. if (i == 0)
  256. {
  257. return NULL;
  258. }
  259. string[i] = '\0';
  260. return string;
  261. }
  262. TextWriter::TextWriter(Stream* s)
  263. {
  264. mStream = s;
  265. }
  266. TextWriter::~TextWriter()
  267. {
  268. }
  269. void TextWriter::WriteChar(char c)
  270. {
  271. mStream->WriteByte(c);
  272. }
  273. void TextWriter::WriteString(const char* string)
  274. {
  275. size_t count = 0;
  276. while(string[count] != '\0')
  277. {
  278. mStream->WriteByte(string[count]);
  279. count++;
  280. }
  281. }
  282. } // namespace Crown