util-src/encodings.c
author Kim Alvefur <zash@zash.se>
Fri, 03 Apr 2015 19:52:48 +0200
changeset 6618 8e4572a642cb
parent 6611 b6e558febb7a
child 6646 127b9f0c6135
permissions -rw-r--r--
util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2923
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 2572
diff changeset
     1
/* Prosody IM
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 2572
diff changeset
     2
-- Copyright (C) 2008-2010 Matthew Wild
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 2572
diff changeset
     3
-- Copyright (C) 2008-2010 Waqas Hussain
6594
fe3018a2f187 util.encodings: Perform validation of UTF-8 strings before passing to libidn (Based on code from the utf8 library in Lua 5.3)
Kim Alvefur <zash@zash.se>
parents: 4302
diff changeset
     4
-- Copyright (C) 1994-2015 Lua.org, PUC-Rio.
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
     5
--
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
     6
-- This project is MIT/X11 licensed. Please see the
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
     7
-- COPYING file in the source package for more information.
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
     8
--
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
     9
*/
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
    10
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
    11
/*
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
    12
* encodings.c
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
    13
* Lua library for base64, stringprep and idna encodings
520
e96ac4bb6dd8 and the C files too
Matthew Wild <mwild1@gmail.com>
parents: 474
diff changeset
    14
*/
e96ac4bb6dd8 and the C files too
Matthew Wild <mwild1@gmail.com>
parents: 474
diff changeset
    15
3965
4ae4b2c0e99d util.encodings: Switch comment styles to build ok as ANSI C
Matthew Wild <mwild1@gmail.com>
parents: 3769
diff changeset
    16
/* Newer MSVC compilers deprecate strcpy as unsafe, but we use it in a safe way */
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
    17
#define _CRT_SECURE_NO_DEPRECATE
520
e96ac4bb6dd8 and the C files too
Matthew Wild <mwild1@gmail.com>
parents: 474
diff changeset
    18
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
    19
#include <string.h>
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
    20
#include <stdlib.h>
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
    21
#include "lua.h"
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
    22
#include "lauxlib.h"
3769
9338d0785277 util-src/Makefile, util-src/encodings.c{,pp}: Port ICU code to C, rename encodings.cpp back to .c and amend the Makefile accordingly
Matthew Wild <mwild1@gmail.com>
parents: 3764
diff changeset
    23
6416
a552f4170aed util-src/*.c: Add macro for compiling with Lua 5.2
Kim Alvefur <zash@zash.se>
parents: 6415
diff changeset
    24
#if (LUA_VERSION_NUM == 502)
a552f4170aed util-src/*.c: Add macro for compiling with Lua 5.2
Kim Alvefur <zash@zash.se>
parents: 6415
diff changeset
    25
#define luaL_register(L, N, R) luaL_setfuncs(L, R, 0)
a552f4170aed util-src/*.c: Add macro for compiling with Lua 5.2
Kim Alvefur <zash@zash.se>
parents: 6415
diff changeset
    26
#endif
a552f4170aed util-src/*.c: Add macro for compiling with Lua 5.2
Kim Alvefur <zash@zash.se>
parents: 6415
diff changeset
    27
3762
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
    28
/***************** BASE64 *****************/
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
    29
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
    30
static const char code[] =
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
    31
    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
    32
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
    33
static void base64_encode(luaL_Buffer* b, unsigned int c1, unsigned int c2, unsigned int c3, int n) {
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
    34
	unsigned long tuple = c3 + 256UL * (c2 + 256UL * c1);
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
    35
	int i;
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
    36
	char s[4];
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
    37
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
    38
	for(i = 0; i < 4; i++) {
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
    39
		s[3 - i] = code[tuple % 64];
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
    40
		tuple /= 64;
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
    41
	}
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
    42
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
    43
	for(i = n + 1; i < 4; i++) {
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
    44
		s[i] = '=';
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
    45
	}
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
    46
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
    47
	luaL_addlstring(b, s, 4);
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
    48
}
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
    49
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
    50
static int Lbase64_encode(lua_State* L) {	/** encode(s) */
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
    51
	size_t l;
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
    52
	const unsigned char* s = (const unsigned char*)luaL_checklstring(L, 1, &l);
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
    53
	luaL_Buffer b;
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
    54
	int n;
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
    55
	luaL_buffinit(L, &b);
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
    56
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
    57
	for(n = l / 3; n--; s += 3) {
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
    58
		base64_encode(&b, s[0], s[1], s[2], 3);
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
    59
	}
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
    60
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
    61
	switch(l % 3) {
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
    62
		case 1:
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
    63
			base64_encode(&b, s[0], 0, 0, 1);
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
    64
			break;
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
    65
		case 2:
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
    66
			base64_encode(&b, s[0], s[1], 0, 2);
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
    67
			break;
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
    68
	}
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
    69
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
    70
	luaL_pushresult(&b);
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
    71
	return 1;
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
    72
}
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
    73
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
    74
static void base64_decode(luaL_Buffer* b, int c1, int c2, int c3, int c4, int n) {
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
    75
	unsigned long tuple = c4 + 64L * (c3 + 64L * (c2 + 64L * c1));
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
    76
	char s[3];
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
    77
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
    78
	switch(--n) {
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
    79
		case 3:
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
    80
			s[2] = (char) tuple;
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
    81
		case 2:
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
    82
			s[1] = (char)(tuple >> 8);
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
    83
		case 1:
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
    84
			s[0] = (char)(tuple >> 16);
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
    85
	}
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
    86
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
    87
	luaL_addlstring(b, s, n);
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
    88
}
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
    89
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
    90
