charset.c revision 251875
1184902Srwatson/* Licensed to the Apache Software Foundation (ASF) under one or more
2184902Srwatson * contributor license agreements.  See the NOTICE file distributed with
3184902Srwatson * this work for additional information regarding copyright ownership.
4184902Srwatson * The ASF licenses this file to You under the Apache License, Version 2.0
5184902Srwatson * (the "License"); you may not use this file except in compliance with
6184902Srwatson * the License.  You may obtain a copy of the License at
7184902Srwatson *
8184902Srwatson *     http://www.apache.org/licenses/LICENSE-2.0
9184902Srwatson *
10184902Srwatson * Unless required by applicable law or agreed to in writing, software
11184902Srwatson * distributed under the License is distributed on an "AS IS" BASIS,
12184902Srwatson * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13184902Srwatson * See the License for the specific language governing permissions and
14184902Srwatson * limitations under the License.
15184902Srwatson */
16184902Srwatson
17184902Srwatson#include "apr.h"
18184902Srwatson#include "apr_private.h"
19184902Srwatson#include "apr_strings.h"
20184902Srwatson#include "apr_portable.h"
21184902Srwatson
22184902Srwatson#ifdef HAVE_LANGINFO_H
23184902Srwatson#include <langinfo.h>
24184902Srwatson#endif
25184902Srwatson
26184902Srwatson/*
27184902Srwatson * simple heuristic to determine codepage of source code so that
28184902Srwatson * literal strings (e.g., "GET /\r\n") in source code can be translated
29184902Srwatson * properly
30189279Srwatson *
31191273Srwatson * If appropriate, a symbol can be set at configure time to determine
32184902Srwatson * this.  On EBCDIC platforms, it will be important how the code was
33184902Srwatson * unpacked.
34184902Srwatson */
35184902Srwatson
36APR_DECLARE(const char*) apr_os_default_encoding (apr_pool_t *pool)
37{
38#ifdef __MVS__
39#    ifdef __CODESET__
40        return __CODESET__;
41#    else
42        return "IBM-1047";
43#    endif
44#endif
45
46    if ('}' == 0xD0) {
47        return "IBM-1047";
48    }
49
50    if ('{' == 0xFB) {
51        return "EDF04";
52    }
53
54    if ('A' == 0xC1) {
55        return "EBCDIC"; /* not useful */
56    }
57
58    if ('A' == 0x41) {
59        return "ISO-8859-1"; /* not necessarily true */
60    }
61
62    return "unknown";
63}
64
65
66APR_DECLARE(const char*) apr_os_locale_encoding (apr_pool_t *pool)
67{
68#if defined(HAVE_NL_LANGINFO) && defined(CODESET)
69    const char *charset;
70
71    charset = nl_langinfo(CODESET);
72    if (charset && *charset) {
73#ifdef _OSD_POSIX /* Bug workaround - delete as soon as fixed in OSD_POSIX */
74        /* Some versions of OSD_POSIX return nl_langinfo(CODESET)="^[nN]" */
75        /* Ignore the bogus information and use apr_os_default_encoding() */
76        if (charset[0] != '^')
77#endif
78        return apr_pstrdup(pool, charset);
79    }
80#endif
81
82    return apr_os_default_encoding(pool);
83}
84