123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474 |
- /*-
- * Copyright (c) 2007, 2008 Hyogeol Lee <hyogeollee@gmail.com>
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer
- * in this position and unchanged.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
- #include <sys/types.h>
- #include <assert.h>
- #include <ctype.h>
- #include <errno.h>
- #include <limits.h>
- #include <stdbool.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- /**
- * @file cpp_demangle.c
- * @brief Decode IA-64 C++ ABI style implementation.
- *
- * IA-64 standard ABI(Itanium C++ ABI) references.
- *
- * http://www.codesourcery.com/cxx-abi/abi.html#mangling \n
- * http://www.codesourcery.com/cxx-abi/abi-mangling.html
- */
- /** @brief Dynamic vector data for string. */
- struct vector_str {
- /** Current size */
- size_t size;
- /** Total capacity */
- size_t capacity;
- /** String array */
- char **container;
- };
- #define BUFFER_GROWFACTOR 1.618
- #define VECTOR_DEF_CAPACITY 8
- #define ELFTC_ISDIGIT(C) (isdigit((C) & 0xFF))
- enum type_qualifier {
- TYPE_PTR, TYPE_REF, TYPE_CMX, TYPE_IMG, TYPE_EXT, TYPE_RST, TYPE_VAT,
- TYPE_CST
- };
- struct vector_type_qualifier {
- size_t size, capacity;
- enum type_qualifier *q_container;
- struct vector_str ext_name;
- };
- enum read_cmd {
- READ_FAIL, READ_NEST, READ_TMPL, READ_EXPR, READ_EXPL, READ_LOCAL,
- READ_TYPE, READ_FUNC, READ_PTRMEM
- };
- struct vector_read_cmd {
- size_t size, capacity;
- enum read_cmd *r_container;
- };
- struct cpp_demangle_data {
- struct vector_str output; /* output string vector */
- struct vector_str output_tmp;
- struct vector_str subst; /* substitution string vector */
- struct vector_str tmpl;
- struct vector_str class_type;
- struct vector_read_cmd cmd;
- bool paren; /* parenthesis opened */
- bool pfirst; /* first element of parameter */
- bool mem_rst; /* restrict member function */
- bool mem_vat; /* volatile member function */
- bool mem_cst; /* const member function */
- int func_type;
- const char *cur; /* current mangled name ptr */
- const char *last_sname; /* last source name */
- int push_head;
- };
- #define CPP_DEMANGLE_TRY_LIMIT 128
- #define FLOAT_SPRINTF_TRY_LIMIT 5
- #define FLOAT_QUADRUPLE_BYTES 16
- #define FLOAT_EXTENED_BYTES 10
- #define SIMPLE_HASH(x,y) (64 * x + y)
- static size_t get_strlen_sum(const struct vector_str *v);
- static bool vector_str_grow(struct vector_str *v);
- static size_t
- get_strlen_sum(const struct vector_str *v)
- {
- size_t i, len = 0;
- if (v == NULL)
- return (0);
- assert(v->size > 0);
- for (i = 0; i < v->size; ++i)
- len += strlen(v->container[i]);
- return (len);
- }
- /**
- * @brief Deallocate resource in vector_str.
- */
- static void
- vector_str_dest(struct vector_str *v)
- {
- size_t i;
- if (v == NULL)
- return;
- for (i = 0; i < v->size; ++i)
- free(v->container[i]);
- free(v->container);
- }
- /**
- * @brief Find string in vector_str.
- * @param v Destination vector.
- * @param o String to find.
- * @param l Length of the string.
- * @return -1 at failed, 0 at not found, 1 at found.
- */
- static int
- vector_str_find(const struct vector_str *v, const char *o, size_t l)
- {
- size_t i;
- if (v == NULL || o == NULL)
- return (-1);
- for (i = 0; i < v->size; ++i)
- if (strncmp(v->container[i], o, l) == 0)
- return (1);
- return (0);
- }
- /**
- * @brief Get new allocated flat string from vector.
- *
- * If l is not NULL, return length of the string.
- * @param v Destination vector.
- * @param l Length of the string.
- * @return NULL at failed or NUL terminated new allocated string.
- */
- static char *
- vector_str_get_flat(const struct vector_str *v, size_t *l)
- {
- ssize_t elem_pos, elem_size, rtn_size;
- size_t i;
- char *rtn;
- if (v == NULL || v->size == 0)
- return (NULL);
- if ((rtn_size = get_strlen_sum(v)) == 0)
- return (NULL);
- if ((rtn = malloc(sizeof(char) * (rtn_size + 1))) == NULL)
- return (NULL);
- elem_pos = 0;
- for (i = 0; i < v->size; ++i) {
- elem_size = strlen(v->container[i]);
- memcpy(rtn + elem_pos, v->container[i], elem_size);
- elem_pos += elem_size;
- }
- rtn[rtn_size] = '\0';
- if (l != NULL)
- *l = rtn_size;
- return (rtn);
- }
- static bool
- vector_str_grow(struct vector_str *v)
- {
- size_t i, tmp_cap;
- char **tmp_ctn;
- if (v == NULL)
- return (false);
- assert(v->capacity > 0);
- tmp_cap = v->capacity * BUFFER_GROWFACTOR;
- assert(tmp_cap > v->capacity);
- if ((tmp_ctn = malloc(sizeof(char *) * tmp_cap)) == NULL)
- return (false);
- for (i = 0; i < v->size; ++i)
- tmp_ctn[i] = v->container[i];
- free(v->container);
- v->container = tmp_ctn;
- v->capacity = tmp_cap;
- return (true);
- }
- /**
- * @brief Initialize vector_str.
- * @return false at failed, true at success.
- */
- static bool
- vector_str_init(struct vector_str *v)
- {
- if (v == NULL)
- return (false);
- v->size = 0;
- v->capacity = VECTOR_DEF_CAPACITY;
- assert(v->capacity > 0);
- if ((v->container = malloc(sizeof(char *) * v->capacity)) == NULL)
- return (false);
- assert(v->container != NULL);
- return (true);
- }
- /**
- * @brief Remove last element in vector_str.
- * @return false at failed, true at success.
- */
- static bool
- vector_str_pop(struct vector_str *v)
- {
- if (v == NULL)
- return (false);
- if (v->size == 0)
- return (true);
- --v->size;
- free(v->container[v->size]);
- v->container[v->size] = NULL;
- return (true);
- }
- /**
- * @brief Push back string to vector.
- * @return false at failed, true at success.
- */
- static bool
- vector_str_push(struct vector_str *v, const char *str, size_t len)
- {
- if (v == NULL || str == NULL)
- return (false);
- if (v->size == v->capacity && vector_str_grow(v) == false)
- return (false);
- if ((v->container[v->size] = malloc(sizeof(char) * (len + 1))) == NULL)
- return (false);
- snprintf(v->container[v->size], len + 1, "%s", str);
- ++v->size;
- return (true);
- }
- /**
- * @brief Push front org vector to det vector.
- * @return false at failed, true at success.
- */
- static bool
- vector_str_push_vector_head(struct vector_str *dst, struct vector_str *org)
- {
- size_t i, j, tmp_cap;
- char **tmp_ctn;
- if (dst == NULL || org == NULL)
- return (false);
- tmp_cap = (dst->size + org->size) * BUFFER_GROWFACTOR;
- if ((tmp_ctn = malloc(sizeof(char *) * tmp_cap)) == NULL)
- return (false);
- for (i = 0; i < org->size; ++i)
- if ((tmp_ctn[i] = strdup(org->container[i])) == NULL) {
- for (j = 0; j < i; ++j)
- free(tmp_ctn[j]);
- free(tmp_ctn);
- return (false);
- }
- for (i = 0; i < dst->size; ++i)
- tmp_ctn[i + org->size] = dst->container[i];
- free(dst->container);
- dst->container = tmp_ctn;
- dst->capacity = tmp_cap;
- dst->size += org->size;
- return (true);
- }
- /**
- * @brief Get new allocated flat string from vector between begin and end.
- *
- * If r_len is not NULL, string length will be returned.
- * @return NULL at failed or NUL terminated new allocated string.
- */
- static char *
- vector_str_substr(const struct vector_str *v, size_t begin, size_t end,
- size_t *r_len)
- {
- size_t cur, i, len;
- char *rtn;
- if (v == NULL || begin > end)
- return (NULL);
- len = 0;
- for (i = begin; i < end + 1; ++i)
- len += strlen(v->container[i]);
- if ((rtn = malloc(sizeof(char) * (len + 1))) == NULL)
- return (NULL);
- if (r_len != NULL)
- *r_len = len;
- cur = 0;
- for (i = begin; i < end + 1; ++i) {
- len = strlen(v->container[i]);
- memcpy(rtn + cur, v->container[i], len);
- cur += len;
- }
- rtn[cur] = '\0';
- return (rtn);
- }
- static void cpp_demangle_data_dest(struct cpp_demangle_data *);
- static int cpp_demangle_data_init(struct cpp_demangle_data *,
- const char *);
- static int cpp_demangle_get_subst(struct cpp_demangle_data *, size_t);
- static int cpp_demangle_get_tmpl_param(struct cpp_demangle_data *, size_t);
- static int cpp_demangle_push_fp(struct cpp_demangle_data *,
- char *(*)(const char *, size_t));
- static int cpp_demangle_push_str(struct cpp_demangle_data *, const char *,
- size_t);
- static int cpp_demangle_push_subst(struct cpp_demangle_data *,
- const char *, size_t);
- static int cpp_demangle_push_subst_v(struct cpp_demangle_data *,
- struct vector_str *);
- static int cpp_demangle_push_type_qualifier(struct cpp_demangle_data *,
- struct vector_type_qualifier *, const char *);
- static int cpp_demangle_read_array(struct cpp_demangle_data *);
- static int cpp_demangle_read_encoding(struct cpp_demangle_data *);
- static int cpp_demangle_read_expr_primary(struct cpp_demangle_data *);
- static int cpp_demangle_read_expression(struct cpp_demangle_data *);
- static int cpp_demangle_read_expression_binary(struct cpp_demangle_data *,
- const char *, size_t);
- static int cpp_demangle_read_expression_unary(struct cpp_demangle_data *,
- const char *, size_t);
- static int cpp_demangle_read_expression_trinary(struct cpp_demangle_data *,
- const char *, size_t, const char *, size_t);
- static int cpp_demangle_read_function(struct cpp_demangle_data *, int *,
- struct vector_type_qualifier *);
- static int cpp_demangle_read_local_name(struct cpp_demangle_data *);
- static int cpp_demangle_read_name(struct cpp_demangle_data *);
- static int cpp_demangle_read_nested_name(struct cpp_demangle_data *);
- static int cpp_demangle_read_number(struct cpp_demangle_data *, long *);
- static int cpp_demangle_read_nv_offset(struct cpp_demangle_data *);
- static int cpp_demangle_read_offset(struct cpp_demangle_data *);
- static int cpp_demangle_read_offset_number(struct cpp_demangle_data *);
- static int cpp_demangle_read_pointer_to_member(struct cpp_demangle_data *);
- static int cpp_demangle_read_sname(struct cpp_demangle_data *);
- static int cpp_demangle_read_subst(struct cpp_demangle_data *);
- static int cpp_demangle_read_subst_std(struct cpp_demangle_data *);
- static int cpp_demangle_read_subst_stdtmpl(struct cpp_demangle_data *,
- const char *, size_t);
- static int cpp_demangle_read_tmpl_arg(struct cpp_demangle_data *);
- static int cpp_demangle_read_tmpl_args(struct cpp_demangle_data *);
- static int cpp_demangle_read_tmpl_param(struct cpp_demangle_data *);
- static int cpp_demangle_read_type(struct cpp_demangle_data *, int);
- static int cpp_demangle_read_uqname(struct cpp_demangle_data *);
- static int cpp_demangle_read_v_offset(struct cpp_demangle_data *);
- static char *decode_fp_to_double(const char *, size_t);
- static char *decode_fp_to_float(const char *, size_t);
- static char *decode_fp_to_float128(const char *, size_t);
- static char *decode_fp_to_float80(const char *, size_t);
- static char *decode_fp_to_long_double(const char *, size_t);
- static int hex_to_dec(char);
- static void vector_read_cmd_dest(struct vector_read_cmd *);
- static int vector_read_cmd_find(struct vector_read_cmd *, enum read_cmd);
- static int vector_read_cmd_init(struct vector_read_cmd *);
- static int vector_read_cmd_pop(struct vector_read_cmd *);
- static int vector_read_cmd_push(struct vector_read_cmd *, enum read_cmd);
- static void vector_type_qualifier_dest(struct vector_type_qualifier *);
- static int vector_type_qualifier_init(struct vector_type_qualifier *);
- static int vector_type_qualifier_push(struct vector_type_qualifier *,
- enum type_qualifier);
- /**
- * @brief Decode the input string by IA-64 C++ ABI style.
- *
- * GNU GCC v3 use IA-64 standard ABI.
- * @return New allocated demangled string or NULL if failed.
- * @todo 1. Testing and more test case. 2. Code cleaning.
- */
- char *
- __cxa_demangle_gnu3(const char *org)
- {
- struct cpp_demangle_data ddata;
- ssize_t org_len;
- unsigned int limit;
- char *rtn = NULL;
- if (org == NULL)
- return (NULL);
- // Try demangling as a type for short encodings
- if (((org_len = strlen(org)) < 2) || (org[0] != '_' || org[1] != 'Z' )) {
- if (!cpp_demangle_data_init(&ddata, org))
- return (NULL);
- if (!cpp_demangle_read_type(&ddata, 0))
- goto clean;
- rtn = vector_str_get_flat(&ddata.output, (size_t *) NULL);
- goto clean;
- }
- if (org_len > 11 && !strncmp(org, "_GLOBAL__I_", 11)) {
- if ((rtn = malloc(org_len + 19)) == NULL)
- return (NULL);
- snprintf(rtn, org_len + 19,
- "global constructors keyed to %s", org + 11);
- return (rtn);
- }
- if (!cpp_demangle_data_init(&ddata, org + 2))
- return (NULL);
- if (!cpp_demangle_read_encoding(&ddata))
- goto clean;
- limit = 0;
- while (*ddata.cur != '\0') {
- /*
- * Breaking at some gcc info at tail. e.g) @@GLIBCXX_3.4
- */
- if (*ddata.cur == '@' && *(ddata.cur + 1) == '@')
- break;
- if (!cpp_demangle_read_type(&ddata, 1))
- goto clean;
- if (limit++ > CPP_DEMANGLE_TRY_LIMIT)
- goto clean;
- }
- if (ddata.output.size == 0)
- goto clean;
- if (ddata.paren && !vector_str_push(&ddata.output, ")", 1))
- goto clean;
- if (ddata.mem_vat && !vector_str_push(&ddata.output, " volatile", 9))
- goto clean;
- if (ddata.mem_cst && !vector_str_push(&ddata.output, " const", 6))
- goto clean;
- if (ddata.mem_rst && !vector_str_push(&ddata.output, " restrict", 9))
- goto clean;
- rtn = vector_str_get_flat(&ddata.output, (size_t *) NULL);
- clean:
- cpp_demangle_data_dest(&ddata);
- return (rtn);
- }
- char *
- __cxa_demangle(const char*org) __attribute__((weak, alias("__cxa_demangle_gnu3")));
- static void
- cpp_demangle_data_dest(struct cpp_demangle_data *d)
- {
- if (d == NULL)
- return;
- vector_read_cmd_dest(&d->cmd);
- vector_str_dest(&d->class_type);
- vector_str_dest(&d->tmpl);
- vector_str_dest(&d->subst);
- vector_str_dest(&d->output_tmp);
- vector_str_dest(&d->output);
- }
- static int
- cpp_demangle_data_init(struct cpp_demangle_data *d, const char *cur)
- {
- if (d == NULL || cur == NULL)
- return (0);
- if (!vector_str_init(&d->output))
- return (0);
- if (!vector_str_init(&d->output_tmp))
- goto clean1;
- if (!vector_str_init(&d->subst))
- goto clean2;
- if (!vector_str_init(&d->tmpl))
- goto clean3;
- if (!vector_str_init(&d->class_type))
- goto clean4;
- if (!vector_read_cmd_init(&d->cmd))
- goto clean5;
- assert(d->output.container != NULL);
- assert(d->output_tmp.container != NULL);
- assert(d->subst.container != NULL);
- assert(d->tmpl.container != NULL);
- assert(d->class_type.container != NULL);
- d->paren = false;
- d->pfirst = false;
- d->mem_rst = false;
- d->mem_vat = false;
- d->mem_cst = false;
- d->func_type = 0;
- d->cur = cur;
- d->last_sname = NULL;
- d->push_head = 0;
- return (1);
- clean5:
- vector_str_dest(&d->class_type);
- clean4:
- vector_str_dest(&d->tmpl);
- clean3:
- vector_str_dest(&d->subst);
- clean2:
- vector_str_dest(&d->output_tmp);
- clean1:
- vector_str_dest(&d->output);
- return (0);
- }
- static int
- cpp_demangle_push_fp(struct cpp_demangle_data *ddata,
- char *(*decoder)(const char *, size_t))
- {
- size_t len;
- int rtn;
- const char *fp;
- char *f;
- if (ddata == NULL || decoder == NULL)
- return (0);
- fp = ddata->cur;
- while (*ddata->cur != 'E')
- ++ddata->cur;
- ++ddata->cur;
- if ((f = decoder(fp, ddata->cur - fp)) == NULL)
- return (0);
- rtn = 0;
- if ((len = strlen(f)) > 0 &&
- cpp_demangle_push_str(ddata, f, len))
- rtn = 1;
- free(f);
- return (rtn);
- }
- static int
- cpp_demangle_push_str(struct cpp_demangle_data *ddata, const char *str,
- size_t len)
- {
- if (ddata == NULL || str == NULL || len == 0)
- return (0);
- if (ddata->push_head > 0)
- return (vector_str_push(&ddata->output_tmp, str, len));
- return (vector_str_push(&ddata->output, str, len));
- }
- static int
- cpp_demangle_push_subst(struct cpp_demangle_data *ddata, const char *str,
- size_t len)
- {
- if (ddata == NULL || str == NULL || len == 0)
- return (0);
- if (!vector_str_find(&ddata->subst, str, len))
- return (vector_str_push(&ddata->subst, str, len));
- return (1);
- }
- static int
- cpp_demangle_push_subst_v(struct cpp_demangle_data *ddata, struct vector_str *v)
- {
- size_t str_len;
- int rtn;
- char *str;
- if (ddata == NULL || v == NULL)
- return (0);
- if ((str = vector_str_get_flat(v, &str_len)) == NULL)
- return (0);
- rtn = cpp_demangle_push_subst(ddata, str, str_len);
- free(str);
- return (rtn);
- }
- static int
- cpp_demangle_push_type_qualifier(struct cpp_demangle_data *ddata,
- struct vector_type_qualifier *v, const char *type_str)
- {
- struct vector_str subst_v;
- size_t idx, e_idx, e_len;
- int rtn;
- char *buf;
- if (ddata == NULL || v == NULL)
- return (0);
- if ((idx = v->size) == 0)
- return (1);
- rtn = 0;
- if (type_str != NULL) {
- if (!vector_str_init(&subst_v))
- return (0);
- if (!vector_str_push(&subst_v, type_str, strlen(type_str)))
- goto clean;
- }
- e_idx = 0;
- while (idx > 0) {
- switch (v->q_container[idx - 1]) {
- case TYPE_PTR:
- if (!cpp_demangle_push_str(ddata, "*", 1))
- goto clean;
- if (type_str != NULL) {
- if (!vector_str_push(&subst_v, "*", 1))
- goto clean;
- if (!cpp_demangle_push_subst_v(ddata, &subst_v))
- goto clean;
- }
- break;
- case TYPE_REF:
- if (!cpp_demangle_push_str(ddata, "&", 1))
- goto clean;
- if (type_str != NULL) {
- if (!vector_str_push(&subst_v, "&", 1))
- goto clean;
- if (!cpp_demangle_push_subst_v(ddata, &subst_v))
- goto clean;
- }
- break;
- case TYPE_CMX:
- if (!cpp_demangle_push_str(ddata, " complex", 8))
- goto clean;
- if (type_str != NULL) {
- if (!vector_str_push(&subst_v, " complex", 8))
- goto clean;
- if (!cpp_demangle_push_subst_v(ddata, &subst_v))
- goto clean;
- }
- break;
- case TYPE_IMG:
- if (!cpp_demangle_push_str(ddata, " imaginary", 10))
- goto clean;
- if (type_str != NULL) {
- if (!vector_str_push(&subst_v, " imaginary", 10))
- goto clean;
- if (!cpp_demangle_push_subst_v(ddata, &subst_v))
- goto clean;
- }
- break;
- case TYPE_EXT:
- if (e_idx > v->ext_name.size - 1)
- goto clean;
- if ((e_len = strlen(v->ext_name.container[e_idx])) == 0)
- goto clean;
- if ((buf = malloc(sizeof(char) * (e_len + 1))) == NULL)
- goto clean;
- memcpy(buf, " ", 1);
- memcpy(buf + 1, v->ext_name.container[e_idx], e_len);
- if (!cpp_demangle_push_str(ddata, buf, e_len + 1)) {
- free(buf);
- goto clean;
- }
- if (type_str != NULL) {
- if (!vector_str_push(&subst_v, buf,
- e_len + 1)) {
- free(buf);
- goto clean;
- }
- if (!cpp_demangle_push_subst_v(ddata, &subst_v)) {
- free(buf);
- goto clean;
- }
- }
- free(buf);
- ++e_idx;
- break;
- case TYPE_RST:
- if (!cpp_demangle_push_str(ddata, " restrict", 9))
- goto clean;
- if (type_str != NULL) {
- if (!vector_str_push(&subst_v, " restrict", 9))
- goto clean;
- if (!cpp_demangle_push_subst_v(ddata, &subst_v))
- goto clean;
- }
- break;
- case TYPE_VAT:
- if (!cpp_demangle_push_str(ddata, " volatile", 9))
- goto clean;
- if (type_str != NULL) {
- if (!vector_str_push(&subst_v, " volatile", 9))
- goto clean;
- if (!cpp_demangle_push_subst_v(ddata, &subst_v))
- goto clean;
- }
- break;
- case TYPE_CST:
- if (!cpp_demangle_push_str(ddata, " const", 6))
- goto clean;
- if (type_str != NULL) {
- if (!vector_str_push(&subst_v, " const", 6))
- goto clean;
- if (!cpp_demangle_push_subst_v(ddata, &subst_v))
- goto clean;
- }
- break;
- };
- --idx;
- }
- rtn = 1;
- clean:
- if (type_str != NULL)
- vector_str_dest(&subst_v);
- return (rtn);
- }
- static int
- cpp_demangle_get_subst(struct cpp_demangle_data *ddata, size_t idx)
- {
- size_t len;
- if (ddata == NULL || ddata->subst.size <= idx)
- return (0);
- if ((len = strlen(ddata->subst.container[idx])) == 0)
- return (0);
- if (!cpp_demangle_push_str(ddata, ddata->subst.container[idx], len))
- return (0);
- /* skip '_' */
- ++ddata->cur;
- return (1);
- }
- static int
- cpp_demangle_get_tmpl_param(struct cpp_demangle_data *ddata, size_t idx)
- {
- size_t len;
- if (ddata == NULL || ddata->tmpl.size <= idx)
- return (0);
- if ((len = strlen(ddata->tmpl.container[idx])) == 0)
- return (0);
- if (!cpp_demangle_push_str(ddata, ddata->tmpl.container[idx], len))
- return (0);
- ++ddata->cur;
- return (1);
- }
- static int
- cpp_demangle_read_array(struct cpp_demangle_data *ddata)
- {
- size_t i, num_len, exp_len, p_idx, idx;
- const char *num;
- char *exp;
- if (ddata == NULL || *(++ddata->cur) == '\0')
- return (0);
- if (*ddata->cur == '_') {
- if (*(++ddata->cur) == '\0')
- return (0);
- if (!cpp_demangle_read_type(ddata, 0))
- return (0);
- if (!cpp_demangle_push_str(ddata, "[]", 2))
- return (0);
- } else {
- if (ELFTC_ISDIGIT(*ddata->cur) != 0) {
- num = ddata->cur;
- while (ELFTC_ISDIGIT(*ddata->cur) != 0)
- ++ddata->cur;
- if (*ddata->cur != '_')
- return (0);
- num_len = ddata->cur - num;
- assert(num_len > 0);
- if (*(++ddata->cur) == '\0')
- return (0);
- if (!cpp_demangle_read_type(ddata, 0))
- return (0);
- if (!cpp_demangle_push_str(ddata, "[", 1))
- return (0);
- if (!cpp_demangle_push_str(ddata, num, num_len))
- return (0);
- if (!cpp_demangle_push_str(ddata, "]", 1))
- return (0);
- } else {
- p_idx = ddata->output.size;
- if (!cpp_demangle_read_expression(ddata))
- return (0);
- if ((exp = vector_str_substr(&ddata->output, p_idx,
- ddata->output.size - 1, &exp_len)) == NULL)
- return (0);
- idx = ddata->output.size;
- for (i = p_idx; i < idx; ++i)
- if (!vector_str_pop(&ddata->output)) {
- free(exp);
- return (0);
- }
- if (*ddata->cur != '_') {
- free(exp);
- return (0);
- }
- ++ddata->cur;
- if (*ddata->cur == '\0') {
- free(exp);
- return (0);
- }
- if (!cpp_demangle_read_type(ddata, 0)) {
- free(exp);
- return (0);
- }
- if (!cpp_demangle_push_str(ddata, "[", 1)) {
- free(exp);
- return (0);
- }
- if (!cpp_demangle_push_str(ddata, exp, exp_len)) {
- free(exp);
- return (0);
- }
- if (!cpp_demangle_push_str(ddata, "]", 1)) {
- free(exp);
- return (0);
- }
- free(exp);
- }
- }
- return (1);
- }
- static int
- cpp_demangle_read_expr_primary(struct cpp_demangle_data *ddata)
- {
- const char *num;
- if (ddata == NULL || *(++ddata->cur) == '\0')
- return (0);
- if (*ddata->cur == '_' && *(ddata->cur + 1) == 'Z') {
- ddata->cur += 2;
- if (*ddata->cur == '\0')
- return (0);
- if (!cpp_demangle_read_encoding(ddata))
- return (0);
- ++ddata->cur;
- return (1);
- }
- switch (*ddata->cur) {
- case 'b':
- switch (*(++ddata->cur)) {
- case '0':
- return (cpp_demangle_push_str(ddata, "false", 5));
- case '1':
- return (cpp_demangle_push_str(ddata, "true", 4));
- default:
- return (0);
- };
- case 'd':
- ++ddata->cur;
- return (cpp_demangle_push_fp(ddata, decode_fp_to_double));
- case 'e':
- ++ddata->cur;
- if (sizeof(long double) == 10)
- return (cpp_demangle_push_fp(ddata,
- decode_fp_to_double));
- return (cpp_demangle_push_fp(ddata, decode_fp_to_float80));
- case 'f':
- ++ddata->cur;
- return (cpp_demangle_push_fp(ddata, decode_fp_to_float));
- case 'g':
- ++ddata->cur;
- if (sizeof(long double) == 16)
- return (cpp_demangle_push_fp(ddata,
- decode_fp_to_double));
- return (cpp_demangle_push_fp(ddata, decode_fp_to_float128));
- case 'i':
- case 'j':
- case 'l':
- case 'm':
- case 'n':
- case 's':
- case 't':
- case 'x':
- case 'y':
- if (*(++ddata->cur) == 'n') {
- if (!cpp_demangle_push_str(ddata, "-", 1))
- return (0);
- ++ddata->cur;
- }
- num = ddata->cur;
- while (*ddata->cur != 'E') {
- if (!ELFTC_ISDIGIT(*ddata->cur))
- return (0);
- ++ddata->cur;
- }
- ++ddata->cur;
- return (cpp_demangle_push_str(ddata, num, ddata->cur - num));
- default:
- return (0);
- };
- }
- static int
- cpp_demangle_read_expression(struct cpp_demangle_data *ddata)
- {
- if (ddata == NULL || *ddata->cur == '\0')
- return (0);
- switch (SIMPLE_HASH(*ddata->cur, *(ddata->cur + 1))) {
- case SIMPLE_HASH('s', 't'):
- ddata->cur += 2;
- return (cpp_demangle_read_type(ddata, 0));
- case SIMPLE_HASH('s', 'r'):
- ddata->cur += 2;
- if (!cpp_demangle_read_type(ddata, 0))
- return (0);
- if (!cpp_demangle_read_uqname(ddata))
- return (0);
- if (*ddata->cur == 'I')
- return (cpp_demangle_read_tmpl_args(ddata));
- return (1);
- case SIMPLE_HASH('a', 'a'):
- /* operator && */
- ddata->cur += 2;
- return (cpp_demangle_read_expression_binary(ddata, "&&", 2));
- case SIMPLE_HASH('a', 'd'):
- /* operator & (unary) */
- ddata->cur += 2;
- return (cpp_demangle_read_expression_unary(ddata, "&", 1));
- case SIMPLE_HASH('a', 'n'):
- /* operator & */
- ddata->cur += 2;
- return (cpp_demangle_read_expression_binary(ddata, "&", 1));
- case SIMPLE_HASH('a', 'N'):
- /* operator &= */
- ddata->cur += 2;
- return (cpp_demangle_read_expression_binary(ddata, "&=", 2));
- case SIMPLE_HASH('a', 'S'):
- /* operator = */
- ddata->cur += 2;
- return (cpp_demangle_read_expression_binary(ddata, "=", 1));
- case SIMPLE_HASH('c', 'l'):
- /* operator () */
- ddata->cur += 2;
- return (cpp_demangle_read_expression_binary(ddata, "()", 2));
- case SIMPLE_HASH('c', 'm'):
- /* operator , */
- ddata->cur += 2;
- return (cpp_demangle_read_expression_binary(ddata, ",", 1));
- case SIMPLE_HASH('c', 'o'):
- /* operator ~ */
- ddata->cur += 2;
- return (cpp_demangle_read_expression_binary(ddata, "~", 1));
- case SIMPLE_HASH('c', 'v'):
- /* operator (cast) */
- ddata->cur += 2;
- return (cpp_demangle_read_expression_binary(ddata, "(cast)", 6));
- case SIMPLE_HASH('d', 'a'):
- /* operator delete [] */
- ddata->cur += 2;
- return (cpp_demangle_read_expression_unary(ddata, "delete []", 9));
- case SIMPLE_HASH('d', 'e'):
- /* operator * (unary) */
- ddata->cur += 2;
- return (cpp_demangle_read_expression_unary(ddata, "*", 1));
- case SIMPLE_HASH('d', 'l'):
- /* operator delete */
- ddata->cur += 2;
- return (cpp_demangle_read_expression_unary(ddata, "delete", 6));
- case SIMPLE_HASH('d', 'v'):
- /* operator / */
- ddata->cur += 2;
- return (cpp_demangle_read_expression_binary(ddata, "/", 1));
- case SIMPLE_HASH('d', 'V'):
- /* operator /= */
- ddata->cur += 2;
- return (cpp_demangle_read_expression_binary(ddata, "/=", 2));
- case SIMPLE_HASH('e', 'o'):
- /* operator ^ */
- ddata->cur += 2;
- return (cpp_demangle_read_expression_binary(ddata, "^", 1));
- case SIMPLE_HASH('e', 'O'):
- /* operator ^= */
- ddata->cur += 2;
- return (cpp_demangle_read_expression_binary(ddata, "^=", 2));
- case SIMPLE_HASH('e', 'q'):
- /* operator == */
- ddata->cur += 2;
- return (cpp_demangle_read_expression_binary(ddata, "==", 2));
- case SIMPLE_HASH('g', 'e'):
- /* operator >= */
- ddata->cur += 2;
- return (cpp_demangle_read_expression_binary(ddata, ">=", 2));
- case SIMPLE_HASH('g', 't'):
- /* operator > */
- ddata->cur += 2;
- return (cpp_demangle_read_expression_binary(ddata, ">", 1));
- case SIMPLE_HASH('i', 'x'):
- /* operator [] */
- ddata->cur += 2;
- return (cpp_demangle_read_expression_binary(ddata, "[]", 2));
- case SIMPLE_HASH('l', 'e'):
- /* operator <= */
- ddata->cur += 2;
- return (cpp_demangle_read_expression_binary(ddata, "<=", 2));
- case SIMPLE_HASH('l', 's'):
- /* operator << */
- ddata->cur += 2;
- return (cpp_demangle_read_expression_binary(ddata, "<<", 2));
- case SIMPLE_HASH('l', 'S'):
- /* operator <<= */
- ddata->cur += 2;
- return (cpp_demangle_read_expression_binary(ddata, "<<=", 3));
- case SIMPLE_HASH('l', 't'):
- /* operator < */
- ddata->cur += 2;
- return (cpp_demangle_read_expression_binary(ddata, "<", 1));
- case SIMPLE_HASH('m', 'i'):
- /* operator - */
- ddata->cur += 2;
- return (cpp_demangle_read_expression_binary(ddata, "-", 1));
- case SIMPLE_HASH('m', 'I'):
- /* operator -= */
- ddata->cur += 2;
- return (cpp_demangle_read_expression_binary(ddata, "-=", 2));
- case SIMPLE_HASH('m', 'l'):
- /* operator * */
- ddata->cur += 2;
- return (cpp_demangle_read_expression_binary(ddata, "*", 1));
- case SIMPLE_HASH('m', 'L'):
- /* operator *= */
- ddata->cur += 2;
- return (cpp_demangle_read_expression_binary(ddata, "*=", 2));
- case SIMPLE_HASH('m', 'm'):
- /* operator -- */
- ddata->cur += 2;
- return (cpp_demangle_read_expression_binary(ddata, "--", 2));
- case SIMPLE_HASH('n', 'a'):
- /* operator new[] */
- ddata->cur += 2;
- return (cpp_demangle_read_expression_unary(ddata, "new []", 6));
- case SIMPLE_HASH('n', 'e'):
- /* operator != */
- ddata->cur += 2;
- return (cpp_demangle_read_expression_binary(ddata, "!=", 2));
- case SIMPLE_HASH('n', 'g'):
- /* operator - (unary) */
- ddata->cur += 2;
- return (cpp_demangle_read_expression_unary(ddata, "-", 1));
- case SIMPLE_HASH('n', 't'):
- /* operator ! */
- ddata->cur += 2;
- return (cpp_demangle_read_expression_binary(ddata, "!", 1));
- case SIMPLE_HASH('n', 'w'):
- /* operator new */
- ddata->cur += 2;
- return (cpp_demangle_read_expression_unary(ddata, "new", 3));
- case SIMPLE_HASH('o', 'o'):
- /* operator || */
- ddata->cur += 2;
- return (cpp_demangle_read_expression_binary(ddata, "||", 2));
- case SIMPLE_HASH('o', 'r'):
- /* operator | */
- ddata->cur += 2;
- return (cpp_demangle_read_expression_binary(ddata, "|", 1));
- case SIMPLE_HASH('o', 'R'):
- /* operator |= */
- ddata->cur += 2;
- return (cpp_demangle_read_expression_binary(ddata, "|=", 2));
- case SIMPLE_HASH('p', 'l'):
- /* operator + */
- ddata->cur += 2;
- return (cpp_demangle_read_expression_binary(ddata, "+", 1));
- case SIMPLE_HASH('p', 'L'):
- /* operator += */
- ddata->cur += 2;
- return (cpp_demangle_read_expression_binary(ddata, "+=", 2));
- case SIMPLE_HASH('p', 'm'):
- /* operator ->* */
- ddata->cur += 2;
- return (cpp_demangle_read_expression_binary(ddata, "->*", 3));
- case SIMPLE_HASH('p', 'p'):
- /* operator ++ */
- ddata->cur += 2;
- return (cpp_demangle_read_expression_binary(ddata, "++", 2));
- case SIMPLE_HASH('p', 's'):
- /* operator + (unary) */
- ddata->cur += 2;
- return (cpp_demangle_read_expression_unary(ddata, "+", 1));
- case SIMPLE_HASH('p', 't'):
- /* operator -> */
- ddata->cur += 2;
- return (cpp_demangle_read_expression_binary(ddata, "->", 2));
- case SIMPLE_HASH('q', 'u'):
- /* operator ? */
- ddata->cur += 2;
- return (cpp_demangle_read_expression_trinary(ddata, "?", 1,
- ":", 1));
- case SIMPLE_HASH('r', 'm'):
- /* operator % */
- ddata->cur += 2;
- return (cpp_demangle_read_expression_binary(ddata, "%", 1));
- case SIMPLE_HASH('r', 'M'):
- /* operator %= */
- ddata->cur += 2;
- return (cpp_demangle_read_expression_binary(ddata, "%=", 2));
- case SIMPLE_HASH('r', 's'):
- /* operator >> */
- ddata->cur += 2;
- return (cpp_demangle_read_expression_binary(ddata, ">>", 2));
- case SIMPLE_HASH('r', 'S'):
- /* operator >>= */
- ddata->cur += 2;
- return (cpp_demangle_read_expression_binary(ddata, ">>=", 3));
- case SIMPLE_HASH('r', 'z'):
- /* operator sizeof */
- ddata->cur += 2;
- return (cpp_demangle_read_expression_unary(ddata, "sizeof", 6));
- case SIMPLE_HASH('s', 'v'):
- /* operator sizeof */
- ddata->cur += 2;
- return (cpp_demangle_read_expression_unary(ddata, "sizeof", 6));
- };
- switch (*ddata->cur) {
- case 'L':
- return (cpp_demangle_read_expr_primary(ddata));
- case 'T':
- return (cpp_demangle_read_tmpl_param(ddata));
- };
- return (0);
- }
- static int
- cpp_demangle_read_expression_binary(struct cpp_demangle_data *ddata,
- const char *name, size_t len)
- {
- if (ddata == NULL || name == NULL || len == 0)
- return (0);
- if (!cpp_demangle_read_expression(ddata))
- return (0);
- if (!cpp_demangle_push_str(ddata, name, len))
- return (0);
- return (cpp_demangle_read_expression(ddata));
- }
- static int
- cpp_demangle_read_expression_unary(struct cpp_demangle_data *ddata,
- const char *name, size_t len)
- {
- if (ddata == NULL || name == NULL || len == 0)
- return (0);
- if (!cpp_demangle_read_expression(ddata))
- return (0);
- return (cpp_demangle_push_str(ddata, name, len));
- }
- static int
- cpp_demangle_read_expression_trinary(struct cpp_demangle_data *ddata,
- const char *name1, size_t len1, const char *name2, size_t len2)
- {
- if (ddata == NULL || name1 == NULL || len1 == 0 || name2 == NULL ||
- len2 == 0)
- return (0);
- if (!cpp_demangle_read_expression(ddata))
- return (0);
- if (!cpp_demangle_push_str(ddata, name1, len1))
- return (0);
- if (!cpp_demangle_read_expression(ddata))
- return (0);
- if (!cpp_demangle_push_str(ddata, name2, len2))
- return (0);
- return (cpp_demangle_read_expression(ddata));
- }
- static int
- cpp_demangle_read_function(struct cpp_demangle_data *ddata, int *ext_c,
- struct vector_type_qualifier *v)
- {
- size_t class_type_size, class_type_len, limit;
- const char *class_type;
- if (ddata == NULL || *ddata->cur != 'F' || v == NULL)
- return (0);
- ++ddata->cur;
- if (*ddata->cur == 'Y') {
- if (ext_c != NULL)
- *ext_c = 1;
- ++ddata->cur;
- }
- if (!cpp_demangle_read_type(ddata, 0))
- return (0);
- if (*ddata->cur != 'E') {
- if (!cpp_demangle_push_str(ddata, "(", 1))
- return (0);
- if (vector_read_cmd_find(&ddata->cmd, READ_PTRMEM)) {
- if ((class_type_size = ddata->class_type.size) == 0)
- return (0);
- class_type =
- ddata->class_type.container[class_type_size - 1];
- if (class_type == NULL)
- return (0);
- if ((class_type_len = strlen(class_type)) == 0)
- return (0);
- if (!cpp_demangle_push_str(ddata, class_type,
- class_type_len))
- return (0);
- if (!cpp_demangle_push_str(ddata, "::*", 3))
- return (0);
- ++ddata->func_type;
- } else {
- if (!cpp_demangle_push_type_qualifier(ddata, v,
- (const char *) NULL))
- return (0);
- vector_type_qualifier_dest(v);
- if (!vector_type_qualifier_init(v))
- return (0);
- }
- if (!cpp_demangle_push_str(ddata, ")(", 2))
- return (0);
- limit = 0;
- for (;;) {
- if (!cpp_demangle_read_type(ddata, 0))
- return (0);
- if (*ddata->cur == 'E')
- break;
- if (limit++ > CPP_DEMANGLE_TRY_LIMIT)
- return (0);
- }
- if (vector_read_cmd_find(&ddata->cmd, READ_PTRMEM) == 1) {
- if (!cpp_demangle_push_type_qualifier(ddata, v,
- (const char *) NULL))
- return (0);
- vector_type_qualifier_dest(v);
- if (!vector_type_qualifier_init(v))
- return (0);
- }
- if (!cpp_demangle_push_str(ddata, ")", 1))
- return (0);
- }
- ++ddata->cur;
- return (1);
- }
- /* read encoding, encoding are function name, data name, special-name */
- static int
- cpp_demangle_read_encoding(struct cpp_demangle_data *ddata)
- {
- if (ddata == NULL || *ddata->cur == '\0')
- return (0);
- /* special name */
- switch (SIMPLE_HASH(*ddata->cur, *(ddata->cur + 1))) {
- case SIMPLE_HASH('G', 'V'):
- /* sentry object for 1 time init */
- if (!cpp_demangle_push_str(ddata, "guard variable for ", 20))
- return (0);
- ddata->cur += 2;
- break;
- case SIMPLE_HASH('T', 'c'):
- /* virtual function covariant override thunk */
- if (!cpp_demangle_push_str(ddata,
- "virtual function covariant override ", 36))
- return (0);
- ddata->cur += 2;
- if (*ddata->cur == '\0')
- return (0);
- if (!cpp_demangle_read_offset(ddata))
- return (0);
- if (!cpp_demangle_read_offset(ddata))
- return (0);
- return (cpp_demangle_read_encoding(ddata));
- case SIMPLE_HASH('T', 'D'):
- /* typeinfo common proxy */
- break;
- case SIMPLE_HASH('T', 'h'):
- /* virtual function non-virtual override thunk */
- if (cpp_demangle_push_str(ddata,
- "virtual function non-virtual override ", 38) == 0)
- return (0);
- ddata->cur += 2;
- if (*ddata->cur == '\0')
- return (0);
- if (!cpp_demangle_read_nv_offset(ddata))
- return (0);
- return (cpp_demangle_read_encoding(ddata));
- case SIMPLE_HASH('T', 'I'):
- /* typeinfo structure */
- /* FALLTHROUGH */
- case SIMPLE_HASH('T', 'S'):
- /* RTTI name (NTBS) */
- if (!cpp_demangle_push_str(ddata, "typeinfo for ", 14))
- return (0);
- ddata->cur += 2;
- if (*ddata->cur == '\0')
- return (0);
- return (cpp_demangle_read_type(ddata, 1));
- case SIMPLE_HASH('T', 'T'):
- /* VTT table */
- if (!cpp_demangle_push_str(ddata, "VTT for ", 8))
- return (0);
- ddata->cur += 2;
- return (cpp_demangle_read_type(ddata, 1));
- case SIMPLE_HASH('T', 'v'):
- /* virtual function virtual override thunk */
- if (!cpp_demangle_push_str(ddata,
- "virtual function virtual override ", 34))
- return (0);
- ddata->cur += 2;
- if (*ddata->cur == '\0')
- return (0);
- if (!cpp_demangle_read_v_offset(ddata))
- return (0);
- return (cpp_demangle_read_encoding(ddata));
- case SIMPLE_HASH('T', 'V'):
- /* virtual table */
- if (!cpp_demangle_push_str(ddata, "vtable for ", 12))
- return (0);
- ddata->cur += 2;
- if (*ddata->cur == '\0')
- return (0);
- return (cpp_demangle_read_type(ddata, 1));
- };
- return (cpp_demangle_read_name(ddata));
- }
- static int
- cpp_demangle_read_local_name(struct cpp_demangle_data *ddata)
- {
- size_t limit;
- if (ddata == NULL)
- return (0);
- if (*(++ddata->cur) == '\0')
- return (0);
- if (!cpp_demangle_read_encoding(ddata))
- return (0);
- limit = 0;
- for (;;) {
- if (!cpp_demangle_read_type(ddata, 1))
- return (0);
- if (*ddata->cur == 'E')
- break;
- if (limit++ > CPP_DEMANGLE_TRY_LIMIT)
- return (0);
- }
- if (*(++ddata->cur) == '\0')
- return (0);
- if (ddata->paren == true) {
- if (!cpp_demangle_push_str(ddata, ")", 1))
- return (0);
- ddata->paren = false;
- }
- if (*ddata->cur == 's')
- ++ddata->cur;
- else {
- if (!cpp_demangle_push_str(ddata, "::", 2))
- return (0);
- if (!cpp_demangle_read_name(ddata))
- return (0);
- }
- if (*ddata->cur == '_') {
- ++ddata->cur;
- while (ELFTC_ISDIGIT(*ddata->cur) != 0)
- ++ddata->cur;
- }
- return (1);
- }
- static int
- cpp_demangle_read_name(struct cpp_demangle_data *ddata)
- {
- struct vector_str *output, v;
- size_t p_idx, subst_str_len;
- int rtn;
- char *subst_str;
- if (ddata == NULL || *ddata->cur == '\0')
- return (0);
- output = ddata->push_head > 0 ? &ddata->output_tmp : &ddata->output;
- subst_str = NULL;
- switch (*ddata->cur) {
- case 'S':
- return (cpp_demangle_read_subst(ddata));
- case 'N':
- return (cpp_demangle_read_nested_name(ddata));
- case 'Z':
- return (cpp_demangle_read_local_name(ddata));
- };
- if (!vector_str_init(&v))
- return (0);
- p_idx = output->size;
- rtn = 0;
- if (!cpp_demangle_read_uqname(ddata))
- goto clean;
- if ((subst_str = vector_str_substr(output, p_idx, output->size - 1,
- &subst_str_len)) == NULL)
- goto clean;
- if (subst_str_len > 8 && strstr(subst_str, "operator") != NULL) {
- rtn = 1;
- goto clean;
- }
- if (!vector_str_push(&v, subst_str, subst_str_len))
- goto clean;
- if (!cpp_demangle_push_subst_v(ddata, &v))
- goto clean;
- if (*ddata->cur == 'I') {
- p_idx = output->size;
- if (!cpp_demangle_read_tmpl_args(ddata))
- goto clean;
- free(subst_str);
- if ((subst_str = vector_str_substr(output, p_idx,
- output->size - 1, &subst_str_len)) == NULL)
- goto clean;
- if (!vector_str_push(&v, subst_str, subst_str_len))
- goto clean;
- if (!cpp_demangle_push_subst_v(ddata, &v))
- goto clean;
- }
- rtn = 1;
- clean:
- if (subst_str) free(subst_str);
- vector_str_dest(&v);
- return (rtn);
- }
- static int
- cpp_demangle_read_nested_name(struct cpp_demangle_data *ddata)
- {
- struct vector_str *output, v;
- size_t limit, p_idx, subst_str_len;
- int rtn;
- char *subst_str;
- if (ddata == NULL || *ddata->cur != 'N')
- return (0);
- if (*(++ddata->cur) == '\0')
- return (0);
- while (*ddata->cur == 'r' || *ddata->cur == 'V' ||
- *ddata->cur == 'K') {
- switch (*ddata->cur) {
- case 'r':
- ddata->mem_rst = true;
- break;
- case 'V':
- ddata->mem_vat = true;
- break;
- case 'K':
- ddata->mem_cst = true;
- break;
- };
- ++ddata->cur;
- }
- output = ddata->push_head > 0 ? &ddata->output_tmp : &ddata->output;
- if (!vector_str_init(&v))
- return (0);
- rtn = 0;
- limit = 0;
- for (;;) {
- p_idx = output->size;
- switch (*ddata->cur) {
- case 'I':
- if (!cpp_demangle_read_tmpl_args(ddata))
- goto clean;
- break;
- case 'S':
- if (!cpp_demangle_read_subst(ddata))
- goto clean;
- break;
- case 'T':
- if (!cpp_demangle_read_tmpl_param(ddata))
- goto clean;
- break;
- default:
- if (!cpp_demangle_read_uqname(ddata))
- goto clean;
- };
- if ((subst_str = vector_str_substr(output, p_idx,
- output->size - 1, &subst_str_len)) == NULL)
- goto clean;
- if (!vector_str_push(&v, subst_str, subst_str_len)) {
- free(subst_str);
- goto clean;
- }
- free(subst_str);
- if (!cpp_demangle_push_subst_v(ddata, &v))
- goto clean;
- if (*ddata->cur == 'E')
- break;
- else if (*ddata->cur != 'I' &&
- *ddata->cur != 'C' && *ddata->cur != 'D') {
- if (!cpp_demangle_push_str(ddata, "::", 2))
- goto clean;
- if (!vector_str_push(&v, "::", 2))
- goto clean;
- }
- if (limit++ > CPP_DEMANGLE_TRY_LIMIT)
- goto clean;
- }
- ++ddata->cur;
- rtn = 1;
- clean:
- vector_str_dest(&v);
- return (rtn);
- }
- /*
- * read number
- * number ::= [n] <decimal>
- */
- static int
- cpp_demangle_read_number(struct cpp_demangle_data *ddata, long *rtn)
- {
- long len, negative_factor;
- if (ddata == NULL || rtn == NULL)
- return (0);
- negative_factor = 1;
- if (*ddata->cur == 'n') {
- negative_factor = -1;
- ++ddata->cur;
- }
- if (ELFTC_ISDIGIT(*ddata->cur) == 0)
- return (0);
- errno = 0;
- if ((len = strtol(ddata->cur, (char **) NULL, 10)) == 0 &&
- errno != 0)
- return (0);
- while (ELFTC_ISDIGIT(*ddata->cur) != 0)
- ++ddata->cur;
- assert(len >= 0);
- assert(negative_factor == 1 || negative_factor == -1);
- *rtn = len * negative_factor;
- return (1);
- }
- static int
- cpp_demangle_read_nv_offset(struct cpp_demangle_data *ddata)
- {
- if (ddata == NULL)
- return (0);
- if (!cpp_demangle_push_str(ddata, "offset : ", 9))
- return (0);
- return (cpp_demangle_read_offset_number(ddata));
- }
- /* read offset, offset are nv-offset, v-offset */
- static int
- cpp_demangle_read_offset(struct cpp_demangle_data *ddata)
- {
- if (ddata == NULL)
- return (0);
- if (*ddata->cur == 'h') {
- ++ddata->cur;
- return (cpp_demangle_read_nv_offset(ddata));
- } else if (*ddata->cur == 'v') {
- ++ddata->cur;
- return (cpp_demangle_read_v_offset(ddata));
- }
- return (0);
- }
- static int
- cpp_demangle_read_offset_number(struct cpp_demangle_data *ddata)
- {
- bool negative;
- const char *start;
- if (ddata == NULL || *ddata->cur == '\0')
- return (0);
- /* offset could be negative */
- if (*ddata->cur == 'n') {
- negative = true;
- start = ddata->cur + 1;
- } else {
- negative = false;
- start = ddata->cur;
- }
- while (*ddata->cur != '_')
- ++ddata->cur;
- if (negative && !cpp_demangle_push_str(ddata, "-", 1))
- return (0);
- assert(start != NULL);
- if (!cpp_demangle_push_str(ddata, start, ddata->cur - start))
- return (0);
- if (!cpp_demangle_push_str(ddata, " ", 1))
- return (0);
- ++ddata->cur;
- return (1);
- }
- static int
- cpp_demangle_read_pointer_to_member(struct cpp_demangle_data *ddata)
- {
- size_t class_type_len, i, idx, p_idx;
- int p_func_type, rtn;
- char *class_type;
- if (ddata == NULL || *ddata->cur != 'M' || *(++ddata->cur) == '\0')
- return (0);
- p_idx = ddata->output.size;
- if (!cpp_demangle_read_type(ddata, 0))
- return (0);
- if ((class_type = vector_str_substr(&ddata->output, p_idx,
- ddata->output.size - 1, &class_type_len)) == NULL)
- return (0);
- rtn = 0;
- idx = ddata->output.size;
- for (i = p_idx; i < idx; ++i)
- if (!vector_str_pop(&ddata->output))
- goto clean1;
- if (!vector_read_cmd_push(&ddata->cmd, READ_PTRMEM))
- goto clean1;
- if (!vector_str_push(&ddata->class_type, class_type, class_type_len))
- goto clean2;
- p_func_type = ddata->func_type;
- if (!cpp_demangle_read_type(ddata, 0))
- goto clean3;
- if (p_func_type == ddata->func_type) {
- if (!cpp_demangle_push_str(ddata, " ", 1))
- goto clean3;
- if (!cpp_demangle_push_str(ddata, class_type, class_type_len))
- goto clean3;
- if (!cpp_demangle_push_str(ddata, "::*", 3))
- goto clean3;
- }
- rtn = 1;
- clean3:
- if (!vector_str_pop(&ddata->class_type))
- rtn = 0;
- clean2:
- if (!vector_read_cmd_pop(&ddata->cmd))
- rtn = 0;
- clean1:
- free(class_type);
- return (rtn);
- }
- /* read source-name, source-name is <len> <ID> */
- static int
- cpp_demangle_read_sname(struct cpp_demangle_data *ddata)
- {
- long len;
- if (ddata == NULL || cpp_demangle_read_number(ddata, &len) == 0 ||
- len <= 0 || cpp_demangle_push_str(ddata, ddata->cur, len) == 0)
- return (0);
- assert(ddata->output.size > 0);
- if (vector_read_cmd_find(&ddata->cmd, READ_TMPL) == 0)
- ddata->last_sname =
- ddata->output.container[ddata->output.size - 1];
- ddata->cur += len;
- return (1);
- }
- static int
- cpp_demangle_read_subst(struct cpp_demangle_data *ddata)
- {
- long nth;
- if (ddata == NULL || *ddata->cur == '\0')
- return (0);
- /* abbreviations of the form Sx */
- switch (SIMPLE_HASH(*ddata->cur, *(ddata->cur + 1))) {
- case SIMPLE_HASH('S', 'a'):
- /* std::allocator */
- if (cpp_demangle_push_str(ddata, "std::allocator", 14) == 0)
- return (0);
- ddata->cur += 2;
- if (*ddata->cur == 'I')
- return (cpp_demangle_read_subst_stdtmpl(ddata,
- "std::allocator", 14));
- return (1);
- case SIMPLE_HASH('S', 'b'):
- /* std::basic_string */
- if (!cpp_demangle_push_str(ddata, "std::basic_string", 17))
- return (0);
- ddata->cur += 2;
- if (*ddata->cur == 'I')
- return (cpp_demangle_read_subst_stdtmpl(ddata,
- "std::basic_string", 17));
- return (1);
- case SIMPLE_HASH('S', 'd'):
- /* std::basic_iostream<char, std::char_traits<char> > */
- if (!cpp_demangle_push_str(ddata, "std::iostream", 19))
- return (0);
- ddata->last_sname = "iostream";
- ddata->cur += 2;
- if (*ddata->cur == 'I')
- return (cpp_demangle_read_subst_stdtmpl(ddata,
- "std::iostream", 19));
- return (1);
- case SIMPLE_HASH('S', 'i'):
- /* std::basic_istream<char, std::char_traits<char> > */
- if (!cpp_demangle_push_str(ddata, "std::istream", 18))
- return (0);
- ddata->last_sname = "istream";
- ddata->cur += 2;
- if (*ddata->cur == 'I')
- return (cpp_demangle_read_subst_stdtmpl(ddata,
- "std::istream", 18));
- return (1);
- case SIMPLE_HASH('S', 'o'):
- /* std::basic_ostream<char, std::char_traits<char> > */
- if (!cpp_demangle_push_str(ddata, "std::ostream", 18))
- return (0);
- ddata->last_sname = "istream";
- ddata->cur += 2;
- if (*ddata->cur == 'I')
- return (cpp_demangle_read_subst_stdtmpl(ddata,
- "std::ostream", 18));
- return (1);
- case SIMPLE_HASH('S', 's'):
- /*
- * std::basic_string<char, std::char_traits<char>,
- * std::allocator<char> >
- *
- * a.k.a std::string
- */
- if (!cpp_demangle_push_str(ddata, "std::string", 11))
- return (0);
- ddata->last_sname = "string";
- ddata->cur += 2;
- if (*ddata->cur == 'I')
- return (cpp_demangle_read_subst_stdtmpl(ddata,
- "std::string", 11));
- return (1);
- case SIMPLE_HASH('S', 't'):
- /* std:: */
- return (cpp_demangle_read_subst_std(ddata));
- };
- if (*(++ddata->cur) == '\0')
- return (0);
- /* substitution */
- if (*ddata->cur == '_')
- return (cpp_demangle_get_subst(ddata, 0));
- else {
- errno = 0;
- /* substitution number is base 36 */
- if ((nth = strtol(ddata->cur, (char **) NULL, 36)) == 0 &&
- errno != 0)
- return (0);
- /* first was '_', so increase one */
- ++nth;
- while (*ddata->cur != '_')
- ++ddata->cur;
- assert(nth > 0);
- return (cpp_demangle_get_subst(ddata, nth));
- }
- /* NOTREACHED */
- return (0);
- }
- static int
- cpp_demangle_read_subst_std(struct cpp_demangle_data *ddata)
- {
- struct vector_str *output, v;
- size_t p_idx, subst_str_len;
- int rtn;
- char *subst_str;
- if (ddata == NULL)
- return (0);
- if (!vector_str_init(&v))
- return (0);
- subst_str = NULL;
- rtn = 0;
- if (!cpp_demangle_push_str(ddata, "std::", 5))
- goto clean;
- if (!vector_str_push(&v, "std::", 5))
- goto clean;
- ddata->cur += 2;
- output = ddata->push_head > 0 ? &ddata->output_tmp : &ddata->output;
- p_idx = output->size;
- if (!cpp_demangle_read_uqname(ddata))
- goto clean;
- if ((subst_str = vector_str_substr(output, p_idx, output->size - 1,
- &subst_str_len)) == NULL)
- goto clean;
- if (!vector_str_push(&v, subst_str, subst_str_len))
- goto clean;
- if (!cpp_demangle_push_subst_v(ddata, &v))
- goto clean;
- if (*ddata->cur == 'I') {
- p_idx = output->size;
- if (!cpp_demangle_read_tmpl_args(ddata))
- goto clean;
- free(subst_str);
- if ((subst_str = vector_str_substr(output, p_idx,
- output->size - 1, &subst_str_len)) == NULL)
- goto clean;
- if (!vector_str_push(&v, subst_str, subst_str_len))
- goto clean;
- if (!cpp_demangle_push_subst_v(ddata, &v))
- goto clean;
- }
- rtn = 1;
- clean:
- if (subst_str) free(subst_str);
- vector_str_dest(&v);
- return (rtn);
- }
- static int
- cpp_demangle_read_subst_stdtmpl(struct cpp_demangle_data *ddata,
- const char *str, size_t len)
- {
- struct vector_str *output;
- size_t p_idx, substr_len;
- int rtn;
- char *subst_str, *substr;
- if (ddata == NULL || str == NULL || len == 0)
- return (0);
- output = ddata->push_head > 0 ? &ddata->output_tmp : &ddata->output;
- p_idx = output->size;
- substr = NULL;
- subst_str = NULL;
- if (!cpp_demangle_read_tmpl_args(ddata))
- return (0);
- if ((substr = vector_str_substr(output, p_idx, output->size - 1,
- &substr_len)) == NULL)
- return (0);
- rtn = 0;
- if ((subst_str = malloc(sizeof(char) * (substr_len + len + 1))) ==
- NULL)
- goto clean;
- memcpy(subst_str, str, len);
- memcpy(subst_str + len, substr, substr_len);
- subst_str[substr_len + len] = '\0';
- if (!cpp_demangle_push_subst(ddata, subst_str, substr_len + len))
- goto clean;
- rtn = 1;
- clean:
- if (subst_str) free(subst_str);
- free(substr);
- return (rtn);
- }
- static int
- cpp_demangle_read_tmpl_arg(struct cpp_demangle_data *ddata)
- {
- if (ddata == NULL || *ddata->cur == '\0')
- return (0);
- switch (*ddata->cur) {
- case 'L':
- return (cpp_demangle_read_expr_primary(ddata));
- case 'X':
- return (cpp_demangle_read_expression(ddata));
- };
- return (cpp_demangle_read_type(ddata, 0));
- }
- static int
- cpp_demangle_read_tmpl_args(struct cpp_demangle_data *ddata)
- {
- struct vector_str *v;
- size_t arg_len, idx, limit, size;
- char *arg;
- if (ddata == NULL || *ddata->cur == '\0')
- return (0);
- ++ddata->cur;
- if (!vector_read_cmd_push(&ddata->cmd, READ_TMPL))
- return (0);
- if (!cpp_demangle_push_str(ddata, "<", 1))
- return (0);
- limit = 0;
- v = ddata->push_head > 0 ? &ddata->output_tmp : &ddata->output;
- for (;;) {
- idx = v->size;
- if (!cpp_demangle_read_tmpl_arg(ddata))
- return (0);
- if ((arg = vector_str_substr(v, idx, v->size - 1, &arg_len)) ==
- NULL)
- return (0);
- if (!vector_str_find(&ddata->tmpl, arg, arg_len) &&
- !vector_str_push(&ddata->tmpl, arg, arg_len)) {
- free(arg);
- return (0);
- }
- free(arg);
- if (*ddata->cur == 'E') {
- ++ddata->cur;
- size = v->size;
- assert(size > 0);
- if (!strncmp(v->container[size - 1], ">", 1)) {
- if (!cpp_demangle_push_str(ddata, " >", 2))
- return (0);
- } else if (!cpp_demangle_push_str(ddata, ">", 1))
- return (0);
- break;
- } else if (*ddata->cur != 'I' &&
- !cpp_demangle_push_str(ddata, ", ", 2))
- return (0);
- if (limit++ > CPP_DEMANGLE_TRY_LIMIT)
- return (0);
- }
- return (vector_read_cmd_pop(&ddata->cmd));
- }
- /*
- * Read template parameter that forms in 'T[number]_'.
- * This function much like to read_subst but only for types.
- */
- static int
- cpp_demangle_read_tmpl_param(struct cpp_demangle_data *ddata)
- {
- long nth;
- if (ddata == NULL || *ddata->cur != 'T')
- return (0);
- ++ddata->cur;
- if (*ddata->cur == '_')
- return (cpp_demangle_get_tmpl_param(ddata, 0));
- else {
- errno = 0;
- if ((nth = strtol(ddata->cur, (char **) NULL, 36)) == 0 &&
- errno != 0)
- return (0);
- /* T_ is first */
- ++nth;
- while (*ddata->cur != '_')
- ++ddata->cur;
- assert(nth > 0);
- return (cpp_demangle_get_tmpl_param(ddata, nth));
- }
- /* NOTREACHED */
- return (0);
- }
- static int
- cpp_demangle_read_type(struct cpp_demangle_data *ddata, int delimit)
- {
- struct vector_type_qualifier v;
- struct vector_str *output;
- size_t p_idx, type_str_len;
- int extern_c, is_builtin;
- long len;
- char *type_str;
- if (ddata == NULL)
- return (0);
- output = &ddata->output;
- if (ddata->output.size > 0 && !strncmp(ddata->output.container[ddata->output.size - 1], ">", 1)) {
- ddata->push_head++;
- output = &ddata->output_tmp;
- } else if (delimit == 1) {
- if (ddata->paren == false) {
- if (!cpp_demangle_push_str(ddata, "(", 1))
- return (0);
- if (ddata->output.size < 2)
- return (0);
- ddata->paren = true;
- ddata->pfirst = true;
- /* Need pop function name */
- if (ddata->subst.size == 1 &&
- !vector_str_pop(&ddata->subst))
- return (0);
- }
- if (ddata->pfirst)
- ddata->pfirst = false;
- else if (*ddata->cur != 'I' &&
- !cpp_demangle_push_str(ddata, ", ", 2))
- return (0);
- }
- assert(output != NULL);
- /*
- * [r, V, K] [P, R, C, G, U] builtin, function, class-enum, array
- * pointer-to-member, template-param, template-template-param, subst
- */
- if (!vector_type_qualifier_init(&v))
- return (0);
- extern_c = 0;
- is_builtin = 1;
- p_idx = output->size;
- type_str = NULL;
- again:
- /* builtin type */
- switch (*ddata->cur) {
- case 'a':
- /* signed char */
- if (!cpp_demangle_push_str(ddata, "signed char", 11))
- goto clean;
- ++ddata->cur;
- goto rtn;
- case 'A':
- /* array type */
- if (!cpp_demangle_read_array(ddata))
- goto clean;
- is_builtin = 0;
- goto rtn;
- case 'b':
- /* bool */
- if (!cpp_demangle_push_str(ddata, "bool", 4))
- goto clean;
- ++ddata->cur;
- goto rtn;
- case 'C':
- /* complex pair */
- if (!vector_type_qualifier_push(&v, TYPE_CMX))
- goto clean;
- ++ddata->cur;
- goto again;
- case 'c':
- /* char */
- if (!cpp_demangle_push_str(ddata, "char", 4))
- goto clean;
- ++ddata->cur;
- goto rtn;
- case 'd':
- /* double */
- if (!cpp_demangle_push_str(ddata, "double", 6))
- goto clean;
- ++ddata->cur;
- goto rtn;
- case 'e':
- /* long double */
- if (!cpp_demangle_push_str(ddata, "long double", 11))
- goto clean;
- ++ddata->cur;
- goto rtn;
- case 'f':
- /* float */
- if (!cpp_demangle_push_str(ddata, "float", 5))
- goto clean;
- ++ddata->cur;
- goto rtn;
- case 'F':
- /* function */
- if (!cpp_demangle_read_function(ddata, &extern_c, &v))
- goto clean;
- is_builtin = 0;
- goto rtn;
- case 'g':
- /* __float128 */
- if (!cpp_demangle_push_str(ddata, "__float128", 10))
- goto clean;
- ++ddata->cur;
- goto rtn;
- case 'G':
- /* imaginary */
- if (!vector_type_qualifier_push(&v, TYPE_IMG))
- goto clean;
- ++ddata->cur;
- goto again;
- case 'h':
- /* unsigned char */
- if (!cpp_demangle_push_str(ddata, "unsigned char", 13))
- goto clean;
- ++ddata->cur;
- goto rtn;
- case 'i':
- /* int */
- if (!cpp_demangle_push_str(ddata, "int", 3))
- goto clean;
- ++ddata->cur;
- goto rtn;
- case 'j':
- /* unsigned int */
- if (!cpp_demangle_push_str(ddata, "unsigned int", 12))
- goto clean;
- ++ddata->cur;
- goto rtn;
- case 'K':
- /* const */
- if (!vector_type_qualifier_push(&v, TYPE_CST))
- goto clean;
- ++ddata->cur;
- goto again;
- case 'l':
- /* long */
- if (!cpp_demangle_push_str(ddata, "long", 4))
- goto clean;
- ++ddata->cur;
- goto rtn;
- case 'm':
- /* unsigned long */
- if (!cpp_demangle_push_str(ddata, "unsigned long", 13))
- goto clean;
- ++ddata->cur;
- goto rtn;
- case 'M':
- /* pointer to member */
- if (!cpp_demangle_read_pointer_to_member(ddata))
- goto clean;
- is_builtin = 0;
- goto rtn;
- case 'n':
- /* __int128 */
- if (!cpp_demangle_push_str(ddata, "__int128", 8))
- goto clean;
- ++ddata->cur;
- goto rtn;
- case 'o':
- /* unsigned __int128 */
- if (!cpp_demangle_push_str(ddata, "unsigned _;int128", 17))
- goto clean;
- ++ddata->cur;
- goto rtn;
- case 'P':
- /* pointer */
- if (!vector_type_qualifier_push(&v, TYPE_PTR))
- goto clean;
- ++ddata->cur;
- goto again;
- case 'r':
- /* restrict */
- if (!vector_type_qualifier_push(&v, TYPE_RST))
- goto clean;
- ++ddata->cur;
- goto again;
- case 'R':
- /* reference */
- if (!vector_type_qualifier_push(&v, TYPE_REF))
- goto clean;
- ++ddata->cur;
- goto again;
- case 's':
- /* short, local string */
- if (!cpp_demangle_push_str(ddata, "short", 5))
- goto clean;
- ++ddata->cur;
- goto rtn;
- case 'S':
- /* substitution */
- if (!cpp_demangle_read_subst(ddata))
- goto clean;
- is_builtin = 0;
- goto rtn;
- case 't':
- /* unsigned short */
- if (!cpp_demangle_push_str(ddata, "unsigned short", 14))
- goto clean;
- ++ddata->cur;
- goto rtn;
- case 'T':
- /* template parameter */
- if (!cpp_demangle_read_tmpl_param(ddata))
- goto clean;
- is_builtin = 0;
- goto rtn;
- case 'u':
- /* vendor extended builtin */
- ++ddata->cur;
- if (!cpp_demangle_read_sname(ddata))
- goto clean;
- is_builtin = 0;
- goto rtn;
- case 'U':
- /* vendor extended type qualifier */
- if (!cpp_demangle_read_number(ddata, &len))
- goto clean;
- if (len <= 0)
- goto clean;
- if (!vector_str_push(&v.ext_name, ddata->cur, len))
- goto clean;
- ddata->cur += len;
- goto again;
- case 'v':
- /* void */
- if (!cpp_demangle_push_str(ddata, "void", 4))
- goto clean;
- ++ddata->cur;
- goto rtn;
- case 'V':
- /* volatile */
- if (!vector_type_qualifier_push(&v, TYPE_VAT))
- goto clean;
- ++ddata->cur;
- goto again;
- case 'w':
- /* wchar_t */
- if (!cpp_demangle_push_str(ddata, "wchar_t", 6))
- goto clean;
- ++ddata->cur;
- goto rtn;
- case 'x':
- /* long long */
- if (!cpp_demangle_push_str(ddata, "long long", 9))
- goto clean;
- ++ddata->cur;
- goto rtn;
- case 'y':
- /* unsigned long long */
- if (!cpp_demangle_push_str(ddata, "unsigned long long", 18))
- goto clean;
- ++ddata->cur;
- goto rtn;
- case 'z':
- /* ellipsis */
- if (!cpp_demangle_push_str(ddata, "ellipsis", 8))
- goto clean;
- ++ddata->cur;
- goto rtn;
- };
- if (!cpp_demangle_read_name(ddata))
- goto clean;
- is_builtin = 0;
- rtn:
- if ((type_str = vector_str_substr(output, p_idx, output->size - 1,
- &type_str_len)) == NULL)
- goto clean;
- if (is_builtin == 0) {
- if (!vector_str_find(&ddata->subst, type_str, type_str_len) &&
- !vector_str_push(&ddata->subst, type_str, type_str_len))
- goto clean;
- }
- if (!cpp_demangle_push_type_qualifier(ddata, &v, type_str))
- goto clean;
- free(type_str);
- vector_type_qualifier_dest(&v);
- if (ddata->push_head > 0) {
- if (*ddata->cur == 'I' && cpp_demangle_read_tmpl_args(ddata)
- == 0)
- return (0);
- if (--ddata->push_head > 0)
- return (1);
- if (!vector_str_push(&ddata->output_tmp, " ", 1))
- return (0);
- if (!vector_str_push_vector_head(&ddata->output,
- &ddata->output_tmp))
- return (0);
- vector_str_dest(&ddata->output_tmp);
- if (!vector_str_init(&ddata->output_tmp))
- return (0);
- if (!cpp_demangle_push_str(ddata, "(", 1))
- return (0);
- ddata->paren = true;
- ddata->pfirst = true;
- }
- return (1);
- clean:
- if (type_str) free(type_str);
- vector_type_qualifier_dest(&v);
- return (0);
- }
- /*
- * read unqualified-name, unqualified name are operator-name, ctor-dtor-name,
- * source-name
- */
- static int
- cpp_demangle_read_uqname(struct cpp_demangle_data *ddata)
- {
- size_t len;
- if (ddata == NULL || *ddata->cur == '\0')
- return (0);
- /* operator name */
- switch (SIMPLE_HASH(*ddata->cur, *(ddata->cur + 1))) {
- case SIMPLE_HASH('a', 'a'):
- /* operator && */
- if (!cpp_demangle_push_str(ddata, "operator&&", 10))
- return (0);
- ddata->cur += 2;
- return (1);
- case SIMPLE_HASH('a', 'd'):
- /* operator & (unary) */
- if (!cpp_demangle_push_str(ddata, "operator&", 9))
- return (0);
- ddata->cur += 2;
- return (1);
- case SIMPLE_HASH('a', 'n'):
- /* operator & */
- if (!cpp_demangle_push_str(ddata, "operator&", 9))
- return (0);
- ddata->cur += 2;
- return (1);
- case SIMPLE_HASH('a', 'N'):
- /* operator &= */
- if (!cpp_demangle_push_str(ddata, "operator&=", 10))
- return (0);
- ddata->cur += 2;
- return (1);
- case SIMPLE_HASH('a', 'S'):
- /* operator = */
- if (!cpp_demangle_push_str(ddata, "operator=", 9))
- return (0);
- ddata->cur += 2;
- return (1);
- case SIMPLE_HASH('c', 'l'):
- /* operator () */
- if (!cpp_demangle_push_str(ddata, "operator()", 10))
- return (0);
- ddata->cur += 2;
- return (1);
- case SIMPLE_HASH('c', 'm'):
- /* operator , */
- if (!cpp_demangle_push_str(ddata, "operator,", 9))
- return (0);
- ddata->cur += 2;
- return (1);
- case SIMPLE_HASH('c', 'o'):
- /* operator ~ */
- if (!cpp_demangle_push_str(ddata, "operator~", 9))
- return (0);
- ddata->cur += 2;
- return (1);
- case SIMPLE_HASH('c', 'v'):
- /* operator (cast) */
- if (!cpp_demangle_push_str(ddata, "operator(cast)", 14))
- return (0);
- ddata->cur += 2;
- return (cpp_demangle_read_type(ddata, 1));
- case SIMPLE_HASH('d', 'a'):
- /* operator delete [] */
- if (!cpp_demangle_push_str(ddata, "operator delete []", 18))
- return (0);
- ddata->cur += 2;
- return (1);
- case SIMPLE_HASH('d', 'e'):
- /* operator * (unary) */
- if (!cpp_demangle_push_str(ddata, "operator*", 9))
- return (0);
- ddata->cur += 2;
- return (1);
- case SIMPLE_HASH('d', 'l'):
- /* operator delete */
- if (!cpp_demangle_push_str(ddata, "operator delete", 15))
- return (0);
- ddata->cur += 2;
- return (1);
- case SIMPLE_HASH('d', 'v'):
- /* operator / */
- if (!cpp_demangle_push_str(ddata, "operator/", 9))
- return (0);
- ddata->cur += 2;
- return (1);
- case SIMPLE_HASH('d', 'V'):
- /* operator /= */
- if (!cpp_demangle_push_str(ddata, "operator/=", 10))
- return (0);
- ddata->cur += 2;
- return (1);
- case SIMPLE_HASH('e', 'o'):
- /* operator ^ */
- if (!cpp_demangle_push_str(ddata, "operator^", 9))
- return (0);
- ddata->cur += 2;
- return (1);
- case SIMPLE_HASH('e', 'O'):
- /* operator ^= */
- if (!cpp_demangle_push_str(ddata, "operator^=", 10))
- return (0);
- ddata->cur += 2;
- return (1);
- case SIMPLE_HASH('e', 'q'):
- /* operator == */
- if (!cpp_demangle_push_str(ddata, "operator==", 10))
- return (0);
- ddata->cur += 2;
- return (1);
- case SIMPLE_HASH('g', 'e'):
- /* operator >= */
- if (!cpp_demangle_push_str(ddata, "operator>=", 10))
- return (0);
- ddata->cur += 2;
- return (1);
- case SIMPLE_HASH('g', 't'):
- /* operator > */
- if (!cpp_demangle_push_str(ddata, "operator>", 9))
- return (0);
- ddata->cur += 2;
- return (1);
- case SIMPLE_HASH('i', 'x'):
- /* operator [] */
- if (!cpp_demangle_push_str(ddata, "operator[]", 10))
- return (0);
- ddata->cur += 2;
- return (1);
- case SIMPLE_HASH('l', 'e'):
- /* operator <= */
- if (!cpp_demangle_push_str(ddata, "operator<=", 10))
- return (0);
- ddata->cur += 2;
- return (1);
- case SIMPLE_HASH('l', 's'):
- /* operator << */
- if (!cpp_demangle_push_str(ddata, "operator<<", 10))
- return (0);
- ddata->cur += 2;
- return (1);
- case SIMPLE_HASH('l', 'S'):
- /* operator <<= */
- if (!cpp_demangle_push_str(ddata, "operator<<=", 11))
- return (0);
- ddata->cur += 2;
- return (1);
- case SIMPLE_HASH('l', 't'):
- /* operator < */
- if (!cpp_demangle_push_str(ddata, "operator<", 9))
- return (0);
- ddata->cur += 2;
- return (1);
- case SIMPLE_HASH('m', 'i'):
- /* operator - */
- if (!cpp_demangle_push_str(ddata, "operator-", 9))
- return (0);
- ddata->cur += 2;
- return (1);
- case SIMPLE_HASH('m', 'I'):
- /* operator -= */
- if (!cpp_demangle_push_str(ddata, "operator-=", 10))
- return (0);
- ddata->cur += 2;
- return (1);
- case SIMPLE_HASH('m', 'l'):
- /* operator * */
- if (!cpp_demangle_push_str(ddata, "operator*", 9))
- return (0);
- ddata->cur += 2;
- return (1);
- case SIMPLE_HASH('m', 'L'):
- /* operator *= */
- if (!cpp_demangle_push_str(ddata, "operator*=", 10))
- return (0);
- ddata->cur += 2;
- return (1);
- case SIMPLE_HASH('m', 'm'):
- /* operator -- */
- if (!cpp_demangle_push_str(ddata, "operator--", 10))
- return (0);
- ddata->cur += 2;
- return (1);
- case SIMPLE_HASH('n', 'a'):
- /* operator new[] */
- if (!cpp_demangle_push_str(ddata, "operator new []", 15))
- return (0);
- ddata->cur += 2;
- return (1);
- case SIMPLE_HASH('n', 'e'):
- /* operator != */
- if (!cpp_demangle_push_str(ddata, "operator!=", 10))
- return (0);
- ddata->cur += 2;
- return (1);
- case SIMPLE_HASH('n', 'g'):
- /* operator - (unary) */
- if (!cpp_demangle_push_str(ddata, "operator-", 9))
- return (0);
- ddata->cur += 2;
- return (1);
- case SIMPLE_HASH('n', 't'):
- /* operator ! */
- if (!cpp_demangle_push_str(ddata, "operator!", 9))
- return (0);
- ddata->cur += 2;
- return (1);
- case SIMPLE_HASH('n', 'w'):
- /* operator new */
- if (!cpp_demangle_push_str(ddata, "operator new", 12))
- return (0);
- ddata->cur += 2;
- return (1);
- case SIMPLE_HASH('o', 'o'):
- /* operator || */
- if (!cpp_demangle_push_str(ddata, "operator||", 10))
- return (0);
- ddata->cur += 2;
- return (1);
- case SIMPLE_HASH('o', 'r'):
- /* operator | */
- if (!cpp_demangle_push_str(ddata, "operator|", 9))
- return (0);
- ddata->cur += 2;
- return (1);
- case SIMPLE_HASH('o', 'R'):
- /* operator |= */
- if (!cpp_demangle_push_str(ddata, "operator|=", 10))
- return (0);
- ddata->cur += 2;
- return (1);
- case SIMPLE_HASH('p', 'l'):
- /* operator + */
- if (!cpp_demangle_push_str(ddata, "operator+", 9))
- return (0);
- ddata->cur += 2;
- return (1);
- case SIMPLE_HASH('p', 'L'):
- /* operator += */
- if (!cpp_demangle_push_str(ddata, "operator+=", 10))
- return (0);
- ddata->cur += 2;
- return (1);
- case SIMPLE_HASH('p', 'm'):
- /* operator ->* */
- if (!cpp_demangle_push_str(ddata, "operator->*", 11))
- return (0);
- ddata->cur += 2;
- return (1);
- case SIMPLE_HASH('p', 'p'):
- /* operator ++ */
- if (!cpp_demangle_push_str(ddata, "operator++", 10))
- return (0);
- ddata->cur += 2;
- return (1);
- case SIMPLE_HASH('p', 's'):
- /* operator + (unary) */
- if (!cpp_demangle_push_str(ddata, "operator+", 9))
- return (0);
- ddata->cur += 2;
- return (1);
- case SIMPLE_HASH('p', 't'):
- /* operator -> */
- if (!cpp_demangle_push_str(ddata, "operator->", 10))
- return (0);
- ddata->cur += 2;
- return (1);
- case SIMPLE_HASH('q', 'u'):
- /* operator ? */
- if (!cpp_demangle_push_str(ddata, "operator?", 9))
- return (0);
- ddata->cur += 2;
- return (1);
- case SIMPLE_HASH('r', 'm'):
- /* operator % */
- if (!cpp_demangle_push_str(ddata, "operator%", 9))
- return (0);
- ddata->cur += 2;
- return (1);
- case SIMPLE_HASH('r', 'M'):
- /* operator %= */
- if (!cpp_demangle_push_str(ddata, "operator%=", 10))
- return (0);
- ddata->cur += 2;
- return (1);
- case SIMPLE_HASH('r', 's'):
- /* operator >> */
- if (!cpp_demangle_push_str(ddata, "operator>>", 10))
- return (0);
- ddata->cur += 2;
- return (1);
- case SIMPLE_HASH('r', 'S'):
- /* operator >>= */
- if (!cpp_demangle_push_str(ddata, "operator>>=", 11))
- return (0);
- ddata->cur += 2;
- return (1);
- case SIMPLE_HASH('r', 'z'):
- /* operator sizeof */
- if (!cpp_demangle_push_str(ddata, "operator sizeof ", 16))
- return (0);
- ddata->cur += 2;
- return (1);
- case SIMPLE_HASH('s', 'r'):
- /* scope resolution operator */
- if (!cpp_demangle_push_str(ddata, "scope resolution operator ",
- 26))
- return (0);
- ddata->cur += 2;
- return (1);
- case SIMPLE_HASH('s', 'v'):
- /* operator sizeof */
- if (!cpp_demangle_push_str(ddata, "operator sizeof ", 16))
- return (0);
- ddata->cur += 2;
- return (1);
- };
- /* vendor extened operator */
- if (*ddata->cur == 'v' && ELFTC_ISDIGIT(*(ddata->cur + 1))) {
- if (!cpp_demangle_push_str(ddata, "vendor extened operator ",
- 24))
- return (0);
- if (!cpp_demangle_push_str(ddata, ddata->cur + 1, 1))
- return (0);
- ddata->cur += 2;
- return (cpp_demangle_read_sname(ddata));
- }
- /* ctor-dtor-name */
- switch (SIMPLE_HASH(*ddata->cur, *(ddata->cur + 1))) {
- case SIMPLE_HASH('C', '1'):
- /* FALLTHROUGH */
- case SIMPLE_HASH('C', '2'):
- /* FALLTHROUGH */
- case SIMPLE_HASH('C', '3'):
- if (ddata->last_sname == NULL)
- return (0);
- if ((len = strlen(ddata->last_sname)) == 0)
- return (0);
- if (!cpp_demangle_push_str(ddata, "::", 2))
- return (0);
- if (!cpp_demangle_push_str(ddata, ddata->last_sname, len))
- return (0);
- ddata->cur +=2;
- return (1);
- case SIMPLE_HASH('D', '0'):
- /* FALLTHROUGH */
- case SIMPLE_HASH('D', '1'):
- /* FALLTHROUGH */
- case SIMPLE_HASH('D', '2'):
- if (ddata->last_sname == NULL)
- return (0);
- if ((len = strlen(ddata->last_sname)) == 0)
- return (0);
- if (!cpp_demangle_push_str(ddata, "::~", 3))
- return (0);
- if (!cpp_demangle_push_str(ddata, ddata->last_sname, len))
- return (0);
- ddata->cur +=2;
- return (1);
- };
- /* source name */
- if (ELFTC_ISDIGIT(*ddata->cur) != 0)
- return (cpp_demangle_read_sname(ddata));
- return (1);
- }
- static int
- cpp_demangle_read_v_offset(struct cpp_demangle_data *ddata)
- {
- if (ddata == NULL)
- return (0);
- if (!cpp_demangle_push_str(ddata, "offset : ", 9))
- return (0);
- if (!cpp_demangle_read_offset_number(ddata))
- return (0);
- if (!cpp_demangle_push_str(ddata, "virtual offset : ", 17))
- return (0);
- return (!cpp_demangle_read_offset_number(ddata));
- }
- /*
- * Decode floating point representation to string
- * Return new allocated string or NULL
- *
- * Todo
- * Replace these functions to macro.
- */
- static char *
- decode_fp_to_double(const char *p, size_t len)
- {
- double f;
- size_t rtn_len, limit, i;
- int byte;
- char *rtn;
- if (p == NULL || len == 0 || len % 2 != 0 || len / 2 > sizeof(double))
- return (NULL);
- memset(&f, 0, sizeof(double));
- for (i = 0; i < len / 2; ++i) {
- byte = hex_to_dec(p[len - i * 2 - 1]) +
- hex_to_dec(p[len - i * 2 - 2]) * 16;
- if (byte < 0 || byte > 255)
- return (NULL);
- #if ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN
- ((unsigned char *)&f)[i] = (unsigned char)(byte);
- #else /* ELFTC_BYTE_ORDER != ELFTC_BYTE_ORDER_LITTLE_ENDIAN */
- ((unsigned char *)&f)[sizeof(double) - i - 1] =
- (unsigned char)(byte);
- #endif /* ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN */
- }
- rtn_len = 64;
- limit = 0;
- again:
- if ((rtn = malloc(sizeof(char) * rtn_len)) == NULL)
- return (NULL);
- if (snprintf(rtn, rtn_len, "%fld", f) >= (int)rtn_len) {
- free(rtn);
- if (limit++ > FLOAT_SPRINTF_TRY_LIMIT)
- return (NULL);
- rtn_len *= BUFFER_GROWFACTOR;
- goto again;
- }
- return rtn;
- }
- static char *
- decode_fp_to_float(const char *p, size_t len)
- {
- size_t i, rtn_len, limit;
- float f;
- int byte;
- char *rtn;
- if (p == NULL || len == 0 || len % 2 != 0 || len / 2 > sizeof(float))
- return (NULL);
- memset(&f, 0, sizeof(float));
- for (i = 0; i < len / 2; ++i) {
- byte = hex_to_dec(p[len - i * 2 - 1]) +
- hex_to_dec(p[len - i * 2 - 2]) * 16;
- if (byte < 0 || byte > 255)
- return (NULL);
- #if ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN
- ((unsigned char *)&f)[i] = (unsigned char)(byte);
- #else /* ELFTC_BYTE_ORDER != ELFTC_BYTE_ORDER_LITTLE_ENDIAN */
- ((unsigned char *)&f)[sizeof(float) - i - 1] =
- (unsigned char)(byte);
- #endif /* ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN */
- }
- rtn_len = 64;
- limit = 0;
- again:
- if ((rtn = malloc(sizeof(char) * rtn_len)) == NULL)
- return (NULL);
- if (snprintf(rtn, rtn_len, "%ff", f) >= (int)rtn_len) {
- free(rtn);
- if (limit++ > FLOAT_SPRINTF_TRY_LIMIT)
- return (NULL);
- rtn_len *= BUFFER_GROWFACTOR;
- goto again;
- }
- return rtn;
- }
- static char *
- decode_fp_to_float128(const char *p, size_t len)
- {
- long double f;
- size_t rtn_len, limit, i;
- int byte;
- unsigned char buf[FLOAT_QUADRUPLE_BYTES];
- char *rtn;
- switch(sizeof(long double)) {
- case FLOAT_QUADRUPLE_BYTES:
- return (decode_fp_to_long_double(p, len));
- case FLOAT_EXTENED_BYTES:
- if (p == NULL || len == 0 || len % 2 != 0 ||
- len / 2 > FLOAT_QUADRUPLE_BYTES)
- return (NULL);
- memset(buf, 0, FLOAT_QUADRUPLE_BYTES);
- for (i = 0; i < len / 2; ++i) {
- byte = hex_to_dec(p[len - i * 2 - 1]) +
- hex_to_dec(p[len - i * 2 - 2]) * 16;
- if (byte < 0 || byte > 255)
- return (NULL);
- #if ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN
- buf[i] = (unsigned char)(byte);
- #else /* ELFTC_BYTE_ORDER != ELFTC_BYTE_ORDER_LITTLE_ENDIAN */
- buf[FLOAT_QUADRUPLE_BYTES - i -1] =
- (unsigned char)(byte);
- #endif /* ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN */
- }
- memset(&f, 0, FLOAT_EXTENED_BYTES);
- #if ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN
- memcpy(&f, buf, FLOAT_EXTENED_BYTES);
- #else /* ELFTC_BYTE_ORDER != ELFTC_BYTE_ORDER_LITTLE_ENDIAN */
- memcpy(&f, buf + 6, FLOAT_EXTENED_BYTES);
- #endif /* ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN */
- rtn_len = 256;
- limit = 0;
- again:
- if ((rtn = malloc(sizeof(char) * rtn_len)) == NULL)
- return (NULL);
- if (snprintf(rtn, rtn_len, "%Lfd", f) >= (int)rtn_len) {
- free(rtn);
- if (limit++ > FLOAT_SPRINTF_TRY_LIMIT)
- return (NULL);
- rtn_len *= BUFFER_GROWFACTOR;
- goto again;
- }
- return (rtn);
- default:
- return (NULL);
- }
- }
- static char *
- decode_fp_to_float80(const char *p, size_t len)
- {
- long double f;
- size_t rtn_len, limit, i;
- int byte;
- unsigned char buf[FLOAT_EXTENED_BYTES];
- char *rtn;
- switch(sizeof(long double)) {
- case FLOAT_QUADRUPLE_BYTES:
- if (p == NULL || len == 0 || len % 2 != 0 ||
- len / 2 > FLOAT_EXTENED_BYTES)
- return (NULL);
- memset(buf, 0, FLOAT_EXTENED_BYTES);
- for (i = 0; i < len / 2; ++i) {
- byte = hex_to_dec(p[len - i * 2 - 1]) +
- hex_to_dec(p[len - i * 2 - 2]) * 16;
- if (byte < 0 || byte > 255)
- return (NULL);
- #if ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN
- buf[i] = (unsigned char)(byte);
- #else /* ELFTC_BYTE_ORDER != ELFTC_BYTE_ORDER_LITTLE_ENDIAN */
- buf[FLOAT_EXTENED_BYTES - i -1] =
- (unsigned char)(byte);
- #endif /* ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN */
- }
- memset(&f, 0, FLOAT_QUADRUPLE_BYTES);
- #if ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN
- memcpy(&f, buf, FLOAT_EXTENED_BYTES);
- #else /* ELFTC_BYTE_ORDER != ELFTC_BYTE_ORDER_LITTLE_ENDIAN */
- memcpy((unsigned char *)(&f) + 6, buf, FLOAT_EXTENED_BYTES);
- #endif /* ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN */
- rtn_len = 256;
- limit = 0;
- again:
- if ((rtn = malloc(sizeof(char) * rtn_len)) == NULL)
- return (NULL);
- if (snprintf(rtn, rtn_len, "%Lfd", f) >= (int)rtn_len) {
- free(rtn);
- if (limit++ > FLOAT_SPRINTF_TRY_LIMIT)
- return (NULL);
- rtn_len *= BUFFER_GROWFACTOR;
- goto again;
- }
- return (rtn);
- case FLOAT_EXTENED_BYTES:
- return (decode_fp_to_long_double(p, len));
- default:
- return (NULL);
- }
- }
- static char *
- decode_fp_to_long_double(const char *p, size_t len)
- {
- long double f;
- size_t rtn_len, limit, i;
- int byte;
- char *rtn;
- if (p == NULL || len == 0 || len % 2 != 0 ||
- len / 2 > sizeof(long double))
- return (NULL);
- memset(&f, 0, sizeof(long double));
- for (i = 0; i < len / 2; ++i) {
- byte = hex_to_dec(p[len - i * 2 - 1]) +
- hex_to_dec(p[len - i * 2 - 2]) * 16;
- if (byte < 0 || byte > 255)
- return (NULL);
- #if ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN
- ((unsigned char *)&f)[i] = (unsigned char)(byte);
- #else /* ELFTC_BYTE_ORDER != ELFTC_BYTE_ORDER_LITTLE_ENDIAN */
- ((unsigned char *)&f)[sizeof(long double) - i - 1] =
- (unsigned char)(byte);
- #endif /* ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN */
- }
- rtn_len = 256;
- limit = 0;
- again:
- if ((rtn = malloc(sizeof(char) * rtn_len)) == NULL)
- return (NULL);
- if (snprintf(rtn, rtn_len, "%Lfd", f) >= (int)rtn_len) {
- free(rtn);
- if (limit++ > FLOAT_SPRINTF_TRY_LIMIT)
- return (NULL);
- rtn_len *= BUFFER_GROWFACTOR;
- goto again;
- }
- return (rtn);
- }
- /* Simple hex to integer function used by decode_to_* function. */
- static int
- hex_to_dec(char c)
- {
- switch (c) {
- case '0':
- return (0);
- case '1':
- return (1);
- case '2':
- return (2);
- case '3':
- return (3);
- case '4':
- return (4);
- case '5':
- return (5);
- case '6':
- return (6);
- case '7':
- return (7);
- case '8':
- return (8);
- case '9':
- return (9);
- case 'a':
- return (10);
- case 'b':
- return (11);
- case 'c':
- return (12);
- case 'd':
- return (13);
- case 'e':
- return (14);
- case 'f':
- return (15);
- default:
- return (-1);
- };
- }
- static void
- vector_read_cmd_dest(struct vector_read_cmd *v)
- {
- if (v == NULL)
- return;
- free(v->r_container);
- }
- /* return -1 at failed, 0 at not found, 1 at found. */
- static int
- vector_read_cmd_find(struct vector_read_cmd *v, enum read_cmd dst)
- {
- size_t i;
- if (v == NULL || dst == READ_FAIL)
- return (-1);
- for (i = 0; i < v->size; ++i)
- if (v->r_container[i] == dst)
- return (1);
- return (0);
- }
- static int
- vector_read_cmd_init(struct vector_read_cmd *v)
- {
- if (v == NULL)
- return (0);
- v->size = 0;
- v->capacity = VECTOR_DEF_CAPACITY;
- if ((v->r_container = malloc(sizeof(enum read_cmd) * v->capacity))
- == NULL)
- return (0);
- return (1);
- }
- static int
- vector_read_cmd_pop(struct vector_read_cmd *v)
- {
- if (v == NULL || v->size == 0)
- return (0);
- --v->size;
- v->r_container[v->size] = READ_FAIL;
- return (1);
- }
- static int
- vector_read_cmd_push(struct vector_read_cmd *v, enum read_cmd cmd)
- {
- enum read_cmd *tmp_r_ctn;
- size_t tmp_cap;
- size_t i;
- if (v == NULL)
- return (0);
- if (v->size == v->capacity) {
- tmp_cap = v->capacity * BUFFER_GROWFACTOR;
- if ((tmp_r_ctn = malloc(sizeof(enum read_cmd) * tmp_cap))
- == NULL)
- return (0);
- for (i = 0; i < v->size; ++i)
- tmp_r_ctn[i] = v->r_container[i];
- free(v->r_container);
- v->r_container = tmp_r_ctn;
- v->capacity = tmp_cap;
- }
- v->r_container[v->size] = cmd;
- ++v->size;
- return (1);
- }
- static void
- vector_type_qualifier_dest(struct vector_type_qualifier *v)
- {
- if (v == NULL)
- return;
- free(v->q_container);
- vector_str_dest(&v->ext_name);
- }
- /* size, capacity, ext_name */
- static int
- vector_type_qualifier_init(struct vector_type_qualifier *v)
- {
- if (v == NULL)
- return (0);
- v->size = 0;
- v->capacity = VECTOR_DEF_CAPACITY;
- if ((v->q_container = malloc(sizeof(enum type_qualifier) * v->capacity))
- == NULL)
- return (0);
- assert(v->q_container != NULL);
- if (vector_str_init(&v->ext_name) == false) {
- free(v->q_container);
- return (0);
- }
- return (1);
- }
- static int
- vector_type_qualifier_push(struct vector_type_qualifier *v,
- enum type_qualifier t)
- {
- enum type_qualifier *tmp_ctn;
- size_t tmp_cap;
- size_t i;
- if (v == NULL)
- return (0);
- if (v->size == v->capacity) {
- tmp_cap = v->capacity * BUFFER_GROWFACTOR;
- if ((tmp_ctn = malloc(sizeof(enum type_qualifier) * tmp_cap))
- == NULL)
- return (0);
- for (i = 0; i < v->size; ++i)
- tmp_ctn[i] = v->q_container[i];
- free(v->q_container);
- v->q_container = tmp_ctn;
- v->capacity = tmp_cap;
- }
- v->q_container[v->size] = t;
- ++v->size;
- return (1);
- }
|