aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthew Wild <mwild1@gmail.com>2010-12-17 22:32:21 +0000
committerMatthew Wild <mwild1@gmail.com>2010-12-17 22:32:21 +0000
commit1f3e0e60e4320c4536c5c73af7daedfaaaf400bf (patch)
treedc9dffdd167afa2ae18c23ca29f054c3ef4fa431
parentb18b85670fe5f6c1b6a6c2eafd8c8723f891e91c (diff)
downloadprosody-1f3e0e60e4320c4536c5c73af7daedfaaaf400bf.tar.gz
prosody-1f3e0e60e4320c4536c5c73af7daedfaaaf400bf.zip
util-src/Makefile, util-src/encodings.c{,pp}: Port ICU code to C, rename encodings.cpp back to .c and amend the Makefile accordingly
-rw-r--r--util-src/Makefile4
-rw-r--r--util-src/encodings.c (renamed from util-src/encodings.cpp)80
2 files changed, 45 insertions, 39 deletions
diff --git a/util-src/Makefile b/util-src/Makefile
index 008a1934..c3669c35 100644
--- a/util-src/Makefile
+++ b/util-src/Makefile
@@ -13,11 +13,11 @@ LD?=gcc
.SUFFIXES: .c .o .so
encodings.o:
- $(CXX) $(CFLAGS) -I$(LUA_INCDIR) -c -o encodings.o encodings.cpp
+ $(CC) $(CFLAGS) -I$(LUA_INCDIR) -c -o encodings.o encodings.c
encodings.so: encodings.o
MACOSX_DEPLOYMENT_TARGET="10.3"; export MACOSX_DEPLOYMENT_TARGET;
- $(CXX) $(LDFLAGS) $(IDNA_LIBS) -o encodings.so encodings.o -lcrypto
+ $(CC) $(LDFLAGS) $(IDNA_LIBS) -o encodings.so encodings.o -lcrypto
.c.o:
$(CC) $(CFLAGS) -I$(LUA_INCDIR) -c -o $@ $<
diff --git a/util-src/encodings.cpp b/util-src/encodings.c
index 81230afe..a6fdada3 100644
--- a/util-src/encodings.cpp
+++ b/util-src/encodings.c
@@ -15,15 +15,12 @@
// Newer MSVC compilers deprecate strcpy as unsafe, but we use it in a safe way
#define _CRT_SECURE_NO_DEPRECATE
-extern "C" {
#include <string.h>
#include <stdlib.h>
#include "lua.h"
#include "lauxlib.h"
-}
-/***************** BASE64 *****************/
-#define LUALIB_API extern "C"
+/***************** BASE64 *****************/
static const char code[]=
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
@@ -122,9 +119,9 @@ static const luaL_Reg Reg_base64[] =
/***************** STRINGPREP *****************/
#ifndef USE_STRINGPREP_ICU
/****************** libidn ********************/
-extern "C" {
+
#include <stringprep.h>
-}
+
static int stringprep_prep(lua_State *L, const Stringprep_profile *profile)
{
size_t len;
@@ -170,36 +167,41 @@ static const luaL_Reg Reg_stringprep[] =
#else
#include <unicode/usprep.h>
-#include <unicode/unistr.h>
+#include <unicode/ustring.h>
#include <unicode/utrace.h>
static int icu_stringprep_prep(lua_State *L, const UStringPrepProfile *profile)
{
- size_t len;
- const char *s;
- UnicodeString ustr;
- UErrorCode err = U_ZERO_ERROR;
- UChar dest[1024];
+ size_t input_len;
+ int32_t unprepped_len, prepped_len, output_len;
+ const char *input;
char output[1024];
+
+ UChar unprepped[1024]; /* Temporary unicode buffer (1024 characters) */
+ UChar prepped[1024];
+
+ UErrorCode err = U_ZERO_ERROR;
+
if(!lua_isstring(L, 1)) {
lua_pushnil(L);
return 1;
}
- s = lua_tolstring(L, 1, &len);
- if (len >= 1024) {
+ input = lua_tolstring(L, 1, &input_len);
+ if (input_len >= 1024) {
lua_pushnil(L);
- return 1; // TODO return error message
+ return 1;
}
- ustr = UnicodeString::fromUTF8(s);
- len = usprep_prepare(profile, ustr.getBuffer(), ustr.length(), dest, 1024, 0, NULL, &err);
+ u_strFromUTF8(unprepped, 1024, &unprepped_len, input, input_len, &err);
+ prepped_len = usprep_prepare(profile, unprepped, unprepped_len, prepped, 1024, 0, NULL, &err);
if (U_FAILURE(err)) {
lua_pushnil(L);
return 1;
} else {
- CheckedArrayByteSink output_sink(output, 1024);
- UnicodeString dest_str(TRUE, dest, len);
- dest_str.toUTF8(output_sink);
- lua_pushstring(L, output);
+ u_strToUTF8(output, 1024, &output_len, prepped, prepped_len, &err);
+ if(output_len < 1024)
+ lua_pushlstring(L, output, output_len);
+ else
+ lua_pushnil(L);
return 1;
}
}
@@ -242,8 +244,10 @@ static const luaL_Reg Reg_stringprep[] =
/***************** IDNA *****************/
#ifndef USE_STRINGPREP_ICU
/****************** libidn ********************/
+
#include <idna.h>
#include <idn-free.h>
+
static int Lidna_to_ascii(lua_State *L) /** idna.to_ascii(s) */
{
size_t len;
@@ -284,23 +288,24 @@ static int Lidna_to_unicode(lua_State *L) /** idna.to_unicode(s) */
static int Lidna_to_ascii(lua_State *L) /** idna.to_ascii(s) */
{
size_t len;
- int32_t out_len;
+ int32_t ulen, dest_len, output_len;
const char *s = luaL_checklstring(L, 1, &len);
- UnicodeString ustr;
+ UChar ustr[1024];
UErrorCode err = U_ZERO_ERROR;
UChar dest[1024];
char output[1024];
- ustr = UnicodeString::fromUTF8(s);
- out_len = uidna_IDNToASCII(ustr.getBuffer(), ustr.length(), dest, 1024, UIDNA_USE_STD3_RULES, NULL, &err);
+ u_strFromUTF8(ustr, 1024, &ulen, s, len, &err);
+ dest_len = uidna_IDNToASCII(ustr, ulen, dest, 1024, UIDNA_USE_STD3_RULES, NULL, &err);
if (U_FAILURE(err)) {
lua_pushnil(L);
return 1;
} else {
- CheckedArrayByteSink output_sink(output, 1024);
- UnicodeString dest_str(TRUE, dest, out_len);
- dest_str.toUTF8(output_sink);
- lua_pushstring(L, output);
+ u_strToUTF8(output, 1024, &output_len, dest, dest_len, &err);
+ if(output_len < 1024)
+ lua_pushlstring(L, output, output_len);
+ else
+ lua_pushnil(L);
return 1;
}
}
@@ -308,23 +313,24 @@ static int Lidna_to_ascii(lua_State *L) /** idna.to_ascii(s) */
static int Lidna_to_unicode(lua_State *L) /** idna.to_unicode(s) */
{
size_t len;
- int32_t out_len;
+ int32_t ulen, dest_len, output_len;
const char *s = luaL_checklstring(L, 1, &len);
- UnicodeString ustr;
+ UChar* ustr;
UErrorCode err = U_ZERO_ERROR;
UChar dest[1024];
char output[1024];
- ustr = UnicodeString::fromUTF8(s);
- out_len = uidna_IDNToUnicode(ustr.getBuffer(), ustr.length(), dest, 1024, UIDNA_USE_STD3_RULES, NULL, &err);
+ u_strFromUTF8(ustr, 1024, &ulen, s, len, &err);
+ dest_len = uidna_IDNToUnicode(ustr, ulen, dest, 1024, UIDNA_USE_STD3_RULES, NULL, &err);
if (U_FAILURE(err)) {
lua_pushnil(L);
return 1;
} else {
- CheckedArrayByteSink output_sink(output, 1024);
- UnicodeString dest_str(TRUE, dest, out_len);
- dest_str.toUTF8(output_sink);
- lua_pushstring(L, output);
+ u_strToUTF8(output, 1024, &output_len, dest, dest_len, &err);
+ if(output_len < 1024)
+ lua_pushlstring(L, output, output_len);
+ else
+ lua_pushnil(L);
return 1;
}
}