static int Lbase64_decode(lua_State* L) {	/** decode(s) */
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
    91
	size_t l;
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
    92
	const char* s = luaL_checklstring(L, 1, &l);
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
    93
	luaL_Buffer b;
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
    94
	int n = 0;
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
    95
	char t[4];
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
    96
	luaL_buffinit(L, &b);
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
    97
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
    98
	for(;;) {
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
    99
		int c = *s++;
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   100
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   101
		switch(c) {
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   102
				const char* p;
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
   103
			default:
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   104
				p = strchr(code, c);
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   105
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   106
				if(p == NULL) {
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   107
					return 0;
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
   108
				}
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   109
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   110
				t[n++] = (char)(p - code);
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   111
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   112
				if(n == 4) {
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   113
					base64_decode(&b, t[0], t[1], t[2], t[3], 4);
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   114
					n = 0;
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   115
				}
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   116
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
   117
				break;
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
   118
			case '=':
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   119
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   120
				switch(n) {
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   121
					case 1:
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   122
						base64_decode(&b, t[0], 0, 0, 0, 1);
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   123
						break;
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   124
					case 2:
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   125
						base64_decode(&b, t[0], t[1], 0, 0, 2);
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   126
						break;
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   127
					case 3:
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   128
						base64_decode(&b, t[0], t[1], t[2], 0, 3);
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   129
						break;
601
6cb908ef01c8 Fixed util.encodings.base64.decode to not truncate results when encountering an '=' before the end of the given input.
Waqas Hussain <waqas20@gmail.com>
parents: 520
diff changeset
   130
				}
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   131
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   132
				n = 0;
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
   133
				break;
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
   134
			case 0:
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
   135
				luaL_pushresult(&b);
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
   136
				return 1;
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   137
			case '\n':
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   138
			case '\r':
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   139
			case '\t':
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   140
			case ' ':
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   141
			case '\f':
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   142
			case '\b':
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
   143
				break;
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
   144
		}
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
   145
	}
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
   146
}
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
   147
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   148
static const luaL_Reg Reg_base64[] = {
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
   149
	{ "encode",	Lbase64_encode	},
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
   150
	{ "decode",	Lbase64_decode	},
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
   151
	{ NULL,		NULL	}
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
   152
};
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
   153
6594
fe3018a2f187 util.encodings: Perform validation of UTF-8 strings before passing to libidn (Based on code from the utf8 library in Lua 5.3)
Kim Alvefur <zash@zash.se>
parents: 4302
diff changeset
   154
/******************* UTF-8 ********************/
fe3018a2f187 util.encodings: Perform validation of UTF-8 strings before passing to libidn (Based on code from the utf8 library in Lua 5.3)
Kim Alvefur <zash@zash.se>
parents: 4302
diff changeset
   155
fe3018a2f187 util.encodings: Perform validation of UTF-8 strings before passing to libidn (Based on code from the utf8 library in Lua 5.3)
Kim Alvefur <zash@zash.se>
parents: 4302
diff changeset
   156
/*
fe3018a2f187 util.encodings: Perform validation of UTF-8 strings before passing to libidn (Based on code from the utf8 library in Lua 5.3)
Kim Alvefur <zash@zash.se>
parents: 4302
diff changeset
   157
 * Adapted from Lua 5.3
fe3018a2f187 util.encodings: Perform validation of UTF-8 strings before passing to libidn (Based on code from the utf8 library in Lua 5.3)
Kim Alvefur <zash@zash.se>
parents: 4302
diff changeset
   158
 * Needed because libidn does not validate that input is valid UTF-8
fe3018a2f187 util.encodings: Perform validation of UTF-8 strings before passing to libidn (Based on code from the utf8 library in Lua 5.3)
Kim Alvefur <zash@zash.se>
parents: 4302
diff changeset
   159
 */
fe3018a2f187 util.encodings: Perform validation of UTF-8 strings before passing to libidn (Based on code from the utf8 library in Lua 5.3)
Kim Alvefur <zash@zash.se>
parents: 4302
diff changeset
   160
fe3018a2f187 util.encodings: Perform validation of UTF-8 strings before passing to libidn (Based on code from the utf8 library in Lua 5.3)
Kim Alvefur <zash@zash.se>
parents: 4302
diff changeset
   161
#define MAXUNICODE	0x10FFFF
fe3018a2f187 util.encodings: Perform validation of UTF-8 strings before passing to libidn (Based on code from the utf8 library in Lua 5.3)
Kim Alvefur <zash@zash.se>
parents: 4302
diff changeset
   162
fe3018a2f187 util.encodings: Perform validation of UTF-8 strings before passing to libidn (Based on code from the utf8 library in Lua 5.3)
Kim Alvefur <zash@zash.se>
parents: 4302
diff changeset
   163
