strbit.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. This software is licensed under the zlib license.
  3. (C) Copyright 2015 Pedro Gimeno Fortea. All rights reserved.
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the author or authors be held liable for any
  6. damages arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include <lua.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. static int bshiftleft(lua_State *L)
  22. {
  23. size_t len;
  24. unsigned char *src, *dst;
  25. int rot;
  26. size_t nbytes;
  27. if (lua_gettop(L) < 2
  28. || lua_type(L, 1) != LUA_TSTRING
  29. || lua_type(L, 2) != LUA_TNUMBER
  30. )
  31. return 0;
  32. src = (unsigned char *)lua_tolstring(L, 1, &len);
  33. dst = malloc(len);
  34. if (!dst)
  35. return 0;
  36. if (!len)
  37. {
  38. lua_pushlstring(L, NULL, 0);
  39. return 1;
  40. }
  41. rot = (int)lua_tonumber(L, 2);
  42. if (rot == 0)
  43. {
  44. memcpy(dst, src, len);
  45. }
  46. else if (rot < 0)
  47. {
  48. rot = -rot;
  49. /* Shift right */
  50. nbytes = (unsigned)rot >> 3;
  51. rot = rot & 7;
  52. if (nbytes >= len)
  53. {
  54. /* Shifted all the way out */
  55. memset(dst, 0, len);
  56. }
  57. else if (rot)
  58. {
  59. /* Bit-granular right shift */
  60. int i;
  61. unsigned tmp;
  62. unsigned char *dst2 = dst + nbytes;
  63. /* at this point 0 <= nbytes < len, so this can't go negative: */
  64. len -= nbytes;
  65. tmp = src[0];
  66. *dst2++ = tmp >> rot;
  67. i = 1;
  68. while (i < len)
  69. {
  70. tmp = (tmp << 8) | src[i++];
  71. *dst2++ = tmp >> rot;
  72. }
  73. len += nbytes; /* restore original length */
  74. /* fill leading chunk with zeros, if it exists */
  75. memset(dst, 0, nbytes);
  76. }
  77. else
  78. {
  79. /* Byte-aligned right shift */
  80. memcpy(dst + nbytes, src, len - nbytes);
  81. memset(dst, 0, nbytes);
  82. }
  83. }
  84. else
  85. {
  86. /* Shift left */
  87. nbytes = (unsigned)rot >> 3;
  88. rot = rot & 7;
  89. if (nbytes >= len)
  90. {
  91. /* Shifted all the way out */
  92. memset(dst, 0, len);
  93. }
  94. else if (rot)
  95. {
  96. /* Bit-granular left shift */
  97. int i;
  98. unsigned tmp;
  99. unsigned char *dst2 = dst;
  100. rot = 8-rot;
  101. tmp = src[i = nbytes];
  102. while (++i < len)
  103. {
  104. tmp = (tmp << 8) | src[i];
  105. *dst2++ = tmp >> rot;
  106. }
  107. *dst2++ = tmp << (8 - rot);
  108. /* fill trailing chunk with zeros, if it exists */
  109. memset(dst2, 0, nbytes);
  110. }
  111. else
  112. {
  113. /* Byte-aligned left shift */
  114. memcpy(dst, src + nbytes, len - nbytes);
  115. memset(dst + (len - nbytes), 0, nbytes);
  116. }
  117. }
  118. lua_pushlstring(L, (char *)dst, len);
  119. free(dst);
  120. return 1;
  121. }
  122. #define BINOP(name, op) \
  123. static int name(lua_State *L) \
  124. { \
  125. unsigned char *src1, *src2, *dst, *dst2, *end; \
  126. size_t len1, len2; \
  127. \
  128. if (lua_gettop(L) < 2 \
  129. || lua_type(L, 1) != LUA_TSTRING \
  130. || lua_type(L, 2) != LUA_TSTRING \
  131. ) \
  132. return 0; \
  133. \
  134. src1 = (unsigned char *)lua_tolstring(L, 1, &len1); \
  135. src2 = (unsigned char *)lua_tolstring(L, 2, &len2); \
  136. if (len2 < len1) len1 = len2; \
  137. dst = dst2 = malloc(len1); \
  138. if (!dst) \
  139. return 0; \
  140. \
  141. end = src1 + len1; \
  142. while (src1 != end) \
  143. { \
  144. *dst2++ = *src1++ op *src2++; \
  145. } \
  146. \
  147. lua_pushlstring(L, (char *)dst, len1); \
  148. free(dst); \
  149. return 1; \
  150. }
  151. BINOP(band, &)
  152. BINOP(bor, |)
  153. BINOP(bxor, ^)
  154. static int bnot(lua_State *L)
  155. {
  156. unsigned char *src, *dst, *end;
  157. size_t len;
  158. if (lua_gettop(L) < 1
  159. || lua_type(L, 1) != LUA_TSTRING
  160. )
  161. return 0;
  162. src = (unsigned char *)lua_tolstring(L, 1, &len);
  163. dst = malloc(len);
  164. if (!dst)
  165. return 0;
  166. end = src + len;
  167. while (src != end)
  168. {
  169. *dst++ = ~*src++;
  170. }
  171. lua_pushlstring(L, (char *)dst-len, len);
  172. return 1;
  173. }
  174. int luaopen_strbit(lua_State *L)
  175. {
  176. lua_newtable(L);
  177. lua_pushcfunction(L, bshiftleft);
  178. lua_setfield(L, -2, "bshiftleft");
  179. lua_pushcfunction(L, band);
  180. lua_setfield(L, -2, "band");
  181. lua_pushcfunction(L, bor);
  182. lua_setfield(L, -2, "bor");
  183. lua_pushcfunction(L, bxor);
  184. lua_setfield(L, -2, "bxor");
  185. lua_pushcfunction(L, bnot);
  186. lua_setfield(L, -2, "bnot");
  187. return 1;
  188. }