3737 * | fixed for a specific type | yes | `int32 *` | assigned little-endian value of the enum |
3838 */
3939
40- #define ENUM_FIXED_SIZE_BITS (type ) \
41- lyplg_type_get_highest_set_bit_pos(\
42- ((struct lysc_type_enum *)type)->enums[LY_ARRAY_COUNT(((struct lysc_type_enum *)type)->enums) - 1].value)
43-
4440static void
4541lyplg_type_lyb_size_enum (const struct lysc_type * type , enum lyplg_lyb_size_type * size_type , uint32_t * fixed_size_bits )
4642{
4743 const struct lysc_type_enum * type_enum = (struct lysc_type_enum * )type ;
48- uint32_t max_value ;
49-
50- * size_type = LYPLG_LYB_SIZE_FIXED_BITS ;
44+ LY_ARRAY_COUNT_TYPE u ;
45+ uint32_t max_value = 0 ;
5146
52- if (type_enum -> enums [0 ].value < 0 ) {
53- /* we need the full 4 bytes */
54- * fixed_size_bits = 32 ;
55- return ;
47+ if (size_type ) {
48+ * size_type = LYPLG_LYB_SIZE_FIXED_BITS ;
5649 }
5750
58- /* value of the last enum, sorted */
59- max_value = type_enum -> enums [LY_ARRAY_COUNT (type_enum -> enums ) - 1 ].value ;
51+ /* learn the max value of enums */
52+ LY_ARRAY_FOR (type_enum -> enums , u ) {
53+ if (type_enum -> enums [u ].value < 0 ) {
54+ /* we need the full 4 bytes */
55+ * fixed_size_bits = 32 ;
56+ return ;
57+ }
58+
59+ if ((unsigned )type_enum -> enums [u ].value > max_value ) {
60+ max_value = type_enum -> enums [u ].value ;
61+ }
62+ }
6063
6164 /* learn the position of the highest set bit, the least amount of bits that can hold the number */
6265 * fixed_size_bits = lyplg_type_get_highest_set_bit_pos (max_value );
@@ -71,6 +74,7 @@ lyplg_type_store_enum(const struct ly_ctx *ctx, const struct lysc_type *type, co
7174 struct lysc_type_enum * type_enum = (struct lysc_type_enum * )type ;
7275 LY_ERR ret = LY_SUCCESS ;
7376 LY_ARRAY_COUNT_TYPE u ;
77+ uint32_t fixed_size_bits ;
7478 ly_bool found = 0 ;
7579 uint32_t value_size ;
7680 int64_t num = 0 ;
@@ -81,8 +85,9 @@ lyplg_type_store_enum(const struct ly_ctx *ctx, const struct lysc_type *type, co
8185 storage -> realtype = type ;
8286
8387 /* check value length */
88+ lyplg_type_lyb_size_enum (type , NULL , & fixed_size_bits );
8489 ret = lyplg_type_check_value_size ("enumeration" , format , value_size_bits , LYPLG_LYB_SIZE_FIXED_BITS ,
85- ENUM_FIXED_SIZE_BITS ( type ) , & value_size , err );
90+ fixed_size_bits , & value_size , err );
8691 LY_CHECK_GOTO (ret , cleanup );
8792
8893 if (format == LY_VALUE_LYB ) {
@@ -176,17 +181,20 @@ static const void *
176181lyplg_type_print_enum (const struct ly_ctx * UNUSED (ctx ), const struct lyd_value * value , LY_VALUE_FORMAT format ,
177182 void * UNUSED (prefix_data ), ly_bool * dynamic , uint32_t * value_size_bits )
178183{
184+ uint32_t fixed_size_bits ;
179185 int64_t prev_num = 0 , num = 0 ;
180186 void * buf ;
181187
182188 if (format == LY_VALUE_LYB ) {
189+ lyplg_type_lyb_size_enum (value -> realtype , NULL , & fixed_size_bits );
190+
183191 prev_num = num = value -> enum_item -> value ;
184192 num = htole64 (num );
185193 if (num == prev_num ) {
186194 /* values are equal, little-endian */
187195 * dynamic = 0 ;
188196 if (value_size_bits ) {
189- * value_size_bits = ENUM_FIXED_SIZE_BITS ( value -> realtype ) ;
197+ * value_size_bits = fixed_size_bits ;
190198 }
191199 return & value -> enum_item -> value ;
192200 } else {
@@ -196,7 +204,7 @@ lyplg_type_print_enum(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *
196204
197205 * dynamic = 1 ;
198206 if (value_size_bits ) {
199- * value_size_bits = ENUM_FIXED_SIZE_BITS ( value -> realtype ) ;
207+ * value_size_bits = fixed_size_bits ;
200208 }
201209 memcpy (buf , & num , 4 );
202210 return buf ;
0 commit comments