/*
fe3018a2f187 util.encodings: Perform validation of UTF-8 strings before passing to libidn (Based on code from the utf8 library in Lua 5.3)
Kim Alvefur <zash@zash.se>
parents: 4302
diff changeset
   164
 * Decode one UTF-8 sequence, returning NULL if byte sequence is invalid.
fe3018a2f187 util.encodings: Perform validation of UTF-8 strings before passing to libidn (Based on code from the utf8 library in Lua 5.3)
Kim Alvefur <zash@zash.se>
parents: 4302
diff changeset
   165
 */
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   166
static const char* utf8_decode(const char* o, int* val) {
6594
fe3018a2f187 util.encodings: Perform validation of UTF-8 strings before passing to libidn (Based on code from the utf8 library in Lua 5.3)
Kim Alvefur <zash@zash.se>
parents: 4302
diff changeset
   167
	static unsigned int limits[] = {0xFF, 0x7F, 0x7FF, 0xFFFF};
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   168
	const unsigned char* s = (const unsigned char*)o;
6594
fe3018a2f187 util.encodings: Perform validation of UTF-8 strings before passing to libidn (Based on code from the utf8 library in Lua 5.3)
Kim Alvefur <zash@zash.se>
parents: 4302
diff changeset
   169
	unsigned int c = s[0];
fe3018a2f187 util.encodings: Perform validation of UTF-8 strings before passing to libidn (Based on code from the utf8 library in Lua 5.3)
Kim Alvefur <zash@zash.se>
parents: 4302
diff changeset
   170
	unsigned int res = 0;  /* final result */
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   171
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   172
	if(c < 0x80) { /* ascii? */
6594
fe3018a2f187 util.encodings: Perform validation of UTF-8 strings before passing to libidn (Based on code from the utf8 library in Lua 5.3)
Kim Alvefur <zash@zash.se>
parents: 4302
diff changeset
   173
		res = c;
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   174
	} else {
6594
fe3018a2f187 util.encodings: Perform validation of UTF-8 strings before passing to libidn (Based on code from the utf8 library in Lua 5.3)
Kim Alvefur <zash@zash.se>
parents: 4302
diff changeset
   175
		int count = 0;  /* to count number of continuation bytes */
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   176
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   177
		while(c & 0x40) {   /* still have continuation bytes? */
6594
fe3018a2f187 util.encodings: Perform validation of UTF-8 strings before passing to libidn (Based on code from the utf8 library in Lua 5.3)
Kim Alvefur <zash@zash.se>
parents: 4302
diff changeset
   178
			int cc = s[++count];  /* read next byte */
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   179
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   180
			if((cc & 0xC0) != 0x80) { /* not a continuation byte? */
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   181
				return NULL;    /* invalid byte sequence */
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   182
			}
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   183
6594
fe3018a2f187 util.encodings: Perform validation of UTF-8 strings before passing to libidn (Based on code from the utf8 library in Lua 5.3)
Kim Alvefur <zash@zash.se>
parents: 4302
diff changeset
   184
			res = (res << 6) | (cc & 0x3F);  /* add lower 6 bits from cont. byte */
fe3018a2f187 util.encodings: Perform validation of UTF-8 strings before passing to libidn (Based on code from the utf8 library in Lua 5.3)
Kim Alvefur <zash@zash.se>
parents: 4302
diff changeset
   185
			c <<= 1;  /* to test next bit */
fe3018a2f187 util.encodings: Perform validation of UTF-8 strings before passing to libidn (Based on code from the utf8 library in Lua 5.3)
Kim Alvefur <zash@zash.se>
parents: 4302
diff changeset
   186
		}
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   187
6594
fe3018a2f187 util.encodings: Perform validation of UTF-8 strings before passing to libidn (Based on code from the utf8 library in Lua 5.3)
Kim Alvefur <zash@zash.se>
parents: 4302
diff changeset
   188
		res |= ((c & 0x7F) << (count * 5));  /* add first byte */
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   189
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   190
		if(count > 3 || res > MAXUNICODE || res <= limits[count] || (0xd800 <= res && res <= 0xdfff)) {
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   191
			return NULL;    /* invalid byte sequence */
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   192
		}
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   193
6594
fe3018a2f187 util.encodings: Perform validation of UTF-8 strings before passing to libidn (Based on code from the utf8 library in Lua 5.3)
Kim Alvefur <zash@zash.se>
parents: 4302
diff changeset
   194
		s += count;  /* skip continuation bytes read */
fe3018a2f187 util.encodings: Perform validation of UTF-8 strings before passing to libidn (Based on code from the utf8 library in Lua 5.3)
Kim Alvefur <zash@zash.se>
parents: 4302
diff changeset
   195
	}
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   196
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   197
	if(val) {
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   198
		*val = res;
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   199
	}
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   200
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   201
	return (const char*)s + 1;   /* +1 to include first byte */
6594
fe3018a2f187 util.encodings: Perform validation of UTF-8 strings before passing to libidn (Based on code from the utf8 library in Lua 5.3)
Kim Alvefur <zash@zash.se>
parents: 4302
diff changeset
   202
}
fe3018a2f187 util.encodings: Perform validation of UTF-8 strings before passing to libidn (Based on code from the utf8 library in Lua 5.3)
Kim Alvefur <zash@zash.se>
parents: 4302
diff changeset
   203
fe3018a2f187 util.encodings: Perform validation of UTF-8 strings before passing to libidn (Based on code from the utf8 library in Lua 5.3)
Kim Alvefur <zash@zash.se>
parents: 4302
diff changeset
   204
