2007-12-24 14:57:53 +01:00
|
|
|
/*
|
2011-02-14 13:50:55 +01:00
|
|
|
* ejabberd, Copyright (C) 2002-2011 ProcessOne
|
2007-12-24 14:57:53 +01:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
2009-06-09 12:56:14 +02:00
|
|
|
*
|
2007-12-24 14:57:53 +01:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
|
|
|
* 02111-1307 USA
|
|
|
|
*
|
|
|
|
*/
|
2003-02-16 21:07:21 +01:00
|
|
|
|
|
|
|
#include <stdio.h>
|
2004-03-21 21:27:09 +01:00
|
|
|
#include <string.h>
|
2003-02-16 21:07:21 +01:00
|
|
|
#include <erl_driver.h>
|
|
|
|
#include <ei.h>
|
|
|
|
#include <iconv.h>
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
ErlDrvPort port;
|
|
|
|
iconv_t cd;
|
|
|
|
} iconv_data;
|
|
|
|
|
|
|
|
|
|
|
|
static ErlDrvData iconv_erl_start(ErlDrvPort port, char *buff)
|
|
|
|
{
|
|
|
|
iconv_data* d = (iconv_data*)driver_alloc(sizeof(iconv_data));
|
|
|
|
d->port = port;
|
|
|
|
d->cd = NULL;
|
|
|
|
|
|
|
|
set_port_control_flags(port, PORT_CONTROL_FLAG_BINARY);
|
|
|
|
|
|
|
|
return (ErlDrvData)d;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void iconv_erl_stop(ErlDrvData handle)
|
|
|
|
{
|
|
|
|
driver_free((char*)handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int iconv_erl_control(ErlDrvData drv_data,
|
|
|
|
unsigned int command,
|
|
|
|
char *buf, int len,
|
|
|
|
char **rbuf, int rlen)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int size;
|
|
|
|
int index = 0;
|
|
|
|
int avail;
|
2004-10-12 22:06:38 +02:00
|
|
|
size_t inleft, outleft;
|
2003-02-16 21:07:21 +01:00
|
|
|
ErlDrvBinary *b;
|
|
|
|
char *from, *to, *string, *stmp, *rstring, *rtmp;
|
|
|
|
iconv_t cd;
|
2009-06-15 20:56:52 +02:00
|
|
|
int invalid_utf8_as_latin1 = 0;
|
2003-02-16 21:07:21 +01:00
|
|
|
|
|
|
|
ei_decode_version(buf, &index, &i);
|
|
|
|
ei_decode_tuple_header(buf, &index, &i);
|
|
|
|
ei_get_type(buf, &index, &i, &size);
|
2010-07-20 07:28:22 +02:00
|
|
|
from = driver_alloc(size + 1);
|
2003-02-16 21:07:21 +01:00
|
|
|
ei_decode_string(buf, &index, from);
|
|
|
|
|
|
|
|
ei_get_type(buf, &index, &i, &size);
|
2010-07-20 07:28:22 +02:00
|
|
|
to = driver_alloc(size + 1);
|
2003-02-16 21:07:21 +01:00
|
|
|
ei_decode_string(buf, &index, to);
|
|
|
|
|
|
|
|
ei_get_type(buf, &index, &i, &size);
|
2010-07-20 07:28:22 +02:00
|
|
|
stmp = string = driver_alloc(size + 1);
|
2003-02-16 21:07:21 +01:00
|
|
|
ei_decode_string(buf, &index, string);
|
2005-12-14 02:32:48 +01:00
|
|
|
|
2009-06-15 20:56:52 +02:00
|
|
|
/* Special mode: parse as UTF-8 if possible; otherwise assume it's
|
|
|
|
Latin-1. Makes no difference when encoding. */
|
|
|
|
if (strcmp(from, "utf-8+latin-1") == 0) {
|
|
|
|
from[5] = '\0';
|
|
|
|
invalid_utf8_as_latin1 = 1;
|
|
|
|
}
|
|
|
|
if (strcmp(to, "utf-8+latin-1") == 0) {
|
|
|
|
to[5] = '\0';
|
|
|
|
}
|
2003-02-16 21:07:21 +01:00
|
|
|
cd = iconv_open(to, from);
|
2003-07-03 09:49:59 +02:00
|
|
|
|
2005-12-14 02:32:48 +01:00
|
|
|
if (cd == (iconv_t) -1) {
|
2003-07-03 09:49:59 +02:00
|
|
|
cd = iconv_open("ascii", "ascii");
|
2005-12-14 02:32:48 +01:00
|
|
|
if (cd == (iconv_t) -1) {
|
|
|
|
*rbuf = (char*)(b = driver_alloc_binary(size));
|
|
|
|
memcpy(b->orig_bytes, string, size);
|
|
|
|
|
2010-07-20 07:28:22 +02:00
|
|
|
driver_free(from);
|
|
|
|
driver_free(to);
|
|
|
|
driver_free(string);
|
2005-12-14 02:32:48 +01:00
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
2003-02-16 21:07:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
outleft = avail = 4*size;
|
|
|
|
inleft = size;
|
2010-07-20 07:28:22 +02:00
|
|
|
rtmp = rstring = driver_alloc(avail);
|
2005-12-14 02:32:48 +01:00
|
|
|
while (inleft > 0) {
|
|
|
|
if (iconv(cd, &stmp, &inleft, &rtmp, &outleft) == (size_t) -1) {
|
2009-06-15 20:56:52 +02:00
|
|
|
if (invalid_utf8_as_latin1 && (*stmp & 0x80) && outleft >= 2) {
|
|
|
|
/* Encode one byte of (assumed) Latin-1 into two bytes of UTF-8 */
|
|
|
|
*rtmp++ = 0xc0 | ((*stmp & 0xc0) >> 6);
|
|
|
|
*rtmp++ = 0x80 | (*stmp & 0x3f);
|
|
|
|
outleft -= 2;
|
|
|
|
}
|
2005-12-14 02:32:48 +01:00
|
|
|
stmp++;
|
|
|
|
inleft--;
|
|
|
|
}
|
|
|
|
}
|
2003-02-16 21:07:21 +01:00
|
|
|
|
|
|
|
size = rtmp - rstring;
|
|
|
|
|
|
|
|
*rbuf = (char*)(b = driver_alloc_binary(size));
|
|
|
|
memcpy(b->orig_bytes, rstring, size);
|
|
|
|
|
2010-07-20 07:28:22 +02:00
|
|
|
driver_free(from);
|
|
|
|
driver_free(to);
|
|
|
|
driver_free(string);
|
|
|
|
driver_free(rstring);
|
2003-02-16 21:07:21 +01:00
|
|
|
iconv_close(cd);
|
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ErlDrvEntry iconv_driver_entry = {
|
|
|
|
NULL, /* F_PTR init, N/A */
|
|
|
|
iconv_erl_start, /* L_PTR start, called when port is opened */
|
|
|
|
iconv_erl_stop, /* F_PTR stop, called when port is closed */
|
|
|
|
NULL, /* F_PTR output, called when erlang has sent */
|
|
|
|
NULL, /* F_PTR ready_input, called when input descriptor ready */
|
|
|
|
NULL, /* F_PTR ready_output, called when output descriptor ready */
|
|
|
|
"iconv_erl", /* char *driver_name, the argument to open_port */
|
|
|
|
NULL, /* F_PTR finish, called when unloaded */
|
|
|
|
NULL, /* handle */
|
|
|
|
iconv_erl_control, /* F_PTR control, port_command callback */
|
|
|
|
NULL, /* F_PTR timeout, reserved */
|
|
|
|
NULL /* F_PTR outputv, reserved */
|
|
|
|
};
|
|
|
|
|
|
|
|
DRIVER_INIT(iconv_erl) /* must match name in driver_entry */
|
|
|
|
{
|
2004-04-15 21:55:38 +02:00
|
|
|
return &iconv_driver_entry;
|
2003-02-16 21:07:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|