[Redbutton-devel] SF.net SVN: redbutton: [398] redbutton-author/trunk/der_encode.c
Brought to you by:
skilvington
|
From: <ski...@us...> - 2007-09-18 16:42:53
|
Revision: 398
http://redbutton.svn.sourceforge.net/redbutton/?rev=398&view=rev
Author: skilvington
Date: 2007-09-18 09:42:05 -0700 (Tue, 18 Sep 2007)
Log Message:
-----------
DER encode -ve INTEGERs the lazy way
Modified Paths:
--------------
redbutton-author/trunk/der_encode.c
Modified: redbutton-author/trunk/der_encode.c
===================================================================
--- redbutton-author/trunk/der_encode.c 2007-09-18 16:29:48 UTC (rev 397)
+++ redbutton-author/trunk/der_encode.c 2007-09-18 16:42:05 UTC (rev 398)
@@ -28,20 +28,22 @@
{
unsigned int shifted;
unsigned int i;
+ unsigned int uval;
/* assert */
if(*out != NULL || *len != 0)
fatal("der_encode_INTEGER: length already %u", *len);
- /*
- * work out how many bytes we need to store 'val'
- * ints in DER are signed, so first byte we store must be <= 127
- * we add a leading 0 if first byte is > 127
- */
+ /* is it +ve or -ve */
if(val >= 0)
{
+ /*
+ * work out how many bytes we need to store 'val'
+ * ints in DER are signed, so first byte we store must be <= 127
+ * we add a leading 0 if first byte is > 127
+ */
shifted = val;
- (*len) = 1;
+ *len = 1;
while(shifted > 127)
{
(*len) ++;
@@ -54,7 +56,17 @@
}
else
{
-fatal("TODO: negative INTEGER: %d", val);
+ /*
+ * TODO
+ * should really use as few bytes as possible
+ * ie chop off leading 0xff's while the next byte has its top bit set
+ */
+ *len = sizeof(unsigned int);
+ *out = safe_malloc(*len);
+ /* big endian */
+ uval = (unsigned int) val;
+ for(i=1; i<=(*len); i++)
+ (*out)[i - 1] = (uval >> (((*len) - i) * 8)) & 0xff;
}
return;
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|