Skip to content

Commit

Permalink
Fixes generation of uper C code for 24 bit integers (#104)
Browse files Browse the repository at this point in the history
* Added test case AQ with 24 bit integer

* Closing #103: Fixed generated uper C code for 24 bit integers
  • Loading branch information
Futsch1 authored Jan 11, 2021
1 parent 094be3b commit ba37a94
Show file tree
Hide file tree
Showing 12 changed files with 331 additions and 11 deletions.
5 changes: 2 additions & 3 deletions asn1tools/source/c/uper.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,8 @@ def format_integer_inner(self, type_, checker):
suffix = type_name[:-2]
location = self.location_inner()

if (type_.number_of_bits % 8) == 0 and checker.minimum in [0, -128, -32768,
-2147483648,
-9223372036854775808]:
if type_.number_of_bits in [8, 16, 32, 64] and \
checker.minimum in [0, -128, -32768, -2147483648, -9223372036854775808]:
return (
[
'encoder_append_{}(encoder_p, src_p->{});'.format(
Expand Down
2 changes: 1 addition & 1 deletion asn1tools/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.155.0'
__version__ = '0.155.1'
2 changes: 2 additions & 0 deletions tests/files/c_source/c_source.asn
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,8 @@ AP ::= SEQUENCE {
d REFERENCED-INT DEFAULT 1
}

AQ ::= INTEGER (0..16777215)

END

CRef DEFINITIONS AUTOMATIC TAGS ::=
Expand Down
42 changes: 41 additions & 1 deletion tests/files/c_source/oer.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
*/

/**
* This file was generated by asn1tools version 0.153.2 Thu Dec 17 08:25:19 2020.
* This file was generated by asn1tools version 0.154.0 Mon Jan 11 17:42:41 2021.
*/

#include <string.h>
Expand Down Expand Up @@ -4704,6 +4704,20 @@ static void oer_c_source_ap_decode_inner(
}
}

static void oer_c_source_aq_encode_inner(
struct encoder_t *encoder_p,
const struct oer_c_source_aq_t *src_p)
{
encoder_append_uint32(encoder_p, src_p->value);
}

static void oer_c_source_aq_decode_inner(
struct decoder_t *decoder_p,
struct oer_c_source_aq_t *dst_p)
{
dst_p->value = decoder_read_uint32(decoder_p);
}

static void oer_c_source_b_encode_inner(
struct encoder_t *encoder_p,
const struct oer_c_source_b_t *src_p)
Expand Down Expand Up @@ -6108,6 +6122,32 @@ ssize_t oer_c_source_ap_decode(
return (decoder_get_result(&decoder));
}

ssize_t oer_c_source_aq_encode(
uint8_t *dst_p,
size_t size,
const struct oer_c_source_aq_t *src_p)
{
struct encoder_t encoder;

encoder_init(&encoder, dst_p, size);
oer_c_source_aq_encode_inner(&encoder, src_p);

return (encoder_get_result(&encoder));
}

ssize_t oer_c_source_aq_decode(
struct oer_c_source_aq_t *dst_p,
const uint8_t *src_p,
size_t size)
{
struct decoder_t decoder;

decoder_init(&decoder, src_p, size);
oer_c_source_aq_decode_inner(&decoder, dst_p);

return (decoder_get_result(&decoder));
}

ssize_t oer_c_source_b_encode(
uint8_t *dst_p,
size_t size,
Expand Down
37 changes: 36 additions & 1 deletion tests/files/c_source/oer.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
*/

/**
* This file was generated by asn1tools version 0.153.2 Thu Dec 17 08:25:19 2020.
* This file was generated by asn1tools version 0.154.0 Mon Jan 11 17:42:41 2021.
*/

#ifndef OER_H
Expand Down Expand Up @@ -901,6 +901,13 @@ struct oer_c_source_ap_t {
uint8_t d;
};

/**
* Type AQ in module CSource.
*/
struct oer_c_source_aq_t {
uint32_t value;
};

/**
* Type B in module CSource.
*/
Expand Down Expand Up @@ -1767,6 +1774,34 @@ ssize_t oer_c_source_ap_decode(
const uint8_t *src_p,
size_t size);

/**
* Encode type AQ defined in module CSource.
*
* @param[out] dst_p Buffer to encode into.
* @param[in] size Size of dst_p.
* @param[in] src_p Data to encode.
*
* @return Encoded data length or negative error code.
*/
ssize_t oer_c_source_aq_encode(
uint8_t *dst_p,
size_t size,
const struct oer_c_source_aq_t *src_p);

/**
* Decode type AQ defined in module CSource.
*
* @param[out] dst_p Decoded data.
* @param[in] src_p Data to decode.
* @param[in] size Size of src_p.
*
* @return Number of bytes decoded or negative error code.
*/
ssize_t oer_c_source_aq_decode(
struct oer_c_source_aq_t *dst_p,
const uint8_t *src_p,
size_t size);

/**
* Encode type B defined in module CSource.
*
Expand Down
52 changes: 51 additions & 1 deletion tests/files/c_source/oer_fuzzer.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
*/

/**
* This file was generated by asn1tools version 0.153.1 Mon Aug 17 18:19:15 2020.
* This file was generated by asn1tools version 0.154.0 Mon Jan 11 17:43:48 2021.
*/

#include <stdint.h>
Expand Down Expand Up @@ -1020,6 +1020,55 @@ static void test_oer_c_source_ap(
}
}

static void test_oer_c_source_aq(
const uint8_t *encoded_p,
size_t size)
{
ssize_t res;
ssize_t res2;
ssize_t i;
uint8_t encoded[size];
uint8_t encoded2[size];
struct oer_c_source_aq_t decoded;
struct oer_c_source_aq_t decoded2;

memset(&decoded, 0, sizeof(decoded));

res = oer_c_source_aq_decode(
&decoded,
encoded_p,
size);

if (res >= 0) {
res = oer_c_source_aq_encode(
&encoded[0],
sizeof(encoded),
&decoded);

assert_first_encode(res);

memset(&decoded2, 0, sizeof(decoded2));

res2 = oer_c_source_aq_decode(
&decoded2,
&encoded[0],
res);

assert_second_decode(res2);
assert_second_decode_data(&decoded,
&decoded2,
sizeof(decoded));

res2 = oer_c_source_aq_encode(
&encoded2[0],
sizeof(encoded2),
&decoded);

assert_second_encode(res, res2);
assert_second_encode_data(&encoded[0], &encoded2[0], res);
}
}

static void test_oer_c_source_b(
const uint8_t *encoded_p,
size_t size)
Expand Down Expand Up @@ -2805,6 +2854,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data_p, size_t size)
test_oer_c_source_an(data_p, size);
test_oer_c_source_ao(data_p, size);
test_oer_c_source_ap(data_p, size);
test_oer_c_source_aq(data_p, size);
test_oer_c_source_b(data_p, size);
test_oer_c_source_c(data_p, size);
test_oer_c_source_d(data_p, size);
Expand Down
48 changes: 47 additions & 1 deletion tests/files/c_source/uper.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
*/

/**
* This file was generated by asn1tools version 0.153.2 Wed Dec 16 21:44:50 2020.
* This file was generated by asn1tools version 0.155.1 Mon Jan 11 20:58:59 2021.
*/

#include <string.h>
Expand Down Expand Up @@ -3808,6 +3808,26 @@ static void uper_c_source_ap_decode_inner(
}
}

static void uper_c_source_aq_encode_inner(
struct encoder_t *encoder_p,
const struct uper_c_source_aq_t *src_p)
{
encoder_append_non_negative_binary_integer(
encoder_p,
(uint64_t)(src_p->value - 0),
24);
}

static void uper_c_source_aq_decode_inner(
struct decoder_t *decoder_p,
struct uper_c_source_aq_t *dst_p)
{
dst_p->value = decoder_read_non_negative_binary_integer(
decoder_p,
24);
dst_p->value += 0;
}

static void uper_c_source_b_encode_inner(
struct encoder_t *encoder_p,
const struct uper_c_source_b_t *src_p)
Expand Down Expand Up @@ -5011,6 +5031,32 @@ ssize_t uper_c_source_ap_decode(
return (decoder_get_result(&decoder));
}

ssize_t uper_c_source_aq_encode(
uint8_t *dst_p,
size_t size,
const struct uper_c_source_aq_t *src_p)
{
struct encoder_t encoder;

encoder_init(&encoder, dst_p, size);
uper_c_source_aq_encode_inner(&encoder, src_p);

return (encoder_get_result(&encoder));
}

ssize_t uper_c_source_aq_decode(
struct uper_c_source_aq_t *dst_p,
const uint8_t *src_p,
size_t size)
{
struct decoder_t decoder;

decoder_init(&decoder, src_p, size);
uper_c_source_aq_decode_inner(&decoder, dst_p);

return (decoder_get_result(&decoder));
}

ssize_t uper_c_source_b_encode(
uint8_t *dst_p,
size_t size,
Expand Down
37 changes: 36 additions & 1 deletion tests/files/c_source/uper.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
*/

/**
* This file was generated by asn1tools version 0.153.2 Wed Dec 16 21:44:50 2020.
* This file was generated by asn1tools version 0.154.0 Mon Jan 11 17:42:41 2021.
*/

#ifndef UPER_H
Expand Down Expand Up @@ -900,6 +900,13 @@ struct uper_c_source_ap_t {
uint8_t d;
};

/**
* Type AQ in module CSource.
*/
struct uper_c_source_aq_t {
uint32_t value;
};

/**
* Type B in module CSource.
*/
Expand Down Expand Up @@ -1689,6 +1696,34 @@ ssize_t uper_c_source_ap_decode(
const uint8_t *src_p,
size_t size);

/**
* Encode type AQ defined in module CSource.
*
* @param[out] dst_p Buffer to encode into.
* @param[in] size Size of dst_p.
* @param[in] src_p Data to encode.
*
* @return Encoded data length or negative error code.
*/
ssize_t uper_c_source_aq_encode(
uint8_t *dst_p,
size_t size,
const struct uper_c_source_aq_t *src_p);

/**
* Decode type AQ defined in module CSource.
*
* @param[out] dst_p Decoded data.
* @param[in] src_p Data to decode.
* @param[in] size Size of src_p.
*
* @return Number of bytes decoded or negative error code.
*/
ssize_t uper_c_source_aq_decode(
struct uper_c_source_aq_t *dst_p,
const uint8_t *src_p,
size_t size);

/**
* Encode type B defined in module CSource.
*
Expand Down
Loading

0 comments on commit ba37a94

Please sign in to comment.