libclamav/entconv.c
3506ac49
 /*
  *  HTML Entity & Encoding normalization.
  *
2023340a
  *  Copyright (C) 2007-2008 Sourcefire, Inc.
  *
  *  Authors: Török Edvin
3506ac49
  *
  *  This program is free software; you can redistribute it and/or modify
2023340a
  *  it under the terms of the GNU General Public License version 2 as
38a00199
  *  published by the Free Software Foundation.
3506ac49
  *
  *  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.
  *
  *  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., 51 Franklin Street, Fifth Floor, Boston,
  *  MA 02110-1301, USA.
  */
 #include "clamav-config.h"
 
 
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <ctype.h>
 #include <errno.h>
 
c8184020
 
 #ifdef CL_THREAD_SAFE
 #include <pthread.h>
 #endif
 
4e1127c5
 #include <assert.h>
 
3506ac49
 #include "clamav.h"
 #include "others.h"
 #include "htmlnorm.h"
 #include "hashtab.h"
 #include "entconv.h"
 #include "entitylist.h"
e98f12a2
 #include "cltypes.h"
3506ac49
 
6eac0e06
 #ifdef HAVE_ICONV
3506ac49
 #include <iconv.h>
 #endif
6eac0e06
 
c8184020
 #include "encoding_aliases.h"
 
4e1127c5
 #define MODULE_NAME "entconv: "
3506ac49
 
 #define MAX_LINE 1024
 
473b954b
 #ifndef EILSEQ
 #define EILSEQ 84
 #endif
3506ac49
 
b0b8398b
 #ifndef HAVE_ICONV
 typedef struct {
 	enum encodings encoding;
 	size_t size;
 } * iconv_t;
 #endif
 
b8a505ee
 static unsigned char tohex[] = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
 
b0b8398b
 /* TODO: gcc refuses to inline because it consider call unlikely and code size grows */
 static inline unsigned char* u16_normalize(uint16_t u16, unsigned char* out, const ssize_t limit)
3506ac49
 {
b0b8398b
 	assert(limit > 0 && "u16_normalize must be called with positive limit");
 	/* \0 is just ignored */
b8a505ee
 	if(!u16) {
 		return out;
 	}
 
 	if(u16 < 0xff) {
b0b8398b
 		assert((uint8_t)u16 != 0);
 		*out++ = (uint8_t)u16;
b8a505ee
 	} else {
 		size_t i;
b0b8398b
 		/* normalize only >255 to speed up */
b8a505ee
 		if(limit <=  8) {
b0b8398b
 			/* not enough space available */
 			return NULL;
 		}
 		/* inline version of
b8a505ee
 		 * out += snprintf(out, max_num_length, "&#x%x;", u16) */
 		out[0] = '&';
 		out[1] = '#';
 		out[2] = 'x';
 		out[7] = ';';
 		for(i=6; i >= 3; --i) {
 			out[i] = tohex[u16 & 0xf];
 			u16 >>= 4;
3506ac49
 		}
b8a505ee
 		out += 8;
b0b8398b
 	}
 	return out;
 }
 
8b22c9b5
 /* buffer must be at least 2 bytes in size */
 unsigned char* u16_normalize_tobuffer(uint16_t u16, unsigned char* dst, size_t dst_size)
 {
 	unsigned char* out = u16_normalize(u16, dst, dst_size-1);
 	if(out) {
 		*out++ = '\0';
 		return out;
 	}
 	return NULL;
 }
 
b0b8398b
 const char* entity_norm(struct entity_conv* conv,const unsigned char* entity)
 {
cc447ac8
 	struct cli_element* e = cli_hashtab_find(&entities_htable, (const char*)entity, strlen((const char*)entity));
b0b8398b
 	if(e && e->key) {
8b22c9b5
 		unsigned char* out = u16_normalize(e->data, conv->entity_buff, sizeof(conv->entity_buff)-1);
b0b8398b
 		if(out) {
 			*out++ = '\0';
8b22c9b5
 			return (const char*)conv->entity_buff;
3506ac49
 		}
 	}
b0b8398b
 	return NULL;
3506ac49
 }
b0b8398b
 
b3fc7f97
 #ifndef HAVE_ICONV
b0b8398b
 static size_t encoding_bytes(const char* fromcode, enum encodings* encoding)
3506ac49
 {
 	/* special case for these unusual byteorders */
cc447ac8
 	struct cli_element * e = cli_hashtab_find(&aliases_htable,fromcode,strlen(fromcode));
4e1127c5
 	if(e && e->key) {
 		*encoding = e->data;
 	} else {
 		*encoding = E_OTHER;
3506ac49
 	}
 
c8184020
 	switch(*encoding) {
3506ac49
 		case E_UCS4:
 		case E_UCS4_1234:
 		case E_UCS4_4321:
4e1127c5
 		case E_UCS4_2143:
3506ac49
 		case E_UCS4_3412:
c8184020
 			return 4;
3506ac49
 		case E_UTF16:
 		case E_UTF16_BE:
 		case E_UTF16_LE:
c8184020
 			return 2;
3506ac49
 		case E_UTF8:
 		case E_UNKNOWN:
 		case E_OTHER:
 		default:
c8184020
 			return 1;
3506ac49
 	}
4e1127c5
 }
c8184020
 
 static iconv_t iconv_open(const char *tocode, const char* fromcode)
 {
 	iconv_t iconv = cli_malloc(sizeof(*iconv));
 	if(!iconv)
 		return NULL;
4e1127c5
 	cli_dbgmsg(MODULE_NAME "Internal iconv\n");
c8184020
 	/* TODO: check that tocode is UTF16BE */
 	iconv->size = encoding_bytes(fromcode,&iconv->encoding);
3506ac49
 	return iconv;
 }
 
 static int iconv_close(iconv_t cd)
 {
 	if(cd)
 		free(cd);
 	return 0;
 }
 
 static int iconv(iconv_t iconv_struct,char **inbuf, size_t *inbytesleft,
 		char** outbuf, size_t *outbytesleft)
 {
1d23b7de
 	const uint8_t* input;
 	uint8_t* output;
 	size_t maxcopy, i;
 	if(!inbuf || !outbuf) {
 		return 0;
 	}
 	maxcopy = (*inbytesleft > *outbytesleft ? *outbytesleft  : *inbytesleft) & ~(iconv_struct->size - 1);
 	input = (const uint8_t*)*inbuf;
 	output = (uint8_t*)*outbuf;
3506ac49
 
 	/*,maxcopy is aligned to data size */
 	/* output is always utf16be !*/
 	switch(iconv_struct->encoding) {
 		case E_UCS4:
4e1127c5
 		case E_UCS4_1234:
3506ac49
 			{
 				for(i=0;i < maxcopy; i += 4) {
 					if(!input[i+2] && !input[i+3]) {
 						output[i/2] = input[i+1]; /* is compiler smart enough to replace /2, with >>1 ? */
 						output[i/2+1] = input[i];
 					}
 					else {
4e1127c5
 						cli_dbgmsg(MODULE_NAME "Warning: unicode character out of utf16 range!\n");
3506ac49
 						output[i/2] = 0xff;
 						output[i/2+1] = 0xff;
 					}
 				}
 				break;
 			}
 		case E_UCS4_4321:
 			{
 				const uint16_t *in = (const uint16_t*)input;/*UCS4_4321, and UTF16_BE have same endianness, no need for byteswap here*/
 				uint16_t *out = (uint16_t*)output;
 				for(i=0;i<maxcopy/2; i+=2) {
 					if(!in[i]) {
 						out[i/2] = in[i+1];
 					}
 					else {
 						out[i/2] = 0xffff;
 					}
 				}
 				break;
 			}
b07ca9d9
 		case E_UCS4_2143: 
3506ac49
 			{
 				const uint16_t *in = (const uint16_t*)input;
 				uint16_t* out = (uint16_t*)output;
 				for(i=0;i<maxcopy/2;i+=2) {
 					if(!in[i+1])
 						out[i/2] = in[i];
 					else
 						out[i/2] = 0xffff;
 				}
 				break;
 			}
 		case E_UCS4_3412:
 			{
 				for(i=0;i < maxcopy;i += 4) {
 					if(!input[i] && !input[i+1]) {
 						output[i/2] = input[i+3];
 						output[i/2+1] = input[i+2];
 					}
 					else {
 						output[i/2] = 0xff;
 						output[i/2+1] = 0xff;
 					}
 				}
 				break;
 			}
 		case E_UTF16:
 		case E_UTF16_LE:
 			{
 				for(i=0;i < maxcopy;i += 2) {
473b954b
 					output[i] = input[i+1];
 					output[i+1] = input[i];
3506ac49
 				}
 				break;
 			}
 		case E_UTF16_BE:
473b954b
 			memcpy(output,input,maxcopy);
3506ac49
 			break;
 		case E_UNKNOWN:
 		case E_OTHER:
 			{
 				const size_t max_copy = *inbytesleft > (*outbytesleft/2) ? (*outbytesleft/2) : *inbytesleft;
 				for(i=0;i<max_copy;i++) {
 					output[i*2]   = 0;
 					output[i*2+1] = input[i];
 				}
 				*outbytesleft -= max_copy*2;
 				*inbytesleft  -= max_copy;
 				*inbuf += max_copy;
 				*outbuf += max_copy*2;
 				if(*inbytesleft)
 					return E2BIG;
 				return 0;
 			}
 		case E_UTF8:
 			{
 				const size_t maxread  = *inbytesleft;
 				const size_t maxwrite = *outbytesleft;
 				size_t j;
 				for(i=0,j=0 ; i < maxread && j < maxwrite;) {
 					if(input[i] < 0x7F)  {
00e7d3b4
 						output[j++] = 0;
 						output[j++] = input[i++];
3506ac49
 							}
 					else if( (input[i]&0xE0) == 0xC0 ) {
 						if ((input[i+1]&0xC0) == 0x80) {
 							/* 2 bytes long 110yyyyy zzzzzzzz -> 00000yyy yyzzzzzz*/
00e7d3b4
 							output[j++] = ((input[i] & 0x1F) >> 2) & 0x07;
3506ac49
 							output[j++] = ((input[i] & 0x1F) << 6) | (input[i+1] & 0x3F);
 						}
00e7d3b4
 						else {
4e1127c5
 							cli_dbgmsg(MODULE_NAME "invalid UTF8 character encountered\n");
00e7d3b4
 							break;
 						}
3506ac49
 						i+=2;
 					}
 					else if( (input[i]&0xE0) == 0xE0) {
 						if( (input[i+1]&0xC0) == 0x80 && (input[i+2]&0xC0) == 0x80) {
 							/* 3 bytes long 1110xxxx 10yyyyyy 10zzzzzzzz -> xxxxyyyy yyzzzzzz*/
00e7d3b4
 							output[j++] = (input[i] << 4) | ((input[i+1] >> 2) & 0x0F);
 							output[j++] = (input[i+1] << 6) | (input[i+2] & 0x3F);
3506ac49
 						}
00e7d3b4
 						else {
4e1127c5
 							cli_dbgmsg(MODULE_NAME "invalid UTF8 character encountered\n");
00e7d3b4
 							break;
 						}
3506ac49
 						i+=3;
 					}
 					else if( (input[i]&0xF8) == 0xF0) {
 						if((input[i+1]&0xC0) == 0x80 && (input[i+2]&0xC0) == 0x80 && (input[i+3]&0xC0) == 0x80) {
 							/* 4 bytes long 11110www 10xxxxxx 10yyyyyy 10zzzzzz -> 000wwwxx xxxxyyyy yyzzzzzz*/
1405207a
 							cli_dbgmsg(MODULE_NAME "UTF8 character out of UTF16 range encountered\n");
00e7d3b4
 							output[j++] = 0xff;
 							output[j++] = 0xff;
 
 							/*out[j++] = ((input[i] & 0x07) << 2) | ((input[i+1] >> 4) & 0x3);
 							out[j++] = (input[i+1] << 4) | ((input[i+2] >> 2) & 0x0F);
 							out[j++] = (input[i+2] << 6) | (input[i+2] & 0x3F);*/
3506ac49
 						}
00e7d3b4
 						else {
4e1127c5
 							cli_dbgmsg(MODULE_NAME "invalid UTF8 character encountered\n");
00e7d3b4
 							break;
 						}
3506ac49
 						i+=4;
 					}
 					else {
4e1127c5
 						cli_dbgmsg(MODULE_NAME "invalid UTF8 character encountered\n");
00e7d3b4
 						break;
3506ac49
 					}							
 				}
 				*inbytesleft -= i;
 				*outbytesleft -= j;
 				*inbuf += i;
 				*outbuf += j;
00e7d3b4
 				if(*inbytesleft && *outbytesleft) {
 					errno = EILSEQ;/* we had an early exit */
 					return -1;
 				}
 				if(*inbytesleft) {
 					errno = E2BIG;
 					return -1;
 				}
3506ac49
 				return 0;
 			}
 	}
 	
 	*outbytesleft -= maxcopy;
 	*inbytesleft  -= maxcopy;
 	*inbuf += maxcopy;
 	*outbuf += maxcopy;
00e7d3b4
 	if(*inbytesleft) {
 		errno = E2BIG;
 		return -1;
 	}
3506ac49
 	return  0;
 }
 
