fixed a bug in the character conversion to utf-8 function

This commit is contained in:
SJ 2012-08-01 21:53:01 +02:00
parent 8bf9fb318b
commit 7c7b30b544
3 changed files with 46 additions and 8 deletions

View File

@ -13,7 +13,7 @@
#define VERSION "0.1.19" #define VERSION "0.1.19"
#define BUILD 675 #define BUILD 677
#define HOSTID "mailarchiver" #define HOSTID "mailarchiver"

View File

@ -5,6 +5,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <strings.h>
#include <ctype.h> #include <ctype.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/types.h> #include <sys/types.h>
@ -14,6 +15,7 @@
#include <fcntl.h> #include <fcntl.h>
#include <unistd.h> #include <unistd.h>
#include <time.h> #include <time.h>
#include <iconv.h>
#include <piler.h> #include <piler.h>
#include "trans.h" #include "trans.h"
#include "html.h" #include "html.h"
@ -218,8 +220,13 @@ int extract_boundary(char *p, struct _state *state){
void fixupEncodedHeaderLine(char *buf){ void fixupEncodedHeaderLine(char *buf){
char *sb, *sq, *p, *q, *r, *s, v[SMALLBUFSIZE], puf[MAXBUFSIZE]; char *sb, *sq, *p, *q, *r, *s, *e, *start, *end;
char *start, *end; char v[SMALLBUFSIZE], puf[MAXBUFSIZE], encoding[SMALLBUFSIZE], tmpbuf[2*SMALLBUFSIZE];
iconv_t cd;
size_t size, inbytesleft, outbytesleft;
char *inbuf, *outbuf;
int need_encoding;
memset(puf, 0, sizeof(puf)); memset(puf, 0, sizeof(puf));
@ -230,6 +237,8 @@ void fixupEncodedHeaderLine(char *buf){
p = v; p = v;
memset(encoding, 0, sizeof(encoding));
do { do {
start = strstr(p, "=?"); start = strstr(p, "=?");
if(start){ if(start){
@ -240,6 +249,13 @@ void fixupEncodedHeaderLine(char *buf){
start++; start++;
e = strchr(start+2, '?');
if(e){
*e = '\0';
snprintf(encoding, sizeof(encoding)-1, "%s", start+1);
*e = '?';
}
s = NULL; s = NULL;
sb = strcasestr(start, "?B?"); if(sb) s = sb; sb = strcasestr(start, "?B?"); if(sb) s = sb;
sq = strcasestr(start, "?Q?"); if(sq) s = sq; sq = strcasestr(start, "?Q?"); if(sq) s = sq;
@ -253,8 +269,29 @@ void fixupEncodedHeaderLine(char *buf){
if(sq){ decodeQP(s+3); r = s + 3; for(; *r; r++){ if(*r == '_') *r = ' '; } } if(sq){ decodeQP(s+3); r = s + 3; for(; *r; r++){ if(*r == '_') *r = ' '; } }
/* encode everything if it's not utf-8 encoded */ /* encode everything if it's not utf-8 encoded */
if(strncasecmp(start+1, "utf-8", 5)) utf8_encode((unsigned char*)s+3); //if(strncasecmp(start+1, "utf-8", 5)) utf8_encode((unsigned char*)s+3);
//strncat(puf, s+3, sizeof(puf)-1);
size = need_encoding = 0;
if(strlen(encoding) > 2 && strcasecmp(encoding, "utf-8")){
need_encoding = 1;
cd = iconv_open("utf-8", encoding);
memset(tmpbuf, 0, sizeof(tmpbuf));
inbuf = s+3;
outbuf = &tmpbuf[0];
inbytesleft = strlen(s+3);
outbytesleft = sizeof(tmpbuf)-1;
size = iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
iconv_close(cd);
}
if(need_encoding == 1 && size >= 0)
strncat(puf, tmpbuf, sizeof(puf)-1);
else
strncat(puf, s+3, sizeof(puf)-1); strncat(puf, s+3, sizeof(puf)-1);
p = end + 2; p = end + 2;

View File

@ -346,6 +346,8 @@ class ModelSearchMessage extends Model {
$what = preg_replace("/^\=\?/", "", $what); $what = preg_replace("/^\=\?/", "", $what);
$what = preg_replace("/\?\=$/", "", $what); $what = preg_replace("/\?\=$/", "", $what);
$encoding = substr($what, 0, strpos($what, '?'));
if(preg_match("/\?Q\?/i", $what)){ if(preg_match("/\?Q\?/i", $what)){
$x = preg_replace("/^([\w\-]+)\?Q\?/i", "", $what); $x = preg_replace("/^([\w\-]+)\?Q\?/i", "", $what);
@ -360,9 +362,8 @@ class ModelSearchMessage extends Model {
$s = preg_replace('/\0/', "*", $s); $s = preg_replace('/\0/', "*", $s);
} }
if(!preg_match("/utf-8/i", $encoding)){
if(!preg_match("/utf-8/i", $what)){ $s = iconv($encoding, 'utf-8', $s);
$s = utf8_encode($s);
} }
} }