ucnv_lmb.cpp 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388
  1. // © 2016 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. /*
  4. **********************************************************************
  5. * Copyright (C) 2000-2016, International Business Machines
  6. * Corporation and others. All Rights Reserved.
  7. **********************************************************************
  8. * file name: ucnv_lmb.cpp
  9. * encoding: UTF-8
  10. * tab size: 4 (not used)
  11. * indentation:4
  12. *
  13. * created on: 2000feb09
  14. * created by: Brendan Murray
  15. * extensively hacked up by: Jim Snyder-Grant
  16. *
  17. * Modification History:
  18. *
  19. * Date Name Description
  20. *
  21. * 06/20/2000 helena OS/400 port changes; mostly typecast.
  22. * 06/27/2000 Jim Snyder-Grant Deal with partial characters and small buffers.
  23. * Add comments to document LMBCS format and implementation
  24. * restructured order & breakdown of functions
  25. * 06/28/2000 helena Major rewrite for the callback API changes.
  26. */
  27. #include "unicode/utypes.h"
  28. #if !UCONFIG_NO_CONVERSION && !UCONFIG_NO_LEGACY_CONVERSION && !UCONFIG_ONLY_HTML_CONVERSION
  29. #include "unicode/ucnv_err.h"
  30. #include "unicode/ucnv.h"
  31. #include "unicode/uset.h"
  32. #include "cmemory.h"
  33. #include "cstring.h"
  34. #include "uassert.h"
  35. #include "ucnv_imp.h"
  36. #include "ucnv_bld.h"
  37. #include "ucnv_cnv.h"
  38. #ifdef EBCDIC_RTL
  39. #error #include "ascii_a.h"
  40. #endif
  41. /*
  42. LMBCS
  43. (Lotus Multi-Byte Character Set)
  44. LMBCS was invented in the late 1980's and is primarily used in Lotus Notes
  45. databases and in Lotus 1-2-3 files. Programmers who work with the APIs
  46. into these products will sometimes need to deal with strings in this format.
  47. The code in this file provides an implementation for an ICU converter of
  48. LMBCS to and from Unicode.
  49. Since the LMBCS character set is only sparsely documented in existing
  50. printed or online material, we have added extensive annotation to this
  51. file to serve as a guide to understanding LMBCS.
  52. LMBCS was originally designed with these four sometimes-competing design goals:
  53. -Provide encodings for the characters in 12 existing national standards
  54. (plus a few other characters)
  55. -Minimal memory footprint
  56. -Maximal speed of conversion into the existing national character sets
  57. -No need to track a changing state as you interpret a string.
  58. All of the national character sets LMBCS was trying to encode are 'ANSI'
  59. based, in that the bytes from 0x20 - 0x7F are almost exactly the
  60. same common Latin unaccented characters and symbols in all character sets.
  61. So, in order to help meet the speed & memory design goals, the common ANSI
  62. bytes from 0x20-0x7F are represented by the same single-byte values in LMBCS.
  63. The general LMBCS code unit is from 1-3 bytes. We can describe the 3 bytes as
  64. follows:
  65. [G] D1 [D2]
  66. That is, a sometimes-optional 'group' byte, followed by 1 and sometimes 2
  67. data bytes. The maximum size of a LMBCS character is 3 bytes:
  68. */
  69. #define ULMBCS_CHARSIZE_MAX 3
  70. /*
  71. The single-byte values from 0x20 to 0x7F are examples of single D1 bytes.
  72. We often have to figure out if byte values are below or above this, so we
  73. use the ANSI nomenclature 'C0' and 'C1' to refer to the range of control
  74. characters just above & below the common lower-ANSI range */
  75. #define ULMBCS_C0END 0x1F
  76. #define ULMBCS_C1START 0x80
  77. /*
  78. Since LMBCS is always dealing in byte units. we create a local type here for
  79. dealing with these units of LMBCS code units:
  80. */
  81. typedef uint8_t ulmbcs_byte_t;
  82. /*
  83. Most of the values less than 0x20 are reserved in LMBCS to announce
  84. which national character standard is being used for the 'D' bytes.
  85. In the comments we show the common name and the IBM character-set ID
  86. for these character-set announcers:
  87. */
  88. #define ULMBCS_GRP_L1 0x01 /* Latin-1 :ibm-850 */
  89. #define ULMBCS_GRP_GR 0x02 /* Greek :ibm-851 */
  90. #define ULMBCS_GRP_HE 0x03 /* Hebrew :ibm-1255 */
  91. #define ULMBCS_GRP_AR 0x04 /* Arabic :ibm-1256 */
  92. #define ULMBCS_GRP_RU 0x05 /* Cyrillic :ibm-1251 */
  93. #define ULMBCS_GRP_L2 0x06 /* Latin-2 :ibm-852 */
  94. #define ULMBCS_GRP_TR 0x08 /* Turkish :ibm-1254 */
  95. #define ULMBCS_GRP_TH 0x0B /* Thai :ibm-874 */
  96. #define ULMBCS_GRP_JA 0x10 /* Japanese :ibm-943 */
  97. #define ULMBCS_GRP_KO 0x11 /* Korean :ibm-1261 */
  98. #define ULMBCS_GRP_TW 0x12 /* Chinese SC :ibm-950 */
  99. #define ULMBCS_GRP_CN 0x13 /* Chinese TC :ibm-1386 */
  100. /*
  101. So, the beginning of understanding LMBCS is that IF the first byte of a LMBCS
  102. character is one of those 12 values, you can interpret the remaining bytes of
  103. that character as coming from one of those character sets. Since the lower
  104. ANSI bytes already are represented in single bytes, using one of the character
  105. set announcers is used to announce a character that starts with a byte of
  106. 0x80 or greater.
  107. The character sets are arranged so that the single byte sets all appear
  108. before the multi-byte character sets. When we need to tell whether a
  109. group byte is for a single byte char set or not we use this define: */
  110. #define ULMBCS_DOUBLEOPTGROUP_START 0x10
  111. /*
  112. However, to fully understand LMBCS, you must also understand a series of
  113. exceptions & optimizations made in service of the design goals.
  114. First, those of you who are character set mavens may have noticed that
  115. the 'double-byte' character sets are actually multi-byte character sets
  116. that can have 1 or two bytes, even in the upper-ascii range. To force
  117. each group byte to introduce a fixed-width encoding (to make it faster to
  118. count characters), we use a convention of doubling up on the group byte
  119. to introduce any single-byte character > 0x80 in an otherwise double-byte
  120. character set. So, for example, the LMBCS sequence x10 x10 xAE is the
  121. same as '0xAE' in the Japanese code page 943.
  122. Next, you will notice that the list of group bytes has some gaps.
  123. These are used in various ways.
  124. We reserve a few special single byte values for common control
  125. characters. These are in the same place as their ANSI equivalents for speed.
  126. */
  127. #define ULMBCS_HT 0x09 /* Fixed control char - Horizontal Tab */
  128. #define ULMBCS_LF 0x0A /* Fixed control char - Line Feed */
  129. #define ULMBCS_CR 0x0D /* Fixed control char - Carriage Return */
  130. /* Then, 1-2-3 reserved a special single-byte character to put at the
  131. beginning of internal 'system' range names: */
  132. #define ULMBCS_123SYSTEMRANGE 0x19
  133. /* Then we needed a place to put all the other ansi control characters
  134. that must be moved to different values because LMBCS reserves those
  135. values for other purposes. To represent the control characters, we start
  136. with a first byte of 0xF & add the control character value as the
  137. second byte */
  138. #define ULMBCS_GRP_CTRL 0x0F
  139. /* For the C0 controls (less than 0x20), we add 0x20 to preserve the
  140. useful doctrine that any byte less than 0x20 in a LMBCS char must be
  141. the first byte of a character:*/
  142. #define ULMBCS_CTRLOFFSET 0x20
  143. /*
  144. Where to put the characters that aren't part of any of the 12 national
  145. character sets? The first thing that was done, in the earlier years of
  146. LMBCS, was to use up the spaces of the form
  147. [G] D1,
  148. where 'G' was one of the single-byte character groups, and
  149. D1 was less than 0x80. These sequences are gathered together
  150. into a Lotus-invented doublebyte character set to represent a
  151. lot of stray values. Internally, in this implementation, we track this
  152. as group '0', as a place to tuck this exceptions list.*/
  153. #define ULMBCS_GRP_EXCEPT 0x00
  154. /*
  155. Finally, as the durability and usefulness of UNICODE became clear,
  156. LOTUS added a new group 0x14 to hold Unicode values not otherwise
  157. represented in LMBCS: */
  158. #define ULMBCS_GRP_UNICODE 0x14
  159. /* The two bytes appearing after a 0x14 are interpreted as UFT-16 BE
  160. (Big-Endian) characters. The exception comes when the UTF16
  161. representation would have a zero as the second byte. In that case,
  162. 'F6' is used in its place, and the bytes are swapped. (This prevents
  163. LMBCS from encoding any Unicode values of the form U+F6xx, but that's OK:
  164. 0xF6xx is in the middle of the Private Use Area.)*/
  165. #define ULMBCS_UNICOMPATZERO 0xF6
  166. /* It is also useful in our code to have a constant for the size of
  167. a LMBCS char that holds a literal Unicode value */
  168. #define ULMBCS_UNICODE_SIZE 3
  169. /*
  170. To squish the LMBCS representations down even further, and to make
  171. translations even faster,sometimes the optimization group byte can be dropped
  172. from a LMBCS character. This is decided on a process-by-process basis. The
  173. group byte that is dropped is called the 'optimization group'.
  174. For Notes, the optimzation group is always 0x1.*/
  175. #define ULMBCS_DEFAULTOPTGROUP 0x1
  176. /* For 1-2-3 files, the optimzation group is stored in the header of the 1-2-3
  177. file.
  178. In any case, when using ICU, you either pass in the
  179. optimization group as part of the name of the converter (LMBCS-1, LMBCS-2,
  180. etc.). Using plain 'LMBCS' as the name of the converter will give you
  181. LMBCS-1.
  182. *** Implementation strategy ***
  183. Because of the extensive use of other character sets, the LMBCS converter
  184. keeps a mapping between optimization groups and IBM character sets, so that
  185. ICU converters can be created and used as needed. */
  186. /* As you can see, even though any byte below 0x20 could be an optimization
  187. byte, only those at 0x13 or below can map to an actual converter. To limit
  188. some loops and searches, we define a value for that last group converter:*/
  189. #define ULMBCS_GRP_LAST 0x13 /* last LMBCS group that has a converter */
  190. static const char * const OptGroupByteToCPName[ULMBCS_GRP_LAST + 1] = {
  191. /* 0x0000 */ "lmb-excp", /* internal home for the LOTUS exceptions list */
  192. /* 0x0001 */ "ibm-850",
  193. /* 0x0002 */ "ibm-851",
  194. /* 0x0003 */ "windows-1255",
  195. /* 0x0004 */ "windows-1256",
  196. /* 0x0005 */ "windows-1251",
  197. /* 0x0006 */ "ibm-852",
  198. /* 0x0007 */ nullptr, /* Unused */
  199. /* 0x0008 */ "windows-1254",
  200. /* 0x0009 */ nullptr, /* Control char HT */
  201. /* 0x000A */ nullptr, /* Control char LF */
  202. /* 0x000B */ "windows-874",
  203. /* 0x000C */ nullptr, /* Unused */
  204. /* 0x000D */ nullptr, /* Control char CR */
  205. /* 0x000E */ nullptr, /* Unused */
  206. /* 0x000F */ nullptr, /* Control chars: 0x0F20 + C0/C1 character: algorithmic */
  207. /* 0x0010 */ "windows-932",
  208. /* 0x0011 */ "windows-949",
  209. /* 0x0012 */ "windows-950",
  210. /* 0x0013 */ "windows-936"
  211. /* The rest are null, including the 0x0014 Unicode compatibility region
  212. and 0x0019, the 1-2-3 system range control char */
  213. };
  214. /* That's approximately all the data that's needed for translating
  215. LMBCS to Unicode.
  216. However, to translate Unicode to LMBCS, we need some more support.
  217. That's because there are often more than one possible mappings from a Unicode
  218. code point back into LMBCS. The first thing we do is look up into a table
  219. to figure out if there are more than one possible mappings. This table,
  220. arranged by Unicode values (including ranges) either lists which group
  221. to use, or says that it could go into one or more of the SBCS sets, or
  222. into one or more of the DBCS sets. (If the character exists in both DBCS &
  223. SBCS, the table will place it in the SBCS sets, to make the LMBCS code point
  224. length as small as possible. Here's the two special markers we use to indicate
  225. ambiguous mappings: */
  226. #define ULMBCS_AMBIGUOUS_SBCS 0x80 /* could fit in more than one
  227. LMBCS sbcs native encoding
  228. (example: most accented latin) */
  229. #define ULMBCS_AMBIGUOUS_MBCS 0x81 /* could fit in more than one
  230. LMBCS mbcs native encoding
  231. (example: Unihan) */
  232. #define ULMBCS_AMBIGUOUS_ALL 0x82
  233. /* And here's a simple way to see if a group falls in an appropriate range */
  234. #define ULMBCS_AMBIGUOUS_MATCH(agroup, xgroup) \
  235. ((((agroup) == ULMBCS_AMBIGUOUS_SBCS) && \
  236. (xgroup) < ULMBCS_DOUBLEOPTGROUP_START) || \
  237. (((agroup) == ULMBCS_AMBIGUOUS_MBCS) && \
  238. (xgroup) >= ULMBCS_DOUBLEOPTGROUP_START)) || \
  239. ((agroup) == ULMBCS_AMBIGUOUS_ALL)
  240. /* The table & some code to use it: */
  241. static const struct _UniLMBCSGrpMap
  242. {
  243. const char16_t uniStartRange;
  244. const char16_t uniEndRange;
  245. const ulmbcs_byte_t GrpType;
  246. } UniLMBCSGrpMap[]
  247. =
  248. {
  249. {0x0001, 0x001F, ULMBCS_GRP_CTRL},
  250. {0x0080, 0x009F, ULMBCS_GRP_CTRL},
  251. {0x00A0, 0x00A6, ULMBCS_AMBIGUOUS_SBCS},
  252. {0x00A7, 0x00A8, ULMBCS_AMBIGUOUS_ALL},
  253. {0x00A9, 0x00AF, ULMBCS_AMBIGUOUS_SBCS},
  254. {0x00B0, 0x00B1, ULMBCS_AMBIGUOUS_ALL},
  255. {0x00B2, 0x00B3, ULMBCS_AMBIGUOUS_SBCS},
  256. {0x00B4, 0x00B4, ULMBCS_AMBIGUOUS_ALL},
  257. {0x00B5, 0x00B5, ULMBCS_AMBIGUOUS_SBCS},
  258. {0x00B6, 0x00B6, ULMBCS_AMBIGUOUS_ALL},
  259. {0x00B7, 0x00D6, ULMBCS_AMBIGUOUS_SBCS},
  260. {0x00D7, 0x00D7, ULMBCS_AMBIGUOUS_ALL},
  261. {0x00D8, 0x00F6, ULMBCS_AMBIGUOUS_SBCS},
  262. {0x00F7, 0x00F7, ULMBCS_AMBIGUOUS_ALL},
  263. {0x00F8, 0x01CD, ULMBCS_AMBIGUOUS_SBCS},
  264. {0x01CE, 0x01CE, ULMBCS_GRP_TW },
  265. {0x01CF, 0x02B9, ULMBCS_AMBIGUOUS_SBCS},
  266. {0x02BA, 0x02BA, ULMBCS_GRP_CN},
  267. {0x02BC, 0x02C8, ULMBCS_AMBIGUOUS_SBCS},
  268. {0x02C9, 0x02D0, ULMBCS_AMBIGUOUS_MBCS},
  269. {0x02D8, 0x02DD, ULMBCS_AMBIGUOUS_SBCS},
  270. {0x0384, 0x0390, ULMBCS_AMBIGUOUS_SBCS},
  271. {0x0391, 0x03A9, ULMBCS_AMBIGUOUS_ALL},
  272. {0x03AA, 0x03B0, ULMBCS_AMBIGUOUS_SBCS},
  273. {0x03B1, 0x03C9, ULMBCS_AMBIGUOUS_ALL},
  274. {0x03CA, 0x03CE, ULMBCS_AMBIGUOUS_SBCS},
  275. {0x0400, 0x0400, ULMBCS_GRP_RU},
  276. {0x0401, 0x0401, ULMBCS_AMBIGUOUS_ALL},
  277. {0x0402, 0x040F, ULMBCS_GRP_RU},
  278. {0x0410, 0x0431, ULMBCS_AMBIGUOUS_ALL},
  279. {0x0432, 0x044E, ULMBCS_GRP_RU},
  280. {0x044F, 0x044F, ULMBCS_AMBIGUOUS_ALL},
  281. {0x0450, 0x0491, ULMBCS_GRP_RU},
  282. {0x05B0, 0x05F2, ULMBCS_GRP_HE},
  283. {0x060C, 0x06AF, ULMBCS_GRP_AR},
  284. {0x0E01, 0x0E5B, ULMBCS_GRP_TH},
  285. {0x200C, 0x200F, ULMBCS_AMBIGUOUS_SBCS},
  286. {0x2010, 0x2010, ULMBCS_AMBIGUOUS_MBCS},
  287. {0x2013, 0x2014, ULMBCS_AMBIGUOUS_SBCS},
  288. {0x2015, 0x2015, ULMBCS_AMBIGUOUS_MBCS},
  289. {0x2016, 0x2016, ULMBCS_AMBIGUOUS_MBCS},
  290. {0x2017, 0x2017, ULMBCS_AMBIGUOUS_SBCS},
  291. {0x2018, 0x2019, ULMBCS_AMBIGUOUS_ALL},
  292. {0x201A, 0x201B, ULMBCS_AMBIGUOUS_SBCS},
  293. {0x201C, 0x201D, ULMBCS_AMBIGUOUS_ALL},
  294. {0x201E, 0x201F, ULMBCS_AMBIGUOUS_SBCS},
  295. {0x2020, 0x2021, ULMBCS_AMBIGUOUS_ALL},
  296. {0x2022, 0x2024, ULMBCS_AMBIGUOUS_SBCS},
  297. {0x2025, 0x2025, ULMBCS_AMBIGUOUS_MBCS},
  298. {0x2026, 0x2026, ULMBCS_AMBIGUOUS_ALL},
  299. {0x2027, 0x2027, ULMBCS_GRP_TW},
  300. {0x2030, 0x2030, ULMBCS_AMBIGUOUS_ALL},
  301. {0x2031, 0x2031, ULMBCS_AMBIGUOUS_SBCS},
  302. {0x2032, 0x2033, ULMBCS_AMBIGUOUS_MBCS},
  303. {0x2035, 0x2035, ULMBCS_AMBIGUOUS_MBCS},
  304. {0x2039, 0x203A, ULMBCS_AMBIGUOUS_SBCS},
  305. {0x203B, 0x203B, ULMBCS_AMBIGUOUS_MBCS},
  306. {0x203C, 0x203C, ULMBCS_GRP_EXCEPT},
  307. {0x2074, 0x2074, ULMBCS_GRP_KO},
  308. {0x207F, 0x207F, ULMBCS_GRP_EXCEPT},
  309. {0x2081, 0x2084, ULMBCS_GRP_KO},
  310. {0x20A4, 0x20AC, ULMBCS_AMBIGUOUS_SBCS},
  311. {0x2103, 0x2109, ULMBCS_AMBIGUOUS_MBCS},
  312. {0x2111, 0x2120, ULMBCS_AMBIGUOUS_SBCS},
  313. /*zhujin: upgrade, for regressiont test, spr HKIA4YHTSU*/
  314. {0x2121, 0x2121, ULMBCS_AMBIGUOUS_MBCS},
  315. {0x2122, 0x2126, ULMBCS_AMBIGUOUS_SBCS},
  316. {0x212B, 0x212B, ULMBCS_AMBIGUOUS_MBCS},
  317. {0x2135, 0x2135, ULMBCS_AMBIGUOUS_SBCS},
  318. {0x2153, 0x2154, ULMBCS_GRP_KO},
  319. {0x215B, 0x215E, ULMBCS_GRP_EXCEPT},
  320. {0x2160, 0x2179, ULMBCS_AMBIGUOUS_MBCS},
  321. {0x2190, 0x2193, ULMBCS_AMBIGUOUS_ALL},
  322. {0x2194, 0x2195, ULMBCS_GRP_EXCEPT},
  323. {0x2196, 0x2199, ULMBCS_AMBIGUOUS_MBCS},
  324. {0x21A8, 0x21A8, ULMBCS_GRP_EXCEPT},
  325. {0x21B8, 0x21B9, ULMBCS_GRP_CN},
  326. {0x21D0, 0x21D1, ULMBCS_GRP_EXCEPT},
  327. {0x21D2, 0x21D2, ULMBCS_AMBIGUOUS_MBCS},
  328. {0x21D3, 0x21D3, ULMBCS_GRP_EXCEPT},
  329. {0x21D4, 0x21D4, ULMBCS_AMBIGUOUS_MBCS},
  330. {0x21D5, 0x21D5, ULMBCS_GRP_EXCEPT},
  331. {0x21E7, 0x21E7, ULMBCS_GRP_CN},
  332. {0x2200, 0x2200, ULMBCS_AMBIGUOUS_MBCS},
  333. {0x2201, 0x2201, ULMBCS_GRP_EXCEPT},
  334. {0x2202, 0x2202, ULMBCS_AMBIGUOUS_MBCS},
  335. {0x2203, 0x2203, ULMBCS_AMBIGUOUS_MBCS},
  336. {0x2204, 0x2206, ULMBCS_GRP_EXCEPT},
  337. {0x2207, 0x2208, ULMBCS_AMBIGUOUS_MBCS},
  338. {0x2209, 0x220A, ULMBCS_GRP_EXCEPT},
  339. {0x220B, 0x220B, ULMBCS_AMBIGUOUS_MBCS},
  340. {0x220F, 0x2215, ULMBCS_AMBIGUOUS_MBCS},
  341. {0x2219, 0x2219, ULMBCS_GRP_EXCEPT},
  342. {0x221A, 0x221A, ULMBCS_AMBIGUOUS_MBCS},
  343. {0x221B, 0x221C, ULMBCS_GRP_EXCEPT},
  344. {0x221D, 0x221E, ULMBCS_AMBIGUOUS_MBCS},
  345. {0x221F, 0x221F, ULMBCS_GRP_EXCEPT},
  346. {0x2220, 0x2220, ULMBCS_AMBIGUOUS_MBCS},
  347. {0x2223, 0x222A, ULMBCS_AMBIGUOUS_MBCS},
  348. {0x222B, 0x223D, ULMBCS_AMBIGUOUS_MBCS},
  349. {0x2245, 0x2248, ULMBCS_GRP_EXCEPT},
  350. {0x224C, 0x224C, ULMBCS_GRP_TW},
  351. {0x2252, 0x2252, ULMBCS_AMBIGUOUS_MBCS},
  352. {0x2260, 0x2261, ULMBCS_AMBIGUOUS_MBCS},
  353. {0x2262, 0x2265, ULMBCS_GRP_EXCEPT},
  354. {0x2266, 0x226F, ULMBCS_AMBIGUOUS_MBCS},
  355. {0x2282, 0x2283, ULMBCS_AMBIGUOUS_MBCS},
  356. {0x2284, 0x2285, ULMBCS_GRP_EXCEPT},
  357. {0x2286, 0x2287, ULMBCS_AMBIGUOUS_MBCS},
  358. {0x2288, 0x2297, ULMBCS_GRP_EXCEPT},
  359. {0x2299, 0x22BF, ULMBCS_AMBIGUOUS_MBCS},
  360. {0x22C0, 0x22C0, ULMBCS_GRP_EXCEPT},
  361. {0x2310, 0x2310, ULMBCS_GRP_EXCEPT},
  362. {0x2312, 0x2312, ULMBCS_AMBIGUOUS_MBCS},
  363. {0x2318, 0x2321, ULMBCS_GRP_EXCEPT},
  364. {0x2318, 0x2321, ULMBCS_GRP_CN},
  365. {0x2460, 0x24E9, ULMBCS_AMBIGUOUS_MBCS},
  366. {0x2500, 0x2500, ULMBCS_AMBIGUOUS_SBCS},
  367. {0x2501, 0x2501, ULMBCS_AMBIGUOUS_MBCS},
  368. {0x2502, 0x2502, ULMBCS_AMBIGUOUS_ALL},
  369. {0x2503, 0x2503, ULMBCS_AMBIGUOUS_MBCS},
  370. {0x2504, 0x2505, ULMBCS_GRP_TW},
  371. {0x2506, 0x2665, ULMBCS_AMBIGUOUS_ALL},
  372. {0x2666, 0x2666, ULMBCS_GRP_EXCEPT},
  373. {0x2667, 0x2669, ULMBCS_AMBIGUOUS_SBCS},
  374. {0x266A, 0x266A, ULMBCS_AMBIGUOUS_ALL},
  375. {0x266B, 0x266C, ULMBCS_AMBIGUOUS_SBCS},
  376. {0x266D, 0x266D, ULMBCS_AMBIGUOUS_MBCS},
  377. {0x266E, 0x266E, ULMBCS_AMBIGUOUS_SBCS},
  378. {0x266F, 0x266F, ULMBCS_GRP_JA},
  379. {0x2670, 0x2E7F, ULMBCS_AMBIGUOUS_SBCS},
  380. {0x2E80, 0xF861, ULMBCS_AMBIGUOUS_MBCS},
  381. {0xF862, 0xF8FF, ULMBCS_GRP_EXCEPT},
  382. {0xF900, 0xFA2D, ULMBCS_AMBIGUOUS_MBCS},
  383. {0xFB00, 0xFEFF, ULMBCS_AMBIGUOUS_SBCS},
  384. {0xFF01, 0xFFEE, ULMBCS_AMBIGUOUS_MBCS},
  385. {0xFFFF, 0xFFFF, ULMBCS_GRP_UNICODE}
  386. };
  387. static ulmbcs_byte_t
  388. FindLMBCSUniRange(char16_t uniChar)
  389. {
  390. const struct _UniLMBCSGrpMap * pTable = UniLMBCSGrpMap;
  391. while (uniChar > pTable->uniEndRange)
  392. {
  393. pTable++;
  394. }
  395. if (uniChar >= pTable->uniStartRange)
  396. {
  397. return pTable->GrpType;
  398. }
  399. return ULMBCS_GRP_UNICODE;
  400. }
  401. /*
  402. We also ask the creator of a converter to send in a preferred locale
  403. that we can use in resolving ambiguous mappings. They send the locale
  404. in as a string, and we map it, if possible, to one of the
  405. LMBCS groups. We use this table, and the associated code, to
  406. do the lookup: */
  407. /**************************************************
  408. This table maps locale ID's to LMBCS opt groups.
  409. The default return is group 0x01. Note that for
  410. performance reasons, the table is sorted in
  411. increasing alphabetic order, with the notable
  412. exception of zhTW. This is to force the check
  413. for Traditonal Chinese before dropping back to
  414. Simplified.
  415. Note too that the Latin-1 groups have been
  416. commented out because it's the default, and
  417. this shortens the table, allowing a serial
  418. search to go quickly.
  419. *************************************************/
  420. static const struct _LocaleLMBCSGrpMap
  421. {
  422. const char *LocaleID;
  423. const ulmbcs_byte_t OptGroup;
  424. } LocaleLMBCSGrpMap[] =
  425. {
  426. {"ar", ULMBCS_GRP_AR},
  427. {"be", ULMBCS_GRP_RU},
  428. {"bg", ULMBCS_GRP_L2},
  429. /* {"ca", ULMBCS_GRP_L1}, */
  430. {"cs", ULMBCS_GRP_L2},
  431. /* {"da", ULMBCS_GRP_L1}, */
  432. /* {"de", ULMBCS_GRP_L1}, */
  433. {"el", ULMBCS_GRP_GR},
  434. /* {"en", ULMBCS_GRP_L1}, */
  435. /* {"es", ULMBCS_GRP_L1}, */
  436. /* {"et", ULMBCS_GRP_L1}, */
  437. /* {"fi", ULMBCS_GRP_L1}, */
  438. /* {"fr", ULMBCS_GRP_L1}, */
  439. {"he", ULMBCS_GRP_HE},
  440. {"hu", ULMBCS_GRP_L2},
  441. /* {"is", ULMBCS_GRP_L1}, */
  442. /* {"it", ULMBCS_GRP_L1}, */
  443. {"iw", ULMBCS_GRP_HE},
  444. {"ja", ULMBCS_GRP_JA},
  445. {"ko", ULMBCS_GRP_KO},
  446. /* {"lt", ULMBCS_GRP_L1}, */
  447. /* {"lv", ULMBCS_GRP_L1}, */
  448. {"mk", ULMBCS_GRP_RU},
  449. /* {"nl", ULMBCS_GRP_L1}, */
  450. /* {"no", ULMBCS_GRP_L1}, */
  451. {"pl", ULMBCS_GRP_L2},
  452. /* {"pt", ULMBCS_GRP_L1}, */
  453. {"ro", ULMBCS_GRP_L2},
  454. {"ru", ULMBCS_GRP_RU},
  455. {"sh", ULMBCS_GRP_L2},
  456. {"sk", ULMBCS_GRP_L2},
  457. {"sl", ULMBCS_GRP_L2},
  458. {"sq", ULMBCS_GRP_L2},
  459. {"sr", ULMBCS_GRP_RU},
  460. /* {"sv", ULMBCS_GRP_L1}, */
  461. {"th", ULMBCS_GRP_TH},
  462. {"tr", ULMBCS_GRP_TR},
  463. {"uk", ULMBCS_GRP_RU},
  464. /* {"vi", ULMBCS_GRP_L1}, */
  465. {"zhTW", ULMBCS_GRP_TW},
  466. {"zh", ULMBCS_GRP_CN},
  467. {nullptr, ULMBCS_GRP_L1}
  468. };
  469. static ulmbcs_byte_t
  470. FindLMBCSLocale(const char *LocaleID)
  471. {
  472. const struct _LocaleLMBCSGrpMap *pTable = LocaleLMBCSGrpMap;
  473. if ((!LocaleID) || (!*LocaleID))
  474. {
  475. return 0;
  476. }
  477. while (pTable->LocaleID)
  478. {
  479. if (*pTable->LocaleID == *LocaleID) /* Check only first char for speed */
  480. {
  481. /* First char matches - check whole name, for entry-length */
  482. if (uprv_strncmp(pTable->LocaleID, LocaleID, strlen(pTable->LocaleID)) == 0)
  483. return pTable->OptGroup;
  484. }
  485. else
  486. if (*pTable->LocaleID > *LocaleID) /* Sorted alphabetically - exit */
  487. break;
  488. pTable++;
  489. }
  490. return ULMBCS_GRP_L1;
  491. }
  492. /*
  493. Before we get to the main body of code, here's how we hook up to the rest
  494. of ICU. ICU converters are required to define a structure that includes
  495. some function pointers, and some common data, in the style of a C++
  496. vtable. There is also room in there for converter-specific data. LMBCS
  497. uses that converter-specific data to keep track of the 12 subconverters
  498. we use, the optimization group, and the group (if any) that matches the
  499. locale. We have one structure instantiated for each of the 12 possible
  500. optimization groups. To avoid typos & to avoid boring the reader, we
  501. put the declarations of these structures and functions into macros. To see
  502. the definitions of these structures, see unicode\ucnv_bld.h
  503. */
  504. typedef struct
  505. {
  506. UConverterSharedData *OptGrpConverter[ULMBCS_GRP_LAST+1]; /* Converter per Opt. grp. */
  507. uint8_t OptGroup; /* default Opt. grp. for this LMBCS session */
  508. uint8_t localeConverterIndex; /* reasonable locale match for index */
  509. }
  510. UConverterDataLMBCS;
  511. U_CDECL_BEGIN
  512. static void U_CALLCONV _LMBCSClose(UConverter * _this);
  513. U_CDECL_END
  514. #define DECLARE_LMBCS_DATA(n) \
  515. static const UConverterImpl _LMBCSImpl##n={\
  516. UCNV_LMBCS_##n,\
  517. nullptr,nullptr,\
  518. _LMBCSOpen##n,\
  519. _LMBCSClose,\
  520. nullptr,\
  521. _LMBCSToUnicodeWithOffsets,\
  522. _LMBCSToUnicodeWithOffsets,\
  523. _LMBCSFromUnicode,\
  524. _LMBCSFromUnicode,\
  525. nullptr,\
  526. nullptr,\
  527. nullptr,\
  528. nullptr,\
  529. _LMBCSSafeClone,\
  530. ucnv_getCompleteUnicodeSet,\
  531. nullptr,\
  532. nullptr\
  533. };\
  534. static const UConverterStaticData _LMBCSStaticData##n={\
  535. sizeof(UConverterStaticData),\
  536. "LMBCS-" #n,\
  537. 0, UCNV_IBM, UCNV_LMBCS_##n, 1, 3,\
  538. { 0x3f, 0, 0, 0 },1,false,false,0,0,{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0} \
  539. };\
  540. const UConverterSharedData _LMBCSData##n= \
  541. UCNV_IMMUTABLE_SHARED_DATA_INITIALIZER(&_LMBCSStaticData##n, &_LMBCSImpl##n);
  542. /* The only function we needed to duplicate 12 times was the 'open'
  543. function, which will do basically the same thing except set a different
  544. optimization group. So, we put the common stuff into a worker function,
  545. and set up another macro to stamp out the 12 open functions:*/
  546. #define DEFINE_LMBCS_OPEN(n) \
  547. static void U_CALLCONV \
  548. _LMBCSOpen##n(UConverter* _this, UConverterLoadArgs* pArgs, UErrorCode* err) \
  549. { _LMBCSOpenWorker(_this, pArgs, err, n); }
  550. /* Here's the open worker & the common close function */
  551. static void
  552. _LMBCSOpenWorker(UConverter* _this,
  553. UConverterLoadArgs *pArgs,
  554. UErrorCode* err,
  555. ulmbcs_byte_t OptGroup)
  556. {
  557. UConverterDataLMBCS* extraInfo = static_cast<UConverterDataLMBCS*>(uprv_malloc(sizeof(UConverterDataLMBCS)));
  558. _this->extraInfo = extraInfo;
  559. if(extraInfo != nullptr)
  560. {
  561. UConverterNamePieces stackPieces;
  562. UConverterLoadArgs stackArgs= UCNV_LOAD_ARGS_INITIALIZER;
  563. ulmbcs_byte_t i;
  564. uprv_memset(extraInfo, 0, sizeof(UConverterDataLMBCS));
  565. stackArgs.onlyTestIsLoadable = pArgs->onlyTestIsLoadable;
  566. for (i=0; i <= ULMBCS_GRP_LAST && U_SUCCESS(*err); i++)
  567. {
  568. if(OptGroupByteToCPName[i] != nullptr) {
  569. extraInfo->OptGrpConverter[i] = ucnv_loadSharedData(OptGroupByteToCPName[i], &stackPieces, &stackArgs, err);
  570. }
  571. }
  572. if(U_FAILURE(*err) || pArgs->onlyTestIsLoadable) {
  573. _LMBCSClose(_this);
  574. return;
  575. }
  576. extraInfo->OptGroup = OptGroup;
  577. extraInfo->localeConverterIndex = FindLMBCSLocale(pArgs->locale);
  578. }
  579. else
  580. {
  581. *err = U_MEMORY_ALLOCATION_ERROR;
  582. }
  583. }
  584. U_CDECL_BEGIN
  585. static void U_CALLCONV
  586. _LMBCSClose(UConverter * _this)
  587. {
  588. if (_this->extraInfo != nullptr)
  589. {
  590. ulmbcs_byte_t Ix;
  591. UConverterDataLMBCS * extraInfo = (UConverterDataLMBCS *) _this->extraInfo;
  592. for (Ix=0; Ix <= ULMBCS_GRP_LAST; Ix++)
  593. {
  594. if (extraInfo->OptGrpConverter[Ix] != nullptr)
  595. ucnv_unloadSharedDataIfReady(extraInfo->OptGrpConverter[Ix]);
  596. }
  597. if (!_this->isExtraLocal) {
  598. uprv_free (_this->extraInfo);
  599. _this->extraInfo = nullptr;
  600. }
  601. }
  602. }
  603. typedef struct LMBCSClone {
  604. UConverter cnv;
  605. UConverterDataLMBCS lmbcs;
  606. } LMBCSClone;
  607. static UConverter * U_CALLCONV
  608. _LMBCSSafeClone(const UConverter *cnv,
  609. void *stackBuffer,
  610. int32_t *pBufferSize,
  611. UErrorCode *status) {
  612. (void)status;
  613. LMBCSClone *newLMBCS;
  614. UConverterDataLMBCS *extraInfo;
  615. int32_t i;
  616. if(*pBufferSize<=0) {
  617. *pBufferSize=(int32_t)sizeof(LMBCSClone);
  618. return nullptr;
  619. }
  620. extraInfo=(UConverterDataLMBCS *)cnv->extraInfo;
  621. newLMBCS=(LMBCSClone *)stackBuffer;
  622. /* ucnv.c/ucnv_safeClone() copied the main UConverter already */
  623. uprv_memcpy(&newLMBCS->lmbcs, extraInfo, sizeof(UConverterDataLMBCS));
  624. /* share the subconverters */
  625. for(i = 0; i <= ULMBCS_GRP_LAST; ++i) {
  626. if(extraInfo->OptGrpConverter[i] != nullptr) {
  627. ucnv_incrementRefCount(extraInfo->OptGrpConverter[i]);
  628. }
  629. }
  630. newLMBCS->cnv.extraInfo = &newLMBCS->lmbcs;
  631. newLMBCS->cnv.isExtraLocal = true;
  632. return &newLMBCS->cnv;
  633. }
  634. /*
  635. * There used to be a _LMBCSGetUnicodeSet() function here (up to svn revision 20117)
  636. * which added all code points except for U+F6xx
  637. * because those cannot be represented in the Unicode group.
  638. * However, it turns out that windows-950 has roundtrips for all of U+F6xx
  639. * which means that LMBCS can convert all Unicode code points after all.
  640. * We now simply use ucnv_getCompleteUnicodeSet().
  641. *
  642. * This may need to be looked at again as Lotus uses _LMBCSGetUnicodeSet(). (091216)
  643. */
  644. /*
  645. Here's the basic helper function that we use when converting from
  646. Unicode to LMBCS, and we suspect that a Unicode character will fit into
  647. one of the 12 groups. The return value is the number of bytes written
  648. starting at pStartLMBCS (if any).
  649. */
  650. static size_t
  651. LMBCSConversionWorker (
  652. UConverterDataLMBCS * extraInfo, /* subconverters, opt & locale groups */
  653. ulmbcs_byte_t group, /* The group to try */
  654. ulmbcs_byte_t * pStartLMBCS, /* where to put the results */
  655. char16_t * pUniChar, /* The input unicode character */
  656. ulmbcs_byte_t * lastConverterIndex, /* output: track last successful group used */
  657. UBool * groups_tried /* output: track any unsuccessful groups */
  658. )
  659. {
  660. ulmbcs_byte_t * pLMBCS = pStartLMBCS;
  661. UConverterSharedData * xcnv = extraInfo->OptGrpConverter[group];
  662. int bytesConverted;
  663. uint32_t value;
  664. ulmbcs_byte_t firstByte;
  665. U_ASSERT(xcnv);
  666. U_ASSERT(group<ULMBCS_GRP_UNICODE);
  667. bytesConverted = ucnv_MBCSFromUChar32(xcnv, *pUniChar, &value, false);
  668. /* get the first result byte */
  669. if(bytesConverted > 0) {
  670. firstByte = (ulmbcs_byte_t)(value >> ((bytesConverted - 1) * 8));
  671. } else {
  672. /* most common failure mode is an unassigned character */
  673. groups_tried[group] = true;
  674. return 0;
  675. }
  676. *lastConverterIndex = group;
  677. /* All initial byte values in lower ascii range should have been caught by now,
  678. except with the exception group.
  679. */
  680. U_ASSERT((firstByte <= ULMBCS_C0END) || (firstByte >= ULMBCS_C1START) || (group == ULMBCS_GRP_EXCEPT));
  681. /* use converted data: first write 0, 1 or two group bytes */
  682. if (group != ULMBCS_GRP_EXCEPT && extraInfo->OptGroup != group)
  683. {
  684. *pLMBCS++ = group;
  685. if (bytesConverted == 1 && group >= ULMBCS_DOUBLEOPTGROUP_START)
  686. {
  687. *pLMBCS++ = group;
  688. }
  689. }
  690. /* don't emit control chars */
  691. if ( bytesConverted == 1 && firstByte < 0x20 )
  692. return 0;
  693. /* then move over the converted data */
  694. switch(bytesConverted)
  695. {
  696. case 4:
  697. *pLMBCS++ = (ulmbcs_byte_t)(value >> 24);
  698. U_FALLTHROUGH;
  699. case 3:
  700. *pLMBCS++ = (ulmbcs_byte_t)(value >> 16);
  701. U_FALLTHROUGH;
  702. case 2:
  703. *pLMBCS++ = (ulmbcs_byte_t)(value >> 8);
  704. U_FALLTHROUGH;
  705. case 1:
  706. *pLMBCS++ = (ulmbcs_byte_t)value;
  707. U_FALLTHROUGH;
  708. default:
  709. /* will never occur */
  710. break;
  711. }
  712. return (pLMBCS - pStartLMBCS);
  713. }
  714. /* This is a much simpler version of above, when we
  715. know we are writing LMBCS using the Unicode group
  716. */
  717. static size_t
  718. LMBCSConvertUni(ulmbcs_byte_t * pLMBCS, char16_t uniChar)
  719. {
  720. /* encode into LMBCS Unicode range */
  721. uint8_t LowCh = (uint8_t)(uniChar & 0x00FF);
  722. uint8_t HighCh = (uint8_t)(uniChar >> 8);
  723. *pLMBCS++ = ULMBCS_GRP_UNICODE;
  724. if (LowCh == 0)
  725. {
  726. *pLMBCS++ = ULMBCS_UNICOMPATZERO;
  727. *pLMBCS++ = HighCh;
  728. }
  729. else
  730. {
  731. *pLMBCS++ = HighCh;
  732. *pLMBCS++ = LowCh;
  733. }
  734. return ULMBCS_UNICODE_SIZE;
  735. }
  736. /* The main Unicode to LMBCS conversion function */
  737. static void U_CALLCONV
  738. _LMBCSFromUnicode(UConverterFromUnicodeArgs* args,
  739. UErrorCode* err)
  740. {
  741. ulmbcs_byte_t lastConverterIndex = 0;
  742. char16_t uniChar;
  743. ulmbcs_byte_t LMBCS[ULMBCS_CHARSIZE_MAX];
  744. ulmbcs_byte_t * pLMBCS;
  745. int32_t bytes_written;
  746. UBool groups_tried[ULMBCS_GRP_LAST+1];
  747. UConverterDataLMBCS * extraInfo = (UConverterDataLMBCS *) args->converter->extraInfo;
  748. int sourceIndex = 0;
  749. /* Basic strategy: attempt to fill in local LMBCS 1-char buffer.(LMBCS)
  750. If that succeeds, see if it will all fit into the target & copy it over
  751. if it does.
  752. We try conversions in the following order:
  753. 1. Single-byte ascii & special fixed control chars (&null)
  754. 2. Look up group in table & try that (could be
  755. A) Unicode group
  756. B) control group,
  757. C) national encoding,
  758. or ambiguous SBCS or MBCS group (on to step 4...)
  759. 3. If its ambiguous, try this order:
  760. A) The optimization group
  761. B) The locale group
  762. C) The last group that succeeded with this string.
  763. D) every other group that's relevant (single or double)
  764. E) If its single-byte ambiguous, try the exceptions group
  765. 4. And as a grand fallback: Unicode
  766. */
  767. /*Fix for SPR#DJOE66JFN3 (Lotus)*/
  768. ulmbcs_byte_t OldConverterIndex = 0;
  769. while (args->source < args->sourceLimit && !U_FAILURE(*err))
  770. {
  771. /*Fix for SPR#DJOE66JFN3 (Lotus)*/
  772. OldConverterIndex = extraInfo->localeConverterIndex;
  773. if (args->target >= args->targetLimit)
  774. {
  775. *err = U_BUFFER_OVERFLOW_ERROR;
  776. break;
  777. }
  778. uniChar = *(args->source);
  779. bytes_written = 0;
  780. pLMBCS = LMBCS;
  781. /* check cases in rough order of how common they are, for speed */
  782. /* single byte matches: strategy 1 */
  783. /*Fix for SPR#DJOE66JFN3 (Lotus)*/
  784. if((uniChar>=0x80) && (uniChar<=0xff)
  785. /*Fix for SPR#JUYA6XAERU and TSAO7GL5NK (Lotus)*/ &&(uniChar!=0xB1) &&(uniChar!=0xD7) &&(uniChar!=0xF7)
  786. &&(uniChar!=0xB0) &&(uniChar!=0xB4) &&(uniChar!=0xB6) &&(uniChar!=0xA7) &&(uniChar!=0xA8))
  787. {
  788. extraInfo->localeConverterIndex = ULMBCS_GRP_L1;
  789. }
  790. if (((uniChar > ULMBCS_C0END) && (uniChar < ULMBCS_C1START)) ||
  791. uniChar == 0 || uniChar == ULMBCS_HT || uniChar == ULMBCS_CR ||
  792. uniChar == ULMBCS_LF || uniChar == ULMBCS_123SYSTEMRANGE
  793. )
  794. {
  795. *pLMBCS++ = (ulmbcs_byte_t ) uniChar;
  796. bytes_written = 1;
  797. }
  798. if (!bytes_written)
  799. {
  800. /* Check by UNICODE range (Strategy 2) */
  801. ulmbcs_byte_t group = FindLMBCSUniRange(uniChar);
  802. if (group == ULMBCS_GRP_UNICODE) /* (Strategy 2A) */
  803. {
  804. pLMBCS += LMBCSConvertUni(pLMBCS,uniChar);
  805. bytes_written = (int32_t)(pLMBCS - LMBCS);
  806. }
  807. else if (group == ULMBCS_GRP_CTRL) /* (Strategy 2B) */
  808. {
  809. /* Handle control characters here */
  810. if (uniChar <= ULMBCS_C0END)
  811. {
  812. *pLMBCS++ = ULMBCS_GRP_CTRL;
  813. *pLMBCS++ = (ulmbcs_byte_t)(ULMBCS_CTRLOFFSET + uniChar);
  814. }
  815. else if (uniChar >= ULMBCS_C1START && uniChar <= ULMBCS_C1START + ULMBCS_CTRLOFFSET)
  816. {
  817. *pLMBCS++ = ULMBCS_GRP_CTRL;
  818. *pLMBCS++ = (ulmbcs_byte_t ) (uniChar & 0x00FF);
  819. }
  820. bytes_written = (int32_t)(pLMBCS - LMBCS);
  821. }
  822. else if (group < ULMBCS_GRP_UNICODE) /* (Strategy 2C) */
  823. {
  824. /* a specific converter has been identified - use it */
  825. bytes_written = (int32_t)LMBCSConversionWorker (
  826. extraInfo, group, pLMBCS, &uniChar,
  827. &lastConverterIndex, groups_tried);
  828. }
  829. if (!bytes_written) /* the ambiguous group cases (Strategy 3) */
  830. {
  831. uprv_memset(groups_tried, 0, sizeof(groups_tried));
  832. /* check for non-default optimization group (Strategy 3A )*/
  833. if ((extraInfo->OptGroup != 1) && (ULMBCS_AMBIGUOUS_MATCH(group, extraInfo->OptGroup)))
  834. {
  835. /*zhujin: upgrade, merge #39299 here (Lotus) */
  836. /*To make R5 compatible translation, look for exceptional group first for non-DBCS*/
  837. if(extraInfo->localeConverterIndex < ULMBCS_DOUBLEOPTGROUP_START)
  838. {
  839. bytes_written = (int32_t)LMBCSConversionWorker (extraInfo,
  840. ULMBCS_GRP_L1, pLMBCS, &uniChar,
  841. &lastConverterIndex, groups_tried);
  842. if(!bytes_written)
  843. {
  844. bytes_written = (int32_t)LMBCSConversionWorker (extraInfo,
  845. ULMBCS_GRP_EXCEPT, pLMBCS, &uniChar,
  846. &lastConverterIndex, groups_tried);
  847. }
  848. if(!bytes_written)
  849. {
  850. bytes_written = (int32_t)LMBCSConversionWorker (extraInfo,
  851. extraInfo->localeConverterIndex, pLMBCS, &uniChar,
  852. &lastConverterIndex, groups_tried);
  853. }
  854. }
  855. else
  856. {
  857. bytes_written = (int32_t)LMBCSConversionWorker (extraInfo,
  858. extraInfo->localeConverterIndex, pLMBCS, &uniChar,
  859. &lastConverterIndex, groups_tried);
  860. }
  861. }
  862. /* check for locale optimization group (Strategy 3B) */
  863. if (!bytes_written && (extraInfo->localeConverterIndex) && (ULMBCS_AMBIGUOUS_MATCH(group, extraInfo->localeConverterIndex)))
  864. {
  865. bytes_written = (int32_t)LMBCSConversionWorker (extraInfo,
  866. extraInfo->localeConverterIndex, pLMBCS, &uniChar, &lastConverterIndex, groups_tried);
  867. }
  868. /* check for last optimization group used for this string (Strategy 3C) */
  869. if (!bytes_written && (lastConverterIndex) && (ULMBCS_AMBIGUOUS_MATCH(group, lastConverterIndex)))
  870. {
  871. bytes_written = (int32_t)LMBCSConversionWorker (extraInfo,
  872. lastConverterIndex, pLMBCS, &uniChar, &lastConverterIndex, groups_tried);
  873. }
  874. if (!bytes_written)
  875. {
  876. /* just check every possible matching converter (Strategy 3D) */
  877. ulmbcs_byte_t grp_start;
  878. ulmbcs_byte_t grp_end;
  879. ulmbcs_byte_t grp_ix;
  880. grp_start = (ulmbcs_byte_t)((group == ULMBCS_AMBIGUOUS_MBCS)
  881. ? ULMBCS_DOUBLEOPTGROUP_START
  882. : ULMBCS_GRP_L1);
  883. grp_end = (ulmbcs_byte_t)((group == ULMBCS_AMBIGUOUS_MBCS)
  884. ? ULMBCS_GRP_LAST
  885. : ULMBCS_GRP_TH);
  886. if(group == ULMBCS_AMBIGUOUS_ALL)
  887. {
  888. grp_start = ULMBCS_GRP_L1;
  889. grp_end = ULMBCS_GRP_LAST;
  890. }
  891. for (grp_ix = grp_start;
  892. grp_ix <= grp_end && !bytes_written;
  893. grp_ix++)
  894. {
  895. if (extraInfo->OptGrpConverter [grp_ix] && !groups_tried [grp_ix])
  896. {
  897. bytes_written = (int32_t)LMBCSConversionWorker (extraInfo,
  898. grp_ix, pLMBCS, &uniChar,
  899. &lastConverterIndex, groups_tried);
  900. }
  901. }
  902. /* a final conversion fallback to the exceptions group if its likely
  903. to be single byte (Strategy 3E) */
  904. if (!bytes_written && grp_start == ULMBCS_GRP_L1)
  905. {
  906. bytes_written = (int32_t)LMBCSConversionWorker (extraInfo,
  907. ULMBCS_GRP_EXCEPT, pLMBCS, &uniChar,
  908. &lastConverterIndex, groups_tried);
  909. }
  910. }
  911. /* all of our other strategies failed. Fallback to Unicode. (Strategy 4)*/
  912. if (!bytes_written)
  913. {
  914. pLMBCS += LMBCSConvertUni(pLMBCS, uniChar);
  915. bytes_written = (int32_t)(pLMBCS - LMBCS);
  916. }
  917. }
  918. }
  919. /* we have a translation. increment source and write as much as possible to target */
  920. args->source++;
  921. pLMBCS = LMBCS;
  922. while (args->target < args->targetLimit && bytes_written--)
  923. {
  924. *(args->target)++ = *pLMBCS++;
  925. if (args->offsets)
  926. {
  927. *(args->offsets)++ = sourceIndex;
  928. }
  929. }
  930. sourceIndex++;
  931. if (bytes_written > 0)
  932. {
  933. /* write any bytes that didn't fit in target to the error buffer,
  934. common code will move this to target if we get called back with
  935. enough target room
  936. */
  937. uint8_t * pErrorBuffer = args->converter->charErrorBuffer;
  938. *err = U_BUFFER_OVERFLOW_ERROR;
  939. args->converter->charErrorBufferLength = (int8_t)bytes_written;
  940. while (bytes_written--)
  941. {
  942. *pErrorBuffer++ = *pLMBCS++;
  943. }
  944. }
  945. /*Fix for SPR#DJOE66JFN3 (Lotus)*/
  946. extraInfo->localeConverterIndex = OldConverterIndex;
  947. }
  948. }
  949. /* Now, the Unicode from LMBCS section */
  950. /* A function to call when we are looking at the Unicode group byte in LMBCS */
  951. static char16_t
  952. GetUniFromLMBCSUni(char const ** ppLMBCSin) /* Called with LMBCS-style Unicode byte stream */
  953. {
  954. uint8_t HighCh = *(*ppLMBCSin)++; /* Big-endian Unicode in LMBCS compatibility group*/
  955. uint8_t LowCh = *(*ppLMBCSin)++;
  956. if (HighCh == ULMBCS_UNICOMPATZERO )
  957. {
  958. HighCh = LowCh;
  959. LowCh = 0; /* zero-byte in LSB special character */
  960. }
  961. return (char16_t)((HighCh << 8) | LowCh);
  962. }
  963. /* CHECK_SOURCE_LIMIT: Helper macro to verify that there are at least'index'
  964. bytes left in source up to sourceLimit.Errors appropriately if not.
  965. If we reach the limit, then update the source pointer to there to consume
  966. all input as required by ICU converter semantics.
  967. */
  968. #define CHECK_SOURCE_LIMIT(index) UPRV_BLOCK_MACRO_BEGIN { \
  969. if (args->source+index > args->sourceLimit) { \
  970. *err = U_TRUNCATED_CHAR_FOUND; \
  971. args->source = args->sourceLimit; \
  972. return 0xffff; \
  973. } \
  974. } UPRV_BLOCK_MACRO_END
  975. /* Return the Unicode representation for the current LMBCS character */
  976. static UChar32 U_CALLCONV
  977. _LMBCSGetNextUCharWorker(UConverterToUnicodeArgs* args,
  978. UErrorCode* err)
  979. {
  980. UChar32 uniChar = 0; /* an output UNICODE char */
  981. ulmbcs_byte_t CurByte; /* A byte from the input stream */
  982. /* error check */
  983. if (args->source >= args->sourceLimit)
  984. {
  985. *err = U_ILLEGAL_ARGUMENT_ERROR;
  986. return 0xffff;
  987. }
  988. /* Grab first byte & save address for error recovery */
  989. CurByte = *((ulmbcs_byte_t *) (args->source++));
  990. /*
  991. * at entry of each if clause:
  992. * 1. 'CurByte' points at the first byte of a LMBCS character
  993. * 2. '*source'points to the next byte of the source stream after 'CurByte'
  994. *
  995. * the job of each if clause is:
  996. * 1. set '*source' to point at the beginning of next char (nop if LMBCS char is only 1 byte)
  997. * 2. set 'uniChar' up with the right Unicode value, or set 'err' appropriately
  998. */
  999. /* First lets check the simple fixed values. */
  1000. if(((CurByte > ULMBCS_C0END) && (CurByte < ULMBCS_C1START)) /* ascii range */
  1001. || (CurByte == 0)
  1002. || CurByte == ULMBCS_HT || CurByte == ULMBCS_CR
  1003. || CurByte == ULMBCS_LF || CurByte == ULMBCS_123SYSTEMRANGE)
  1004. {
  1005. uniChar = CurByte;
  1006. }
  1007. else
  1008. {
  1009. UConverterDataLMBCS * extraInfo;
  1010. ulmbcs_byte_t group;
  1011. UConverterSharedData *cnv;
  1012. if (CurByte == ULMBCS_GRP_CTRL) /* Control character group - no opt group update */
  1013. {
  1014. ulmbcs_byte_t C0C1byte;
  1015. CHECK_SOURCE_LIMIT(1);
  1016. C0C1byte = *(args->source)++;
  1017. uniChar = (C0C1byte < ULMBCS_C1START) ? C0C1byte - ULMBCS_CTRLOFFSET : C0C1byte;
  1018. }
  1019. else
  1020. if (CurByte == ULMBCS_GRP_UNICODE) /* Unicode compatibility group: BigEndian UTF16 */
  1021. {
  1022. CHECK_SOURCE_LIMIT(2);
  1023. /* don't check for error indicators fffe/ffff below */
  1024. return GetUniFromLMBCSUni(&(args->source));
  1025. }
  1026. else if (CurByte <= ULMBCS_CTRLOFFSET)
  1027. {
  1028. group = CurByte; /* group byte is in the source */
  1029. extraInfo = (UConverterDataLMBCS *) args->converter->extraInfo;
  1030. if (group > ULMBCS_GRP_LAST || (cnv = extraInfo->OptGrpConverter[group]) == nullptr)
  1031. {
  1032. /* this is not a valid group byte - no converter*/
  1033. *err = U_INVALID_CHAR_FOUND;
  1034. }
  1035. else if (group >= ULMBCS_DOUBLEOPTGROUP_START) /* double byte conversion */
  1036. {
  1037. CHECK_SOURCE_LIMIT(2);
  1038. /* check for LMBCS doubled-group-byte case */
  1039. if (*args->source == group) {
  1040. /* single byte */
  1041. ++args->source;
  1042. uniChar = ucnv_MBCSSimpleGetNextUChar(cnv, args->source, 1, false);
  1043. ++args->source;
  1044. } else {
  1045. /* double byte */
  1046. uniChar = ucnv_MBCSSimpleGetNextUChar(cnv, args->source, 2, false);
  1047. args->source += 2;
  1048. }
  1049. }
  1050. else { /* single byte conversion */
  1051. CHECK_SOURCE_LIMIT(1);
  1052. CurByte = *(args->source)++;
  1053. if (CurByte >= ULMBCS_C1START)
  1054. {
  1055. uniChar = _MBCS_SINGLE_SIMPLE_GET_NEXT_BMP(cnv, CurByte);
  1056. }
  1057. else
  1058. {
  1059. /* The non-optimizable oddballs where there is an explicit byte
  1060. * AND the second byte is not in the upper ascii range
  1061. */
  1062. char bytes[2];
  1063. extraInfo = (UConverterDataLMBCS *) args->converter->extraInfo;
  1064. cnv = extraInfo->OptGrpConverter [ULMBCS_GRP_EXCEPT];
  1065. /* Lookup value must include opt group */
  1066. bytes[0] = group;
  1067. bytes[1] = CurByte;
  1068. uniChar = ucnv_MBCSSimpleGetNextUChar(cnv, bytes, 2, false);
  1069. }
  1070. }
  1071. }
  1072. else if (CurByte >= ULMBCS_C1START) /* group byte is implicit */
  1073. {
  1074. extraInfo = (UConverterDataLMBCS *) args->converter->extraInfo;
  1075. group = extraInfo->OptGroup;
  1076. cnv = extraInfo->OptGrpConverter[group];
  1077. if (group >= ULMBCS_DOUBLEOPTGROUP_START) /* double byte conversion */
  1078. {
  1079. if (!ucnv_MBCSIsLeadByte(cnv, CurByte))
  1080. {
  1081. CHECK_SOURCE_LIMIT(0);
  1082. /* let the MBCS conversion consume CurByte again */
  1083. uniChar = ucnv_MBCSSimpleGetNextUChar(cnv, args->source - 1, 1, false);
  1084. }
  1085. else
  1086. {
  1087. CHECK_SOURCE_LIMIT(1);
  1088. /* let the MBCS conversion consume CurByte again */
  1089. uniChar = ucnv_MBCSSimpleGetNextUChar(cnv, args->source - 1, 2, false);
  1090. ++args->source;
  1091. }
  1092. }
  1093. else /* single byte conversion */
  1094. {
  1095. uniChar = _MBCS_SINGLE_SIMPLE_GET_NEXT_BMP(cnv, CurByte);
  1096. }
  1097. }
  1098. }
  1099. return uniChar;
  1100. }
  1101. /* The exported function that converts lmbcs to one or more
  1102. UChars - currently UTF-16
  1103. */
  1104. static void U_CALLCONV
  1105. _LMBCSToUnicodeWithOffsets(UConverterToUnicodeArgs* args,
  1106. UErrorCode* err)
  1107. {
  1108. char LMBCS [ULMBCS_CHARSIZE_MAX];
  1109. char16_t uniChar; /* one output UNICODE char */
  1110. const char * saveSource; /* beginning of current code point */
  1111. const char * pStartLMBCS = args->source; /* beginning of whole string */
  1112. const char * errSource = nullptr; /* pointer to actual input in case an error occurs */
  1113. int8_t savebytes = 0;
  1114. /* Process from source to limit, or until error */
  1115. while (U_SUCCESS(*err) && args->sourceLimit > args->source && args->targetLimit > args->target)
  1116. {
  1117. saveSource = args->source; /* beginning of current code point */
  1118. if (args->converter->toULength) /* reassemble char from previous call */
  1119. {
  1120. const char *saveSourceLimit;
  1121. size_t size_old = args->converter->toULength;
  1122. /* limit from source is either remainder of temp buffer, or user limit on source */
  1123. size_t size_new_maybe_1 = sizeof(LMBCS) - size_old;
  1124. size_t size_new_maybe_2 = args->sourceLimit - args->source;
  1125. size_t size_new = (size_new_maybe_1 < size_new_maybe_2) ? size_new_maybe_1 : size_new_maybe_2;
  1126. uprv_memcpy(LMBCS, args->converter->toUBytes, size_old);
  1127. uprv_memcpy(LMBCS + size_old, args->source, size_new);
  1128. saveSourceLimit = args->sourceLimit;
  1129. args->source = errSource = LMBCS;
  1130. args->sourceLimit = LMBCS+size_old+size_new;
  1131. savebytes = (int8_t)(size_old+size_new);
  1132. uniChar = (char16_t) _LMBCSGetNextUCharWorker(args, err);
  1133. args->source = saveSource + ((args->source - LMBCS) - size_old);
  1134. args->sourceLimit = saveSourceLimit;
  1135. if (*err == U_TRUNCATED_CHAR_FOUND)
  1136. {
  1137. /* evil special case: source buffers so small a char spans more than 2 buffers */
  1138. args->converter->toULength = savebytes;
  1139. uprv_memcpy(args->converter->toUBytes, LMBCS, savebytes);
  1140. args->source = args->sourceLimit;
  1141. *err = U_ZERO_ERROR;
  1142. return;
  1143. }
  1144. else
  1145. {
  1146. /* clear the partial-char marker */
  1147. args->converter->toULength = 0;
  1148. }
  1149. }
  1150. else
  1151. {
  1152. errSource = saveSource;
  1153. uniChar = (char16_t) _LMBCSGetNextUCharWorker(args, err);
  1154. savebytes = (int8_t)(args->source - saveSource);
  1155. }
  1156. if (U_SUCCESS(*err))
  1157. {
  1158. if (uniChar < 0xfffe)
  1159. {
  1160. *(args->target)++ = uniChar;
  1161. if(args->offsets)
  1162. {
  1163. *(args->offsets)++ = (int32_t)(saveSource - pStartLMBCS);
  1164. }
  1165. }
  1166. else if (uniChar == 0xfffe)
  1167. {
  1168. *err = U_INVALID_CHAR_FOUND;
  1169. }
  1170. else /* if (uniChar == 0xffff) */
  1171. {
  1172. *err = U_ILLEGAL_CHAR_FOUND;
  1173. }
  1174. }
  1175. }
  1176. /* if target ran out before source, return U_BUFFER_OVERFLOW_ERROR */
  1177. if (U_SUCCESS(*err) && args->sourceLimit > args->source && args->targetLimit <= args->target)
  1178. {
  1179. *err = U_BUFFER_OVERFLOW_ERROR;
  1180. }
  1181. else if (U_FAILURE(*err))
  1182. {
  1183. /* If character incomplete or unmappable/illegal, store it in toUBytes[] */
  1184. args->converter->toULength = savebytes;
  1185. if (savebytes > 0) {
  1186. uprv_memcpy(args->converter->toUBytes, errSource, savebytes);
  1187. }
  1188. if (*err == U_TRUNCATED_CHAR_FOUND) {
  1189. *err = U_ZERO_ERROR;
  1190. }
  1191. }
  1192. }
  1193. /* And now, the macroized declarations of data & functions: */
  1194. DEFINE_LMBCS_OPEN(1)
  1195. DEFINE_LMBCS_OPEN(2)
  1196. DEFINE_LMBCS_OPEN(3)
  1197. DEFINE_LMBCS_OPEN(4)
  1198. DEFINE_LMBCS_OPEN(5)
  1199. DEFINE_LMBCS_OPEN(6)
  1200. DEFINE_LMBCS_OPEN(8)
  1201. DEFINE_LMBCS_OPEN(11)
  1202. DEFINE_LMBCS_OPEN(16)
  1203. DEFINE_LMBCS_OPEN(17)
  1204. DEFINE_LMBCS_OPEN(18)
  1205. DEFINE_LMBCS_OPEN(19)
  1206. DECLARE_LMBCS_DATA(1)
  1207. DECLARE_LMBCS_DATA(2)
  1208. DECLARE_LMBCS_DATA(3)
  1209. DECLARE_LMBCS_DATA(4)
  1210. DECLARE_LMBCS_DATA(5)
  1211. DECLARE_LMBCS_DATA(6)
  1212. DECLARE_LMBCS_DATA(8)
  1213. DECLARE_LMBCS_DATA(11)
  1214. DECLARE_LMBCS_DATA(16)
  1215. DECLARE_LMBCS_DATA(17)
  1216. DECLARE_LMBCS_DATA(18)
  1217. DECLARE_LMBCS_DATA(19)
  1218. U_CDECL_END
  1219. #endif /* #if !UCONFIG_NO_LEGACY_CONVERSION */