/** * Copyright (C) 2008 Happy Fish / YuQing * * FastDFS may be copied only under the terms of the GNU General * Public License V3, which may be found in the FastDFS source kit. * Please visit the FastDFS Home Page http://www.csource.org/ for more detail. **/ #include #include #include #include #include #include "logger.h" #include "shared_func.h" #include "char_converter.h" int char_converter_init_ex(FastCharConverter *pCharConverter, const FastCharPair *charPairs, const int count, const unsigned op) { int i; unsigned char src; if (count > FAST_MAX_CHAR_COUNT) { logError("file: "__FILE__", line: %d, " "count: %d is too large, exceeds %d!", __LINE__, count, FAST_MAX_CHAR_COUNT); return EINVAL; } memset(pCharConverter, 0, sizeof(FastCharConverter)); pCharConverter->count = count; for (i=0; ichar_table[src].op = op; pCharConverter->char_table[src].dest = charPairs[i].dest; } return 0; } int std_space_char_converter_init(FastCharConverter *pCharConverter, const unsigned char dest_base) { #define SPACE_CHAR_PAIR_COUNT1 7 int i; FastCharPair pairs[SPACE_CHAR_PAIR_COUNT1]; pairs[0].src = '\0'; pairs[1].src = '\t'; pairs[2].src = '\n'; pairs[3].src = '\v'; pairs[4].src = '\f'; pairs[5].src = '\r'; pairs[6].src = ' '; for (i=0; ichar_table[src].op != FAST_CHAR_OP_NONE) { --pCharConverter->count; } } else { if (pCharConverter->char_table[src].op == FAST_CHAR_OP_NONE) { ++pCharConverter->count; } } pCharConverter->char_table[src].op = op; pCharConverter->char_table[src].dest = dest; } int fast_char_convert(FastCharConverter *pCharConverter, const char *input, const int input_len, char *output, int *out_len, const int out_size) { int count; unsigned char *pi; unsigned char *po; unsigned char *end; int out_size_sub1; count = 0; po = (unsigned char *)output; if (out_size >= input_len) { end = (unsigned char *)input + input_len; } else { end = (unsigned char *)input + out_size; } for (pi=(unsigned char *)input; pichar_table[*pi].op != FAST_CHAR_OP_NONE) { if (pCharConverter->char_table[*pi].op == FAST_CHAR_OP_ADD_BACKSLASH) { break; } *po++ = pCharConverter->char_table[*pi].dest; ++count; } else { *po++ = *pi; } } if (pi == end) { *out_len = po - (unsigned char *)output; return count; } out_size_sub1 = out_size - 1; for (; pi= out_size_sub1) { logDebug("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; }