OcSerializeLib.c 10.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/** @file

OcSerializeLib

Copyright (c) 2018, vit9696

All rights reserved.

This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution.  The full text of the license may be found at
http://opensource.org/licenses/bsd-license.php

THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.

**/

#include <Library/OcSerializeLib.h>
#include <Library/BaseLib.h>
#include <Library/DebugLib.h>

23 24
#if !defined(MDEPKG_NDEBUG)

25 26 27 28 29 30 31 32 33 34
STATIC
CONST CHAR8 *
mSchemaTypeNames[] = {
  [OC_SCHEMA_VALUE_BOOLEAN] = "boolean",
  [OC_SCHEMA_VALUE_INTEGER] = "integer",
  [OC_SCHEMA_VALUE_DATA] = "data",
  [OC_SCHEMA_VALUE_STRING] = "string",
  [OC_SCHEMA_VALUE_MDATA] = "mdata"
};

35
STATIC
36 37 38 39 40 41 42 43 44 45 46
CONST CHAR8 *
GetSchemaTypeName (
  IN UINT32 Type
  )
{
  if (Type < ARRAY_SIZE (mSchemaTypeNames)) {
    return mSchemaTypeNames[Type];
  }
  return "custom";
}

47 48
#endif

49 50
OC_SCHEMA *
LookupConfigSchema (
51 52 53
  IN OC_SCHEMA      *SortedList,
  IN UINT32         Size,
  IN CONST CHAR8    *Name
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
  )
{
  UINT32  Start;
  UINT32  End;
  UINT32  Curr;
  INTN    Cmp;

  if (Size == 0) {
    return NULL;
  }

  //
  // Classic binary search in a sorted string list.
  //
  Start = 0;
  End   = Size - 1;

  while (Start <= End) {
    Curr = (Start + End) / 2;
    Cmp = AsciiStrCmp (SortedList[Curr].Name, Name);

    if (Cmp == 0) {
      return &SortedList[Curr];
    } else if (Cmp < 0) {
      Start = Curr + 1;
    } else if (Curr > 0) {
      End = Curr - 1;
    } else {
      //
      // Even the first element does not match, required due to unsigned End.
      //
      return NULL;
    }
  }

  return NULL;
}

VOID
ParseSerializedDict (
94 95 96 97
  OUT VOID            *Serialized,
  IN  XML_NODE        *Node,
  IN  OC_SCHEMA_INFO  *Info,
  IN  CONST CHAR8     *Context  OPTIONAL
98 99 100 101
  )
{
  UINT32         DictSize;
  UINT32         Index;
102
  UINT32         Index2;
103 104
  CONST CHAR8    *CurrentKey;
  XML_NODE       *CurrentValue;
105
  XML_NODE       *OldValue;
106 107 108 109 110 111 112 113
  OC_SCHEMA      *NewSchema;

  DictSize = PlistDictChildren (Node);

  for (Index = 0; Index < DictSize; Index++) {
    CurrentKey = PlistKeyValue (PlistDictChild (Node, Index, &CurrentValue));

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

118 119 120 121 122 123 124
    //
    // Skip comments.
    //
    if (CurrentKey[0] == '#') {
      continue;
    }

125
    DEBUG ((DEBUG_VERBOSE, "OCS: Parsing serialized at %a at %u index!\n", CurrentKey, Index));
126 127 128 129 130 131 132

    //
    // We do not protect from duplicating serialized entries.
    //
    NewSchema = LookupConfigSchema (Info->Dict.Schema, Info->Dict.SchemaSize, CurrentKey);

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

137
    OldValue = CurrentValue;
138 139
    CurrentValue = PlistNodeCast (CurrentValue, NewSchema->Type);
    if (CurrentValue == NULL) {
140 141
      DEBUG ((
        DEBUG_WARN,
142
        "OCS: No type match for %a at %u index, expected type %a got %a, context <%a>!\n",
143 144 145
        CurrentKey,
        Index,
        GetSchemaTypeName (NewSchema->Type),
146 147
        XmlNodeName (OldValue),
        Context
148
        ));
149 150 151
      continue;
    }

152
    NewSchema->Apply (Serialized, CurrentValue, &NewSchema->Info, CurrentKey);
153
  }
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184

  DEBUG_CODE_BEGIN ();

  for (Index = 0; Index < Info->Dict.SchemaSize; ++Index) {
    if (Info->Dict.Schema[Index].Optional) {
      continue;
    }

    for (Index2 = 0; Index2 < DictSize; ++Index2) {
      CurrentKey = PlistKeyValue (PlistDictChild (Node, Index2, NULL));

      if (CurrentKey == NULL) {
        continue;
      }

      if (AsciiStrCmp (CurrentKey, Info->Dict.Schema[Index].Name) == 0) {
        break;
      }
    }

    if (Index2 == DictSize) {
      DEBUG ((
        DEBUG_WARN,
        "OCS: Missing key %a, context <%a>!\n",
        Info->Dict.Schema[Index].Name,
        Context
        ));
    }
  }

  DEBUG_CODE_END ();
185 186 187 188
}

