594 lines
14 KiB
C
594 lines
14 KiB
C
#define _UTIL_C_
|
||
|
||
#include <sys/types.h>
|
||
#include <sys/socket.h>
|
||
#include <sys/time.h>
|
||
#include <sys/stat.h>
|
||
|
||
#include <netinet/in.h>
|
||
#include <arpa/inet.h>
|
||
|
||
#include <stdio.h>
|
||
#include <string.h>
|
||
#include <stdlib.h>
|
||
#include <unistd.h>
|
||
#include <dirent.h>
|
||
#include <netdb.h>
|
||
#include <errno.h>
|
||
#include <ctype.h>
|
||
|
||
#include "main.h"
|
||
#include "util.h"
|
||
|
||
#define min( x,y ) ({typeof(x) __x=(x),__y=(y);(__x < __y) ? __x : __y; })
|
||
#define max( x,y ) ({typeof(x) __x=(x),__y=(y);(__x < __y) ? __y : __x; })
|
||
#define IS_2BYTEWORD( _a_ ) ( (char)(0x80) <= (_a_) && (_a_) <= (char)(0xFF) )
|
||
#define PRIME 211
|
||
int hashpjw ( char* s )
|
||
{
|
||
char *p;
|
||
unsigned int h= 0 ,g;
|
||
for( p = s ; *p ; p ++ ){
|
||
h = ( h<< 4 ) + (*p);
|
||
if( (g = h & 0xf0000000) != 0){
|
||
h = h ^ (g>>24);
|
||
h = h ^ g;
|
||
}
|
||
}
|
||
return h % PRIME;
|
||
}
|
||
|
||
int getHash ( char* s )
|
||
{
|
||
int i;
|
||
int h=0;
|
||
|
||
for(i=0;; i++){
|
||
if( s[i] == 0 ){
|
||
break;
|
||
}
|
||
h += s[i];
|
||
}
|
||
return h;
|
||
}
|
||
|
||
void easyGetTokenFromString( char *src,int count,char*output,int len )
|
||
{
|
||
int i;
|
||
int counter = 0;
|
||
|
||
if( len <= 0 )return;
|
||
|
||
#define ISSPACETAB( c ) ( (c) == ' ' || (c) == '\t' )
|
||
|
||
for(i=0;;i++){
|
||
if( src[i]=='\0'){
|
||
output[0] = '\0';
|
||
return;
|
||
}
|
||
if( i > 0 && ! ISSPACETAB( src[i-1] ) &&
|
||
! ISSPACETAB( src[i] ) ){
|
||
continue;
|
||
}
|
||
|
||
if( ! ISSPACETAB( src[i]) ){
|
||
counter++;
|
||
if( counter == count){
|
||
int j;
|
||
for(j=0;j<len-1;j++){
|
||
if( src[i+j] == '\0' ||
|
||
ISSPACETAB( src[i+j] ) ){
|
||
break;
|
||
}
|
||
output[j]=src[i+j];
|
||
}
|
||
output[j]='\0';
|
||
return;
|
||
|
||
}
|
||
}
|
||
|
||
}
|
||
}
|
||
|
||
void prepareDirectories(char *base)
|
||
{
|
||
int i;
|
||
char dname[1024];
|
||
|
||
for(i=0;i<256;i++){
|
||
int ret;
|
||
snprintf( dname , sizeof( dname ) , "%s/0x%x", base , i );
|
||
ret = mkdir( dname , 0755 );
|
||
if( ret <0 && errno != EEXIST ){
|
||
log( "mkdir error:%d %s: %s\n", ret ,strerror(errno), dname );
|
||
}
|
||
if( ret == 0 ) log(".");
|
||
}
|
||
}
|
||
|
||
void remove_r( char *s )
|
||
{
|
||
int i;
|
||
for(i=0;;i++){
|
||
if(s[i]=='\r')s[i] =0;
|
||
if( s[i]==0)break;
|
||
}
|
||
}
|
||
|
||
void makeDirFilename( char *out , int outlen ,
|
||
char *base , int dirchar , char *child )
|
||
{
|
||
snprintf( out , outlen ,
|
||
"%s/0x%x/%s" , base ,
|
||
dirchar & 0xff , child );
|
||
}
|
||
|
||
int isFile( char *fn )
|
||
{
|
||
FILE*fp=fopen(fn,"r");
|
||
if( fp){
|
||
fclose(fp);
|
||
return 1;
|
||
}
|
||
return 0;
|
||
}
|
||
|
||
int createFile( char *fn , char *line )
|
||
{
|
||
FILE *fp = fopen( fn , "w" );
|
||
if( fp== NULL ){
|
||
return -1;
|
||
} else {
|
||
fprintf( fp , "%s" , line );
|
||
fclose(fp);
|
||
return 0;
|
||
}
|
||
}
|
||
|
||
#define BOOL int
|
||
#define FALSE 0
|
||
#define TRUE 1
|
||
|
||
typedef struct tagEscapeChar
|
||
{
|
||
char escapechar;
|
||
char escapedchar;
|
||
}EscapeChar;
|
||
static EscapeChar escapeChar[]=
|
||
{
|
||
{ '\n', 'n' },
|
||
{ ',', 'c' },
|
||
{ '|', 'z' },
|
||
{ '\\', 'y' },
|
||
};
|
||
|
||
char* makeStringFromEscaped( char* src )
|
||
{
|
||
int i;
|
||
int srclen = strlen( src );
|
||
int searchindex=0;
|
||
|
||
for( i = 0 ; i < srclen ; i ++ ){
|
||
// for 2Byte Word
|
||
if( IS_2BYTEWORD( src[i] ) ){
|
||
src[searchindex++] = src[i++];
|
||
src[searchindex++] = src[i];
|
||
}else{
|
||
if( src[i] == '\\' ){
|
||
int j;
|
||
|
||
/* <20>ݼ<EFBFBD> ٯ<><D9AF><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
||
i++;
|
||
for( j = 0; j<sizeof(escapeChar)/sizeof(escapeChar[0]); j++){
|
||
if( escapeChar[j].escapedchar == src[i] ){
|
||
/* <20>ݻ<EFBFBD><DDBB><EFBFBD><DEB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƽ<EFBFBD>ҷ·<D2B7> */
|
||
src[searchindex++] = escapeChar[j].escapechar;
|
||
break;
|
||
}
|
||
}
|
||
/* <20>¡<DEB7>֧<EFBFBD><D6A7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƻ<EFBFBD><C6BB><EFBFBD><EFBFBD><EFBFBD> */
|
||
if(escapeChar[j].escapedchar != src[i])
|
||
src[searchindex++] = src[i];
|
||
}else{
|
||
src[searchindex++] = src[i];
|
||
}
|
||
}
|
||
}
|
||
src[searchindex] = '\0';
|
||
return src;
|
||
}
|
||
|
||
char * makeEscapeString( char* src , char* dest, int sizeofdest)
|
||
{
|
||
int i;
|
||
int srclen = strlen( src );
|
||
int destindex=0;
|
||
|
||
for( i = 0 ; i < srclen ; i ++ ){
|
||
BOOL dirty=FALSE;
|
||
int j;
|
||
char escapechar='\0';
|
||
if( destindex + 1 >= sizeofdest ) break;
|
||
if( IS_2BYTEWORD( src[i] ) ){
|
||
if( destindex + 2 >= sizeofdest ) break;
|
||
dest[destindex] = src[i];
|
||
dest[destindex+1] = src[i+1];
|
||
destindex += 2;
|
||
i ++;
|
||
continue;
|
||
}
|
||
|
||
for( j = 0; j<sizeof(escapeChar)/sizeof(escapeChar[0]); j++){
|
||
if( src[i] == escapeChar[j].escapechar ){
|
||
dirty=TRUE;
|
||
escapechar= escapeChar[j].escapedchar;
|
||
break;
|
||
}
|
||
}
|
||
if( dirty == TRUE ){
|
||
if( destindex + 2 < sizeofdest ){
|
||
dest[destindex] = '\\';
|
||
dest[destindex+1] = escapechar;
|
||
destindex+=2;
|
||
dirty=TRUE;
|
||
continue; /* <20>ݼ<EFBFBD> ٯ<><D9AF><EFBFBD><EFBFBD> */
|
||
}else{
|
||
dest[destindex] = '\0';
|
||
return dest;
|
||
}
|
||
}else{
|
||
dest[destindex] = src[i];
|
||
destindex++;
|
||
}
|
||
}
|
||
|
||
dest[destindex] = '\0';
|
||
return dest;
|
||
}
|
||
|
||
char * makeEscapeString1( char* src , char* dest, int sizeofdest)
|
||
{
|
||
int i;
|
||
int srclen = strlen( src );
|
||
int destindex=0;
|
||
|
||
for( i = 0 ; i < srclen ; i ++ ){
|
||
BOOL dirty=FALSE;
|
||
int j;
|
||
char escapechar='\0';
|
||
if( destindex + 1 >= sizeofdest )
|
||
/* '\0'<27>л<EFBFBD><D0BB><EFBFBD>Իئ<D4BB>м<EFBFBD>ƥ<EFBFBD><C6A5><EFBFBD><EFBFBD>ƥ<EFBFBD><C6A5>Ի */
|
||
break;
|
||
|
||
/* // <20><>ʣ<EFBFBD><CAA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ë<EFBFBD><C3AB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||
if( IS_2BYTEWORD( src[i] ) ){
|
||
// <20><>ʣ<EFBFBD>֣۹<D6A3><DBB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>б<EFBFBD><D0B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>£<EFBFBD>
|
||
// <20><EFBFBD><D7B7>ƨ<EFBFBD><C6A8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƾ<EFBFBD>ئ<EFBFBD><D8A6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ئ<EFBFBD><D8A6>
|
||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˪
|
||
if( destindex + 2 >= sizeofdest )break;
|
||
|
||
dest[destindex] = src[i];
|
||
dest[destindex+1] = src[i+1];
|
||
destindex += 2;
|
||
i ++;
|
||
continue;
|
||
}
|
||
*/
|
||
for( j = 0; j<sizeof(escapeChar)/sizeof(escapeChar[0]); j++)
|
||
if( src[i] == escapeChar[j].escapechar ){
|
||
dirty=TRUE;
|
||
escapechar= escapeChar[j].escapedchar;
|
||
break;
|
||
}
|
||
|
||
if( dirty == TRUE ){
|
||
/* <20><EFBFBD><DEB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> ٯ<><D9AF> */
|
||
if( destindex + 2 < sizeofdest ){
|
||
/* +2 <20><><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> '\\' <20><> 'n'<27><><EFBFBD>Ƿ<EFBFBD> */
|
||
/* <20><>Ի ٯ<><D9AF><EFBFBD><EFBFBD><EFBFBD>з<EFBFBD> */
|
||
dest[destindex] = '\\';
|
||
dest[destindex+1] = escapechar;
|
||
destindex+=2;
|
||
dirty=TRUE;
|
||
continue; /* <20>ݼ<EFBFBD> ٯ<><D9AF><EFBFBD><EFBFBD> */
|
||
}else{
|
||
/* <20><>Ի<EFBFBD><D4BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>뻥<EFBFBD><EBBBA5><EFBFBD><EFBFBD><EFBFBD>ƻ<EFBFBD><C6BB><EFBFBD><EFBFBD><EFBFBD> */
|
||
dest[destindex] = '\0';
|
||
return dest;
|
||
}
|
||
}else{
|
||
dest[destindex] = src[i];
|
||
destindex++;
|
||
}
|
||
|
||
}
|
||
dest[destindex] = '\0';
|
||
return dest;
|
||
}
|
||
#undef BOOL
|
||
#undef FALSE
|
||
#undef TRUE
|
||
|
||
|
||
|
||
/* <20><><EFBFBD><EFBFBD>strcatsafe<66><65><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>뻥ؤ<EBBBA5><D8A4>ľ<EFBFBD><C4BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ئ<EFBFBD>У<EFBFBD> */
|
||
int
|
||
strcatsafe( char *dest, int destlen , char *append )
|
||
{
|
||
int dl = strlen( dest);
|
||
int al = strlen(append);
|
||
|
||
if( (dl + al) >= destlen ){
|
||
return -1;
|
||
} else {
|
||
strcat( dest, append );
|
||
}
|
||
return 0;
|
||
}
|
||
|
||
char *chop( char *s )
|
||
{
|
||
int l = strlen(s);
|
||
if( l >= 1 ){
|
||
s[l-1]=0;
|
||
}
|
||
return s;
|
||
}
|
||
|
||
// CoolFish: Family 2001/5/30
|
||
|
||
/*void easyGetTokenFromBuf(char *src, char delim, int count, char *output, int len)
|
||
{
|
||
int i;
|
||
int counter = 0;
|
||
int wordflag = 0;
|
||
|
||
if( len <= 0 ) return;
|
||
|
||
#define ISSEPARATE( c ) ((c) == delim)
|
||
|
||
for( i=0; ; i++){
|
||
if( src[i] == '\0' ){
|
||
output[0] = '\0';
|
||
return;
|
||
}
|
||
if(IS_2BYTEWORD(src[i])){
|
||
if( i>0 && wordflag == 1 ){
|
||
i++;
|
||
wordflag = 1;
|
||
continue;
|
||
}
|
||
if( i>0 && !ISSEPARATE(src[i-1]) && !ISSEPARATE(src[i])){
|
||
i++;
|
||
wordflag = 1;
|
||
continue;
|
||
}
|
||
wordflag = 1;
|
||
}else{
|
||
if( i>0 && wordflag ){
|
||
wordflag = 0;
|
||
continue;
|
||
}
|
||
if( i>0 && !ISSEPARATE(src[i-1]) && !ISSEPARATE(src[i])){
|
||
wordflag = 0;
|
||
continue;
|
||
}
|
||
wordflag = 0;
|
||
}
|
||
if( !ISSEPARATE(src[i]) ){
|
||
counter++;
|
||
if( counter == count ){
|
||
int j;
|
||
for ( j=0; j<len-1; j++){
|
||
if( IS_2BYTEWORD( src[i+j] ) ){
|
||
output[j] = src[i+(j++)];
|
||
output[j] = src[i+j];
|
||
}else{
|
||
if( src[i+j]=='\0' || ISSEPARATE(src[i+j])){
|
||
break;
|
||
}
|
||
output[j] = src[i+j];
|
||
}
|
||
}
|
||
output[j] = '\0';
|
||
return;
|
||
}
|
||
if( wordflag )
|
||
i++;
|
||
if ( i==0)
|
||
{
|
||
if( IS_2BYTEWORD(src[i]) ){
|
||
wordflag = 1;
|
||
i++;
|
||
}
|
||
else
|
||
{
|
||
wordflag = 0;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
*/
|
||
|
||
char* strncpy2( char* dest, const char* src, size_t n )
|
||
{
|
||
if( n > 0 ){
|
||
char* d = dest;
|
||
const char* s = src;
|
||
int i;
|
||
for( i=0; i<n ; i++ ){
|
||
if( *(s+i) == 0 ){
|
||
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƴ<EFBFBD><C6B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> NULL ٯë ľ<><C4BE> */
|
||
*(d+i) = '\0';
|
||
return dest;
|
||
}
|
||
if( *(s+i) & 0x80 ){
|
||
*(d+i) = *(s+i);
|
||
i++;
|
||
if( i>=n ){
|
||
*(d+i-1)='\0';
|
||
break;
|
||
}
|
||
*(d+i) = *(s+i);
|
||
}else
|
||
*(d+i) = *(s+i);
|
||
}
|
||
}
|
||
return dest;
|
||
}
|
||
|
||
void strncpysafe( char* dest , const size_t n ,
|
||
const char* src ,const int length )
|
||
{
|
||
/*
|
||
* src <20><><EFBFBD><EFBFBD> dest <20><> length <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||
* strcpy, strncpy ƥ<><C6A5> dest <20><>Ի <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڻ<EFBFBD>
|
||
* <20><><EFBFBD><EFBFBD><EFBFBD>ݱ<EFBFBD><DDB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,<2C><>ƹ<EFBFBD><C6B9>½ʧ<C2BD>ͱ<EFBFBD><CDB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>.
|
||
* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƥ<EFBFBD><C6A5><EFBFBD><EFBFBD>strlen( src ) <20><> length <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>лõ<D0BB>
|
||
* ( <20>˱<EFBFBD><CBB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>) <20><> dest <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ë <20>ͻ<EFBFBD><CDBB><EFBFBD>
|
||
* strcpysafe <20><> Ԫ<><D4AA>ë<EFBFBD><C3AB><EFBFBD>£<EFBFBD>
|
||
*/
|
||
|
||
int Short;
|
||
Short = min( strlen( src ) , length );
|
||
|
||
/* NULL ٯ ë<><C3AB> <20><><EFBFBD><EFBFBD> ʤ */
|
||
if( n < Short + 1 ){
|
||
/*
|
||
* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>뻥<EFBFBD><EBBBA5>Իئ<D4BB>м<EFBFBD>ƥ n - 1(NULL ٯ)
|
||
* ƥ strncpy ë<><C3AB><EFBFBD><EFBFBD>
|
||
*/
|
||
strncpy2( dest , src , n-1 );
|
||
dest[n-1]='\0';
|
||
|
||
}else if( n <= 0 ){
|
||
return;
|
||
}else{
|
||
/*
|
||
* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>뷴<EFBFBD><EBB7B4><EFBFBD>б<EFBFBD>ؤ<EFBFBD>¼<EFBFBD>ƥ Short ƥstrncpyë<79><C3AB><EFBFBD><EFBFBD>
|
||
* ئ<><D8A6> src <20>巴 Short <20><>Ӯ<EFBFBD><D3AE> <20><> NULL <20><>ئ<EFBFBD>м<EFBFBD>ƥ<EFBFBD><C6A5>
|
||
* dest <20>巴 ܰ<><DCB0><EFBFBD>ƻ<EFBFBD><C6BB><EFBFBD><EFBFBD>ʣ<EFBFBD>
|
||
*/
|
||
|
||
strncpy2( dest , src , Short );
|
||
dest[Short]= '\0';
|
||
|
||
}
|
||
}
|
||
|
||
void strcpysafe( char* dest ,size_t n ,const char* src )
|
||
{
|
||
/*
|
||
* src <20><><EFBFBD><EFBFBD> dest <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>.
|
||
* strcpy, strncpy ƥ<><C6A5> dest <20><>Ի <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڻ<EFBFBD>
|
||
* <20><><EFBFBD><EFBFBD><EFBFBD>ݱ<EFBFBD><DDB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,<2C><>ƹ<EFBFBD><C6B9>½ʧ<C2BD>ͱ<EFBFBD><CDB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>.
|
||
* <20><>ľë ʲ<>б<EFBFBD>, strncpy <20><>ؤ<EFBFBD>»<EFBFBD> strlen( src ) <20><> n <20><>Ի
|
||
* <20><><EFBFBD><EFBFBD><EFBFBD>ݱ巴, dest <20><> <20><> NULL ٯ<>練ئ<E7B7B4><D8A6>ئ<EFBFBD><D8A6>.
|
||
*
|
||
* <20><><EFBFBD><EFBFBD><D7BB>Ȼ<EFBFBD> dest <20><> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ի src <20><><EFBFBD>õ<EFBFBD><C3B5><EFBFBD>Ӯ<EFBFBD><D3AE><EFBFBD>ݱ巴
|
||
* n-1 ƥ strncpy ë<><C3AB><EFBFBD><EFBFBD>. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݷ<EFBFBD><DDB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||
*
|
||
* n <20><> <20><><EFBFBD>ݷ<EFBFBD><DDB7>ƾ<EFBFBD><C6BE><EFBFBD><EFBFBD><EFBFBD>ئ<EFBFBD>¼<EFBFBD>ƥ <20><><EFBFBD>ݷ<EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ئ<EFBFBD>У<EFBFBD>
|
||
*
|
||
*/
|
||
// Nuke +1 (08/25): Danger if src=0
|
||
if (!src) {
|
||
*dest = '\0';
|
||
return;
|
||
}
|
||
if( n <= 0 ) /* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ئ<EFBFBD><D8A6> */
|
||
return;
|
||
|
||
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƥ<EFBFBD><C6A5> n >= 1 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
||
/* NULL ٯë<D9AF><C3AB> <20>ƻ<EFBFBD> ʤ<><CAA4><EFBFBD><EFBFBD> */
|
||
else if( n < strlen( src ) + 1 ){
|
||
/*
|
||
* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>뻥<EFBFBD><EBBBA5>Իئ<D4BB>м<EFBFBD>ƥ n - 1(NULL ٯ)
|
||
* ƥ strncpy ë<><C3AB><EFBFBD><EFBFBD>
|
||
*/
|
||
strncpy2( dest , src , n-1 );
|
||
dest[n-1]='\0';
|
||
}else
|
||
strcpy( dest , src );
|
||
|
||
}
|
||
|
||
char * ScanOneByte( char *src, char delim )
|
||
{
|
||
// Nuke
|
||
if (!src) return NULL;
|
||
|
||
// ٯ <20><>ئ<EFBFBD><D8A6>ئ<EFBFBD><D8A6><EFBFBD><EFBFBD>ƥ<EFBFBD><C6A5><EFBFBD><EFBFBD>
|
||
for( ;src[0] != '\0'; src ++ ){
|
||
if( IS_2BYTEWORD( src[0] ) ){
|
||
// <20><>ʣ<EFBFBD>֣۹<D6A3><DBB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>б<EFBFBD><D0B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>£<EFBFBD>
|
||
// <20><EFBFBD><D7B7>ƨ<EFBFBD><C6A8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƾ<EFBFBD>ئ<EFBFBD><D8A6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ئ<EFBFBD><D8A6>
|
||
if( src[1] != 0 ){
|
||
src ++;
|
||
}
|
||
continue;
|
||
}
|
||
// ʣ<><CAA3><EFBFBD><EFBFBD><EFBFBD>ף۳<D7A3><DBB3><EFBFBD>ƥ<EFBFBD><C6A5><EFBFBD><EFBFBD> ٯ<><D9AF> ʤ
|
||
if( src[0] == delim ){
|
||
return src;
|
||
}
|
||
}
|
||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>έ<EFBFBD><CEAD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ئ<EFBFBD><D8A6><EFBFBD><EFBFBD><EFBFBD>ף<EFBFBD>
|
||
return NULL;
|
||
}
|
||
|
||
int easyGetTokenFromBuf( char* src ,char* delim ,int count, char* output , int len )
|
||
{//ttom this function all change,copy from the second
|
||
int i; /* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><> */
|
||
int length =0; /* <20><>Ի<EFBFBD><D4BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD> ٯ <20><>Ӯ<EFBFBD><D3AE> */
|
||
int addlen=0; /* <20><><EFBFBD><EFBFBD>ľ<EFBFBD><C4BE>Ӯ<EFBFBD><D3AE> */
|
||
int oneByteMode = 0; /* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƹ<EFBFBD><C6B9><EFBFBD><EFBFBD> */
|
||
|
||
if(strlen(src)==0)return 0;
|
||
|
||
if( strlen( delim ) == 1 ){ // <20><><EFBFBD><EFBFBD><F1BBA5A8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ئ<EFBFBD>ը<EFBFBD><D5A8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƹ<EFBFBD><C6B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||
oneByteMode = 1;// <20><><EFBFBD><EFBFBD><EFBFBD>˨<EFBFBD><CBA8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> ٯ<><D9AF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ئ<EFBFBD><D8A6>
|
||
}
|
||
for( i = 0 ; i < count ; i ++ ){
|
||
char* last;
|
||
src += addlen;/* <20>Ĺ<EFBFBD><C4B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӯ<EFBFBD><D3AE>ë<EFBFBD><C3AB><EFBFBD><EFBFBD> */
|
||
|
||
if( oneByteMode ){
|
||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƹ<EFBFBD><C6B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ճ<EFBFBD><D5B3><EFBFBD><EFBFBD><EFBFBD>ƥ<EFBFBD><C6A5><EFBFBD><EFBFBD>
|
||
last = ScanOneByte( src, delim[0] );
|
||
}else{
|
||
last = strstr( src , delim ); /* έ<><CEAD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
||
}
|
||
if( last == NULL ){
|
||
/*
|
||
* <20>Ĺ<EFBFBD><C4B9><EFBFBD><EFBFBD><EFBFBD>ئ<EFBFBD><D8A6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƥ<EFBFBD><C6A5><EFBFBD>ͻ<EFBFBD><CDBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƻ<EFBFBD> return<72><6E>
|
||
*/
|
||
strcpysafe( output , len, src );
|
||
|
||
if( i == count - 1 )
|
||
/*<2A>нﵤ<D0BD><EFB5A4><EFBFBD>Ĺ<EFBFBD><C4B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*/
|
||
return 1;
|
||
|
||
/*<2A>Ĺ<EFBFBD><C4B9><EFBFBD><EFBFBD><EFBFBD>ئ<EFBFBD><D8A6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*/
|
||
return 0;
|
||
}
|
||
|
||
/*
|
||
* <20>Ĺ<EFBFBD><C4B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20>ټ<EFBFBD><D9BC><EFBFBD> <20><><EFBFBD><EFBFBD>ë<EFBFBD>ƻ<EFBFBD><C6BB><EFBFBD>
|
||
* <20><><EFBFBD><EFBFBD>Ի<EFBFBD><D4BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ľ<EFBFBD><C4BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD> ٯ <20><>Ӯ<EFBFBD><D3AE>
|
||
*/
|
||
length = last - src;
|
||
|
||
/*
|
||
* <20>ݼ<EFBFBD><DDBC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>б<F3BCB0BF><D0B1>Ĺ<EFBFBD><C4B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӯ<EFBFBD><D3AE><EFBFBD><EFBFBD> delim <20><>Ӯ<EFBFBD><D3AE>ë<EFBFBD><C3AB><EFBFBD>ƻ<EFBFBD><C6BB><EFBFBD><EFBFBD><EFBFBD>
|
||
*/
|
||
addlen= length + strlen( delim );
|
||
}
|
||
strncpysafe( output, len , src,length );
|
||
|
||
return 1;
|
||
}
|
||
|