diff options
-rw-r--r-- | includes/StacklessTask.h | 6 | ||||
-rw-r--r-- | lcrypt/lcrypt.c | 44 | ||||
-rw-r--r-- | lcrypt/lcrypt_bits.c | 4 | ||||
-rw-r--r-- | lcrypt/lcrypt_ciphers.c | 38 | ||||
-rw-r--r-- | lcrypt/lcrypt_hashes.c | 8 | ||||
-rw-r--r-- | src/BLua.cc | 31 | ||||
-rw-r--r-- | src/LuaHandle.cc | 3 | ||||
-rw-r--r-- | src/LuaTask.cc | 4 |
8 files changed, 103 insertions, 35 deletions
diff --git a/includes/StacklessTask.h b/includes/StacklessTask.h index 9ab6b6b..3c86c0e 100644 --- a/includes/StacklessTask.h +++ b/includes/StacklessTask.h @@ -26,7 +26,7 @@ class StacklessTask : public Task { try { \ operation; \ } \ - catch (Balau::EAgain & e) { \ + catch (Balau::EAgain &) { \ taskSwitch(); \ } \ @@ -40,7 +40,7 @@ class StacklessTask : public Task { operation; \ } \ } \ - catch (Balau::EAgain & e) { \ + catch (Balau::EAgain &) { \ taskSwitch(); \ } \ @@ -66,7 +66,7 @@ class StacklessTask : public Task { try { \ yieldNoWait(); \ } \ - catch (Balau::EAgain & e) { \ + catch (Balau::EAgain &) { \ taskSwitch(); \ } \ } \ diff --git a/lcrypt/lcrypt.c b/lcrypt/lcrypt.c index 779504e..987638f 100644 --- a/lcrypt/lcrypt.c +++ b/lcrypt/lcrypt.c @@ -1,20 +1,30 @@ // gcc -Wall -O3 -shared -fPIC -DLITTLE_ENDIAN -DLTM_DESC -DLTC_SOURCE -DUSE_LTM -I/usr/include/tomcrypt -I/usr/include/tommath -lz -lutil -ltomcrypt -ltommath lcrypt.c -o /usr/lib64/lua/5.1/lcrypt.so +#ifdef _WIN32 +#include <windows.h> +#else #include <unistd.h> +#include <sys/time.h> +#endif #include <stdio.h> #include <stdlib.h> #include <string.h> #include <signal.h> +#include <malloc.h> #include <errno.h> #include <sys/types.h> -#include <sys/time.h> #include <zlib.h> #include "lua.h" #include "lauxlib.h" #include "lualib.h" #include "tomcrypt.h" +#ifdef _WIN32 +#define likely(x) (x) +#define unlikely(x) (x) +#else #define likely(x) __builtin_expect((x),1) #define unlikely(x) __builtin_expect((x),0) +#endif #define ADD_FUNCTION(L,name) { lua_pushstring(L, #name); lua_pushcfunction(L, lcrypt_ ## name); lua_settable(L, -3); } #define ADD_CONSTANT(L,name) { lua_pushstring(L, #name); lua_pushinteger(L, name); lua_settable(L, -3); } @@ -77,7 +87,7 @@ static int lcrypt_fromhex(lua_State *L) { size_t in_length; const unsigned char *in = (const unsigned char*)luaL_checklstring(L, 1, &in_length); - unsigned char result[in_length]; + unsigned char * result = (unsigned char *)alloca(in_length); int i, d = -1, e = -1, pos = 0; for(i = 0; i < (int)in_length; i++) { @@ -207,6 +217,28 @@ static int lcrypt_xor(lua_State *L) return 1; } +#ifdef _WIN32 +
+static const unsigned __int64 epoch = 116444736000000000ULL;
+
+static int gettimeofday(struct timeval * tp, struct timezone * tzp)
+{
+ FILETIME file_time;
+ SYSTEMTIME system_time;
+ ULARGE_INTEGER ularge;
+
+ GetSystemTime(&system_time);
+ SystemTimeToFileTime(&system_time, &file_time);
+ ularge.LowPart = file_time.dwLowDateTime;
+ ularge.HighPart = file_time.dwHighDateTime;
+
+ tp->tv_sec = (long)((ularge.QuadPart - epoch) / 10000000L);
+ tp->tv_usec = (long)(system_time.wMilliseconds * 1000);
+
+ return 0;
+} +#endif + static int lcrypt_time(lua_State *L) { double ret; @@ -217,12 +249,18 @@ static int lcrypt_time(lua_State *L) return(1); } +#ifdef UNICODE
+#define _T(x) L##x
+#else
+#define _T(x) x
+#endif + static int lcrypt_random(lua_State *L) { int len = luaL_checkint(L, 1); char *buffer = lcrypt_malloc(L, len); #ifdef _WIN32 - HMODULE hLib = LoadLibrary("ADVAPI32.DLL"); + HMODULE hLib = LoadLibrary((LPCTSTR)_T("ADVAPI32.DLL")); if (unlikely(!hLib)) { lua_pushstring(L, "Unable to open ADVAPI32.DLL"); diff --git a/lcrypt/lcrypt_bits.c b/lcrypt/lcrypt_bits.c index 0ea7851..9712933 100644 --- a/lcrypt/lcrypt_bits.c +++ b/lcrypt/lcrypt_bits.c @@ -126,7 +126,7 @@ static int lcrypt_bget(lua_State *L) else if(type == B_STR) { int len = (bits + 7) / 8; - uint8_t data[len]; + uint8_t * data = (uint8_t *)alloca(len); memset(data, 0, len); copy_bits(data, 0, in, offset, bits); lua_pushlstring(L, (char*)data, len); @@ -176,7 +176,7 @@ static int lcrypt_bput(lua_State *L) len += bits; } len = (len + 7) / 8; - uint8_t ret[len]; + uint8_t * ret = (uint8_t *)alloca(len); memset(ret, 0, len); for(i = 1; i <= argc; i += 3) diff --git a/lcrypt/lcrypt_ciphers.c b/lcrypt/lcrypt_ciphers.c index dc8c47b..4dc17ae 100644 --- a/lcrypt/lcrypt_ciphers.c +++ b/lcrypt/lcrypt_ciphers.c @@ -33,7 +33,7 @@ static int lcrypt_cipher_ ## name (lua_State *L) static int lcrypt_cipher_ ## _name ## _index(lua_State *L) \ { \ symmetric_ ## NAME *skey = luaL_checkudata(L, 1, "LCRYPT_CIPHER_" #NAME); \ - if(unlikely(skey->cipher < 0)) return 0; \ + if(unlikely(skey->cipher < 0)) return 0;; \ const char *index = luaL_checkstring(L, 2); \ if(strcmp(index, "type") == 0) { lua_pushstring(L, "LCRYPT_CIPHER_" #NAME); return 1; } \ if(strcmp(index, "iv") == 0) \ @@ -238,15 +238,15 @@ static int lcrypt_cipher_ctr(lua_State *L) return 1; } -LCRYPT_CIPHER_XXX_ENCRYPT(CTR, ctr) -LCRYPT_CIPHER_XXX_DECRYPT(CTR, ctr) -LCRYPT_CIPHER_XXX_INDEX(CTR, ctr) -LCRYPT_CIPHER_XXX_NEWINDEX(CTR, ctr) -LCRYPT_CIPHER_XXX_GC(CTR, ctr) +LCRYPT_CIPHER_XXX_ENCRYPT(CTR, ctr); +LCRYPT_CIPHER_XXX_DECRYPT(CTR, ctr); +LCRYPT_CIPHER_XXX_INDEX(CTR, ctr); +LCRYPT_CIPHER_XXX_NEWINDEX(CTR, ctr); +LCRYPT_CIPHER_XXX_GC(CTR, ctr); -LCRYPT_CIPHER_XXX(CBC, cbc) -LCRYPT_CIPHER_XXX(CFB, cfb) -LCRYPT_CIPHER_XXX(OFB, ofb) +LCRYPT_CIPHER_XXX(CBC, cbc); +LCRYPT_CIPHER_XXX(CFB, cfb); +LCRYPT_CIPHER_XXX(OFB, ofb); static int lcrypt_cipher_lrw(lua_State *L) { @@ -274,11 +274,11 @@ static int lcrypt_cipher_lrw(lua_State *L) return 1; } -LCRYPT_CIPHER_XXX_ENCRYPT(LRW, lrw) -LCRYPT_CIPHER_XXX_DECRYPT(LRW, lrw) -LCRYPT_CIPHER_XXX_INDEX(LRW, lrw) -LCRYPT_CIPHER_XXX_NEWINDEX(LRW, lrw) -LCRYPT_CIPHER_XXX_GC(LRW, lrw) +LCRYPT_CIPHER_XXX_ENCRYPT(LRW, lrw); +LCRYPT_CIPHER_XXX_DECRYPT(LRW, lrw); +LCRYPT_CIPHER_XXX_INDEX(LRW, lrw); +LCRYPT_CIPHER_XXX_NEWINDEX(LRW, lrw); +LCRYPT_CIPHER_XXX_GC(LRW, lrw); static int lcrypt_cipher_f8(lua_State *L) { @@ -301,11 +301,11 @@ static int lcrypt_cipher_f8(lua_State *L) return 1; } -LCRYPT_CIPHER_XXX_ENCRYPT(F8, f8) -LCRYPT_CIPHER_XXX_DECRYPT(F8, f8) -LCRYPT_CIPHER_XXX_INDEX(F8, f8) -LCRYPT_CIPHER_XXX_NEWINDEX(F8, f8) -LCRYPT_CIPHER_XXX_GC(F8, f8) +LCRYPT_CIPHER_XXX_ENCRYPT(F8, f8); +LCRYPT_CIPHER_XXX_DECRYPT(F8, f8); +LCRYPT_CIPHER_XXX_INDEX(F8, f8); +LCRYPT_CIPHER_XXX_NEWINDEX(F8, f8); +LCRYPT_CIPHER_XXX_GC(F8, f8); #undef LCRYPT_CIPHER_XXX #undef LCRYPT_CIPHER_XXX_START diff --git a/lcrypt/lcrypt_hashes.c b/lcrypt/lcrypt_hashes.c index 4eccbfc..46edf28 100644 --- a/lcrypt/lcrypt_hashes.c +++ b/lcrypt/lcrypt_hashes.c @@ -21,7 +21,7 @@ static int lcrypt_hash_done(lua_State *L) lcrypt_hash *h = luaL_checkudata(L, 1, "LCRYPT_HASH_STATE"); if(likely(h->hash >= 0)) { - unsigned char out[hash_descriptor[h->hash].hashsize]; + unsigned char * out = (unsigned char *)alloca(hash_descriptor[h->hash].hashsize); lcrypt_error(L, hash_descriptor[h->hash].done(&h->state, out), NULL); lua_pushlstring(L, (char*)out, hash_descriptor[h->hash].hashsize); } @@ -35,7 +35,7 @@ static int lcrypt_hash_state_gc(lua_State *L) lcrypt_hash *h = luaL_checkudata(L, 1, "LCRYPT_HASH_STATE"); if(likely(h->hash >= 0)) { - unsigned char out[hash_descriptor[h->hash].hashsize]; + unsigned char * out = (unsigned char *)alloca(hash_descriptor[h->hash].hashsize); lcrypt_error(L, hash_descriptor[h->hash].done(&h->state, out), NULL); memset(h, 0, sizeof(lcrypt_hash)); h->hash = -1; @@ -96,7 +96,7 @@ static int lcrypt_hmac_done(lua_State *L) if(likely(h->hash >= 0)) { unsigned long out_length = hash_descriptor[h->hash].hashsize; - unsigned char out[out_length]; + unsigned char * out = (unsigned char *)alloca(out_length); lcrypt_error(L, hmac_done(h, out, &out_length), NULL); lua_pushlstring(L, (char*)out, out_length); memset(h, 0, sizeof(hmac_state)); @@ -112,7 +112,7 @@ static int lcrypt_hmac_state_gc(lua_State *L) if(likely(h->hash >= 0)) { unsigned long out_length = hash_descriptor[h->hash].hashsize; - unsigned char out[out_length]; + unsigned char * out = (unsigned char *)alloca(out_length); lcrypt_error(L, hmac_done(h, out, &out_length), NULL); memset(h, 0, sizeof(hmac_state)); h->hash = -1; diff --git a/src/BLua.cc b/src/BLua.cc index 7e3a1d2..d3e50b6 100644 --- a/src/BLua.cc +++ b/src/BLua.cc @@ -1,3 +1,7 @@ +#ifdef _WIN32 +#include <windows.h> +#endif + #include <memory> #include <stdlib.h> #include "BLua.h" @@ -167,12 +171,28 @@ int Balau::LuaStatics::getenv(lua_State * __L) { L.error("Incorrect arguments to function `getenv'"); #ifdef _WIN32 +#ifdef UNICODE + wchar_t wbuffer[BUFSIZ + 1]; + String varStr = L.tostring(1).iconv("UTF-8", "UNICODE"); + if (GetEnvironmentVariable((wchar_t *)varStr.to_charp(), wbuffer, BUFSIZ)) { + char buffer[BUFSIZ + 1]; + const wchar_t * pwbuffer = wbuffer; + mbstate_t mbs; + mbrlen(NULL, 0, &mbs); + memset(buffer, 0, BUFSIZ + 1); + wcsrtombs(buffer, &pwbuffer, BUFSIZ, &mbs); + L.push(buffer); + } else { + L.push(); + } +#else char buffer[BUFSIZ + 1]; if (GetEnvironmentVariable(L.tostring(1).to_charp(), buffer, BUFSIZ)) { L.push(buffer); } else { L.push(); } +#endif #else char * var = ::getenv(L.tostring(1).to_charp()); if (var) { @@ -194,7 +214,13 @@ int Balau::LuaStatics::setenv(lua_State * __L) { } #ifdef _WIN32 +#ifdef UNICODE + String varStr = L.tostring(1).iconv("UTF-8", "UNICODE"); + String valStr = L.tostring(2).iconv("UTF-8", "UNICODE"); + SetEnvironmentVariable((wchar_t *)varStr.to_charp(), (wchar_t *)valStr.to_charp()); +#else SetEnvironmentVariable(L.tostring(1).to_charp(), L.tostring(2).to_charp()); +#endif #else ::setenv(L.tostring(1).to_charp(), L.tostring(2).to_charp(), 1); #endif @@ -210,7 +236,12 @@ int Balau::LuaStatics::unsetenv(lua_State * __L) { L.error("Incorrect arguments to function `unsetenv'"); #ifdef _WIN32 +#ifdef UNICODE + String varStr = L.tostring(1).iconv("UTF-8", "UNICODE"); + SetEnvironmentVariable((wchar_t *)varStr.to_charp(), NULL); +#else SetEnvironmentVariable(L.tostring(1).to_charp(), NULL); +#endif #else ::unsetenv(L.tostring(1).to_charp()); #endif diff --git a/src/LuaHandle.cc b/src/LuaHandle.cc index de9e7e2..67a1496 100644 --- a/src/LuaHandle.cc +++ b/src/LuaHandle.cc @@ -262,7 +262,6 @@ int sLua_IOInput::IOInput_proceed_static(Balau::Lua & L, int n, int caller) { } int sLua_IOInput::IOInput_proceed(Balau::Lua & L, int n, IOInput * obj, int caller) { - int r; Balau::IO<Balau::Input> h = *obj; switch (caller) { @@ -271,7 +270,7 @@ int sLua_IOInput::IOInput_proceed(Balau::Lua & L, int n, IOInput * obj, int call break; } - return r; + return 0; } void Balau::LuaInputFactory::pushStatics(Balau::Lua & L) { diff --git a/src/LuaTask.cc b/src/LuaTask.cc index c22cb68..0835765 100644 --- a/src/LuaTask.cc +++ b/src/LuaTask.cc @@ -52,7 +52,7 @@ void Balau::LuaMainTask::Do() { try { cell = m_queue.pop(); } - catch (Balau::EAgain & e) { + catch (Balau::EAgain &) { taskSwitch(); } Printer::elog(E_TASK, "LuaMainTask at %p popped %p", this, cell); @@ -81,7 +81,7 @@ void Balau::LuaTask::Do() { else m_cell->run(L); } - catch (EAgain & e) { + catch (EAgain &) { } catch (GeneralException & e) { m_cell->m_exception = new GeneralException(e); |