c8184020
 #else
 
 
 
3506ac49
 #endif
 
b3fc7f97
 static inline const char* detect_encoding(const unsigned char* bom, uint8_t* bom_found, uint8_t* enc_width)
3506ac49
 {
baedf04d
 	const char* encoding = NULL;
3506ac49
 	int has_bom = 0;
b3fc7f97
 	uint8_t enc_bytes = 1; /* default is UTF8, which has a minimum of 1 bytes */
4e1127c5
 	/* undecided 32-bit encodings are treated as ucs4, and
 	 * 16 bit as utf16*/
3506ac49
 	switch(bom[0]) {
 		case 0x00:
 			if(bom[1] == 0x00) {
 				if(bom[2] == 0xFE && bom[3] == 0xFF) {
 					encoding = UCS4_1234;/* UCS-4 big-endian*/
 					has_bom = 1;
b3fc7f97
 					enc_bytes = 4;
3506ac49
 				}
 				else if(bom[2] == 0xFF && bom[3] == 0xFE) {
 					encoding = UCS4_2143;/* UCS-4 unusual order 2143 */
 					has_bom = 1;
b3fc7f97
 					enc_bytes = 4;
3506ac49
 				}
 				else if(bom[2] == 0x00 && bom[3] == 0x3C) {
4e1127c5
 					/* undecided, treat as ucs4 */
 					encoding = UCS4_1234;
b3fc7f97
 					enc_bytes = 4;
4e1127c5
 				}
3506ac49
 				else if(bom[2] == 0x3C && bom[3] == 0x00) {
4e1127c5
 					encoding = UCS4_2143;
b3fc7f97
 					enc_bytes = 4;
3506ac49
 				}
 			}/* 0x00 0x00 */
 			else if(bom[1] == 0x3C) {
 				if(bom[2] == 0x00) {
 					if(bom[3] == 0x00) {
4e1127c5
 						encoding = UCS4_3412;
b3fc7f97
 						enc_bytes = 4;
3506ac49
 					}
 					else if(bom[3] == 0x3F) {
4e1127c5
 						encoding = UTF16_BE;
3506ac49
 						enc_bytes = 2;
 					}
 				}/*0x00 0x3C 0x00*/
 			}/*0x00 0x3C*/
 			break;
 		case 0xFF:
 			if(bom[1] == 0xFE) {
 				if(bom[2] == 0x00 && bom[3] == 0x00) {
 					encoding = UCS4_4321;
b3fc7f97
 					enc_bytes = 4;
3506ac49
 					has_bom = 1;
 				}
 				else {
 					encoding = UTF16_LE;
 					has_bom = 1;
 					enc_bytes = 2;
 				}
 			}/*0xFF 0xFE*/
 			break;
4e1127c5
 		case 0xFE:
3506ac49
 			if(bom[1] == 0xFF) {
 					if(bom[2] == 0x00 && bom[3] == 0x00) {
 						encoding = UCS4_3412;
b3fc7f97
 						enc_bytes = 4;
3506ac49
 						has_bom = 1;
 					}
 					else {
 						encoding = UTF16_BE;
 						has_bom = 1;
 						enc_bytes = 2;
4e1127c5
 					}
3506ac49
 			}/*0xFE 0xFF*/
 			break;
4e1127c5
 		case 0xEF:
3506ac49
 			if(bom[1] == 0xBB && bom[2] == 0xBF)  {
 					encoding = UTF8;
 					has_bom = 1;
 					/*enc_bytes = 4;- default, maximum 4 bytes*/
4e1127c5
 			}/*0xEF 0xBB 0xBF*/
3506ac49
 			break;
4e1127c5
 		case 0x3C:
3506ac49
 				if(bom[1] == 0x00) {
 					if(bom[2] == 0x00 && bom[3] == 0x00) {
4e1127c5
 						encoding = UCS4_4321;
b3fc7f97
 						enc_bytes = 4;
3506ac49
 					}
 					else if(bom[2] == 0x3F && bom[3] == 0x00) {
4e1127c5
 						encoding = UTF16_LE;
3506ac49
 						enc_bytes = 2;
 					}
 				}/*0x3C 0x00*/
 				else if(bom[1] == 0x3F && bom[2] == 0x78 && bom[3]==0x6D) {
4e1127c5
 					encoding = NULL;
3506ac49
 					enc_bytes = 1;
 				}/*0x3C 3F 78 6D*/
 				break;
4e1127c5
 		case 0x4C:
3506ac49
 				if(bom[1] == 0x6F && bom[2] == 0xA7 && bom[3] == 0x94) {
4e1127c5
 					cli_dbgmsg(MODULE_NAME "EBCDIC encoding is not supported in line mode\n");
 					encoding = NULL;
3506ac49
 					enc_bytes = 1;
 				}/*4C 6F A7 94*/
 				break;
 	}/*switch*/
b3fc7f97
 	*enc_width = enc_bytes;
 	*bom_found = has_bom;
 	return encoding;
 }
 
 /* detects UTF-16(LE/BE), UCS-4(all 4 variants).
  * UTF-8 and simple ASCII are ignored, because we can process those as text */
