12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377 |
- /* This Source Code Form is subject to the terms of the Mozilla Public
- * License, v. 2.0. If a copy of the MPL was not distributed with this
- * file, You can obtain one at http://mozilla.org/MPL/2.0/.
- *
- * Copyright 2014-2018 (c) Fraunhofer IOSB (Author: Julius Pfrommer)
- * Copyright 2018 (c) Fraunhofer IOSB (Author: Lukas Meling)
- */
- #include "ua_types_encoding_json.h"
- #include "ua_types_encoding_binary.h"
- #include "ua_types_generated.h"
- #include "ua_types_generated_handling.h"
- #include "ua_plugin_log.h"
- #include <math.h>
- #include <float.h>
- #ifdef UA_ENABLE_CUSTOM_LIBC
- #include "../deps/musl/floatscan.h"
- #include "../deps/musl/vfprintf.h"
- #endif
- #include "../deps/itoa.h"
- #include "../deps/atoi.h"
- #include "../deps/string_escape.h"
- #include "../deps/base64.h"
- #include "../deps/libc_time.h"
- #if defined(_MSC_VER)
- # define strtoll _strtoi64
- # define strtoull _strtoui64
- #endif
- /* vs2008 does not have INFINITY and NAN defined */
- #ifndef INFINITY
- # define INFINITY ((UA_Double)(DBL_MAX+DBL_MAX))
- #endif
- #ifndef NAN
- # define NAN ((UA_Double)(INFINITY-INFINITY))
- #endif
- #if defined(_MSC_VER)
- # pragma warning(disable: 4756)
- # pragma warning(disable: 4056)
- #endif
- #define UA_NODEIDTYPE_NUMERIC_TWOBYTE 0
- #define UA_NODEIDTYPE_NUMERIC_FOURBYTE 1
- #define UA_NODEIDTYPE_NUMERIC_COMPLETE 2
- #define UA_EXPANDEDNODEID_SERVERINDEX_FLAG 0x40
- #define UA_EXPANDEDNODEID_NAMESPACEURI_FLAG 0x80
- #define UA_JSON_DATETIME_LENGTH 24
- /* Max length of numbers for the allocation of temp buffers. Don't forget that
- * printf adds an additional \0 at the end!
- *
- * Sources:
- * https://www.exploringbinary.com/maximum-number-of-decimal-digits-in-binary-floating-point-numbers/
- *
- * UInt16: 3 + 1
- * SByte: 3 + 1
- * UInt32:
- * Int32:
- * UInt64:
- * Int64:
- * Float: 149 + 1
- * Double: 767 + 1
- */
- /************/
- /* Encoding */
- /************/
- #define ENCODE_JSON(TYPE) static status \
- TYPE##_encodeJson(const UA_##TYPE *src, const UA_DataType *type, CtxJson *ctx)
- #define ENCODE_DIRECT_JSON(SRC, TYPE) \
- TYPE##_encodeJson((const UA_##TYPE*)SRC, NULL, ctx)
- extern const encodeJsonSignature encodeJsonJumpTable[UA_DATATYPEKINDS];
- extern const decodeJsonSignature decodeJsonJumpTable[UA_DATATYPEKINDS];
- /* Forward declarations */
- UA_String UA_DateTime_toJSON(UA_DateTime t);
- ENCODE_JSON(ByteString);
- static status UA_FUNC_ATTR_WARN_UNUSED_RESULT
- writeChar(CtxJson *ctx, char c) {
- if(ctx->pos >= ctx->end)
- return UA_STATUSCODE_BADENCODINGLIMITSEXCEEDED;
- if(!ctx->calcOnly)
- *ctx->pos = (UA_Byte)c;
- ctx->pos++;
- return UA_STATUSCODE_GOOD;
- }
- #define WRITE_JSON_ELEMENT(ELEM) \
- UA_FUNC_ATTR_WARN_UNUSED_RESULT status \
- writeJson##ELEM(CtxJson *ctx)
- static WRITE_JSON_ELEMENT(Quote) {
- return writeChar(ctx, '\"');
- }
- WRITE_JSON_ELEMENT(ObjStart) {
- /* increase depth, save: before first key-value no comma needed. */
- ctx->depth++;
- ctx->commaNeeded[ctx->depth] = false;
- return writeChar(ctx, '{');
- }
- WRITE_JSON_ELEMENT(ObjEnd) {
- ctx->depth--; //decrease depth
- ctx->commaNeeded[ctx->depth] = true;
- return writeChar(ctx, '}');
- }
- WRITE_JSON_ELEMENT(ArrStart) {
- /* increase depth, save: before first array entry no comma needed. */
- ctx->commaNeeded[++ctx->depth] = false;
- return writeChar(ctx, '[');
- }
- WRITE_JSON_ELEMENT(ArrEnd) {
- ctx->depth--; //decrease depth
- ctx->commaNeeded[ctx->depth] = true;
- return writeChar(ctx, ']');
- }
- WRITE_JSON_ELEMENT(CommaIfNeeded) {
- if(ctx->commaNeeded[ctx->depth])
- return writeChar(ctx, ',');
- return UA_STATUSCODE_GOOD;
- }
- status
- writeJsonArrElm(CtxJson *ctx, const void *value,
- const UA_DataType *type) {
- status ret = writeJsonCommaIfNeeded(ctx);
- ctx->commaNeeded[ctx->depth] = true;
- ret |= encodeJsonInternal(value, type, ctx);
- return ret;
- }
- status writeJsonObjElm(CtxJson *ctx, UA_String *key,
- const void *value, const UA_DataType *type){
- UA_STACKARRAY(char, out, key->length + 1);
- memcpy(out, key->data, key->length);
- out[key->length] = 0;
- status ret = writeJsonKey(ctx, out);
- ret |= encodeJsonInternal(value, type, ctx);
- return ret;
- }
- status writeJsonNull(CtxJson *ctx) {
- if(ctx->pos + 4 > ctx->end)
- return UA_STATUSCODE_BADENCODINGLIMITSEXCEEDED;
- if(ctx->calcOnly) {
- ctx->pos += 4;
- } else {
- *(ctx->pos++) = 'n';
- *(ctx->pos++) = 'u';
- *(ctx->pos++) = 'l';
- *(ctx->pos++) = 'l';
- }
- return UA_STATUSCODE_GOOD;
- }
- #define UA_STRING_TO_CSTRING(in,out) \
- UA_STACKARRAY(char, out, in->length + 1); \
- memcpy(out, in->data, in->length); \
- out[in->length] = 0;
- #define NULL_TERMINATE(in, inlength, out) \
- UA_STACKARRAY(char, out, inlength + 1); \
- memcpy(out, in, inlength); \
- out[inlength] = 0;
- /* Keys for JSON */
- /* LocalizedText */
- static const char* UA_JSONKEY_LOCALE = "Locale";
- static const char* UA_JSONKEY_TEXT = "Text";
- /* QualifiedName */
- static const char* UA_JSONKEY_NAME = "Name";
- static const char* UA_JSONKEY_URI = "Uri";
- /* NodeId */
- static const char* UA_JSONKEY_ID = "Id";
- static const char* UA_JSONKEY_IDTYPE = "IdType";
- static const char* UA_JSONKEY_NAMESPACE = "Namespace";
- /* ExpandedNodeId */
- static const char* UA_JSONKEY_SERVERURI = "ServerUri";
- /* Variant */
- static const char* UA_JSONKEY_TYPE = "Type";
- static const char* UA_JSONKEY_BODY = "Body";
- static const char* UA_JSONKEY_DIMENSION = "Dimension";
- /* DataValue */
- static const char* UA_JSONKEY_VALUE = "Value";
- static const char* UA_JSONKEY_STATUS = "Status";
- static const char* UA_JSONKEY_SOURCETIMESTAMP = "SourceTimestamp";
- static const char* UA_JSONKEY_SOURCEPICOSECONDS = "SourcePicoseconds";
- static const char* UA_JSONKEY_SERVERTIMESTAMP = "ServerTimestamp";
- static const char* UA_JSONKEY_SERVERPICOSECONDS = "ServerPicoseconds";
- /* ExtensionObject */
- static const char* UA_JSONKEY_ENCODING = "Encoding";
- static const char* UA_JSONKEY_TYPEID = "TypeId";
- /* StatusCode */
- static const char* UA_JSONKEY_CODE = "Code";
- static const char* UA_JSONKEY_SYMBOL = "Symbol";
- /* DiagnosticInfo */
- static const char* UA_JSONKEY_SYMBOLICID = "SymbolicId";
- static const char* UA_JSONKEY_NAMESPACEURI = "NamespaceUri";
- static const char* UA_JSONKEY_LOCALIZEDTEXT = "LocalizedText";
- static const char* UA_JSONKEY_ADDITIONALINFO = "AdditionalInfo";
- static const char* UA_JSONKEY_INNERSTATUSCODE = "InnerStatusCode";
- static const char* UA_JSONKEY_INNERDIAGNOSTICINFO = "InnerDiagnosticInfo";
- /* Writes null terminated string to output buffer (current ctx->pos). Writes
- * comma in front of key if needed. Encapsulates key in quotes. */
- status UA_FUNC_ATTR_WARN_UNUSED_RESULT
- writeJsonKey(CtxJson *ctx, const char* key) {
- size_t size = strlen(key);
- if(ctx->pos + size + 4 > ctx->end) /* +4 because of " " : and , */
- return UA_STATUSCODE_BADENCODINGLIMITSEXCEEDED;
- status ret = writeJsonCommaIfNeeded(ctx);
- ctx->commaNeeded[ctx->depth] = true;
- if(ctx->calcOnly) {
- ctx->commaNeeded[ctx->depth] = true;
- ctx->pos += 3;
- ctx->pos += size;
- return ret;
- }
- ret |= writeChar(ctx, '\"');
- for(size_t i = 0; i < size; i++) {
- *(ctx->pos++) = (u8)key[i];
- }
- ret |= writeChar(ctx, '\"');
- ret |= writeChar(ctx, ':');
- return ret;
- }
- /* Boolean */
- ENCODE_JSON(Boolean) {
- size_t sizeOfJSONBool;
- if(*src == true) {
- sizeOfJSONBool = 4; /*"true"*/
- } else {
- sizeOfJSONBool = 5; /*"false"*/
- }
- if(ctx->calcOnly) {
- ctx->pos += sizeOfJSONBool;
- return UA_STATUSCODE_GOOD;
- }
- if(ctx->pos + sizeOfJSONBool > ctx->end)
- return UA_STATUSCODE_BADENCODINGLIMITSEXCEEDED;
- if(*src) {
- *(ctx->pos++) = 't';
- *(ctx->pos++) = 'r';
- *(ctx->pos++) = 'u';
- *(ctx->pos++) = 'e';
- } else {
- *(ctx->pos++) = 'f';
- *(ctx->pos++) = 'a';
- *(ctx->pos++) = 'l';
- *(ctx->pos++) = 's';
- *(ctx->pos++) = 'e';
- }
- return UA_STATUSCODE_GOOD;
- }
- /*****************/
- /* Integer Types */
- /*****************/
- /* Byte */
- ENCODE_JSON(Byte) {
- char buf[4];
- UA_UInt16 digits = itoaUnsigned(*src, buf, 10);
- /* Ensure destination can hold the data- */
- if(ctx->pos + digits > ctx->end)
- return UA_STATUSCODE_BADENCODINGLIMITSEXCEEDED;
- /* Copy digits to the output string/buffer. */
- if(!ctx->calcOnly)
- memcpy(ctx->pos, buf, digits);
- ctx->pos += digits;
- return UA_STATUSCODE_GOOD;
- }
- /* signed Byte */
- ENCODE_JSON(SByte) {
- char buf[5];
- UA_UInt16 digits = itoaSigned(*src, buf);
- if(ctx->pos + digits > ctx->end)
- return UA_STATUSCODE_BADENCODINGLIMITSEXCEEDED;
- if(!ctx->calcOnly)
- memcpy(ctx->pos, buf, digits);
- ctx->pos += digits;
- return UA_STATUSCODE_GOOD;
- }
- /* UInt16 */
- ENCODE_JSON(UInt16) {
- char buf[6];
- UA_UInt16 digits = itoaUnsigned(*src, buf, 10);
- if(ctx->pos + digits > ctx->end)
- return UA_STATUSCODE_BADENCODINGLIMITSEXCEEDED;
- if(!ctx->calcOnly)
- memcpy(ctx->pos, buf, digits);
- ctx->pos += digits;
- return UA_STATUSCODE_GOOD;
- }
- /* Int16 */
- ENCODE_JSON(Int16) {
- char buf[7];
- UA_UInt16 digits = itoaSigned(*src, buf);
- if(ctx->pos + digits > ctx->end)
- return UA_STATUSCODE_BADENCODINGLIMITSEXCEEDED;
- if(!ctx->calcOnly)
- memcpy(ctx->pos, buf, digits);
- ctx->pos += digits;
- return UA_STATUSCODE_GOOD;
- }
- /* UInt32 */
- ENCODE_JSON(UInt32) {
- char buf[11];
- UA_UInt16 digits = itoaUnsigned(*src, buf, 10);
- if(ctx->pos + digits > ctx->end)
- return UA_STATUSCODE_BADENCODINGLIMITSEXCEEDED;
- if(!ctx->calcOnly)
- memcpy(ctx->pos, buf, digits);
- ctx->pos += digits;
- return UA_STATUSCODE_GOOD;
- }
- /* Int32 */
- ENCODE_JSON(Int32) {
- char buf[12];
- UA_UInt16 digits = itoaSigned(*src, buf);
- if(ctx->pos + digits > ctx->end)
- return UA_STATUSCODE_BADENCODINGLIMITSEXCEEDED;
- if(!ctx->calcOnly)
- memcpy(ctx->pos, buf, digits);
- ctx->pos += digits;
- return UA_STATUSCODE_GOOD;
- }
- /* UInt64 */
- ENCODE_JSON(UInt64) {
- char buf[21];
- UA_UInt16 digits = itoaUnsigned(*src, buf, 10);
- if(ctx->pos + digits > ctx->end)
- return UA_STATUSCODE_BADENCODINGLIMITSEXCEEDED;
- if(!ctx->calcOnly)
- memcpy(ctx->pos, buf, digits);
- ctx->pos += digits;
- return UA_STATUSCODE_GOOD;
- }
- /* Int64 */
- ENCODE_JSON(Int64) {
- char buf[21];
- UA_UInt16 digits = itoaSigned(*src, buf);
- if(ctx->pos + digits > ctx->end)
- return UA_STATUSCODE_BADENCODINGLIMITSEXCEEDED;
- if(!ctx->calcOnly)
- memcpy(ctx->pos, buf, digits);
- ctx->pos += digits;
- return UA_STATUSCODE_GOOD;
- }
- /************************/
- /* Floating Point Types */
- /************************/
- /* Convert special numbers to string
- * - fmt_fp gives NAN, nan,-NAN, -nan, inf, INF, -inf, -INF
- * - Special floating-point numbers such as positive infinity (INF), negative
- * infinity (-INF) and not-a-number (NaN) shall be represented by the values
- * “Infinity”, “-Infinity” and “NaN” encoded as a JSON string. */
- static status
- checkAndEncodeSpecialFloatingPoint(char *buffer, size_t *len) {
- /*nan and NaN*/
- if(*len == 3 &&
- (buffer[0] == 'n' || buffer[0] == 'N') &&
- (buffer[1] == 'a' || buffer[1] == 'A') &&
- (buffer[2] == 'n' || buffer[2] == 'N')) {
- *len = 5;
- memcpy(buffer, "\"NaN\"", *len);
- return UA_STATUSCODE_GOOD;
- }
- /*-nan and -NaN*/
- if(*len == 4 && buffer[0] == '-' &&
- (buffer[1] == 'n' || buffer[1] == 'N') &&
- (buffer[2] == 'a' || buffer[2] == 'A') &&
- (buffer[3] == 'n' || buffer[3] == 'N')) {
- *len = 6;
- memcpy(buffer, "\"-NaN\"", *len);
- return UA_STATUSCODE_GOOD;
- }
- /*inf*/
- if(*len == 3 &&
- (buffer[0] == 'i' || buffer[0] == 'I') &&
- (buffer[1] == 'n' || buffer[1] == 'N') &&
- (buffer[2] == 'f' || buffer[2] == 'F')) {
- *len = 10;
- memcpy(buffer, "\"Infinity\"", *len);
- return UA_STATUSCODE_GOOD;
- }
- /*-inf*/
- if(*len == 4 && buffer[0] == '-' &&
- (buffer[1] == 'i' || buffer[1] == 'I') &&
- (buffer[2] == 'n' || buffer[2] == 'N') &&
- (buffer[3] == 'f' || buffer[3] == 'F')) {
- *len = 11;
- memcpy(buffer, "\"-Infinity\"", *len);
- return UA_STATUSCODE_GOOD;
- }
- return UA_STATUSCODE_GOOD;
- }
- ENCODE_JSON(Float) {
- char buffer[200];
- if(*src == *src) {
- #ifdef UA_ENABLE_CUSTOM_LIBC
- fmt_fp(buffer, *src, 0, -1, 0, 'g');
- #else
- UA_snprintf(buffer, 200, "%.149g", (UA_Double)*src);
- #endif
- } else {
- strcpy(buffer, "NaN");
- }
- size_t len = strlen(buffer);
- if(len == 0)
- return UA_STATUSCODE_BADENCODINGERROR;
-
- checkAndEncodeSpecialFloatingPoint(buffer, &len);
-
- if(ctx->pos + len > ctx->end)
- return UA_STATUSCODE_BADENCODINGLIMITSEXCEEDED;
- if(!ctx->calcOnly)
- memcpy(ctx->pos, buffer, len);
- ctx->pos += len;
- return UA_STATUSCODE_GOOD;
- }
- ENCODE_JSON(Double) {
- char buffer[2000];
- if(*src == *src) {
- #ifdef UA_ENABLE_CUSTOM_LIBC
- fmt_fp(buffer, *src, 0, 17, 0, 'g');
- #else
- UA_snprintf(buffer, 2000, "%.1074g", *src);
- #endif
- } else {
- strcpy(buffer, "NaN");
- }
- size_t len = strlen(buffer);
- checkAndEncodeSpecialFloatingPoint(buffer, &len);
- if(ctx->pos + len > ctx->end)
- return UA_STATUSCODE_BADENCODINGLIMITSEXCEEDED;
- if(!ctx->calcOnly)
- memcpy(ctx->pos, buffer, len);
- ctx->pos += len;
- return UA_STATUSCODE_GOOD;
- }
- static status
- encodeJsonArray(CtxJson *ctx, const void *ptr, size_t length,
- const UA_DataType *type) {
- encodeJsonSignature encodeType = encodeJsonJumpTable[type->typeKind];
- status ret = writeJsonArrStart(ctx);
- uintptr_t uptr = (uintptr_t)ptr;
- for(size_t i = 0; i < length && ret == UA_STATUSCODE_GOOD; ++i) {
- ret |= writeJsonCommaIfNeeded(ctx);
- ret |= encodeType((const void*)uptr, type, ctx);
- ctx->commaNeeded[ctx->depth] = true;
- uptr += type->memSize;
- }
- ret |= writeJsonArrEnd(ctx);
- return ret;
- }
- /*****************/
- /* Builtin Types */
- /*****************/
- static const u8 hexmapLower[16] =
- {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
- static const u8 hexmapUpper[16] =
- {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
- ENCODE_JSON(String) {
- if(!src->data)
- return writeJsonNull(ctx);
- if(src->length == 0)
- return writeJsonQuote(ctx) | writeJsonQuote(ctx);
- UA_StatusCode ret = writeJsonQuote(ctx);
-
- /* Escaping adapted from https://github.com/akheron/jansson dump.c */
- const char *str = (char*)src->data;
- const char *pos = str;
- const char *end = str;
- const char *lim = str + src->length;
- UA_Int32 codepoint = 0;
- while(1) {
- const char *text;
- u8 seq[13];
- size_t length;
- while(end < lim) {
- end = utf8_iterate(pos, (size_t)(lim - pos), &codepoint);
- if(!end)
- return UA_STATUSCODE_BADENCODINGERROR;
- /* mandatory escape or control char */
- if(codepoint == '\\' || codepoint == '"' || codepoint < 0x20)
- break;
- /* slash
- if((flags & JSON_ESCAPE_SLASH) && codepoint == '/')
- break;*/
- /* non-ASCII
- if((flags & JSON_ENSURE_ASCII) && codepoint > 0x7F)
- break;*/
- pos = end;
- }
- if(pos != str) {
- if(ctx->pos + (pos - str) > ctx->end)
- return UA_STATUSCODE_BADENCODINGLIMITSEXCEEDED;
- if(!ctx->calcOnly)
- memcpy(ctx->pos, str, (size_t)(pos - str));
- ctx->pos += pos - str;
- }
- if(end == pos)
- break;
- /* handle \, /, ", and control codes */
- length = 2;
- switch(codepoint) {
- case '\\': text = "\\\\"; break;
- case '\"': text = "\\\""; break;
- case '\b': text = "\\b"; break;
- case '\f': text = "\\f"; break;
- case '\n': text = "\\n"; break;
- case '\r': text = "\\r"; break;
- case '\t': text = "\\t"; break;
- case '/': text = "\\/"; break;
- default:
- if(codepoint < 0x10000) {
- /* codepoint is in BMP */
- seq[0] = '\\';
- seq[1] = 'u';
- UA_Byte b1 = (UA_Byte)(codepoint >> 8);
- UA_Byte b2 = (UA_Byte)(codepoint >> 0);
- seq[2] = hexmapLower[(b1 & 0xF0) >> 4];
- seq[3] = hexmapLower[b1 & 0x0F];
- seq[4] = hexmapLower[(b2 & 0xF0) >> 4];
- seq[5] = hexmapLower[b2 & 0x0F];
- length = 6;
- } else {
- /* not in BMP -> construct a UTF-16 surrogate pair */
- codepoint -= 0x10000;
- UA_Int32 first = 0xD800 | ((codepoint & 0xffc00) >> 10);
- UA_Int32 last = 0xDC00 | (codepoint & 0x003ff);
- UA_Byte fb1 = (UA_Byte)(first >> 8);
- UA_Byte fb2 = (UA_Byte)(first >> 0);
-
- UA_Byte lb1 = (UA_Byte)(last >> 8);
- UA_Byte lb2 = (UA_Byte)(last >> 0);
-
- seq[0] = '\\';
- seq[1] = 'u';
- seq[2] = hexmapLower[(fb1 & 0xF0) >> 4];
- seq[3] = hexmapLower[fb1 & 0x0F];
- seq[4] = hexmapLower[(fb2 & 0xF0) >> 4];
- seq[5] = hexmapLower[fb2 & 0x0F];
-
- seq[6] = '\\';
- seq[7] = 'u';
- seq[8] = hexmapLower[(lb1 & 0xF0) >> 4];
- seq[9] = hexmapLower[lb1 & 0x0F];
- seq[10] = hexmapLower[(lb2 & 0xF0) >> 4];
- seq[11] = hexmapLower[lb2 & 0x0F];
- length = 12;
- }
- text = (char*)seq;
- break;
- }
- if(ctx->pos + length > ctx->end)
- return UA_STATUSCODE_BADENCODINGLIMITSEXCEEDED;
- if(!ctx->calcOnly)
- memcpy(ctx->pos, text, length);
- ctx->pos += length;
- str = pos = end;
- }
- ret |= writeJsonQuote(ctx);
- return ret;
- }
-
- ENCODE_JSON(ByteString) {
- if(!src->data)
- return writeJsonNull(ctx);
- if(src->length == 0)
- return writeJsonQuote(ctx) | writeJsonQuote(ctx);
- status ret = writeJsonQuote(ctx);
- int flen;
- char *ba64 = UA_base64(src->data, (int)src->length, &flen);
-
- /* Not converted, no mem */
- if(!ba64)
- return UA_STATUSCODE_BADENCODINGERROR;
- /* Check if negative... (TODO: Why is base64 3rd argument type int?) */
- if(flen < 0) {
- free(ba64);
- return UA_STATUSCODE_BADENCODINGERROR;
- }
- if(ctx->pos + flen > ctx->end) {
- free(ba64);
- return UA_STATUSCODE_BADENCODINGLIMITSEXCEEDED;
- }
-
- /* Copy flen bytes to output stream. */
- if(!ctx->calcOnly)
- memcpy(ctx->pos, ba64, (size_t)flen);
- ctx->pos += flen;
- /* Base64 result no longer needed */
- free(ba64);
-
- ret |= writeJsonQuote(ctx);
- return ret;
- }
- /* Converts Guid to a hexadecimal represenation */
- static void UA_Guid_to_hex(const UA_Guid *guid, u8* out) {
- /*
- 16 byte
- +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
- | data1 |data2|data3| data4 |
- +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
- |aa aa aa aa-bb bb-cc cc-dd dd-ee ee ee ee ee ee|
- +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
- 36 character
- */
- #ifdef hexCharlowerCase
- const u8 *hexmap = hexmapLower;
- #else
- const u8 *hexmap = hexmapUpper;
- #endif
- size_t i = 0, j = 28;
- for(; i<8;i++,j-=4) /* pos 0-7, 4byte, (a) */
- out[i] = hexmap[(guid->data1 >> j) & 0x0F];
- out[i++] = '-'; /* pos 8 */
- for(j=12; i<13;i++,j-=4) /* pos 9-12, 2byte, (b) */
- out[i] = hexmap[(guid->data2 >> j) & 0x0F];
- out[i++] = '-'; /* pos 13 */
- for(j=12; i<18;i++,j-=4) /* pos 14-17, 2byte (c) */
- out[i] = hexmap[(guid->data3 >> j) & 0x0F];
- out[i++] = '-'; /* pos 18 */
- for(j=0;i<23;i+=2,j++) { /* pos 19-22, 2byte (d) */
- out[i] = hexmap[(guid->data4[j] & 0xF0) >> 4];
- out[i+1] = hexmap[guid->data4[j] & 0x0F];
- }
- out[i++] = '-'; /* pos 23 */
- for(j=2; i<36;i+=2,j++) { /* pos 24-35, 6byte (e) */
- out[i] = hexmap[(guid->data4[j] & 0xF0) >> 4];
- out[i+1] = hexmap[guid->data4[j] & 0x0F];
- }
- }
- /* Guid */
- ENCODE_JSON(Guid) {
- if(ctx->pos + 38 > ctx->end) /* 36 + 2 (") */
- return UA_STATUSCODE_BADENCODINGLIMITSEXCEEDED;
- status ret = writeJsonQuote(ctx);
- u8 *buf = ctx->pos;
- if(!ctx->calcOnly)
- UA_Guid_to_hex(src, buf);
- ctx->pos += 36;
- ret |= writeJsonQuote(ctx);
- return ret;
- }
- static void
- printNumber(u16 n, u8 *pos, size_t digits) {
- for(size_t i = digits; i > 0; --i) {
- pos[i - 1] = (u8) ((n % 10) + '0');
- n = n / 10;
- }
- }
- UA_String
- UA_DateTime_toJSON(UA_DateTime t) {
- UA_DateTimeStruct tSt = UA_DateTime_toStruct(t);
- /* Format: yyyy-MM-dd'T'HH:mm:ss.SSS'Z' is used. 24 bytes.*/
- UA_String str = {UA_JSON_DATETIME_LENGTH,
- (u8*) UA_malloc(UA_JSON_DATETIME_LENGTH)};
- if(!str.data)
- return UA_STRING_NULL;
-
- printNumber(tSt.year, str.data, 4);
- str.data[4] = '-';
- printNumber(tSt.month, &str.data[5], 2);
- str.data[7] = '-';
- printNumber(tSt.day, &str.data[8], 2);
- str.data[10] = 'T';
- printNumber(tSt.hour, &str.data[11], 2);
- str.data[13] = ':';
- printNumber(tSt.min, &str.data[14], 2);
- str.data[16] = ':';
- printNumber(tSt.sec, &str.data[17], 2);
- str.data[19] = '.';
- printNumber(tSt.milliSec, &str.data[20], 3);
- str.data[23] = 'Z';
- return str;
- }
- ENCODE_JSON(DateTime) {
- if(ctx->pos + UA_JSON_DATETIME_LENGTH > ctx->end)
- return UA_STATUSCODE_BADENCODINGLIMITSEXCEEDED;
-
- UA_String str = UA_DateTime_toJSON(*src);
- UA_StatusCode ret = ENCODE_DIRECT_JSON(&str, String);
- UA_String_deleteMembers(&str);
- return ret;
- }
- /* NodeId */
- static status
- NodeId_encodeJsonInternal(UA_NodeId const *src, CtxJson *ctx) {
- status ret = UA_STATUSCODE_GOOD;
- switch (src->identifierType) {
- case UA_NODEIDTYPE_NUMERIC:
- ret |= writeJsonKey(ctx, UA_JSONKEY_ID);
- ret |= ENCODE_DIRECT_JSON(&src->identifier.numeric, UInt32);
- break;
- case UA_NODEIDTYPE_STRING:
- ret |= writeJsonKey(ctx, UA_JSONKEY_IDTYPE);
- ret |= writeChar(ctx, '1');
- ret |= writeJsonKey(ctx, UA_JSONKEY_ID);
- ret |= ENCODE_DIRECT_JSON(&src->identifier.string, String);
- break;
- case UA_NODEIDTYPE_GUID:
- ret |= writeJsonKey(ctx, UA_JSONKEY_IDTYPE);
- ret |= writeChar(ctx, '2');
- ret |= writeJsonKey(ctx, UA_JSONKEY_ID); /* Id */
- ret |= ENCODE_DIRECT_JSON(&src->identifier.guid, Guid);
- break;
- case UA_NODEIDTYPE_BYTESTRING:
- ret |= writeJsonKey(ctx, UA_JSONKEY_IDTYPE);
- ret |= writeChar(ctx, '3');
- ret |= writeJsonKey(ctx, UA_JSONKEY_ID); /* Id */
- ret |= ENCODE_DIRECT_JSON(&src->identifier.byteString, ByteString);
- break;
- default:
- return UA_STATUSCODE_BADINTERNALERROR;
- }
- return ret;
- }
- ENCODE_JSON(NodeId) {
- UA_StatusCode ret = writeJsonObjStart(ctx);
- ret |= NodeId_encodeJsonInternal(src, ctx);
- if(ctx->useReversible) {
- if(src->namespaceIndex > 0) {
- ret |= writeJsonKey(ctx, UA_JSONKEY_NAMESPACE);
- ret |= ENCODE_DIRECT_JSON(&src->namespaceIndex, UInt16);
- }
- } else {
- /* For the non-reversible encoding, the field is the NamespaceUri
- * associated with the NamespaceIndex, encoded as a JSON string.
- * A NamespaceIndex of 1 is always encoded as a JSON number. */
- if(src->namespaceIndex == 1) {
- ret |= writeJsonKey(ctx, UA_JSONKEY_NAMESPACE);
- ret |= ENCODE_DIRECT_JSON(&src->namespaceIndex, UInt16);
- } else {
- ret |= writeJsonKey(ctx, UA_JSONKEY_NAMESPACE);
-
- /* Check if Namespace given and in range */
- if(src->namespaceIndex < ctx->namespacesSize && ctx->namespaces != NULL) {
- UA_String namespaceEntry = ctx->namespaces[src->namespaceIndex];
- ret |= ENCODE_DIRECT_JSON(&namespaceEntry, String);
- } else {
- return UA_STATUSCODE_BADNOTFOUND;
- }
- }
- }
-
- ret = writeJsonObjEnd(ctx);
- return ret;
- }
- /* ExpandedNodeId */
- ENCODE_JSON(ExpandedNodeId) {
- status ret = writeJsonObjStart(ctx);
- /* Encode the NodeId */
- ret |= NodeId_encodeJsonInternal(&src->nodeId, ctx);
-
- if(ctx->useReversible) {
- if(src->namespaceUri.data != NULL && src->namespaceUri.length != 0 &&
- (void*) src->namespaceUri.data > UA_EMPTY_ARRAY_SENTINEL) {
- /* If the NamespaceUri is specified it is encoded as a JSON string in this field. */
- ret |= writeJsonKey(ctx, UA_JSONKEY_NAMESPACE);
- ret |= ENCODE_DIRECT_JSON(&src->namespaceUri, String);
- } else {
- /* If the NamespaceUri is not specified, the NamespaceIndex is encoded with these rules:
- * The field is encoded as a JSON number for the reversible encoding.
- * The field is omitted if the NamespaceIndex equals 0. */
- if(src->nodeId.namespaceIndex > 0) {
- ret |= writeJsonKey(ctx, UA_JSONKEY_NAMESPACE);
- ret |= ENCODE_DIRECT_JSON(&src->nodeId.namespaceIndex, UInt16);
- }
- }
- /* Encode the serverIndex/Url
- * This field is encoded as a JSON number for the reversible encoding.
- * This field is omitted if the ServerIndex equals 0. */
- if(src->serverIndex > 0) {
- ret |= writeJsonKey(ctx, UA_JSONKEY_SERVERURI);
- ret |= ENCODE_DIRECT_JSON(&src->serverIndex, UInt32);
- }
- ret |= writeJsonObjEnd(ctx);
- return ret;
- }
-
-
- /* NON-Reversible Case */
- /* If the NamespaceUri is not specified, the NamespaceIndex is encoded with these rules:
- * For the non-reversible encoding the field is the NamespaceUri associated with the
- * NamespaceIndex encoded as a JSON string.
- * A NamespaceIndex of 1 is always encoded as a JSON number. */
- if(src->namespaceUri.data != NULL && src->namespaceUri.length != 0) {
- ret |= writeJsonKey(ctx, UA_JSONKEY_NAMESPACE);
- ret |= ENCODE_DIRECT_JSON(&src->namespaceUri, String);
- if(ret != UA_STATUSCODE_GOOD)
- return ret;
- } else {
- if(src->nodeId.namespaceIndex == 1) {
- ret |= writeJsonKey(ctx, UA_JSONKEY_NAMESPACE);
- ret |= ENCODE_DIRECT_JSON(&src->nodeId.namespaceIndex, UInt16);
- if(ret != UA_STATUSCODE_GOOD)
- return ret;
- } else {
- ret |= writeJsonKey(ctx, UA_JSONKEY_NAMESPACE);
- /* Check if Namespace given and in range */
- if(src->nodeId.namespaceIndex < ctx->namespacesSize
- && ctx->namespaces != NULL) {
- UA_String namespaceEntry = ctx->namespaces[src->nodeId.namespaceIndex];
- ret |= ENCODE_DIRECT_JSON(&namespaceEntry, String);
- if(ret != UA_STATUSCODE_GOOD)
- return ret;
- } else {
- return UA_STATUSCODE_BADNOTFOUND;
- }
- }
- }
- /* For the non-reversible encoding, this field is the ServerUri associated
- * with the ServerIndex portion of the ExpandedNodeId, encoded as a JSON
- * string. */
- /* Check if Namespace given and in range */
- if(src->serverIndex < ctx->serverUrisSize && ctx->serverUris != NULL) {
- UA_String serverUriEntry = ctx->serverUris[src->serverIndex];
- ret |= writeJsonKey(ctx, UA_JSONKEY_SERVERURI);
- ret |= ENCODE_DIRECT_JSON(&serverUriEntry, String);
- } else {
- return UA_STATUSCODE_BADNOTFOUND;
- }
- ret |= writeJsonObjEnd(ctx);
- return ret;
- }
- /* LocalizedText */
- ENCODE_JSON(LocalizedText) {
- if(ctx->useReversible) {
- status ret = writeJsonObjStart(ctx);
- ret |= writeJsonKey(ctx, UA_JSONKEY_LOCALE);
- ret |= ENCODE_DIRECT_JSON(&src->locale, String);
- ret |= writeJsonKey(ctx, UA_JSONKEY_TEXT);
- ret |= ENCODE_DIRECT_JSON(&src->text, String);
- ret |= writeJsonObjEnd(ctx);
- return ret;
- }
-
- /* For the non-reversible form, LocalizedText value shall be encoded as a
- * JSON string containing the Text component.*/
- return ENCODE_DIRECT_JSON(&src->text, String);
- }
- ENCODE_JSON(QualifiedName) {
- status ret = writeJsonObjStart(ctx);
- ret |= writeJsonKey(ctx, UA_JSONKEY_NAME);
- ret |= ENCODE_DIRECT_JSON(&src->name, String);
- if(ctx->useReversible) {
- if(src->namespaceIndex != 0) {
- ret |= writeJsonKey(ctx, UA_JSONKEY_URI);
- ret |= ENCODE_DIRECT_JSON(&src->namespaceIndex, UInt16);
- }
- } else {
- /* For the non-reversible form, the NamespaceUri associated with the
- * NamespaceIndex portion of the QualifiedName is encoded as JSON string
- * unless the NamespaceIndex is 1 or if NamespaceUri is unknown. In
- * these cases, the NamespaceIndex is encoded as a JSON number. */
- if(src->namespaceIndex == 1) {
- ret |= writeJsonKey(ctx, UA_JSONKEY_URI);
- ret |= ENCODE_DIRECT_JSON(&src->namespaceIndex, UInt16);
- } else {
- ret |= writeJsonKey(ctx, UA_JSONKEY_URI);
- /* Check if Namespace given and in range */
- if(src->namespaceIndex < ctx->namespacesSize && ctx->namespaces != NULL) {
- UA_String namespaceEntry = ctx->namespaces[src->namespaceIndex];
- ret |= ENCODE_DIRECT_JSON(&namespaceEntry, String);
- } else {
- /* If not encode as number */
- ret |= ENCODE_DIRECT_JSON(&src->namespaceIndex, UInt16);
- }
- }
- }
- return ret | writeJsonObjEnd(ctx);
- }
- ENCODE_JSON(StatusCode) {
- if(!src)
- return writeJsonNull(ctx);
- if(ctx->useReversible)
- return ENCODE_DIRECT_JSON(src, UInt32);
- if(*src == UA_STATUSCODE_GOOD)
- return writeJsonNull(ctx);
- status ret = UA_STATUSCODE_GOOD;
- ret |= writeJsonObjStart(ctx);
- ret |= writeJsonKey(ctx, UA_JSONKEY_CODE);
- ret |= ENCODE_DIRECT_JSON(src, UInt32);
- ret |= writeJsonKey(ctx, UA_JSONKEY_SYMBOL);
- const char *codename = UA_StatusCode_name(*src);
- UA_String statusDescription = UA_STRING((char*)(uintptr_t)codename);
- ret |= ENCODE_DIRECT_JSON(&statusDescription, String);
- ret |= writeJsonObjEnd(ctx);
- return ret;
- }
- /* ExtensionObject */
- ENCODE_JSON(ExtensionObject) {
- u8 encoding = (u8) src->encoding;
- if(encoding == UA_EXTENSIONOBJECT_ENCODED_NOBODY)
- return writeJsonNull(ctx);
-
- status ret = UA_STATUSCODE_GOOD;
- /* already encoded content.*/
- if(encoding <= UA_EXTENSIONOBJECT_ENCODED_XML) {
- ret |= writeJsonObjStart(ctx);
- if(ctx->useReversible) {
- ret |= writeJsonKey(ctx, UA_JSONKEY_TYPEID);
- ret |= ENCODE_DIRECT_JSON(&src->content.encoded.typeId, NodeId);
- if(ret != UA_STATUSCODE_GOOD)
- return ret;
- }
-
- switch (src->encoding) {
- case UA_EXTENSIONOBJECT_ENCODED_BYTESTRING:
- {
- if(ctx->useReversible) {
- ret |= writeJsonKey(ctx, UA_JSONKEY_ENCODING);
- ret |= writeChar(ctx, '1');
- }
- ret |= writeJsonKey(ctx, UA_JSONKEY_BODY);
- ret |= ENCODE_DIRECT_JSON(&src->content.encoded.body, String);
- break;
- }
- case UA_EXTENSIONOBJECT_ENCODED_XML:
- {
- if(ctx->useReversible) {
- ret |= writeJsonKey(ctx, UA_JSONKEY_ENCODING);
- ret |= writeChar(ctx, '2');
- }
- ret |= writeJsonKey(ctx, UA_JSONKEY_BODY);
- ret |= ENCODE_DIRECT_JSON(&src->content.encoded.body, String);
- break;
- }
- default:
- ret = UA_STATUSCODE_BADINTERNALERROR;
- }
- ret |= writeJsonObjEnd(ctx);
- return ret;
- } /* encoding <= UA_EXTENSIONOBJECT_ENCODED_XML */
-
- /* Cannot encode with no type description */
- if(!src->content.decoded.type)
- return UA_STATUSCODE_BADENCODINGERROR;
- if(!src->content.decoded.data)
- return writeJsonNull(ctx);
- UA_NodeId typeId = src->content.decoded.type->typeId;
- if(typeId.identifierType != UA_NODEIDTYPE_NUMERIC)
- return UA_STATUSCODE_BADENCODINGERROR;
- ret |= writeJsonObjStart(ctx);
- const UA_DataType *contentType = src->content.decoded.type;
- if(ctx->useReversible) {
- /* REVERSIBLE */
- ret |= writeJsonKey(ctx, UA_JSONKEY_TYPEID);
- ret |= ENCODE_DIRECT_JSON(&typeId, NodeId);
- /* Encode the content */
- ret |= writeJsonKey(ctx, UA_JSONKEY_BODY);
- ret |= encodeJsonInternal(src->content.decoded.data, contentType, ctx);
- } else {
- /* NON-REVERSIBLE
- * For the non-reversible form, ExtensionObject values
- * shall be encoded as a JSON object containing only the
- * value of the Body field. The TypeId and Encoding fields are dropped.
- *
- * TODO: UA_JSONKEY_BODY key in the ExtensionObject?
- */
- ret |= writeJsonKey(ctx, UA_JSONKEY_BODY);
- ret |= encodeJsonInternal(src->content.decoded.data, contentType, ctx);
- }
- ret |= writeJsonObjEnd(ctx);
- return ret;
- }
- static status
- Variant_encodeJsonWrapExtensionObject(const UA_Variant *src, const bool isArray, CtxJson *ctx) {
- size_t length = 1;
- status ret = UA_STATUSCODE_GOOD;
- if(isArray) {
- if(src->arrayLength > UA_INT32_MAX)
- return UA_STATUSCODE_BADENCODINGERROR;
-
- length = src->arrayLength;
- }
- /* Set up the ExtensionObject */
- UA_ExtensionObject eo;
- UA_ExtensionObject_init(&eo);
- eo.encoding = UA_EXTENSIONOBJECT_DECODED;
- eo.content.decoded.type = src->type;
- const u16 memSize = src->type->memSize;
- uintptr_t ptr = (uintptr_t) src->data;
- if(isArray) {
- ret |= writeJsonArrStart(ctx);
- ctx->commaNeeded[ctx->depth] = false;
- /* Iterate over the array */
- for(size_t i = 0; i < length && ret == UA_STATUSCODE_GOOD; ++i) {
- eo.content.decoded.data = (void*) ptr;
- ret |= writeJsonArrElm(ctx, &eo, &UA_TYPES[UA_TYPES_EXTENSIONOBJECT]);
- ptr += memSize;
- }
-
- ret |= writeJsonArrEnd(ctx);
- return ret;
- }
- eo.content.decoded.data = (void*) ptr;
- return encodeJsonInternal(&eo, &UA_TYPES[UA_TYPES_EXTENSIONOBJECT], ctx);
- }
- static status
- addMultiArrayContentJSON(CtxJson *ctx, void* array, const UA_DataType *type,
- size_t *index, UA_UInt32 *arrayDimensions, size_t dimensionIndex,
- size_t dimensionSize) {
- /* Check the recursion limit */
- if(ctx->depth > UA_JSON_ENCODING_MAX_RECURSION)
- return UA_STATUSCODE_BADENCODINGERROR;
-
- /* Stop recursion: The inner Arrays are written */
- status ret;
- if(dimensionIndex == (dimensionSize - 1)) {
- ret = encodeJsonArray(ctx, ((u8*)array) + (type->memSize * *index),
- arrayDimensions[dimensionIndex], type);
- (*index) += arrayDimensions[dimensionIndex];
- return ret;
- }
- /* Recurse to the next dimension */
- ret = writeJsonArrStart(ctx);
- for(size_t i = 0; i < arrayDimensions[dimensionIndex]; i++) {
- ret |= writeJsonCommaIfNeeded(ctx);
- ret |= addMultiArrayContentJSON(ctx, array, type, index, arrayDimensions,
- dimensionIndex + 1, dimensionSize);
- ctx->commaNeeded[ctx->depth] = true;
- if(ret != UA_STATUSCODE_GOOD)
- return ret;
- }
- ret |= writeJsonArrEnd(ctx);
- return ret;
- }
- ENCODE_JSON(Variant) {
- /* If type is 0 (NULL) the Variant contains a NULL value and the containing
- * JSON object shall be omitted or replaced by the JSON literal ‘null’ (when
- * an element of a JSON array). */
- if(!src->type) {
- return writeJsonNull(ctx);
- }
-
- /* Set the content type in the encoding mask */
- const UA_Boolean isBuiltin = (src->type->typeKind <= UA_DATATYPEKIND_DIAGNOSTICINFO);
- const UA_Boolean isEnum = (src->type->typeKind == UA_DATATYPEKIND_ENUM);
-
- /* Set the array type in the encoding mask */
- const bool isArray = src->arrayLength > 0 || src->data <= UA_EMPTY_ARRAY_SENTINEL;
- const bool hasDimensions = isArray && src->arrayDimensionsSize > 0;
- status ret = UA_STATUSCODE_GOOD;
-
- if(ctx->useReversible) {
- ret |= writeJsonObjStart(ctx);
- if(ret != UA_STATUSCODE_GOOD)
- return ret;
- /* Encode the content */
- if(!isBuiltin && !isEnum) {
- /* REVERSIBLE: NOT BUILTIN, can it be encoded? Wrap in extension object.*/
- ret |= writeJsonKey(ctx, UA_JSONKEY_TYPE);
- ret |= ENCODE_DIRECT_JSON(&UA_TYPES[UA_TYPES_EXTENSIONOBJECT].typeId.identifier.numeric, UInt32);
- ret |= writeJsonKey(ctx, UA_JSONKEY_BODY);
- ret |= Variant_encodeJsonWrapExtensionObject(src, isArray, ctx);
- } else if(!isArray) {
- /*REVERSIBLE: BUILTIN, single value.*/
- ret |= writeJsonKey(ctx, UA_JSONKEY_TYPE);
- ret |= ENCODE_DIRECT_JSON(&src->type->typeId.identifier.numeric, UInt32);
- ret |= writeJsonKey(ctx, UA_JSONKEY_BODY);
- ret |= encodeJsonInternal(src->data, src->type, ctx);
- } else {
- /*REVERSIBLE: BUILTIN, array.*/
- ret |= writeJsonKey(ctx, UA_JSONKEY_TYPE);
- ret |= ENCODE_DIRECT_JSON(&src->type->typeId.identifier.numeric, UInt32);
- ret |= writeJsonKey(ctx, UA_JSONKEY_BODY);
- ret |= encodeJsonArray(ctx, src->data, src->arrayLength, src->type);
- }
-
- if(ret != UA_STATUSCODE_GOOD)
- return ret;
-
- /* REVERSIBLE: Encode the array dimensions */
- if(hasDimensions && ret == UA_STATUSCODE_GOOD) {
- ret |= writeJsonKey(ctx, UA_JSONKEY_DIMENSION);
- ret |= encodeJsonArray(ctx, src->arrayDimensions, src->arrayDimensionsSize,
- &UA_TYPES[UA_TYPES_INT32]);
- if(ret != UA_STATUSCODE_GOOD)
- return ret;
- }
- ret |= writeJsonObjEnd(ctx);
- return ret;
- } /* reversible */
-
- /* NON-REVERSIBLE
- * For the non-reversible form, Variant values shall be encoded as a JSON object containing only
- * the value of the Body field. The Type and Dimensions fields are dropped. Multi-dimensional
- * arrays are encoded as a multi dimensional JSON array as described in 5.4.5.
- */
- ret |= writeJsonObjStart(ctx);
- if(!isBuiltin && !isEnum) {
- /*NON REVERSIBLE: NOT BUILTIN, can it be encoded? Wrap in extension object.*/
- if(src->arrayDimensionsSize > 1) {
- return UA_STATUSCODE_BADNOTIMPLEMENTED;
- }
- ret |= writeJsonKey(ctx, UA_JSONKEY_BODY);
- ret |= Variant_encodeJsonWrapExtensionObject(src, isArray, ctx);
- } else if(!isArray) {
- /*NON REVERSIBLE: BUILTIN, single value.*/
- ret |= writeJsonKey(ctx, UA_JSONKEY_BODY);
- ret |= encodeJsonInternal(src->data, src->type, ctx);
- } else {
- /*NON REVERSIBLE: BUILTIN, array.*/
- ret |= writeJsonKey(ctx, UA_JSONKEY_BODY);
- size_t dimensionSize = src->arrayDimensionsSize;
- if(dimensionSize > 1) {
- /*nonreversible multidimensional array*/
- size_t index = 0; size_t dimensionIndex = 0;
- void *ptr = src->data;
- const UA_DataType *arraytype = src->type;
- ret |= addMultiArrayContentJSON(ctx, ptr, arraytype, &index,
- src->arrayDimensions, dimensionIndex, dimensionSize);
- } else {
- /*nonreversible simple array*/
- ret |= encodeJsonArray(ctx, src->data, src->arrayLength, src->type);
- }
- }
- ret |= writeJsonObjEnd(ctx);
- return ret;
- }
- /* DataValue */
- ENCODE_JSON(DataValue) {
- if(!src->hasServerPicoseconds && !src->hasServerTimestamp &&
- !src->hasSourcePicoseconds && !src->hasSourceTimestamp &&
- !src->hasStatus && !src->hasValue) {
- return writeJsonNull(ctx); /*no element, encode as null*/
- }
-
- status ret = UA_STATUSCODE_GOOD;
- ret |= writeJsonObjStart(ctx);
- if(src->hasValue) {
- ret |= writeJsonKey(ctx, UA_JSONKEY_VALUE);
- ret |= ENCODE_DIRECT_JSON(&src->value, Variant);
- if(ret != UA_STATUSCODE_GOOD)
- return ret;
- }
- if(src->hasStatus) {
- ret |= writeJsonKey(ctx, UA_JSONKEY_STATUS);
- ret |= ENCODE_DIRECT_JSON(&src->status, StatusCode);
- if(ret != UA_STATUSCODE_GOOD)
- return ret;
- }
-
- if(src->hasSourceTimestamp) {
- ret |= writeJsonKey(ctx, UA_JSONKEY_SOURCETIMESTAMP);
- ret |= ENCODE_DIRECT_JSON(&src->sourceTimestamp, DateTime);
- if(ret != UA_STATUSCODE_GOOD)
- return ret;
- }
-
- if(src->hasSourcePicoseconds) {
- ret |= writeJsonKey(ctx, UA_JSONKEY_SOURCEPICOSECONDS);
- ret |= ENCODE_DIRECT_JSON(&src->sourcePicoseconds, UInt16);
- if(ret != UA_STATUSCODE_GOOD)
- return ret;
- }
-
- if(src->hasServerTimestamp) {
- ret |= writeJsonKey(ctx, UA_JSONKEY_SERVERTIMESTAMP);
- ret |= ENCODE_DIRECT_JSON(&src->serverTimestamp, DateTime);
- if(ret != UA_STATUSCODE_GOOD)
- return ret;
- }
-
- if(src->hasServerPicoseconds) {
- ret |= writeJsonKey(ctx, UA_JSONKEY_SERVERPICOSECONDS);
- ret |= ENCODE_DIRECT_JSON(&src->serverPicoseconds, UInt16);
- if(ret != UA_STATUSCODE_GOOD)
- return ret;
- }
- ret |= writeJsonObjEnd(ctx);
- return ret;
- }
- /* DiagnosticInfo */
- ENCODE_JSON(DiagnosticInfo) {
- status ret = UA_STATUSCODE_GOOD;
- if(!src->hasSymbolicId && !src->hasNamespaceUri && !src->hasLocalizedText &&
- !src->hasLocale && !src->hasAdditionalInfo && !src->hasInnerDiagnosticInfo &&
- !src->hasInnerStatusCode) {
- return writeJsonNull(ctx); /*no element present, encode as null.*/
- }
-
- ret |= writeJsonObjStart(ctx);
-
- if(src->hasSymbolicId) {
- ret |= writeJsonKey(ctx, UA_JSONKEY_SYMBOLICID);
- ret |= ENCODE_DIRECT_JSON(&src->symbolicId, UInt32);
- if(ret != UA_STATUSCODE_GOOD)
- return ret;
- }
- if(src->hasNamespaceUri) {
- ret |= writeJsonKey(ctx, UA_JSONKEY_NAMESPACEURI);
- ret |= ENCODE_DIRECT_JSON(&src->namespaceUri, UInt32);
- if(ret != UA_STATUSCODE_GOOD)
- return ret;
- }
-
- if(src->hasLocalizedText) {
- ret |= writeJsonKey(ctx, UA_JSONKEY_LOCALIZEDTEXT);
- ret |= ENCODE_DIRECT_JSON(&src->localizedText, UInt32);
- if(ret != UA_STATUSCODE_GOOD)
- return ret;
- }
-
- if(src->hasLocale) {
- ret |= writeJsonKey(ctx, UA_JSONKEY_LOCALE);
- ret |= ENCODE_DIRECT_JSON(&src->locale, UInt32);
- if(ret != UA_STATUSCODE_GOOD)
- return ret;
- }
-
- if(src->hasAdditionalInfo) {
- ret |= writeJsonKey(ctx, UA_JSONKEY_ADDITIONALINFO);
- ret |= ENCODE_DIRECT_JSON(&src->additionalInfo, String);
- if(ret != UA_STATUSCODE_GOOD)
- return ret;
- }
- if(src->hasInnerStatusCode) {
- ret |= writeJsonKey(ctx, UA_JSONKEY_INNERSTATUSCODE);
- ret |= ENCODE_DIRECT_JSON(&src->innerStatusCode, StatusCode);
- if(ret != UA_STATUSCODE_GOOD)
- return ret;
- }
- if(src->hasInnerDiagnosticInfo && src->innerDiagnosticInfo) {
- ret |= writeJsonKey(ctx, UA_JSONKEY_INNERDIAGNOSTICINFO);
- /* Check recursion depth in encodeJsonInternal */
- ret |= encodeJsonInternal(src->innerDiagnosticInfo, &UA_TYPES[UA_TYPES_DIAGNOSTICINFO], ctx);
- if(ret != UA_STATUSCODE_GOOD)
- return ret;
- }
- ret |= writeJsonObjEnd(ctx);
- return ret;
- }
- static status
- encodeJsonStructure(const void *src, const UA_DataType *type, CtxJson *ctx) {
- /* Check the recursion limit */
- if(ctx->depth > UA_JSON_ENCODING_MAX_RECURSION)
- return UA_STATUSCODE_BADENCODINGERROR;
- ctx->depth++;
- status ret = writeJsonObjStart(ctx);
- uintptr_t ptr = (uintptr_t) src;
- u8 membersSize = type->membersSize;
- const UA_DataType * typelists[2] = {UA_TYPES, &type[-type->typeIndex]};
- for(size_t i = 0; i < membersSize && ret == UA_STATUSCODE_GOOD; ++i) {
- const UA_DataTypeMember *m = &type->members[i];
- const UA_DataType *mt = &typelists[!m->namespaceZero][m->memberTypeIndex];
- if(m->memberName != NULL && *m->memberName != 0)
- ret |= writeJsonKey(ctx, m->memberName);
- if(!m->isArray) {
- ptr += m->padding;
- size_t memSize = mt->memSize;
- ret = encodeJsonJumpTable[mt->typeKind]((const void*) ptr, mt, ctx);
- ptr += memSize;
- } else {
- ptr += m->padding;
- const size_t length = *((const size_t*) ptr);
- ptr += sizeof (size_t);
- ret = encodeJsonArray(ctx, *(void * const *)ptr, length, mt);
- ptr += sizeof (void*);
- }
- }
- ret |= writeJsonObjEnd(ctx);
- ctx->depth--;
- return ret;
- }
- static status
- encodeJsonNotImplemented(const void *src, const UA_DataType *type, CtxJson *ctx) {
- return UA_STATUSCODE_BADNOTIMPLEMENTED;
- }
- const encodeJsonSignature encodeJsonJumpTable[UA_DATATYPEKINDS] = {
- (encodeJsonSignature)Boolean_encodeJson,
- (encodeJsonSignature)SByte_encodeJson, /* SByte */
- (encodeJsonSignature)Byte_encodeJson,
- (encodeJsonSignature)Int16_encodeJson, /* Int16 */
- (encodeJsonSignature)UInt16_encodeJson,
- (encodeJsonSignature)Int32_encodeJson, /* Int32 */
- (encodeJsonSignature)UInt32_encodeJson,
- (encodeJsonSignature)Int64_encodeJson, /* Int64 */
- (encodeJsonSignature)UInt64_encodeJson,
- (encodeJsonSignature)Float_encodeJson,
- (encodeJsonSignature)Double_encodeJson,
- (encodeJsonSignature)String_encodeJson,
- (encodeJsonSignature)DateTime_encodeJson, /* DateTime */
- (encodeJsonSignature)Guid_encodeJson,
- (encodeJsonSignature)ByteString_encodeJson, /* ByteString */
- (encodeJsonSignature)String_encodeJson, /* XmlElement */
- (encodeJsonSignature)NodeId_encodeJson,
- (encodeJsonSignature)ExpandedNodeId_encodeJson,
- (encodeJsonSignature)StatusCode_encodeJson, /* StatusCode */
- (encodeJsonSignature)QualifiedName_encodeJson, /* QualifiedName */
- (encodeJsonSignature)LocalizedText_encodeJson,
- (encodeJsonSignature)ExtensionObject_encodeJson,
- (encodeJsonSignature)DataValue_encodeJson,
- (encodeJsonSignature)Variant_encodeJson,
- (encodeJsonSignature)DiagnosticInfo_encodeJson,
- (encodeJsonSignature)encodeJsonNotImplemented, /* Decimal */
- (encodeJsonSignature)Int32_encodeJson, /* Enum */
- (encodeJsonSignature)encodeJsonStructure,
- (encodeJsonSignature)encodeJsonNotImplemented, /* Structure with optional fields */
- (encodeJsonSignature)encodeJsonNotImplemented, /* Union */
- (encodeJsonSignature)encodeJsonNotImplemented /* BitfieldCluster */
- };
- status
- encodeJsonInternal(const void *src, const UA_DataType *type, CtxJson *ctx) {
- return encodeJsonJumpTable[type->typeKind](src, type, ctx);
- }
- status UA_FUNC_ATTR_WARN_UNUSED_RESULT
- UA_encodeJson(const void *src, const UA_DataType *type,
- u8 **bufPos, const u8 **bufEnd, UA_String *namespaces,
- size_t namespaceSize, UA_String *serverUris,
- size_t serverUriSize, UA_Boolean useReversible) {
- if(!src || !type)
- return UA_STATUSCODE_BADINTERNALERROR;
-
- /* Set up the context */
- CtxJson ctx;
- memset(&ctx, 0, sizeof(ctx));
- ctx.pos = *bufPos;
- ctx.end = *bufEnd;
- ctx.depth = 0;
- ctx.namespaces = namespaces;
- ctx.namespacesSize = namespaceSize;
- ctx.serverUris = serverUris;
- ctx.serverUrisSize = serverUriSize;
- ctx.useReversible = useReversible;
- ctx.calcOnly = false;
-
- /* Encode */
- status ret = encodeJsonJumpTable[type->typeKind](src, type, &ctx);
-
- *bufPos = ctx.pos;
- *bufEnd = ctx.end;
- return ret;
- }
- /************/
- /* CalcSize */
- /************/
- size_t
- UA_calcSizeJson(const void *src, const UA_DataType *type,
- UA_String *namespaces, size_t namespaceSize,
- UA_String *serverUris, size_t serverUriSize,
- UA_Boolean useReversible) {
- if(!src || !type)
- return UA_STATUSCODE_BADINTERNALERROR;
- /* Set up the context */
- CtxJson ctx;
- memset(&ctx, 0, sizeof(ctx));
- ctx.pos = 0;
- ctx.end = (const UA_Byte*)(uintptr_t)SIZE_MAX;
- ctx.depth = 0;
- ctx.namespaces = namespaces;
- ctx.namespacesSize = namespaceSize;
- ctx.serverUris = serverUris;
- ctx.serverUrisSize = serverUriSize;
- ctx.useReversible = useReversible;
- ctx.calcOnly = true;
- /* Encode */
- status ret = encodeJsonJumpTable[type->typeKind](src, type, &ctx);
- if(ret != UA_STATUSCODE_GOOD)
- return 0;
- return (size_t)ctx.pos;
- }
- /**********/
- /* Decode */
- /**********/
- /* Macro which gets current size and char pointer of current Token. Needs
- * ParseCtx (parseCtx) and CtxJson (ctx). Does NOT increment index of Token. */
- #define GET_TOKEN \
- size_t tokenSize = (size_t)(parseCtx->tokenArray[parseCtx->index].end - parseCtx->tokenArray[parseCtx->index].start); \
- char* tokenData = (char*)(ctx->pos + parseCtx->tokenArray[parseCtx->index].start);
- #define CHECK_TOKEN_BOUNDS \
- if(parseCtx->index >= parseCtx->tokenCount) \
- return UA_STATUSCODE_BADDECODINGERROR;
- #define CHECK_PRIMITIVE \
- jsmntype_t tokenType = getJsmnType(parseCtx); \
- if(tokenType != JSMN_PRIMITIVE) { \
- return UA_STATUSCODE_BADDECODINGERROR; \
- }
- /* Forward declarations*/
- #define DECODE_JSON(TYPE) static status \
- TYPE##_decodeJson(UA_##TYPE *dst, const UA_DataType *type, \
- CtxJson *ctx, ParseCtx *parseCtx, UA_Boolean moveToken)
- /* decode without moving the token index */
- #define DECODE_DIRECT_JSON(DST, TYPE) TYPE##_decodeJson((UA_##TYPE*)DST, NULL, ctx, parseCtx, false)
- static status
- Array_decodeJson(void *dst, const UA_DataType *type, CtxJson *ctx,
- ParseCtx *parseCtx, UA_Boolean moveToken);
- static status
- Array_decodeJson_internal(void **dst, const UA_DataType *type,
- CtxJson *ctx, ParseCtx *parseCtx, UA_Boolean moveToken);
- static status
- Variant_decodeJsonUnwrapExtensionObject(UA_Variant *dst, const UA_DataType *type,
- CtxJson *ctx, ParseCtx *parseCtx, UA_Boolean moveToken);
- /* Json decode Helper */
- jsmntype_t
- getJsmnType(const ParseCtx *parseCtx) {
- if(parseCtx->index >= parseCtx->tokenCount)
- return JSMN_UNDEFINED;
- return parseCtx->tokenArray[parseCtx->index].type;
- }
- static UA_Boolean
- isJsonTokenNull(const CtxJson *ctx, jsmntok_t *token) {
- if(token->type != JSMN_PRIMITIVE)
- return false;
- char* elem = (char*)(ctx->pos + token->start);
- return (elem[0] == 'n' && elem[1] == 'u' && elem[2] == 'l' && elem[3] == 'l');
- }
- UA_Boolean
- isJsonNull(const CtxJson *ctx, const ParseCtx *parseCtx) {
- if(parseCtx->index >= parseCtx->tokenCount)
- return false;
- if(parseCtx->tokenArray[parseCtx->index].type != JSMN_PRIMITIVE) {
- return false;
- }
- char* elem = (char*)(ctx->pos + parseCtx->tokenArray[parseCtx->index].start);
- return (elem[0] == 'n' && elem[1] == 'u' && elem[2] == 'l' && elem[3] == 'l');
- }
- static UA_SByte jsoneq(const char *json, jsmntok_t *tok, const char *searchKey) {
- /* TODO: necessary?
- if(json == NULL
- || tok == NULL
- || searchKey == NULL) {
- return -1;
- } */
-
- if(tok->type == JSMN_STRING) {
- if(strlen(searchKey) == (size_t)(tok->end - tok->start) ) {
- if(strncmp(json + tok->start,
- (const char*)searchKey, (size_t)(tok->end - tok->start)) == 0) {
- return 0;
- }
- }
- }
- return -1;
- }
- DECODE_JSON(Boolean) {
- if(isJsonNull(ctx, parseCtx)) {
- /* Any value for a Built-In type that is NULL shall be encoded as the
- * JSON literal ‘null’ if the value is an element of an array. If the
- * NULL value is a field within a Structure or Union, the field shall
- * not be encoded. */
- parseCtx->index++;
- return UA_STATUSCODE_GOOD;
- }
- jsmntype_t tokenType = getJsmnType(parseCtx);
- if(tokenType != JSMN_PRIMITIVE)
- return UA_STATUSCODE_BADDECODINGERROR;
-
- CHECK_TOKEN_BOUNDS;
- GET_TOKEN;
- if(tokenSize == 4 &&
- tokenData[0] == 't' && tokenData[1] == 'r' &&
- tokenData[2] == 'u' && tokenData[3] == 'e') {
- *dst = true;
- } else if(tokenSize == 5 &&
- tokenData[0] == 'f' && tokenData[1] == 'a' &&
- tokenData[2] == 'l' && tokenData[3] == 's' &&
- tokenData[4] == 'e') {
- *dst = false;
- } else {
- return UA_STATUSCODE_BADDECODINGERROR;
- }
-
- if(moveToken)
- parseCtx->index++;
- return UA_STATUSCODE_GOOD;
- }
- #ifdef UA_ENABLE_CUSTOM_LIBC
- static UA_StatusCode
- parseUnsignedInteger(char* inputBuffer, size_t sizeOfBuffer,
- UA_UInt64 *destinationOfNumber) {
- UA_UInt64 d = 0;
- atoiUnsigned(inputBuffer, sizeOfBuffer, &d);
- if(!destinationOfNumber)
- return UA_STATUSCODE_BADDECODINGERROR;
- *destinationOfNumber = d;
- return UA_STATUSCODE_GOOD;
- }
- static UA_StatusCode
- parseSignedInteger(char* inputBuffer, size_t sizeOfBuffer,
- UA_Int64 *destinationOfNumber) {
- UA_Int64 d = 0;
- atoiSigned(inputBuffer, sizeOfBuffer, &d);
- if(!destinationOfNumber)
- return UA_STATUSCODE_BADDECODINGERROR;
- *destinationOfNumber = d;
- return UA_STATUSCODE_GOOD;
- }
- #else
- /* Safe strtol variant of unsigned string conversion.
- * Returns UA_STATUSCODE_BADDECODINGERROR in case of overflows.
- * Buffer limit is 20 digits. */
- static UA_StatusCode
- parseUnsignedInteger(char* inputBuffer, size_t sizeOfBuffer,
- UA_UInt64 *destinationOfNumber) {
- /* Check size to avoid huge malicious stack allocation.
- * No UInt64 can have more digits than 20. */
- if(sizeOfBuffer > 20) {
- return UA_STATUSCODE_BADDECODINGERROR;
- }
- /* convert to null terminated string */
- UA_STACKARRAY(char, string, sizeOfBuffer+1);
- memcpy(string, inputBuffer, sizeOfBuffer);
- string[sizeOfBuffer] = 0;
- /* Conversion */
- char *endptr, *str;
- str = string;
- errno = 0; /* To distinguish success/failure after call */
- UA_UInt64 val = strtoull(str, &endptr, 10);
- /* Check for various possible errors */
- if((errno == ERANGE && (val == LLONG_MAX || val == 0))
- || (errno != 0 )) {
- return UA_STATUSCODE_BADDECODINGERROR;
- }
- /* Check if no digits were found */
- if(endptr == str)
- return UA_STATUSCODE_BADDECODINGERROR;
- /* copy to destination */
- *destinationOfNumber = val;
- return UA_STATUSCODE_GOOD;
- }
- /* Safe strtol variant of unsigned string conversion.
- * Returns UA_STATUSCODE_BADDECODINGERROR in case of overflows.
- * Buffer limit is 20 digits. */
- static UA_StatusCode
- parseSignedInteger(char* inputBuffer, size_t sizeOfBuffer,
- UA_Int64 *destinationOfNumber) {
- /* Check size to avoid huge malicious stack allocation.
- * No UInt64 can have more digits than 20. */
- if(sizeOfBuffer > 20)
- return UA_STATUSCODE_BADDECODINGERROR;
- /* convert to null terminated string */
- NULL_TERMINATE(inputBuffer, sizeOfBuffer, string);
- /* Conversion */
- char *endptr, *str;
- str = string;
- errno = 0; /* To distinguish success/failure after call */
- UA_Int64 val = strtoll(str, &endptr, 10);
- /* Check for various possible errors */
- if((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN))
- || (errno != 0 )) {
- return UA_STATUSCODE_BADDECODINGERROR;
- }
- /* Check if no digits were found */
- if(endptr == str)
- return UA_STATUSCODE_BADDECODINGERROR;
- /* copy to destination */
- *destinationOfNumber = val;
- return UA_STATUSCODE_GOOD;
- }
- #endif
- DECODE_JSON(Byte) {
- CHECK_TOKEN_BOUNDS;
- CHECK_PRIMITIVE;
- GET_TOKEN;
- UA_UInt64 out = 0;
- UA_StatusCode s = parseUnsignedInteger(tokenData, tokenSize, &out);
- *dst = (UA_Byte)out;
- if(moveToken)
- parseCtx->index++;
- return s;
- }
- DECODE_JSON(UInt16) {
- CHECK_TOKEN_BOUNDS;
- CHECK_PRIMITIVE;
- GET_TOKEN;
- UA_UInt64 out = 0;
- UA_StatusCode s = parseUnsignedInteger(tokenData, tokenSize, &out);
- *dst = (UA_UInt16)out;
- if(moveToken)
- parseCtx->index++;
- return s;
- }
- DECODE_JSON(UInt32) {
- CHECK_TOKEN_BOUNDS;
- CHECK_PRIMITIVE;
- GET_TOKEN;
- UA_UInt64 out = 0;
- UA_StatusCode s = parseUnsignedInteger(tokenData, tokenSize, &out);
- *dst = (UA_UInt32)out;
- if(moveToken)
- parseCtx->index++;
- return s;
- }
- DECODE_JSON(UInt64) {
- CHECK_TOKEN_BOUNDS;
- CHECK_PRIMITIVE;
- GET_TOKEN;
- UA_UInt64 out = 0;
- UA_StatusCode s = parseUnsignedInteger(tokenData, tokenSize, &out);
- *dst = (UA_UInt64)out;
- if(moveToken)
- parseCtx->index++;
- return s;
- }
- DECODE_JSON(SByte) {
- CHECK_TOKEN_BOUNDS;
- CHECK_PRIMITIVE;
- GET_TOKEN;
- UA_Int64 out = 0;
- UA_StatusCode s = parseSignedInteger(tokenData, tokenSize, &out);
- *dst = (UA_SByte)out;
- if(moveToken)
- parseCtx->index++;
- return s;
- }
- DECODE_JSON(Int16) {
- CHECK_TOKEN_BOUNDS;
- CHECK_PRIMITIVE;
- GET_TOKEN;
- UA_Int64 out = 0;
- UA_StatusCode s = parseSignedInteger(tokenData, tokenSize, &out);
- *dst = (UA_Int16)out;
- if(moveToken)
- parseCtx->index++;
- return s;
- }
- DECODE_JSON(Int32) {
- CHECK_TOKEN_BOUNDS;
- CHECK_PRIMITIVE;
- GET_TOKEN;
- UA_Int64 out = 0;
- UA_StatusCode s = parseSignedInteger(tokenData, tokenSize, &out);
- *dst = (UA_Int32)out;
- if(moveToken)
- parseCtx->index++;
- return s;
- }
- DECODE_JSON(Int64) {
- CHECK_TOKEN_BOUNDS;
- CHECK_PRIMITIVE;
- GET_TOKEN;
- UA_Int64 out = 0;
- UA_StatusCode s = parseSignedInteger(tokenData, tokenSize, &out);
- *dst = (UA_Int64)out;
- if(moveToken)
- parseCtx->index++;
- return s;
- }
- static UA_UInt32 hex2int(char ch) {
- if(ch >= '0' && ch <= '9')
- return (UA_UInt32)(ch - '0');
- if(ch >= 'A' && ch <= 'F')
- return (UA_UInt32)(ch - 'A' + 10);
- if(ch >= 'a' && ch <= 'f')
- return (UA_UInt32)(ch - 'a' + 10);
- return 0;
- }
- /* Float
- * Either a JSMN_STRING or JSMN_PRIMITIVE
- */
- DECODE_JSON(Float) {
- CHECK_TOKEN_BOUNDS;
- GET_TOKEN;
-
- /* https://www.exploringbinary.com/maximum-number-of-decimal-digits-in-binary-floating-point-numbers/
- * Maximum digit counts for select IEEE floating-point formats: 149
- * Sanity check. */
- if(tokenSize > 150)
- return UA_STATUSCODE_BADDECODINGERROR;
- jsmntype_t tokenType = getJsmnType(parseCtx);
- if(tokenType == JSMN_STRING) {
- /*It could be a String with Nan, Infinity*/
- if(tokenSize == 8 && memcmp(tokenData, "Infinity", 8) == 0) {
- *dst = (UA_Float)INFINITY;
- return UA_STATUSCODE_GOOD;
- }
-
- if(tokenSize == 9 && memcmp(tokenData, "-Infinity", 9) == 0) {
- /* workaround an MSVC 2013 issue */
- *dst = (UA_Float)-INFINITY;
- return UA_STATUSCODE_GOOD;
- }
-
- if(tokenSize == 3 && memcmp(tokenData, "NaN", 3) == 0) {
- *dst = (UA_Float)NAN;
- return UA_STATUSCODE_GOOD;
- }
-
- if(tokenSize == 4 && memcmp(tokenData, "-NaN", 4) == 0) {
- *dst = (UA_Float)NAN;
- return UA_STATUSCODE_GOOD;
- }
- return UA_STATUSCODE_BADDECODINGERROR;
- }
-
- if(tokenType != JSMN_PRIMITIVE)
- return UA_STATUSCODE_BADDECODINGERROR;
- /* Null-Terminate for sscanf. */
- UA_STACKARRAY(char, string, tokenSize+1);
- memcpy(string, tokenData, tokenSize);
- string[tokenSize] = 0;
-
- UA_Float d = 0;
- #ifdef UA_ENABLE_CUSTOM_LIBC
- d = (UA_Float)__floatscan(string, 1, 0);
- #else
- char c = 0;
- /* On success, the function returns the number of variables filled.
- * In the case of an input failure before any data could be successfully read, EOF is returned. */
- int ret = sscanf(string, "%f%c", &d, &c);
- /* Exactly one var must be filled. %c acts as a guard for wrong input which is accepted by sscanf.
- E.g. 1.23.45 is not accepted. */
- if(ret == EOF || (ret != 1))
- return UA_STATUSCODE_BADDECODINGERROR;
- #endif
-
- *dst = d;
- parseCtx->index++;
- return UA_STATUSCODE_GOOD;
- }
- /* Either a JSMN_STRING or JSMN_PRIMITIVE */
- DECODE_JSON(Double) {
- CHECK_TOKEN_BOUNDS;
- GET_TOKEN;
-
- /* https://www.exploringbinary.com/maximum-number-of-decimal-digits-in-binary-floating-point-numbers/
- * Maximum digit counts for select IEEE floating-point formats: 1074
- * Sanity check.
- */
- if(tokenSize > 1075)
- return UA_STATUSCODE_BADDECODINGERROR;
- jsmntype_t tokenType = getJsmnType(parseCtx);
- if(tokenType == JSMN_STRING) {
- /*It could be a String with Nan, Infinity*/
- if(tokenSize == 8 && memcmp(tokenData, "Infinity", 8) == 0) {
- *dst = INFINITY;
- return UA_STATUSCODE_GOOD;
- }
-
- if(tokenSize == 9 && memcmp(tokenData, "-Infinity", 9) == 0) {
- /* workaround an MSVC 2013 issue */
- *dst = -INFINITY;
- return UA_STATUSCODE_GOOD;
- }
-
- if(tokenSize == 3 && memcmp(tokenData, "NaN", 3) == 0) {
- *dst = NAN;
- return UA_STATUSCODE_GOOD;
- }
-
- if(tokenSize == 4 && memcmp(tokenData, "-NaN", 4) == 0) {
- *dst = NAN;
- return UA_STATUSCODE_GOOD;
- }
- return UA_STATUSCODE_BADDECODINGERROR;
- }
-
- if(tokenType != JSMN_PRIMITIVE)
- return UA_STATUSCODE_BADDECODINGERROR;
- /* Null-Terminate for sscanf. Should this better be handled on heap? Max
- * 1075 input chars allowed. Not using heap. */
- UA_STACKARRAY(char, string, tokenSize+1);
- memcpy(string, tokenData, tokenSize);
- string[tokenSize] = 0;
-
- UA_Double d = 0;
- #ifdef UA_ENABLE_CUSTOM_LIBC
- d = (UA_Double)__floatscan(string, 2, 0);
- #else
- char c = 0;
- /* On success, the function returns the number of variables filled.
- * In the case of an input failure before any data could be successfully read, EOF is returned. */
- int ret = sscanf(string, "%lf%c", &d, &c);
- /* Exactly one var must be filled. %c acts as a guard for wrong input which is accepted by sscanf.
- E.g. 1.23.45 is not accepted. */
- if(ret == EOF || (ret != 1))
- return UA_STATUSCODE_BADDECODINGERROR;
- #endif
-
- *dst = d;
- parseCtx->index++;
- return UA_STATUSCODE_GOOD;
- }
- /*
- Expects 36 chars in format 00000003-0009-000A-0807-060504030201
- | data1| |d2| |d3| |d4| | data4 |
- */
- static UA_Guid UA_Guid_fromChars(const char* chars) {
- UA_Guid dst;
- UA_Guid_init(&dst);
- for(size_t i = 0; i < 8; i++)
- dst.data1 |= (UA_UInt32)(hex2int(chars[i]) << (28 - (i*4)));
- for(size_t i = 0; i < 4; i++) {
- dst.data2 |= (UA_UInt16)(hex2int(chars[9+i]) << (12 - (i*4)));
- dst.data3 |= (UA_UInt16)(hex2int(chars[14+i]) << (12 - (i*4)));
- }
- dst.data4[0] |= (UA_Byte)(hex2int(chars[19]) << 4);
- dst.data4[0] |= (UA_Byte)(hex2int(chars[20]) << 0);
- dst.data4[1] |= (UA_Byte)(hex2int(chars[21]) << 4);
- dst.data4[1] |= (UA_Byte)(hex2int(chars[22]) << 0);
- for(size_t i = 0; i < 6; i++) {
- dst.data4[2+i] |= (UA_Byte)(hex2int(chars[24 + i*2]) << 4);
- dst.data4[2+i] |= (UA_Byte)(hex2int(chars[25 + i*2]) << 0);
- }
- return dst;
- }
- DECODE_JSON(Guid) {
- CHECK_TOKEN_BOUNDS;
-
- if(isJsonNull(ctx, parseCtx)) {
- parseCtx->index++;
- return UA_STATUSCODE_GOOD;
- }
- jsmntype_t tokenType = getJsmnType(parseCtx);
- if(tokenType != JSMN_STRING && tokenType != JSMN_PRIMITIVE)
- return UA_STATUSCODE_BADDECODINGERROR;
-
- GET_TOKEN;
- if(tokenSize != 36)
- return UA_STATUSCODE_BADDECODINGERROR;
- /* check if incorrect chars are present */
- for(size_t i = 0; i < tokenSize; i++) {
- if(!(tokenData[i] == '-'
- || (tokenData[i] >= '0' && tokenData[i] <= '9')
- || (tokenData[i] >= 'A' && tokenData[i] <= 'F')
- || (tokenData[i] >= 'a' && tokenData[i] <= 'f'))) {
- return UA_STATUSCODE_BADDECODINGERROR;
- }
- }
- *dst = UA_Guid_fromChars(tokenData);
- if(moveToken)
- parseCtx->index++;
- return UA_STATUSCODE_GOOD;
- }
- DECODE_JSON(String) {
- if(isJsonNull(ctx, parseCtx)) {
- parseCtx->index++;
- return UA_STATUSCODE_GOOD;
- }
- jsmntype_t tokenType = getJsmnType(parseCtx);
- if(tokenType != JSMN_STRING)
- return UA_STATUSCODE_BADDECODINGERROR;
- GET_TOKEN;
- CHECK_TOKEN_BOUNDS;
- /* Empty string? */
- if(tokenSize == 0) {
- dst->data = (UA_Byte*)UA_EMPTY_ARRAY_SENTINEL;
- dst->length = 0;
- if(moveToken)
- parseCtx->index++;
- return UA_STATUSCODE_GOOD;
- }
-
- /* The actual value is at most of the same length as the source string:
- * - Shortcut escapes (e.g. "\t") (length 2) are converted to 1 byte
- * - A single \uXXXX escape (length 6) is converted to at most 3 bytes
- * - Two \uXXXX escapes (length 12) forming an UTF-16 surrogate pair are
- * converted to 4 bytes */
- char *outputBuffer = (char*)UA_malloc(tokenSize);
- if(!outputBuffer)
- return UA_STATUSCODE_BADOUTOFMEMORY;
-
- const char *p = (char*)tokenData;
- const char *end = (char*)&tokenData[tokenSize];
- char *pos = outputBuffer;
- while(p < end) {
- /* No escaping */
- if(*p != '\\') {
- *(pos++) = *(p++);
- continue;
- }
- /* Escape character */
- p++;
- if(p == end)
- goto cleanup;
-
- if(*p != 'u') {
- switch(*p) {
- case '"': case '\\': case '/': *pos = *p; break;
- case 'b': *pos = '\b'; break;
- case 'f': *pos = '\f'; break;
- case 'n': *pos = '\n'; break;
- case 'r': *pos = '\r'; break;
- case 't': *pos = '\t'; break;
- default: goto cleanup;
- }
- pos++;
- p++;
- continue;
- }
-
- /* Unicode */
- if(p + 4 >= end)
- goto cleanup;
- int32_t value = decode_unicode_escape(p);
- if(value < 0)
- goto cleanup;
- p += 5;
- if(0xD800 <= value && value <= 0xDBFF) {
- /* Surrogate pair */
- if(p + 5 >= end)
- goto cleanup;
- if(*p != '\\' || *(p + 1) != 'u')
- goto cleanup;
- int32_t value2 = decode_unicode_escape(p+1);
- if(value2 < 0xDC00 || value2 > 0xDFFF)
- goto cleanup;
- value = ((value - 0xD800) << 10) + (value2 - 0xDC00) + 0x10000;
- p += 6;
- } else if(0xDC00 <= value && value <= 0xDFFF) {
- /* Invalid Unicode '\\u%04X' */
- goto cleanup;
- }
- size_t length;
- if(utf8_encode(value, pos, &length))
- goto cleanup;
- pos += length;
- }
- dst->length = (size_t)(pos - outputBuffer);
- if(dst->length > 0) {
- dst->data = (UA_Byte*)outputBuffer;
- } else {
- dst->data = (UA_Byte*)UA_EMPTY_ARRAY_SENTINEL;
- UA_free(outputBuffer);
- }
- if(moveToken)
- parseCtx->index++;
- return UA_STATUSCODE_GOOD;
-
- cleanup:
- UA_free(outputBuffer);
- return UA_STATUSCODE_BADDECODINGERROR;
- }
- DECODE_JSON(ByteString) {
- jsmntype_t tokenType = getJsmnType(parseCtx);
- if(isJsonNull(ctx, parseCtx)) {
- parseCtx->index++;
- return UA_STATUSCODE_GOOD;
- }
- if(tokenType != JSMN_STRING && tokenType != JSMN_PRIMITIVE) {
- return UA_STATUSCODE_BADDECODINGERROR;
- }
-
- GET_TOKEN;
- /* Empty bytestring? */
- if(tokenSize == 0) {
- dst->data = (UA_Byte*)UA_EMPTY_ARRAY_SENTINEL;
- dst->length = 0;
- return UA_STATUSCODE_GOOD;
- }
- int flen;
- unsigned char* unB64 = UA_unbase64(tokenData, (int)tokenSize, &flen);
- if(unB64 == 0)
- return UA_STATUSCODE_BADDECODINGERROR;
- dst->data = (u8*)unB64;
- dst->length = (size_t)flen;
-
- if(moveToken)
- parseCtx->index++;
- return UA_STATUSCODE_GOOD;
- }
- DECODE_JSON(LocalizedText) {
- if(getJsmnType(parseCtx) != JSMN_OBJECT) {
- if(isJsonNull(ctx, parseCtx)) {
- parseCtx->index++;
- return UA_STATUSCODE_GOOD;
- }
- return UA_STATUSCODE_BADDECODINGERROR;
- }
-
- DecodeEntry entries[2] = {
- {UA_JSONKEY_LOCALE, &dst->locale, (decodeJsonSignature) String_decodeJson, false, NULL},
- {UA_JSONKEY_TEXT, &dst->text, (decodeJsonSignature) String_decodeJson, false, NULL}
- };
- return decodeFields(ctx, parseCtx, entries, 2, type);
- }
- DECODE_JSON(QualifiedName) {
- if(getJsmnType(parseCtx) != JSMN_OBJECT) {
- if(isJsonNull(ctx, parseCtx)) {
- parseCtx->index++;
- return UA_STATUSCODE_GOOD;
- }
- return UA_STATUSCODE_BADDECODINGERROR;
- }
-
- DecodeEntry entries[2] = {
- {UA_JSONKEY_NAME, &dst->name, (decodeJsonSignature) String_decodeJson, false, NULL},
- {UA_JSONKEY_URI, &dst->namespaceIndex, (decodeJsonSignature) UInt16_decodeJson, false, NULL}
- };
- return decodeFields(ctx, parseCtx, entries, 2, type);
- }
- /* Function for searching ahead of the current token. Used for retrieving the
- * OPC UA type of a token */
- static status
- searchObjectForKeyRec(const char *searchKey, CtxJson *ctx,
- ParseCtx *parseCtx, size_t *resultIndex, UA_UInt16 depth) {
- UA_StatusCode ret = UA_STATUSCODE_BADNOTFOUND;
-
- CHECK_TOKEN_BOUNDS;
-
- if(parseCtx->tokenArray[parseCtx->index].type == JSMN_OBJECT) {
- size_t objectCount = (size_t)(parseCtx->tokenArray[parseCtx->index].size);
-
- parseCtx->index++; /*Object to first Key*/
- CHECK_TOKEN_BOUNDS;
-
- size_t i;
- for(i = 0; i < objectCount; i++) {
-
- CHECK_TOKEN_BOUNDS;
- if(depth == 0) { /* we search only on first layer */
- if(jsoneq((char*)ctx->pos, &parseCtx->tokenArray[parseCtx->index], searchKey) == 0) {
- /*found*/
- parseCtx->index++; /*We give back a pointer to the value of the searched key!*/
- *resultIndex = parseCtx->index;
- ret = UA_STATUSCODE_GOOD;
- break;
- }
- }
-
- parseCtx->index++; /* value */
- CHECK_TOKEN_BOUNDS;
-
- if(parseCtx->tokenArray[parseCtx->index].type == JSMN_OBJECT) {
- ret = searchObjectForKeyRec(searchKey, ctx, parseCtx, resultIndex,
- (UA_UInt16)(depth + 1));
- } else if(parseCtx->tokenArray[parseCtx->index].type == JSMN_ARRAY) {
- ret = searchObjectForKeyRec(searchKey, ctx, parseCtx, resultIndex,
- (UA_UInt16)(depth + 1));
- } else {
- /* Only Primitive or string */
- parseCtx->index++;
- }
- }
- } else if(parseCtx->tokenArray[parseCtx->index].type == JSMN_ARRAY) {
- size_t arraySize = (size_t)(parseCtx->tokenArray[parseCtx->index].size);
-
- parseCtx->index++; /*Object to first element*/
- CHECK_TOKEN_BOUNDS;
-
- size_t i;
- for(i = 0; i < arraySize; i++) {
- if(parseCtx->tokenArray[parseCtx->index].type == JSMN_OBJECT) {
- ret = searchObjectForKeyRec(searchKey, ctx, parseCtx, resultIndex,
- (UA_UInt16)(depth + 1));
- } else if(parseCtx->tokenArray[parseCtx->index].type == JSMN_ARRAY) {
- ret = searchObjectForKeyRec(searchKey, ctx, parseCtx, resultIndex,
- (UA_UInt16)(depth + 1));
- } else {
- /* Only Primitive or string */
- parseCtx->index++;
- }
- }
- }
- return ret;
- }
- UA_FUNC_ATTR_WARN_UNUSED_RESULT status
- lookAheadForKey(const char* search, CtxJson *ctx,
- ParseCtx *parseCtx, size_t *resultIndex) {
- UA_UInt16 oldIndex = parseCtx->index; /* Save index for later restore */
-
- UA_UInt16 depth = 0;
- UA_StatusCode ret = searchObjectForKeyRec(search, ctx, parseCtx, resultIndex, depth);
- parseCtx->index = oldIndex; /* Restore index */
- return ret;
- }
- /* Function used to jump over an object which cannot be parsed */
- static status
- jumpOverRec(CtxJson *ctx, ParseCtx *parseCtx,
- size_t *resultIndex, UA_UInt16 depth) {
- UA_StatusCode ret = UA_STATUSCODE_BADDECODINGERROR;
- CHECK_TOKEN_BOUNDS;
-
- if(parseCtx->tokenArray[parseCtx->index].type == JSMN_OBJECT) {
- size_t objectCount = (size_t)(parseCtx->tokenArray[parseCtx->index].size);
-
- parseCtx->index++; /*Object to first Key*/
- CHECK_TOKEN_BOUNDS;
-
- size_t i;
- for(i = 0; i < objectCount; i++) {
- CHECK_TOKEN_BOUNDS;
-
- parseCtx->index++; /*value*/
- CHECK_TOKEN_BOUNDS;
-
- if(parseCtx->tokenArray[parseCtx->index].type == JSMN_OBJECT) {
- jumpOverRec(ctx, parseCtx, resultIndex, (UA_UInt16)(depth + 1));
- } else if(parseCtx->tokenArray[parseCtx->index].type == JSMN_ARRAY) {
- jumpOverRec(ctx, parseCtx, resultIndex, (UA_UInt16)(depth + 1));
- } else {
- /*Only Primitive or string*/
- parseCtx->index++;
- }
- }
- } else if(parseCtx->tokenArray[parseCtx->index].type == JSMN_ARRAY) {
- size_t arraySize = (size_t)(parseCtx->tokenArray[parseCtx->index].size);
-
- parseCtx->index++; /*Object to first element*/
- CHECK_TOKEN_BOUNDS;
-
- size_t i;
- for(i = 0; i < arraySize; i++) {
- if(parseCtx->tokenArray[parseCtx->index].type == JSMN_OBJECT) {
- jumpOverRec(ctx, parseCtx, resultIndex, (UA_UInt16)(depth + 1));
- } else if(parseCtx->tokenArray[parseCtx->index].type == JSMN_ARRAY) {
- jumpOverRec(ctx, parseCtx, resultIndex, (UA_UInt16)(depth + 1));
- } else {
- /*Only Primitive or string*/
- parseCtx->index++;
- }
- }
- }
- return ret;
- }
- static status
- jumpOverObject(CtxJson *ctx, ParseCtx *parseCtx, size_t *resultIndex) {
- UA_UInt16 oldIndex = parseCtx->index; /* Save index for later restore */
- UA_UInt16 depth = 0;
- jumpOverRec(ctx, parseCtx, resultIndex, depth);
- *resultIndex = parseCtx->index;
- parseCtx->index = oldIndex; /* Restore index */
- return UA_STATUSCODE_GOOD;
- }
- static status
- prepareDecodeNodeIdJson(UA_NodeId *dst, CtxJson *ctx, ParseCtx *parseCtx,
- u8 *fieldCount, DecodeEntry *entries) {
- /* possible keys: Id, IdType*/
- /* Id must always be present */
- entries[*fieldCount].fieldName = UA_JSONKEY_ID;
- entries[*fieldCount].found = false;
-
- /* IdType */
- UA_Boolean hasIdType = false;
- size_t searchResult = 0;
- status ret = lookAheadForKey(UA_JSONKEY_IDTYPE, ctx, parseCtx, &searchResult);
- if(ret == UA_STATUSCODE_GOOD) { /*found*/
- hasIdType = true;
- }
-
- if(hasIdType) {
- size_t size = (size_t)(parseCtx->tokenArray[searchResult].end -
- parseCtx->tokenArray[searchResult].start);
- if(size < 1) {
- return UA_STATUSCODE_BADDECODINGERROR;
- }
- char *idType = (char*)(ctx->pos + parseCtx->tokenArray[searchResult].start);
-
- if(idType[0] == '2') {
- dst->identifierType = UA_NODEIDTYPE_GUID;
- entries[*fieldCount].fieldPointer = &dst->identifier.guid;
- entries[*fieldCount].function = (decodeJsonSignature) Guid_decodeJson;
- } else if(idType[0] == '1') {
- dst->identifierType = UA_NODEIDTYPE_STRING;
- entries[*fieldCount].fieldPointer = &dst->identifier.string;
- entries[*fieldCount].function = (decodeJsonSignature) String_decodeJson;
- } else if(idType[0] == '3') {
- dst->identifierType = UA_NODEIDTYPE_BYTESTRING;
- entries[*fieldCount].fieldPointer = &dst->identifier.byteString;
- entries[*fieldCount].function = (decodeJsonSignature) ByteString_decodeJson;
- } else {
- return UA_STATUSCODE_BADDECODINGERROR;
- }
-
- /* Id always present */
- (*fieldCount)++;
-
- entries[*fieldCount].fieldName = UA_JSONKEY_IDTYPE;
- entries[*fieldCount].fieldPointer = NULL;
- entries[*fieldCount].function = NULL;
- entries[*fieldCount].found = false;
-
- /* IdType */
- (*fieldCount)++;
- } else {
- dst->identifierType = UA_NODEIDTYPE_NUMERIC;
- entries[*fieldCount].fieldPointer = &dst->identifier.numeric;
- entries[*fieldCount].function = (decodeJsonSignature) UInt32_decodeJson;
- (*fieldCount)++;
- }
-
- return UA_STATUSCODE_GOOD;
- }
- DECODE_JSON(NodeId) {
- if(isJsonNull(ctx, parseCtx)) {
- parseCtx->index++;
- return UA_STATUSCODE_GOOD;
- }
-
- if(getJsmnType(parseCtx) != JSMN_OBJECT)
- return UA_STATUSCODE_BADDECODINGERROR;
-
- /* NameSpace */
- UA_Boolean hasNamespace = false;
- size_t searchResultNamespace = 0;
- status ret = lookAheadForKey(UA_JSONKEY_NAMESPACE, ctx, parseCtx, &searchResultNamespace);
- if(ret != UA_STATUSCODE_GOOD) {
- dst->namespaceIndex = 0;
- } else {
- hasNamespace = true;
- }
-
- /* Keep track over number of keys present, incremented if key found */
- u8 fieldCount = 0;
- DecodeEntry entries[3];
- ret = prepareDecodeNodeIdJson(dst, ctx, parseCtx, &fieldCount, entries);
- if(ret != UA_STATUSCODE_GOOD)
- return ret;
- if(hasNamespace) {
- entries[fieldCount].fieldName = UA_JSONKEY_NAMESPACE;
- entries[fieldCount].fieldPointer = &dst->namespaceIndex;
- entries[fieldCount].function = (decodeJsonSignature) UInt16_decodeJson;
- entries[fieldCount].found = false;
- fieldCount++;
- } else {
- dst->namespaceIndex = 0;
- }
- ret = decodeFields(ctx, parseCtx, entries, fieldCount, type);
- return ret;
- }
- DECODE_JSON(ExpandedNodeId) {
- if(isJsonNull(ctx, parseCtx)) {
- parseCtx->index++;
- return UA_STATUSCODE_GOOD;
- }
-
- if(getJsmnType(parseCtx) != JSMN_OBJECT)
- return UA_STATUSCODE_BADDECODINGERROR;
-
- /* Keep track over number of keys present, incremented if key found */
- u8 fieldCount = 0;
-
- /* ServerUri */
- UA_Boolean hasServerUri = false;
- size_t searchResultServerUri = 0;
- status ret = lookAheadForKey(UA_JSONKEY_SERVERURI, ctx, parseCtx, &searchResultServerUri);
- if(ret != UA_STATUSCODE_GOOD) {
- dst->serverIndex = 0;
- } else {
- hasServerUri = true;
- }
-
- /* NameSpace */
- UA_Boolean hasNamespace = false;
- UA_Boolean isNamespaceString = false;
- size_t searchResultNamespace = 0;
- ret = lookAheadForKey(UA_JSONKEY_NAMESPACE, ctx, parseCtx, &searchResultNamespace);
- if(ret != UA_STATUSCODE_GOOD) {
- dst->namespaceUri = UA_STRING_NULL;
- } else {
- hasNamespace = true;
- jsmntok_t nsToken = parseCtx->tokenArray[searchResultNamespace];
- if(nsToken.type == JSMN_STRING)
- isNamespaceString = true;
- }
- DecodeEntry entries[4];
- ret = prepareDecodeNodeIdJson(&dst->nodeId, ctx, parseCtx, &fieldCount, entries);
- if(ret != UA_STATUSCODE_GOOD)
- return ret;
- if(hasNamespace) {
- entries[fieldCount].fieldName = UA_JSONKEY_NAMESPACE;
- if(isNamespaceString) {
- entries[fieldCount].fieldPointer = &dst->namespaceUri;
- entries[fieldCount].function = (decodeJsonSignature) String_decodeJson;
- } else {
- entries[fieldCount].fieldPointer = &dst->nodeId.namespaceIndex;
- entries[fieldCount].function = (decodeJsonSignature) UInt16_decodeJson;
- }
- entries[fieldCount].found = false;
- fieldCount++;
- }
-
- if(hasServerUri) {
- entries[fieldCount].fieldName = UA_JSONKEY_SERVERURI;
- entries[fieldCount].fieldPointer = &dst->serverIndex;
- entries[fieldCount].function = (decodeJsonSignature) UInt32_decodeJson;
- entries[fieldCount].found = false;
- fieldCount++;
- } else {
- dst->serverIndex = 0;
- }
-
- return decodeFields(ctx, parseCtx, entries, fieldCount, type);
- }
- DECODE_JSON(DateTime) {
- if(isJsonNull(ctx, parseCtx)) {
- parseCtx->index++;
- return UA_STATUSCODE_GOOD;
- }
-
- if(getJsmnType(parseCtx) != JSMN_STRING)
- return UA_STATUSCODE_BADDECODINGERROR;
-
- GET_TOKEN;
-
- /* TODO: proper ISO 8601:2004 parsing, musl strptime!*/
- /* DateTime ISO 8601:2004 without milli is 20 Characters, with millis 24 */
- if(tokenSize != 20 && tokenSize != 24) {
- return UA_STATUSCODE_BADDECODINGERROR;
- }
-
- /* sanity check */
- if(tokenData[4] != '-' || tokenData[7] != '-' || tokenData[10] != 'T' ||
- tokenData[13] != ':' || tokenData[16] != ':' ||
- !(tokenData[19] == 'Z' || tokenData[19] == '.')) {
- return UA_STATUSCODE_BADDECODINGERROR;
- }
-
- struct mytm dts;
- memset(&dts, 0, sizeof(dts));
-
- UA_UInt64 year = 0;
- atoiUnsigned(&tokenData[0], 4, &year);
- dts.tm_year = (UA_UInt16)year - 1900;
- UA_UInt64 month = 0;
- atoiUnsigned(&tokenData[5], 2, &month);
- dts.tm_mon = (UA_UInt16)month - 1;
- UA_UInt64 day = 0;
- atoiUnsigned(&tokenData[8], 2, &day);
- dts.tm_mday = (UA_UInt16)day;
- UA_UInt64 hour = 0;
- atoiUnsigned(&tokenData[11], 2, &hour);
- dts.tm_hour = (UA_UInt16)hour;
- UA_UInt64 min = 0;
- atoiUnsigned(&tokenData[14], 2, &min);
- dts.tm_min = (UA_UInt16)min;
- UA_UInt64 sec = 0;
- atoiUnsigned(&tokenData[17], 2, &sec);
- dts.tm_sec = (UA_UInt16)sec;
-
- UA_UInt64 msec = 0;
- if(tokenSize == 24) {
- atoiUnsigned(&tokenData[20], 3, &msec);
- }
-
- long long sinceunix = __tm_to_secs(&dts);
- UA_DateTime dt = (UA_DateTime)((UA_UInt64)(sinceunix*UA_DATETIME_SEC +
- UA_DATETIME_UNIX_EPOCH) +
- (UA_UInt64)(UA_DATETIME_MSEC * msec));
- *dst = dt;
-
- if(moveToken)
- parseCtx->index++;
- return UA_STATUSCODE_GOOD;
- }
- DECODE_JSON(StatusCode) {
- if(isJsonNull(ctx, parseCtx)) {
- parseCtx->index++;
- return UA_STATUSCODE_GOOD;
- }
-
- status ret = DECODE_DIRECT_JSON(dst, UInt32);
- if(ret != UA_STATUSCODE_GOOD)
- return ret;
- if(moveToken)
- parseCtx->index++;
- return UA_STATUSCODE_GOOD;
- }
- static status
- VariantDimension_decodeJson(void * dst, const UA_DataType *type,
- CtxJson *ctx, ParseCtx *parseCtx, UA_Boolean moveToken) {
- const UA_DataType *dimType = &UA_TYPES[UA_TYPES_UINT32];
- return Array_decodeJson_internal((void**)dst, dimType, ctx, parseCtx, moveToken);
- }
- DECODE_JSON(Variant) {
- status ret = UA_STATUSCODE_GOOD;
-
- if(getJsmnType(parseCtx) != JSMN_OBJECT) {
- /* If type is 0 (NULL) the Variant contains a NULL value and the
- * containing JSON object shall be omitted or replaced by the JSON
- * literal ‘null’ (when an element of a JSON array). */
- if(isJsonNull(ctx, parseCtx)) {
- /*set an empty Variant*/
- UA_Variant_init(dst);
- dst->type = NULL;
- parseCtx->index++;
- return UA_STATUSCODE_GOOD;
- }
-
- return UA_STATUSCODE_BADDECODINGERROR;
- }
-
- /* First search for the variant type in the json object. */
- size_t searchResultType = 0;
- ret = lookAheadForKey(UA_JSONKEY_TYPE, ctx, parseCtx, &searchResultType);
- if(ret != UA_STATUSCODE_GOOD)
- return UA_STATUSCODE_BADDECODINGERROR;
- size_t size = (size_t)(parseCtx->tokenArray[searchResultType].end - parseCtx->tokenArray[searchResultType].start);
- /* check if size is zero or the type is not a number */
- if(size < 1 || parseCtx->tokenArray[searchResultType].type != JSMN_PRIMITIVE) {
- return UA_STATUSCODE_BADDECODINGERROR;
- }
-
- /*Parse the type*/
- UA_UInt64 idTypeDecoded = 0;
- char *idTypeEncoded = (char*)(ctx->pos + parseCtx->tokenArray[searchResultType].start);
- status typeDecodeStatus = atoiUnsigned(idTypeEncoded, size, &idTypeDecoded);
-
- /* value is not a valid number */
- if(typeDecodeStatus != UA_STATUSCODE_GOOD) {
- return typeDecodeStatus;
- }
- /*Set the type, Get the Type by nodeID!*/
- UA_NodeId typeNodeId = UA_NODEID_NUMERIC(0, (UA_UInt32)idTypeDecoded);
- const UA_DataType *bodyType = UA_findDataType(&typeNodeId);
- if(bodyType == NULL) {
- return UA_STATUSCODE_BADDECODINGERROR;
- }
-
- /*Set the type*/
- dst->type = bodyType;
-
- /* LookAhead BODY */
- /* Does the variant contain an array? */
- UA_Boolean isArray = false;
- UA_Boolean isBodyNull = false;
-
- /* Search for body */
- size_t searchResultBody = 0;
- ret = lookAheadForKey(UA_JSONKEY_BODY, ctx, parseCtx, &searchResultBody);
- if(ret == UA_STATUSCODE_GOOD) { /* body found */
- /* get body token */
- jsmntok_t bodyToken = parseCtx->tokenArray[searchResultBody];
-
- /*BODY is null?*/
- if(isJsonTokenNull(ctx, &bodyToken)) {
- dst->data = NULL;
- isBodyNull = true;
- }
-
- if(bodyToken.type == JSMN_ARRAY) {
- isArray = true;
-
- size_t arraySize = 0;
- arraySize = (size_t)parseCtx->tokenArray[searchResultBody].size;
- dst->arrayLength = arraySize;
- }
- } else {
- /*TODO: no body? set value NULL?*/
- return UA_STATUSCODE_BADDECODINGERROR;
- }
-
- /* LookAhead DIMENSION */
- UA_Boolean hasDimension = false;
- /* Has the variant dimension? */
- size_t searchResultDim = 0;
- ret = lookAheadForKey(UA_JSONKEY_DIMENSION, ctx, parseCtx, &searchResultDim);
- if(ret == UA_STATUSCODE_GOOD) {
- hasDimension = true;
- size_t dimensionSize = 0;
- dimensionSize = (size_t)parseCtx->tokenArray[searchResultDim].size;
- dst->arrayDimensionsSize = dimensionSize;
- }
-
- /* no array but has dimension. error? */
- if(!isArray && hasDimension)
- return UA_STATUSCODE_BADDECODINGERROR;
-
- /* Get the datatype of the content. The type must be a builtin data type.
- * All not-builtin types are wrapped in an ExtensionObject. */
- if(bodyType->typeIndex > UA_TYPES_DIAGNOSTICINFO)
- return UA_STATUSCODE_BADDECODINGERROR;
- /* A variant cannot contain a variant. But it can contain an array of
- * variants */
- if(bodyType->typeIndex == UA_TYPES_VARIANT && !isArray)
- return UA_STATUSCODE_BADDECODINGERROR;
-
- if(isArray) {
- DecodeEntry entries[3] = {
- {UA_JSONKEY_TYPE, NULL, NULL, false, NULL},
- {UA_JSONKEY_BODY, &dst->data, (decodeJsonSignature) Array_decodeJson, false, NULL},
- {UA_JSONKEY_DIMENSION, &dst->arrayDimensions,
- (decodeJsonSignature) VariantDimension_decodeJson, false, NULL}};
- if(!hasDimension) {
- ret = decodeFields(ctx, parseCtx, entries, 2, bodyType); /*use first 2 fields*/
- } else {
- ret = decodeFields(ctx, parseCtx, entries, 3, bodyType); /*use all fields*/
- }
- } else if(bodyType->typeIndex != UA_TYPES_EXTENSIONOBJECT) {
- /* Allocate Memory for Body */
- if(!isBodyNull) {
- dst->data = UA_new(bodyType);
- if(!dst->data)
- return UA_STATUSCODE_BADOUTOFMEMORY;
- }
-
- DecodeEntry entries[2] = {{UA_JSONKEY_TYPE, NULL, NULL, false, NULL},
- {UA_JSONKEY_BODY, dst->data, (decodeJsonSignature) decodeJsonInternal, false, NULL}};
- ret = decodeFields(ctx, parseCtx, entries, 2, bodyType);
- } else { /* extensionObject */
- DecodeEntry entries[2] = {{UA_JSONKEY_TYPE, NULL, NULL, false, NULL},
- {UA_JSONKEY_BODY, dst,
- (decodeJsonSignature) Variant_decodeJsonUnwrapExtensionObject, false, NULL}};
- ret = decodeFields(ctx, parseCtx, entries, 2, bodyType);
- }
- return ret;
- }
- DECODE_JSON(DataValue) {
- if(getJsmnType(parseCtx) != JSMN_OBJECT) {
- if(isJsonNull(ctx, parseCtx)) {
- dst = NULL;
- parseCtx->index++;
- return UA_STATUSCODE_GOOD;
- }
- return UA_STATUSCODE_BADDECODINGERROR;
- }
-
- DecodeEntry entries[6] = {
- {UA_JSONKEY_VALUE, &dst->value, (decodeJsonSignature) Variant_decodeJson, false, NULL},
- {UA_JSONKEY_STATUS, &dst->status, (decodeJsonSignature) StatusCode_decodeJson, false, NULL},
- {UA_JSONKEY_SOURCETIMESTAMP, &dst->sourceTimestamp, (decodeJsonSignature) DateTime_decodeJson, false, NULL},
- {UA_JSONKEY_SOURCEPICOSECONDS, &dst->sourcePicoseconds, (decodeJsonSignature) UInt16_decodeJson, false, NULL},
- {UA_JSONKEY_SERVERTIMESTAMP, &dst->serverTimestamp, (decodeJsonSignature) DateTime_decodeJson, false, NULL},
- {UA_JSONKEY_SERVERPICOSECONDS, &dst->serverPicoseconds, (decodeJsonSignature) UInt16_decodeJson, false, NULL}};
- status ret = decodeFields(ctx, parseCtx, entries, 6, type);
- dst->hasValue = entries[0].found; dst->hasStatus = entries[1].found;
- dst->hasSourceTimestamp = entries[2].found; dst->hasSourcePicoseconds = entries[3].found;
- dst->hasServerTimestamp = entries[4].found; dst->hasServerPicoseconds = entries[5].found;
- return ret;
- }
- DECODE_JSON(ExtensionObject) {
- if(isJsonNull(ctx, parseCtx)) {
- /* If the Body is empty, the ExtensionObject is NULL and is omitted or
- * encoded as a JSON null. */
- parseCtx->index++;
- return UA_STATUSCODE_GOOD;
- }
-
- if(getJsmnType(parseCtx) != JSMN_OBJECT)
- return UA_STATUSCODE_BADDECODINGERROR;
-
- /* Search for Encoding */
- size_t searchEncodingResult = 0;
- status ret = lookAheadForKey(UA_JSONKEY_ENCODING, ctx, parseCtx, &searchEncodingResult);
-
- /* If no encoding found it is structure encoding */
- if(ret != UA_STATUSCODE_GOOD) {
- UA_NodeId typeId;
- UA_NodeId_init(&typeId);
- size_t searchTypeIdResult = 0;
- ret = lookAheadForKey(UA_JSONKEY_TYPEID, ctx, parseCtx, &searchTypeIdResult);
- if(ret != UA_STATUSCODE_GOOD) {
- /* TYPEID not found, abort */
- return UA_STATUSCODE_BADENCODINGERROR;
- }
- /* parse the nodeid */
- /*for restore*/
- UA_UInt16 index = parseCtx->index;
- parseCtx->index = (UA_UInt16)searchTypeIdResult;
- ret = NodeId_decodeJson(&typeId, &UA_TYPES[UA_TYPES_NODEID], ctx, parseCtx, true);
- if(ret != UA_STATUSCODE_GOOD)
- return ret;
-
- /*restore*/
- parseCtx->index = index;
- const UA_DataType *typeOfBody = UA_findDataType(&typeId);
- if(!typeOfBody) {
- /*dont decode body: 1. save as bytestring, 2. jump over*/
- dst->encoding = UA_EXTENSIONOBJECT_ENCODED_BYTESTRING;
- UA_NodeId_copy(&typeId, &dst->content.encoded.typeId);
-
- /*Check if Object in Extentionobject*/
- if(getJsmnType(parseCtx) != JSMN_OBJECT) {
- UA_NodeId_deleteMembers(&typeId);
- return UA_STATUSCODE_BADDECODINGERROR;
- }
-
- /*Search for Body to save*/
- size_t searchBodyResult = 0;
- ret = lookAheadForKey(UA_JSONKEY_BODY, ctx, parseCtx, &searchBodyResult);
- if(ret != UA_STATUSCODE_GOOD) {
- /*No Body*/
- UA_NodeId_deleteMembers(&typeId);
- return UA_STATUSCODE_BADDECODINGERROR;
- }
-
- if(searchBodyResult >= (size_t)parseCtx->tokenCount) {
- /*index not in Tokenarray*/
- UA_NodeId_deleteMembers(&typeId);
- return UA_STATUSCODE_BADDECODINGERROR;
- }
- /* Get the size of the Object as a string, not the Object key count! */
- UA_Int64 sizeOfJsonString =(parseCtx->tokenArray[searchBodyResult].end -
- parseCtx->tokenArray[searchBodyResult].start);
-
- char* bodyJsonString = (char*)(ctx->pos + parseCtx->tokenArray[searchBodyResult].start);
-
- if(sizeOfJsonString <= 0) {
- UA_NodeId_deleteMembers(&typeId);
- return UA_STATUSCODE_BADDECODINGERROR;
- }
-
- /* Save encoded as bytestring. */
- ret = UA_ByteString_allocBuffer(&dst->content.encoded.body, (size_t)sizeOfJsonString);
- if(ret != UA_STATUSCODE_GOOD) {
- UA_NodeId_deleteMembers(&typeId);
- return ret;
- }
- memcpy(dst->content.encoded.body.data, bodyJsonString, (size_t)sizeOfJsonString);
-
- size_t tokenAfteExtensionObject = 0;
- jumpOverObject(ctx, parseCtx, &tokenAfteExtensionObject);
-
- if(tokenAfteExtensionObject == 0) {
- /*next object token not found*/
- UA_NodeId_deleteMembers(&typeId);
- UA_ByteString_deleteMembers(&dst->content.encoded.body);
- return UA_STATUSCODE_BADDECODINGERROR;
- }
-
- parseCtx->index = (UA_UInt16)tokenAfteExtensionObject;
-
- return UA_STATUSCODE_GOOD;
- }
-
- /*Type id not used anymore, typeOfBody has type*/
- UA_NodeId_deleteMembers(&typeId);
-
- /*Set Found Type*/
- dst->content.decoded.type = typeOfBody;
- dst->encoding = UA_EXTENSIONOBJECT_DECODED;
-
- if(searchTypeIdResult != 0) {
- dst->content.decoded.data = UA_new(typeOfBody);
- if(!dst->content.decoded.data)
- return UA_STATUSCODE_BADOUTOFMEMORY;
- UA_NodeId typeId_dummy;
- DecodeEntry entries[2] = {
- {UA_JSONKEY_TYPEID, &typeId_dummy, (decodeJsonSignature) NodeId_decodeJson, false, NULL},
- {UA_JSONKEY_BODY, dst->content.decoded.data,
- (decodeJsonSignature) decodeJsonJumpTable[typeOfBody->typeKind], false, NULL}
- };
- return decodeFields(ctx, parseCtx, entries, 2, typeOfBody);
- } else {
- return UA_STATUSCODE_BADDECODINGERROR;
- }
- } else { /* UA_JSONKEY_ENCODING found */
- /*Parse the encoding*/
- UA_UInt64 encoding = 0;
- char *extObjEncoding = (char*)(ctx->pos + parseCtx->tokenArray[searchEncodingResult].start);
- size_t size = (size_t)(parseCtx->tokenArray[searchEncodingResult].end - parseCtx->tokenArray[searchEncodingResult].start);
- atoiUnsigned(extObjEncoding, size, &encoding);
- if(encoding == 1) {
- /* BYTESTRING in Json Body */
- dst->encoding = UA_EXTENSIONOBJECT_ENCODED_BYTESTRING;
- UA_UInt16 encodingTypeJson;
- DecodeEntry entries[3] = {
- {UA_JSONKEY_ENCODING, &encodingTypeJson, (decodeJsonSignature) UInt16_decodeJson, false, NULL},
- {UA_JSONKEY_BODY, &dst->content.encoded.body, (decodeJsonSignature) String_decodeJson, false, NULL},
- {UA_JSONKEY_TYPEID, &dst->content.encoded.typeId, (decodeJsonSignature) NodeId_decodeJson, false, NULL}
- };
- return decodeFields(ctx, parseCtx, entries, 3, type);
- } else if(encoding == 2) {
- /* XmlElement in Json Body */
- dst->encoding = UA_EXTENSIONOBJECT_ENCODED_XML;
- UA_UInt16 encodingTypeJson;
- DecodeEntry entries[3] = {
- {UA_JSONKEY_ENCODING, &encodingTypeJson, (decodeJsonSignature) UInt16_decodeJson, false, NULL},
- {UA_JSONKEY_BODY, &dst->content.encoded.body, (decodeJsonSignature) String_decodeJson, false, NULL},
- {UA_JSONKEY_TYPEID, &dst->content.encoded.typeId, (decodeJsonSignature) NodeId_decodeJson, false, NULL}
- };
- return decodeFields(ctx, parseCtx, entries, 3, type);
- } else {
- return UA_STATUSCODE_BADDECODINGERROR;
- }
- }
- return UA_STATUSCODE_BADNOTIMPLEMENTED;
- }
- static status
- Variant_decodeJsonUnwrapExtensionObject(UA_Variant *dst, const UA_DataType *type,
- CtxJson *ctx, ParseCtx *parseCtx, UA_Boolean moveToken) {
- /*EXTENSIONOBJECT POSITION!*/
- UA_UInt16 old_index = parseCtx->index;
- UA_Boolean typeIdFound = false;
-
- /* Decode the DataType */
- UA_NodeId typeId;
- UA_NodeId_init(&typeId);
- size_t searchTypeIdResult = 0;
- status ret = lookAheadForKey(UA_JSONKEY_TYPEID, ctx, parseCtx, &searchTypeIdResult);
- if(ret != UA_STATUSCODE_GOOD) {
- /*No Typeid found*/
- typeIdFound = false;
- /*return UA_STATUSCODE_BADDECODINGERROR;*/
- } else {
- typeIdFound = true;
- /* parse the nodeid */
- parseCtx->index = (UA_UInt16)searchTypeIdResult;
- ret = NodeId_decodeJson(&typeId, &UA_TYPES[UA_TYPES_NODEID], ctx, parseCtx, true);
- if(ret != UA_STATUSCODE_GOOD) {
- UA_NodeId_deleteMembers(&typeId);
- return ret;
- }
- /*restore index, ExtensionObject position*/
- parseCtx->index = old_index;
- }
- /* ---Decode the EncodingByte--- */
- if(!typeIdFound)
- return UA_STATUSCODE_BADDECODINGERROR;
- UA_Boolean encodingFound = false;
- /*Search for Encoding*/
- size_t searchEncodingResult = 0;
- ret = lookAheadForKey(UA_JSONKEY_ENCODING, ctx, parseCtx, &searchEncodingResult);
- UA_UInt64 encoding = 0;
- /*If no encoding found it is Structure encoding*/
- if(ret == UA_STATUSCODE_GOOD) { /*FOUND*/
- encodingFound = true;
- char *extObjEncoding = (char*)(ctx->pos + parseCtx->tokenArray[searchEncodingResult].start);
- size_t size = (size_t)(parseCtx->tokenArray[searchEncodingResult].end
- - parseCtx->tokenArray[searchEncodingResult].start);
- atoiUnsigned(extObjEncoding, size, &encoding);
- }
-
- const UA_DataType *typeOfBody = UA_findDataType(&typeId);
-
- if(encoding == 0 || typeOfBody != NULL) {
- /*This value is 0 if the body is Structure encoded as a JSON object (see 5.4.6).*/
- /* Found a valid type and it is structure encoded so it can be unwrapped */
- dst->type = typeOfBody;
- /* Allocate memory for type*/
- dst->data = UA_new(dst->type);
- if(!dst->data) {
- UA_NodeId_deleteMembers(&typeId);
- return UA_STATUSCODE_BADOUTOFMEMORY;
- }
- /* Decode the content */
- UA_NodeId nodeIddummy;
- DecodeEntry entries[3] =
- {
- {UA_JSONKEY_TYPEID, &nodeIddummy, (decodeJsonSignature) NodeId_decodeJson, false, NULL},
- {UA_JSONKEY_BODY, dst->data,
- (decodeJsonSignature) decodeJsonJumpTable[dst->type->typeKind], false, NULL},
- {UA_JSONKEY_ENCODING, NULL, NULL, false, NULL}};
- ret = decodeFields(ctx, parseCtx, entries, encodingFound ? 3:2, typeOfBody);
- if(ret != UA_STATUSCODE_GOOD) {
- UA_free(dst->data);
- }
- } else if(encoding == 1 || encoding == 2 || typeOfBody == NULL) {
- UA_NodeId_deleteMembers(&typeId);
-
- /* decode as ExtensionObject */
- dst->type = &UA_TYPES[UA_TYPES_EXTENSIONOBJECT];
- /* Allocate memory for extensionobject*/
- dst->data = UA_new(dst->type);
- if(!dst->data)
- return UA_STATUSCODE_BADOUTOFMEMORY;
- /* decode: Does not move tokenindex. */
- ret = DECODE_DIRECT_JSON(dst->data, ExtensionObject);
- if(ret != UA_STATUSCODE_GOOD)
- UA_free(dst->data);
- } else {
- /*no recognized encoding type*/
- return UA_STATUSCODE_BADDECODINGERROR;
- }
- return ret;
- }
- status DiagnosticInfoInner_decodeJson(void* dst, const UA_DataType* type,
- CtxJson* ctx, ParseCtx* parseCtx, UA_Boolean moveToken);
- DECODE_JSON(DiagnosticInfo) {
- if(isJsonNull(ctx, parseCtx)) {
- parseCtx->index++;
- return UA_STATUSCODE_GOOD;
- }
- if(getJsmnType(parseCtx) != JSMN_OBJECT)
- return UA_STATUSCODE_BADDECODINGERROR;
-
- DecodeEntry entries[7] = {
- {UA_JSONKEY_SYMBOLICID, &dst->symbolicId, (decodeJsonSignature) Int32_decodeJson, false, NULL},
- {UA_JSONKEY_NAMESPACEURI, &dst->namespaceUri, (decodeJsonSignature) Int32_decodeJson, false, NULL},
- {UA_JSONKEY_LOCALIZEDTEXT, &dst->localizedText, (decodeJsonSignature) Int32_decodeJson, false, NULL},
- {UA_JSONKEY_LOCALE, &dst->locale, (decodeJsonSignature) Int32_decodeJson, false, NULL},
- {UA_JSONKEY_ADDITIONALINFO, &dst->additionalInfo, (decodeJsonSignature) String_decodeJson, false, NULL},
- {UA_JSONKEY_INNERSTATUSCODE, &dst->innerStatusCode, (decodeJsonSignature) StatusCode_decodeJson, false, NULL},
- {UA_JSONKEY_INNERDIAGNOSTICINFO, &dst->innerDiagnosticInfo, (decodeJsonSignature) DiagnosticInfoInner_decodeJson, false, NULL}};
- status ret = decodeFields(ctx, parseCtx, entries, 7, type);
- dst->hasSymbolicId = entries[0].found; dst->hasNamespaceUri = entries[1].found;
- dst->hasLocalizedText = entries[2].found; dst->hasLocale = entries[3].found;
- dst->hasAdditionalInfo = entries[4].found; dst->hasInnerStatusCode = entries[5].found;
- dst->hasInnerDiagnosticInfo = entries[6].found;
- return ret;
- }
- status
- DiagnosticInfoInner_decodeJson(void* dst, const UA_DataType* type,
- CtxJson* ctx, ParseCtx* parseCtx, UA_Boolean moveToken) {
- UA_DiagnosticInfo *inner = (UA_DiagnosticInfo*)UA_calloc(1, sizeof(UA_DiagnosticInfo));
- if(inner == NULL) {
- return UA_STATUSCODE_BADOUTOFMEMORY;
- }
- memcpy(dst, &inner, sizeof(UA_DiagnosticInfo*)); /* Copy new Pointer do dest */
- return DiagnosticInfo_decodeJson(inner, type, ctx, parseCtx, moveToken);
- }
- status
- decodeFields(CtxJson *ctx, ParseCtx *parseCtx, DecodeEntry *entries,
- size_t entryCount, const UA_DataType *type) {
- CHECK_TOKEN_BOUNDS;
- size_t objectCount = (size_t)(parseCtx->tokenArray[parseCtx->index].size);
- status ret = UA_STATUSCODE_GOOD;
- if(entryCount == 1) {
- if(*(entries[0].fieldName) == 0) { /*No MemberName*/
- return entries[0].function(entries[0].fieldPointer, type,
- ctx, parseCtx, true); /*ENCODE DIRECT*/
- }
- } else if(entryCount == 0) {
- return UA_STATUSCODE_BADDECODINGERROR;
- }
- parseCtx->index++; /*go to first key*/
- CHECK_TOKEN_BOUNDS;
-
- for (size_t currentObjectCount = 0; currentObjectCount < objectCount &&
- parseCtx->index < parseCtx->tokenCount; currentObjectCount++) {
- /* start searching at the index of currentObjectCount */
- for (size_t i = currentObjectCount; i < entryCount + currentObjectCount; i++) {
- /* Search for KEY, if found outer loop will be one less. Best case
- * is objectCount if in order! */
- size_t index = i % entryCount;
-
- CHECK_TOKEN_BOUNDS;
- if(jsoneq((char*) ctx->pos, &parseCtx->tokenArray[parseCtx->index],
- entries[index].fieldName) != 0)
- continue;
- if(entries[index].found) {
- /*Duplicate Key found, abort.*/
- return UA_STATUSCODE_BADDECODINGERROR;
- }
- entries[index].found = true;
- parseCtx->index++; /*goto value*/
- CHECK_TOKEN_BOUNDS;
-
- /* Find the data type.
- * TODO: get rid of parameter type. Only forward via DecodeEntry.
- */
- const UA_DataType *membertype = type;
- if(entries[index].type)
- membertype = entries[index].type;
- if(entries[index].function != NULL) {
- ret = entries[index].function(entries[index].fieldPointer,
- membertype, ctx, parseCtx, true); /*Move Token True*/
- if(ret != UA_STATUSCODE_GOOD)
- return ret;
- } else {
- /*overstep single value, this will not work if object or array
- Only used not to double parse pre looked up type, but it has to be overstepped*/
- parseCtx->index++;
- }
- break;
- }
- }
- return ret;
- }
- static status
- Array_decodeJson_internal(void **dst, const UA_DataType *type,
- CtxJson *ctx, ParseCtx *parseCtx, UA_Boolean moveToken) {
- status ret = UA_STATUSCODE_GOOD;
-
- if(parseCtx->tokenArray[parseCtx->index].type != JSMN_ARRAY)
- return UA_STATUSCODE_BADDECODINGERROR;
-
- size_t length = (size_t)parseCtx->tokenArray[parseCtx->index].size;
- /* Save the length of the array */
- size_t *p = (size_t*) dst - 1;
- *p = length;
- /* Return early for empty arrays */
- if(length == 0) {
- *dst = UA_EMPTY_ARRAY_SENTINEL;
- return UA_STATUSCODE_GOOD;
- }
- /* Allocate memory */
- *dst = UA_calloc(length, type->memSize);
- if(*dst == NULL)
- return UA_STATUSCODE_BADOUTOFMEMORY;
-
- parseCtx->index++; /* We go to first Array member!*/
-
- /* Decode array members */
- uintptr_t ptr = (uintptr_t)*dst;
- for(size_t i = 0; i < length; ++i) {
- ret = decodeJsonJumpTable[type->typeKind]((void*)ptr, type, ctx, parseCtx, true);
- if(ret != UA_STATUSCODE_GOOD) {
- UA_Array_delete(*dst, i+1, type);
- *dst = NULL;
- return ret;
- }
- ptr += type->memSize;
- }
- return UA_STATUSCODE_GOOD;
- }
- /*Wrapper for array with valid decodingStructure.*/
- static status
- Array_decodeJson(void * dst, const UA_DataType *type, CtxJson *ctx,
- ParseCtx *parseCtx, UA_Boolean moveToken) {
- return Array_decodeJson_internal((void **)dst, type, ctx, parseCtx, moveToken);
- }
- static status
- decodeJsonStructure(void *dst, const UA_DataType *type, CtxJson *ctx,
- ParseCtx *parseCtx, UA_Boolean moveToken) {
- /* Check the recursion limit */
- if(ctx->depth > UA_JSON_ENCODING_MAX_RECURSION)
- return UA_STATUSCODE_BADENCODINGERROR;
- ctx->depth++;
- uintptr_t ptr = (uintptr_t)dst;
- status ret = UA_STATUSCODE_GOOD;
- u8 membersSize = type->membersSize;
- const UA_DataType *typelists[2] = { UA_TYPES, &type[-type->typeIndex] };
-
- UA_STACKARRAY(DecodeEntry, entries, membersSize);
- for(size_t i = 0; i < membersSize && ret == UA_STATUSCODE_GOOD; ++i) {
- const UA_DataTypeMember *m = &type->members[i];
- const UA_DataType *mt = &typelists[!m->namespaceZero][m->memberTypeIndex];
- entries[i].type = mt;
- if(!m->isArray) {
- ptr += m->padding;
- entries[i].fieldName = m->memberName;
- entries[i].fieldPointer = (void*)ptr;
- entries[i].function = decodeJsonJumpTable[mt->typeKind];
- entries[i].found = false;
- ptr += mt->memSize;
- } else {
- ptr += m->padding;
- ptr += sizeof(size_t);
- entries[i].fieldName = m->memberName;
- entries[i].fieldPointer = (void*)ptr;
- entries[i].function = (decodeJsonSignature)Array_decodeJson;
- entries[i].found = false;
- ptr += sizeof(void*);
- }
- }
-
- ret = decodeFields(ctx, parseCtx, entries, membersSize, type);
- ctx->depth--;
- return ret;
- }
- static status
- decodeJsonNotImplemented(void *dst, const UA_DataType *type, CtxJson *ctx,
- ParseCtx *parseCtx, UA_Boolean moveToken) {
- return UA_STATUSCODE_BADNOTIMPLEMENTED;
- }
- const decodeJsonSignature decodeJsonJumpTable[UA_DATATYPEKINDS] = {
- (decodeJsonSignature)Boolean_decodeJson,
- (decodeJsonSignature)SByte_decodeJson, /* SByte */
- (decodeJsonSignature)Byte_decodeJson,
- (decodeJsonSignature)Int16_decodeJson, /* Int16 */
- (decodeJsonSignature)UInt16_decodeJson,
- (decodeJsonSignature)Int32_decodeJson, /* Int32 */
- (decodeJsonSignature)UInt32_decodeJson,
- (decodeJsonSignature)Int64_decodeJson, /* Int64 */
- (decodeJsonSignature)UInt64_decodeJson,
- (decodeJsonSignature)Float_decodeJson,
- (decodeJsonSignature)Double_decodeJson,
- (decodeJsonSignature)String_decodeJson,
- (decodeJsonSignature)DateTime_decodeJson, /* DateTime */
- (decodeJsonSignature)Guid_decodeJson,
- (decodeJsonSignature)ByteString_decodeJson, /* ByteString */
- (decodeJsonSignature)String_decodeJson, /* XmlElement */
- (decodeJsonSignature)NodeId_decodeJson,
- (decodeJsonSignature)ExpandedNodeId_decodeJson,
- (decodeJsonSignature)StatusCode_decodeJson, /* StatusCode */
- (decodeJsonSignature)QualifiedName_decodeJson, /* QualifiedName */
- (decodeJsonSignature)LocalizedText_decodeJson,
- (decodeJsonSignature)ExtensionObject_decodeJson,
- (decodeJsonSignature)DataValue_decodeJson,
- (decodeJsonSignature)Variant_decodeJson,
- (decodeJsonSignature)DiagnosticInfo_decodeJson,
- (decodeJsonSignature)decodeJsonNotImplemented, /* Decimal */
- (decodeJsonSignature)Int32_decodeJson, /* Enum */
- (decodeJsonSignature)decodeJsonStructure,
- (decodeJsonSignature)decodeJsonNotImplemented, /* Structure with optional fields */
- (decodeJsonSignature)decodeJsonNotImplemented, /* Union */
- (decodeJsonSignature)decodeJsonNotImplemented /* BitfieldCluster */
- };
- decodeJsonSignature getDecodeSignature(u8 index) {
- return decodeJsonJumpTable[index];
- }
- status
- tokenize(ParseCtx *parseCtx, CtxJson *ctx, const UA_ByteString *src) {
- /* Set up the context */
- ctx->pos = &src->data[0];
- ctx->end = &src->data[src->length];
- ctx->depth = 0;
- parseCtx->tokenCount = 0;
- parseCtx->index = 0;
- /*Set up tokenizer jsmn*/
- jsmn_parser p;
- jsmn_init(&p);
- parseCtx->tokenCount = (UA_Int32)
- jsmn_parse(&p, (char*)src->data, src->length,
- parseCtx->tokenArray, TOKENCOUNT);
-
- if(parseCtx->tokenCount < 0) {
- if(parseCtx->tokenCount == JSMN_ERROR_NOMEM)
- return UA_STATUSCODE_BADOUTOFMEMORY;
- return UA_STATUSCODE_BADDECODINGERROR;
- }
-
- return UA_STATUSCODE_GOOD;
- }
- UA_StatusCode
- decodeJsonInternal(void *dst, const UA_DataType *type,
- CtxJson *ctx, ParseCtx *parseCtx, UA_Boolean moveToken) {
- return decodeJsonJumpTable[type->typeKind](dst, type, ctx, parseCtx, moveToken);
- }
- status UA_FUNC_ATTR_WARN_UNUSED_RESULT
- UA_decodeJson(const UA_ByteString *src, void *dst, const UA_DataType *type) {
-
- #ifndef UA_ENABLE_TYPENAMES
- return UA_STATUSCODE_BADNOTSUPPORTED;
- #endif
-
- if(dst == NULL || src == NULL || type == NULL) {
- return UA_STATUSCODE_BADARGUMENTSMISSING;
- }
-
- /* Set up the context */
- CtxJson ctx;
- ParseCtx parseCtx;
- parseCtx.tokenArray = (jsmntok_t*)UA_malloc(sizeof(jsmntok_t) * TOKENCOUNT);
- if(!parseCtx.tokenArray)
- return UA_STATUSCODE_BADOUTOFMEMORY;
-
- status ret = tokenize(&parseCtx, &ctx, src);
- if(ret != UA_STATUSCODE_GOOD)
- goto cleanup;
- /* Assume the top-level element is an object */
- if(parseCtx.tokenCount < 1 || parseCtx.tokenArray[0].type != JSMN_OBJECT) {
- if(parseCtx.tokenCount == 1) {
- if(parseCtx.tokenArray[0].type == JSMN_PRIMITIVE ||
- parseCtx.tokenArray[0].type == JSMN_STRING) {
- /* Only a primitive to parse. Do it directly. */
- memset(dst, 0, type->memSize); /* Initialize the value */
- ret = decodeJsonJumpTable[type->typeKind](dst, type, &ctx, &parseCtx, true);
- goto cleanup;
- }
- }
- ret = UA_STATUSCODE_BADDECODINGERROR;
- goto cleanup;
- }
- /* Decode */
- memset(dst, 0, type->memSize); /* Initialize the value */
- ret = decodeJsonJumpTable[type->typeKind](dst, type, &ctx, &parseCtx, true);
- cleanup:
- free(parseCtx.tokenArray);
-
- /* sanity check if all Tokens were processed */
- if(!(parseCtx.index == parseCtx.tokenCount ||
- parseCtx.index == parseCtx.tokenCount-1)) {
- ret = UA_STATUSCODE_BADDECODINGERROR;
- }
-
- if(ret != UA_STATUSCODE_GOOD)
- UA_deleteMembers(dst, type); /* Clean up */
- return ret;
- }
|