373 lines
7.2 KiB
C
373 lines
7.2 KiB
C
/*
|
|
**
|
|
** function.c
|
|
**
|
|
** Functions for data conversion, tools.
|
|
**
|
|
*/
|
|
|
|
#include "../include/includes.h"
|
|
|
|
void
|
|
SafeFree(void *pmem)
|
|
{
|
|
if (pmem != NULL)
|
|
free(pmem);
|
|
}
|
|
|
|
/*
|
|
** Exchange between a and b
|
|
*/
|
|
void
|
|
Exchange(long a, long b)
|
|
{
|
|
long c;
|
|
|
|
c = a;
|
|
a = b;
|
|
b = c;
|
|
}
|
|
|
|
int
|
|
ByteLength(BYTE *bcd, int c)
|
|
{
|
|
int length=0;
|
|
|
|
while (*bcd++ != c)
|
|
{
|
|
length ++;
|
|
// if(length>64) break;
|
|
}
|
|
return length+1;
|
|
}
|
|
|
|
/*
|
|
** Fill string 'c' into last area of 'str'.
|
|
*/
|
|
/*
|
|
void
|
|
StringCat(char *str, char *c, int slen)
|
|
{
|
|
if (strlen(str) >= slen) return;
|
|
while (strlen(str) < slen)
|
|
strcat(str, c);
|
|
str[slen] = 0;
|
|
}
|
|
*/
|
|
/*
|
|
** Cut string while match character c.
|
|
*/
|
|
|
|
void
|
|
StringCut(char *str, int c)
|
|
{
|
|
if (toupper(*str) == toupper(c)) *str=0;
|
|
while (str && *str++)
|
|
if (toupper(*str) == toupper(c)) {
|
|
*str = 0;
|
|
break;
|
|
}
|
|
}
|
|
|
|
/*
|
|
void Bstr2Hstr(char *cptr)
|
|
{
|
|
char s[] = "";
|
|
unsigned int i, j = 0;
|
|
|
|
while (cptr && *cptr && strchr("01", *cptr)) {
|
|
i = *cptr++ - '0';
|
|
j <<= 1;
|
|
j |= (i & 0x01);
|
|
}
|
|
sprintf(s, "%x", j);
|
|
cptr = (char *) s;
|
|
}
|
|
|
|
void bitstring(char *str, long byze, int biz, int strwid)
|
|
{
|
|
int i, j;
|
|
|
|
j = strwid - (biz + (biz >> 2)- (biz % 4 ? 0 : 1));
|
|
for (i = 0; i < j; i++)
|
|
*str++ = ' ';
|
|
while (--biz >= 0) {
|
|
*str++ = ((byze >> biz) & 1) + '0';
|
|
//
|
|
// if (!(biz % 4) && biz)
|
|
// *str++ = ' ';
|
|
//
|
|
}
|
|
*str = '\0';
|
|
}
|
|
*/
|
|
/*
|
|
** Binary string convert to int.
|
|
*/
|
|
/*
|
|
unsigned int Bstr2Int(char *cptr)
|
|
{
|
|
unsigned int i, j = 0;
|
|
|
|
while (cptr && *cptr && strchr("01", *cptr)) {
|
|
i = *cptr++ - '0';
|
|
j <<= 1;
|
|
j |= (i & 0x01);
|
|
}
|
|
return(j);
|
|
}
|
|
*/
|
|
/*
|
|
void AsciiToBcd (BYTE *bcd_buf, const char *ascii_buf, int len)
|
|
{
|
|
int i;
|
|
char ch;
|
|
|
|
for (i=0; i<len; i++)
|
|
{
|
|
ch = ascii_buf[i];
|
|
if (ch>='a') ch -= 'a' - 10;
|
|
else if (ch>='A') ch -= 'A' - 10;
|
|
else ch -= '0';
|
|
if (i & 1) *(bcd_buf++) |= ch & 0x0f;
|
|
else *bcd_buf = ch << 4;
|
|
}
|
|
}
|
|
*/
|
|
/* Convert data from left-aligned compact BCD form to ASCII form. */
|
|
/*
|
|
void BcdToAscii (char *ascii_buf, const BYTE *bcd_buf, int len)
|
|
{
|
|
int i;
|
|
char ch;
|
|
|
|
for (i=0; i<len; i++) {
|
|
if (i & 1) ch = *(bcd_buf++) & 0x0f;
|
|
else ch = *bcd_buf >> 4;
|
|
ascii_buf[i] = ch + ((ch > 9)? 'A'-10 : '0');
|
|
}
|
|
ascii_buf[i] = '\0';
|
|
}
|
|
*/
|
|
/* Convert data from ASCII form to right-aligned compact BCD form. */
|
|
/*
|
|
void AsciiToRbcd (BYTE *bcd_buf, const char *ascii_buf, int len)
|
|
{
|
|
int i;
|
|
char ch;
|
|
|
|
if (len > 0) memset(bcd_buf ,0,len/2);
|
|
for (i=0; i<len; i++) {
|
|
ch = ascii_buf[i];
|
|
if (ch>='a') ch -= 'a' - 10;
|
|
else if (ch>='A') ch -= 'A' - 10;
|
|
else ch -= '0';
|
|
if ((len - i) & 1) *(bcd_buf++) |= ch & 0x0f;
|
|
else *bcd_buf = ch << 4;
|
|
}
|
|
}
|
|
*/
|
|
/* Convert data from right-aligned compact BCD form to ASCII form. */
|
|
/*
|
|
void RbcdToAscii (char *ascii_buf, const BYTE *bcd_buf, int len)
|
|
{
|
|
int i;
|
|
char ch;
|
|
|
|
for (i=0; i<len; i++) {
|
|
if ((len - i) & 1) ch = *(bcd_buf++) & 0x0f;
|
|
else ch = *bcd_buf >> 4;
|
|
ascii_buf[i] = ch + ((ch > 9)? 'A'-10 : '0');
|
|
}
|
|
ascii_buf[i] = '\0';
|
|
}
|
|
*/
|
|
/* Convert number to compact BCD, the number must in the range from 0 to 99. */
|
|
/*
|
|
BYTE IntToBcd (int n)
|
|
{
|
|
return ((n / 10) << 4) | (n % 10);
|
|
}
|
|
*/
|
|
/* Convert number from compact BCD. */
|
|
/*
|
|
int BcdToInt (BYTE n)
|
|
{
|
|
return (n >> 4) * 10 + (n & 0x0f);
|
|
}
|
|
*/
|
|
/* Copy charaters, then append a null character to array pointed by dst. */
|
|
/*
|
|
void CopyToString (char *dst, const char *src, int len)
|
|
{
|
|
if (len) memcpy(dst, src, len);
|
|
dst[len] = '\0';
|
|
}
|
|
*/
|
|
char *
|
|
FormatOutput(char *sp, size_t size, int len, int c)
|
|
{
|
|
char buf[1024];
|
|
int i, j=0;
|
|
|
|
strcpy(buf, sp);
|
|
for(i=0; i<size; i+=len) {
|
|
strncpy(sp, buf+i, len);
|
|
sp +=len;
|
|
j +=len;
|
|
if ( (j % 80)!=0 ) {
|
|
*sp++ = c;
|
|
j++;
|
|
}
|
|
}
|
|
*sp = 0;
|
|
|
|
return sp;
|
|
}
|
|
|
|
/* Convert data from ASCII form to right-aligned compact BCD form. */
|
|
|
|
void ppsAsciiToBcdR (BYTE *bcd_buf, const char *ascii_buf, int len)
|
|
{
|
|
int i;
|
|
char ch;
|
|
|
|
for (i=0; i<len; i++) {
|
|
ch = ascii_buf[i];
|
|
if (ch>='a') ch -= 'a' - 10;
|
|
else if (ch>='A') ch -= 'A' - 10;
|
|
else ch -= '0';
|
|
if(i%2==0)
|
|
{
|
|
*bcd_buf = ch & 0x0f; ;
|
|
}
|
|
else
|
|
{
|
|
*(bcd_buf) |= ((ch&0x0f) <<4 ) ;
|
|
bcd_buf ++;
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
/* Convert data from right-aligned compact BCD form to ASCII form. */
|
|
|
|
void ppsBcdToAsciiR (char *ascii_buf, const BYTE *bcd_buf, int len)
|
|
{
|
|
int i;
|
|
char ch;
|
|
|
|
for (i=0; i<len/2; i++) {
|
|
ch = bcd_buf[i] & 0x0f;
|
|
ascii_buf[i*2] = ch + ((ch > 9)? 'A'-10 : '0');
|
|
ch = bcd_buf[i]>> 4;
|
|
ascii_buf[i*2+1] = ch + ((ch > 9)? 'A'-10 : '0');
|
|
}
|
|
ascii_buf[len] = '\0';
|
|
}
|
|
|
|
int AsciiToMsisdn(char *charin, u_char *msgout, int len)
|
|
{
|
|
u_char tmpbuf[64];
|
|
char msgin[64];
|
|
int msglen;
|
|
if(len>18) return 0;
|
|
memcpy(msgin,charin,len);
|
|
msglen = len;
|
|
if(len%2!=0){
|
|
msgin[len] = 'f';
|
|
msgin[len+1] = 0x00;
|
|
msglen = len+1;
|
|
}
|
|
ppsAsciiToBcdR(tmpbuf,msgin,msglen);
|
|
msgout[0] = msglen/2;
|
|
msgout[1] = 0x91;
|
|
if(tmpbuf[0] !=0x19 && tmpbuf[0] != 0x18)
|
|
{
|
|
msgout[0] ++;
|
|
memcpy(msgout+2,tmpbuf,msglen/2);
|
|
}
|
|
else
|
|
{
|
|
memcpy(msgout+2,tmpbuf+1,msglen/2);
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
void GetVersionID(char *cversion, BYTE *version)
|
|
{
|
|
int i,start,len,index;
|
|
char buf[3][32]={"","",""};
|
|
|
|
start = 0,index = 0;
|
|
len = strlen(cversion);
|
|
for(i=0;i<len;i++)
|
|
{
|
|
if(cversion[i] !='.')
|
|
buf[index][start++] = cversion[i];
|
|
else
|
|
{
|
|
index++;
|
|
start = 0;
|
|
}
|
|
}
|
|
version[0] = atoi(buf[0]);
|
|
version[1] = atoi(buf[1]);
|
|
version[2] = atoi(buf[2]);
|
|
|
|
}
|
|
|
|
void
|
|
StringCutString(char *strbase, char *strcut)
|
|
{
|
|
char *start=NULL;
|
|
int end;
|
|
start = strstr(strbase,strcut);
|
|
if(start)
|
|
{
|
|
end = start - strbase;
|
|
strbase[end] = 0;
|
|
}
|
|
}
|
|
|
|
void string2ltime(time_t *nowtime,char *timestr)
|
|
{
|
|
struct tm mytime;
|
|
char tmp[5];
|
|
if(timestr==NULL||nowtime==NULL)
|
|
return;
|
|
strncpy(tmp,timestr,4);
|
|
tmp[4]='\0';
|
|
mytime.tm_year=atoi(tmp)-1900;
|
|
strncpy(tmp,timestr+4,2);
|
|
tmp[2]='\0';
|
|
mytime.tm_mon =atoi(tmp)-1;
|
|
strncpy(tmp,timestr+6,2);
|
|
tmp[2]='\0';
|
|
mytime.tm_mday=atoi(tmp);
|
|
strncpy(tmp,timestr+8,2);
|
|
tmp[2]='\0';
|
|
mytime.tm_hour =atoi(tmp);
|
|
strncpy(tmp,timestr+10,2);
|
|
tmp[2]='\0';
|
|
mytime.tm_min =atoi(tmp);
|
|
strncpy(tmp,timestr+12,2);
|
|
tmp[2]='\0';
|
|
mytime.tm_sec =atoi(tmp);
|
|
|
|
*nowtime=mktime(&mytime);
|
|
}
|
|
|
|
unsigned int HstrToInt(char *cptr)
|
|
{
|
|
unsigned int i,j=0;
|
|
while(cptr && *cptr && isxdigit(*cptr)){
|
|
i = *cptr++ -'0';
|
|
if(9<i)
|
|
i -=7;
|
|
j <<=4;
|
|
j |=(i&0x0f);
|
|
}
|
|
return j;
|
|
}
|