baedf04d
 const char* encoding_detect_bom(const unsigned char* bom, const size_t length)
b3fc7f97
 {
 	uint8_t has_bom;
 	uint8_t enc_width;
baedf04d
 	const char* encoding;
 
 	if(length < 4) {
 		return NULL;
 	}
 	encoding = detect_encoding(bom, &has_bom, &enc_width);
b3fc7f97
 	return enc_width > 1 ? encoding : NULL;
3506ac49
 }
 
cd75ab40
 /*()-./0123456789:ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz*/
4e1127c5
 static const uint8_t encname_chars[256] = {
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1,
cd75ab40
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
4e1127c5
         0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
cd75ab40
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1,
4e1127c5
         0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
cd75ab40
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
4e1127c5
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
 };
 
 /* checks that encoding is sane, and normalizes to uppercase */
b0b8398b
 static char* normalize_encoding(const unsigned char* enc)
3506ac49
 {
b0b8398b
 	char* norm;
4e1127c5
 	size_t i, len;
 
 	if(!enc)
 		return NULL;
 	len = strlen((const char*)enc);
 	if(len > 32)
 		return NULL;
 	for(i=0;i<len;i++) {
 		if(!encname_chars[enc[i]])
 			return NULL;
 	}
 	norm = cli_malloc( len+1 );
3506ac49
 	if(!norm)
 		return NULL;
b0b8398b
 	for(i=0;i < len; i++)
3506ac49
 		norm[i] = toupper(enc[i]);
 	norm[len]='\0';
 	return norm;
 }
 