/*
fe3018a2f187 util.encodings: Perform validation of UTF-8 strings before passing to libidn (Based on code from the utf8 library in Lua 5.3)
Kim Alvefur <zash@zash.se>
parents: 4302
diff changeset
   205
 * Check that a string is valid UTF-8
fe3018a2f187 util.encodings: Perform validation of UTF-8 strings before passing to libidn (Based on code from the utf8 library in Lua 5.3)
Kim Alvefur <zash@zash.se>
parents: 4302
diff changeset
   206
 * Returns NULL if not
fe3018a2f187 util.encodings: Perform validation of UTF-8 strings before passing to libidn (Based on code from the utf8 library in Lua 5.3)
Kim Alvefur <zash@zash.se>
parents: 4302
diff changeset
   207
 */
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   208
const char* check_utf8(lua_State* L, int idx, size_t* l) {
6594
fe3018a2f187 util.encodings: Perform validation of UTF-8 strings before passing to libidn (Based on code from the utf8 library in Lua 5.3)
Kim Alvefur <zash@zash.se>
parents: 4302
diff changeset
   209
	size_t pos, len;
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   210
	const char* s = luaL_checklstring(L, 1, &len);
6594
fe3018a2f187 util.encodings: Perform validation of UTF-8 strings before passing to libidn (Based on code from the utf8 library in Lua 5.3)
Kim Alvefur <zash@zash.se>
parents: 4302
diff changeset
   211
	pos = 0;
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   212
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   213
	while(pos <= len) {
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   214
		const char* s1 = utf8_decode(s + pos, NULL);
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   215
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   216
		if(s1 == NULL) {   /* conversion error? */
6594
fe3018a2f187 util.encodings: Perform validation of UTF-8 strings before passing to libidn (Based on code from the utf8 library in Lua 5.3)
Kim Alvefur <zash@zash.se>
parents: 4302
diff changeset
   217
			return NULL;
fe3018a2f187 util.encodings: Perform validation of UTF-8 strings before passing to libidn (Based on code from the utf8 library in Lua 5.3)
Kim Alvefur <zash@zash.se>
parents: 4302
diff changeset
   218
		}
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   219
6594
fe3018a2f187 util.encodings: Perform validation of UTF-8 strings before passing to libidn (Based on code from the utf8 library in Lua 5.3)
Kim Alvefur <zash@zash.se>
parents: 4302
diff changeset
   220
		pos = s1 - s;
fe3018a2f187 util.encodings: Perform validation of UTF-8 strings before passing to libidn (Based on code from the utf8 library in Lua 5.3)
Kim Alvefur <zash@zash.se>
parents: 4302
diff changeset
   221
	}
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   222
6594
fe3018a2f187 util.encodings: Perform validation of UTF-8 strings before passing to libidn (Based on code from the utf8 library in Lua 5.3)
Kim Alvefur <zash@zash.se>
parents: 4302
diff changeset
   223
	if(l != NULL) {
fe3018a2f187 util.encodings: Perform validation of UTF-8 strings before passing to libidn (Based on code from the utf8 library in Lua 5.3)
Kim Alvefur <zash@zash.se>
parents: 4302
diff changeset
   224
		*l = len;
fe3018a2f187 util.encodings: Perform validation of UTF-8 strings before passing to libidn (Based on code from the utf8 library in Lua 5.3)
Kim Alvefur <zash@zash.se>
parents: 4302
diff changeset
   225
	}
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   226
6594
fe3018a2f187 util.encodings: Perform validation of UTF-8 strings before passing to libidn (Based on code from the utf8 library in Lua 5.3)
Kim Alvefur <zash@zash.se>
parents: 4302
diff changeset
   227
	return s;
fe3018a2f187 util.encodings: Perform validation of UTF-8 strings before passing to libidn (Based on code from the utf8 library in Lua 5.3)
Kim Alvefur <zash@zash.se>
parents: 4302
diff changeset
   228
}
fe3018a2f187 util.encodings: Perform validation of UTF-8 strings before passing to libidn (Based on code from the utf8 library in Lua 5.3)
Kim Alvefur <zash@zash.se>
parents: 4302
diff changeset
   229
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   230
static int Lutf8_valid(lua_State* L) {
6595
141afe8a167b util.encodings: Expose UTF-8 validation and length checking functions
Kim Alvefur <zash@zash.se>
parents: 6594
diff changeset
   231
	lua_pushboolean(L, check_utf8(L, 1, NULL) != NULL);
141afe8a167b util.encodings: Expose UTF-8 validation and length checking functions
Kim Alvefur <zash@zash.se>
parents: 6594
diff changeset
   232
	return 1;
141afe8a167b util.encodings: Expose UTF-8 validation and length checking functions
Kim Alvefur <zash@zash.se>
parents: 6594
diff changeset
   233
}
141afe8a167b util.encodings: Expose UTF-8 validation and length checking functions
Kim Alvefur <zash@zash.se>
parents: 6594
diff changeset
   234
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   235
static int Lutf8_length(lua_State* L) {
6595
141afe8a167b util.encodings: Expose UTF-8 validation and length checking functions
Kim Alvefur <zash@zash.se>
parents: 6594
diff changeset
   236
	size_t len;
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   237
6595
141afe8a167b util.encodings: Expose UTF-8 validation and length checking functions
Kim Alvefur <zash@zash.se>
parents: 6594
diff changeset
   238
	if(!check_utf8(L, 1, &len)) {
141afe8a167b util.encodings: Expose UTF-8 validation and length checking functions
Kim Alvefur <zash@zash.se>
parents: 6594
diff changeset
   239
		lua_pushnil(L);
141afe8a167b util.encodings: Expose UTF-8 validation and length checking functions
Kim Alvefur <zash@zash.se>
parents: 6594
diff changeset
   240
		lua_pushliteral(L, "invalid utf8");
141afe8a167b util.encodings: Expose UTF-8 validation and length checking functions
Kim Alvefur <zash@zash.se>
parents: 6594
diff changeset
   241
		return 2;
141afe8a167b util.encodings: Expose UTF-8 validation and length checking functions
Kim Alvefur <zash@zash.se>
parents: 6594
diff changeset
   242
	}
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   243
6595
141afe8a167b util.encodings: Expose UTF-8 validation and length checking functions
Kim Alvefur <zash@zash.se>
parents: 6594
diff changeset
   244
	lua_pushinteger(L, len);
141afe8a167b util.encodings: Expose UTF-8 validation and length checking functions
Kim Alvefur <zash@zash.se>
parents: 6594
diff changeset
   245
	return 1;
141afe8a167b util.encodings: Expose UTF-8 validation and length checking functions
Kim Alvefur <zash@zash.se>
parents: 6594
diff changeset
   246
}
141afe8a167b util.encodings: Expose UTF-8 validation and length checking functions
Kim Alvefur <zash@zash.se>
parents: 6594
diff changeset
   247
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   248
static const luaL_Reg Reg_utf8[] = {
6595
141afe8a167b util.encodings: Expose UTF-8 validation and length checking functions
Kim Alvefur <zash@zash.se>
parents: 6594
diff changeset
   249
	{ "valid",	Lutf8_valid	},
141afe8a167b util.encodings: Expose UTF-8 validation and length checking functions
Kim Alvefur <zash@zash.se>
parents: 6594
diff changeset
   250
	{ "length",	Lutf8_length	},
141afe8a167b util.encodings: Expose UTF-8 validation and length checking functions
Kim Alvefur <zash@zash.se>
parents: 6594
diff changeset
   251
	{ NULL,		NULL	}
141afe8a167b util.encodings: Expose UTF-8 validation and length checking functions
Kim Alvefur <zash@zash.se>
parents: 6594
diff changeset
   252
};
141afe8a167b util.encodings: Expose UTF-8 validation and length checking functions
Kim Alvefur <zash@zash.se>
parents: 6594
diff changeset
   253
6594
fe3018a2f187 util.encodings: Perform validation of UTF-8 strings before passing to libidn (Based on code from the utf8 library in Lua 5.3)
Kim Alvefur <zash@zash.se>
parents: 4302
diff changeset
   254
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
   255
/***************** STRINGPREP *****************/
4272
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
   256
#ifdef USE_STRINGPREP_ICU
3769
9338d0785277 util-src/Makefile, util-src/encodings.c{,pp}: Port ICU code to C, rename encodings.cpp back to .c and amend the Makefile accordingly
Matthew Wild <mwild1@gmail.com>
parents: 3764
diff changeset
   257
3762
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
   258
#include <unicode/usprep.h>
3769
9338d0785277 util-src/Makefile, util-src/encodings.c{,pp}: Port ICU code to C, rename encodings.cpp back to .c and amend the Makefile accordingly
Matthew Wild <mwild1@gmail.com>
parents: 3764
diff changeset
   259
#include <unicode/ustring.h>
3762
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
   260
