Skip to content

Commit

Permalink
Default put/unput routines
Browse files Browse the repository at this point in the history
 - After der_unpack() use der_put_default() to allow defaults to be set
 - Before der_pack() use der_unput_default() to remove values matching the default
  • Loading branch information
vanrein committed May 19, 2019
1 parent aa2460a commit f346d69
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 0 deletions.
18 changes: 18 additions & 0 deletions include/arpa2/quick-der.h
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,24 @@ int der_get_bool (dercursor crs, bool *valp);
typedef uint8_t der_buf_bool_t [1];
dercursor der_put_bool (uint8_t *der_buf_bool, bool value);

/* When the optional is absent, set its value to the default.
* Since Quick DER does not process default values, this must be
* done manually. The work can help to simplify programs, by
* reducing the number of code paths and improve coverage.
*
* This function is not directly usable for CHOICE, which is
* unrolled into a sequence of dercursor values that may or may
* not have a value, but the multiplicity of the values is not
* taken care of below.
*/
void der_put_default (dercursor *optional, dercursor default_value);

/* Test if the appointed optional equals the default. If so, set it
* to the default/absent entry, .derptr==NULL and .derlen==0
* This is useful prior to sending.
*/
void der_unput_default (dercursor *optional, dercursor default_value);

/* Compare the values pointed to by cursors @p c1 and @p c2.
* Returns 0 if the (binary) values are equal, otherwise returns
* the difference between the first two differing bytes (similar
Expand Down
2 changes: 2 additions & 0 deletions lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ set(quickder_SRC
der_put_uint32.c
der_get_bool.c
der_put_bool.c
der_put_default.c
der_unput_default.c
der_cmp.c
der_cmp_int.c
)
Expand Down
18 changes: 18 additions & 0 deletions lib/der_put_default.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

#include <arpa2/quick-der.h>

/* When the optional is absent, set its value to the default.
* Since Quick DER does not process default values, this must be
* done manually. The work can help to simplify programs, by
* reducing the number of code paths and improve coverage.
*
* This function is not directly usable for CHOICE, which is
* unrolled into a sequence of dercursor values that may or may
* not have a value, but the multiplicity of the values is not
* taken care of below.
*/
void der_put_default (dercursor *optional, dercursor default_value) {
if (optional->derptr == NULL) {
*optional = default_value;
}
}
12 changes: 12 additions & 0 deletions lib/der_unput_default.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

#include <arpa2/quick-der.h>

/* Test if the appointed optional equals the default. If so, set it
* to the default/absent entry, .derptr==NULL and .derlen==0
* This is useful prior to sending.
*/
void der_unput_default (dercursor *optional, dercursor default_value) {
if (der_cmp (*optional, default_value) == 0) {
memset (optional, 0, sizeof (dercursor));
}
}
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,4 @@ c_test(der_data)
c_test(int_putget)
c_test(bool_putget)
c_test(data_putget)
c_test(default_putunput)
91 changes: 91 additions & 0 deletions test/default_putunput.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@

#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>

#include <arpa2/quick-der.h>


dercursor abba = {
.derptr = "ABBA",
.derlen = 4
};

dercursor ledz = {
.derptr = "LED Zeppelin",
.derlen = 12
};

dercursor ledy = {
.derptr = "LED Zeppelinny",
.derlen = 12
};

dercursor null = {
.derptr = NULL,
.derlen = 0
};

dercursor band;


int errors = 0;


void maybe (dercursor dflt) {
der_put_default (&band, dflt);
}


void notbe (dercursor dflt) {
der_unput_default (&band, dflt);
}


void should (dercursor target) {
static int testnr = 0;
if (der_cmp (band, target) != 0) {
errors++;
fprintf (stderr, "Test #%d failed; found \"%.*s\", expected \"%.*s\"\n",
testnr, band.derlen, band.derptr, target.derlen, target.derptr);
}
testnr++;
}


int main (int argc, char *argv []) {
errors = 0;
band = null;
should (null);
maybe (abba);
should (abba);
maybe (abba);
should (abba);
maybe (ledz);
should (abba);
notbe (ledz);
should (abba);
notbe (abba);
should (null);
maybe (ledz);
should (ledz);
should (ledy);
notbe (ledy);
should (null);
maybe (ledy);
should (ledy);
should (ledz);
maybe (abba);
should (ledy);
should (ledz);
maybe (ledz);
should (ledy);
should (ledz);
maybe (null);
should (ledz);
should (ledy);
notbe (ledz);
should (null);
exit (errors);
}

0 comments on commit f346d69

Please sign in to comment.