#include "bej_encoder_metadata.h" #include "bej_common.h" #include "bej_dictionary.h" #include #include #include #include /** * @brief Maximum digits supported in the fractional part of a real number. */ #define BEJ_REAL_PRECISION 16 /** * @brief bejTupleL size of an integer. * * Maximum bytes possible for an integer is 8. Therefore to encode the length of * an integer using a nnint, we only need two bytes. [byte1: nnint length, * byte2: integer length [0-8]] */ #define BEJ_TUPLE_L_SIZE_FOR_BEJ_INTEGER 2 /** * @brief bejTupleL size of a bool. * * 1byte for the nnint length and 1 byte for the value. */ #define BEJ_TUPLE_L_SIZE_FOR_BEJ_BOOL 2 /** * @brief bejTupleF size. */ #define BEJ_TUPLE_F_SIZE 1 /** * @brief Check the name is an annotation type name. * * @param[in] name - property name. * @return true for annotation name, false otherwise. */ static bool bejIsAnnotation(const char* name) { if (name == NULL) { return false; } return name[0] == '@'; } /** * @brief Get the dictionary for the provided node. * * @param[in] dictionaries - available dictionaries for encoding. * @param[in] parentDictionary - dictionary used for the parent of this node. * @param[in] nodeName - name of the interested node. Can be NULL if the node * doesn't have a name. * @return a pointer to the dictionary to be used. */ static const uint8_t* bejGetRelatedDictionary( const struct BejDictionaries* dictionaries, const uint8_t* parentDictionary, const char* nodeName) { // If the node name is NULL, we have to use parent dictionary. if (nodeName == NULL) { return parentDictionary; } // If the parent is using annotation dictionary, that means the parent is an // annotation. Therefore the child (this node) should be an annotation too // (Could this be false?). Therefore we should use the annotation dictionary // for this node as well. if (parentDictionary == dictionaries->annotationDictionary) { return dictionaries->annotationDictionary; } return bejIsAnnotation(nodeName) ? dictionaries->annotationDictionary : dictionaries->schemaDictionary; } /** * @brief Get dictionary data for the given node. * * @param[in] dictionaries - available dictionaries. * @param[in] parentDictionary - the dictionary used by the provided node's * parent. * @param[in] node - node that caller is interested in. * @param[in] nodeIndex - index of this node within its parent. * @param[in] dictStartingOffset - starting dictionary child offset value of * this node's parent. * @param[out] sequenceNumber - sequence number of the node. bit0 specifies the * dictionary schema type: [major|annotation]. * @param[out] nodeDictionary - if not NULL, return a pointer to the dictionary * used for the node. * @param[out] childEntryOffset - if not NULL, return the dictionary starting * offset used for this nodes children. If this node is not supposed to have * children, caller should ignore this value. * @return 0 if successful. */ static int bejFindSeqNumAndChildDictOffset( const struct BejDictionaries* dictionaries, const uint8_t* parentDictionary, struct RedfishPropertyNode* node, uint16_t nodeIndex, uint16_t dictStartingOffset, uint32_t* sequenceNumber, const uint8_t** nodeDictionary, uint16_t* childEntryOffset) { // If the node doesn't have a name, we can't use a dictionary. So we can use // its parent's info. if (node->name == NULL || node->name[0] == '\0') { if (nodeDictionary != NULL) { *nodeDictionary = parentDictionary; } if (childEntryOffset != NULL) { *childEntryOffset = dictStartingOffset; } // If the property doesn't have a name, it has to be an element of an // array. In that case, sequence number is the array index. *sequenceNumber = (uint32_t)nodeIndex << 1; if (dictionaries->annotationDictionary == parentDictionary) { *sequenceNumber |= 1; } return 0; } // If we are here, the property has a name. const uint8_t* dictionary = bejGetRelatedDictionary(dictionaries, parentDictionary, node->name); bool isAnnotation = dictionary == dictionaries->annotationDictionary; // If this node's dictionary and its parent's dictionary is different, // this node should start searching from the beginning of its // dictionary. This should only happen for property annotations of form // property@annotation_class.annotation_name. if (dictionary != parentDictionary) { // Redundancy check. if (!isAnnotation) { fprintf(stderr, "Dictionary for property %s should be the annotation " "dictionary. Might be a encoding failure. Maybe the " "JSON tree is not created correctly.", node->name); return -1; } dictStartingOffset = bejDictGetFirstAnnotatedPropertyOffset(); } const struct BejDictionaryProperty* property; int ret = bejDictGetPropertyByName(dictionary, dictStartingOffset, node->name, &property, NULL); if (ret != 0) { fprintf(stderr, "Failed to find dictionary entry for name %s. Search started " "at offset: %u. ret: %d\n", node->name, dictStartingOffset, ret); return ret; } if (nodeDictionary != NULL) { *nodeDictionary = dictionary; } if (childEntryOffset != NULL) { *childEntryOffset = property->childPointerOffset; } *sequenceNumber = (uint32_t)(property->sequenceNumber) << 1; if (isAnnotation) { *sequenceNumber |= 1; } return 0; } static int bejUpdateIntMetaData(const struct BejDictionaries* dictionaries, const uint8_t* parentDictionary, struct RedfishPropertyLeafInt* node, uint16_t nodeIndex, uint16_t dictStartingOffset) { uint32_t sequenceNumber; RETURN_IF_IERROR(bejFindSeqNumAndChildDictOffset( dictionaries, parentDictionary, &node->leaf.nodeAttr, nodeIndex, dictStartingOffset, &sequenceNumber, NULL, NULL)); node->leaf.metaData.sequenceNumber = sequenceNumber; // Calculate the size for encoding this in a SFLV tuple. // S: Size needed for encoding sequence number. node->leaf.metaData.sflSize = bejNnintEncodingSizeOfUInt(sequenceNumber); // F: Size of the format byte is 1. node->leaf.metaData.sflSize += BEJ_TUPLE_F_SIZE; // L: Length needed for the value. node->leaf.metaData.sflSize += BEJ_TUPLE_L_SIZE_FOR_BEJ_INTEGER; // V: Bytes used for the value. node->leaf.metaData.vSize = bejIntLengthOfValue(node->value); return 0; } static int bejUpdateStringMetaData( const struct BejDictionaries* dictionaries, const uint8_t* parentDictionary, struct RedfishPropertyLeafString* node, uint16_t nodeIndex, uint16_t dictStartingOffset) { uint32_t sequenceNumber; RETURN_IF_IERROR(bejFindSeqNumAndChildDictOffset( dictionaries, parentDictionary, &(node->leaf.nodeAttr), nodeIndex, dictStartingOffset, &sequenceNumber, NULL, NULL)); node->leaf.metaData.sequenceNumber = sequenceNumber; // Calculate the size for encoding this in a SFLV tuple. // S: Size needed for encoding sequence number. node->leaf.metaData.sflSize = bejNnintEncodingSizeOfUInt(sequenceNumber); // F: Size of the format byte is 1. node->leaf.metaData.sflSize += BEJ_TUPLE_F_SIZE; // L: Length needed for the string including the NULL character. Length is // in nnint format. size_t strLenWithNull = strlen(node->value) + 1; node->leaf.metaData.sflSize += bejNnintEncodingSizeOfUInt(strLenWithNull); // V: Bytes used for the value. node->leaf.metaData.vSize = strLenWithNull; return 0; } static int bejUpdateRealMetaData( const struct BejDictionaries* dictionaries, const uint8_t* parentDictionary, struct RedfishPropertyLeafReal* node, uint16_t nodeIndex, uint16_t dictStartingOffset) { uint32_t sequenceNumber; RETURN_IF_IERROR(bejFindSeqNumAndChildDictOffset( dictionaries, parentDictionary, &(node->leaf.nodeAttr), nodeIndex, dictStartingOffset, &sequenceNumber, NULL, NULL)); node->leaf.metaData.sequenceNumber = sequenceNumber; if (node->value > (double)INT64_MAX) { // TODO: We should use the exponent. fprintf( stderr, "Need to add support to encode double value larger than INT64_MAX\n"); return -1; } // Calculate the size for encoding this in a SFLV tuple. // S: Size needed for encoding sequence number. node->leaf.metaData.sflSize = bejNnintEncodingSizeOfUInt(sequenceNumber); // F: Size of the format byte is 1. node->leaf.metaData.sflSize += BEJ_TUPLE_F_SIZE; // We need to breakdown the real number to bejReal type to determine the // length. We are not gonna add an exponent. It will only be the whole part // and the fraction part. Get the whole part double originalWhole; double originalFract = modf(node->value, &originalWhole); // Convert the fraction to a whole value for encoding. // Create a new value by multiplying the original fraction by 10. Do this // until the fraction of the new value is 0 or we reach the precision. Eg // 0.00105: This fraction value has two leading zeros. We will keep // multiplying this by 10 until the fraction of the result of that // multiplication is 0. double originalFactConvertedToWhole = fabs(originalFract); double fract = originalFract; double intPart; uint32_t leadingZeros = 0; uint32_t precision = 0; while (fract != 0 && precision < BEJ_REAL_PRECISION) { originalFactConvertedToWhole = originalFactConvertedToWhole * 10; fract = modf(originalFactConvertedToWhole, &intPart); // If the integer portion is 0, that means we still have leading zeros. if (intPart == 0) { ++leadingZeros; } ++precision; } node->bejReal.whole = (int64_t)originalWhole; node->bejReal.zeroCount = leadingZeros; node->bejReal.fract = (int64_t)originalFactConvertedToWhole; // We are omitting exp. So the exp length should be 0. node->bejReal.expLen = 0; node->bejReal.exp = 0; // Calculate the sizes needed for storing bejReal fields. // nnint for the length of the "whole" value. node->leaf.metaData.vSize = BEJ_TUPLE_L_SIZE_FOR_BEJ_INTEGER; // Length needed for the "whole" value. node->leaf.metaData.vSize += bejIntLengthOfValue((int64_t)originalWhole); // nnint for leading zero count. node->leaf.metaData.vSize += bejNnintEncodingSizeOfUInt(leadingZeros); // nnint for the factional part. node->leaf.metaData.vSize += bejNnintEncodingSizeOfUInt((int64_t)originalFactConvertedToWhole); // nnint for the exp length. We are omitting exp. So the exp length should // be 0. node->leaf.metaData.vSize += bejNnintEncodingSizeOfUInt(0); // L: nnint for the size needed for encoding the bejReal value. node->leaf.metaData.sflSize += bejNnintEncodingSizeOfUInt(node->leaf.metaData.vSize); return 0; } static int bejUpdateEnumMetaData( const struct BejDictionaries* dictionaries, const uint8_t* parentDictionary, struct RedfishPropertyLeafEnum* node, uint16_t nodeIndex, uint16_t dictStartingOffset) { const uint8_t* nodeDictionary; uint16_t childEntryOffset; uint32_t sequenceNumber; // If the enum property doesn't have a name, this will simply return the // nodeIndex encoded as the sequence number. If not, this will return the // sequence number in the dictionary and the starting dictionary index for // the enum values. RETURN_IF_IERROR(bejFindSeqNumAndChildDictOffset( dictionaries, parentDictionary, &(node->leaf.nodeAttr), nodeIndex, dictStartingOffset, &sequenceNumber, &nodeDictionary, &childEntryOffset)); // Update the sequence number of the property. node->leaf.metaData.sequenceNumber = sequenceNumber; // Get the sequence number for the Enum value. if (node->leaf.nodeAttr.name != NULL && node->leaf.nodeAttr.name[0] != '\0') { dictStartingOffset = childEntryOffset; } const struct BejDictionaryProperty* enumValueProperty; int ret = bejDictGetPropertyByName(nodeDictionary, dictStartingOffset, node->value, &enumValueProperty, NULL); if (ret != 0) { fprintf( stderr, "Failed to find dictionary entry for enum value %s. Search started " "at offset: %u. ret: %d\n", node->value, dictStartingOffset, ret); return ret; } node->enumValueSeq = enumValueProperty->sequenceNumber; // Calculate the size for encoding this in a SFLV tuple. // S: Size needed for encoding sequence number. node->leaf.metaData.sflSize = bejNnintEncodingSizeOfUInt(sequenceNumber); // F: Size of the format byte is 1. node->leaf.metaData.sflSize += BEJ_TUPLE_F_SIZE; // V: Bytes used for the value. node->leaf.metaData.vSize = bejNnintEncodingSizeOfUInt(enumValueProperty->sequenceNumber); // L: Length needed for the value nnint. node->leaf.metaData.sflSize += bejNnintEncodingSizeOfUInt(node->leaf.metaData.vSize); return 0; } static int bejUpdateBoolMetaData( const struct BejDictionaries* dictionaries, const uint8_t* parentDictionary, struct RedfishPropertyLeafBool* node, uint16_t nodeIndex, uint16_t dictStartingOffset) { uint32_t sequenceNumber; RETURN_IF_IERROR(bejFindSeqNumAndChildDictOffset( dictionaries, parentDictionary, &node->leaf.nodeAttr, nodeIndex, dictStartingOffset, &sequenceNumber, NULL, NULL)); node->leaf.metaData.sequenceNumber = sequenceNumber; // Calculate the size for encoding this in a SFLV tuple. // S: Size needed for encoding sequence number. node->leaf.metaData.sflSize = bejNnintEncodingSizeOfUInt(sequenceNumber); // F: Size of the format byte is 1. node->leaf.metaData.sflSize += BEJ_TUPLE_F_SIZE; // L: Length needed for the value. node->leaf.metaData.sflSize += BEJ_TUPLE_L_SIZE_FOR_BEJ_BOOL; // V: Bytes used for the value; 0x00 or 0xFF. node->leaf.metaData.vSize = 1; return 0; } static int bejUpdateNullMetaData( const struct BejDictionaries* dictionaries, const uint8_t* parentDictionary, struct RedfishPropertyLeafNull* node, uint16_t nodeIndex, uint16_t dictStartingOffset) { uint32_t sequenceNumber; RETURN_IF_IERROR(bejFindSeqNumAndChildDictOffset( dictionaries, parentDictionary, &node->leaf.nodeAttr, nodeIndex, dictStartingOffset, &sequenceNumber, NULL, NULL)); node->leaf.metaData.sequenceNumber = sequenceNumber; // Calculate the size for encoding this in a SFLV tuple. // S: Size needed for encoding sequence number. node->leaf.metaData.sflSize = bejNnintEncodingSizeOfUInt(sequenceNumber); // F: Size of the format byte is 1. node->leaf.metaData.sflSize += BEJ_TUPLE_F_SIZE; // L: Length needed for the value. Value length for NULL type is 0. So we // need to encode [0x01 0x00] node->leaf.metaData.sflSize += BEJ_TUPLE_L_SIZE_FOR_BEJ_INTEGER; node->leaf.metaData.vSize = 0; return 0; } /** * @brief Update metadata of leaf nodes. * * @param dictionaries - dictionaries needed for encoding. * @param parentDictionary - dictionary used by this node's parent. * @param childPtr - a pointer to the leaf node. * @param childIndex - if this node is an array element, this is the array * index. * @param dictStartingOffset - starting dictionary child offset value of this * node's parent. * @return 0 if successful. */ static int bejUpdateLeafNodeMetaData( const struct BejDictionaries* dictionaries, const uint8_t* parentDictionary, void* childPtr, uint16_t childIndex, uint16_t dictStartingOffset) { struct RedfishPropertyLeaf* chNode = childPtr; switch (chNode->nodeAttr.format.principalDataType) { case bejInteger: RETURN_IF_IERROR( bejUpdateIntMetaData(dictionaries, parentDictionary, childPtr, childIndex, dictStartingOffset)); break; case bejString: RETURN_IF_IERROR(bejUpdateStringMetaData( dictionaries, parentDictionary, childPtr, childIndex, dictStartingOffset)); break; case bejReal: RETURN_IF_IERROR( bejUpdateRealMetaData(dictionaries, parentDictionary, childPtr, childIndex, dictStartingOffset)); break; case bejEnum: RETURN_IF_IERROR( bejUpdateEnumMetaData(dictionaries, parentDictionary, childPtr, childIndex, dictStartingOffset)); break; case bejBoolean: RETURN_IF_IERROR( bejUpdateBoolMetaData(dictionaries, parentDictionary, childPtr, childIndex, dictStartingOffset)); break; case bejNull: RETURN_IF_IERROR( bejUpdateNullMetaData(dictionaries, parentDictionary, childPtr, childIndex, dictStartingOffset)); break; default: fprintf(stderr, "Child type %u not supported\n", chNode->nodeAttr.format.principalDataType); return -1; } return 0; } /** * @brief Update metadata of a parent node. * * @param dictionaries - dictionaries needed for encoding. * @param parentDictionary - dictionary used by this node's parent. * @param dictStartingOffset - starting dictionary child offset value of this * node's parent. * @param node - a pointer to the parent node. * @param nodeIndex - If this node is an array element, this is the array index. * @return 0 if successful. */ static int bejUpdateParentMetaData( const struct BejDictionaries* dictionaries, const uint8_t* parentDictionary, uint16_t dictStartingOffset, struct RedfishPropertyParent* node, uint16_t nodeIndex) { const uint8_t* nodeDictionary; uint16_t childEntryOffset; uint32_t sequenceNumber; // Get the dictionary related data from the node. RETURN_IF_IERROR(bejFindSeqNumAndChildDictOffset( dictionaries, parentDictionary, &node->nodeAttr, nodeIndex, dictStartingOffset, &sequenceNumber, &nodeDictionary, &childEntryOffset)); node->metaData.sequenceNumber = sequenceNumber; node->metaData.childrenDictPropOffset = childEntryOffset; node->metaData.nextChild = node->firstChild; node->metaData.nextChildIndex = 0; node->metaData.dictionary = nodeDictionary; node->metaData.vSize = 0; // S: Size needed for encoding sequence number. node->metaData.sflSize = bejNnintEncodingSizeOfUInt(node->metaData.sequenceNumber); // F: Size of the format byte is 1. node->metaData.sflSize += 1; // V: Only for bejArray and bejSet types, value size should include the // children count. We need to add the size needs to encode all the children // later. if (node->nodeAttr.format.principalDataType != bejPropertyAnnotation) { node->metaData.vSize = bejNnintEncodingSizeOfUInt(node->nChildren); } return 0; } /** * @brief Update metadata of child nodes. * * If a child node contains its own child nodes, it will be added to the stack * and function will return. * * @param dictionaries - dictionaries needed for encoding. * @param parent - parent node. * @param stack - stack holding parent nodes. * @return 0 if successful. */ static int bejProcessChildNodes(const struct BejDictionaries* dictionaries, struct RedfishPropertyParent* parent, struct BejPointerStackCallback* stack) { // Get the next child of the parent. void* childPtr = parent->metaData.nextChild; // Process all the children belongs to the parent. while (childPtr != NULL) { // If we find a child with its own child nodes, add it to the stack and // return. if (bejTreeIsParentType(childPtr)) { RETURN_IF_IERROR(bejUpdateParentMetaData( dictionaries, parent->metaData.dictionary, parent->metaData.childrenDictPropOffset, childPtr, parent->metaData.nextChildIndex)); RETURN_IF_IERROR(stack->stackPush(childPtr, stack->stackContext)); bejParentGoToNextChild(parent, childPtr); return 0; } RETURN_IF_IERROR(bejUpdateLeafNodeMetaData( dictionaries, parent->metaData.dictionary, childPtr, parent->metaData.nextChildIndex, parent->metaData.childrenDictPropOffset)); // Use the child value size to update the parent value size. struct RedfishPropertyLeaf* leafChild = childPtr; // V: Include the child size in parent's value size. parent->metaData.vSize += (leafChild->metaData.sflSize + leafChild->metaData.vSize); // Get the next child belongs to the parent. childPtr = bejParentGoToNextChild(parent, childPtr); } return 0; } int bejUpdateNodeMetadata(const struct BejDictionaries* dictionaries, uint16_t majorSchemaStartingOffset, struct RedfishPropertyParent* root, struct BejPointerStackCallback* stack) { // Decide the starting property offset of the dictionary. uint16_t dictOffset = bejDictGetPropertyHeadOffset(); if (majorSchemaStartingOffset != BEJ_DICTIONARY_START_AT_HEAD) { dictOffset = majorSchemaStartingOffset; } // Initialize root node metadata. RETURN_IF_IERROR( bejUpdateParentMetaData(dictionaries, dictionaries->schemaDictionary, dictOffset, root, /*childIndex=*/0)); // Push the root to the stack. Because we are not done with the parent node // yet. Need to figure out all bytes need to encode children of this parent, // and save it in the parent metadata. RETURN_IF_IERROR(stack->stackPush(root, stack->stackContext)); while (!stack->stackEmpty(stack->stackContext)) { // Get the parent at the top of the stack. Stack is only popped if the // parent stack entry has no pending children; That is // parent->metaData.nextChild == NULL. struct RedfishPropertyParent* parent = stack->stackPeek(stack->stackContext); // Calculate metadata of all the child nodes of the current parent node. // If one of these child nodes has its own child nodes, that child node // will be added to the stack and this function will return. RETURN_IF_IERROR(bejProcessChildNodes(dictionaries, parent, stack)); // If a new node hasn't been added to the stack, we know that this // parent's child nodes have been processed. If not, do not pop the // stack. if (parent != stack->stackPeek(stack->stackContext)) { continue; } // If we are here; // Then "parent" is the top element of the stack. // All the children of "parent" has been processed. // Remove the "parent" from the stack. parent = stack->stackPop(stack->stackContext); // L: Add the length needed to store the number of bytes used for the // parent's value. parent->metaData.sflSize += bejNnintEncodingSizeOfUInt(parent->metaData.vSize); // Since we now know the total size needs to encode the node pointed by // "parent" variable, we should add that to the value size of this // node's parent. Since we already popped this node from the stack, top // of the stack element is this nodes's parent. "parentsParent" can be // NULL if the node pointed by "parent" variable is the root. struct RedfishPropertyParent* parentsParent = stack->stackPeek(stack->stackContext); if (parentsParent != NULL) { // V: Include the total size to encode the current parent in its // parent's value size. parentsParent->metaData.vSize += (parent->metaData.sflSize + parent->metaData.vSize); } } return 0; }