summaryrefslogtreecommitdiff
path: root/src/3rd_party/apache-log4cxx-0.10.0/src/main/cpp/charsetdecoder.cpp
blob: ea5a680a6c8f175bf89d1501509d8603daea83b3 (plain)
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include <log4cxx/logstring.h>
#include <log4cxx/helpers/charsetdecoder.h>
#include <log4cxx/helpers/bytebuffer.h>
#include <log4cxx/helpers/exception.h>
#include <log4cxx/helpers/mutex.h>
#include <log4cxx/helpers/synchronized.h>
#include <log4cxx/helpers/pool.h>
#include <apr_xlate.h>
#if !defined(LOG4CXX)
#define LOG4CXX 1
#endif
#include <log4cxx/private/log4cxx_private.h>
#include <locale.h>
#include <apr_portable.h>
#include <log4cxx/helpers/stringhelper.h>
#include <log4cxx/helpers/transcoder.h>

using namespace log4cxx;
using namespace log4cxx::helpers;

IMPLEMENT_LOG4CXX_OBJECT(CharsetDecoder)


namespace log4cxx
{
        namespace helpers {

#if APR_HAS_XLATE
          /**
           *  Converts from an arbitrary encoding to LogString
           *    using apr_xlate.  Requires real iconv implementation,
         *    apr-iconv will crash in use.
           */
          class APRCharsetDecoder : public CharsetDecoder
          {
          public:
           /**
            *  Creates a new instance.
            *  @param frompage name of source encoding.
            */
              APRCharsetDecoder(const LogString& frompage) : pool(), mutex(pool) {
#if LOG4CXX_LOGCHAR_IS_WCHAR
                const char* topage = "WCHAR_T";
#endif
#if LOG4CXX_LOGCHAR_IS_UTF8
                const char* topage = "UTF-8";
#endif
#if LOG4CXX_LOGCHAR_IS_UNICHAR
                const char* topage = "UTF-16";
#endif
                std::string fpage(Transcoder::encodeCharsetName(frompage));
                apr_status_t stat = apr_xlate_open(&convset,
                    topage,
                    fpage.c_str(),
                    pool.getAPRPool());
                if (stat != APR_SUCCESS) {
                    throw IllegalArgumentException(frompage);
                }
              }

           /**
            *  Destructor.
            */
              virtual ~APRCharsetDecoder() {
              }

              virtual log4cxx_status_t decode(ByteBuffer& in,
                  LogString& out) {
                  enum { BUFSIZE = 256 };
                  logchar buf[BUFSIZE];
                  const apr_size_t initial_outbytes_left = BUFSIZE * sizeof(logchar);
                  apr_status_t stat = APR_SUCCESS;
                  if (in.remaining() == 0) {
                    size_t outbytes_left = initial_outbytes_left;
                    {
                      synchronized sync(mutex);
                      stat = apr_xlate_conv_buffer((apr_xlate_t*) convset,
                        NULL, NULL, (char*) buf, &outbytes_left);
                    }
                    out.append(buf, (initial_outbytes_left - outbytes_left)/sizeof(logchar));
                  } else {
                    while(in.remaining() > 0 && stat == APR_SUCCESS) {
                      size_t inbytes_left = in.remaining();
                      size_t initial_inbytes_left = inbytes_left;
                      size_t pos = in.position();
                      apr_size_t outbytes_left = initial_outbytes_left;
                      {
                        synchronized sync(mutex);
                        stat = apr_xlate_conv_buffer((apr_xlate_t*) convset,
                           in.data() + pos,
                           &inbytes_left,
                           (char*) buf,
                           &outbytes_left);
                      }
                      out.append(buf, (initial_outbytes_left - outbytes_left)/sizeof(logchar));
                      in.position(pos + (initial_inbytes_left - inbytes_left));
                    }
                  }
                  return stat;
              }

          private:
                  APRCharsetDecoder(const APRCharsetDecoder&);
                  APRCharsetDecoder& operator=(const APRCharsetDecoder&);
                  log4cxx::helpers::Pool pool;
                  Mutex mutex;
                  apr_xlate_t *convset;
          };
          
#endif

#if LOG4CXX_LOGCHAR_IS_WCHAR && LOG4CXX_HAS_MBSRTOWCS
          /**
          *    Converts from the default multi-byte string to
          *        LogString using mbstowcs.
          *
          */
          class MbstowcsCharsetDecoder : public CharsetDecoder
          {
          public:
              MbstowcsCharsetDecoder() {
              }

              virtual ~MbstowcsCharsetDecoder() {
              }

          private:
              inline log4cxx_status_t append(LogString& out, const wchar_t* buf) {
                  out.append(buf);
                  return APR_SUCCESS;
              }

