|
| 1 | +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 | + |
| 3 | +/* Fluent Bit |
| 4 | + * ========== |
| 5 | + * Copyright (C) 2019 The Fluent Bit Authors |
| 6 | + * Copyright (C) 2015-2018 Treasure Data Inc. |
| 7 | + * |
| 8 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | + * you may not use this file except in compliance with the License. |
| 10 | + * You may obtain a copy of the License at |
| 11 | + * |
| 12 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | + * |
| 14 | + * Unless required by applicable law or agreed to in writing, software |
| 15 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | + * See the License for the specific language governing permissions and |
| 18 | + * limitations under the License. |
| 19 | + */ |
| 20 | + |
| 21 | +#include <stdio.h> |
| 22 | +#include <iconv.h> |
| 23 | +#include <string.h> |
| 24 | +#include <time.h> |
| 25 | +#include <ctype.h> |
| 26 | + |
| 27 | +#include <fluent-bit/flb_macros.h> |
| 28 | +#include <fluent-bit/flb_config.h> |
| 29 | +#include <fluent-bit/flb_error.h> |
| 30 | +#include <fluent-bit/flb_iconv.h> |
| 31 | +#include <fluent-bit/flb_mem.h> |
| 32 | + |
| 33 | +struct flb_iconv *flb_iconv_open(char *to, char *from) { |
| 34 | + iconv_t ic; |
| 35 | + struct flb_iconv *c; |
| 36 | + ic = iconv_open(to,from); |
| 37 | + if (ic == (iconv_t) -1) { |
| 38 | + return NULL; |
| 39 | + } |
| 40 | + c = flb_calloc(sizeof(struct flb_iconv),1); |
| 41 | + c->conv = ic; |
| 42 | + return c; |
| 43 | +} |
| 44 | + |
| 45 | +void flb_iconv_close(struct flb_iconv *c) { |
| 46 | + if(c) { |
| 47 | + iconv_close(c->conv); |
| 48 | + flb_free(c); |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | + |
| 53 | +int flb_iconv_execute(struct flb_iconv *c, |
| 54 | + char *str, size_t slen, |
| 55 | + char **result, size_t *result_len, |
| 56 | + unsigned int flags) |
| 57 | +{ |
| 58 | + char *outbuf; |
| 59 | + char *inp; |
| 60 | + char *outp; |
| 61 | + size_t outlen; |
| 62 | + size_t outroom; |
| 63 | + size_t outleft; |
| 64 | + size_t ret; |
| 65 | + size_t inleft; |
| 66 | + size_t outdone; |
| 67 | + |
| 68 | + if(slen == 0) { |
| 69 | + if(flags & FLB_ICONV_ACCEPT_NOT_CHANGED) { |
| 70 | + *result = NULL; |
| 71 | + *result_len = 0; |
| 72 | + return FLB_ICONV_NOT_CHANGED; |
| 73 | + } else { |
| 74 | + outp = flb_malloc(1); |
| 75 | + if(outp == NULL) { |
| 76 | + return FLB_ICONV_FAILURE; |
| 77 | + } |
| 78 | + *result = outp; |
| 79 | + *result_len = 0; |
| 80 | + return FLB_ICONV_SUCCESS; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + inp = str; |
| 85 | + inleft = slen; |
| 86 | + |
| 87 | + outroom = slen + (slen / 2) + 3; // just add something |
| 88 | + outbuf = flb_malloc(outroom); |
| 89 | + if(outbuf == NULL) { |
| 90 | + return FLB_ICONV_FAILURE; |
| 91 | + } |
| 92 | + outp = outbuf; |
| 93 | + outlen = 0; |
| 94 | + outleft = outroom - 1; |
| 95 | + |
| 96 | + iconv(c->conv, NULL, NULL, NULL, NULL); |
| 97 | + |
| 98 | + while(inleft > 0) { |
| 99 | + ret = iconv(c->conv, &inp, &inleft, &outp, &outleft); |
| 100 | + if(ret == -1) { |
| 101 | + switch(errno) { |
| 102 | + case EILSEQ: // bad input sequence of char (ignore) |
| 103 | + flb_warn("[flb_iconv] bad input char 0x%02x", *inp & 0xff); |
| 104 | + inp++; |
| 105 | + inleft--; |
| 106 | + break; |
| 107 | + case EINVAL: // imcomplete char (ignore) |
| 108 | + flb_warn("[flb_iconv] incomplete char 0x%02x", *inp & 0xff); |
| 109 | + inp++; |
| 110 | + inleft--; |
| 111 | + break; |
| 112 | + |
| 113 | + case E2BIG: |
| 114 | + outdone = outp - outbuf; |
| 115 | + outroom = outroom * 2; |
| 116 | + outbuf = flb_realloc(outbuf, outroom); |
| 117 | + if(outbuf == NULL) { |
| 118 | + return FLB_ICONV_FAILURE; |
| 119 | + } |
| 120 | + outp = outbuf + outdone; |
| 121 | + outleft = outroom - outdone - 1; |
| 122 | + break; |
| 123 | + default: |
| 124 | + flb_error("[flb_iconv] unknown error: %d", errno); |
| 125 | + break; |
| 126 | + } |
| 127 | + } else { |
| 128 | + break; |
| 129 | + } |
| 130 | + } |
| 131 | + outdone = outp - outbuf; |
| 132 | + if((flags & FLB_ICONV_ACCEPT_NOT_CHANGED) && (slen == outdone) && (memcmp(str,outbuf,slen) == 0)) { |
| 133 | + flb_free(outbuf); |
| 134 | + *result = NULL; |
| 135 | + *result_len = 0; |
| 136 | + return FLB_ICONV_NOT_CHANGED; |
| 137 | + } else { |
| 138 | + *result = outbuf; |
| 139 | + *result_len = outdone; |
| 140 | + return FLB_ICONV_SUCCESS; |
| 141 | + } |
| 142 | +} |
| 143 | + |
0 commit comments