#include <unicode/utrace.h>
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
   261
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   262
static int icu_stringprep_prep(lua_State* L, const UStringPrepProfile* profile) {
3769
9338d0785277 util-src/Makefile, util-src/encodings.c{,pp}: Port ICU code to C, rename encodings.cpp back to .c and amend the Makefile accordingly
Matthew Wild <mwild1@gmail.com>
parents: 3764
diff changeset
   263
	size_t input_len;
9338d0785277 util-src/Makefile, util-src/encodings.c{,pp}: Port ICU code to C, rename encodings.cpp back to .c and amend the Makefile accordingly
Matthew Wild <mwild1@gmail.com>
parents: 3764
diff changeset
   264
	int32_t unprepped_len, prepped_len, output_len;
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   265
	const char* input;
3769
9338d0785277 util-src/Makefile, util-src/encodings.c{,pp}: Port ICU code to C, rename encodings.cpp back to .c and amend the Makefile accordingly
Matthew Wild <mwild1@gmail.com>
parents: 3764
diff changeset
   266
	char output[1024];
9338d0785277 util-src/Makefile, util-src/encodings.c{,pp}: Port ICU code to C, rename encodings.cpp back to .c and amend the Makefile accordingly
Matthew Wild <mwild1@gmail.com>
parents: 3764
diff changeset
   267
9338d0785277 util-src/Makefile, util-src/encodings.c{,pp}: Port ICU code to C, rename encodings.cpp back to .c and amend the Makefile accordingly
Matthew Wild <mwild1@gmail.com>
parents: 3764
diff changeset
   268
	UChar unprepped[1024]; /* Temporary unicode buffer (1024 characters) */
9338d0785277 util-src/Makefile, util-src/encodings.c{,pp}: Port ICU code to C, rename encodings.cpp back to .c and amend the Makefile accordingly
Matthew Wild <mwild1@gmail.com>
parents: 3764
diff changeset
   269
	UChar prepped[1024];
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   270
3762
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
   271
	UErrorCode err = U_ZERO_ERROR;
3769
9338d0785277 util-src/Makefile, util-src/encodings.c{,pp}: Port ICU code to C, rename encodings.cpp back to .c and amend the Makefile accordingly
Matthew Wild <mwild1@gmail.com>
parents: 3764
diff changeset
   272
3762
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
   273
	if(!lua_isstring(L, 1)) {
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
   274
		lua_pushnil(L);
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
   275
		return 1;
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
   276
	}
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   277
3769
9338d0785277 util-src/Makefile, util-src/encodings.c{,pp}: Port ICU code to C, rename encodings.cpp back to .c and amend the Makefile accordingly
Matthew Wild <mwild1@gmail.com>
parents: 3764
diff changeset
   278
	input = lua_tolstring(L, 1, &input_len);
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   279
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   280
	if(input_len >= 1024) {
3762
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
   281
		lua_pushnil(L);
3769
9338d0785277 util-src/Makefile, util-src/encodings.c{,pp}: Port ICU code to C, rename encodings.cpp back to .c and amend the Makefile accordingly
Matthew Wild <mwild1@gmail.com>
parents: 3764
diff changeset
   282
		return 1;
3762
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
   283
	}
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   284
3769
9338d0785277 util-src/Makefile, util-src/encodings.c{,pp}: Port ICU code to C, rename encodings.cpp back to .c and amend the Makefile accordingly
Matthew Wild <mwild1@gmail.com>
parents: 3764
diff changeset
   285
	u_strFromUTF8(unprepped, 1024, &unprepped_len, input, input_len, &err);
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   286
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   287
	if(U_FAILURE(err)) {
4302
bbb0bf0a09f5 util.encodings: Fix small typo introduced in 7f789266b741
Matthew Wild <mwild1@gmail.com>
parents: 4273
diff changeset
   288
		lua_pushnil(L);
4273
7f789266b741 util.encodings: Check return values before proceeding
Paul Aurich <paul@darkrain42.org>
parents: 4272
diff changeset
   289
		return 1;
7f789266b741 util.encodings: Check return values before proceeding
Paul Aurich <paul@darkrain42.org>
parents: 4272
diff changeset
   290
	}
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   291
3769
9338d0785277 util-src/Makefile, util-src/encodings.c{,pp}: Port ICU code to C, rename encodings.cpp back to .c and amend the Makefile accordingly
Matthew Wild <mwild1@gmail.com>
parents: 3764
diff changeset
   292
	prepped_len = usprep_prepare(profile, unprepped, unprepped_len, prepped, 1024, 0, NULL, &err);
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   293
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   294
	if(U_FAILURE(err)) {
3762
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
   295
		lua_pushnil(L);
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
   296
		return 1;
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
   297
	} else {
3769
9338d0785277 util-src/Makefile, util-src/encodings.c{,pp}: Port ICU code to C, rename encodings.cpp back to .c and amend the Makefile accordingly
Matthew Wild <mwild1@gmail.com>
parents: 3764
diff changeset
   298
		u_strToUTF8(output, 1024, &output_len, prepped, prepped_len, &err);
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   299
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   300
		if(U_SUCCESS(err) && output_len < 1024) {
3769
9338d0785277 util-src/Makefile, util-src/encodings.c{,pp}: Port ICU code to C, rename encodings.cpp back to .c and amend the Makefile accordingly
Matthew Wild <mwild1@gmail.com>
parents: 3764
diff changeset
   301
			lua_pushlstring(L, output, output_len);
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   302
		} else {
3769
9338d0785277 util-src/Makefile, util-src/encodings.c{,pp}: Port ICU code to C, rename encodings.cpp back to .c and amend the Makefile accordingly
Matthew Wild <mwild1@gmail.com>
parents: 3764
diff changeset
   303
			lua_pushnil(L);
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   304
		}
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   305
3762
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
   306
		return 1;
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
   307
	}
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
   308
}
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
   309
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   310
UStringPrepProfile* icu_nameprep;
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   311
UStringPrepProfile* icu_nodeprep;
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   312
UStringPrepProfile* icu_resourceprep;
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   313
UStringPrepProfile* icu_saslprep;
3762
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
   314
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
   315
