ucnv.cpp 94 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920
  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) 1998-2016, International Business Machines
  7. * Corporation and others. All Rights Reserved.
  8. *
  9. ******************************************************************************
  10. *
  11. * ucnv.c:
  12. * Implements APIs for the ICU's codeset conversion library;
  13. * mostly calls through internal functions;
  14. * created by Bertrand A. Damiba
  15. *
  16. * Modification History:
  17. *
  18. * Date Name Description
  19. * 04/04/99 helena Fixed internal header inclusion.
  20. * 05/09/00 helena Added implementation to handle fallback mappings.
  21. * 06/20/2000 helena OS/400 port changes; mostly typecast.
  22. */
  23. #include "unicode/utypes.h"
  24. #if !UCONFIG_NO_CONVERSION
  25. #include <memory>
  26. #include "unicode/ustring.h"
  27. #include "unicode/ucnv.h"
  28. #include "unicode/ucnv_err.h"
  29. #include "unicode/uset.h"
  30. #include "unicode/utf.h"
  31. #include "unicode/utf16.h"
  32. #include "putilimp.h"
  33. #include "cmemory.h"
  34. #include "cstring.h"
  35. #include "uassert.h"
  36. #include "utracimp.h"
  37. #include "ustr_imp.h"
  38. #include "ucnv_imp.h"
  39. #include "ucnv_cnv.h"
  40. #include "ucnv_bld.h"
  41. /* size of intermediate and preflighting buffers in ucnv_convert() */
  42. #define CHUNK_SIZE 1024
  43. typedef struct UAmbiguousConverter {
  44. const char *name;
  45. const char16_t variant5c;
  46. } UAmbiguousConverter;
  47. static const UAmbiguousConverter ambiguousConverters[]={
  48. { "ibm-897_P100-1995", 0xa5 },
  49. { "ibm-942_P120-1999", 0xa5 },
  50. { "ibm-943_P130-1999", 0xa5 },
  51. { "ibm-946_P100-1995", 0xa5 },
  52. { "ibm-33722_P120-1999", 0xa5 },
  53. { "ibm-1041_P100-1995", 0xa5 },
  54. /*{ "ibm-54191_P100-2006", 0xa5 },*/
  55. /*{ "ibm-62383_P100-2007", 0xa5 },*/
  56. /*{ "ibm-891_P100-1995", 0x20a9 },*/
  57. { "ibm-944_P100-1995", 0x20a9 },
  58. { "ibm-949_P110-1999", 0x20a9 },
  59. { "ibm-1363_P110-1997", 0x20a9 },
  60. { "ISO_2022,locale=ko,version=0", 0x20a9 },
  61. { "ibm-1088_P100-1995", 0x20a9 }
  62. };
  63. /*Calls through createConverter */
  64. U_CAPI UConverter* U_EXPORT2
  65. ucnv_open (const char *name,
  66. UErrorCode * err)
  67. {
  68. UConverter *r;
  69. if (err == nullptr || U_FAILURE (*err)) {
  70. return nullptr;
  71. }
  72. r = ucnv_createConverter(nullptr, name, err);
  73. return r;
  74. }
  75. U_CAPI UConverter* U_EXPORT2
  76. ucnv_openPackage (const char *packageName, const char *converterName, UErrorCode * err)
  77. {
  78. return ucnv_createConverterFromPackage(packageName, converterName, err);
  79. }
  80. /*Extracts the char16_t* to a char* and calls through createConverter */
  81. U_CAPI UConverter* U_EXPORT2
  82. ucnv_openU (const char16_t * name,
  83. UErrorCode * err)
  84. {
  85. char asciiName[UCNV_MAX_CONVERTER_NAME_LENGTH];
  86. if (err == nullptr || U_FAILURE(*err))
  87. return nullptr;
  88. if (name == nullptr)
  89. return ucnv_open (nullptr, err);
  90. if (u_strlen(name) >= UCNV_MAX_CONVERTER_NAME_LENGTH)
  91. {
  92. *err = U_ILLEGAL_ARGUMENT_ERROR;
  93. return nullptr;
  94. }
  95. return ucnv_open(u_austrcpy(asciiName, name), err);
  96. }
  97. /* Copy the string that is represented by the UConverterPlatform enum
  98. * @param platformString An output buffer
  99. * @param platform An enum representing a platform
  100. * @return the length of the copied string.
  101. */
  102. static int32_t
  103. ucnv_copyPlatformString(char *platformString, UConverterPlatform pltfrm)
  104. {
  105. switch (pltfrm)
  106. {
  107. case UCNV_IBM:
  108. uprv_strcpy(platformString, "ibm-");
  109. return 4;
  110. case UCNV_UNKNOWN:
  111. break;
  112. }
  113. /* default to empty string */
  114. *platformString = 0;
  115. return 0;
  116. }
  117. /*Assumes a $platform-#codepage.$CONVERTER_FILE_EXTENSION scheme and calls
  118. *through createConverter*/
  119. U_CAPI UConverter* U_EXPORT2
  120. ucnv_openCCSID (int32_t codepage,
  121. UConverterPlatform platform,
  122. UErrorCode * err)
  123. {
  124. char myName[UCNV_MAX_CONVERTER_NAME_LENGTH];
  125. int32_t myNameLen;
  126. if (err == nullptr || U_FAILURE (*err))
  127. return nullptr;
  128. /* ucnv_copyPlatformString could return "ibm-" or "cp" */
  129. myNameLen = ucnv_copyPlatformString(myName, platform);
  130. T_CString_integerToString(myName + myNameLen, codepage, 10);
  131. return ucnv_createConverter(nullptr, myName, err);
  132. }
  133. /* Creating a temporary stack-based object that can be used in one thread,
  134. and created from a converter that is shared across threads.
  135. */
  136. U_CAPI UConverter* U_EXPORT2
  137. ucnv_safeClone(const UConverter* cnv, void *stackBuffer, int32_t *pBufferSize, UErrorCode *status)
  138. {
  139. UConverter *localConverter, *allocatedConverter;
  140. int32_t stackBufferSize;
  141. int32_t bufferSizeNeeded;
  142. UErrorCode cbErr;
  143. UConverterToUnicodeArgs toUArgs = {
  144. sizeof(UConverterToUnicodeArgs),
  145. true,
  146. nullptr,
  147. nullptr,
  148. nullptr,
  149. nullptr,
  150. nullptr,
  151. nullptr
  152. };
  153. UConverterFromUnicodeArgs fromUArgs = {
  154. sizeof(UConverterFromUnicodeArgs),
  155. true,
  156. nullptr,
  157. nullptr,
  158. nullptr,
  159. nullptr,
  160. nullptr,
  161. nullptr
  162. };
  163. UTRACE_ENTRY_OC(UTRACE_UCNV_CLONE);
  164. if (status == nullptr || U_FAILURE(*status)){
  165. UTRACE_EXIT_STATUS(status? *status: U_ILLEGAL_ARGUMENT_ERROR);
  166. return nullptr;
  167. }
  168. if (cnv == nullptr) {
  169. *status = U_ILLEGAL_ARGUMENT_ERROR;
  170. UTRACE_EXIT_STATUS(*status);
  171. return nullptr;
  172. }
  173. UTRACE_DATA3(UTRACE_OPEN_CLOSE, "clone converter %s at %p into stackBuffer %p",
  174. ucnv_getName(cnv, status), cnv, stackBuffer);
  175. if (cnv->sharedData->impl->safeClone != nullptr) {
  176. /* call the custom safeClone function for sizing */
  177. bufferSizeNeeded = 0;
  178. cnv->sharedData->impl->safeClone(cnv, nullptr, &bufferSizeNeeded, status);
  179. if (U_FAILURE(*status)) {
  180. UTRACE_EXIT_STATUS(*status);
  181. return nullptr;
  182. }
  183. }
  184. else
  185. {
  186. /* inherent sizing */
  187. bufferSizeNeeded = sizeof(UConverter);
  188. }
  189. if (pBufferSize == nullptr) {
  190. stackBufferSize = 1;
  191. pBufferSize = &stackBufferSize;
  192. } else {
  193. stackBufferSize = *pBufferSize;
  194. if (stackBufferSize <= 0){ /* 'preflighting' request - set needed size into *pBufferSize */
  195. *pBufferSize = bufferSizeNeeded;
  196. UTRACE_EXIT_VALUE(bufferSizeNeeded);
  197. return nullptr;
  198. }
  199. }
  200. /* Adjust (if necessary) the stackBuffer pointer to be aligned correctly for a UConverter.
  201. * TODO(Jira ICU-20736) Redo this using std::align() once g++4.9 compatibility is no longer needed.
  202. */
  203. if (stackBuffer) {
  204. uintptr_t p = reinterpret_cast<uintptr_t>(stackBuffer);
  205. uintptr_t aligned_p = (p + alignof(UConverter) - 1) & ~(alignof(UConverter) - 1);
  206. ptrdiff_t pointerAdjustment = aligned_p - p;
  207. if (bufferSizeNeeded + pointerAdjustment <= stackBufferSize) {
  208. stackBuffer = reinterpret_cast<void *>(aligned_p);
  209. stackBufferSize -= static_cast<int32_t>(pointerAdjustment);
  210. } else {
  211. /* prevent using the stack buffer but keep the size > 0 so that we do not just preflight */
  212. stackBufferSize = 1;
  213. }
  214. }
  215. /* Now, see if we must allocate any memory */
  216. if (stackBufferSize < bufferSizeNeeded || stackBuffer == nullptr)
  217. {
  218. /* allocate one here...*/
  219. localConverter = allocatedConverter = (UConverter *) uprv_malloc (bufferSizeNeeded);
  220. if(localConverter == nullptr) {
  221. *status = U_MEMORY_ALLOCATION_ERROR;
  222. UTRACE_EXIT_STATUS(*status);
  223. return nullptr;
  224. }
  225. // If pBufferSize was nullptr as the input, pBufferSize is set to &stackBufferSize in this function.
  226. if (pBufferSize != &stackBufferSize) {
  227. *status = U_SAFECLONE_ALLOCATED_WARNING;
  228. }
  229. /* record the fact that memory was allocated */
  230. *pBufferSize = bufferSizeNeeded;
  231. } else {
  232. /* just use the stack buffer */
  233. localConverter = (UConverter*) stackBuffer;
  234. allocatedConverter = nullptr;
  235. }
  236. uprv_memset(localConverter, 0, bufferSizeNeeded);
  237. /* Copy initial state */
  238. uprv_memcpy(localConverter, cnv, sizeof(UConverter));
  239. localConverter->isCopyLocal = localConverter->isExtraLocal = false;
  240. /* copy the substitution string */
  241. if (cnv->subChars == (uint8_t *)cnv->subUChars) {
  242. localConverter->subChars = (uint8_t *)localConverter->subUChars;
  243. } else {
  244. localConverter->subChars = (uint8_t *)uprv_malloc(UCNV_ERROR_BUFFER_LENGTH * U_SIZEOF_UCHAR);
  245. if (localConverter->subChars == nullptr) {
  246. uprv_free(allocatedConverter);
  247. UTRACE_EXIT_STATUS(*status);
  248. return nullptr;
  249. }
  250. uprv_memcpy(localConverter->subChars, cnv->subChars, UCNV_ERROR_BUFFER_LENGTH * U_SIZEOF_UCHAR);
  251. }
  252. /* now either call the safeclone fcn or not */
  253. if (cnv->sharedData->impl->safeClone != nullptr) {
  254. /* call the custom safeClone function */
  255. localConverter = cnv->sharedData->impl->safeClone(cnv, localConverter, pBufferSize, status);
  256. }
  257. if(localConverter==nullptr || U_FAILURE(*status)) {
  258. if (allocatedConverter != nullptr && allocatedConverter->subChars != (uint8_t *)allocatedConverter->subUChars) {
  259. uprv_free(allocatedConverter->subChars);
  260. }
  261. uprv_free(allocatedConverter);
  262. UTRACE_EXIT_STATUS(*status);
  263. return nullptr;
  264. }
  265. /* increment refcount of shared data if needed */
  266. if (cnv->sharedData->isReferenceCounted) {
  267. ucnv_incrementRefCount(cnv->sharedData);
  268. }
  269. if(localConverter == (UConverter*)stackBuffer) {
  270. /* we're using user provided data - set to not destroy */
  271. localConverter->isCopyLocal = true;
  272. }
  273. /* allow callback functions to handle any memory allocation */
  274. toUArgs.converter = fromUArgs.converter = localConverter;
  275. cbErr = U_ZERO_ERROR;
  276. cnv->fromCharErrorBehaviour(cnv->toUContext, &toUArgs, nullptr, 0, UCNV_CLONE, &cbErr);
  277. cbErr = U_ZERO_ERROR;
  278. cnv->fromUCharErrorBehaviour(cnv->fromUContext, &fromUArgs, nullptr, 0, 0, UCNV_CLONE, &cbErr);
  279. UTRACE_EXIT_PTR_STATUS(localConverter, *status);
  280. return localConverter;
  281. }
  282. U_CAPI UConverter* U_EXPORT2
  283. ucnv_clone(const UConverter* cnv, UErrorCode *status)
  284. {
  285. return ucnv_safeClone(cnv, nullptr, nullptr, status);
  286. }
  287. /*Decreases the reference counter in the shared immutable section of the object
  288. *and frees the mutable part*/
  289. U_CAPI void U_EXPORT2
  290. ucnv_close (UConverter * converter)
  291. {
  292. UErrorCode errorCode = U_ZERO_ERROR;
  293. UTRACE_ENTRY_OC(UTRACE_UCNV_CLOSE);
  294. if (converter == nullptr)
  295. {
  296. UTRACE_EXIT();
  297. return;
  298. }
  299. UTRACE_DATA3(UTRACE_OPEN_CLOSE, "close converter %s at %p, isCopyLocal=%b",
  300. ucnv_getName(converter, &errorCode), converter, converter->isCopyLocal);
  301. /* In order to speed up the close, only call the callbacks when they have been changed.
  302. This performance check will only work when the callbacks are set within a shared library
  303. or from user code that statically links this code. */
  304. /* first, notify the callback functions that the converter is closed */
  305. if (converter->fromCharErrorBehaviour != UCNV_TO_U_DEFAULT_CALLBACK) {
  306. UConverterToUnicodeArgs toUArgs = {
  307. sizeof(UConverterToUnicodeArgs),
  308. true,
  309. nullptr,
  310. nullptr,
  311. nullptr,
  312. nullptr,
  313. nullptr,
  314. nullptr
  315. };
  316. toUArgs.converter = converter;
  317. errorCode = U_ZERO_ERROR;
  318. converter->fromCharErrorBehaviour(converter->toUContext, &toUArgs, nullptr, 0, UCNV_CLOSE, &errorCode);
  319. }
  320. if (converter->fromUCharErrorBehaviour != UCNV_FROM_U_DEFAULT_CALLBACK) {
  321. UConverterFromUnicodeArgs fromUArgs = {
  322. sizeof(UConverterFromUnicodeArgs),
  323. true,
  324. nullptr,
  325. nullptr,
  326. nullptr,
  327. nullptr,
  328. nullptr,
  329. nullptr
  330. };
  331. fromUArgs.converter = converter;
  332. errorCode = U_ZERO_ERROR;
  333. converter->fromUCharErrorBehaviour(converter->fromUContext, &fromUArgs, nullptr, 0, 0, UCNV_CLOSE, &errorCode);
  334. }
  335. if (converter->sharedData->impl->close != nullptr) {
  336. converter->sharedData->impl->close(converter);
  337. }
  338. if (converter->subChars != (uint8_t *)converter->subUChars) {
  339. uprv_free(converter->subChars);
  340. }
  341. if (converter->sharedData->isReferenceCounted) {
  342. ucnv_unloadSharedDataIfReady(converter->sharedData);
  343. }
  344. if(!converter->isCopyLocal){
  345. uprv_free(converter);
  346. }
  347. UTRACE_EXIT();
  348. }
  349. /*returns a single Name from the list, will return nullptr if out of bounds
  350. */
  351. U_CAPI const char* U_EXPORT2
  352. ucnv_getAvailableName (int32_t n)
  353. {
  354. if (0 <= n && n <= 0xffff) {
  355. UErrorCode err = U_ZERO_ERROR;
  356. const char *name = ucnv_bld_getAvailableConverter((uint16_t)n, &err);
  357. if (U_SUCCESS(err)) {
  358. return name;
  359. }
  360. }
  361. return nullptr;
  362. }
  363. U_CAPI int32_t U_EXPORT2
  364. ucnv_countAvailable ()
  365. {
  366. UErrorCode err = U_ZERO_ERROR;
  367. return ucnv_bld_countAvailableConverters(&err);
  368. }
  369. U_CAPI void U_EXPORT2
  370. ucnv_getSubstChars (const UConverter * converter,
  371. char *mySubChar,
  372. int8_t * len,
  373. UErrorCode * err)
  374. {
  375. if (U_FAILURE (*err))
  376. return;
  377. if (converter->subCharLen <= 0) {
  378. /* Unicode string or empty string from ucnv_setSubstString(). */
  379. *len = 0;
  380. return;
  381. }
  382. if (*len < converter->subCharLen) /*not enough space in subChars */
  383. {
  384. *err = U_INDEX_OUTOFBOUNDS_ERROR;
  385. return;
  386. }
  387. uprv_memcpy (mySubChar, converter->subChars, converter->subCharLen); /*fills in the subchars */
  388. *len = converter->subCharLen; /*store # of bytes copied to buffer */
  389. }
  390. U_CAPI void U_EXPORT2
  391. ucnv_setSubstChars (UConverter * converter,
  392. const char *mySubChar,
  393. int8_t len,
  394. UErrorCode * err)
  395. {
  396. if (U_FAILURE (*err))
  397. return;
  398. /*Makes sure that the subChar is within the codepages char length boundaries */
  399. if ((len > converter->sharedData->staticData->maxBytesPerChar)
  400. || (len < converter->sharedData->staticData->minBytesPerChar))
  401. {
  402. *err = U_ILLEGAL_ARGUMENT_ERROR;
  403. return;
  404. }
  405. uprv_memcpy (converter->subChars, mySubChar, len); /*copies the subchars */
  406. converter->subCharLen = len; /*sets the new len */
  407. /*
  408. * There is currently (2001Feb) no separate API to set/get subChar1.
  409. * In order to always have subChar written after it is explicitly set,
  410. * we set subChar1 to 0.
  411. */
  412. converter->subChar1 = 0;
  413. }
  414. U_CAPI void U_EXPORT2
  415. ucnv_setSubstString(UConverter *cnv,
  416. const char16_t *s,
  417. int32_t length,
  418. UErrorCode *err) {
  419. alignas(UConverter) char cloneBuffer[U_CNV_SAFECLONE_BUFFERSIZE];
  420. char chars[UCNV_ERROR_BUFFER_LENGTH];
  421. UConverter *clone;
  422. uint8_t *subChars;
  423. int32_t cloneSize, length8;
  424. /* Let the following functions check all arguments. */
  425. cloneSize = sizeof(cloneBuffer);
  426. clone = ucnv_safeClone(cnv, cloneBuffer, &cloneSize, err);
  427. ucnv_setFromUCallBack(clone, UCNV_FROM_U_CALLBACK_STOP, nullptr, nullptr, nullptr, err);
  428. length8 = ucnv_fromUChars(clone, chars, (int32_t)sizeof(chars), s, length, err);
  429. ucnv_close(clone);
  430. if (U_FAILURE(*err)) {
  431. return;
  432. }
  433. if (cnv->sharedData->impl->writeSub == nullptr
  434. #if !UCONFIG_NO_LEGACY_CONVERSION
  435. || (cnv->sharedData->staticData->conversionType == UCNV_MBCS &&
  436. ucnv_MBCSGetType(cnv) != UCNV_EBCDIC_STATEFUL)
  437. #endif
  438. ) {
  439. /* The converter is not stateful. Store the charset bytes as a fixed string. */
  440. subChars = (uint8_t *)chars;
  441. } else {
  442. /*
  443. * The converter has a non-default writeSub() function, indicating
  444. * that it is stateful.
  445. * Store the Unicode string for on-the-fly conversion for correct
  446. * state handling.
  447. */
  448. if (length > UCNV_ERROR_BUFFER_LENGTH) {
  449. /*
  450. * Should not occur. The converter should output at least one byte
  451. * per char16_t, which means that ucnv_fromUChars() should catch all
  452. * overflows.
  453. */
  454. *err = U_BUFFER_OVERFLOW_ERROR;
  455. return;
  456. }
  457. subChars = (uint8_t *)s;
  458. if (length < 0) {
  459. length = u_strlen(s);
  460. }
  461. length8 = length * U_SIZEOF_UCHAR;
  462. }
  463. /*
  464. * For storing the substitution string, select either the small buffer inside
  465. * UConverter or allocate a subChars buffer.
  466. */
  467. if (length8 > UCNV_MAX_SUBCHAR_LEN) {
  468. /* Use a separate buffer for the string. Outside UConverter to not make it too large. */
  469. if (cnv->subChars == (uint8_t *)cnv->subUChars) {
  470. /* Allocate a new buffer for the string. */
  471. cnv->subChars = (uint8_t *)uprv_malloc(UCNV_ERROR_BUFFER_LENGTH * U_SIZEOF_UCHAR);
  472. if (cnv->subChars == nullptr) {
  473. cnv->subChars = (uint8_t *)cnv->subUChars;
  474. *err = U_MEMORY_ALLOCATION_ERROR;
  475. return;
  476. }
  477. uprv_memset(cnv->subChars, 0, UCNV_ERROR_BUFFER_LENGTH * U_SIZEOF_UCHAR);
  478. }
  479. }
  480. /* Copy the substitution string into the UConverter or its subChars buffer. */
  481. if (length8 == 0) {
  482. cnv->subCharLen = 0;
  483. } else {
  484. uprv_memcpy(cnv->subChars, subChars, length8);
  485. if (subChars == (uint8_t *)chars) {
  486. cnv->subCharLen = (int8_t)length8;
  487. } else /* subChars == s */ {
  488. cnv->subCharLen = (int8_t)-length;
  489. }
  490. }
  491. /* See comment in ucnv_setSubstChars(). */
  492. cnv->subChar1 = 0;
  493. }
  494. /*resets the internal states of a converter
  495. *goal : have the same behaviour than a freshly created converter
  496. */
  497. static void _reset(UConverter *converter, UConverterResetChoice choice,
  498. UBool callCallback) {
  499. if(converter == nullptr) {
  500. return;
  501. }
  502. if(callCallback) {
  503. /* first, notify the callback functions that the converter is reset */
  504. UErrorCode errorCode;
  505. if(choice<=UCNV_RESET_TO_UNICODE && converter->fromCharErrorBehaviour != UCNV_TO_U_DEFAULT_CALLBACK) {
  506. UConverterToUnicodeArgs toUArgs = {
  507. sizeof(UConverterToUnicodeArgs),
  508. true,
  509. nullptr,
  510. nullptr,
  511. nullptr,
  512. nullptr,
  513. nullptr,
  514. nullptr
  515. };
  516. toUArgs.converter = converter;
  517. errorCode = U_ZERO_ERROR;
  518. converter->fromCharErrorBehaviour(converter->toUContext, &toUArgs, nullptr, 0, UCNV_RESET, &errorCode);
  519. }
  520. if(choice!=UCNV_RESET_TO_UNICODE && converter->fromUCharErrorBehaviour != UCNV_FROM_U_DEFAULT_CALLBACK) {
  521. UConverterFromUnicodeArgs fromUArgs = {
  522. sizeof(UConverterFromUnicodeArgs),
  523. true,
  524. nullptr,
  525. nullptr,
  526. nullptr,
  527. nullptr,
  528. nullptr,
  529. nullptr
  530. };
  531. fromUArgs.converter = converter;
  532. errorCode = U_ZERO_ERROR;
  533. converter->fromUCharErrorBehaviour(converter->fromUContext, &fromUArgs, nullptr, 0, 0, UCNV_RESET, &errorCode);
  534. }
  535. }
  536. /* now reset the converter itself */
  537. if(choice<=UCNV_RESET_TO_UNICODE) {
  538. converter->toUnicodeStatus = converter->sharedData->toUnicodeStatus;
  539. converter->mode = 0;
  540. converter->toULength = 0;
  541. converter->invalidCharLength = converter->UCharErrorBufferLength = 0;
  542. converter->preToULength = 0;
  543. }
  544. if(choice!=UCNV_RESET_TO_UNICODE) {
  545. converter->fromUnicodeStatus = 0;
  546. converter->fromUChar32 = 0;
  547. converter->invalidUCharLength = converter->charErrorBufferLength = 0;
  548. converter->preFromUFirstCP = U_SENTINEL;
  549. converter->preFromULength = 0;
  550. }
  551. if (converter->sharedData->impl->reset != nullptr) {
  552. /* call the custom reset function */
  553. converter->sharedData->impl->reset(converter, choice);
  554. }
  555. }
  556. U_CAPI void U_EXPORT2
  557. ucnv_reset(UConverter *converter)
  558. {
  559. _reset(converter, UCNV_RESET_BOTH, true);
  560. }
  561. U_CAPI void U_EXPORT2
  562. ucnv_resetToUnicode(UConverter *converter)
  563. {
  564. _reset(converter, UCNV_RESET_TO_UNICODE, true);
  565. }
  566. U_CAPI void U_EXPORT2
  567. ucnv_resetFromUnicode(UConverter *converter)
  568. {
  569. _reset(converter, UCNV_RESET_FROM_UNICODE, true);
  570. }
  571. U_CAPI int8_t U_EXPORT2
  572. ucnv_getMaxCharSize (const UConverter * converter)
  573. {
  574. return converter->maxBytesPerUChar;
  575. }
  576. U_CAPI int8_t U_EXPORT2
  577. ucnv_getMinCharSize (const UConverter * converter)
  578. {
  579. return converter->sharedData->staticData->minBytesPerChar;
  580. }
  581. U_CAPI const char* U_EXPORT2
  582. ucnv_getName (const UConverter * converter, UErrorCode * err)
  583. {
  584. if (U_FAILURE (*err))
  585. return nullptr;
  586. if(converter->sharedData->impl->getName){
  587. const char* temp= converter->sharedData->impl->getName(converter);
  588. if(temp)
  589. return temp;
  590. }
  591. return converter->sharedData->staticData->name;
  592. }
  593. U_CAPI int32_t U_EXPORT2
  594. ucnv_getCCSID(const UConverter * converter,
  595. UErrorCode * err)
  596. {
  597. int32_t ccsid;
  598. if (U_FAILURE (*err))
  599. return -1;
  600. ccsid = converter->sharedData->staticData->codepage;
  601. if (ccsid == 0) {
  602. /* Rare case. This is for cases like gb18030,
  603. which doesn't have an IBM canonical name, but does have an IBM alias. */
  604. const char *standardName = ucnv_getStandardName(ucnv_getName(converter, err), "IBM", err);
  605. if (U_SUCCESS(*err) && standardName) {
  606. const char *ccsidStr = uprv_strchr(standardName, '-');
  607. if (ccsidStr) {
  608. ccsid = (int32_t)atol(ccsidStr+1); /* +1 to skip '-' */
  609. }
  610. }
  611. }
  612. return ccsid;
  613. }
  614. U_CAPI UConverterPlatform U_EXPORT2
  615. ucnv_getPlatform (const UConverter * converter,
  616. UErrorCode * err)
  617. {
  618. if (U_FAILURE (*err))
  619. return UCNV_UNKNOWN;
  620. return (UConverterPlatform)converter->sharedData->staticData->platform;
  621. }
  622. U_CAPI void U_EXPORT2
  623. ucnv_getToUCallBack (const UConverter * converter,
  624. UConverterToUCallback *action,
  625. const void **context)
  626. {
  627. *action = converter->fromCharErrorBehaviour;
  628. *context = converter->toUContext;
  629. }
  630. U_CAPI void U_EXPORT2
  631. ucnv_getFromUCallBack (const UConverter * converter,
  632. UConverterFromUCallback *action,
  633. const void **context)
  634. {
  635. *action = converter->fromUCharErrorBehaviour;
  636. *context = converter->fromUContext;
  637. }
  638. U_CAPI void U_EXPORT2
  639. ucnv_setToUCallBack (UConverter * converter,
  640. UConverterToUCallback newAction,
  641. const void* newContext,
  642. UConverterToUCallback *oldAction,
  643. const void** oldContext,
  644. UErrorCode * err)
  645. {
  646. if (U_FAILURE (*err))
  647. return;
  648. if (oldAction) *oldAction = converter->fromCharErrorBehaviour;
  649. converter->fromCharErrorBehaviour = newAction;
  650. if (oldContext) *oldContext = converter->toUContext;
  651. converter->toUContext = newContext;
  652. }
  653. U_CAPI void U_EXPORT2
  654. ucnv_setFromUCallBack (UConverter * converter,
  655. UConverterFromUCallback newAction,
  656. const void* newContext,
  657. UConverterFromUCallback *oldAction,
  658. const void** oldContext,
  659. UErrorCode * err)
  660. {
  661. if (U_FAILURE (*err))
  662. return;
  663. if (oldAction) *oldAction = converter->fromUCharErrorBehaviour;
  664. converter->fromUCharErrorBehaviour = newAction;
  665. if (oldContext) *oldContext = converter->fromUContext;
  666. converter->fromUContext = newContext;
  667. }
  668. static void
  669. _updateOffsets(int32_t *offsets, int32_t length,
  670. int32_t sourceIndex, int32_t errorInputLength) {
  671. int32_t *limit;
  672. int32_t delta, offset;
  673. if(sourceIndex>=0) {
  674. /*
  675. * adjust each offset by adding the previous sourceIndex
  676. * minus the length of the input sequence that caused an
  677. * error, if any
  678. */
  679. delta=sourceIndex-errorInputLength;
  680. } else {
  681. /*
  682. * set each offset to -1 because this conversion function
  683. * does not handle offsets
  684. */
  685. delta=-1;
  686. }
  687. limit=offsets+length;
  688. if(delta==0) {
  689. /* most common case, nothing to do */
  690. } else if(delta>0) {
  691. /* add the delta to each offset (but not if the offset is <0) */
  692. while(offsets<limit) {
  693. offset=*offsets;
  694. if(offset>=0) {
  695. *offsets=offset+delta;
  696. }
  697. ++offsets;
  698. }
  699. } else /* delta<0 */ {
  700. /*
  701. * set each offset to -1 because this conversion function
  702. * does not handle offsets
  703. * or the error input sequence started in a previous buffer
  704. */
  705. while(offsets<limit) {
  706. *offsets++=-1;
  707. }
  708. }
  709. }
  710. /* ucnv_fromUnicode --------------------------------------------------------- */
  711. /*
  712. * Implementation note for m:n conversions
  713. *
  714. * While collecting source units to find the longest match for m:n conversion,
  715. * some source units may need to be stored for a partial match.
  716. * When a second buffer does not yield a match on all of the previously stored
  717. * source units, then they must be "replayed", i.e., fed back into the converter.
  718. *
  719. * The code relies on the fact that replaying will not nest -
  720. * converting a replay buffer will not result in a replay.
  721. * This is because a replay is necessary only after the _continuation_ of a
  722. * partial match failed, but a replay buffer is converted as a whole.
  723. * It may result in some of its units being stored again for a partial match,
  724. * but there will not be a continuation _during_ the replay which could fail.
  725. *
  726. * It is conceivable that a callback function could call the converter
  727. * recursively in a way that causes another replay to be stored, but that
  728. * would be an error in the callback function.
  729. * Such violations will cause assertion failures in a debug build,
  730. * and wrong output, but they will not cause a crash.
  731. */
  732. static void
  733. _fromUnicodeWithCallback(UConverterFromUnicodeArgs *pArgs, UErrorCode *err) {
  734. UConverterFromUnicode fromUnicode;
  735. UConverter *cnv;
  736. const char16_t *s;
  737. char *t;
  738. int32_t *offsets;
  739. int32_t sourceIndex;
  740. int32_t errorInputLength;
  741. UBool converterSawEndOfInput, calledCallback;
  742. /* variables for m:n conversion */
  743. char16_t replay[UCNV_EXT_MAX_UCHARS];
  744. const char16_t *realSource, *realSourceLimit;
  745. int32_t realSourceIndex;
  746. UBool realFlush;
  747. cnv=pArgs->converter;
  748. s=pArgs->source;
  749. t=pArgs->target;
  750. offsets=pArgs->offsets;
  751. /* get the converter implementation function */
  752. sourceIndex=0;
  753. if(offsets==nullptr) {
  754. fromUnicode=cnv->sharedData->impl->fromUnicode;
  755. } else {
  756. fromUnicode=cnv->sharedData->impl->fromUnicodeWithOffsets;
  757. if(fromUnicode==nullptr) {
  758. /* there is no WithOffsets implementation */
  759. fromUnicode=cnv->sharedData->impl->fromUnicode;
  760. /* we will write -1 for each offset */
  761. sourceIndex=-1;
  762. }
  763. }
  764. if(cnv->preFromULength>=0) {
  765. /* normal mode */
  766. realSource=nullptr;
  767. /* avoid compiler warnings - not otherwise necessary, and the values do not matter */
  768. realSourceLimit=nullptr;
  769. realFlush=false;
  770. realSourceIndex=0;
  771. } else {
  772. /*
  773. * Previous m:n conversion stored source units from a partial match
  774. * and failed to consume all of them.
  775. * We need to "replay" them from a temporary buffer and convert them first.
  776. */
  777. realSource=pArgs->source;
  778. realSourceLimit=pArgs->sourceLimit;
  779. realFlush=pArgs->flush;
  780. realSourceIndex=sourceIndex;
  781. uprv_memcpy(replay, cnv->preFromU, -cnv->preFromULength*U_SIZEOF_UCHAR);
  782. pArgs->source=replay;
  783. pArgs->sourceLimit=replay-cnv->preFromULength;
  784. pArgs->flush=false;
  785. sourceIndex=-1;
  786. cnv->preFromULength=0;
  787. }
  788. /*
  789. * loop for conversion and error handling
  790. *
  791. * loop {
  792. * convert
  793. * loop {
  794. * update offsets
  795. * handle end of input
  796. * handle errors/call callback
  797. * }
  798. * }
  799. */
  800. for(;;) {
  801. if(U_SUCCESS(*err)) {
  802. /* convert */
  803. fromUnicode(pArgs, err);
  804. /*
  805. * set a flag for whether the converter
  806. * successfully processed the end of the input
  807. *
  808. * need not check cnv->preFromULength==0 because a replay (<0) will cause
  809. * s<sourceLimit before converterSawEndOfInput is checked
  810. */
  811. converterSawEndOfInput=
  812. static_cast<UBool>(U_SUCCESS(*err) &&
  813. pArgs->flush && pArgs->source==pArgs->sourceLimit &&
  814. cnv->fromUChar32==0);
  815. } else {
  816. /* handle error from ucnv_convertEx() */
  817. converterSawEndOfInput=false;
  818. }
  819. /* no callback called yet for this iteration */
  820. calledCallback=false;
  821. /* no sourceIndex adjustment for conversion, only for callback output */
  822. errorInputLength=0;
  823. /*
  824. * loop for offsets and error handling
  825. *
  826. * iterates at most 3 times:
  827. * 1. to clean up after the conversion function
  828. * 2. after the callback
  829. * 3. after the callback again if there was truncated input
  830. */
  831. for(;;) {
  832. /* update offsets if we write any */
  833. if(offsets!=nullptr) {
  834. int32_t length = static_cast<int32_t>(pArgs->target - t);
  835. if(length>0) {
  836. _updateOffsets(offsets, length, sourceIndex, errorInputLength);
  837. /*
  838. * if a converter handles offsets and updates the offsets
  839. * pointer at the end, then pArgs->offset should not change
  840. * here;
  841. * however, some converters do not handle offsets at all
  842. * (sourceIndex<0) or may not update the offsets pointer
  843. */
  844. pArgs->offsets=offsets+=length;
  845. }
  846. if(sourceIndex>=0) {
  847. sourceIndex += static_cast<int32_t>(pArgs->source - s);
  848. }
  849. }
  850. if(cnv->preFromULength<0) {
  851. /*
  852. * switch the source to new replay units (cannot occur while replaying)
  853. * after offset handling and before end-of-input and callback handling
  854. */
  855. if(realSource==nullptr) {
  856. realSource=pArgs->source;
  857. realSourceLimit=pArgs->sourceLimit;
  858. realFlush=pArgs->flush;
  859. realSourceIndex=sourceIndex;
  860. uprv_memcpy(replay, cnv->preFromU, -cnv->preFromULength*U_SIZEOF_UCHAR);
  861. pArgs->source=replay;
  862. pArgs->sourceLimit=replay-cnv->preFromULength;
  863. pArgs->flush=false;
  864. if((sourceIndex+=cnv->preFromULength)<0) {
  865. sourceIndex=-1;
  866. }
  867. cnv->preFromULength=0;
  868. } else {
  869. /* see implementation note before _fromUnicodeWithCallback() */
  870. U_ASSERT(realSource==nullptr);
  871. *err=U_INTERNAL_PROGRAM_ERROR;
  872. }
  873. }
  874. /* update pointers */
  875. s=pArgs->source;
  876. t=pArgs->target;
  877. if(U_SUCCESS(*err)) {
  878. if(s<pArgs->sourceLimit) {
  879. /*
  880. * continue with the conversion loop while there is still input left
  881. * (continue converting by breaking out of only the inner loop)
  882. */
  883. break;
  884. } else if(realSource!=nullptr) {
  885. /* switch back from replaying to the real source and continue */
  886. pArgs->source=realSource;
  887. pArgs->sourceLimit=realSourceLimit;
  888. pArgs->flush=realFlush;
  889. sourceIndex=realSourceIndex;
  890. realSource=nullptr;
  891. break;
  892. } else if(pArgs->flush && cnv->fromUChar32!=0) {
  893. /*
  894. * the entire input stream is consumed
  895. * and there is a partial, truncated input sequence left
  896. */
  897. /* inject an error and continue with callback handling */
  898. *err=U_TRUNCATED_CHAR_FOUND;
  899. calledCallback=false; /* new error condition */
  900. } else {
  901. /* input consumed */
  902. if(pArgs->flush) {
  903. /*
  904. * return to the conversion loop once more if the flush
  905. * flag is set and the conversion function has not
  906. * successfully processed the end of the input yet
  907. *
  908. * (continue converting by breaking out of only the inner loop)
  909. */
  910. if(!converterSawEndOfInput) {
  911. break;
  912. }
  913. /* reset the converter without calling the callback function */
  914. _reset(cnv, UCNV_RESET_FROM_UNICODE, false);
  915. }
  916. /* done successfully */
  917. return;
  918. }
  919. }
  920. /* U_FAILURE(*err) */
  921. {
  922. UErrorCode e;
  923. if( calledCallback ||
  924. (e=*err)==U_BUFFER_OVERFLOW_ERROR ||
  925. (e!=U_INVALID_CHAR_FOUND &&
  926. e!=U_ILLEGAL_CHAR_FOUND &&
  927. e!=U_TRUNCATED_CHAR_FOUND)
  928. ) {
  929. /*
  930. * the callback did not or cannot resolve the error:
  931. * set output pointers and return
  932. *
  933. * the check for buffer overflow is redundant but it is
  934. * a high-runner case and hopefully documents the intent
  935. * well
  936. *
  937. * if we were replaying, then the replay buffer must be
  938. * copied back into the UConverter
  939. * and the real arguments must be restored
  940. */
  941. if(realSource!=nullptr) {
  942. int32_t length;
  943. U_ASSERT(cnv->preFromULength==0);
  944. length = static_cast<int32_t>(pArgs->sourceLimit - pArgs->source);
  945. if(length>0) {
  946. u_memcpy(cnv->preFromU, pArgs->source, length);
  947. cnv->preFromULength = static_cast<int8_t>(-length);
  948. }
  949. pArgs->source=realSource;
  950. pArgs->sourceLimit=realSourceLimit;
  951. pArgs->flush=realFlush;
  952. }
  953. return;
  954. }
  955. }
  956. /* callback handling */
  957. {
  958. UChar32 codePoint;
  959. /* get and write the code point */
  960. codePoint=cnv->fromUChar32;
  961. errorInputLength=0;
  962. U16_APPEND_UNSAFE(cnv->invalidUCharBuffer, errorInputLength, codePoint);
  963. cnv->invalidUCharLength = static_cast<int8_t>(errorInputLength);
  964. /* set the converter state to deal with the next character */
  965. cnv->fromUChar32=0;
  966. /* call the callback function */
  967. cnv->fromUCharErrorBehaviour(cnv->fromUContext, pArgs,
  968. cnv->invalidUCharBuffer, errorInputLength, codePoint,
  969. *err==U_INVALID_CHAR_FOUND ? UCNV_UNASSIGNED : UCNV_ILLEGAL,
  970. err);
  971. }
  972. /*
  973. * loop back to the offset handling
  974. *
  975. * this flag will indicate after offset handling
  976. * that a callback was called;
  977. * if the callback did not resolve the error, then we return
  978. */
  979. calledCallback=true;
  980. }
  981. }
  982. }
  983. /*
  984. * Output the fromUnicode overflow buffer.
  985. * Call this function if(cnv->charErrorBufferLength>0).
  986. * @return true if overflow
  987. */
  988. static UBool
  989. ucnv_outputOverflowFromUnicode(UConverter *cnv,
  990. char **target, const char *targetLimit,
  991. int32_t **pOffsets,
  992. UErrorCode *err) {
  993. int32_t *offsets;
  994. char *overflow, *t;
  995. int32_t i, length;
  996. t=*target;
  997. if(pOffsets!=nullptr) {
  998. offsets=*pOffsets;
  999. } else {
  1000. offsets=nullptr;
  1001. }
  1002. overflow = reinterpret_cast<char*>(cnv->charErrorBuffer);
  1003. length=cnv->charErrorBufferLength;
  1004. i=0;
  1005. while(i<length) {
  1006. if(t==targetLimit) {
  1007. /* the overflow buffer contains too much, keep the rest */
  1008. int32_t j=0;
  1009. do {
  1010. overflow[j++]=overflow[i++];
  1011. } while(i<length);
  1012. cnv->charErrorBufferLength = static_cast<int8_t>(j);
  1013. *target=t;
  1014. if(offsets!=nullptr) {
  1015. *pOffsets=offsets;
  1016. }
  1017. *err=U_BUFFER_OVERFLOW_ERROR;
  1018. return true;
  1019. }
  1020. /* copy the overflow contents to the target */
  1021. *t++=overflow[i++];
  1022. if(offsets!=nullptr) {
  1023. *offsets++=-1; /* no source index available for old output */
  1024. }
  1025. }
  1026. /* the overflow buffer is completely copied to the target */
  1027. cnv->charErrorBufferLength=0;
  1028. *target=t;
  1029. if(offsets!=nullptr) {
  1030. *pOffsets=offsets;
  1031. }
  1032. return false;
  1033. }
  1034. U_CAPI void U_EXPORT2
  1035. ucnv_fromUnicode(UConverter *cnv,
  1036. char **target, const char *targetLimit,
  1037. const char16_t **source, const char16_t *sourceLimit,
  1038. int32_t *offsets,
  1039. UBool flush,
  1040. UErrorCode *err) {
  1041. UConverterFromUnicodeArgs args;
  1042. const char16_t *s;
  1043. char *t;
  1044. /* check parameters */
  1045. if(err==nullptr || U_FAILURE(*err)) {
  1046. return;
  1047. }
  1048. if(cnv==nullptr || target==nullptr || source==nullptr) {
  1049. *err=U_ILLEGAL_ARGUMENT_ERROR;
  1050. return;
  1051. }
  1052. s=*source;
  1053. t=*target;
  1054. if ((const void *)U_MAX_PTR(sourceLimit) == (const void *)sourceLimit) {
  1055. /*
  1056. Prevent code from going into an infinite loop in case we do hit this
  1057. limit. The limit pointer is expected to be on a char16_t * boundary.
  1058. This also prevents the next argument check from failing.
  1059. */
  1060. sourceLimit = (const char16_t *)(((const char *)sourceLimit) - 1);
  1061. }
  1062. /*
  1063. * All these conditions should never happen.
  1064. *
  1065. * 1) Make sure that the limits are >= to the address source or target
  1066. *
  1067. * 2) Make sure that the buffer sizes do not exceed the number range for
  1068. * int32_t because some functions use the size (in units or bytes)
  1069. * rather than comparing pointers, and because offsets are int32_t values.
  1070. *
  1071. * size_t is guaranteed to be unsigned and large enough for the job.
  1072. *
  1073. * Return with an error instead of adjusting the limits because we would
  1074. * not be able to maintain the semantics that either the source must be
  1075. * consumed or the target filled (unless an error occurs).
  1076. * An adjustment would be targetLimit=t+0x7fffffff; for example.
  1077. *
  1078. * 3) Make sure that the user didn't incorrectly cast a char16_t * pointer
  1079. * to a char * pointer and provide an incomplete char16_t code unit.
  1080. */
  1081. if (sourceLimit<s || targetLimit<t ||
  1082. ((size_t)(sourceLimit-s)>(size_t)0x3fffffff && sourceLimit>s) ||
  1083. ((size_t)(targetLimit-t)>(size_t)0x7fffffff && targetLimit>t) ||
  1084. (((const char *)sourceLimit-(const char *)s) & 1) != 0)
  1085. {
  1086. *err=U_ILLEGAL_ARGUMENT_ERROR;
  1087. return;
  1088. }
  1089. /* output the target overflow buffer */
  1090. if( cnv->charErrorBufferLength>0 &&
  1091. ucnv_outputOverflowFromUnicode(cnv, target, targetLimit, &offsets, err)
  1092. ) {
  1093. /* U_BUFFER_OVERFLOW_ERROR */
  1094. return;
  1095. }
  1096. /* *target may have moved, therefore stop using t */
  1097. if(!flush && s==sourceLimit && cnv->preFromULength>=0) {
  1098. /* the overflow buffer is emptied and there is no new input: we are done */
  1099. return;
  1100. }
  1101. /*
  1102. * Do not simply return with a buffer overflow error if
  1103. * !flush && t==targetLimit
  1104. * because it is possible that the source will not generate any output.
  1105. * For example, the skip callback may be called;
  1106. * it does not output anything.
  1107. */
  1108. /* prepare the converter arguments */
  1109. args.converter=cnv;
  1110. args.flush=flush;
  1111. args.offsets=offsets;
  1112. args.source=s;
  1113. args.sourceLimit=sourceLimit;
  1114. args.target=*target;
  1115. args.targetLimit=targetLimit;
  1116. args.size=sizeof(args);
  1117. _fromUnicodeWithCallback(&args, err);
  1118. *source=args.source;
  1119. *target=args.target;
  1120. }
  1121. /* ucnv_toUnicode() --------------------------------------------------------- */
  1122. static void
  1123. _toUnicodeWithCallback(UConverterToUnicodeArgs *pArgs, UErrorCode *err) {
  1124. UConverterToUnicode toUnicode;
  1125. UConverter *cnv;
  1126. const char *s;
  1127. char16_t *t;
  1128. int32_t *offsets;
  1129. int32_t sourceIndex;
  1130. int32_t errorInputLength;
  1131. UBool converterSawEndOfInput, calledCallback;
  1132. /* variables for m:n conversion */
  1133. char replay[UCNV_EXT_MAX_BYTES];
  1134. const char *realSource, *realSourceLimit;
  1135. int32_t realSourceIndex;
  1136. UBool realFlush;
  1137. cnv=pArgs->converter;
  1138. s=pArgs->source;
  1139. t=pArgs->target;
  1140. offsets=pArgs->offsets;
  1141. /* get the converter implementation function */
  1142. sourceIndex=0;
  1143. if(offsets==nullptr) {
  1144. toUnicode=cnv->sharedData->impl->toUnicode;
  1145. } else {
  1146. toUnicode=cnv->sharedData->impl->toUnicodeWithOffsets;
  1147. if(toUnicode==nullptr) {
  1148. /* there is no WithOffsets implementation */
  1149. toUnicode=cnv->sharedData->impl->toUnicode;
  1150. /* we will write -1 for each offset */
  1151. sourceIndex=-1;
  1152. }
  1153. }
  1154. if(cnv->preToULength>=0) {
  1155. /* normal mode */
  1156. realSource=nullptr;
  1157. /* avoid compiler warnings - not otherwise necessary, and the values do not matter */
  1158. realSourceLimit=nullptr;
  1159. realFlush=false;
  1160. realSourceIndex=0;
  1161. } else {
  1162. /*
  1163. * Previous m:n conversion stored source units from a partial match
  1164. * and failed to consume all of them.
  1165. * We need to "replay" them from a temporary buffer and convert them first.
  1166. */
  1167. realSource=pArgs->source;
  1168. realSourceLimit=pArgs->sourceLimit;
  1169. realFlush=pArgs->flush;
  1170. realSourceIndex=sourceIndex;
  1171. uprv_memcpy(replay, cnv->preToU, -cnv->preToULength);
  1172. pArgs->source=replay;
  1173. pArgs->sourceLimit=replay-cnv->preToULength;
  1174. pArgs->flush=false;
  1175. sourceIndex=-1;
  1176. cnv->preToULength=0;
  1177. }
  1178. /*
  1179. * loop for conversion and error handling
  1180. *
  1181. * loop {
  1182. * convert
  1183. * loop {
  1184. * update offsets
  1185. * handle end of input
  1186. * handle errors/call callback
  1187. * }
  1188. * }
  1189. */
  1190. for(;;) {
  1191. if(U_SUCCESS(*err)) {
  1192. /* convert */
  1193. toUnicode(pArgs, err);
  1194. /*
  1195. * set a flag for whether the converter
  1196. * successfully processed the end of the input
  1197. *
  1198. * need not check cnv->preToULength==0 because a replay (<0) will cause
  1199. * s<sourceLimit before converterSawEndOfInput is checked
  1200. */
  1201. converterSawEndOfInput=
  1202. static_cast<UBool>(U_SUCCESS(*err) &&
  1203. pArgs->flush && pArgs->source==pArgs->sourceLimit &&
  1204. cnv->toULength==0);
  1205. } else {
  1206. /* handle error from getNextUChar() or ucnv_convertEx() */
  1207. converterSawEndOfInput=false;
  1208. }
  1209. /* no callback called yet for this iteration */
  1210. calledCallback=false;
  1211. /* no sourceIndex adjustment for conversion, only for callback output */
  1212. errorInputLength=0;
  1213. /*
  1214. * loop for offsets and error handling
  1215. *
  1216. * iterates at most 3 times:
  1217. * 1. to clean up after the conversion function
  1218. * 2. after the callback
  1219. * 3. after the callback again if there was truncated input
  1220. */
  1221. for(;;) {
  1222. /* update offsets if we write any */
  1223. if(offsets!=nullptr) {
  1224. int32_t length = static_cast<int32_t>(pArgs->target - t);
  1225. if(length>0) {
  1226. _updateOffsets(offsets, length, sourceIndex, errorInputLength);
  1227. /*
  1228. * if a converter handles offsets and updates the offsets
  1229. * pointer at the end, then pArgs->offset should not change
  1230. * here;
  1231. * however, some converters do not handle offsets at all
  1232. * (sourceIndex<0) or may not update the offsets pointer
  1233. */
  1234. pArgs->offsets=offsets+=length;
  1235. }
  1236. if(sourceIndex>=0) {
  1237. sourceIndex += static_cast<int32_t>(pArgs->source - s);
  1238. }
  1239. }
  1240. if(cnv->preToULength<0) {
  1241. /*
  1242. * switch the source to new replay units (cannot occur while replaying)
  1243. * after offset handling and before end-of-input and callback handling
  1244. */
  1245. if(realSource==nullptr) {
  1246. realSource=pArgs->source;
  1247. realSourceLimit=pArgs->sourceLimit;
  1248. realFlush=pArgs->flush;
  1249. realSourceIndex=sourceIndex;
  1250. uprv_memcpy(replay, cnv->preToU, -cnv->preToULength);
  1251. pArgs->source=replay;
  1252. pArgs->sourceLimit=replay-cnv->preToULength;
  1253. pArgs->flush=false;
  1254. if((sourceIndex+=cnv->preToULength)<0) {
  1255. sourceIndex=-1;
  1256. }
  1257. cnv->preToULength=0;
  1258. } else {
  1259. /* see implementation note before _fromUnicodeWithCallback() */
  1260. U_ASSERT(realSource==nullptr);
  1261. *err=U_INTERNAL_PROGRAM_ERROR;
  1262. }
  1263. }
  1264. /* update pointers */
  1265. s=pArgs->source;
  1266. t=pArgs->target;
  1267. if(U_SUCCESS(*err)) {
  1268. if(s<pArgs->sourceLimit) {
  1269. /*
  1270. * continue with the conversion loop while there is still input left
  1271. * (continue converting by breaking out of only the inner loop)
  1272. */
  1273. break;
  1274. } else if(realSource!=nullptr) {
  1275. /* switch back from replaying to the real source and continue */
  1276. pArgs->source=realSource;
  1277. pArgs->sourceLimit=realSourceLimit;
  1278. pArgs->flush=realFlush;
  1279. sourceIndex=realSourceIndex;
  1280. realSource=nullptr;
  1281. break;
  1282. } else if(pArgs->flush && cnv->toULength>0) {
  1283. /*
  1284. * the entire input stream is consumed
  1285. * and there is a partial, truncated input sequence left
  1286. */
  1287. /* inject an error and continue with callback handling */
  1288. *err=U_TRUNCATED_CHAR_FOUND;
  1289. calledCallback=false; /* new error condition */
  1290. } else {
  1291. /* input consumed */
  1292. if(pArgs->flush) {
  1293. /*
  1294. * return to the conversion loop once more if the flush
  1295. * flag is set and the conversion function has not
  1296. * successfully processed the end of the input yet
  1297. *
  1298. * (continue converting by breaking out of only the inner loop)
  1299. */
  1300. if(!converterSawEndOfInput) {
  1301. break;
  1302. }
  1303. /* reset the converter without calling the callback function */
  1304. _reset(cnv, UCNV_RESET_TO_UNICODE, false);
  1305. }
  1306. /* done successfully */
  1307. return;
  1308. }
  1309. }
  1310. /* U_FAILURE(*err) */
  1311. {
  1312. UErrorCode e;
  1313. if( calledCallback ||
  1314. (e=*err)==U_BUFFER_OVERFLOW_ERROR ||
  1315. (e!=U_INVALID_CHAR_FOUND &&
  1316. e!=U_ILLEGAL_CHAR_FOUND &&
  1317. e!=U_TRUNCATED_CHAR_FOUND &&
  1318. e!=U_ILLEGAL_ESCAPE_SEQUENCE &&
  1319. e!=U_UNSUPPORTED_ESCAPE_SEQUENCE)
  1320. ) {
  1321. /*
  1322. * the callback did not or cannot resolve the error:
  1323. * set output pointers and return
  1324. *
  1325. * the check for buffer overflow is redundant but it is
  1326. * a high-runner case and hopefully documents the intent
  1327. * well
  1328. *
  1329. * if we were replaying, then the replay buffer must be
  1330. * copied back into the UConverter
  1331. * and the real arguments must be restored
  1332. */
  1333. if(realSource!=nullptr) {
  1334. int32_t length;
  1335. U_ASSERT(cnv->preToULength==0);
  1336. length = static_cast<int32_t>(pArgs->sourceLimit - pArgs->source);
  1337. if(length>0) {
  1338. uprv_memcpy(cnv->preToU, pArgs->source, length);
  1339. cnv->preToULength = static_cast<int8_t>(-length);
  1340. }
  1341. pArgs->source=realSource;
  1342. pArgs->sourceLimit=realSourceLimit;
  1343. pArgs->flush=realFlush;
  1344. }
  1345. return;
  1346. }
  1347. }
  1348. /* copy toUBytes[] to invalidCharBuffer[] */
  1349. errorInputLength=cnv->invalidCharLength=cnv->toULength;
  1350. if(errorInputLength>0) {
  1351. uprv_memcpy(cnv->invalidCharBuffer, cnv->toUBytes, errorInputLength);
  1352. }
  1353. /* set the converter state to deal with the next character */
  1354. cnv->toULength=0;
  1355. /* call the callback function */
  1356. if(cnv->toUCallbackReason==UCNV_ILLEGAL && *err==U_INVALID_CHAR_FOUND) {
  1357. cnv->toUCallbackReason = UCNV_UNASSIGNED;
  1358. }
  1359. cnv->fromCharErrorBehaviour(cnv->toUContext, pArgs,
  1360. cnv->invalidCharBuffer, errorInputLength,
  1361. cnv->toUCallbackReason,
  1362. err);
  1363. cnv->toUCallbackReason = UCNV_ILLEGAL; /* reset to default value */
  1364. /*
  1365. * loop back to the offset handling
  1366. *
  1367. * this flag will indicate after offset handling
  1368. * that a callback was called;
  1369. * if the callback did not resolve the error, then we return
  1370. */
  1371. calledCallback=true;
  1372. }
  1373. }
  1374. }
  1375. /*
  1376. * Output the toUnicode overflow buffer.
  1377. * Call this function if(cnv->UCharErrorBufferLength>0).
  1378. * @return true if overflow
  1379. */
  1380. static UBool
  1381. ucnv_outputOverflowToUnicode(UConverter *cnv,
  1382. char16_t **target, const char16_t *targetLimit,
  1383. int32_t **pOffsets,
  1384. UErrorCode *err) {
  1385. int32_t *offsets;
  1386. char16_t *overflow, *t;
  1387. int32_t i, length;
  1388. t=*target;
  1389. if(pOffsets!=nullptr) {
  1390. offsets=*pOffsets;
  1391. } else {
  1392. offsets=nullptr;
  1393. }
  1394. overflow=cnv->UCharErrorBuffer;
  1395. length=cnv->UCharErrorBufferLength;
  1396. i=0;
  1397. while(i<length) {
  1398. if(t==targetLimit) {
  1399. /* the overflow buffer contains too much, keep the rest */
  1400. int32_t j=0;
  1401. do {
  1402. overflow[j++]=overflow[i++];
  1403. } while(i<length);
  1404. cnv->UCharErrorBufferLength = static_cast<int8_t>(j);
  1405. *target=t;
  1406. if(offsets!=nullptr) {
  1407. *pOffsets=offsets;
  1408. }
  1409. *err=U_BUFFER_OVERFLOW_ERROR;
  1410. return true;
  1411. }
  1412. /* copy the overflow contents to the target */
  1413. *t++=overflow[i++];
  1414. if(offsets!=nullptr) {
  1415. *offsets++=-1; /* no source index available for old output */
  1416. }
  1417. }
  1418. /* the overflow buffer is completely copied to the target */
  1419. cnv->UCharErrorBufferLength=0;
  1420. *target=t;
  1421. if(offsets!=nullptr) {
  1422. *pOffsets=offsets;
  1423. }
  1424. return false;
  1425. }
  1426. U_CAPI void U_EXPORT2
  1427. ucnv_toUnicode(UConverter *cnv,
  1428. char16_t **target, const char16_t *targetLimit,
  1429. const char **source, const char *sourceLimit,
  1430. int32_t *offsets,
  1431. UBool flush,
  1432. UErrorCode *err) {
  1433. UConverterToUnicodeArgs args;
  1434. const char *s;
  1435. char16_t *t;
  1436. /* check parameters */
  1437. if(err==nullptr || U_FAILURE(*err)) {
  1438. return;
  1439. }
  1440. if(cnv==nullptr || target==nullptr || source==nullptr) {
  1441. *err=U_ILLEGAL_ARGUMENT_ERROR;
  1442. return;
  1443. }
  1444. s=*source;
  1445. t=*target;
  1446. if ((const void *)U_MAX_PTR(targetLimit) == (const void *)targetLimit) {
  1447. /*
  1448. Prevent code from going into an infinite loop in case we do hit this
  1449. limit. The limit pointer is expected to be on a char16_t * boundary.
  1450. This also prevents the next argument check from failing.
  1451. */
  1452. targetLimit = (const char16_t *)(((const char *)targetLimit) - 1);
  1453. }
  1454. /*
  1455. * All these conditions should never happen.
  1456. *
  1457. * 1) Make sure that the limits are >= to the address source or target
  1458. *
  1459. * 2) Make sure that the buffer sizes do not exceed the number range for
  1460. * int32_t because some functions use the size (in units or bytes)
  1461. * rather than comparing pointers, and because offsets are int32_t values.
  1462. *
  1463. * size_t is guaranteed to be unsigned and large enough for the job.
  1464. *
  1465. * Return with an error instead of adjusting the limits because we would
  1466. * not be able to maintain the semantics that either the source must be
  1467. * consumed or the target filled (unless an error occurs).
  1468. * An adjustment would be sourceLimit=t+0x7fffffff; for example.
  1469. *
  1470. * 3) Make sure that the user didn't incorrectly cast a char16_t * pointer
  1471. * to a char * pointer and provide an incomplete char16_t code unit.
  1472. */
  1473. if (sourceLimit<s || targetLimit<t ||
  1474. ((size_t)(sourceLimit-s)>(size_t)0x7fffffff && sourceLimit>s) ||
  1475. ((size_t)(targetLimit-t)>(size_t)0x3fffffff && targetLimit>t) ||
  1476. (((const char *)targetLimit-(const char *)t) & 1) != 0
  1477. ) {
  1478. *err=U_ILLEGAL_ARGUMENT_ERROR;
  1479. return;
  1480. }
  1481. /* output the target overflow buffer */
  1482. if( cnv->UCharErrorBufferLength>0 &&
  1483. ucnv_outputOverflowToUnicode(cnv, target, targetLimit, &offsets, err)
  1484. ) {
  1485. /* U_BUFFER_OVERFLOW_ERROR */
  1486. return;
  1487. }
  1488. /* *target may have moved, therefore stop using t */
  1489. if(!flush && s==sourceLimit && cnv->preToULength>=0) {
  1490. /* the overflow buffer is emptied and there is no new input: we are done */
  1491. return;
  1492. }
  1493. /*
  1494. * Do not simply return with a buffer overflow error if
  1495. * !flush && t==targetLimit
  1496. * because it is possible that the source will not generate any output.
  1497. * For example, the skip callback may be called;
  1498. * it does not output anything.
  1499. */
  1500. /* prepare the converter arguments */
  1501. args.converter=cnv;
  1502. args.flush=flush;
  1503. args.offsets=offsets;
  1504. args.source=s;
  1505. args.sourceLimit=sourceLimit;
  1506. args.target=*target;
  1507. args.targetLimit=targetLimit;
  1508. args.size=sizeof(args);
  1509. _toUnicodeWithCallback(&args, err);
  1510. *source=args.source;
  1511. *target=args.target;
  1512. }
  1513. /* ucnv_to/fromUChars() ----------------------------------------------------- */
  1514. U_CAPI int32_t U_EXPORT2
  1515. ucnv_fromUChars(UConverter *cnv,
  1516. char *dest, int32_t destCapacity,
  1517. const char16_t *src, int32_t srcLength,
  1518. UErrorCode *pErrorCode) {
  1519. const char16_t *srcLimit;
  1520. char *originalDest, *destLimit;
  1521. int32_t destLength;
  1522. /* check arguments */
  1523. if(pErrorCode==nullptr || U_FAILURE(*pErrorCode)) {
  1524. return 0;
  1525. }
  1526. if( cnv==nullptr ||
  1527. destCapacity<0 || (destCapacity>0 && dest==nullptr) ||
  1528. srcLength<-1 || (srcLength!=0 && src==nullptr)
  1529. ) {
  1530. *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
  1531. return 0;
  1532. }
  1533. /* initialize */
  1534. ucnv_resetFromUnicode(cnv);
  1535. originalDest=dest;
  1536. if(srcLength==-1) {
  1537. srcLength=u_strlen(src);
  1538. }
  1539. if(srcLength>0) {
  1540. srcLimit=src+srcLength;
  1541. destCapacity=pinCapacity(dest, destCapacity);
  1542. destLimit=dest+destCapacity;
  1543. /* perform the conversion */
  1544. ucnv_fromUnicode(cnv, &dest, destLimit, &src, srcLimit, nullptr, true, pErrorCode);
  1545. destLength=(int32_t)(dest-originalDest);
  1546. /* if an overflow occurs, then get the preflighting length */
  1547. if(*pErrorCode==U_BUFFER_OVERFLOW_ERROR) {
  1548. char buffer[1024];
  1549. destLimit=buffer+sizeof(buffer);
  1550. do {
  1551. dest=buffer;
  1552. *pErrorCode=U_ZERO_ERROR;
  1553. ucnv_fromUnicode(cnv, &dest, destLimit, &src, srcLimit, nullptr, true, pErrorCode);
  1554. destLength+=(int32_t)(dest-buffer);
  1555. } while(*pErrorCode==U_BUFFER_OVERFLOW_ERROR);
  1556. }
  1557. } else {
  1558. destLength=0;
  1559. }
  1560. return u_terminateChars(originalDest, destCapacity, destLength, pErrorCode);
  1561. }
  1562. U_CAPI int32_t U_EXPORT2
  1563. ucnv_toUChars(UConverter *cnv,
  1564. char16_t *dest, int32_t destCapacity,
  1565. const char *src, int32_t srcLength,
  1566. UErrorCode *pErrorCode) {
  1567. const char *srcLimit;
  1568. char16_t *originalDest, *destLimit;
  1569. int32_t destLength;
  1570. /* check arguments */
  1571. if(pErrorCode==nullptr || U_FAILURE(*pErrorCode)) {
  1572. return 0;
  1573. }
  1574. if( cnv==nullptr ||
  1575. destCapacity<0 || (destCapacity>0 && dest==nullptr) ||
  1576. srcLength<-1 || (srcLength!=0 && src==nullptr))
  1577. {
  1578. *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
  1579. return 0;
  1580. }
  1581. /* initialize */
  1582. ucnv_resetToUnicode(cnv);
  1583. originalDest=dest;
  1584. if(srcLength==-1) {
  1585. srcLength=(int32_t)uprv_strlen(src);
  1586. }
  1587. if(srcLength>0) {
  1588. srcLimit=src+srcLength;
  1589. destCapacity=pinCapacity(dest, destCapacity);
  1590. destLimit=dest+destCapacity;
  1591. /* perform the conversion */
  1592. ucnv_toUnicode(cnv, &dest, destLimit, &src, srcLimit, nullptr, true, pErrorCode);
  1593. destLength=(int32_t)(dest-originalDest);
  1594. /* if an overflow occurs, then get the preflighting length */
  1595. if(*pErrorCode==U_BUFFER_OVERFLOW_ERROR)
  1596. {
  1597. char16_t buffer[1024];
  1598. destLimit=buffer+UPRV_LENGTHOF(buffer);
  1599. do {
  1600. dest=buffer;
  1601. *pErrorCode=U_ZERO_ERROR;
  1602. ucnv_toUnicode(cnv, &dest, destLimit, &src, srcLimit, nullptr, true, pErrorCode);
  1603. destLength+=(int32_t)(dest-buffer);
  1604. }
  1605. while(*pErrorCode==U_BUFFER_OVERFLOW_ERROR);
  1606. }
  1607. } else {
  1608. destLength=0;
  1609. }
  1610. return u_terminateUChars(originalDest, destCapacity, destLength, pErrorCode);
  1611. }
  1612. /* ucnv_getNextUChar() ------------------------------------------------------ */
  1613. U_CAPI UChar32 U_EXPORT2
  1614. ucnv_getNextUChar(UConverter *cnv,
  1615. const char **source, const char *sourceLimit,
  1616. UErrorCode *err) {
  1617. UConverterToUnicodeArgs args;
  1618. char16_t buffer[U16_MAX_LENGTH];
  1619. const char *s;
  1620. UChar32 c;
  1621. int32_t i, length;
  1622. /* check parameters */
  1623. if(err==nullptr || U_FAILURE(*err)) {
  1624. return 0xffff;
  1625. }
  1626. if(cnv==nullptr || source==nullptr) {
  1627. *err=U_ILLEGAL_ARGUMENT_ERROR;
  1628. return 0xffff;
  1629. }
  1630. s=*source;
  1631. if(sourceLimit<s) {
  1632. *err=U_ILLEGAL_ARGUMENT_ERROR;
  1633. return 0xffff;
  1634. }
  1635. /*
  1636. * Make sure that the buffer sizes do not exceed the number range for
  1637. * int32_t because some functions use the size (in units or bytes)
  1638. * rather than comparing pointers, and because offsets are int32_t values.
  1639. *
  1640. * size_t is guaranteed to be unsigned and large enough for the job.
  1641. *
  1642. * Return with an error instead of adjusting the limits because we would
  1643. * not be able to maintain the semantics that either the source must be
  1644. * consumed or the target filled (unless an error occurs).
  1645. * An adjustment would be sourceLimit=t+0x7fffffff; for example.
  1646. */
  1647. if(((size_t)(sourceLimit-s)>(size_t)0x7fffffff && sourceLimit>s)) {
  1648. *err=U_ILLEGAL_ARGUMENT_ERROR;
  1649. return 0xffff;
  1650. }
  1651. c=U_SENTINEL;
  1652. /* flush the target overflow buffer */
  1653. if(cnv->UCharErrorBufferLength>0) {
  1654. char16_t *overflow;
  1655. overflow=cnv->UCharErrorBuffer;
  1656. i=0;
  1657. length=cnv->UCharErrorBufferLength;
  1658. U16_NEXT(overflow, i, length, c);
  1659. /* move the remaining overflow contents up to the beginning */
  1660. if((cnv->UCharErrorBufferLength=(int8_t)(length-i))>0) {
  1661. uprv_memmove(cnv->UCharErrorBuffer, cnv->UCharErrorBuffer+i,
  1662. cnv->UCharErrorBufferLength*U_SIZEOF_UCHAR);
  1663. }
  1664. if(!U16_IS_LEAD(c) || i<length) {
  1665. return c;
  1666. }
  1667. /*
  1668. * Continue if the overflow buffer contained only a lead surrogate,
  1669. * in case the converter outputs single surrogates from complete
  1670. * input sequences.
  1671. */
  1672. }
  1673. /*
  1674. * flush==true is implied for ucnv_getNextUChar()
  1675. *
  1676. * do not simply return even if s==sourceLimit because the converter may
  1677. * not have seen flush==true before
  1678. */
  1679. /* prepare the converter arguments */
  1680. args.converter=cnv;
  1681. args.flush=true;
  1682. args.offsets=nullptr;
  1683. args.source=s;
  1684. args.sourceLimit=sourceLimit;
  1685. args.target=buffer;
  1686. args.targetLimit=buffer+1;
  1687. args.size=sizeof(args);
  1688. if(c<0) {
  1689. /*
  1690. * call the native getNextUChar() implementation if we are
  1691. * at a character boundary (toULength==0)
  1692. *
  1693. * unlike with _toUnicode(), getNextUChar() implementations must set
  1694. * U_TRUNCATED_CHAR_FOUND for truncated input,
  1695. * in addition to setting toULength/toUBytes[]
  1696. */
  1697. if(cnv->toULength==0 && cnv->sharedData->impl->getNextUChar!=nullptr) {
  1698. c=cnv->sharedData->impl->getNextUChar(&args, err);
  1699. *source=s=args.source;
  1700. if(*err==U_INDEX_OUTOFBOUNDS_ERROR) {
  1701. /* reset the converter without calling the callback function */
  1702. _reset(cnv, UCNV_RESET_TO_UNICODE, false);
  1703. return 0xffff; /* no output */
  1704. } else if(U_SUCCESS(*err) && c>=0) {
  1705. return c;
  1706. /*
  1707. * else fall through to use _toUnicode() because
  1708. * UCNV_GET_NEXT_UCHAR_USE_TO_U: the native function did not want to handle it after all
  1709. * U_FAILURE: call _toUnicode() for callback handling (do not output c)
  1710. */
  1711. }
  1712. }
  1713. /* convert to one char16_t in buffer[0], or handle getNextUChar() errors */
  1714. _toUnicodeWithCallback(&args, err);
  1715. if(*err==U_BUFFER_OVERFLOW_ERROR) {
  1716. *err=U_ZERO_ERROR;
  1717. }
  1718. i=0;
  1719. length=(int32_t)(args.target-buffer);
  1720. } else {
  1721. /* write the lead surrogate from the overflow buffer */
  1722. buffer[0]=(char16_t)c;
  1723. args.target=buffer+1;
  1724. i=0;
  1725. length=1;
  1726. }
  1727. /* buffer contents starts at i and ends before length */
  1728. if(U_FAILURE(*err)) {
  1729. c=0xffff; /* no output */
  1730. } else if(length==0) {
  1731. /* no input or only state changes */
  1732. *err=U_INDEX_OUTOFBOUNDS_ERROR;
  1733. /* no need to reset explicitly because _toUnicodeWithCallback() did it */
  1734. c=0xffff; /* no output */
  1735. } else {
  1736. c=buffer[0];
  1737. i=1;
  1738. if(!U16_IS_LEAD(c)) {
  1739. /* consume c=buffer[0], done */
  1740. } else {
  1741. /* got a lead surrogate, see if a trail surrogate follows */
  1742. char16_t c2;
  1743. if(cnv->UCharErrorBufferLength>0) {
  1744. /* got overflow output from the conversion */
  1745. if(U16_IS_TRAIL(c2=cnv->UCharErrorBuffer[0])) {
  1746. /* got a trail surrogate, too */
  1747. c=U16_GET_SUPPLEMENTARY(c, c2);
  1748. /* move the remaining overflow contents up to the beginning */
  1749. if((--cnv->UCharErrorBufferLength)>0) {
  1750. uprv_memmove(cnv->UCharErrorBuffer, cnv->UCharErrorBuffer+1,
  1751. cnv->UCharErrorBufferLength*U_SIZEOF_UCHAR);
  1752. }
  1753. } else {
  1754. /* c is an unpaired lead surrogate, just return it */
  1755. }
  1756. } else if(args.source<sourceLimit) {
  1757. /* convert once more, to buffer[1] */
  1758. args.targetLimit=buffer+2;
  1759. _toUnicodeWithCallback(&args, err);
  1760. if(*err==U_BUFFER_OVERFLOW_ERROR) {
  1761. *err=U_ZERO_ERROR;
  1762. }
  1763. length=(int32_t)(args.target-buffer);
  1764. if(U_SUCCESS(*err) && length==2 && U16_IS_TRAIL(c2=buffer[1])) {
  1765. /* got a trail surrogate, too */
  1766. c=U16_GET_SUPPLEMENTARY(c, c2);
  1767. i=2;
  1768. }
  1769. }
  1770. }
  1771. }
  1772. /*
  1773. * move leftover output from buffer[i..length[
  1774. * into the beginning of the overflow buffer
  1775. */
  1776. if(i<length) {
  1777. /* move further overflow back */
  1778. int32_t delta=length-i;
  1779. if((length=cnv->UCharErrorBufferLength)>0) {
  1780. uprv_memmove(cnv->UCharErrorBuffer+delta, cnv->UCharErrorBuffer,
  1781. length*U_SIZEOF_UCHAR);
  1782. }
  1783. cnv->UCharErrorBufferLength=(int8_t)(length+delta);
  1784. cnv->UCharErrorBuffer[0]=buffer[i++];
  1785. if(delta>1) {
  1786. cnv->UCharErrorBuffer[1]=buffer[i];
  1787. }
  1788. }
  1789. *source=args.source;
  1790. return c;
  1791. }
  1792. /* ucnv_convert() and siblings ---------------------------------------------- */
  1793. U_CAPI void U_EXPORT2
  1794. ucnv_convertEx(UConverter *targetCnv, UConverter *sourceCnv,
  1795. char **target, const char *targetLimit,
  1796. const char **source, const char *sourceLimit,
  1797. char16_t *pivotStart, char16_t **pivotSource,
  1798. char16_t **pivotTarget, const char16_t *pivotLimit,
  1799. UBool reset, UBool flush,
  1800. UErrorCode *pErrorCode) {
  1801. char16_t pivotBuffer[CHUNK_SIZE];
  1802. const char16_t *myPivotSource;
  1803. char16_t *myPivotTarget;
  1804. const char *s;
  1805. char *t;
  1806. UConverterToUnicodeArgs toUArgs;
  1807. UConverterFromUnicodeArgs fromUArgs;
  1808. UConverterConvert convert;
  1809. /* error checking */
  1810. if(pErrorCode==nullptr || U_FAILURE(*pErrorCode)) {
  1811. return;
  1812. }
  1813. if( targetCnv==nullptr || sourceCnv==nullptr ||
  1814. source==nullptr || *source==nullptr ||
  1815. target==nullptr || *target==nullptr || targetLimit==nullptr
  1816. ) {
  1817. *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
  1818. return;
  1819. }
  1820. s=*source;
  1821. t=*target;
  1822. if((sourceLimit!=nullptr && sourceLimit<s) || targetLimit<t) {
  1823. *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
  1824. return;
  1825. }
  1826. /*
  1827. * Make sure that the buffer sizes do not exceed the number range for
  1828. * int32_t. See ucnv_toUnicode() for a more detailed comment.
  1829. */
  1830. if(
  1831. (sourceLimit!=nullptr && ((size_t)(sourceLimit-s)>(size_t)0x7fffffff && sourceLimit>s)) ||
  1832. ((size_t)(targetLimit-t)>(size_t)0x7fffffff && targetLimit>t)
  1833. ) {
  1834. *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
  1835. return;
  1836. }
  1837. if(pivotStart==nullptr) {
  1838. if(!flush) {
  1839. /* streaming conversion requires an explicit pivot buffer */
  1840. *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
  1841. return;
  1842. }
  1843. /* use the stack pivot buffer */
  1844. myPivotSource=myPivotTarget=pivotStart=pivotBuffer;
  1845. pivotSource=(char16_t **)&myPivotSource;
  1846. pivotTarget=&myPivotTarget;
  1847. pivotLimit=pivotBuffer+CHUNK_SIZE;
  1848. } else if( pivotStart>=pivotLimit ||
  1849. pivotSource==nullptr || *pivotSource==nullptr ||
  1850. pivotTarget==nullptr || *pivotTarget==nullptr ||
  1851. pivotLimit==nullptr
  1852. ) {
  1853. *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
  1854. return;
  1855. }
  1856. if(sourceLimit==nullptr) {
  1857. /* get limit of single-byte-NUL-terminated source string */
  1858. sourceLimit=uprv_strchr(*source, 0);
  1859. }
  1860. if(reset) {
  1861. ucnv_resetToUnicode(sourceCnv);
  1862. ucnv_resetFromUnicode(targetCnv);
  1863. *pivotSource=*pivotTarget=pivotStart;
  1864. } else if(targetCnv->charErrorBufferLength>0) {
  1865. /* output the targetCnv overflow buffer */
  1866. if(ucnv_outputOverflowFromUnicode(targetCnv, target, targetLimit, nullptr, pErrorCode)) {
  1867. /* U_BUFFER_OVERFLOW_ERROR */
  1868. return;
  1869. }
  1870. /* *target has moved, therefore stop using t */
  1871. if( !flush &&
  1872. targetCnv->preFromULength>=0 && *pivotSource==*pivotTarget &&
  1873. sourceCnv->UCharErrorBufferLength==0 && sourceCnv->preToULength>=0 && s==sourceLimit
  1874. ) {
  1875. /* the fromUnicode overflow buffer is emptied and there is no new input: we are done */
  1876. return;
  1877. }
  1878. }
  1879. /* Is direct-UTF-8 conversion available? */
  1880. if( sourceCnv->sharedData->staticData->conversionType==UCNV_UTF8 &&
  1881. targetCnv->sharedData->impl->fromUTF8!=nullptr
  1882. ) {
  1883. convert=targetCnv->sharedData->impl->fromUTF8;
  1884. } else if( targetCnv->sharedData->staticData->conversionType==UCNV_UTF8 &&
  1885. sourceCnv->sharedData->impl->toUTF8!=nullptr
  1886. ) {
  1887. convert=sourceCnv->sharedData->impl->toUTF8;
  1888. } else {
  1889. convert=nullptr;
  1890. }
  1891. /*
  1892. * If direct-UTF-8 conversion is available, then we use a smaller
  1893. * pivot buffer for error handling and partial matches
  1894. * so that we quickly return to direct conversion.
  1895. *
  1896. * 32 is large enough for UCNV_EXT_MAX_UCHARS and UCNV_ERROR_BUFFER_LENGTH.
  1897. *
  1898. * We could reduce the pivot buffer size further, at the cost of
  1899. * buffer overflows from callbacks.
  1900. * The pivot buffer should not be smaller than the maximum number of
  1901. * fromUnicode extension table input UChars
  1902. * (for m:n conversion, see
  1903. * targetCnv->sharedData->mbcs.extIndexes[UCNV_EXT_COUNT_UCHARS])
  1904. * or 2 for surrogate pairs.
  1905. *
  1906. * Too small a buffer can cause thrashing between pivoting and direct
  1907. * conversion, with function call overhead outweighing the benefits
  1908. * of direct conversion.
  1909. */
  1910. if(convert!=nullptr && (pivotLimit-pivotStart)>32) {
  1911. pivotLimit=pivotStart+32;
  1912. }
  1913. /* prepare the converter arguments */
  1914. fromUArgs.converter=targetCnv;
  1915. fromUArgs.flush=false;
  1916. fromUArgs.offsets=nullptr;
  1917. fromUArgs.target=*target;
  1918. fromUArgs.targetLimit=targetLimit;
  1919. fromUArgs.size=sizeof(fromUArgs);
  1920. toUArgs.converter=sourceCnv;
  1921. toUArgs.flush=flush;
  1922. toUArgs.offsets=nullptr;
  1923. toUArgs.source=s;
  1924. toUArgs.sourceLimit=sourceLimit;
  1925. toUArgs.targetLimit=pivotLimit;
  1926. toUArgs.size=sizeof(toUArgs);
  1927. /*
  1928. * TODO: Consider separating this function into two functions,
  1929. * extracting exactly the conversion loop,
  1930. * for readability and to reduce the set of visible variables.
  1931. *
  1932. * Otherwise stop using s and t from here on.
  1933. */
  1934. s=t=nullptr;
  1935. /*
  1936. * conversion loop
  1937. *
  1938. * The sequence of steps in the loop may appear backward,
  1939. * but the principle is simple:
  1940. * In the chain of
  1941. * source - sourceCnv overflow - pivot - targetCnv overflow - target
  1942. * empty out later buffers before refilling them from earlier ones.
  1943. *
  1944. * The targetCnv overflow buffer is flushed out only once before the loop.
  1945. */
  1946. for(;;) {
  1947. /*
  1948. * if(pivot not empty or error or replay or flush fromUnicode) {
  1949. * fromUnicode(pivot -> target);
  1950. * }
  1951. *
  1952. * For pivoting conversion; and for direct conversion for
  1953. * error callback handling and flushing the replay buffer.
  1954. */
  1955. if( *pivotSource<*pivotTarget ||
  1956. U_FAILURE(*pErrorCode) ||
  1957. targetCnv->preFromULength<0 ||
  1958. fromUArgs.flush
  1959. ) {
  1960. fromUArgs.source=*pivotSource;
  1961. fromUArgs.sourceLimit=*pivotTarget;
  1962. _fromUnicodeWithCallback(&fromUArgs, pErrorCode);
  1963. if(U_FAILURE(*pErrorCode)) {
  1964. /* target overflow, or conversion error */
  1965. *pivotSource=(char16_t *)fromUArgs.source;
  1966. break;
  1967. }
  1968. /*
  1969. * _fromUnicodeWithCallback() must have consumed the pivot contents
  1970. * (*pivotSource==*pivotTarget) since it returned with U_SUCCESS()
  1971. */
  1972. }
  1973. /* The pivot buffer is empty; reset it so we start at pivotStart. */
  1974. *pivotSource=*pivotTarget=pivotStart;
  1975. /*
  1976. * if(sourceCnv overflow buffer not empty) {
  1977. * move(sourceCnv overflow buffer -> pivot);
  1978. * continue;
  1979. * }
  1980. */
  1981. /* output the sourceCnv overflow buffer */
  1982. if(sourceCnv->UCharErrorBufferLength>0) {
  1983. if(ucnv_outputOverflowToUnicode(sourceCnv, pivotTarget, pivotLimit, nullptr, pErrorCode)) {
  1984. /* U_BUFFER_OVERFLOW_ERROR */
  1985. *pErrorCode=U_ZERO_ERROR;
  1986. }
  1987. continue;
  1988. }
  1989. /*
  1990. * check for end of input and break if done
  1991. *
  1992. * Checking both flush and fromUArgs.flush ensures that the converters
  1993. * have been called with the flush flag set if the ucnv_convertEx()
  1994. * caller set it.
  1995. */
  1996. if( toUArgs.source==sourceLimit &&
  1997. sourceCnv->preToULength>=0 && sourceCnv->toULength==0 &&
  1998. (!flush || fromUArgs.flush)
  1999. ) {
  2000. /* done successfully */
  2001. break;
  2002. }
  2003. /*
  2004. * use direct conversion if available
  2005. * but not if continuing a partial match
  2006. * or flushing the toUnicode replay buffer
  2007. */
  2008. if(convert!=nullptr && targetCnv->preFromUFirstCP<0 && sourceCnv->preToULength==0) {
  2009. if(*pErrorCode==U_USING_DEFAULT_WARNING) {
  2010. /* remove a warning that may be set by this function */
  2011. *pErrorCode=U_ZERO_ERROR;
  2012. }
  2013. convert(&fromUArgs, &toUArgs, pErrorCode);
  2014. if(*pErrorCode==U_BUFFER_OVERFLOW_ERROR) {
  2015. break;
  2016. } else if(U_FAILURE(*pErrorCode)) {
  2017. if(sourceCnv->toULength>0) {
  2018. /*
  2019. * Fall through to calling _toUnicodeWithCallback()
  2020. * for callback handling.
  2021. *
  2022. * The pivot buffer will be reset with
  2023. * *pivotSource=*pivotTarget=pivotStart;
  2024. * which indicates a toUnicode error to the caller
  2025. * (*pivotSource==pivotStart shows no pivot UChars consumed).
  2026. */
  2027. } else {
  2028. /*
  2029. * Indicate a fromUnicode error to the caller
  2030. * (*pivotSource>pivotStart shows some pivot UChars consumed).
  2031. */
  2032. *pivotSource=*pivotTarget=pivotStart+1;
  2033. /*
  2034. * Loop around to calling _fromUnicodeWithCallbacks()
  2035. * for callback handling.
  2036. */
  2037. continue;
  2038. }
  2039. } else if(*pErrorCode==U_USING_DEFAULT_WARNING) {
  2040. /*
  2041. * No error, but the implementation requested to temporarily
  2042. * fall back to pivoting.
  2043. */
  2044. *pErrorCode=U_ZERO_ERROR;
  2045. /*
  2046. * The following else branches are almost identical to the end-of-input
  2047. * handling in _toUnicodeWithCallback().
  2048. * Avoid calling it just for the end of input.
  2049. */
  2050. } else if(flush && sourceCnv->toULength>0) { /* flush==toUArgs.flush */
  2051. /*
  2052. * the entire input stream is consumed
  2053. * and there is a partial, truncated input sequence left
  2054. */
  2055. /* inject an error and continue with callback handling */
  2056. *pErrorCode=U_TRUNCATED_CHAR_FOUND;
  2057. } else {
  2058. /* input consumed */
  2059. if(flush) {
  2060. /* reset the converters without calling the callback functions */
  2061. _reset(sourceCnv, UCNV_RESET_TO_UNICODE, false);
  2062. _reset(targetCnv, UCNV_RESET_FROM_UNICODE, false);
  2063. }
  2064. /* done successfully */
  2065. break;
  2066. }
  2067. }
  2068. /*
  2069. * toUnicode(source -> pivot);
  2070. *
  2071. * For pivoting conversion; and for direct conversion for
  2072. * error callback handling, continuing partial matches
  2073. * and flushing the replay buffer.
  2074. *
  2075. * The pivot buffer is empty and reset.
  2076. */
  2077. toUArgs.target=pivotStart; /* ==*pivotTarget */
  2078. /* toUArgs.targetLimit=pivotLimit; already set before the loop */
  2079. _toUnicodeWithCallback(&toUArgs, pErrorCode);
  2080. *pivotTarget=toUArgs.target;
  2081. if(*pErrorCode==U_BUFFER_OVERFLOW_ERROR) {
  2082. /* pivot overflow: continue with the conversion loop */
  2083. *pErrorCode=U_ZERO_ERROR;
  2084. } else if(U_FAILURE(*pErrorCode) || (!flush && *pivotTarget==pivotStart)) {
  2085. /* conversion error, or there was nothing left to convert */
  2086. break;
  2087. }
  2088. /*
  2089. * else:
  2090. * _toUnicodeWithCallback() wrote into the pivot buffer,
  2091. * continue with fromUnicode conversion.
  2092. *
  2093. * Set the fromUnicode flush flag if we flush and if toUnicode has
  2094. * processed the end of the input.
  2095. */
  2096. if( flush && toUArgs.source==sourceLimit &&
  2097. sourceCnv->preToULength>=0 &&
  2098. sourceCnv->UCharErrorBufferLength==0
  2099. ) {
  2100. fromUArgs.flush=true;
  2101. }
  2102. }
  2103. /*
  2104. * The conversion loop is exited when one of the following is true:
  2105. * - the entire source text has been converted successfully to the target buffer
  2106. * - a target buffer overflow occurred
  2107. * - a conversion error occurred
  2108. */
  2109. *source=toUArgs.source;
  2110. *target=fromUArgs.target;
  2111. /* terminate the target buffer if possible */
  2112. if(flush && U_SUCCESS(*pErrorCode)) {
  2113. if(*target!=targetLimit) {
  2114. **target=0;
  2115. if(*pErrorCode==U_STRING_NOT_TERMINATED_WARNING) {
  2116. *pErrorCode=U_ZERO_ERROR;
  2117. }
  2118. } else {
  2119. *pErrorCode=U_STRING_NOT_TERMINATED_WARNING;
  2120. }
  2121. }
  2122. }
  2123. /* internal implementation of ucnv_convert() etc. with preflighting */
  2124. static int32_t
  2125. ucnv_internalConvert(UConverter *outConverter, UConverter *inConverter,
  2126. char *target, int32_t targetCapacity,
  2127. const char *source, int32_t sourceLength,
  2128. UErrorCode *pErrorCode) {
  2129. char16_t pivotBuffer[CHUNK_SIZE];
  2130. char16_t *pivot, *pivot2;
  2131. char *myTarget;
  2132. const char *sourceLimit;
  2133. const char *targetLimit;
  2134. int32_t targetLength=0;
  2135. /* set up */
  2136. if(sourceLength<0) {
  2137. sourceLimit=uprv_strchr(source, 0);
  2138. } else {
  2139. sourceLimit=source+sourceLength;
  2140. }
  2141. /* if there is no input data, we're done */
  2142. if(source==sourceLimit) {
  2143. return u_terminateChars(target, targetCapacity, 0, pErrorCode);
  2144. }
  2145. pivot=pivot2=pivotBuffer;
  2146. myTarget=target;
  2147. targetLength=0;
  2148. if(targetCapacity>0) {
  2149. /* perform real conversion */
  2150. targetLimit=target+targetCapacity;
  2151. ucnv_convertEx(outConverter, inConverter,
  2152. &myTarget, targetLimit,
  2153. &source, sourceLimit,
  2154. pivotBuffer, &pivot, &pivot2, pivotBuffer+CHUNK_SIZE,
  2155. false,
  2156. true,
  2157. pErrorCode);
  2158. targetLength = static_cast<int32_t>(myTarget - target);
  2159. }
  2160. /*
  2161. * If the output buffer is exhausted (or we are only "preflighting"), we need to stop writing
  2162. * to it but continue the conversion in order to store in targetCapacity
  2163. * the number of bytes that was required.
  2164. */
  2165. if(*pErrorCode==U_BUFFER_OVERFLOW_ERROR || targetCapacity==0)
  2166. {
  2167. char targetBuffer[CHUNK_SIZE];
  2168. targetLimit=targetBuffer+CHUNK_SIZE;
  2169. do {
  2170. *pErrorCode=U_ZERO_ERROR;
  2171. myTarget=targetBuffer;
  2172. ucnv_convertEx(outConverter, inConverter,
  2173. &myTarget, targetLimit,
  2174. &source, sourceLimit,
  2175. pivotBuffer, &pivot, &pivot2, pivotBuffer+CHUNK_SIZE,
  2176. false,
  2177. true,
  2178. pErrorCode);
  2179. targetLength += static_cast<int32_t>(myTarget - targetBuffer);
  2180. } while(*pErrorCode==U_BUFFER_OVERFLOW_ERROR);
  2181. /* done with preflighting, set warnings and errors as appropriate */
  2182. return u_terminateChars(target, targetCapacity, targetLength, pErrorCode);
  2183. }
  2184. /* no need to call u_terminateChars() because ucnv_convertEx() took care of that */
  2185. return targetLength;
  2186. }
  2187. U_CAPI int32_t U_EXPORT2
  2188. ucnv_convert(const char *toConverterName, const char *fromConverterName,
  2189. char *target, int32_t targetCapacity,
  2190. const char *source, int32_t sourceLength,
  2191. UErrorCode *pErrorCode) {
  2192. UConverter in, out; /* stack-allocated */
  2193. UConverter *inConverter, *outConverter;
  2194. int32_t targetLength;
  2195. if(pErrorCode==nullptr || U_FAILURE(*pErrorCode)) {
  2196. return 0;
  2197. }
  2198. if( source==nullptr || sourceLength<-1 ||
  2199. targetCapacity<0 || (targetCapacity>0 && target==nullptr)
  2200. ) {
  2201. *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
  2202. return 0;
  2203. }
  2204. /* if there is no input data, we're done */
  2205. if(sourceLength==0 || (sourceLength<0 && *source==0)) {
  2206. return u_terminateChars(target, targetCapacity, 0, pErrorCode);
  2207. }
  2208. /* create the converters */
  2209. inConverter=ucnv_createConverter(&in, fromConverterName, pErrorCode);
  2210. if(U_FAILURE(*pErrorCode)) {
  2211. return 0;
  2212. }
  2213. outConverter=ucnv_createConverter(&out, toConverterName, pErrorCode);
  2214. if(U_FAILURE(*pErrorCode)) {
  2215. ucnv_close(inConverter);
  2216. return 0;
  2217. }
  2218. targetLength=ucnv_internalConvert(outConverter, inConverter,
  2219. target, targetCapacity,
  2220. source, sourceLength,
  2221. pErrorCode);
  2222. ucnv_close(inConverter);
  2223. ucnv_close(outConverter);
  2224. return targetLength;
  2225. }
  2226. /* @internal */
  2227. static int32_t
  2228. ucnv_convertAlgorithmic(UBool convertToAlgorithmic,
  2229. UConverterType algorithmicType,
  2230. UConverter *cnv,
  2231. char *target, int32_t targetCapacity,
  2232. const char *source, int32_t sourceLength,
  2233. UErrorCode *pErrorCode) {
  2234. UConverter algoConverterStatic; /* stack-allocated */
  2235. UConverter *algoConverter, *to, *from;
  2236. int32_t targetLength;
  2237. if(pErrorCode==nullptr || U_FAILURE(*pErrorCode)) {
  2238. return 0;
  2239. }
  2240. if( cnv==nullptr || source==nullptr || sourceLength<-1 ||
  2241. targetCapacity<0 || (targetCapacity>0 && target==nullptr)
  2242. ) {
  2243. *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
  2244. return 0;
  2245. }
  2246. /* if there is no input data, we're done */
  2247. if(sourceLength==0 || (sourceLength<0 && *source==0)) {
  2248. return u_terminateChars(target, targetCapacity, 0, pErrorCode);
  2249. }
  2250. /* create the algorithmic converter */
  2251. algoConverter=ucnv_createAlgorithmicConverter(&algoConverterStatic, algorithmicType,
  2252. "", 0, pErrorCode);
  2253. if(U_FAILURE(*pErrorCode)) {
  2254. return 0;
  2255. }
  2256. /* reset the other converter */
  2257. if(convertToAlgorithmic) {
  2258. /* cnv->Unicode->algo */
  2259. ucnv_resetToUnicode(cnv);
  2260. to=algoConverter;
  2261. from=cnv;
  2262. } else {
  2263. /* algo->Unicode->cnv */
  2264. ucnv_resetFromUnicode(cnv);
  2265. from=algoConverter;
  2266. to=cnv;
  2267. }
  2268. targetLength=ucnv_internalConvert(to, from,
  2269. target, targetCapacity,
  2270. source, sourceLength,
  2271. pErrorCode);
  2272. ucnv_close(algoConverter);
  2273. return targetLength;
  2274. }
  2275. U_CAPI int32_t U_EXPORT2
  2276. ucnv_toAlgorithmic(UConverterType algorithmicType,
  2277. UConverter *cnv,
  2278. char *target, int32_t targetCapacity,
  2279. const char *source, int32_t sourceLength,
  2280. UErrorCode *pErrorCode) {
  2281. return ucnv_convertAlgorithmic(true, algorithmicType, cnv,
  2282. target, targetCapacity,
  2283. source, sourceLength,
  2284. pErrorCode);
  2285. }
  2286. U_CAPI int32_t U_EXPORT2
  2287. ucnv_fromAlgorithmic(UConverter *cnv,
  2288. UConverterType algorithmicType,
  2289. char *target, int32_t targetCapacity,
  2290. const char *source, int32_t sourceLength,
  2291. UErrorCode *pErrorCode) UPRV_NO_SANITIZE_UNDEFINED {
  2292. if(algorithmicType<0 || UCNV_NUMBER_OF_SUPPORTED_CONVERTER_TYPES<=algorithmicType) {
  2293. *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR;
  2294. return 0;
  2295. }
  2296. return ucnv_convertAlgorithmic(false, algorithmicType, cnv,
  2297. target, targetCapacity,
  2298. source, sourceLength,
  2299. pErrorCode);
  2300. }
  2301. U_CAPI UConverterType U_EXPORT2
  2302. ucnv_getType(const UConverter* converter)
  2303. {
  2304. int8_t type = converter->sharedData->staticData->conversionType;
  2305. #if !UCONFIG_NO_LEGACY_CONVERSION
  2306. if(type == UCNV_MBCS) {
  2307. return ucnv_MBCSGetType(converter);
  2308. }
  2309. #endif
  2310. return (UConverterType)type;
  2311. }
  2312. U_CAPI void U_EXPORT2
  2313. ucnv_getStarters(const UConverter* converter,
  2314. UBool starters[256],
  2315. UErrorCode* err)
  2316. {
  2317. if (err == nullptr || U_FAILURE(*err)) {
  2318. return;
  2319. }
  2320. if(converter->sharedData->impl->getStarters != nullptr) {
  2321. converter->sharedData->impl->getStarters(converter, starters, err);
  2322. } else {
  2323. *err = U_ILLEGAL_ARGUMENT_ERROR;
  2324. }
  2325. }
  2326. static const UAmbiguousConverter *ucnv_getAmbiguous(const UConverter *cnv)
  2327. {
  2328. UErrorCode errorCode;
  2329. const char *name;
  2330. int32_t i;
  2331. if(cnv==nullptr) {
  2332. return nullptr;
  2333. }
  2334. errorCode=U_ZERO_ERROR;
  2335. name=ucnv_getName(cnv, &errorCode);
  2336. if(U_FAILURE(errorCode)) {
  2337. return nullptr;
  2338. }
  2339. for(i=0; i<UPRV_LENGTHOF(ambiguousConverters); ++i)
  2340. {
  2341. if(0==uprv_strcmp(name, ambiguousConverters[i].name))
  2342. {
  2343. return ambiguousConverters+i;
  2344. }
  2345. }
  2346. return nullptr;
  2347. }
  2348. U_CAPI void U_EXPORT2
  2349. ucnv_fixFileSeparator(const UConverter *cnv,
  2350. char16_t* source,
  2351. int32_t sourceLength) {
  2352. const UAmbiguousConverter *a;
  2353. int32_t i;
  2354. char16_t variant5c;
  2355. if(cnv==nullptr || source==nullptr || sourceLength<=0 || (a=ucnv_getAmbiguous(cnv))==nullptr)
  2356. {
  2357. return;
  2358. }
  2359. variant5c=a->variant5c;
  2360. for(i=0; i<sourceLength; ++i) {
  2361. if(source[i]==variant5c) {
  2362. source[i]=0x5c;
  2363. }
  2364. }
  2365. }
  2366. U_CAPI UBool U_EXPORT2
  2367. ucnv_isAmbiguous(const UConverter *cnv) {
  2368. return ucnv_getAmbiguous(cnv)!=nullptr;
  2369. }
  2370. U_CAPI void U_EXPORT2
  2371. ucnv_setFallback(UConverter *cnv, UBool usesFallback)
  2372. {
  2373. cnv->useFallback = usesFallback;
  2374. }
  2375. U_CAPI UBool U_EXPORT2
  2376. ucnv_usesFallback(const UConverter *cnv)
  2377. {
  2378. return cnv->useFallback;
  2379. }
  2380. U_CAPI void U_EXPORT2
  2381. ucnv_getInvalidChars (const UConverter * converter,
  2382. char *errBytes,
  2383. int8_t * len,
  2384. UErrorCode * err)
  2385. {
  2386. if (err == nullptr || U_FAILURE(*err))
  2387. {
  2388. return;
  2389. }
  2390. if (len == nullptr || errBytes == nullptr || converter == nullptr)
  2391. {
  2392. *err = U_ILLEGAL_ARGUMENT_ERROR;
  2393. return;
  2394. }
  2395. if (*len < converter->invalidCharLength)
  2396. {
  2397. *err = U_INDEX_OUTOFBOUNDS_ERROR;
  2398. return;
  2399. }
  2400. if ((*len = converter->invalidCharLength) > 0)
  2401. {
  2402. uprv_memcpy (errBytes, converter->invalidCharBuffer, *len);
  2403. }
  2404. }
  2405. U_CAPI void U_EXPORT2
  2406. ucnv_getInvalidUChars (const UConverter * converter,
  2407. char16_t *errChars,
  2408. int8_t * len,
  2409. UErrorCode * err)
  2410. {
  2411. if (err == nullptr || U_FAILURE(*err))
  2412. {
  2413. return;
  2414. }
  2415. if (len == nullptr || errChars == nullptr || converter == nullptr)
  2416. {
  2417. *err = U_ILLEGAL_ARGUMENT_ERROR;
  2418. return;
  2419. }
  2420. if (*len < converter->invalidUCharLength)
  2421. {
  2422. *err = U_INDEX_OUTOFBOUNDS_ERROR;
  2423. return;
  2424. }
  2425. if ((*len = converter->invalidUCharLength) > 0)
  2426. {
  2427. u_memcpy (errChars, converter->invalidUCharBuffer, *len);
  2428. }
  2429. }
  2430. #define SIG_MAX_LEN 5
  2431. U_CAPI const char* U_EXPORT2
  2432. ucnv_detectUnicodeSignature( const char* source,
  2433. int32_t sourceLength,
  2434. int32_t* signatureLength,
  2435. UErrorCode* pErrorCode) {
  2436. int32_t dummy;
  2437. /* initial 0xa5 bytes: make sure that if we read <SIG_MAX_LEN
  2438. * bytes we don't misdetect something
  2439. */
  2440. char start[SIG_MAX_LEN]={ '\xa5', '\xa5', '\xa5', '\xa5', '\xa5' };
  2441. int i = 0;
  2442. if((pErrorCode==nullptr) || U_FAILURE(*pErrorCode)){
  2443. return nullptr;
  2444. }
  2445. if(source == nullptr || sourceLength < -1){
  2446. *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR;
  2447. return nullptr;
  2448. }
  2449. if(signatureLength == nullptr) {
  2450. signatureLength = &dummy;
  2451. }
  2452. if(sourceLength==-1){
  2453. sourceLength=(int32_t)uprv_strlen(source);
  2454. }
  2455. while(i<sourceLength&& i<SIG_MAX_LEN){
  2456. start[i]=source[i];
  2457. i++;
  2458. }
  2459. if(start[0] == '\xFE' && start[1] == '\xFF') {
  2460. *signatureLength=2;
  2461. return "UTF-16BE";
  2462. } else if(start[0] == '\xFF' && start[1] == '\xFE') {
  2463. if(start[2] == '\x00' && start[3] =='\x00') {
  2464. *signatureLength=4;
  2465. return "UTF-32LE";
  2466. } else {
  2467. *signatureLength=2;
  2468. return "UTF-16LE";
  2469. }
  2470. } else if(start[0] == '\xEF' && start[1] == '\xBB' && start[2] == '\xBF') {
  2471. *signatureLength=3;
  2472. return "UTF-8";
  2473. } else if(start[0] == '\x00' && start[1] == '\x00' &&
  2474. start[2] == '\xFE' && start[3]=='\xFF') {
  2475. *signatureLength=4;
  2476. return "UTF-32BE";
  2477. } else if(start[0] == '\x0E' && start[1] == '\xFE' && start[2] == '\xFF') {
  2478. *signatureLength=3;
  2479. return "SCSU";
  2480. } else if(start[0] == '\xFB' && start[1] == '\xEE' && start[2] == '\x28') {
  2481. *signatureLength=3;
  2482. return "BOCU-1";
  2483. } else if(start[0] == '\x2B' && start[1] == '\x2F' && start[2] == '\x76') {
  2484. /*
  2485. * UTF-7: Initial U+FEFF is encoded as +/v8 or +/v9 or +/v+ or +/v/
  2486. * depending on the second UTF-16 code unit.
  2487. * Detect the entire, closed Unicode mode sequence +/v8- for only U+FEFF
  2488. * if it occurs.
  2489. *
  2490. * So far we have +/v
  2491. */
  2492. if(start[3] == '\x38' && start[4] == '\x2D') {
  2493. /* 5 bytes +/v8- */
  2494. *signatureLength=5;
  2495. return "UTF-7";
  2496. } else if(start[3] == '\x38' || start[3] == '\x39' || start[3] == '\x2B' || start[3] == '\x2F') {
  2497. /* 4 bytes +/v8 or +/v9 or +/v+ or +/v/ */
  2498. *signatureLength=4;
  2499. return "UTF-7";
  2500. }
  2501. }else if(start[0]=='\xDD' && start[1]== '\x73'&& start[2]=='\x66' && start[3]=='\x73'){
  2502. *signatureLength=4;
  2503. return "UTF-EBCDIC";
  2504. }
  2505. /* no known Unicode signature byte sequence recognized */
  2506. *signatureLength=0;
  2507. return nullptr;
  2508. }
  2509. U_CAPI int32_t U_EXPORT2
  2510. ucnv_fromUCountPending(const UConverter* cnv, UErrorCode* status)
  2511. {
  2512. if(status == nullptr || U_FAILURE(*status)){
  2513. return -1;
  2514. }
  2515. if(cnv == nullptr){
  2516. *status = U_ILLEGAL_ARGUMENT_ERROR;
  2517. return -1;
  2518. }
  2519. if(cnv->preFromUFirstCP >= 0){
  2520. return U16_LENGTH(cnv->preFromUFirstCP)+cnv->preFromULength ;
  2521. }else if(cnv->preFromULength < 0){
  2522. return -cnv->preFromULength ;
  2523. }else if(cnv->fromUChar32 > 0){
  2524. return 1;
  2525. }
  2526. return 0;
  2527. }
  2528. U_CAPI int32_t U_EXPORT2
  2529. ucnv_toUCountPending(const UConverter* cnv, UErrorCode* status){
  2530. if(status == nullptr || U_FAILURE(*status)){
  2531. return -1;
  2532. }
  2533. if(cnv == nullptr){
  2534. *status = U_ILLEGAL_ARGUMENT_ERROR;
  2535. return -1;
  2536. }
  2537. if(cnv->preToULength > 0){
  2538. return cnv->preToULength ;
  2539. }else if(cnv->preToULength < 0){
  2540. return -cnv->preToULength;
  2541. }else if(cnv->toULength > 0){
  2542. return cnv->toULength;
  2543. }
  2544. return 0;
  2545. }
  2546. U_CAPI UBool U_EXPORT2
  2547. ucnv_isFixedWidth(UConverter *cnv, UErrorCode *status){
  2548. if (U_FAILURE(*status)) {
  2549. return false;
  2550. }
  2551. if (cnv == nullptr) {
  2552. *status = U_ILLEGAL_ARGUMENT_ERROR;
  2553. return false;
  2554. }
  2555. switch (ucnv_getType(cnv)) {
  2556. case UCNV_SBCS:
  2557. case UCNV_DBCS:
  2558. case UCNV_UTF32_BigEndian:
  2559. case UCNV_UTF32_LittleEndian:
  2560. case UCNV_UTF32:
  2561. case UCNV_US_ASCII:
  2562. return true;
  2563. default:
  2564. return false;
  2565. }
  2566. }
  2567. #endif
  2568. /*
  2569. * Hey, Emacs, please set the following:
  2570. *
  2571. * Local Variables:
  2572. * indent-tabs-mode: nil
  2573. * End:
  2574. *
  2575. */