1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
|
/* -*- c-basic-offset:2; tab-width:2; indent-tabs-mode:nil -*- */
#ifndef __IM_COMMON_H__
#define __IM_COMMON_H__
static inline u_int im_convert_encoding(ef_parser_t *parser, ef_conv_t *conv, const u_char *from,
u_char **to, u_int from_len) {
u_int len;
u_int filled_len;
if (from == NULL || parser == NULL || conv == NULL) {
return 0;
}
*to = NULL;
len = 0;
(*parser->init)(parser);
(*parser->set_str)(parser, from, from_len);
(*conv->init)(conv);
#define UNIT__ 32
while (1) {
u_char *p;
if (!(p = realloc(*to, len + UNIT__ + 1))) {
#ifdef DEBUG
bl_warn_printf(BL_DEBUG_TAG " malloc failed.\n");
#endif
if (*to) {
free(*to);
}
return 0;
}
*to = p;
p += len;
filled_len = (*conv->convert)(conv, p, UNIT__, parser);
len += filled_len;
if (filled_len == 0 && parser->is_eos) {
/* finished converting */
break;
}
}
#undef UNIT__
if (len) {
(*to)[len] = '\0';
}
return len;
}
#endif
|