sieelf/CodeShowPatch/CodeShow.C
2009-01-28 11:09:53 +00:00

404 lines
11 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/********************************************************************
(C) Copyright free source ,by Bennie.Joe@Gmail.com
filename: CodeShow.c
created: 2005-10-1
author: Bennie
describe: replaces provider name with custom format
history: 2005-06-22 V2
*********************************************************************/
#include "addr.h"
#include "type_.h"
#define VERSION "2.0b"
//#define BASEADDRESS 0xA1580000
#define CODESHOWDATAADDRESS BASEADDRESS
#define IPCODETABLECOUNT (BASEADDRESS+0x1E000)
#define REPEATSELECTTABLE (BASEADDRESS+0x1F000)
#define LOCALNOINFOTABLE (BASEADDRESS+0x1F100)
#define CODESHOWFLAG 0xFB
#define CODESHOWVERSION 2
#define MAXCITYNO 0x1FF
#define COUNTRYCODETABLE (BASEADDRESS+0x1E100)
#define LOBYTE(w) ((byte)(w))
#define HIBYTE(w) ((byte)(((word)(w) >> 8) & 0xFF))
extern int strlen(const char*);
extern int memcmp(const void*, const void*, int size);
extern uint atou(const char*);
extern WSTRING* WS_InitByZero(WSTRING*, word* buf, int count);
extern void GetCalleeNumber(int index, WSTRING *ws);
extern void UpdateWndItem(void* Countext, WSTRING* ws, int index);
int GetProvAndCity(word *pBSTR, char *pNoStr);
void BSTRAdd(word *pDst, const word * pSrc, int Count );
word GetCode(byte *pBuf, dword Index);
word FindLocale(LOCALE *pLocale, int LocaleCount, int LocaleNo, int *nRepeatNum);
void AppendInfoW(WSTRING *pWS, WSTRING * pNo);
const word szUnknownUser[] = { 0x672A, 0x77E5, 0x53F7,0x7801, 0};
const word szErrorData[] = { 0x6570, 0x636E, 0x5E93, 0x9519, 0x8BEF, 0};
const word szLocalCode[] = { 0x672C, 0x5730, 0x53F7,0x7801, 0};
const word szSplit[] = { 0x2027, 0 };
const word szAddInfo[] = { 0x20, 0 };
const word szShortNo[] = { 0x96C6, 0x56E2, 0x77ED, 0x53F7, 0};//¼¯ÍŶ̺ÅUnicode
/****************/
/* main program */
/****************/
void memcpy(char *szDest, char *szSrc, int len)
{
int i;
for(i=0; i<len; i++)
szDest[i] = szSrc[i];
szDest[len] = 0;
}
//²Î¿¼along´úÂë ¹ú¼Ê´úÂë
char FindCRName(word *pBSTR, char *szNo)
#ifdef SK6Cv50
@ "CODE_X"
#endif
{
if(memcmp(szNo, "86", 2) == 0)//ÖйúÄÚµØ
return 0;
else
{
char szTemp[5]="\0";
word wCRNo;
word wTable=*(word *)COUNTRYCODETABLE;
word wCount=*(word *)(COUNTRYCODETABLE+2);
const word *pTable=(word *)(COUNTRYCODETABLE+4);
CRNO *pCRNO=(CRNO *)(COUNTRYCODETABLE+(wTable+2<<1));
int j = 0, k, nRepeatNum = 0;
int i = 0, nStart = 0, nEnd = wCount;
int len=strlen(szNo);
if(len > 4)len = 4;
for(k=len; k>0; k--)
{
memcpy(szTemp, szNo, k);
nStart = 0, nEnd = wCount;
wCRNo = atou(szTemp);
while(nStart <= nEnd)
{
i = (nEnd+nStart)/2;
if(wCRNo == pCRNO[i].CRNo)
{
do{
if(wCRNo != pCRNO[i].CRNo)break;
i--;
}while(i > 0);
i++;
j = i;
do{
if(wCRNo != pCRNO[j].CRNo)break;
j++;
nRepeatNum++;
}while(j <= nEnd);
break;
}
if(wCRNo < pCRNO[i].CRNo)nStart = i+1;
else nEnd = i-1;
}
if(nRepeatNum > 0)break;
}
if(nRepeatNum == 0)
{
BSTRAdd(pBSTR, szUnknownUser, 4);
}
else
{
for(j=0; j<nRepeatNum; j++)
{
for(k=0; k<12; k++)
{
wCRNo = GetCode(pCRNO[i+j].Names, k);
if(wCRNo == MAXCITYNO)break;
else BSTRAdd(pBSTR, pTable+wCRNo, 1);
}
if(j+1 != nRepeatNum)
{
BSTRAdd(pBSTR, szAddInfo, 1);
}
}
}
}
return 1;
}
void UpdateLocaleToItem(int Index, int NoIndex, void* Context)
#ifdef SK6Cv50
@ "CODE_X"
#endif
{
WSTRING ws1, ws2, *pws1, *pws2;
word buf1[0x3E], buf2[0x3E];
pws1 = WS_InitByZero(&ws1, buf1, 0x3D);
pws2 = WS_InitByZero(&ws2, buf2, 0x3D);
GetCalleeNumber(NoIndex, pws1);
AppendInfoW(pws2, pws1);
UpdateWndItem(Context, pws2, Index);
}
//WStringºÅÂë°æ±¾
void AppendInfoW(WSTRING *pWS, WSTRING * pNo)
{
char szPNo[0x30];
int i=0;
for(; i<*pNo->pstr && i<0x2B; ++i)
{
szPNo[i] = pNo->pstr[i+1];
}
szPNo[i] = '\0';
GetProvAndCity(pWS->pstr, szPNo);
}
word GetLocalNoInfo(char *szLocalNo, char *szLocalName)
{
int i = 0;
word len = 0;
word no1, no2;
int nLocalNo=atou(szLocalNo);
int nLocalNum=*(int*)LOCALNOINFOTABLE;
const char * pLocalInfo = (const char *)(LOCALNOINFOTABLE+20);
for(; i< nLocalNum; ++i, pLocalInfo += 14)
{
no1 = *(word *)(pLocalInfo);
no2 = *(word *)(pLocalInfo+2);
if(no1 <= nLocalNo && nLocalNo <= no2)
{
len = 5;
memcpy(szLocalName, (char *)(pLocalInfo+4), 10);
break;
}
}
return len;
}
//µÃµ½ºÅÂëËù¶ÔÓ¦µÄÊ¡ÊÐ×Ö´®
int GetProvAndCity(word *pBSTR, char *pNoStr)
{
int bLocal=0;
char szLocalNo[5];
word CityNo = MAXCITYNO;
char szLocalInfo[11];
const PROVINCE * pProvince;
const CITY* pCity;
LOCALE *pLocale;
const CODESHOWHEAD * pHead;
int CodeLen;
int nLocalNum=*(int*)LOCALNOINFOTABLE;
const PROVINCE LocalProvince=*(PROVINCE*)(LOCALNOINFOTABLE+4);
const CITY LocalCity=*(CITY*)(LOCALNOINFOTABLE+10);
int nRepeatNum=1;
pHead = (const CODESHOWHEAD *)CODESHOWDATAADDRESS;
//УÑéÊý¾Ý¿âµÄÓÐЧÐÔ
if(pHead->Flag != CODESHOWFLAG || pHead->Version != CODESHOWVERSION) {
BSTRAdd(pBSTR, szErrorData, 4);
return 0;
}
//µÃµ½Ê¡±íºÍÊбí
pProvince = (PROVINCE*)(CODESHOWDATAADDRESS+pHead->ProvinceTableOffset);
pCity = (CITY*)(CODESHOWDATAADDRESS+pHead->CityTableOffset);
//È¥µô¹ú¼ÊÂë+86
if(*pNoStr == '+')
{
if(FindCRName(pBSTR, pNoStr+1))return 0;
else pNoStr += 3;
}
//Èç¹û³¤¶È´óÓÚ12£¬Ôò°üÀ¨IPǰ׺£¬ÏÂÃæÂËÈ¥ÒÑÖªIPǰ׺
CodeLen = strlen(pNoStr);
if(CodeLen > 12)
{
int i = 0;
const char * pIPCode = (const char *)(IPCODETABLECOUNT+4);
for(; i< *(const int *)IPCODETABLECOUNT; ++i, pIPCode += 8)
{
if(!memcmp(pNoStr, pIPCode+1, *pIPCode)){
pNoStr += *pIPCode;
if(!memcmp(pNoStr, "013", 3) || !memcmp(pNoStr, "015", 3) /*|| !memcmp(pNoStr, "018", 3)*/)//ɾ³ýÊÖ»úºÅǰµÄ0
++pNoStr;
break;
}
}
}
//µÃµ½³¤Í¾µç»°ÇøºÅ±í
pLocale = (LOCALE*)(CODESHOWDATAADDRESS+pHead->LocaleTableOffset);
//Èç¹ûÒÔ0¿ªÍ·£¬ÔòÅжÏΪÊǹ̶¨³¤Í¾µç»°
if(*pNoStr == '0')
{
++pNoStr;
//ÅжÏÊÇ·ñÊÇÈýÎ»ÇøºÅ010ºÍ02x
if(*pNoStr == '1' || *pNoStr == '2')
{
memcpy(szLocalNo, pNoStr+2, 4);
*(pNoStr+2) = '\0';
}
else
{
memcpy(szLocalNo, pNoStr+3, 4);
*(pNoStr+3) = '\0';
}
//µÃµ½ÇøºÅ£¬ÔÚÇøºÅ±íÖвéÕÒ¶ÔÓ¦µÄ³ÇÊкÅ
CityNo = FindLocale(pLocale, pHead->LocaleCount, atou(pNoStr), &nRepeatNum);
}
//Èç¹ûÊÇ13x£¬15x£¬18x£¬ÔòÅж¨ÎªÒƶ¯µç»°
else if(*pNoStr == '1' && (*(pNoStr+1) == '3' || *(pNoStr+1) == '5' /*|| *(pNoStr+1) == '8'*/))
{
char chTemp=*(pNoStr+1);
bLocal = 2;
pNoStr += 2;
*(pNoStr+5) = '\0';
//µÃµ½ÊÖ»úºÅÂëµÄǰÎå룬ÔÚÊÖ»úºÅÂë±íÖвéÕÒ¶ÔÓ¦µÄ³ÇÊкÅ
if(chTemp == '3')//13X
CityNo = GetCode((byte *)(CODESHOWDATAADDRESS+pHead->CodeTableOffset), atou(pNoStr));
else //if(chTemp == '5')//15X
CityNo = GetCode((byte *)(CODESHOWDATAADDRESS+0x20000), atou(pNoStr));
//else //18x
//CityNo = GetCode((byte *)(CODESHOWDATAADDRESS+?????), atou(pNoStr));
}
//Èç¹û²»Êdz¤»°ºÍÒÆ¶¯µç»°£¬ÅжÏΪ±¾µØÇøºÅ¡£»òÕß²»ÄÜÔÚIP±íÖÐÕÒµ½¶ÔÓ¦Ïî¡£
else
{
//¼¯ÍŶ̺ŹæÔò£º61-69¿ªÍ·£¬3-6λ¶ÌºÅ
if(*pNoStr=='6'&&CodeLen>=4&&CodeLen<=7)//°üÀ¨×îºóµÄ'\0'
{
BSTRAdd(pBSTR, szShortNo, 4);
}
else //±¾µØºÅÂë
{
BSTRAdd(pBSTR, szLocalCode, 4);
if(nLocalNum > 0 && nLocalNum < 200)//?????
{
memcpy(szLocalNo, pNoStr, 4);
if(GetLocalNoInfo(szLocalNo, szLocalInfo))
{
BSTRAdd(pBSTR, szAddInfo, 1);
BSTRAdd(pBSTR, (word *)szLocalInfo, 5);
}
}
}
return 0;
}
//Èç¹û³ÇÊкŲ»ÔÚ·¶Î§ÄÚ
if(CityNo >= pHead->CityCount){
BSTRAdd(pBSTR, szUnknownUser, 4);
return 0;
}
else{
if(nRepeatNum == 1)
{
BSTRAdd(pBSTR, pProvince[pCity[CityNo].ProvinceNo].ProvinceName, 3);
BSTRAdd(pBSTR, szSplit, 1);
BSTRAdd(pBSTR, pCity[CityNo].CityName, 5);
if(bLocal == 0)
bLocal = (memcmp(LocalCity.CityName, pCity[CityNo].CityName, 10) == 0);
}
else
{
int i;
BSTRAdd(pBSTR, pProvince[pCity[pLocale[CityNo].CityNo].ProvinceNo].ProvinceName, 3);
BSTRAdd(pBSTR, szSplit, 1);
for(i=0; i<nRepeatNum; i++)
{
if(nRepeatNum < 3)
BSTRAdd(pBSTR, szAddInfo, 1);
BSTRAdd(pBSTR, pCity[pLocale[i+CityNo].CityNo].CityName, 5);
if(bLocal == 0)
bLocal = (memcmp(LocalCity.CityName, pCity[CityNo].CityName, 10) == 0);//Èç¹û³ÇÊÐÃûÏàͬ£¬bLocal=1£¬ÏÔÊ¾Çø¼¶µØÃû
}
}
if(bLocal == 1 && nLocalNum > 0 && nLocalNum < 200 && (memcmp(LocalProvince.ProvinceName, pProvince[pCity[CityNo].ProvinceNo].ProvinceName, 6) == 0))
{
if(GetLocalNoInfo(szLocalNo, szLocalInfo))
{
BSTRAdd(pBSTR, szAddInfo, 1);
BSTRAdd(pBSTR, (word *)szLocalInfo, 5);
}
}
return 1;
}
}
//´ÓÊÖ»úºÅÂë±íÖвéÕÒ¶ÔÓ¦µÄ³ÇÊкÅ
word GetCode(byte *pBuf, dword Index)
{
word CodeL, CodeH;
pBuf = pBuf + (Index + (Index>>3)); //ÿÌõ¼Ç¼9¸öbit,¼´Index*9/8
Index &= 0x7; //µÃµ½ÔÚ×Ö½ÚÄ򵀮«ÒÆÁ¿
CodeL = ((word)pBuf[0])>>Index ; //µÍ×Ö½ÚÓÒÒÆÆ«ÒÆÁ¿
CodeH = ((pBuf[1]<<(15-Index)) & 0xFFFF)>>7; //¸ß×Ö½ÚÈ¥µô¸ß´¦ÎÞЧλ,²¢×óÒÆµ½ºÏÊʵÄλÖÃ
return (CodeH + CodeL); //Æ´½Ó¸ßµÍ×Ö½Ú,¹²9λ
}
//´Ó³¤»°ÇøºÅ±íÖвéÕÒ¶ÔÓ¦µÄ³ÇÊкÅ
word FindLocale(LOCALE *pLocale, int LocaleCount, int LocaleNo, int *nRepeatNum)
{
//³¤»°ÇøºÅ±íÊÇ˳Ðò´æ´¢µÄ,ÏÂÃæÊǸö¼òµ¥µÄ¶þ·Ö·¨²éÕÒ.
int j = 0;
int i = 0, nStart = 0, nEnd = LocaleCount;
//´¦Àí¼¸¸öÌØÊâµÄµØÇø£¬ÕâЩµØÇøÓÐÏàͬµÄÇøºÅ£¬ÔÚ¶þ·Ö·¨Ê±½á¹û²»È·¶¨
*nRepeatNum = 0;
/*
while(i < *(int*)REPEATSELECTTABLE)
{
if( LocaleNo == *(word*)(REPEATSELECTTABLE+4+i*4))
{
return *(word*)(REPEATSELECTTABLE+6+i*4);
}
++i;
}*/
//¶þ·Ö·¨²éÕÒ¶ÔÓ¦µÄ³ÇÊкţ¬ÇøºÅ±íÊÇÉýÐò´æ´¢µÄ
while(nStart <= nEnd)
{
i = (nEnd+nStart)/2;
if(LocaleNo == pLocale[i].LocaleNo)
{
do
{
if(LocaleNo != pLocale[i].LocaleNo)
break;
i--;
}
while(i > 0);
i++;
j = i;
do
{
if(LocaleNo != pLocale[j].LocaleNo)
break;
j++;
(*nRepeatNum)++;
}
while(j <= nEnd);
break;
}
if(LocaleNo < pLocale[i].LocaleNo)
nEnd = i-1;
else
nStart = i+1;
}
if(*nRepeatNum > 1)
return i;
else
return (nStart > nEnd)? MAXCITYNO:pLocale[i].CityNo;
}
//¸½¼ÓUNICODE×Ö´®µ½ÁíÒ»¸ö×Ö´®ÉÏ,½áÊø±êÖ¾ÊÇL'\0'»òCount¸ö×Ö·û
void BSTRAdd(word *pDst, const word * pSrc, int Count)
{
uint nSize = *pDst, i=1;
while(*pSrc != '\0' && i<=Count)
*(pDst+ nSize + i++) = *pSrc++;
*pDst = nSize+i-1;
}