-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added der_put_int32() and der_put_uint32() tools
- Loading branch information
Showing
6 changed files
with
168 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* Data-packing function. | ||
* | ||
* From: Rick van Rein <[email protected]> | ||
*/ | ||
|
||
|
||
#include <stdint.h> | ||
|
||
#include <quick-der/api.h> | ||
|
||
|
||
/* | ||
* Pack an Int32 and return the number of bytes. Do not pack a header | ||
* around it. The function returns the number of bytes taken, even 0 is valid. | ||
*/ | ||
dercursor der_put_int32 (uint8_t *der_buf_int32, int32_t value) { | ||
dercursor retval; | ||
int shift = 24; | ||
retval.derptr = der_buf_int32; | ||
retval.derlen = 0; | ||
while (shift >= 0) { | ||
if ((retval.derlen == 0) && (shift > 0)) { | ||
// Skip sign-extending initial bytes | ||
uint32_t neutro = (value >> (shift - 1) ) & 0x000001ff; | ||
if ((neutro == 0x000001ff) || (neutro == 0x00000000)) { | ||
shift -= 8; | ||
continue; | ||
} | ||
} | ||
der_buf_int32 [retval.derlen] = (value >> shift) & 0xff; | ||
retval.derlen++; | ||
shift -= 8; | ||
} | ||
return retval; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* Data-packing function. | ||
* | ||
* From: Rick van Rein <[email protected]> | ||
*/ | ||
|
||
|
||
#include <stdint.h> | ||
|
||
#include <quick-der/api.h> | ||
|
||
|
||
/* | ||
* Pack an UInt32 and return the number of bytes. Do not pack a header | ||
* around it. The function returns the number of bytes taken, even 0 is valid. | ||
*/ | ||
dercursor der_put_uint32 (uint8_t *der_buf_uint32, uint32_t value) { | ||
dercursor retval; | ||
int ofs = 0; | ||
if (value & 0x80000000) { | ||
*der_buf_uint32 = 0x00; | ||
ofs = 1; | ||
} | ||
retval = der_put_int32 (der_buf_uint32 + ofs, (int32_t) value); | ||
retval.derptr -= ofs; | ||
retval.derlen += ofs; | ||
return retval; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* der_putget.c -- Test der_put_[u]int32 followed by der_get[u]int32 for identity. | ||
* | ||
* From: Rick van Rein <[email protected]> | ||
*/ | ||
|
||
|
||
#include <stdlib.h> | ||
#include <stdint.h> | ||
#include <stdio.h> | ||
|
||
#include <quick-der/api.h> | ||
|
||
|
||
int unsigned_tests (void) { | ||
uint32_t tests [] = { | ||
0, 1, 255, 256, 32767, 32768, 65535, 65536, | ||
0x7fffffff, 0x80000000, 0xc0000000, 0xf0000000, 0xffffffff | ||
}; | ||
int numtests = sizeof (tests) / sizeof (tests [0]); | ||
int ok = 1; | ||
int i; | ||
for (i=0; i<numtests; i++) { | ||
der_buf_uint32_t buf; | ||
uint32_t val; | ||
dercursor crs = der_put_uint32 (buf, tests [i]); | ||
int fit = (der_get_uint32 (crs, &val) == 0); | ||
int match = (val == tests [i]); | ||
ok = ok && fit && match; | ||
if (fit != 0) { | ||
fprintf (stderr, "Unsigned integer %ul took %d bytes %02x %02x... and does not fit in 32 bits anymore\n", tests [i], crs.derlen, crs.derptr [0], crs.derptr [1]); | ||
} else if (match != 0) { | ||
fprintf (stderr, "Unsigned integer %ul took %d bytes and came back as %ul\n", tests [i], crs.derlen, val); | ||
} | ||
} | ||
return ok; | ||
} | ||
|
||
|
||
int signed_tests (void) { | ||
int32_t tests [] = { | ||
0, 1, 255, 256, 32767, 32768, 65535, 65536, | ||
-1, -255, -256, -257, -32767, -32768, -32769, | ||
0x7fffffff, 0x7fffffff, | ||
-0x7fffffff, -0x80000000, -0x40000000 | ||
}; | ||
int numtests = sizeof (tests) / sizeof (tests [0]); | ||
int ok = 1; | ||
int i; | ||
for (i=0; i<numtests; i++) { | ||
der_buf_int32_t buf; | ||
int32_t val; | ||
dercursor crs = der_put_int32 (buf, tests [i]); | ||
int fit = (der_get_int32 (crs, &val) == 0); | ||
int match = (val == tests [i]); | ||
ok = ok && fit && match; | ||
if (fit != 0) { | ||
fprintf (stderr, "Signed integer %l took %d bytes %02x %02x... and does not fit in 32 bits anymore\n", tests [i], crs.derlen, crs.derptr [0], crs.derptr [1]); | ||
} else if (match != 0) { | ||
fprintf (stderr, "Signed integer %l took %d bytes and came back as %l\n", tests [i], crs.derlen, val); | ||
} | ||
} | ||
return ok; | ||
} | ||
|
||
|
||
int main (int argc, char *argv []) { | ||
|
||
int ok = 1; | ||
|
||
ok = ok && unsigned_tests (); | ||
ok = ok && signed_tests (); | ||
|
||
exit (ok? 0: 1); | ||
|
||
} |