sqstdblob.cpp 9.8 KB

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