lua-bson.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364
  1. #define LUA_LIB
  2. #include <lua.h>
  3. #include <lauxlib.h>
  4. #include <time.h>
  5. #include <unistd.h>
  6. #include <stdint.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <stdbool.h>
  10. #include "atomic.h"
  11. #define DEFAULT_CAP 64
  12. #define MAX_NUMBER 1024
  13. // avoid circular reference while encodeing
  14. #define MAX_DEPTH 128
  15. #define BSON_REAL 1
  16. #define BSON_STRING 2
  17. #define BSON_DOCUMENT 3
  18. #define BSON_ARRAY 4
  19. #define BSON_BINARY 5
  20. #define BSON_UNDEFINED 6
  21. #define BSON_OBJECTID 7
  22. #define BSON_BOOLEAN 8
  23. #define BSON_DATE 9
  24. #define BSON_NULL 10
  25. #define BSON_REGEX 11
  26. #define BSON_DBPOINTER 12
  27. #define BSON_JSCODE 13
  28. #define BSON_SYMBOL 14
  29. #define BSON_CODEWS 15
  30. #define BSON_INT32 16
  31. #define BSON_TIMESTAMP 17
  32. #define BSON_INT64 18
  33. #define BSON_MINKEY 255
  34. #define BSON_MAXKEY 127
  35. #define BSON_TYPE_SHIFT 5
  36. static char bson_numstrs[MAX_NUMBER][4];
  37. static int bson_numstr_len[MAX_NUMBER];
  38. struct bson {
  39. int size;
  40. int cap;
  41. uint8_t *ptr;
  42. uint8_t buffer[DEFAULT_CAP];
  43. };
  44. struct bson_reader {
  45. const uint8_t * ptr;
  46. int size;
  47. };
  48. static inline int32_t
  49. get_length(const uint8_t * data) {
  50. const uint8_t * b = (const uint8_t *)data;
  51. int32_t len = b[0] | b[1]<<8 | b[2]<<16 | b[3]<<24;
  52. return len;
  53. }
  54. static inline void
  55. bson_destroy(struct bson *b) {
  56. if (b->ptr != b->buffer) {
  57. free(b->ptr);
  58. }
  59. }
  60. static inline void
  61. bson_create(struct bson *b) {
  62. b->size = 0;
  63. b->cap = DEFAULT_CAP;
  64. b->ptr = b->buffer;
  65. }
  66. static inline void
  67. bson_reserve(struct bson *b, int sz) {
  68. if (b->size + sz <= b->cap)
  69. return;
  70. do {
  71. b->cap *= 2;
  72. } while (b->cap <= b->size + sz);
  73. if (b->ptr == b->buffer) {
  74. b->ptr = (uint8_t*)malloc(b->cap);
  75. memcpy(b->ptr, b->buffer, b->size);
  76. } else {
  77. b->ptr = (uint8_t*)realloc(b->ptr, b->cap);
  78. }
  79. }
  80. static inline void
  81. check_reader(lua_State *L, struct bson_reader *br, int sz) {
  82. if (br->size < sz) {
  83. luaL_error(L, "Invalid bson block (%d:%d)", br->size, sz);
  84. }
  85. }
  86. static inline int
  87. read_byte(lua_State *L, struct bson_reader *br) {
  88. check_reader(L, br, 1);
  89. const uint8_t * b = br->ptr;
  90. int r = b[0];
  91. ++br->ptr;
  92. --br->size;
  93. return r;
  94. }
  95. static inline int32_t
  96. read_int32(lua_State *L, struct bson_reader *br) {
  97. check_reader(L, br, 4);
  98. const uint8_t * b = br->ptr;
  99. uint32_t v = b[0] | b[1]<<8 | b[2]<<16 | b[3]<<24;
  100. br->ptr+=4;
  101. br->size-=4;
  102. return (int32_t)v;
  103. }
  104. static inline int64_t
  105. read_int64(lua_State *L, struct bson_reader *br) {
  106. check_reader(L, br, 8);
  107. const uint8_t * b = br->ptr;
  108. uint32_t lo = b[0] | b[1]<<8 | b[2]<<16 | b[3]<<24;
  109. uint32_t hi = b[4] | b[5]<<8 | b[6]<<16 | b[7]<<24;
  110. uint64_t v = (uint64_t)lo | (uint64_t)hi<<32;
  111. br->ptr+=8;
  112. br->size-=8;
  113. return (int64_t)v;
  114. }
  115. static inline lua_Number
  116. read_double(lua_State *L, struct bson_reader *br) {
  117. check_reader(L, br, 8);
  118. union {
  119. uint64_t i;
  120. double d;
  121. } v;
  122. const uint8_t * b = br->ptr;
  123. uint32_t lo = b[0] | b[1]<<8 | b[2]<<16 | b[3]<<24;
  124. uint32_t hi = b[4] | b[5]<<8 | b[6]<<16 | b[7]<<24;
  125. v.i = (uint64_t)lo | (uint64_t)hi<<32;
  126. br->ptr+=8;
  127. br->size-=8;
  128. return v.d;
  129. }
  130. static inline const void *
  131. read_bytes(lua_State *L, struct bson_reader *br, int sz) {
  132. const void * r = br->ptr;
  133. check_reader(L, br, sz);
  134. br->ptr+=sz;
  135. br->size-=sz;
  136. return r;
  137. }
  138. static inline const char *
  139. read_cstring(lua_State *L, struct bson_reader *br, size_t *sz) {
  140. int i;
  141. for (i=0;;i++) {
  142. if (i==br->size) {
  143. luaL_error(L, "Invalid bson block : cstring");
  144. }
  145. if (br->ptr[i] == '\0') {
  146. break;
  147. }
  148. }
  149. *sz = i;
  150. const char * r = (const char *)br->ptr;
  151. br->ptr += i+1;
  152. br->size -= i+1;
  153. return r;
  154. }
  155. static inline void
  156. write_byte(struct bson *b, uint8_t v) {
  157. bson_reserve(b,1);
  158. b->ptr[b->size++] = v;
  159. }
  160. static inline void
  161. write_int32(struct bson *b, int32_t v) {
  162. uint32_t uv = (uint32_t)v;
  163. bson_reserve(b,4);
  164. b->ptr[b->size++] = uv & 0xff;
  165. b->ptr[b->size++] = (uv >> 8)&0xff;
  166. b->ptr[b->size++] = (uv >> 16)&0xff;
  167. b->ptr[b->size++] = (uv >> 24)&0xff;
  168. }
  169. static inline void
  170. write_length(struct bson *b, int32_t v, int off) {
  171. uint32_t uv = (uint32_t)v;
  172. b->ptr[off++] = uv & 0xff;
  173. b->ptr[off++] = (uv >> 8)&0xff;
  174. b->ptr[off++] = (uv >> 16)&0xff;
  175. b->ptr[off++] = (uv >> 24)&0xff;
  176. }
  177. #define MAXUNICODE 0x10FFFF
  178. static int
  179. utf8_copy(const char *s, char *d, size_t limit) {
  180. static const unsigned int limits[] = {0xFF, 0x7F, 0x7FF, 0xFFFF};
  181. unsigned int c = s[0];
  182. unsigned int res = 0;
  183. if (limit < 1)
  184. return 0;
  185. d[0] = s[0];
  186. if (c < 0x80) {
  187. return 1;
  188. } else {
  189. int count = 0;
  190. while (c & 0x40) {
  191. int cc = s[++count];
  192. if (limit <= count || (cc & 0xC0) != 0x80)
  193. return 0;
  194. d[count] = s[count];
  195. res = (res << 6) | (cc & 0x3F);
  196. c <<= 1;
  197. }
  198. res |= ((c & 0x7F) << (count * 5));
  199. if (count > 3 || res > MAXUNICODE || res <= limits[count])
  200. return 0;
  201. return count+1;
  202. }
  203. }
  204. static void
  205. write_string(struct bson *b, lua_State *L, const char *key, size_t sz) {
  206. bson_reserve(b,sz+1);
  207. char *dst = (char *)(b->ptr + b->size);
  208. const char *src = key;
  209. size_t n = sz;
  210. while(n > 0) {
  211. int c = utf8_copy(src, dst, n);
  212. if (c == 0) {
  213. luaL_error(L, "Invalid utf8 string");
  214. }
  215. src += c;
  216. dst += c;
  217. n -= c;
  218. }
  219. b->ptr[b->size+sz] = '\0';
  220. b->size+=sz+1;
  221. }
  222. static inline int
  223. reserve_length(struct bson *b) {
  224. int sz = b->size;
  225. bson_reserve(b,4);
  226. b->size +=4;
  227. return sz;
  228. }
  229. static inline void
  230. write_int64(struct bson *b, int64_t v) {
  231. uint64_t uv = (uint64_t)v;
  232. int i;
  233. bson_reserve(b,8);
  234. for (i=0;i<64;i+=8) {
  235. b->ptr[b->size++] = (uv>>i) & 0xff;
  236. }
  237. }
  238. static inline void
  239. write_double(struct bson *b, lua_Number d) {
  240. union {
  241. double d;
  242. uint64_t i;
  243. } v;
  244. v.d = d;
  245. int i;
  246. bson_reserve(b,8);
  247. for (i=0;i<64;i+=8) {
  248. b->ptr[b->size++] = (v.i>>i) & 0xff;
  249. }
  250. }
  251. static inline void
  252. append_key(struct bson *bs, lua_State *L, int type, const char *key, size_t sz) {
  253. write_byte(bs, type);
  254. write_string(bs, L, key, sz);
  255. }
  256. static inline int
  257. is_32bit(int64_t v) {
  258. return v >= INT32_MIN && v <= INT32_MAX;
  259. }
  260. static void
  261. append_number(struct bson *bs, lua_State *L, const char *key, size_t sz) {
  262. if (lua_isinteger(L, -1)) {
  263. int64_t i = lua_tointeger(L, -1);
  264. if (is_32bit(i)) {
  265. append_key(bs, L, BSON_INT32, key, sz);
  266. write_int32(bs, i);
  267. } else {
  268. append_key(bs, L, BSON_INT64, key, sz);
  269. write_int64(bs, i);
  270. }
  271. } else {
  272. lua_Number d = lua_tonumber(L,-1);
  273. append_key(bs, L, BSON_REAL, key, sz);
  274. write_double(bs, d);
  275. }
  276. }
  277. static void append_table(struct bson *bs, lua_State *L, const char *key, size_t sz, int depth);
  278. static void
  279. write_binary(struct bson *b, const void * buffer, size_t sz) {
  280. int length = reserve_length(b);
  281. bson_reserve(b,sz);
  282. memcpy(b->ptr + b->size, buffer, sz); // include sub type
  283. b->size+=sz;
  284. write_length(b, sz-1, length); // not include sub type
  285. }
  286. static void
  287. append_one(struct bson *bs, lua_State *L, const char *key, size_t sz, int depth) {
  288. int vt = lua_type(L,-1);
  289. switch(vt) {
  290. case LUA_TNUMBER:
  291. append_number(bs, L, key, sz);
  292. break;
  293. case LUA_TUSERDATA: {
  294. append_key(bs, L, BSON_DOCUMENT, key, sz);
  295. int32_t * doc = (int32_t*)lua_touserdata(L,-1);
  296. int32_t sz = *doc;
  297. bson_reserve(bs,sz);
  298. memcpy(bs->ptr + bs->size, doc, sz);
  299. bs->size += sz;
  300. break;
  301. }
  302. case LUA_TSTRING: {
  303. size_t len;
  304. const char * str = lua_tolstring(L,-1,&len);
  305. if (len > 1 && str[0]==0) {
  306. int subt = (uint8_t)str[1];
  307. append_key(bs, L, subt, key, sz);
  308. switch(subt) {
  309. case BSON_BINARY:
  310. write_binary(bs, str+2, len-2);
  311. break;
  312. case BSON_OBJECTID:
  313. if (len != 2+12) {
  314. luaL_error(L, "Invalid object id %s", str+2);
  315. }
  316. // go though
  317. case BSON_JSCODE:
  318. case BSON_DBPOINTER:
  319. case BSON_SYMBOL:
  320. case BSON_CODEWS:
  321. bson_reserve(bs,len-2);
  322. memcpy(bs->ptr + bs->size, str+2, len-2);
  323. bs->size += len-2;
  324. break;
  325. case BSON_DATE: {
  326. if (len != 2+4) {
  327. luaL_error(L, "Invalid date");
  328. }
  329. const uint32_t * ts = (const uint32_t *)(str + 2);
  330. int64_t v = (int64_t)*ts * 1000;
  331. write_int64(bs, v);
  332. break;
  333. }
  334. case BSON_TIMESTAMP: {
  335. if (len != 2+8) {
  336. luaL_error(L, "Invalid timestamp");
  337. }
  338. const uint32_t * inc = (const uint32_t *)(str + 2);
  339. const uint32_t * ts = (const uint32_t *)(str + 6);
  340. write_int32(bs, *inc);
  341. write_int32(bs, *ts);
  342. break;
  343. }
  344. case BSON_REGEX: {
  345. str+=2;
  346. len-=3;
  347. size_t i;
  348. for (i=0;i<len;i++) {
  349. if (str[len-i-1]==0) {
  350. break;
  351. }
  352. }
  353. write_string(bs, L, str, len-i-1);
  354. write_string(bs, L, str + len-i, i);
  355. break;
  356. }
  357. case BSON_MINKEY:
  358. case BSON_MAXKEY:
  359. case BSON_NULL:
  360. break;
  361. case BSON_INT64: {
  362. if (len != 2 + 8) {
  363. luaL_error(L, "Invalid int64");
  364. }
  365. const int64_t * v = (const int64_t *)(str + 2);
  366. write_int64(bs, *v);
  367. break;
  368. }
  369. default:
  370. luaL_error(L,"Invalid subtype %d", subt);
  371. }
  372. } else {
  373. size_t len;
  374. const char * str = lua_tolstring(L,-1,&len);
  375. append_key(bs, L, BSON_STRING, key, sz);
  376. int off = reserve_length(bs);
  377. write_string(bs, L, str, len);
  378. write_length(bs, len+1, off);
  379. }
  380. break;
  381. }
  382. case LUA_TTABLE:
  383. append_table(bs, L, key, sz, depth+1);
  384. break;
  385. case LUA_TBOOLEAN:
  386. append_key(bs, L, BSON_BOOLEAN, key, sz);
  387. write_byte(bs, lua_toboolean(L,-1));
  388. break;
  389. case LUA_TNIL:
  390. luaL_error(L, "Bson array has a hole (nil), Use bson.null instead");
  391. default:
  392. luaL_error(L, "Invalid value type : %s", lua_typename(L,vt));
  393. }
  394. }
  395. static inline int
  396. bson_numstr( char *str, unsigned int i ) {
  397. if ( i < MAX_NUMBER) {
  398. memcpy( str, bson_numstrs[i], 4 );
  399. return bson_numstr_len[i];
  400. } else {
  401. return sprintf( str,"%u", i );
  402. }
  403. }
  404. static void
  405. pack_array(lua_State *L, struct bson *b, int depth, size_t len) {
  406. int length = reserve_length(b);
  407. size_t i;
  408. for (i=1;i<=len;i++) {
  409. char numberkey[32];
  410. size_t sz = bson_numstr(numberkey, i - 1);
  411. const char * key = numberkey;
  412. lua_geti(L, -1, i);
  413. append_one(b, L, key, sz, depth);
  414. lua_pop(L, 1);
  415. }
  416. write_byte(b,0);
  417. write_length(b, b->size - length, length);
  418. }
  419. static void
  420. pack_dict_data(lua_State *L, struct bson *b, int depth, int kt) {
  421. const char * key = NULL;
  422. size_t sz;
  423. switch(kt) {
  424. case LUA_TNUMBER:
  425. luaL_error(L, "Bson dictionary's key can't be number");
  426. break;
  427. case LUA_TSTRING:
  428. key = lua_tolstring(L,-2,&sz);
  429. append_one(b, L, key, sz, depth);
  430. lua_pop(L,1);
  431. break;
  432. default:
  433. luaL_error(L, "Invalid key type : %s", lua_typename(L, kt));
  434. return;
  435. }
  436. }
  437. static void
  438. pack_simple_dict(lua_State *L, struct bson *b, int depth) {
  439. int length = reserve_length(b);
  440. lua_pushnil(L);
  441. while(lua_next(L,-2) != 0) {
  442. int kt = lua_type(L, -2);
  443. pack_dict_data(L, b, depth, kt);
  444. }
  445. write_byte(b,0);
  446. write_length(b, b->size - length, length);
  447. }
  448. static void
  449. pack_meta_dict(lua_State *L, struct bson *b, int depth) {
  450. int length = reserve_length(b);
  451. lua_pushvalue(L, -2); // push meta_obj
  452. lua_call(L, 1, 3); // call __pairs_func => next_func, t_data, first_k
  453. for(;;) {
  454. lua_pushvalue(L, -2); // copy data
  455. lua_pushvalue(L, -2); // copy k
  456. lua_copy(L, -5, -3); // copy next_func replace old_k
  457. lua_call(L, 2, 2); // call next_func
  458. int kt = lua_type(L, -2);
  459. if (kt == LUA_TNIL) {
  460. lua_pop(L, 4); // pop all k, v, next_func, obj
  461. break;
  462. }
  463. pack_dict_data(L, b, depth, kt);
  464. }
  465. write_byte(b,0);
  466. write_length(b, b->size - length, length);
  467. }
  468. static bool
  469. is_rawarray(lua_State *L) {
  470. lua_pushnil(L);
  471. if (lua_next(L, -2) == 0) {
  472. // empty table
  473. return false;
  474. }
  475. lua_Integer firstkey = lua_isinteger(L, -2) ? lua_tointeger(L, -2) : 0;
  476. lua_pop(L, 2);
  477. if (firstkey <= 1) {
  478. return firstkey > 0;
  479. }
  480. return firstkey <= lua_rawlen(L, -1);
  481. }
  482. static void
  483. append_table(struct bson *bs, lua_State *L, const char *key, size_t sz, int depth) {
  484. if (depth > MAX_DEPTH) {
  485. luaL_error(L, "Too depth while encoding bson");
  486. }
  487. luaL_checkstack(L, 16, NULL); // reserve enough stack space to pack table
  488. if (luaL_getmetafield(L, -1, "__len") != LUA_TNIL) {
  489. lua_pushvalue(L, -2);
  490. lua_call(L, 1, 1);
  491. if (!lua_isinteger(L, -1)) {
  492. luaL_error(L, "__len should return integer");
  493. }
  494. size_t len = lua_tointeger(L, -1);
  495. lua_pop(L, 1);
  496. append_key(bs, L, BSON_ARRAY, key, sz);
  497. pack_array(L, bs, depth, len);
  498. } else if (luaL_getmetafield(L, -1, "__pairs") != LUA_TNIL) {
  499. append_key(bs, L, BSON_DOCUMENT, key, sz);
  500. pack_meta_dict(L, bs, depth);
  501. } else if (is_rawarray(L)) {
  502. append_key(bs, L, BSON_ARRAY, key, sz);
  503. pack_array(L, bs, depth, lua_rawlen(L, -1));
  504. } else {
  505. append_key(bs, L, BSON_DOCUMENT, key, sz);
  506. pack_simple_dict(L, bs, depth);
  507. }
  508. }
  509. static void
  510. pack_ordered_dict(lua_State *L, struct bson *b, int n, int depth) {
  511. int length = reserve_length(b);
  512. int i;
  513. size_t sz;
  514. // the first key is at index n
  515. const char * key = lua_tolstring(L, n, &sz);
  516. for (i=0;i<n;i+=2) {
  517. if (key == NULL) {
  518. luaL_error(L, "Argument %d need a string", i+1);
  519. }
  520. lua_pushvalue(L, i+1);
  521. append_one(b, L, key, sz, depth);
  522. lua_pop(L,1);
  523. key = lua_tolstring(L, i+2, &sz); // next key
  524. }
  525. write_byte(b,0);
  526. write_length(b, b->size - length, length);
  527. }
  528. static int
  529. ltostring(lua_State *L) {
  530. size_t sz = lua_rawlen(L, 1);
  531. void * ud = lua_touserdata(L,1);
  532. lua_pushlstring(L, (const char*)ud, sz);
  533. return 1;
  534. }
  535. static int
  536. llen(lua_State *L) {
  537. size_t sz = lua_rawlen(L, 1);
  538. lua_pushinteger(L, sz);
  539. return 1;
  540. }
  541. static void
  542. make_object(lua_State *L, int type, const void * ptr, size_t len) {
  543. luaL_Buffer b;
  544. luaL_buffinit(L, &b);
  545. luaL_addchar(&b, 0);
  546. luaL_addchar(&b, type);
  547. luaL_addlstring(&b, (const char*)ptr, len);
  548. luaL_pushresult(&b);
  549. }
  550. static void
  551. unpack_dict(lua_State *L, struct bson_reader *br, bool array) {
  552. luaL_checkstack(L, 16, NULL); // reserve enough stack space to unpack table
  553. int sz = read_int32(L, br);
  554. const void * bytes = read_bytes(L, br, sz-5);
  555. struct bson_reader t = { (const uint8_t*)bytes, sz-5 };
  556. int end = read_byte(L, br);
  557. if (end != '\0') {
  558. luaL_error(L, "Invalid document end");
  559. }
  560. lua_newtable(L);
  561. for (;;) {
  562. if (t.size == 0)
  563. break;
  564. int bt = read_byte(L, &t);
  565. size_t klen = 0;
  566. const char * key = read_cstring(L, &t, &klen);
  567. if (array) {
  568. int id = strtol(key, NULL, 10) + 1;
  569. lua_pushinteger(L,id);
  570. } else {
  571. lua_pushlstring(L, key, klen);
  572. }
  573. switch (bt) {
  574. case BSON_REAL:
  575. lua_pushnumber(L, read_double(L, &t));
  576. break;
  577. case BSON_BOOLEAN:
  578. lua_pushboolean(L, read_byte(L, &t));
  579. break;
  580. case BSON_STRING: {
  581. int sz = read_int32(L, &t);
  582. if (sz <= 0) {
  583. luaL_error(L, "Invalid bson string , length = %d", sz);
  584. }
  585. lua_pushlstring(L, (const char*)read_bytes(L, &t, sz), sz-1);
  586. break;
  587. }
  588. case BSON_DOCUMENT:
  589. unpack_dict(L, &t, false);
  590. break;
  591. case BSON_ARRAY:
  592. unpack_dict(L, &t, true);
  593. break;
  594. case BSON_BINARY: {
  595. int sz = read_int32(L, &t);
  596. int subtype = read_byte(L, &t);
  597. luaL_Buffer b;
  598. luaL_buffinit(L, &b);
  599. luaL_addchar(&b, 0);
  600. luaL_addchar(&b, BSON_BINARY);
  601. luaL_addchar(&b, subtype);
  602. luaL_addlstring(&b, (const char*)read_bytes(L, &t, sz), sz);
  603. luaL_pushresult(&b);
  604. break;
  605. }
  606. case BSON_OBJECTID:
  607. make_object(L, BSON_OBJECTID, read_bytes(L, &t, 12), 12);
  608. break;
  609. case BSON_DATE: {
  610. int64_t date = read_int64(L, &t);
  611. uint32_t v = date / 1000;
  612. make_object(L, BSON_DATE, &v, 4);
  613. break;
  614. }
  615. case BSON_MINKEY:
  616. case BSON_MAXKEY:
  617. case BSON_NULL: {
  618. char key[] = { 0, (char)bt };
  619. lua_pushlstring(L, key, sizeof(key));
  620. break;
  621. }
  622. case BSON_REGEX: {
  623. size_t rlen1=0;
  624. size_t rlen2=0;
  625. const char * r1 = read_cstring(L, &t, &rlen1);
  626. const char * r2 = read_cstring(L, &t, &rlen2);
  627. luaL_Buffer b;
  628. luaL_buffinit(L, &b);
  629. luaL_addchar(&b, 0);
  630. luaL_addchar(&b, BSON_REGEX);
  631. luaL_addlstring(&b, r1, rlen1);
  632. luaL_addchar(&b,0);
  633. luaL_addlstring(&b, r2, rlen2);
  634. luaL_addchar(&b,0);
  635. luaL_pushresult(&b);
  636. break;
  637. }
  638. case BSON_INT32:
  639. lua_pushinteger(L, read_int32(L, &t));
  640. break;
  641. case BSON_TIMESTAMP: {
  642. int32_t inc = read_int32(L, &t);
  643. int32_t ts = read_int32(L, &t);
  644. luaL_Buffer b;
  645. luaL_buffinit(L, &b);
  646. luaL_addchar(&b, 0);
  647. luaL_addchar(&b, BSON_TIMESTAMP);
  648. luaL_addlstring(&b, (const char *)&inc, 4);
  649. luaL_addlstring(&b, (const char *)&ts, 4);
  650. luaL_pushresult(&b);
  651. break;
  652. }
  653. case BSON_INT64:
  654. lua_pushinteger(L, read_int64(L, &t));
  655. break;
  656. case BSON_DBPOINTER: {
  657. const void * ptr = t.ptr;
  658. int sz = read_int32(L, &t);
  659. read_bytes(L, &t, sz+12);
  660. make_object(L, BSON_DBPOINTER, ptr, sz + 16);
  661. break;
  662. }
  663. case BSON_JSCODE:
  664. case BSON_SYMBOL: {
  665. const void * ptr = t.ptr;
  666. int sz = read_int32(L, &t);
  667. read_bytes(L, &t, sz);
  668. make_object(L, bt, ptr, sz + 4);
  669. break;
  670. }
  671. case BSON_CODEWS: {
  672. const void * ptr = t.ptr;
  673. int sz = read_int32(L, &t);
  674. read_bytes(L, &t, sz-4);
  675. make_object(L, bt, ptr, sz);
  676. break;
  677. }
  678. default:
  679. // unsupported
  680. luaL_error(L, "Invalid bson type : %d", bt);
  681. lua_pop(L,1);
  682. continue;
  683. }
  684. lua_rawset(L,-3);
  685. }
  686. }
  687. static int
  688. lmakeindex(lua_State *L) {
  689. int32_t *bson = (int32_t*)luaL_checkudata(L,1,"bson");
  690. const uint8_t * start = (const uint8_t *)bson;
  691. struct bson_reader br = { start+4, get_length(start) - 5 };
  692. lua_newtable(L);
  693. for (;;) {
  694. if (br.size == 0)
  695. break;
  696. int bt = read_byte(L, &br);
  697. size_t klen = 0;
  698. const char * key = read_cstring(L, &br, &klen);
  699. int field_size = 0;
  700. switch (bt) {
  701. case BSON_INT64:
  702. case BSON_TIMESTAMP:
  703. case BSON_DATE:
  704. case BSON_REAL:
  705. field_size = 8;
  706. break;
  707. case BSON_BOOLEAN:
  708. field_size = 1;
  709. break;
  710. case BSON_JSCODE:
  711. case BSON_SYMBOL:
  712. case BSON_STRING: {
  713. int sz = read_int32(L, &br);
  714. read_bytes(L, &br, sz);
  715. break;
  716. }
  717. case BSON_CODEWS:
  718. case BSON_ARRAY:
  719. case BSON_DOCUMENT: {
  720. int sz = read_int32(L, &br);
  721. read_bytes(L, &br, sz-4);
  722. break;
  723. }
  724. case BSON_BINARY: {
  725. int sz = read_int32(L, &br);
  726. read_bytes(L, &br, sz+1);
  727. break;
  728. }
  729. case BSON_OBJECTID:
  730. field_size = 12;
  731. break;
  732. case BSON_MINKEY:
  733. case BSON_MAXKEY:
  734. case BSON_NULL:
  735. break;
  736. case BSON_REGEX: {
  737. size_t rlen1=0;
  738. size_t rlen2=0;
  739. read_cstring(L, &br, &rlen1);
  740. read_cstring(L, &br, &rlen2);
  741. break;
  742. }
  743. case BSON_INT32:
  744. field_size = 4;
  745. break;
  746. case BSON_DBPOINTER: {
  747. int sz = read_int32(L, &br);
  748. read_bytes(L, &br, sz+12);
  749. break;
  750. }
  751. default:
  752. // unsupported
  753. luaL_error(L, "Invalid bson type : %d", bt);
  754. lua_pop(L,1);
  755. continue;
  756. }
  757. if (field_size > 0) {
  758. int id = bt | (int)(br.ptr - start) << BSON_TYPE_SHIFT;
  759. read_bytes(L, &br, field_size);
  760. lua_pushlstring(L, key, klen);
  761. lua_pushinteger(L,id);
  762. lua_rawset(L,-3);
  763. }
  764. }
  765. lua_setiuservalue(L,1,1);
  766. lua_settop(L,1);
  767. return 1;
  768. }
  769. static void
  770. replace_object(lua_State *L, int type, struct bson * bs) {
  771. size_t len = 0;
  772. const char * data = luaL_checklstring(L,3, &len);
  773. if (len < 6 || data[0] != 0 || data[1] != type) {
  774. luaL_error(L, "Type mismatch, need bson type %d", type);
  775. }
  776. switch (type) {
  777. case BSON_OBJECTID:
  778. if (len != 2+12) {
  779. luaL_error(L, "Invalid object id");
  780. }
  781. memcpy(bs->ptr, data+2, 12);
  782. break;
  783. case BSON_DATE: {
  784. if (len != 2+4) {
  785. luaL_error(L, "Invalid date");
  786. }
  787. const uint32_t * ts = (const uint32_t *)(data + 2);
  788. int64_t v = (int64_t)*ts * 1000;
  789. write_int64(bs, v);
  790. break;
  791. }
  792. case BSON_TIMESTAMP: {
  793. if (len != 2+8) {
  794. luaL_error(L, "Invalid timestamp");
  795. }
  796. const uint32_t * inc = (const uint32_t *)(data + 2);
  797. const uint32_t * ts = (const uint32_t *)(data + 6);
  798. write_int32(bs, *inc);
  799. write_int32(bs, *ts);
  800. break;
  801. }
  802. }
  803. }
  804. static int
  805. lreplace(lua_State *L) {
  806. lua_getiuservalue(L,1,1);
  807. if (!lua_istable(L,-1)) {
  808. return luaL_error(L, "call makeindex first");
  809. }
  810. lua_pushvalue(L,2);
  811. if (lua_rawget(L, -2) != LUA_TNUMBER) {
  812. return luaL_error(L, "Can't replace key : %s", lua_tostring(L,2));
  813. }
  814. int id = lua_tointeger(L, -1);
  815. int type = id & ((1<<(BSON_TYPE_SHIFT)) - 1);
  816. int offset = id >> BSON_TYPE_SHIFT;
  817. uint8_t * start = (uint8_t*)lua_touserdata(L,1);
  818. struct bson b = { 0,16, start + offset };
  819. switch (type) {
  820. case BSON_REAL:
  821. write_double(&b, luaL_checknumber(L, 3));
  822. break;
  823. case BSON_BOOLEAN:
  824. write_byte(&b, lua_toboolean(L,3));
  825. break;
  826. case BSON_OBJECTID:
  827. case BSON_DATE:
  828. case BSON_TIMESTAMP:
  829. replace_object(L, type, &b);
  830. break;
  831. case BSON_INT32: {
  832. if (!lua_isinteger(L, 3)) {
  833. luaL_error(L, "%f must be a 32bit integer ", lua_tonumber(L, 3));
  834. }
  835. int32_t i = lua_tointeger(L,3);
  836. write_int32(&b, i);
  837. break;
  838. }
  839. case BSON_INT64: {
  840. if (!lua_isinteger(L, 3)) {
  841. luaL_error(L, "%f must be a 64bit integer ", lua_tonumber(L, 3));
  842. }
  843. int64_t i = lua_tointeger(L,3);
  844. write_int64(&b, i);
  845. break;
  846. }
  847. default:
  848. luaL_error(L, "Can't replace type %d", type);
  849. break;
  850. }
  851. return 0;
  852. }
  853. static int
  854. ldecode(lua_State *L) {
  855. const int32_t * data = (const int32_t*)lua_touserdata(L,1);
  856. if (data == NULL) {
  857. return 0;
  858. }
  859. const uint8_t * b = (const uint8_t *)data;
  860. int32_t len = get_length(b);
  861. struct bson_reader br = { b , len };
  862. unpack_dict(L, &br, false);
  863. return 1;
  864. }
  865. static void
  866. bson_meta(lua_State *L) {
  867. if (luaL_newmetatable(L, "bson")) {
  868. luaL_Reg l[] = {
  869. { "decode", ldecode },
  870. { "makeindex", lmakeindex },
  871. { NULL, NULL },
  872. };
  873. luaL_newlib(L,l);
  874. lua_setfield(L, -2, "__index");
  875. lua_pushcfunction(L, ltostring);
  876. lua_setfield(L, -2, "__tostring");
  877. lua_pushcfunction(L, llen);
  878. lua_setfield(L, -2, "__len");
  879. lua_pushcfunction(L, lreplace);
  880. lua_setfield(L, -2, "__newindex");
  881. }
  882. lua_setmetatable(L, -2);
  883. }
  884. static int
  885. encode_bson(lua_State *L) {
  886. struct bson *b = (struct bson*)lua_touserdata(L, 2);
  887. lua_settop(L, 1);
  888. if (luaL_getmetafield(L, -1, "__pairs") != LUA_TNIL) {
  889. pack_meta_dict(L, b, 0);
  890. } else {
  891. pack_simple_dict(L, b, 0);
  892. }
  893. void * ud = lua_newuserdatauv(L, b->size, 1);
  894. memcpy(ud, b->ptr, b->size);
  895. return 1;
  896. }
  897. static int
  898. lencode(lua_State *L) {
  899. struct bson b;
  900. lua_settop(L,1);
  901. luaL_checktype(L, 1, LUA_TTABLE);
  902. bson_create(&b);
  903. lua_pushcfunction(L, encode_bson);
  904. lua_pushvalue(L, 1);
  905. lua_pushlightuserdata(L, &b);
  906. if (lua_pcall(L, 2, 1, 0) != LUA_OK) {
  907. bson_destroy(&b);
  908. return lua_error(L);
  909. }
  910. bson_destroy(&b);
  911. bson_meta(L);
  912. return 1;
  913. }
  914. static int
  915. encode_bson_byorder(lua_State *L) {
  916. int n = lua_gettop(L);
  917. struct bson *b = (struct bson*)lua_touserdata(L, n);
  918. lua_settop(L, --n);
  919. pack_ordered_dict(L, b, n, 0);
  920. lua_settop(L,0);
  921. void * ud = lua_newuserdatauv(L, b->size, 1);
  922. memcpy(ud, b->ptr, b->size);
  923. return 1;
  924. }
  925. static int
  926. lencode_order(lua_State *L) {
  927. struct bson b;
  928. int n = lua_gettop(L);
  929. if (n%2 != 0) {
  930. return luaL_error(L, "Invalid ordered dict");
  931. }
  932. bson_create(&b);
  933. lua_pushvalue(L, 1); // copy the first arg to n
  934. lua_pushcfunction(L, encode_bson_byorder);
  935. lua_replace(L, 1);
  936. lua_pushlightuserdata(L, &b);
  937. if (lua_pcall(L, n+1, 1, 0) != LUA_OK) {
  938. bson_destroy(&b);
  939. return lua_error(L);
  940. }
  941. bson_destroy(&b);
  942. bson_meta(L);
  943. return 1;
  944. }
  945. static int
  946. ldate(lua_State *L) {
  947. int d = luaL_checkinteger(L,1);
  948. luaL_Buffer b;
  949. luaL_buffinit(L, &b);
  950. luaL_addchar(&b, 0);
  951. luaL_addchar(&b, BSON_DATE);
  952. luaL_addlstring(&b, (const char *)&d, sizeof(d));
  953. luaL_pushresult(&b);
  954. return 1;
  955. }
  956. static int
  957. lint64(lua_State *L) {
  958. int64_t d = luaL_checkinteger(L, 1);
  959. luaL_Buffer b;
  960. luaL_buffinit(L, &b);
  961. luaL_addchar(&b, 0);
  962. luaL_addchar(&b, BSON_INT64);
  963. luaL_addlstring(&b, (const char *)&d, sizeof(d));
  964. luaL_pushresult(&b);
  965. return 1;
  966. }
  967. static int
  968. ltimestamp(lua_State *L) {
  969. int d = luaL_checkinteger(L,1);
  970. luaL_Buffer b;
  971. luaL_buffinit(L, &b);
  972. luaL_addchar(&b, 0);
  973. luaL_addchar(&b, BSON_TIMESTAMP);
  974. if (lua_isnoneornil(L,2)) {
  975. static uint32_t inc = 0;
  976. luaL_addlstring(&b, (const char *)&inc, sizeof(inc));
  977. ++inc;
  978. } else {
  979. uint32_t i = (uint32_t)lua_tointeger(L,2);
  980. luaL_addlstring(&b, (const char *)&i, sizeof(i));
  981. }
  982. luaL_addlstring(&b, (const char *)&d, sizeof(d));
  983. luaL_pushresult(&b);
  984. return 1;
  985. }
  986. static int
  987. lregex(lua_State *L) {
  988. luaL_checkstring(L,1);
  989. if (lua_gettop(L) < 2) {
  990. lua_pushliteral(L,"");
  991. }
  992. luaL_Buffer b;
  993. luaL_buffinit(L, &b);
  994. luaL_addchar(&b, 0);
  995. luaL_addchar(&b, BSON_REGEX);
  996. lua_pushvalue(L,1);
  997. luaL_addvalue(&b);
  998. luaL_addchar(&b,0);
  999. lua_pushvalue(L,2);
  1000. luaL_addvalue(&b);
  1001. luaL_addchar(&b,0);
  1002. luaL_pushresult(&b);
  1003. return 1;
  1004. }
  1005. static int
  1006. lbinary(lua_State *L) {
  1007. lua_settop(L,1);
  1008. luaL_Buffer b;
  1009. luaL_buffinit(L, &b);
  1010. luaL_addchar(&b, 0);
  1011. luaL_addchar(&b, BSON_BINARY);
  1012. luaL_addchar(&b, 0); // sub type
  1013. lua_pushvalue(L,1);
  1014. luaL_addvalue(&b);
  1015. luaL_pushresult(&b);
  1016. return 1;
  1017. }
  1018. static int
  1019. lsubtype(lua_State *L, int subtype, const uint8_t * buf, size_t sz) {
  1020. switch(subtype) {
  1021. case BSON_BINARY:
  1022. lua_pushvalue(L, lua_upvalueindex(6));
  1023. lua_pushlstring(L, (const char *)buf+1, sz-1);
  1024. lua_pushinteger(L, buf[0]);
  1025. return 3;
  1026. case BSON_OBJECTID: {
  1027. if (sz != 12) {
  1028. return luaL_error(L, "Invalid object id");
  1029. }
  1030. char oid[24];
  1031. int i;
  1032. const uint8_t * id = buf;
  1033. static const char *hex = "0123456789abcdef";
  1034. for (i=0;i<12;i++) {
  1035. oid[i*2] = hex[id[i] >> 4];
  1036. oid[i*2+1] = hex[id[i] & 0xf];
  1037. }
  1038. lua_pushvalue(L, lua_upvalueindex(7));
  1039. lua_pushlstring(L, oid, 24);
  1040. return 2;
  1041. }
  1042. case BSON_DATE: {
  1043. if (sz != 4) {
  1044. return luaL_error(L, "Invalid date");
  1045. }
  1046. int d = *(const int *)buf;
  1047. lua_pushvalue(L, lua_upvalueindex(9));
  1048. lua_pushinteger(L, d);
  1049. return 2;
  1050. }
  1051. case BSON_TIMESTAMP: {
  1052. if (sz != 8) {
  1053. return luaL_error(L, "Invalid timestamp");
  1054. }
  1055. const uint32_t * ts = (const uint32_t *)buf;
  1056. lua_pushvalue(L, lua_upvalueindex(8));
  1057. lua_pushinteger(L, (lua_Integer)ts[1]);
  1058. lua_pushinteger(L, (lua_Integer)ts[0]);
  1059. return 3;
  1060. }
  1061. case BSON_REGEX: {
  1062. --sz;
  1063. size_t i;
  1064. const uint8_t *str = buf;
  1065. for (i=0;i<sz;i++) {
  1066. if (str[sz-i-1]==0) {
  1067. break;
  1068. }
  1069. }
  1070. lua_pushvalue(L, lua_upvalueindex(10));
  1071. if (i==sz) {
  1072. return luaL_error(L, "Invalid regex");
  1073. }
  1074. lua_pushlstring(L, (const char *)str, sz - i - 1);
  1075. lua_pushlstring(L, (const char *)str+sz-i, i);
  1076. return 3;
  1077. }
  1078. case BSON_MINKEY:
  1079. lua_pushvalue(L, lua_upvalueindex(11));
  1080. return 1;
  1081. case BSON_MAXKEY:
  1082. lua_pushvalue(L, lua_upvalueindex(12));
  1083. return 1;
  1084. case BSON_NULL:
  1085. lua_pushvalue(L, lua_upvalueindex(4));
  1086. return 1;
  1087. case BSON_JSCODE:
  1088. case BSON_DBPOINTER:
  1089. case BSON_SYMBOL:
  1090. case BSON_CODEWS:
  1091. lua_pushvalue(L, lua_upvalueindex(14));
  1092. lua_pushlstring(L, (const char *)buf, sz);
  1093. return 2;
  1094. case BSON_INT64: {
  1095. if (sz != 8) {
  1096. return luaL_error(L, "Invalid int64");
  1097. }
  1098. int64_t d = *(const int64_t *)buf;
  1099. lua_pushvalue(L, lua_upvalueindex(13));
  1100. lua_pushinteger(L, d);
  1101. return 2;
  1102. }
  1103. default:
  1104. return luaL_error(L, "Invalid subtype %d", subtype);
  1105. }
  1106. }
  1107. static int
  1108. ltype(lua_State *L) {
  1109. int t = lua_type(L,1);
  1110. int type = 0;
  1111. switch (t) {
  1112. case LUA_TNUMBER:
  1113. type = 1;
  1114. break;
  1115. case LUA_TBOOLEAN:
  1116. type = 2;
  1117. break;
  1118. case LUA_TTABLE:
  1119. type = 3;
  1120. break;
  1121. case LUA_TNIL:
  1122. lua_pushvalue(L, lua_upvalueindex(4));
  1123. return 1;
  1124. case LUA_TSTRING: {
  1125. size_t len = 0;
  1126. const char * str = lua_tolstring(L,1,&len);
  1127. if (str[0] == 0 && len >= 2) {
  1128. return lsubtype(L, (uint8_t)str[1], (const uint8_t *)str+2, len-2);
  1129. } else {
  1130. type = 5;
  1131. break;
  1132. }
  1133. }
  1134. default:
  1135. return luaL_error(L, "Invalid type %s",lua_typename(L,t));
  1136. }
  1137. lua_pushvalue(L, lua_upvalueindex(type));
  1138. lua_pushvalue(L,1);
  1139. return 2;
  1140. }
  1141. static void
  1142. typeclosure(lua_State *L) {
  1143. static const char * typename_[] = {
  1144. "number", // 1
  1145. "boolean", // 2
  1146. "table", // 3
  1147. "nil", // 4
  1148. "string", // 5
  1149. "binary", // 6
  1150. "objectid", // 7
  1151. "timestamp", // 8
  1152. "date", // 9
  1153. "regex", // 10
  1154. "minkey", // 11
  1155. "maxkey", // 12
  1156. "int64", // 13
  1157. "unsupported", // 14
  1158. };
  1159. int i;
  1160. int n = sizeof(typename_)/sizeof(typename_[0]);
  1161. for (i=0;i<n;i++) {
  1162. lua_pushstring(L, typename_[i]);
  1163. }
  1164. lua_pushcclosure(L, ltype, n);
  1165. }
  1166. static uint8_t oid_header[5];
  1167. static ATOM_ULONG oid_counter;
  1168. static void
  1169. init_oid_header() {
  1170. if (ATOM_LOAD(&oid_counter)) {
  1171. // already init
  1172. return;
  1173. }
  1174. pid_t pid = getpid();
  1175. uint32_t h = 0;
  1176. char hostname[256];
  1177. if (gethostname(hostname, sizeof(hostname))==0) {
  1178. int i;
  1179. for (i=0;i<sizeof(hostname) && hostname[i];i++) {
  1180. h = h ^ ((h<<5)+(h>>2)+hostname[i]);
  1181. }
  1182. h ^= i;
  1183. }
  1184. oid_header[0] = h & 0xff;
  1185. oid_header[1] = (h>>8) & 0xff;
  1186. oid_header[2] = (h>>16) & 0xff;
  1187. oid_header[3] = pid & 0xff;
  1188. oid_header[4] = (pid >> 8) & 0xff;
  1189. unsigned long c = h ^ time(NULL) ^ (uintptr_t)&h;
  1190. if (c == 0) {
  1191. c = 1;
  1192. }
  1193. ATOM_STORE(&oid_counter, c);
  1194. }
  1195. static inline int
  1196. hextoint(char c) {
  1197. if (c>='0' && c<='9')
  1198. return c-'0';
  1199. if (c>='a' && c<='z')
  1200. return c-'a'+10;
  1201. if (c>='A' && c<='Z')
  1202. return c-'A'+10;
  1203. return 0;
  1204. }
  1205. static int
  1206. lobjectid(lua_State *L) {
  1207. uint8_t oid[14] = { 0, BSON_OBJECTID };
  1208. if (lua_isstring(L,1)) {
  1209. size_t len;
  1210. const char * str = lua_tolstring(L,1,&len);
  1211. if (len != 24) {
  1212. return luaL_error(L, "Invalid objectid %s", str);
  1213. }
  1214. int i;
  1215. for (i=0;i<12;i++) {
  1216. oid[i+2] = hextoint(str[i*2]) << 4 | hextoint(str[i*2+1]);
  1217. }
  1218. } else {
  1219. time_t ti = time(NULL);
  1220. // old_counter is a static var, use atom inc.
  1221. uint32_t id = ATOM_FINC(&oid_counter);
  1222. oid[2] = (ti>>24) & 0xff;
  1223. oid[3] = (ti>>16) & 0xff;
  1224. oid[4] = (ti>>8) & 0xff;
  1225. oid[5] = ti & 0xff;
  1226. memcpy(oid+6 , oid_header, 5);
  1227. oid[11] = (id>>16) & 0xff;
  1228. oid[12] = (id>>8) & 0xff;
  1229. oid[13] = id & 0xff;
  1230. }
  1231. lua_pushlstring( L, (const char *)oid, 14);
  1232. return 1;
  1233. }
  1234. LUAMOD_API int
  1235. luaopen_bson(lua_State *L) {
  1236. luaL_checkversion(L);
  1237. int i;
  1238. for (i=0;i<MAX_NUMBER;i++) {
  1239. char tmp[8];
  1240. bson_numstr_len[i] = sprintf(tmp,"%d",i);
  1241. memcpy(bson_numstrs[i], tmp, bson_numstr_len[i]);
  1242. }
  1243. luaL_Reg l[] = {
  1244. { "encode", lencode },
  1245. { "encode_order", lencode_order },
  1246. { "date", ldate },
  1247. { "timestamp", ltimestamp },
  1248. { "regex", lregex },
  1249. { "binary", lbinary },
  1250. { "objectid", lobjectid },
  1251. { "int64", lint64 },
  1252. { "decode", ldecode },
  1253. { NULL, NULL },
  1254. };
  1255. luaL_newlib(L,l);
  1256. typeclosure(L);
  1257. lua_setfield(L,-2,"type");
  1258. char null[] = { 0, BSON_NULL };
  1259. lua_pushlstring(L, null, sizeof(null));
  1260. lua_setfield(L,-2,"null");
  1261. char minkey[] = { 0, (char)BSON_MINKEY };
  1262. lua_pushlstring(L, minkey, sizeof(minkey));
  1263. lua_setfield(L,-2,"minkey");
  1264. char maxkey[] = { 0, BSON_MAXKEY };
  1265. lua_pushlstring(L, maxkey, sizeof(maxkey));
  1266. lua_setfield(L,-2,"maxkey");
  1267. init_oid_header();
  1268. return 1;
  1269. }