Changeset

504:efc5184effa1

Added function latin1toutf8 to sasl.lua, for processing non-utf8 responses
author Waqas Hussain <waqas20@gmail.com>
date Sun, 30 Nov 2008 20:58:48 +0500
parents 503:00702b66beb1
children 507:4d3ccc6b5817
files util/sasl.lua
diffstat 1 files changed, 16 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/util/sasl.lua	Sun Nov 30 14:30:22 2008 +0000
+++ b/util/sasl.lua	Sun Nov 30 20:58:48 2008 +0500
@@ -4,6 +4,8 @@
 local tostring = tostring;
 local st = require "util.stanza";
 local generate_uuid = require "util.uuid".generate;
+local t_insert, t_concat = table.insert, table.concat;
+local to_byte, to_char = string.byte, string.char;
 local s_match = string.match;
 local gmatch = string.gmatch
 local string = string
@@ -66,6 +68,20 @@
 		return data
 	end
 	
+	local function latin1toutf8(str)
+		local p = {};
+		for ch in gmatch(str, ".") do
+			ch = to_byte(ch);
+			if (ch < 0x80) then
+				t_insert(p, to_char(ch));
+			elseif (ch < 0xC0) then
+				t_insert(p, to_char(0xC2, ch));
+			else
+				t_insert(p, to_char(0xC3, ch - 64));
+			end
+		end
+		return t_concat(p);
+	end
 	local function parse(data)
 		message = {}
 		for k, v in gmatch(data, [[([%w%-]+)="?([^",]*)"?,?]]) do -- FIXME The hacky regex makes me shudder