sq_base64.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /*
  2. * lbase64.c
  3. * base64 encoding and decoding for Lua 5.1
  4. * Luiz Henrique de Figueiredo <[email protected]>
  5. * 27 Jun 2007 19:04:40
  6. * This code is hereby placed in the public domain.
  7. * Ported to Squrrel by Domingo Alvarez Duarte
  8. */
  9. /*
  10. * lascii85.c
  11. * ascii85 encoding and decoding for Lua 5.1
  12. * Luiz Henrique de Figueiredo <[email protected]>
  13. * 23 Mar 2010 22:25:18
  14. * This code is hereby placed in the public domain.
  15. * Ported to Squrrel by Domingo Alvarez Duarte
  16. */
  17. #include "squirrel.h"
  18. #include <string.h>
  19. #include "sqstdblobimpl.h"
  20. #define uint unsigned int
  21. #define ROUND_BLOB_SIZE(x) (x < 8192 ? 8192 : x*2)
  22. static void encode85(SQBlob &b, uint c1, uint c2, uint c3, uint c4, int n)
  23. {
  24. unsigned long tuple=c4+256UL*(c3+256UL*(c2+256UL*c1));
  25. if (tuple==0 && n==4)
  26. b.Write("z",1);
  27. else
  28. {
  29. int i;
  30. char s[5];
  31. for (i=0; i<5; i++)
  32. {
  33. s[4-i] = '!' + (tuple % 85);
  34. tuple /= 85;
  35. }
  36. b.Write(s,n+1);
  37. }
  38. }
  39. static SQRESULT ascii85_encode(HSQUIRRELVM v) /** encode(s) */
  40. {
  41. SQInteger l;
  42. const unsigned char *s;
  43. SQRESULT rc;
  44. if((rc = sq_getstr_and_size(v,2, (const SQChar**)&s, &l)) != SQ_OK) return rc;
  45. SQBlob b(0, ROUND_BLOB_SIZE(l));
  46. int n;
  47. b.Write("<~",2);
  48. for (n=l/4; n--; s+=4) encode85(b,s[0],s[1],s[2],s[3],4);
  49. switch (l%4)
  50. {
  51. case 1:
  52. encode85(b,s[0],0,0,0,1);
  53. break;
  54. case 2:
  55. encode85(b,s[0],s[1],0,0,2);
  56. break;
  57. case 3:
  58. encode85(b,s[0],s[1],s[2],0,3);
  59. break;
  60. }
  61. b.Write("~>",2);
  62. sq_pushstring(v, (SQChar*)b.GetBuf(), b.Len());
  63. return 1;
  64. }
  65. static void decode85(SQBlob &b, int c1, int c2, int c3, int c4, int c5, int n)
  66. {
  67. unsigned long tuple=c5+85L*(c4+85L*(c3+85L*(c2+85L*c1)));
  68. char s[4];
  69. switch (--n)
  70. {
  71. case 4:
  72. s[3]=tuple;
  73. case 3:
  74. s[2]=tuple >> 8;
  75. case 2:
  76. s[1]=tuple >> 16;
  77. case 1:
  78. s[0]=tuple >> 24;
  79. }
  80. b.Write(s,n);
  81. }
  82. static SQRESULT ascii85_decode(HSQUIRRELVM v) /** decode(s) */
  83. {
  84. SQInteger l;
  85. const unsigned char *s;
  86. SQRESULT rc;
  87. if((rc = sq_getstr_and_size(v,2, (const SQChar**)&s, &l)) != SQ_OK) return rc;
  88. SQBlob b(0, ROUND_BLOB_SIZE(l));
  89. int n=0;
  90. char t[5];
  91. if (*s++!='<') return 0;
  92. if (*s++!='~') return 0;
  93. for (;;)
  94. {
  95. int c=*s++;
  96. switch (c)
  97. {
  98. default:
  99. if (c<'!' || c>'u') return 0;
  100. t[n++]=c-'!';
  101. if (n==5)
  102. {
  103. decode85(b,t[0],t[1],t[2],t[3],t[4],5);
  104. n=0;
  105. }
  106. break;
  107. case 'z':
  108. b.Write("\0\0\0\0",4);
  109. break;
  110. case '~':
  111. if (*s!='>') return 0;
  112. switch (n)
  113. {
  114. case 1:
  115. decode85(b, t[0],85,0,0,0,1);
  116. break;
  117. case 2:
  118. decode85(b, t[0],t[1],85,0,0,2);
  119. break;
  120. case 3:
  121. decode85(b,t[0],t[1],t[2],85,0,3);
  122. break;
  123. case 4:
  124. decode85(b,t[0],t[1],t[2],t[3],85,4);
  125. break;
  126. }
  127. sq_pushstring(v, (SQChar*)b.GetBuf(), b.Len());
  128. return 1;
  129. case '\n':
  130. case '\r':
  131. case '\t':
  132. case ' ':
  133. case '\0':
  134. case '\f':
  135. case '\b':
  136. case 0177:
  137. break;
  138. }
  139. }
  140. return 0;
  141. }
  142. #define _DECL_FUNC(name,nparams,tycheck) {_SC(#name),ascii85_##name,nparams,tycheck}
  143. static SQRegFunction ascii85_methods[] =
  144. {
  145. _DECL_FUNC(encode,2,_SC(".s")),
  146. _DECL_FUNC(decode,2,_SC(".s")),
  147. {0,0}
  148. };
  149. #undef _DECL_FUNC
  150. static const char code[]=
  151. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  152. static void encode64(SQBlob &b, uint c1, uint c2, uint c3, int n)
  153. {
  154. unsigned long tuple=c3+256UL*(c2+256UL*c1);
  155. int i;
  156. char s[4];
  157. for (i=0; i<4; i++)
  158. {
  159. s[3-i] = code[tuple % 64];
  160. tuple /= 64;
  161. }
  162. for (i=n+1; i<4; i++) s[i]='=';
  163. b.Write(s,4);
  164. }
  165. static SQRESULT base64_encode(HSQUIRRELVM v) /** encode(s) */
  166. {
  167. SQInteger l;
  168. const unsigned char *s;
  169. SQRESULT rc;
  170. if((rc = sq_getstr_and_size(v,2, (const SQChar**)&s, &l)) != SQ_OK) return rc;
  171. SQBlob b(0, ROUND_BLOB_SIZE(l));
  172. int n;
  173. for (n=l/3; n--; s+=3) encode64(b,s[0],s[1],s[2],3);
  174. switch (l%3)
  175. {
  176. case 1:
  177. encode64(b,s[0],0,0,1);
  178. break;
  179. case 2:
  180. encode64(b,s[0],s[1],0,2);
  181. break;
  182. }
  183. sq_pushstring(v, (SQChar*)b.GetBuf(), b.Len());
  184. return 1;
  185. }
  186. static void decode64(SQBlob &b, int c1, int c2, int c3, int c4, int n)
  187. {
  188. unsigned long tuple=c4+64L*(c3+64L*(c2+64L*c1));
  189. char s[3];
  190. switch (--n)
  191. {
  192. case 3:
  193. s[2]=tuple;
  194. case 2:
  195. s[1]=tuple >> 8;
  196. case 1:
  197. s[0]=tuple >> 16;
  198. }
  199. b.Write(s,n);
  200. }
  201. static SQRESULT base64_decode(HSQUIRRELVM v) /** decode(s) */
  202. {
  203. SQInteger l;
  204. const unsigned char *s;
  205. SQRESULT rc;
  206. if((rc = sq_getstr_and_size(v,2, (const SQChar**)&s, &l)) != SQ_OK) return rc;
  207. SQBlob b(0, ROUND_BLOB_SIZE(l));
  208. int n=0;
  209. char t[4];
  210. for (;;)
  211. {
  212. int c=*s++;
  213. switch (c)
  214. {
  215. const char *p;
  216. default:
  217. p=strchr(code,c);
  218. if (p==NULL) return 0;
  219. t[n++]= p-code;
  220. if (n==4)
  221. {
  222. decode64(b,t[0],t[1],t[2],t[3],4);
  223. n=0;
  224. }
  225. break;
  226. case '=':
  227. switch (n)
  228. {
  229. case 1:
  230. decode64(b,t[0],0,0,0,1);
  231. break;
  232. case 2:
  233. decode64(b,t[0],t[1],0,0,2);
  234. break;
  235. case 3:
  236. decode64(b,t[0],t[1],t[2],0,3);
  237. break;
  238. }
  239. case 0:
  240. sq_pushstring(v, (SQChar*)b.GetBuf(), b.Len());
  241. return 1;
  242. case '\n':
  243. case '\r':
  244. case '\t':
  245. case ' ':
  246. case '\f':
  247. case '\b':
  248. break;
  249. }
  250. }
  251. return 0;
  252. }
  253. #define _DECL_FUNC(name,nparams,tycheck) {_SC(#name),base64_##name,nparams,tycheck}
  254. static SQRegFunction base64_methods[] =
  255. {
  256. _DECL_FUNC(encode,2,_SC(".s")),
  257. _DECL_FUNC(decode,2,_SC(".s")),
  258. {0,0}
  259. };
  260. #undef _DECL_FUNC
  261. #ifdef __cplusplus
  262. extern "C" {
  263. #endif
  264. SQRESULT sqext_register_base64(HSQUIRRELVM v)
  265. {
  266. sq_pushstring(v,_SC("base64"),-1);
  267. sq_newtable(v);
  268. sq_insert_reg_funcs(v, base64_methods);
  269. sq_newslot(v,-3,SQTrue);
  270. sq_pushstring(v,_SC("ascii85"),-1);
  271. sq_newtable(v);
  272. sq_insert_reg_funcs(v, ascii85_methods);
  273. sq_newslot(v,-3,SQTrue);
  274. return 0;
  275. }
  276. #ifdef __cplusplus
  277. }
  278. #endif