| 1 |
// This file is included in both the libgcobol and gcc/cobol compilations |
| 2 |
/* |
| 3 |
* Copyright (c) 2021-2025 Symas Corporation |
| 4 |
* |
| 5 |
* Redistribution and use in source and binary forms, with or without |
| 6 |
* modification, are permitted provided that the following conditions are |
| 7 |
* met: |
| 8 |
* |
| 9 |
* * Redistributions of source code must retain the above copyright |
| 10 |
* notice, this list of conditions and the following disclaimer. |
| 11 |
* * Redistributions in binary form must reproduce the above |
| 12 |
* copyright notice, this list of conditions and the following disclaimer |
| 13 |
* in the documentation and/or other materials provided with the |
| 14 |
* distribution. |
| 15 |
* * Neither the name of the Symas Corporation nor the names of its |
| 16 |
* contributors may be used to endorse or promote products derived from |
| 17 |
* this software without specific prior written permission. |
| 18 |
* |
| 19 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 |
*/ |
| 31 |
|
| 32 |
#include <iconv.h> |
| 33 |
|
| 34 |
#include <cctype> |
| 35 |
#include <clocale> |
| 36 |
#include <cstdio> |
| 37 |
#include <cstring> |
| 38 |
#include <ctime> |
| 39 |
|
| 40 |
#include <algorithm> |
| 41 |
#include <unordered_map> |
| 42 |
#include <vector> |
| 43 |
|
| 44 |
#include "ec.h" |
| 45 |
#include "common-defs.h" |
| 46 |
#include "io.h" |
| 47 |
#include "gcobolio.h" |
| 48 |
#include "charmaps.h" |
| 49 |
#include "valconv.h" |
| 50 |
|
| 51 |
// First: single-byte-coded (SBC) character sets: |
| 52 |
|
| 53 |
// 7-bit ASCII is a subset of the various ISO/IEC 8859 code pages. |
| 54 |
// 8859 is a subset of code page 1252. |
| 55 |
// CP1252 is informally, and improperly, known as the "ANSI" code set. In |
| 56 |
// modern usage, when somebody says "8859-1", they almost invariably are |
| 57 |
// referring to a CP1252 code set. |
| 58 |
|
| 59 |
// EBCDIC is also an SBC character set. IBM's original "international EBCDIC" |
| 60 |
// code set was Code Page 37, which did not have a Euro sign. Code Page 1140 |
| 61 |
// is the same as CP37, but with the Euro sign replacing the "universal |
| 62 |
// currency symbol" at position 0x9F. The table below maps the 256 values of |
| 63 |
// CodePage 1140 to the 256 values of CodePage 1252 in a way that allows for |
| 64 |
// "round trip" conversion without any loss. |
| 65 |
|
| 66 |
// See https://en.wikipedia.org/w/index.php?title=Code_page_37&oldid=1082467670, |
| 67 |
|
| 68 |
// The modern world increasingly uses UTF-8, which is in conflict with ordinary |
| 69 |
// COBOL's inherently single-byte nature. In UTF-8, the encoding for a Euro |
| 70 |
// sign is three bytes (U+20AC encodes to E2 A2 AC). In single-byte CP1252, the |
| 71 |
// Euro is encoded as 0x80. |
| 72 |
|
| 73 |
// So, we are going to assume that internally, the generated COBOL executable |
| 74 |
// operates in code page 1252 or [hopefully some day] code page 1140. |
| 75 |
|
| 76 |
// We will convert output, as in DISPLAY <something> from the internal character |
| 77 |
// set to the running machine's locale (for now, that locale will be assumed to |
| 78 |
// be 1252/8859 if it isn't UTF-8). |
| 79 |
|
| 80 |
// And we will take some pains to figure out if the source code file was done |
| 81 |
// as UTF-8; if not, we will assume 1252/8859-1 |
| 82 |
|
| 83 |
// __gg__ebcdic_codeset_in_use is the ultimate determinator of whether the |
| 84 |
// internal codeset is ASCII/CP1252 or EBCDIC/CP1140. |
| 85 |
bool __gg__ebcdic_codeset_in_use = false ; |
| 86 |
|
| 87 |
static text_codeset_t source_codeset = cs_cp1252_e; |
| 88 |
static text_codeset_t console_codeset = cs_default_e; |
| 89 |
|
| 90 |
#define UNICODE_REPLACEMENT 0xFFFD // This a white question mark in a black diamond |
| 91 |
#define ASCII_REPLACEMENT 0x87 // In CP1252, 0x87 is a double-dagger |
| 92 |
|
| 93 |
// This table is the default one-to-one mapping that's used, for example, when |
| 94 |
// starting with ASCII and doing ASCII comparisons: |
| 95 |
|
| 96 |
const unsigned short |
| 97 |
__gg__one_to_one_values[256] = |
| 98 |
{ |
| 99 |
0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F, |
| 100 |
0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1A,0x1B,0x1C,0x1D,0x1E,0x1F, |
| 101 |
0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0x2A,0x2B,0x2C,0x2D,0x2E,0x2F, |
| 102 |
0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x3A,0x3B,0x3C,0x3D,0x3E,0x3F, |
| 103 |
0x40,0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4A,0x4B,0x4C,0x4D,0x4E,0x4F, |
| 104 |
0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5A,0x5B,0x5C,0x5D,0x5E,0x5F, |
| 105 |
0x60,0x61,0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6A,0x6B,0x6C,0x6D,0x6E,0x6F, |
| 106 |
0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7A,0x7B,0x7C,0x7D,0x7E,0x7F, |
| 107 |
0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F, |
| 108 |
0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, |
| 109 |
0xA0,0xA1,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF, |
| 110 |
0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, |
| 111 |
0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, |
| 112 |
0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, |
| 113 |
0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xEC,0xED,0xEE,0xEF, |
| 114 |
0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF |
| 115 |
}; |
| 116 |
|
| 117 |
// This table can be used for converting EBCDIC values to CP1252. |
| 118 |
|
| 119 |
// There is an unfortunate caveat, one that undoubtedly will have unintended |
| 120 |
// consequences. But COBOL has has the concept of a HIGH-VALUE, a character |
| 121 |
// that theoretically tests alphanumercially greater than all other characters. |
| 122 |
// In the CP1252 code page, the default HIGH-VALUE (it can be changed by the |
| 123 |
// ALPHABET clause is 0xFF, which is displayed as the character 'ÿ'). In the |
| 124 |
// EBCDIC code page 1140, that character is an EO control code. |
| 125 |
|
| 126 |
// So. In order that the default HIGH-VALUE once and always is 0xFF, these |
| 127 |
// two tables have been modified slightly so that 0xFF always maps to 0xFF |
| 128 |
|
| 129 |
// Programmers who use the ALPHABET clause to change the HIGH-VALUE are on their |
| 130 |
// own. |
| 131 |
|
| 132 |
|
| 133 |
const unsigned short |
| 134 |
__gg__cp1140_to_cp1252_values[256] = |
| 135 |
{ |
| 136 |
0x00, 0x01, 0x02, 0x03, 0x9C, 0x09, 0x86, 0x7F, 0x97, 0x8D, 0x8E, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, |
| 137 |
0x10, 0x11, 0x12, 0x13, 0x9D, 0x85, 0x08, 0x87, 0x18, 0x19, 0x92, 0x8F, 0x1C, 0x1D, 0x1E, 0x1F, |
| 138 |
0xA4, 0x81, 0x82, 0x83, 0x84, 0x0A, 0x17, 0x1B, 0x88, 0x89, 0x8A, 0x8B, 0x8C, 0x05, 0x06, 0x07, |
| 139 |
0x90, 0x91, 0x16, 0x93, 0x94, 0x95, 0x96, 0x04, 0x98, 0x99, 0x9A, 0x9B, 0x14, 0x15, 0x9E, 0x1A, |
| 140 |
0x20, 0xA0, 0xE2, 0xE4, 0xE0, 0xE1, 0xE3, 0xE5, 0xE7, 0xF1, 0xA2, 0x2E, 0x3C, 0x28, 0x2B, 0x7C, |
| 141 |
0x26, 0xE9, 0xEA, 0xEB, 0xE8, 0xED, 0xEE, 0xEF, 0xEC, 0xDF, 0x21, 0x24, 0x2A, 0x29, 0x3B, 0xAC, |
| 142 |
0x2D, 0x2F, 0xC2, 0xC4, 0xC0, 0xC1, 0xC3, 0xC5, 0xC7, 0xD1, 0xA6, 0x2C, 0x25, 0x5F, 0x3E, 0x3F, |
| 143 |
0xF8, 0xC9, 0xCA, 0xCB, 0xC8, 0xCD, 0xCE, 0xCF, 0xCC, 0x60, 0x3A, 0x23, 0x40, 0x27, 0x3D, 0x22, |
| 144 |
0xD8, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0xAB, 0xBB, 0xF0, 0xFD, 0xFE, 0xB1, |
| 145 |
0xB0, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, 0x70, 0x71, 0x72, 0xAA, 0xBA, 0xE6, 0xB8, 0xC6, 0x80, |
| 146 |
0xB5, 0x7E, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A, 0xA1, 0xBF, 0xD0, 0xDD, 0xDE, 0xAE, |
| 147 |
0x5E, 0xA3, 0xA5, 0xB7, 0xA9, 0xA7, 0xB6, 0xBC, 0xBD, 0xBE, 0x5B, 0x5D, 0xAF, 0xA8, 0xB4, 0xD7, |
| 148 |
0x7B, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0xAD, 0xF4, 0xF6, 0xF2, 0xF3, 0xF5, |
| 149 |
0x7D, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50, 0x51, 0x52, 0xB9, 0xFB, 0xFC, 0xF9, 0xFA, 0xFF, |
| 150 |
0x5C, 0xF7, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0xB2, 0xD4, 0xD6, 0xD2, 0xD3, 0xD5, |
| 151 |
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0xB3, 0xDB, 0xDC, 0xD9, 0xDA, /*0x9F*/ 0xFF, |
| 152 |
}; |
| 153 |
|
| 154 |
// This table is the mirror image of cp1140_to_cp1252_values, except for the |
| 155 |
// above-mentioned 0xFF |
| 156 |
const unsigned short |
| 157 |
__gg__cp1252_to_cp1140_values[256] = |
| 158 |
{ |
| 159 |
0x00, 0x01, 0x02, 0x03, 0x37, 0x2D, 0x2E, 0x2F, 0x16, 0x05, 0x25, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, |
| 160 |
0x10, 0x11, 0x12, 0x13, 0x3C, 0x3D, 0x32, 0x26, 0x18, 0x19, 0x3F, 0x27, 0x1C, 0x1D, 0x1E, 0x1F, |
| 161 |
0x40, 0x5A, 0x7F, 0x7B, 0x5B, 0x6C, 0x50, 0x7D, 0x4D, 0x5D, 0x5C, 0x4E, 0x6B, 0x60, 0x4B, 0x61, |
| 162 |
0xF0, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8, 0xF9, 0x7A, 0x5E, 0x4C, 0x7E, 0x6E, 0x6F, |
| 163 |
0x7C, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8, 0xC9, 0xD1, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6, |
| 164 |
0xD7, 0xD8, 0xD9, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0xE9, 0xBA, 0xE0, 0xBB, 0xB0, 0x6D, |
| 165 |
0x79, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, |
| 166 |
0x97, 0x98, 0x99, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7, 0xA8, 0xA9, 0xC0, 0x4F, 0xD0, 0xA1, 0x07, |
| 167 |
0x9F, 0x21, 0x22, 0x23, 0x24, 0x15, 0x06, 0x17, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x09, 0x0A, 0x1B, |
| 168 |
0x30, 0x31, 0x1A, 0x33, 0x34, 0x35, 0x36, 0x08, 0x38, 0x39, 0x3A, 0x3B, 0x04, 0x14, 0x3E, 0xFF, |
| 169 |
0x41, 0xAA, 0x4A, 0xB1, 0x20, 0xB2, 0x6A, 0xB5, 0xBD, 0xB4, 0x9A, 0x8A, 0x5F, 0xCA, 0xAF, 0xBC, |
| 170 |
0x90, 0x8F, 0xEA, 0xFA, 0xBE, 0xA0, 0xB6, 0xB3, 0x9D, 0xDA, 0x9B, 0x8B, 0xB7, 0xB8, 0xB9, 0xAB, |
| 171 |
0x64, 0x65, 0x62, 0x66, 0x63, 0x67, 0x9E, 0x68, 0x74, 0x71, 0x72, 0x73, 0x78, 0x75, 0x76, 0x77, |
| 172 |
0xAC, 0x69, 0xED, 0xEE, 0xEB, 0xEF, 0xEC, 0xBF, 0x80, 0xFD, 0xFE, 0xFB, 0xFC, 0xAD, 0xAE, 0x59, |
| 173 |
0x44, 0x45, 0x42, 0x46, 0x43, 0x47, 0x9C, 0x48, 0x54, 0x51, 0x52, 0x53, 0x58, 0x55, 0x56, 0x57, |
| 174 |
0x8C, 0x49, 0xCD, 0xCE, 0xCB, 0xCF, 0xCC, 0xE1, 0x70, 0xDD, 0xDE, 0xDB, 0xDC, 0x8D, 0x8E, /*0xDF*/ 0xFF, |
| 175 |
}; |
| 176 |
|
| 177 |
// This is the EBCDIC collating sequence when the internal character set is CP1252. It's actually |
| 178 |
// a copy of __gg__cp1252_to_cp1140_values, but modified so that 0xFF maps to 0xFF. |
| 179 |
// Doing this meant swapping the CP1252 upper-Y-umlaut with lower-Y-umlaut. |
| 180 |
const unsigned short |
| 181 |
__gg__cp1252_to_ebcdic_collation[256] = |
| 182 |
{ |
| 183 |
0x00, 0x01, 0x02, 0x03, 0x37, 0x2d, 0x2e, 0x2f, 0x16, 0x05, 0x25, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, |
| 184 |
0x10, 0x11, 0x12, 0x13, 0x3c, 0x3d, 0x32, 0x26, 0x18, 0x19, 0x3f, 0x27, 0x1c, 0x1d, 0x1e, 0x1f, |
| 185 |
0x40, 0x5a, 0x7f, 0x7b, 0x5b, 0x6c, 0x50, 0x7d, 0x4d, 0x5d, 0x5c, 0x4e, 0x6b, 0x60, 0x4b, 0x61, |
| 186 |
0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0x7a, 0x5e, 0x4c, 0x7e, 0x6e, 0x6f, |
| 187 |
0x7c, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, |
| 188 |
0xd7, 0xd8, 0xd9, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xba, 0xe0, 0xbb, 0xb0, 0x6d, |
| 189 |
0x79, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, |
| 190 |
0x97, 0x98, 0x99, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xc0, 0x4f, 0xd0, 0xa1, 0x07, |
| 191 |
0x9f, 0x21, 0x22, 0x23, 0x24, 0x15, 0x06, 0x17, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x09, 0x0a, 0x1b, |
| 192 |
0x30, 0x31, 0x1a, 0x33, 0x34, 0x35, 0x36, 0x08, 0x38, 0x39, 0x3a, 0x3b, 0x04, 0x14, 0x3e, 0xdf, |
| 193 |
0x41, 0xaa, 0x4a, 0xb1, 0x20, 0xb2, 0x6a, 0xb5, 0xbd, 0xb4, 0x9a, 0x8a, 0x5f, 0xca, 0xaf, 0xbc, |
| 194 |
0x90, 0x8f, 0xea, 0xfa, 0xbe, 0xa0, 0xb6, 0xb3, 0x9d, 0xda, 0x9b, 0x8b, 0xb7, 0xb8, 0xb9, 0xab, |
| 195 |
0x64, 0x65, 0x62, 0x66, 0x63, 0x67, 0x9e, 0x68, 0x74, 0x71, 0x72, 0x73, 0x78, 0x75, 0x76, 0x77, |
| 196 |
0xac, 0x69, 0xed, 0xee, 0xeb, 0xef, 0xec, 0xbf, 0x80, 0xfd, 0xfe, 0xfb, 0xfc, 0xad, 0xae, 0x59, |
| 197 |
0x44, 0x45, 0x42, 0x46, 0x43, 0x47, 0x9c, 0x48, 0x54, 0x51, 0x52, 0x53, 0x58, 0x55, 0x56, 0x57, |
| 198 |
0x8c, 0x49, 0xcd, 0xce, 0xcb, 0xcf, 0xcc, 0xe1, 0x70, 0xdd, 0xde, 0xdb, 0xdc, 0x8d, 0x8e, 0xff, |
| 199 |
}; |
| 200 |
|
| 201 |
// When using the EBCDIC internal character set, but if told to use the ASCII collating sequence, |
| 202 |
// this table can be used. It's based on the __gg__cp1140_to_cp1252_values, but with the two |
| 203 |
// characters at locations DF and FF swapped so that the HIGH-VALUE 0xFF maps to 0xFF. |
| 204 |
const unsigned short |
| 205 |
__gg__ebcdic_to_cp1252_collation[256] = |
| 206 |
{ |
| 207 |
0x00, 0x01, 0x02, 0x03, 0x9C, 0x09, 0x86, 0x7F, 0x97, 0x8D, 0x8E, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, |
| 208 |
0x10, 0x11, 0x12, 0x13, 0x9D, 0x85, 0x08, 0x87, 0x18, 0x19, 0x92, 0x8F, 0x1C, 0x1D, 0x1E, 0x1F, |
| 209 |
0xA4, 0x81, 0x82, 0x83, 0x84, 0x0A, 0x17, 0x1B, 0x88, 0x89, 0x8A, 0x8B, 0x8C, 0x05, 0x06, 0x07, |
| 210 |
0x90, 0x91, 0x16, 0x93, 0x94, 0x95, 0x96, 0x04, 0x98, 0x99, 0x9A, 0x9B, 0x14, 0x15, 0x9E, 0x1A, |
| 211 |
0x20, 0xA0, 0xE2, 0xE4, 0xE0, 0xE1, 0xE3, 0xE5, 0xE7, 0xF1, 0xA2, 0x2E, 0x3C, 0x28, 0x2B, 0x7C, |
| 212 |
0x26, 0xE9, 0xEA, 0xEB, 0xE8, 0xED, 0xEE, 0xEF, 0xEC, 0xDF, 0x21, 0x24, 0x2A, 0x29, 0x3B, 0xAC, |
| 213 |
0x2D, 0x2F, 0xC2, 0xC4, 0xC0, 0xC1, 0xC3, 0xC5, 0xC7, 0xD1, 0xA6, 0x2C, 0x25, 0x5F, 0x3E, 0x3F, |
| 214 |
0xF8, 0xC9, 0xCA, 0xCB, 0xC8, 0xCD, 0xCE, 0xCF, 0xCC, 0x60, 0x3A, 0x23, 0x40, 0x27, 0x3D, 0x22, |
| 215 |
0xD8, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0xAB, 0xBB, 0xF0, 0xFD, 0xFE, 0xB1, |
| 216 |
0xB0, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, 0x70, 0x71, 0x72, 0xAA, 0xBA, 0xE6, 0xB8, 0xC6, 0x80, |
| 217 |
0xB5, 0x7E, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A, 0xA1, 0xBF, 0xD0, 0xDD, 0xDE, 0xAE, |
| 218 |
0x5E, 0xA3, 0xA5, 0xB7, 0xA9, 0xA7, 0xB6, 0xBC, 0xBD, 0xBE, 0x5B, 0x5D, 0xAF, 0xA8, 0xB4, 0xD7, |
| 219 |
0x7B, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0xAD, 0xF4, 0xF6, 0xF2, 0xF3, 0xF5, |
| 220 |
0x7D, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50, 0x51, 0x52, 0xB9, 0xFB, 0xFC, 0xF9, 0xFA, 0xDF, |
| 221 |
0x5C, 0xF7, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0xB2, 0xD4, 0xD6, 0xD2, 0xD3, 0xD5, |
| 222 |
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0xB3, 0xDB, 0xDC, 0xD9, 0xDA, 0xFF, |
| 223 |
}; |
| 224 |
|
| 225 |
// This table is used for converting code page 1252 to the subset of UTF-8 that |
| 226 |
// that contains CP1252 |
| 227 |
|
| 228 |
static const unsigned short |
| 229 |
cp1252_to_utf8_values[256] = |
| 230 |
{ |
| 231 |
0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007, 0x0008, 0x0009, 0x000a, 0x000b, 0x000c, 0x000d, 0x000e, 0x000f, // 00 |
| 232 |
0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017, 0x0018, 0x0019, 0x001a, 0x001b, 0x001c, 0x001d, 0x001e, 0x001f, // 10 |
| 233 |
0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027, 0x0028, 0x0029, 0x002a, 0x002b, 0x002c, 0x002d, 0x002e, 0x002f, // 20 |
| 234 |
0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037, 0x0038, 0x0039, 0x003a, 0x003b, 0x003c, 0x003d, 0x003e, 0x003f, // 30 |
| 235 |
0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047, 0x0048, 0x0049, 0x004a, 0x004b, 0x004c, 0x004d, 0x004e, 0x004f, // 40 |
| 236 |
0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, 0x0058, 0x0059, 0x005a, 0x005b, 0x005c, 0x005d, 0x005e, 0x005f, // 50 |
| 237 |
0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067, 0x0068, 0x0069, 0x006a, 0x006b, 0x006c, 0x006d, 0x006e, 0x006f, // 60 |
| 238 |
0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077, 0x0078, 0x0079, 0x007a, 0x007b, 0x007c, 0x007d, 0x007e, 0x007f, // 70 |
| 239 |
0x20ac, 0x0081, 0x201a, 0x0192, 0x201e, 0x2026, 0x2020, 0x2021, 0x02c6, 0x2030, 0x0160, 0x2039, 0x0152, 0x008d, 0x017d, 0x008f, // 80 |
| 240 |
0x0090, 0x2018, 0x2019, 0x201c, 0x201d, 0x2022, 0x2013, 0x2014, 0x02dc, 0x2122, 0x0161, 0x203a, 0x0153, 0x009d, 0x017e, 0x0178, // 90 |
| 241 |
0x00a0, 0x00a1, 0x00a2, 0x00a3, 0x00a4, 0x00a5, 0x00a6, 0x00a7, 0x00a8, 0x00a9, 0x00aa, 0x00ab, 0x00ac, 0x00ad, 0x00ae, 0x00af, // A0 |
| 242 |
0x00b0, 0x00b1, 0x00b2, 0x00b3, 0x00b4, 0x00b5, 0x00b6, 0x00b7, 0x00b8, 0x00b9, 0x00ba, 0x00bb, 0x00bc, 0x00bd, 0x00be, 0x00bf, // B0 |
| 243 |
0x00c0, 0x00c1, 0x00c2, 0x00c3, 0x00c4, 0x00c5, 0x00c6, 0x00c7, 0x00c8, 0x00c9, 0x00ca, 0x00cb, 0x00cc, 0x00cd, 0x00ce, 0x00cf, // C0 |
| 244 |
0x00d0, 0x00d1, 0x00d2, 0x00d3, 0x00d4, 0x00d5, 0x00d6, 0x00d7, 0x00d8, 0x00d9, 0x00da, 0x00db, 0x00dc, 0x00dd, 0x00de, 0x00df, // D0 |
| 245 |
0x00e0, 0x00e1, 0x00e2, 0x00e3, 0x00e4, 0x00e5, 0x00e6, 0x00e7, 0x00e8, 0x00e9, 0x00ea, 0x00eb, 0x00ec, 0x00ed, 0x00ee, 0x00ef, // E0 |
| 246 |
0x00f0, 0x00f1, 0x00f2, 0x00f3, 0x00f4, 0x00f5, 0x00f6, 0x00f7, 0x00f8, 0x00f9, 0x00fa, 0x00fb, 0x00fc, 0x00fd, 0x00fe, 0x00ff, // F0 |
| 247 |
}; |
| 248 |
|
| 249 |
// This map table does the reverse UTF-8 conversion back to cp1252 |
| 250 |
static const std::unordered_map<unsigned short, unsigned char>utf8_to_cp1252_values = |
| 251 |
{ |
| 252 |
{0x0000, 0x00}, {0x0001, 0x01}, {0x0002, 0x02}, {0x0003, 0x03}, {0x0004, 0x04}, {0x0005, 0x05}, {0x0006, 0x06}, {0x0007, 0x07}, |
| 253 |
{0x0008, 0x08}, {0x0009, 0x09}, {0x000a, 0x0a}, {0x000b, 0x0b}, {0x000c, 0x0c}, {0x000d, 0x0d}, {0x000e, 0x0e}, {0x000f, 0x0f}, |
| 254 |
{0x0010, 0x10}, {0x0011, 0x11}, {0x0012, 0x12}, {0x0013, 0x13}, {0x0014, 0x14}, {0x0015, 0x15}, {0x0016, 0x16}, {0x0017, 0x17}, |
| 255 |
{0x0018, 0x18}, {0x0019, 0x19}, {0x001a, 0x1a}, {0x001b, 0x1b}, {0x001c, 0x1c}, {0x001d, 0x1d}, {0x001e, 0x1e}, {0x001f, 0x1f}, |
| 256 |
{0x0020, 0x20}, {0x0021, 0x21}, {0x0022, 0x22}, {0x0023, 0x23}, {0x0024, 0x24}, {0x0025, 0x25}, {0x0026, 0x26}, {0x0027, 0x27}, |
| 257 |
{0x0028, 0x28}, {0x0029, 0x29}, {0x002a, 0x2a}, {0x002b, 0x2b}, {0x002c, 0x2c}, {0x002d, 0x2d}, {0x002e, 0x2e}, {0x002f, 0x2f}, |
| 258 |
{0x0030, 0x30}, {0x0031, 0x31}, {0x0032, 0x32}, {0x0033, 0x33}, {0x0034, 0x34}, {0x0035, 0x35}, {0x0036, 0x36}, {0x0037, 0x37}, |
| 259 |
{0x0038, 0x38}, {0x0039, 0x39}, {0x003a, 0x3a}, {0x003b, 0x3b}, {0x003c, 0x3c}, {0x003d, 0x3d}, {0x003e, 0x3e}, {0x003f, 0x3f}, |
| 260 |
{0x0040, 0x40}, {0x0041, 0x41}, {0x0042, 0x42}, {0x0043, 0x43}, {0x0044, 0x44}, {0x0045, 0x45}, {0x0046, 0x46}, {0x0047, 0x47}, |
| 261 |
{0x0048, 0x48}, {0x0049, 0x49}, {0x004a, 0x4a}, {0x004b, 0x4b}, {0x004c, 0x4c}, {0x004d, 0x4d}, {0x004e, 0x4e}, {0x004f, 0x4f}, |
| 262 |
{0x0050, 0x50}, {0x0051, 0x51}, {0x0052, 0x52}, {0x0053, 0x53}, {0x0054, 0x54}, {0x0055, 0x55}, {0x0056, 0x56}, {0x0057, 0x57}, |
| 263 |
{0x0058, 0x58}, {0x0059, 0x59}, {0x005a, 0x5a}, {0x005b, 0x5b}, {0x005c, 0x5c}, {0x005d, 0x5d}, {0x005e, 0x5e}, {0x005f, 0x5f}, |
| 264 |
{0x0060, 0x60}, {0x0061, 0x61}, {0x0062, 0x62}, {0x0063, 0x63}, {0x0064, 0x64}, {0x0065, 0x65}, {0x0066, 0x66}, {0x0067, 0x67}, |
| 265 |
{0x0068, 0x68}, {0x0069, 0x69}, {0x006a, 0x6a}, {0x006b, 0x6b}, {0x006c, 0x6c}, {0x006d, 0x6d}, {0x006e, 0x6e}, {0x006f, 0x6f}, |
| 266 |
{0x0070, 0x70}, {0x0071, 0x71}, {0x0072, 0x72}, {0x0073, 0x73}, {0x0074, 0x74}, {0x0075, 0x75}, {0x0076, 0x76}, {0x0077, 0x77}, |
| 267 |
{0x0078, 0x78}, {0x0079, 0x79}, {0x007a, 0x7a}, {0x007b, 0x7b}, {0x007c, 0x7c}, {0x007d, 0x7d}, {0x007e, 0x7e}, {0x007f, 0x7f}, |
| 268 |
{0x20ac, 0x80}, {0x0081, 0x81}, {0x201a, 0x82}, {0x0192, 0x83}, {0x201e, 0x84}, {0x2026, 0x85}, {0x2020, 0x86}, {0x2021, 0x87}, |
| 269 |
{0x02c6, 0x88}, {0x2030, 0x89}, {0x0160, 0x8a}, {0x2039, 0x8b}, {0x0152, 0x8c}, {0x008d, 0x8d}, {0x017d, 0x8e}, {0x008f, 0x8f}, |
| 270 |
{0x0090, 0x90}, {0x2018, 0x91}, {0x2019, 0x92}, {0x201c, 0x93}, {0x201d, 0x94}, {0x2022, 0x95}, {0x2013, 0x96}, {0x2014, 0x97}, |
| 271 |
{0x02dc, 0x98}, {0x2122, 0x99}, {0x0161, 0x9a}, {0x203a, 0x9b}, {0x0153, 0x9c}, {0x009d, 0x9d}, {0x017e, 0x9e}, {0x0178, 0x9f}, |
| 272 |
{0x00a0, 0xa0}, {0x00a1, 0xa1}, {0x00a2, 0xa2}, {0x00a3, 0xa3}, {0x00a4, 0xa4}, {0x00a5, 0xa5}, {0x00a6, 0xa6}, {0x00a7, 0xa7}, |
| 273 |
{0x00a8, 0xa8}, {0x00a9, 0xa9}, {0x00aa, 0xaa}, {0x00ab, 0xab}, {0x00ac, 0xac}, {0x00ad, 0xad}, {0x00ae, 0xae}, {0x00af, 0xaf}, |
| 274 |
{0x00b0, 0xb0}, {0x00b1, 0xb1}, {0x00b2, 0xb2}, {0x00b3, 0xb3}, {0x00b4, 0xb4}, {0x00b5, 0xb5}, {0x00b6, 0xb6}, {0x00b7, 0xb7}, |
| 275 |
{0x00b8, 0xb8}, {0x00b9, 0xb9}, {0x00ba, 0xba}, {0x00bb, 0xbb}, {0x00bc, 0xbc}, {0x00bd, 0xbd}, {0x00be, 0xbe}, {0x00bf, 0xbf}, |
| 276 |
{0x00c0, 0xc0}, {0x00c1, 0xc1}, {0x00c2, 0xc2}, {0x00c3, 0xc3}, {0x00c4, 0xc4}, {0x00c5, 0xc5}, {0x00c6, 0xc6}, {0x00c7, 0xc7}, |
| 277 |
{0x00c8, 0xc8}, {0x00c9, 0xc9}, {0x00ca, 0xca}, {0x00cb, 0xcb}, {0x00cc, 0xcc}, {0x00cd, 0xcd}, {0x00ce, 0xce}, {0x00cf, 0xcf}, |
| 278 |
{0x00d0, 0xd0}, {0x00d1, 0xd1}, {0x00d2, 0xd2}, {0x00d3, 0xd3}, {0x00d4, 0xd4}, {0x00d5, 0xd5}, {0x00d6, 0xd6}, {0x00d7, 0xd7}, |
| 279 |
{0x00d8, 0xd8}, {0x00d9, 0xd9}, {0x00da, 0xda}, {0x00db, 0xdb}, {0x00dc, 0xdc}, {0x00dd, 0xdd}, {0x00de, 0xde}, {0x00df, 0xdf}, |
| 280 |
{0x00e0, 0xe0}, {0x00e1, 0xe1}, {0x00e2, 0xe2}, {0x00e3, 0xe3}, {0x00e4, 0xe4}, {0x00e5, 0xe5}, {0x00e6, 0xe6}, {0x00e7, 0xe7}, |
| 281 |
{0x00e8, 0xe8}, {0x00e9, 0xe9}, {0x00ea, 0xea}, {0x00eb, 0xeb}, {0x00ec, 0xec}, {0x00ed, 0xed}, {0x00ee, 0xee}, {0x00ef, 0xef}, |
| 282 |
{0x00f0, 0xf0}, {0x00f1, 0xf1}, {0x00f2, 0xf2}, {0x00f3, 0xf3}, {0x00f4, 0xf4}, {0x00f5, 0xf5}, {0x00f6, 0xf6}, {0x00f7, 0xf7}, |
| 283 |
{0x00f8, 0xf8}, {0x00f9, 0xf9}, {0x00fa, 0xfa}, {0x00fb, 0xfb}, {0x00fc, 0xfc}, {0x00fd, 0xfd}, {0x00fe, 0xfe}, {0x00ff, 0xff}, |
| 284 |
}; |
| 285 |
|
| 286 |
// This function extracts the next unicode code point from a stream of UTF-8 |
| 287 |
// data. |
| 288 |
|
| 289 |
static bool |
| 290 |
raw_is_SBC() |
| 291 |
{ |
| 292 |
bool retval = false; |
| 293 |
switch(source_codeset) |
| 294 |
{ |
| 295 |
case cs_cp1252_e: |
| 296 |
retval = true; |
| 297 |
break; |
| 298 |
default: |
| 299 |
break; |
| 300 |
} |
| 301 |
return retval; |
| 302 |
} |
| 303 |
|
| 304 |
|
| 305 |
static size_t |
| 306 |
extract_next_code_point(const unsigned char *utf8, |
| 307 |
const size_t /*length_in_bytes*/, |
| 308 |
size_t &position) |
| 309 |
{ |
| 310 |
long retval = -1; // Means a badly formed code point |
| 311 |
unsigned char ch = utf8[position++]; |
| 312 |
long under_construction = 0; |
| 313 |
int countdown = 0; |
| 314 |
|
| 315 |
if( (ch & 0x80) == 0x00 ) |
| 316 |
{ |
| 317 |
// We are in the ASCII subset of UTF-8, and we are done |
| 318 |
retval = ch; |
| 319 |
goto done; |
| 320 |
} |
| 321 |
else if( (ch & 0xE0) == 0xC0 ) |
| 322 |
{ |
| 323 |
// There is one byte to follow |
| 324 |
countdown = 1; |
| 325 |
under_construction = ch & 0x1F; |
| 326 |
} |
| 327 |
else if( (ch & 0xF0) == 0xE0 ) |
| 328 |
{ |
| 329 |
countdown = 2; |
| 330 |
under_construction = ch & 0x0F; |
| 331 |
} |
| 332 |
else if( (ch & 0xF8) == 0xF0 ) |
| 333 |
{ |
| 334 |
countdown = 3; |
| 335 |
under_construction = ch & 0x07; |
| 336 |
} |
| 337 |
else |
| 338 |
{ |
| 339 |
// We have a poorly-constructed UTF-8 encoding |
| 340 |
goto done; |
| 341 |
} |
| 342 |
while( countdown-- ) |
| 343 |
{ |
| 344 |
ch = utf8[position++]; |
| 345 |
// We are in a follow-up encoded byte: |
| 346 |
if( (ch & 0xC0) == 0x80 ) |
| 347 |
{ |
| 348 |
// The top two bits are 10, so build in the bottom six bits |
| 349 |
under_construction <<= 6; |
| 350 |
under_construction |= (ch & 0x3F); |
| 351 |
} |
| 352 |
else |
| 353 |
{ |
| 354 |
// This is a poorly-formed encoding |
| 355 |
goto done; |
| 356 |
} |
| 357 |
} |
| 358 |
retval = under_construction; |
| 359 |
|
| 360 |
done: |
| 361 |
return retval; |
| 362 |
} |
| 363 |
|
| 364 |
void flipper(void) |
| 365 |
{ |
| 366 |
for(int i=0; i<256; i++) |
| 367 |
{ |
| 368 |
fprintf(stderr, "{0x%4.4x, 0x%2.2x}, ", cp1252_to_utf8_values[i], i); |
| 369 |
if( (i % 8) == 7 ) |
| 370 |
{ |
| 371 |
fprintf(stderr, "\n"); |
| 372 |
} |
| 373 |
} |
| 374 |
} |
| 375 |
|
| 376 |
extern "C" |
| 377 |
char __gg__ascii_to_ascii_chr(char ch) |
| 378 |
{ |
| 379 |
return ch; |
| 380 |
} |
| 381 |
|
| 382 |
extern "C" |
| 383 |
char __gg__ascii_to_ebcdic_chr(char ch) |
| 384 |
{ |
| 385 |
return (char)__gg__cp1252_to_cp1140_values[(ch&0xFF)]; |
| 386 |
} |
| 387 |
|
| 388 |
extern "C" |
| 389 |
char * |
| 390 |
__gg__raw_to_ascii(char **dest, size_t *dest_size, const char *in, size_t length) |
| 391 |
{ |
| 392 |
// We are anticipating `length` characters, some of which might be multi- |
| 393 |
// character UTF-8 codepoints. We are sending back a nul-terminated string |
| 394 |
// of SBC ASCII values. |
| 395 |
|
| 396 |
__gg__realloc_if_necessary(dest, dest_size, length+1); |
| 397 |
|
| 398 |
// This is the byte position of the output |
| 399 |
size_t index = 0; |
| 400 |
|
| 401 |
// This is the byte position of the input |
| 402 |
size_t position = 0; |
| 403 |
|
| 404 |
while( index < length ) |
| 405 |
{ |
| 406 |
// In the case of "display "âêîôû", when the source code is encoded in |
| 407 |
// UTF-8, the field->data.capacity is showing up as 10, because that |
| 408 |
// UTF-8 string is ten bytes long, and the parser is not counting |
| 409 |
// characters. The data.initial field is indeed nul-terminated, so when we |
| 410 |
// hit a nul, we bug out: |
| 411 |
if( in[position] == '\0' ) |
| 412 |
{ |
| 413 |
// We have hit the end. We want to space-fill to the right: |
| 414 |
while( index < length ) |
| 415 |
{ |
| 416 |
(*dest)[index++] = internal_space; |
| 417 |
} |
| 418 |
break; |
| 419 |
} |
| 420 |
|
| 421 |
// Special handling for PIC X VALUE HIGH-VALUE. If we just hand default |
| 422 |
// 0xFF values to the rest of the routine, the utf-8 detection will give |
| 423 |
// us a result that confuses the remainder of the processing. |
| 424 |
if( (in[position]&0xFF) == 0xFF ) |
| 425 |
{ |
| 426 |
(*dest)[index++] = in[position++]; |
| 427 |
continue; |
| 428 |
} |
| 429 |
|
| 430 |
if( raw_is_SBC() ) |
| 431 |
{ |
| 432 |
(*dest)[index++] = in[position++]; |
| 433 |
continue; |
| 434 |
} |
| 435 |
|
| 436 |
size_t code_point; |
| 437 |
// Pull the next code_point from the UTF-8 stream |
| 438 |
long unicode_point = extract_next_code_point(reinterpret_cast<const unsigned char *>(in), |
| 439 |
length, |
| 440 |
position ); |
| 441 |
|
| 442 |
// Check for that unicode code point in the subset of characters we |
| 443 |
// know about: |
| 444 |
auto it = utf8_to_cp1252_values.find(unicode_point); |
| 445 |
if( it == utf8_to_cp1252_values.end() ) |
| 446 |
{ |
| 447 |
// That unicode character isn't in our list |
| 448 |
code_point = ASCII_REPLACEMENT; |
| 449 |
} |
| 450 |
else |
| 451 |
{ |
| 452 |
code_point = it->second; |
| 453 |
} |
| 454 |
(*dest)[index++] = (char)code_point; |
| 455 |
} |
| 456 |
(*dest)[index++] = '\0'; |
| 457 |
|
| 458 |
return *dest; |
| 459 |
} |
| 460 |
|
| 461 |
extern "C" |
| 462 |
char * |
| 463 |
__gg__raw_to_ebcdic(char **dest, size_t *dest_size, const char *in, size_t length) |
| 464 |
{ |
| 465 |
// A UTF-8 string is at least as long as the single-byte-coded resulting |
| 466 |
// string: |
| 467 |
__gg__realloc_if_necessary(dest, dest_size, length+1); |
| 468 |
|
| 469 |
size_t index = 0; |
| 470 |
|
| 471 |
size_t position = 0; |
| 472 |
size_t code_point; |
| 473 |
while( index < length ) |
| 474 |
{ |
| 475 |
// See comments in __gg__raw_to_ascii |
| 476 |
if( in[position] == '\0' ) |
| 477 |
{ |
| 478 |
// We have hit the end. We want to space-fill to the right: |
| 479 |
while( index < length ) |
| 480 |
{ |
| 481 |
(*dest)[index++] = internal_space; |
| 482 |
} |
| 483 |
break; |
| 484 |
} |
| 485 |
if( raw_is_SBC() ) |
| 486 |
{ |
| 487 |
code_point = in[position++]; |
| 488 |
long ebcdic_code_point = __gg__cp1252_to_cp1140_values[code_point&0xFF]; |
| 489 |
(*dest)[index++] = ebcdic_code_point; |
| 490 |
continue; |
| 491 |
} |
| 492 |
if( (in[position]&0xff) == 0xff ) |
| 493 |
{ |
| 494 |
// HIGH-VALUE is a special case |
| 495 |
(*dest)[index++] = in[position++]; |
| 496 |
continue; |
| 497 |
} |
| 498 |
|
| 499 |
// Pull the next code_point from the UTF-8 stream |
| 500 |
long unicode_point = extract_next_code_point( reinterpret_cast<const unsigned char *>(in), |
| 501 |
length, |
| 502 |
position ); |
| 503 |
// Check for that unicode code point in the subset of characters we |
| 504 |
// know about: |
| 505 |
auto it = utf8_to_cp1252_values.find(unicode_point); |
| 506 |
if( it == utf8_to_cp1252_values.end() ) |
| 507 |
{ |
| 508 |
// That unicode character isn't in our list |
| 509 |
code_point = ASCII_REPLACEMENT; |
| 510 |
} |
| 511 |
else |
| 512 |
{ |
| 513 |
code_point = it->second; |
| 514 |
} |
| 515 |
// TODO: This could be sped up by creating a utf8_to_cp1140_values map. |
| 516 |
// But sufficient unto the day are the evils thereof |
| 517 |
long ebcdic_code_point = __gg__cp1252_to_cp1140_values[code_point&0xFF]; |
| 518 |
(*dest)[index++] = ebcdic_code_point; |
| 519 |
} |
| 520 |
(*dest)[index++] = '\0'; |
| 521 |
|
| 522 |
return *dest; |
| 523 |
} |
| 524 |
|
| 525 |
static |
| 526 |
char * |
| 527 |
convert_cp1252_to_utf8(char **dest, size_t *dest_size, const char *in, size_t length) |
| 528 |
{ |
| 529 |
// Worst case is all unicode characters. |
| 530 |
__gg__realloc_if_necessary(dest, dest_size, 4 * length + 1); |
| 531 |
|
| 532 |
size_t index = 0; |
| 533 |
for(size_t i=0; i<length; i++) |
| 534 |
{ |
| 535 |
unsigned char ch = *in++; |
| 536 |
size_t unicode_point = cp1252_to_utf8_values[ch]; |
| 537 |
if( unicode_point < 0x0080 ) |
| 538 |
{ |
| 539 |
// Single-byte |
| 540 |
(*dest)[index++] = (char)unicode_point; |
| 541 |
} |
| 542 |
else if(unicode_point < 0x0800) |
| 543 |
{ |
| 544 |
// Two-byte: |
| 545 |
(*dest)[index++] = 0xC0 + (unicode_point>>6); |
| 546 |
(*dest)[index++] = 0x80 + ((unicode_point>>0) & 0x3F); |
| 547 |
} |
| 548 |
else if(unicode_point < 0x10000) |
| 549 |
{ |
| 550 |
// Three-byte: |
| 551 |
(*dest)[index++] = 0xE0 + (unicode_point>>12); |
| 552 |
(*dest)[index++] = 0x80 + ((unicode_point>>6) & 0x3F); |
| 553 |
(*dest)[index++] = 0x80 + ((unicode_point>>0) & 0x3F); |
| 554 |
} |
| 555 |
else |
| 556 |
{ |
| 557 |
// Four-byte: |
| 558 |
(*dest)[index++] = 0xF0 + (unicode_point>>18); |
| 559 |
(*dest)[index++] = 0x80 + ((unicode_point>>12) & 0x3F); |
| 560 |
(*dest)[index++] = 0x80 + ((unicode_point>>6) & 0x3F); |
| 561 |
(*dest)[index++] = 0x80 + ((unicode_point>>0) & 0x3F); |
| 562 |
} |
| 563 |
} |
| 564 |
(*dest)[index++] = '\0'; |
| 565 |
|
| 566 |
return *dest; |
| 567 |
} |
| 568 |
|
| 569 |
// This is the address of the 256-character map for internal characters |
| 570 |
// It'll be set to one-to-one for ASCII, and to cp1252-to-cp1140_values for |
| 571 |
// EBCDIC. |
| 572 |
unsigned short const *__gg__internal_codeset_map; |
| 573 |
|
| 574 |
// Here is the list of function pointers establish which ones of the paired |
| 575 |
// possibilities of conversion routines are actually in use. |
| 576 |
|
| 577 |
char (*__gg__ascii_to_internal_chr)(char); |
| 578 |
void (*__gg__ascii_to_internal_str)(char *str, size_t length); |
| 579 |
char *(*__gg__raw_to_internal)(char **dest, size_t *dest_size, const char *in, const size_t length); |
| 580 |
char *(*__gg__internal_to_console_cm)(char **dest, size_t *dest_size, const char *in, size_t length); |
| 581 |
void (*__gg__console_to_internal_cm)(char * const str, size_t length); |
| 582 |
void (*__gg__internal_to_ascii)(char *str, size_t length); |
| 583 |
|
| 584 |
extern "C" |
| 585 |
void __gg__set_internal_codeset(int use_ebcdic) |
| 586 |
{ |
| 587 |
__gg__ebcdic_codeset_in_use = !!use_ebcdic; |
| 588 |
} |
| 589 |
|
| 590 |
extern "C" |
| 591 |
void __gg__text_conversion_override(text_device_t device, |
| 592 |
text_codeset_t codeset) |
| 593 |
{ |
| 594 |
// Establish the default sourcecode and console codesets, and |
| 595 |
// establish the codeset conversion routines: |
| 596 |
|
| 597 |
if( internal_is_ebcdic ) |
| 598 |
{ |
| 599 |
// fprintf(stderr, "Setting up EBCDIC\n"); |
| 600 |
__gg__internal_codeset_map = __gg__cp1252_to_cp1140_values; |
| 601 |
__gg__ascii_to_internal_chr = &__gg__ascii_to_ebcdic_chr; |
| 602 |
__gg__ascii_to_internal_str = &__gg__ascii_to_ebcdic; |
| 603 |
__gg__raw_to_internal = &__gg__raw_to_ebcdic; |
| 604 |
__gg__internal_to_console_cm = &__gg__ebcdic_to_console; |
| 605 |
__gg__console_to_internal_cm = &__gg__console_to_ebcdic; |
| 606 |
__gg__internal_to_ascii = &__gg__ebcdic_to_ascii; |
| 607 |
} |
| 608 |
else |
| 609 |
{ |
| 610 |
// fprintf(stderr, "Setting up ASCII\n"); |
| 611 |
__gg__internal_codeset_map = __gg__one_to_one_values; |
| 612 |
__gg__ascii_to_internal_chr = &__gg__ascii_to_ascii_chr; |
| 613 |
__gg__ascii_to_internal_str = &__gg__ascii_to_ascii; |
| 614 |
__gg__raw_to_internal = &__gg__raw_to_ascii; |
| 615 |
__gg__internal_to_console_cm = &__gg__ascii_to_console; |
| 616 |
__gg__console_to_internal_cm = &__gg__console_to_ascii; |
| 617 |
__gg__internal_to_ascii = &__gg__ascii_to_ascii; |
| 618 |
} |
| 619 |
|
| 620 |
switch(device) |
| 621 |
{ |
| 622 |
case td_default_e: |
| 623 |
{ |
| 624 |
// We are setting our codesets to the defaults |
| 625 |
|
| 626 |
// First, sort out the console: |
| 627 |
|
| 628 |
// It is my understanding that the environment variable LANG is |
| 629 |
// supposed to be set by the terminal to indicate the terminal's |
| 630 |
// current character set. Let's use that as the winner, even if |
| 631 |
// that's not quite the way locale(3) works. |
| 632 |
const char *envLANG = getenv("LANG"); |
| 633 |
if( !envLANG ) |
| 634 |
{ |
| 635 |
// This is odd. No "LANG"? |
| 636 |
envLANG = setlocale(LC_CTYPE, NULL); |
| 637 |
} |
| 638 |
if( !envLANG ) |
| 639 |
{ |
| 640 |
// This is even more odd. Pick something as a backup to the backup |
| 641 |
envLANG = "UTF-8"; |
| 642 |
} |
| 643 |
if( envLANG ) |
| 644 |
{ |
| 645 |
if( strcasestr(envLANG, "UTF-8") ) |
| 646 |
{ |
| 647 |
console_codeset = cs_utf8_e; |
| 648 |
} |
| 649 |
else |
| 650 |
{ |
| 651 |
// If it isn't UTF-8, then figure on it being CP1252 as a |
| 652 |
// convenient way of specifying an SBC codeset. |
| 653 |
console_codeset = cs_cp1252_e; |
| 654 |
} |
| 655 |
} |
| 656 |
break; |
| 657 |
} |
| 658 |
|
| 659 |
case td_sourcecode_e: |
| 660 |
// Explicitly set the source code codeset: |
| 661 |
source_codeset = codeset; |
| 662 |
break; |
| 663 |
|
| 664 |
case td_console_e: |
| 665 |
// Explicitly set the console codeset: |
| 666 |
console_codeset = codeset; |
| 667 |
break; |
| 668 |
} |
| 669 |
} |
| 670 |
|
| 671 |
extern "C" |
| 672 |
void |
| 673 |
__gg__ascii_to_ascii(char *, size_t ) |
| 674 |
{ |
| 675 |
return; |
| 676 |
} |
| 677 |
|
| 678 |
extern "C" |
| 679 |
void |
| 680 |
__gg__ascii_to_ebcdic(char *str, size_t length) |
| 681 |
{ |
| 682 |
for(size_t i=0; i<length; i++) |
| 683 |
{ |
| 684 |
str[i] = __gg__cp1252_to_cp1140_values[str[i]&0xFF]; |
| 685 |
} |
| 686 |
} |
| 687 |
|
| 688 |
extern "C" |
| 689 |
void |
| 690 |
__gg__ebcdic_to_ascii(char * const str, size_t length) |
| 691 |
{ |
| 692 |
for(size_t i=0; i<length; i++) |
| 693 |
{ |
| 694 |
str[i] = __gg__cp1140_to_cp1252_values[str[i]&0xFF]; |
| 695 |
} |
| 696 |
} |
| 697 |
|
| 698 |
extern "C" |
| 699 |
char *__gg__ascii_to_console( char **dest, |
| 700 |
size_t *dest_size, |
| 701 |
char const * const str, |
| 702 |
const size_t length) |
| 703 |
{ |
| 704 |
if( console_codeset == cs_utf8_e ) |
| 705 |
{ |
| 706 |
__gg__realloc_if_necessary(dest, dest_size, length); |
| 707 |
convert_cp1252_to_utf8(dest, dest_size, str, length); |
| 708 |
} |
| 709 |
else |
| 710 |
{ |
| 711 |
__gg__realloc_if_necessary(dest, dest_size, length+1); |
| 712 |
memcpy(*dest, str, length); |
| 713 |
(*dest)[length] = '\0'; |
| 714 |
} |
| 715 |
return *dest; |
| 716 |
} |
| 717 |
|
| 718 |
extern "C" |
| 719 |
char *__gg__ebcdic_to_console(char **dest, |
| 720 |
size_t *dest_size, |
| 721 |
char const * const str, |
| 722 |
const size_t length) |
| 723 |
{ |
| 724 |
static size_t ebcdic_size = MINIMUM_ALLOCATION_SIZE; |
| 725 |
static char *ebcdic = static_cast<char *>(malloc(ebcdic_size)); |
| 726 |
if(!ebcdic)abort(); |
| 727 |
__gg__realloc_if_necessary(&ebcdic, &ebcdic_size, length); |
| 728 |
|
| 729 |
memcpy(ebcdic, str, length); |
| 730 |
__gg__ebcdic_to_ascii(ebcdic, length); |
| 731 |
|
| 732 |
if( console_codeset == cs_utf8_e ) |
| 733 |
{ |
| 734 |
convert_cp1252_to_utf8(dest, dest_size, ebcdic, length); |
| 735 |
} |
| 736 |
else |
| 737 |
{ |
| 738 |
__gg__realloc_if_necessary(dest, dest_size, length+1); |
| 739 |
strcpy(*dest, ebcdic); |
| 740 |
} |
| 741 |
return *dest; |
| 742 |
} |
| 743 |
|
| 744 |
extern "C" |
| 745 |
void __gg__console_to_ascii(char * const str, size_t length) |
| 746 |
{ |
| 747 |
// In-place conversion of ASCII data that might be UTF-8 to CP1252 |
| 748 |
if( console_codeset == cs_cp1252_e ) |
| 749 |
{ |
| 750 |
// It's already what we want it to be |
| 751 |
return; |
| 752 |
} |
| 753 |
char *dest = str; |
| 754 |
|
| 755 |
size_t position = 0; |
| 756 |
while( position < length ) |
| 757 |
{ |
| 758 |
size_t code_point; |
| 759 |
// Pull the next code_point from the UTF-8 stream |
| 760 |
long unicode_point |
| 761 |
= extract_next_code_point( reinterpret_cast<const unsigned char *>(str), |
| 762 |
length, |
| 763 |
position ); |
| 764 |
if( unicode_point == -1 ) |
| 765 |
{ |
| 766 |
// The UTF-8 stream was poorly formed. |
| 767 |
code_point = ASCII_REPLACEMENT; |
| 768 |
} |
| 769 |
else |
| 770 |
{ |
| 771 |
// Check for that unicode code point in the subset of characters we |
| 772 |
// know about: |
| 773 |
auto it = utf8_to_cp1252_values.find(unicode_point); |
| 774 |
if( it == utf8_to_cp1252_values.end() ) |
| 775 |
{ |
| 776 |
// That unicode character isn't in our list |
| 777 |
code_point = ASCII_REPLACEMENT; |
| 778 |
} |
| 779 |
else |
| 780 |
{ |
| 781 |
code_point = it->second; |
| 782 |
} |
| 783 |
} |
| 784 |
*dest++ = (char)code_point; |
| 785 |
} |
| 786 |
*dest++ = '\0'; |
| 787 |
} |
| 788 |
|
| 789 |
extern "C" |
| 790 |
void |
| 791 |
__gg__console_to_ebcdic(char * const str, size_t length) |
| 792 |
{ |
| 793 |
char *dest = str; |
| 794 |
|
| 795 |
size_t position = 0; |
| 796 |
while( position < length ) |
| 797 |
{ |
| 798 |
size_t code_point; |
| 799 |
// Pull the next code_point from the UTF-8 stream |
| 800 |
long unicode_point |
| 801 |
= extract_next_code_point( reinterpret_cast<const unsigned char *>(str), |
| 802 |
length, |
| 803 |
position ); |
| 804 |
if( unicode_point == -1 ) |
| 805 |
{ |
| 806 |
// The UTF-8 stream was poorly formed. |
| 807 |
code_point = ASCII_REPLACEMENT; |
| 808 |
} |
| 809 |
else |
| 810 |
{ |
| 811 |
// Check for that unicode code point in the subset of characters we |
| 812 |
// know about: |
| 813 |
auto it = utf8_to_cp1252_values.find(unicode_point); |
| 814 |
if( it == utf8_to_cp1252_values.end() ) |
| 815 |
{ |
| 816 |
// That unicode character isn't in our list |
| 817 |
code_point = ASCII_REPLACEMENT; |
| 818 |
} |
| 819 |
else |
| 820 |
{ |
| 821 |
code_point = it->second; |
| 822 |
} |
| 823 |
} |
| 824 |
*dest++ = __gg__cp1252_to_cp1140_values[code_point&0xFF] ; |
| 825 |
} |
| 826 |
*dest++ = '\0'; |
| 827 |
} |
| 828 |
|
| 829 |
extern "C" |
| 830 |
size_t |
| 831 |
_to_ctype(char * const location, size_t length) |
| 832 |
{ |
| 833 |
// Converts from our internal codeset to the system LC_TYPE codeset |
| 834 |
const char *fromcode; |
| 835 |
const char *tocode; |
| 836 |
if( __gg__ebcdic_codeset_in_use ) |
| 837 |
{ |
| 838 |
fromcode = "CP1140"; |
| 839 |
} |
| 840 |
else |
| 841 |
{ |
| 842 |
fromcode = "CP1252"; |
| 843 |
} |
| 844 |
const char *ctype = setlocale(LC_CTYPE, ""); |
| 845 |
|
| 846 |
if( strcasestr(ctype, "UTF") ) |
| 847 |
{ |
| 848 |
tocode = "UTF-8"; |
| 849 |
} |
| 850 |
else |
| 851 |
{ |
| 852 |
tocode = "CP1252"; |
| 853 |
} |
| 854 |
|
| 855 |
iconv_t cd = iconv_open(tocode, fromcode); |
| 856 |
assert( cd != (iconv_t)-1 ); |
| 857 |
|
| 858 |
static char *dest = NULL; |
| 859 |
static size_t dest_size = 0; |
| 860 |
|
| 861 |
// create a buffer long enough that iconv() won't fail: |
| 862 |
__gg__realloc_if_necessary(&dest, &dest_size, 4*length+1); |
| 863 |
|
| 864 |
// Set up for the iconv() call: |
| 865 |
char *inbuf = location; |
| 866 |
size_t inbytesleft = length; |
| 867 |
char *outbuf = dest; |
| 868 |
size_t outbytesleft = 2*length+1; |
| 869 |
|
| 870 |
memset(dest, ' ', 2*length+1); |
| 871 |
iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft); |
| 872 |
memcpy(location, dest, length); |
| 873 |
return 0; |
| 874 |
} |
| 875 |
|
| 876 |
extern "C" |
| 877 |
size_t |
| 878 |
_from_ctype(char * const location, size_t length) |
| 879 |
{ |
| 880 |
// Converts from our internal codeset to the system LC_TYPE codeset |
| 881 |
const char *fromcode; |
| 882 |
const char *tocode; |
| 883 |
if( __gg__ebcdic_codeset_in_use ) |
| 884 |
{ |
| 885 |
tocode = "CP1140"; |
| 886 |
} |
| 887 |
else |
| 888 |
{ |
| 889 |
tocode = "CP1252"; |
| 890 |
} |
| 891 |
const char *ctype = setlocale(LC_CTYPE, ""); |
| 892 |
|
| 893 |
if( strcasestr(ctype, "UTF") ) |
| 894 |
{ |
| 895 |
fromcode = "UTF-8"; |
| 896 |
} |
| 897 |
else |
| 898 |
{ |
| 899 |
fromcode = "CP1252"; |
| 900 |
} |
| 901 |
|
| 902 |
iconv_t cd = iconv_open(tocode, fromcode); |
| 903 |
assert( cd != (iconv_t)-1 ); |
| 904 |
|
| 905 |
static char *dest = NULL; |
| 906 |
static size_t dest_size = 0; |
| 907 |
|
| 908 |
// create a buffer long enough that iconv() won't fail: |
| 909 |
__gg__realloc_if_necessary(&dest, &dest_size, length+1); |
| 910 |
|
| 911 |
// Set up for the iconv() call: |
| 912 |
char *inbuf = location; |
| 913 |
size_t inbytesleft = length; |
| 914 |
char *outbuf = dest; |
| 915 |
size_t outbytesleft = length+1; |
| 916 |
|
| 917 |
memset(dest, internal_space, length+1); |
| 918 |
///size_t iret = |
| 919 |
iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft); |
| 920 |
memcpy(location, dest, length); |
| 921 |
return 0; |
| 922 |
} |