Skip to content

Commit 14b7704

Browse files
committed
integer BUGFIX big-endian print fixes
Refs #2455
1 parent 47fd95b commit 14b7704

File tree

1 file changed

+24
-72
lines changed

1 file changed

+24
-72
lines changed

src/plugins_types/integer.c

Lines changed: 24 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -291,64 +291,6 @@ lyplg_type_sort_int(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *va
291291
return 0;
292292
}
293293

294-
static const void *
295-
lyplg_type_print_int(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *value, LY_VALUE_FORMAT format,
296-
void *UNUSED(prefix_data), ly_bool *dynamic, uint32_t *value_size_bits)
297-
{
298-
int64_t prev_num = 0, num = 0;
299-
void *buf;
300-
301-
if (format == LY_VALUE_LYB) {
302-
switch (value->realtype->basetype) {
303-
case LY_TYPE_INT8:
304-
prev_num = num = value->int8;
305-
break;
306-
case LY_TYPE_INT16:
307-
prev_num = num = value->int16;
308-
break;
309-
case LY_TYPE_INT32:
310-
prev_num = num = value->int32;
311-
break;
312-
case LY_TYPE_INT64:
313-
prev_num = num = value->int64;
314-
break;
315-
default:
316-
break;
317-
}
318-
319-
num = htole64(num);
320-
if (num == prev_num) {
321-
/* values are equal, little-endian or int8 */
322-
*dynamic = 0;
323-
if (value_size_bits) {
324-
/* the least amount of bits that can hold the number */
325-
*value_size_bits = lyplg_type_get_highest_set_bit_pos(num);
326-
}
327-
return &value->int64;
328-
} else {
329-
/* values differ, big-endian */
330-
buf = calloc(1, LYPLG_BITS2BYTES(lyplg_type_get_highest_set_bit_pos(num)));
331-
LY_CHECK_RET(!buf, NULL);
332-
333-
*dynamic = 1;
334-
if (value_size_bits) {
335-
*value_size_bits = lyplg_type_get_highest_set_bit_pos(num);
336-
}
337-
memcpy(buf, &num, LYPLG_BITS2BYTES(lyplg_type_get_highest_set_bit_pos(num)));
338-
return buf;
339-
}
340-
}
341-
342-
/* use the cached canonical value */
343-
if (dynamic) {
344-
*dynamic = 0;
345-
}
346-
if (value_size_bits) {
347-
*value_size_bits = strlen(value->_canonical) * 8;
348-
}
349-
return value->_canonical;
350-
}
351-
352294
static LY_ERR
353295
lyplg_type_store_uint(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value, uint32_t value_size_bits,
354296
uint32_t options, LY_VALUE_FORMAT format, void *UNUSED(prefix_data), uint32_t hints,
@@ -573,31 +515,37 @@ lyplg_type_sort_uint(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *v
573515
}
574516

575517
static const void *
576-
lyplg_type_print_uint(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *value, LY_VALUE_FORMAT format,
518+
lyplg_type_print_u_int(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *value, LY_VALUE_FORMAT format,
577519
void *UNUSED(prefix_data), ly_bool *dynamic, uint32_t *value_size_bits)
578520
{
579521
uint64_t num = 0;
522+
uint8_t bytes_used;
523+
uint16_t bits_used;
580524
void *buf;
581525

582526
if (format == LY_VALUE_LYB) {
583527
switch (value->realtype->basetype) {
584528
case LY_TYPE_UINT8:
529+
case LY_TYPE_INT8:
585530
num = value->uint8;
586531
break;
587532
case LY_TYPE_UINT16:
533+
case LY_TYPE_INT16:
588534
num = value->uint16;
589535
break;
590536
case LY_TYPE_UINT32:
537+
case LY_TYPE_INT32:
591538
num = value->uint32;
592539
break;
593540
case LY_TYPE_UINT64:
541+
case LY_TYPE_INT64:
594542
num = value->uint64;
595543
break;
596544
default:
597545
break;
598546
}
599-
num = htole64(num);
600-
if (num == value->uint64) {
547+
548+
if (htole64(num) == value->uint64) {
601549
/* values are equal, little-endian or uint8 */
602550
*dynamic = 0;
603551
if (value_size_bits) {
@@ -607,14 +555,18 @@ lyplg_type_print_uint(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *
607555
return &value->uint64;
608556
} else {
609557
/* values differ, big-endian */
610-
buf = calloc(1, LYPLG_BITS2BYTES(lyplg_type_get_highest_set_bit_pos(num)));
558+
bits_used = lyplg_type_get_highest_set_bit_pos(num);
559+
bytes_used = LYPLG_BITS2BYTES(bits_used);
560+
561+
buf = calloc(1, bytes_used);
611562
LY_CHECK_RET(!buf, NULL);
563+
num = htole64(num);
564+
memcpy(buf, &num, bytes_used);
612565

613566
*dynamic = 1;
614567
if (value_size_bits) {
615-
*value_size_bits = lyplg_type_get_highest_set_bit_pos(num);
568+
*value_size_bits = bits_used;
616569
}
617-
memcpy(buf, &num, LYPLG_BITS2BYTES(lyplg_type_get_highest_set_bit_pos(num)));
618570
return buf;
619571
}
620572
}
@@ -649,7 +601,7 @@ const struct lyplg_type_record plugins_integer[] = {
649601
.plugin.validate_tree = NULL,
650602
.plugin.compare = lyplg_type_compare_uint,
651603
.plugin.sort = lyplg_type_sort_uint,
652-
.plugin.print = lyplg_type_print_uint,
604+
.plugin.print = lyplg_type_print_u_int,
653605
.plugin.duplicate = lyplg_type_dup_simple,
654606
.plugin.free = lyplg_type_free_simple,
655607
}, {
@@ -664,7 +616,7 @@ const struct lyplg_type_record plugins_integer[] = {
664616
.plugin.validate_tree = NULL,
665617
.plugin.compare = lyplg_type_compare_uint,
666618
.plugin.sort = lyplg_type_sort_uint,
667-
.plugin.print = lyplg_type_print_uint,
619+
.plugin.print = lyplg_type_print_u_int,
668620
.plugin.duplicate = lyplg_type_dup_simple,
669621
.plugin.free = lyplg_type_free_simple,
670622
}, {
@@ -679,7 +631,7 @@ const struct lyplg_type_record plugins_integer[] = {
679631
.plugin.validate_tree = NULL,
680632
.plugin.compare = lyplg_type_compare_uint,
681633
.plugin.sort = lyplg_type_sort_uint,
682-
.plugin.print = lyplg_type_print_uint,
634+
.plugin.print = lyplg_type_print_u_int,
683635
.plugin.duplicate = lyplg_type_dup_simple,
684636
.plugin.free = lyplg_type_free_simple,
685637
}, {
@@ -694,7 +646,7 @@ const struct lyplg_type_record plugins_integer[] = {
694646
.plugin.validate_tree = NULL,
695647
.plugin.compare = lyplg_type_compare_uint,
696648
.plugin.sort = lyplg_type_sort_uint,
697-
.plugin.print = lyplg_type_print_uint,
649+
.plugin.print = lyplg_type_print_u_int,
698650
.plugin.duplicate = lyplg_type_dup_simple,
699651
.plugin.free = lyplg_type_free_simple,
700652
}, {
@@ -709,7 +661,7 @@ const struct lyplg_type_record plugins_integer[] = {
709661
.plugin.validate_tree = NULL,
710662
.plugin.compare = lyplg_type_compare_int,
711663
.plugin.sort = lyplg_type_sort_int,
712-
.plugin.print = lyplg_type_print_int,
664+
.plugin.print = lyplg_type_print_u_int,
713665
.plugin.duplicate = lyplg_type_dup_simple,
714666
.plugin.free = lyplg_type_free_simple,
715667
}, {
@@ -724,7 +676,7 @@ const struct lyplg_type_record plugins_integer[] = {
724676
.plugin.validate_tree = NULL,
725677
.plugin.compare = lyplg_type_compare_int,
726678
.plugin.sort = lyplg_type_sort_int,
727-
.plugin.print = lyplg_type_print_int,
679+
.plugin.print = lyplg_type_print_u_int,
728680
.plugin.duplicate = lyplg_type_dup_simple,
729681
.plugin.free = lyplg_type_free_simple,
730682
}, {
@@ -739,7 +691,7 @@ const struct lyplg_type_record plugins_integer[] = {
739691
.plugin.validate_tree = NULL,
740692
.plugin.compare = lyplg_type_compare_int,
741693
.plugin.sort = lyplg_type_sort_int,
742-
.plugin.print = lyplg_type_print_int,
694+
.plugin.print = lyplg_type_print_u_int,
743695
.plugin.duplicate = lyplg_type_dup_simple,
744696
.plugin.free = lyplg_type_free_simple,
745697
}, {
@@ -754,7 +706,7 @@ const struct lyplg_type_record plugins_integer[] = {
754706
.plugin.validate_tree = NULL,
755707
.plugin.compare = lyplg_type_compare_int,
756708
.plugin.sort = lyplg_type_sort_int,
757-
.plugin.print = lyplg_type_print_int,
709+
.plugin.print = lyplg_type_print_u_int,
758710
.plugin.duplicate = lyplg_type_dup_simple,
759711
.plugin.free = lyplg_type_free_simple,
760712
},

0 commit comments

Comments
 (0)