VOID
ParseSerializedValue (
189 190 191 192
  OUT VOID            *Serialized,
  IN  XML_NODE        *Node,
  IN  OC_SCHEMA_INFO  *Info,
  IN  CONST CHAR8     *Context  OPTIONAL
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
  )
{
  BOOLEAN  Result;
  VOID     *Field;
  UINT32   Size;

  Result = FALSE;
  Field  = OC_SCHEMA_FIELD (Serialized, VOID, Info->Value.Field);
  Size   = Info->Value.FieldSize;

  switch (Info->Value.Type) {
    case OC_SCHEMA_VALUE_BOOLEAN:
      Result = PlistBooleanValue (Node, (BOOLEAN *) Field);
      break;
    case OC_SCHEMA_VALUE_INTEGER:
208
      Result = PlistIntegerValue (Node, Field, Size, FALSE);
209 210 211 212 213 214 215 216 217 218 219 220 221
      break;
    case OC_SCHEMA_VALUE_DATA:
      Result = PlistDataValue (Node, Field, &Size);
      break;
    case OC_SCHEMA_VALUE_STRING:
      Result = PlistStringValue (Node, Field, &Size);
      break;
    case OC_SCHEMA_VALUE_MDATA:
      Result = PlistMetaDataValue (Node, Field, &Size);
      break;
  }

  if (Result == FALSE) {
222
    DEBUG ((
223
      DEBUG_WARN,
224
      "OCS: Failed to parse %a field as value with type %a and <%a> contents, context <%a>!\n",
225 226
      XmlNodeName (Node),
      GetSchemaTypeName (Info->Value.Type),
227 228
      XmlNodeContent (Node) != NULL ? XmlNodeContent (Node) : "empty",
      Context
229
      ));
230 231 232 233 234
  }
}

VOID
ParseSerializedBlob (
235 236 237 238
  OUT VOID            *Serialized,
  IN  XML_NODE        *Node,
  IN  OC_SCHEMA_INFO  *Info,
  IN  CONST CHAR8     *Context  OPTIONAL
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
  )
{
  BOOLEAN  Result;
  VOID     *Field;
  UINT32   Size;
  VOID     *BlobMemory;
  UINT32   *BlobSize;

  Result = FALSE;

  switch (Info->Blob.Type) {
    case OC_SCHEMA_BLOB_DATA:
      Result = PlistDataSize (Node, &Size);
      break;
    case OC_SCHEMA_BLOB_STRING:
      Result = PlistStringSize (Node, &Size);
      break;
    case OC_SCHEMA_BLOB_MDATA:
      Result = PlistMetaDataSize (Node, &Size);
      break;
  }

  if (Result == FALSE) {
262 263
    DEBUG ((
      DEBUG_WARN,
264
      "OCS: Failed to calculate size of %a field containing <%a> as type %a, context <%a>!\n",
265
      XmlNodeName (Node),
266
      XmlNodeContent (Node) != NULL ? XmlNodeContent (Node) : "empty",
267 268
      GetSchemaTypeName (Info->Blob.Type),
      Context
269
      ));
270 271 272 273 274 275 276
    return;
  }

  Field  = OC_SCHEMA_FIELD (Serialized, VOID, Info->Blob.Field);
  BlobMemory = OcBlobAllocate (Field, Size, &BlobSize);

  if (BlobMemory == NULL) {
277 278
    DEBUG ((
      DEBUG_INFO,
279
      "OCS: Failed to allocate %u bytes %a field of type %a, context <%a>!\n",
280 281
      Size,
      XmlNodeName (Node),
282 283
      GetSchemaTypeName (Info->Value.Type),
      Context
284
      ));
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
    return;
  }

  Result = FALSE;

  switch (Info->Blob.Type) {
    case OC_SCHEMA_BLOB_DATA:
      Result = PlistDataValue (Node, (UINT8 *) BlobMemory, BlobSize);
      break;
    case OC_SCHEMA_BLOB_STRING:
      Result = PlistStringValue (Node, (CHAR8 *) BlobMemory, BlobSize);
      break;
    case OC_SCHEMA_BLOB_MDATA:
      Result = PlistMetaDataValue (Node, (UINT8 *) BlobMemory, BlobSize);
      break;
  }

  if (Result == FALSE) {
303 304
    DEBUG ((
      DEBUG_WARN,
305
      "OCS: Failed to parse %a field as blob with type %a and <%a> contents, context <%a>!\n",
306 307
      XmlNodeName (Node),
      GetSchemaTypeName (Info->Value.Type),
308 309
      XmlNodeContent (Node) != NULL ? XmlNodeContent (Node) : "empty",
      Context
310
      ));
311 312 313 314 315
  }
}