c8184020
 /* sarge leaks on iconv_open/iconv_close, so lets not open/close so many times,
  * just keep on each thread its own pool of iconvs*/
 
 struct iconv_cache {
 	iconv_t* tab;
 	size_t     len;
 	size_t   last;
cc447ac8
 	struct   cli_hashtable hashtab;
c8184020
 };
 
 static void iconv_cache_init(struct iconv_cache* cache)
 {
 /*	cache->tab = NULL;
 	cache->len = 0;
 	cache->used = 0; - already done by memset*/
4e1127c5
 	cli_dbgmsg(MODULE_NAME "Initializing iconv pool:%p\n",(void*)cache);
cc447ac8
 	cli_hashtab_init(&cache->hashtab, 32);
c8184020
 }
 
 static void iconv_cache_destroy(struct iconv_cache* cache)
 {
 	size_t i;
4e1127c5
 	cli_dbgmsg(MODULE_NAME "Destroying iconv pool:%p\n",(void*)cache);
c8184020
 	for(i=0;i < cache->last;i++) {
4e1127c5
 		cli_dbgmsg(MODULE_NAME "closing iconv:%p\n",cache->tab[i]);
c8184020
 		iconv_close(cache->tab[i]);
 	}
cc447ac8
 	cli_hashtab_clear(&cache->hashtab);
c8184020
 	free(cache->hashtab.htable);
 	free(cache->tab);
 	free(cache);
 }
 
 
 #ifdef CL_THREAD_SAFE
 static pthread_key_t iconv_pool_tls_key;
 static pthread_once_t iconv_pool_tls_key_once = PTHREAD_ONCE_INIT;
 
 /* destructor called for all threads that exit via pthread_exit, or cancellation. Unfortunately that doesn't include
  * the main thread, so we have to call this manually for the main thread.*/
 
 static int cache_atexit_registered = 0;
 
 static void iconv_pool_tls_instance_destroy(void* ptr)
 {
 	if(ptr) {
 		iconv_cache_destroy(ptr);
 	}
 }
 
 static void iconv_cache_cleanup_main(void)
 {
 	struct iconv_cache* cache = pthread_getspecific(iconv_pool_tls_key);
 	if(cache) {
 		iconv_pool_tls_instance_destroy(cache);
 		pthread_setspecific(iconv_pool_tls_key,NULL);
 	}
 	pthread_key_delete(iconv_pool_tls_key);
 }
 
 static void iconv_pool_tls_key_alloc(void)
 {
 	pthread_key_create(&iconv_pool_tls_key, iconv_pool_tls_instance_destroy);
 	if(!cache_atexit_registered) {
4e1127c5
 		cli_dbgmsg(MODULE_NAME "iconv:registering atexit\n");
c8184020
 		if(atexit(iconv_cache_cleanup_main)) {
4e1127c5
 			cli_dbgmsg(MODULE_NAME "failed to register atexit\n");
c8184020
 		}
 		cache_atexit_registered = 1;
 	}
 }
 
 static void init_iconv_pool_ifneeded(void)
 {
 	pthread_once(&iconv_pool_tls_key_once, iconv_pool_tls_key_alloc);
 }
 
 static inline struct iconv_cache* cache_get_tls_instance(void)
 {
 	struct iconv_cache* cache = pthread_getspecific(iconv_pool_tls_key);
 	if(!cache) {
 		cache = cli_calloc(1,sizeof(*cache));
 		if(!cache) {
4e1127c5
 			cli_dbgmsg(MODULE_NAME "!Out of memory allocating TLS iconv instance\n");
c8184020
 			return NULL;
 		}
 		iconv_cache_init(cache);
 		pthread_setspecific(iconv_pool_tls_key, cache);
 	}
 	return cache;
 }
 
 #else
 
 static struct iconv_cache* global_iconv_cache = NULL;
 static int    iconv_global_inited = 0;
 
 
 static void iconv_cache_cleanup_main(void)
 {
 	iconv_cache_destroy(global_iconv_cache);
 }
 
 static inline void init_iconv_pool_ifneeded() 
 {
 	if(!iconv_global_inited) {
 		global_iconv_cache = cli_calloc(1,sizeof(*global_iconv_cache));
 		if(global_iconv_cache) {
 			iconv_cache_init(global_iconv_cache);
 			atexit(iconv_cache_cleanup_main);
 			iconv_global_inited = 1;
 		}
 	}
 }
 
 
 static inline struct iconv_cache* cache_get_tls_instance(void)
 {
 	return global_iconv_cache;
 }
 
 #endif
 
