| 1 |
/* |
| 2 |
* The contents of this file are subject to the Interbase Public |
| 3 |
* License Version 1.0 (the "License"); you may not use this file |
| 4 |
* except in compliance with the License. You may obtain a copy |
| 5 |
* of the License at http://www.Inprise.com/IPL.html |
| 6 |
* |
| 7 |
* Software distributed under the License is distributed on an |
| 8 |
* "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express |
| 9 |
* or implied. See the License for the specific language governing |
| 10 |
* rights and limitations under the License. |
| 11 |
* |
| 12 |
* The Original Code was created by Inprise Corporation |
| 13 |
* and its predecessors. Portions created by Inprise Corporation are |
| 14 |
* Copyright (C) Inprise Corporation. |
| 15 |
* |
| 16 |
* All Rights Reserved. |
| 17 |
* Contributor(s): ______________________________________. |
| 18 |
*/ |
| 19 |
|
| 20 |
#include "firebird.h" |
| 21 |
#include <stdio.h> |
| 22 |
#include <stdlib.h> |
| 23 |
|
| 24 |
#define CANT_MAP_CHARACTER 0 |
| 25 |
|
| 26 |
class codepage_map |
| 27 |
{ |
| 28 |
|
| 29 |
public: |
| 30 |
unsigned short *to_unicode_map; |
| 31 |
unsigned char *from_unicode_mapping_array; |
| 32 |
unsigned short *from_unicode_map; |
| 33 |
unsigned short sizeof_to_unicode_map; |
| 34 |
unsigned short sizeof_from_unicode_mapping_array; |
| 35 |
unsigned short sizeof_from_unicode_map; |
| 36 |
const char* name; |
| 37 |
|
| 38 |
codepage_map() |
| 39 |
{ |
| 40 |
to_unicode_map = NULL; |
| 41 |
from_unicode_map = NULL; |
| 42 |
from_unicode_mapping_array = NULL; |
| 43 |
sizeof_to_unicode_map = 0; |
| 44 |
sizeof_from_unicode_map = 0; |
| 45 |
sizeof_from_unicode_mapping_array = 0; |
| 46 |
name = NULL; |
| 47 |
} |
| 48 |
|
| 49 |
codepage_map(const unsigned short *_to, |
| 50 |
const unsigned char *_from_array, |
| 51 |
const unsigned short *_from_map, |
| 52 |
const unsigned short sizeof_to, |
| 53 |
unsigned short sizeof_from_array, |
| 54 |
unsigned short sizeof_from_map, const char *_name) |
| 55 |
{ |
| 56 |
to_unicode_map = _to; |
| 57 |
from_unicode_mapping_array = _from_array; |
| 58 |
from_unicode_map = _from_map; |
| 59 |
sizeof_to_unicode_map = sizeof_to; |
| 60 |
sizeof_from_unicode_mapping_array = sizeof_from_array; |
| 61 |
sizeof_from_unicode_map = sizeof_from_map; |
| 62 |
name = _name; |
| 63 |
test_codepage(); |
| 64 |
} |
| 65 |
|
| 66 |
unsigned short to_unicode(unsigned char c) |
| 67 |
{ |
| 68 |
return to_unicode_map[c]; |
| 69 |
} |
| 70 |
|
| 71 |
unsigned short from_unicode(unsigned short c) |
| 72 |
{ |
| 73 |
return from_unicode_mapping_array[from_unicode_map[c / 256] + (c % 256)]; |
| 74 |
} |
| 75 |
|
| 76 |
void test_codepage() |
| 77 |
{ |
| 78 |
if (sizeof_to_unicode_map != 256 * sizeof(to_unicode_map[0])) |
| 79 |
printf("The to_unicode_map is too small! %d entries!\n", |
| 80 |
sizeof_to_unicode_map / sizeof(to_unicode_map[0])); |
| 81 |
|
| 82 |
if (sizeof_from_unicode_map != 256 * sizeof(from_unicode_map[0])) |
| 83 |
printf("The from_unicode_map is too small! %d entries!\n", |
| 84 |
sizeof_from_unicode_map / sizeof(from_unicode_map[0])); |
| 85 |
|
| 86 |
for (int i = 0; i <= 255; i++) |
| 87 |
{ |
| 88 |
unsigned short uch = to_unicode((unsigned char) i); |
| 89 |
|
| 90 |
if (uch == CANT_MAP_CHARACTER) |
| 91 |
continue; |
| 92 |
|
| 93 |
unsigned char ch2 = from_unicode(uch); |
| 94 |
if (ch2 != i) |
| 95 |
{ |
| 96 |
printf("Mapping error: Character %02x -> Unicode %04x -> Char %02x\n", i, uch, ch2); |
| 97 |
|
| 98 |
/* Find the Character in the from_unicode_mapping_array */ |
| 99 |
int j; |
| 100 |
for (j = 0; j < sizeof_from_unicode_mapping_array; j++) |
| 101 |
{ |
| 102 |
if (from_unicode_mapping_array[j] == i) |
| 103 |
{ |
| 104 |
/* Mapping table is wrong - recommend a fix for it */ |
| 105 |
printf |
| 106 |
("Recommend from_unicode_map[0x%02x] be set to %d\n", |
| 107 |
uch / 256, j - (uch % 256)); |
| 108 |
break; |
| 109 |
} |
| 110 |
} |
| 111 |
if (j == sizeof_from_unicode_mapping_array) |
| 112 |
{ |
| 113 |
/* Oops - found a character that does exists in the character set |
| 114 |
but not in unicode - an obvious error! */ |
| 115 |
printf("Error: Character %d does not exist in the from_unicode_mapping_array\n", i); |
| 116 |
} |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
for (i = 0; i <= 255; i++) |
| 121 |
{ |
| 122 |
if (from_unicode_map[i] + 0xFF >= sizeof_from_unicode_mapping_array) |
| 123 |
{ |
| 124 |
printf("From_unicode array bounds error at position %02x00\n", i); |
| 125 |
continue; |
| 126 |
} |
| 127 |
|
| 128 |
for (int j = 0; j <= 255; j++) |
| 129 |
{ |
| 130 |
unsigned short uch = i * 256 + j; |
| 131 |
unsigned char ch2 = from_unicode(uch); |
| 132 |
if (ch2 == CANT_MAP_CHARACTER) |
| 133 |
continue; |
| 134 |
|
| 135 |
unsigned short uch2 = to_unicode(ch2); |
| 136 |
if (uch != uch2) |
| 137 |
{ |
| 138 |
printf("Mapping error: Unicode %04x -> ch %02x -> Unicode %04x\n", uch, ch2, uch2); |
| 139 |
|
| 140 |
int k; |
| 141 |
for (k = 0; k <= 255; k++) |
| 142 |
{ |
| 143 |
if (to_unicode_map[k] == uch) { |
| 144 |
/* Can map this character from charset to unicode */ |
| 145 |
/* Assume fix was printed out above */ |
| 146 |
} |
| 147 |
} |
| 148 |
if (k > 255) { |
| 149 |
/* This unicode doesn't exist in charset */ |
| 150 |
/* Mapping table is wrong - recommend a fix for it */ |
| 151 |
printf("Recommend from_unicode_map[0x%02x] be set to %d\n", uch / 256, 0); |
| 152 |
} |
| 153 |
} |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
printf("Test of %s completed\n", name); |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
/* |
| 166 |
typedef unsigned short USHORT; |
| 167 |
typedef unsigned char UCHAR; |
| 168 |
|
| 169 |
*/ |
| 170 |
#define UCHAR unsigned char |
| 171 |
#define USHORT unsigned short |
| 172 |
#define JRD_COMMON_H |
| 173 |
|
| 174 |
codepage_map *get_w1250() |
| 175 |
{ |
| 176 |
#include "cs_w1250.h" |
| 177 |
return FB_NEW codepage_map(to_unicode_map, |
| 178 |
from_unicode_mapping_array, |
| 179 |
from_unicode_map, |
| 180 |
sizeof(to_unicode_map), |
| 181 |
sizeof(from_unicode_mapping_array), |
| 182 |
sizeof(from_unicode_map), "cs_w1250.h"); |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
int main(int argc, char *argv[]) |
| 187 |
{ |
| 188 |
codepage_map *w1250 = get_w1250(); |
| 189 |
w1250->test_codepage(); |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
/* |
| 194 |
fixed |
| 195 |
#include "cs_w1254.h" |
| 196 |
#include "cs_cyrl.h" |
| 197 |
#include "cs_865.h" |
| 198 |
#include "cs_863.h" |
| 199 |
#include "cs_861.h" |
| 200 |
#include "cs_860.h" |
| 201 |
#include "cs_857.h" |
| 202 |
#include "cs_w1251.h" |
| 203 |
#include "cs_437.h" |
| 204 |
#include "cs_850.h" |
| 205 |
#include "cs_852.h" |
| 206 |
#include "cs_w1250.h" |
| 207 |
#include "cs_w1252.h" |
| 208 |
#include "cs_w1253.h" |
| 209 |
|
| 210 |
fixed with errors |
| 211 |
#include "cs_next.h" -- NeXT has two characters mapping to the same |
| 212 |
Unicode (FE & FF). So round trip is not possible. |
| 213 |
They map to FFFD, which is the replacement character |
| 214 |
so this is not an error |
| 215 |
|
| 216 |
No errors |
| 217 |
#include "../intl/cs_iso8859_1.h" |
| 218 |
*/ |
| 219 |
|
| 220 |
/* |
| 221 |
-- Multibyte character sets -- |
| 222 |
#include "../intl/cs_big5.h" |
| 223 |
#include "../intl/cs_gb2312.h" |
| 224 |
#include "../intl/cs_jis_0208_1990.h" |
| 225 |
#include "../intl/cs_ksc5601.h" |
| 226 |
#include "../intl/csjis2_p.h" |
| 227 |
*/ |
| 228 |
|