Skip to content

Commit

Permalink
bson功能扩展
Browse files Browse the repository at this point in the history
  • Loading branch information
xiyoo0812 committed May 11, 2024
1 parent da2fd4b commit 44db378
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 25 deletions.
35 changes: 14 additions & 21 deletions extend/lbson/src/bson.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ namespace lbson {
const uint32_t OP_CHECKSUM = 1 << 0;
const uint32_t OP_MORE_COME = 1 << 1;


static char hex[] = "0123456789abcdef";
static char bson_numstrs[max_bson_index][4];
static int bson_numstr_len[max_bson_index];

Expand Down Expand Up @@ -171,17 +173,6 @@ namespace lbson {
return 1;
}

int tohex(lua_State* L, const char* text, size_t sz) {
static char hex[] = "0123456789abcdef";
char buffer[UCHAR_MAX];
for (size_t i = 0; i < sz; i++) {
buffer[i * 2] = hex[text[i] >> 4];
buffer[i * 2 + 1] = hex[text[i] & 0xf];
}
lua_pushlstring(L, buffer, sz * 2);
return 1;
}

size_t bson_index(char* str, size_t i) {
if (i < max_bson_index) {
memcpy(str, bson_numstrs[i], 4);
Expand Down Expand Up @@ -256,6 +247,16 @@ namespace lbson {
return *value;
}

void read_objectid(lua_State* L, slice* slice) {
char buffer[32];
const char* text = read_bytes(L, slice, 12);
for (size_t i = 0; i < 12; i++) {
buffer[i * 2] = hex[text[i] >> 4];
buffer[i * 2 + 1] = hex[text[i] & 0xf];
}
lua_pushlstring(L, buffer, 24);
}

void write_number(lua_State *L, const char* key, size_t len) {
if (lua_isinteger(L, -1)) {
int64_t v = lua_tointeger(L, -1);
Expand Down Expand Up @@ -497,16 +498,8 @@ namespace lbson {
case bson_type::BSON_TIMESTAMP:
lua_pushinteger(L, read_val<int64_t>(L, slice));
break;
case bson_type::BSON_OBJECTID:{
const char* s = read_bytes(L, slice, 12);
lua_createtable(L, 0, 4);
lua_pushinteger(L, (uint32_t)bt);
lua_setfield(L, -2, "__type");
lua_pushlstring(L, s, 12);
lua_setfield(L, -2, "value");
tohex(L, s, 12);
lua_setfield(L, -2, "id");
}
case bson_type::BSON_OBJECTID:
read_objectid(L, slice);
break;
case bson_type::BSON_JSCODE:
case bson_type::BSON_STRING:{
Expand Down
8 changes: 4 additions & 4 deletions extend/lssl/src/ssl/lssl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,15 @@ namespace lssl {
}

static int lrandomkey(lua_State* L) {
char tmp[8];
char tmp[12];
int i;
for (i = 0; i < 8; i++) {
for (i = 0; i < 12; i++) {
tmp[i] = rand() & 0xff;
}
if (luaL_optinteger(L, 1, 0)) {
return tohex(L, (const unsigned char*)tmp, 8);
return tohex(L, (const unsigned char*)tmp, 12);
}
lua_pushlstring(L, tmp, 8);
lua_pushlstring(L, tmp, 12);
return 1;
}

Expand Down

0 comments on commit 44db378

Please sign in to comment.