fast_char_convert function changed
parent
33085e9bf6
commit
562513b358
|
|
@ -85,83 +85,62 @@ void char_converter_set_pair_ex(FastCharConverter *pCharConverter,
|
||||||
}
|
}
|
||||||
|
|
||||||
int fast_char_convert(FastCharConverter *pCharConverter,
|
int fast_char_convert(FastCharConverter *pCharConverter,
|
||||||
char *text, int *text_len, const int max_size)
|
const char *input, const int input_len,
|
||||||
|
char *output, int *out_len, const int out_size)
|
||||||
{
|
{
|
||||||
int count;
|
int count;
|
||||||
unsigned char *p;
|
|
||||||
unsigned char *pi;
|
unsigned char *pi;
|
||||||
|
unsigned char *po;
|
||||||
unsigned char *end;
|
unsigned char *end;
|
||||||
char fixed_buff[16 * 1024];
|
int out_size_sub1;
|
||||||
char *buff;
|
|
||||||
int max_size_sub1;
|
|
||||||
int remain_len;
|
int remain_len;
|
||||||
|
|
||||||
if (pCharConverter->count <= 0) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
count = 0;
|
count = 0;
|
||||||
end = (unsigned char *)text + *text_len;
|
po = (unsigned char *)output;
|
||||||
for (p=(unsigned char *)text; p<end; p++)
|
if (out_size >= input_len) {
|
||||||
{
|
end = (unsigned char *)input + input_len;
|
||||||
if (pCharConverter->char_table[*p].op != FAST_CHAR_OP_NONE)
|
|
||||||
{
|
|
||||||
if (pCharConverter->char_table[*p].op == FAST_CHAR_OP_ADD_BACKSLASH) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
*p = pCharConverter->char_table[*p].dest;
|
|
||||||
++count;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
remain_len = end - p;
|
|
||||||
if (remain_len == 0) {
|
|
||||||
return count;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (remain_len < sizeof(fixed_buff)) {
|
|
||||||
buff = fixed_buff;
|
|
||||||
} else {
|
} else {
|
||||||
buff = (char *)malloc(remain_len);
|
end = (unsigned char *)input + out_size;
|
||||||
if (buff == NULL) {
|
|
||||||
logError("file: "__FILE__", line: %d, "
|
|
||||||
"malloc %d bytes fail", __LINE__, remain_len);
|
|
||||||
return count;
|
|
||||||
}
|
}
|
||||||
}
|
for (pi=(unsigned char *)input; pi<end; pi++) {
|
||||||
memcpy(buff, p, remain_len);
|
if (pCharConverter->char_table[*pi].op != FAST_CHAR_OP_NONE) {
|
||||||
|
if (pCharConverter->char_table[*pi].op == FAST_CHAR_OP_ADD_BACKSLASH) {
|
||||||
max_size_sub1 = max_size - 1;
|
|
||||||
end = (unsigned char *)buff + remain_len;
|
|
||||||
for (pi=(unsigned char *)buff; pi<end; pi++)
|
|
||||||
{
|
|
||||||
if (p - (unsigned char *)text >= max_size_sub1)
|
|
||||||
{
|
|
||||||
logWarning("file: "__FILE__", line: %d, "
|
|
||||||
"exceeds max size: %d", __LINE__, max_size);
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (pCharConverter->char_table[*pi].op != FAST_CHAR_OP_NONE)
|
|
||||||
{
|
|
||||||
if (pCharConverter->char_table[*pi].op == FAST_CHAR_OP_ADD_BACKSLASH)
|
|
||||||
{
|
|
||||||
*p++ = '\\';
|
|
||||||
}
|
|
||||||
|
|
||||||
*p++ = pCharConverter->char_table[*pi].dest;
|
*po++ = pCharConverter->char_table[*pi].dest;
|
||||||
++count;
|
++count;
|
||||||
}
|
} else {
|
||||||
else
|
*po++ = *pi;
|
||||||
{
|
|
||||||
*p++ = *pi;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (buff != fixed_buff) {
|
remain_len = end - pi;
|
||||||
free(buff);
|
if (remain_len == 0) {
|
||||||
}
|
*out_len = po - (unsigned char *)output;
|
||||||
*text_len = p - (unsigned char *)text;
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
|
out_size_sub1 = out_size - 1;
|
||||||
|
for (; pi<end; pi++) {
|
||||||
|
if (po - (unsigned char *)output >= out_size_sub1) {
|
||||||
|
logWarning("file: "__FILE__", line: %d, "
|
||||||
|
"exceeds max size: %d", __LINE__, out_size);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (pCharConverter->char_table[*pi].op != FAST_CHAR_OP_NONE) {
|
||||||
|
if (pCharConverter->char_table[*pi].op == FAST_CHAR_OP_ADD_BACKSLASH) {
|
||||||
|
*po++ = '\\';
|
||||||
|
}
|
||||||
|
|
||||||
|
*po++ = pCharConverter->char_table[*pi].dest;
|
||||||
|
++count;
|
||||||
|
} else {
|
||||||
|
*po++ = *pi;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
*out_len = po - (unsigned char *)output;
|
||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -97,13 +97,16 @@ void char_converter_set_pair_ex(FastCharConverter *pCharConverter,
|
||||||
* char convert function
|
* char convert function
|
||||||
* parameters:
|
* parameters:
|
||||||
* pCharConverter: the char converter
|
* pCharConverter: the char converter
|
||||||
* text: the text to convert (input and output)
|
* input: the input to convert
|
||||||
* text_len: the length of text (input and output)
|
* input_len: the length of input
|
||||||
* max_size: max buff size
|
* output: the input to convert
|
||||||
|
* out_len: the length of output
|
||||||
|
* out_size: output buff size
|
||||||
* return: converted char count
|
* return: converted char count
|
||||||
*/
|
*/
|
||||||
int fast_char_convert(FastCharConverter *pCharConverter,
|
int fast_char_convert(FastCharConverter *pCharConverter,
|
||||||
char *text, int *text_len, const int max_size);
|
const char *input, const int input_len,
|
||||||
|
char *output, int *out_len, const int out_size);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue