add two more variable length integer encoding functionsmaster
@ -0,0 +1,18 @@ | |||
.TH fmt_asn1dertag 3 | |||
.SH NAME | |||
fmt_asn1dertag \- encode unsigned integer like ASN.1 DER tag | |||
.SH SYNTAX | |||
.B #include <fmt.h> | |||
size_t \fBfmt_asn1dertag\fP(char *\fIdest\fR,unsigned long long \fIsource\fR); | |||
.SH DESCRIPTION | |||
fmt_asn1dertag encodes an unsigned integer using the ASN.1 DER for | |||
encoding tag. This takes one byte for every 7 bits in the number. | |||
If \fIdest\fR equals FMT_LEN (i.e. is NULL), fmt_asn1dertag returns the | |||
number of bytes it would have written. | |||
For convenience, fmt.h defines the integer FMT_ASN1TAG to be big | |||
enough to contain every possible fmt_asn1dertag output. | |||
.SH "SEE ALSO" | |||
scan_asn1dertag(3) |
@ -0,0 +1,15 @@ | |||
#include "fmt.h" | |||
/* write int in least amount of bytes, return number of bytes */ | |||
/* as used in ASN.1 DER tag */ | |||
size_t fmt_asn1dertag(char* dest,unsigned long long l) { | |||
/* encoding is either l%128 or (0x80+number of bytes,bytes) */ | |||
size_t n=0,i; | |||
unsigned long long t; | |||
for (t=l, n=1; t>0x7f; t>>=7) ++n; | |||
for (i=0; i<n; ++i) { | |||
if (dest) dest[n-i-1]=((i!=0)<<7) | (l&0x7f); | |||
l>>=7; | |||
} | |||
return i; | |||
} |
@ -1,20 +1,20 @@ | |||
.TH scan_asn1length 3 | |||
.TH scan_asn1derlength 3 | |||
.SH NAME | |||
scan_asn1length \- decode an unsigned integer from ASN.1 DER length encoding | |||
scan_asn1derlength \- decode an unsigned integer from ASN.1 DER length encoding | |||
.SH SYNTAX | |||
.B #include <scan.h> | |||
size_t \fBscan_asn1length\fP(const char *\fIsrc\fR,size_t \fIlen\fR,unsigned long long *\fIdest\fR); | |||
size_t \fBscan_asn1derlength\fP(const char *\fIsrc\fR,size_t \fIlen\fR,unsigned long long *\fIdest\fR); | |||
.SH DESCRIPTION | |||
scan_asn1length decodes an unsigned integer in ASN.1 DER length encoding | |||
scan_asn1derlength decodes an unsigned integer in ASN.1 DER length encoding | |||
from a memory area holding binary data. It writes the decode value in | |||
\fIdest\fR and returns the number of bytes it read from \fIsrc\fR. | |||
scan_asn1length never reads more than \fIlen\fR bytes from \fIsrc\fR. If the | |||
scan_asn1derlength never reads more than \fIlen\fR bytes from \fIsrc\fR. If the | |||
sequence is longer than that, or the memory area contains an invalid | |||
sequence, scan_asn1length returns 0 and does not touch \fIdest\fR. | |||
sequence, scan_asn1derlength returns 0 and does not touch \fIdest\fR. | |||
The length of the longest ASN.1 DER length sequence is 128 bytes. In | |||
practice the largest sequence is sizeof(*dest)+1. | |||
.SH "SEE ALSO" | |||
fmt_asn1length(3) | |||
fmt_asn1derlength(3) |
@ -0,0 +1,20 @@ | |||
.TH scan_asn1dertag 3 | |||
.SH NAME | |||
scan_asn1dertag \- decode an unsigned integer from ASN.1 DER length encoding | |||
.SH SYNTAX | |||
.B #include <scan.h> | |||
size_t \fBscan_asn1dertag\fP(const char *\fIsrc\fR,size_t \fIlen\fR,unsigned long long *\fIdest\fR); | |||
.SH DESCRIPTION | |||
scan_asn1dertag decodes an unsigned integer in ASN.1 DER tag encoding | |||
from a memory area holding binary data. It writes the decode value in | |||
\fIdest\fR and returns the number of bytes it read from \fIsrc\fR. | |||
scan_asn1dertag never reads more than \fIlen\fR bytes from \fIsrc\fR. If the | |||
sequence is longer than that, or the memory area contains an invalid | |||
sequence, scan_asn1dertag returns 0 and does not touch \fIdest\fR. | |||
The length of the longest ASN.1 DER length sequence is 128 bytes. In | |||
practice the largest sequence is sizeof(*dest)+1. | |||
.SH "SEE ALSO" | |||
fmt_asn1dertag(3) |
@ -0,0 +1,14 @@ | |||
#include "scan.h" | |||
size_t scan_asn1dertag(const char* src,size_t len,unsigned long long* length) { | |||
size_t n; | |||
unsigned long long l=0; | |||
for (n=0; n<len; ++n) { | |||
l=(l<<7) | (src[n]&0x7f); | |||
if (!(src[n]&0x80)) { | |||
*length=l; | |||
return n+1; | |||
} | |||
} | |||
return 0; | |||
} |