DataModule.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /**
  2. * Copyright (c) 2006-2022 LOVE Development Team
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * 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 it
  10. * freely, subject to the following restrictions:
  11. *
  12. * 1. The origin of this software must not be misrepresented; you must not
  13. * claim that you wrote the original software. If you use this software
  14. * in a product, an acknowledgment in the product documentation would be
  15. * appreciated but is not required.
  16. * 2. Altered source versions must be plainly marked as such, and must not be
  17. * misrepresented as being the original software.
  18. * 3. This notice may not be removed or altered from any source distribution.
  19. **/
  20. // LOVE
  21. #include "DataModule.h"
  22. #include "common/b64.h"
  23. #include "common/int.h"
  24. #include "common/StringMap.h"
  25. // STL
  26. #include <cmath>
  27. #include <list>
  28. #include <iostream>
  29. namespace
  30. {
  31. static const char hexchars[] = "0123456789abcdef";
  32. char *bytesToHex(const love::uint8 *src, size_t srclen, size_t &dstlen)
  33. {
  34. dstlen = srclen * 2;
  35. if (dstlen == 0)
  36. return nullptr;
  37. char *dst = nullptr;
  38. try
  39. {
  40. dst = new char[dstlen + 1];
  41. }
  42. catch (std::exception &)
  43. {
  44. throw love::Exception("Out of memory.");
  45. }
  46. for (size_t i = 0; i < srclen; i++)
  47. {
  48. love::uint8 b = src[i];
  49. dst[i * 2 + 0] = hexchars[b >> 4];
  50. dst[i * 2 + 1] = hexchars[b & 0xF];
  51. }
  52. dst[dstlen] = '\0';
  53. return dst;
  54. }
  55. love::uint8 nibble(char c)
  56. {
  57. if (c >= '0' && c <= '9')
  58. return (love::uint8) (c - '0');
  59. if (c >= 'A' && c <= 'F')
  60. return (love::uint8) (c - 'A' + 0x0a);
  61. if (c >= 'a' && c <= 'f')
  62. return (love::uint8) (c - 'a' + 0x0a);
  63. return 0;
  64. }
  65. love::uint8 *hexToBytes(const char *src, size_t srclen, size_t &dstlen)
  66. {
  67. if (srclen >= 2 && src[0] == '0' && (src[1] == 'x' || src[1] == 'X'))
  68. {
  69. src += 2;
  70. srclen -= 2;
  71. }
  72. dstlen = (srclen + 1) / 2;
  73. if (dstlen == 0)
  74. return nullptr;
  75. love::uint8 *dst = nullptr;
  76. try
  77. {
  78. dst = new love::uint8[dstlen];
  79. }
  80. catch (std::exception &)
  81. {
  82. throw love::Exception("Out of memory.");
  83. }
  84. for (size_t i = 0; i < dstlen; i++)
  85. {
  86. dst[i] = nibble(src[i * 2]) << 4;
  87. if (i * 2 + 1 < srclen)
  88. dst[i] |= nibble(src[i * 2 + 1]);
  89. }
  90. return dst;
  91. }
  92. } // anonymous namespace
  93. namespace love
  94. {
  95. namespace data
  96. {
  97. CompressedData *compress(Compressor::Format format, const char *rawbytes, size_t rawsize, int level)
  98. {
  99. Compressor *compressor = Compressor::getCompressor(format);
  100. if (compressor == nullptr)
  101. throw love::Exception("Invalid compression format.");
  102. size_t compressedsize = 0;
  103. char *cbytes = compressor->compress(format, rawbytes, rawsize, level, compressedsize);
  104. CompressedData *data = nullptr;
  105. try
  106. {
  107. data = new CompressedData(format, cbytes, compressedsize, rawsize, true);
  108. }
  109. catch (love::Exception &)
  110. {
  111. delete[] cbytes;
  112. throw;
  113. }
  114. return data;
  115. }
  116. char *decompress(CompressedData *data, size_t &decompressedsize)
  117. {
  118. size_t rawsize = data->getDecompressedSize();
  119. char *rawbytes = decompress(data->getFormat(), (const char *) data->getData(),
  120. data->getSize(), rawsize);
  121. decompressedsize = rawsize;
  122. return rawbytes;
  123. }
  124. char *decompress(Compressor::Format format, const char *cbytes, size_t compressedsize, size_t &rawsize)
  125. {
  126. Compressor *compressor = Compressor::getCompressor(format);
  127. if (compressor == nullptr)
  128. throw love::Exception("Invalid compression format.");
  129. return compressor->decompress(format, cbytes, compressedsize, rawsize);
  130. }
  131. char *encode(EncodeFormat format, const char *src, size_t srclen, size_t &dstlen, size_t linelen)
  132. {
  133. switch (format)
  134. {
  135. case ENCODE_BASE64:
  136. default:
  137. return b64_encode(src, srclen, linelen, dstlen);
  138. case ENCODE_HEX:
  139. return bytesToHex((const uint8 *) src, srclen, dstlen);
  140. }
  141. }
  142. char *decode(EncodeFormat format, const char *src, size_t srclen, size_t &dstlen)
  143. {
  144. switch (format)
  145. {
  146. case ENCODE_BASE64:
  147. default:
  148. return b64_decode(src, srclen, dstlen);
  149. case ENCODE_HEX:
  150. return (char *) hexToBytes(src, srclen, dstlen);
  151. }
  152. }
  153. std::string hash(HashFunction::Function function, Data *input)
  154. {
  155. return hash(function, (const char*) input->getData(), input->getSize());
  156. }
  157. std::string hash(HashFunction::Function function, const char *input, uint64_t size)
  158. {
  159. HashFunction::Value output;
  160. hash(function, input, size, output);
  161. return std::string(output.data, output.size);
  162. }
  163. void hash(HashFunction::Function function, Data *input, HashFunction::Value &output)
  164. {
  165. hash(function, (const char*) input->getData(), input->getSize(), output);
  166. }
  167. void hash(HashFunction::Function function, const char *input, uint64_t size, HashFunction::Value &output)
  168. {
  169. HashFunction *hashfunction = HashFunction::getHashFunction(function);
  170. if (hashfunction == nullptr)
  171. throw love::Exception("Invalid hash function.");
  172. hashfunction->hash(function, input, size, output);
  173. }
  174. DataModule::DataModule()
  175. {
  176. }
  177. DataModule::~DataModule()
  178. {
  179. }
  180. DataView *DataModule::newDataView(Data *data, size_t offset, size_t size)
  181. {
  182. return new DataView(data, offset, size);
  183. }
  184. ByteData *DataModule::newByteData(size_t size)
  185. {
  186. return new ByteData(size);
  187. }
  188. ByteData *DataModule::newByteData(const void *d, size_t size)
  189. {
  190. return new ByteData(d, size);
  191. }
  192. ByteData *DataModule::newByteData(void *d, size_t size, bool own)
  193. {
  194. return new ByteData(d, size, own);
  195. }
  196. static StringMap<EncodeFormat, ENCODE_MAX_ENUM>::Entry encoderEntries[] =
  197. {
  198. { "base64", ENCODE_BASE64 },
  199. { "hex", ENCODE_HEX },
  200. };
  201. static StringMap<EncodeFormat, ENCODE_MAX_ENUM> encoders(encoderEntries, sizeof(encoderEntries));
  202. static StringMap<ContainerType, CONTAINER_MAX_ENUM>::Entry containerEntries[] =
  203. {
  204. { "data", CONTAINER_DATA },
  205. { "string", CONTAINER_STRING },
  206. };
  207. static StringMap<ContainerType, CONTAINER_MAX_ENUM> containers(containerEntries, sizeof(containerEntries));
  208. bool getConstant(const char *in, EncodeFormat &out)
  209. {
  210. return encoders.find(in, out);
  211. }
  212. bool getConstant(EncodeFormat in, const char *&out)
  213. {
  214. return encoders.find(in, out);
  215. }
  216. std::vector<std::string> getConstants(EncodeFormat)
  217. {
  218. return encoders.getNames();
  219. }
  220. bool getConstant(const char *in, ContainerType &out)
  221. {
  222. return containers.find(in, out);
  223. }
  224. bool getConstant(ContainerType in, const char *&out)
  225. {
  226. return containers.find(in, out);
  227. }
  228. std::vector<std::string> getConstants(ContainerType)
  229. {
  230. return containers.getNames();
  231. }
  232. } // data
  233. } // love