[Redbutton-devel] SF.net SVN: redbutton: [408] redbutton-author/trunk/der_encode.c
Brought to you by:
skilvington
|
From: <ski...@us...> - 2007-09-21 16:17:24
|
Revision: 408
http://redbutton.svn.sourceforge.net/redbutton/?rev=408&view=rev
Author: skilvington
Date: 2007-09-21 09:17:21 -0700 (Fri, 21 Sep 2007)
Log Message:
-----------
use as few bytes as possible to store -ve ints
Modified Paths:
--------------
redbutton-author/trunk/der_encode.c
Modified: redbutton-author/trunk/der_encode.c
===================================================================
--- redbutton-author/trunk/der_encode.c 2007-09-21 15:52:33 UTC (rev 407)
+++ redbutton-author/trunk/der_encode.c 2007-09-21 16:17:21 UTC (rev 408)
@@ -56,15 +56,21 @@
}
else
{
+ /* work with an unsigned value */
+ uval = (unsigned int) val;
/*
- * TODO
- * should really use as few bytes as possible
+ * 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);
+ while((*len) > 1
+ && ((uval >> (((*len) - 1) * 8)) & 0xff) == 0xff
+ && ((uval >> (((*len) - 2) * 8)) & 0x80) == 0x80)
+ {
+ (*len) --;
+ }
*out = safe_malloc(*len);
/* big endian */
- uval = (unsigned int) val;
for(i=1; i<=(*len); i++)
(*out)[i - 1] = (uval >> (((*len) - i) * 8)) & 0xff;
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|