libelftc_dem_gnu3.c 76 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474
  1. /*-
  2. * Copyright (c) 2007, 2008 Hyogeol Lee <hyogeollee@gmail.com>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer
  10. * in this position and unchanged.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
  16. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  17. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  18. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  19. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  20. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  21. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  22. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  24. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <sys/types.h>
  27. #include <assert.h>
  28. #include <ctype.h>
  29. #include <errno.h>
  30. #include <limits.h>
  31. #include <stdbool.h>
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <string.h>
  35. /**
  36. * @file cpp_demangle.c
  37. * @brief Decode IA-64 C++ ABI style implementation.
  38. *
  39. * IA-64 standard ABI(Itanium C++ ABI) references.
  40. *
  41. * http://www.codesourcery.com/cxx-abi/abi.html#mangling \n
  42. * http://www.codesourcery.com/cxx-abi/abi-mangling.html
  43. */
  44. /** @brief Dynamic vector data for string. */
  45. struct vector_str {
  46. /** Current size */
  47. size_t size;
  48. /** Total capacity */
  49. size_t capacity;
  50. /** String array */
  51. char **container;
  52. };
  53. #define BUFFER_GROWFACTOR 1.618
  54. #define VECTOR_DEF_CAPACITY 8
  55. #define ELFTC_ISDIGIT(C) (isdigit((C) & 0xFF))
  56. enum type_qualifier {
  57. TYPE_PTR, TYPE_REF, TYPE_CMX, TYPE_IMG, TYPE_EXT, TYPE_RST, TYPE_VAT,
  58. TYPE_CST
  59. };
  60. struct vector_type_qualifier {
  61. size_t size, capacity;
  62. enum type_qualifier *q_container;
  63. struct vector_str ext_name;
  64. };
  65. enum read_cmd {
  66. READ_FAIL, READ_NEST, READ_TMPL, READ_EXPR, READ_EXPL, READ_LOCAL,
  67. READ_TYPE, READ_FUNC, READ_PTRMEM
  68. };
  69. struct vector_read_cmd {
  70. size_t size, capacity;
  71. enum read_cmd *r_container;
  72. };
  73. struct cpp_demangle_data {
  74. struct vector_str output; /* output string vector */
  75. struct vector_str output_tmp;
  76. struct vector_str subst; /* substitution string vector */
  77. struct vector_str tmpl;
  78. struct vector_str class_type;
  79. struct vector_read_cmd cmd;
  80. bool paren; /* parenthesis opened */
  81. bool pfirst; /* first element of parameter */
  82. bool mem_rst; /* restrict member function */
  83. bool mem_vat; /* volatile member function */
  84. bool mem_cst; /* const member function */
  85. int func_type;
  86. const char *cur; /* current mangled name ptr */
  87. const char *last_sname; /* last source name */
  88. int push_head;
  89. };
  90. #define CPP_DEMANGLE_TRY_LIMIT 128
  91. #define FLOAT_SPRINTF_TRY_LIMIT 5
  92. #define FLOAT_QUADRUPLE_BYTES 16
  93. #define FLOAT_EXTENED_BYTES 10
  94. #define SIMPLE_HASH(x,y) (64 * x + y)
  95. static size_t get_strlen_sum(const struct vector_str *v);
  96. static bool vector_str_grow(struct vector_str *v);
  97. static size_t
  98. get_strlen_sum(const struct vector_str *v)
  99. {
  100. size_t i, len = 0;
  101. if (v == NULL)
  102. return (0);
  103. assert(v->size > 0);
  104. for (i = 0; i < v->size; ++i)
  105. len += strlen(v->container[i]);
  106. return (len);
  107. }
  108. /**
  109. * @brief Deallocate resource in vector_str.
  110. */
  111. static void
  112. vector_str_dest(struct vector_str *v)
  113. {
  114. size_t i;
  115. if (v == NULL)
  116. return;
  117. for (i = 0; i < v->size; ++i)
  118. free(v->container[i]);
  119. free(v->container);
  120. }
  121. /**
  122. * @brief Find string in vector_str.
  123. * @param v Destination vector.
  124. * @param o String to find.
  125. * @param l Length of the string.
  126. * @return -1 at failed, 0 at not found, 1 at found.
  127. */
  128. static int
  129. vector_str_find(const struct vector_str *v, const char *o, size_t l)
  130. {
  131. size_t i;
  132. if (v == NULL || o == NULL)
  133. return (-1);
  134. for (i = 0; i < v->size; ++i)
  135. if (strncmp(v->container[i], o, l) == 0)
  136. return (1);
  137. return (0);
  138. }
  139. /**
  140. * @brief Get new allocated flat string from vector.
  141. *
  142. * If l is not NULL, return length of the string.
  143. * @param v Destination vector.
  144. * @param l Length of the string.
  145. * @return NULL at failed or NUL terminated new allocated string.
  146. */
  147. static char *
  148. vector_str_get_flat(const struct vector_str *v, size_t *l)
  149. {
  150. ssize_t elem_pos, elem_size, rtn_size;
  151. size_t i;
  152. char *rtn;
  153. if (v == NULL || v->size == 0)
  154. return (NULL);
  155. if ((rtn_size = get_strlen_sum(v)) == 0)
  156. return (NULL);
  157. if ((rtn = malloc(sizeof(char) * (rtn_size + 1))) == NULL)
  158. return (NULL);
  159. elem_pos = 0;
  160. for (i = 0; i < v->size; ++i) {
  161. elem_size = strlen(v->container[i]);
  162. memcpy(rtn + elem_pos, v->container[i], elem_size);
  163. elem_pos += elem_size;
  164. }
  165. rtn[rtn_size] = '\0';
  166. if (l != NULL)
  167. *l = rtn_size;
  168. return (rtn);
  169. }
  170. static bool
  171. vector_str_grow(struct vector_str *v)
  172. {
  173. size_t i, tmp_cap;
  174. char **tmp_ctn;
  175. if (v == NULL)
  176. return (false);
  177. assert(v->capacity > 0);
  178. tmp_cap = v->capacity * BUFFER_GROWFACTOR;
  179. assert(tmp_cap > v->capacity);
  180. if ((tmp_ctn = malloc(sizeof(char *) * tmp_cap)) == NULL)
  181. return (false);
  182. for (i = 0; i < v->size; ++i)
  183. tmp_ctn[i] = v->container[i];
  184. free(v->container);
  185. v->container = tmp_ctn;
  186. v->capacity = tmp_cap;
  187. return (true);
  188. }
  189. /**
  190. * @brief Initialize vector_str.
  191. * @return false at failed, true at success.
  192. */
  193. static bool
  194. vector_str_init(struct vector_str *v)
  195. {
  196. if (v == NULL)
  197. return (false);
  198. v->size = 0;
  199. v->capacity = VECTOR_DEF_CAPACITY;
  200. assert(v->capacity > 0);
  201. if ((v->container = malloc(sizeof(char *) * v->capacity)) == NULL)
  202. return (false);
  203. assert(v->container != NULL);
  204. return (true);
  205. }
  206. /**
  207. * @brief Remove last element in vector_str.
  208. * @return false at failed, true at success.
  209. */
  210. static bool
  211. vector_str_pop(struct vector_str *v)
  212. {
  213. if (v == NULL)
  214. return (false);
  215. if (v->size == 0)
  216. return (true);
  217. --v->size;
  218. free(v->container[v->size]);
  219. v->container[v->size] = NULL;
  220. return (true);
  221. }
  222. /**
  223. * @brief Push back string to vector.
  224. * @return false at failed, true at success.
  225. */
  226. static bool
  227. vector_str_push(struct vector_str *v, const char *str, size_t len)
  228. {
  229. if (v == NULL || str == NULL)
  230. return (false);
  231. if (v->size == v->capacity && vector_str_grow(v) == false)
  232. return (false);
  233. if ((v->container[v->size] = malloc(sizeof(char) * (len + 1))) == NULL)
  234. return (false);
  235. snprintf(v->container[v->size], len + 1, "%s", str);
  236. ++v->size;
  237. return (true);
  238. }
  239. /**
  240. * @brief Push front org vector to det vector.
  241. * @return false at failed, true at success.
  242. */
  243. static bool
  244. vector_str_push_vector_head(struct vector_str *dst, struct vector_str *org)
  245. {
  246. size_t i, j, tmp_cap;
  247. char **tmp_ctn;
  248. if (dst == NULL || org == NULL)
  249. return (false);
  250. tmp_cap = (dst->size + org->size) * BUFFER_GROWFACTOR;
  251. if ((tmp_ctn = malloc(sizeof(char *) * tmp_cap)) == NULL)
  252. return (false);
  253. for (i = 0; i < org->size; ++i)
  254. if ((tmp_ctn[i] = strdup(org->container[i])) == NULL) {
  255. for (j = 0; j < i; ++j)
  256. free(tmp_ctn[j]);
  257. free(tmp_ctn);
  258. return (false);
  259. }
  260. for (i = 0; i < dst->size; ++i)
  261. tmp_ctn[i + org->size] = dst->container[i];
  262. free(dst->container);
  263. dst->container = tmp_ctn;
  264. dst->capacity = tmp_cap;
  265. dst->size += org->size;
  266. return (true);
  267. }
  268. /**
  269. * @brief Get new allocated flat string from vector between begin and end.
  270. *
  271. * If r_len is not NULL, string length will be returned.
  272. * @return NULL at failed or NUL terminated new allocated string.
  273. */
  274. static char *
  275. vector_str_substr(const struct vector_str *v, size_t begin, size_t end,
  276. size_t *r_len)
  277. {
  278. size_t cur, i, len;
  279. char *rtn;
  280. if (v == NULL || begin > end)
  281. return (NULL);
  282. len = 0;
  283. for (i = begin; i < end + 1; ++i)
  284. len += strlen(v->container[i]);
  285. if ((rtn = malloc(sizeof(char) * (len + 1))) == NULL)
  286. return (NULL);
  287. if (r_len != NULL)
  288. *r_len = len;
  289. cur = 0;
  290. for (i = begin; i < end + 1; ++i) {
  291. len = strlen(v->container[i]);
  292. memcpy(rtn + cur, v->container[i], len);
  293. cur += len;
  294. }
  295. rtn[cur] = '\0';
  296. return (rtn);
  297. }
  298. static void cpp_demangle_data_dest(struct cpp_demangle_data *);
  299. static int cpp_demangle_data_init(struct cpp_demangle_data *,
  300. const char *);
  301. static int cpp_demangle_get_subst(struct cpp_demangle_data *, size_t);
  302. static int cpp_demangle_get_tmpl_param(struct cpp_demangle_data *, size_t);
  303. static int cpp_demangle_push_fp(struct cpp_demangle_data *,
  304. char *(*)(const char *, size_t));
  305. static int cpp_demangle_push_str(struct cpp_demangle_data *, const char *,
  306. size_t);
  307. static int cpp_demangle_push_subst(struct cpp_demangle_data *,
  308. const char *, size_t);
  309. static int cpp_demangle_push_subst_v(struct cpp_demangle_data *,
  310. struct vector_str *);
  311. static int cpp_demangle_push_type_qualifier(struct cpp_demangle_data *,
  312. struct vector_type_qualifier *, const char *);
  313. static int cpp_demangle_read_array(struct cpp_demangle_data *);
  314. static int cpp_demangle_read_encoding(struct cpp_demangle_data *);
  315. static int cpp_demangle_read_expr_primary(struct cpp_demangle_data *);
  316. static int cpp_demangle_read_expression(struct cpp_demangle_data *);
  317. static int cpp_demangle_read_expression_binary(struct cpp_demangle_data *,
  318. const char *, size_t);
  319. static int cpp_demangle_read_expression_unary(struct cpp_demangle_data *,
  320. const char *, size_t);
  321. static int cpp_demangle_read_expression_trinary(struct cpp_demangle_data *,
  322. const char *, size_t, const char *, size_t);
  323. static int cpp_demangle_read_function(struct cpp_demangle_data *, int *,
  324. struct vector_type_qualifier *);
  325. static int cpp_demangle_read_local_name(struct cpp_demangle_data *);
  326. static int cpp_demangle_read_name(struct cpp_demangle_data *);
  327. static int cpp_demangle_read_nested_name(struct cpp_demangle_data *);
  328. static int cpp_demangle_read_number(struct cpp_demangle_data *, long *);
  329. static int cpp_demangle_read_nv_offset(struct cpp_demangle_data *);
  330. static int cpp_demangle_read_offset(struct cpp_demangle_data *);
  331. static int cpp_demangle_read_offset_number(struct cpp_demangle_data *);
  332. static int cpp_demangle_read_pointer_to_member(struct cpp_demangle_data *);
  333. static int cpp_demangle_read_sname(struct cpp_demangle_data *);
  334. static int cpp_demangle_read_subst(struct cpp_demangle_data *);
  335. static int cpp_demangle_read_subst_std(struct cpp_demangle_data *);
  336. static int cpp_demangle_read_subst_stdtmpl(struct cpp_demangle_data *,
  337. const char *, size_t);
  338. static int cpp_demangle_read_tmpl_arg(struct cpp_demangle_data *);
  339. static int cpp_demangle_read_tmpl_args(struct cpp_demangle_data *);
  340. static int cpp_demangle_read_tmpl_param(struct cpp_demangle_data *);
  341. static int cpp_demangle_read_type(struct cpp_demangle_data *, int);
  342. static int cpp_demangle_read_uqname(struct cpp_demangle_data *);
  343. static int cpp_demangle_read_v_offset(struct cpp_demangle_data *);
  344. static char *decode_fp_to_double(const char *, size_t);
  345. static char *decode_fp_to_float(const char *, size_t);
  346. static char *decode_fp_to_float128(const char *, size_t);
  347. static char *decode_fp_to_float80(const char *, size_t);
  348. static char *decode_fp_to_long_double(const char *, size_t);
  349. static int hex_to_dec(char);
  350. static void vector_read_cmd_dest(struct vector_read_cmd *);
  351. static int vector_read_cmd_find(struct vector_read_cmd *, enum read_cmd);
  352. static int vector_read_cmd_init(struct vector_read_cmd *);
  353. static int vector_read_cmd_pop(struct vector_read_cmd *);
  354. static int vector_read_cmd_push(struct vector_read_cmd *, enum read_cmd);
  355. static void vector_type_qualifier_dest(struct vector_type_qualifier *);
  356. static int vector_type_qualifier_init(struct vector_type_qualifier *);
  357. static int vector_type_qualifier_push(struct vector_type_qualifier *,
  358. enum type_qualifier);
  359. /**
  360. * @brief Decode the input string by IA-64 C++ ABI style.
  361. *
  362. * GNU GCC v3 use IA-64 standard ABI.
  363. * @return New allocated demangled string or NULL if failed.
  364. * @todo 1. Testing and more test case. 2. Code cleaning.
  365. */
  366. char *
  367. __cxa_demangle_gnu3(const char *org)
  368. {
  369. struct cpp_demangle_data ddata;
  370. ssize_t org_len;
  371. unsigned int limit;
  372. char *rtn = NULL;
  373. if (org == NULL)
  374. return (NULL);
  375. // Try demangling as a type for short encodings
  376. if (((org_len = strlen(org)) < 2) || (org[0] != '_' || org[1] != 'Z' )) {
  377. if (!cpp_demangle_data_init(&ddata, org))
  378. return (NULL);
  379. if (!cpp_demangle_read_type(&ddata, 0))
  380. goto clean;
  381. rtn = vector_str_get_flat(&ddata.output, (size_t *) NULL);
  382. goto clean;
  383. }
  384. if (org_len > 11 && !strncmp(org, "_GLOBAL__I_", 11)) {
  385. if ((rtn = malloc(org_len + 19)) == NULL)
  386. return (NULL);
  387. snprintf(rtn, org_len + 19,
  388. "global constructors keyed to %s", org + 11);
  389. return (rtn);
  390. }
  391. if (!cpp_demangle_data_init(&ddata, org + 2))
  392. return (NULL);
  393. if (!cpp_demangle_read_encoding(&ddata))
  394. goto clean;
  395. limit = 0;
  396. while (*ddata.cur != '\0') {
  397. /*
  398. * Breaking at some gcc info at tail. e.g) @@GLIBCXX_3.4
  399. */
  400. if (*ddata.cur == '@' && *(ddata.cur + 1) == '@')
  401. break;
  402. if (!cpp_demangle_read_type(&ddata, 1))
  403. goto clean;
  404. if (limit++ > CPP_DEMANGLE_TRY_LIMIT)
  405. goto clean;
  406. }
  407. if (ddata.output.size == 0)
  408. goto clean;
  409. if (ddata.paren && !vector_str_push(&ddata.output, ")", 1))
  410. goto clean;
  411. if (ddata.mem_vat && !vector_str_push(&ddata.output, " volatile", 9))
  412. goto clean;
  413. if (ddata.mem_cst && !vector_str_push(&ddata.output, " const", 6))
  414. goto clean;
  415. if (ddata.mem_rst && !vector_str_push(&ddata.output, " restrict", 9))
  416. goto clean;
  417. rtn = vector_str_get_flat(&ddata.output, (size_t *) NULL);
  418. clean:
  419. cpp_demangle_data_dest(&ddata);
  420. return (rtn);
  421. }
  422. char *
  423. __cxa_demangle(const char*org) __attribute__((weak, alias("__cxa_demangle_gnu3")));
  424. static void
  425. cpp_demangle_data_dest(struct cpp_demangle_data *d)
  426. {
  427. if (d == NULL)
  428. return;
  429. vector_read_cmd_dest(&d->cmd);
  430. vector_str_dest(&d->class_type);
  431. vector_str_dest(&d->tmpl);
  432. vector_str_dest(&d->subst);
  433. vector_str_dest(&d->output_tmp);
  434. vector_str_dest(&d->output);
  435. }
  436. static int
  437. cpp_demangle_data_init(struct cpp_demangle_data *d, const char *cur)
  438. {
  439. if (d == NULL || cur == NULL)
  440. return (0);
  441. if (!vector_str_init(&d->output))
  442. return (0);
  443. if (!vector_str_init(&d->output_tmp))
  444. goto clean1;
  445. if (!vector_str_init(&d->subst))
  446. goto clean2;
  447. if (!vector_str_init(&d->tmpl))
  448. goto clean3;
  449. if (!vector_str_init(&d->class_type))
  450. goto clean4;
  451. if (!vector_read_cmd_init(&d->cmd))
  452. goto clean5;
  453. assert(d->output.container != NULL);
  454. assert(d->output_tmp.container != NULL);
  455. assert(d->subst.container != NULL);
  456. assert(d->tmpl.container != NULL);
  457. assert(d->class_type.container != NULL);
  458. d->paren = false;
  459. d->pfirst = false;
  460. d->mem_rst = false;
  461. d->mem_vat = false;
  462. d->mem_cst = false;
  463. d->func_type = 0;
  464. d->cur = cur;
  465. d->last_sname = NULL;
  466. d->push_head = 0;
  467. return (1);
  468. clean5:
  469. vector_str_dest(&d->class_type);
  470. clean4:
  471. vector_str_dest(&d->tmpl);
  472. clean3:
  473. vector_str_dest(&d->subst);
  474. clean2:
  475. vector_str_dest(&d->output_tmp);
  476. clean1:
  477. vector_str_dest(&d->output);
  478. return (0);
  479. }
  480. static int
  481. cpp_demangle_push_fp(struct cpp_demangle_data *ddata,
  482. char *(*decoder)(const char *, size_t))
  483. {
  484. size_t len;
  485. int rtn;
  486. const char *fp;
  487. char *f;
  488. if (ddata == NULL || decoder == NULL)
  489. return (0);
  490. fp = ddata->cur;
  491. while (*ddata->cur != 'E')
  492. ++ddata->cur;
  493. ++ddata->cur;
  494. if ((f = decoder(fp, ddata->cur - fp)) == NULL)
  495. return (0);
  496. rtn = 0;
  497. if ((len = strlen(f)) > 0 &&
  498. cpp_demangle_push_str(ddata, f, len))
  499. rtn = 1;
  500. free(f);
  501. return (rtn);
  502. }
  503. static int
  504. cpp_demangle_push_str(struct cpp_demangle_data *ddata, const char *str,
  505. size_t len)
  506. {
  507. if (ddata == NULL || str == NULL || len == 0)
  508. return (0);
  509. if (ddata->push_head > 0)
  510. return (vector_str_push(&ddata->output_tmp, str, len));
  511. return (vector_str_push(&ddata->output, str, len));
  512. }
  513. static int
  514. cpp_demangle_push_subst(struct cpp_demangle_data *ddata, const char *str,
  515. size_t len)
  516. {
  517. if (ddata == NULL || str == NULL || len == 0)
  518. return (0);
  519. if (!vector_str_find(&ddata->subst, str, len))
  520. return (vector_str_push(&ddata->subst, str, len));
  521. return (1);
  522. }
  523. static int
  524. cpp_demangle_push_subst_v(struct cpp_demangle_data *ddata, struct vector_str *v)
  525. {
  526. size_t str_len;
  527. int rtn;
  528. char *str;
  529. if (ddata == NULL || v == NULL)
  530. return (0);
  531. if ((str = vector_str_get_flat(v, &str_len)) == NULL)
  532. return (0);
  533. rtn = cpp_demangle_push_subst(ddata, str, str_len);
  534. free(str);
  535. return (rtn);
  536. }
  537. static int
  538. cpp_demangle_push_type_qualifier(struct cpp_demangle_data *ddata,
  539. struct vector_type_qualifier *v, const char *type_str)
  540. {
  541. struct vector_str subst_v;
  542. size_t idx, e_idx, e_len;
  543. int rtn;
  544. char *buf;
  545. if (ddata == NULL || v == NULL)
  546. return (0);
  547. if ((idx = v->size) == 0)
  548. return (1);
  549. rtn = 0;
  550. if (type_str != NULL) {
  551. if (!vector_str_init(&subst_v))
  552. return (0);
  553. if (!vector_str_push(&subst_v, type_str, strlen(type_str)))
  554. goto clean;
  555. }
  556. e_idx = 0;
  557. while (idx > 0) {
  558. switch (v->q_container[idx - 1]) {
  559. case TYPE_PTR:
  560. if (!cpp_demangle_push_str(ddata, "*", 1))
  561. goto clean;
  562. if (type_str != NULL) {
  563. if (!vector_str_push(&subst_v, "*", 1))
  564. goto clean;
  565. if (!cpp_demangle_push_subst_v(ddata, &subst_v))
  566. goto clean;
  567. }
  568. break;
  569. case TYPE_REF:
  570. if (!cpp_demangle_push_str(ddata, "&", 1))
  571. goto clean;
  572. if (type_str != NULL) {
  573. if (!vector_str_push(&subst_v, "&", 1))
  574. goto clean;
  575. if (!cpp_demangle_push_subst_v(ddata, &subst_v))
  576. goto clean;
  577. }
  578. break;
  579. case TYPE_CMX:
  580. if (!cpp_demangle_push_str(ddata, " complex", 8))
  581. goto clean;
  582. if (type_str != NULL) {
  583. if (!vector_str_push(&subst_v, " complex", 8))
  584. goto clean;
  585. if (!cpp_demangle_push_subst_v(ddata, &subst_v))
  586. goto clean;
  587. }
  588. break;
  589. case TYPE_IMG:
  590. if (!cpp_demangle_push_str(ddata, " imaginary", 10))
  591. goto clean;
  592. if (type_str != NULL) {
  593. if (!vector_str_push(&subst_v, " imaginary", 10))
  594. goto clean;
  595. if (!cpp_demangle_push_subst_v(ddata, &subst_v))
  596. goto clean;
  597. }
  598. break;
  599. case TYPE_EXT:
  600. if (e_idx > v->ext_name.size - 1)
  601. goto clean;
  602. if ((e_len = strlen(v->ext_name.container[e_idx])) == 0)
  603. goto clean;
  604. if ((buf = malloc(sizeof(char) * (e_len + 1))) == NULL)
  605. goto clean;
  606. memcpy(buf, " ", 1);
  607. memcpy(buf + 1, v->ext_name.container[e_idx], e_len);
  608. if (!cpp_demangle_push_str(ddata, buf, e_len + 1)) {
  609. free(buf);
  610. goto clean;
  611. }
  612. if (type_str != NULL) {
  613. if (!vector_str_push(&subst_v, buf,
  614. e_len + 1)) {
  615. free(buf);
  616. goto clean;
  617. }
  618. if (!cpp_demangle_push_subst_v(ddata, &subst_v)) {
  619. free(buf);
  620. goto clean;
  621. }
  622. }
  623. free(buf);
  624. ++e_idx;
  625. break;
  626. case TYPE_RST:
  627. if (!cpp_demangle_push_str(ddata, " restrict", 9))
  628. goto clean;
  629. if (type_str != NULL) {
  630. if (!vector_str_push(&subst_v, " restrict", 9))
  631. goto clean;
  632. if (!cpp_demangle_push_subst_v(ddata, &subst_v))
  633. goto clean;
  634. }
  635. break;
  636. case TYPE_VAT:
  637. if (!cpp_demangle_push_str(ddata, " volatile", 9))
  638. goto clean;
  639. if (type_str != NULL) {
  640. if (!vector_str_push(&subst_v, " volatile", 9))
  641. goto clean;
  642. if (!cpp_demangle_push_subst_v(ddata, &subst_v))
  643. goto clean;
  644. }
  645. break;
  646. case TYPE_CST:
  647. if (!cpp_demangle_push_str(ddata, " const", 6))
  648. goto clean;
  649. if (type_str != NULL) {
  650. if (!vector_str_push(&subst_v, " const", 6))
  651. goto clean;
  652. if (!cpp_demangle_push_subst_v(ddata, &subst_v))
  653. goto clean;
  654. }
  655. break;
  656. };
  657. --idx;
  658. }
  659. rtn = 1;
  660. clean:
  661. if (type_str != NULL)
  662. vector_str_dest(&subst_v);
  663. return (rtn);
  664. }
  665. static int
  666. cpp_demangle_get_subst(struct cpp_demangle_data *ddata, size_t idx)
  667. {
  668. size_t len;
  669. if (ddata == NULL || ddata->subst.size <= idx)
  670. return (0);
  671. if ((len = strlen(ddata->subst.container[idx])) == 0)
  672. return (0);
  673. if (!cpp_demangle_push_str(ddata, ddata->subst.container[idx], len))
  674. return (0);
  675. /* skip '_' */
  676. ++ddata->cur;
  677. return (1);
  678. }
  679. static int
  680. cpp_demangle_get_tmpl_param(struct cpp_demangle_data *ddata, size_t idx)
  681. {
  682. size_t len;
  683. if (ddata == NULL || ddata->tmpl.size <= idx)
  684. return (0);
  685. if ((len = strlen(ddata->tmpl.container[idx])) == 0)
  686. return (0);
  687. if (!cpp_demangle_push_str(ddata, ddata->tmpl.container[idx], len))
  688. return (0);
  689. ++ddata->cur;
  690. return (1);
  691. }
  692. static int
  693. cpp_demangle_read_array(struct cpp_demangle_data *ddata)
  694. {
  695. size_t i, num_len, exp_len, p_idx, idx;
  696. const char *num;
  697. char *exp;
  698. if (ddata == NULL || *(++ddata->cur) == '\0')
  699. return (0);
  700. if (*ddata->cur == '_') {
  701. if (*(++ddata->cur) == '\0')
  702. return (0);
  703. if (!cpp_demangle_read_type(ddata, 0))
  704. return (0);
  705. if (!cpp_demangle_push_str(ddata, "[]", 2))
  706. return (0);
  707. } else {
  708. if (ELFTC_ISDIGIT(*ddata->cur) != 0) {
  709. num = ddata->cur;
  710. while (ELFTC_ISDIGIT(*ddata->cur) != 0)
  711. ++ddata->cur;
  712. if (*ddata->cur != '_')
  713. return (0);
  714. num_len = ddata->cur - num;
  715. assert(num_len > 0);
  716. if (*(++ddata->cur) == '\0')
  717. return (0);
  718. if (!cpp_demangle_read_type(ddata, 0))
  719. return (0);
  720. if (!cpp_demangle_push_str(ddata, "[", 1))
  721. return (0);
  722. if (!cpp_demangle_push_str(ddata, num, num_len))
  723. return (0);
  724. if (!cpp_demangle_push_str(ddata, "]", 1))
  725. return (0);
  726. } else {
  727. p_idx = ddata->output.size;
  728. if (!cpp_demangle_read_expression(ddata))
  729. return (0);
  730. if ((exp = vector_str_substr(&ddata->output, p_idx,
  731. ddata->output.size - 1, &exp_len)) == NULL)
  732. return (0);
  733. idx = ddata->output.size;
  734. for (i = p_idx; i < idx; ++i)
  735. if (!vector_str_pop(&ddata->output)) {
  736. free(exp);
  737. return (0);
  738. }
  739. if (*ddata->cur != '_') {
  740. free(exp);
  741. return (0);
  742. }
  743. ++ddata->cur;
  744. if (*ddata->cur == '\0') {
  745. free(exp);
  746. return (0);
  747. }
  748. if (!cpp_demangle_read_type(ddata, 0)) {
  749. free(exp);
  750. return (0);
  751. }
  752. if (!cpp_demangle_push_str(ddata, "[", 1)) {
  753. free(exp);
  754. return (0);
  755. }
  756. if (!cpp_demangle_push_str(ddata, exp, exp_len)) {
  757. free(exp);
  758. return (0);
  759. }
  760. if (!cpp_demangle_push_str(ddata, "]", 1)) {
  761. free(exp);
  762. return (0);
  763. }
  764. free(exp);
  765. }
  766. }
  767. return (1);
  768. }
  769. static int
  770. cpp_demangle_read_expr_primary(struct cpp_demangle_data *ddata)
  771. {
  772. const char *num;
  773. if (ddata == NULL || *(++ddata->cur) == '\0')
  774. return (0);
  775. if (*ddata->cur == '_' && *(ddata->cur + 1) == 'Z') {
  776. ddata->cur += 2;
  777. if (*ddata->cur == '\0')
  778. return (0);
  779. if (!cpp_demangle_read_encoding(ddata))
  780. return (0);
  781. ++ddata->cur;
  782. return (1);
  783. }
  784. switch (*ddata->cur) {
  785. case 'b':
  786. switch (*(++ddata->cur)) {
  787. case '0':
  788. return (cpp_demangle_push_str(ddata, "false", 5));
  789. case '1':
  790. return (cpp_demangle_push_str(ddata, "true", 4));
  791. default:
  792. return (0);
  793. };
  794. case 'd':
  795. ++ddata->cur;
  796. return (cpp_demangle_push_fp(ddata, decode_fp_to_double));
  797. case 'e':
  798. ++ddata->cur;
  799. if (sizeof(long double) == 10)
  800. return (cpp_demangle_push_fp(ddata,
  801. decode_fp_to_double));
  802. return (cpp_demangle_push_fp(ddata, decode_fp_to_float80));
  803. case 'f':
  804. ++ddata->cur;
  805. return (cpp_demangle_push_fp(ddata, decode_fp_to_float));
  806. case 'g':
  807. ++ddata->cur;
  808. if (sizeof(long double) == 16)
  809. return (cpp_demangle_push_fp(ddata,
  810. decode_fp_to_double));
  811. return (cpp_demangle_push_fp(ddata, decode_fp_to_float128));
  812. case 'i':
  813. case 'j':
  814. case 'l':
  815. case 'm':
  816. case 'n':
  817. case 's':
  818. case 't':
  819. case 'x':
  820. case 'y':
  821. if (*(++ddata->cur) == 'n') {
  822. if (!cpp_demangle_push_str(ddata, "-", 1))
  823. return (0);
  824. ++ddata->cur;
  825. }
  826. num = ddata->cur;
  827. while (*ddata->cur != 'E') {
  828. if (!ELFTC_ISDIGIT(*ddata->cur))
  829. return (0);
  830. ++ddata->cur;
  831. }
  832. ++ddata->cur;
  833. return (cpp_demangle_push_str(ddata, num, ddata->cur - num));
  834. default:
  835. return (0);
  836. };
  837. }
  838. static int
  839. cpp_demangle_read_expression(struct cpp_demangle_data *ddata)
  840. {
  841. if (ddata == NULL || *ddata->cur == '\0')
  842. return (0);
  843. switch (SIMPLE_HASH(*ddata->cur, *(ddata->cur + 1))) {
  844. case SIMPLE_HASH('s', 't'):
  845. ddata->cur += 2;
  846. return (cpp_demangle_read_type(ddata, 0));
  847. case SIMPLE_HASH('s', 'r'):
  848. ddata->cur += 2;
  849. if (!cpp_demangle_read_type(ddata, 0))
  850. return (0);
  851. if (!cpp_demangle_read_uqname(ddata))
  852. return (0);
  853. if (*ddata->cur == 'I')
  854. return (cpp_demangle_read_tmpl_args(ddata));
  855. return (1);
  856. case SIMPLE_HASH('a', 'a'):
  857. /* operator && */
  858. ddata->cur += 2;
  859. return (cpp_demangle_read_expression_binary(ddata, "&&", 2));
  860. case SIMPLE_HASH('a', 'd'):
  861. /* operator & (unary) */
  862. ddata->cur += 2;
  863. return (cpp_demangle_read_expression_unary(ddata, "&", 1));
  864. case SIMPLE_HASH('a', 'n'):
  865. /* operator & */
  866. ddata->cur += 2;
  867. return (cpp_demangle_read_expression_binary(ddata, "&", 1));
  868. case SIMPLE_HASH('a', 'N'):
  869. /* operator &= */
  870. ddata->cur += 2;
  871. return (cpp_demangle_read_expression_binary(ddata, "&=", 2));
  872. case SIMPLE_HASH('a', 'S'):
  873. /* operator = */
  874. ddata->cur += 2;
  875. return (cpp_demangle_read_expression_binary(ddata, "=", 1));
  876. case SIMPLE_HASH('c', 'l'):
  877. /* operator () */
  878. ddata->cur += 2;
  879. return (cpp_demangle_read_expression_binary(ddata, "()", 2));
  880. case SIMPLE_HASH('c', 'm'):
  881. /* operator , */
  882. ddata->cur += 2;
  883. return (cpp_demangle_read_expression_binary(ddata, ",", 1));
  884. case SIMPLE_HASH('c', 'o'):
  885. /* operator ~ */
  886. ddata->cur += 2;
  887. return (cpp_demangle_read_expression_binary(ddata, "~", 1));
  888. case SIMPLE_HASH('c', 'v'):
  889. /* operator (cast) */
  890. ddata->cur += 2;
  891. return (cpp_demangle_read_expression_binary(ddata, "(cast)", 6));
  892. case SIMPLE_HASH('d', 'a'):
  893. /* operator delete [] */
  894. ddata->cur += 2;
  895. return (cpp_demangle_read_expression_unary(ddata, "delete []", 9));
  896. case SIMPLE_HASH('d', 'e'):
  897. /* operator * (unary) */
  898. ddata->cur += 2;
  899. return (cpp_demangle_read_expression_unary(ddata, "*", 1));
  900. case SIMPLE_HASH('d', 'l'):
  901. /* operator delete */
  902. ddata->cur += 2;
  903. return (cpp_demangle_read_expression_unary(ddata, "delete", 6));
  904. case SIMPLE_HASH('d', 'v'):
  905. /* operator / */
  906. ddata->cur += 2;
  907. return (cpp_demangle_read_expression_binary(ddata, "/", 1));
  908. case SIMPLE_HASH('d', 'V'):
  909. /* operator /= */
  910. ddata->cur += 2;
  911. return (cpp_demangle_read_expression_binary(ddata, "/=", 2));
  912. case SIMPLE_HASH('e', 'o'):
  913. /* operator ^ */
  914. ddata->cur += 2;
  915. return (cpp_demangle_read_expression_binary(ddata, "^", 1));
  916. case SIMPLE_HASH('e', 'O'):
  917. /* operator ^= */
  918. ddata->cur += 2;
  919. return (cpp_demangle_read_expression_binary(ddata, "^=", 2));
  920. case SIMPLE_HASH('e', 'q'):
  921. /* operator == */
  922. ddata->cur += 2;
  923. return (cpp_demangle_read_expression_binary(ddata, "==", 2));
  924. case SIMPLE_HASH('g', 'e'):
  925. /* operator >= */
  926. ddata->cur += 2;
  927. return (cpp_demangle_read_expression_binary(ddata, ">=", 2));
  928. case SIMPLE_HASH('g', 't'):
  929. /* operator > */
  930. ddata->cur += 2;
  931. return (cpp_demangle_read_expression_binary(ddata, ">", 1));
  932. case SIMPLE_HASH('i', 'x'):
  933. /* operator [] */
  934. ddata->cur += 2;
  935. return (cpp_demangle_read_expression_binary(ddata, "[]", 2));
  936. case SIMPLE_HASH('l', 'e'):
  937. /* operator <= */
  938. ddata->cur += 2;
  939. return (cpp_demangle_read_expression_binary(ddata, "<=", 2));
  940. case SIMPLE_HASH('l', 's'):
  941. /* operator << */
  942. ddata->cur += 2;
  943. return (cpp_demangle_read_expression_binary(ddata, "<<", 2));
  944. case SIMPLE_HASH('l', 'S'):
  945. /* operator <<= */
  946. ddata->cur += 2;
  947. return (cpp_demangle_read_expression_binary(ddata, "<<=", 3));
  948. case SIMPLE_HASH('l', 't'):
  949. /* operator < */
  950. ddata->cur += 2;
  951. return (cpp_demangle_read_expression_binary(ddata, "<", 1));
  952. case SIMPLE_HASH('m', 'i'):
  953. /* operator - */
  954. ddata->cur += 2;
  955. return (cpp_demangle_read_expression_binary(ddata, "-", 1));
  956. case SIMPLE_HASH('m', 'I'):
  957. /* operator -= */
  958. ddata->cur += 2;
  959. return (cpp_demangle_read_expression_binary(ddata, "-=", 2));
  960. case SIMPLE_HASH('m', 'l'):
  961. /* operator * */
  962. ddata->cur += 2;
  963. return (cpp_demangle_read_expression_binary(ddata, "*", 1));
  964. case SIMPLE_HASH('m', 'L'):
  965. /* operator *= */
  966. ddata->cur += 2;
  967. return (cpp_demangle_read_expression_binary(ddata, "*=", 2));
  968. case SIMPLE_HASH('m', 'm'):
  969. /* operator -- */
  970. ddata->cur += 2;
  971. return (cpp_demangle_read_expression_binary(ddata, "--", 2));
  972. case SIMPLE_HASH('n', 'a'):
  973. /* operator new[] */
  974. ddata->cur += 2;
  975. return (cpp_demangle_read_expression_unary(ddata, "new []", 6));
  976. case SIMPLE_HASH('n', 'e'):
  977. /* operator != */
  978. ddata->cur += 2;
  979. return (cpp_demangle_read_expression_binary(ddata, "!=", 2));
  980. case SIMPLE_HASH('n', 'g'):
  981. /* operator - (unary) */
  982. ddata->cur += 2;
  983. return (cpp_demangle_read_expression_unary(ddata, "-", 1));
  984. case SIMPLE_HASH('n', 't'):
  985. /* operator ! */
  986. ddata->cur += 2;
  987. return (cpp_demangle_read_expression_binary(ddata, "!", 1));
  988. case SIMPLE_HASH('n', 'w'):
  989. /* operator new */
  990. ddata->cur += 2;
  991. return (cpp_demangle_read_expression_unary(ddata, "new", 3));
  992. case SIMPLE_HASH('o', 'o'):
  993. /* operator || */
  994. ddata->cur += 2;
  995. return (cpp_demangle_read_expression_binary(ddata, "||", 2));
  996. case SIMPLE_HASH('o', 'r'):
  997. /* operator | */
  998. ddata->cur += 2;
  999. return (cpp_demangle_read_expression_binary(ddata, "|", 1));
  1000. case SIMPLE_HASH('o', 'R'):
  1001. /* operator |= */
  1002. ddata->cur += 2;
  1003. return (cpp_demangle_read_expression_binary(ddata, "|=", 2));
  1004. case SIMPLE_HASH('p', 'l'):
  1005. /* operator + */
  1006. ddata->cur += 2;
  1007. return (cpp_demangle_read_expression_binary(ddata, "+", 1));
  1008. case SIMPLE_HASH('p', 'L'):
  1009. /* operator += */
  1010. ddata->cur += 2;
  1011. return (cpp_demangle_read_expression_binary(ddata, "+=", 2));
  1012. case SIMPLE_HASH('p', 'm'):
  1013. /* operator ->* */
  1014. ddata->cur += 2;
  1015. return (cpp_demangle_read_expression_binary(ddata, "->*", 3));
  1016. case SIMPLE_HASH('p', 'p'):
  1017. /* operator ++ */
  1018. ddata->cur += 2;
  1019. return (cpp_demangle_read_expression_binary(ddata, "++", 2));
  1020. case SIMPLE_HASH('p', 's'):
  1021. /* operator + (unary) */
  1022. ddata->cur += 2;
  1023. return (cpp_demangle_read_expression_unary(ddata, "+", 1));
  1024. case SIMPLE_HASH('p', 't'):
  1025. /* operator -> */
  1026. ddata->cur += 2;
  1027. return (cpp_demangle_read_expression_binary(ddata, "->", 2));
  1028. case SIMPLE_HASH('q', 'u'):
  1029. /* operator ? */
  1030. ddata->cur += 2;
  1031. return (cpp_demangle_read_expression_trinary(ddata, "?", 1,
  1032. ":", 1));
  1033. case SIMPLE_HASH('r', 'm'):
  1034. /* operator % */
  1035. ddata->cur += 2;
  1036. return (cpp_demangle_read_expression_binary(ddata, "%", 1));
  1037. case SIMPLE_HASH('r', 'M'):
  1038. /* operator %= */
  1039. ddata->cur += 2;
  1040. return (cpp_demangle_read_expression_binary(ddata, "%=", 2));
  1041. case SIMPLE_HASH('r', 's'):
  1042. /* operator >> */
  1043. ddata->cur += 2;
  1044. return (cpp_demangle_read_expression_binary(ddata, ">>", 2));
  1045. case SIMPLE_HASH('r', 'S'):
  1046. /* operator >>= */
  1047. ddata->cur += 2;
  1048. return (cpp_demangle_read_expression_binary(ddata, ">>=", 3));
  1049. case SIMPLE_HASH('r', 'z'):
  1050. /* operator sizeof */
  1051. ddata->cur += 2;
  1052. return (cpp_demangle_read_expression_unary(ddata, "sizeof", 6));
  1053. case SIMPLE_HASH('s', 'v'):
  1054. /* operator sizeof */
  1055. ddata->cur += 2;
  1056. return (cpp_demangle_read_expression_unary(ddata, "sizeof", 6));
  1057. };
  1058. switch (*ddata->cur) {
  1059. case 'L':
  1060. return (cpp_demangle_read_expr_primary(ddata));
  1061. case 'T':
  1062. return (cpp_demangle_read_tmpl_param(ddata));
  1063. };
  1064. return (0);
  1065. }
  1066. static int
  1067. cpp_demangle_read_expression_binary(struct cpp_demangle_data *ddata,
  1068. const char *name, size_t len)
  1069. {
  1070. if (ddata == NULL || name == NULL || len == 0)
  1071. return (0);
  1072. if (!cpp_demangle_read_expression(ddata))
  1073. return (0);
  1074. if (!cpp_demangle_push_str(ddata, name, len))
  1075. return (0);
  1076. return (cpp_demangle_read_expression(ddata));
  1077. }
  1078. static int
  1079. cpp_demangle_read_expression_unary(struct cpp_demangle_data *ddata,
  1080. const char *name, size_t len)
  1081. {
  1082. if (ddata == NULL || name == NULL || len == 0)
  1083. return (0);
  1084. if (!cpp_demangle_read_expression(ddata))
  1085. return (0);
  1086. return (cpp_demangle_push_str(ddata, name, len));
  1087. }
  1088. static int
  1089. cpp_demangle_read_expression_trinary(struct cpp_demangle_data *ddata,
  1090. const char *name1, size_t len1, const char *name2, size_t len2)
  1091. {
  1092. if (ddata == NULL || name1 == NULL || len1 == 0 || name2 == NULL ||
  1093. len2 == 0)
  1094. return (0);
  1095. if (!cpp_demangle_read_expression(ddata))
  1096. return (0);
  1097. if (!cpp_demangle_push_str(ddata, name1, len1))
  1098. return (0);
  1099. if (!cpp_demangle_read_expression(ddata))
  1100. return (0);
  1101. if (!cpp_demangle_push_str(ddata, name2, len2))
  1102. return (0);
  1103. return (cpp_demangle_read_expression(ddata));
  1104. }
  1105. static int
  1106. cpp_demangle_read_function(struct cpp_demangle_data *ddata, int *ext_c,
  1107. struct vector_type_qualifier *v)
  1108. {
  1109. size_t class_type_size, class_type_len, limit;
  1110. const char *class_type;
  1111. if (ddata == NULL || *ddata->cur != 'F' || v == NULL)
  1112. return (0);
  1113. ++ddata->cur;
  1114. if (*ddata->cur == 'Y') {
  1115. if (ext_c != NULL)
  1116. *ext_c = 1;
  1117. ++ddata->cur;
  1118. }
  1119. if (!cpp_demangle_read_type(ddata, 0))
  1120. return (0);
  1121. if (*ddata->cur != 'E') {
  1122. if (!cpp_demangle_push_str(ddata, "(", 1))
  1123. return (0);
  1124. if (vector_read_cmd_find(&ddata->cmd, READ_PTRMEM)) {
  1125. if ((class_type_size = ddata->class_type.size) == 0)
  1126. return (0);
  1127. class_type =
  1128. ddata->class_type.container[class_type_size - 1];
  1129. if (class_type == NULL)
  1130. return (0);
  1131. if ((class_type_len = strlen(class_type)) == 0)
  1132. return (0);
  1133. if (!cpp_demangle_push_str(ddata, class_type,
  1134. class_type_len))
  1135. return (0);
  1136. if (!cpp_demangle_push_str(ddata, "::*", 3))
  1137. return (0);
  1138. ++ddata->func_type;
  1139. } else {
  1140. if (!cpp_demangle_push_type_qualifier(ddata, v,
  1141. (const char *) NULL))
  1142. return (0);
  1143. vector_type_qualifier_dest(v);
  1144. if (!vector_type_qualifier_init(v))
  1145. return (0);
  1146. }
  1147. if (!cpp_demangle_push_str(ddata, ")(", 2))
  1148. return (0);
  1149. limit = 0;
  1150. for (;;) {
  1151. if (!cpp_demangle_read_type(ddata, 0))
  1152. return (0);
  1153. if (*ddata->cur == 'E')
  1154. break;
  1155. if (limit++ > CPP_DEMANGLE_TRY_LIMIT)
  1156. return (0);
  1157. }
  1158. if (vector_read_cmd_find(&ddata->cmd, READ_PTRMEM) == 1) {
  1159. if (!cpp_demangle_push_type_qualifier(ddata, v,
  1160. (const char *) NULL))
  1161. return (0);
  1162. vector_type_qualifier_dest(v);
  1163. if (!vector_type_qualifier_init(v))
  1164. return (0);
  1165. }
  1166. if (!cpp_demangle_push_str(ddata, ")", 1))
  1167. return (0);
  1168. }
  1169. ++ddata->cur;
  1170. return (1);
  1171. }
  1172. /* read encoding, encoding are function name, data name, special-name */
  1173. static int
  1174. cpp_demangle_read_encoding(struct cpp_demangle_data *ddata)
  1175. {
  1176. if (ddata == NULL || *ddata->cur == '\0')
  1177. return (0);
  1178. /* special name */
  1179. switch (SIMPLE_HASH(*ddata->cur, *(ddata->cur + 1))) {
  1180. case SIMPLE_HASH('G', 'V'):
  1181. /* sentry object for 1 time init */
  1182. if (!cpp_demangle_push_str(ddata, "guard variable for ", 20))
  1183. return (0);
  1184. ddata->cur += 2;
  1185. break;
  1186. case SIMPLE_HASH('T', 'c'):
  1187. /* virtual function covariant override thunk */
  1188. if (!cpp_demangle_push_str(ddata,
  1189. "virtual function covariant override ", 36))
  1190. return (0);
  1191. ddata->cur += 2;
  1192. if (*ddata->cur == '\0')
  1193. return (0);
  1194. if (!cpp_demangle_read_offset(ddata))
  1195. return (0);
  1196. if (!cpp_demangle_read_offset(ddata))
  1197. return (0);
  1198. return (cpp_demangle_read_encoding(ddata));
  1199. case SIMPLE_HASH('T', 'D'):
  1200. /* typeinfo common proxy */
  1201. break;
  1202. case SIMPLE_HASH('T', 'h'):
  1203. /* virtual function non-virtual override thunk */
  1204. if (cpp_demangle_push_str(ddata,
  1205. "virtual function non-virtual override ", 38) == 0)
  1206. return (0);
  1207. ddata->cur += 2;
  1208. if (*ddata->cur == '\0')
  1209. return (0);
  1210. if (!cpp_demangle_read_nv_offset(ddata))
  1211. return (0);
  1212. return (cpp_demangle_read_encoding(ddata));
  1213. case SIMPLE_HASH('T', 'I'):
  1214. /* typeinfo structure */
  1215. /* FALLTHROUGH */
  1216. case SIMPLE_HASH('T', 'S'):
  1217. /* RTTI name (NTBS) */
  1218. if (!cpp_demangle_push_str(ddata, "typeinfo for ", 14))
  1219. return (0);
  1220. ddata->cur += 2;
  1221. if (*ddata->cur == '\0')
  1222. return (0);
  1223. return (cpp_demangle_read_type(ddata, 1));
  1224. case SIMPLE_HASH('T', 'T'):
  1225. /* VTT table */
  1226. if (!cpp_demangle_push_str(ddata, "VTT for ", 8))
  1227. return (0);
  1228. ddata->cur += 2;
  1229. return (cpp_demangle_read_type(ddata, 1));
  1230. case SIMPLE_HASH('T', 'v'):
  1231. /* virtual function virtual override thunk */
  1232. if (!cpp_demangle_push_str(ddata,
  1233. "virtual function virtual override ", 34))
  1234. return (0);
  1235. ddata->cur += 2;
  1236. if (*ddata->cur == '\0')
  1237. return (0);
  1238. if (!cpp_demangle_read_v_offset(ddata))
  1239. return (0);
  1240. return (cpp_demangle_read_encoding(ddata));
  1241. case SIMPLE_HASH('T', 'V'):
  1242. /* virtual table */
  1243. if (!cpp_demangle_push_str(ddata, "vtable for ", 12))
  1244. return (0);
  1245. ddata->cur += 2;
  1246. if (*ddata->cur == '\0')
  1247. return (0);
  1248. return (cpp_demangle_read_type(ddata, 1));
  1249. };
  1250. return (cpp_demangle_read_name(ddata));
  1251. }
  1252. static int
  1253. cpp_demangle_read_local_name(struct cpp_demangle_data *ddata)
  1254. {
  1255. size_t limit;
  1256. if (ddata == NULL)
  1257. return (0);
  1258. if (*(++ddata->cur) == '\0')
  1259. return (0);
  1260. if (!cpp_demangle_read_encoding(ddata))
  1261. return (0);
  1262. limit = 0;
  1263. for (;;) {
  1264. if (!cpp_demangle_read_type(ddata, 1))
  1265. return (0);
  1266. if (*ddata->cur == 'E')
  1267. break;
  1268. if (limit++ > CPP_DEMANGLE_TRY_LIMIT)
  1269. return (0);
  1270. }
  1271. if (*(++ddata->cur) == '\0')
  1272. return (0);
  1273. if (ddata->paren == true) {
  1274. if (!cpp_demangle_push_str(ddata, ")", 1))
  1275. return (0);
  1276. ddata->paren = false;
  1277. }
  1278. if (*ddata->cur == 's')
  1279. ++ddata->cur;
  1280. else {
  1281. if (!cpp_demangle_push_str(ddata, "::", 2))
  1282. return (0);
  1283. if (!cpp_demangle_read_name(ddata))
  1284. return (0);
  1285. }
  1286. if (*ddata->cur == '_') {
  1287. ++ddata->cur;
  1288. while (ELFTC_ISDIGIT(*ddata->cur) != 0)
  1289. ++ddata->cur;
  1290. }
  1291. return (1);
  1292. }
  1293. static int
  1294. cpp_demangle_read_name(struct cpp_demangle_data *ddata)
  1295. {
  1296. struct vector_str *output, v;
  1297. size_t p_idx, subst_str_len;
  1298. int rtn;
  1299. char *subst_str;
  1300. if (ddata == NULL || *ddata->cur == '\0')
  1301. return (0);
  1302. output = ddata->push_head > 0 ? &ddata->output_tmp : &ddata->output;
  1303. subst_str = NULL;
  1304. switch (*ddata->cur) {
  1305. case 'S':
  1306. return (cpp_demangle_read_subst(ddata));
  1307. case 'N':
  1308. return (cpp_demangle_read_nested_name(ddata));
  1309. case 'Z':
  1310. return (cpp_demangle_read_local_name(ddata));
  1311. };
  1312. if (!vector_str_init(&v))
  1313. return (0);
  1314. p_idx = output->size;
  1315. rtn = 0;
  1316. if (!cpp_demangle_read_uqname(ddata))
  1317. goto clean;
  1318. if ((subst_str = vector_str_substr(output, p_idx, output->size - 1,
  1319. &subst_str_len)) == NULL)
  1320. goto clean;
  1321. if (subst_str_len > 8 && strstr(subst_str, "operator") != NULL) {
  1322. rtn = 1;
  1323. goto clean;
  1324. }
  1325. if (!vector_str_push(&v, subst_str, subst_str_len))
  1326. goto clean;
  1327. if (!cpp_demangle_push_subst_v(ddata, &v))
  1328. goto clean;
  1329. if (*ddata->cur == 'I') {
  1330. p_idx = output->size;
  1331. if (!cpp_demangle_read_tmpl_args(ddata))
  1332. goto clean;
  1333. free(subst_str);
  1334. if ((subst_str = vector_str_substr(output, p_idx,
  1335. output->size - 1, &subst_str_len)) == NULL)
  1336. goto clean;
  1337. if (!vector_str_push(&v, subst_str, subst_str_len))
  1338. goto clean;
  1339. if (!cpp_demangle_push_subst_v(ddata, &v))
  1340. goto clean;
  1341. }
  1342. rtn = 1;
  1343. clean:
  1344. if (subst_str) free(subst_str);
  1345. vector_str_dest(&v);
  1346. return (rtn);
  1347. }
  1348. static int
  1349. cpp_demangle_read_nested_name(struct cpp_demangle_data *ddata)
  1350. {
  1351. struct vector_str *output, v;
  1352. size_t limit, p_idx, subst_str_len;
  1353. int rtn;
  1354. char *subst_str;
  1355. if (ddata == NULL || *ddata->cur != 'N')
  1356. return (0);
  1357. if (*(++ddata->cur) == '\0')
  1358. return (0);
  1359. while (*ddata->cur == 'r' || *ddata->cur == 'V' ||
  1360. *ddata->cur == 'K') {
  1361. switch (*ddata->cur) {
  1362. case 'r':
  1363. ddata->mem_rst = true;
  1364. break;
  1365. case 'V':
  1366. ddata->mem_vat = true;
  1367. break;
  1368. case 'K':
  1369. ddata->mem_cst = true;
  1370. break;
  1371. };
  1372. ++ddata->cur;
  1373. }
  1374. output = ddata->push_head > 0 ? &ddata->output_tmp : &ddata->output;
  1375. if (!vector_str_init(&v))
  1376. return (0);
  1377. rtn = 0;
  1378. limit = 0;
  1379. for (;;) {
  1380. p_idx = output->size;
  1381. switch (*ddata->cur) {
  1382. case 'I':
  1383. if (!cpp_demangle_read_tmpl_args(ddata))
  1384. goto clean;
  1385. break;
  1386. case 'S':
  1387. if (!cpp_demangle_read_subst(ddata))
  1388. goto clean;
  1389. break;
  1390. case 'T':
  1391. if (!cpp_demangle_read_tmpl_param(ddata))
  1392. goto clean;
  1393. break;
  1394. default:
  1395. if (!cpp_demangle_read_uqname(ddata))
  1396. goto clean;
  1397. };
  1398. if ((subst_str = vector_str_substr(output, p_idx,
  1399. output->size - 1, &subst_str_len)) == NULL)
  1400. goto clean;
  1401. if (!vector_str_push(&v, subst_str, subst_str_len)) {
  1402. free(subst_str);
  1403. goto clean;
  1404. }
  1405. free(subst_str);
  1406. if (!cpp_demangle_push_subst_v(ddata, &v))
  1407. goto clean;
  1408. if (*ddata->cur == 'E')
  1409. break;
  1410. else if (*ddata->cur != 'I' &&
  1411. *ddata->cur != 'C' && *ddata->cur != 'D') {
  1412. if (!cpp_demangle_push_str(ddata, "::", 2))
  1413. goto clean;
  1414. if (!vector_str_push(&v, "::", 2))
  1415. goto clean;
  1416. }
  1417. if (limit++ > CPP_DEMANGLE_TRY_LIMIT)
  1418. goto clean;
  1419. }
  1420. ++ddata->cur;
  1421. rtn = 1;
  1422. clean:
  1423. vector_str_dest(&v);
  1424. return (rtn);
  1425. }
  1426. /*
  1427. * read number
  1428. * number ::= [n] <decimal>
  1429. */
  1430. static int
  1431. cpp_demangle_read_number(struct cpp_demangle_data *ddata, long *rtn)
  1432. {
  1433. long len, negative_factor;
  1434. if (ddata == NULL || rtn == NULL)
  1435. return (0);
  1436. negative_factor = 1;
  1437. if (*ddata->cur == 'n') {
  1438. negative_factor = -1;
  1439. ++ddata->cur;
  1440. }
  1441. if (ELFTC_ISDIGIT(*ddata->cur) == 0)
  1442. return (0);
  1443. errno = 0;
  1444. if ((len = strtol(ddata->cur, (char **) NULL, 10)) == 0 &&
  1445. errno != 0)
  1446. return (0);
  1447. while (ELFTC_ISDIGIT(*ddata->cur) != 0)
  1448. ++ddata->cur;
  1449. assert(len >= 0);
  1450. assert(negative_factor == 1 || negative_factor == -1);
  1451. *rtn = len * negative_factor;
  1452. return (1);
  1453. }
  1454. static int
  1455. cpp_demangle_read_nv_offset(struct cpp_demangle_data *ddata)
  1456. {
  1457. if (ddata == NULL)
  1458. return (0);
  1459. if (!cpp_demangle_push_str(ddata, "offset : ", 9))
  1460. return (0);
  1461. return (cpp_demangle_read_offset_number(ddata));
  1462. }
  1463. /* read offset, offset are nv-offset, v-offset */
  1464. static int
  1465. cpp_demangle_read_offset(struct cpp_demangle_data *ddata)
  1466. {
  1467. if (ddata == NULL)
  1468. return (0);
  1469. if (*ddata->cur == 'h') {
  1470. ++ddata->cur;
  1471. return (cpp_demangle_read_nv_offset(ddata));
  1472. } else if (*ddata->cur == 'v') {
  1473. ++ddata->cur;
  1474. return (cpp_demangle_read_v_offset(ddata));
  1475. }
  1476. return (0);
  1477. }
  1478. static int
  1479. cpp_demangle_read_offset_number(struct cpp_demangle_data *ddata)
  1480. {
  1481. bool negative;
  1482. const char *start;
  1483. if (ddata == NULL || *ddata->cur == '\0')
  1484. return (0);
  1485. /* offset could be negative */
  1486. if (*ddata->cur == 'n') {
  1487. negative = true;
  1488. start = ddata->cur + 1;
  1489. } else {
  1490. negative = false;
  1491. start = ddata->cur;
  1492. }
  1493. while (*ddata->cur != '_')
  1494. ++ddata->cur;
  1495. if (negative && !cpp_demangle_push_str(ddata, "-", 1))
  1496. return (0);
  1497. assert(start != NULL);
  1498. if (!cpp_demangle_push_str(ddata, start, ddata->cur - start))
  1499. return (0);
  1500. if (!cpp_demangle_push_str(ddata, " ", 1))
  1501. return (0);
  1502. ++ddata->cur;
  1503. return (1);
  1504. }
  1505. static int
  1506. cpp_demangle_read_pointer_to_member(struct cpp_demangle_data *ddata)
  1507. {
  1508. size_t class_type_len, i, idx, p_idx;
  1509. int p_func_type, rtn;
  1510. char *class_type;
  1511. if (ddata == NULL || *ddata->cur != 'M' || *(++ddata->cur) == '\0')
  1512. return (0);
  1513. p_idx = ddata->output.size;
  1514. if (!cpp_demangle_read_type(ddata, 0))
  1515. return (0);
  1516. if ((class_type = vector_str_substr(&ddata->output, p_idx,
  1517. ddata->output.size - 1, &class_type_len)) == NULL)
  1518. return (0);
  1519. rtn = 0;
  1520. idx = ddata->output.size;
  1521. for (i = p_idx; i < idx; ++i)
  1522. if (!vector_str_pop(&ddata->output))
  1523. goto clean1;
  1524. if (!vector_read_cmd_push(&ddata->cmd, READ_PTRMEM))
  1525. goto clean1;
  1526. if (!vector_str_push(&ddata->class_type, class_type, class_type_len))
  1527. goto clean2;
  1528. p_func_type = ddata->func_type;
  1529. if (!cpp_demangle_read_type(ddata, 0))
  1530. goto clean3;
  1531. if (p_func_type == ddata->func_type) {
  1532. if (!cpp_demangle_push_str(ddata, " ", 1))
  1533. goto clean3;
  1534. if (!cpp_demangle_push_str(ddata, class_type, class_type_len))
  1535. goto clean3;
  1536. if (!cpp_demangle_push_str(ddata, "::*", 3))
  1537. goto clean3;
  1538. }
  1539. rtn = 1;
  1540. clean3:
  1541. if (!vector_str_pop(&ddata->class_type))
  1542. rtn = 0;
  1543. clean2:
  1544. if (!vector_read_cmd_pop(&ddata->cmd))
  1545. rtn = 0;
  1546. clean1:
  1547. free(class_type);
  1548. return (rtn);
  1549. }
  1550. /* read source-name, source-name is <len> <ID> */
  1551. static int
  1552. cpp_demangle_read_sname(struct cpp_demangle_data *ddata)
  1553. {
  1554. long len;
  1555. if (ddata == NULL || cpp_demangle_read_number(ddata, &len) == 0 ||
  1556. len <= 0 || cpp_demangle_push_str(ddata, ddata->cur, len) == 0)
  1557. return (0);
  1558. assert(ddata->output.size > 0);
  1559. if (vector_read_cmd_find(&ddata->cmd, READ_TMPL) == 0)
  1560. ddata->last_sname =
  1561. ddata->output.container[ddata->output.size - 1];
  1562. ddata->cur += len;
  1563. return (1);
  1564. }
  1565. static int
  1566. cpp_demangle_read_subst(struct cpp_demangle_data *ddata)
  1567. {
  1568. long nth;
  1569. if (ddata == NULL || *ddata->cur == '\0')
  1570. return (0);
  1571. /* abbreviations of the form Sx */
  1572. switch (SIMPLE_HASH(*ddata->cur, *(ddata->cur + 1))) {
  1573. case SIMPLE_HASH('S', 'a'):
  1574. /* std::allocator */
  1575. if (cpp_demangle_push_str(ddata, "std::allocator", 14) == 0)
  1576. return (0);
  1577. ddata->cur += 2;
  1578. if (*ddata->cur == 'I')
  1579. return (cpp_demangle_read_subst_stdtmpl(ddata,
  1580. "std::allocator", 14));
  1581. return (1);
  1582. case SIMPLE_HASH('S', 'b'):
  1583. /* std::basic_string */
  1584. if (!cpp_demangle_push_str(ddata, "std::basic_string", 17))
  1585. return (0);
  1586. ddata->cur += 2;
  1587. if (*ddata->cur == 'I')
  1588. return (cpp_demangle_read_subst_stdtmpl(ddata,
  1589. "std::basic_string", 17));
  1590. return (1);
  1591. case SIMPLE_HASH('S', 'd'):
  1592. /* std::basic_iostream<char, std::char_traits<char> > */
  1593. if (!cpp_demangle_push_str(ddata, "std::iostream", 19))
  1594. return (0);
  1595. ddata->last_sname = "iostream";
  1596. ddata->cur += 2;
  1597. if (*ddata->cur == 'I')
  1598. return (cpp_demangle_read_subst_stdtmpl(ddata,
  1599. "std::iostream", 19));
  1600. return (1);
  1601. case SIMPLE_HASH('S', 'i'):
  1602. /* std::basic_istream<char, std::char_traits<char> > */
  1603. if (!cpp_demangle_push_str(ddata, "std::istream", 18))
  1604. return (0);
  1605. ddata->last_sname = "istream";
  1606. ddata->cur += 2;
  1607. if (*ddata->cur == 'I')
  1608. return (cpp_demangle_read_subst_stdtmpl(ddata,
  1609. "std::istream", 18));
  1610. return (1);
  1611. case SIMPLE_HASH('S', 'o'):
  1612. /* std::basic_ostream<char, std::char_traits<char> > */
  1613. if (!cpp_demangle_push_str(ddata, "std::ostream", 18))
  1614. return (0);
  1615. ddata->last_sname = "istream";
  1616. ddata->cur += 2;
  1617. if (*ddata->cur == 'I')
  1618. return (cpp_demangle_read_subst_stdtmpl(ddata,
  1619. "std::ostream", 18));
  1620. return (1);
  1621. case SIMPLE_HASH('S', 's'):
  1622. /*
  1623. * std::basic_string<char, std::char_traits<char>,
  1624. * std::allocator<char> >
  1625. *
  1626. * a.k.a std::string
  1627. */
  1628. if (!cpp_demangle_push_str(ddata, "std::string", 11))
  1629. return (0);
  1630. ddata->last_sname = "string";
  1631. ddata->cur += 2;
  1632. if (*ddata->cur == 'I')
  1633. return (cpp_demangle_read_subst_stdtmpl(ddata,
  1634. "std::string", 11));
  1635. return (1);
  1636. case SIMPLE_HASH('S', 't'):
  1637. /* std:: */
  1638. return (cpp_demangle_read_subst_std(ddata));
  1639. };
  1640. if (*(++ddata->cur) == '\0')
  1641. return (0);
  1642. /* substitution */
  1643. if (*ddata->cur == '_')
  1644. return (cpp_demangle_get_subst(ddata, 0));
  1645. else {
  1646. errno = 0;
  1647. /* substitution number is base 36 */
  1648. if ((nth = strtol(ddata->cur, (char **) NULL, 36)) == 0 &&
  1649. errno != 0)
  1650. return (0);
  1651. /* first was '_', so increase one */
  1652. ++nth;
  1653. while (*ddata->cur != '_')
  1654. ++ddata->cur;
  1655. assert(nth > 0);
  1656. return (cpp_demangle_get_subst(ddata, nth));
  1657. }
  1658. /* NOTREACHED */
  1659. return (0);
  1660. }
  1661. static int
  1662. cpp_demangle_read_subst_std(struct cpp_demangle_data *ddata)
  1663. {
  1664. struct vector_str *output, v;
  1665. size_t p_idx, subst_str_len;
  1666. int rtn;
  1667. char *subst_str;
  1668. if (ddata == NULL)
  1669. return (0);
  1670. if (!vector_str_init(&v))
  1671. return (0);
  1672. subst_str = NULL;
  1673. rtn = 0;
  1674. if (!cpp_demangle_push_str(ddata, "std::", 5))
  1675. goto clean;
  1676. if (!vector_str_push(&v, "std::", 5))
  1677. goto clean;
  1678. ddata->cur += 2;
  1679. output = ddata->push_head > 0 ? &ddata->output_tmp : &ddata->output;
  1680. p_idx = output->size;
  1681. if (!cpp_demangle_read_uqname(ddata))
  1682. goto clean;
  1683. if ((subst_str = vector_str_substr(output, p_idx, output->size - 1,
  1684. &subst_str_len)) == NULL)
  1685. goto clean;
  1686. if (!vector_str_push(&v, subst_str, subst_str_len))
  1687. goto clean;
  1688. if (!cpp_demangle_push_subst_v(ddata, &v))
  1689. goto clean;
  1690. if (*ddata->cur == 'I') {
  1691. p_idx = output->size;
  1692. if (!cpp_demangle_read_tmpl_args(ddata))
  1693. goto clean;
  1694. free(subst_str);
  1695. if ((subst_str = vector_str_substr(output, p_idx,
  1696. output->size - 1, &subst_str_len)) == NULL)
  1697. goto clean;
  1698. if (!vector_str_push(&v, subst_str, subst_str_len))
  1699. goto clean;
  1700. if (!cpp_demangle_push_subst_v(ddata, &v))
  1701. goto clean;
  1702. }
  1703. rtn = 1;
  1704. clean:
  1705. if (subst_str) free(subst_str);
  1706. vector_str_dest(&v);
  1707. return (rtn);
  1708. }
  1709. static int
  1710. cpp_demangle_read_subst_stdtmpl(struct cpp_demangle_data *ddata,
  1711. const char *str, size_t len)
  1712. {
  1713. struct vector_str *output;
  1714. size_t p_idx, substr_len;
  1715. int rtn;
  1716. char *subst_str, *substr;
  1717. if (ddata == NULL || str == NULL || len == 0)
  1718. return (0);
  1719. output = ddata->push_head > 0 ? &ddata->output_tmp : &ddata->output;
  1720. p_idx = output->size;
  1721. substr = NULL;
  1722. subst_str = NULL;
  1723. if (!cpp_demangle_read_tmpl_args(ddata))
  1724. return (0);
  1725. if ((substr = vector_str_substr(output, p_idx, output->size - 1,
  1726. &substr_len)) == NULL)
  1727. return (0);
  1728. rtn = 0;
  1729. if ((subst_str = malloc(sizeof(char) * (substr_len + len + 1))) ==
  1730. NULL)
  1731. goto clean;
  1732. memcpy(subst_str, str, len);
  1733. memcpy(subst_str + len, substr, substr_len);
  1734. subst_str[substr_len + len] = '\0';
  1735. if (!cpp_demangle_push_subst(ddata, subst_str, substr_len + len))
  1736. goto clean;
  1737. rtn = 1;
  1738. clean:
  1739. if (subst_str) free(subst_str);
  1740. free(substr);
  1741. return (rtn);
  1742. }
  1743. static int
  1744. cpp_demangle_read_tmpl_arg(struct cpp_demangle_data *ddata)
  1745. {
  1746. if (ddata == NULL || *ddata->cur == '\0')
  1747. return (0);
  1748. switch (*ddata->cur) {
  1749. case 'L':
  1750. return (cpp_demangle_read_expr_primary(ddata));
  1751. case 'X':
  1752. return (cpp_demangle_read_expression(ddata));
  1753. };
  1754. return (cpp_demangle_read_type(ddata, 0));
  1755. }
  1756. static int
  1757. cpp_demangle_read_tmpl_args(struct cpp_demangle_data *ddata)
  1758. {
  1759. struct vector_str *v;
  1760. size_t arg_len, idx, limit, size;
  1761. char *arg;
  1762. if (ddata == NULL || *ddata->cur == '\0')
  1763. return (0);
  1764. ++ddata->cur;
  1765. if (!vector_read_cmd_push(&ddata->cmd, READ_TMPL))
  1766. return (0);
  1767. if (!cpp_demangle_push_str(ddata, "<", 1))
  1768. return (0);
  1769. limit = 0;
  1770. v = ddata->push_head > 0 ? &ddata->output_tmp : &ddata->output;
  1771. for (;;) {
  1772. idx = v->size;
  1773. if (!cpp_demangle_read_tmpl_arg(ddata))
  1774. return (0);
  1775. if ((arg = vector_str_substr(v, idx, v->size - 1, &arg_len)) ==
  1776. NULL)
  1777. return (0);
  1778. if (!vector_str_find(&ddata->tmpl, arg, arg_len) &&
  1779. !vector_str_push(&ddata->tmpl, arg, arg_len)) {
  1780. free(arg);
  1781. return (0);
  1782. }
  1783. free(arg);
  1784. if (*ddata->cur == 'E') {
  1785. ++ddata->cur;
  1786. size = v->size;
  1787. assert(size > 0);
  1788. if (!strncmp(v->container[size - 1], ">", 1)) {
  1789. if (!cpp_demangle_push_str(ddata, " >", 2))
  1790. return (0);
  1791. } else if (!cpp_demangle_push_str(ddata, ">", 1))
  1792. return (0);
  1793. break;
  1794. } else if (*ddata->cur != 'I' &&
  1795. !cpp_demangle_push_str(ddata, ", ", 2))
  1796. return (0);
  1797. if (limit++ > CPP_DEMANGLE_TRY_LIMIT)
  1798. return (0);
  1799. }
  1800. return (vector_read_cmd_pop(&ddata->cmd));
  1801. }
  1802. /*
  1803. * Read template parameter that forms in 'T[number]_'.
  1804. * This function much like to read_subst but only for types.
  1805. */
  1806. static int
  1807. cpp_demangle_read_tmpl_param(struct cpp_demangle_data *ddata)
  1808. {
  1809. long nth;
  1810. if (ddata == NULL || *ddata->cur != 'T')
  1811. return (0);
  1812. ++ddata->cur;
  1813. if (*ddata->cur == '_')
  1814. return (cpp_demangle_get_tmpl_param(ddata, 0));
  1815. else {
  1816. errno = 0;
  1817. if ((nth = strtol(ddata->cur, (char **) NULL, 36)) == 0 &&
  1818. errno != 0)
  1819. return (0);
  1820. /* T_ is first */
  1821. ++nth;
  1822. while (*ddata->cur != '_')
  1823. ++ddata->cur;
  1824. assert(nth > 0);
  1825. return (cpp_demangle_get_tmpl_param(ddata, nth));
  1826. }
  1827. /* NOTREACHED */
  1828. return (0);
  1829. }
  1830. static int
  1831. cpp_demangle_read_type(struct cpp_demangle_data *ddata, int delimit)
  1832. {
  1833. struct vector_type_qualifier v;
  1834. struct vector_str *output;
  1835. size_t p_idx, type_str_len;
  1836. int extern_c, is_builtin;
  1837. long len;
  1838. char *type_str;
  1839. if (ddata == NULL)
  1840. return (0);
  1841. output = &ddata->output;
  1842. if (ddata->output.size > 0 && !strncmp(ddata->output.container[ddata->output.size - 1], ">", 1)) {
  1843. ddata->push_head++;
  1844. output = &ddata->output_tmp;
  1845. } else if (delimit == 1) {
  1846. if (ddata->paren == false) {
  1847. if (!cpp_demangle_push_str(ddata, "(", 1))
  1848. return (0);
  1849. if (ddata->output.size < 2)
  1850. return (0);
  1851. ddata->paren = true;
  1852. ddata->pfirst = true;
  1853. /* Need pop function name */
  1854. if (ddata->subst.size == 1 &&
  1855. !vector_str_pop(&ddata->subst))
  1856. return (0);
  1857. }
  1858. if (ddata->pfirst)
  1859. ddata->pfirst = false;
  1860. else if (*ddata->cur != 'I' &&
  1861. !cpp_demangle_push_str(ddata, ", ", 2))
  1862. return (0);
  1863. }
  1864. assert(output != NULL);
  1865. /*
  1866. * [r, V, K] [P, R, C, G, U] builtin, function, class-enum, array
  1867. * pointer-to-member, template-param, template-template-param, subst
  1868. */
  1869. if (!vector_type_qualifier_init(&v))
  1870. return (0);
  1871. extern_c = 0;
  1872. is_builtin = 1;
  1873. p_idx = output->size;
  1874. type_str = NULL;
  1875. again:
  1876. /* builtin type */
  1877. switch (*ddata->cur) {
  1878. case 'a':
  1879. /* signed char */
  1880. if (!cpp_demangle_push_str(ddata, "signed char", 11))
  1881. goto clean;
  1882. ++ddata->cur;
  1883. goto rtn;
  1884. case 'A':
  1885. /* array type */
  1886. if (!cpp_demangle_read_array(ddata))
  1887. goto clean;
  1888. is_builtin = 0;
  1889. goto rtn;
  1890. case 'b':
  1891. /* bool */
  1892. if (!cpp_demangle_push_str(ddata, "bool", 4))
  1893. goto clean;
  1894. ++ddata->cur;
  1895. goto rtn;
  1896. case 'C':
  1897. /* complex pair */
  1898. if (!vector_type_qualifier_push(&v, TYPE_CMX))
  1899. goto clean;
  1900. ++ddata->cur;
  1901. goto again;
  1902. case 'c':
  1903. /* char */
  1904. if (!cpp_demangle_push_str(ddata, "char", 4))
  1905. goto clean;
  1906. ++ddata->cur;
  1907. goto rtn;
  1908. case 'd':
  1909. /* double */
  1910. if (!cpp_demangle_push_str(ddata, "double", 6))
  1911. goto clean;
  1912. ++ddata->cur;
  1913. goto rtn;
  1914. case 'e':
  1915. /* long double */
  1916. if (!cpp_demangle_push_str(ddata, "long double", 11))
  1917. goto clean;
  1918. ++ddata->cur;
  1919. goto rtn;
  1920. case 'f':
  1921. /* float */
  1922. if (!cpp_demangle_push_str(ddata, "float", 5))
  1923. goto clean;
  1924. ++ddata->cur;
  1925. goto rtn;
  1926. case 'F':
  1927. /* function */
  1928. if (!cpp_demangle_read_function(ddata, &extern_c, &v))
  1929. goto clean;
  1930. is_builtin = 0;
  1931. goto rtn;
  1932. case 'g':
  1933. /* __float128 */
  1934. if (!cpp_demangle_push_str(ddata, "__float128", 10))
  1935. goto clean;
  1936. ++ddata->cur;
  1937. goto rtn;
  1938. case 'G':
  1939. /* imaginary */
  1940. if (!vector_type_qualifier_push(&v, TYPE_IMG))
  1941. goto clean;
  1942. ++ddata->cur;
  1943. goto again;
  1944. case 'h':
  1945. /* unsigned char */
  1946. if (!cpp_demangle_push_str(ddata, "unsigned char", 13))
  1947. goto clean;
  1948. ++ddata->cur;
  1949. goto rtn;
  1950. case 'i':
  1951. /* int */
  1952. if (!cpp_demangle_push_str(ddata, "int", 3))
  1953. goto clean;
  1954. ++ddata->cur;
  1955. goto rtn;
  1956. case 'j':
  1957. /* unsigned int */
  1958. if (!cpp_demangle_push_str(ddata, "unsigned int", 12))
  1959. goto clean;
  1960. ++ddata->cur;
  1961. goto rtn;
  1962. case 'K':
  1963. /* const */
  1964. if (!vector_type_qualifier_push(&v, TYPE_CST))
  1965. goto clean;
  1966. ++ddata->cur;
  1967. goto again;
  1968. case 'l':
  1969. /* long */
  1970. if (!cpp_demangle_push_str(ddata, "long", 4))
  1971. goto clean;
  1972. ++ddata->cur;
  1973. goto rtn;
  1974. case 'm':
  1975. /* unsigned long */
  1976. if (!cpp_demangle_push_str(ddata, "unsigned long", 13))
  1977. goto clean;
  1978. ++ddata->cur;
  1979. goto rtn;
  1980. case 'M':
  1981. /* pointer to member */
  1982. if (!cpp_demangle_read_pointer_to_member(ddata))
  1983. goto clean;
  1984. is_builtin = 0;
  1985. goto rtn;
  1986. case 'n':
  1987. /* __int128 */
  1988. if (!cpp_demangle_push_str(ddata, "__int128", 8))
  1989. goto clean;
  1990. ++ddata->cur;
  1991. goto rtn;
  1992. case 'o':
  1993. /* unsigned __int128 */
  1994. if (!cpp_demangle_push_str(ddata, "unsigned _;int128", 17))
  1995. goto clean;
  1996. ++ddata->cur;
  1997. goto rtn;
  1998. case 'P':
  1999. /* pointer */
  2000. if (!vector_type_qualifier_push(&v, TYPE_PTR))
  2001. goto clean;
  2002. ++ddata->cur;
  2003. goto again;
  2004. case 'r':
  2005. /* restrict */
  2006. if (!vector_type_qualifier_push(&v, TYPE_RST))
  2007. goto clean;
  2008. ++ddata->cur;
  2009. goto again;
  2010. case 'R':
  2011. /* reference */
  2012. if (!vector_type_qualifier_push(&v, TYPE_REF))
  2013. goto clean;
  2014. ++ddata->cur;
  2015. goto again;
  2016. case 's':
  2017. /* short, local string */
  2018. if (!cpp_demangle_push_str(ddata, "short", 5))
  2019. goto clean;
  2020. ++ddata->cur;
  2021. goto rtn;
  2022. case 'S':
  2023. /* substitution */
  2024. if (!cpp_demangle_read_subst(ddata))
  2025. goto clean;
  2026. is_builtin = 0;
  2027. goto rtn;
  2028. case 't':
  2029. /* unsigned short */
  2030. if (!cpp_demangle_push_str(ddata, "unsigned short", 14))
  2031. goto clean;
  2032. ++ddata->cur;
  2033. goto rtn;
  2034. case 'T':
  2035. /* template parameter */
  2036. if (!cpp_demangle_read_tmpl_param(ddata))
  2037. goto clean;
  2038. is_builtin = 0;
  2039. goto rtn;
  2040. case 'u':
  2041. /* vendor extended builtin */
  2042. ++ddata->cur;
  2043. if (!cpp_demangle_read_sname(ddata))
  2044. goto clean;
  2045. is_builtin = 0;
  2046. goto rtn;
  2047. case 'U':
  2048. /* vendor extended type qualifier */
  2049. if (!cpp_demangle_read_number(ddata, &len))
  2050. goto clean;
  2051. if (len <= 0)
  2052. goto clean;
  2053. if (!vector_str_push(&v.ext_name, ddata->cur, len))
  2054. goto clean;
  2055. ddata->cur += len;
  2056. goto again;
  2057. case 'v':
  2058. /* void */
  2059. if (!cpp_demangle_push_str(ddata, "void", 4))
  2060. goto clean;
  2061. ++ddata->cur;
  2062. goto rtn;
  2063. case 'V':
  2064. /* volatile */
  2065. if (!vector_type_qualifier_push(&v, TYPE_VAT))
  2066. goto clean;
  2067. ++ddata->cur;
  2068. goto again;
  2069. case 'w':
  2070. /* wchar_t */
  2071. if (!cpp_demangle_push_str(ddata, "wchar_t", 6))
  2072. goto clean;
  2073. ++ddata->cur;
  2074. goto rtn;
  2075. case 'x':
  2076. /* long long */
  2077. if (!cpp_demangle_push_str(ddata, "long long", 9))
  2078. goto clean;
  2079. ++ddata->cur;
  2080. goto rtn;
  2081. case 'y':
  2082. /* unsigned long long */
  2083. if (!cpp_demangle_push_str(ddata, "unsigned long long", 18))
  2084. goto clean;
  2085. ++ddata->cur;
  2086. goto rtn;
  2087. case 'z':
  2088. /* ellipsis */
  2089. if (!cpp_demangle_push_str(ddata, "ellipsis", 8))
  2090. goto clean;
  2091. ++ddata->cur;
  2092. goto rtn;
  2093. };
  2094. if (!cpp_demangle_read_name(ddata))
  2095. goto clean;
  2096. is_builtin = 0;
  2097. rtn:
  2098. if ((type_str = vector_str_substr(output, p_idx, output->size - 1,
  2099. &type_str_len)) == NULL)
  2100. goto clean;
  2101. if (is_builtin == 0) {
  2102. if (!vector_str_find(&ddata->subst, type_str, type_str_len) &&
  2103. !vector_str_push(&ddata->subst, type_str, type_str_len))
  2104. goto clean;
  2105. }
  2106. if (!cpp_demangle_push_type_qualifier(ddata, &v, type_str))
  2107. goto clean;
  2108. free(type_str);
  2109. vector_type_qualifier_dest(&v);
  2110. if (ddata->push_head > 0) {
  2111. if (*ddata->cur == 'I' && cpp_demangle_read_tmpl_args(ddata)
  2112. == 0)
  2113. return (0);
  2114. if (--ddata->push_head > 0)
  2115. return (1);
  2116. if (!vector_str_push(&ddata->output_tmp, " ", 1))
  2117. return (0);
  2118. if (!vector_str_push_vector_head(&ddata->output,
  2119. &ddata->output_tmp))
  2120. return (0);
  2121. vector_str_dest(&ddata->output_tmp);
  2122. if (!vector_str_init(&ddata->output_tmp))
  2123. return (0);
  2124. if (!cpp_demangle_push_str(ddata, "(", 1))
  2125. return (0);
  2126. ddata->paren = true;
  2127. ddata->pfirst = true;
  2128. }
  2129. return (1);
  2130. clean:
  2131. if (type_str) free(type_str);
  2132. vector_type_qualifier_dest(&v);
  2133. return (0);
  2134. }
  2135. /*
  2136. * read unqualified-name, unqualified name are operator-name, ctor-dtor-name,
  2137. * source-name
  2138. */
  2139. static int
  2140. cpp_demangle_read_uqname(struct cpp_demangle_data *ddata)
  2141. {
  2142. size_t len;
  2143. if (ddata == NULL || *ddata->cur == '\0')
  2144. return (0);
  2145. /* operator name */
  2146. switch (SIMPLE_HASH(*ddata->cur, *(ddata->cur + 1))) {
  2147. case SIMPLE_HASH('a', 'a'):
  2148. /* operator && */
  2149. if (!cpp_demangle_push_str(ddata, "operator&&", 10))
  2150. return (0);
  2151. ddata->cur += 2;
  2152. return (1);
  2153. case SIMPLE_HASH('a', 'd'):
  2154. /* operator & (unary) */
  2155. if (!cpp_demangle_push_str(ddata, "operator&", 9))
  2156. return (0);
  2157. ddata->cur += 2;
  2158. return (1);
  2159. case SIMPLE_HASH('a', 'n'):
  2160. /* operator & */
  2161. if (!cpp_demangle_push_str(ddata, "operator&", 9))
  2162. return (0);
  2163. ddata->cur += 2;
  2164. return (1);
  2165. case SIMPLE_HASH('a', 'N'):
  2166. /* operator &= */
  2167. if (!cpp_demangle_push_str(ddata, "operator&=", 10))
  2168. return (0);
  2169. ddata->cur += 2;
  2170. return (1);
  2171. case SIMPLE_HASH('a', 'S'):
  2172. /* operator = */
  2173. if (!cpp_demangle_push_str(ddata, "operator=", 9))
  2174. return (0);
  2175. ddata->cur += 2;
  2176. return (1);
  2177. case SIMPLE_HASH('c', 'l'):
  2178. /* operator () */
  2179. if (!cpp_demangle_push_str(ddata, "operator()", 10))
  2180. return (0);
  2181. ddata->cur += 2;
  2182. return (1);
  2183. case SIMPLE_HASH('c', 'm'):
  2184. /* operator , */
  2185. if (!cpp_demangle_push_str(ddata, "operator,", 9))
  2186. return (0);
  2187. ddata->cur += 2;
  2188. return (1);
  2189. case SIMPLE_HASH('c', 'o'):
  2190. /* operator ~ */
  2191. if (!cpp_demangle_push_str(ddata, "operator~", 9))
  2192. return (0);
  2193. ddata->cur += 2;
  2194. return (1);
  2195. case SIMPLE_HASH('c', 'v'):
  2196. /* operator (cast) */
  2197. if (!cpp_demangle_push_str(ddata, "operator(cast)", 14))
  2198. return (0);
  2199. ddata->cur += 2;
  2200. return (cpp_demangle_read_type(ddata, 1));
  2201. case SIMPLE_HASH('d', 'a'):
  2202. /* operator delete [] */
  2203. if (!cpp_demangle_push_str(ddata, "operator delete []", 18))
  2204. return (0);
  2205. ddata->cur += 2;
  2206. return (1);
  2207. case SIMPLE_HASH('d', 'e'):
  2208. /* operator * (unary) */
  2209. if (!cpp_demangle_push_str(ddata, "operator*", 9))
  2210. return (0);
  2211. ddata->cur += 2;
  2212. return (1);
  2213. case SIMPLE_HASH('d', 'l'):
  2214. /* operator delete */
  2215. if (!cpp_demangle_push_str(ddata, "operator delete", 15))
  2216. return (0);
  2217. ddata->cur += 2;
  2218. return (1);
  2219. case SIMPLE_HASH('d', 'v'):
  2220. /* operator / */
  2221. if (!cpp_demangle_push_str(ddata, "operator/", 9))
  2222. return (0);
  2223. ddata->cur += 2;
  2224. return (1);
  2225. case SIMPLE_HASH('d', 'V'):
  2226. /* operator /= */
  2227. if (!cpp_demangle_push_str(ddata, "operator/=", 10))
  2228. return (0);
  2229. ddata->cur += 2;
  2230. return (1);
  2231. case SIMPLE_HASH('e', 'o'):
  2232. /* operator ^ */
  2233. if (!cpp_demangle_push_str(ddata, "operator^", 9))
  2234. return (0);
  2235. ddata->cur += 2;
  2236. return (1);
  2237. case SIMPLE_HASH('e', 'O'):
  2238. /* operator ^= */
  2239. if (!cpp_demangle_push_str(ddata, "operator^=", 10))
  2240. return (0);
  2241. ddata->cur += 2;
  2242. return (1);
  2243. case SIMPLE_HASH('e', 'q'):
  2244. /* operator == */
  2245. if (!cpp_demangle_push_str(ddata, "operator==", 10))
  2246. return (0);
  2247. ddata->cur += 2;
  2248. return (1);
  2249. case SIMPLE_HASH('g', 'e'):
  2250. /* operator >= */
  2251. if (!cpp_demangle_push_str(ddata, "operator>=", 10))
  2252. return (0);
  2253. ddata->cur += 2;
  2254. return (1);
  2255. case SIMPLE_HASH('g', 't'):
  2256. /* operator > */
  2257. if (!cpp_demangle_push_str(ddata, "operator>", 9))
  2258. return (0);
  2259. ddata->cur += 2;
  2260. return (1);
  2261. case SIMPLE_HASH('i', 'x'):
  2262. /* operator [] */
  2263. if (!cpp_demangle_push_str(ddata, "operator[]", 10))
  2264. return (0);
  2265. ddata->cur += 2;
  2266. return (1);
  2267. case SIMPLE_HASH('l', 'e'):
  2268. /* operator <= */
  2269. if (!cpp_demangle_push_str(ddata, "operator<=", 10))
  2270. return (0);
  2271. ddata->cur += 2;
  2272. return (1);
  2273. case SIMPLE_HASH('l', 's'):
  2274. /* operator << */
  2275. if (!cpp_demangle_push_str(ddata, "operator<<", 10))
  2276. return (0);
  2277. ddata->cur += 2;
  2278. return (1);
  2279. case SIMPLE_HASH('l', 'S'):
  2280. /* operator <<= */
  2281. if (!cpp_demangle_push_str(ddata, "operator<<=", 11))
  2282. return (0);
  2283. ddata->cur += 2;
  2284. return (1);
  2285. case SIMPLE_HASH('l', 't'):
  2286. /* operator < */
  2287. if (!cpp_demangle_push_str(ddata, "operator<", 9))
  2288. return (0);
  2289. ddata->cur += 2;
  2290. return (1);
  2291. case SIMPLE_HASH('m', 'i'):
  2292. /* operator - */
  2293. if (!cpp_demangle_push_str(ddata, "operator-", 9))
  2294. return (0);
  2295. ddata->cur += 2;
  2296. return (1);
  2297. case SIMPLE_HASH('m', 'I'):
  2298. /* operator -= */
  2299. if (!cpp_demangle_push_str(ddata, "operator-=", 10))
  2300. return (0);
  2301. ddata->cur += 2;
  2302. return (1);
  2303. case SIMPLE_HASH('m', 'l'):
  2304. /* operator * */
  2305. if (!cpp_demangle_push_str(ddata, "operator*", 9))
  2306. return (0);
  2307. ddata->cur += 2;
  2308. return (1);
  2309. case SIMPLE_HASH('m', 'L'):
  2310. /* operator *= */
  2311. if (!cpp_demangle_push_str(ddata, "operator*=", 10))
  2312. return (0);
  2313. ddata->cur += 2;
  2314. return (1);
  2315. case SIMPLE_HASH('m', 'm'):
  2316. /* operator -- */
  2317. if (!cpp_demangle_push_str(ddata, "operator--", 10))
  2318. return (0);
  2319. ddata->cur += 2;
  2320. return (1);
  2321. case SIMPLE_HASH('n', 'a'):
  2322. /* operator new[] */
  2323. if (!cpp_demangle_push_str(ddata, "operator new []", 15))
  2324. return (0);
  2325. ddata->cur += 2;
  2326. return (1);
  2327. case SIMPLE_HASH('n', 'e'):
  2328. /* operator != */
  2329. if (!cpp_demangle_push_str(ddata, "operator!=", 10))
  2330. return (0);
  2331. ddata->cur += 2;
  2332. return (1);
  2333. case SIMPLE_HASH('n', 'g'):
  2334. /* operator - (unary) */
  2335. if (!cpp_demangle_push_str(ddata, "operator-", 9))
  2336. return (0);
  2337. ddata->cur += 2;
  2338. return (1);
  2339. case SIMPLE_HASH('n', 't'):
  2340. /* operator ! */
  2341. if (!cpp_demangle_push_str(ddata, "operator!", 9))
  2342. return (0);
  2343. ddata->cur += 2;
  2344. return (1);
  2345. case SIMPLE_HASH('n', 'w'):
  2346. /* operator new */
  2347. if (!cpp_demangle_push_str(ddata, "operator new", 12))
  2348. return (0);
  2349. ddata->cur += 2;
  2350. return (1);
  2351. case SIMPLE_HASH('o', 'o'):
  2352. /* operator || */
  2353. if (!cpp_demangle_push_str(ddata, "operator||", 10))
  2354. return (0);
  2355. ddata->cur += 2;
  2356. return (1);
  2357. case SIMPLE_HASH('o', 'r'):
  2358. /* operator | */
  2359. if (!cpp_demangle_push_str(ddata, "operator|", 9))
  2360. return (0);
  2361. ddata->cur += 2;
  2362. return (1);
  2363. case SIMPLE_HASH('o', 'R'):
  2364. /* operator |= */
  2365. if (!cpp_demangle_push_str(ddata, "operator|=", 10))
  2366. return (0);
  2367. ddata->cur += 2;
  2368. return (1);
  2369. case SIMPLE_HASH('p', 'l'):
  2370. /* operator + */
  2371. if (!cpp_demangle_push_str(ddata, "operator+", 9))
  2372. return (0);
  2373. ddata->cur += 2;
  2374. return (1);
  2375. case SIMPLE_HASH('p', 'L'):
  2376. /* operator += */
  2377. if (!cpp_demangle_push_str(ddata, "operator+=", 10))
  2378. return (0);
  2379. ddata->cur += 2;
  2380. return (1);
  2381. case SIMPLE_HASH('p', 'm'):
  2382. /* operator ->* */
  2383. if (!cpp_demangle_push_str(ddata, "operator->*", 11))
  2384. return (0);
  2385. ddata->cur += 2;
  2386. return (1);
  2387. case SIMPLE_HASH('p', 'p'):
  2388. /* operator ++ */
  2389. if (!cpp_demangle_push_str(ddata, "operator++", 10))
  2390. return (0);
  2391. ddata->cur += 2;
  2392. return (1);
  2393. case SIMPLE_HASH('p', 's'):
  2394. /* operator + (unary) */
  2395. if (!cpp_demangle_push_str(ddata, "operator+", 9))
  2396. return (0);
  2397. ddata->cur += 2;
  2398. return (1);
  2399. case SIMPLE_HASH('p', 't'):
  2400. /* operator -> */
  2401. if (!cpp_demangle_push_str(ddata, "operator->", 10))
  2402. return (0);
  2403. ddata->cur += 2;
  2404. return (1);
  2405. case SIMPLE_HASH('q', 'u'):
  2406. /* operator ? */
  2407. if (!cpp_demangle_push_str(ddata, "operator?", 9))
  2408. return (0);
  2409. ddata->cur += 2;
  2410. return (1);
  2411. case SIMPLE_HASH('r', 'm'):
  2412. /* operator % */
  2413. if (!cpp_demangle_push_str(ddata, "operator%", 9))
  2414. return (0);
  2415. ddata->cur += 2;
  2416. return (1);
  2417. case SIMPLE_HASH('r', 'M'):
  2418. /* operator %= */
  2419. if (!cpp_demangle_push_str(ddata, "operator%=", 10))
  2420. return (0);
  2421. ddata->cur += 2;
  2422. return (1);
  2423. case SIMPLE_HASH('r', 's'):
  2424. /* operator >> */
  2425. if (!cpp_demangle_push_str(ddata, "operator>>", 10))
  2426. return (0);
  2427. ddata->cur += 2;
  2428. return (1);
  2429. case SIMPLE_HASH('r', 'S'):
  2430. /* operator >>= */
  2431. if (!cpp_demangle_push_str(ddata, "operator>>=", 11))
  2432. return (0);
  2433. ddata->cur += 2;
  2434. return (1);
  2435. case SIMPLE_HASH('r', 'z'):
  2436. /* operator sizeof */
  2437. if (!cpp_demangle_push_str(ddata, "operator sizeof ", 16))
  2438. return (0);
  2439. ddata->cur += 2;
  2440. return (1);
  2441. case SIMPLE_HASH('s', 'r'):
  2442. /* scope resolution operator */
  2443. if (!cpp_demangle_push_str(ddata, "scope resolution operator ",
  2444. 26))
  2445. return (0);
  2446. ddata->cur += 2;
  2447. return (1);
  2448. case SIMPLE_HASH('s', 'v'):
  2449. /* operator sizeof */
  2450. if (!cpp_demangle_push_str(ddata, "operator sizeof ", 16))
  2451. return (0);
  2452. ddata->cur += 2;
  2453. return (1);
  2454. };
  2455. /* vendor extened operator */
  2456. if (*ddata->cur == 'v' && ELFTC_ISDIGIT(*(ddata->cur + 1))) {
  2457. if (!cpp_demangle_push_str(ddata, "vendor extened operator ",
  2458. 24))
  2459. return (0);
  2460. if (!cpp_demangle_push_str(ddata, ddata->cur + 1, 1))
  2461. return (0);
  2462. ddata->cur += 2;
  2463. return (cpp_demangle_read_sname(ddata));
  2464. }
  2465. /* ctor-dtor-name */
  2466. switch (SIMPLE_HASH(*ddata->cur, *(ddata->cur + 1))) {
  2467. case SIMPLE_HASH('C', '1'):
  2468. /* FALLTHROUGH */
  2469. case SIMPLE_HASH('C', '2'):
  2470. /* FALLTHROUGH */
  2471. case SIMPLE_HASH('C', '3'):
  2472. if (ddata->last_sname == NULL)
  2473. return (0);
  2474. if ((len = strlen(ddata->last_sname)) == 0)
  2475. return (0);
  2476. if (!cpp_demangle_push_str(ddata, "::", 2))
  2477. return (0);
  2478. if (!cpp_demangle_push_str(ddata, ddata->last_sname, len))
  2479. return (0);
  2480. ddata->cur +=2;
  2481. return (1);
  2482. case SIMPLE_HASH('D', '0'):
  2483. /* FALLTHROUGH */
  2484. case SIMPLE_HASH('D', '1'):
  2485. /* FALLTHROUGH */
  2486. case SIMPLE_HASH('D', '2'):
  2487. if (ddata->last_sname == NULL)
  2488. return (0);
  2489. if ((len = strlen(ddata->last_sname)) == 0)
  2490. return (0);
  2491. if (!cpp_demangle_push_str(ddata, "::~", 3))
  2492. return (0);
  2493. if (!cpp_demangle_push_str(ddata, ddata->last_sname, len))
  2494. return (0);
  2495. ddata->cur +=2;
  2496. return (1);
  2497. };
  2498. /* source name */
  2499. if (ELFTC_ISDIGIT(*ddata->cur) != 0)
  2500. return (cpp_demangle_read_sname(ddata));
  2501. return (1);
  2502. }
  2503. static int
  2504. cpp_demangle_read_v_offset(struct cpp_demangle_data *ddata)
  2505. {
  2506. if (ddata == NULL)
  2507. return (0);
  2508. if (!cpp_demangle_push_str(ddata, "offset : ", 9))
  2509. return (0);
  2510. if (!cpp_demangle_read_offset_number(ddata))
  2511. return (0);
  2512. if (!cpp_demangle_push_str(ddata, "virtual offset : ", 17))
  2513. return (0);
  2514. return (!cpp_demangle_read_offset_number(ddata));
  2515. }
  2516. /*
  2517. * Decode floating point representation to string
  2518. * Return new allocated string or NULL
  2519. *
  2520. * Todo
  2521. * Replace these functions to macro.
  2522. */
  2523. static char *
  2524. decode_fp_to_double(const char *p, size_t len)
  2525. {
  2526. double f;
  2527. size_t rtn_len, limit, i;
  2528. int byte;
  2529. char *rtn;
  2530. if (p == NULL || len == 0 || len % 2 != 0 || len / 2 > sizeof(double))
  2531. return (NULL);
  2532. memset(&f, 0, sizeof(double));
  2533. for (i = 0; i < len / 2; ++i) {
  2534. byte = hex_to_dec(p[len - i * 2 - 1]) +
  2535. hex_to_dec(p[len - i * 2 - 2]) * 16;
  2536. if (byte < 0 || byte > 255)
  2537. return (NULL);
  2538. #if ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN
  2539. ((unsigned char *)&f)[i] = (unsigned char)(byte);
  2540. #else /* ELFTC_BYTE_ORDER != ELFTC_BYTE_ORDER_LITTLE_ENDIAN */
  2541. ((unsigned char *)&f)[sizeof(double) - i - 1] =
  2542. (unsigned char)(byte);
  2543. #endif /* ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN */
  2544. }
  2545. rtn_len = 64;
  2546. limit = 0;
  2547. again:
  2548. if ((rtn = malloc(sizeof(char) * rtn_len)) == NULL)
  2549. return (NULL);
  2550. if (snprintf(rtn, rtn_len, "%fld", f) >= (int)rtn_len) {
  2551. free(rtn);
  2552. if (limit++ > FLOAT_SPRINTF_TRY_LIMIT)
  2553. return (NULL);
  2554. rtn_len *= BUFFER_GROWFACTOR;
  2555. goto again;
  2556. }
  2557. return rtn;
  2558. }
  2559. static char *
  2560. decode_fp_to_float(const char *p, size_t len)
  2561. {
  2562. size_t i, rtn_len, limit;
  2563. float f;
  2564. int byte;
  2565. char *rtn;
  2566. if (p == NULL || len == 0 || len % 2 != 0 || len / 2 > sizeof(float))
  2567. return (NULL);
  2568. memset(&f, 0, sizeof(float));
  2569. for (i = 0; i < len / 2; ++i) {
  2570. byte = hex_to_dec(p[len - i * 2 - 1]) +
  2571. hex_to_dec(p[len - i * 2 - 2]) * 16;
  2572. if (byte < 0 || byte > 255)
  2573. return (NULL);
  2574. #if ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN
  2575. ((unsigned char *)&f)[i] = (unsigned char)(byte);
  2576. #else /* ELFTC_BYTE_ORDER != ELFTC_BYTE_ORDER_LITTLE_ENDIAN */
  2577. ((unsigned char *)&f)[sizeof(float) - i - 1] =
  2578. (unsigned char)(byte);
  2579. #endif /* ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN */
  2580. }
  2581. rtn_len = 64;
  2582. limit = 0;
  2583. again:
  2584. if ((rtn = malloc(sizeof(char) * rtn_len)) == NULL)
  2585. return (NULL);
  2586. if (snprintf(rtn, rtn_len, "%ff", f) >= (int)rtn_len) {
  2587. free(rtn);
  2588. if (limit++ > FLOAT_SPRINTF_TRY_LIMIT)
  2589. return (NULL);
  2590. rtn_len *= BUFFER_GROWFACTOR;
  2591. goto again;
  2592. }
  2593. return rtn;
  2594. }
  2595. static char *
  2596. decode_fp_to_float128(const char *p, size_t len)
  2597. {
  2598. long double f;
  2599. size_t rtn_len, limit, i;
  2600. int byte;
  2601. unsigned char buf[FLOAT_QUADRUPLE_BYTES];
  2602. char *rtn;
  2603. switch(sizeof(long double)) {
  2604. case FLOAT_QUADRUPLE_BYTES:
  2605. return (decode_fp_to_long_double(p, len));
  2606. case FLOAT_EXTENED_BYTES:
  2607. if (p == NULL || len == 0 || len % 2 != 0 ||
  2608. len / 2 > FLOAT_QUADRUPLE_BYTES)
  2609. return (NULL);
  2610. memset(buf, 0, FLOAT_QUADRUPLE_BYTES);
  2611. for (i = 0; i < len / 2; ++i) {
  2612. byte = hex_to_dec(p[len - i * 2 - 1]) +
  2613. hex_to_dec(p[len - i * 2 - 2]) * 16;
  2614. if (byte < 0 || byte > 255)
  2615. return (NULL);
  2616. #if ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN
  2617. buf[i] = (unsigned char)(byte);
  2618. #else /* ELFTC_BYTE_ORDER != ELFTC_BYTE_ORDER_LITTLE_ENDIAN */
  2619. buf[FLOAT_QUADRUPLE_BYTES - i -1] =
  2620. (unsigned char)(byte);
  2621. #endif /* ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN */
  2622. }
  2623. memset(&f, 0, FLOAT_EXTENED_BYTES);
  2624. #if ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN
  2625. memcpy(&f, buf, FLOAT_EXTENED_BYTES);
  2626. #else /* ELFTC_BYTE_ORDER != ELFTC_BYTE_ORDER_LITTLE_ENDIAN */
  2627. memcpy(&f, buf + 6, FLOAT_EXTENED_BYTES);
  2628. #endif /* ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN */
  2629. rtn_len = 256;
  2630. limit = 0;
  2631. again:
  2632. if ((rtn = malloc(sizeof(char) * rtn_len)) == NULL)
  2633. return (NULL);
  2634. if (snprintf(rtn, rtn_len, "%Lfd", f) >= (int)rtn_len) {
  2635. free(rtn);
  2636. if (limit++ > FLOAT_SPRINTF_TRY_LIMIT)
  2637. return (NULL);
  2638. rtn_len *= BUFFER_GROWFACTOR;
  2639. goto again;
  2640. }
  2641. return (rtn);
  2642. default:
  2643. return (NULL);
  2644. }
  2645. }
  2646. static char *
  2647. decode_fp_to_float80(const char *p, size_t len)
  2648. {
  2649. long double f;
  2650. size_t rtn_len, limit, i;
  2651. int byte;
  2652. unsigned char buf[FLOAT_EXTENED_BYTES];
  2653. char *rtn;
  2654. switch(sizeof(long double)) {
  2655. case FLOAT_QUADRUPLE_BYTES:
  2656. if (p == NULL || len == 0 || len % 2 != 0 ||
  2657. len / 2 > FLOAT_EXTENED_BYTES)
  2658. return (NULL);
  2659. memset(buf, 0, FLOAT_EXTENED_BYTES);
  2660. for (i = 0; i < len / 2; ++i) {
  2661. byte = hex_to_dec(p[len - i * 2 - 1]) +
  2662. hex_to_dec(p[len - i * 2 - 2]) * 16;
  2663. if (byte < 0 || byte > 255)
  2664. return (NULL);
  2665. #if ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN
  2666. buf[i] = (unsigned char)(byte);
  2667. #else /* ELFTC_BYTE_ORDER != ELFTC_BYTE_ORDER_LITTLE_ENDIAN */
  2668. buf[FLOAT_EXTENED_BYTES - i -1] =
  2669. (unsigned char)(byte);
  2670. #endif /* ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN */
  2671. }
  2672. memset(&f, 0, FLOAT_QUADRUPLE_BYTES);
  2673. #if ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN
  2674. memcpy(&f, buf, FLOAT_EXTENED_BYTES);
  2675. #else /* ELFTC_BYTE_ORDER != ELFTC_BYTE_ORDER_LITTLE_ENDIAN */
  2676. memcpy((unsigned char *)(&f) + 6, buf, FLOAT_EXTENED_BYTES);
  2677. #endif /* ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN */
  2678. rtn_len = 256;
  2679. limit = 0;
  2680. again:
  2681. if ((rtn = malloc(sizeof(char) * rtn_len)) == NULL)
  2682. return (NULL);
  2683. if (snprintf(rtn, rtn_len, "%Lfd", f) >= (int)rtn_len) {
  2684. free(rtn);
  2685. if (limit++ > FLOAT_SPRINTF_TRY_LIMIT)
  2686. return (NULL);
  2687. rtn_len *= BUFFER_GROWFACTOR;
  2688. goto again;
  2689. }
  2690. return (rtn);
  2691. case FLOAT_EXTENED_BYTES:
  2692. return (decode_fp_to_long_double(p, len));
  2693. default:
  2694. return (NULL);
  2695. }
  2696. }
  2697. static char *
  2698. decode_fp_to_long_double(const char *p, size_t len)
  2699. {
  2700. long double f;
  2701. size_t rtn_len, limit, i;
  2702. int byte;
  2703. char *rtn;
  2704. if (p == NULL || len == 0 || len % 2 != 0 ||
  2705. len / 2 > sizeof(long double))
  2706. return (NULL);
  2707. memset(&f, 0, sizeof(long double));
  2708. for (i = 0; i < len / 2; ++i) {
  2709. byte = hex_to_dec(p[len - i * 2 - 1]) +
  2710. hex_to_dec(p[len - i * 2 - 2]) * 16;
  2711. if (byte < 0 || byte > 255)
  2712. return (NULL);
  2713. #if ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN
  2714. ((unsigned char *)&f)[i] = (unsigned char)(byte);
  2715. #else /* ELFTC_BYTE_ORDER != ELFTC_BYTE_ORDER_LITTLE_ENDIAN */
  2716. ((unsigned char *)&f)[sizeof(long double) - i - 1] =
  2717. (unsigned char)(byte);
  2718. #endif /* ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN */
  2719. }
  2720. rtn_len = 256;
  2721. limit = 0;
  2722. again:
  2723. if ((rtn = malloc(sizeof(char) * rtn_len)) == NULL)
  2724. return (NULL);
  2725. if (snprintf(rtn, rtn_len, "%Lfd", f) >= (int)rtn_len) {
  2726. free(rtn);
  2727. if (limit++ > FLOAT_SPRINTF_TRY_LIMIT)
  2728. return (NULL);
  2729. rtn_len *= BUFFER_GROWFACTOR;
  2730. goto again;
  2731. }
  2732. return (rtn);
  2733. }
  2734. /* Simple hex to integer function used by decode_to_* function. */
  2735. static int
  2736. hex_to_dec(char c)
  2737. {
  2738. switch (c) {
  2739. case '0':
  2740. return (0);
  2741. case '1':
  2742. return (1);
  2743. case '2':
  2744. return (2);
  2745. case '3':
  2746. return (3);
  2747. case '4':
  2748. return (4);
  2749. case '5':
  2750. return (5);
  2751. case '6':
  2752. return (6);
  2753. case '7':
  2754. return (7);
  2755. case '8':
  2756. return (8);
  2757. case '9':
  2758. return (9);
  2759. case 'a':
  2760. return (10);
  2761. case 'b':
  2762. return (11);
  2763. case 'c':
  2764. return (12);
  2765. case 'd':
  2766. return (13);
  2767. case 'e':
  2768. return (14);
  2769. case 'f':
  2770. return (15);
  2771. default:
  2772. return (-1);
  2773. };
  2774. }
  2775. static void
  2776. vector_read_cmd_dest(struct vector_read_cmd *v)
  2777. {
  2778. if (v == NULL)
  2779. return;
  2780. free(v->r_container);
  2781. }
  2782. /* return -1 at failed, 0 at not found, 1 at found. */
  2783. static int
  2784. vector_read_cmd_find(struct vector_read_cmd *v, enum read_cmd dst)
  2785. {
  2786. size_t i;
  2787. if (v == NULL || dst == READ_FAIL)
  2788. return (-1);
  2789. for (i = 0; i < v->size; ++i)
  2790. if (v->r_container[i] == dst)
  2791. return (1);
  2792. return (0);
  2793. }
  2794. static int
  2795. vector_read_cmd_init(struct vector_read_cmd *v)
  2796. {
  2797. if (v == NULL)
  2798. return (0);
  2799. v->size = 0;
  2800. v->capacity = VECTOR_DEF_CAPACITY;
  2801. if ((v->r_container = malloc(sizeof(enum read_cmd) * v->capacity))
  2802. == NULL)
  2803. return (0);
  2804. return (1);
  2805. }
  2806. static int
  2807. vector_read_cmd_pop(struct vector_read_cmd *v)
  2808. {
  2809. if (v == NULL || v->size == 0)
  2810. return (0);
  2811. --v->size;
  2812. v->r_container[v->size] = READ_FAIL;
  2813. return (1);
  2814. }
  2815. static int
  2816. vector_read_cmd_push(struct vector_read_cmd *v, enum read_cmd cmd)
  2817. {
  2818. enum read_cmd *tmp_r_ctn;
  2819. size_t tmp_cap;
  2820. size_t i;
  2821. if (v == NULL)
  2822. return (0);
  2823. if (v->size == v->capacity) {
  2824. tmp_cap = v->capacity * BUFFER_GROWFACTOR;
  2825. if ((tmp_r_ctn = malloc(sizeof(enum read_cmd) * tmp_cap))
  2826. == NULL)
  2827. return (0);
  2828. for (i = 0; i < v->size; ++i)
  2829. tmp_r_ctn[i] = v->r_container[i];
  2830. free(v->r_container);
  2831. v->r_container = tmp_r_ctn;
  2832. v->capacity = tmp_cap;
  2833. }
  2834. v->r_container[v->size] = cmd;
  2835. ++v->size;
  2836. return (1);
  2837. }
  2838. static void
  2839. vector_type_qualifier_dest(struct vector_type_qualifier *v)
  2840. {
  2841. if (v == NULL)
  2842. return;
  2843. free(v->q_container);
  2844. vector_str_dest(&v->ext_name);
  2845. }
  2846. /* size, capacity, ext_name */
  2847. static int
  2848. vector_type_qualifier_init(struct vector_type_qualifier *v)
  2849. {
  2850. if (v == NULL)
  2851. return (0);
  2852. v->size = 0;
  2853. v->capacity = VECTOR_DEF_CAPACITY;
  2854. if ((v->q_container = malloc(sizeof(enum type_qualifier) * v->capacity))
  2855. == NULL)
  2856. return (0);
  2857. assert(v->q_container != NULL);
  2858. if (vector_str_init(&v->ext_name) == false) {
  2859. free(v->q_container);
  2860. return (0);
  2861. }
  2862. return (1);
  2863. }
  2864. static int
  2865. vector_type_qualifier_push(struct vector_type_qualifier *v,
  2866. enum type_qualifier t)
  2867. {
  2868. enum type_qualifier *tmp_ctn;
  2869. size_t tmp_cap;
  2870. size_t i;
  2871. if (v == NULL)
  2872. return (0);
  2873. if (v->size == v->capacity) {
  2874. tmp_cap = v->capacity * BUFFER_GROWFACTOR;
  2875. if ((tmp_ctn = malloc(sizeof(enum type_qualifier) * tmp_cap))
  2876. == NULL)
  2877. return (0);
  2878. for (i = 0; i < v->size; ++i)
  2879. tmp_ctn[i] = v->q_container[i];
  2880. free(v->q_container);
  2881. v->q_container = tmp_ctn;
  2882. v->capacity = tmp_cap;
  2883. }
  2884. v->q_container[v->size] = t;
  2885. ++v->size;
  2886. return (1);
  2887. }