              virtual log4cxx_status_t decode(ByteBuffer& in,
                  LogString& out) {
                  log4cxx_status_t stat = APR_SUCCESS;
                  enum { BUFSIZE = 256 };
                  wchar_t buf[BUFSIZE];

                  mbstate_t mbstate;
                  memset(&mbstate, 0, sizeof(mbstate));

                  while(in.remaining() > 0) {
                      size_t requested = in.remaining();
                      if (requested > BUFSIZE - 1) {
                          requested = BUFSIZE - 1;
                      }

                      memset(buf, 0, BUFSIZE*sizeof(wchar_t));
                      const char* src = in.current();
                      if(*src == 0) {
                           out.append(1, (logchar) 0);
                           in.position(in.position() + 1);
                      } else {
                           size_t converted = mbsrtowcs(buf,
                               &src,
                               requested,
                               &mbstate);
                           if (converted == (size_t) -1) {
                               stat = APR_BADARG;
                               in.position(src - in.data());
                               break;
                           } else {
                               stat = append(out, buf);
                               in.position(in.position() + converted);
                           }
                      }
                  }
                  return stat;
              }



          private:
                  MbstowcsCharsetDecoder(const MbstowcsCharsetDecoder&);
                  MbstowcsCharsetDecoder& operator=(const MbstowcsCharsetDecoder&);
          };
#endif


          /**
          *    Decoder used when the external and internal charsets
          *    are the same.
          *
          */
          class TrivialCharsetDecoder : public CharsetDecoder
          {
          public:
              TrivialCharsetDecoder() {
              }

              virtual ~TrivialCharsetDecoder() {
              }

              virtual log4cxx_status_t decode(ByteBuffer& in,
                  LogString& out) {
                  size_t remaining = in.remaining();
              if( remaining > 0) {
               const logchar* src = (const logchar*) (in.data() + in.position());
               size_t count = remaining / sizeof(logchar);
               out.append(src, count);
               in.position(in.position() + remaining);
              }
                  return APR_SUCCESS;
              }



          private:
                  TrivialCharsetDecoder(const TrivialCharsetDecoder&);
                  TrivialCharsetDecoder& operator=(const TrivialCharsetDecoder&);
          };


#if LOG4CXX_LOGCHAR_IS_UTF8
typedef TrivialCharsetDecoder UTF8CharsetDecoder;
#else
/**
*    Converts from UTF-8 to std::wstring
*
*/
class UTF8CharsetDecoder : public CharsetDecoder
{
public:
    UTF8CharsetDecoder() {
    }

    virtual ~UTF8CharsetDecoder() {
    }

private:
    virtual log4cxx_status_t decode(ByteBuffer& in,
        LogString& out) {
        if (in.remaining() > 0) {
          std::string tmp(in.current(), in.remaining());
          std::string::const_iterator iter = tmp.begin();
          while(iter != tmp.end()) {
               unsigned int sv = Transcoder::decode(tmp, iter);
               if (sv == 0xFFFF) {
                   size_t offset = iter - tmp.begin();
                   in.position(in.position() + offset);
                   return APR_BADARG;
               } else {
                   Transcoder::encode(sv, out);
               }
          }
          in.position(in.limit());
        }
        return APR_SUCCESS;
    }

private:
        UTF8CharsetDecoder(const UTF8CharsetDecoder&);
        UTF8CharsetDecoder& operator=(const UTF8CharsetDecoder&);
};
#endif

/**
*    Converts from ISO-8859-1 to LogString.
*
*/
class ISOLatinCharsetDecoder : public CharsetDecoder
{
public:
    ISOLatinCharsetDecoder() {
    }

    virtual ~ISOLatinCharsetDecoder() {
    }

private:
    virtual log4cxx_status_t decode(ByteBuffer& in,
        LogString& out) {
        if (in.remaining() > 0) {

          const unsigned char* src = (unsigned char*) in.current();
          const unsigned char* srcEnd = src + in.remaining();
          while(src < srcEnd) {
             unsigned int sv = *(src++);
             Transcoder::encode(sv, out);
          }
          in.position(in.limit());
        }
        return APR_SUCCESS;
    }



private:
        ISOLatinCharsetDecoder(const ISOLatinCharsetDecoder&);
        ISOLatinCharsetDecoder& operator=(const ISOLatinCharsetDecoder&);
};


/**
*    Converts from US-ASCII to LogString.
*
*/
class USASCIICharsetDecoder : public CharsetDecoder
{
public:
    USASCIICharsetDecoder() {
    }

    virtual ~USASCIICharsetDecoder() {
    }

private:

  virtual log4cxx_status_t decode(ByteBuffer& in,
      LogString& out) {
      log4cxx_status_t stat = APR_SUCCESS;
      if (in.remaining() > 0) {

        const unsigned char* src = (unsigned char*) in.current();
        const unsigned char* srcEnd = src + in.remaining();
        while(src < srcEnd) {
           unsigned char sv = *src;
           if (sv < 0x80) {
              src++;
              Transcoder::encode(sv, out);
           } else {
             stat = APR_BADARG;
             break;
           }
        }
        in.position(src - (const unsigned char*) in.data());
      }
      return stat;
    }



private:
        USASCIICharsetDecoder(const USASCIICharsetDecoder&);
        USASCIICharsetDecoder& operator=(const USASCIICharsetDecoder&);
};

