Skip to content

Commit

Permalink
OcSerializeLib: Implement context for better error reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
vit9696 committed May 29, 2020
1 parent 87ab56e commit a04f236
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 63 deletions.
56 changes: 31 additions & 25 deletions Include/Acidanthera/Library/OcSerializeLib.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ typedef union OC_SCHEMA_INFO_ OC_SCHEMA_INFO;
typedef
VOID
(*OC_APPLY) (
VOID *Serialized,
XML_NODE *Node,
OC_SCHEMA_INFO *Info
OUT VOID *Serialized,
IN XML_NODE *Node,
IN OC_SCHEMA_INFO *Info,
IN CONST CHAR8 *Context OPTIONAL
);

//
Expand Down Expand Up @@ -150,49 +151,53 @@ struct OC_SCHEMA_ {
//
OC_SCHEMA *
LookupConfigSchema (
OC_SCHEMA *SortedList,
UINT32 Size,
CONST CHAR8 *Name
IN OC_SCHEMA *SortedList,
IN UINT32 Size,
IN CONST CHAR8 *Name
);

//
// Apply interface to parse serialized dictionaries
//
VOID
ParseSerializedDict (
VOID *Serialized,
XML_NODE *Node,
OC_SCHEMA_INFO *Info
OUT VOID *Serialized,
IN XML_NODE *Node,
IN OC_SCHEMA_INFO *Info,
IN CONST CHAR8 *Context OPTIONAL
);

//
// Apply interface to parse serialized OC_SCHEMA_VALUE_TYPE.
//
VOID
ParseSerializedValue (
VOID *Serialized,
XML_NODE *Node,
OC_SCHEMA_INFO *Info
OUT VOID *Serialized,
IN XML_NODE *Node,
IN OC_SCHEMA_INFO *Info,
IN CONST CHAR8 *Context OPTIONAL
);

//
// Apply interface to parse serialized OC_SCHEMA_BLOB_TYPE.
//
VOID
ParseSerializedBlob (
VOID *Serialized,
XML_NODE *Node,
OC_SCHEMA_INFO *Info
OUT VOID *Serialized,
IN XML_NODE *Node,
IN OC_SCHEMA_INFO *Info,
IN CONST CHAR8 *Context OPTIONAL
);

//
// Apply interface to parse serialized array
//
VOID
ParseSerializedArray (
VOID *Serialized,
XML_NODE *Node,
OC_SCHEMA_INFO *Info
OUT VOID *Serialized,
IN XML_NODE *Node,
IN OC_SCHEMA_INFO *Info,
IN CONST CHAR8 *Context OPTIONAL
);

//
Expand All @@ -201,9 +206,10 @@ ParseSerializedArray (
//
VOID
ParseSerializedMap (
VOID *Serialized,
XML_NODE *Node,
OC_SCHEMA_INFO *Info
OUT VOID *Serialized,
IN XML_NODE *Node,
IN OC_SCHEMA_INFO *Info,
IN CONST CHAR8 *Context OPTIONAL
);

//
Expand All @@ -212,10 +218,10 @@ ParseSerializedMap (
//
BOOLEAN
ParseSerialized (
VOID *Serialized,
OC_SCHEMA_INFO *RootSchema,
VOID *PlistBuffer,
UINT32 PlistSize
OUT VOID *Serialized,
IN OC_SCHEMA_INFO *RootSchema,
IN VOID *PlistBuffer,
IN UINT32 PlistSize
);

//
Expand Down
88 changes: 50 additions & 38 deletions Library/OcSerializeLib/OcSerializeLib.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ mSchemaTypeNames[] = {
[OC_SCHEMA_VALUE_MDATA] = "mdata"
};

STATIC
CONST CHAR8 *
GetSchemaTypeName (
IN UINT32 Type
Expand All @@ -43,9 +44,9 @@ GetSchemaTypeName (

OC_SCHEMA *
LookupConfigSchema (
OC_SCHEMA *SortedList,
UINT32 Size,
CONST CHAR8 *Name
IN OC_SCHEMA *SortedList,
IN UINT32 Size,
IN CONST CHAR8 *Name
)
{
UINT32 Start;
Expand Down Expand Up @@ -86,9 +87,10 @@ LookupConfigSchema (

VOID
ParseSerializedDict (
VOID *Serialized,
XML_NODE *Node,
OC_SCHEMA_INFO *Info
OUT VOID *Serialized,
IN XML_NODE *Node,
IN OC_SCHEMA_INFO *Info,
IN CONST CHAR8 *Context OPTIONAL
)
{
UINT32 DictSize;
Expand All @@ -104,7 +106,7 @@ ParseSerializedDict (
CurrentKey = PlistKeyValue (PlistDictChild (Node, Index, &CurrentValue));

if (CurrentKey == NULL) {
DEBUG ((DEBUG_WARN, "OCS: No serialized key at %u index!\n", Index));
DEBUG ((DEBUG_WARN, "OCS: No serialized key at %u index, context <%a>!\n", Index, Context));
continue;
}

Expand All @@ -123,7 +125,7 @@ ParseSerializedDict (
NewSchema = LookupConfigSchema (Info->Dict.Schema, Info->Dict.SchemaSize, CurrentKey);

if (NewSchema == NULL) {
DEBUG ((DEBUG_WARN, "OCS: No schema for %a at %u index!\n", CurrentKey, Index));
DEBUG ((DEBUG_WARN, "OCS: No schema for %a at %u index, context <%a>!\n", CurrentKey, Index, Context));
continue;
}

Expand All @@ -132,24 +134,26 @@ ParseSerializedDict (
if (CurrentValue == NULL) {
DEBUG ((
DEBUG_WARN,
"OCS: No type match for %a at %u index, expected type %a got %a!\n",
"OCS: No type match for %a at %u index, expected type %a got %a, context <%a>!\n",
CurrentKey,
Index,
GetSchemaTypeName (NewSchema->Type),
XmlNodeName (OldValue)
XmlNodeName (OldValue),
Context
));
continue;
}

NewSchema->Apply (Serialized, CurrentValue, &NewSchema->Info);
NewSchema->Apply (Serialized, CurrentValue, &NewSchema->Info, CurrentKey);
}
}

VOID
ParseSerializedValue (
VOID *Serialized,
XML_NODE *Node,
OC_SCHEMA_INFO *Info
OUT VOID *Serialized,
IN XML_NODE *Node,
IN OC_SCHEMA_INFO *Info,
IN CONST CHAR8 *Context OPTIONAL
)
{
BOOLEAN Result;
Expand Down Expand Up @@ -181,19 +185,21 @@ ParseSerializedValue (
if (Result == FALSE) {
DEBUG ((
DEBUG_WARN,
"OCS: Failed to parse %a field as value with type %a and <%a> contents\n",
"OCS: Failed to parse %a field as value with type %a and <%a> contents, context <%a>!\n",
XmlNodeName (Node),
GetSchemaTypeName (Info->Value.Type),
XmlNodeContent (Node) != NULL ? XmlNodeContent (Node) : "empty"
XmlNodeContent (Node) != NULL ? XmlNodeContent (Node) : "empty",
Context
));
}
}

VOID
ParseSerializedBlob (
VOID *Serialized,
XML_NODE *Node,
OC_SCHEMA_INFO *Info
OUT VOID *Serialized,
IN XML_NODE *Node,
IN OC_SCHEMA_INFO *Info,
IN CONST CHAR8 *Context OPTIONAL
)
{
BOOLEAN Result;
Expand All @@ -219,10 +225,11 @@ ParseSerializedBlob (
if (Result == FALSE) {
DEBUG ((
DEBUG_WARN,
"OCS: Failed to calculate size of %a field containing <%a> as type %a\n",
"OCS: Failed to calculate size of %a field containing <%a> as type %a, context <%a>!\n",
XmlNodeName (Node),
XmlNodeContent (Node) != NULL ? XmlNodeContent (Node) : "empty",
GetSchemaTypeName (Info->Blob.Type)
GetSchemaTypeName (Info->Blob.Type),
Context
));
return;
}
Expand All @@ -233,10 +240,11 @@ ParseSerializedBlob (
if (BlobMemory == NULL) {
DEBUG ((
DEBUG_INFO,
"OCS: Failed to allocate %u bytes %a field of type %a\n",
"OCS: Failed to allocate %u bytes %a field of type %a, context <%a>!\n",
Size,
XmlNodeName (Node),
GetSchemaTypeName (Info->Value.Type)
GetSchemaTypeName (Info->Value.Type),
Context
));
return;
}
Expand All @@ -258,19 +266,21 @@ ParseSerializedBlob (
if (Result == FALSE) {
DEBUG ((
DEBUG_WARN,
"OCS: Failed to parse %a field as blob with type %a and <%a> contents\n",
"OCS: Failed to parse %a field as blob with type %a and <%a> contents, context <%a>!\n",
XmlNodeName (Node),
GetSchemaTypeName (Info->Value.Type),
XmlNodeContent (Node) != NULL ? XmlNodeContent (Node) : "empty"
XmlNodeContent (Node) != NULL ? XmlNodeContent (Node) : "empty",
Context
));
}
}

VOID
ParseSerializedMap (
VOID *Serialized,
XML_NODE *Node,
OC_SCHEMA_INFO *Info
OUT VOID *Serialized,
IN XML_NODE *Node,
IN OC_SCHEMA_INFO *Info,
IN CONST CHAR8 *Context OPTIONAL
)
{
UINT32 DictSize;
Expand Down Expand Up @@ -323,15 +333,16 @@ ParseSerializedMap (
DEBUG ((DEBUG_INFO, "OCS: Couldn't allocate key name at %u index!\n", Index));
}

Info->List.Schema->Apply (NewValue, ChildNode, &Info->List.Schema->Info);
Info->List.Schema->Apply (NewValue, ChildNode, &Info->List.Schema->Info, CurrentKey);
}
}

VOID
ParseSerializedArray (
VOID *Serialized,
XML_NODE *Node,
OC_SCHEMA_INFO *Info
OUT VOID *Serialized,
IN XML_NODE *Node,
IN OC_SCHEMA_INFO *Info,
IN CONST CHAR8 *Context OPTIONAL
)
{
UINT32 ArraySize;
Expand Down Expand Up @@ -362,16 +373,16 @@ ParseSerializedArray (
continue;
}

Info->List.Schema->Apply (NewValue, ChildNode, &Info->List.Schema->Info);
Info->List.Schema->Apply (NewValue, ChildNode, &Info->List.Schema->Info, Context);
}
}

BOOLEAN
ParseSerialized (
VOID *Serialized,
OC_SCHEMA_INFO *RootSchema,
VOID *PlistBuffer,
UINT32 PlistSize
OUT VOID *Serialized,
IN OC_SCHEMA_INFO *RootSchema,
IN VOID *PlistBuffer,
IN UINT32 PlistSize
)
{
XML_DOCUMENT *Document;
Expand All @@ -395,7 +406,8 @@ ParseSerialized (
ParseSerializedDict (
Serialized,
RootDict,
RootSchema
RootSchema,
"root"
);

XmlDocumentFree (Document);
Expand Down

0 comments on commit a04f236

Please sign in to comment.