b0b8398b
 static iconv_t iconv_open_cached(const char* fromcode)
c8184020
 {
 	struct iconv_cache * cache;
 	size_t idx;
 	const size_t fromcode_len = strlen((const char*)fromcode);
cc447ac8
 	struct cli_element * e;
0134b0e9
 	iconv_t  iconv_struct;
c8184020
 
 	init_iconv_pool_ifneeded();
 	cache = cache_get_tls_instance();/* gets TLS iconv pool */
 	if(!cache) {
4e1127c5
 		cli_dbgmsg(MODULE_NAME "!Unable to get TLS iconv cache!\n");
c8184020
 		errno = EINVAL;
 		return (iconv_t)-1;
 	}
 
cc447ac8
 	e = cli_hashtab_find(&cache->hashtab, fromcode, fromcode_len);
c8184020
 	if(e && (e->data < 0 || (size_t)e->data > cache->len)) {
 		e = NULL;
 	}
 	if(e) {
cd75ab40
 		size_t dummy_in, dummy_out;
 		/* reset state */
 		iconv(cache->tab[e->data], NULL, &dummy_in, NULL, &dummy_out);
c8184020
 		return cache->tab[e->data];
 	}
4e1127c5
 	cli_dbgmsg(MODULE_NAME "iconv not found in cache, for encoding:%s\n",fromcode);
0134b0e9
 	iconv_struct = iconv_open("UTF-16BE",(const char*)fromcode);
 	if(iconv_struct != (iconv_t)-1) {
b3fc7f97
 		idx = cache->last++;
 		if(idx >= cache->len) {
 			cache->len += 16;
 			cache->tab = cli_realloc2(cache->tab, cache->len*sizeof(cache->tab[0]));
 			if(!cache->tab) {
 				cli_dbgmsg(MODULE_NAME "!Out of mem in iconv-pool\n");
 				errno = ENOMEM;
 				return (iconv_t)-1;
 			}
c8184020
 		}
 
cc447ac8
 		cli_hashtab_insert(&cache->hashtab, fromcode, fromcode_len, idx);
0134b0e9
 		cache->tab[idx] = iconv_struct;
b3fc7f97
 		cli_dbgmsg(MODULE_NAME "iconv_open(),for:%s -> %p\n",fromcode,(void*)cache->tab[idx]);
 		return cache->tab[idx];
4e1127c5
 	}
b3fc7f97
 	return (iconv_t)-1;
4e1127c5
 }
 
