ucnvmbcs.cpp 216 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723
  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) 2000-2016, International Business Machines
  7. * Corporation and others. All Rights Reserved.
  8. *
  9. ******************************************************************************
  10. * file name: ucnvmbcs.cpp
  11. * encoding: UTF-8
  12. * tab size: 8 (not used)
  13. * indentation:4
  14. *
  15. * created on: 2000jul03
  16. * created by: Markus W. Scherer
  17. *
  18. * The current code in this file replaces the previous implementation
  19. * of conversion code from multi-byte codepages to Unicode and back.
  20. * This implementation supports the following:
  21. * - legacy variable-length codepages with up to 4 bytes per character
  22. * - all Unicode code points (up to 0x10ffff)
  23. * - efficient distinction of unassigned vs. illegal byte sequences
  24. * - it is possible in fromUnicode() to directly deal with simple
  25. * stateful encodings (used for EBCDIC_STATEFUL)
  26. * - it is possible to convert Unicode code points
  27. * to a single zero byte (but not as a fallback except for SBCS)
  28. *
  29. * Remaining limitations in fromUnicode:
  30. * - byte sequences must not have leading zero bytes
  31. * - except for SBCS codepages: no fallback mapping from Unicode to a zero byte
  32. * - limitation to up to 4 bytes per character
  33. *
  34. * ICU 2.8 (late 2003) adds a secondary data structure which lifts some of these
  35. * limitations and adds m:n character mappings and other features.
  36. * See ucnv_ext.h for details.
  37. *
  38. * Change history:
  39. *
  40. * 5/6/2001 Ram Moved MBCS_SINGLE_RESULT_FROM_U,MBCS_STAGE_2_FROM_U,
  41. * MBCS_VALUE_2_FROM_STAGE_2, MBCS_VALUE_4_FROM_STAGE_2
  42. * macros to ucnvmbcs.h file
  43. */
  44. #include "unicode/utypes.h"
  45. #if !UCONFIG_NO_CONVERSION && !UCONFIG_NO_LEGACY_CONVERSION
  46. #include "unicode/ucnv.h"
  47. #include "unicode/ucnv_cb.h"
  48. #include "unicode/udata.h"
  49. #include "unicode/uset.h"
  50. #include "unicode/utf8.h"
  51. #include "unicode/utf16.h"
  52. #include "ucnv_bld.h"
  53. #include "ucnvmbcs.h"
  54. #include "ucnv_ext.h"
  55. #include "ucnv_cnv.h"
  56. #include "cmemory.h"
  57. #include "cstring.h"
  58. #include "umutex.h"
  59. #include "ustr_imp.h"
  60. /* control optimizations according to the platform */
  61. #define MBCS_UNROLL_SINGLE_TO_BMP 1
  62. #define MBCS_UNROLL_SINGLE_FROM_BMP 0
  63. /*
  64. * _MBCSHeader versions 5.3 & 4.3
  65. * (Note that the _MBCSHeader version is in addition to the converter formatVersion.)
  66. *
  67. * This version is optional. Version 5 is used for incompatible data format changes.
  68. * makeconv will continue to generate version 4 files if possible.
  69. *
  70. * Changes from version 4:
  71. *
  72. * The main difference is an additional _MBCSHeader field with
  73. * - the length (number of uint32_t) of the _MBCSHeader
  74. * - flags for further incompatible data format changes
  75. * - flags for further, backward compatible data format changes
  76. *
  77. * The MBCS_OPT_FROM_U flag indicates that most of the fromUnicode data is omitted from
  78. * the file and needs to be reconstituted at load time.
  79. * This requires a utf8Friendly format with an additional mbcsIndex table for fast
  80. * (and UTF-8-friendly) fromUnicode conversion for Unicode code points up to maxFastUChar.
  81. * (For details about these structures see below, and see ucnvmbcs.h.)
  82. *
  83. * utf8Friendly also implies that the fromUnicode mappings are stored in ascending order
  84. * of the Unicode code points. (This requires that the .ucm file has the |0 etc.
  85. * precision markers for all mappings.)
  86. *
  87. * All fallbacks have been moved to the extension table, leaving only roundtrips in the
  88. * omitted data that can be reconstituted from the toUnicode data.
  89. *
  90. * Of the stage 2 table, the part corresponding to maxFastUChar and below is omitted.
  91. * With only roundtrip mappings in the base fromUnicode data, this part is fully
  92. * redundant with the mbcsIndex and will be reconstituted from that (also using the
  93. * stage 1 table which contains the information about how stage 2 was compacted).
  94. *
  95. * The rest of the stage 2 table, the part for code points above maxFastUChar,
  96. * is stored in the file and will be appended to the reconstituted part.
  97. *
  98. * The entire fromUBytes array is omitted from the file and will be reconstitued.
  99. * This is done by enumerating all toUnicode roundtrip mappings, performing
  100. * each mapping (using the stage 1 and reconstituted stage 2 tables) and
  101. * writing instead of reading the byte values.
  102. *
  103. * _MBCSHeader version 4.3
  104. *
  105. * Change from version 4.2:
  106. * - Optional utf8Friendly data structures, with 64-entry stage 3 block
  107. * allocation for parts of the BMP, and an additional mbcsIndex in non-SBCS
  108. * files which can be used instead of stages 1 & 2.
  109. * Faster lookups for roundtrips from most commonly used characters,
  110. * and lookups from UTF-8 byte sequences with a natural bit distribution.
  111. * See ucnvmbcs.h for more details.
  112. *
  113. * Change from version 4.1:
  114. * - Added an optional extension table structure at the end of the .cnv file.
  115. * It is present if the upper bits of the header flags field contains a non-zero
  116. * byte offset to it.
  117. * Files that contain only a conversion table and no base table
  118. * use the special outputType MBCS_OUTPUT_EXT_ONLY.
  119. * These contain the base table name between the MBCS header and the extension
  120. * data.
  121. *
  122. * Change from version 4.0:
  123. * - Replace header.reserved with header.fromUBytesLength so that all
  124. * fields in the data have length.
  125. *
  126. * Changes from version 3 (for performance improvements):
  127. * - new bit distribution for state table entries
  128. * - reordered action codes
  129. * - new data structure for single-byte fromUnicode
  130. * + stage 2 only contains indexes
  131. * + stage 3 stores 16 bits per character with classification bits 15..8
  132. * - no multiplier for stage 1 entries
  133. * - stage 2 for non-single-byte codepages contains the index and the flags in
  134. * one 32-bit value
  135. * - 2-byte and 4-byte fromUnicode results are stored directly as 16/32-bit integers
  136. *
  137. * For more details about old versions of the MBCS data structure, see
  138. * the corresponding versions of this file.
  139. *
  140. * Converting stateless codepage data ---------------------------------------***
  141. * (or codepage data with simple states) to Unicode.
  142. *
  143. * Data structure and algorithm for converting from complex legacy codepages
  144. * to Unicode. (Designed before 2000-may-22.)
  145. *
  146. * The basic idea is that the structure of legacy codepages can be described
  147. * with state tables.
  148. * When reading a byte stream, each input byte causes a state transition.
  149. * Some transitions result in the output of a code point, some result in
  150. * "unassigned" or "illegal" output.
  151. * This is used here for character conversion.
  152. *
  153. * The data structure begins with a state table consisting of a row
  154. * per state, with 256 entries (columns) per row for each possible input
  155. * byte value.
  156. * Each entry is 32 bits wide, with two formats distinguished by
  157. * the sign bit (bit 31):
  158. *
  159. * One format for transitional entries (bit 31 not set) for non-final bytes, and
  160. * one format for final entries (bit 31 set).
  161. * Both formats contain the number of the next state in the same bit
  162. * positions.
  163. * State 0 is the initial state.
  164. *
  165. * Most of the time, the offset values of subsequent states are added
  166. * up to a scalar value. This value will eventually be the index of
  167. * the Unicode code point in a table that follows the state table.
  168. * The effect is that the code points for final state table rows
  169. * are contiguous. The code points of final state rows follow each other
  170. * in the order of the references to those final states by previous
  171. * states, etc.
  172. *
  173. * For some terminal states, the offset is itself the output Unicode
  174. * code point (16 bits for a BMP code point or 20 bits for a supplementary
  175. * code point (stored as code point minus 0x10000 so that 20 bits are enough).
  176. * For others, the code point in the Unicode table is stored with either
  177. * one or two code units: one for BMP code points, two for a pair of
  178. * surrogates.
  179. * All code points for a final state entry take up the same number of code
  180. * units, regardless of whether they all actually _use_ the same number
  181. * of code units. This is necessary for simple array access.
  182. *
  183. * An additional feature comes in with what in ICU is called "fallback"
  184. * mappings:
  185. *
  186. * In addition to round-trippable, precise, 1:1 mappings, there are often
  187. * mappings defined between similar, though not the same, characters.
  188. * Typically, such mappings occur only in fromUnicode mapping tables because
  189. * Unicode has a superset repertoire of most other codepages. However, it
  190. * is possible to provide such mappings in the toUnicode tables, too.
  191. * In this case, the fallback mappings are partly integrated into the
  192. * general state tables because the structure of the encoding includes their
  193. * byte sequences.
  194. * For final entries in an initial state, fallback mappings are stored in
  195. * the entry itself like with roundtrip mappings.
  196. * For other final entries, they are stored in the code units table if
  197. * the entry is for a pair of code units.
  198. * For single-unit results in the code units table, there is no space to
  199. * alternatively hold a fallback mapping; in this case, the code unit
  200. * is stored as U+fffe (unassigned), and the fallback mapping needs to
  201. * be looked up by the scalar offset value in a separate table.
  202. *
  203. * "Unassigned" state entries really mean "structurally unassigned",
  204. * i.e., such a byte sequence will never have a mapping result.
  205. *
  206. * The interpretation of the bits in each entry is as follows:
  207. *
  208. * Bit 31 not set, not a terminal entry ("transitional"):
  209. * 30..24 next state
  210. * 23..0 offset delta, to be added up
  211. *
  212. * Bit 31 set, terminal ("final") entry:
  213. * 30..24 next state (regardless of action code)
  214. * 23..20 action code:
  215. * action codes 0 and 1 result in precise-mapping Unicode code points
  216. * 0 valid byte sequence
  217. * 19..16 not used, 0
  218. * 15..0 16-bit Unicode BMP code point
  219. * never U+fffe or U+ffff
  220. * 1 valid byte sequence
  221. * 19..0 20-bit Unicode supplementary code point
  222. * never U+fffe or U+ffff
  223. *
  224. * action codes 2 and 3 result in fallback (unidirectional-mapping) Unicode code points
  225. * 2 valid byte sequence (fallback)
  226. * 19..16 not used, 0
  227. * 15..0 16-bit Unicode BMP code point as fallback result
  228. * 3 valid byte sequence (fallback)
  229. * 19..0 20-bit Unicode supplementary code point as fallback result
  230. *
  231. * action codes 4 and 5 may result in roundtrip/fallback/unassigned/illegal results
  232. * depending on the code units they result in
  233. * 4 valid byte sequence
  234. * 19..9 not used, 0
  235. * 8..0 final offset delta
  236. * pointing to one 16-bit code unit which may be
  237. * fffe unassigned -- look for a fallback for this offset
  238. * ffff illegal
  239. * 5 valid byte sequence
  240. * 19..9 not used, 0
  241. * 8..0 final offset delta
  242. * pointing to two 16-bit code units
  243. * (typically UTF-16 surrogates)
  244. * the result depends on the first code unit as follows:
  245. * 0000..d7ff roundtrip BMP code point (1st alone)
  246. * d800..dbff roundtrip surrogate pair (1st, 2nd)
  247. * dc00..dfff fallback surrogate pair (1st-400, 2nd)
  248. * e000 roundtrip BMP code point (2nd alone)
  249. * e001 fallback BMP code point (2nd alone)
  250. * fffe unassigned
  251. * ffff illegal
  252. * (the final offset deltas are at most 255 * 2,
  253. * times 2 because of storing code unit pairs)
  254. *
  255. * 6 unassigned byte sequence
  256. * 19..16 not used, 0
  257. * 15..0 16-bit Unicode BMP code point U+fffe (new with version 2)
  258. * this does not contain a final offset delta because the main
  259. * purpose of this action code is to save scalar offset values;
  260. * therefore, fallback values cannot be assigned to byte
  261. * sequences that result in this action code
  262. * 7 illegal byte sequence
  263. * 19..16 not used, 0
  264. * 15..0 16-bit Unicode BMP code point U+ffff (new with version 2)
  265. * 8 state change only
  266. * 19..0 not used, 0
  267. * useful for state changes in simple stateful encodings,
  268. * at Shift-In/Shift-Out codes
  269. *
  270. *
  271. * 9..15 reserved for future use
  272. * current implementations will only perform a state change
  273. * and ignore bits 19..0
  274. *
  275. * An encoding with contiguous ranges of unassigned byte sequences, like
  276. * Shift-JIS and especially EUC-TW, can be stored efficiently by having
  277. * at least two states for the trail bytes:
  278. * One trail byte state that results in code points, and one that only
  279. * has "unassigned" and "illegal" terminal states.
  280. *
  281. * Note: partly by accident, this data structure supports simple stateful
  282. * encodings without any additional logic.
  283. * Currently, only simple Shift-In/Shift-Out schemes are handled with
  284. * appropriate state tables (especially EBCDIC_STATEFUL!).
  285. *
  286. * MBCS version 2 added:
  287. * unassigned and illegal action codes have U+fffe and U+ffff
  288. * instead of unused bits; this is useful for _MBCS_SINGLE_SIMPLE_GET_NEXT_BMP()
  289. *
  290. * Converting from Unicode to codepage bytes --------------------------------***
  291. *
  292. * The conversion data structure for fromUnicode is designed for the known
  293. * structure of Unicode. It maps from 21-bit code points (0..0x10ffff) to
  294. * a sequence of 1..4 bytes, in addition to a flag that indicates if there is
  295. * a roundtrip mapping.
  296. *
  297. * The lookup is done with a 3-stage trie, using 11/6/4 bits for stage 1/2/3
  298. * like in the character properties table.
  299. * The beginning of the trie is at offsetFromUTable, the beginning of stage 3
  300. * with the resulting bytes is at offsetFromUBytes.
  301. *
  302. * Beginning with version 4, single-byte codepages have a significantly different
  303. * trie compared to other codepages.
  304. * In all cases, the entry in stage 1 is directly the index of the block of
  305. * 64 entries in stage 2.
  306. *
  307. * Single-byte lookup:
  308. *
  309. * Stage 2 only contains 16-bit indexes directly to the 16-blocks in stage 3.
  310. * Stage 3 contains one 16-bit word per result:
  311. * Bits 15..8 indicate the kind of result:
  312. * f roundtrip result
  313. * c fallback result from private-use code point
  314. * 8 fallback result from other code points
  315. * 0 unassigned
  316. * Bits 7..0 contain the codepage byte. A zero byte is always possible.
  317. *
  318. * In version 4.3, the runtime code can build an sbcsIndex for a utf8Friendly
  319. * file. For 2-byte UTF-8 byte sequences and some 3-byte sequences the lookup
  320. * becomes a 2-stage (single-index) trie lookup with 6 bits for stage 3.
  321. * ASCII code points can be looked up with a linear array access into stage 3.
  322. * See maxFastUChar and other details in ucnvmbcs.h.
  323. *
  324. * Multi-byte lookup:
  325. *
  326. * Stage 2 contains a 32-bit word for each 16-block in stage 3:
  327. * Bits 31..16 contain flags for which stage 3 entries contain roundtrip results
  328. * test: MBCS_FROM_U_IS_ROUNDTRIP(stage2Entry, c)
  329. * If this test is false, then a non-zero result will be interpreted as
  330. * a fallback mapping.
  331. * Bits 15..0 contain the index to stage 3, which must be multiplied by 16*(bytes per char)
  332. *
  333. * Stage 3 contains 2, 3, or 4 bytes per result.
  334. * 2 or 4 bytes are stored as uint16_t/uint32_t in platform endianness,
  335. * while 3 bytes are stored as bytes in big-endian order.
  336. * Leading zero bytes are ignored, and the number of bytes is counted.
  337. * A zero byte mapping result is possible as a roundtrip result.
  338. * For some output types, the actual result is processed from this;
  339. * see ucnv_MBCSFromUnicodeWithOffsets().
  340. *
  341. * Note that stage 1 always contains 0x440=1088 entries (0x440==0x110000>>10),
  342. * or (version 3 and up) for BMP-only codepages, it contains 64 entries.
  343. *
  344. * In version 4.3, a utf8Friendly file contains an mbcsIndex table.
  345. * For 2-byte UTF-8 byte sequences and most 3-byte sequences the lookup
  346. * becomes a 2-stage (single-index) trie lookup with 6 bits for stage 3.
  347. * ASCII code points can be looked up with a linear array access into stage 3.
  348. * See maxFastUChar, mbcsIndex and other details in ucnvmbcs.h.
  349. *
  350. * In version 3, stage 2 blocks may overlap by multiples of the multiplier
  351. * for compaction.
  352. * In version 4, stage 2 blocks (and for single-byte codepages, stage 3 blocks)
  353. * may overlap by any number of entries.
  354. *
  355. * MBCS version 2 added:
  356. * the converter checks for known output types, which allows
  357. * adding new ones without crashing an unaware converter
  358. */
  359. /**
  360. * Callback from ucnv_MBCSEnumToUnicode(), takes 32 mappings from
  361. * consecutive sequences of bytes, starting from the one encoded in value,
  362. * to Unicode code points. (Multiple mappings to reduce per-function call overhead.)
  363. * Does not currently support m:n mappings or reverse fallbacks.
  364. * This function will not be called for sequences of bytes with leading zeros.
  365. *
  366. * @param context an opaque pointer, as passed into ucnv_MBCSEnumToUnicode()
  367. * @param value contains 1..4 bytes of the first byte sequence, right-aligned
  368. * @param codePoints resulting Unicode code points, or negative if a byte sequence does
  369. * not map to anything
  370. * @return true to continue enumeration, false to stop
  371. */
  372. typedef UBool U_CALLCONV
  373. UConverterEnumToUCallback(const void *context, uint32_t value, UChar32 codePoints[32]);
  374. static void U_CALLCONV
  375. ucnv_MBCSLoad(UConverterSharedData *sharedData,
  376. UConverterLoadArgs *pArgs,
  377. const uint8_t *raw,
  378. UErrorCode *pErrorCode);
  379. static void U_CALLCONV
  380. ucnv_MBCSUnload(UConverterSharedData *sharedData);
  381. static void U_CALLCONV
  382. ucnv_MBCSOpen(UConverter *cnv,
  383. UConverterLoadArgs *pArgs,
  384. UErrorCode *pErrorCode);
  385. static UChar32 U_CALLCONV
  386. ucnv_MBCSGetNextUChar(UConverterToUnicodeArgs *pArgs,
  387. UErrorCode *pErrorCode);
  388. static void U_CALLCONV
  389. ucnv_MBCSGetStarters(const UConverter* cnv,
  390. UBool starters[256],
  391. UErrorCode *pErrorCode);
  392. U_CDECL_BEGIN
  393. static const char* U_CALLCONV
  394. ucnv_MBCSGetName(const UConverter *cnv);
  395. U_CDECL_END
  396. static void U_CALLCONV
  397. ucnv_MBCSWriteSub(UConverterFromUnicodeArgs *pArgs,
  398. int32_t offsetIndex,
  399. UErrorCode *pErrorCode);
  400. static UChar32 U_CALLCONV
  401. ucnv_MBCSGetNextUChar(UConverterToUnicodeArgs *pArgs,
  402. UErrorCode *pErrorCode);
  403. static void U_CALLCONV
  404. ucnv_SBCSFromUTF8(UConverterFromUnicodeArgs *pFromUArgs,
  405. UConverterToUnicodeArgs *pToUArgs,
  406. UErrorCode *pErrorCode);
  407. static void U_CALLCONV
  408. ucnv_MBCSGetUnicodeSet(const UConverter *cnv,
  409. const USetAdder *sa,
  410. UConverterUnicodeSet which,
  411. UErrorCode *pErrorCode);
  412. static void U_CALLCONV
  413. ucnv_DBCSFromUTF8(UConverterFromUnicodeArgs *pFromUArgs,
  414. UConverterToUnicodeArgs *pToUArgs,
  415. UErrorCode *pErrorCode);
  416. static const UConverterImpl _SBCSUTF8Impl={
  417. UCNV_MBCS,
  418. ucnv_MBCSLoad,
  419. ucnv_MBCSUnload,
  420. ucnv_MBCSOpen,
  421. nullptr,
  422. nullptr,
  423. ucnv_MBCSToUnicodeWithOffsets,
  424. ucnv_MBCSToUnicodeWithOffsets,
  425. ucnv_MBCSFromUnicodeWithOffsets,
  426. ucnv_MBCSFromUnicodeWithOffsets,
  427. ucnv_MBCSGetNextUChar,
  428. ucnv_MBCSGetStarters,
  429. ucnv_MBCSGetName,
  430. ucnv_MBCSWriteSub,
  431. nullptr,
  432. ucnv_MBCSGetUnicodeSet,
  433. nullptr,
  434. ucnv_SBCSFromUTF8
  435. };
  436. static const UConverterImpl _DBCSUTF8Impl={
  437. UCNV_MBCS,
  438. ucnv_MBCSLoad,
  439. ucnv_MBCSUnload,
  440. ucnv_MBCSOpen,
  441. nullptr,
  442. nullptr,
  443. ucnv_MBCSToUnicodeWithOffsets,
  444. ucnv_MBCSToUnicodeWithOffsets,
  445. ucnv_MBCSFromUnicodeWithOffsets,
  446. ucnv_MBCSFromUnicodeWithOffsets,
  447. ucnv_MBCSGetNextUChar,
  448. ucnv_MBCSGetStarters,
  449. ucnv_MBCSGetName,
  450. ucnv_MBCSWriteSub,
  451. nullptr,
  452. ucnv_MBCSGetUnicodeSet,
  453. nullptr,
  454. ucnv_DBCSFromUTF8
  455. };
  456. static const UConverterImpl _MBCSImpl={
  457. UCNV_MBCS,
  458. ucnv_MBCSLoad,
  459. ucnv_MBCSUnload,
  460. ucnv_MBCSOpen,
  461. nullptr,
  462. nullptr,
  463. ucnv_MBCSToUnicodeWithOffsets,
  464. ucnv_MBCSToUnicodeWithOffsets,
  465. ucnv_MBCSFromUnicodeWithOffsets,
  466. ucnv_MBCSFromUnicodeWithOffsets,
  467. ucnv_MBCSGetNextUChar,
  468. ucnv_MBCSGetStarters,
  469. ucnv_MBCSGetName,
  470. ucnv_MBCSWriteSub,
  471. nullptr,
  472. ucnv_MBCSGetUnicodeSet,
  473. nullptr,
  474. nullptr
  475. };
  476. /* Static data is in tools/makeconv/ucnvstat.c for data-based
  477. * converters. Be sure to update it as well.
  478. */
  479. const UConverterSharedData _MBCSData={
  480. sizeof(UConverterSharedData), 1,
  481. nullptr, nullptr, false, true, &_MBCSImpl,
  482. 0, UCNV_MBCS_TABLE_INITIALIZER
  483. };
  484. /* GB 18030 data ------------------------------------------------------------ */
  485. /* helper macros for linear values for GB 18030 four-byte sequences */
  486. #define LINEAR_18030(a, b, c, d) ((((a)*10+(b))*126L+(c))*10L+(d))
  487. #define LINEAR_18030_BASE LINEAR_18030(0x81, 0x30, 0x81, 0x30)
  488. #define LINEAR(x) LINEAR_18030(x>>24, (x>>16)&0xff, (x>>8)&0xff, x&0xff)
  489. /*
  490. * Some ranges of GB 18030 where both the Unicode code points and the
  491. * GB four-byte sequences are contiguous and are handled algorithmically by
  492. * the special callback functions below.
  493. * The values are start & end of Unicode & GB codes.
  494. *
  495. * Note that single surrogates are not mapped by GB 18030
  496. * as of the re-released mapping tables from 2000-nov-30.
  497. */
  498. static const uint32_t
  499. gb18030Ranges[14][4]={
  500. {0x10000, 0x10FFFF, LINEAR(0x90308130), LINEAR(0xE3329A35)},
  501. {0x9FA6, 0xD7FF, LINEAR(0x82358F33), LINEAR(0x8336C738)},
  502. {0x0452, 0x1E3E, LINEAR(0x8130D330), LINEAR(0x8135F436)},
  503. {0x1E40, 0x200F, LINEAR(0x8135F438), LINEAR(0x8136A531)},
  504. {0xE865, 0xF92B, LINEAR(0x8336D030), LINEAR(0x84308534)},
  505. {0x2643, 0x2E80, LINEAR(0x8137A839), LINEAR(0x8138FD38)},
  506. {0xFA2A, 0xFE2F, LINEAR(0x84309C38), LINEAR(0x84318537)},
  507. {0x3CE1, 0x4055, LINEAR(0x8231D438), LINEAR(0x8232AF32)},
  508. {0x361B, 0x3917, LINEAR(0x8230A633), LINEAR(0x8230F237)},
  509. {0x49B8, 0x4C76, LINEAR(0x8234A131), LINEAR(0x8234E733)},
  510. {0x4160, 0x4336, LINEAR(0x8232C937), LINEAR(0x8232F837)},
  511. {0x478E, 0x4946, LINEAR(0x8233E838), LINEAR(0x82349638)},
  512. {0x44D7, 0x464B, LINEAR(0x8233A339), LINEAR(0x8233C931)},
  513. {0xFFE6, 0xFFFF, LINEAR(0x8431A234), LINEAR(0x8431A439)}
  514. };
  515. /* bit flag for UConverter.options indicating GB 18030 special handling */
  516. #define _MBCS_OPTION_GB18030 0x8000
  517. /* bit flag for UConverter.options indicating KEIS,JEF,JIF special handling */
  518. #define _MBCS_OPTION_KEIS 0x01000
  519. #define _MBCS_OPTION_JEF 0x02000
  520. #define _MBCS_OPTION_JIPS 0x04000
  521. #define KEIS_SO_CHAR_1 0x0A
  522. #define KEIS_SO_CHAR_2 0x42
  523. #define KEIS_SI_CHAR_1 0x0A
  524. #define KEIS_SI_CHAR_2 0x41
  525. #define JEF_SO_CHAR 0x28
  526. #define JEF_SI_CHAR 0x29
  527. #define JIPS_SO_CHAR_1 0x1A
  528. #define JIPS_SO_CHAR_2 0x70
  529. #define JIPS_SI_CHAR_1 0x1A
  530. #define JIPS_SI_CHAR_2 0x71
  531. enum SISO_Option {
  532. SI,
  533. SO
  534. };
  535. typedef enum SISO_Option SISO_Option;
  536. static int32_t getSISOBytes(SISO_Option option, uint32_t cnvOption, uint8_t *value) {
  537. int32_t SISOLength = 0;
  538. switch (option) {
  539. case SI:
  540. if ((cnvOption&_MBCS_OPTION_KEIS)!=0) {
  541. value[0] = KEIS_SI_CHAR_1;
  542. value[1] = KEIS_SI_CHAR_2;
  543. SISOLength = 2;
  544. } else if ((cnvOption&_MBCS_OPTION_JEF)!=0) {
  545. value[0] = JEF_SI_CHAR;
  546. SISOLength = 1;
  547. } else if ((cnvOption&_MBCS_OPTION_JIPS)!=0) {
  548. value[0] = JIPS_SI_CHAR_1;
  549. value[1] = JIPS_SI_CHAR_2;
  550. SISOLength = 2;
  551. } else {
  552. value[0] = UCNV_SI;
  553. SISOLength = 1;
  554. }
  555. break;
  556. case SO:
  557. if ((cnvOption&_MBCS_OPTION_KEIS)!=0) {
  558. value[0] = KEIS_SO_CHAR_1;
  559. value[1] = KEIS_SO_CHAR_2;
  560. SISOLength = 2;
  561. } else if ((cnvOption&_MBCS_OPTION_JEF)!=0) {
  562. value[0] = JEF_SO_CHAR;
  563. SISOLength = 1;
  564. } else if ((cnvOption&_MBCS_OPTION_JIPS)!=0) {
  565. value[0] = JIPS_SO_CHAR_1;
  566. value[1] = JIPS_SO_CHAR_2;
  567. SISOLength = 2;
  568. } else {
  569. value[0] = UCNV_SO;
  570. SISOLength = 1;
  571. }
  572. break;
  573. default:
  574. /* Should never happen. */
  575. break;
  576. }
  577. return SISOLength;
  578. }
  579. /* Miscellaneous ------------------------------------------------------------ */
  580. /* similar to ucnv_MBCSGetNextUChar() but recursive */
  581. static UBool
  582. enumToU(UConverterMBCSTable *mbcsTable, int8_t stateProps[],
  583. int32_t state, uint32_t offset,
  584. uint32_t value,
  585. UConverterEnumToUCallback *callback, const void *context,
  586. UErrorCode *pErrorCode) {
  587. UChar32 codePoints[32];
  588. const int32_t *row;
  589. const uint16_t *unicodeCodeUnits;
  590. UChar32 anyCodePoints;
  591. int32_t b, limit;
  592. row=mbcsTable->stateTable[state];
  593. unicodeCodeUnits=mbcsTable->unicodeCodeUnits;
  594. value<<=8;
  595. anyCodePoints=-1; /* becomes non-negative if there is a mapping */
  596. b=(stateProps[state]&0x38)<<2;
  597. if(b==0 && stateProps[state]>=0x40) {
  598. /* skip byte sequences with leading zeros because they are not stored in the fromUnicode table */
  599. codePoints[0]=U_SENTINEL;
  600. b=1;
  601. }
  602. limit=((stateProps[state]&7)+1)<<5;
  603. while(b<limit) {
  604. int32_t entry=row[b];
  605. if(MBCS_ENTRY_IS_TRANSITION(entry)) {
  606. int32_t nextState=MBCS_ENTRY_TRANSITION_STATE(entry);
  607. if(stateProps[nextState]>=0) {
  608. /* recurse to a state with non-ignorable actions */
  609. if(!enumToU(
  610. mbcsTable, stateProps, nextState,
  611. offset+MBCS_ENTRY_TRANSITION_OFFSET(entry),
  612. value | static_cast<uint32_t>(b),
  613. callback, context,
  614. pErrorCode)) {
  615. return false;
  616. }
  617. }
  618. codePoints[b&0x1f]=U_SENTINEL;
  619. } else {
  620. UChar32 c;
  621. int32_t action;
  622. /*
  623. * An if-else-if chain provides more reliable performance for
  624. * the most common cases compared to a switch.
  625. */
  626. action=MBCS_ENTRY_FINAL_ACTION(entry);
  627. if(action==MBCS_STATE_VALID_DIRECT_16) {
  628. /* output BMP code point */
  629. c = static_cast<char16_t>(MBCS_ENTRY_FINAL_VALUE_16(entry));
  630. } else if(action==MBCS_STATE_VALID_16) {
  631. int32_t finalOffset=offset+MBCS_ENTRY_FINAL_VALUE_16(entry);
  632. c=unicodeCodeUnits[finalOffset];
  633. if(c<0xfffe) {
  634. /* output BMP code point */
  635. } else {
  636. c=U_SENTINEL;
  637. }
  638. } else if(action==MBCS_STATE_VALID_16_PAIR) {
  639. int32_t finalOffset=offset+MBCS_ENTRY_FINAL_VALUE_16(entry);
  640. c=unicodeCodeUnits[finalOffset++];
  641. if(c<0xd800) {
  642. /* output BMP code point below 0xd800 */
  643. } else if(c<=0xdbff) {
  644. /* output roundtrip or fallback supplementary code point */
  645. c=((c&0x3ff)<<10)+unicodeCodeUnits[finalOffset]+(0x10000-0xdc00);
  646. } else if(c==0xe000) {
  647. /* output roundtrip BMP code point above 0xd800 or fallback BMP code point */
  648. c=unicodeCodeUnits[finalOffset];
  649. } else {
  650. c=U_SENTINEL;
  651. }
  652. } else if(action==MBCS_STATE_VALID_DIRECT_20) {
  653. /* output supplementary code point */
  654. c = static_cast<UChar32>(MBCS_ENTRY_FINAL_VALUE(entry) + 0x10000);
  655. } else {
  656. c=U_SENTINEL;
  657. }
  658. codePoints[b&0x1f]=c;
  659. anyCodePoints&=c;
  660. }
  661. if(((++b)&0x1f)==0) {
  662. if(anyCodePoints>=0) {
  663. if (!callback(context, value | static_cast<uint32_t>(b - 0x20), codePoints)) {
  664. return false;
  665. }
  666. anyCodePoints=-1;
  667. }
  668. }
  669. }
  670. return true;
  671. }
  672. /*
  673. * Only called if stateProps[state]==-1.
  674. * A recursive call may do stateProps[state]|=0x40 if this state is the target of an
  675. * MBCS_STATE_CHANGE_ONLY.
  676. */
  677. static int8_t
  678. getStateProp(const int32_t (*stateTable)[256], int8_t stateProps[], int state) {
  679. const int32_t *row;
  680. int32_t min, max, entry, nextState;
  681. row=stateTable[state];
  682. stateProps[state]=0;
  683. /* find first non-ignorable state */
  684. for(min=0;; ++min) {
  685. entry=row[min];
  686. nextState=MBCS_ENTRY_STATE(entry);
  687. if(stateProps[nextState]==-1) {
  688. getStateProp(stateTable, stateProps, nextState);
  689. }
  690. if(MBCS_ENTRY_IS_TRANSITION(entry)) {
  691. if(stateProps[nextState]>=0) {
  692. break;
  693. }
  694. } else if(MBCS_ENTRY_FINAL_ACTION(entry)<MBCS_STATE_UNASSIGNED) {
  695. break;
  696. }
  697. if(min==0xff) {
  698. stateProps[state]=-0x40; /* (int8_t)0xc0 */
  699. return stateProps[state];
  700. }
  701. }
  702. stateProps[state] |= static_cast<int8_t>((min >> 5) << 3);
  703. /* find last non-ignorable state */
  704. for(max=0xff; min<max; --max) {
  705. entry=row[max];
  706. nextState=MBCS_ENTRY_STATE(entry);
  707. if(stateProps[nextState]==-1) {
  708. getStateProp(stateTable, stateProps, nextState);
  709. }
  710. if(MBCS_ENTRY_IS_TRANSITION(entry)) {
  711. if(stateProps[nextState]>=0) {
  712. break;
  713. }
  714. } else if(MBCS_ENTRY_FINAL_ACTION(entry)<MBCS_STATE_UNASSIGNED) {
  715. break;
  716. }
  717. }
  718. stateProps[state] |= static_cast<int8_t>(max >> 5);
  719. /* recurse further and collect direct-state information */
  720. while(min<=max) {
  721. entry=row[min];
  722. nextState=MBCS_ENTRY_STATE(entry);
  723. if(stateProps[nextState]==-1) {
  724. getStateProp(stateTable, stateProps, nextState);
  725. }
  726. if(MBCS_ENTRY_IS_FINAL(entry)) {
  727. stateProps[nextState]|=0x40;
  728. if(MBCS_ENTRY_FINAL_ACTION(entry)<=MBCS_STATE_FALLBACK_DIRECT_20) {
  729. stateProps[state]|=0x40;
  730. }
  731. }
  732. ++min;
  733. }
  734. return stateProps[state];
  735. }
  736. /*
  737. * Internal function enumerating the toUnicode data of an MBCS converter.
  738. * Currently only used for reconstituting data for a MBCS_OPT_NO_FROM_U
  739. * table, but could also be used for a future ucnv_getUnicodeSet() option
  740. * that includes reverse fallbacks (after updating this function's implementation).
  741. * Currently only handles roundtrip mappings.
  742. * Does not currently handle extensions.
  743. */
  744. static void
  745. ucnv_MBCSEnumToUnicode(UConverterMBCSTable *mbcsTable,
  746. UConverterEnumToUCallback *callback, const void *context,
  747. UErrorCode *pErrorCode) {
  748. /*
  749. * Properties for each state, to speed up the enumeration.
  750. * Ignorable actions are unassigned/illegal/state-change-only:
  751. * They do not lead to mappings.
  752. *
  753. * Bits 7..6:
  754. * 1 direct/initial state (stateful converters have multiple)
  755. * 0 non-initial state with transitions or with non-ignorable result actions
  756. * -1 final state with only ignorable actions
  757. *
  758. * Bits 5..3:
  759. * The lowest byte value with non-ignorable actions is
  760. * value<<5 (rounded down).
  761. *
  762. * Bits 2..0:
  763. * The highest byte value with non-ignorable actions is
  764. * (value<<5)&0x1f (rounded up).
  765. */
  766. int8_t stateProps[MBCS_MAX_STATE_COUNT];
  767. int32_t state;
  768. uprv_memset(stateProps, -1, sizeof(stateProps));
  769. /* recurse from state 0 and set all stateProps */
  770. getStateProp(mbcsTable->stateTable, stateProps, 0);
  771. for(state=0; state<mbcsTable->countStates; ++state) {
  772. /*if(stateProps[state]==-1) {
  773. printf("unused/unreachable <icu:state> %d\n", state);
  774. }*/
  775. if(stateProps[state]>=0x40) {
  776. /* start from each direct state */
  777. enumToU(
  778. mbcsTable, stateProps, state, 0, 0,
  779. callback, context,
  780. pErrorCode);
  781. }
  782. }
  783. }
  784. U_CFUNC void
  785. ucnv_MBCSGetFilteredUnicodeSetForUnicode(const UConverterSharedData *sharedData,
  786. const USetAdder *sa,
  787. UConverterUnicodeSet which,
  788. UConverterSetFilter filter,
  789. UErrorCode *pErrorCode) {
  790. const UConverterMBCSTable *mbcsTable;
  791. const uint16_t *table;
  792. uint32_t st3;
  793. uint16_t st1, maxStage1, st2;
  794. UChar32 c;
  795. /* enumerate the from-Unicode trie table */
  796. mbcsTable=&sharedData->mbcs;
  797. table=mbcsTable->fromUnicodeTable;
  798. if(mbcsTable->unicodeMask&UCNV_HAS_SUPPLEMENTARY) {
  799. maxStage1=0x440;
  800. } else {
  801. maxStage1=0x40;
  802. }
  803. c=0; /* keep track of the current code point while enumerating */
  804. if(mbcsTable->outputType==MBCS_OUTPUT_1) {
  805. const uint16_t *stage2, *stage3, *results;
  806. uint16_t minValue;
  807. results=(const uint16_t *)mbcsTable->fromUnicodeBytes;
  808. /*
  809. * Set a threshold variable for selecting which mappings to use.
  810. * See ucnv_MBCSSingleFromBMPWithOffsets() and
  811. * MBCS_SINGLE_RESULT_FROM_U() for details.
  812. */
  813. if(which==UCNV_ROUNDTRIP_SET) {
  814. /* use only roundtrips */
  815. minValue=0xf00;
  816. } else /* UCNV_ROUNDTRIP_AND_FALLBACK_SET */ {
  817. /* use all roundtrip and fallback results */
  818. minValue=0x800;
  819. }
  820. for(st1=0; st1<maxStage1; ++st1) {
  821. st2=table[st1];
  822. if(st2>maxStage1) {
  823. stage2=table+st2;
  824. for(st2=0; st2<64; ++st2) {
  825. if((st3=stage2[st2])!=0) {
  826. /* read the stage 3 block */
  827. stage3=results+st3;
  828. do {
  829. if(*stage3++>=minValue) {
  830. sa->add(sa->set, c);
  831. }
  832. } while((++c&0xf)!=0);
  833. } else {
  834. c+=16; /* empty stage 3 block */
  835. }
  836. }
  837. } else {
  838. c+=1024; /* empty stage 2 block */
  839. }
  840. }
  841. } else {
  842. const uint32_t *stage2;
  843. const uint8_t *stage3, *bytes;
  844. uint32_t st3Multiplier;
  845. uint32_t value;
  846. UBool useFallback;
  847. bytes=mbcsTable->fromUnicodeBytes;
  848. useFallback = which == UCNV_ROUNDTRIP_AND_FALLBACK_SET;
  849. switch(mbcsTable->outputType) {
  850. case MBCS_OUTPUT_3:
  851. case MBCS_OUTPUT_4_EUC:
  852. st3Multiplier=3;
  853. break;
  854. case MBCS_OUTPUT_4:
  855. st3Multiplier=4;
  856. break;
  857. default:
  858. st3Multiplier=2;
  859. break;
  860. }
  861. for(st1=0; st1<maxStage1; ++st1) {
  862. st2=table[st1];
  863. if(st2>(maxStage1>>1)) {
  864. stage2=(const uint32_t *)table+st2;
  865. for(st2=0; st2<64; ++st2) {
  866. if((st3=stage2[st2])!=0) {
  867. /* read the stage 3 block */
  868. stage3=bytes+st3Multiplier*16*(uint32_t)(uint16_t)st3;
  869. /* get the roundtrip flags for the stage 3 block */
  870. st3>>=16;
  871. /*
  872. * Add code points for which the roundtrip flag is set,
  873. * or which map to non-zero bytes if we use fallbacks.
  874. * See ucnv_MBCSFromUnicodeWithOffsets() for details.
  875. */
  876. switch(filter) {
  877. case UCNV_SET_FILTER_NONE:
  878. do {
  879. if(st3&1) {
  880. sa->add(sa->set, c);
  881. stage3+=st3Multiplier;
  882. } else if(useFallback) {
  883. uint8_t b=0;
  884. switch(st3Multiplier) {
  885. case 4:
  886. b|=*stage3++;
  887. U_FALLTHROUGH;
  888. case 3:
  889. b|=*stage3++;
  890. U_FALLTHROUGH;
  891. case 2:
  892. b|=stage3[0]|stage3[1];
  893. stage3+=2;
  894. U_FALLTHROUGH;
  895. default:
  896. break;
  897. }
  898. if(b!=0) {
  899. sa->add(sa->set, c);
  900. }
  901. }
  902. st3>>=1;
  903. } while((++c&0xf)!=0);
  904. break;
  905. case UCNV_SET_FILTER_DBCS_ONLY:
  906. /* Ignore single-byte results (<0x100). */
  907. do {
  908. if(((st3&1)!=0 || useFallback) && *((const uint16_t *)stage3)>=0x100) {
  909. sa->add(sa->set, c);
  910. }
  911. st3>>=1;
  912. stage3+=2; /* +=st3Multiplier */
  913. } while((++c&0xf)!=0);
  914. break;
  915. case UCNV_SET_FILTER_2022_CN:
  916. /* Only add code points that map to CNS 11643 planes 1 & 2 for non-EXT ISO-2022-CN. */
  917. do {
  918. if(((st3&1)!=0 || useFallback) && ((value=*stage3)==0x81 || value==0x82)) {
  919. sa->add(sa->set, c);
  920. }
  921. st3>>=1;
  922. stage3+=3; /* +=st3Multiplier */
  923. } while((++c&0xf)!=0);
  924. break;
  925. case UCNV_SET_FILTER_SJIS:
  926. /* Only add code points that map to Shift-JIS codes corresponding to JIS X 0208. */
  927. do {
  928. if(((st3&1)!=0 || useFallback) && (value=*((const uint16_t *)stage3))>=0x8140 && value<=0xeffc) {
  929. sa->add(sa->set, c);
  930. }
  931. st3>>=1;
  932. stage3+=2; /* +=st3Multiplier */
  933. } while((++c&0xf)!=0);
  934. break;
  935. case UCNV_SET_FILTER_GR94DBCS:
  936. /* Only add code points that map to ISO 2022 GR 94 DBCS codes (each byte A1..FE). */
  937. do {
  938. if( ((st3&1)!=0 || useFallback) &&
  939. (uint16_t)((value=*((const uint16_t *)stage3)) - 0xa1a1)<=(0xfefe - 0xa1a1) &&
  940. (uint8_t)(value-0xa1)<=(0xfe - 0xa1)
  941. ) {
  942. sa->add(sa->set, c);
  943. }
  944. st3>>=1;
  945. stage3+=2; /* +=st3Multiplier */
  946. } while((++c&0xf)!=0);
  947. break;
  948. case UCNV_SET_FILTER_HZ:
  949. /* Only add code points that are suitable for HZ DBCS (lead byte A1..FD). */
  950. do {
  951. if( ((st3&1)!=0 || useFallback) &&
  952. (uint16_t)((value=*((const uint16_t *)stage3))-0xa1a1)<=(0xfdfe - 0xa1a1) &&
  953. (uint8_t)(value-0xa1)<=(0xfe - 0xa1)
  954. ) {
  955. sa->add(sa->set, c);
  956. }
  957. st3>>=1;
  958. stage3+=2; /* +=st3Multiplier */
  959. } while((++c&0xf)!=0);
  960. break;
  961. default:
  962. *pErrorCode=U_INTERNAL_PROGRAM_ERROR;
  963. return;
  964. }
  965. } else {
  966. c+=16; /* empty stage 3 block */
  967. }
  968. }
  969. } else {
  970. c+=1024; /* empty stage 2 block */
  971. }
  972. }
  973. }
  974. ucnv_extGetUnicodeSet(sharedData, sa, which, filter, pErrorCode);
  975. }
  976. U_CFUNC void
  977. ucnv_MBCSGetUnicodeSetForUnicode(const UConverterSharedData *sharedData,
  978. const USetAdder *sa,
  979. UConverterUnicodeSet which,
  980. UErrorCode *pErrorCode) {
  981. ucnv_MBCSGetFilteredUnicodeSetForUnicode(
  982. sharedData, sa, which,
  983. sharedData->mbcs.outputType==MBCS_OUTPUT_DBCS_ONLY ?
  984. UCNV_SET_FILTER_DBCS_ONLY :
  985. UCNV_SET_FILTER_NONE,
  986. pErrorCode);
  987. }
  988. static void U_CALLCONV
  989. ucnv_MBCSGetUnicodeSet(const UConverter *cnv,
  990. const USetAdder *sa,
  991. UConverterUnicodeSet which,
  992. UErrorCode *pErrorCode) {
  993. if(cnv->options&_MBCS_OPTION_GB18030) {
  994. sa->addRange(sa->set, 0, 0xd7ff);
  995. sa->addRange(sa->set, 0xe000, 0x10ffff);
  996. } else {
  997. ucnv_MBCSGetUnicodeSetForUnicode(cnv->sharedData, sa, which, pErrorCode);
  998. }
  999. }
  1000. /* conversion extensions for input not in the main table -------------------- */
  1001. /*
  1002. * Hardcoded extension handling for GB 18030.
  1003. * Definition of LINEAR macros and gb18030Ranges see near the beginning of the file.
  1004. *
  1005. * In the future, conversion extensions may handle m:n mappings and delta tables,
  1006. * see https://htmlpreview.github.io/?https://github.com/unicode-org/icu-docs/blob/main/design/conversion/conversion_extensions.html
  1007. *
  1008. * If an input character cannot be mapped, then these functions set an error
  1009. * code. The framework will then call the callback function.
  1010. */
  1011. /*
  1012. * @return if(U_FAILURE) return the code point for cnv->fromUChar32
  1013. * else return 0 after output has been written to the target
  1014. */
  1015. static UChar32
  1016. _extFromU(UConverter *cnv, const UConverterSharedData *sharedData,
  1017. UChar32 cp,
  1018. const char16_t **source, const char16_t *sourceLimit,
  1019. uint8_t **target, const uint8_t *targetLimit,
  1020. int32_t **offsets, int32_t sourceIndex,
  1021. UBool flush,
  1022. UErrorCode *pErrorCode) {
  1023. const int32_t *cx;
  1024. cnv->useSubChar1=false;
  1025. if( (cx=sharedData->mbcs.extIndexes)!=nullptr &&
  1026. ucnv_extInitialMatchFromU(
  1027. cnv, cx,
  1028. cp, source, sourceLimit,
  1029. reinterpret_cast<char**>(target), reinterpret_cast<const char*>(targetLimit),
  1030. offsets, sourceIndex,
  1031. flush,
  1032. pErrorCode)
  1033. ) {
  1034. return 0; /* an extension mapping handled the input */
  1035. }
  1036. /* GB 18030 */
  1037. if((cnv->options&_MBCS_OPTION_GB18030)!=0) {
  1038. const uint32_t *range;
  1039. int32_t i;
  1040. range=gb18030Ranges[0];
  1041. for(i=0; i<UPRV_LENGTHOF(gb18030Ranges); range+=4, ++i) {
  1042. if (range[0] <= static_cast<uint32_t>(cp) && static_cast<uint32_t>(cp) <= range[1]) {
  1043. /* found the Unicode code point, output the four-byte sequence for it */
  1044. uint32_t linear;
  1045. char bytes[4];
  1046. /* get the linear value of the first GB 18030 code in this range */
  1047. linear=range[2]-LINEAR_18030_BASE;
  1048. /* add the offset from the beginning of the range */
  1049. linear += (static_cast<uint32_t>(cp) - range[0]);
  1050. /* turn this into a four-byte sequence */
  1051. bytes[3] = static_cast<char>(0x30 + linear % 10); linear /= 10;
  1052. bytes[2] = static_cast<char>(0x81 + linear % 126); linear /= 126;
  1053. bytes[1] = static_cast<char>(0x30 + linear % 10); linear /= 10;
  1054. bytes[0] = static_cast<char>(0x81 + linear);
  1055. /* output this sequence */
  1056. ucnv_fromUWriteBytes(cnv,
  1057. bytes, 4, reinterpret_cast<char**>(target), reinterpret_cast<const char*>(targetLimit),
  1058. offsets, sourceIndex, pErrorCode);
  1059. return 0;
  1060. }
  1061. }
  1062. }
  1063. /* no mapping */
  1064. *pErrorCode=U_INVALID_CHAR_FOUND;
  1065. return cp;
  1066. }
  1067. /*
  1068. * Input sequence: cnv->toUBytes[0..length[
  1069. * @return if(U_FAILURE) return the length (toULength, byteIndex) for the input
  1070. * else return 0 after output has been written to the target
  1071. */
  1072. static int8_t
  1073. _extToU(UConverter *cnv, const UConverterSharedData *sharedData,
  1074. int8_t length,
  1075. const uint8_t **source, const uint8_t *sourceLimit,
  1076. char16_t **target, const char16_t *targetLimit,
  1077. int32_t **offsets, int32_t sourceIndex,
  1078. UBool flush,
  1079. UErrorCode *pErrorCode) {
  1080. const int32_t *cx;
  1081. if( (cx=sharedData->mbcs.extIndexes)!=nullptr &&
  1082. ucnv_extInitialMatchToU(
  1083. cnv, cx,
  1084. length, reinterpret_cast<const char**>(source), reinterpret_cast<const char*>(sourceLimit),
  1085. target, targetLimit,
  1086. offsets, sourceIndex,
  1087. flush,
  1088. pErrorCode)
  1089. ) {
  1090. return 0; /* an extension mapping handled the input */
  1091. }
  1092. /* GB 18030 */
  1093. if(length==4 && (cnv->options&_MBCS_OPTION_GB18030)!=0) {
  1094. const uint32_t *range;
  1095. uint32_t linear;
  1096. int32_t i;
  1097. linear=LINEAR_18030(cnv->toUBytes[0], cnv->toUBytes[1], cnv->toUBytes[2], cnv->toUBytes[3]);
  1098. range=gb18030Ranges[0];
  1099. for(i=0; i<UPRV_LENGTHOF(gb18030Ranges); range+=4, ++i) {
  1100. if(range[2]<=linear && linear<=range[3]) {
  1101. /* found the sequence, output the Unicode code point for it */
  1102. *pErrorCode=U_ZERO_ERROR;
  1103. /* add the linear difference between the input and start sequences to the start code point */
  1104. linear=range[0]+(linear-range[2]);
  1105. /* output this code point */
  1106. ucnv_toUWriteCodePoint(cnv, linear, target, targetLimit, offsets, sourceIndex, pErrorCode);
  1107. return 0;
  1108. }
  1109. }
  1110. }
  1111. /* no mapping */
  1112. *pErrorCode=U_INVALID_CHAR_FOUND;
  1113. return length;
  1114. }
  1115. /* EBCDIC swap LF<->NL ------------------------------------------------------ */
  1116. /*
  1117. * This code modifies a standard EBCDIC<->Unicode mapping table for
  1118. * OS/390 (z/OS) Unix System Services (Open Edition).
  1119. * The difference is in the mapping of Line Feed and New Line control codes:
  1120. * Standard EBCDIC maps
  1121. *
  1122. * <U000A> \x25 |0
  1123. * <U0085> \x15 |0
  1124. *
  1125. * but OS/390 USS EBCDIC swaps the control codes for LF and NL,
  1126. * mapping
  1127. *
  1128. * <U000A> \x15 |0
  1129. * <U0085> \x25 |0
  1130. *
  1131. * This code modifies a loaded standard EBCDIC<->Unicode mapping table
  1132. * by copying it into allocated memory and swapping the LF and NL values.
  1133. * It allows to support the same EBCDIC charset in both versions without
  1134. * duplicating the entire installed table.
  1135. */
  1136. /* standard EBCDIC codes */
  1137. #define EBCDIC_LF 0x25
  1138. #define EBCDIC_NL 0x15
  1139. /* standard EBCDIC codes with roundtrip flag as stored in Unicode-to-single-byte tables */
  1140. #define EBCDIC_RT_LF 0xf25
  1141. #define EBCDIC_RT_NL 0xf15
  1142. /* Unicode code points */
  1143. #define U_LF 0x0a
  1144. #define U_NL 0x85
  1145. static UBool
  1146. _EBCDICSwapLFNL(UConverterSharedData *sharedData, UErrorCode *pErrorCode) {
  1147. UConverterMBCSTable *mbcsTable;
  1148. const uint16_t *table, *results;
  1149. const uint8_t *bytes;
  1150. int32_t (*newStateTable)[256];
  1151. uint16_t *newResults;
  1152. uint8_t *p;
  1153. char *name;
  1154. uint32_t stage2Entry;
  1155. uint32_t size, sizeofFromUBytes;
  1156. mbcsTable=&sharedData->mbcs;
  1157. table=mbcsTable->fromUnicodeTable;
  1158. bytes=mbcsTable->fromUnicodeBytes;
  1159. results = reinterpret_cast<const uint16_t*>(bytes);
  1160. /*
  1161. * Check that this is an EBCDIC table with SBCS portion -
  1162. * SBCS or EBCDIC_STATEFUL with standard EBCDIC LF and NL mappings.
  1163. *
  1164. * If not, ignore the option. Options are always ignored if they do not apply.
  1165. */
  1166. if(!(
  1167. (mbcsTable->outputType==MBCS_OUTPUT_1 || mbcsTable->outputType==MBCS_OUTPUT_2_SISO) &&
  1168. mbcsTable->stateTable[0][EBCDIC_LF]==MBCS_ENTRY_FINAL(0, MBCS_STATE_VALID_DIRECT_16, U_LF) &&
  1169. mbcsTable->stateTable[0][EBCDIC_NL]==MBCS_ENTRY_FINAL(0, MBCS_STATE_VALID_DIRECT_16, U_NL)
  1170. )) {
  1171. return false;
  1172. }
  1173. if(mbcsTable->outputType==MBCS_OUTPUT_1) {
  1174. if(!(
  1175. EBCDIC_RT_LF==MBCS_SINGLE_RESULT_FROM_U(table, results, U_LF) &&
  1176. EBCDIC_RT_NL==MBCS_SINGLE_RESULT_FROM_U(table, results, U_NL)
  1177. )) {
  1178. return false;
  1179. }
  1180. } else /* MBCS_OUTPUT_2_SISO */ {
  1181. stage2Entry=MBCS_STAGE_2_FROM_U(table, U_LF);
  1182. if(!(
  1183. MBCS_FROM_U_IS_ROUNDTRIP(stage2Entry, U_LF)!=0 &&
  1184. EBCDIC_LF==MBCS_VALUE_2_FROM_STAGE_2(bytes, stage2Entry, U_LF)
  1185. )) {
  1186. return false;
  1187. }
  1188. stage2Entry=MBCS_STAGE_2_FROM_U(table, U_NL);
  1189. if(!(
  1190. MBCS_FROM_U_IS_ROUNDTRIP(stage2Entry, U_NL)!=0 &&
  1191. EBCDIC_NL==MBCS_VALUE_2_FROM_STAGE_2(bytes, stage2Entry, U_NL)
  1192. )) {
  1193. return false;
  1194. }
  1195. }
  1196. if(mbcsTable->fromUBytesLength>0) {
  1197. /*
  1198. * We _know_ the number of bytes in the fromUnicodeBytes array
  1199. * starting with header.version 4.1.
  1200. */
  1201. sizeofFromUBytes=mbcsTable->fromUBytesLength;
  1202. } else {
  1203. /*
  1204. * Otherwise:
  1205. * There used to be code to enumerate the fromUnicode
  1206. * trie and find the highest entry, but it was removed in ICU 3.2
  1207. * because it was not tested and caused a low code coverage number.
  1208. * See Jitterbug 3674.
  1209. * This affects only some .cnv file formats with a header.version
  1210. * below 4.1, and only when swaplfnl is requested.
  1211. *
  1212. * ucnvmbcs.c revision 1.99 is the last one with the
  1213. * ucnv_MBCSSizeofFromUBytes() function.
  1214. */
  1215. *pErrorCode=U_INVALID_FORMAT_ERROR;
  1216. return false;
  1217. }
  1218. /*
  1219. * The table has an appropriate format.
  1220. * Allocate and build
  1221. * - a modified to-Unicode state table
  1222. * - a modified from-Unicode output array
  1223. * - a converter name string with the swap option appended
  1224. */
  1225. size=
  1226. mbcsTable->countStates*1024+
  1227. sizeofFromUBytes+
  1228. UCNV_MAX_CONVERTER_NAME_LENGTH+20;
  1229. p = static_cast<uint8_t*>(uprv_malloc(size));
  1230. if(p==nullptr) {
  1231. *pErrorCode=U_MEMORY_ALLOCATION_ERROR;
  1232. return false;
  1233. }
  1234. /* copy and modify the to-Unicode state table */
  1235. newStateTable = reinterpret_cast<int32_t(*)[256]>(p);
  1236. uprv_memcpy(newStateTable, mbcsTable->stateTable, mbcsTable->countStates*1024);
  1237. newStateTable[0][EBCDIC_LF]=MBCS_ENTRY_FINAL(0, MBCS_STATE_VALID_DIRECT_16, U_NL);
  1238. newStateTable[0][EBCDIC_NL]=MBCS_ENTRY_FINAL(0, MBCS_STATE_VALID_DIRECT_16, U_LF);
  1239. /* copy and modify the from-Unicode result table */
  1240. newResults = reinterpret_cast<uint16_t*>(newStateTable[mbcsTable->countStates]);
  1241. uprv_memcpy(newResults, bytes, sizeofFromUBytes);
  1242. /* conveniently, the table access macros work on the left side of expressions */
  1243. if(mbcsTable->outputType==MBCS_OUTPUT_1) {
  1244. MBCS_SINGLE_RESULT_FROM_U(table, newResults, U_LF)=EBCDIC_RT_NL;
  1245. MBCS_SINGLE_RESULT_FROM_U(table, newResults, U_NL)=EBCDIC_RT_LF;
  1246. } else /* MBCS_OUTPUT_2_SISO */ {
  1247. stage2Entry=MBCS_STAGE_2_FROM_U(table, U_LF);
  1248. MBCS_VALUE_2_FROM_STAGE_2(newResults, stage2Entry, U_LF)=EBCDIC_NL;
  1249. stage2Entry=MBCS_STAGE_2_FROM_U(table, U_NL);
  1250. MBCS_VALUE_2_FROM_STAGE_2(newResults, stage2Entry, U_NL)=EBCDIC_LF;
  1251. }
  1252. /* set the canonical converter name */
  1253. name = reinterpret_cast<char*>(newResults) + sizeofFromUBytes;
  1254. uprv_strcpy(name, sharedData->staticData->name);
  1255. uprv_strcat(name, UCNV_SWAP_LFNL_OPTION_STRING);
  1256. /* set the pointers */
  1257. icu::umtx_lock(nullptr);
  1258. if(mbcsTable->swapLFNLStateTable==nullptr) {
  1259. mbcsTable->swapLFNLStateTable=newStateTable;
  1260. mbcsTable->swapLFNLFromUnicodeBytes = reinterpret_cast<uint8_t*>(newResults);
  1261. mbcsTable->swapLFNLName=name;
  1262. newStateTable=nullptr;
  1263. }
  1264. icu::umtx_unlock(nullptr);
  1265. /* release the allocated memory if another thread beat us to it */
  1266. if(newStateTable!=nullptr) {
  1267. uprv_free(newStateTable);
  1268. }
  1269. return true;
  1270. }
  1271. /* reconstitute omitted fromUnicode data ------------------------------------ */
  1272. /* for details, compare with genmbcs.c MBCSAddFromUnicode() and transformEUC() */
  1273. static UBool U_CALLCONV
  1274. writeStage3Roundtrip(const void *context, uint32_t value, UChar32 codePoints[32]) {
  1275. UConverterMBCSTable *mbcsTable=(UConverterMBCSTable *)context;
  1276. const uint16_t *table;
  1277. uint32_t *stage2;
  1278. uint8_t *bytes, *p;
  1279. UChar32 c;
  1280. int32_t i, st3;
  1281. table=mbcsTable->fromUnicodeTable;
  1282. bytes = const_cast<uint8_t*>(mbcsTable->fromUnicodeBytes);
  1283. /* for EUC outputTypes, modify the value like genmbcs.c's transformEUC() */
  1284. switch(mbcsTable->outputType) {
  1285. case MBCS_OUTPUT_3_EUC:
  1286. if(value<=0xffff) {
  1287. /* short sequences are stored directly */
  1288. /* code set 0 or 1 */
  1289. } else if(value<=0x8effff) {
  1290. /* code set 2 */
  1291. value&=0x7fff;
  1292. } else /* first byte is 0x8f */ {
  1293. /* code set 3 */
  1294. value&=0xff7f;
  1295. }
  1296. break;
  1297. case MBCS_OUTPUT_4_EUC:
  1298. if(value<=0xffffff) {
  1299. /* short sequences are stored directly */
  1300. /* code set 0 or 1 */
  1301. } else if(value<=0x8effffff) {
  1302. /* code set 2 */
  1303. value&=0x7fffff;
  1304. } else /* first byte is 0x8f */ {
  1305. /* code set 3 */
  1306. value&=0xff7fff;
  1307. }
  1308. break;
  1309. default:
  1310. break;
  1311. }
  1312. for(i=0; i<=0x1f; ++value, ++i) {
  1313. c=codePoints[i];
  1314. if(c<0) {
  1315. continue;
  1316. }
  1317. /* locate the stage 2 & 3 data */
  1318. stage2=((uint32_t *)table)+table[c>>10]+((c>>4)&0x3f);
  1319. p=bytes;
  1320. st3 = static_cast<int32_t>(static_cast<uint16_t>(*stage2)) * 16 + (c & 0xf);
  1321. /* write the codepage bytes into stage 3 */
  1322. switch(mbcsTable->outputType) {
  1323. case MBCS_OUTPUT_3:
  1324. case MBCS_OUTPUT_4_EUC:
  1325. p+=st3*3;
  1326. p[0] = static_cast<uint8_t>(value >> 16);
  1327. p[1] = static_cast<uint8_t>(value >> 8);
  1328. p[2] = static_cast<uint8_t>(value);
  1329. break;
  1330. case MBCS_OUTPUT_4:
  1331. reinterpret_cast<uint32_t*>(p)[st3] = value;
  1332. break;
  1333. default:
  1334. /* 2 bytes per character */
  1335. reinterpret_cast<uint16_t*>(p)[st3] = static_cast<uint16_t>(value);
  1336. break;
  1337. }
  1338. /* set the roundtrip flag */
  1339. *stage2|=(1UL<<(16+(c&0xf)));
  1340. }
  1341. return true;
  1342. }
  1343. static void
  1344. reconstituteData(UConverterMBCSTable *mbcsTable,
  1345. uint32_t stage1Length, uint32_t stage2Length,
  1346. uint32_t fullStage2Length, /* lengths are numbers of units, not bytes */
  1347. UErrorCode *pErrorCode) {
  1348. uint16_t *stage1;
  1349. uint32_t *stage2;
  1350. uint32_t dataLength=stage1Length*2+fullStage2Length*4+mbcsTable->fromUBytesLength;
  1351. mbcsTable->reconstitutedData = static_cast<uint8_t*>(uprv_malloc(dataLength));
  1352. if(mbcsTable->reconstitutedData==nullptr) {
  1353. *pErrorCode=U_MEMORY_ALLOCATION_ERROR;
  1354. return;
  1355. }
  1356. uprv_memset(mbcsTable->reconstitutedData, 0, dataLength);
  1357. /* copy existing data and reroute the pointers */
  1358. stage1 = reinterpret_cast<uint16_t*>(mbcsTable->reconstitutedData);
  1359. uprv_memcpy(stage1, mbcsTable->fromUnicodeTable, stage1Length*2);
  1360. stage2 = reinterpret_cast<uint32_t*>(stage1 + stage1Length);
  1361. uprv_memcpy(stage2+(fullStage2Length-stage2Length),
  1362. mbcsTable->fromUnicodeTable+stage1Length,
  1363. stage2Length*4);
  1364. mbcsTable->fromUnicodeTable=stage1;
  1365. mbcsTable->fromUnicodeBytes = reinterpret_cast<uint8_t*>(stage2 + fullStage2Length);
  1366. /* indexes into stage 2 count from the bottom of the fromUnicodeTable */
  1367. stage2 = reinterpret_cast<uint32_t*>(stage1);
  1368. /* reconstitute the initial part of stage 2 from the mbcsIndex */
  1369. {
  1370. int32_t stageUTF8Length = (static_cast<int32_t>(mbcsTable->maxFastUChar) + 1) >> 6;
  1371. int32_t stageUTF8Index=0;
  1372. int32_t st1, st2, st3, i;
  1373. for(st1=0; stageUTF8Index<stageUTF8Length; ++st1) {
  1374. st2=stage1[st1];
  1375. if (st2 != static_cast<int32_t>(stage1Length) / 2) {
  1376. /* each stage 2 block has 64 entries corresponding to 16 entries in the mbcsIndex */
  1377. for(i=0; i<16; ++i) {
  1378. st3=mbcsTable->mbcsIndex[stageUTF8Index++];
  1379. if(st3!=0) {
  1380. /* an stage 2 entry's index is per stage 3 16-block, not per stage 3 entry */
  1381. st3>>=4;
  1382. /*
  1383. * 4 stage 2 entries point to 4 consecutive stage 3 16-blocks which are
  1384. * allocated together as a single 64-block for access from the mbcsIndex
  1385. */
  1386. stage2[st2++]=st3++;
  1387. stage2[st2++]=st3++;
  1388. stage2[st2++]=st3++;
  1389. stage2[st2++]=st3;
  1390. } else {
  1391. /* no stage 3 block, skip */
  1392. st2+=4;
  1393. }
  1394. }
  1395. } else {
  1396. /* no stage 2 block, skip */
  1397. stageUTF8Index+=16;
  1398. }
  1399. }
  1400. }
  1401. /* reconstitute fromUnicodeBytes with roundtrips from toUnicode data */
  1402. ucnv_MBCSEnumToUnicode(mbcsTable, writeStage3Roundtrip, mbcsTable, pErrorCode);
  1403. }
  1404. /* MBCS setup functions ----------------------------------------------------- */
  1405. static void U_CALLCONV
  1406. ucnv_MBCSLoad(UConverterSharedData *sharedData,
  1407. UConverterLoadArgs *pArgs,
  1408. const uint8_t *raw,
  1409. UErrorCode *pErrorCode) {
  1410. UDataInfo info;
  1411. UConverterMBCSTable *mbcsTable=&sharedData->mbcs;
  1412. _MBCSHeader *header=(_MBCSHeader *)raw;
  1413. uint32_t offset;
  1414. uint32_t headerLength;
  1415. UBool noFromU=false;
  1416. if(header->version[0]==4) {
  1417. headerLength=MBCS_HEADER_V4_LENGTH;
  1418. } else if(header->version[0]==5 && header->version[1]>=3 &&
  1419. (header->options&MBCS_OPT_UNKNOWN_INCOMPATIBLE_MASK)==0) {
  1420. headerLength=header->options&MBCS_OPT_LENGTH_MASK;
  1421. noFromU = static_cast<UBool>((header->options & MBCS_OPT_NO_FROM_U) != 0);
  1422. } else {
  1423. *pErrorCode=U_INVALID_TABLE_FORMAT;
  1424. return;
  1425. }
  1426. mbcsTable->outputType = static_cast<uint8_t>(header->flags);
  1427. if(noFromU && mbcsTable->outputType==MBCS_OUTPUT_1) {
  1428. *pErrorCode=U_INVALID_TABLE_FORMAT;
  1429. return;
  1430. }
  1431. /* extension data, header version 4.2 and higher */
  1432. offset=header->flags>>8;
  1433. if(offset!=0) {
  1434. mbcsTable->extIndexes = reinterpret_cast<const int32_t*>(raw + offset);
  1435. }
  1436. if(mbcsTable->outputType==MBCS_OUTPUT_EXT_ONLY) {
  1437. UConverterLoadArgs args=UCNV_LOAD_ARGS_INITIALIZER;
  1438. UConverterSharedData *baseSharedData;
  1439. const int32_t *extIndexes;
  1440. const char *baseName;
  1441. /* extension-only file, load the base table and set values appropriately */
  1442. if((extIndexes=mbcsTable->extIndexes)==nullptr) {
  1443. /* extension-only file without extension */
  1444. *pErrorCode=U_INVALID_TABLE_FORMAT;
  1445. return;
  1446. }
  1447. if(pArgs->nestedLoads!=1) {
  1448. /* an extension table must not be loaded as a base table */
  1449. *pErrorCode=U_INVALID_TABLE_FILE;
  1450. return;
  1451. }
  1452. /* load the base table */
  1453. baseName = reinterpret_cast<const char*>(header) + headerLength * 4;
  1454. if(0==uprv_strcmp(baseName, sharedData->staticData->name)) {
  1455. /* forbid loading this same extension-only file */
  1456. *pErrorCode=U_INVALID_TABLE_FORMAT;
  1457. return;
  1458. }
  1459. /* TODO parse package name out of the prefix of the base name in the extension .cnv file? */
  1460. args.size=sizeof(UConverterLoadArgs);
  1461. args.nestedLoads=2;
  1462. args.onlyTestIsLoadable=pArgs->onlyTestIsLoadable;
  1463. args.reserved=pArgs->reserved;
  1464. args.options=pArgs->options;
  1465. args.pkg=pArgs->pkg;
  1466. args.name=baseName;
  1467. baseSharedData=ucnv_load(&args, pErrorCode);
  1468. if(U_FAILURE(*pErrorCode)) {
  1469. return;
  1470. }
  1471. if( baseSharedData->staticData->conversionType!=UCNV_MBCS ||
  1472. baseSharedData->mbcs.baseSharedData!=nullptr
  1473. ) {
  1474. ucnv_unload(baseSharedData);
  1475. *pErrorCode=U_INVALID_TABLE_FORMAT;
  1476. return;
  1477. }
  1478. if(pArgs->onlyTestIsLoadable) {
  1479. /*
  1480. * Exit as soon as we know that we can load the converter
  1481. * and the format is valid and supported.
  1482. * The worst that can happen in the following code is a memory
  1483. * allocation error.
  1484. */
  1485. ucnv_unload(baseSharedData);
  1486. return;
  1487. }
  1488. /* copy the base table data */
  1489. uprv_memcpy(mbcsTable, &baseSharedData->mbcs, sizeof(UConverterMBCSTable));
  1490. /* overwrite values with relevant ones for the extension converter */
  1491. mbcsTable->baseSharedData=baseSharedData;
  1492. mbcsTable->extIndexes=extIndexes;
  1493. /*
  1494. * It would be possible to share the swapLFNL data with a base converter,
  1495. * but the generated name would have to be different, and the memory
  1496. * would have to be free'd only once.
  1497. * It is easier to just create the data for the extension converter
  1498. * separately when it is requested.
  1499. */
  1500. mbcsTable->swapLFNLStateTable=nullptr;
  1501. mbcsTable->swapLFNLFromUnicodeBytes=nullptr;
  1502. mbcsTable->swapLFNLName=nullptr;
  1503. /*
  1504. * The reconstitutedData must be deleted only when the base converter
  1505. * is unloaded.
  1506. */
  1507. mbcsTable->reconstitutedData=nullptr;
  1508. /*
  1509. * Set a special, runtime-only outputType if the extension converter
  1510. * is a DBCS version of a base converter that also maps single bytes.
  1511. */
  1512. if( sharedData->staticData->conversionType==UCNV_DBCS ||
  1513. (sharedData->staticData->conversionType==UCNV_MBCS &&
  1514. sharedData->staticData->minBytesPerChar>=2)
  1515. ) {
  1516. if(baseSharedData->mbcs.outputType==MBCS_OUTPUT_2_SISO) {
  1517. /* the base converter is SI/SO-stateful */
  1518. int32_t entry;
  1519. /* get the dbcs state from the state table entry for SO=0x0e */
  1520. entry=mbcsTable->stateTable[0][0xe];
  1521. if( MBCS_ENTRY_IS_FINAL(entry) &&
  1522. MBCS_ENTRY_FINAL_ACTION(entry)==MBCS_STATE_CHANGE_ONLY &&
  1523. MBCS_ENTRY_FINAL_STATE(entry)!=0
  1524. ) {
  1525. mbcsTable->dbcsOnlyState = static_cast<uint8_t>(MBCS_ENTRY_FINAL_STATE(entry));
  1526. mbcsTable->outputType=MBCS_OUTPUT_DBCS_ONLY;
  1527. }
  1528. } else if(
  1529. baseSharedData->staticData->conversionType==UCNV_MBCS &&
  1530. baseSharedData->staticData->minBytesPerChar==1 &&
  1531. baseSharedData->staticData->maxBytesPerChar==2 &&
  1532. mbcsTable->countStates<=127
  1533. ) {
  1534. /* non-stateful base converter, need to modify the state table */
  1535. int32_t (*newStateTable)[256];
  1536. int32_t *state;
  1537. int32_t i, count;
  1538. /* allocate a new state table and copy the base state table contents */
  1539. count=mbcsTable->countStates;
  1540. newStateTable = static_cast<int32_t(*)[256]>(uprv_malloc((count + 1) * 1024));
  1541. if(newStateTable==nullptr) {
  1542. ucnv_unload(baseSharedData);
  1543. *pErrorCode=U_MEMORY_ALLOCATION_ERROR;
  1544. return;
  1545. }
  1546. uprv_memcpy(newStateTable, mbcsTable->stateTable, count*1024);
  1547. /* change all final single-byte entries to go to a new all-illegal state */
  1548. state=newStateTable[0];
  1549. for(i=0; i<256; ++i) {
  1550. if(MBCS_ENTRY_IS_FINAL(state[i])) {
  1551. state[i]=MBCS_ENTRY_TRANSITION(count, 0);
  1552. }
  1553. }
  1554. /* build the new all-illegal state */
  1555. state=newStateTable[count];
  1556. for(i=0; i<256; ++i) {
  1557. state[i]=MBCS_ENTRY_FINAL(0, MBCS_STATE_ILLEGAL, 0);
  1558. }
  1559. mbcsTable->stateTable=(const int32_t (*)[256])newStateTable;
  1560. mbcsTable->countStates = static_cast<uint8_t>(count + 1);
  1561. mbcsTable->stateTableOwned=true;
  1562. mbcsTable->outputType=MBCS_OUTPUT_DBCS_ONLY;
  1563. }
  1564. }
  1565. /*
  1566. * unlike below for files with base tables, do not get the unicodeMask
  1567. * from the sharedData; instead, use the base table's unicodeMask,
  1568. * which we copied in the memcpy above;
  1569. * this is necessary because the static data unicodeMask, especially
  1570. * the UCNV_HAS_SUPPLEMENTARY flag, is part of the base table data
  1571. */
  1572. } else {
  1573. /* conversion file with a base table; an additional extension table is optional */
  1574. /* make sure that the output type is known */
  1575. switch(mbcsTable->outputType) {
  1576. case MBCS_OUTPUT_1:
  1577. case MBCS_OUTPUT_2:
  1578. case MBCS_OUTPUT_3:
  1579. case MBCS_OUTPUT_4:
  1580. case MBCS_OUTPUT_3_EUC:
  1581. case MBCS_OUTPUT_4_EUC:
  1582. case MBCS_OUTPUT_2_SISO:
  1583. /* OK */
  1584. break;
  1585. default:
  1586. *pErrorCode=U_INVALID_TABLE_FORMAT;
  1587. return;
  1588. }
  1589. if(pArgs->onlyTestIsLoadable) {
  1590. /*
  1591. * Exit as soon as we know that we can load the converter
  1592. * and the format is valid and supported.
  1593. * The worst that can happen in the following code is a memory
  1594. * allocation error.
  1595. */
  1596. return;
  1597. }
  1598. mbcsTable->countStates = static_cast<uint8_t>(header->countStates);
  1599. mbcsTable->countToUFallbacks=header->countToUFallbacks;
  1600. mbcsTable->stateTable = reinterpret_cast<const int32_t(*)[256]>(raw + headerLength * 4);
  1601. mbcsTable->toUFallbacks = reinterpret_cast<const _MBCSToUFallback*>(mbcsTable->stateTable + header->countStates);
  1602. mbcsTable->unicodeCodeUnits = reinterpret_cast<const uint16_t*>(raw + header->offsetToUCodeUnits);
  1603. mbcsTable->fromUnicodeTable = reinterpret_cast<const uint16_t*>(raw + header->offsetFromUTable);
  1604. mbcsTable->fromUnicodeBytes = raw + header->offsetFromUBytes;
  1605. mbcsTable->fromUBytesLength=header->fromUBytesLength;
  1606. /*
  1607. * converter versions 6.1 and up contain a unicodeMask that is
  1608. * used here to select the most efficient function implementations
  1609. */
  1610. info.size=sizeof(UDataInfo);
  1611. udata_getInfo((UDataMemory *)sharedData->dataMemory, &info);
  1612. if(info.formatVersion[0]>6 || (info.formatVersion[0]==6 && info.formatVersion[1]>=1)) {
  1613. /* mask off possible future extensions to be safe */
  1614. mbcsTable->unicodeMask = static_cast<uint8_t>(sharedData->staticData->unicodeMask & 3);
  1615. } else {
  1616. /* for older versions, assume worst case: contains anything possible (prevent over-optimizations) */
  1617. mbcsTable->unicodeMask=UCNV_HAS_SUPPLEMENTARY|UCNV_HAS_SURROGATES;
  1618. }
  1619. /*
  1620. * _MBCSHeader.version 4.3 adds utf8Friendly data structures.
  1621. * Check for the header version, SBCS vs. MBCS, and for whether the
  1622. * data structures are optimized for code points as high as what the
  1623. * runtime code is designed for.
  1624. * The implementation does not handle mapping tables with entries for
  1625. * unpaired surrogates.
  1626. */
  1627. if( header->version[1]>=3 &&
  1628. (mbcsTable->unicodeMask&UCNV_HAS_SURROGATES)==0 &&
  1629. (mbcsTable->countStates==1 ?
  1630. (header->version[2]>=(SBCS_FAST_MAX>>8)) :
  1631. (header->version[2]>=(MBCS_FAST_MAX>>8))
  1632. )
  1633. ) {
  1634. mbcsTable->utf8Friendly=true;
  1635. if(mbcsTable->countStates==1) {
  1636. /*
  1637. * SBCS: Stage 3 is allocated in 64-entry blocks for U+0000..SBCS_FAST_MAX or higher.
  1638. * Build a table with indexes to each block, to be used instead of
  1639. * the regular stage 1/2 table.
  1640. */
  1641. int32_t i;
  1642. for(i=0; i<(SBCS_FAST_LIMIT>>6); ++i) {
  1643. mbcsTable->sbcsIndex[i]=mbcsTable->fromUnicodeTable[mbcsTable->fromUnicodeTable[i>>4]+((i<<2)&0x3c)];
  1644. }
  1645. /* set SBCS_FAST_MAX to reflect the reach of sbcsIndex[] even if header->version[2]>(SBCS_FAST_MAX>>8) */
  1646. mbcsTable->maxFastUChar=SBCS_FAST_MAX;
  1647. } else {
  1648. /*
  1649. * MBCS: Stage 3 is allocated in 64-entry blocks for U+0000..MBCS_FAST_MAX or higher.
  1650. * The .cnv file is prebuilt with an additional stage table with indexes
  1651. * to each block.
  1652. */
  1653. mbcsTable->mbcsIndex = reinterpret_cast<const uint16_t*>(
  1654. mbcsTable->fromUnicodeBytes +
  1655. (noFromU ? 0 : mbcsTable->fromUBytesLength));
  1656. mbcsTable->maxFastUChar = (static_cast<char16_t>(header->version[2]) << 8) | 0xff;
  1657. }
  1658. }
  1659. /* calculate a bit set of 4 ASCII characters per bit that round-trip to ASCII bytes */
  1660. {
  1661. uint32_t asciiRoundtrips=0xffffffff;
  1662. int32_t i;
  1663. for(i=0; i<0x80; ++i) {
  1664. if(mbcsTable->stateTable[0][i]!=MBCS_ENTRY_FINAL(0, MBCS_STATE_VALID_DIRECT_16, i)) {
  1665. asciiRoundtrips &= ~(static_cast<uint32_t>(1) << (i >> 2));
  1666. }
  1667. }
  1668. mbcsTable->asciiRoundtrips=asciiRoundtrips;
  1669. }
  1670. if(noFromU) {
  1671. uint32_t stage1Length=
  1672. mbcsTable->unicodeMask&UCNV_HAS_SUPPLEMENTARY ?
  1673. 0x440 : 0x40;
  1674. uint32_t stage2Length=
  1675. (header->offsetFromUBytes-header->offsetFromUTable)/4-
  1676. stage1Length/2;
  1677. reconstituteData(mbcsTable, stage1Length, stage2Length, header->fullStage2Length, pErrorCode);
  1678. }
  1679. }
  1680. /* Set the impl pointer here so that it is set for both extension-only and base tables. */
  1681. if(mbcsTable->utf8Friendly) {
  1682. if(mbcsTable->countStates==1) {
  1683. sharedData->impl=&_SBCSUTF8Impl;
  1684. } else {
  1685. if(mbcsTable->outputType==MBCS_OUTPUT_2) {
  1686. sharedData->impl=&_DBCSUTF8Impl;
  1687. }
  1688. }
  1689. }
  1690. if(mbcsTable->outputType==MBCS_OUTPUT_DBCS_ONLY || mbcsTable->outputType==MBCS_OUTPUT_2_SISO) {
  1691. /*
  1692. * MBCS_OUTPUT_DBCS_ONLY: No SBCS mappings, therefore ASCII does not roundtrip.
  1693. * MBCS_OUTPUT_2_SISO: Bypass the ASCII fastpath to handle prevLength correctly.
  1694. */
  1695. mbcsTable->asciiRoundtrips=0;
  1696. }
  1697. }
  1698. static void U_CALLCONV
  1699. ucnv_MBCSUnload(UConverterSharedData *sharedData) {
  1700. UConverterMBCSTable *mbcsTable=&sharedData->mbcs;
  1701. if(mbcsTable->swapLFNLStateTable!=nullptr) {
  1702. uprv_free(mbcsTable->swapLFNLStateTable);
  1703. }
  1704. if(mbcsTable->stateTableOwned) {
  1705. uprv_free((void *)mbcsTable->stateTable);
  1706. }
  1707. if(mbcsTable->baseSharedData!=nullptr) {
  1708. ucnv_unload(mbcsTable->baseSharedData);
  1709. }
  1710. if(mbcsTable->reconstitutedData!=nullptr) {
  1711. uprv_free(mbcsTable->reconstitutedData);
  1712. }
  1713. }
  1714. static void U_CALLCONV
  1715. ucnv_MBCSOpen(UConverter *cnv,
  1716. UConverterLoadArgs *pArgs,
  1717. UErrorCode *pErrorCode) {
  1718. UConverterMBCSTable *mbcsTable;
  1719. const int32_t *extIndexes;
  1720. uint8_t outputType;
  1721. int8_t maxBytesPerUChar;
  1722. if(pArgs->onlyTestIsLoadable) {
  1723. return;
  1724. }
  1725. mbcsTable=&cnv->sharedData->mbcs;
  1726. outputType=mbcsTable->outputType;
  1727. if(outputType==MBCS_OUTPUT_DBCS_ONLY) {
  1728. /* the swaplfnl option does not apply, remove it */
  1729. cnv->options=pArgs->options&=~UCNV_OPTION_SWAP_LFNL;
  1730. }
  1731. if((pArgs->options&UCNV_OPTION_SWAP_LFNL)!=0) {
  1732. /* do this because double-checked locking is broken */
  1733. UBool isCached;
  1734. icu::umtx_lock(nullptr);
  1735. isCached=mbcsTable->swapLFNLStateTable!=nullptr;
  1736. icu::umtx_unlock(nullptr);
  1737. if(!isCached) {
  1738. if(!_EBCDICSwapLFNL(cnv->sharedData, pErrorCode)) {
  1739. if(U_FAILURE(*pErrorCode)) {
  1740. return; /* something went wrong */
  1741. }
  1742. /* the option does not apply, remove it */
  1743. cnv->options=pArgs->options&=~UCNV_OPTION_SWAP_LFNL;
  1744. }
  1745. }
  1746. }
  1747. if(uprv_strstr(pArgs->name, "18030")!=nullptr) {
  1748. if(uprv_strstr(pArgs->name, "gb18030")!=nullptr || uprv_strstr(pArgs->name, "GB18030")!=nullptr) {
  1749. /* set a flag for GB 18030 mode, which changes the callback behavior */
  1750. cnv->options|=_MBCS_OPTION_GB18030;
  1751. }
  1752. } else if((uprv_strstr(pArgs->name, "KEIS")!=nullptr) || (uprv_strstr(pArgs->name, "keis")!=nullptr)) {
  1753. /* set a flag for KEIS converter, which changes the SI/SO character sequence */
  1754. cnv->options|=_MBCS_OPTION_KEIS;
  1755. } else if((uprv_strstr(pArgs->name, "JEF")!=nullptr) || (uprv_strstr(pArgs->name, "jef")!=nullptr)) {
  1756. /* set a flag for JEF converter, which changes the SI/SO character sequence */
  1757. cnv->options|=_MBCS_OPTION_JEF;
  1758. } else if((uprv_strstr(pArgs->name, "JIPS")!=nullptr) || (uprv_strstr(pArgs->name, "jips")!=nullptr)) {
  1759. /* set a flag for JIPS converter, which changes the SI/SO character sequence */
  1760. cnv->options|=_MBCS_OPTION_JIPS;
  1761. }
  1762. /* fix maxBytesPerUChar depending on outputType and options etc. */
  1763. if(outputType==MBCS_OUTPUT_2_SISO) {
  1764. cnv->maxBytesPerUChar=3; /* SO+DBCS */
  1765. }
  1766. extIndexes=mbcsTable->extIndexes;
  1767. if(extIndexes!=nullptr) {
  1768. maxBytesPerUChar = static_cast<int8_t>(UCNV_GET_MAX_BYTES_PER_UCHAR(extIndexes));
  1769. if(outputType==MBCS_OUTPUT_2_SISO) {
  1770. ++maxBytesPerUChar; /* SO + multiple DBCS */
  1771. }
  1772. if(maxBytesPerUChar>cnv->maxBytesPerUChar) {
  1773. cnv->maxBytesPerUChar=maxBytesPerUChar;
  1774. }
  1775. }
  1776. #if 0
  1777. /*
  1778. * documentation of UConverter fields used for status
  1779. * all of these fields are (re)set to 0 by ucnv_bld.c and ucnv_reset()
  1780. */
  1781. /* toUnicode */
  1782. cnv->toUnicodeStatus=0; /* offset */
  1783. cnv->mode=0; /* state */
  1784. cnv->toULength=0; /* byteIndex */
  1785. /* fromUnicode */
  1786. cnv->fromUChar32=0;
  1787. cnv->fromUnicodeStatus=1; /* prevLength */
  1788. #endif
  1789. }
  1790. U_CDECL_BEGIN
  1791. static const char* U_CALLCONV
  1792. ucnv_MBCSGetName(const UConverter *cnv) {
  1793. if((cnv->options&UCNV_OPTION_SWAP_LFNL)!=0 && cnv->sharedData->mbcs.swapLFNLName!=nullptr) {
  1794. return cnv->sharedData->mbcs.swapLFNLName;
  1795. } else {
  1796. return cnv->sharedData->staticData->name;
  1797. }
  1798. }
  1799. U_CDECL_END
  1800. /* MBCS-to-Unicode conversion functions ------------------------------------- */
  1801. static UChar32 U_CALLCONV
  1802. ucnv_MBCSGetFallback(UConverterMBCSTable *mbcsTable, uint32_t offset) {
  1803. const _MBCSToUFallback *toUFallbacks;
  1804. uint32_t i, start, limit;
  1805. limit=mbcsTable->countToUFallbacks;
  1806. if(limit>0) {
  1807. /* do a binary search for the fallback mapping */
  1808. toUFallbacks=mbcsTable->toUFallbacks;
  1809. start=0;
  1810. while(start<limit-1) {
  1811. i=(start+limit)/2;
  1812. if(offset<toUFallbacks[i].offset) {
  1813. limit=i;
  1814. } else {
  1815. start=i;
  1816. }
  1817. }
  1818. /* did we really find it? */
  1819. if(offset==toUFallbacks[start].offset) {
  1820. return toUFallbacks[start].codePoint;
  1821. }
  1822. }
  1823. return 0xfffe;
  1824. }
  1825. /* This version of ucnv_MBCSToUnicodeWithOffsets() is optimized for single-byte, single-state codepages. */
  1826. static void
  1827. ucnv_MBCSSingleToUnicodeWithOffsets(UConverterToUnicodeArgs *pArgs,
  1828. UErrorCode *pErrorCode) {
  1829. UConverter *cnv;
  1830. const uint8_t *source, *sourceLimit;
  1831. char16_t *target;
  1832. const char16_t *targetLimit;
  1833. int32_t *offsets;
  1834. const int32_t (*stateTable)[256];
  1835. int32_t sourceIndex;
  1836. int32_t entry;
  1837. char16_t c;
  1838. uint8_t action;
  1839. /* set up the local pointers */
  1840. cnv=pArgs->converter;
  1841. source = reinterpret_cast<const uint8_t*>(pArgs->source);
  1842. sourceLimit = reinterpret_cast<const uint8_t*>(pArgs->sourceLimit);
  1843. target=pArgs->target;
  1844. targetLimit=pArgs->targetLimit;
  1845. offsets=pArgs->offsets;
  1846. if((cnv->options&UCNV_OPTION_SWAP_LFNL)!=0) {
  1847. stateTable=(const int32_t (*)[256])cnv->sharedData->mbcs.swapLFNLStateTable;
  1848. } else {
  1849. stateTable=cnv->sharedData->mbcs.stateTable;
  1850. }
  1851. /* sourceIndex=-1 if the current character began in the previous buffer */
  1852. sourceIndex=0;
  1853. /* conversion loop */
  1854. while(source<sourceLimit) {
  1855. /*
  1856. * This following test is to see if available input would overflow the output.
  1857. * It does not catch output of more than one code unit that
  1858. * overflows as a result of a surrogate pair or callback output
  1859. * from the last source byte.
  1860. * Therefore, those situations also test for overflows and will
  1861. * then break the loop, too.
  1862. */
  1863. if(target>=targetLimit) {
  1864. /* target is full */
  1865. *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
  1866. break;
  1867. }
  1868. entry=stateTable[0][*source++];
  1869. /* MBCS_ENTRY_IS_FINAL(entry) */
  1870. /* test the most common case first */
  1871. if(MBCS_ENTRY_FINAL_IS_VALID_DIRECT_16(entry)) {
  1872. /* output BMP code point */
  1873. *target++ = static_cast<char16_t>(MBCS_ENTRY_FINAL_VALUE_16(entry));
  1874. if(offsets!=nullptr) {
  1875. *offsets++=sourceIndex;
  1876. }
  1877. /* normal end of action codes: prepare for a new character */
  1878. ++sourceIndex;
  1879. continue;
  1880. }
  1881. /*
  1882. * An if-else-if chain provides more reliable performance for
  1883. * the most common cases compared to a switch.
  1884. */
  1885. action = static_cast<uint8_t>(MBCS_ENTRY_FINAL_ACTION(entry));
  1886. if(action==MBCS_STATE_VALID_DIRECT_20 ||
  1887. (action==MBCS_STATE_FALLBACK_DIRECT_20 && UCNV_TO_U_USE_FALLBACK(cnv))
  1888. ) {
  1889. entry=MBCS_ENTRY_FINAL_VALUE(entry);
  1890. /* output surrogate pair */
  1891. *target++ = static_cast<char16_t>(0xd800 | static_cast<char16_t>(entry >> 10));
  1892. if(offsets!=nullptr) {
  1893. *offsets++=sourceIndex;
  1894. }
  1895. c = static_cast<char16_t>(0xdc00 | static_cast<char16_t>(entry & 0x3ff));
  1896. if(target<targetLimit) {
  1897. *target++=c;
  1898. if(offsets!=nullptr) {
  1899. *offsets++=sourceIndex;
  1900. }
  1901. } else {
  1902. /* target overflow */
  1903. cnv->UCharErrorBuffer[0]=c;
  1904. cnv->UCharErrorBufferLength=1;
  1905. *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
  1906. break;
  1907. }
  1908. ++sourceIndex;
  1909. continue;
  1910. } else if(action==MBCS_STATE_FALLBACK_DIRECT_16) {
  1911. if(UCNV_TO_U_USE_FALLBACK(cnv)) {
  1912. /* output BMP code point */
  1913. *target++ = static_cast<char16_t>(MBCS_ENTRY_FINAL_VALUE_16(entry));
  1914. if(offsets!=nullptr) {
  1915. *offsets++=sourceIndex;
  1916. }
  1917. ++sourceIndex;
  1918. continue;
  1919. }
  1920. } else if(action==MBCS_STATE_UNASSIGNED) {
  1921. /* just fall through */
  1922. } else if(action==MBCS_STATE_ILLEGAL) {
  1923. /* callback(illegal) */
  1924. *pErrorCode=U_ILLEGAL_CHAR_FOUND;
  1925. } else {
  1926. /* reserved, must never occur */
  1927. ++sourceIndex;
  1928. continue;
  1929. }
  1930. if(U_FAILURE(*pErrorCode)) {
  1931. /* callback(illegal) */
  1932. break;
  1933. } else /* unassigned sequences indicated with byteIndex>0 */ {
  1934. /* try an extension mapping */
  1935. pArgs->source = reinterpret_cast<const char*>(source);
  1936. cnv->toUBytes[0]=*(source-1);
  1937. cnv->toULength=_extToU(cnv, cnv->sharedData,
  1938. 1, &source, sourceLimit,
  1939. &target, targetLimit,
  1940. &offsets, sourceIndex,
  1941. pArgs->flush,
  1942. pErrorCode);
  1943. sourceIndex += 1 + static_cast<int32_t>(source - reinterpret_cast<const uint8_t*>(pArgs->source));
  1944. if(U_FAILURE(*pErrorCode)) {
  1945. /* not mappable or buffer overflow */
  1946. break;
  1947. }
  1948. }
  1949. }
  1950. /* write back the updated pointers */
  1951. pArgs->source = reinterpret_cast<const char*>(source);
  1952. pArgs->target=target;
  1953. pArgs->offsets=offsets;
  1954. }
  1955. /*
  1956. * This version of ucnv_MBCSSingleToUnicodeWithOffsets() is optimized for single-byte, single-state codepages
  1957. * that only map to and from the BMP.
  1958. * In addition to single-byte optimizations, the offset calculations
  1959. * become much easier.
  1960. */
  1961. static void
  1962. ucnv_MBCSSingleToBMPWithOffsets(UConverterToUnicodeArgs *pArgs,
  1963. UErrorCode *pErrorCode) {
  1964. UConverter *cnv;
  1965. const uint8_t *source, *sourceLimit, *lastSource;
  1966. char16_t *target;
  1967. int32_t targetCapacity, length;
  1968. int32_t *offsets;
  1969. const int32_t (*stateTable)[256];
  1970. int32_t sourceIndex;
  1971. int32_t entry;
  1972. uint8_t action;
  1973. /* set up the local pointers */
  1974. cnv=pArgs->converter;
  1975. source = reinterpret_cast<const uint8_t*>(pArgs->source);
  1976. sourceLimit = reinterpret_cast<const uint8_t*>(pArgs->sourceLimit);
  1977. target=pArgs->target;
  1978. targetCapacity = static_cast<int32_t>(pArgs->targetLimit - pArgs->target);
  1979. offsets=pArgs->offsets;
  1980. if((cnv->options&UCNV_OPTION_SWAP_LFNL)!=0) {
  1981. stateTable=(const int32_t (*)[256])cnv->sharedData->mbcs.swapLFNLStateTable;
  1982. } else {
  1983. stateTable=cnv->sharedData->mbcs.stateTable;
  1984. }
  1985. /* sourceIndex=-1 if the current character began in the previous buffer */
  1986. sourceIndex=0;
  1987. lastSource=source;
  1988. /*
  1989. * since the conversion here is 1:1 char16_t:uint8_t, we need only one counter
  1990. * for the minimum of the sourceLength and targetCapacity
  1991. */
  1992. length = static_cast<int32_t>(sourceLimit - source);
  1993. if(length<targetCapacity) {
  1994. targetCapacity=length;
  1995. }
  1996. #if MBCS_UNROLL_SINGLE_TO_BMP
  1997. /* unrolling makes it faster on Pentium III/Windows 2000 */
  1998. /* unroll the loop with the most common case */
  1999. unrolled:
  2000. if(targetCapacity>=16) {
  2001. int32_t count, loops, oredEntries;
  2002. loops=count=targetCapacity>>4;
  2003. do {
  2004. oredEntries=entry=stateTable[0][*source++];
  2005. *target++ = static_cast<char16_t>(MBCS_ENTRY_FINAL_VALUE_16(entry));
  2006. oredEntries|=entry=stateTable[0][*source++];
  2007. *target++ = static_cast<char16_t>(MBCS_ENTRY_FINAL_VALUE_16(entry));
  2008. oredEntries|=entry=stateTable[0][*source++];
  2009. *target++ = static_cast<char16_t>(MBCS_ENTRY_FINAL_VALUE_16(entry));
  2010. oredEntries|=entry=stateTable[0][*source++];
  2011. *target++ = static_cast<char16_t>(MBCS_ENTRY_FINAL_VALUE_16(entry));
  2012. oredEntries|=entry=stateTable[0][*source++];
  2013. *target++ = static_cast<char16_t>(MBCS_ENTRY_FINAL_VALUE_16(entry));
  2014. oredEntries|=entry=stateTable[0][*source++];
  2015. *target++ = static_cast<char16_t>(MBCS_ENTRY_FINAL_VALUE_16(entry));
  2016. oredEntries|=entry=stateTable[0][*source++];
  2017. *target++ = static_cast<char16_t>(MBCS_ENTRY_FINAL_VALUE_16(entry));
  2018. oredEntries|=entry=stateTable[0][*source++];
  2019. *target++ = static_cast<char16_t>(MBCS_ENTRY_FINAL_VALUE_16(entry));
  2020. oredEntries|=entry=stateTable[0][*source++];
  2021. *target++ = static_cast<char16_t>(MBCS_ENTRY_FINAL_VALUE_16(entry));
  2022. oredEntries|=entry=stateTable[0][*source++];
  2023. *target++ = static_cast<char16_t>(MBCS_ENTRY_FINAL_VALUE_16(entry));
  2024. oredEntries|=entry=stateTable[0][*source++];
  2025. *target++ = static_cast<char16_t>(MBCS_ENTRY_FINAL_VALUE_16(entry));
  2026. oredEntries|=entry=stateTable[0][*source++];
  2027. *target++ = static_cast<char16_t>(MBCS_ENTRY_FINAL_VALUE_16(entry));
  2028. oredEntries|=entry=stateTable[0][*source++];
  2029. *target++ = static_cast<char16_t>(MBCS_ENTRY_FINAL_VALUE_16(entry));
  2030. oredEntries|=entry=stateTable[0][*source++];
  2031. *target++ = static_cast<char16_t>(MBCS_ENTRY_FINAL_VALUE_16(entry));
  2032. oredEntries|=entry=stateTable[0][*source++];
  2033. *target++ = static_cast<char16_t>(MBCS_ENTRY_FINAL_VALUE_16(entry));
  2034. oredEntries|=entry=stateTable[0][*source++];
  2035. *target++ = static_cast<char16_t>(MBCS_ENTRY_FINAL_VALUE_16(entry));
  2036. /* were all 16 entries really valid? */
  2037. if(!MBCS_ENTRY_FINAL_IS_VALID_DIRECT_16(oredEntries)) {
  2038. /* no, return to the first of these 16 */
  2039. source-=16;
  2040. target-=16;
  2041. break;
  2042. }
  2043. } while(--count>0);
  2044. count=loops-count;
  2045. targetCapacity-=16*count;
  2046. if(offsets!=nullptr) {
  2047. lastSource+=16*count;
  2048. while(count>0) {
  2049. *offsets++=sourceIndex++;
  2050. *offsets++=sourceIndex++;
  2051. *offsets++=sourceIndex++;
  2052. *offsets++=sourceIndex++;
  2053. *offsets++=sourceIndex++;
  2054. *offsets++=sourceIndex++;
  2055. *offsets++=sourceIndex++;
  2056. *offsets++=sourceIndex++;
  2057. *offsets++=sourceIndex++;
  2058. *offsets++=sourceIndex++;
  2059. *offsets++=sourceIndex++;
  2060. *offsets++=sourceIndex++;
  2061. *offsets++=sourceIndex++;
  2062. *offsets++=sourceIndex++;
  2063. *offsets++=sourceIndex++;
  2064. *offsets++=sourceIndex++;
  2065. --count;
  2066. }
  2067. }
  2068. }
  2069. #endif
  2070. /* conversion loop */
  2071. while(targetCapacity > 0 && source < sourceLimit) {
  2072. entry=stateTable[0][*source++];
  2073. /* MBCS_ENTRY_IS_FINAL(entry) */
  2074. /* test the most common case first */
  2075. if(MBCS_ENTRY_FINAL_IS_VALID_DIRECT_16(entry)) {
  2076. /* output BMP code point */
  2077. *target++ = static_cast<char16_t>(MBCS_ENTRY_FINAL_VALUE_16(entry));
  2078. --targetCapacity;
  2079. continue;
  2080. }
  2081. /*
  2082. * An if-else-if chain provides more reliable performance for
  2083. * the most common cases compared to a switch.
  2084. */
  2085. action = static_cast<uint8_t>(MBCS_ENTRY_FINAL_ACTION(entry));
  2086. if(action==MBCS_STATE_FALLBACK_DIRECT_16) {
  2087. if(UCNV_TO_U_USE_FALLBACK(cnv)) {
  2088. /* output BMP code point */
  2089. *target++ = static_cast<char16_t>(MBCS_ENTRY_FINAL_VALUE_16(entry));
  2090. --targetCapacity;
  2091. continue;
  2092. }
  2093. } else if(action==MBCS_STATE_UNASSIGNED) {
  2094. /* just fall through */
  2095. } else if(action==MBCS_STATE_ILLEGAL) {
  2096. /* callback(illegal) */
  2097. *pErrorCode=U_ILLEGAL_CHAR_FOUND;
  2098. } else {
  2099. /* reserved, must never occur */
  2100. continue;
  2101. }
  2102. /* set offsets since the start or the last extension */
  2103. if(offsets!=nullptr) {
  2104. int32_t count = static_cast<int32_t>(source - lastSource);
  2105. /* predecrement: do not set the offset for the callback-causing character */
  2106. while(--count>0) {
  2107. *offsets++=sourceIndex++;
  2108. }
  2109. /* offset and sourceIndex are now set for the current character */
  2110. }
  2111. if(U_FAILURE(*pErrorCode)) {
  2112. /* callback(illegal) */
  2113. break;
  2114. } else /* unassigned sequences indicated with byteIndex>0 */ {
  2115. /* try an extension mapping */
  2116. lastSource=source;
  2117. cnv->toUBytes[0]=*(source-1);
  2118. cnv->toULength=_extToU(cnv, cnv->sharedData,
  2119. 1, &source, sourceLimit,
  2120. &target, pArgs->targetLimit,
  2121. &offsets, sourceIndex,
  2122. pArgs->flush,
  2123. pErrorCode);
  2124. sourceIndex += 1 + static_cast<int32_t>(source - lastSource);
  2125. if(U_FAILURE(*pErrorCode)) {
  2126. /* not mappable or buffer overflow */
  2127. break;
  2128. }
  2129. /* recalculate the targetCapacity after an extension mapping */
  2130. targetCapacity = static_cast<int32_t>(pArgs->targetLimit - target);
  2131. length = static_cast<int32_t>(sourceLimit - source);
  2132. if(length<targetCapacity) {
  2133. targetCapacity=length;
  2134. }
  2135. }
  2136. #if MBCS_UNROLL_SINGLE_TO_BMP
  2137. /* unrolling makes it faster on Pentium III/Windows 2000 */
  2138. goto unrolled;
  2139. #endif
  2140. }
  2141. if(U_SUCCESS(*pErrorCode) && source<sourceLimit && target>=pArgs->targetLimit) {
  2142. /* target is full */
  2143. *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
  2144. }
  2145. /* set offsets since the start or the last callback */
  2146. if(offsets!=nullptr) {
  2147. size_t count=source-lastSource;
  2148. while(count>0) {
  2149. *offsets++=sourceIndex++;
  2150. --count;
  2151. }
  2152. }
  2153. /* write back the updated pointers */
  2154. pArgs->source = reinterpret_cast<const char*>(source);
  2155. pArgs->target=target;
  2156. pArgs->offsets=offsets;
  2157. }
  2158. static UBool
  2159. hasValidTrailBytes(const int32_t (*stateTable)[256], uint8_t state) {
  2160. const int32_t *row=stateTable[state];
  2161. int32_t b, entry;
  2162. /* First test for final entries in this state for some commonly valid byte values. */
  2163. entry=row[0xa1];
  2164. if( !MBCS_ENTRY_IS_TRANSITION(entry) &&
  2165. MBCS_ENTRY_FINAL_ACTION(entry)!=MBCS_STATE_ILLEGAL
  2166. ) {
  2167. return true;
  2168. }
  2169. entry=row[0x41];
  2170. if( !MBCS_ENTRY_IS_TRANSITION(entry) &&
  2171. MBCS_ENTRY_FINAL_ACTION(entry)!=MBCS_STATE_ILLEGAL
  2172. ) {
  2173. return true;
  2174. }
  2175. /* Then test for final entries in this state. */
  2176. for(b=0; b<=0xff; ++b) {
  2177. entry=row[b];
  2178. if( !MBCS_ENTRY_IS_TRANSITION(entry) &&
  2179. MBCS_ENTRY_FINAL_ACTION(entry)!=MBCS_STATE_ILLEGAL
  2180. ) {
  2181. return true;
  2182. }
  2183. }
  2184. /* Then recurse for transition entries. */
  2185. for(b=0; b<=0xff; ++b) {
  2186. entry=row[b];
  2187. if( MBCS_ENTRY_IS_TRANSITION(entry) &&
  2188. hasValidTrailBytes(stateTable, static_cast<uint8_t>(MBCS_ENTRY_TRANSITION_STATE(entry)))
  2189. ) {
  2190. return true;
  2191. }
  2192. }
  2193. return false;
  2194. }
  2195. /*
  2196. * Is byte b a single/lead byte in this state?
  2197. * Recurse for transition states, because here we don't want to say that
  2198. * b is a lead byte if all byte sequences that start with b are illegal.
  2199. */
  2200. static UBool
  2201. isSingleOrLead(const int32_t (*stateTable)[256], uint8_t state, UBool isDBCSOnly, uint8_t b) {
  2202. const int32_t *row=stateTable[state];
  2203. int32_t entry=row[b];
  2204. if(MBCS_ENTRY_IS_TRANSITION(entry)) { /* lead byte */
  2205. return hasValidTrailBytes(stateTable, static_cast<uint8_t>(MBCS_ENTRY_TRANSITION_STATE(entry)));
  2206. } else {
  2207. uint8_t action = static_cast<uint8_t>(MBCS_ENTRY_FINAL_ACTION(entry));
  2208. if(action==MBCS_STATE_CHANGE_ONLY && isDBCSOnly) {
  2209. return false; /* SI/SO are illegal for DBCS-only conversion */
  2210. } else {
  2211. return action!=MBCS_STATE_ILLEGAL;
  2212. }
  2213. }
  2214. }
  2215. U_CFUNC void
  2216. ucnv_MBCSToUnicodeWithOffsets(UConverterToUnicodeArgs *pArgs,
  2217. UErrorCode *pErrorCode) {
  2218. UConverter *cnv;
  2219. const uint8_t *source, *sourceLimit;
  2220. char16_t *target;
  2221. const char16_t *targetLimit;
  2222. int32_t *offsets;
  2223. const int32_t (*stateTable)[256];
  2224. const uint16_t *unicodeCodeUnits;
  2225. uint32_t offset;
  2226. uint8_t state;
  2227. int8_t byteIndex;
  2228. uint8_t *bytes;
  2229. int32_t sourceIndex, nextSourceIndex;
  2230. int32_t entry;
  2231. char16_t c;
  2232. uint8_t action;
  2233. /* use optimized function if possible */
  2234. cnv=pArgs->converter;
  2235. if(cnv->preToULength>0) {
  2236. /*
  2237. * pass sourceIndex=-1 because we continue from an earlier buffer
  2238. * in the future, this may change with continuous offsets
  2239. */
  2240. ucnv_extContinueMatchToU(cnv, pArgs, -1, pErrorCode);
  2241. if(U_FAILURE(*pErrorCode) || cnv->preToULength<0) {
  2242. return;
  2243. }
  2244. }
  2245. if(cnv->sharedData->mbcs.countStates==1) {
  2246. if(!(cnv->sharedData->mbcs.unicodeMask&UCNV_HAS_SUPPLEMENTARY)) {
  2247. ucnv_MBCSSingleToBMPWithOffsets(pArgs, pErrorCode);
  2248. } else {
  2249. ucnv_MBCSSingleToUnicodeWithOffsets(pArgs, pErrorCode);
  2250. }
  2251. return;
  2252. }
  2253. /* set up the local pointers */
  2254. source=(const uint8_t *)pArgs->source;
  2255. sourceLimit=(const uint8_t *)pArgs->sourceLimit;
  2256. target=pArgs->target;
  2257. targetLimit=pArgs->targetLimit;
  2258. offsets=pArgs->offsets;
  2259. if((cnv->options&UCNV_OPTION_SWAP_LFNL)!=0) {
  2260. stateTable=(const int32_t (*)[256])cnv->sharedData->mbcs.swapLFNLStateTable;
  2261. } else {
  2262. stateTable=cnv->sharedData->mbcs.stateTable;
  2263. }
  2264. unicodeCodeUnits=cnv->sharedData->mbcs.unicodeCodeUnits;
  2265. /* get the converter state from UConverter */
  2266. offset=cnv->toUnicodeStatus;
  2267. byteIndex=cnv->toULength;
  2268. bytes=cnv->toUBytes;
  2269. /*
  2270. * if we are in the SBCS state for a DBCS-only converter,
  2271. * then load the DBCS state from the MBCS data
  2272. * (dbcsOnlyState==0 if it is not a DBCS-only converter)
  2273. */
  2274. if((state=(uint8_t)(cnv->mode))==0) {
  2275. state=cnv->sharedData->mbcs.dbcsOnlyState;
  2276. }
  2277. /* sourceIndex=-1 if the current character began in the previous buffer */
  2278. sourceIndex=byteIndex==0 ? 0 : -1;
  2279. nextSourceIndex=0;
  2280. /* conversion loop */
  2281. while(source<sourceLimit) {
  2282. /*
  2283. * This following test is to see if available input would overflow the output.
  2284. * It does not catch output of more than one code unit that
  2285. * overflows as a result of a surrogate pair or callback output
  2286. * from the last source byte.
  2287. * Therefore, those situations also test for overflows and will
  2288. * then break the loop, too.
  2289. */
  2290. if(target>=targetLimit) {
  2291. /* target is full */
  2292. *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
  2293. break;
  2294. }
  2295. if(byteIndex==0) {
  2296. /* optimized loop for 1/2-byte input and BMP output */
  2297. if(offsets==nullptr) {
  2298. do {
  2299. entry=stateTable[state][*source];
  2300. if(MBCS_ENTRY_IS_TRANSITION(entry)) {
  2301. state=(uint8_t)MBCS_ENTRY_TRANSITION_STATE(entry);
  2302. offset=MBCS_ENTRY_TRANSITION_OFFSET(entry);
  2303. ++source;
  2304. if( source<sourceLimit &&
  2305. MBCS_ENTRY_IS_FINAL(entry=stateTable[state][*source]) &&
  2306. MBCS_ENTRY_FINAL_ACTION(entry)==MBCS_STATE_VALID_16 &&
  2307. (c=unicodeCodeUnits[offset+MBCS_ENTRY_FINAL_VALUE_16(entry)])<0xfffe
  2308. ) {
  2309. ++source;
  2310. *target++=c;
  2311. state=(uint8_t)MBCS_ENTRY_FINAL_STATE(entry); /* typically 0 */
  2312. offset=0;
  2313. } else {
  2314. /* set the state and leave the optimized loop */
  2315. bytes[0]=*(source-1);
  2316. byteIndex=1;
  2317. break;
  2318. }
  2319. } else {
  2320. if(MBCS_ENTRY_FINAL_IS_VALID_DIRECT_16(entry)) {
  2321. /* output BMP code point */
  2322. ++source;
  2323. *target++=(char16_t)MBCS_ENTRY_FINAL_VALUE_16(entry);
  2324. state=(uint8_t)MBCS_ENTRY_FINAL_STATE(entry); /* typically 0 */
  2325. } else {
  2326. /* leave the optimized loop */
  2327. break;
  2328. }
  2329. }
  2330. } while(source<sourceLimit && target<targetLimit);
  2331. } else /* offsets!=nullptr */ {
  2332. do {
  2333. entry=stateTable[state][*source];
  2334. if(MBCS_ENTRY_IS_TRANSITION(entry)) {
  2335. state=(uint8_t)MBCS_ENTRY_TRANSITION_STATE(entry);
  2336. offset=MBCS_ENTRY_TRANSITION_OFFSET(entry);
  2337. ++source;
  2338. if( source<sourceLimit &&
  2339. MBCS_ENTRY_IS_FINAL(entry=stateTable[state][*source]) &&
  2340. MBCS_ENTRY_FINAL_ACTION(entry)==MBCS_STATE_VALID_16 &&
  2341. (c=unicodeCodeUnits[offset+MBCS_ENTRY_FINAL_VALUE_16(entry)])<0xfffe
  2342. ) {
  2343. ++source;
  2344. *target++=c;
  2345. if(offsets!=nullptr) {
  2346. *offsets++=sourceIndex;
  2347. sourceIndex=(nextSourceIndex+=2);
  2348. }
  2349. state=(uint8_t)MBCS_ENTRY_FINAL_STATE(entry); /* typically 0 */
  2350. offset=0;
  2351. } else {
  2352. /* set the state and leave the optimized loop */
  2353. ++nextSourceIndex;
  2354. bytes[0]=*(source-1);
  2355. byteIndex=1;
  2356. break;
  2357. }
  2358. } else {
  2359. if(MBCS_ENTRY_FINAL_IS_VALID_DIRECT_16(entry)) {
  2360. /* output BMP code point */
  2361. ++source;
  2362. *target++=(char16_t)MBCS_ENTRY_FINAL_VALUE_16(entry);
  2363. if(offsets!=nullptr) {
  2364. *offsets++=sourceIndex;
  2365. sourceIndex=++nextSourceIndex;
  2366. }
  2367. state=(uint8_t)MBCS_ENTRY_FINAL_STATE(entry); /* typically 0 */
  2368. } else {
  2369. /* leave the optimized loop */
  2370. break;
  2371. }
  2372. }
  2373. } while(source<sourceLimit && target<targetLimit);
  2374. }
  2375. /*
  2376. * these tests and break statements could be put inside the loop
  2377. * if C had "break outerLoop" like Java
  2378. */
  2379. if(source>=sourceLimit) {
  2380. break;
  2381. }
  2382. if(target>=targetLimit) {
  2383. /* target is full */
  2384. *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
  2385. break;
  2386. }
  2387. ++nextSourceIndex;
  2388. bytes[byteIndex++]=*source++;
  2389. } else /* byteIndex>0 */ {
  2390. ++nextSourceIndex;
  2391. entry=stateTable[state][bytes[byteIndex++]=*source++];
  2392. }
  2393. if(MBCS_ENTRY_IS_TRANSITION(entry)) {
  2394. state=(uint8_t)MBCS_ENTRY_TRANSITION_STATE(entry);
  2395. offset+=MBCS_ENTRY_TRANSITION_OFFSET(entry);
  2396. continue;
  2397. }
  2398. /* save the previous state for proper extension mapping with SI/SO-stateful converters */
  2399. cnv->mode=state;
  2400. /* set the next state early so that we can reuse the entry variable */
  2401. state=(uint8_t)MBCS_ENTRY_FINAL_STATE(entry); /* typically 0 */
  2402. /*
  2403. * An if-else-if chain provides more reliable performance for
  2404. * the most common cases compared to a switch.
  2405. */
  2406. action=(uint8_t)(MBCS_ENTRY_FINAL_ACTION(entry));
  2407. if(action==MBCS_STATE_VALID_16) {
  2408. offset+=MBCS_ENTRY_FINAL_VALUE_16(entry);
  2409. c=unicodeCodeUnits[offset];
  2410. if(c<0xfffe) {
  2411. /* output BMP code point */
  2412. *target++=c;
  2413. if(offsets!=nullptr) {
  2414. *offsets++=sourceIndex;
  2415. }
  2416. byteIndex=0;
  2417. } else if(c==0xfffe) {
  2418. if(UCNV_TO_U_USE_FALLBACK(cnv) && (entry=(int32_t)ucnv_MBCSGetFallback(&cnv->sharedData->mbcs, offset))!=0xfffe) {
  2419. /* output fallback BMP code point */
  2420. *target++=(char16_t)entry;
  2421. if(offsets!=nullptr) {
  2422. *offsets++=sourceIndex;
  2423. }
  2424. byteIndex=0;
  2425. }
  2426. } else {
  2427. /* callback(illegal) */
  2428. *pErrorCode=U_ILLEGAL_CHAR_FOUND;
  2429. }
  2430. } else if(action==MBCS_STATE_VALID_DIRECT_16) {
  2431. /* output BMP code point */
  2432. *target++=(char16_t)MBCS_ENTRY_FINAL_VALUE_16(entry);
  2433. if(offsets!=nullptr) {
  2434. *offsets++=sourceIndex;
  2435. }
  2436. byteIndex=0;
  2437. } else if(action==MBCS_STATE_VALID_16_PAIR) {
  2438. offset+=MBCS_ENTRY_FINAL_VALUE_16(entry);
  2439. c=unicodeCodeUnits[offset++];
  2440. if(c<0xd800) {
  2441. /* output BMP code point below 0xd800 */
  2442. *target++=c;
  2443. if(offsets!=nullptr) {
  2444. *offsets++=sourceIndex;
  2445. }
  2446. byteIndex=0;
  2447. } else if(UCNV_TO_U_USE_FALLBACK(cnv) ? c<=0xdfff : c<=0xdbff) {
  2448. /* output roundtrip or fallback surrogate pair */
  2449. *target++=(char16_t)(c&0xdbff);
  2450. if(offsets!=nullptr) {
  2451. *offsets++=sourceIndex;
  2452. }
  2453. byteIndex=0;
  2454. if(target<targetLimit) {
  2455. *target++=unicodeCodeUnits[offset];
  2456. if(offsets!=nullptr) {
  2457. *offsets++=sourceIndex;
  2458. }
  2459. } else {
  2460. /* target overflow */
  2461. cnv->UCharErrorBuffer[0]=unicodeCodeUnits[offset];
  2462. cnv->UCharErrorBufferLength=1;
  2463. *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
  2464. offset=0;
  2465. break;
  2466. }
  2467. } else if(UCNV_TO_U_USE_FALLBACK(cnv) ? (c&0xfffe)==0xe000 : c==0xe000) {
  2468. /* output roundtrip BMP code point above 0xd800 or fallback BMP code point */
  2469. *target++=unicodeCodeUnits[offset];
  2470. if(offsets!=nullptr) {
  2471. *offsets++=sourceIndex;
  2472. }
  2473. byteIndex=0;
  2474. } else if(c==0xffff) {
  2475. /* callback(illegal) */
  2476. *pErrorCode=U_ILLEGAL_CHAR_FOUND;
  2477. }
  2478. } else if(action==MBCS_STATE_VALID_DIRECT_20 ||
  2479. (action==MBCS_STATE_FALLBACK_DIRECT_20 && UCNV_TO_U_USE_FALLBACK(cnv))
  2480. ) {
  2481. entry=MBCS_ENTRY_FINAL_VALUE(entry);
  2482. /* output surrogate pair */
  2483. *target++=(char16_t)(0xd800|(char16_t)(entry>>10));
  2484. if(offsets!=nullptr) {
  2485. *offsets++=sourceIndex;
  2486. }
  2487. byteIndex=0;
  2488. c=(char16_t)(0xdc00|(char16_t)(entry&0x3ff));
  2489. if(target<targetLimit) {
  2490. *target++=c;
  2491. if(offsets!=nullptr) {
  2492. *offsets++=sourceIndex;
  2493. }
  2494. } else {
  2495. /* target overflow */
  2496. cnv->UCharErrorBuffer[0]=c;
  2497. cnv->UCharErrorBufferLength=1;
  2498. *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
  2499. offset=0;
  2500. break;
  2501. }
  2502. } else if(action==MBCS_STATE_CHANGE_ONLY) {
  2503. /*
  2504. * This serves as a state change without any output.
  2505. * It is useful for reading simple stateful encodings,
  2506. * for example using just Shift-In/Shift-Out codes.
  2507. * The 21 unused bits may later be used for more sophisticated
  2508. * state transitions.
  2509. */
  2510. if(cnv->sharedData->mbcs.dbcsOnlyState==0) {
  2511. byteIndex=0;
  2512. } else {
  2513. /* SI/SO are illegal for DBCS-only conversion */
  2514. state=(uint8_t)(cnv->mode); /* restore the previous state */
  2515. /* callback(illegal) */
  2516. *pErrorCode=U_ILLEGAL_CHAR_FOUND;
  2517. }
  2518. } else if(action==MBCS_STATE_FALLBACK_DIRECT_16) {
  2519. if(UCNV_TO_U_USE_FALLBACK(cnv)) {
  2520. /* output BMP code point */
  2521. *target++=(char16_t)MBCS_ENTRY_FINAL_VALUE_16(entry);
  2522. if(offsets!=nullptr) {
  2523. *offsets++=sourceIndex;
  2524. }
  2525. byteIndex=0;
  2526. }
  2527. } else if(action==MBCS_STATE_UNASSIGNED) {
  2528. /* just fall through */
  2529. } else if(action==MBCS_STATE_ILLEGAL) {
  2530. /* callback(illegal) */
  2531. *pErrorCode=U_ILLEGAL_CHAR_FOUND;
  2532. } else {
  2533. /* reserved, must never occur */
  2534. byteIndex=0;
  2535. }
  2536. /* end of action codes: prepare for a new character */
  2537. offset=0;
  2538. if(byteIndex==0) {
  2539. sourceIndex=nextSourceIndex;
  2540. } else if(U_FAILURE(*pErrorCode)) {
  2541. /* callback(illegal) */
  2542. if(byteIndex>1) {
  2543. /*
  2544. * Ticket 5691: consistent illegal sequences:
  2545. * - We include at least the first byte in the illegal sequence.
  2546. * - If any of the non-initial bytes could be the start of a character,
  2547. * we stop the illegal sequence before the first one of those.
  2548. */
  2549. UBool isDBCSOnly = cnv->sharedData->mbcs.dbcsOnlyState != 0;
  2550. int8_t i;
  2551. for(i=1;
  2552. i<byteIndex && !isSingleOrLead(stateTable, state, isDBCSOnly, bytes[i]);
  2553. ++i) {}
  2554. if(i<byteIndex) {
  2555. /* Back out some bytes. */
  2556. int8_t backOutDistance=byteIndex-i;
  2557. int32_t bytesFromThisBuffer=(int32_t)(source-(const uint8_t *)pArgs->source);
  2558. byteIndex=i; /* length of reported illegal byte sequence */
  2559. if(backOutDistance<=bytesFromThisBuffer) {
  2560. source-=backOutDistance;
  2561. } else {
  2562. /* Back out bytes from the previous buffer: Need to replay them. */
  2563. cnv->preToULength=(int8_t)(bytesFromThisBuffer-backOutDistance);
  2564. /* preToULength is negative! */
  2565. uprv_memcpy(cnv->preToU, bytes+i, -cnv->preToULength);
  2566. source=(const uint8_t *)pArgs->source;
  2567. }
  2568. }
  2569. }
  2570. break;
  2571. } else /* unassigned sequences indicated with byteIndex>0 */ {
  2572. /* try an extension mapping */
  2573. pArgs->source=(const char *)source;
  2574. byteIndex=_extToU(cnv, cnv->sharedData,
  2575. byteIndex, &source, sourceLimit,
  2576. &target, targetLimit,
  2577. &offsets, sourceIndex,
  2578. pArgs->flush,
  2579. pErrorCode);
  2580. sourceIndex=nextSourceIndex+=(int32_t)(source-(const uint8_t *)pArgs->source);
  2581. if(U_FAILURE(*pErrorCode)) {
  2582. /* not mappable or buffer overflow */
  2583. break;
  2584. }
  2585. }
  2586. }
  2587. /* set the converter state back into UConverter */
  2588. cnv->toUnicodeStatus=offset;
  2589. cnv->mode=state;
  2590. cnv->toULength=byteIndex;
  2591. /* write back the updated pointers */
  2592. pArgs->source=(const char *)source;
  2593. pArgs->target=target;
  2594. pArgs->offsets=offsets;
  2595. }
  2596. /*
  2597. * This version of ucnv_MBCSGetNextUChar() is optimized for single-byte, single-state codepages.
  2598. * We still need a conversion loop in case we find reserved action codes, which are to be ignored.
  2599. */
  2600. static UChar32
  2601. ucnv_MBCSSingleGetNextUChar(UConverterToUnicodeArgs *pArgs,
  2602. UErrorCode *pErrorCode) {
  2603. UConverter *cnv;
  2604. const int32_t (*stateTable)[256];
  2605. const uint8_t *source, *sourceLimit;
  2606. int32_t entry;
  2607. uint8_t action;
  2608. /* set up the local pointers */
  2609. cnv=pArgs->converter;
  2610. source = reinterpret_cast<const uint8_t*>(pArgs->source);
  2611. sourceLimit = reinterpret_cast<const uint8_t*>(pArgs->sourceLimit);
  2612. if((cnv->options&UCNV_OPTION_SWAP_LFNL)!=0) {
  2613. stateTable=(const int32_t (*)[256])cnv->sharedData->mbcs.swapLFNLStateTable;
  2614. } else {
  2615. stateTable=cnv->sharedData->mbcs.stateTable;
  2616. }
  2617. /* conversion loop */
  2618. while(source<sourceLimit) {
  2619. entry=stateTable[0][*source++];
  2620. /* MBCS_ENTRY_IS_FINAL(entry) */
  2621. /* write back the updated pointer early so that we can return directly */
  2622. pArgs->source = reinterpret_cast<const char*>(source);
  2623. if(MBCS_ENTRY_FINAL_IS_VALID_DIRECT_16(entry)) {
  2624. /* output BMP code point */
  2625. return static_cast<char16_t>(MBCS_ENTRY_FINAL_VALUE_16(entry));
  2626. }
  2627. /*
  2628. * An if-else-if chain provides more reliable performance for
  2629. * the most common cases compared to a switch.
  2630. */
  2631. action = static_cast<uint8_t>(MBCS_ENTRY_FINAL_ACTION(entry));
  2632. if( action==MBCS_STATE_VALID_DIRECT_20 ||
  2633. (action==MBCS_STATE_FALLBACK_DIRECT_20 && UCNV_TO_U_USE_FALLBACK(cnv))
  2634. ) {
  2635. /* output supplementary code point */
  2636. return static_cast<UChar32>(MBCS_ENTRY_FINAL_VALUE(entry) + 0x10000);
  2637. } else if(action==MBCS_STATE_FALLBACK_DIRECT_16) {
  2638. if(UCNV_TO_U_USE_FALLBACK(cnv)) {
  2639. /* output BMP code point */
  2640. return static_cast<char16_t>(MBCS_ENTRY_FINAL_VALUE_16(entry));
  2641. }
  2642. } else if(action==MBCS_STATE_UNASSIGNED) {
  2643. /* just fall through */
  2644. } else if(action==MBCS_STATE_ILLEGAL) {
  2645. /* callback(illegal) */
  2646. *pErrorCode=U_ILLEGAL_CHAR_FOUND;
  2647. } else {
  2648. /* reserved, must never occur */
  2649. continue;
  2650. }
  2651. if(U_FAILURE(*pErrorCode)) {
  2652. /* callback(illegal) */
  2653. break;
  2654. } else /* unassigned sequence */ {
  2655. /* defer to the generic implementation */
  2656. pArgs->source = reinterpret_cast<const char*>(source) - 1;
  2657. return UCNV_GET_NEXT_UCHAR_USE_TO_U;
  2658. }
  2659. }
  2660. /* no output because of empty input or only state changes */
  2661. *pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR;
  2662. return 0xffff;
  2663. }
  2664. /*
  2665. * Version of _MBCSToUnicodeWithOffsets() optimized for single-character
  2666. * conversion without offset handling.
  2667. *
  2668. * When a character does not have a mapping to Unicode, then we return to the
  2669. * generic ucnv_getNextUChar() code for extension/GB 18030 and error/callback
  2670. * handling.
  2671. * We also defer to the generic code in other complicated cases and have them
  2672. * ultimately handled by _MBCSToUnicodeWithOffsets() itself.
  2673. *
  2674. * All normal mappings and errors are handled here.
  2675. */
  2676. static UChar32 U_CALLCONV
  2677. ucnv_MBCSGetNextUChar(UConverterToUnicodeArgs *pArgs,
  2678. UErrorCode *pErrorCode) {
  2679. UConverter *cnv;
  2680. const uint8_t *source, *sourceLimit, *lastSource;
  2681. const int32_t (*stateTable)[256];
  2682. const uint16_t *unicodeCodeUnits;
  2683. uint32_t offset;
  2684. uint8_t state;
  2685. int32_t entry;
  2686. UChar32 c;
  2687. uint8_t action;
  2688. /* use optimized function if possible */
  2689. cnv=pArgs->converter;
  2690. if(cnv->preToULength>0) {
  2691. /* use the generic code in ucnv_getNextUChar() to continue with a partial match */
  2692. return UCNV_GET_NEXT_UCHAR_USE_TO_U;
  2693. }
  2694. if(cnv->sharedData->mbcs.unicodeMask&UCNV_HAS_SURROGATES) {
  2695. /*
  2696. * Using the generic ucnv_getNextUChar() code lets us deal correctly
  2697. * with the rare case of a codepage that maps single surrogates
  2698. * without adding the complexity to this already complicated function here.
  2699. */
  2700. return UCNV_GET_NEXT_UCHAR_USE_TO_U;
  2701. } else if(cnv->sharedData->mbcs.countStates==1) {
  2702. return ucnv_MBCSSingleGetNextUChar(pArgs, pErrorCode);
  2703. }
  2704. /* set up the local pointers */
  2705. source = lastSource = reinterpret_cast<const uint8_t*>(pArgs->source);
  2706. sourceLimit = reinterpret_cast<const uint8_t*>(pArgs->sourceLimit);
  2707. if((cnv->options&UCNV_OPTION_SWAP_LFNL)!=0) {
  2708. stateTable=(const int32_t (*)[256])cnv->sharedData->mbcs.swapLFNLStateTable;
  2709. } else {
  2710. stateTable=cnv->sharedData->mbcs.stateTable;
  2711. }
  2712. unicodeCodeUnits=cnv->sharedData->mbcs.unicodeCodeUnits;
  2713. /* get the converter state from UConverter */
  2714. offset=cnv->toUnicodeStatus;
  2715. /*
  2716. * if we are in the SBCS state for a DBCS-only converter,
  2717. * then load the DBCS state from the MBCS data
  2718. * (dbcsOnlyState==0 if it is not a DBCS-only converter)
  2719. */
  2720. if ((state = static_cast<uint8_t>(cnv->mode)) == 0) {
  2721. state=cnv->sharedData->mbcs.dbcsOnlyState;
  2722. }
  2723. /* conversion loop */
  2724. c=U_SENTINEL;
  2725. while(source<sourceLimit) {
  2726. entry=stateTable[state][*source++];
  2727. if(MBCS_ENTRY_IS_TRANSITION(entry)) {
  2728. state = static_cast<uint8_t>(MBCS_ENTRY_TRANSITION_STATE(entry));
  2729. offset+=MBCS_ENTRY_TRANSITION_OFFSET(entry);
  2730. /* optimization for 1/2-byte input and BMP output */
  2731. if( source<sourceLimit &&
  2732. MBCS_ENTRY_IS_FINAL(entry=stateTable[state][*source]) &&
  2733. MBCS_ENTRY_FINAL_ACTION(entry)==MBCS_STATE_VALID_16 &&
  2734. (c=unicodeCodeUnits[offset+MBCS_ENTRY_FINAL_VALUE_16(entry)])<0xfffe
  2735. ) {
  2736. ++source;
  2737. state = static_cast<uint8_t>(MBCS_ENTRY_FINAL_STATE(entry)); /* typically 0 */
  2738. /* output BMP code point */
  2739. break;
  2740. }
  2741. } else {
  2742. /* save the previous state for proper extension mapping with SI/SO-stateful converters */
  2743. cnv->mode=state;
  2744. /* set the next state early so that we can reuse the entry variable */
  2745. state = static_cast<uint8_t>(MBCS_ENTRY_FINAL_STATE(entry)); /* typically 0 */
  2746. /*
  2747. * An if-else-if chain provides more reliable performance for
  2748. * the most common cases compared to a switch.
  2749. */
  2750. action = static_cast<uint8_t>(MBCS_ENTRY_FINAL_ACTION(entry));
  2751. if(action==MBCS_STATE_VALID_DIRECT_16) {
  2752. /* output BMP code point */
  2753. c = static_cast<char16_t>(MBCS_ENTRY_FINAL_VALUE_16(entry));
  2754. break;
  2755. } else if(action==MBCS_STATE_VALID_16) {
  2756. offset+=MBCS_ENTRY_FINAL_VALUE_16(entry);
  2757. c=unicodeCodeUnits[offset];
  2758. if(c<0xfffe) {
  2759. /* output BMP code point */
  2760. break;
  2761. } else if(c==0xfffe) {
  2762. if(UCNV_TO_U_USE_FALLBACK(cnv) && (c=ucnv_MBCSGetFallback(&cnv->sharedData->mbcs, offset))!=0xfffe) {
  2763. break;
  2764. }
  2765. } else {
  2766. /* callback(illegal) */
  2767. *pErrorCode=U_ILLEGAL_CHAR_FOUND;
  2768. }
  2769. } else if(action==MBCS_STATE_VALID_16_PAIR) {
  2770. offset+=MBCS_ENTRY_FINAL_VALUE_16(entry);
  2771. c=unicodeCodeUnits[offset++];
  2772. if(c<0xd800) {
  2773. /* output BMP code point below 0xd800 */
  2774. break;
  2775. } else if(UCNV_TO_U_USE_FALLBACK(cnv) ? c<=0xdfff : c<=0xdbff) {
  2776. /* output roundtrip or fallback supplementary code point */
  2777. c=((c&0x3ff)<<10)+unicodeCodeUnits[offset]+(0x10000-0xdc00);
  2778. break;
  2779. } else if(UCNV_TO_U_USE_FALLBACK(cnv) ? (c&0xfffe)==0xe000 : c==0xe000) {
  2780. /* output roundtrip BMP code point above 0xd800 or fallback BMP code point */
  2781. c=unicodeCodeUnits[offset];
  2782. break;
  2783. } else if(c==0xffff) {
  2784. /* callback(illegal) */
  2785. *pErrorCode=U_ILLEGAL_CHAR_FOUND;
  2786. }
  2787. } else if(action==MBCS_STATE_VALID_DIRECT_20 ||
  2788. (action==MBCS_STATE_FALLBACK_DIRECT_20 && UCNV_TO_U_USE_FALLBACK(cnv))
  2789. ) {
  2790. /* output supplementary code point */
  2791. c = static_cast<UChar32>(MBCS_ENTRY_FINAL_VALUE(entry) + 0x10000);
  2792. break;
  2793. } else if(action==MBCS_STATE_CHANGE_ONLY) {
  2794. /*
  2795. * This serves as a state change without any output.
  2796. * It is useful for reading simple stateful encodings,
  2797. * for example using just Shift-In/Shift-Out codes.
  2798. * The 21 unused bits may later be used for more sophisticated
  2799. * state transitions.
  2800. */
  2801. if(cnv->sharedData->mbcs.dbcsOnlyState!=0) {
  2802. /* SI/SO are illegal for DBCS-only conversion */
  2803. state = static_cast<uint8_t>(cnv->mode); /* restore the previous state */
  2804. /* callback(illegal) */
  2805. *pErrorCode=U_ILLEGAL_CHAR_FOUND;
  2806. }
  2807. } else if(action==MBCS_STATE_FALLBACK_DIRECT_16) {
  2808. if(UCNV_TO_U_USE_FALLBACK(cnv)) {
  2809. /* output BMP code point */
  2810. c = static_cast<char16_t>(MBCS_ENTRY_FINAL_VALUE_16(entry));
  2811. break;
  2812. }
  2813. } else if(action==MBCS_STATE_UNASSIGNED) {
  2814. /* just fall through */
  2815. } else if(action==MBCS_STATE_ILLEGAL) {
  2816. /* callback(illegal) */
  2817. *pErrorCode=U_ILLEGAL_CHAR_FOUND;
  2818. } else {
  2819. /* reserved (must never occur), or only state change */
  2820. offset=0;
  2821. lastSource=source;
  2822. continue;
  2823. }
  2824. /* end of action codes: prepare for a new character */
  2825. offset=0;
  2826. if(U_FAILURE(*pErrorCode)) {
  2827. /* callback(illegal) */
  2828. break;
  2829. } else /* unassigned sequence */ {
  2830. /* defer to the generic implementation */
  2831. cnv->toUnicodeStatus=0;
  2832. cnv->mode=state;
  2833. pArgs->source = reinterpret_cast<const char*>(lastSource);
  2834. return UCNV_GET_NEXT_UCHAR_USE_TO_U;
  2835. }
  2836. }
  2837. }
  2838. if(c<0) {
  2839. if(U_SUCCESS(*pErrorCode) && source==sourceLimit && lastSource<source) {
  2840. /* incomplete character byte sequence */
  2841. uint8_t *bytes=cnv->toUBytes;
  2842. cnv->toULength = static_cast<int8_t>(source - lastSource);
  2843. do {
  2844. *bytes++=*lastSource++;
  2845. } while(lastSource<source);
  2846. *pErrorCode=U_TRUNCATED_CHAR_FOUND;
  2847. } else if(U_FAILURE(*pErrorCode)) {
  2848. /* callback(illegal) */
  2849. /*
  2850. * Ticket 5691: consistent illegal sequences:
  2851. * - We include at least the first byte in the illegal sequence.
  2852. * - If any of the non-initial bytes could be the start of a character,
  2853. * we stop the illegal sequence before the first one of those.
  2854. */
  2855. UBool isDBCSOnly = static_cast<UBool>(cnv->sharedData->mbcs.dbcsOnlyState != 0);
  2856. uint8_t *bytes=cnv->toUBytes;
  2857. *bytes++=*lastSource++; /* first byte */
  2858. if(lastSource==source) {
  2859. cnv->toULength=1;
  2860. } else /* lastSource<source: multi-byte character */ {
  2861. int8_t i;
  2862. for(i=1;
  2863. lastSource<source && !isSingleOrLead(stateTable, state, isDBCSOnly, *lastSource);
  2864. ++i
  2865. ) {
  2866. *bytes++=*lastSource++;
  2867. }
  2868. cnv->toULength=i;
  2869. source=lastSource;
  2870. }
  2871. } else {
  2872. /* no output because of empty input or only state changes */
  2873. *pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR;
  2874. }
  2875. c=0xffff;
  2876. }
  2877. /* set the converter state back into UConverter, ready for a new character */
  2878. cnv->toUnicodeStatus=0;
  2879. cnv->mode=state;
  2880. /* write back the updated pointer */
  2881. pArgs->source = reinterpret_cast<const char*>(source);
  2882. return c;
  2883. }
  2884. #if 0
  2885. /*
  2886. * Code disabled 2002dec09 (ICU 2.4) because it is not currently used in ICU. markus
  2887. * Removal improves code coverage.
  2888. */
  2889. /**
  2890. * This version of ucnv_MBCSSimpleGetNextUChar() is optimized for single-byte, single-state codepages.
  2891. * It does not handle the EBCDIC swaplfnl option (set in UConverter).
  2892. * It does not handle conversion extensions (_extToU()).
  2893. */
  2894. U_CFUNC UChar32
  2895. ucnv_MBCSSingleSimpleGetNextUChar(UConverterSharedData *sharedData,
  2896. uint8_t b, UBool useFallback) {
  2897. int32_t entry;
  2898. uint8_t action;
  2899. entry=sharedData->mbcs.stateTable[0][b];
  2900. /* MBCS_ENTRY_IS_FINAL(entry) */
  2901. if(MBCS_ENTRY_FINAL_IS_VALID_DIRECT_16(entry)) {
  2902. /* output BMP code point */
  2903. return (char16_t)MBCS_ENTRY_FINAL_VALUE_16(entry);
  2904. }
  2905. /*
  2906. * An if-else-if chain provides more reliable performance for
  2907. * the most common cases compared to a switch.
  2908. */
  2909. action=(uint8_t)(MBCS_ENTRY_FINAL_ACTION(entry));
  2910. if(action==MBCS_STATE_VALID_DIRECT_20) {
  2911. /* output supplementary code point */
  2912. return 0x10000+MBCS_ENTRY_FINAL_VALUE(entry);
  2913. } else if(action==MBCS_STATE_FALLBACK_DIRECT_16) {
  2914. if(!TO_U_USE_FALLBACK(useFallback)) {
  2915. return 0xfffe;
  2916. }
  2917. /* output BMP code point */
  2918. return (char16_t)MBCS_ENTRY_FINAL_VALUE_16(entry);
  2919. } else if(action==MBCS_STATE_FALLBACK_DIRECT_20) {
  2920. if(!TO_U_USE_FALLBACK(useFallback)) {
  2921. return 0xfffe;
  2922. }
  2923. /* output supplementary code point */
  2924. return 0x10000+MBCS_ENTRY_FINAL_VALUE(entry);
  2925. } else if(action==MBCS_STATE_UNASSIGNED) {
  2926. return 0xfffe;
  2927. } else if(action==MBCS_STATE_ILLEGAL) {
  2928. return 0xffff;
  2929. } else {
  2930. /* reserved, must never occur */
  2931. return 0xffff;
  2932. }
  2933. }
  2934. #endif
  2935. /*
  2936. * This is a simple version of _MBCSGetNextUChar() that is used
  2937. * by other converter implementations.
  2938. * It only returns an "assigned" result if it consumes the entire input.
  2939. * It does not use state from the converter, nor error codes.
  2940. * It does not handle the EBCDIC swaplfnl option (set in UConverter).
  2941. * It handles conversion extensions but not GB 18030.
  2942. *
  2943. * Return value:
  2944. * U+fffe unassigned
  2945. * U+ffff illegal
  2946. * otherwise the Unicode code point
  2947. */
  2948. U_CFUNC UChar32
  2949. ucnv_MBCSSimpleGetNextUChar(UConverterSharedData *sharedData,
  2950. const char *source, int32_t length,
  2951. UBool useFallback) {
  2952. const int32_t (*stateTable)[256];
  2953. const uint16_t *unicodeCodeUnits;
  2954. uint32_t offset;
  2955. uint8_t state, action;
  2956. UChar32 c;
  2957. int32_t i, entry;
  2958. if(length<=0) {
  2959. /* no input at all: "illegal" */
  2960. return 0xffff;
  2961. }
  2962. #if 0
  2963. /*
  2964. * Code disabled 2002dec09 (ICU 2.4) because it is not currently used in ICU. markus
  2965. * TODO In future releases, verify that this function is never called for SBCS
  2966. * conversions, i.e., that sharedData->mbcs.countStates==1 is still true.
  2967. * Removal improves code coverage.
  2968. */
  2969. /* use optimized function if possible */
  2970. if(sharedData->mbcs.countStates==1) {
  2971. if(length==1) {
  2972. return ucnv_MBCSSingleSimpleGetNextUChar(sharedData, (uint8_t)*source, useFallback);
  2973. } else {
  2974. return 0xffff; /* illegal: more than a single byte for an SBCS converter */
  2975. }
  2976. }
  2977. #endif
  2978. /* set up the local pointers */
  2979. stateTable=sharedData->mbcs.stateTable;
  2980. unicodeCodeUnits=sharedData->mbcs.unicodeCodeUnits;
  2981. /* converter state */
  2982. offset=0;
  2983. state=sharedData->mbcs.dbcsOnlyState;
  2984. /* conversion loop */
  2985. for(i=0;;) {
  2986. entry=stateTable[state][(uint8_t)source[i++]];
  2987. if(MBCS_ENTRY_IS_TRANSITION(entry)) {
  2988. state=(uint8_t)MBCS_ENTRY_TRANSITION_STATE(entry);
  2989. offset+=MBCS_ENTRY_TRANSITION_OFFSET(entry);
  2990. if(i==length) {
  2991. return 0xffff; /* truncated character */
  2992. }
  2993. } else {
  2994. /*
  2995. * An if-else-if chain provides more reliable performance for
  2996. * the most common cases compared to a switch.
  2997. */
  2998. action=(uint8_t)(MBCS_ENTRY_FINAL_ACTION(entry));
  2999. if(action==MBCS_STATE_VALID_16) {
  3000. offset+=MBCS_ENTRY_FINAL_VALUE_16(entry);
  3001. c=unicodeCodeUnits[offset];
  3002. if(c!=0xfffe) {
  3003. /* done */
  3004. } else if(UCNV_TO_U_USE_FALLBACK(cnv)) {
  3005. c=ucnv_MBCSGetFallback(&sharedData->mbcs, offset);
  3006. /* else done with 0xfffe */
  3007. }
  3008. break;
  3009. } else if(action==MBCS_STATE_VALID_DIRECT_16) {
  3010. /* output BMP code point */
  3011. c=(char16_t)MBCS_ENTRY_FINAL_VALUE_16(entry);
  3012. break;
  3013. } else if(action==MBCS_STATE_VALID_16_PAIR) {
  3014. offset+=MBCS_ENTRY_FINAL_VALUE_16(entry);
  3015. c=unicodeCodeUnits[offset++];
  3016. if(c<0xd800) {
  3017. /* output BMP code point below 0xd800 */
  3018. } else if(UCNV_TO_U_USE_FALLBACK(cnv) ? c<=0xdfff : c<=0xdbff) {
  3019. /* output roundtrip or fallback supplementary code point */
  3020. c=(UChar32)(((c&0x3ff)<<10)+unicodeCodeUnits[offset]+(0x10000-0xdc00));
  3021. } else if(UCNV_TO_U_USE_FALLBACK(cnv) ? (c&0xfffe)==0xe000 : c==0xe000) {
  3022. /* output roundtrip BMP code point above 0xd800 or fallback BMP code point */
  3023. c=unicodeCodeUnits[offset];
  3024. } else if(c==0xffff) {
  3025. return 0xffff;
  3026. } else {
  3027. c=0xfffe;
  3028. }
  3029. break;
  3030. } else if(action==MBCS_STATE_VALID_DIRECT_20) {
  3031. /* output supplementary code point */
  3032. c=0x10000+MBCS_ENTRY_FINAL_VALUE(entry);
  3033. break;
  3034. } else if(action==MBCS_STATE_FALLBACK_DIRECT_16) {
  3035. if(!TO_U_USE_FALLBACK(useFallback)) {
  3036. c=0xfffe;
  3037. break;
  3038. }
  3039. /* output BMP code point */
  3040. c=(char16_t)MBCS_ENTRY_FINAL_VALUE_16(entry);
  3041. break;
  3042. } else if(action==MBCS_STATE_FALLBACK_DIRECT_20) {
  3043. if(!TO_U_USE_FALLBACK(useFallback)) {
  3044. c=0xfffe;
  3045. break;
  3046. }
  3047. /* output supplementary code point */
  3048. c=0x10000+MBCS_ENTRY_FINAL_VALUE(entry);
  3049. break;
  3050. } else if(action==MBCS_STATE_UNASSIGNED) {
  3051. c=0xfffe;
  3052. break;
  3053. }
  3054. /*
  3055. * forbid MBCS_STATE_CHANGE_ONLY for this function,
  3056. * and MBCS_STATE_ILLEGAL and reserved action codes
  3057. */
  3058. return 0xffff;
  3059. }
  3060. }
  3061. if(i!=length) {
  3062. /* illegal for this function: not all input consumed */
  3063. return 0xffff;
  3064. }
  3065. if(c==0xfffe) {
  3066. /* try an extension mapping */
  3067. const int32_t *cx=sharedData->mbcs.extIndexes;
  3068. if(cx!=nullptr) {
  3069. return ucnv_extSimpleMatchToU(cx, source, length, useFallback);
  3070. }
  3071. }
  3072. return c;
  3073. }
  3074. /* MBCS-from-Unicode conversion functions ----------------------------------- */
  3075. /* This version of ucnv_MBCSFromUnicodeWithOffsets() is optimized for double-byte codepages. */
  3076. static void
  3077. ucnv_MBCSDoubleFromUnicodeWithOffsets(UConverterFromUnicodeArgs *pArgs,
  3078. UErrorCode *pErrorCode) {
  3079. UConverter *cnv;
  3080. const char16_t *source, *sourceLimit;
  3081. uint8_t *target;
  3082. int32_t targetCapacity;
  3083. int32_t *offsets;
  3084. const uint16_t *table;
  3085. const uint16_t *mbcsIndex;
  3086. const uint8_t *bytes;
  3087. UChar32 c;
  3088. int32_t sourceIndex, nextSourceIndex;
  3089. uint32_t stage2Entry;
  3090. uint32_t asciiRoundtrips;
  3091. uint32_t value;
  3092. uint8_t unicodeMask;
  3093. /* use optimized function if possible */
  3094. cnv=pArgs->converter;
  3095. unicodeMask=cnv->sharedData->mbcs.unicodeMask;
  3096. /* set up the local pointers */
  3097. source=pArgs->source;
  3098. sourceLimit=pArgs->sourceLimit;
  3099. target = reinterpret_cast<uint8_t*>(pArgs->target);
  3100. targetCapacity = static_cast<int32_t>(pArgs->targetLimit - pArgs->target);
  3101. offsets=pArgs->offsets;
  3102. table=cnv->sharedData->mbcs.fromUnicodeTable;
  3103. mbcsIndex=cnv->sharedData->mbcs.mbcsIndex;
  3104. if((cnv->options&UCNV_OPTION_SWAP_LFNL)!=0) {
  3105. bytes=cnv->sharedData->mbcs.swapLFNLFromUnicodeBytes;
  3106. } else {
  3107. bytes=cnv->sharedData->mbcs.fromUnicodeBytes;
  3108. }
  3109. asciiRoundtrips=cnv->sharedData->mbcs.asciiRoundtrips;
  3110. /* get the converter state from UConverter */
  3111. c=cnv->fromUChar32;
  3112. /* sourceIndex=-1 if the current character began in the previous buffer */
  3113. sourceIndex= c==0 ? 0 : -1;
  3114. nextSourceIndex=0;
  3115. /* conversion loop */
  3116. if(c!=0 && targetCapacity>0) {
  3117. goto getTrail;
  3118. }
  3119. while(source<sourceLimit) {
  3120. /*
  3121. * This following test is to see if available input would overflow the output.
  3122. * It does not catch output of more than one byte that
  3123. * overflows as a result of a multi-byte character or callback output
  3124. * from the last source character.
  3125. * Therefore, those situations also test for overflows and will
  3126. * then break the loop, too.
  3127. */
  3128. if(targetCapacity>0) {
  3129. /*
  3130. * Get a correct Unicode code point:
  3131. * a single char16_t for a BMP code point or
  3132. * a matched surrogate pair for a "supplementary code point".
  3133. */
  3134. c=*source++;
  3135. ++nextSourceIndex;
  3136. if(c<=0x7f && IS_ASCII_ROUNDTRIP(c, asciiRoundtrips)) {
  3137. *target++ = static_cast<uint8_t>(c);
  3138. if(offsets!=nullptr) {
  3139. *offsets++=sourceIndex;
  3140. sourceIndex=nextSourceIndex;
  3141. }
  3142. --targetCapacity;
  3143. c=0;
  3144. continue;
  3145. }
  3146. /*
  3147. * utf8Friendly table: Test for <=0xd7ff rather than <=MBCS_FAST_MAX
  3148. * to avoid dealing with surrogates.
  3149. * MBCS_FAST_MAX must be >=0xd7ff.
  3150. */
  3151. if(c<=0xd7ff) {
  3152. value=DBCS_RESULT_FROM_MOST_BMP(mbcsIndex, (const uint16_t *)bytes, c);
  3153. /* There are only roundtrips (!=0) and no-mapping (==0) entries. */
  3154. if(value==0) {
  3155. goto unassigned;
  3156. }
  3157. /* output the value */
  3158. } else {
  3159. /*
  3160. * This also tests if the codepage maps single surrogates.
  3161. * If it does, then surrogates are not paired but mapped separately.
  3162. * Note that in this case unmatched surrogates are not detected.
  3163. */
  3164. if(U16_IS_SURROGATE(c) && !(unicodeMask&UCNV_HAS_SURROGATES)) {
  3165. if(U16_IS_SURROGATE_LEAD(c)) {
  3166. getTrail:
  3167. if(source<sourceLimit) {
  3168. /* test the following code unit */
  3169. char16_t trail=*source;
  3170. if(U16_IS_TRAIL(trail)) {
  3171. ++source;
  3172. ++nextSourceIndex;
  3173. c=U16_GET_SUPPLEMENTARY(c, trail);
  3174. if(!(unicodeMask&UCNV_HAS_SUPPLEMENTARY)) {
  3175. /* BMP-only codepages are stored without stage 1 entries for supplementary code points */
  3176. /* callback(unassigned) */
  3177. goto unassigned;
  3178. }
  3179. /* convert this supplementary code point */
  3180. /* exit this condition tree */
  3181. } else {
  3182. /* this is an unmatched lead code unit (1st surrogate) */
  3183. /* callback(illegal) */
  3184. *pErrorCode=U_ILLEGAL_CHAR_FOUND;
  3185. break;
  3186. }
  3187. } else {
  3188. /* no more input */
  3189. break;
  3190. }
  3191. } else {
  3192. /* this is an unmatched trail code unit (2nd surrogate) */
  3193. /* callback(illegal) */
  3194. *pErrorCode=U_ILLEGAL_CHAR_FOUND;
  3195. break;
  3196. }
  3197. }
  3198. /* convert the Unicode code point in c into codepage bytes */
  3199. stage2Entry=MBCS_STAGE_2_FROM_U(table, c);
  3200. /* get the bytes and the length for the output */
  3201. /* MBCS_OUTPUT_2 */
  3202. value=MBCS_VALUE_2_FROM_STAGE_2(bytes, stage2Entry, c);
  3203. /* is this code point assigned, or do we use fallbacks? */
  3204. if(!(MBCS_FROM_U_IS_ROUNDTRIP(stage2Entry, c) ||
  3205. (UCNV_FROM_U_USE_FALLBACK(cnv, c) && value!=0))
  3206. ) {
  3207. /*
  3208. * We allow a 0 byte output if the "assigned" bit is set for this entry.
  3209. * There is no way with this data structure for fallback output
  3210. * to be a zero byte.
  3211. */
  3212. unassigned:
  3213. /* try an extension mapping */
  3214. pArgs->source=source;
  3215. c=_extFromU(cnv, cnv->sharedData,
  3216. c, &source, sourceLimit,
  3217. &target, target+targetCapacity,
  3218. &offsets, sourceIndex,
  3219. pArgs->flush,
  3220. pErrorCode);
  3221. nextSourceIndex += static_cast<int32_t>(source - pArgs->source);
  3222. if(U_FAILURE(*pErrorCode)) {
  3223. /* not mappable or buffer overflow */
  3224. break;
  3225. } else {
  3226. /* a mapping was written to the target, continue */
  3227. /* recalculate the targetCapacity after an extension mapping */
  3228. targetCapacity = static_cast<int32_t>(pArgs->targetLimit - reinterpret_cast<char*>(target));
  3229. /* normal end of conversion: prepare for a new character */
  3230. sourceIndex=nextSourceIndex;
  3231. continue;
  3232. }
  3233. }
  3234. }
  3235. /* write the output character bytes from value and length */
  3236. /* from the first if in the loop we know that targetCapacity>0 */
  3237. if(value<=0xff) {
  3238. /* this is easy because we know that there is enough space */
  3239. *target++ = static_cast<uint8_t>(value);
  3240. if(offsets!=nullptr) {
  3241. *offsets++=sourceIndex;
  3242. }
  3243. --targetCapacity;
  3244. } else /* length==2 */ {
  3245. *target++ = static_cast<uint8_t>(value >> 8);
  3246. if(2<=targetCapacity) {
  3247. *target++ = static_cast<uint8_t>(value);
  3248. if(offsets!=nullptr) {
  3249. *offsets++=sourceIndex;
  3250. *offsets++=sourceIndex;
  3251. }
  3252. targetCapacity-=2;
  3253. } else {
  3254. if(offsets!=nullptr) {
  3255. *offsets++=sourceIndex;
  3256. }
  3257. cnv->charErrorBuffer[0] = static_cast<char>(value);
  3258. cnv->charErrorBufferLength=1;
  3259. /* target overflow */
  3260. targetCapacity=0;
  3261. *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
  3262. c=0;
  3263. break;
  3264. }
  3265. }
  3266. /* normal end of conversion: prepare for a new character */
  3267. c=0;
  3268. sourceIndex=nextSourceIndex;
  3269. continue;
  3270. } else {
  3271. /* target is full */
  3272. *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
  3273. break;
  3274. }
  3275. }
  3276. /* set the converter state back into UConverter */
  3277. cnv->fromUChar32=c;
  3278. /* write back the updated pointers */
  3279. pArgs->source=source;
  3280. pArgs->target = reinterpret_cast<char*>(target);
  3281. pArgs->offsets=offsets;
  3282. }
  3283. /* This version of ucnv_MBCSFromUnicodeWithOffsets() is optimized for single-byte codepages. */
  3284. static void
  3285. ucnv_MBCSSingleFromUnicodeWithOffsets(UConverterFromUnicodeArgs *pArgs,
  3286. UErrorCode *pErrorCode) {
  3287. UConverter *cnv;
  3288. const char16_t *source, *sourceLimit;
  3289. uint8_t *target;
  3290. int32_t targetCapacity;
  3291. int32_t *offsets;
  3292. const uint16_t *table;
  3293. const uint16_t *results;
  3294. UChar32 c;
  3295. int32_t sourceIndex, nextSourceIndex;
  3296. uint16_t value, minValue;
  3297. UBool hasSupplementary;
  3298. /* set up the local pointers */
  3299. cnv=pArgs->converter;
  3300. source=pArgs->source;
  3301. sourceLimit=pArgs->sourceLimit;
  3302. target = reinterpret_cast<uint8_t*>(pArgs->target);
  3303. targetCapacity = static_cast<int32_t>(pArgs->targetLimit - pArgs->target);
  3304. offsets=pArgs->offsets;
  3305. table=cnv->sharedData->mbcs.fromUnicodeTable;
  3306. if((cnv->options&UCNV_OPTION_SWAP_LFNL)!=0) {
  3307. results = reinterpret_cast<uint16_t*>(cnv->sharedData->mbcs.swapLFNLFromUnicodeBytes);
  3308. } else {
  3309. results=(uint16_t *)cnv->sharedData->mbcs.fromUnicodeBytes;
  3310. }
  3311. if(cnv->useFallback) {
  3312. /* use all roundtrip and fallback results */
  3313. minValue=0x800;
  3314. } else {
  3315. /* use only roundtrips and fallbacks from private-use characters */
  3316. minValue=0xc00;
  3317. }
  3318. hasSupplementary = static_cast<UBool>(cnv->sharedData->mbcs.unicodeMask & UCNV_HAS_SUPPLEMENTARY);
  3319. /* get the converter state from UConverter */
  3320. c=cnv->fromUChar32;
  3321. /* sourceIndex=-1 if the current character began in the previous buffer */
  3322. sourceIndex= c==0 ? 0 : -1;
  3323. nextSourceIndex=0;
  3324. /* conversion loop */
  3325. if(c!=0 && targetCapacity>0) {
  3326. goto getTrail;
  3327. }
  3328. while(source<sourceLimit) {
  3329. /*
  3330. * This following test is to see if available input would overflow the output.
  3331. * It does not catch output of more than one byte that
  3332. * overflows as a result of a multi-byte character or callback output
  3333. * from the last source character.
  3334. * Therefore, those situations also test for overflows and will
  3335. * then break the loop, too.
  3336. */
  3337. if(targetCapacity>0) {
  3338. /*
  3339. * Get a correct Unicode code point:
  3340. * a single char16_t for a BMP code point or
  3341. * a matched surrogate pair for a "supplementary code point".
  3342. */
  3343. c=*source++;
  3344. ++nextSourceIndex;
  3345. if(U16_IS_SURROGATE(c)) {
  3346. if(U16_IS_SURROGATE_LEAD(c)) {
  3347. getTrail:
  3348. if(source<sourceLimit) {
  3349. /* test the following code unit */
  3350. char16_t trail=*source;
  3351. if(U16_IS_TRAIL(trail)) {
  3352. ++source;
  3353. ++nextSourceIndex;
  3354. c=U16_GET_SUPPLEMENTARY(c, trail);
  3355. if(!hasSupplementary) {
  3356. /* BMP-only codepages are stored without stage 1 entries for supplementary code points */
  3357. /* callback(unassigned) */
  3358. goto unassigned;
  3359. }
  3360. /* convert this supplementary code point */
  3361. /* exit this condition tree */
  3362. } else {
  3363. /* this is an unmatched lead code unit (1st surrogate) */
  3364. /* callback(illegal) */
  3365. *pErrorCode=U_ILLEGAL_CHAR_FOUND;
  3366. break;
  3367. }
  3368. } else {
  3369. /* no more input */
  3370. break;
  3371. }
  3372. } else {
  3373. /* this is an unmatched trail code unit (2nd surrogate) */
  3374. /* callback(illegal) */
  3375. *pErrorCode=U_ILLEGAL_CHAR_FOUND;
  3376. break;
  3377. }
  3378. }
  3379. /* convert the Unicode code point in c into codepage bytes */
  3380. value=MBCS_SINGLE_RESULT_FROM_U(table, results, c);
  3381. /* is this code point assigned, or do we use fallbacks? */
  3382. if(value>=minValue) {
  3383. /* assigned, write the output character bytes from value and length */
  3384. /* length==1 */
  3385. /* this is easy because we know that there is enough space */
  3386. *target++ = static_cast<uint8_t>(value);
  3387. if(offsets!=nullptr) {
  3388. *offsets++=sourceIndex;
  3389. }
  3390. --targetCapacity;
  3391. /* normal end of conversion: prepare for a new character */
  3392. c=0;
  3393. sourceIndex=nextSourceIndex;
  3394. } else { /* unassigned */
  3395. unassigned:
  3396. /* try an extension mapping */
  3397. pArgs->source=source;
  3398. c=_extFromU(cnv, cnv->sharedData,
  3399. c, &source, sourceLimit,
  3400. &target, target+targetCapacity,
  3401. &offsets, sourceIndex,
  3402. pArgs->flush,
  3403. pErrorCode);
  3404. nextSourceIndex += static_cast<int32_t>(source - pArgs->source);
  3405. if(U_FAILURE(*pErrorCode)) {
  3406. /* not mappable or buffer overflow */
  3407. break;
  3408. } else {
  3409. /* a mapping was written to the target, continue */
  3410. /* recalculate the targetCapacity after an extension mapping */
  3411. targetCapacity = static_cast<int32_t>(pArgs->targetLimit - reinterpret_cast<char*>(target));
  3412. /* normal end of conversion: prepare for a new character */
  3413. sourceIndex=nextSourceIndex;
  3414. }
  3415. }
  3416. } else {
  3417. /* target is full */
  3418. *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
  3419. break;
  3420. }
  3421. }
  3422. /* set the converter state back into UConverter */
  3423. cnv->fromUChar32=c;
  3424. /* write back the updated pointers */
  3425. pArgs->source=source;
  3426. pArgs->target = reinterpret_cast<char*>(target);
  3427. pArgs->offsets=offsets;
  3428. }
  3429. /*
  3430. * This version of ucnv_MBCSFromUnicode() is optimized for single-byte codepages
  3431. * that map only to and from the BMP.
  3432. * In addition to single-byte/state optimizations, the offset calculations
  3433. * become much easier.
  3434. * It would be possible to use the sbcsIndex for UTF-8-friendly tables,
  3435. * but measurements have shown that this diminishes performance
  3436. * in more cases than it improves it.
  3437. * See SVN revision 21013 (2007-feb-06) for the last version with #if switches
  3438. * for various MBCS and SBCS optimizations.
  3439. */
  3440. static void
  3441. ucnv_MBCSSingleFromBMPWithOffsets(UConverterFromUnicodeArgs *pArgs,
  3442. UErrorCode *pErrorCode) {
  3443. UConverter *cnv;
  3444. const char16_t *source, *sourceLimit, *lastSource;
  3445. uint8_t *target;
  3446. int32_t targetCapacity, length;
  3447. int32_t *offsets;
  3448. const uint16_t *table;
  3449. const uint16_t *results;
  3450. UChar32 c;
  3451. int32_t sourceIndex;
  3452. uint32_t asciiRoundtrips;
  3453. uint16_t value, minValue;
  3454. /* set up the local pointers */
  3455. cnv=pArgs->converter;
  3456. source=pArgs->source;
  3457. sourceLimit=pArgs->sourceLimit;
  3458. target = reinterpret_cast<uint8_t*>(pArgs->target);
  3459. targetCapacity = static_cast<int32_t>(pArgs->targetLimit - pArgs->target);
  3460. offsets=pArgs->offsets;
  3461. table=cnv->sharedData->mbcs.fromUnicodeTable;
  3462. if((cnv->options&UCNV_OPTION_SWAP_LFNL)!=0) {
  3463. results = reinterpret_cast<uint16_t*>(cnv->sharedData->mbcs.swapLFNLFromUnicodeBytes);
  3464. } else {
  3465. results=(uint16_t *)cnv->sharedData->mbcs.fromUnicodeBytes;
  3466. }
  3467. asciiRoundtrips=cnv->sharedData->mbcs.asciiRoundtrips;
  3468. if(cnv->useFallback) {
  3469. /* use all roundtrip and fallback results */
  3470. minValue=0x800;
  3471. } else {
  3472. /* use only roundtrips and fallbacks from private-use characters */
  3473. minValue=0xc00;
  3474. }
  3475. /* get the converter state from UConverter */
  3476. c=cnv->fromUChar32;
  3477. /* sourceIndex=-1 if the current character began in the previous buffer */
  3478. sourceIndex= c==0 ? 0 : -1;
  3479. lastSource=source;
  3480. /*
  3481. * since the conversion here is 1:1 char16_t:uint8_t, we need only one counter
  3482. * for the minimum of the sourceLength and targetCapacity
  3483. */
  3484. length = static_cast<int32_t>(sourceLimit - source);
  3485. if(length<targetCapacity) {
  3486. targetCapacity=length;
  3487. }
  3488. /* conversion loop */
  3489. if(c!=0 && targetCapacity>0) {
  3490. goto getTrail;
  3491. }
  3492. #if MBCS_UNROLL_SINGLE_FROM_BMP
  3493. /* unrolling makes it slower on Pentium III/Windows 2000?! */
  3494. /* unroll the loop with the most common case */
  3495. unrolled:
  3496. if(targetCapacity>=4) {
  3497. int32_t count, loops;
  3498. uint16_t andedValues;
  3499. loops=count=targetCapacity>>2;
  3500. do {
  3501. c=*source++;
  3502. andedValues=value=MBCS_SINGLE_RESULT_FROM_U(table, results, c);
  3503. *target++=(uint8_t)value;
  3504. c=*source++;
  3505. andedValues&=value=MBCS_SINGLE_RESULT_FROM_U(table, results, c);
  3506. *target++=(uint8_t)value;
  3507. c=*source++;
  3508. andedValues&=value=MBCS_SINGLE_RESULT_FROM_U(table, results, c);
  3509. *target++=(uint8_t)value;
  3510. c=*source++;
  3511. andedValues&=value=MBCS_SINGLE_RESULT_FROM_U(table, results, c);
  3512. *target++=(uint8_t)value;
  3513. /* were all 4 entries really valid? */
  3514. if(andedValues<minValue) {
  3515. /* no, return to the first of these 4 */
  3516. source-=4;
  3517. target-=4;
  3518. break;
  3519. }
  3520. } while(--count>0);
  3521. count=loops-count;
  3522. targetCapacity-=4*count;
  3523. if(offsets!=nullptr) {
  3524. lastSource+=4*count;
  3525. while(count>0) {
  3526. *offsets++=sourceIndex++;
  3527. *offsets++=sourceIndex++;
  3528. *offsets++=sourceIndex++;
  3529. *offsets++=sourceIndex++;
  3530. --count;
  3531. }
  3532. }
  3533. c=0;
  3534. }
  3535. #endif
  3536. while(targetCapacity>0) {
  3537. /*
  3538. * Get a correct Unicode code point:
  3539. * a single char16_t for a BMP code point or
  3540. * a matched surrogate pair for a "supplementary code point".
  3541. */
  3542. c=*source++;
  3543. /*
  3544. * Do not immediately check for single surrogates:
  3545. * Assume that they are unassigned and check for them in that case.
  3546. * This speeds up the conversion of assigned characters.
  3547. */
  3548. /* convert the Unicode code point in c into codepage bytes */
  3549. if(c<=0x7f && IS_ASCII_ROUNDTRIP(c, asciiRoundtrips)) {
  3550. *target++ = static_cast<uint8_t>(c);
  3551. --targetCapacity;
  3552. c=0;
  3553. continue;
  3554. }
  3555. value=MBCS_SINGLE_RESULT_FROM_U(table, results, c);
  3556. /* is this code point assigned, or do we use fallbacks? */
  3557. if(value>=minValue) {
  3558. /* assigned, write the output character bytes from value and length */
  3559. /* length==1 */
  3560. /* this is easy because we know that there is enough space */
  3561. *target++ = static_cast<uint8_t>(value);
  3562. --targetCapacity;
  3563. /* normal end of conversion: prepare for a new character */
  3564. c=0;
  3565. continue;
  3566. } else if(!U16_IS_SURROGATE(c)) {
  3567. /* normal, unassigned BMP character */
  3568. } else if(U16_IS_SURROGATE_LEAD(c)) {
  3569. getTrail:
  3570. if(source<sourceLimit) {
  3571. /* test the following code unit */
  3572. char16_t trail=*source;
  3573. if(U16_IS_TRAIL(trail)) {
  3574. ++source;
  3575. c=U16_GET_SUPPLEMENTARY(c, trail);
  3576. /* this codepage does not map supplementary code points */
  3577. /* callback(unassigned) */
  3578. } else {
  3579. /* this is an unmatched lead code unit (1st surrogate) */
  3580. /* callback(illegal) */
  3581. *pErrorCode=U_ILLEGAL_CHAR_FOUND;
  3582. break;
  3583. }
  3584. } else {
  3585. /* no more input */
  3586. if (pArgs->flush) {
  3587. *pErrorCode=U_TRUNCATED_CHAR_FOUND;
  3588. }
  3589. break;
  3590. }
  3591. } else {
  3592. /* this is an unmatched trail code unit (2nd surrogate) */
  3593. /* callback(illegal) */
  3594. *pErrorCode=U_ILLEGAL_CHAR_FOUND;
  3595. break;
  3596. }
  3597. /* c does not have a mapping */
  3598. /* get the number of code units for c to correctly advance sourceIndex */
  3599. length=U16_LENGTH(c);
  3600. /* set offsets since the start or the last extension */
  3601. if(offsets!=nullptr) {
  3602. int32_t count = static_cast<int32_t>(source - lastSource);
  3603. /* do not set the offset for this character */
  3604. count-=length;
  3605. while(count>0) {
  3606. *offsets++=sourceIndex++;
  3607. --count;
  3608. }
  3609. /* offsets and sourceIndex are now set for the current character */
  3610. }
  3611. /* try an extension mapping */
  3612. lastSource=source;
  3613. c=_extFromU(cnv, cnv->sharedData,
  3614. c, &source, sourceLimit,
  3615. &target, reinterpret_cast<const uint8_t*>(pArgs->targetLimit),
  3616. &offsets, sourceIndex,
  3617. pArgs->flush,
  3618. pErrorCode);
  3619. sourceIndex += length + static_cast<int32_t>(source - lastSource);
  3620. lastSource=source;
  3621. if(U_FAILURE(*pErrorCode)) {
  3622. /* not mappable or buffer overflow */
  3623. break;
  3624. } else {
  3625. /* a mapping was written to the target, continue */
  3626. /* recalculate the targetCapacity after an extension mapping */
  3627. targetCapacity = static_cast<int32_t>(pArgs->targetLimit - reinterpret_cast<char*>(target));
  3628. length = static_cast<int32_t>(sourceLimit - source);
  3629. if(length<targetCapacity) {
  3630. targetCapacity=length;
  3631. }
  3632. }
  3633. #if MBCS_UNROLL_SINGLE_FROM_BMP
  3634. /* unrolling makes it slower on Pentium III/Windows 2000?! */
  3635. goto unrolled;
  3636. #endif
  3637. }
  3638. if(U_SUCCESS(*pErrorCode) && source<sourceLimit && target>=(uint8_t *)pArgs->targetLimit) {
  3639. /* target is full */
  3640. *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
  3641. }
  3642. /* set offsets since the start or the last callback */
  3643. if(offsets!=nullptr) {
  3644. size_t count=source-lastSource;
  3645. if (count > 0 && *pErrorCode == U_TRUNCATED_CHAR_FOUND) {
  3646. /*
  3647. Caller gave us a partial supplementary character,
  3648. which this function couldn't convert in any case.
  3649. The callback will handle the offset.
  3650. */
  3651. count--;
  3652. }
  3653. while(count>0) {
  3654. *offsets++=sourceIndex++;
  3655. --count;
  3656. }
  3657. }
  3658. /* set the converter state back into UConverter */
  3659. cnv->fromUChar32=c;
  3660. /* write back the updated pointers */
  3661. pArgs->source=source;
  3662. pArgs->target = reinterpret_cast<char*>(target);
  3663. pArgs->offsets=offsets;
  3664. }
  3665. U_CFUNC void
  3666. ucnv_MBCSFromUnicodeWithOffsets(UConverterFromUnicodeArgs *pArgs,
  3667. UErrorCode *pErrorCode) {
  3668. UConverter *cnv;
  3669. const char16_t *source, *sourceLimit;
  3670. uint8_t *target;
  3671. int32_t targetCapacity;
  3672. int32_t *offsets;
  3673. const uint16_t *table;
  3674. const uint16_t *mbcsIndex;
  3675. const uint8_t *p, *bytes;
  3676. uint8_t outputType;
  3677. UChar32 c;
  3678. int32_t prevSourceIndex, sourceIndex, nextSourceIndex;
  3679. uint32_t stage2Entry;
  3680. uint32_t asciiRoundtrips;
  3681. uint32_t value;
  3682. /* Shift-In and Shift-Out byte sequences differ by encoding scheme. */
  3683. uint8_t siBytes[2] = {0, 0};
  3684. uint8_t soBytes[2] = {0, 0};
  3685. uint8_t siLength, soLength;
  3686. int32_t length = 0, prevLength;
  3687. uint8_t unicodeMask;
  3688. cnv=pArgs->converter;
  3689. if(cnv->preFromUFirstCP>=0) {
  3690. /*
  3691. * pass sourceIndex=-1 because we continue from an earlier buffer
  3692. * in the future, this may change with continuous offsets
  3693. */
  3694. ucnv_extContinueMatchFromU(cnv, pArgs, -1, pErrorCode);
  3695. if(U_FAILURE(*pErrorCode) || cnv->preFromULength<0) {
  3696. return;
  3697. }
  3698. }
  3699. /* use optimized function if possible */
  3700. outputType=cnv->sharedData->mbcs.outputType;
  3701. unicodeMask=cnv->sharedData->mbcs.unicodeMask;
  3702. if(outputType==MBCS_OUTPUT_1 && !(unicodeMask&UCNV_HAS_SURROGATES)) {
  3703. if(!(unicodeMask&UCNV_HAS_SUPPLEMENTARY)) {
  3704. ucnv_MBCSSingleFromBMPWithOffsets(pArgs, pErrorCode);
  3705. } else {
  3706. ucnv_MBCSSingleFromUnicodeWithOffsets(pArgs, pErrorCode);
  3707. }
  3708. return;
  3709. } else if(outputType==MBCS_OUTPUT_2 && cnv->sharedData->mbcs.utf8Friendly) {
  3710. ucnv_MBCSDoubleFromUnicodeWithOffsets(pArgs, pErrorCode);
  3711. return;
  3712. }
  3713. /* set up the local pointers */
  3714. source=pArgs->source;
  3715. sourceLimit=pArgs->sourceLimit;
  3716. target=(uint8_t *)pArgs->target;
  3717. targetCapacity=(int32_t)(pArgs->targetLimit-pArgs->target);
  3718. offsets=pArgs->offsets;
  3719. table=cnv->sharedData->mbcs.fromUnicodeTable;
  3720. if(cnv->sharedData->mbcs.utf8Friendly) {
  3721. mbcsIndex=cnv->sharedData->mbcs.mbcsIndex;
  3722. } else {
  3723. mbcsIndex=nullptr;
  3724. }
  3725. if((cnv->options&UCNV_OPTION_SWAP_LFNL)!=0) {
  3726. bytes=cnv->sharedData->mbcs.swapLFNLFromUnicodeBytes;
  3727. } else {
  3728. bytes=cnv->sharedData->mbcs.fromUnicodeBytes;
  3729. }
  3730. asciiRoundtrips=cnv->sharedData->mbcs.asciiRoundtrips;
  3731. /* get the converter state from UConverter */
  3732. c=cnv->fromUChar32;
  3733. if(outputType==MBCS_OUTPUT_2_SISO) {
  3734. prevLength=cnv->fromUnicodeStatus;
  3735. if(prevLength==0) {
  3736. /* set the real value */
  3737. prevLength=1;
  3738. }
  3739. } else {
  3740. /* prevent fromUnicodeStatus from being set to something non-0 */
  3741. prevLength=0;
  3742. }
  3743. /* sourceIndex=-1 if the current character began in the previous buffer */
  3744. prevSourceIndex=-1;
  3745. sourceIndex= c==0 ? 0 : -1;
  3746. nextSourceIndex=0;
  3747. /* Get the SI/SO character for the converter */
  3748. siLength = static_cast<uint8_t>(getSISOBytes(SI, cnv->options, siBytes));
  3749. soLength = static_cast<uint8_t>(getSISOBytes(SO, cnv->options, soBytes));
  3750. /* conversion loop */
  3751. /*
  3752. * This is another piece of ugly code:
  3753. * A goto into the loop if the converter state contains a first surrogate
  3754. * from the previous function call.
  3755. * It saves me to check in each loop iteration a check of if(c==0)
  3756. * and duplicating the trail-surrogate-handling code in the else
  3757. * branch of that check.
  3758. * I could not find any other way to get around this other than
  3759. * using a function call for the conversion and callback, which would
  3760. * be even more inefficient.
  3761. *
  3762. * Markus Scherer 2000-jul-19
  3763. */
  3764. if(c!=0 && targetCapacity>0) {
  3765. goto getTrail;
  3766. }
  3767. while(source<sourceLimit) {
  3768. /*
  3769. * This following test is to see if available input would overflow the output.
  3770. * It does not catch output of more than one byte that
  3771. * overflows as a result of a multi-byte character or callback output
  3772. * from the last source character.
  3773. * Therefore, those situations also test for overflows and will
  3774. * then break the loop, too.
  3775. */
  3776. if(targetCapacity>0) {
  3777. /*
  3778. * Get a correct Unicode code point:
  3779. * a single char16_t for a BMP code point or
  3780. * a matched surrogate pair for a "supplementary code point".
  3781. */
  3782. c=*source++;
  3783. ++nextSourceIndex;
  3784. if(c<=0x7f && IS_ASCII_ROUNDTRIP(c, asciiRoundtrips)) {
  3785. *target++=(uint8_t)c;
  3786. if(offsets!=nullptr) {
  3787. *offsets++=sourceIndex;
  3788. prevSourceIndex=sourceIndex;
  3789. sourceIndex=nextSourceIndex;
  3790. }
  3791. --targetCapacity;
  3792. c=0;
  3793. continue;
  3794. }
  3795. /*
  3796. * utf8Friendly table: Test for <=0xd7ff rather than <=MBCS_FAST_MAX
  3797. * to avoid dealing with surrogates.
  3798. * MBCS_FAST_MAX must be >=0xd7ff.
  3799. */
  3800. if(c<=0xd7ff && mbcsIndex!=nullptr) {
  3801. value=mbcsIndex[c>>6];
  3802. /* get the bytes and the length for the output (copied from below and adapted for utf8Friendly data) */
  3803. /* There are only roundtrips (!=0) and no-mapping (==0) entries. */
  3804. switch(outputType) {
  3805. case MBCS_OUTPUT_2:
  3806. value=((const uint16_t *)bytes)[value +(c&0x3f)];
  3807. if(value<=0xff) {
  3808. if(value==0) {
  3809. goto unassigned;
  3810. } else {
  3811. length=1;
  3812. }
  3813. } else {
  3814. length=2;
  3815. }
  3816. break;
  3817. case MBCS_OUTPUT_2_SISO:
  3818. /* 1/2-byte stateful with Shift-In/Shift-Out */
  3819. /*
  3820. * Save the old state in the converter object
  3821. * right here, then change the local prevLength state variable if necessary.
  3822. * Then, if this character turns out to be unassigned or a fallback that
  3823. * is not taken, the callback code must not save the new state in the converter
  3824. * because the new state is for a character that is not output.
  3825. * However, the callback must still restore the state from the converter
  3826. * in case the callback function changed it for its output.
  3827. */
  3828. cnv->fromUnicodeStatus=prevLength; /* save the old state */
  3829. value=((const uint16_t *)bytes)[value +(c&0x3f)];
  3830. if(value<=0xff) {
  3831. if(value==0) {
  3832. goto unassigned;
  3833. } else if(prevLength<=1) {
  3834. length=1;
  3835. } else {
  3836. /* change from double-byte mode to single-byte */
  3837. if (siLength == 1) {
  3838. value|=(uint32_t)siBytes[0]<<8;
  3839. length = 2;
  3840. } else if (siLength == 2) {
  3841. value|=(uint32_t)siBytes[1]<<8;
  3842. value|=(uint32_t)siBytes[0]<<16;
  3843. length = 3;
  3844. }
  3845. prevLength=1;
  3846. }
  3847. } else {
  3848. if(prevLength==2) {
  3849. length=2;
  3850. } else {
  3851. /* change from single-byte mode to double-byte */
  3852. if (soLength == 1) {
  3853. value|=(uint32_t)soBytes[0]<<16;
  3854. length = 3;
  3855. } else if (soLength == 2) {
  3856. value|=(uint32_t)soBytes[1]<<16;
  3857. value|=(uint32_t)soBytes[0]<<24;
  3858. length = 4;
  3859. }
  3860. prevLength=2;
  3861. }
  3862. }
  3863. break;
  3864. case MBCS_OUTPUT_DBCS_ONLY:
  3865. /* table with single-byte results, but only DBCS mappings used */
  3866. value=((const uint16_t *)bytes)[value +(c&0x3f)];
  3867. if(value<=0xff) {
  3868. /* no mapping or SBCS result, not taken for DBCS-only */
  3869. goto unassigned;
  3870. } else {
  3871. length=2;
  3872. }
  3873. break;
  3874. case MBCS_OUTPUT_3:
  3875. p=bytes+(value+(c&0x3f))*3;
  3876. value=((uint32_t)*p<<16)|((uint32_t)p[1]<<8)|p[2];
  3877. if(value<=0xff) {
  3878. if(value==0) {
  3879. goto unassigned;
  3880. } else {
  3881. length=1;
  3882. }
  3883. } else if(value<=0xffff) {
  3884. length=2;
  3885. } else {
  3886. length=3;
  3887. }
  3888. break;
  3889. case MBCS_OUTPUT_4:
  3890. value=((const uint32_t *)bytes)[value +(c&0x3f)];
  3891. if(value<=0xff) {
  3892. if(value==0) {
  3893. goto unassigned;
  3894. } else {
  3895. length=1;
  3896. }
  3897. } else if(value<=0xffff) {
  3898. length=2;
  3899. } else if(value<=0xffffff) {
  3900. length=3;
  3901. } else {
  3902. length=4;
  3903. }
  3904. break;
  3905. case MBCS_OUTPUT_3_EUC:
  3906. value=((const uint16_t *)bytes)[value +(c&0x3f)];
  3907. /* EUC 16-bit fixed-length representation */
  3908. if(value<=0xff) {
  3909. if(value==0) {
  3910. goto unassigned;
  3911. } else {
  3912. length=1;
  3913. }
  3914. } else if((value&0x8000)==0) {
  3915. value|=0x8e8000;
  3916. length=3;
  3917. } else if((value&0x80)==0) {
  3918. value|=0x8f0080;
  3919. length=3;
  3920. } else {
  3921. length=2;
  3922. }
  3923. break;
  3924. case MBCS_OUTPUT_4_EUC:
  3925. p=bytes+(value+(c&0x3f))*3;
  3926. value=((uint32_t)*p<<16)|((uint32_t)p[1]<<8)|p[2];
  3927. /* EUC 16-bit fixed-length representation applied to the first two bytes */
  3928. if(value<=0xff) {
  3929. if(value==0) {
  3930. goto unassigned;
  3931. } else {
  3932. length=1;
  3933. }
  3934. } else if(value<=0xffff) {
  3935. length=2;
  3936. } else if((value&0x800000)==0) {
  3937. value|=0x8e800000;
  3938. length=4;
  3939. } else if((value&0x8000)==0) {
  3940. value|=0x8f008000;
  3941. length=4;
  3942. } else {
  3943. length=3;
  3944. }
  3945. break;
  3946. default:
  3947. /* must not occur */
  3948. /*
  3949. * To avoid compiler warnings that value & length may be
  3950. * used without having been initialized, we set them here.
  3951. * In reality, this is unreachable code.
  3952. * Not having a default branch also causes warnings with
  3953. * some compilers.
  3954. */
  3955. value=0;
  3956. length=0;
  3957. break;
  3958. }
  3959. /* output the value */
  3960. } else {
  3961. /*
  3962. * This also tests if the codepage maps single surrogates.
  3963. * If it does, then surrogates are not paired but mapped separately.
  3964. * Note that in this case unmatched surrogates are not detected.
  3965. */
  3966. if(U16_IS_SURROGATE(c) && !(unicodeMask&UCNV_HAS_SURROGATES)) {
  3967. if(U16_IS_SURROGATE_LEAD(c)) {
  3968. getTrail:
  3969. if(source<sourceLimit) {
  3970. /* test the following code unit */
  3971. char16_t trail=*source;
  3972. if(U16_IS_TRAIL(trail)) {
  3973. ++source;
  3974. ++nextSourceIndex;
  3975. c=U16_GET_SUPPLEMENTARY(c, trail);
  3976. if(!(unicodeMask&UCNV_HAS_SUPPLEMENTARY)) {
  3977. /* BMP-only codepages are stored without stage 1 entries for supplementary code points */
  3978. cnv->fromUnicodeStatus=prevLength; /* save the old state */
  3979. /* callback(unassigned) */
  3980. goto unassigned;
  3981. }
  3982. /* convert this supplementary code point */
  3983. /* exit this condition tree */
  3984. } else {
  3985. /* this is an unmatched lead code unit (1st surrogate) */
  3986. /* callback(illegal) */
  3987. *pErrorCode=U_ILLEGAL_CHAR_FOUND;
  3988. break;
  3989. }
  3990. } else {
  3991. /* no more input */
  3992. break;
  3993. }
  3994. } else {
  3995. /* this is an unmatched trail code unit (2nd surrogate) */
  3996. /* callback(illegal) */
  3997. *pErrorCode=U_ILLEGAL_CHAR_FOUND;
  3998. break;
  3999. }
  4000. }
  4001. /* convert the Unicode code point in c into codepage bytes */
  4002. /*
  4003. * The basic lookup is a triple-stage compact array (trie) lookup.
  4004. * For details see the beginning of this file.
  4005. *
  4006. * Single-byte codepages are handled with a different data structure
  4007. * by _MBCSSingle... functions.
  4008. *
  4009. * The result consists of a 32-bit value from stage 2 and
  4010. * a pointer to as many bytes as are stored per character.
  4011. * The pointer points to the character's bytes in stage 3.
  4012. * Bits 15..0 of the stage 2 entry contain the stage 3 index
  4013. * for that pointer, while bits 31..16 are flags for which of
  4014. * the 16 characters in the block are roundtrip-assigned.
  4015. *
  4016. * For 2-byte and 4-byte codepages, the bytes are stored as uint16_t
  4017. * respectively as uint32_t, in the platform encoding.
  4018. * For 3-byte codepages, the bytes are always stored in big-endian order.
  4019. *
  4020. * For EUC encodings that use only either 0x8e or 0x8f as the first
  4021. * byte of their longest byte sequences, the first two bytes in
  4022. * this third stage indicate with their 7th bits whether these bytes
  4023. * are to be written directly or actually need to be preceded by
  4024. * one of the two Single-Shift codes. With this, the third stage
  4025. * stores one byte fewer per character than the actual maximum length of
  4026. * EUC byte sequences.
  4027. *
  4028. * Other than that, leading zero bytes are removed and the other
  4029. * bytes output. A single zero byte may be output if the "assigned"
  4030. * bit in stage 2 was on.
  4031. * The data structure does not support zero byte output as a fallback,
  4032. * and also does not allow output of leading zeros.
  4033. */
  4034. stage2Entry=MBCS_STAGE_2_FROM_U(table, c);
  4035. /* get the bytes and the length for the output */
  4036. switch(outputType) {
  4037. case MBCS_OUTPUT_2:
  4038. value=MBCS_VALUE_2_FROM_STAGE_2(bytes, stage2Entry, c);
  4039. if(value<=0xff) {
  4040. length=1;
  4041. } else {
  4042. length=2;
  4043. }
  4044. break;
  4045. case MBCS_OUTPUT_2_SISO:
  4046. /* 1/2-byte stateful with Shift-In/Shift-Out */
  4047. /*
  4048. * Save the old state in the converter object
  4049. * right here, then change the local prevLength state variable if necessary.
  4050. * Then, if this character turns out to be unassigned or a fallback that
  4051. * is not taken, the callback code must not save the new state in the converter
  4052. * because the new state is for a character that is not output.
  4053. * However, the callback must still restore the state from the converter
  4054. * in case the callback function changed it for its output.
  4055. */
  4056. cnv->fromUnicodeStatus=prevLength; /* save the old state */
  4057. value=MBCS_VALUE_2_FROM_STAGE_2(bytes, stage2Entry, c);
  4058. if(value<=0xff) {
  4059. if(value==0 && MBCS_FROM_U_IS_ROUNDTRIP(stage2Entry, c)==0) {
  4060. /* no mapping, leave value==0 */
  4061. length=0;
  4062. } else if(prevLength<=1) {
  4063. length=1;
  4064. } else {
  4065. /* change from double-byte mode to single-byte */
  4066. if (siLength == 1) {
  4067. value|=(uint32_t)siBytes[0]<<8;
  4068. length = 2;
  4069. } else if (siLength == 2) {
  4070. value|=(uint32_t)siBytes[1]<<8;
  4071. value|=(uint32_t)siBytes[0]<<16;
  4072. length = 3;
  4073. }
  4074. prevLength=1;
  4075. }
  4076. } else {
  4077. if(prevLength==2) {
  4078. length=2;
  4079. } else {
  4080. /* change from single-byte mode to double-byte */
  4081. if (soLength == 1) {
  4082. value|=(uint32_t)soBytes[0]<<16;
  4083. length = 3;
  4084. } else if (soLength == 2) {
  4085. value|=(uint32_t)soBytes[1]<<16;
  4086. value|=(uint32_t)soBytes[0]<<24;
  4087. length = 4;
  4088. }
  4089. prevLength=2;
  4090. }
  4091. }
  4092. break;
  4093. case MBCS_OUTPUT_DBCS_ONLY:
  4094. /* table with single-byte results, but only DBCS mappings used */
  4095. value=MBCS_VALUE_2_FROM_STAGE_2(bytes, stage2Entry, c);
  4096. if(value<=0xff) {
  4097. /* no mapping or SBCS result, not taken for DBCS-only */
  4098. value=stage2Entry=0; /* stage2Entry=0 to reset roundtrip flags */
  4099. length=0;
  4100. } else {
  4101. length=2;
  4102. }
  4103. break;
  4104. case MBCS_OUTPUT_3:
  4105. p=MBCS_POINTER_3_FROM_STAGE_2(bytes, stage2Entry, c);
  4106. value=((uint32_t)*p<<16)|((uint32_t)p[1]<<8)|p[2];
  4107. if(value<=0xff) {
  4108. length=1;
  4109. } else if(value<=0xffff) {
  4110. length=2;
  4111. } else {
  4112. length=3;
  4113. }
  4114. break;
  4115. case MBCS_OUTPUT_4:
  4116. value=MBCS_VALUE_4_FROM_STAGE_2(bytes, stage2Entry, c);
  4117. if(value<=0xff) {
  4118. length=1;
  4119. } else if(value<=0xffff) {
  4120. length=2;
  4121. } else if(value<=0xffffff) {
  4122. length=3;
  4123. } else {
  4124. length=4;
  4125. }
  4126. break;
  4127. case MBCS_OUTPUT_3_EUC:
  4128. value=MBCS_VALUE_2_FROM_STAGE_2(bytes, stage2Entry, c);
  4129. /* EUC 16-bit fixed-length representation */
  4130. if(value<=0xff) {
  4131. length=1;
  4132. } else if((value&0x8000)==0) {
  4133. value|=0x8e8000;
  4134. length=3;
  4135. } else if((value&0x80)==0) {
  4136. value|=0x8f0080;
  4137. length=3;
  4138. } else {
  4139. length=2;
  4140. }
  4141. break;
  4142. case MBCS_OUTPUT_4_EUC:
  4143. p=MBCS_POINTER_3_FROM_STAGE_2(bytes, stage2Entry, c);
  4144. value=((uint32_t)*p<<16)|((uint32_t)p[1]<<8)|p[2];
  4145. /* EUC 16-bit fixed-length representation applied to the first two bytes */
  4146. if(value<=0xff) {
  4147. length=1;
  4148. } else if(value<=0xffff) {
  4149. length=2;
  4150. } else if((value&0x800000)==0) {
  4151. value|=0x8e800000;
  4152. length=4;
  4153. } else if((value&0x8000)==0) {
  4154. value|=0x8f008000;
  4155. length=4;
  4156. } else {
  4157. length=3;
  4158. }
  4159. break;
  4160. default:
  4161. /* must not occur */
  4162. /*
  4163. * To avoid compiler warnings that value & length may be
  4164. * used without having been initialized, we set them here.
  4165. * In reality, this is unreachable code.
  4166. * Not having a default branch also causes warnings with
  4167. * some compilers.
  4168. */
  4169. value=stage2Entry=0; /* stage2Entry=0 to reset roundtrip flags */
  4170. length=0;
  4171. break;
  4172. }
  4173. /* is this code point assigned, or do we use fallbacks? */
  4174. if(!(MBCS_FROM_U_IS_ROUNDTRIP(stage2Entry, c)!=0 ||
  4175. (UCNV_FROM_U_USE_FALLBACK(cnv, c) && value!=0))
  4176. ) {
  4177. /*
  4178. * We allow a 0 byte output if the "assigned" bit is set for this entry.
  4179. * There is no way with this data structure for fallback output
  4180. * to be a zero byte.
  4181. */
  4182. unassigned:
  4183. /* try an extension mapping */
  4184. pArgs->source=source;
  4185. c=_extFromU(cnv, cnv->sharedData,
  4186. c, &source, sourceLimit,
  4187. &target, target+targetCapacity,
  4188. &offsets, sourceIndex,
  4189. pArgs->flush,
  4190. pErrorCode);
  4191. nextSourceIndex+=(int32_t)(source-pArgs->source);
  4192. prevLength=cnv->fromUnicodeStatus; /* restore SISO state */
  4193. if(U_FAILURE(*pErrorCode)) {
  4194. /* not mappable or buffer overflow */
  4195. break;
  4196. } else {
  4197. /* a mapping was written to the target, continue */
  4198. /* recalculate the targetCapacity after an extension mapping */
  4199. targetCapacity=(int32_t)(pArgs->targetLimit-(char *)target);
  4200. /* normal end of conversion: prepare for a new character */
  4201. if(offsets!=nullptr) {
  4202. prevSourceIndex=sourceIndex;
  4203. sourceIndex=nextSourceIndex;
  4204. }
  4205. continue;
  4206. }
  4207. }
  4208. }
  4209. /* write the output character bytes from value and length */
  4210. /* from the first if in the loop we know that targetCapacity>0 */
  4211. if(length<=targetCapacity) {
  4212. if(offsets==nullptr) {
  4213. switch(length) {
  4214. /* each branch falls through to the next one */
  4215. case 4:
  4216. *target++=(uint8_t)(value>>24);
  4217. U_FALLTHROUGH;
  4218. case 3:
  4219. *target++=(uint8_t)(value>>16);
  4220. U_FALLTHROUGH;
  4221. case 2:
  4222. *target++=(uint8_t)(value>>8);
  4223. U_FALLTHROUGH;
  4224. case 1:
  4225. *target++=(uint8_t)value;
  4226. U_FALLTHROUGH;
  4227. default:
  4228. /* will never occur */
  4229. break;
  4230. }
  4231. } else {
  4232. switch(length) {
  4233. /* each branch falls through to the next one */
  4234. case 4:
  4235. *target++=(uint8_t)(value>>24);
  4236. *offsets++=sourceIndex;
  4237. U_FALLTHROUGH;
  4238. case 3:
  4239. *target++=(uint8_t)(value>>16);
  4240. *offsets++=sourceIndex;
  4241. U_FALLTHROUGH;
  4242. case 2:
  4243. *target++=(uint8_t)(value>>8);
  4244. *offsets++=sourceIndex;
  4245. U_FALLTHROUGH;
  4246. case 1:
  4247. *target++=(uint8_t)value;
  4248. *offsets++=sourceIndex;
  4249. U_FALLTHROUGH;
  4250. default:
  4251. /* will never occur */
  4252. break;
  4253. }
  4254. }
  4255. targetCapacity-=length;
  4256. } else {
  4257. uint8_t *charErrorBuffer;
  4258. /*
  4259. * We actually do this backwards here:
  4260. * In order to save an intermediate variable, we output
  4261. * first to the overflow buffer what does not fit into the
  4262. * regular target.
  4263. */
  4264. /* we know that 1<=targetCapacity<length<=4 */
  4265. length-=targetCapacity;
  4266. charErrorBuffer=(uint8_t *)cnv->charErrorBuffer;
  4267. switch(length) {
  4268. /* each branch falls through to the next one */
  4269. case 3:
  4270. *charErrorBuffer++=(uint8_t)(value>>16);
  4271. U_FALLTHROUGH;
  4272. case 2:
  4273. *charErrorBuffer++=(uint8_t)(value>>8);
  4274. U_FALLTHROUGH;
  4275. case 1:
  4276. *charErrorBuffer=(uint8_t)value;
  4277. U_FALLTHROUGH;
  4278. default:
  4279. /* will never occur */
  4280. break;
  4281. }
  4282. cnv->charErrorBufferLength=(int8_t)length;
  4283. /* now output what fits into the regular target */
  4284. value>>=8*length; /* length was reduced by targetCapacity */
  4285. switch(targetCapacity) {
  4286. /* each branch falls through to the next one */
  4287. case 3:
  4288. *target++=(uint8_t)(value>>16);
  4289. if(offsets!=nullptr) {
  4290. *offsets++=sourceIndex;
  4291. }
  4292. U_FALLTHROUGH;
  4293. case 2:
  4294. *target++=(uint8_t)(value>>8);
  4295. if(offsets!=nullptr) {
  4296. *offsets++=sourceIndex;
  4297. }
  4298. U_FALLTHROUGH;
  4299. case 1:
  4300. *target++=(uint8_t)value;
  4301. if(offsets!=nullptr) {
  4302. *offsets++=sourceIndex;
  4303. }
  4304. U_FALLTHROUGH;
  4305. default:
  4306. /* will never occur */
  4307. break;
  4308. }
  4309. /* target overflow */
  4310. targetCapacity=0;
  4311. *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
  4312. c=0;
  4313. break;
  4314. }
  4315. /* normal end of conversion: prepare for a new character */
  4316. c=0;
  4317. if(offsets!=nullptr) {
  4318. prevSourceIndex=sourceIndex;
  4319. sourceIndex=nextSourceIndex;
  4320. }
  4321. continue;
  4322. } else {
  4323. /* target is full */
  4324. *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
  4325. break;
  4326. }
  4327. }
  4328. /*
  4329. * the end of the input stream and detection of truncated input
  4330. * are handled by the framework, but for EBCDIC_STATEFUL conversion
  4331. * we need to emit an SI at the very end
  4332. *
  4333. * conditions:
  4334. * successful
  4335. * EBCDIC_STATEFUL in DBCS mode
  4336. * end of input and no truncated input
  4337. */
  4338. if( U_SUCCESS(*pErrorCode) &&
  4339. outputType==MBCS_OUTPUT_2_SISO && prevLength==2 &&
  4340. pArgs->flush && source>=sourceLimit && c==0
  4341. ) {
  4342. /* EBCDIC_STATEFUL ending with DBCS: emit an SI to return the output stream to SBCS */
  4343. if(targetCapacity>0) {
  4344. *target++ = siBytes[0];
  4345. if (siLength == 2) {
  4346. if (targetCapacity<2) {
  4347. cnv->charErrorBuffer[0] = siBytes[1];
  4348. cnv->charErrorBufferLength=1;
  4349. *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
  4350. } else {
  4351. *target++ = siBytes[1];
  4352. }
  4353. }
  4354. if(offsets!=nullptr) {
  4355. /* set the last source character's index (sourceIndex points at sourceLimit now) */
  4356. *offsets++=prevSourceIndex;
  4357. }
  4358. } else {
  4359. /* target is full */
  4360. cnv->charErrorBuffer[0] = siBytes[0];
  4361. if (siLength == 2) {
  4362. cnv->charErrorBuffer[1] = siBytes[1];
  4363. }
  4364. cnv->charErrorBufferLength=siLength;
  4365. *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
  4366. }
  4367. prevLength=1; /* we switched into SBCS */
  4368. }
  4369. /* set the converter state back into UConverter */
  4370. cnv->fromUChar32=c;
  4371. cnv->fromUnicodeStatus=prevLength;
  4372. /* write back the updated pointers */
  4373. pArgs->source=source;
  4374. pArgs->target=(char *)target;
  4375. pArgs->offsets=offsets;
  4376. }
  4377. /*
  4378. * This is another simple conversion function for internal use by other
  4379. * conversion implementations.
  4380. * It does not use the converter state nor call callbacks.
  4381. * It does not handle the EBCDIC swaplfnl option (set in UConverter).
  4382. * It handles conversion extensions but not GB 18030.
  4383. *
  4384. * It converts one single Unicode code point into codepage bytes, encoded
  4385. * as one 32-bit value. The function returns the number of bytes in *pValue:
  4386. * 1..4 the number of bytes in *pValue
  4387. * 0 unassigned (*pValue undefined)
  4388. * -1 illegal (currently not used, *pValue undefined)
  4389. *
  4390. * *pValue will contain the resulting bytes with the last byte in bits 7..0,
  4391. * the second to last byte in bits 15..8, etc.
  4392. * Currently, the function assumes but does not check that 0<=c<=0x10ffff.
  4393. */
  4394. U_CFUNC int32_t
  4395. ucnv_MBCSFromUChar32(UConverterSharedData *sharedData,
  4396. UChar32 c, uint32_t *pValue,
  4397. UBool useFallback) {
  4398. const int32_t *cx;
  4399. const uint16_t *table;
  4400. #if 0
  4401. /* #if 0 because this is not currently used in ICU - reduce code, increase code coverage */
  4402. const uint8_t *p;
  4403. #endif
  4404. uint32_t stage2Entry;
  4405. uint32_t value;
  4406. int32_t length;
  4407. /* BMP-only codepages are stored without stage 1 entries for supplementary code points */
  4408. if(c<=0xffff || (sharedData->mbcs.unicodeMask&UCNV_HAS_SUPPLEMENTARY)) {
  4409. table=sharedData->mbcs.fromUnicodeTable;
  4410. /* convert the Unicode code point in c into codepage bytes (same as in _MBCSFromUnicodeWithOffsets) */
  4411. if(sharedData->mbcs.outputType==MBCS_OUTPUT_1) {
  4412. value=MBCS_SINGLE_RESULT_FROM_U(table, (uint16_t *)sharedData->mbcs.fromUnicodeBytes, c);
  4413. /* is this code point assigned, or do we use fallbacks? */
  4414. if(useFallback ? value>=0x800 : value>=0xc00) {
  4415. *pValue=value&0xff;
  4416. return 1;
  4417. }
  4418. } else /* outputType!=MBCS_OUTPUT_1 */ {
  4419. stage2Entry=MBCS_STAGE_2_FROM_U(table, c);
  4420. /* get the bytes and the length for the output */
  4421. switch(sharedData->mbcs.outputType) {
  4422. case MBCS_OUTPUT_2:
  4423. value=MBCS_VALUE_2_FROM_STAGE_2(sharedData->mbcs.fromUnicodeBytes, stage2Entry, c);
  4424. if(value<=0xff) {
  4425. length=1;
  4426. } else {
  4427. length=2;
  4428. }
  4429. break;
  4430. #if 0
  4431. /* #if 0 because this is not currently used in ICU - reduce code, increase code coverage */
  4432. case MBCS_OUTPUT_DBCS_ONLY:
  4433. /* table with single-byte results, but only DBCS mappings used */
  4434. value=MBCS_VALUE_2_FROM_STAGE_2(sharedData->mbcs.fromUnicodeBytes, stage2Entry, c);
  4435. if(value<=0xff) {
  4436. /* no mapping or SBCS result, not taken for DBCS-only */
  4437. value=stage2Entry=0; /* stage2Entry=0 to reset roundtrip flags */
  4438. length=0;
  4439. } else {
  4440. length=2;
  4441. }
  4442. break;
  4443. case MBCS_OUTPUT_3:
  4444. p=MBCS_POINTER_3_FROM_STAGE_2(sharedData->mbcs.fromUnicodeBytes, stage2Entry, c);
  4445. value=((uint32_t)*p<<16)|((uint32_t)p[1]<<8)|p[2];
  4446. if(value<=0xff) {
  4447. length=1;
  4448. } else if(value<=0xffff) {
  4449. length=2;
  4450. } else {
  4451. length=3;
  4452. }
  4453. break;
  4454. case MBCS_OUTPUT_4:
  4455. value=MBCS_VALUE_4_FROM_STAGE_2(sharedData->mbcs.fromUnicodeBytes, stage2Entry, c);
  4456. if(value<=0xff) {
  4457. length=1;
  4458. } else if(value<=0xffff) {
  4459. length=2;
  4460. } else if(value<=0xffffff) {
  4461. length=3;
  4462. } else {
  4463. length=4;
  4464. }
  4465. break;
  4466. case MBCS_OUTPUT_3_EUC:
  4467. value=MBCS_VALUE_2_FROM_STAGE_2(sharedData->mbcs.fromUnicodeBytes, stage2Entry, c);
  4468. /* EUC 16-bit fixed-length representation */
  4469. if(value<=0xff) {
  4470. length=1;
  4471. } else if((value&0x8000)==0) {
  4472. value|=0x8e8000;
  4473. length=3;
  4474. } else if((value&0x80)==0) {
  4475. value|=0x8f0080;
  4476. length=3;
  4477. } else {
  4478. length=2;
  4479. }
  4480. break;
  4481. case MBCS_OUTPUT_4_EUC:
  4482. p=MBCS_POINTER_3_FROM_STAGE_2(sharedData->mbcs.fromUnicodeBytes, stage2Entry, c);
  4483. value=((uint32_t)*p<<16)|((uint32_t)p[1]<<8)|p[2];
  4484. /* EUC 16-bit fixed-length representation applied to the first two bytes */
  4485. if(value<=0xff) {
  4486. length=1;
  4487. } else if(value<=0xffff) {
  4488. length=2;
  4489. } else if((value&0x800000)==0) {
  4490. value|=0x8e800000;
  4491. length=4;
  4492. } else if((value&0x8000)==0) {
  4493. value|=0x8f008000;
  4494. length=4;
  4495. } else {
  4496. length=3;
  4497. }
  4498. break;
  4499. #endif
  4500. default:
  4501. /* must not occur */
  4502. return -1;
  4503. }
  4504. /* is this code point assigned, or do we use fallbacks? */
  4505. if( MBCS_FROM_U_IS_ROUNDTRIP(stage2Entry, c) ||
  4506. (FROM_U_USE_FALLBACK(useFallback, c) && value!=0)
  4507. ) {
  4508. /*
  4509. * We allow a 0 byte output if the "assigned" bit is set for this entry.
  4510. * There is no way with this data structure for fallback output
  4511. * to be a zero byte.
  4512. */
  4513. /* assigned */
  4514. *pValue=value;
  4515. return length;
  4516. }
  4517. }
  4518. }
  4519. cx=sharedData->mbcs.extIndexes;
  4520. if(cx!=nullptr) {
  4521. length=ucnv_extSimpleMatchFromU(cx, c, pValue, useFallback);
  4522. return length>=0 ? length : -length; /* return abs(length); */
  4523. }
  4524. /* unassigned */
  4525. return 0;
  4526. }
  4527. #if 0
  4528. /*
  4529. * This function has been moved to ucnv2022.c for inlining.
  4530. * This implementation is here only for documentation purposes
  4531. */
  4532. /**
  4533. * This version of ucnv_MBCSFromUChar32() is optimized for single-byte codepages.
  4534. * It does not handle the EBCDIC swaplfnl option (set in UConverter).
  4535. * It does not handle conversion extensions (_extFromU()).
  4536. *
  4537. * It returns the codepage byte for the code point, or -1 if it is unassigned.
  4538. */
  4539. U_CFUNC int32_t
  4540. ucnv_MBCSSingleFromUChar32(UConverterSharedData *sharedData,
  4541. UChar32 c,
  4542. UBool useFallback) {
  4543. const uint16_t *table;
  4544. int32_t value;
  4545. /* BMP-only codepages are stored without stage 1 entries for supplementary code points */
  4546. if(c>=0x10000 && !(sharedData->mbcs.unicodeMask&UCNV_HAS_SUPPLEMENTARY)) {
  4547. return -1;
  4548. }
  4549. /* convert the Unicode code point in c into codepage bytes (same as in _MBCSFromUnicodeWithOffsets) */
  4550. table=sharedData->mbcs.fromUnicodeTable;
  4551. /* get the byte for the output */
  4552. value=MBCS_SINGLE_RESULT_FROM_U(table, (uint16_t *)sharedData->mbcs.fromUnicodeBytes, c);
  4553. /* is this code point assigned, or do we use fallbacks? */
  4554. if(useFallback ? value>=0x800 : value>=0xc00) {
  4555. return value&0xff;
  4556. } else {
  4557. return -1;
  4558. }
  4559. }
  4560. #endif
  4561. /* MBCS-from-UTF-8 conversion functions ------------------------------------- */
  4562. /* offsets for n-byte UTF-8 sequences that were calculated with ((lead<<6)+trail)<<6+trail... */
  4563. static const UChar32
  4564. utf8_offsets[5]={ 0, 0, 0x3080, 0xE2080, 0x3C82080 };
  4565. static void U_CALLCONV
  4566. ucnv_SBCSFromUTF8(UConverterFromUnicodeArgs *pFromUArgs,
  4567. UConverterToUnicodeArgs *pToUArgs,
  4568. UErrorCode *pErrorCode) {
  4569. UConverter *utf8, *cnv;
  4570. const uint8_t *source, *sourceLimit;
  4571. uint8_t *target;
  4572. int32_t targetCapacity;
  4573. const uint16_t *table, *sbcsIndex;
  4574. const uint16_t *results;
  4575. int8_t oldToULength, toULength, toULimit;
  4576. UChar32 c;
  4577. uint8_t b, t1, t2;
  4578. uint32_t asciiRoundtrips;
  4579. uint16_t value, minValue = 0;
  4580. UBool hasSupplementary;
  4581. /* set up the local pointers */
  4582. utf8=pToUArgs->converter;
  4583. cnv=pFromUArgs->converter;
  4584. source=(uint8_t *)pToUArgs->source;
  4585. sourceLimit=(uint8_t *)pToUArgs->sourceLimit;
  4586. target = reinterpret_cast<uint8_t*>(pFromUArgs->target);
  4587. targetCapacity = static_cast<int32_t>(pFromUArgs->targetLimit - pFromUArgs->target);
  4588. table=cnv->sharedData->mbcs.fromUnicodeTable;
  4589. sbcsIndex=cnv->sharedData->mbcs.sbcsIndex;
  4590. if((cnv->options&UCNV_OPTION_SWAP_LFNL)!=0) {
  4591. results = reinterpret_cast<uint16_t*>(cnv->sharedData->mbcs.swapLFNLFromUnicodeBytes);
  4592. } else {
  4593. results=(uint16_t *)cnv->sharedData->mbcs.fromUnicodeBytes;
  4594. }
  4595. asciiRoundtrips=cnv->sharedData->mbcs.asciiRoundtrips;
  4596. if(cnv->useFallback) {
  4597. /* use all roundtrip and fallback results */
  4598. minValue=0x800;
  4599. } else {
  4600. /* use only roundtrips and fallbacks from private-use characters */
  4601. minValue=0xc00;
  4602. }
  4603. hasSupplementary = static_cast<UBool>(cnv->sharedData->mbcs.unicodeMask & UCNV_HAS_SUPPLEMENTARY);
  4604. /* get the converter state from the UTF-8 UConverter */
  4605. if(utf8->toULength > 0) {
  4606. toULength=oldToULength=utf8->toULength;
  4607. toULimit = static_cast<int8_t>(utf8->mode);
  4608. c = static_cast<UChar32>(utf8->toUnicodeStatus);
  4609. } else {
  4610. toULength=oldToULength=toULimit=0;
  4611. c = 0;
  4612. }
  4613. // The conversion loop checks source<sourceLimit only once per 1/2/3-byte character.
  4614. // If the buffer ends with a truncated 2- or 3-byte sequence,
  4615. // then we reduce the sourceLimit to before that,
  4616. // and collect the remaining bytes after the conversion loop.
  4617. {
  4618. // Do not go back into the bytes that will be read for finishing a partial
  4619. // sequence from the previous buffer.
  4620. int32_t length = static_cast<int32_t>(sourceLimit - source) - (toULimit - oldToULength);
  4621. if(length>0) {
  4622. uint8_t b1=*(sourceLimit-1);
  4623. if(U8_IS_SINGLE(b1)) {
  4624. // common ASCII character
  4625. } else if(U8_IS_TRAIL(b1) && length>=2) {
  4626. uint8_t b2=*(sourceLimit-2);
  4627. if(0xe0<=b2 && b2<0xf0 && U8_IS_VALID_LEAD3_AND_T1(b2, b1)) {
  4628. // truncated 3-byte sequence
  4629. sourceLimit-=2;
  4630. }
  4631. } else if(0xc2<=b1 && b1<0xf0) {
  4632. // truncated 2- or 3-byte sequence
  4633. --sourceLimit;
  4634. }
  4635. }
  4636. }
  4637. if(c!=0 && targetCapacity>0) {
  4638. utf8->toUnicodeStatus=0;
  4639. utf8->toULength=0;
  4640. goto moreBytes;
  4641. /*
  4642. * Note: We could avoid the goto by duplicating some of the moreBytes
  4643. * code, but only up to the point of collecting a complete UTF-8
  4644. * sequence; then recurse for the toUBytes[toULength]
  4645. * and then continue with normal conversion.
  4646. *
  4647. * If so, move this code to just after initializing the minimum
  4648. * set of local variables for reading the UTF-8 input
  4649. * (utf8, source, target, limits but not cnv, table, minValue, etc.).
  4650. *
  4651. * Potential advantages:
  4652. * - avoid the goto
  4653. * - oldToULength could become a local variable in just those code blocks
  4654. * that deal with buffer boundaries
  4655. * - possibly faster if the goto prevents some compiler optimizations
  4656. * (this would need measuring to confirm)
  4657. * Disadvantage:
  4658. * - code duplication
  4659. */
  4660. }
  4661. /* conversion loop */
  4662. while(source<sourceLimit) {
  4663. if(targetCapacity>0) {
  4664. b=*source++;
  4665. if(U8_IS_SINGLE(b)) {
  4666. /* convert ASCII */
  4667. if(IS_ASCII_ROUNDTRIP(b, asciiRoundtrips)) {
  4668. *target++ = b;
  4669. --targetCapacity;
  4670. continue;
  4671. } else {
  4672. c=b;
  4673. value=SBCS_RESULT_FROM_UTF8(sbcsIndex, results, 0, c);
  4674. }
  4675. } else {
  4676. if(b<0xe0) {
  4677. if( /* handle U+0080..U+07FF inline */
  4678. b>=0xc2 &&
  4679. (t1 = static_cast<uint8_t>(*source - 0x80)) <= 0x3f
  4680. ) {
  4681. c=b&0x1f;
  4682. ++source;
  4683. value=SBCS_RESULT_FROM_UTF8(sbcsIndex, results, c, t1);
  4684. if(value>=minValue) {
  4685. *target++ = static_cast<uint8_t>(value);
  4686. --targetCapacity;
  4687. continue;
  4688. } else {
  4689. c=(c<<6)|t1;
  4690. }
  4691. } else {
  4692. c=-1;
  4693. }
  4694. } else if(b==0xe0) {
  4695. if( /* handle U+0800..U+0FFF inline */
  4696. (t1 = static_cast<uint8_t>(source[0] - 0x80)) <= 0x3f && t1 >= 0x20 &&
  4697. (t2 = static_cast<uint8_t>(source[1] - 0x80)) <= 0x3f
  4698. ) {
  4699. c=t1;
  4700. source+=2;
  4701. value=SBCS_RESULT_FROM_UTF8(sbcsIndex, results, c, t2);
  4702. if(value>=minValue) {
  4703. *target++ = static_cast<uint8_t>(value);
  4704. --targetCapacity;
  4705. continue;
  4706. } else {
  4707. c=(c<<6)|t2;
  4708. }
  4709. } else {
  4710. c=-1;
  4711. }
  4712. } else {
  4713. c=-1;
  4714. }
  4715. if(c<0) {
  4716. /* handle "complicated" and error cases, and continuing partial characters */
  4717. oldToULength=0;
  4718. toULength=1;
  4719. toULimit=U8_COUNT_BYTES_NON_ASCII(b);
  4720. c=b;
  4721. moreBytes:
  4722. while(toULength<toULimit) {
  4723. /*
  4724. * The sourceLimit may have been adjusted before the conversion loop
  4725. * to stop before a truncated sequence.
  4726. * Here we need to use the real limit in case we have two truncated
  4727. * sequences at the end.
  4728. * See ticket #7492.
  4729. */
  4730. if(source<(uint8_t *)pToUArgs->sourceLimit) {
  4731. b=*source;
  4732. if(icu::UTF8::isValidTrail(c, b, toULength, toULimit)) {
  4733. ++source;
  4734. ++toULength;
  4735. c=(c<<6)+b;
  4736. } else {
  4737. break; /* sequence too short, stop with toULength<toULimit */
  4738. }
  4739. } else {
  4740. /* store the partial UTF-8 character, compatible with the regular UTF-8 converter */
  4741. source-=(toULength-oldToULength);
  4742. while(oldToULength<toULength) {
  4743. utf8->toUBytes[oldToULength++]=*source++;
  4744. }
  4745. utf8->toUnicodeStatus=c;
  4746. utf8->toULength=toULength;
  4747. utf8->mode=toULimit;
  4748. pToUArgs->source=(char *)source;
  4749. pFromUArgs->target = reinterpret_cast<char*>(target);
  4750. return;
  4751. }
  4752. }
  4753. if(toULength==toULimit) {
  4754. c-=utf8_offsets[toULength];
  4755. if(toULength<=3) { /* BMP */
  4756. value=MBCS_SINGLE_RESULT_FROM_U(table, results, c);
  4757. } else {
  4758. /* supplementary code point */
  4759. if(!hasSupplementary) {
  4760. /* BMP-only codepages are stored without stage 1 entries for supplementary code points */
  4761. value=0;
  4762. } else {
  4763. value=MBCS_SINGLE_RESULT_FROM_U(table, results, c);
  4764. }
  4765. }
  4766. } else {
  4767. /* error handling: illegal UTF-8 byte sequence */
  4768. source-=(toULength-oldToULength);
  4769. while(oldToULength<toULength) {
  4770. utf8->toUBytes[oldToULength++]=*source++;
  4771. }
  4772. utf8->toULength=toULength;
  4773. pToUArgs->source=(char *)source;
  4774. pFromUArgs->target = reinterpret_cast<char*>(target);
  4775. *pErrorCode=U_ILLEGAL_CHAR_FOUND;
  4776. return;
  4777. }
  4778. }
  4779. }
  4780. if(value>=minValue) {
  4781. /* output the mapping for c */
  4782. *target++ = static_cast<uint8_t>(value);
  4783. --targetCapacity;
  4784. } else {
  4785. /* value<minValue means c is unassigned (unmappable) */
  4786. /*
  4787. * Try an extension mapping.
  4788. * Pass in no source because we don't have UTF-16 input.
  4789. * If we have a partial match on c, we will return and revert
  4790. * to UTF-8->UTF-16->charset conversion.
  4791. */
  4792. static const char16_t nul=0;
  4793. const char16_t *noSource=&nul;
  4794. c=_extFromU(cnv, cnv->sharedData,
  4795. c, &noSource, noSource,
  4796. &target, target+targetCapacity,
  4797. nullptr, -1,
  4798. pFromUArgs->flush,
  4799. pErrorCode);
  4800. if(U_FAILURE(*pErrorCode)) {
  4801. /* not mappable or buffer overflow */
  4802. cnv->fromUChar32=c;
  4803. break;
  4804. } else if(cnv->preFromUFirstCP>=0) {
  4805. /*
  4806. * Partial match, return and revert to pivoting.
  4807. * In normal from-UTF-16 conversion, we would just continue
  4808. * but then exit the loop because the extension match would
  4809. * have consumed the source.
  4810. */
  4811. *pErrorCode=U_USING_DEFAULT_WARNING;
  4812. break;
  4813. } else {
  4814. /* a mapping was written to the target, continue */
  4815. /* recalculate the targetCapacity after an extension mapping */
  4816. targetCapacity = static_cast<int32_t>(pFromUArgs->targetLimit - reinterpret_cast<char*>(target));
  4817. }
  4818. }
  4819. } else {
  4820. /* target is full */
  4821. *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
  4822. break;
  4823. }
  4824. }
  4825. /*
  4826. * The sourceLimit may have been adjusted before the conversion loop
  4827. * to stop before a truncated sequence.
  4828. * If so, then collect the truncated sequence now.
  4829. */
  4830. if(U_SUCCESS(*pErrorCode) &&
  4831. cnv->preFromUFirstCP<0 &&
  4832. source<(sourceLimit=(uint8_t *)pToUArgs->sourceLimit)) {
  4833. c=utf8->toUBytes[0]=b=*source++;
  4834. toULength=1;
  4835. toULimit=U8_COUNT_BYTES(b);
  4836. while(source<sourceLimit) {
  4837. utf8->toUBytes[toULength++]=b=*source++;
  4838. c=(c<<6)+b;
  4839. }
  4840. utf8->toUnicodeStatus=c;
  4841. utf8->toULength=toULength;
  4842. utf8->mode=toULimit;
  4843. }
  4844. /* write back the updated pointers */
  4845. pToUArgs->source=(char *)source;
  4846. pFromUArgs->target = reinterpret_cast<char*>(target);
  4847. }
  4848. static void U_CALLCONV
  4849. ucnv_DBCSFromUTF8(UConverterFromUnicodeArgs *pFromUArgs,
  4850. UConverterToUnicodeArgs *pToUArgs,
  4851. UErrorCode *pErrorCode) {
  4852. UConverter *utf8, *cnv;
  4853. const uint8_t *source, *sourceLimit;
  4854. uint8_t *target;
  4855. int32_t targetCapacity;
  4856. const uint16_t *table, *mbcsIndex;
  4857. const uint16_t *results;
  4858. int8_t oldToULength, toULength, toULimit;
  4859. UChar32 c;
  4860. uint8_t b, t1, t2;
  4861. uint32_t stage2Entry;
  4862. uint32_t asciiRoundtrips;
  4863. uint16_t value = 0;
  4864. UBool hasSupplementary;
  4865. /* set up the local pointers */
  4866. utf8=pToUArgs->converter;
  4867. cnv=pFromUArgs->converter;
  4868. source=(uint8_t *)pToUArgs->source;
  4869. sourceLimit=(uint8_t *)pToUArgs->sourceLimit;
  4870. target = reinterpret_cast<uint8_t*>(pFromUArgs->target);
  4871. targetCapacity = static_cast<int32_t>(pFromUArgs->targetLimit - pFromUArgs->target);
  4872. table=cnv->sharedData->mbcs.fromUnicodeTable;
  4873. mbcsIndex=cnv->sharedData->mbcs.mbcsIndex;
  4874. if((cnv->options&UCNV_OPTION_SWAP_LFNL)!=0) {
  4875. results = reinterpret_cast<uint16_t*>(cnv->sharedData->mbcs.swapLFNLFromUnicodeBytes);
  4876. } else {
  4877. results=(uint16_t *)cnv->sharedData->mbcs.fromUnicodeBytes;
  4878. }
  4879. asciiRoundtrips=cnv->sharedData->mbcs.asciiRoundtrips;
  4880. hasSupplementary = static_cast<UBool>(cnv->sharedData->mbcs.unicodeMask & UCNV_HAS_SUPPLEMENTARY);
  4881. /* get the converter state from the UTF-8 UConverter */
  4882. if(utf8->toULength > 0) {
  4883. toULength=oldToULength=utf8->toULength;
  4884. toULimit = static_cast<int8_t>(utf8->mode);
  4885. c = static_cast<UChar32>(utf8->toUnicodeStatus);
  4886. } else {
  4887. toULength=oldToULength=toULimit=0;
  4888. c = 0;
  4889. }
  4890. // The conversion loop checks source<sourceLimit only once per 1/2/3-byte character.
  4891. // If the buffer ends with a truncated 2- or 3-byte sequence,
  4892. // then we reduce the sourceLimit to before that,
  4893. // and collect the remaining bytes after the conversion loop.
  4894. {
  4895. // Do not go back into the bytes that will be read for finishing a partial
  4896. // sequence from the previous buffer.
  4897. int32_t length = static_cast<int32_t>(sourceLimit - source) - (toULimit - oldToULength);
  4898. if(length>0) {
  4899. uint8_t b1=*(sourceLimit-1);
  4900. if(U8_IS_SINGLE(b1)) {
  4901. // common ASCII character
  4902. } else if(U8_IS_TRAIL(b1) && length>=2) {
  4903. uint8_t b2=*(sourceLimit-2);
  4904. if(0xe0<=b2 && b2<0xf0 && U8_IS_VALID_LEAD3_AND_T1(b2, b1)) {
  4905. // truncated 3-byte sequence
  4906. sourceLimit-=2;
  4907. }
  4908. } else if(0xc2<=b1 && b1<0xf0) {
  4909. // truncated 2- or 3-byte sequence
  4910. --sourceLimit;
  4911. }
  4912. }
  4913. }
  4914. if(c!=0 && targetCapacity>0) {
  4915. utf8->toUnicodeStatus=0;
  4916. utf8->toULength=0;
  4917. goto moreBytes;
  4918. /* See note in ucnv_SBCSFromUTF8() about this goto. */
  4919. }
  4920. /* conversion loop */
  4921. while(source<sourceLimit) {
  4922. if(targetCapacity>0) {
  4923. b=*source++;
  4924. if(U8_IS_SINGLE(b)) {
  4925. /* convert ASCII */
  4926. if(IS_ASCII_ROUNDTRIP(b, asciiRoundtrips)) {
  4927. *target++=b;
  4928. --targetCapacity;
  4929. continue;
  4930. } else {
  4931. value=DBCS_RESULT_FROM_UTF8(mbcsIndex, results, 0, b);
  4932. if(value==0) {
  4933. c=b;
  4934. goto unassigned;
  4935. }
  4936. }
  4937. } else {
  4938. if(b>=0xe0) {
  4939. if( /* handle U+0800..U+D7FF inline */
  4940. b<=0xed && // do not assume maxFastUChar>0xd7ff
  4941. U8_IS_VALID_LEAD3_AND_T1(b, t1=source[0]) &&
  4942. (t2 = static_cast<uint8_t>(source[1] - 0x80)) <= 0x3f
  4943. ) {
  4944. c=((b&0xf)<<6)|(t1&0x3f);
  4945. source+=2;
  4946. value=DBCS_RESULT_FROM_UTF8(mbcsIndex, results, c, t2);
  4947. if(value==0) {
  4948. c=(c<<6)|t2;
  4949. goto unassigned;
  4950. }
  4951. } else {
  4952. c=-1;
  4953. }
  4954. } else {
  4955. if( /* handle U+0080..U+07FF inline */
  4956. b>=0xc2 &&
  4957. (t1 = static_cast<uint8_t>(*source - 0x80)) <= 0x3f
  4958. ) {
  4959. c=b&0x1f;
  4960. ++source;
  4961. value=DBCS_RESULT_FROM_UTF8(mbcsIndex, results, c, t1);
  4962. if(value==0) {
  4963. c=(c<<6)|t1;
  4964. goto unassigned;
  4965. }
  4966. } else {
  4967. c=-1;
  4968. }
  4969. }
  4970. if(c<0) {
  4971. /* handle "complicated" and error cases, and continuing partial characters */
  4972. oldToULength=0;
  4973. toULength=1;
  4974. toULimit=U8_COUNT_BYTES_NON_ASCII(b);
  4975. c=b;
  4976. moreBytes:
  4977. while(toULength<toULimit) {
  4978. /*
  4979. * The sourceLimit may have been adjusted before the conversion loop
  4980. * to stop before a truncated sequence.
  4981. * Here we need to use the real limit in case we have two truncated
  4982. * sequences at the end.
  4983. * See ticket #7492.
  4984. */
  4985. if(source<(uint8_t *)pToUArgs->sourceLimit) {
  4986. b=*source;
  4987. if(icu::UTF8::isValidTrail(c, b, toULength, toULimit)) {
  4988. ++source;
  4989. ++toULength;
  4990. c=(c<<6)+b;
  4991. } else {
  4992. break; /* sequence too short, stop with toULength<toULimit */
  4993. }
  4994. } else {
  4995. /* store the partial UTF-8 character, compatible with the regular UTF-8 converter */
  4996. source-=(toULength-oldToULength);
  4997. while(oldToULength<toULength) {
  4998. utf8->toUBytes[oldToULength++]=*source++;
  4999. }
  5000. utf8->toUnicodeStatus=c;
  5001. utf8->toULength=toULength;
  5002. utf8->mode=toULimit;
  5003. pToUArgs->source=(char *)source;
  5004. pFromUArgs->target = reinterpret_cast<char*>(target);
  5005. return;
  5006. }
  5007. }
  5008. if(toULength==toULimit) {
  5009. c-=utf8_offsets[toULength];
  5010. if(toULength<=3) { /* BMP */
  5011. stage2Entry=MBCS_STAGE_2_FROM_U(table, c);
  5012. } else {
  5013. /* supplementary code point */
  5014. if(!hasSupplementary) {
  5015. /* BMP-only codepages are stored without stage 1 entries for supplementary code points */
  5016. stage2Entry=0;
  5017. } else {
  5018. stage2Entry=MBCS_STAGE_2_FROM_U(table, c);
  5019. }
  5020. }
  5021. } else {
  5022. /* error handling: illegal UTF-8 byte sequence */
  5023. source-=(toULength-oldToULength);
  5024. while(oldToULength<toULength) {
  5025. utf8->toUBytes[oldToULength++]=*source++;
  5026. }
  5027. utf8->toULength=toULength;
  5028. pToUArgs->source=(char *)source;
  5029. pFromUArgs->target = reinterpret_cast<char*>(target);
  5030. *pErrorCode=U_ILLEGAL_CHAR_FOUND;
  5031. return;
  5032. }
  5033. /* get the bytes and the length for the output */
  5034. /* MBCS_OUTPUT_2 */
  5035. value=MBCS_VALUE_2_FROM_STAGE_2(results, stage2Entry, c);
  5036. /* is this code point assigned, or do we use fallbacks? */
  5037. if(!(MBCS_FROM_U_IS_ROUNDTRIP(stage2Entry, c) ||
  5038. (UCNV_FROM_U_USE_FALLBACK(cnv, c) && value!=0))
  5039. ) {
  5040. goto unassigned;
  5041. }
  5042. }
  5043. }
  5044. /* write the output character bytes from value and length */
  5045. /* from the first if in the loop we know that targetCapacity>0 */
  5046. if(value<=0xff) {
  5047. /* this is easy because we know that there is enough space */
  5048. *target++ = static_cast<uint8_t>(value);
  5049. --targetCapacity;
  5050. } else /* length==2 */ {
  5051. *target++ = static_cast<uint8_t>(value >> 8);
  5052. if(2<=targetCapacity) {
  5053. *target++ = static_cast<uint8_t>(value);
  5054. targetCapacity-=2;
  5055. } else {
  5056. cnv->charErrorBuffer[0] = static_cast<char>(value);
  5057. cnv->charErrorBufferLength=1;
  5058. /* target overflow */
  5059. *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
  5060. break;
  5061. }
  5062. }
  5063. continue;
  5064. unassigned:
  5065. {
  5066. /*
  5067. * Try an extension mapping.
  5068. * Pass in no source because we don't have UTF-16 input.
  5069. * If we have a partial match on c, we will return and revert
  5070. * to UTF-8->UTF-16->charset conversion.
  5071. */
  5072. static const char16_t nul=0;
  5073. const char16_t *noSource=&nul;
  5074. c=_extFromU(cnv, cnv->sharedData,
  5075. c, &noSource, noSource,
  5076. &target, target+targetCapacity,
  5077. nullptr, -1,
  5078. pFromUArgs->flush,
  5079. pErrorCode);
  5080. if(U_FAILURE(*pErrorCode)) {
  5081. /* not mappable or buffer overflow */
  5082. cnv->fromUChar32=c;
  5083. break;
  5084. } else if(cnv->preFromUFirstCP>=0) {
  5085. /*
  5086. * Partial match, return and revert to pivoting.
  5087. * In normal from-UTF-16 conversion, we would just continue
  5088. * but then exit the loop because the extension match would
  5089. * have consumed the source.
  5090. */
  5091. *pErrorCode=U_USING_DEFAULT_WARNING;
  5092. break;
  5093. } else {
  5094. /* a mapping was written to the target, continue */
  5095. /* recalculate the targetCapacity after an extension mapping */
  5096. targetCapacity = static_cast<int32_t>(pFromUArgs->targetLimit - reinterpret_cast<char*>(target));
  5097. continue;
  5098. }
  5099. }
  5100. } else {
  5101. /* target is full */
  5102. *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
  5103. break;
  5104. }
  5105. }
  5106. /*
  5107. * The sourceLimit may have been adjusted before the conversion loop
  5108. * to stop before a truncated sequence.
  5109. * If so, then collect the truncated sequence now.
  5110. */
  5111. if(U_SUCCESS(*pErrorCode) &&
  5112. cnv->preFromUFirstCP<0 &&
  5113. source<(sourceLimit=(uint8_t *)pToUArgs->sourceLimit)) {
  5114. c=utf8->toUBytes[0]=b=*source++;
  5115. toULength=1;
  5116. toULimit=U8_COUNT_BYTES(b);
  5117. while(source<sourceLimit) {
  5118. utf8->toUBytes[toULength++]=b=*source++;
  5119. c=(c<<6)+b;
  5120. }
  5121. utf8->toUnicodeStatus=c;
  5122. utf8->toULength=toULength;
  5123. utf8->mode=toULimit;
  5124. }
  5125. /* write back the updated pointers */
  5126. pToUArgs->source=(char *)source;
  5127. pFromUArgs->target = reinterpret_cast<char*>(target);
  5128. }
  5129. /* miscellaneous ------------------------------------------------------------ */
  5130. static void U_CALLCONV
  5131. ucnv_MBCSGetStarters(const UConverter* cnv,
  5132. UBool starters[256],
  5133. UErrorCode *) {
  5134. const int32_t *state0;
  5135. int i;
  5136. state0=cnv->sharedData->mbcs.stateTable[cnv->sharedData->mbcs.dbcsOnlyState];
  5137. for(i=0; i<256; ++i) {
  5138. /* all bytes that cause a state transition from state 0 are lead bytes */
  5139. starters[i] = static_cast<UBool>(MBCS_ENTRY_IS_TRANSITION(state0[i]));
  5140. }
  5141. }
  5142. /*
  5143. * This is an internal function that allows other converter implementations
  5144. * to check whether a byte is a lead byte.
  5145. */
  5146. U_CFUNC UBool
  5147. ucnv_MBCSIsLeadByte(UConverterSharedData *sharedData, char byte) {
  5148. return MBCS_ENTRY_IS_TRANSITION(sharedData->mbcs.stateTable[0][(uint8_t)byte]);
  5149. }
  5150. static void U_CALLCONV
  5151. ucnv_MBCSWriteSub(UConverterFromUnicodeArgs *pArgs,
  5152. int32_t offsetIndex,
  5153. UErrorCode *pErrorCode) {
  5154. UConverter *cnv=pArgs->converter;
  5155. char *p, *subchar;
  5156. char buffer[4];
  5157. int32_t length;
  5158. /* first, select between subChar and subChar1 */
  5159. if( cnv->subChar1!=0 &&
  5160. (cnv->sharedData->mbcs.extIndexes!=nullptr ?
  5161. cnv->useSubChar1 :
  5162. (cnv->invalidUCharBuffer[0]<=0xff))
  5163. ) {
  5164. /* select subChar1 if it is set (not 0) and the unmappable Unicode code point is up to U+00ff (IBM MBCS behavior) */
  5165. subchar = reinterpret_cast<char*>(&cnv->subChar1);
  5166. length=1;
  5167. } else {
  5168. /* select subChar in all other cases */
  5169. subchar = reinterpret_cast<char*>(cnv->subChars);
  5170. length=cnv->subCharLen;
  5171. }
  5172. /* reset the selector for the next code point */
  5173. cnv->useSubChar1=false;
  5174. if (cnv->sharedData->mbcs.outputType == MBCS_OUTPUT_2_SISO) {
  5175. p=buffer;
  5176. /* fromUnicodeStatus contains prevLength */
  5177. switch(length) {
  5178. case 1:
  5179. if(cnv->fromUnicodeStatus==2) {
  5180. /* DBCS mode and SBCS sub char: change to SBCS */
  5181. cnv->fromUnicodeStatus=1;
  5182. *p++=UCNV_SI;
  5183. }
  5184. *p++=subchar[0];
  5185. break;
  5186. case 2:
  5187. if(cnv->fromUnicodeStatus<=1) {
  5188. /* SBCS mode and DBCS sub char: change to DBCS */
  5189. cnv->fromUnicodeStatus=2;
  5190. *p++=UCNV_SO;
  5191. }
  5192. *p++=subchar[0];
  5193. *p++=subchar[1];
  5194. break;
  5195. default:
  5196. *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
  5197. return;
  5198. }
  5199. subchar=buffer;
  5200. length = static_cast<int32_t>(p - buffer);
  5201. }
  5202. ucnv_cbFromUWriteBytes(pArgs, subchar, length, offsetIndex, pErrorCode);
  5203. }
  5204. U_CFUNC UConverterType
  5205. ucnv_MBCSGetType(const UConverter* converter) {
  5206. /* SBCS, DBCS, and EBCDIC_STATEFUL are replaced by MBCS, but here we cheat a little */
  5207. if(converter->sharedData->mbcs.countStates==1) {
  5208. return (UConverterType)UCNV_SBCS;
  5209. } else if((converter->sharedData->mbcs.outputType&0xff)==MBCS_OUTPUT_2_SISO) {
  5210. return (UConverterType)UCNV_EBCDIC_STATEFUL;
  5211. } else if(converter->sharedData->staticData->minBytesPerChar==2 && converter->sharedData->staticData->maxBytesPerChar==2) {
  5212. return (UConverterType)UCNV_DBCS;
  5213. }
  5214. return (UConverterType)UCNV_MBCS;
  5215. }
  5216. #endif /* #if !UCONFIG_NO_LEGACY_CONVERSION */