Added check for zero length buffer size in primitive decoders that returns zero to enable simpler complex value optional element decoding. (#876)

This commit is contained in:
Steve Karg
2024-12-18 15:29:27 -06:00
committed by GitHub
parent 389b6a7d4d
commit 6b6be4a434
11 changed files with 723 additions and 270 deletions
+239 -66
View File
@@ -132,7 +132,8 @@ int bacapp_encode_device_obj_property_ref(
* @param apdu_size Size of the buffer containing the encoded value
* @param value Pointer to the structure which contains the decoded value
*
* @return number of bytes decoded or BACNET_STATUS_ERROR on failure.
* @return number of bytes decoded, zero if tag mismatch,
* or #BACNET_STATUS_ERROR (-1) if malformed
*/
int bacnet_device_object_property_reference_decode(
const uint8_t *apdu,
@@ -141,7 +142,6 @@ int bacnet_device_object_property_reference_decode(
{
int apdu_len = 0;
int len = 0;
uint32_t len_value_type = 0;
BACNET_UNSIGNED_INTEGER array_index = 0;
BACNET_OBJECT_TYPE object_type = 0;
uint32_t object_instance = 0;
@@ -161,7 +161,7 @@ int bacnet_device_object_property_reference_decode(
value->objectIdentifier.type = object_type;
}
} else {
return BACNET_STATUS_ERROR;
return len;
}
/* property-identifier [1] BACnetPropertyIdentifier */
len = bacnet_enumerated_context_decode(
@@ -175,20 +175,15 @@ int bacnet_device_object_property_reference_decode(
return BACNET_STATUS_ERROR;
}
/* property-array-index [2] Unsigned OPTIONAL */
if (bacnet_is_context_tag_number(
&apdu[apdu_len], apdu_size - apdu_len, 2, &len, &len_value_type)) {
len = bacnet_unsigned_context_decode(
&apdu[apdu_len], apdu_size - apdu_len, 2, &array_index);
if (len > 0) {
apdu_len += len;
len = bacnet_unsigned_decode(
&apdu[apdu_len], apdu_size - apdu_len, len_value_type,
&array_index);
if (len > 0) {
apdu_len += len;
if (value) {
value->arrayIndex = array_index;
}
} else {
return BACNET_STATUS_ERROR;
if (value) {
value->arrayIndex = array_index;
}
} else if (len < 0) {
return BACNET_STATUS_ERROR;
} else {
/* OPTIONAL - skip apdu_len increment */
if (value) {
@@ -196,21 +191,17 @@ int bacnet_device_object_property_reference_decode(
}
}
/* device-identifier [3] BACnetObjectIdentifier OPTIONAL */
if (bacnet_is_context_tag_number(
&apdu[apdu_len], apdu_size - apdu_len, 3, &len, &len_value_type)) {
len = bacnet_object_id_context_decode(
&apdu[apdu_len], apdu_size - apdu_len, 3, &object_type,
&object_instance);
if (len > 0) {
apdu_len += len;
len = bacnet_object_id_decode(
&apdu[apdu_len], apdu_size - apdu_len, len_value_type, &object_type,
&object_instance);
if (len > 0) {
apdu_len += len;
if (value) {
value->deviceIdentifier.type = object_type;
value->deviceIdentifier.instance = object_instance;
}
} else {
return BACNET_STATUS_ERROR;
if (value) {
value->deviceIdentifier.type = object_type;
value->deviceIdentifier.instance = object_instance;
}
} else if (len < 0) {
return BACNET_STATUS_ERROR;
} else {
/* OPTIONAL - skip apdu_len increment */
if (value) {
@@ -231,7 +222,8 @@ int bacnet_device_object_property_reference_decode(
* @param tag_number Tag number
* @param value Pointer to the structure which contains the decoded value
*
* @return number of bytes decoded or BACNET_STATUS_ERROR on failure.
* @return number of bytes decoded, zero if tag mismatch,
* or #BACNET_STATUS_ERROR (-1) if malformed
*/
int bacnet_device_object_property_reference_context_decode(
const uint8_t *apdu,
@@ -261,8 +253,6 @@ int bacnet_device_object_property_reference_context_decode(
} else {
return BACNET_STATUS_ERROR;
}
} else {
return BACNET_STATUS_ERROR;
}
return apdu_len;
@@ -450,7 +440,6 @@ int bacnet_device_object_reference_decode(
{
int len;
int apdu_len = 0;
uint32_t len_value_type = 0;
BACNET_OBJECT_TYPE object_type = 0;
uint32_t object_instance = 0;
@@ -458,21 +447,17 @@ int bacnet_device_object_reference_decode(
return BACNET_STATUS_ERROR;
}
/* device-identifier [0] BACnetObjectIdentifier OPTIONAL */
if (bacnet_is_context_tag_number(
&apdu[apdu_len], apdu_size - apdu_len, 0, &len, &len_value_type)) {
len = bacnet_object_id_context_decode(
&apdu[apdu_len], apdu_size - apdu_len, 0, &object_type,
&object_instance);
if (len > 0) {
apdu_len += len;
len = bacnet_object_id_decode(
&apdu[apdu_len], apdu_size - apdu_len, len_value_type, &object_type,
&object_instance);
if (len > 0) {
apdu_len += len;
if (value) {
value->deviceIdentifier.instance = object_instance;
value->deviceIdentifier.type = object_type;
}
} else {
return BACNET_STATUS_ERROR;
if (value) {
value->deviceIdentifier.instance = object_instance;
value->deviceIdentifier.type = object_type;
}
} else if (len < 0) {
return BACNET_STATUS_ERROR;
} else {
/* OPTIONAL - skip apdu_len increment */
value->deviceIdentifier.type = BACNET_NO_DEV_TYPE;
@@ -504,7 +489,8 @@ int bacnet_device_object_reference_decode(
* @param tag_number Tag number
* @param value Pointer to the structure containing the decoded value
*
* @return number of bytes decoded or BACNET_STATUS_ERROR on failure.
* @return number of bytes decoded, zero if wrong tag number,
* or #BACNET_STATUS_ERROR (-1) if malformed
*/
int bacnet_device_object_reference_context_decode(
const uint8_t *apdu,
@@ -534,8 +520,6 @@ int bacnet_device_object_reference_context_decode(
} else {
return BACNET_STATUS_ERROR;
}
} else {
return BACNET_STATUS_ERROR;
}
return apdu_len;
@@ -704,7 +688,8 @@ int bacapp_encode_context_obj_property_ref(
* @param apdu Pointer to the buffer containing the encoded value
* @param apdu_size Size of the buffer containing the encoded value
* @param reference - BACnetObjectPropertyReference to decode into
* @return number of bytes decoded or BACNET_STATUS_ERROR on failure.
* @return number of bytes decoded, zero if tag mismatch,
* or BACNET_STATUS_ERROR on failure.
*/
int bacapp_decode_obj_property_ref(
const uint8_t *apdu,
@@ -727,7 +712,7 @@ int bacapp_decode_obj_property_ref(
if (len > 0) {
apdu_len += len;
} else {
return BACNET_STATUS_ERROR;
return len;
}
/* property-identifier [1] BACnetPropertyIdentifier */
len = bacnet_enumerated_context_decode(
@@ -735,7 +720,7 @@ int bacapp_decode_obj_property_ref(
if (len > 0) {
apdu_len += len;
} else {
return BACNET_STATUS_ERROR;
return len;
}
if (reference) {
reference->object_identifier.type = object_identifier.type;
@@ -744,21 +729,18 @@ int bacapp_decode_obj_property_ref(
(BACNET_PROPERTY_ID)property_identifier;
}
/* property-array-index [2] Unsigned OPTIONAL */
if (bacnet_is_opening_tag_number(
&apdu[apdu_len], apdu_size - apdu_len, 2, NULL)) {
len = bacnet_unsigned_context_decode(
&apdu[apdu_len], apdu_size - apdu_len, 2, &unsigned_value);
if (len > 0) {
apdu_len += len;
if (unsigned_value > UINT32_MAX) {
return BACNET_STATUS_ERROR;
}
if (reference) {
reference->property_array_index = unsigned_value;
}
} else {
len = bacnet_unsigned_context_decode(
&apdu[apdu_len], apdu_size - apdu_len, 2, &unsigned_value);
if (len > 0) {
apdu_len += len;
if (unsigned_value > UINT32_MAX) {
return BACNET_STATUS_ERROR;
}
if (reference) {
reference->property_array_index = unsigned_value;
}
} else if (len < 0) {
return BACNET_STATUS_ERROR;
} else {
/* OPTIONAL - skip apdu_len increment */
if (reference) {
@@ -778,7 +760,8 @@ int bacapp_decode_obj_property_ref(
* @param tag_number Tag number
* @param value Pointer to the structure that shall be decoded into.
*
* @return number of bytes decoded or BACNET_STATUS_ERROR on failure.
* @return number of bytes decoded, zero if wrong tag number,
* or #BACNET_STATUS_ERROR (-1) if malformed
*/
int bacapp_decode_context_obj_property_ref(
const uint8_t *apdu,
@@ -794,7 +777,7 @@ int bacapp_decode_context_obj_property_ref(
}
if (!bacnet_is_opening_tag_number(
&apdu[apdu_len], apdu_size - apdu_len, tag_number, &len)) {
return BACNET_STATUS_ERROR;
return 0;
}
apdu_len += len;
len = bacapp_decode_obj_property_ref(
@@ -838,3 +821,193 @@ bool bacnet_object_property_reference_same(
return status;
}
/**
* @brief Encode a BACnetPropertyReference into a buffer
*
* BACnetPropertyReference ::= SEQUENCE {
* propertyIdentifier [0] BACnetPropertyIdentifier,
* propertyArrayIndex [1] Unsigned OPTIONAL
* -- used only with array datatype
* -- if omitted with an array the entire array is referenced
* }
*
* @param apdu - the APDU buffer, or NULL for length
* @param reference - BACnetPropertyReference
* @return length of the APDU buffer
*/
int bacnet_property_reference_encode(
uint8_t *apdu, const struct BACnetPropertyReference *reference)
{
int len = 0;
int apdu_len = 0;
if (!reference) {
return 0;
}
len = encode_context_enumerated(apdu, 0, reference->property_identifier);
apdu_len += len;
if (apdu) {
apdu += len;
}
if (reference->property_array_index != BACNET_ARRAY_ALL) {
len = encode_context_unsigned(apdu, 1, reference->property_array_index);
apdu_len += len;
}
return apdu_len;
}
/**
* @brief Encode a BACnetPropertyReference into a buffer
* BACnetPropertyReference ::= SEQUENCE {
* propertyIdentifier [0] BACnetPropertyIdentifier,
* propertyArrayIndex [1] Unsigned OPTIONAL
* -- used only with array datatype
* -- if omitted with an array the entire array is referenced
* }
*
* @param apdu - the APDU buffer, or NULL for length
* @param tag_number - context tag number to be encoded
* @param reference - BACnetPropertyReference
* @return length of the APDU buffer
*/
int bacnet_property_reference_context_encode(
uint8_t *apdu,
uint8_t tag_number,
const struct BACnetPropertyReference *reference)
{
int len = 0;
int apdu_len = 0;
len = encode_opening_tag(apdu, tag_number);
apdu_len += len;
if (apdu) {
apdu += len;
}
len = bacnet_property_reference_encode(apdu, reference);
apdu_len += len;
if (apdu) {
apdu += len;
}
len = encode_closing_tag(apdu, tag_number);
apdu_len += len;
return apdu_len;
}
/**
* @brief Compare the complex data of value1 and value2
* @param value1 - value 1 structure
* @param value2 - value 2 structure
* @return true if the values are the same
*/
bool bacnet_property_reference_same(
const struct BACnetPropertyReference *value1,
const struct BACnetPropertyReference *value2)
{
bool status = false;
if (value1 && value2) {
if ((value1->property_identifier == value2->property_identifier) &&
(value1->property_array_index == value2->property_array_index)) {
status = true;
}
}
return status;
}
/**
* @brief Decode a BACnetPropertyReference from a buffer
* @param apdu - the APDU buffer
* @param apdu_size - the size of the APDU buffer
* @param reference - BACnetPropertyReference to decode into
* @return number of bytes decoded or BACNET_STATUS_ERROR on failure.
*/
int bacnet_property_reference_decode(
const uint8_t *apdu,
uint32_t apdu_size,
struct BACnetPropertyReference *value)
{
int apdu_len = 0;
int len = 0;
uint32_t property_identifier = 0;
BACNET_UNSIGNED_INTEGER unsigned_value;
if (!apdu) {
return BACNET_STATUS_ERROR;
}
/* propertyIdentifier [0] BACnetPropertyIdentifier */
len = bacnet_enumerated_context_decode(
&apdu[apdu_len], apdu_size - apdu_len, 0, &property_identifier);
if (len > 0) {
apdu_len += len;
if (value) {
value->property_identifier = property_identifier;
}
} else {
return len;
}
/* propertyArrayIndex [1] Unsigned OPTIONAL */
len = bacnet_unsigned_context_decode(
&apdu[apdu_len], apdu_size - apdu_len, 1, &unsigned_value);
if (len > 0) {
apdu_len += len;
if (value) {
value->property_array_index = unsigned_value;
}
} else {
/* OPTIONAL - skip apdu_len increment */
if (value) {
value->property_array_index = BACNET_ARRAY_ALL;
}
}
return apdu_len;
}
/**
* @brief Decode the context property reference. Check for
* an opening tag and a closing tag as well.
* @param apdu - the APDU buffer
* @param apdu_size - the size of the APDU buffer
* @param tag_number - the tag number
* @param value - BACnetPropertyReference to decode into
* @return number of bytes decoded, zero if wrong tag number,
* or #BACNET_STATUS_ERROR (-1) if malformed
*/
int bacnet_property_reference_context_decode(
const uint8_t *apdu,
uint32_t apdu_size,
uint8_t tag_number,
struct BACnetPropertyReference *value)
{
int len = 0;
int apdu_len = 0;
if (!apdu) {
return BACNET_STATUS_ERROR;
}
if (!bacnet_is_opening_tag_number(
&apdu[apdu_len], apdu_size - apdu_len, tag_number, &len)) {
return 0;
}
apdu_len += len;
len = bacnet_property_reference_decode(
&apdu[apdu_len], apdu_size - apdu_len, value);
if (len > 0) {
apdu_len += len;
if (bacnet_is_closing_tag_number(
&apdu[apdu_len], apdu_size - apdu_len, tag_number, &len)) {
apdu_len += len;
} else {
return BACNET_STATUS_ERROR;
}
} else {
return BACNET_STATUS_ERROR;
}
return apdu_len;
}