sqstdblob.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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 void __swap_dword(unsigned int *n)
  123. {
  124. *n=(unsigned int)(((*n&0xFF000000)>>24) |
  125. ((*n&0x00FF0000)>>8) |
  126. ((*n&0x0000FF00)<<8) |
  127. ((*n&0x000000FF)<<24));
  128. }
  129. static void __swap_word(unsigned short *n)
  130. {
  131. *n=(unsigned short)((*n>>8)&0x00FF)| ((*n<<8)&0xFF00);
  132. }
  133. static SQInteger _blob_swap4(HSQUIRRELVM v)
  134. {
  135. SETUP_BLOB(v);
  136. SQInteger num=(self->Len()-(self->Len()%4))>>2;
  137. unsigned int *t=(unsigned int *)self->GetBuf();
  138. for(SQInteger i = 0; i < num; i++) {
  139. __swap_dword(&t[i]);
  140. }
  141. return 0;
  142. }
  143. static SQInteger _blob_swap2(HSQUIRRELVM v)
  144. {
  145. SETUP_BLOB(v);
  146. SQInteger num=(self->Len()-(self->Len()%2))>>1;
  147. unsigned short *t = (unsigned short *)self->GetBuf();
  148. for(SQInteger i = 0; i < num; i++) {
  149. __swap_word(&t[i]);
  150. }
  151. return 0;
  152. }
  153. static SQInteger _blob_memset(HSQUIRRELVM v)
  154. {
  155. SETUP_BLOB(v);
  156. SQInteger idx,val,size;
  157. sq_getinteger(v,2,&idx);
  158. sq_getinteger(v,3,&val);
  159. sq_getinteger(v,4,&size);
  160. if(idx < 0 || idx >= self->Len())
  161. return sq_throwerror(v,_SC("index out of range"));
  162. if(idx+size < 0 || idx+size >= self->Len())
  163. return sq_throwerror(v,_SC("index+size out of range"));
  164. memset(((unsigned char*)self->GetBuf())+idx, val, size);
  165. sq_push(v,3);
  166. return 1;
  167. }
  168. static SQInteger _blob__set(HSQUIRRELVM v)
  169. {
  170. SETUP_BLOB(v);
  171. SQInteger idx,val;
  172. sq_getinteger(v,2,&idx);
  173. sq_getinteger(v,3,&val);
  174. if(idx < 0 || idx >= self->Len())
  175. return sq_throwerror(v,_SC("index out of range"));
  176. ((unsigned char *)self->GetBuf())[idx] = (unsigned char) val;
  177. sq_push(v,3);
  178. return 1;
  179. }
  180. static SQInteger _blob__get(HSQUIRRELVM v)
  181. {
  182. SETUP_BLOB(v);
  183. SQInteger idx;
  184. sq_getinteger(v,2,&idx);
  185. if(idx < 0 || idx >= self->Len())
  186. return sq_throwerror(v,_SC("index out of range"));
  187. sq_pushinteger(v,((unsigned char *)self->GetBuf())[idx]);
  188. return 1;
  189. }
  190. static SQInteger _blob__nexti(HSQUIRRELVM v)
  191. {
  192. SETUP_BLOB(v);
  193. if(sq_gettype(v,2) == OT_NULL) {
  194. sq_pushinteger(v, 0);
  195. return 1;
  196. }
  197. SQInteger idx;
  198. if(SQ_SUCCEEDED(sq_getinteger(v, 2, &idx))) {
  199. if(idx+1 < self->Len()) {
  200. sq_pushinteger(v, idx+1);
  201. return 1;
  202. }
  203. sq_pushnull(v);
  204. return 1;
  205. }
  206. return sq_throwerror(v,_SC("internal error (_nexti) wrong argument type"));
  207. }
  208. static SQInteger _blob__typeof(HSQUIRRELVM v)
  209. {
  210. sq_pushstring(v,_SC("blob"),-1);
  211. return 1;
  212. }
  213. static SQInteger _blob_releasehook(SQUserPointer p, SQInteger size, HSQUIRRELVM v)
  214. {
  215. SQBlob *self = (SQBlob*)p;
  216. self->~SQBlob();
  217. sq_free(self,sizeof(SQBlob));
  218. return 1;
  219. }
  220. static SQInteger _blob_constructor(HSQUIRRELVM v)
  221. {
  222. SQInteger nparam = sq_gettop(v);
  223. SQInteger size = 0, allocate = 0;
  224. if(nparam >= 2) {
  225. sq_getinteger(v, 2, &size);
  226. }
  227. if(nparam >= 3) {
  228. sq_getinteger(v, 2, &allocate);
  229. }
  230. if(size < 0) return sq_throwerror(v, _SC("cannot create blob with negative size"));
  231. if(allocate < 0) return sq_throwerror(v, _SC("cannot create blob with negative allocate"));
  232. //SQBlob *b = new SQBlob(size);
  233. SQBlob *b = new (sq_malloc(sizeof(SQBlob)))SQBlob(size, allocate);
  234. if(SQ_FAILED(sq_setinstanceup(v,1,b))) {
  235. b->~SQBlob();
  236. sq_free(b,sizeof(SQBlob));
  237. return sq_throwerror(v, _SC("cannot create blob"));
  238. }
  239. sq_setreleasehook(v,1,_blob_releasehook);
  240. return 0;
  241. }
  242. static SQInteger _blob__cloned(HSQUIRRELVM v)
  243. {
  244. SQBlob *other = NULL;
  245. {
  246. if(SQ_FAILED(sq_getinstanceup(v,2,(SQUserPointer*)&other,(SQUserPointer)SQSTD_BLOB_TYPE_TAG)))
  247. return SQ_ERROR;
  248. }
  249. //SQBlob *thisone = new SQBlob(other->Len());
  250. SQBlob *thisone = new (sq_malloc(sizeof(SQBlob)))SQBlob(other->Len());
  251. memcpy(thisone->GetBuf(),other->GetBuf(),thisone->Len());
  252. if(SQ_FAILED(sq_setinstanceup(v,1,thisone))) {
  253. thisone->~SQBlob();
  254. sq_free(thisone,sizeof(SQBlob));
  255. return sq_throwerror(v, _SC("cannot clone blob"));
  256. }
  257. sq_setreleasehook(v,1,_blob_releasehook);
  258. return 0;
  259. }
  260. static SQInteger _blob__tostring(HSQUIRRELVM v)
  261. {
  262. SETUP_BLOB(v);
  263. sq_pushstring(v, (const SQChar*)self->GetBuf(), self->Len());
  264. return 1;
  265. }
  266. static SQInteger _blob_asString(HSQUIRRELVM v)
  267. {
  268. return _blob__tostring(v);
  269. }
  270. static SQInteger _blob_setLen(HSQUIRRELVM v)
  271. {
  272. SQ_FUNC_VARS_NO_TOP(v);
  273. SETUP_BLOB(v);
  274. SQ_GET_INTEGER(v, 2, newLen);
  275. self->SetLen(newLen);
  276. return 0;
  277. }
  278. static SQInteger _blob_clear(HSQUIRRELVM v)
  279. {
  280. SETUP_BLOB(v);
  281. self->SetLen(0);
  282. return 0;
  283. }
  284. #define _DECL_BLOB_FUNC(name,nparams,typecheck) {_SC(#name),_blob_##name,nparams,typecheck}
  285. static SQRegFunction _blob_methods[] = {
  286. _DECL_BLOB_FUNC(constructor,-1,_SC("xnn")),
  287. _DECL_BLOB_FUNC(resize,2,_SC("xn")),
  288. _DECL_BLOB_FUNC(swap2,1,_SC("x")),
  289. _DECL_BLOB_FUNC(swap4,1,_SC("x")),
  290. _DECL_BLOB_FUNC(memset,4,_SC("xnnn")),
  291. _DECL_BLOB_FUNC(_set,3,_SC("xnn")),
  292. _DECL_BLOB_FUNC(_get,2,_SC("xn")),
  293. _DECL_BLOB_FUNC(_typeof,1,_SC("x")),
  294. _DECL_BLOB_FUNC(_nexti,2,_SC("x")),
  295. _DECL_BLOB_FUNC(_cloned,2,_SC("xx")),
  296. _DECL_BLOB_FUNC(_tostring,1,_SC("x")),
  297. _DECL_BLOB_FUNC(asString,1,_SC("x")),
  298. _DECL_BLOB_FUNC(setLen,2,_SC("xi")),
  299. _DECL_BLOB_FUNC(clear,1,_SC("x")),
  300. {0,0,0,0}
  301. };
  302. //GLOBAL FUNCTIONS
  303. static SQInteger _g_blob_casti2f(HSQUIRRELVM v)
  304. {
  305. SQInteger i;
  306. sq_getinteger(v,2,&i);
  307. sq_pushfloat(v,*((SQFloat *)&i));
  308. return 1;
  309. }
  310. static SQInteger _g_blob_castf2i(HSQUIRRELVM v)
  311. {
  312. SQFloat f;
  313. sq_getfloat(v,2,&f);
  314. sq_pushinteger(v,*((SQInteger *)&f));
  315. return 1;
  316. }
  317. static SQInteger _g_blob_swap2(HSQUIRRELVM v)
  318. {
  319. SQInteger i;
  320. sq_getinteger(v,2,&i);
  321. short s=(short)i;
  322. sq_pushinteger(v,(s<<8)|((s>>8)&0x00FF));
  323. return 1;
  324. }
  325. static SQInteger _g_blob_swap4(HSQUIRRELVM v)
  326. {
  327. SQInteger i;
  328. sq_getinteger(v,2,&i);
  329. unsigned int t4 = (unsigned int)i;
  330. __swap_dword(&t4);
  331. sq_pushinteger(v,(SQInteger)t4);
  332. return 1;
  333. }
  334. static SQInteger _g_blob_swapfloat(HSQUIRRELVM v)
  335. {
  336. SQFloat f;
  337. sq_getfloat(v,2,&f);
  338. __swap_dword((unsigned int *)&f);
  339. sq_pushfloat(v,f);
  340. return 1;
  341. }
  342. #define _DECL_GLOBALBLOB_FUNC(name,nparams,typecheck) {_SC(#name),_g_blob_##name,nparams,typecheck}
  343. static SQRegFunction bloblib_funcs[]={
  344. _DECL_GLOBALBLOB_FUNC(casti2f,2,_SC(".n")),
  345. _DECL_GLOBALBLOB_FUNC(castf2i,2,_SC(".n")),
  346. _DECL_GLOBALBLOB_FUNC(swap2,2,_SC(".n")),
  347. _DECL_GLOBALBLOB_FUNC(swap4,2,_SC(".n")),
  348. _DECL_GLOBALBLOB_FUNC(swapfloat,2,_SC(".n")),
  349. {0,0}
  350. };
  351. SQRESULT sqstd_getblob(HSQUIRRELVM v,SQInteger idx,SQUserPointer *ptr)
  352. {
  353. SQBlob *blob;
  354. if(SQ_FAILED(sq_getinstanceup(v,idx,(SQUserPointer *)&blob,(SQUserPointer)SQSTD_BLOB_TYPE_TAG)))
  355. return -1;
  356. *ptr = blob->GetBuf();
  357. return SQ_OK;
  358. }
  359. SQInteger sqstd_getblobsize(HSQUIRRELVM v,SQInteger idx)
  360. {
  361. SQBlob *blob;
  362. if(SQ_FAILED(sq_getinstanceup(v,idx,(SQUserPointer *)&blob,(SQUserPointer)SQSTD_BLOB_TYPE_TAG)))
  363. return -1;
  364. return blob->Len();
  365. }
  366. SQInteger blob_read(SQUserPointer file,SQUserPointer buf,SQInteger size)
  367. {
  368. SQInteger ret;
  369. SQBlob *blob = (SQBlob *)file;
  370. if( ( ret = blob->Read(buf, size)) !=0 ) return ret;
  371. return -1;
  372. }
  373. SQInteger blob_write(SQUserPointer file,SQUserPointer p,SQInteger size)
  374. {
  375. SQBlob *blob = (SQBlob *)file;
  376. return blob->Write(p,size);
  377. }
  378. SQUserPointer sqstd_createblob(HSQUIRRELVM v, SQInteger size)
  379. {
  380. SQInteger top = sq_gettop(v);
  381. sq_pushregistrytable(v);
  382. sq_pushstring(v,_SC("std_blob"),-1);
  383. if(SQ_SUCCEEDED(sq_get(v,-2))) {
  384. sq_remove(v,-2); //removes the registry
  385. sq_push(v,1); // push the this
  386. sq_pushinteger(v,size); //size
  387. SQBlob *blob = NULL;
  388. if(SQ_SUCCEEDED(sq_call(v,2,SQTrue,SQFalse))
  389. && SQ_SUCCEEDED(sq_getinstanceup(v,-1,(SQUserPointer *)&blob,(SQUserPointer)SQSTD_BLOB_TYPE_TAG))) {
  390. sq_remove(v,-2);
  391. return blob->GetBuf();
  392. }
  393. }
  394. sq_settop(v,top);
  395. return NULL;
  396. }
  397. SQRESULT sqstd_register_bloblib(HSQUIRRELVM v)
  398. {
  399. return declare_stream(v,_SC("blob"),(SQUserPointer)SQSTD_BLOB_TYPE_TAG,_SC("std_blob"),_blob_methods,bloblib_funcs);
  400. }