123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355 |
- #include "includes.h"
- #include "unicode.h"
- #include <stdint.h>
- #include <string.h>
- #include <stdlib.h>
- #include <stdio.h>
- #define GB2312_TO_UNICODE_BASE 0x134000
- #define UNICODE_TO_GB2312_BASE 0x138000
- extern const uint16_t GB2312_UNICODE_MAP[];
- typedef struct {
- uint16_t unicode;
- uint16_t gb2312;
- } unicode_gb2312_map_t;
- extern const unicode_gb2312_map_t UNICODE_GB2312_MAP[];
- #define GB2312_CODE_ZONE_MIN 0xA1
- #define GB2312_CODE_ZONE_MAX 0xF7
- #define GB2312_CODE_POS_MIN 0xA1
- #define GB2312_CODE_POS_MAX 0xFE
- uint16_t
- GB2312_to_Unicode(uint16_t c)
- {
- uint8_t zone, pos;
- uint32_t offset;
- uint16_t retval;
- zone = c >> 8;
- pos = c & 0xFF;
- if ((zone > GB2312_CODE_ZONE_MAX) ||
- (zone < GB2312_CODE_ZONE_MIN) ||
- (pos > GB2312_CODE_POS_MAX) ||
- (pos < GB2312_CODE_POS_MIN))
- {
- return c;
- }
- offset = (zone - GB2312_CODE_ZONE_MIN) * (GB2312_CODE_POS_MAX - GB2312_CODE_POS_MIN + 1) +
- (pos - GB2312_CODE_POS_MIN);
-
- retval=GB2312_UNICODE_MAP[offset];
- return retval;
- }
- uint16_t
- Unicode_to_GB2312(uint16_t c)
- {
- int offset;
- int low, high, mid;
-
- unicode_gb2312_map_t map;
- if (((c >> 8) == 0) && ((c & 0xFF) < 0x7F))
- {
- return c;
- }
- low = 0;
- high = (GB2312_CODE_ZONE_MAX - GB2312_CODE_ZONE_MIN + 1) *
- (GB2312_CODE_POS_MAX - GB2312_CODE_POS_MIN + 1);
- while (low <= high)
- {
- mid = (low + high) / 2;
-
-
-
-
- ReadFileData(UniToGBKIndex, mid * sizeof(unicode_gb2312_map_t),sizeof(unicode_gb2312_map_t),(uint8_t *)&map);
- if (c > map.unicode)
- {
- low = mid + 1;
- }
- if (c < map.unicode)
- {
- high = mid - 1;
- }
- if (c == map.unicode)
- {
- return map.gb2312;
- }
- }
- return '?';
- }
- uint16_t
- Ansi_to_Unicode(uint16_t *dest, uint16_t size, const uint8_t *src, uint16_t length)
- {
- uint16_t count = 0;
- while((count < size) && length)
- {
- if ((*src > 0x7F) && (length > 1))
- {
- dest[count] = GB2312_to_Unicode(((uint16_t)src[0] << 8) | src[1]);
- src += 2;
- length -= 2;
- }
- else
- {
- dest[count] = *src;
- src++;
- length--;
- }
- ++count;
- }
- return count;
- }
- uint16_t
- Unicode_to_Ansi(uint8_t *dest, uint16_t size, const uint8_t *src, uint16_t length)
- {
- uint16_t count = 0;
- uint16_t t;
- while((count + 1 < size) && length)
- {
- t = Unicode_to_GB2312(((uint16_t)src[1] << 8) | src[0]);
- if ((t >> 8) == 0)
- {
- *dest++ = t;
- ++count;
- }
- else
- {
- *dest++ = t >> 8;
- *dest++ = t;
- count += 2;
- }
- src += 2;
- length -= 2;
- }
- *dest++ = 0;
- return count;
- }
- uint16_t StrUnicodeToAnsi(uint8_t *dest,uint16_t size,const char *src)
- {
- uint16_t count = 0;
- uint16_t t;
- int length;
- uint16_t v;
- char temp[5];
-
- length=strlen(src);
- if(length<4) return 0;
- memset(dest,0,size);
-
- while((count + 1 < size) && length>0)
- {
-
- temp[0]=src[0];temp[1]=src[1];
- temp[2]=src[2];temp[3]=src[3];
- temp[4]=0;
- v=strtol(temp,NULL,16);
- t = Unicode_to_GB2312(v);
- if ((t >> 8) == 0)
- {
- *dest++ = t;
- ++count;
- }
- else
- {
- *dest++ = t >> 8;
- *dest++ = t;
- count += 2;
- }
- src += 4;
- length -= 4;
- }
- *dest++ = 0;
- return count;
- }
- uint16_t AnsiToStrUnicode(uint16_t *dest,uint16_t size,const char *src)
- {
- #if 1
- uint16_t count = 0;
- uint16_t t,v;
- int length;
- char temp[5];
-
- unsigned char t1,t2;
- length=strlen(src);
- if(length<2) return 0;
-
- memset(dest,0,size);
- while((count < size) && length)
- {
- if(src[0] == 0x30 && src[1] == 0x30) return count;
- temp[0]=src[0];temp[1]=src[1];
- temp[2]=src[2];temp[3]=src[3];
- temp[4]=0;
- v=strtol(temp,NULL,16);
- t1=v&0xff;
- t2=(v>>8)&0xff;
- if((t1 > 0x7F) && (t2 > 0x7f) && (length > 1))
- {
- dest[count] = GB2312_to_Unicode(v);
- src += 4;
- length -= 4;
- }
- else
- {
- t=t2;
- t &= 0xff;
- dest[count] = t;
- src +=2;
- length -=2;
- }
- ++count;
- }
- return 0;
- #else
- uint16_t count = 0;
- uint16_t t,v;
- int length;
- char temp[5];
- length=strlen(src);
- if(length<2) return 0;
-
- memset(dest,0,size);
- g_GroupNameLen=0;
- while((count < size) && length)
- {
- temp[0]=src[0];temp[1]=src[1];
- temp[2]=src[2];temp[3]=src[3];
- temp[4]=0;
- v=strtol(temp,NULL,16);
- if(v!=0){g_GroupNameLen++;}else{return 0;}
- if ((v > 0x7F)&&((v>>8) > 0x7F) && (length > 1))
- {
- dest[count] = GB2312_to_Unicode(v);
- src += 4;
- length -= 4;
- }
- else
- {
- dest[count] = *src;
- src++;
- length--;
- }
- ++count;
- }
- return 0;
- #endif
- }
|