VOID
ParseSerializedMap (
316 317 318 319
  OUT VOID            *Serialized,
  IN  XML_NODE        *Node,
  IN  OC_SCHEMA_INFO  *Info,
  IN  CONST CHAR8     *Context  OPTIONAL
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
  )
{
  UINT32       DictSize;
  UINT32       Index;
  CONST CHAR8  *CurrentKey;
  UINT32       CurrentKeyLen;
  XML_NODE     *ChildNode;
  VOID         *NewValue;
  VOID         *NewKey;
  VOID         *NewKeyValue;
  BOOLEAN      Success;

  DictSize = PlistDictChildren (Node);

  for (Index = 0; Index < DictSize; Index++) {
    CurrentKey = PlistKeyValue (PlistDictChild (Node, Index, &ChildNode));
    CurrentKeyLen = CurrentKey != NULL ? (UINT32) (AsciiStrLen (CurrentKey) + 1) : 0;

    if (CurrentKeyLen == 0) {
339
      DEBUG ((DEBUG_INFO, "OCS: No get serialized key at %u index!\n", Index));
340 341 342
      continue;
    }

343 344 345 346 347 348 349
    //
    // Skip comments.
    //
    if (CurrentKey[0] == '#') {
      continue;
    }

350
    if (PlistNodeCast (ChildNode, Info->List.Schema->Type) == NULL) {
351
      DEBUG ((DEBUG_INFO, "OCS: No valid serialized value at %u index!\n", Index));
352 353 354 355 356 357 358 359 360
      continue;
    }

    Success = OcListEntryAllocate (
      OC_SCHEMA_FIELD (Serialized, VOID, Info->List.Field),
      &NewValue,
      &NewKey
      );
    if (Success == FALSE) {
361
      DEBUG ((DEBUG_INFO, "OCS: Couldn't insert dict serialized at %u index!\n", Index));
362 363 364 365 366 367 368
      continue;
    }

    NewKeyValue = OcBlobAllocate (NewKey, CurrentKeyLen, NULL);
    if (NewKeyValue != NULL) {
      AsciiStrnCpyS ((CHAR8 *) NewKeyValue, CurrentKeyLen, CurrentKey, CurrentKeyLen - 1);
    } else {
369
      DEBUG ((DEBUG_INFO, "OCS: Couldn't allocate key name at %u index!\n", Index));
370 371
    }

372
    Info->List.Schema->Apply (NewValue, ChildNode, &Info->List.Schema->Info, CurrentKey);
373 374 375 376 377
  }
}

VOID
ParseSerializedArray (
378 379 380 381
  OUT VOID            *Serialized,
  IN  XML_NODE        *Node,
  IN  OC_SCHEMA_INFO  *Info,
  IN  CONST CHAR8     *Context  OPTIONAL
382 383 384 385 386 387 388 389 390 391 392 393 394
  )
{
  UINT32    ArraySize;
  UINT32    Index;
  XML_NODE  *ChildNode;
  VOID      *NewValue;
  BOOLEAN   Success;

  ArraySize = XmlNodeChildren (Node);

  for (Index = 0; Index < ArraySize; Index++) {
    ChildNode = PlistNodeCast (XmlNodeChild (Node, Index), Info->List.Schema->Type);

395
    DEBUG ((DEBUG_VERBOSE, "OCS: Processing array %u/%u element\n", Index + 1, ArraySize));
396 397

    if (ChildNode == NULL) {
398
      DEBUG ((DEBUG_INFO, "OCS: Couldn't get array serialized at %u index!\n", Index));
399 400 401 402 403 404 405 406 407
      continue;
    }

    Success = OcListEntryAllocate (
      OC_SCHEMA_FIELD (Serialized, VOID, Info->List.Field),
      &NewValue,
      NULL
      );
    if (Success == FALSE) {
408
      DEBUG ((DEBUG_INFO, "OCS: Couldn't insert array serialized at %u index!\n", Index));
409 410 411
      continue;
    }

412
    Info->List.Schema->Apply (NewValue, ChildNode, &Info->List.Schema->Info, Context);
413 414 415 416 417
  }
}

BOOLEAN
ParseSerialized (
418 419 420 421
  OUT VOID            *Serialized,
  IN  OC_SCHEMA_INFO  *RootSchema,
  IN  VOID            *PlistBuffer,
  IN  UINT32          PlistSize
422 423 424 425 426
  )
{
  XML_DOCUMENT        *Document;
  XML_NODE            *RootDict;

427
  Document = XmlDocumentParse (PlistBuffer, PlistSize, FALSE);
428 429

  if (Document == NULL) {
430
    DEBUG ((DEBUG_INFO, "OCS: Couldn't parse serialized file!\n"));
431 432 433 434 435 436
    return FALSE;
  }

  RootDict = PlistNodeCast (PlistDocumentRoot (Document), PLIST_NODE_TYPE_DICT);

  if (RootDict == NULL) {
437
    DEBUG ((DEBUG_INFO, "OCS: Couldn't get serialized root!\n"));
438 439 440 441 442 443 444
    XmlDocumentFree (Document);
    return FALSE;
  }

  ParseSerializedDict (
    Serialized,
    RootDict,
445 446
    RootSchema,
    "root"
447 448 449 450 451
    );

  XmlDocumentFree (Document);
  return TRUE;
}