sq_base64.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. #include "squirrel.h"
  10. #include <string.h>
  11. #include "sqstdblobimpl.h"
  12. #define MYNAME _SC("base64")
  13. #define MYVERSION _SC(MYNAME " library for " LUA_VERSION " / Jun 2007")
  14. #define uint unsigned int
  15. static const char code[]=
  16. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  17. static void encode(SQBlob &b, uint c1, uint c2, uint c3, int n)
  18. {
  19. unsigned long tuple=c3+256UL*(c2+256UL*c1);
  20. int i;
  21. char s[4];
  22. for (i=0; i<4; i++)
  23. {
  24. s[3-i] = code[tuple % 64];
  25. tuple /= 64;
  26. }
  27. for (i=n+1; i<4; i++) s[i]='=';
  28. b.Write(s,4);
  29. }
  30. static SQRESULT base64_encode(HSQUIRRELVM v) /** encode(s) */
  31. {
  32. SQInteger l;
  33. const unsigned char *s;
  34. SQRESULT rc;
  35. if((rc = sq_getstr_and_size(v,2, (const char **)&s, &l)) != SQ_OK) return rc;
  36. SQBlob b(0, l*2);
  37. int n;
  38. for (n=l/3; n--; s+=3) encode(b,s[0],s[1],s[2],3);
  39. switch (l%3)
  40. {
  41. case 1:
  42. encode(b,s[0],0,0,1);
  43. break;
  44. case 2:
  45. encode(b,s[0],s[1],0,2);
  46. break;
  47. }
  48. sq_pushstring(v, (SQChar*)b.GetBuf(), b.Len());
  49. return 1;
  50. }
  51. static void decode(SQBlob &b, int c1, int c2, int c3, int c4, int n)
  52. {
  53. unsigned long tuple=c4+64L*(c3+64L*(c2+64L*c1));
  54. char s[3];
  55. switch (--n)
  56. {
  57. case 3:
  58. s[2]=tuple;
  59. case 2:
  60. s[1]=tuple >> 8;
  61. case 1:
  62. s[0]=tuple >> 16;
  63. }
  64. b.Write(s,n);
  65. }
  66. static SQRESULT base64_decode(HSQUIRRELVM v) /** decode(s) */
  67. {
  68. SQInteger l;
  69. const unsigned char *s;
  70. SQRESULT rc;
  71. if((rc = sq_getstr_and_size(v,2, (const char **)&s, &l)) != SQ_OK) return rc;
  72. SQBlob b(0, l*2);
  73. int n=0;
  74. char t[4];
  75. for (;;)
  76. {
  77. int c=*s++;
  78. switch (c)
  79. {
  80. const char *p;
  81. default:
  82. p=strchr(code,c);
  83. if (p==NULL) return 0;
  84. t[n++]= p-code;
  85. if (n==4)
  86. {
  87. decode(b,t[0],t[1],t[2],t[3],4);
  88. n=0;
  89. }
  90. break;
  91. case '=':
  92. switch (n)
  93. {
  94. case 1:
  95. decode(b,t[0],0,0,0,1);
  96. break;
  97. case 2:
  98. decode(b,t[0],t[1],0,0,2);
  99. break;
  100. case 3:
  101. decode(b,t[0],t[1],t[2],0,3);
  102. break;
  103. }
  104. case 0:
  105. sq_pushstring(v, (SQChar*)b.GetBuf(), b.Len());
  106. return 1;
  107. case '\n':
  108. case '\r':
  109. case '\t':
  110. case ' ':
  111. case '\f':
  112. case '\b':
  113. break;
  114. }
  115. }
  116. return 0;
  117. }
  118. #define _DECL_FUNC(name,nparams,tycheck) {_SC(#name),base64_##name,nparams,tycheck}
  119. static SQRegFunction base64_methods[] =
  120. {
  121. _DECL_FUNC(encode,2,_SC(".s")),
  122. _DECL_FUNC(decode,2,_SC(".s")),
  123. {0,0}
  124. };
  125. #ifdef __cplusplus
  126. extern "C" {
  127. #endif
  128. SQRESULT sqext_register_base64(HSQUIRRELVM v)
  129. {
  130. sq_pushstring(v,_SC("base64"),-1);
  131. sq_newtable(v);
  132. sq_insert_reg_funcs(v, base64_methods);
  133. sq_newslot(v,-3,SQTrue);
  134. return 1;
  135. }
  136. #ifdef __cplusplus
  137. }
  138. #endif