Skip to content

Commit

Permalink
Tested and fixed der_prepack (hitherto underused functionality)
Browse files Browse the repository at this point in the history
  • Loading branch information
vanrein committed Sep 16, 2016
1 parent 7ff61ac commit f8eb6a9
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
2 changes: 2 additions & 0 deletions lib/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ CFLAGS += -std=gnu11

CC ?= gcc
AR ?= ar

PREFIX ?= /usr/local
DESTPATH=$(abspath $(DESTDIR)/$(PREFIX))

all: $(TARGETS)
Expand Down
20 changes: 10 additions & 10 deletions lib/der_pack.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,23 @@ static size_t der_pack_prepack (const derprep *derp, uint8_t **bufend) {
size_t totlen = 0;
size_t elmlen;
size_t cnt = derp->derlen_msb & ~DER_DERLEN_FLAG_CONSTRUCTED;
dercursor *crs = derp->derray;
dercursor *crs = derp->derray + cnt;
uint8_t *buf;
while (cnt-- > 0) {
while (crs--, cnt-- > 0) {
if (crs->derlen & DER_DERLEN_FLAG_CONSTRUCTED) {
elmlen = der_pack_prepack ((const derprep *) crs+cnt, bufend);
elmlen = der_pack_prepack ((const derprep *) crs, bufend);
if (elmlen == DER_DERLEN_ERROR) {
return DER_DERLEN_ERROR;
}
} else {
elmlen = crs->derlen;
if (bufend) {
buf = *bufend;
buf -= elmlen;
memcpy (buf, crs [cnt].derptr, elmlen);
memcpy (buf, crs->derptr, elmlen);
DPRINTF ("DEBUG: Wrote %4d bytes to 0x%016llx\n", elmlen, buf);
*bufend = buf;
}
elmlen = crs->derlen;
}
totlen += elmlen;
if ((totlen | elmlen) & DER_DERLEN_FLAG_CONSTRUCTED) {
Expand Down Expand Up @@ -111,7 +111,7 @@ DPRINTF ("DEBUG: Updated offset to %zd, pointer is 0x%016llx\n", *offsetp, derne
elmlen = 0;
addhdr = 0;
DPRINTF ("DEBUG: Consumed NULL entry at %zd, so elmlen set to 0\n", *offsetp);
} else if (derray->derlen & DER_DERLEN_FLAG_CONSTRUCTED) {
} else if (dernext->derlen & DER_DERLEN_FLAG_CONSTRUCTED) {
// Prepacked Constructed, non-NULL entry
elmlen = der_pack_prepack ((const derprep *) dernext, bufend);
if (elmlen == DER_DERLEN_ERROR) {
Expand All @@ -128,12 +128,12 @@ DPRINTF ("DEBUG: Fetched length from constructed, recursive element\n");
DPRINTF ("DEBUG: Wrote %4d bytes to 0x%016llx\n", elmlen, buf);
*bufend = buf;
}
if ((tag == 0x08) || (tag == 0x0b)
|| (tag == 0x10) || (tag == 0x11)) {
tag |= 0x20; // Constructed, even STORED
}
DPRINTF ("DEBUG: Fetched length from primitive element\n");
}
if ((tag == 0x08) || (tag == 0x0b)
|| (tag == 0x10) || (tag == 0x11)) {
tag |= 0x20; // Constructed, even STORED
}
DPRINTF ("DEBUG: Stored element length set to %zd at offset %zd\n", elmlen, *offsetp);
}
if (addhdr) {
Expand Down
5 changes: 4 additions & 1 deletion lib/der_prepack.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@
* manner, but it _does_ prepare it for der_pack() in a way that will include
* the array of dercursor as a consecutive sequence, without any additional
* headers, markers or other frills.
*
* This routine sets a special marker bit DER_DERLEN_FLAG_CONSTRUCTED in the
* length field, so it can be recognised later on as an array reference.
*/
void der_prepack (dercursor *derray, size_t arraycount, derarray *target) {
target->derray = derray;
target->dercnt = arraycount;
target->dercnt = arraycount | DER_DERLEN_FLAG_CONSTRUCTED;
}

0 comments on commit f8eb6a9

Please sign in to comment.