Skip to content

Commit

Permalink
structure comparison functions
Browse files Browse the repository at this point in the history
  • Loading branch information
vlm committed Aug 10, 2017
1 parent 086ee9a commit cd2f48e
Show file tree
Hide file tree
Showing 119 changed files with 975 additions and 123 deletions.
9 changes: 9 additions & 0 deletions asn1c/unber.c
Original file line number Diff line number Diff line change
Expand Up @@ -828,3 +828,12 @@ xer_whitespace_span(const void *chunk_buf, size_t chunk_size) {
(void)chunk_size;
return 0;
}

int
OCTET_STRING_compare(const asn_TYPE_descriptor_t *td, const void *a,
const void *b) {
(void)td;
(void)a;
(void)b;
return 0;
}
1 change: 1 addition & 0 deletions libasn1compiler/asn1c_C.c
Original file line number Diff line number Diff line change
Expand Up @@ -2829,6 +2829,7 @@ do { \

FUNCREF2(free);
FUNCREF2(print);
FUNCREF2(compare);
if (arg->flags & A1C_NO_CONSTRAINTS)
OUT("0,\t/* No check because of -fno-constraints */\n");
else
Expand Down
1 change: 1 addition & 0 deletions skeletons/ANY.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ asn_TYPE_descriptor_t asn_DEF_ANY = {
"ANY",
OCTET_STRING_free,
OCTET_STRING_print,
OCTET_STRING_compare,
asn_generic_no_constraint,
OCTET_STRING_decode_ber,
OCTET_STRING_encode_der,
Expand Down
44 changes: 44 additions & 0 deletions skeletons/BIT_STRING.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ asn_TYPE_descriptor_t asn_DEF_BIT_STRING = {
"BIT_STRING",
OCTET_STRING_free, /* Implemented in terms of OCTET STRING */
BIT_STRING_print,
BIT_STRING_compare,
BIT_STRING_constraint,
OCTET_STRING_decode_ber, /* Implemented in terms of OCTET STRING */
OCTET_STRING_encode_der, /* Implemented in terms of OCTET STRING */
Expand Down Expand Up @@ -200,3 +201,46 @@ BIT_STRING_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
return 0;
}

/*
* Lexicographically compare the common prefix of both strings,
* and if it is the same return -1 for the smallest string.
*/
int
BIT_STRING_compare(const asn_TYPE_descriptor_t *td, const void *aptr,
const void *bptr) {
const BIT_STRING_t *a = aptr;
const BIT_STRING_t *b = bptr;

(void)td;

if(a && b) {
size_t common_prefix_size = a->size <= b->size ? a->size : b->size;
int ret = memcmp(a->buf, b->buf, common_prefix_size);
if(ret == 0) {
/* Figure out which string with equal prefixes is longer. */
if(a->size < b->size) {
return -1;
} else if(a->size > b->size) {
return 1;
} else {
/* Figure out how many unused bits */
if(a->bits_unused < b->bits_unused) {
return -1;
} else if(a->bits_unused > b->bits_unused) {
return 1;
} else {
return 0;
}
}
} else {
return ret;
}
} else if(!a && !b) {
return 0;
} else if(!a) {
return -1;
} else {
return 1;
}
}

1 change: 1 addition & 0 deletions skeletons/BIT_STRING.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ extern asn_TYPE_descriptor_t asn_DEF_BIT_STRING;
extern asn_OCTET_STRING_specifics_t asn_SPC_BIT_STRING_specs;

asn_struct_print_f BIT_STRING_print; /* Human-readable output */
asn_struct_compare_f BIT_STRING_compare;
asn_constr_check_f BIT_STRING_constraint;
xer_type_encoder_f BIT_STRING_encode_xer;

Expand Down
1 change: 1 addition & 0 deletions skeletons/BMPString.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ asn_TYPE_descriptor_t asn_DEF_BMPString = {
"BMPString",
OCTET_STRING_free, /* Implemented in terms of OCTET STRING */
BMPString_print,
OCTET_STRING_compare,
asn_generic_no_constraint, /* No constraint by default */
OCTET_STRING_decode_ber,
OCTET_STRING_encode_der,
Expand Down
15 changes: 8 additions & 7 deletions skeletons/BMPString.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ asn_struct_print_f BMPString_print; /* Human-readable output */
xer_type_decoder_f BMPString_decode_xer;
xer_type_encoder_f BMPString_encode_xer;

#define BMPString_free OCTET_STRING_free
#define BMPString_print BMPString_print
#define BMPString_constraint asn_generic_no_constraint
#define BMPString_decode_ber OCTET_STRING_decode_ber
#define BMPString_encode_der OCTET_STRING_encode_der
#define BMPString_decode_uper OCTET_STRING_decode_uper
#define BMPString_encode_uper OCTET_STRING_encode_uper
#define BMPString_free OCTET_STRING_free
#define BMPString_print BMPString_print
#define BMPString_compare OCTET_STRING_compare
#define BMPString_constraint asn_generic_no_constraint
#define BMPString_decode_ber OCTET_STRING_decode_ber
#define BMPString_encode_der OCTET_STRING_encode_der
#define BMPString_decode_uper OCTET_STRING_decode_uper
#define BMPString_encode_uper OCTET_STRING_encode_uper

#ifdef __cplusplus
}
Expand Down
25 changes: 25 additions & 0 deletions skeletons/BOOLEAN.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ asn_TYPE_descriptor_t asn_DEF_BOOLEAN = {
"BOOLEAN",
BOOLEAN_free,
BOOLEAN_print,
BOOLEAN_compare,
asn_generic_no_constraint,
BOOLEAN_decode_ber,
BOOLEAN_encode_der,
Expand Down Expand Up @@ -296,3 +297,27 @@ BOOLEAN_encode_uper(asn_TYPE_descriptor_t *td,

ASN__ENCODED_OK(er);
}


int
BOOLEAN_compare(const asn_TYPE_descriptor_t *td, const void *aptr,
const void *bptr) {
const BOOLEAN_t *a = aptr;
const BOOLEAN_t *b = bptr;

(void)td;

if(a && b) {
if(*a == *b) {
return 0;
} else if(!*a) {
return -1;
} else {
return 1;
}
} else if(!a) {
return -1;
} else {
return 1;
}
}
1 change: 1 addition & 0 deletions skeletons/BOOLEAN.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ extern asn_TYPE_descriptor_t asn_DEF_BOOLEAN;

asn_struct_free_f BOOLEAN_free;
asn_struct_print_f BOOLEAN_print;
asn_struct_compare_f BOOLEAN_compare;
ber_type_decoder_f BOOLEAN_decode_ber;
der_type_encoder_f BOOLEAN_encode_der;
xer_type_decoder_f BOOLEAN_decode_xer;
Expand Down
1 change: 1 addition & 0 deletions skeletons/ENUMERATED.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ asn_TYPE_descriptor_t asn_DEF_ENUMERATED = {
"ENUMERATED",
ASN__PRIMITIVE_TYPE_free,
INTEGER_print, /* Implemented in terms of INTEGER */
INTEGER_compare, /* Implemented in terms of INTEGER */
asn_generic_no_constraint,
ber_decode_primitive,
INTEGER_encode_der, /* Implemented in terms of INTEGER */
Expand Down
1 change: 1 addition & 0 deletions skeletons/ENUMERATED.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ per_type_encoder_f ENUMERATED_encode_uper;

#define ENUMERATED_free ASN__PRIMITIVE_TYPE_free
#define ENUMERATED_print INTEGER_print
#define ENUMERATED_compare INTEGER_compare
#define ENUMERATED_constraint asn_generic_no_constraint
#define ENUMERATED_decode_ber ber_decode_primitive
#define ENUMERATED_encode_der INTEGER_encode_der
Expand Down
1 change: 1 addition & 0 deletions skeletons/GeneralString.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ asn_TYPE_descriptor_t asn_DEF_GeneralString = {
"GeneralString",
OCTET_STRING_free,
OCTET_STRING_print, /* non-ascii string */
OCTET_STRING_compare,
asn_generic_unknown_constraint,
OCTET_STRING_decode_ber, /* Implemented in terms of OCTET STRING */
OCTET_STRING_encode_der,
Expand Down
19 changes: 10 additions & 9 deletions skeletons/GeneralString.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@ typedef OCTET_STRING_t GeneralString_t; /* Implemented via OCTET STRING */

extern asn_TYPE_descriptor_t asn_DEF_GeneralString;

#define GeneralString_free OCTET_STRING_free
#define GeneralString_print OCTET_STRING_print
#define GeneralString_constraint asn_generic_unknown_constraint
#define GeneralString_decode_ber OCTET_STRING_decode_ber
#define GeneralString_encode_der OCTET_STRING_encode_der
#define GeneralString_decode_xer OCTET_STRING_decode_xer_hex
#define GeneralString_encode_xer OCTET_STRING_encode_xer
#define GeneralString_decode_uper OCTET_STRING_decode_uper
#define GeneralString_encode_uper OCTET_STRING_encode_uper
#define GeneralString_free OCTET_STRING_free
#define GeneralString_print OCTET_STRING_print
#define GeneralString_compare OCTET_STRING_compare
#define GeneralString_constraint asn_generic_unknown_constraint
#define GeneralString_decode_ber OCTET_STRING_decode_ber
#define GeneralString_encode_der OCTET_STRING_encode_der
#define GeneralString_decode_xer OCTET_STRING_decode_xer_hex
#define GeneralString_encode_xer OCTET_STRING_encode_xer
#define GeneralString_decode_uper OCTET_STRING_decode_uper
#define GeneralString_encode_uper OCTET_STRING_encode_uper

#ifdef __cplusplus
}
Expand Down
1 change: 1 addition & 0 deletions skeletons/GeneralizedTime.c
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ asn_TYPE_descriptor_t asn_DEF_GeneralizedTime = {
"GeneralizedTime",
OCTET_STRING_free,
GeneralizedTime_print,
OCTET_STRING_compare, /* Does not normalize time zones! */
GeneralizedTime_constraint, /* Check validity of time */
OCTET_STRING_decode_ber, /* Implemented in terms of OCTET STRING */
GeneralizedTime_encode_der,
Expand Down
1 change: 1 addition & 0 deletions skeletons/GraphicString.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ asn_TYPE_descriptor_t asn_DEF_GraphicString = {
"GraphicString",
OCTET_STRING_free,
OCTET_STRING_print, /* non-ascii string */
OCTET_STRING_compare,
asn_generic_unknown_constraint,
OCTET_STRING_decode_ber, /* Implemented in terms of OCTET STRING */
OCTET_STRING_encode_der,
Expand Down
19 changes: 10 additions & 9 deletions skeletons/GraphicString.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@ typedef OCTET_STRING_t GraphicString_t; /* Implemented via OCTET STRING */

extern asn_TYPE_descriptor_t asn_DEF_GraphicString;

#define GraphicString_free OCTET_STRING_free
#define GraphicString_print OCTET_STRING_print
#define GraphicString_constraint asn_generic_unknown_constraint
#define GraphicString_decode_ber OCTET_STRING_decode_ber
#define GraphicString_encode_der OCTET_STRING_encode_der
#define GraphicString_decode_xer OCTET_STRING_decode_xer_hex
#define GraphicString_encode_xer OCTET_STRING_encode_xer
#define GraphicString_decode_uper OCTET_STRING_decode_uper
#define GraphicString_encode_uper OCTET_STRING_encode_uper
#define GraphicString_free OCTET_STRING_free
#define GraphicString_print OCTET_STRING_print
#define GraphicString_compare OCTET_STRING_compare
#define GraphicString_constraint asn_generic_unknown_constraint
#define GraphicString_decode_ber OCTET_STRING_decode_ber
#define GraphicString_encode_der OCTET_STRING_encode_der
#define GraphicString_decode_xer OCTET_STRING_decode_xer_hex
#define GraphicString_encode_xer OCTET_STRING_encode_xer
#define GraphicString_decode_uper OCTET_STRING_decode_uper
#define GraphicString_encode_uper OCTET_STRING_encode_uper

#ifdef __cplusplus
}
Expand Down
1 change: 1 addition & 0 deletions skeletons/IA5String.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ asn_TYPE_descriptor_t asn_DEF_IA5String = {
"IA5String",
OCTET_STRING_free,
OCTET_STRING_print_utf8, /* ASCII subset */
OCTET_STRING_compare,
IA5String_constraint, /* Constraint on the alphabet */
OCTET_STRING_decode_ber, /* Implemented in terms of OCTET STRING */
OCTET_STRING_encode_der,
Expand Down
17 changes: 9 additions & 8 deletions skeletons/IA5String.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@ extern asn_TYPE_descriptor_t asn_DEF_IA5String;

asn_constr_check_f IA5String_constraint;

#define IA5String_free OCTET_STRING_free
#define IA5String_print OCTET_STRING_print_utf8
#define IA5String_decode_ber OCTET_STRING_decode_ber
#define IA5String_encode_der OCTET_STRING_encode_der
#define IA5String_decode_xer OCTET_STRING_decode_xer_utf8
#define IA5String_encode_xer OCTET_STRING_encode_xer_utf8
#define IA5String_decode_uper OCTET_STRING_decode_uper
#define IA5String_encode_uper OCTET_STRING_encode_uper
#define IA5String_free OCTET_STRING_free
#define IA5String_print OCTET_STRING_print_utf8
#define IA5String_compare OCTET_STRING_compare
#define IA5String_decode_ber OCTET_STRING_decode_ber
#define IA5String_encode_der OCTET_STRING_encode_der
#define IA5String_decode_xer OCTET_STRING_decode_xer_utf8
#define IA5String_encode_xer OCTET_STRING_encode_xer_utf8
#define IA5String_decode_uper OCTET_STRING_decode_uper
#define IA5String_encode_uper OCTET_STRING_encode_uper

#ifdef __cplusplus
}
Expand Down
44 changes: 44 additions & 0 deletions skeletons/INTEGER.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ asn_TYPE_descriptor_t asn_DEF_INTEGER = {
"INTEGER",
INTEGER_free,
INTEGER_print,
INTEGER_compare,
asn_generic_no_constraint,
ber_decode_primitive,
INTEGER_encode_der,
Expand Down Expand Up @@ -1084,3 +1085,46 @@ asn_strtol_lim(const char *str, const char **end, long *lp) {
return ASN_STRTOX_ERROR_INVAL;
}

int
INTEGER_compare(const asn_TYPE_descriptor_t *td, const void *aptr,
const void *bptr) {
const INTEGER_t *a = aptr;
const INTEGER_t *b = bptr;

(void)td;

if(a && b) {
if(a->size && b->size) {
int sign_a = (a->buf[0] & 0x80) ? -1 : 1;
int sign_b = (b->buf[0] & 0x80) ? -1 : 1;

if(sign_a < sign_b) return -1;
if(sign_a > sign_b) return 1;

/* The shortest integer wins, unless comparing negatives */
if(a->size < b->size) {
return -1 * sign_a;
} else if(a->size > b->size) {
return 1 * sign_b;
}

return sign_a * memcmp(a->buf, b->buf, a->size);
} else if(a->size) {
int sign = (a->buf[0] & 0x80) ? -1 : 1;
return (1) * sign;
} else if(b->size) {
int sign = (a->buf[0] & 0x80) ? -1 : 1;
return (-1) * sign;
} else {
return 0;
}
} else if(!a && !b) {
return 0;
} else if(!a) {
return -1;
} else {
return 1;
}

}

1 change: 1 addition & 0 deletions skeletons/INTEGER.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ typedef const struct asn_INTEGER_specifics_s {

#define INTEGER_free ASN__PRIMITIVE_TYPE_free
asn_struct_print_f INTEGER_print;
asn_struct_compare_f INTEGER_compare;
ber_type_decoder_f INTEGER_decode_ber;
der_type_encoder_f INTEGER_encode_der;
xer_type_decoder_f INTEGER_decode_xer;
Expand Down
1 change: 1 addition & 0 deletions skeletons/ISO646String.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ asn_TYPE_descriptor_t asn_DEF_ISO646String = {
"ISO646String",
OCTET_STRING_free,
OCTET_STRING_print_utf8, /* ASCII subset */
OCTET_STRING_compare,
VisibleString_constraint,
OCTET_STRING_decode_ber, /* Implemented in terms of OCTET STRING */
OCTET_STRING_encode_der,
Expand Down
19 changes: 10 additions & 9 deletions skeletons/ISO646String.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,16 @@ typedef VisibleString_t ISO646String_t; /* Implemented using VisibleString */

extern asn_TYPE_descriptor_t asn_DEF_ISO646String;

#define ISO646String_free OCTET_STRING_free
#define ISO646String_print OCTET_STRING_print_utf8
#define ISO646String_constraint VisibleString_constraint
#define ISO646String_decode_ber OCTET_STRING_decode_ber
#define ISO646String_encode_der OCTET_STRING_encode_der
#define ISO646String_decode_xer OCTET_STRING_decode_xer_utf8
#define ISO646String_encode_xer OCTET_STRING_encode_xer_utf8
#define ISO646String_decode_uper OCTET_STRING_decode_uper
#define ISO646String_encode_uper OCTET_STRING_encode_uper
#define ISO646String_free OCTET_STRING_free
#define ISO646String_print OCTET_STRING_print_utf8
#define ISO646String_compare OCTET_STRING_compare
#define ISO646String_constraint VisibleString_constraint
#define ISO646String_decode_ber OCTET_STRING_decode_ber
#define ISO646String_encode_der OCTET_STRING_encode_der
#define ISO646String_decode_xer OCTET_STRING_decode_xer_utf8
#define ISO646String_encode_xer OCTET_STRING_encode_xer_utf8
#define ISO646String_decode_uper OCTET_STRING_decode_uper
#define ISO646String_encode_uper OCTET_STRING_encode_uper

#ifdef __cplusplus
}
Expand Down
Loading

0 comments on commit cd2f48e

Please sign in to comment.