TianoCore EDK2 master
Loading...
Searching...
No Matches
JsonLib.c
Go to the documentation of this file.
1
15#include <Uefi.h>
16#include <Library/JsonLib.h>
19
20#include "jansson.h"
21
22extern volatile UINT32 hashtable_seed;
23
37EDKII_JSON_VALUE
38EFIAPI
40 VOID
41 )
42{
43 return (EDKII_JSON_VALUE)json_array ();
44}
45
59EDKII_JSON_VALUE
60EFIAPI
62 VOID
63 )
64{
65 return (EDKII_JSON_VALUE)json_object ();
66}
67
87EDKII_JSON_VALUE
88EFIAPI
90 IN CONST CHAR8 *String
91 )
92{
93 UINTN Index;
94
95 if (String == NULL) {
96 return NULL;
97 }
98
99 Index = 0;
100 while (*(String + Index) != '\0') {
101 if (((*(String + Index)) & 0x80) != 0x00) {
102 return NULL;
103 }
104
105 Index++;
106 }
107
108 return (EDKII_JSON_VALUE)json_string (String);
109}
110
128EDKII_JSON_VALUE
129EFIAPI
131 IN CHAR16 *String
132 )
133{
134 EFI_STATUS Status;
135 CHAR8 *Utf8Str;
136
137 if (String == NULL) {
138 return NULL;
139 }
140
141 Utf8Str = NULL;
142 Status = UCS2StrToUTF8 (String, &Utf8Str);
143 if (EFI_ERROR (Status)) {
144 return NULL;
145 }
146
147 return (EDKII_JSON_VALUE)json_string (Utf8Str);
148}
149
164EDKII_JSON_VALUE
165EFIAPI
167 IN INT64 Value
168 )
169{
170 return (EDKII_JSON_VALUE)json_integer (Value);
171}
172
184EDKII_JSON_VALUE
185EFIAPI
187 IN BOOLEAN Value
188 )
189{
190 return (EDKII_JSON_VALUE)json_boolean (Value);
191}
192
202EDKII_JSON_VALUE
203EFIAPI
205 VOID
206 )
207{
208 return (EDKII_JSON_VALUE)json_true ();
209}
210
220EDKII_JSON_VALUE
221EFIAPI
223 VOID
224 )
225{
226 return (EDKII_JSON_VALUE)json_false ();
227}
228
238EDKII_JSON_VALUE
239EFIAPI
241 VOID
242 )
243{
244 return (EDKII_JSON_VALUE)json_null ();
245}
246
267VOID
268EFIAPI
270 IN EDKII_JSON_VALUE Json
271 )
272{
273 json_decref ((json_t *)Json);
274}
275
293EDKII_JSON_VALUE
294EFIAPI
296 IN EDKII_JSON_VALUE Json
297 )
298{
299 return (EDKII_JSON_VALUE)json_deep_copy ((json_t *)Json);
300}
301
311BOOLEAN
312EFIAPI
314 IN EDKII_JSON_VALUE Json
315 )
316{
317 return json_is_array ((json_t *)Json);
318}
319
329BOOLEAN
330EFIAPI
332 IN EDKII_JSON_VALUE Json
333 )
334{
335 return json_is_object ((json_t *)Json);
336}
337
348BOOLEAN
349EFIAPI
351 IN EDKII_JSON_VALUE Json
352 )
353{
354 return json_is_string ((json_t *)Json);
355}
356
366BOOLEAN
367EFIAPI
369 IN EDKII_JSON_VALUE Json
370 )
371{
372 return json_is_integer ((json_t *)Json);
373}
374
384BOOLEAN
385EFIAPI
387 IN EDKII_JSON_VALUE Json
388 )
389{
390 return json_is_number ((json_t *)Json);
391}
392
402BOOLEAN
403EFIAPI
405 IN EDKII_JSON_VALUE Json
406 )
407{
408 return json_is_boolean ((json_t *)Json);
409}
410
420BOOLEAN
421EFIAPI
423 IN EDKII_JSON_VALUE Json
424 )
425{
426 if (json_is_true ((json_t *)Json)) {
427 return TRUE;
428 }
429
430 return FALSE;
431}
432
442BOOLEAN
443EFIAPI
445 IN EDKII_JSON_VALUE Json
446 )
447{
448 if (json_is_false ((json_t *)Json)) {
449 return TRUE;
450 }
451
452 return FALSE;
453}
454
464BOOLEAN
465EFIAPI
467 IN EDKII_JSON_VALUE Json
468 )
469{
470 return json_is_null ((json_t *)Json);
471}
472
483EDKII_JSON_ARRAY
484EFIAPI
486 IN EDKII_JSON_VALUE Json
487 )
488{
489 if ((Json == NULL) || !JsonValueIsArray (Json)) {
490 return NULL;
491 }
492
493 return (EDKII_JSON_ARRAY)Json;
494}
495
506EDKII_JSON_OBJECT
507EFIAPI
509 IN EDKII_JSON_VALUE Json
510 )
511{
512 if ((Json == NULL) || !JsonValueIsObject (Json)) {
513 return NULL;
514 }
515
516 return (EDKII_JSON_OBJECT)Json;
517}
518
529CONST CHAR8 *
530EFIAPI
532 IN EDKII_JSON_VALUE Json
533 )
534{
535 CONST CHAR8 *AsciiStr;
536 UINTN Index;
537
538 AsciiStr = json_string_value ((json_t *)Json);
539 if (AsciiStr == NULL) {
540 return NULL;
541 }
542
543 Index = 0;
544 while (*(AsciiStr + Index) != '\0') {
545 if (((*(AsciiStr + Index)) & 0x80) != 0x00) {
546 return NULL;
547 }
548
549 Index++;
550 }
551
552 return AsciiStr;
553}
554
566CHAR16 *
567EFIAPI
569 IN EDKII_JSON_VALUE Json
570 )
571{
572 EFI_STATUS Status;
573 CONST CHAR8 *Utf8Str;
574 CHAR16 *Ucs2Str;
575
576 Ucs2Str = NULL;
577 Utf8Str = json_string_value ((json_t *)Json);
578 if (Utf8Str == NULL) {
579 return NULL;
580 }
581
582 Status = UTF8StrToUCS2 ((CHAR8 *)Utf8Str, &Ucs2Str);
583 if (EFI_ERROR (Status)) {
584 return NULL;
585 }
586
587 return Ucs2Str;
588}
589
601INT64
602EFIAPI
604 IN EDKII_JSON_VALUE Json
605 )
606{
607 ASSERT (Json != NULL && JsonValueIsInteger (Json));
608 if ((Json == NULL) || !JsonValueIsInteger (Json)) {
609 return 0;
610 }
611
612 return json_integer_value ((json_t *)Json);
613}
614
626BOOLEAN
627EFIAPI
629 IN EDKII_JSON_VALUE Json
630 )
631{
632 ASSERT (Json != NULL && JsonValueIsBoolean (Json));
633 if ((Json == NULL) || !JsonValueIsBoolean (Json)) {
634 return FALSE;
635 }
636
637 return json_is_true ((json_t *)Json);
638}
639
650CONST CHAR8 *
651EFIAPI
653 IN EDKII_JSON_VALUE Json
654 )
655{
656 return json_string_value ((const json_t *)Json);
657}
658
668UINTN
669EFIAPI
671 IN EDKII_JSON_OBJECT JsonObject
672 )
673{
674 return json_object_size ((json_t *)JsonObject);
675}
676
688EFIAPI
690 IN EDKII_JSON_OBJECT JsonObject
691 )
692{
693 if (json_object_clear ((json_t *)JsonObject) != 0) {
694 return EFI_ABORTED;
695 }
696
697 return EFI_SUCCESS;
698}
699
713CHAR8 **
715 IN EDKII_JSON_OBJECT JsonObj,
716 OUT UINTN *KeyCount
717 )
718{
719 UINTN Index;
720 CONST CHAR8 **KeyArray;
721 CONST CHAR8 *Key;
722 EDKII_JSON_VALUE Value;
723
724 if ((JsonObj == NULL) || (KeyCount == NULL)) {
725 return NULL;
726 }
727
728 Index = 0;
729 json_object_foreach (JsonObj, Key, Value) {
730 Index++;
731 }
732 if (Index == 0) {
733 *KeyCount = 0;
734 return NULL;
735 }
736
737 *KeyCount = Index;
738 KeyArray = (CONST CHAR8 **)AllocateZeroPool (*KeyCount * sizeof (CHAR8 *));
739 if (KeyArray == NULL) {
740 return NULL;
741 }
742
743 Key = NULL;
744 Value = NULL;
745 Index = 0;
746 json_object_foreach ((json_t *)JsonObj, Key, Value) {
747 KeyArray[Index] = Key;
748 Index++;
749 }
750
751 return (CHAR8 **)KeyArray;
752}
753
770EDKII_JSON_VALUE
771EFIAPI
773 IN CONST EDKII_JSON_OBJECT JsonObj,
774 IN CONST CHAR8 *Key
775 )
776{
777 return (EDKII_JSON_VALUE)json_object_get ((const json_t *)JsonObj, (const char *)Key);
778}
779
799EFIAPI
801 IN EDKII_JSON_OBJECT JsonObj,
802 IN CONST CHAR8 *Key,
803 IN EDKII_JSON_VALUE Json
804 )
805{
806 if (json_object_set ((json_t *)JsonObj, Key, (json_t *)Json) != 0) {
807 return EFI_ABORTED;
808 } else {
809 return EFI_SUCCESS;
810 }
811}
812
824EFIAPI
826 IN EDKII_JSON_OBJECT JsonObj,
827 IN CONST CHAR8 *Key
828 )
829{
830 if (json_object_del ((json_t *)JsonObj, (const char *)Key) != 0) {
831 return EFI_ABORTED;
832 } else {
833 return EFI_SUCCESS;
834 }
835}
836
846UINTN
847EFIAPI
849 IN EDKII_JSON_ARRAY JsonArray
850 )
851{
852 return json_array_size ((json_t *)JsonArray);
853}
854
871EDKII_JSON_VALUE
872EFIAPI
874 IN EDKII_JSON_ARRAY JsonArray,
875 IN UINTN Index
876 )
877{
878 return (EDKII_JSON_VALUE)json_array_get ((json_t *)JsonArray, Index);
879}
880
895EFIAPI
897 IN EDKII_JSON_ARRAY JsonArray,
898 IN EDKII_JSON_VALUE Json
899 )
900{
901 if (json_array_append ((json_t *)JsonArray, (json_t *)Json) != 0) {
902 return EFI_ABORTED;
903 } else {
904 return EFI_SUCCESS;
905 }
906}
907
923EFIAPI
925 IN EDKII_JSON_ARRAY JsonArray,
926 IN UINTN Index
927 )
928{
929 if (json_array_remove ((json_t *)JsonArray, Index) != 0) {
930 return EFI_ABORTED;
931 } else {
932 return EFI_SUCCESS;
933 }
934}
935
958CHAR8 *
959EFIAPI
961 IN EDKII_JSON_VALUE JsonValue,
962 IN UINTN Flags
963 )
964{
965 if (JsonValue == NULL) {
966 return NULL;
967 }
968
969 return json_dumps ((json_t *)JsonValue, Flags);
970}
971
989EDKII_JSON_VALUE
990EFIAPI
992 IN CONST CHAR8 *String,
993 IN UINT64 Flags,
994 IN EDKII_JSON_ERROR *Error
995 )
996{
997 return (EDKII_JSON_VALUE)json_loads ((const char *)String, Flags, (json_error_t *)Error);
998}
999
1019EDKII_JSON_VALUE
1020EFIAPI
1022 IN CONST CHAR8 *Buffer,
1023 IN UINTN BufferLen,
1024 IN UINTN Flags,
1025 IN OUT EDKII_JSON_ERROR *Error
1026 )
1027{
1028 return json_loadb (Buffer, BufferLen, Flags, (json_error_t *)Error);
1029}
1030
1044VOID
1045EFIAPI
1047 IN EDKII_JSON_VALUE JsonValue
1048 )
1049{
1050 json_decref (JsonValue);
1051}
1052
1065EDKII_JSON_VALUE
1066EFIAPI
1068 IN EDKII_JSON_VALUE JsonValue
1069 )
1070{
1071 return json_incref (JsonValue);
1072}
1073
1081VOID *
1082EFIAPI
1084 IN EDKII_JSON_VALUE JsonValue
1085 )
1086{
1087 return json_object_iter (JsonValue);
1088}
1089
1096EDKII_JSON_VALUE
1097EFIAPI
1099 IN VOID *Iterator
1100 )
1101{
1102 return json_object_iter_value (Iterator);
1103}
1104
1113VOID *
1114EFIAPI
1116 IN EDKII_JSON_VALUE JsonValue,
1117 IN VOID *Iterator
1118 )
1119{
1120 return json_object_iter_next (JsonValue, Iterator);
1121}
1122
1129CHAR8 *
1130EFIAPI
1132 IN VOID *Iterator
1133 )
1134{
1135 return (CHAR8 *)json_object_iter_key (Iterator);
1136}
1137
1144VOID *
1145EFIAPI
1147 IN CHAR8 *Key
1148 )
1149{
1150 return json_object_key_to_iter (Key);
1151}
1152
1160EFIAPI
1162 IN EDKII_JSON_VALUE JsonValue
1163 )
1164{
1165 return (EDKII_JSON_TYPE)(((json_t *)JsonValue)->type);
1166}
1167
1178EFIAPI
1180 IN EFI_HANDLE ImageHandle,
1181 IN EFI_SYSTEM_TABLE *SystemTable
1182 )
1183{
1184 //
1185 // hashtable_seed is initalized by current time while JsonLib is loaded.
1186 // Due to above mechanism, hashtable_seed will be different in each individual
1187 // UEFI driver. As the result, the hash of same key in different UEFI driver
1188 // would be different. This breaks JsonObjectGetValue() because
1189 // JsonObjectGetValue() won't be able to find corresponding JSON value if
1190 // this EDKII_JSON_VALUE is created by another UEFI driver.
1191 //
1192 // Initial the seed to a fixed magic value for JsonLib to be working in all
1193 // UEFI drivers. This fixed number will be removed after the protocol version
1194 // of JsonLib is implemented in the future.
1195 //
1196 hashtable_seed = 0xFDAE2143;
1197
1198 return EFI_SUCCESS;
1199}
UINT64 UINTN
EFI_STATUS UCS2StrToUTF8(IN CHAR16 *Ucs2Str, OUT CHAR8 **Utf8StrAddr)
EFI_STATUS UTF8StrToUCS2(IN CHAR8 *Utf8Str, OUT CHAR16 **Ucs2StrAddr)
VOID *EFIAPI AllocateZeroPool(IN UINTN AllocationSize)
VOID *EFIAPI JsonObjectKeyToIterator(IN CHAR8 *Key)
Definition: JsonLib.c:1146
BOOLEAN EFIAPI JsonValueGetBoolean(IN EDKII_JSON_VALUE Json)
Definition: JsonLib.c:628
EDKII_JSON_VALUE EFIAPI JsonValueInitUnicodeString(IN CHAR16 *String)
Definition: JsonLib.c:130
BOOLEAN EFIAPI JsonValueIsString(IN EDKII_JSON_VALUE Json)
Definition: JsonLib.c:350
EDKII_JSON_VALUE EFIAPI JsonLoadBuffer(IN CONST CHAR8 *Buffer, IN UINTN BufferLen, IN UINTN Flags, IN OUT EDKII_JSON_ERROR *Error)
Definition: JsonLib.c:1021
BOOLEAN EFIAPI JsonValueIsNull(IN EDKII_JSON_VALUE Json)
Definition: JsonLib.c:466
UINTN EFIAPI JsonObjectSize(IN EDKII_JSON_OBJECT JsonObject)
Definition: JsonLib.c:670
CONST CHAR8 *EFIAPI JsonValueGetString(IN EDKII_JSON_VALUE Json)
Definition: JsonLib.c:652
VOID EFIAPI JsonValueFree(IN EDKII_JSON_VALUE Json)
Definition: JsonLib.c:269
EDKII_JSON_VALUE EFIAPI JsonValueInitFalse(VOID)
Definition: JsonLib.c:222
EFI_STATUS EFIAPI JsonArrayAppendValue(IN EDKII_JSON_ARRAY JsonArray, IN EDKII_JSON_VALUE Json)
Definition: JsonLib.c:896
EDKII_JSON_ARRAY EFIAPI JsonValueGetArray(IN EDKII_JSON_VALUE Json)
Definition: JsonLib.c:485
BOOLEAN EFIAPI JsonValueIsFalse(IN EDKII_JSON_VALUE Json)
Definition: JsonLib.c:444
BOOLEAN EFIAPI JsonValueIsInteger(IN EDKII_JSON_VALUE Json)
Definition: JsonLib.c:368
EDKII_JSON_VALUE EFIAPI JsonLoadString(IN CONST CHAR8 *String, IN UINT64 Flags, IN EDKII_JSON_ERROR *Error)
Definition: JsonLib.c:991
EDKII_JSON_VALUE EFIAPI JsonValueInitObject(VOID)
Definition: JsonLib.c:61
CHAR8 *EFIAPI JsonDumpString(IN EDKII_JSON_VALUE JsonValue, IN UINTN Flags)
Definition: JsonLib.c:960
EFI_STATUS EFIAPI JsonObjectClear(IN EDKII_JSON_OBJECT JsonObject)
Definition: JsonLib.c:689
BOOLEAN EFIAPI JsonValueIsNumber(IN EDKII_JSON_VALUE Json)
Definition: JsonLib.c:386
CHAR8 ** JsonObjectGetKeys(IN EDKII_JSON_OBJECT JsonObj, OUT UINTN *KeyCount)
Definition: JsonLib.c:714
VOID EFIAPI JsonDecreaseReference(IN EDKII_JSON_VALUE JsonValue)
Definition: JsonLib.c:1046
EFI_STATUS EFIAPI JsonObjectDelete(IN EDKII_JSON_OBJECT JsonObj, IN CONST CHAR8 *Key)
Definition: JsonLib.c:825
EDKII_JSON_VALUE EFIAPI JsonValueInitNull(VOID)
Definition: JsonLib.c:240
EDKII_JSON_OBJECT EFIAPI JsonValueGetObject(IN EDKII_JSON_VALUE Json)
Definition: JsonLib.c:508
EDKII_JSON_VALUE EFIAPI JsonArrayGetValue(IN EDKII_JSON_ARRAY JsonArray, IN UINTN Index)
Definition: JsonLib.c:873
BOOLEAN EFIAPI JsonValueIsBoolean(IN EDKII_JSON_VALUE Json)
Definition: JsonLib.c:404
EDKII_JSON_VALUE EFIAPI JsonValueClone(IN EDKII_JSON_VALUE Json)
Definition: JsonLib.c:295
EDKII_JSON_VALUE EFIAPI JsonValueInitBoolean(IN BOOLEAN Value)
Definition: JsonLib.c:186
CONST CHAR8 *EFIAPI JsonValueGetAsciiString(IN EDKII_JSON_VALUE Json)
Definition: JsonLib.c:531
CHAR8 *EFIAPI JsonObjectIteratorKey(IN VOID *Iterator)
Definition: JsonLib.c:1131
VOID *EFIAPI JsonObjectIteratorNext(IN EDKII_JSON_VALUE JsonValue, IN VOID *Iterator)
Definition: JsonLib.c:1115
BOOLEAN EFIAPI JsonValueIsArray(IN EDKII_JSON_VALUE Json)
Definition: JsonLib.c:313
EDKII_JSON_VALUE EFIAPI JsonObjectGetValue(IN CONST EDKII_JSON_OBJECT JsonObj, IN CONST CHAR8 *Key)
Definition: JsonLib.c:772
EFI_STATUS EFIAPI JsonArrayRemoveValue(IN EDKII_JSON_ARRAY JsonArray, IN UINTN Index)
Definition: JsonLib.c:924
EDKII_JSON_VALUE EFIAPI JsonValueInitTrue(VOID)
Definition: JsonLib.c:204
EFI_STATUS EFIAPI JsonLibConstructor(IN EFI_HANDLE ImageHandle, IN EFI_SYSTEM_TABLE *SystemTable)
Definition: JsonLib.c:1179
EDKII_JSON_TYPE EFIAPI JsonGetType(IN EDKII_JSON_VALUE JsonValue)
Definition: JsonLib.c:1161
UINTN EFIAPI JsonArrayCount(IN EDKII_JSON_ARRAY JsonArray)
Definition: JsonLib.c:848
EDKII_JSON_VALUE EFIAPI JsonIncreaseReference(IN EDKII_JSON_VALUE JsonValue)
Definition: JsonLib.c:1067
INT64 EFIAPI JsonValueGetInteger(IN EDKII_JSON_VALUE Json)
Definition: JsonLib.c:603
EDKII_JSON_VALUE EFIAPI JsonValueInitArray(VOID)
Definition: JsonLib.c:39
EDKII_JSON_VALUE EFIAPI JsonValueInitAsciiString(IN CONST CHAR8 *String)
Definition: JsonLib.c:89
EFI_STATUS EFIAPI JsonObjectSetValue(IN EDKII_JSON_OBJECT JsonObj, IN CONST CHAR8 *Key, IN EDKII_JSON_VALUE Json)
Definition: JsonLib.c:800
EDKII_JSON_VALUE EFIAPI JsonObjectIteratorValue(IN VOID *Iterator)
Definition: JsonLib.c:1098
VOID *EFIAPI JsonObjectIterator(IN EDKII_JSON_VALUE JsonValue)
Definition: JsonLib.c:1083
CHAR16 *EFIAPI JsonValueGetUnicodeString(IN EDKII_JSON_VALUE Json)
Definition: JsonLib.c:568
EDKII_JSON_VALUE EFIAPI JsonValueInitInteger(IN INT64 Value)
Definition: JsonLib.c:166
BOOLEAN EFIAPI JsonValueIsTrue(IN EDKII_JSON_VALUE Json)
Definition: JsonLib.c:422
BOOLEAN EFIAPI JsonValueIsObject(IN EDKII_JSON_VALUE Json)
Definition: JsonLib.c:331
EDKII_JSON_TYPE
Definition: JsonLib.h:81
#define NULL
Definition: Base.h:319
#define CONST
Definition: Base.h:259
#define TRUE
Definition: Base.h:301
#define FALSE
Definition: Base.h:307
#define IN
Definition: Base.h:279
#define OUT
Definition: Base.h:284
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:29
VOID * EFI_HANDLE
Definition: UefiBaseType.h:33
#define EFI_SUCCESS
Definition: UefiBaseType.h:112