/* initialize global ICU stringprep profiles */
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   316
void init_icu() {
3762
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
   317
	UErrorCode err = U_ZERO_ERROR;
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
   318
	utrace_setLevel(UTRACE_VERBOSE);
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
   319
	icu_nameprep = usprep_openByType(USPREP_RFC3491_NAMEPREP, &err);
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
   320
	icu_nodeprep = usprep_openByType(USPREP_RFC3920_NODEPREP, &err);
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
   321
	icu_resourceprep = usprep_openByType(USPREP_RFC3920_RESOURCEPREP, &err);
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
   322
	icu_saslprep = usprep_openByType(USPREP_RFC4013_SASLPREP, &err);
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   323
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   324
	if(U_FAILURE(err)) {
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   325
		fprintf(stderr, "[c] util.encodings: error: %s\n", u_errorName((UErrorCode)err));
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   326
	}
3762
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
   327
}
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
   328
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
   329
#define MAKE_PREP_FUNC(myFunc, prep) \
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
   330
static int myFunc(lua_State *L) { return icu_stringprep_prep(L, prep); }
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
   331
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
   332
MAKE_PREP_FUNC(Lstringprep_nameprep, icu_nameprep)		/** stringprep.nameprep(s) */
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
   333
MAKE_PREP_FUNC(Lstringprep_nodeprep, icu_nodeprep)		/** stringprep.nodeprep(s) */
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
   334
MAKE_PREP_FUNC(Lstringprep_resourceprep, icu_resourceprep)		/** stringprep.resourceprep(s) */
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
   335
MAKE_PREP_FUNC(Lstringprep_saslprep, icu_saslprep)		/** stringprep.saslprep(s) */
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
   336
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   337
static const luaL_Reg Reg_stringprep[] = {
3762
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
   338
	{ "nameprep",	Lstringprep_nameprep	},
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
   339
	{ "nodeprep",	Lstringprep_nodeprep	},
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
   340
	{ "resourceprep",	Lstringprep_resourceprep	},
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
   341
	{ "saslprep",	Lstringprep_saslprep	},
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
   342
	{ NULL,		NULL	}
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
   343
};
4272
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
   344
#else /* USE_STRINGPREP_ICU */
3762
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
   345
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
   346
/****************** libidn ********************/
3769
9338d0785277 util-src/Makefile, util-src/encodings.c{,pp}: Port ICU code to C, rename encodings.cpp back to .c and amend the Makefile accordingly
Matthew Wild <mwild1@gmail.com>
parents: 3764
diff changeset
   347
4272
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
   348
#include <stringprep.h>
3769
9338d0785277 util-src/Makefile, util-src/encodings.c{,pp}: Port ICU code to C, rename encodings.cpp back to .c and amend the Makefile accordingly
Matthew Wild <mwild1@gmail.com>
parents: 3764
diff changeset
   349
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   350
static int stringprep_prep(lua_State* L, const Stringprep_profile* profile) {
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
   351
	size_t len;
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   352
	const char* s;
4272
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
   353
	char string[1024];
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
   354
	int ret;
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   355
4272
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
   356
	if(!lua_isstring(L, 1)) {
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
   357
		lua_pushnil(L);
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
   358
		return 1;
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
   359
	}
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   360
6594
fe3018a2f187 util.encodings: Perform validation of UTF-8 strings before passing to libidn (Based on code from the utf8 library in Lua 5.3)
Kim Alvefur <zash@zash.se>
parents: 4302
diff changeset
   361
	s = check_utf8(L, 1, &len);
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   362
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   363
	if(s == NULL || len >= 1024 || len != strlen(s)) {
4272
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
   364
		lua_pushnil(L);
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
   365
		return 1; /* TODO return error message */
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
   366
	}
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   367
4272
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
   368
	strcpy(string, s);
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
   369
	ret = stringprep(string, 1024, (Stringprep_profile_flags)0, profile);
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   370
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   371
	if(ret == STRINGPREP_OK) {
4272
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
   372
		lua_pushstring(L, string);
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
   373
		return 1;
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
   374
	} else {
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
   375
		lua_pushnil(L);
3965
4ae4b2c0e99d util.encodings: Switch comment styles to build ok as ANSI C
Matthew Wild <mwild1@gmail.com>
parents: 3769
diff changeset
   376
		return 1; /* TODO return error message */
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
   377
	}
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
   378
}
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
   379
4272
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
   380
#define MAKE_PREP_FUNC(myFunc, prep) \
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
   381
static int myFunc(lua_State *L) { return stringprep_prep(L, prep); }
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
   382
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
   383
MAKE_PREP_FUNC(Lstringprep_nameprep, stringprep_nameprep)		/** stringprep.nameprep(s) */
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
   384
MAKE_PREP_FUNC(Lstringprep_nodeprep, stringprep_xmpp_nodeprep)		/** stringprep.nodeprep(s) */
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
   385
MAKE_PREP_FUNC(Lstringprep_resourceprep, stringprep_xmpp_resourceprep)		/** stringprep.resourceprep(s) */
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
   386
MAKE_PREP_FUNC(Lstringprep_saslprep, stringprep_saslprep)		/** stringprep.saslprep(s) */
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
   387
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   388
static const luaL_Reg Reg_stringprep[] = {
4272
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
   389
	{ "nameprep",	Lstringprep_nameprep	},
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
   390
	{ "nodeprep",	Lstringprep_nodeprep	},
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
   391
	{ "resourceprep",	Lstringprep_resourceprep	},
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
   392
	{ "saslprep",	Lstringprep_saslprep	},
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
   393
	{ NULL,		NULL	}
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
   394
};
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
   395
#endif
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
   396
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
   397
/***************** IDNA *****************/
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
   398
#ifdef USE_STRINGPREP_ICU
3762
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
   399
#include <unicode/ustdio.h>
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
   400
#include <unicode/uidna.h>
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
   401
