1*0e209d39SAndroid Build Coastguard Worker // © 2016 and later: Unicode, Inc. and others.
2*0e209d39SAndroid Build Coastguard Worker // License & terms of use: http://www.unicode.org/copyright.html
3*0e209d39SAndroid Build Coastguard Worker /*
4*0e209d39SAndroid Build Coastguard Worker *******************************************************************************
5*0e209d39SAndroid Build Coastguard Worker *
6*0e209d39SAndroid Build Coastguard Worker * Copyright (C) 1999-2010, International Business Machines
7*0e209d39SAndroid Build Coastguard Worker * Corporation and others. All Rights Reserved.
8*0e209d39SAndroid Build Coastguard Worker *
9*0e209d39SAndroid Build Coastguard Worker *******************************************************************************
10*0e209d39SAndroid Build Coastguard Worker * file name: uinvchar.c
11*0e209d39SAndroid Build Coastguard Worker * encoding: UTF-8
12*0e209d39SAndroid Build Coastguard Worker * tab size: 8 (not used)
13*0e209d39SAndroid Build Coastguard Worker * indentation:2
14*0e209d39SAndroid Build Coastguard Worker *
15*0e209d39SAndroid Build Coastguard Worker * created on: 2004sep14
16*0e209d39SAndroid Build Coastguard Worker * created by: Markus W. Scherer
17*0e209d39SAndroid Build Coastguard Worker *
18*0e209d39SAndroid Build Coastguard Worker * Functions for handling invariant characters, moved here from putil.c
19*0e209d39SAndroid Build Coastguard Worker * for better modularization.
20*0e209d39SAndroid Build Coastguard Worker */
21*0e209d39SAndroid Build Coastguard Worker
22*0e209d39SAndroid Build Coastguard Worker #include "unicode/utypes.h"
23*0e209d39SAndroid Build Coastguard Worker #include "unicode/ustring.h"
24*0e209d39SAndroid Build Coastguard Worker #include "udataswp.h"
25*0e209d39SAndroid Build Coastguard Worker #include "cstring.h"
26*0e209d39SAndroid Build Coastguard Worker #include "cmemory.h"
27*0e209d39SAndroid Build Coastguard Worker #include "uassert.h"
28*0e209d39SAndroid Build Coastguard Worker #include "uinvchar.h"
29*0e209d39SAndroid Build Coastguard Worker
30*0e209d39SAndroid Build Coastguard Worker /* invariant-character handling --------------------------------------------- */
31*0e209d39SAndroid Build Coastguard Worker
32*0e209d39SAndroid Build Coastguard Worker /*
33*0e209d39SAndroid Build Coastguard Worker * These maps for ASCII to/from EBCDIC map invariant characters (see utypes.h)
34*0e209d39SAndroid Build Coastguard Worker * appropriately for most EBCDIC codepages.
35*0e209d39SAndroid Build Coastguard Worker *
36*0e209d39SAndroid Build Coastguard Worker * They currently also map most other ASCII graphic characters,
37*0e209d39SAndroid Build Coastguard Worker * appropriately for codepages 37 and 1047.
38*0e209d39SAndroid Build Coastguard Worker * Exceptions: The characters for []^ have different codes in 37 & 1047.
39*0e209d39SAndroid Build Coastguard Worker * Both versions are mapped to ASCII.
40*0e209d39SAndroid Build Coastguard Worker *
41*0e209d39SAndroid Build Coastguard Worker * ASCII 37 1047
42*0e209d39SAndroid Build Coastguard Worker * [ 5B BA AD
43*0e209d39SAndroid Build Coastguard Worker * ] 5D BB BD
44*0e209d39SAndroid Build Coastguard Worker * ^ 5E B0 5F
45*0e209d39SAndroid Build Coastguard Worker *
46*0e209d39SAndroid Build Coastguard Worker * There are no mappings for variant characters from Unicode to EBCDIC.
47*0e209d39SAndroid Build Coastguard Worker *
48*0e209d39SAndroid Build Coastguard Worker * Currently, C0 control codes are also included in these maps.
49*0e209d39SAndroid Build Coastguard Worker * Exceptions: S/390 Open Edition swaps LF and NEL codes compared with other
50*0e209d39SAndroid Build Coastguard Worker * EBCDIC platforms; both codes (15 and 25) are mapped to ASCII LF (0A),
51*0e209d39SAndroid Build Coastguard Worker * but there is no mapping for ASCII LF back to EBCDIC.
52*0e209d39SAndroid Build Coastguard Worker *
53*0e209d39SAndroid Build Coastguard Worker * ASCII EBCDIC S/390-OE
54*0e209d39SAndroid Build Coastguard Worker * LF 0A 25 15
55*0e209d39SAndroid Build Coastguard Worker * NEL 85 15 25
56*0e209d39SAndroid Build Coastguard Worker *
57*0e209d39SAndroid Build Coastguard Worker * The maps below explicitly exclude the variant
58*0e209d39SAndroid Build Coastguard Worker * control and graphical characters that are in ASCII-based
59*0e209d39SAndroid Build Coastguard Worker * codepages at 0x80 and above.
60*0e209d39SAndroid Build Coastguard Worker * "No mapping" is expressed by mapping to a 00 byte.
61*0e209d39SAndroid Build Coastguard Worker *
62*0e209d39SAndroid Build Coastguard Worker * These tables do not establish a converter or a codepage.
63*0e209d39SAndroid Build Coastguard Worker */
64*0e209d39SAndroid Build Coastguard Worker
65*0e209d39SAndroid Build Coastguard Worker static const uint8_t asciiFromEbcdic[256]={
66*0e209d39SAndroid Build Coastguard Worker 0x00, 0x01, 0x02, 0x03, 0x00, 0x09, 0x00, 0x7f, 0x00, 0x00, 0x00, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
67*0e209d39SAndroid Build Coastguard Worker 0x10, 0x11, 0x12, 0x13, 0x00, 0x0a, 0x08, 0x00, 0x18, 0x19, 0x00, 0x00, 0x1c, 0x1d, 0x1e, 0x1f,
68*0e209d39SAndroid Build Coastguard Worker 0x00, 0x00, 0x00, 0x00, 0x00, 0x0a, 0x17, 0x1b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x06, 0x07,
69*0e209d39SAndroid Build Coastguard Worker 0x00, 0x00, 0x16, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x14, 0x15, 0x00, 0x1a,
70*0e209d39SAndroid Build Coastguard Worker
71*0e209d39SAndroid Build Coastguard Worker 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2e, 0x3c, 0x28, 0x2b, 0x7c,
72*0e209d39SAndroid Build Coastguard Worker 0x26, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x21, 0x24, 0x2a, 0x29, 0x3b, 0x5e,
73*0e209d39SAndroid Build Coastguard Worker 0x2d, 0x2f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2c, 0x25, 0x5f, 0x3e, 0x3f,
74*0e209d39SAndroid Build Coastguard Worker 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x3a, 0x23, 0x40, 0x27, 0x3d, 0x22,
75*0e209d39SAndroid Build Coastguard Worker
76*0e209d39SAndroid Build Coastguard Worker 0x00, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
77*0e209d39SAndroid Build Coastguard Worker 0x00, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
78*0e209d39SAndroid Build Coastguard Worker 0x00, 0x7e, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x00, 0x00, 0x00, 0x5b, 0x00, 0x00,
79*0e209d39SAndroid Build Coastguard Worker 0x5e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x5b, 0x5d, 0x00, 0x5d, 0x00, 0x00,
80*0e209d39SAndroid Build Coastguard Worker
81*0e209d39SAndroid Build Coastguard Worker 0x7b, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
82*0e209d39SAndroid Build Coastguard Worker 0x7d, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
83*0e209d39SAndroid Build Coastguard Worker 0x5c, 0x00, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
84*0e209d39SAndroid Build Coastguard Worker 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
85*0e209d39SAndroid Build Coastguard Worker };
86*0e209d39SAndroid Build Coastguard Worker
87*0e209d39SAndroid Build Coastguard Worker static const uint8_t ebcdicFromAscii[256]={
88*0e209d39SAndroid Build Coastguard Worker 0x00, 0x01, 0x02, 0x03, 0x37, 0x2d, 0x2e, 0x2f, 0x16, 0x05, 0x00, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
89*0e209d39SAndroid Build Coastguard Worker 0x10, 0x11, 0x12, 0x13, 0x3c, 0x3d, 0x32, 0x26, 0x18, 0x19, 0x3f, 0x27, 0x1c, 0x1d, 0x1e, 0x1f,
90*0e209d39SAndroid Build Coastguard Worker 0x40, 0x00, 0x7f, 0x00, 0x00, 0x6c, 0x50, 0x7d, 0x4d, 0x5d, 0x5c, 0x4e, 0x6b, 0x60, 0x4b, 0x61,
91*0e209d39SAndroid Build Coastguard Worker 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0x7a, 0x5e, 0x4c, 0x7e, 0x6e, 0x6f,
92*0e209d39SAndroid Build Coastguard Worker
93*0e209d39SAndroid Build Coastguard Worker 0x00, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6,
94*0e209d39SAndroid Build Coastguard Worker 0xd7, 0xd8, 0xd9, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0x00, 0x00, 0x00, 0x00, 0x6d,
95*0e209d39SAndroid Build Coastguard Worker 0x00, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96,
96*0e209d39SAndroid Build Coastguard Worker 0x97, 0x98, 0x99, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0x00, 0x00, 0x00, 0x00, 0x07,
97*0e209d39SAndroid Build Coastguard Worker
98*0e209d39SAndroid Build Coastguard Worker 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
99*0e209d39SAndroid Build Coastguard Worker 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
100*0e209d39SAndroid Build Coastguard Worker 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
101*0e209d39SAndroid Build Coastguard Worker 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
102*0e209d39SAndroid Build Coastguard Worker
103*0e209d39SAndroid Build Coastguard Worker 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
104*0e209d39SAndroid Build Coastguard Worker 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
105*0e209d39SAndroid Build Coastguard Worker 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
106*0e209d39SAndroid Build Coastguard Worker 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
107*0e209d39SAndroid Build Coastguard Worker };
108*0e209d39SAndroid Build Coastguard Worker
109*0e209d39SAndroid Build Coastguard Worker /* Same as asciiFromEbcdic[] except maps all letters to lowercase. */
110*0e209d39SAndroid Build Coastguard Worker static const uint8_t lowercaseAsciiFromEbcdic[256]={
111*0e209d39SAndroid Build Coastguard Worker 0x00, 0x01, 0x02, 0x03, 0x00, 0x09, 0x00, 0x7f, 0x00, 0x00, 0x00, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
112*0e209d39SAndroid Build Coastguard Worker 0x10, 0x11, 0x12, 0x13, 0x00, 0x0a, 0x08, 0x00, 0x18, 0x19, 0x00, 0x00, 0x1c, 0x1d, 0x1e, 0x1f,
113*0e209d39SAndroid Build Coastguard Worker 0x00, 0x00, 0x00, 0x00, 0x00, 0x0a, 0x17, 0x1b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x06, 0x07,
114*0e209d39SAndroid Build Coastguard Worker 0x00, 0x00, 0x16, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x14, 0x15, 0x00, 0x1a,
115*0e209d39SAndroid Build Coastguard Worker
116*0e209d39SAndroid Build Coastguard Worker 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2e, 0x3c, 0x28, 0x2b, 0x7c,
117*0e209d39SAndroid Build Coastguard Worker 0x26, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x21, 0x24, 0x2a, 0x29, 0x3b, 0x5e,
118*0e209d39SAndroid Build Coastguard Worker 0x2d, 0x2f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2c, 0x25, 0x5f, 0x3e, 0x3f,
119*0e209d39SAndroid Build Coastguard Worker 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x3a, 0x23, 0x40, 0x27, 0x3d, 0x22,
120*0e209d39SAndroid Build Coastguard Worker
121*0e209d39SAndroid Build Coastguard Worker 0x00, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
122*0e209d39SAndroid Build Coastguard Worker 0x00, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
123*0e209d39SAndroid Build Coastguard Worker 0x00, 0x7e, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x00, 0x00, 0x00, 0x5b, 0x00, 0x00,
124*0e209d39SAndroid Build Coastguard Worker 0x5e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x5b, 0x5d, 0x00, 0x5d, 0x00, 0x00,
125*0e209d39SAndroid Build Coastguard Worker
126*0e209d39SAndroid Build Coastguard Worker 0x7b, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
127*0e209d39SAndroid Build Coastguard Worker 0x7d, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
128*0e209d39SAndroid Build Coastguard Worker 0x7c, 0x00, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
129*0e209d39SAndroid Build Coastguard Worker 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
130*0e209d39SAndroid Build Coastguard Worker };
131*0e209d39SAndroid Build Coastguard Worker
132*0e209d39SAndroid Build Coastguard Worker /*
133*0e209d39SAndroid Build Coastguard Worker * Bit sets indicating which characters of the ASCII repertoire
134*0e209d39SAndroid Build Coastguard Worker * (by ASCII/Unicode code) are "invariant".
135*0e209d39SAndroid Build Coastguard Worker * See utypes.h for more details.
136*0e209d39SAndroid Build Coastguard Worker *
137*0e209d39SAndroid Build Coastguard Worker * As invariant are considered the characters of the ASCII repertoire except
138*0e209d39SAndroid Build Coastguard Worker * for the following:
139*0e209d39SAndroid Build Coastguard Worker * 21 '!' <exclamation mark>
140*0e209d39SAndroid Build Coastguard Worker * 23 '#' <number sign>
141*0e209d39SAndroid Build Coastguard Worker * 24 '$' <dollar sign>
142*0e209d39SAndroid Build Coastguard Worker *
143*0e209d39SAndroid Build Coastguard Worker * 40 '@' <commercial at>
144*0e209d39SAndroid Build Coastguard Worker *
145*0e209d39SAndroid Build Coastguard Worker * 5b '[' <left bracket>
146*0e209d39SAndroid Build Coastguard Worker * 5c '\' <backslash>
147*0e209d39SAndroid Build Coastguard Worker * 5d ']' <right bracket>
148*0e209d39SAndroid Build Coastguard Worker * 5e '^' <circumflex>
149*0e209d39SAndroid Build Coastguard Worker *
150*0e209d39SAndroid Build Coastguard Worker * 60 '`' <grave accent>
151*0e209d39SAndroid Build Coastguard Worker *
152*0e209d39SAndroid Build Coastguard Worker * 7b '{' <left brace>
153*0e209d39SAndroid Build Coastguard Worker * 7c '|' <vertical line>
154*0e209d39SAndroid Build Coastguard Worker * 7d '}' <right brace>
155*0e209d39SAndroid Build Coastguard Worker * 7e '~' <tilde>
156*0e209d39SAndroid Build Coastguard Worker */
157*0e209d39SAndroid Build Coastguard Worker static const uint32_t invariantChars[4]={
158*0e209d39SAndroid Build Coastguard Worker 0xfffffbff, /* 00..1f but not 0a */
159*0e209d39SAndroid Build Coastguard Worker 0xffffffe5, /* 20..3f but not 21 23 24 */
160*0e209d39SAndroid Build Coastguard Worker 0x87fffffe, /* 40..5f but not 40 5b..5e */
161*0e209d39SAndroid Build Coastguard Worker 0x87fffffe /* 60..7f but not 60 7b..7e */
162*0e209d39SAndroid Build Coastguard Worker };
163*0e209d39SAndroid Build Coastguard Worker
164*0e209d39SAndroid Build Coastguard Worker /*
165*0e209d39SAndroid Build Coastguard Worker * test unsigned types (or values known to be non-negative) for invariant characters,
166*0e209d39SAndroid Build Coastguard Worker * tests ASCII-family character values
167*0e209d39SAndroid Build Coastguard Worker */
168*0e209d39SAndroid Build Coastguard Worker #define UCHAR_IS_INVARIANT(c) (((c)<=0x7f) && (invariantChars[(c)>>5]&((uint32_t)1<<((c)&0x1f)))!=0)
169*0e209d39SAndroid Build Coastguard Worker
170*0e209d39SAndroid Build Coastguard Worker /* test signed types for invariant characters, adds test for positive values */
171*0e209d39SAndroid Build Coastguard Worker #define SCHAR_IS_INVARIANT(c) ((0<=(c)) && UCHAR_IS_INVARIANT(c))
172*0e209d39SAndroid Build Coastguard Worker
173*0e209d39SAndroid Build Coastguard Worker #if U_CHARSET_FAMILY==U_ASCII_FAMILY
174*0e209d39SAndroid Build Coastguard Worker #define CHAR_TO_UCHAR(c) c
175*0e209d39SAndroid Build Coastguard Worker #define UCHAR_TO_CHAR(c) c
176*0e209d39SAndroid Build Coastguard Worker #elif U_CHARSET_FAMILY==U_EBCDIC_FAMILY
177*0e209d39SAndroid Build Coastguard Worker #define CHAR_TO_UCHAR(u) asciiFromEbcdic[u]
178*0e209d39SAndroid Build Coastguard Worker #define UCHAR_TO_CHAR(u) ebcdicFromAscii[u]
179*0e209d39SAndroid Build Coastguard Worker #else
180*0e209d39SAndroid Build Coastguard Worker # error U_CHARSET_FAMILY is not valid
181*0e209d39SAndroid Build Coastguard Worker #endif
182*0e209d39SAndroid Build Coastguard Worker
183*0e209d39SAndroid Build Coastguard Worker
184*0e209d39SAndroid Build Coastguard Worker U_CAPI void U_EXPORT2
u_charsToUChars(const char * cs,char16_t * us,int32_t length)185*0e209d39SAndroid Build Coastguard Worker u_charsToUChars(const char *cs, char16_t *us, int32_t length) {
186*0e209d39SAndroid Build Coastguard Worker char16_t u;
187*0e209d39SAndroid Build Coastguard Worker uint8_t c;
188*0e209d39SAndroid Build Coastguard Worker
189*0e209d39SAndroid Build Coastguard Worker /*
190*0e209d39SAndroid Build Coastguard Worker * Allow the entire ASCII repertoire to be mapped _to_ Unicode.
191*0e209d39SAndroid Build Coastguard Worker * For EBCDIC systems, this works for characters with codes from
192*0e209d39SAndroid Build Coastguard Worker * codepages 37 and 1047 or compatible.
193*0e209d39SAndroid Build Coastguard Worker */
194*0e209d39SAndroid Build Coastguard Worker while(length>0) {
195*0e209d39SAndroid Build Coastguard Worker c=(uint8_t)(*cs++);
196*0e209d39SAndroid Build Coastguard Worker u=(char16_t)CHAR_TO_UCHAR(c);
197*0e209d39SAndroid Build Coastguard Worker U_ASSERT((u!=0 || c==0)); /* only invariant chars converted? */
198*0e209d39SAndroid Build Coastguard Worker *us++=u;
199*0e209d39SAndroid Build Coastguard Worker --length;
200*0e209d39SAndroid Build Coastguard Worker }
201*0e209d39SAndroid Build Coastguard Worker }
202*0e209d39SAndroid Build Coastguard Worker
203*0e209d39SAndroid Build Coastguard Worker U_CAPI void U_EXPORT2
u_UCharsToChars(const char16_t * us,char * cs,int32_t length)204*0e209d39SAndroid Build Coastguard Worker u_UCharsToChars(const char16_t *us, char *cs, int32_t length) {
205*0e209d39SAndroid Build Coastguard Worker char16_t u;
206*0e209d39SAndroid Build Coastguard Worker
207*0e209d39SAndroid Build Coastguard Worker while(length>0) {
208*0e209d39SAndroid Build Coastguard Worker u=*us++;
209*0e209d39SAndroid Build Coastguard Worker if(!UCHAR_IS_INVARIANT(u)) {
210*0e209d39SAndroid Build Coastguard Worker U_ASSERT(false); /* Variant characters were used. These are not portable in ICU. */
211*0e209d39SAndroid Build Coastguard Worker u=0;
212*0e209d39SAndroid Build Coastguard Worker }
213*0e209d39SAndroid Build Coastguard Worker *cs++=(char)UCHAR_TO_CHAR(u);
214*0e209d39SAndroid Build Coastguard Worker --length;
215*0e209d39SAndroid Build Coastguard Worker }
216*0e209d39SAndroid Build Coastguard Worker }
217*0e209d39SAndroid Build Coastguard Worker
218*0e209d39SAndroid Build Coastguard Worker U_CAPI UBool U_EXPORT2
uprv_isInvariantString(const char * s,int32_t length)219*0e209d39SAndroid Build Coastguard Worker uprv_isInvariantString(const char *s, int32_t length) {
220*0e209d39SAndroid Build Coastguard Worker uint8_t c;
221*0e209d39SAndroid Build Coastguard Worker
222*0e209d39SAndroid Build Coastguard Worker for(;;) {
223*0e209d39SAndroid Build Coastguard Worker if(length<0) {
224*0e209d39SAndroid Build Coastguard Worker /* NUL-terminated */
225*0e209d39SAndroid Build Coastguard Worker c=(uint8_t)*s++;
226*0e209d39SAndroid Build Coastguard Worker if(c==0) {
227*0e209d39SAndroid Build Coastguard Worker break;
228*0e209d39SAndroid Build Coastguard Worker }
229*0e209d39SAndroid Build Coastguard Worker } else {
230*0e209d39SAndroid Build Coastguard Worker /* count length */
231*0e209d39SAndroid Build Coastguard Worker if(length==0) {
232*0e209d39SAndroid Build Coastguard Worker break;
233*0e209d39SAndroid Build Coastguard Worker }
234*0e209d39SAndroid Build Coastguard Worker --length;
235*0e209d39SAndroid Build Coastguard Worker c=(uint8_t)*s++;
236*0e209d39SAndroid Build Coastguard Worker if(c==0) {
237*0e209d39SAndroid Build Coastguard Worker continue; /* NUL is invariant */
238*0e209d39SAndroid Build Coastguard Worker }
239*0e209d39SAndroid Build Coastguard Worker }
240*0e209d39SAndroid Build Coastguard Worker /* c!=0 now, one branch below checks c==0 for variant characters */
241*0e209d39SAndroid Build Coastguard Worker
242*0e209d39SAndroid Build Coastguard Worker /*
243*0e209d39SAndroid Build Coastguard Worker * no assertions here because these functions are legitimately called
244*0e209d39SAndroid Build Coastguard Worker * for strings with variant characters
245*0e209d39SAndroid Build Coastguard Worker */
246*0e209d39SAndroid Build Coastguard Worker #if U_CHARSET_FAMILY==U_ASCII_FAMILY
247*0e209d39SAndroid Build Coastguard Worker if(!UCHAR_IS_INVARIANT(c)) {
248*0e209d39SAndroid Build Coastguard Worker return false; /* found a variant char */
249*0e209d39SAndroid Build Coastguard Worker }
250*0e209d39SAndroid Build Coastguard Worker #elif U_CHARSET_FAMILY==U_EBCDIC_FAMILY
251*0e209d39SAndroid Build Coastguard Worker c=CHAR_TO_UCHAR(c);
252*0e209d39SAndroid Build Coastguard Worker if(c==0 || !UCHAR_IS_INVARIANT(c)) {
253*0e209d39SAndroid Build Coastguard Worker return false; /* found a variant char */
254*0e209d39SAndroid Build Coastguard Worker }
255*0e209d39SAndroid Build Coastguard Worker #else
256*0e209d39SAndroid Build Coastguard Worker # error U_CHARSET_FAMILY is not valid
257*0e209d39SAndroid Build Coastguard Worker #endif
258*0e209d39SAndroid Build Coastguard Worker }
259*0e209d39SAndroid Build Coastguard Worker return true;
260*0e209d39SAndroid Build Coastguard Worker }
261*0e209d39SAndroid Build Coastguard Worker
262*0e209d39SAndroid Build Coastguard Worker U_CAPI UBool U_EXPORT2
uprv_isInvariantUString(const char16_t * s,int32_t length)263*0e209d39SAndroid Build Coastguard Worker uprv_isInvariantUString(const char16_t *s, int32_t length) {
264*0e209d39SAndroid Build Coastguard Worker char16_t c;
265*0e209d39SAndroid Build Coastguard Worker
266*0e209d39SAndroid Build Coastguard Worker for(;;) {
267*0e209d39SAndroid Build Coastguard Worker if(length<0) {
268*0e209d39SAndroid Build Coastguard Worker /* NUL-terminated */
269*0e209d39SAndroid Build Coastguard Worker c=*s++;
270*0e209d39SAndroid Build Coastguard Worker if(c==0) {
271*0e209d39SAndroid Build Coastguard Worker break;
272*0e209d39SAndroid Build Coastguard Worker }
273*0e209d39SAndroid Build Coastguard Worker } else {
274*0e209d39SAndroid Build Coastguard Worker /* count length */
275*0e209d39SAndroid Build Coastguard Worker if(length==0) {
276*0e209d39SAndroid Build Coastguard Worker break;
277*0e209d39SAndroid Build Coastguard Worker }
278*0e209d39SAndroid Build Coastguard Worker --length;
279*0e209d39SAndroid Build Coastguard Worker c=*s++;
280*0e209d39SAndroid Build Coastguard Worker }
281*0e209d39SAndroid Build Coastguard Worker
282*0e209d39SAndroid Build Coastguard Worker /*
283*0e209d39SAndroid Build Coastguard Worker * no assertions here because these functions are legitimately called
284*0e209d39SAndroid Build Coastguard Worker * for strings with variant characters
285*0e209d39SAndroid Build Coastguard Worker */
286*0e209d39SAndroid Build Coastguard Worker if(!UCHAR_IS_INVARIANT(c)) {
287*0e209d39SAndroid Build Coastguard Worker return false; /* found a variant char */
288*0e209d39SAndroid Build Coastguard Worker }
289*0e209d39SAndroid Build Coastguard Worker }
290*0e209d39SAndroid Build Coastguard Worker return true;
291*0e209d39SAndroid Build Coastguard Worker }
292*0e209d39SAndroid Build Coastguard Worker
293*0e209d39SAndroid Build Coastguard Worker /* UDataSwapFn implementations used in udataswp.c ------- */
294*0e209d39SAndroid Build Coastguard Worker
295*0e209d39SAndroid Build Coastguard Worker /* convert ASCII to EBCDIC and verify that all characters are invariant */
296*0e209d39SAndroid Build Coastguard Worker U_CAPI int32_t U_EXPORT2
uprv_ebcdicFromAscii(const UDataSwapper * ds,const void * inData,int32_t length,void * outData,UErrorCode * pErrorCode)297*0e209d39SAndroid Build Coastguard Worker uprv_ebcdicFromAscii(const UDataSwapper *ds,
298*0e209d39SAndroid Build Coastguard Worker const void *inData, int32_t length, void *outData,
299*0e209d39SAndroid Build Coastguard Worker UErrorCode *pErrorCode) {
300*0e209d39SAndroid Build Coastguard Worker const uint8_t *s;
301*0e209d39SAndroid Build Coastguard Worker uint8_t *t;
302*0e209d39SAndroid Build Coastguard Worker uint8_t c;
303*0e209d39SAndroid Build Coastguard Worker
304*0e209d39SAndroid Build Coastguard Worker int32_t count;
305*0e209d39SAndroid Build Coastguard Worker
306*0e209d39SAndroid Build Coastguard Worker if(pErrorCode==nullptr || U_FAILURE(*pErrorCode)) {
307*0e209d39SAndroid Build Coastguard Worker return 0;
308*0e209d39SAndroid Build Coastguard Worker }
309*0e209d39SAndroid Build Coastguard Worker if(ds==nullptr || inData==nullptr || length<0 || (length>0 && outData==nullptr)) {
310*0e209d39SAndroid Build Coastguard Worker *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
311*0e209d39SAndroid Build Coastguard Worker return 0;
312*0e209d39SAndroid Build Coastguard Worker }
313*0e209d39SAndroid Build Coastguard Worker
314*0e209d39SAndroid Build Coastguard Worker /* setup and swapping */
315*0e209d39SAndroid Build Coastguard Worker s=(const uint8_t *)inData;
316*0e209d39SAndroid Build Coastguard Worker t=(uint8_t *)outData;
317*0e209d39SAndroid Build Coastguard Worker count=length;
318*0e209d39SAndroid Build Coastguard Worker while(count>0) {
319*0e209d39SAndroid Build Coastguard Worker c=*s++;
320*0e209d39SAndroid Build Coastguard Worker if(!UCHAR_IS_INVARIANT(c)) {
321*0e209d39SAndroid Build Coastguard Worker udata_printError(ds, "uprv_ebcdicFromAscii() string[%d] contains a variant character in position %d\n",
322*0e209d39SAndroid Build Coastguard Worker length, length-count);
323*0e209d39SAndroid Build Coastguard Worker *pErrorCode=U_INVALID_CHAR_FOUND;
324*0e209d39SAndroid Build Coastguard Worker return 0;
325*0e209d39SAndroid Build Coastguard Worker }
326*0e209d39SAndroid Build Coastguard Worker *t++=ebcdicFromAscii[c];
327*0e209d39SAndroid Build Coastguard Worker --count;
328*0e209d39SAndroid Build Coastguard Worker }
329*0e209d39SAndroid Build Coastguard Worker
330*0e209d39SAndroid Build Coastguard Worker return length;
331*0e209d39SAndroid Build Coastguard Worker }
332*0e209d39SAndroid Build Coastguard Worker
333*0e209d39SAndroid Build Coastguard Worker /* this function only checks and copies ASCII strings without conversion */
334*0e209d39SAndroid Build Coastguard Worker U_CFUNC int32_t
uprv_copyAscii(const UDataSwapper * ds,const void * inData,int32_t length,void * outData,UErrorCode * pErrorCode)335*0e209d39SAndroid Build Coastguard Worker uprv_copyAscii(const UDataSwapper *ds,
336*0e209d39SAndroid Build Coastguard Worker const void *inData, int32_t length, void *outData,
337*0e209d39SAndroid Build Coastguard Worker UErrorCode *pErrorCode) {
338*0e209d39SAndroid Build Coastguard Worker const uint8_t *s;
339*0e209d39SAndroid Build Coastguard Worker uint8_t c;
340*0e209d39SAndroid Build Coastguard Worker
341*0e209d39SAndroid Build Coastguard Worker int32_t count;
342*0e209d39SAndroid Build Coastguard Worker
343*0e209d39SAndroid Build Coastguard Worker if(pErrorCode==nullptr || U_FAILURE(*pErrorCode)) {
344*0e209d39SAndroid Build Coastguard Worker return 0;
345*0e209d39SAndroid Build Coastguard Worker }
346*0e209d39SAndroid Build Coastguard Worker if(ds==nullptr || inData==nullptr || length<0 || (length>0 && outData==nullptr)) {
347*0e209d39SAndroid Build Coastguard Worker *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
348*0e209d39SAndroid Build Coastguard Worker return 0;
349*0e209d39SAndroid Build Coastguard Worker }
350*0e209d39SAndroid Build Coastguard Worker
351*0e209d39SAndroid Build Coastguard Worker /* setup and checking */
352*0e209d39SAndroid Build Coastguard Worker s=(const uint8_t *)inData;
353*0e209d39SAndroid Build Coastguard Worker count=length;
354*0e209d39SAndroid Build Coastguard Worker while(count>0) {
355*0e209d39SAndroid Build Coastguard Worker c=*s++;
356*0e209d39SAndroid Build Coastguard Worker if(!UCHAR_IS_INVARIANT(c)) {
357*0e209d39SAndroid Build Coastguard Worker udata_printError(ds, "uprv_copyFromAscii() string[%d] contains a variant character in position %d\n",
358*0e209d39SAndroid Build Coastguard Worker length, length-count);
359*0e209d39SAndroid Build Coastguard Worker *pErrorCode=U_INVALID_CHAR_FOUND;
360*0e209d39SAndroid Build Coastguard Worker return 0;
361*0e209d39SAndroid Build Coastguard Worker }
362*0e209d39SAndroid Build Coastguard Worker --count;
363*0e209d39SAndroid Build Coastguard Worker }
364*0e209d39SAndroid Build Coastguard Worker
365*0e209d39SAndroid Build Coastguard Worker if(length>0 && inData!=outData) {
366*0e209d39SAndroid Build Coastguard Worker uprv_memcpy(outData, inData, length);
367*0e209d39SAndroid Build Coastguard Worker }
368*0e209d39SAndroid Build Coastguard Worker
369*0e209d39SAndroid Build Coastguard Worker return length;
370*0e209d39SAndroid Build Coastguard Worker }
371*0e209d39SAndroid Build Coastguard Worker
372*0e209d39SAndroid Build Coastguard Worker /* convert EBCDIC to ASCII and verify that all characters are invariant */
373*0e209d39SAndroid Build Coastguard Worker U_CFUNC int32_t
uprv_asciiFromEbcdic(const UDataSwapper * ds,const void * inData,int32_t length,void * outData,UErrorCode * pErrorCode)374*0e209d39SAndroid Build Coastguard Worker uprv_asciiFromEbcdic(const UDataSwapper *ds,
375*0e209d39SAndroid Build Coastguard Worker const void *inData, int32_t length, void *outData,
376*0e209d39SAndroid Build Coastguard Worker UErrorCode *pErrorCode) {
377*0e209d39SAndroid Build Coastguard Worker const uint8_t *s;
378*0e209d39SAndroid Build Coastguard Worker uint8_t *t;
379*0e209d39SAndroid Build Coastguard Worker uint8_t c;
380*0e209d39SAndroid Build Coastguard Worker
381*0e209d39SAndroid Build Coastguard Worker int32_t count;
382*0e209d39SAndroid Build Coastguard Worker
383*0e209d39SAndroid Build Coastguard Worker if(pErrorCode==nullptr || U_FAILURE(*pErrorCode)) {
384*0e209d39SAndroid Build Coastguard Worker return 0;
385*0e209d39SAndroid Build Coastguard Worker }
386*0e209d39SAndroid Build Coastguard Worker if(ds==nullptr || inData==nullptr || length<0 || (length>0 && outData==nullptr)) {
387*0e209d39SAndroid Build Coastguard Worker *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
388*0e209d39SAndroid Build Coastguard Worker return 0;
389*0e209d39SAndroid Build Coastguard Worker }
390*0e209d39SAndroid Build Coastguard Worker
391*0e209d39SAndroid Build Coastguard Worker /* setup and swapping */
392*0e209d39SAndroid Build Coastguard Worker s=(const uint8_t *)inData;
393*0e209d39SAndroid Build Coastguard Worker t=(uint8_t *)outData;
394*0e209d39SAndroid Build Coastguard Worker count=length;
395*0e209d39SAndroid Build Coastguard Worker while(count>0) {
396*0e209d39SAndroid Build Coastguard Worker c=*s++;
397*0e209d39SAndroid Build Coastguard Worker if(c!=0 && ((c=asciiFromEbcdic[c])==0 || !UCHAR_IS_INVARIANT(c))) {
398*0e209d39SAndroid Build Coastguard Worker udata_printError(ds, "uprv_asciiFromEbcdic() string[%d] contains a variant character in position %d\n",
399*0e209d39SAndroid Build Coastguard Worker length, length-count);
400*0e209d39SAndroid Build Coastguard Worker *pErrorCode=U_INVALID_CHAR_FOUND;
401*0e209d39SAndroid Build Coastguard Worker return 0;
402*0e209d39SAndroid Build Coastguard Worker }
403*0e209d39SAndroid Build Coastguard Worker *t++=c;
404*0e209d39SAndroid Build Coastguard Worker --count;
405*0e209d39SAndroid Build Coastguard Worker }
406*0e209d39SAndroid Build Coastguard Worker
407*0e209d39SAndroid Build Coastguard Worker return length;
408*0e209d39SAndroid Build Coastguard Worker }
409*0e209d39SAndroid Build Coastguard Worker
410*0e209d39SAndroid Build Coastguard Worker /* this function only checks and copies EBCDIC strings without conversion */
411*0e209d39SAndroid Build Coastguard Worker U_CFUNC int32_t
uprv_copyEbcdic(const UDataSwapper * ds,const void * inData,int32_t length,void * outData,UErrorCode * pErrorCode)412*0e209d39SAndroid Build Coastguard Worker uprv_copyEbcdic(const UDataSwapper *ds,
413*0e209d39SAndroid Build Coastguard Worker const void *inData, int32_t length, void *outData,
414*0e209d39SAndroid Build Coastguard Worker UErrorCode *pErrorCode) {
415*0e209d39SAndroid Build Coastguard Worker const uint8_t *s;
416*0e209d39SAndroid Build Coastguard Worker uint8_t c;
417*0e209d39SAndroid Build Coastguard Worker
418*0e209d39SAndroid Build Coastguard Worker int32_t count;
419*0e209d39SAndroid Build Coastguard Worker
420*0e209d39SAndroid Build Coastguard Worker if(pErrorCode==nullptr || U_FAILURE(*pErrorCode)) {
421*0e209d39SAndroid Build Coastguard Worker return 0;
422*0e209d39SAndroid Build Coastguard Worker }
423*0e209d39SAndroid Build Coastguard Worker if(ds==nullptr || inData==nullptr || length<0 || (length>0 && outData==nullptr)) {
424*0e209d39SAndroid Build Coastguard Worker *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
425*0e209d39SAndroid Build Coastguard Worker return 0;
426*0e209d39SAndroid Build Coastguard Worker }
427*0e209d39SAndroid Build Coastguard Worker
428*0e209d39SAndroid Build Coastguard Worker /* setup and checking */
429*0e209d39SAndroid Build Coastguard Worker s=(const uint8_t *)inData;
430*0e209d39SAndroid Build Coastguard Worker count=length;
431*0e209d39SAndroid Build Coastguard Worker while(count>0) {
432*0e209d39SAndroid Build Coastguard Worker c=*s++;
433*0e209d39SAndroid Build Coastguard Worker if(c!=0 && ((c=asciiFromEbcdic[c])==0 || !UCHAR_IS_INVARIANT(c))) {
434*0e209d39SAndroid Build Coastguard Worker udata_printError(ds, "uprv_copyEbcdic() string[%] contains a variant character in position %d\n",
435*0e209d39SAndroid Build Coastguard Worker length, length-count);
436*0e209d39SAndroid Build Coastguard Worker *pErrorCode=U_INVALID_CHAR_FOUND;
437*0e209d39SAndroid Build Coastguard Worker return 0;
438*0e209d39SAndroid Build Coastguard Worker }
439*0e209d39SAndroid Build Coastguard Worker --count;
440*0e209d39SAndroid Build Coastguard Worker }
441*0e209d39SAndroid Build Coastguard Worker
442*0e209d39SAndroid Build Coastguard Worker if(length>0 && inData!=outData) {
443*0e209d39SAndroid Build Coastguard Worker uprv_memcpy(outData, inData, length);
444*0e209d39SAndroid Build Coastguard Worker }
445*0e209d39SAndroid Build Coastguard Worker
446*0e209d39SAndroid Build Coastguard Worker return length;
447*0e209d39SAndroid Build Coastguard Worker }
448*0e209d39SAndroid Build Coastguard Worker
449*0e209d39SAndroid Build Coastguard Worker U_CFUNC UBool
uprv_isEbcdicAtSign(char c)450*0e209d39SAndroid Build Coastguard Worker uprv_isEbcdicAtSign(char c) {
451*0e209d39SAndroid Build Coastguard Worker static const uint8_t ebcdicAtSigns[] = {
452*0e209d39SAndroid Build Coastguard Worker 0x7C, 0x44, 0x66, 0x80, 0xAC, 0xAE, 0xAF, 0xB5, 0xEC, 0xEF, 0x00 };
453*0e209d39SAndroid Build Coastguard Worker return c != 0 && uprv_strchr((const char *)ebcdicAtSigns, c) != nullptr;
454*0e209d39SAndroid Build Coastguard Worker }
455*0e209d39SAndroid Build Coastguard Worker
456*0e209d39SAndroid Build Coastguard Worker /* compare invariant strings; variant characters compare less than others and unlike each other */
457*0e209d39SAndroid Build Coastguard Worker U_CFUNC int32_t
uprv_compareInvAscii(const UDataSwapper * ds,const char * outString,int32_t outLength,const char16_t * localString,int32_t localLength)458*0e209d39SAndroid Build Coastguard Worker uprv_compareInvAscii(const UDataSwapper *ds,
459*0e209d39SAndroid Build Coastguard Worker const char *outString, int32_t outLength,
460*0e209d39SAndroid Build Coastguard Worker const char16_t *localString, int32_t localLength) {
461*0e209d39SAndroid Build Coastguard Worker (void)ds;
462*0e209d39SAndroid Build Coastguard Worker int32_t minLength;
463*0e209d39SAndroid Build Coastguard Worker UChar32 c1, c2;
464*0e209d39SAndroid Build Coastguard Worker uint8_t c;
465*0e209d39SAndroid Build Coastguard Worker
466*0e209d39SAndroid Build Coastguard Worker if(outString==nullptr || outLength<-1 || localString==nullptr || localLength<-1) {
467*0e209d39SAndroid Build Coastguard Worker return 0;
468*0e209d39SAndroid Build Coastguard Worker }
469*0e209d39SAndroid Build Coastguard Worker
470*0e209d39SAndroid Build Coastguard Worker if(outLength<0) {
471*0e209d39SAndroid Build Coastguard Worker outLength=(int32_t)uprv_strlen(outString);
472*0e209d39SAndroid Build Coastguard Worker }
473*0e209d39SAndroid Build Coastguard Worker if(localLength<0) {
474*0e209d39SAndroid Build Coastguard Worker localLength=u_strlen(localString);
475*0e209d39SAndroid Build Coastguard Worker }
476*0e209d39SAndroid Build Coastguard Worker
477*0e209d39SAndroid Build Coastguard Worker minLength= outLength<localLength ? outLength : localLength;
478*0e209d39SAndroid Build Coastguard Worker
479*0e209d39SAndroid Build Coastguard Worker while(minLength>0) {
480*0e209d39SAndroid Build Coastguard Worker c=(uint8_t)*outString++;
481*0e209d39SAndroid Build Coastguard Worker if(UCHAR_IS_INVARIANT(c)) {
482*0e209d39SAndroid Build Coastguard Worker c1=c;
483*0e209d39SAndroid Build Coastguard Worker } else {
484*0e209d39SAndroid Build Coastguard Worker c1=-1;
485*0e209d39SAndroid Build Coastguard Worker }
486*0e209d39SAndroid Build Coastguard Worker
487*0e209d39SAndroid Build Coastguard Worker c2=*localString++;
488*0e209d39SAndroid Build Coastguard Worker if(!UCHAR_IS_INVARIANT(c2)) {
489*0e209d39SAndroid Build Coastguard Worker c2=-2;
490*0e209d39SAndroid Build Coastguard Worker }
491*0e209d39SAndroid Build Coastguard Worker
492*0e209d39SAndroid Build Coastguard Worker if((c1-=c2)!=0) {
493*0e209d39SAndroid Build Coastguard Worker return c1;
494*0e209d39SAndroid Build Coastguard Worker }
495*0e209d39SAndroid Build Coastguard Worker
496*0e209d39SAndroid Build Coastguard Worker --minLength;
497*0e209d39SAndroid Build Coastguard Worker }
498*0e209d39SAndroid Build Coastguard Worker
499*0e209d39SAndroid Build Coastguard Worker /* strings start with same prefix, compare lengths */
500*0e209d39SAndroid Build Coastguard Worker return outLength-localLength;
501*0e209d39SAndroid Build Coastguard Worker }
502*0e209d39SAndroid Build Coastguard Worker
503*0e209d39SAndroid Build Coastguard Worker U_CFUNC int32_t
uprv_compareInvEbcdic(const UDataSwapper * ds,const char * outString,int32_t outLength,const char16_t * localString,int32_t localLength)504*0e209d39SAndroid Build Coastguard Worker uprv_compareInvEbcdic(const UDataSwapper *ds,
505*0e209d39SAndroid Build Coastguard Worker const char *outString, int32_t outLength,
506*0e209d39SAndroid Build Coastguard Worker const char16_t *localString, int32_t localLength) {
507*0e209d39SAndroid Build Coastguard Worker (void)ds;
508*0e209d39SAndroid Build Coastguard Worker int32_t minLength;
509*0e209d39SAndroid Build Coastguard Worker UChar32 c1, c2;
510*0e209d39SAndroid Build Coastguard Worker uint8_t c;
511*0e209d39SAndroid Build Coastguard Worker
512*0e209d39SAndroid Build Coastguard Worker if(outString==nullptr || outLength<-1 || localString==nullptr || localLength<-1) {
513*0e209d39SAndroid Build Coastguard Worker return 0;
514*0e209d39SAndroid Build Coastguard Worker }
515*0e209d39SAndroid Build Coastguard Worker
516*0e209d39SAndroid Build Coastguard Worker if(outLength<0) {
517*0e209d39SAndroid Build Coastguard Worker outLength=(int32_t)uprv_strlen(outString);
518*0e209d39SAndroid Build Coastguard Worker }
519*0e209d39SAndroid Build Coastguard Worker if(localLength<0) {
520*0e209d39SAndroid Build Coastguard Worker localLength=u_strlen(localString);
521*0e209d39SAndroid Build Coastguard Worker }
522*0e209d39SAndroid Build Coastguard Worker
523*0e209d39SAndroid Build Coastguard Worker minLength= outLength<localLength ? outLength : localLength;
524*0e209d39SAndroid Build Coastguard Worker
525*0e209d39SAndroid Build Coastguard Worker while(minLength>0) {
526*0e209d39SAndroid Build Coastguard Worker c=(uint8_t)*outString++;
527*0e209d39SAndroid Build Coastguard Worker if(c==0) {
528*0e209d39SAndroid Build Coastguard Worker c1=0;
529*0e209d39SAndroid Build Coastguard Worker } else if((c1=asciiFromEbcdic[c])!=0 && UCHAR_IS_INVARIANT(c1)) {
530*0e209d39SAndroid Build Coastguard Worker /* c1 is set */
531*0e209d39SAndroid Build Coastguard Worker } else {
532*0e209d39SAndroid Build Coastguard Worker c1=-1;
533*0e209d39SAndroid Build Coastguard Worker }
534*0e209d39SAndroid Build Coastguard Worker
535*0e209d39SAndroid Build Coastguard Worker c2=*localString++;
536*0e209d39SAndroid Build Coastguard Worker if(!UCHAR_IS_INVARIANT(c2)) {
537*0e209d39SAndroid Build Coastguard Worker c2=-2;
538*0e209d39SAndroid Build Coastguard Worker }
539*0e209d39SAndroid Build Coastguard Worker
540*0e209d39SAndroid Build Coastguard Worker if((c1-=c2)!=0) {
541*0e209d39SAndroid Build Coastguard Worker return c1;
542*0e209d39SAndroid Build Coastguard Worker }
543*0e209d39SAndroid Build Coastguard Worker
544*0e209d39SAndroid Build Coastguard Worker --minLength;
545*0e209d39SAndroid Build Coastguard Worker }
546*0e209d39SAndroid Build Coastguard Worker
547*0e209d39SAndroid Build Coastguard Worker /* strings start with same prefix, compare lengths */
548*0e209d39SAndroid Build Coastguard Worker return outLength-localLength;
549*0e209d39SAndroid Build Coastguard Worker }
550*0e209d39SAndroid Build Coastguard Worker
551*0e209d39SAndroid Build Coastguard Worker U_CAPI int32_t U_EXPORT2
uprv_compareInvEbcdicAsAscii(const char * s1,const char * s2)552*0e209d39SAndroid Build Coastguard Worker uprv_compareInvEbcdicAsAscii(const char *s1, const char *s2) {
553*0e209d39SAndroid Build Coastguard Worker int32_t c1, c2;
554*0e209d39SAndroid Build Coastguard Worker
555*0e209d39SAndroid Build Coastguard Worker for(;; ++s1, ++s2) {
556*0e209d39SAndroid Build Coastguard Worker c1=(uint8_t)*s1;
557*0e209d39SAndroid Build Coastguard Worker c2=(uint8_t)*s2;
558*0e209d39SAndroid Build Coastguard Worker if(c1!=c2) {
559*0e209d39SAndroid Build Coastguard Worker if(c1!=0 && ((c1=asciiFromEbcdic[c1])==0 || !UCHAR_IS_INVARIANT(c1))) {
560*0e209d39SAndroid Build Coastguard Worker c1=-(int32_t)(uint8_t)*s1;
561*0e209d39SAndroid Build Coastguard Worker }
562*0e209d39SAndroid Build Coastguard Worker if(c2!=0 && ((c2=asciiFromEbcdic[c2])==0 || !UCHAR_IS_INVARIANT(c2))) {
563*0e209d39SAndroid Build Coastguard Worker c2=-(int32_t)(uint8_t)*s2;
564*0e209d39SAndroid Build Coastguard Worker }
565*0e209d39SAndroid Build Coastguard Worker return c1-c2;
566*0e209d39SAndroid Build Coastguard Worker } else if(c1==0) {
567*0e209d39SAndroid Build Coastguard Worker return 0;
568*0e209d39SAndroid Build Coastguard Worker }
569*0e209d39SAndroid Build Coastguard Worker }
570*0e209d39SAndroid Build Coastguard Worker }
571*0e209d39SAndroid Build Coastguard Worker
572*0e209d39SAndroid Build Coastguard Worker U_CAPI char U_EXPORT2
uprv_ebcdicToAscii(char c)573*0e209d39SAndroid Build Coastguard Worker uprv_ebcdicToAscii(char c) {
574*0e209d39SAndroid Build Coastguard Worker return (char)asciiFromEbcdic[(uint8_t)c];
575*0e209d39SAndroid Build Coastguard Worker }
576*0e209d39SAndroid Build Coastguard Worker
577*0e209d39SAndroid Build Coastguard Worker U_CAPI char U_EXPORT2
uprv_ebcdicToLowercaseAscii(char c)578*0e209d39SAndroid Build Coastguard Worker uprv_ebcdicToLowercaseAscii(char c) {
579*0e209d39SAndroid Build Coastguard Worker return (char)lowercaseAsciiFromEbcdic[(uint8_t)c];
580*0e209d39SAndroid Build Coastguard Worker }
581*0e209d39SAndroid Build Coastguard Worker
582*0e209d39SAndroid Build Coastguard Worker U_CAPI uint8_t* U_EXPORT2
uprv_aestrncpy(uint8_t * dst,const uint8_t * src,int32_t n)583*0e209d39SAndroid Build Coastguard Worker uprv_aestrncpy(uint8_t *dst, const uint8_t *src, int32_t n)
584*0e209d39SAndroid Build Coastguard Worker {
585*0e209d39SAndroid Build Coastguard Worker uint8_t *orig_dst = dst;
586*0e209d39SAndroid Build Coastguard Worker
587*0e209d39SAndroid Build Coastguard Worker if(n==-1) {
588*0e209d39SAndroid Build Coastguard Worker n = static_cast<int32_t>(uprv_strlen((const char*)src)+1); /* copy NUL */
589*0e209d39SAndroid Build Coastguard Worker }
590*0e209d39SAndroid Build Coastguard Worker /* copy non-null */
591*0e209d39SAndroid Build Coastguard Worker while(*src && n>0) {
592*0e209d39SAndroid Build Coastguard Worker *(dst++) = asciiFromEbcdic[*(src++)];
593*0e209d39SAndroid Build Coastguard Worker n--;
594*0e209d39SAndroid Build Coastguard Worker }
595*0e209d39SAndroid Build Coastguard Worker /* pad */
596*0e209d39SAndroid Build Coastguard Worker while(n>0) {
597*0e209d39SAndroid Build Coastguard Worker *(dst++) = 0;
598*0e209d39SAndroid Build Coastguard Worker n--;
599*0e209d39SAndroid Build Coastguard Worker }
600*0e209d39SAndroid Build Coastguard Worker return orig_dst;
601*0e209d39SAndroid Build Coastguard Worker }
602*0e209d39SAndroid Build Coastguard Worker
603*0e209d39SAndroid Build Coastguard Worker U_CAPI uint8_t* U_EXPORT2
uprv_eastrncpy(uint8_t * dst,const uint8_t * src,int32_t n)604*0e209d39SAndroid Build Coastguard Worker uprv_eastrncpy(uint8_t *dst, const uint8_t *src, int32_t n)
605*0e209d39SAndroid Build Coastguard Worker {
606*0e209d39SAndroid Build Coastguard Worker uint8_t *orig_dst = dst;
607*0e209d39SAndroid Build Coastguard Worker
608*0e209d39SAndroid Build Coastguard Worker if(n==-1) {
609*0e209d39SAndroid Build Coastguard Worker n = static_cast<int32_t>(uprv_strlen((const char*)src)+1); /* copy NUL */
610*0e209d39SAndroid Build Coastguard Worker }
611*0e209d39SAndroid Build Coastguard Worker /* copy non-null */
612*0e209d39SAndroid Build Coastguard Worker while(*src && n>0) {
613*0e209d39SAndroid Build Coastguard Worker char ch = ebcdicFromAscii[*(src++)];
614*0e209d39SAndroid Build Coastguard Worker if(ch == 0) {
615*0e209d39SAndroid Build Coastguard Worker ch = ebcdicFromAscii[0x3f]; /* questionmark (subchar) */
616*0e209d39SAndroid Build Coastguard Worker }
617*0e209d39SAndroid Build Coastguard Worker *(dst++) = ch;
618*0e209d39SAndroid Build Coastguard Worker n--;
619*0e209d39SAndroid Build Coastguard Worker }
620*0e209d39SAndroid Build Coastguard Worker /* pad */
621*0e209d39SAndroid Build Coastguard Worker while(n>0) {
622*0e209d39SAndroid Build Coastguard Worker *(dst++) = 0;
623*0e209d39SAndroid Build Coastguard Worker n--;
624*0e209d39SAndroid Build Coastguard Worker }
625*0e209d39SAndroid Build Coastguard Worker return orig_dst;
626*0e209d39SAndroid Build Coastguard Worker }
627*0e209d39SAndroid Build Coastguard Worker
628