          /**
           *    Charset decoder that uses an embedded CharsetDecoder consistent
           *     with current locale settings.
           */
          class LocaleCharsetDecoder : public CharsetDecoder {
          public:
               LocaleCharsetDecoder() : pool(), mutex(pool), decoder(), encoding() {
               }
               virtual ~LocaleCharsetDecoder() {
               }
               virtual log4cxx_status_t decode(ByteBuffer& in,
                  LogString& out) {
                  const char* p = in.current();
                  size_t i = in.position();
#if !LOG4CXX_CHARSET_EBCDIC                  
                  for (; i < in.limit() && ((unsigned int) *p) < 0x80; i++, p++) {
                      out.append(1, *p);
                  }
                  in.position(i);
#endif                  
                  if (i < in.limit()) {
                           Pool subpool;
                           const char* enc = apr_os_locale_encoding(subpool.getAPRPool());
                           {
                                synchronized sync(mutex);
                                if (enc == 0) {
                                    if (decoder == 0) {
                                        encoding = "C";
                                        decoder = new USASCIICharsetDecoder();
                                    }
                                } else if (encoding != enc) {
                                    encoding = enc;
                                    try {
                                       LogString e;
                                       Transcoder::decode(encoding, e);
                                       decoder = getDecoder(e);
                                    } catch (IllegalArgumentException& ex) {
                                       decoder = new USASCIICharsetDecoder();
                                    }
                                }
                            }
                            return decoder->decode(in, out);        
                  }
                  return APR_SUCCESS;  
               }
          private:
               Pool pool;
               Mutex mutex;
               CharsetDecoderPtr decoder;
               std::string encoding;
          };



        } // namespace helpers

}  //namespace log4cxx


CharsetDecoder::CharsetDecoder() {
}


CharsetDecoder::~CharsetDecoder() {
}

CharsetDecoder* CharsetDecoder::createDefaultDecoder() {
#if LOG4CXX_CHARSET_UTF8
     return new UTF8CharsetDecoder();
#elif LOG4CXX_CHARSET_ISO88591 || defined(_WIN32_WCE)
     return new ISOLatinCharsetDecoder();
#elif LOG4CXX_CHARSET_USASCII
     return new USASCIICharsetDecoder();
#elif LOG4CXX_LOGCHAR_IS_WCHAR && LOG4CXX_HAS_MBSRTOWCS
    return new MbstowcsCharsetDecoder();
#else
    return new LocaleCharsetDecoder();
#endif
}

CharsetDecoderPtr CharsetDecoder::getDefaultDecoder() {
    static CharsetDecoderPtr decoder(createDefaultDecoder());
    //
    //  if invoked after static variable destruction
    //     (if logging is called in the destructor of a static object)
    //     then create a new decoder.
    //
    if (decoder == 0) {
       return createDefaultDecoder();
    }
    return decoder;
}

CharsetDecoderPtr CharsetDecoder::getUTF8Decoder() {
    static CharsetDecoderPtr decoder(new UTF8CharsetDecoder());
    //
    //  if invoked after static variable destruction
    //     (if logging is called in the destructor of a static object)
    //     then create a new decoder.
    //
    if (decoder == 0) {
       return new UTF8CharsetDecoder();
    }
    return decoder;
}

CharsetDecoderPtr CharsetDecoder::getISOLatinDecoder() {
    return new ISOLatinCharsetDecoder();
}


CharsetDecoderPtr CharsetDecoder::getDecoder(const LogString& charset) {
    if (StringHelper::equalsIgnoreCase(charset, LOG4CXX_STR("UTF-8"), LOG4CXX_STR("utf-8")) ||
        StringHelper::equalsIgnoreCase(charset, LOG4CXX_STR("UTF8"), LOG4CXX_STR("utf8"))) {
        return new UTF8CharsetDecoder();
    } else if (StringHelper::equalsIgnoreCase(charset, LOG4CXX_STR("C"), LOG4CXX_STR("c")) ||
        charset == LOG4CXX_STR("646") ||
        StringHelper::equalsIgnoreCase(charset, LOG4CXX_STR("US-ASCII"), LOG4CXX_STR("us-ascii")) ||
        StringHelper::equalsIgnoreCase(charset, LOG4CXX_STR("ISO646-US"), LOG4CXX_STR("iso646-US")) ||
        StringHelper::equalsIgnoreCase(charset, LOG4CXX_STR("ANSI_X3.4-1968"), LOG4CXX_STR("ansi_x3.4-1968"))) {
        return new USASCIICharsetDecoder();
    } else if (StringHelper::equalsIgnoreCase(charset, LOG4CXX_STR("ISO-8859-1"), LOG4CXX_STR("iso-8859-1")) ||
        StringHelper::equalsIgnoreCase(charset, LOG4CXX_STR("ISO-LATIN-1"), LOG4CXX_STR("iso-latin-1"))) {
        return new ISOLatinCharsetDecoder();
    }
#if APR_HAS_XLATE
    return new APRCharsetDecoder(charset);
#else    
    throw IllegalArgumentException(charset);
#endif
}