/* IDNA2003 or IDNA2008 ? ? ? */
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   402
static int Lidna_to_ascii(lua_State* L) {	/** idna.to_ascii(s) */
3762
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
   403
	size_t len;
3769
9338d0785277 util-src/Makefile, util-src/encodings.c{,pp}: Port ICU code to C, rename encodings.cpp back to .c and amend the Makefile accordingly
Matthew Wild <mwild1@gmail.com>
parents: 3764
diff changeset
   404
	int32_t ulen, dest_len, output_len;
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   405
	const char* s = luaL_checklstring(L, 1, &len);
3769
9338d0785277 util-src/Makefile, util-src/encodings.c{,pp}: Port ICU code to C, rename encodings.cpp back to .c and amend the Makefile accordingly
Matthew Wild <mwild1@gmail.com>
parents: 3764
diff changeset
   406
	UChar ustr[1024];
3762
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
   407
	UErrorCode err = U_ZERO_ERROR;
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
   408
	UChar dest[1024];
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
   409
	char output[1024];
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
   410
3769
9338d0785277 util-src/Makefile, util-src/encodings.c{,pp}: Port ICU code to C, rename encodings.cpp back to .c and amend the Makefile accordingly
Matthew Wild <mwild1@gmail.com>
parents: 3764
diff changeset
   411
	u_strFromUTF8(ustr, 1024, &ulen, s, len, &err);
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   412
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   413
	if(U_FAILURE(err)) {
4273
7f789266b741 util.encodings: Check return values before proceeding
Paul Aurich <paul@darkrain42.org>
parents: 4272
diff changeset
   414
		lua_pushnil(L);
7f789266b741 util.encodings: Check return values before proceeding
Paul Aurich <paul@darkrain42.org>
parents: 4272
diff changeset
   415
		return 1;
7f789266b741 util.encodings: Check return values before proceeding
Paul Aurich <paul@darkrain42.org>
parents: 4272
diff changeset
   416
	}
7f789266b741 util.encodings: Check return values before proceeding
Paul Aurich <paul@darkrain42.org>
parents: 4272
diff changeset
   417
3769
9338d0785277 util-src/Makefile, util-src/encodings.c{,pp}: Port ICU code to C, rename encodings.cpp back to .c and amend the Makefile accordingly
Matthew Wild <mwild1@gmail.com>
parents: 3764
diff changeset
   418
	dest_len = uidna_IDNToASCII(ustr, ulen, dest, 1024, UIDNA_USE_STD3_RULES, NULL, &err);
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   419
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   420
	if(U_FAILURE(err)) {
3762
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
   421
		lua_pushnil(L);
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
   422
		return 1;
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
   423
	} else {
3769
9338d0785277 util-src/Makefile, util-src/encodings.c{,pp}: Port ICU code to C, rename encodings.cpp back to .c and amend the Makefile accordingly
Matthew Wild <mwild1@gmail.com>
parents: 3764
diff changeset
   424
		u_strToUTF8(output, 1024, &output_len, dest, dest_len, &err);
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   425
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   426
		if(U_SUCCESS(err) && output_len < 1024) {
3769
9338d0785277 util-src/Makefile, util-src/encodings.c{,pp}: Port ICU code to C, rename encodings.cpp back to .c and amend the Makefile accordingly
Matthew Wild <mwild1@gmail.com>
parents: 3764
diff changeset
   427
			lua_pushlstring(L, output, output_len);
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   428
		} else {
3769
9338d0785277 util-src/Makefile, util-src/encodings.c{,pp}: Port ICU code to C, rename encodings.cpp back to .c and amend the Makefile accordingly
Matthew Wild <mwild1@gmail.com>
parents: 3764
diff changeset
   429
			lua_pushnil(L);
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   430
		}
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   431
3762
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
   432
		return 1;
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
   433
	}
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
   434
}
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
   435
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   436
static int Lidna_to_unicode(lua_State* L) {	/** idna.to_unicode(s) */
3762
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
   437
	size_t len;
3769
9338d0785277 util-src/Makefile, util-src/encodings.c{,pp}: Port ICU code to C, rename encodings.cpp back to .c and amend the Makefile accordingly
Matthew Wild <mwild1@gmail.com>
parents: 3764
diff changeset
   438
	int32_t ulen, dest_len, output_len;
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   439
	const char* s = luaL_checklstring(L, 1, &len);
4271
18d888c8c12d util.encodings: Fix idna.to_unicode
Paul Aurich <paul@darkrain42.org>
parents: 3965
diff changeset
   440
	UChar ustr[1024];
3762
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
   441
	UErrorCode err = U_ZERO_ERROR;
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
   442
	UChar dest[1024];
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
   443
	char output[1024];
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
   444
3769
9338d0785277 util-src/Makefile, util-src/encodings.c{,pp}: Port ICU code to C, rename encodings.cpp back to .c and amend the Makefile accordingly
Matthew Wild <mwild1@gmail.com>
parents: 3764
diff changeset
   445
	u_strFromUTF8(ustr, 1024, &ulen, s, len, &err);
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   446
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   447
	if(U_FAILURE(err)) {
4273
7f789266b741 util.encodings: Check return values before proceeding
Paul Aurich <paul@darkrain42.org>
parents: 4272
diff changeset
   448
		lua_pushnil(L);
7f789266b741 util.encodings: Check return values before proceeding
Paul Aurich <paul@darkrain42.org>
parents: 4272
diff changeset
   449
		return 1;
7f789266b741 util.encodings: Check return values before proceeding
Paul Aurich <paul@darkrain42.org>
parents: 4272
diff changeset
   450
	}
7f789266b741 util.encodings: Check return values before proceeding
Paul Aurich <paul@darkrain42.org>
parents: 4272
diff changeset
   451
3769
9338d0785277 util-src/Makefile, util-src/encodings.c{,pp}: Port ICU code to C, rename encodings.cpp back to .c and amend the Makefile accordingly
Matthew Wild <mwild1@gmail.com>
parents: 3764
diff changeset
   452
	dest_len = uidna_IDNToUnicode(ustr, ulen, dest, 1024, UIDNA_USE_STD3_RULES, NULL, &err);
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   453
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   454
	if(U_FAILURE(err)) {
3762
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
   455
		lua_pushnil(L);
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
   456
		return 1;
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
   457
	} else {
3769
9338d0785277 util-src/Makefile, util-src/encodings.c{,pp}: Port ICU code to C, rename encodings.cpp back to .c and amend the Makefile accordingly
Matthew Wild <mwild1@gmail.com>
parents: 3764
diff changeset
   458
		u_strToUTF8(output, 1024, &output_len, dest, dest_len, &err);
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   459
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   460
		if(U_SUCCESS(err) && output_len < 1024) {
3769
9338d0785277 util-src/Makefile, util-src/encodings.c{,pp}: Port ICU code to C, rename encodings.cpp back to .c and amend the Makefile accordingly
Matthew Wild <mwild1@gmail.com>
parents: 3764
diff changeset
   461
			lua_pushlstring(L, output, output_len);
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   462
		} else {
3769
9338d0785277 util-src/Makefile, util-src/encodings.c{,pp}: Port ICU code to C, rename encodings.cpp back to .c and amend the Makefile accordingly
Matthew Wild <mwild1@gmail.com>
parents: 3764
diff changeset
   463
			lua_pushnil(L);
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   464
		}
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   465
3762
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
   466
		return 1;
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
   467
	}
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
   468
}
4272
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
   469
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
   470
