/* Copyright (c) 2015 Alex Diener This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution. Alex Diener alex@ludobloom.com */ #include "dynamictypes/DataArray.h" #include "dynamictypes/DataAssociativeArray.h" #include "dynamictypes/DataHashTable.h" #include "dynamictypes/DataSerializer.h" #include #define enumKV(value) {#value, value} #define ALL_DATA_TYPE_ENUM_KEYS_AND_VALUES { \ enumKV(DATA_TYPE_BOOLEAN), \ enumKV(DATA_TYPE_INT8), \ enumKV(DATA_TYPE_UINT8), \ enumKV(DATA_TYPE_INT16), \ enumKV(DATA_TYPE_UINT16), \ enumKV(DATA_TYPE_INT32), \ enumKV(DATA_TYPE_UINT32), \ enumKV(DATA_TYPE_INT64), \ enumKV(DATA_TYPE_UINT64), \ enumKV(DATA_TYPE_FLOAT), \ enumKV(DATA_TYPE_DOUBLE), \ enumKV(DATA_TYPE_STRING), \ enumKV(DATA_TYPE_BLOB), \ enumKV(DATA_TYPE_HASH_TABLE), \ enumKV(DATA_TYPE_ARRAY), \ enumKV(DATA_TYPE_ASSOCIATIVE_ARRAY), \ enumKV(DATA_TYPE_FIXED_16_16) \ } static void deserializeContainer(DataValue * container, DeserializationContext * context) { size_t index, count; const char * key = NULL; DataValueType type; DataValue value; const void * blob; size_t length = 0; count = call_virtual(beginArray, context, NULL); if (context->status != SERIALIZATION_ERROR_OK) { return; } for (index = 0; index < count; index++) { if (container->type == DATA_TYPE_HASH_TABLE || container->type == DATA_TYPE_ASSOCIATIVE_ARRAY) { key = call_virtual(readString, context, NULL); if (context->status != SERIALIZATION_ERROR_OK) { return; } index++; } Serialization_enumKeyValue dataTypeValues[] = ALL_DATA_TYPE_ENUM_KEYS_AND_VALUES; type = call_virtual(readEnumeration, context, NULL, sizeof(dataTypeValues) / sizeof(dataTypeValues[0]), dataTypeValues); if (context->status != SERIALIZATION_ERROR_OK) { return; } index++; switch (type) { case DATA_TYPE_BOOLEAN: value = valueCreateBoolean(call_virtual(readBoolean, context, NULL)); break; case DATA_TYPE_INT8: value = valueCreateInt8(call_virtual(readInt8, context, NULL)); break; case DATA_TYPE_UINT8: value = valueCreateUInt8(call_virtual(readUInt8, context, NULL)); break; case DATA_TYPE_INT16: value = valueCreateInt16(call_virtual(readInt16, context, NULL)); break; case DATA_TYPE_UINT16: value = valueCreateUInt16(call_virtual(readUInt16, context, NULL)); break; case DATA_TYPE_INT32: value = valueCreateInt32(call_virtual(readInt32, context, NULL)); break; case DATA_TYPE_UINT32: value = valueCreateUInt32(call_virtual(readUInt32, context, NULL)); break; case DATA_TYPE_INT64: value = valueCreateInt64(call_virtual(readInt64, context, NULL)); break; case DATA_TYPE_UINT64: value = valueCreateUInt64(call_virtual(readUInt64, context, NULL)); break; case DATA_TYPE_FLOAT: value = valueCreateFloat(call_virtual(readFloat, context, NULL)); break; case DATA_TYPE_DOUBLE: value = valueCreateDouble(call_virtual(readDouble, context, NULL)); break; case DATA_TYPE_FIXED_16_16: value = valueCreateFixed16_16(call_virtual(readFixed16_16, context, NULL)); break; case DATA_TYPE_STRING: value = valueCreateString(call_virtual(readString, context, NULL), DATA_USE_STRLEN, true, true); break; case DATA_TYPE_BLOB: blob = call_virtual(readBlob, context, NULL, &length); if (context->status != SERIALIZATION_ERROR_OK) { return; } value = valueCreateBlob(blob, length, true, true); break; case DATA_TYPE_ARRAY: value = valueCreateArray(arrayCreate(), true, false); deserializeContainer(&value, context); break; case DATA_TYPE_HASH_TABLE: value = valueCreateHashTable(hashCreate(), true, false); deserializeContainer(&value, context); break; case DATA_TYPE_ASSOCIATIVE_ARRAY: value = valueCreateAssociativeArray(associativeArrayCreate(), true, false); deserializeContainer(&value, context); break; case DATA_TYPE_POINTER: break; } if (context->status != SERIALIZATION_ERROR_OK) { return; } switch (container->type) { case DATA_TYPE_ARRAY: arrayAppend(container->value.array, value); break; case DATA_TYPE_HASH_TABLE: hashSet(container->value.hashTable, key, value); break; case DATA_TYPE_ASSOCIATIVE_ARRAY: associativeArrayAppend(container->value.associativeArray, key, value); break; default: break; } } call_virtual(endArray, context); } DataValue DataValue_deserializeGeneric(compat_type(DeserializationContext *) deserializationContext) { DeserializationContext * context = deserializationContext; DataValue value = {{.boolean = false}, DATA_TYPE_BOOLEAN, false}; const char * type; call_virtual(beginArray, context, "data"); type = call_virtual(readString, context, NULL); if (!strcmp(type, "array")) { value.type = DATA_TYPE_ARRAY; value.value.array = arrayCreate(); } else if (!strcmp(type, "structure")) { value.type = DATA_TYPE_HASH_TABLE; value.value.hashTable = hashCreate(); } else if (!strcmp(type, "dictionary")) { value.type = DATA_TYPE_ASSOCIATIVE_ARRAY; value.value.associativeArray = associativeArrayCreate(); } deserializeContainer(&value, context); if (context->status != SERIALIZATION_ERROR_OK) { return valueCreateBoolean(false); } call_virtual(endArray, context); if (context->status != SERIALIZATION_ERROR_OK) { return valueCreateBoolean(false); } return value; } static void serializeContainer(DataValue * value, SerializationContext * context); static void serializeValue(DataValue * value, SerializationContext * context) { Serialization_enumKeyValue dataTypeValues[] = ALL_DATA_TYPE_ENUM_KEYS_AND_VALUES; call_virtual(writeEnumeration, context, NULL, value->type, sizeof(dataTypeValues) / sizeof(dataTypeValues[0]), dataTypeValues); switch (value->type) { case DATA_TYPE_BOOLEAN: call_virtual(writeBoolean, context, NULL, value->value.boolean); break; case DATA_TYPE_INT8: call_virtual(writeInt8, context, NULL, value->value.int8); break; case DATA_TYPE_UINT8: call_virtual(writeUInt8, context, NULL, value->value.uint8); break; case DATA_TYPE_INT16: call_virtual(writeInt16, context, NULL, value->value.int16); break; case DATA_TYPE_UINT16: call_virtual(writeUInt16, context, NULL, value->value.uint16); break; case DATA_TYPE_INT32: call_virtual(writeInt32, context, NULL, value->value.int32); break; case DATA_TYPE_UINT32: call_virtual(writeUInt32, context, NULL, value->value.uint32); break; case DATA_TYPE_INT64: call_virtual(writeInt64, context, NULL, value->value.int64); break; case DATA_TYPE_UINT64: call_virtual(writeUInt64, context, NULL, value->value.uint64); break; case DATA_TYPE_FLOAT: call_virtual(writeFloat, context, NULL, value->value.float32); break; case DATA_TYPE_DOUBLE: call_virtual(writeDouble, context, NULL, value->value.float64); break; case DATA_TYPE_FIXED_16_16: call_virtual(writeFixed16_16, context, NULL, value->value.fixed); break; case DATA_TYPE_POINTER: break; case DATA_TYPE_STRING: call_virtual(writeString, context, NULL, value->value.string); break; case DATA_TYPE_BLOB: call_virtual(writeBlob, context, NULL, value->value.blob.bytes, value->value.blob.length); break; case DATA_TYPE_HASH_TABLE: case DATA_TYPE_ARRAY: case DATA_TYPE_ASSOCIATIVE_ARRAY: serializeContainer(value, context); break; } } static void serializeContainer(DataValue * container, SerializationContext * context) { switch (container->type) { case DATA_TYPE_ARRAY: call_virtual(beginArray, context, NULL); for (size_t index = 0; index < container->value.array->count; index++) { DataValue * value = arrayGet(container->value.array, index); if (value->type == DATA_TYPE_POINTER) { continue; } serializeValue(value, context); } call_virtual(endArray, context); break; case DATA_TYPE_HASH_TABLE: { call_virtual(beginArray, context, NULL); size_t count; const char ** keys = hashGetKeys(container->value.hashTable, &count); for (size_t index = 0; index < count; index++) { DataValue * value = hashGet(container->value.hashTable, keys[index]); if (value->type == DATA_TYPE_POINTER) { continue; } call_virtual(writeString, context, NULL, keys[index]); serializeValue(value, context); } free(keys); call_virtual(endArray, context); break; } case DATA_TYPE_ASSOCIATIVE_ARRAY: call_virtual(beginArray, context, NULL); for (size_t index = 0; index < container->value.associativeArray->count; index++) { DataValue * value = associativeArrayGetValueAtIndex(container->value.associativeArray, index); if (value->type == DATA_TYPE_POINTER) { continue; } call_virtual(writeString, context, NULL, associativeArrayGetKeyAtIndex(container->value.associativeArray, index)); serializeValue(value, context); } call_virtual(endArray, context); break; default: break; } } void DataValue_serializeGeneric(DataValue * value, compat_type(SerializationContext *) serializationContext) { SerializationContext * context = serializationContext; call_virtual(beginArray, context, "data"); switch (value->type) { case DATA_TYPE_ARRAY: call_virtual(writeString, context, NULL, "array"); break; case DATA_TYPE_HASH_TABLE: call_virtual(writeString, context, NULL, "structure"); break; case DATA_TYPE_ASSOCIATIVE_ARRAY: call_virtual(writeString, context, NULL, "dictionary"); break; default: break; } serializeContainer(value, context); call_virtual(endArray, context); } static bool serializeItemWithSchemaField(DataValue * item, DataValueSchemaField * field, const char * key, SerializationContext * context) { if (item->type != field->type) { context->status = DATA_SERIALIZER_SCHEMA_ITEM_MISMATCH; return false; } switch (field->type) { case DATA_TYPE_BOOLEAN: call_virtual(writeBoolean, context, key, item->value.boolean); return true; case DATA_TYPE_INT8: call_virtual(writeInt8, context, key, item->value.int8); return true; case DATA_TYPE_UINT8: call_virtual(writeUInt8, context, key, item->value.uint8); return true; case DATA_TYPE_INT16: call_virtual(writeInt16, context, key, item->value.int16); return true; case DATA_TYPE_UINT16: call_virtual(writeUInt16, context, key, item->value.uint16); return true; case DATA_TYPE_INT32: call_virtual(writeInt32, context, key, item->value.int32); return true; case DATA_TYPE_UINT32: call_virtual(writeUInt32, context, key, item->value.uint32); return true; case DATA_TYPE_INT64: call_virtual(writeInt64, context, key, item->value.int64); return true; case DATA_TYPE_UINT64: call_virtual(writeUInt64, context, key, item->value.int64); return true; case DATA_TYPE_FLOAT: call_virtual(writeFloat, context, key, item->value.float32); return true; case DATA_TYPE_DOUBLE: call_virtual(writeDouble, context, key, item->value.float64); return true; case DATA_TYPE_FIXED_16_16: call_virtual(writeFixed16_16, context, key, item->value.fixed); return true; case DATA_TYPE_STRING: call_virtual(writeString, context, key, item->value.string); return true; case DATA_TYPE_BLOB: call_virtual(writeBlob, context, key, item->value.blob.bytes, item->value.blob.length); return true; case DATA_TYPE_HASH_TABLE: case DATA_TYPE_ARRAY: case DATA_TYPE_ASSOCIATIVE_ARRAY: if (!DataValue_serializeWithSchema(item, field->subitemSchema, key, context)) { return false; } return true; case DATA_TYPE_POINTER: context->status = DATA_SERIALIZER_UNSUPPORTED_DATA_TYPE; return false; } context->status = DATA_SERIALIZER_UNKNOWN_DATA_TYPE; return false; } bool DataValue_serializeWithSchema(DataValue * value, DataValueSchema * schema, const char * topLevelKey, compat_type(SerializationContext *) serializationContext) { SerializationContext * context = serializationContext; switch (value->type) { case DATA_TYPE_HASH_TABLE: { DataHashTable * hashTable = value->value.hashTable; call_virtual(beginStructure, context, topLevelKey); for (unsigned int fieldIndex = 0; fieldIndex < schema->fieldCount; fieldIndex++) { DataValue * item = hashGet(hashTable, schema->fields[fieldIndex].name); if (item == NULL) { context->status = DATA_SERIALIZER_MISSING_ITEM; return false; } if (!serializeItemWithSchemaField(item, &schema->fields[fieldIndex], schema->fields[fieldIndex].name, context)) { return false; } } call_virtual(endStructure, context); return true; } case DATA_TYPE_ARRAY: { DataArray * array = value->value.array; call_virtual(beginArray, context, topLevelKey); for (unsigned int itemIndex = 0; itemIndex < array->count; itemIndex++) { if (!serializeItemWithSchemaField(arrayGet(array, itemIndex), &schema->fields[itemIndex % schema->fieldCount], NULL, context)) { return false; } } call_virtual(endArray, context); return true; } case DATA_TYPE_ASSOCIATIVE_ARRAY: { DataAssociativeArray * associativeArray = value->value.associativeArray; call_virtual(beginDictionary, context, topLevelKey); for (unsigned int itemIndex = 0; itemIndex < associativeArray->count; itemIndex++) { if (!serializeItemWithSchemaField(associativeArrayGetValueAtIndex(associativeArray, itemIndex), &schema->fields[itemIndex % schema->fieldCount], associativeArrayGetKeyAtIndex(associativeArray, itemIndex), context)) { return false; } } call_virtual(endDictionary, context); return true; } default: break; } return false; } #define deserializeFieldCase(value_type, primitive_type, function_name, value_create_function) \ case value_type: { \ primitive_type value = call_virtual(function_name, context, key); \ if (context->status != SERIALIZATION_ERROR_OK) { \ if (tolerateErrors) { \ *outValue = valueCopy(&field->defaultValue); \ return true; \ } \ return false; \ } \ *outValue = value_create_function(value); \ return true; \ } static bool deserializeItemWithSchemaField(DataValueSchemaField * field, const char * key, DeserializationContext * context, bool tolerateErrors, DataValue * outValue) { if (tolerateErrors) { context->status = SERIALIZATION_ERROR_OK; } switch (field->type) { deserializeFieldCase(DATA_TYPE_BOOLEAN, bool, readBoolean, valueCreateBoolean) deserializeFieldCase(DATA_TYPE_INT8, int8_t, readInt8, valueCreateInt8) deserializeFieldCase(DATA_TYPE_UINT8, uint8_t, readUInt8, valueCreateUInt8) deserializeFieldCase(DATA_TYPE_INT16, int16_t, readInt16, valueCreateInt16) deserializeFieldCase(DATA_TYPE_UINT16, uint16_t, readUInt16, valueCreateUInt16) deserializeFieldCase(DATA_TYPE_INT32, int32_t, readInt32, valueCreateInt32) deserializeFieldCase(DATA_TYPE_UINT32, uint32_t, readUInt32, valueCreateUInt32) deserializeFieldCase(DATA_TYPE_INT64, int64_t, readInt64, valueCreateInt64) deserializeFieldCase(DATA_TYPE_UINT64, uint64_t, readUInt64, valueCreateUInt64) deserializeFieldCase(DATA_TYPE_FLOAT, float, readFloat, valueCreateFloat) deserializeFieldCase(DATA_TYPE_DOUBLE, double, readDouble, valueCreateDouble) deserializeFieldCase(DATA_TYPE_FIXED_16_16, fixed16_16, readFixed16_16, valueCreateFixed16_16) case DATA_TYPE_STRING: { const char * string = call_virtual(readString, context, key); if (string == NULL || context->status != SERIALIZATION_ERROR_OK) { if (tolerateErrors) { *outValue = valueCopy(&field->defaultValue); return true; } return false; } *outValue = valueCreateString(string, DATA_USE_STRLEN, true, true); return true; } case DATA_TYPE_BLOB: { size_t size; const void * data = call_virtual(readBlob, context, key, &size); if (data == NULL || context->status != SERIALIZATION_ERROR_OK) { if (tolerateErrors) { *outValue = valueCopy(&field->defaultValue); return true; } return false; } *outValue = valueCreateBlob(data, size, true, true); return true; } case DATA_TYPE_HASH_TABLE: case DATA_TYPE_ARRAY: case DATA_TYPE_ASSOCIATIVE_ARRAY: { DataValue value = DataValue_deserializeWithSchema(field->subitemSchema, key, field->type, context, tolerateErrors); if (value.type == DATA_TYPE_BOOLEAN) { return false; } *outValue = value; return true; } case DATA_TYPE_POINTER: break; } return false; } static bool isContainerFailStatus(int status) { return status == SERIALIZATION_ERROR_END_OF_CONTAINER || status == SERIALIZATION_ERROR_KEY_NOT_FOUND || status == SERIALIZATION_ERROR_INCORRECT_TYPE || status == SERIALIZATION_ERROR_NULL_KEY || status == SERIALIZATION_ERROR_MULTIPLE_TOP_LEVEL_CONTAINERS; } DataValue DataValue_deserializeWithSchema(DataValueSchema * schema, const char * topLevelKey, DataValueType topLevelContainerType, compat_type(DeserializationContext *) deserializationContext, bool tolerateErrors) { DeserializationContext * context = deserializationContext; switch (topLevelContainerType) { case DATA_TYPE_HASH_TABLE: { DataHashTable * hashTable = hashCreate(); if (tolerateErrors) { context->status = SERIALIZATION_ERROR_OK; } call_virtual(beginStructure, context, topLevelKey); if (!isContainerFailStatus(context->status)) { for (unsigned int fieldIndex = 0; fieldIndex < schema->fieldCount; fieldIndex++) { DataValue value; if (!deserializeItemWithSchemaField(&schema->fields[fieldIndex], schema->fields[fieldIndex].name, context, tolerateErrors, &value)) { if (!tolerateErrors) { hashDispose(hashTable); return valueCreateBoolean(false); } value = valueCopy(&schema->fields[fieldIndex].defaultValue); } hashSet(hashTable, schema->fields[fieldIndex].name, value); } call_virtual(endStructure, context); } return valueCreateHashTable(hashTable, true, false); } case DATA_TYPE_ARRAY: { DataArray * array = arrayCreate(); if (tolerateErrors) { context->status = SERIALIZATION_ERROR_OK; } unsigned int itemCount = call_virtual(beginArray, context, topLevelKey); if (!isContainerFailStatus(context->status)) { for (unsigned int itemIndex = 0; itemIndex < itemCount; itemIndex++) { DataValue value; if (!deserializeItemWithSchemaField(&schema->fields[itemIndex % schema->fieldCount], NULL, context, tolerateErrors, &value)) { if (!tolerateErrors) { arrayDispose(array); return valueCreateBoolean(false); } value = valueCopy(&schema->fields[itemIndex % schema->fieldCount].defaultValue); } arrayAppend(array, value); } call_virtual(endArray, context); } return valueCreateArray(array, true, false); } case DATA_TYPE_ASSOCIATIVE_ARRAY: { DataAssociativeArray * associativeArray = associativeArrayCreate(); if (tolerateErrors) { context->status = SERIALIZATION_ERROR_OK; } unsigned int itemCount = call_virtual(beginDictionary, context, topLevelKey); if (!isContainerFailStatus(context->status)) { for (unsigned int itemIndex = 0; itemIndex < itemCount; itemIndex++) { DataValue value; const char * key = call_virtual(readNextDictionaryKey, context); if (!deserializeItemWithSchemaField(&schema->fields[itemIndex % schema->fieldCount], key, context, tolerateErrors, &value)) { if (!tolerateErrors) { associativeArrayDispose(associativeArray); return valueCreateBoolean(false); } value = valueCopy(&schema->fields[itemIndex % schema->fieldCount].defaultValue); } associativeArrayAppend(associativeArray, key, value); } call_virtual(endDictionary, context); } return valueCreateAssociativeArray(associativeArray, true, false); } default: break; } return valueCreateBoolean(false); }