sqstdblob.cpp 11 KB

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