#else /* USE_STRINGPREP_ICU */
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
   471
/****************** libidn ********************/
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
   472
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
   473
#include <idna.h>
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
   474
#include <idn-free.h>
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
   475
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   476
static int Lidna_to_ascii(lua_State* L) {	/** idna.to_ascii(s) */
4272
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
   477
	size_t len;
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   478
	const char* s = check_utf8(L, 1, &len);
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   479
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   480
	if(s == NULL || len != strlen(s)) {
6594
fe3018a2f187 util.encodings: Perform validation of UTF-8 strings before passing to libidn (Based on code from the utf8 library in Lua 5.3)
Kim Alvefur <zash@zash.se>
parents: 4302
diff changeset
   481
		lua_pushnil(L);
fe3018a2f187 util.encodings: Perform validation of UTF-8 strings before passing to libidn (Based on code from the utf8 library in Lua 5.3)
Kim Alvefur <zash@zash.se>
parents: 4302
diff changeset
   482
		return 1; /* TODO return error message */
fe3018a2f187 util.encodings: Perform validation of UTF-8 strings before passing to libidn (Based on code from the utf8 library in Lua 5.3)
Kim Alvefur <zash@zash.se>
parents: 4302
diff changeset
   483
	}
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   484
4272
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
   485
	char* output = NULL;
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
   486
	int ret = idna_to_ascii_8z(s, &output, IDNA_USE_STD3_ASCII_RULES);
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   487
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   488
	if(ret == IDNA_SUCCESS) {
4272
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
   489
		lua_pushstring(L, output);
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
   490
		idn_free(output);
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
   491
		return 1;
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
   492
	} else {
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
   493
		lua_pushnil(L);
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
   494
		idn_free(output);
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
   495
		return 1; /* TODO return error message */
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
   496
	}
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
   497
}
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
   498
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   499
static int Lidna_to_unicode(lua_State* L) {	/** idna.to_unicode(s) */
4272
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
   500
	size_t len;
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   501
	const char* s = luaL_checklstring(L, 1, &len);
4272
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
   502
	char* output = NULL;
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
   503
	int ret = idna_to_unicode_8z8z(s, &output, 0);
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   504
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   505
	if(ret == IDNA_SUCCESS) {
4272
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
   506
		lua_pushstring(L, output);
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
   507
		idn_free(output);
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
   508
		return 1;
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
   509
	} else {
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
   510
		lua_pushnil(L);
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
   511
		idn_free(output);
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
   512
		return 1; /* TODO return error message */
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
   513
	}
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
   514
}
3762
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
   515
#endif
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
   516
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   517
static const luaL_Reg Reg_idna[] = {
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
   518
	{ "to_ascii",	Lidna_to_ascii	},
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
   519
	{ "to_unicode",	Lidna_to_unicode	},
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
   520
	{ NULL,		NULL	}
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
   521
};
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
   522
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
   523
/***************** end *****************/
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
   524
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6611
diff changeset
   525
LUALIB_API int luaopen_util_encodings(lua_State* L) {
3762
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
   526
#ifdef USE_STRINGPREP_ICU
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
   527
	init_icu();
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
   528
#endif
6414
6c8f6364bc48 util-src/*.c: Don't create globals when loaded
Kim Alvefur <zash@zash.se>
parents: 4302
diff changeset
   529
	lua_newtable(L);
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
   530
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
   531
	lua_newtable(L);
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
   532
	luaL_register(L, NULL, Reg_base64);
6415
0e94f89d0e62 util-src/*.c: Use the more concise lua_setfield
Kim Alvefur <zash@zash.se>
parents: 6414
diff changeset
   533
	lua_setfield(L, -2, "base64");
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
   534
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
   535
	lua_newtable(L);
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
   536
	luaL_register(L, NULL, Reg_stringprep);
6415
0e94f89d0e62 util-src/*.c: Use the more concise lua_setfield
Kim Alvefur <zash@zash.se>
parents: 6414
diff changeset
   537
	lua_setfield(L, -2, "stringprep");
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
   538
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
   539
	lua_newtable(L);
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
   540
	luaL_register(L, NULL, Reg_idna);
6415
0e94f89d0e62 util-src/*.c: Use the more concise lua_setfield
Kim Alvefur <zash@zash.se>
parents: 6414
diff changeset
   541
	lua_setfield(L, -2, "idna");
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
   542
6595
141afe8a167b util.encodings: Expose UTF-8 validation and length checking functions
Kim Alvefur <zash@zash.se>
parents: 6594
diff changeset
   543
	lua_newtable(L);
141afe8a167b util.encodings: Expose UTF-8 validation and length checking functions
Kim Alvefur <zash@zash.se>
parents: 6594
diff changeset
   544
	luaL_register(L, NULL, Reg_utf8);
6607
478308ee29dd Merge 0.9->0.10 again
Kim Alvefur <zash@zash.se>
parents: 6416 6595
diff changeset
   545
	lua_setfield(L, -2, "utf8");
6595
141afe8a167b util.encodings: Expose UTF-8 validation and length checking functions
Kim Alvefur <zash@zash.se>
parents: 6594
diff changeset
   546
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
   547
	lua_pushliteral(L, "-3.14");
6415
0e94f89d0e62 util-src/*.c: Use the more concise lua_setfield
Kim Alvefur <zash@zash.se>
parents: 6414
diff changeset
   548
	lua_setfield(L, -2, "version");
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
   549
	return 1;
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
   550
}