whelk/unicode.c
changeset 14 1a62417393a2
parent 0 8525965e930d
     1.1 --- a/whelk/unicode.c	Thu Jul 09 08:52:03 2009 +0100
     1.2 +++ b/whelk/unicode.c	Tue Feb 09 00:42:09 2010 +0000
     1.3 @@ -8,11 +8,15 @@
     1.4      int n;
     1.5      char *utf8;
     1.6      n=WideCharToMultiByte(CP_UTF8,0,utf16,len,NULL,0,NULL,NULL);
     1.7 -    if (n)
     1.8 +    if (n || !len)
     1.9      {
    1.10 -	utf8=malloc(n);
    1.11 +	utf8=malloc(len<0?n:n+1);
    1.12  	if (utf8)
    1.13 +	{
    1.14  	    WideCharToMultiByte(CP_UTF8,0,utf16,len,utf8,n,NULL,NULL);
    1.15 +	    if (len>=0)
    1.16 +		utf8[n]=0;
    1.17 +	}
    1.18      }
    1.19      else
    1.20  	utf8=NULL;
    1.21 @@ -24,11 +28,15 @@
    1.22      int n;
    1.23      WCHAR *utf16;
    1.24      n=MultiByteToWideChar(CP_UTF8,0,utf8,len,NULL,0);
    1.25 -    if (n)
    1.26 +    if (n || !len)
    1.27      {
    1.28 -	utf16=malloc(n*sizeof(WCHAR));
    1.29 +	utf16=malloc((len<0?n:n+1)*sizeof(WCHAR));
    1.30  	if (utf16)
    1.31 +	{
    1.32  	    MultiByteToWideChar(CP_UTF8,0,utf8,len,utf16,n);
    1.33 +	    if (len>=0)
    1.34 +		utf16[n]=0;
    1.35 +	}
    1.36      }
    1.37      else
    1.38  	utf16=NULL;