summaryrefslogtreecommitdiffstats
path: root/scan_asn1length.c
diff options
context:
space:
mode:
authorSimon Rettberg2014-03-15 01:49:50 +0100
committerSimon Rettberg2014-03-15 01:49:50 +0100
commitbedd2e7ccb1595c23e159eaa952ae1b0b5a3d2ad (patch)
treec7d1995a09f6ed0c4e6873252e957d72f5d07d07 /scan_asn1length.c
downloadldadp-bedd2e7ccb1595c23e159eaa952ae1b0b5a3d2ad.tar.gz
ldadp-bedd2e7ccb1595c23e159eaa952ae1b0b5a3d2ad.tar.xz
ldadp-bedd2e7ccb1595c23e159eaa952ae1b0b5a3d2ad.zip
Lean and mean initial commit
Not much functionality yet
Diffstat (limited to 'scan_asn1length.c')
-rw-r--r--scan_asn1length.c25
1 files changed, 25 insertions, 0 deletions
diff --git a/scan_asn1length.c b/scan_asn1length.c
new file mode 100644
index 0000000..944bd52
--- /dev/null
+++ b/scan_asn1length.c
@@ -0,0 +1,25 @@
+#include <inttypes.h>
+#include "asn1.h"
+
+size_t scan_asn1length(const char* src,const char* max,size_t* length) {
+ const char* orig=src;
+ if (src>=max) return 0;
+/* If the highest bit of the first byte is clear, the byte is the length.
+ * Otherwise the next n bytes are the length (n being the lower 7 bits) */
+ if (*src&0x80) {
+ int chars=*src&0x7f;
+ size_t l=0;
+ while (chars>0) {
+ if (++src>=max) return 0;
+ if (l>(((unsigned long)-1)>>8)) return 0; /* catch integer overflow */
+ l=l*256+(unsigned char)*src;
+ --chars;
+ }
+ *length=l;
+ } else
+ *length=*src&0x7f;
+ src++;
+ if (src+*length>max) return 0; /* catch integer overflow */
+ if ((uintptr_t)src+*length<(uintptr_t)src) return 0; /* gcc 4.1 removes this check without the cast to uintptr_t */
+ return src-orig;
+}