b3fc7f97
 static int in_iconv_u16(const m_area_t* in_m_area, iconv_t* iconv_struct, m_area_t* out_m_area)
4e1127c5
 {
 	char   tmp4[4];
 	size_t inleft = in_m_area->length - in_m_area->offset;
 	size_t rc, alignfix;
 	char*  input   = (char*)in_m_area->buffer + in_m_area->offset;
b3fc7f97
 	size_t outleft = out_m_area->length > 0 ? out_m_area->length : 0;
4e1127c5
 	char* out      = (char*)out_m_area->buffer;
e68d70e7
 	char err[128];
4e1127c5
 
b3fc7f97
 	out_m_area->offset = 0;
b0b8398b
 	if(!inleft) {
f2895a0d
 		return 0;
b0b8398b
 	}
4e1127c5
 	/* convert encoding conv->tmp_area. conv->out_area */
 	alignfix = inleft%4;/* iconv gives an error if we give him 3 bytes to convert, 
 			       and we are using ucs4, ditto for utf16, and 1 byte*/
 	inleft -= alignfix;
 
 	if(!inleft && alignfix) {
 		/* EOF, and we have less than 4 bytes to convert */
 		memset(tmp4, 0, 4);
 		memcpy(tmp4, input, alignfix);
 		input = tmp4;
 		inleft = 4;
cd75ab40
 		alignfix = 0;
4e1127c5
 	}
 
f2895a0d
 	while (inleft && (outleft >= 2)) { /* iconv doesn't like inleft to be 0 */
 		const size_t outleft_last = outleft;
4e1127c5
 		assert(*iconv_struct != (iconv_t)-1);
b3fc7f97
 		rc = iconv(*iconv_struct, &input,  &inleft, &out, &outleft);
f2895a0d
 		if(rc == (size_t)-1) {
 			if(errno == E2BIG) {
 				/* not enough space in output buffer */
 				break;
 			}
0090efc0
 			/*cli_dbgmsg(MODULE_NAME "iconv error:%s\n", cli_strerror(errno, err, sizeof(err)));*/
f2895a0d
 		} else if(outleft == outleft_last) {
 			cli_dbgmsg(MODULE_NAME "iconv stall (no output)\n");
 		} else {
 			/* everything ok */
 			continue;
4e1127c5
 		}
0090efc0
 		/*cli_dbgmsg(MODULE_NAME "resuming (inleft:%lu, outleft:%lu, inpos:%ld, %ld)\n",
f2895a0d
 					inleft, outleft, input - (char*)in_m_area->buffer,
0090efc0
 					out - (char*)out_m_area->buffer);*/
f2895a0d
 		/* output raw byte, and resume at next byte */
 		if(outleft < 2) break;
 		outleft -= 2;
 		*out++ = 0;
 		*out++ = *input++;
 		inleft--;
4e1127c5
 	}
b3fc7f97
 	cli_dbgmsg("in_iconv_u16: unprocessed bytes: %lu\n", (unsigned long)inleft);
4e1127c5
 	if(out_m_area->length >= 0 && out_m_area->length >= (off_t)outleft) {
 		out_m_area->length -= (off_t)outleft;
 	} else {
 		cli_dbgmsg(MODULE_NAME "outleft overflown, ignoring\n");
 		out_m_area->length = 0;
 	}
 	out_m_area->offset  = 0;
 	return 0;
 }
 
