* the data type.
*/
if (flags->extra) {
- if ((flags->subtype != FLAG_KEY_FIELD) && (flags->subtype != FLAG_BIT_FIELD) &&
- (flags->subtype != FLAG_LENGTH_UINT8) && (flags->subtype != FLAG_LENGTH_UINT16)) {
+ if (!fr_dict_attr_is_key_field(da) && !da_is_length_field(da) && !da_is_bit_field(da)) {
fr_strerror_const("The 'key' and 'length' flags cannot be used with any other flags.");
return false;
}
if (flags->array) {
ALLOW_FLAG(array);
- if ((flags->subtype != FLAG_LENGTH_UINT8) && (flags->subtype != FLAG_LENGTH_UINT16)) goto invalid_extra;
+ if (!da_is_length_field(da)) {
+ fr_assert(0);
+ goto invalid_extra;
+ }
+
} else if (flags->subtype) {
invalid_extra:
fr_strerror_const("Invalid type (not 'length=...') for extra flag.");
break;
case FR_TYPE_STRUCT:
- if ((flags->subtype != FLAG_LENGTH_UINT8) && (flags->subtype != FLAG_LENGTH_UINT16)) {
+ if (!da_is_length_field(da)) {
fr_strerror_const("Invalid type (not 'length=...') for extra flag.");
return false;
}
return false;
}
- if (((flags->subtype == FLAG_LENGTH_UINT8) || (flags->subtype == FLAG_LENGTH_UINT16)) &&
+ if (da_is_length_field(da) &&
((type != FR_TYPE_STRING) && (type != FR_TYPE_OCTETS) && (type != FR_TYPE_STRUCT))) {
fr_strerror_printf("The 'length' flag cannot be used used with type %s",
fr_type_to_str(type));
/*
* Set how many bytes there are in the "length" field.
*/
- if (parent->flags.subtype == FLAG_LENGTH_UINT8) {
+ if (da_is_length_field8(parent)) {
field_len = 1;
} else {
+ fr_assert(da_is_length_field16(parent));
field_len = 2;
}
*/
if (!da_is_length_field(parent)) {
work_dbuff = FR_DBUFF(dbuff);
+
+ } else if (da_is_length_field8(parent)) {
+ work_dbuff = FR_DBUFF_MAX(dbuff, UINT8_MAX);
+ fr_dbuff_marker(&hdr, &work_dbuff);
+
+ FR_DBUFF_ADVANCE_RETURN(&work_dbuff, 1);
+ prefix_length = 1;
+ do_length = true;
+
} else {
- if (parent->flags.subtype == FLAG_LENGTH_UINT8) {
- work_dbuff = FR_DBUFF_MAX(dbuff, 256);
- fr_dbuff_marker(&hdr, &work_dbuff);
+ fr_assert(da_is_length_field16(parent));
- FR_DBUFF_ADVANCE_RETURN(&work_dbuff, 1);
- prefix_length = 1;
- } else {
- work_dbuff = FR_DBUFF_MAX(dbuff, 65536);
- fr_dbuff_marker(&hdr, &work_dbuff);
+ work_dbuff = FR_DBUFF_MAX(dbuff, UINT16_MAX);
+ fr_dbuff_marker(&hdr, &work_dbuff);
- FR_DBUFF_ADVANCE_RETURN(&work_dbuff, 2);
- prefix_length = 2;
- }
+ FR_DBUFF_ADVANCE_RETURN(&work_dbuff, 2);
+ prefix_length = 2;
do_length = true;
}
*/
if (length < prefix_length) return PAIR_ENCODE_FATAL_ERROR;
#endif
- if (parent->flags.subtype == FLAG_LENGTH_UINT8) {
+ if (da_is_length_field8(parent)) {
length -= prefix_length;
length += da_length_offset(parent);
/*
* Clamp length at maximum we're allowed to encode.
*/
- if (da_is_length_field(value->enumv)) {
- if (value->enumv->flags.subtype == FLAG_LENGTH_UINT8) {
- if (value->vb_length > 255) return 255;
+ if (da_is_length_field8(value->enumv)) {
+ if (value->vb_length > UINT8_MAX) return UINT8_MAX;
- } else if (value->enumv->flags.subtype == FLAG_LENGTH_UINT16) {
- if (value->vb_length > 65535) return 65535;
- }
+ } else if (da_is_length_field16(value->enumv)) {
+ if (value->vb_length > UINT16_MAX) return UINT16_MAX;
}
}
return value->vb_length;
max = value->enumv->flags.length;
}
- } else if (da_is_length_field(value->enumv)) {
+ } else if (da_is_length_field8(value->enumv)) {
/*
* Truncate the output to the max allowed for this field and encode the length.
*/
- if (value->enumv->flags.subtype == FLAG_LENGTH_UINT8) {
- if (max > 255) max = 255;
- FR_DBUFF_IN_RETURN(&work_dbuff, (uint8_t) max);
+ if (max > UINT8_MAX) max = UINT8_MAX;
+ FR_DBUFF_IN_RETURN(&work_dbuff, (uint8_t) max);
- } else if (value->enumv->flags.subtype == FLAG_LENGTH_UINT16) {
- if (max > 65536) max = 65535;
- FR_DBUFF_IN_RETURN(&work_dbuff, (uint16_t) max);
+ } else if (da_is_length_field16(value->enumv)) {
- } else {
- return -1;
- }
+ if (max > UINT16_MAX) max = UINT16_MAX;
+ FR_DBUFF_IN_RETURN(&work_dbuff, (uint16_t) max);
}
}
if (enumv->flags.length) {
newlen = enumv->flags.length;
- } else if (da_is_length_field(enumv)) {
- /*
- * Or fields with a length prefix.
- */
- if (enumv->flags.subtype == FLAG_LENGTH_UINT8) {
- uint8_t num = 0;
-
- FR_DBUFF_OUT_RETURN(&num, &work_dbuff);
- newlen = num;
- offset = 1;
+ } else if (da_is_length_field8(enumv)) {
+ uint8_t num = 0;
- } else if (enumv->flags.subtype == FLAG_LENGTH_UINT16) {
- uint16_t num = 0;
+ FR_DBUFF_OUT_RETURN(&num, &work_dbuff);
+ newlen = num;
+ offset = 1;
- FR_DBUFF_OUT_RETURN(&num, &work_dbuff);
- newlen = num;
- offset = 2;
+ } else if (da_is_length_field16(enumv)) {
+ uint16_t num = 0;
- } else {
- return -1;
- }
+ FR_DBUFF_OUT_RETURN(&num, &work_dbuff);
+ newlen = num;
+ offset = 2;
}
}