wrap_DataModule.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. /**
  2. * Copyright (c) 2006-2018 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 "wrap_DataModule.h"
  22. #include "wrap_Data.h"
  23. #include "wrap_ByteData.h"
  24. #include "wrap_DataView.h"
  25. #include "wrap_CompressedData.h"
  26. #include "DataModule.h"
  27. #include "common/b64.h"
  28. // Lua 5.3
  29. #include "libraries/lua53/lstrlib.h"
  30. // C
  31. #include <cmath>
  32. #include <iostream>
  33. #include <algorithm>
  34. #include <limits>
  35. namespace love
  36. {
  37. namespace data
  38. {
  39. ContainerType luax_checkcontainertype(lua_State *L, int idx)
  40. {
  41. const char *str = luaL_checkstring(L, idx);
  42. ContainerType ctype = CONTAINER_STRING;
  43. if (!getConstant(str, ctype))
  44. luax_enumerror(L, "container type", getConstants(ctype), str);
  45. return ctype;
  46. }
  47. int w_newDataView(lua_State *L)
  48. {
  49. Data *data = luax_checkdata(L, 1);
  50. lua_Integer offset = luaL_checkinteger(L, 2);
  51. lua_Integer size = luaL_checkinteger(L, 3);
  52. if (offset < 0 || size < 0)
  53. return luaL_error(L, "DataView offset and size must not be negative.");
  54. DataView *d = DataModule::instance.newDataView(data, (size_t) offset, (size_t) size);
  55. luax_pushtype(L, d);
  56. d->release();
  57. return 1;
  58. }
  59. int w_newByteData(lua_State *L)
  60. {
  61. ByteData *d = nullptr;
  62. if (luax_istype(L, 1, Data::type))
  63. {
  64. Data *data = luax_checkdata(L, 1);
  65. if (data->getSize() > std::numeric_limits<lua_Integer>::max())
  66. return luaL_error(L, "Data's size is too large!");
  67. lua_Integer offset = luaL_optinteger(L, 2, 0);
  68. if (offset < 0)
  69. return luaL_error(L, "Offset argument must not be negative.");
  70. lua_Integer size = luaL_optinteger(L, 3, data->getSize() - offset);
  71. if (size <= 0)
  72. return luaL_error(L, "Size argument must be greater than zero.");
  73. else if ((size_t)(offset + size) > data->getSize())
  74. return luaL_error(L, "Offset and size arguments must fit within the given Data's size.");
  75. const char *bytes = (const char *) data->getData() + offset;
  76. luax_catchexcept(L, [&]() { d = DataModule::instance.newByteData(bytes, (size_t) size); });
  77. }
  78. else if (lua_type(L, 1) == LUA_TSTRING)
  79. {
  80. size_t size = 0;
  81. const char *data = luaL_checklstring(L, 1, &size);
  82. luax_catchexcept(L, [&]() { d = DataModule::instance.newByteData(data, size); });
  83. }
  84. else
  85. {
  86. lua_Integer size = luaL_checkinteger(L, 1);
  87. if (size <= 0)
  88. return luaL_error(L, "Data size must be a positive number.");
  89. luax_catchexcept(L, [&]() { d = DataModule::instance.newByteData((size_t) size); });
  90. }
  91. luax_pushtype(L, d);
  92. d->release();
  93. return 1;
  94. }
  95. int w_compress(lua_State *L)
  96. {
  97. ContainerType ctype = luax_checkcontainertype(L, 1);
  98. const char *fstr = luaL_checkstring(L, 2);
  99. Compressor::Format format = Compressor::FORMAT_LZ4;
  100. if (!Compressor::getConstant(fstr, format))
  101. return luax_enumerror(L, "compressed data format", Compressor::getConstants(format), fstr);
  102. int level = (int) luaL_optinteger(L, 4, -1);
  103. size_t rawsize = 0;
  104. const char *rawbytes = nullptr;
  105. if (lua_isstring(L, 3))
  106. rawbytes = luaL_checklstring(L, 3, &rawsize);
  107. else
  108. {
  109. Data *rawdata = luax_checktype<Data>(L, 3);
  110. rawsize = rawdata->getSize();
  111. rawbytes = (const char *) rawdata->getData();
  112. }
  113. CompressedData *cdata = nullptr;
  114. luax_catchexcept(L, [&](){ cdata = compress(format, rawbytes, rawsize, level); });
  115. if (ctype == CONTAINER_DATA)
  116. luax_pushtype(L, cdata);
  117. else
  118. lua_pushlstring(L, (const char *) cdata->getData(), cdata->getSize());
  119. cdata->release();
  120. return 1;
  121. }
  122. int w_decompress(lua_State *L)
  123. {
  124. ContainerType ctype = luax_checkcontainertype(L, 1);
  125. char *rawbytes = nullptr;
  126. size_t rawsize = 0;
  127. if (luax_istype(L, 2, CompressedData::type))
  128. {
  129. CompressedData *data = luax_checkcompresseddata(L, 2);
  130. rawsize = data->getDecompressedSize();
  131. luax_catchexcept(L, [&](){ rawbytes = decompress(data, rawsize); });
  132. }
  133. else
  134. {
  135. Compressor::Format format = Compressor::FORMAT_LZ4;
  136. const char *fstr = luaL_checkstring(L, 2);
  137. if (!Compressor::getConstant(fstr, format))
  138. return luax_enumerror(L, "compressed data format", Compressor::getConstants(format), fstr);
  139. size_t compressedsize = 0;
  140. const char *cbytes = nullptr;
  141. if (luax_istype(L, 3, Data::type))
  142. {
  143. Data *data = luax_checktype<Data>(L, 3);
  144. cbytes = (const char *) data->getData();
  145. compressedsize = data->getSize();
  146. }
  147. else
  148. cbytes = luaL_checklstring(L, 3, &compressedsize);
  149. luax_catchexcept(L, [&](){ rawbytes = decompress(format, cbytes, compressedsize, rawsize); });
  150. }
  151. if (ctype == CONTAINER_DATA)
  152. {
  153. ByteData *data = nullptr;
  154. luax_catchexcept(L, [&]() { data = DataModule::instance.newByteData(rawbytes, rawsize, true); });
  155. luax_pushtype(L, Data::type, data);
  156. data->release();
  157. }
  158. else
  159. {
  160. lua_pushlstring(L, rawbytes, rawsize);
  161. delete[] rawbytes;
  162. }
  163. return 1;
  164. }
  165. int w_encode(lua_State *L)
  166. {
  167. ContainerType ctype = luax_checkcontainertype(L, 1);
  168. const char *formatstr = luaL_checkstring(L, 2);
  169. EncodeFormat format;
  170. if (!getConstant(formatstr, format))
  171. return luax_enumerror(L, "encode format", getConstants(format), formatstr);
  172. size_t srclen = 0;
  173. const char *src = nullptr;
  174. if (luax_istype(L, 3, Data::type))
  175. {
  176. Data *data = luax_totype<Data>(L, 3);
  177. src = (const char *) data->getData();
  178. srclen = data->getSize();
  179. }
  180. else
  181. src = luaL_checklstring(L, 3, &srclen);
  182. size_t linelen = (size_t) luaL_optinteger(L, 4, 0);
  183. size_t dstlen = 0;
  184. char *dst = nullptr;
  185. luax_catchexcept(L, [&](){ dst = encode(format, src, srclen, dstlen, linelen); });
  186. if (ctype == CONTAINER_DATA)
  187. {
  188. ByteData *data = nullptr;
  189. if (dst != nullptr)
  190. luax_catchexcept(L, [&]() { data = DataModule::instance.newByteData(dst, dstlen, true); });
  191. else
  192. luax_catchexcept(L, [&]() { data = DataModule::instance.newByteData(0); });
  193. luax_pushtype(L, Data::type, data);
  194. data->release();
  195. }
  196. else
  197. {
  198. if (dst != nullptr)
  199. lua_pushlstring(L, dst, dstlen);
  200. else
  201. lua_pushstring(L, "");
  202. delete[] dst;
  203. }
  204. return 1;
  205. }
  206. int w_decode(lua_State *L)
  207. {
  208. ContainerType ctype = luax_checkcontainertype(L, 1);
  209. const char *formatstr = luaL_checkstring(L, 2);
  210. EncodeFormat format;
  211. if (!getConstant(formatstr, format))
  212. return luax_enumerror(L, "decode format", getConstants(format), formatstr);
  213. size_t srclen = 0;
  214. const char *src = nullptr;
  215. if (luax_istype(L, 3, Data::type))
  216. {
  217. Data *data = luax_totype<Data>(L, 3);
  218. src = (const char *) data->getData();
  219. srclen = data->getSize();
  220. }
  221. else
  222. src = luaL_checklstring(L, 3, &srclen);
  223. size_t dstlen = 0;
  224. char *dst = nullptr;
  225. luax_catchexcept(L, [&](){ dst = decode(format, src, srclen, dstlen); });
  226. if (ctype == CONTAINER_DATA)
  227. {
  228. ByteData *data = nullptr;
  229. if (dst != nullptr)
  230. luax_catchexcept(L, [&]() { data = DataModule::instance.newByteData(dst, dstlen, true); });
  231. else
  232. luax_catchexcept(L, [&]() { data = DataModule::instance.newByteData(0); });
  233. luax_pushtype(L, Data::type, data);
  234. data->release();
  235. }
  236. else
  237. {
  238. if (dst != nullptr)
  239. lua_pushlstring(L, dst, dstlen);
  240. else
  241. lua_pushstring(L, "");
  242. delete[] dst;
  243. }
  244. return 1;
  245. }
  246. int w_hash(lua_State *L)
  247. {
  248. const char *fstr = luaL_checkstring(L, 1);
  249. HashFunction::Function function;
  250. if (!HashFunction::getConstant(fstr, function))
  251. return luax_enumerror(L, "hash function", HashFunction::getConstants(function), fstr);
  252. HashFunction::Value hashvalue;
  253. if (lua_isstring(L, 2))
  254. {
  255. size_t rawsize = 0;
  256. const char *rawbytes = luaL_checklstring(L, 2, &rawsize);
  257. luax_catchexcept(L, [&](){ love::data::hash(function, rawbytes, rawsize, hashvalue); });
  258. }
  259. else
  260. {
  261. Data *rawdata = luax_checktype<Data>(L, 2);
  262. luax_catchexcept(L, [&](){ love::data::hash(function, rawdata, hashvalue); });
  263. }
  264. lua_pushlstring(L, hashvalue.data, hashvalue.size);
  265. return 1;
  266. }
  267. int w_pack(lua_State *L)
  268. {
  269. ContainerType ctype = luax_checkcontainertype(L, 1);
  270. const char *fmt = luaL_checkstring(L, 2);
  271. luaL_Buffer_53 b;
  272. lua53_str_pack(L, fmt, 3, &b);
  273. if (ctype == CONTAINER_DATA)
  274. {
  275. Data *d = nullptr;
  276. luax_catchexcept(L, [&]() { d = DataModule::instance.newByteData(b.nelems); });
  277. memcpy(d->getData(), b.ptr, d->getSize());
  278. lua53_cleanupbuffer(&b);
  279. luax_pushtype(L, Data::type, d);
  280. d->release();
  281. }
  282. else
  283. lua53_pushresult(&b);
  284. return 1;
  285. }
  286. int w_unpack(lua_State *L)
  287. {
  288. const char *fmt = luaL_checkstring(L, 1);
  289. const char *data = nullptr;
  290. size_t datasize = 0;
  291. if (luax_istype(L, 2, Data::type))
  292. {
  293. Data *d = luax_checkdata(L, 2);
  294. data = (const char *) d->getData();
  295. datasize = d->getSize();
  296. }
  297. else
  298. data = luaL_checklstring(L, 2, &datasize);
  299. return lua53_str_unpack(L, fmt, data, datasize, 2, 3);
  300. }
  301. // List of functions to wrap.
  302. static const luaL_Reg functions[] =
  303. {
  304. { "newDataView", w_newDataView },
  305. { "newByteData", w_newByteData },
  306. { "compress", w_compress },
  307. { "decompress", w_decompress },
  308. { "encode", w_encode },
  309. { "decode", w_decode },
  310. { "hash", w_hash },
  311. { "pack", w_pack },
  312. { "unpack", w_unpack },
  313. { "getPackedSize", lua53_str_packsize },
  314. { 0, 0 }
  315. };
  316. static const lua_CFunction types[] =
  317. {
  318. luaopen_data,
  319. luaopen_bytedata,
  320. luaopen_dataview,
  321. luaopen_compresseddata,
  322. nullptr
  323. };
  324. extern "C" int luaopen_love_data(lua_State *L)
  325. {
  326. DataModule::instance.retain();
  327. WrappedModule w;
  328. w.module = &DataModule::instance;
  329. w.name = "data";
  330. w.type = &Module::type;
  331. w.functions = functions;
  332. w.types = types;
  333. int n = luax_register_module(L, w);
  334. return n;
  335. }
  336. } // data
  337. } // love