sqstdblob.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. /* see copyright notice in squirrel.h */
  2. #include <new>
  3. #include <squirrel.h>
  4. #include <sqstdio.h>
  5. #include <string.h>
  6. #include <sqstdblob.h>
  7. #include "sqstdstream.h"
  8. #include "sqstdblobimpl.h"
  9. #define SQSTD_BLOB_TYPE_TAG (SQSTD_STREAM_TYPE_TAG | 0x00000002)
  10. //Blob
  11. SQUserPointer SQBlob::SQBlob_TAG = (SQUserPointer)SQSTD_BLOB_TYPE_TAG;
  12. SQBlob::SQBlob(SQInteger size, SQInteger allocated) {
  13. _size = size;
  14. _allocated = allocated > size ? allocated : size;
  15. _buf = (unsigned char *)sq_malloc(_allocated);
  16. memset(_buf, 0, _allocated);
  17. _ptr = 0;
  18. _owns = true;
  19. }
  20. SQBlob::~SQBlob() {
  21. if(_buf) sq_free(_buf, _allocated);
  22. }
  23. SQInteger SQBlob::Write(const void *buffer, SQInteger size) {
  24. if(!CanAdvance(size)) {
  25. GrowBufOf(_ptr + size - _size);
  26. }
  27. memcpy(&_buf[_ptr], buffer, size);
  28. _ptr += size;
  29. return size;
  30. }
  31. SQInteger SQBlob::WriteZstr(const char *zStr) {
  32. SQInteger size = strlen(zStr);
  33. return Write(zStr, size);
  34. }
  35. SQInteger SQBlob::WriteChar(const char c) {
  36. return Write(&c, 1);
  37. }
  38. SQInteger SQBlob::Read(void *buffer,SQInteger size) {
  39. SQInteger n = size;
  40. if(!CanAdvance(size)) {
  41. if((_size - _ptr) > 0)
  42. n = _size - _ptr;
  43. else return 0;
  44. }
  45. memcpy(buffer, &_buf[_ptr], n);
  46. _ptr += n;
  47. return n;
  48. }
  49. bool SQBlob::Resize(SQInteger n) {
  50. if(!_owns) return false;
  51. if(n != _allocated) {
  52. unsigned char *newbuf = (unsigned char *)sq_malloc(n);
  53. memset(newbuf,0,n);
  54. if(_size > n)
  55. memcpy(newbuf,_buf,n);
  56. else
  57. memcpy(newbuf,_buf,_size);
  58. sq_free(_buf,_allocated);
  59. _buf=newbuf;
  60. _allocated = n;
  61. if(_size > _allocated)
  62. _size = _allocated;
  63. if(_ptr > _size)
  64. _ptr = _size;
  65. }
  66. return true;
  67. }
  68. bool SQBlob::GrowBufOf(SQInteger n)
  69. {
  70. bool ret = true;
  71. if(_size + n > _allocated) {
  72. if(_size + n > _size * 2)
  73. ret = Resize(_size + n);
  74. else
  75. ret = Resize(_size * 2);
  76. }
  77. _size = _size + n;
  78. return ret;
  79. }
  80. SQInteger SQBlob::Seek(SQInteger offset, SQInteger origin) {
  81. switch(origin) {
  82. case SQ_SEEK_SET:
  83. if(offset > _size || offset < 0) return -1;
  84. _ptr = offset;
  85. break;
  86. case SQ_SEEK_CUR:
  87. if(_ptr + offset > _size || _ptr + offset < 0) return -1;
  88. _ptr += offset;
  89. break;
  90. case SQ_SEEK_END:
  91. if(_size + offset > _size || _size + offset < 0) return -1;
  92. _ptr = _size + offset;
  93. break;
  94. default: return -1;
  95. }
  96. return 0;
  97. }
  98. bool SQBlob::SetLen(SQInteger len){
  99. if(len <= _allocated || Resize(len)){
  100. _size = len;
  101. if(_ptr > _size)
  102. _ptr = _size;
  103. return true;
  104. }
  105. return false;
  106. }
  107. #define SETUP_BLOB(v) \
  108. SQBlob *self = NULL; \
  109. { if(SQ_FAILED(sq_getinstanceup(v,1,(SQUserPointer*)&self,(SQUserPointer)SQSTD_BLOB_TYPE_TAG))) \
  110. return sq_throwerror(v,_SC("invalid type tag")); } \
  111. if(!self || !self->IsValid()) \
  112. return sq_throwerror(v,_SC("the blob is invalid"));
  113. static SQInteger _blob_resize(HSQUIRRELVM v)
  114. {
  115. SETUP_BLOB(v);
  116. SQInteger size;
  117. sq_getinteger(v,2,&size);
  118. if(!self->Resize(size))
  119. return sq_throwerror(v,_SC("resize failed"));
  120. return 0;
  121. }
  122. static SQInteger _blob_reserve(HSQUIRRELVM v)
  123. {
  124. SETUP_BLOB(v);
  125. SQInteger size;
  126. sq_getinteger(v,2,&size);
  127. if(!self->GrowBufOf(size))
  128. return sq_throwerror(v,_SC("reserve failed"));
  129. return 0;
  130. }
  131. static void __swap_dword(unsigned int *n)
  132. {
  133. *n=(unsigned int)(((*n&0xFF000000)>>24) |
  134. ((*n&0x00FF0000)>>8) |
  135. ((*n&0x0000FF00)<<8) |
  136. ((*n&0x000000FF)<<24));
  137. }
  138. static void __swap_word(unsigned short *n)
  139. {
  140. *n=(unsigned short)((*n>>8)&0x00FF)| ((*n<<8)&0xFF00);
  141. }
  142. static SQInteger _blob_swap4(HSQUIRRELVM v)
  143. {
  144. SETUP_BLOB(v);
  145. SQInteger num=(self->Len()-(self->Len()%4))>>2;
  146. unsigned int *t=(unsigned int *)self->GetBuf();
  147. for(SQInteger i = 0; i < num; i++) {
  148. __swap_dword(&t[i]);
  149. }
  150. return 0;
  151. }
  152. static SQInteger _blob_swap2(HSQUIRRELVM v)
  153. {
  154. SETUP_BLOB(v);
  155. SQInteger num=(self->Len()-(self->Len()%2))>>1;
  156. unsigned short *t = (unsigned short *)self->GetBuf();
  157. for(SQInteger i = 0; i < num; i++) {
  158. __swap_word(&t[i]);
  159. }
  160. return 0;
  161. }
  162. static SQInteger _blob_memset(HSQUIRRELVM v)
  163. {
  164. SQ_FUNC_VARS_NO_TOP(v);
  165. SETUP_BLOB(v);
  166. SQ_GET_INTEGER(v, 2, idx);
  167. SQ_GET_INTEGER(v, 3, val);
  168. SQ_GET_INTEGER(v, 4, size);
  169. if(idx < 0 || idx >= self->Len())
  170. return sq_throwerror(v,_SC("index out of range"));
  171. if(idx+size < 0 || idx+size >= self->Len())
  172. return sq_throwerror(v,_SC("index+size out of range"));
  173. memset(((unsigned char*)self->GetBuf())+idx, val, size);
  174. return 0;
  175. }
  176. static SQInteger _blob__set(HSQUIRRELVM v)
  177. {
  178. SETUP_BLOB(v);
  179. SQInteger idx,val;
  180. sq_getinteger(v,2,&idx);
  181. sq_getinteger(v,3,&val);
  182. if(idx < 0 || idx >= self->Len())
  183. return sq_throwerror(v,_SC("index out of range"));
  184. ((unsigned char *)self->GetBuf())[idx] = (unsigned char) val;
  185. sq_push(v,3);
  186. return 1;
  187. }
  188. static SQInteger _blob__get(HSQUIRRELVM v)
  189. {
  190. SETUP_BLOB(v);
  191. SQInteger idx;
  192. sq_getinteger(v,2,&idx);
  193. if(idx < 0 || idx >= self->Len())
  194. return sq_throwerror(v,_SC("index out of range"));
  195. sq_pushinteger(v,((unsigned char *)self->GetBuf())[idx]);
  196. return 1;
  197. }
  198. static SQInteger _blob__nexti(HSQUIRRELVM v)
  199. {
  200. SETUP_BLOB(v);
  201. if(sq_gettype(v,2) == OT_NULL) {
  202. sq_pushinteger(v, 0);
  203. return 1;
  204. }
  205. SQInteger idx;
  206. if(SQ_SUCCEEDED(sq_getinteger(v, 2, &idx))) {
  207. if(idx+1 < self->Len()) {
  208. sq_pushinteger(v, idx+1);
  209. return 1;
  210. }
  211. sq_pushnull(v);
  212. return 1;
  213. }
  214. return sq_throwerror(v,_SC("internal error (_nexti) wrong argument type"));
  215. }
  216. static SQInteger _blob__typeof(HSQUIRRELVM v)
  217. {
  218. sq_pushstring(v,_SC("blob"),-1);
  219. return 1;
  220. }
  221. static SQInteger _blob_releasehook(SQUserPointer p, SQInteger size, HSQUIRRELVM v)
  222. {
  223. SQBlob *self = (SQBlob*)p;
  224. self->~SQBlob();
  225. sq_free(self,sizeof(SQBlob));
  226. return 1;
  227. }
  228. static SQInteger _blob_constructor(HSQUIRRELVM v)
  229. {
  230. SQInteger nparam = sq_gettop(v);
  231. SQInteger size = 0, allocate = 0;
  232. if(nparam >= 2) {
  233. sq_getinteger(v, 2, &size);
  234. }
  235. if(nparam >= 3) {
  236. sq_getinteger(v, 2, &allocate);
  237. }
  238. if(size < 0) return sq_throwerror(v, _SC("cannot create blob with negative size"));
  239. if(allocate < 0) return sq_throwerror(v, _SC("cannot create blob with negative allocate"));
  240. //SQBlob *b = new SQBlob(size);
  241. SQBlob *b = new (sq_malloc(sizeof(SQBlob)))SQBlob(size, allocate);
  242. if(SQ_FAILED(sq_setinstanceup(v,1,b))) {
  243. b->~SQBlob();
  244. sq_free(b,sizeof(SQBlob));
  245. return sq_throwerror(v, _SC("cannot create blob"));
  246. }
  247. sq_setreleasehook(v,1,_blob_releasehook);
  248. return 0;
  249. }
  250. static SQInteger _blob__cloned(HSQUIRRELVM v)
  251. {
  252. SQBlob *other = NULL;
  253. {
  254. if(SQ_FAILED(sq_getinstanceup(v,2,(SQUserPointer*)&other,(SQUserPointer)SQSTD_BLOB_TYPE_TAG)))
  255. return SQ_ERROR;
  256. }
  257. //SQBlob *thisone = new SQBlob(other->Len());
  258. SQBlob *thisone = new (sq_malloc(sizeof(SQBlob)))SQBlob(other->Len());
  259. memcpy(thisone->GetBuf(),other->GetBuf(),thisone->Len());
  260. if(SQ_FAILED(sq_setinstanceup(v,1,thisone))) {
  261. thisone->~SQBlob();
  262. sq_free(thisone,sizeof(SQBlob));
  263. return sq_throwerror(v, _SC("cannot clone blob"));
  264. }
  265. sq_setreleasehook(v,1,_blob_releasehook);
  266. return 0;
  267. }
  268. static SQInteger _blob__tostring(HSQUIRRELVM v)
  269. {
  270. SETUP_BLOB(v);
  271. sq_pushstring(v, (const SQChar*)self->GetBuf(), self->Len());
  272. return 1;
  273. }
  274. static SQInteger _blob_tostring(HSQUIRRELVM v)
  275. {
  276. return _blob__tostring(v);
  277. }
  278. static SQInteger _blob_setLen(HSQUIRRELVM v)
  279. {
  280. SQ_FUNC_VARS_NO_TOP(v);
  281. SETUP_BLOB(v);
  282. SQ_GET_INTEGER(v, 2, newLen);
  283. self->SetLen(newLen);
  284. return 0;
  285. }
  286. static SQInteger _blob_clear(HSQUIRRELVM v)
  287. {
  288. SETUP_BLOB(v);
  289. self->SetLen(0);
  290. return 0;
  291. }
  292. #define _DECL_BLOB_FUNC(name,nparams,typecheck) {_SC(#name),_blob_##name,nparams,typecheck}
  293. static SQRegFunction _blob_methods[] = {
  294. _DECL_BLOB_FUNC(constructor,-1,_SC("xnn")),
  295. _DECL_BLOB_FUNC(resize,2,_SC("xn")),
  296. _DECL_BLOB_FUNC(reserve,2,_SC("xn")),
  297. _DECL_BLOB_FUNC(swap2,1,_SC("x")),
  298. _DECL_BLOB_FUNC(swap4,1,_SC("x")),
  299. _DECL_BLOB_FUNC(memset,4,_SC("xiii")),
  300. _DECL_BLOB_FUNC(_set,3,_SC("xnn")),
  301. _DECL_BLOB_FUNC(_get,2,_SC("xn")),
  302. _DECL_BLOB_FUNC(_typeof,1,_SC("x")),
  303. _DECL_BLOB_FUNC(_nexti,2,_SC("x")),
  304. _DECL_BLOB_FUNC(_cloned,2,_SC("xx")),
  305. //_DECL_BLOB_FUNC(_tostring,1,_SC("x")),
  306. _DECL_BLOB_FUNC(tostring,1,_SC("x")),
  307. _DECL_BLOB_FUNC(setLen,2,_SC("xi")),
  308. _DECL_BLOB_FUNC(clear,1,_SC("x")),
  309. {0,0,0,0}
  310. };
  311. //GLOBAL FUNCTIONS
  312. static SQInteger _g_blob_casti2f(HSQUIRRELVM v)
  313. {
  314. SQInteger i;
  315. sq_getinteger(v,2,&i);
  316. sq_pushfloat(v,*((SQFloat *)&i));
  317. return 1;
  318. }
  319. static SQInteger _g_blob_castf2i(HSQUIRRELVM v)
  320. {
  321. SQFloat f;
  322. sq_getfloat(v,2,&f);
  323. sq_pushinteger(v,*((SQInteger *)&f));
  324. return 1;
  325. }
  326. static SQInteger _g_blob_swap2(HSQUIRRELVM v)
  327. {
  328. SQInteger i;
  329. sq_getinteger(v,2,&i);
  330. short s=(short)i;
  331. sq_pushinteger(v,(s<<8)|((s>>8)&0x00FF));
  332. return 1;
  333. }
  334. static SQInteger _g_blob_swap4(HSQUIRRELVM v)
  335. {
  336. SQInteger i;
  337. sq_getinteger(v,2,&i);
  338. unsigned int t4 = (unsigned int)i;
  339. __swap_dword(&t4);
  340. sq_pushinteger(v,(SQInteger)t4);
  341. return 1;
  342. }
  343. static SQInteger _g_blob_swapfloat(HSQUIRRELVM v)
  344. {
  345. SQFloat f;
  346. sq_getfloat(v,2,&f);
  347. __swap_dword((unsigned int *)&f);
  348. sq_pushfloat(v,f);
  349. return 1;
  350. }
  351. #define _DECL_GLOBALBLOB_FUNC(name,nparams,typecheck) {_SC(#name),_g_blob_##name,nparams,typecheck}
  352. static SQRegFunction bloblib_funcs[]={
  353. _DECL_GLOBALBLOB_FUNC(casti2f,2,_SC(".n")),
  354. _DECL_GLOBALBLOB_FUNC(castf2i,2,_SC(".n")),
  355. _DECL_GLOBALBLOB_FUNC(swap2,2,_SC(".n")),
  356. _DECL_GLOBALBLOB_FUNC(swap4,2,_SC(".n")),
  357. _DECL_GLOBALBLOB_FUNC(swapfloat,2,_SC(".n")),
  358. {0,0}
  359. };
  360. SQRESULT sqstd_getblob(HSQUIRRELVM v,SQInteger idx,SQUserPointer *ptr)
  361. {
  362. SQBlob *blob;
  363. if(SQ_FAILED(sq_getinstanceup(v,idx,(SQUserPointer *)&blob,(SQUserPointer)SQSTD_BLOB_TYPE_TAG)))
  364. return -1;
  365. *ptr = blob->GetBuf();
  366. return SQ_OK;
  367. }
  368. SQInteger sqstd_getblobsize(HSQUIRRELVM v,SQInteger idx)
  369. {
  370. SQBlob *blob;
  371. if(SQ_FAILED(sq_getinstanceup(v,idx,(SQUserPointer *)&blob,(SQUserPointer)SQSTD_BLOB_TYPE_TAG)))
  372. return -1;
  373. return blob->Len();
  374. }
  375. SQInteger blob_read(SQUserPointer file,SQUserPointer buf,SQInteger size)
  376. {
  377. SQInteger ret;
  378. SQBlob *blob = (SQBlob *)file;
  379. if( ( ret = blob->Read(buf, size)) !=0 ) return ret;
  380. return -1;
  381. }
  382. SQInteger blob_write(SQUserPointer file,SQUserPointer p,SQInteger size)
  383. {
  384. SQBlob *blob = (SQBlob *)file;
  385. return blob->Write(p,size);
  386. }
  387. SQUserPointer sqstd_createblob(HSQUIRRELVM v, SQInteger size)
  388. {
  389. SQInteger top = sq_gettop(v);
  390. sq_pushregistrytable(v);
  391. sq_pushstring(v,_SC("std_blob"),-1);
  392. if(SQ_SUCCEEDED(sq_get(v,-2))) {
  393. sq_remove(v,-2); //removes the registry
  394. sq_push(v,1); // push the this
  395. sq_pushinteger(v,size); //size
  396. SQBlob *blob = NULL;
  397. if(SQ_SUCCEEDED(sq_call(v,2,SQTrue,SQFalse))
  398. && SQ_SUCCEEDED(sq_getinstanceup(v,-1,(SQUserPointer *)&blob,(SQUserPointer)SQSTD_BLOB_TYPE_TAG))) {
  399. sq_remove(v,-2);
  400. return blob->GetBuf();
  401. }
  402. }
  403. sq_settop(v,top);
  404. return NULL;
  405. }
  406. SQRESULT sqstd_register_bloblib(HSQUIRRELVM v)
  407. {
  408. return declare_stream(v,_SC("blob"),(SQUserPointer)SQSTD_BLOB_TYPE_TAG,_SC("std_blob"),_blob_methods,bloblib_funcs);
  409. }