/* ** Escapes a string for use within an SQL statement. ** Returns a string with the escaped string. */ static int conn_escape (lua_State *L) { conn_data *conn = getconnection (L); size_t len; const char *from = luaL_checklstring (L, 2, &len); int error; int ret = 1; luaL_Buffer b; #if defined(luaL_buffinitsize) char *to = luaL_buffinitsize (L, &b, 2*len+1); #else char *to; luaL_buffinit (L, &b); to = luaL_prepbuffer (&b); #endif len = PQescapeStringConn (conn->pg_conn, to, from, len, &error); if (error == 0) { /* success ! */ #if defined(luaL_pushresultsize) luaL_pushresultsize (&b, len); #else luaL_addsize (&b, len); luaL_pushresult (&b); #endif } else { ret = luasql_failmsg (L, "cannot escape string. PostgreSQL: ", PQerrorMessage (conn->pg_conn)); } return ret; }
static int l_transliterate(lua_State *L) { size_t len; int i; const char *str = luaL_checklstring(L, 1, &len); luaL_checktype(L, 2, LUA_TTABLE); char key[2] = { 0, 0 }; luaL_Buffer buf; char *p = luaL_buffinitsize(L, &buf, len); const char * const buf_start = p; for (i = 0; i < len; i++) { key[0] = str[i]; lua_getfield(L, 2, key); if (!lua_isnil(L, -1)) { if (lua_isboolean(L, -1)) { int b = lua_toboolean(L, -1); if (b == 0) /* false */ { } else { luaL_error(L, "Error: expected false as boolean value in transliterate table"); } } else if (lua_isstring(L, -1)) { const char *v = lua_tostring(L, -1); *p++ = *v; } else { luaL_error(L, "Error: expected boolean or string values in transliterate table"); } } else { *p++ = str[i]; } lua_pop(L, 1); } luaL_pushresultsize(&buf, p - buf_start); return 1; }
static int fs_getCurrentPath(lua_State* L) { luaL_Buffer currentpath; size_t len = minfs_current_working_directory_len(); luaL_buffinitsize(L, ¤tpath, len); len = minfs_current_working_directory(currentpath.b, len); luaL_pushresultsize(¤tpath, len); return 1; }
static int str_reverse (lua_State *L) { size_t l, i; luaL_Buffer b; const char *s = luaL_checklstring(L, 1, &l); char *p = luaL_buffinitsize(L, &b, l); for (i = 0; i < l; i++) p[i] = s[l - i - 1]; luaL_pushresultsize(&b, l); return 1; }
static int fs_canonical(lua_State* L) { const char* filepath = luaL_checkstring(L, -1); luaL_Buffer canonpath; size_t len = 1024; luaL_buffinitsize(L, &canonpath, len); len = minfs_canonical_path(filepath, canonpath.b, len); luaL_pushresultsize(&canonpath, len); return 1; }
static int str_upper (lua_State *L) { size_t l; size_t i; luaL_Buffer b; const char *s = luaL_checklstring(L, 1, &l); char *p = luaL_buffinitsize(L, &b, l); for (i=0; i<l; i++) p[i] = toupper(uchar(s[i])); luaL_pushresultsize(&b, l); return 1; }
static int _common_encode(lua_State *L, EncodeFunc_t encode) { size_t len; const char *str = lua64_tolstring(L, 1, &len); size_t dlen = base64_encode_length(len); luaL_Buffer b; char *dst = luaL_buffinitsize(L, &b, dlen); encode(str, len, dst); luaL_pushresultsize(&b, dlen); return 1; }
static int _scheme_encode(lua_State *L) { struct base64_scheme *sch = lua64_touserdata(L, 1); size_t len; const char *str = lua64_tolstring(L, 2, &len); size_t dlen = base64_encode_length(len); luaL_Buffer b; char *dst = luaL_buffinitsize(L, &b, dlen); base64_scheme_encode(sch, str, len, dst); luaL_pushresultsize(&b, dlen); return 1; }
static int str_char (lua_State *L) { int n = lua_gettop(L); /* number of arguments */ int i; luaL_Buffer b; char *p = luaL_buffinitsize(L, &b, n); for (i=1; i<=n; i++) { int c = luaL_checkint(L, i); luaL_argcheck(L, uchar(c) == c, i, "value out of range"); p[i - 1] = uchar(c); } luaL_pushresultsize(&b, n); return 1; }
static int fs_pathRoot(lua_State* L) { size_t len, newlen; const char* filepath = luaL_checklstring(L, 1, &len); luaL_Buffer buffer; luaL_buffinitsize(L, &buffer, len); newlen = minfs_path_parent(filepath, buffer.b, len); if (FS_FAILED(newlen)) { lua_pushnil(L); } else { luaL_pushresultsize(&buffer, newlen); } return 1; }
/* return the escaped string and the error message */ static int l_mysqlclient_escape(lua_State* l) { MYSQL* conn; const char* content; char* buf; luaL_Buffer lbuf; unsigned long len; conn = luaL_testudata(l, 1, MySQLLib); if (!conn) { lua_pushnil(l); lua_pushstring(l, "argument #1 is not a mysql client."); return 2; } if (!lua_isstring(l, 2)) { int type = lua_type(l, 2); lua_pushnil(l); lua_pushfstring(l, "argument #2 expects a sql string, but given a %s.", lua_typename(l, type)); return 2; } content = lua_tolstring(l, 2, &len); if (len == 0) { lua_pushstring(l, ""); lua_pushnil(l); return 2; } buf = luaL_buffinitsize(l, &lbuf, len * 2 + 1); if (!buf) { lua_pushnil(l); lua_pushstring(l, "allocating buffer failed."); return 2; } len = mysql_real_escape_string(conn, buf, content, len); if (len == (unsigned long)(-1)) { lua_pushnil(l); luaL_addstring(&lbuf, mysql_error(conn)); luaL_pushresult(&lbuf); } else { luaL_pushresultsize(&lbuf, len); lua_pushnil(l); } return 2; }
static int _common_decode(lua_State *L, DecodeFunc_t decode) { size_t len; const char *str = lua64_tolstring(L, 1, &len); size_t tlen = base64_decode_length(len); luaL_Buffer b; char *dst = luaL_buffinitsize(L, &b, tlen); size_t dlen; if (decode(str, len, dst, &dlen) < 0) { lua_pushnil(L); } else { luaL_pushresultsize(&b, dlen); } lua_pushnil(L); return 2; }
static int _scheme_decode(lua_State *L) { struct base64_scheme *sch = lua64_touserdata(L, 1); size_t len; const char *str = lua64_tolstring(L, 2, &len); size_t tlen = base64_decode_length(len); luaL_Buffer b; char *dst = luaL_buffinitsize(L, &b, tlen); size_t dlen; if (base64_scheme_decode(sch, str, len, dst, &dlen) < 0) { lua_pushnil(L); } else { luaL_pushresultsize(&b, dlen); } lua_pushnil(L); return 2; }
static int fs_pathWithoutExt(lua_State* L) { size_t len, newlen; const char* filepath = luaL_checklstring(L, 1, &len); luaL_Buffer buffer; luaL_buffinitsize(L, &buffer, len); if (!minfs_is_file(filepath)) { lua_pushnil(L); return 1; } newlen = minfs_path_without_ext(filepath, buffer.b, len); if (FS_FAILED(newlen)) { lua_pushnil(L); } else { luaL_pushresultsize(&buffer, newlen); } return 1; }
static int str_rep (lua_State *L) { size_t l, lsep; const char *s = luaL_checklstring(L, 1, &l); int n = luaL_checkint(L, 2); const char *sep = luaL_optlstring(L, 3, "", &lsep); if (n <= 0) lua_pushliteral(L, ""); else if (l + lsep < l || l + lsep >= MAXSIZE / n) /* may overflow? */ return luaL_error(L, "resulting string too large"); else { size_t totallen = n * l + (n - 1) * lsep; luaL_Buffer b; char *p = luaL_buffinitsize(L, &b, totallen); while (n-- > 1) { /* first n-1 copies (followed by separator) */ memcpy(p, s, l * sizeof(char)); p += l; memcpy(p, sep, lsep * sizeof(char)); p += lsep; } memcpy(p, s, l * sizeof(char)); /* last copy (not followed by separator) */ luaL_pushresultsize(&b, totallen); } return 1; }
static int l_transliterate(lua_State *L) { int i, ssize, rsize = 0; luaL_Buffer buffer; char *buf; const char *s = luaL_checklstring(L, 1, &ssize); lua_pushvalue(L, lua_upvalueindex(1)); lua_getfield(L, 2, TABLE_INDEX); luaL_checktype(L, 3, LUA_TTABLE); buf = luaL_buffinitsize(L, &buffer, ssize); for (i = 0; i < ssize; ++i) { char chr = s[i]; lua_pushnil(L); while (lua_next(L, 3) != 0) { luaL_checktype(L, -2, LUA_TSTRING); const char *key = lua_tostring(L, -2); if (*key == chr) { if (lua_isstring(L, -1)) chr = *lua_tostring(L, -1); else if (!lua_toboolean(L, -1)) chr = 0; lua_pop(L, 1); break; } lua_pop(L, 1); } if (chr != 0) buf[rsize++] = chr; if (lua_gettop(L) > 3) lua_pop(L, 1); } luaL_pushresultsize(&buffer, rsize); return 1; }