uinvchar.cpp 19 KB

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