b3fc7f97
 int encoding_normalize_toascii(const m_area_t* in_m_area, const char* initial_encoding, m_area_t* out_m_area)
4e1127c5
 {
b3fc7f97
 	iconv_t iconv_struct;
 	off_t i, j;
 	char *encoding;
4e1127c5
 
b3fc7f97
 	if(!initial_encoding || !in_m_area || !out_m_area) {
 		return CL_ENULLARG;
4e1127c5
 	}
 
b3fc7f97
 	encoding = normalize_encoding((const unsigned char*)initial_encoding);
 	if(!encoding) {
 		cli_dbgmsg(MODULE_NAME "encoding name is not valid, ignoring\n");
 		return -1;
4e1127c5
 	}
 
b3fc7f97
 	cli_dbgmsg(MODULE_NAME "Encoding %s\n", encoding);
 	iconv_struct = iconv_open_cached( encoding );
 	if(iconv_struct == (iconv_t)-1) {
 		cli_dbgmsg(MODULE_NAME "Encoding not accepted by iconv_open(): %s\n", encoding);
 		free(encoding);
 		return -1;
 	}
758b2755
 	free(encoding);
b3fc7f97
 	in_iconv_u16(in_m_area, &iconv_struct, out_m_area);
 	for(i = 0, j = 0; i < out_m_area->length ; i += 2) {
 		const unsigned char c = (out_m_area->buffer[i] << 4) + out_m_area->buffer[i+1];
 		if(c) {
 			out_m_area->buffer[j++] = c;
4e1127c5
 		}
 	}
b3fc7f97
 	out_m_area->length = j;
 	return 0;
4e1127c5
 }