Index: common/gsm-encoding.c =================================================================== RCS file: /cvsroot/gnokii/gnokii/common/gsm-encoding.c,v retrieving revision 1.27 diff -u -r1.27 gsm-encoding.c --- common/gsm-encoding.c 17 Sep 2002 18:29:38 -0000 1.27 +++ common/gsm-encoding.c 27 Sep 2002 10:51:59 -0000 @@ -162,12 +162,25 @@ return gsm_reverse_default_alphabet[value]; } -static wchar_t char_encode_uni_alphabet(unsigned char value) +static int char_encode_uni_alphabet(unsigned char value, wchar_t *dest) { - wchar_t retval; + int length; + + switch (length = mbtowc(dest, &value, 2)) { + case 1: + case 2: + return length; + break; + case -1: + dprintf("Error calling mctowb!\n"); + return -1; + break; + default: + dprintf("Character length (%i) not supported!\n", length); + return -1; + break; + } - if (mbtowc(&retval, &value, 1) == -1) return '?'; - else return retval; } static unsigned char char_decode_def_alphabet(unsigned char value) @@ -179,15 +192,26 @@ } } -static unsigned char char_decode_uni_alphabet(wchar_t value) +static int char_decode_uni_alphabet(wchar_t value, unsigned char *dest) { - unsigned char retval; + int length; - if (wctomb(&retval, value) == -1) return '?'; - else return retval; + switch (length = wctomb(dest, value)) { + case 1: + case 2: + return length; + break; + case -1: + dprintf("Error calling wctomb!\n"); + return -1; + break; + default: + dprintf("Character length (%i) not supported!\n", length); + return -1; + break; + } } - #define GN_BYTE_MASK ((1 << bits) - 1) int char_unpack_7bit(unsigned int offset, unsigned int in_length, unsigned int out_length, @@ -342,7 +366,7 @@ for (i = 0; i < (len / 4); i++) { buf[0] = *(src + i * 4); buf[1] = *(src + i * 4 + 1); buf[2] = *(src + i * 4 + 2); buf[3] = *(src + i * 4 + 3); - dest[i] = char_decode_uni_alphabet(strtol(buf, NULL, 16)); + char_decode_uni_alphabet(strtol(buf, NULL, 16), &dest[i]); } return; } @@ -350,37 +374,45 @@ void char_encode_ucs2(unsigned char* dest, const unsigned char* src, int len) { int i; + wchar_t wc; for (i = 0; i < (len / 4); i++) { - sprintf(dest + i * 4, "%lx", char_encode_uni_alphabet(src[i])); + char_encode_uni_alphabet(src[i], &wc); + sprintf(dest + i * 4, "%lx", wc); } return; } -void char_decode_unicode(unsigned char* dest, const unsigned char* src, int len) +int char_decode_unicode(unsigned char* dest, const unsigned char* src, int len) { - int i; - wchar_t wc; + int i, length = 0, pos = 0; - for (i = 0; i < len; i++) { - wc = src[(2 * i) + 1] | (src[2 * i] << 8); - dest[i] = char_decode_uni_alphabet(wc); + for (i = 0; i < len / 2; i++) { + length = wctomb(dest, (src[i * 2] << 8) | src[(i * 2) + 1]); + dest += length; + pos += length; } - dest[len] = 0; - return; + return pos; } -void char_encode_unicode(unsigned char* dest, const unsigned char* src, int len) +int char_encode_unicode(unsigned char* dest, const unsigned char* src, int len) { - int i; - wchar_t wc; + int i, length, offset = 0, pos = 0; + wchar_t wc; - for (i = 0; i < len; i++) { - wc = char_decode_uni_alphabet(src[i]); - dest[i*2] = (wc >> 8) & 0xff; - dest[(i*2)+1] = wc & 0xff; - } - return; + for (i = 0; i < len; i++) { + switch (length = mbtowc (&wc, &src[offset], 2)){ + case 1: /* ASCII char */ + case 2: /* multi char */ + dest[pos++] = wc >> 8 & 0xFF; + dest[pos++] = wc & 0xFF; + break; + default: + break; + } + offset += length; + } + return pos; } /* Conversion bin -> hex and hex -> bin */