Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't compute headerlength when it can be tracked in ASN1 #23891

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions libr/include/r_util/r_asn1.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,7 @@ typedef struct r_asn1_object_t {
ut32 bitlength; /* Sector length in bits */
ut64 offset; /* Object offset */
RASN1List list; /* List of objects contained in the sector */
#if R2_600
ut8 headerlength; /*Header length*/
#endif
ut8 headerlength; /* Header length */
} RASN1Object;

typedef struct r_asn1_t {
Expand Down
53 changes: 0 additions & 53 deletions libr/util/asn1.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,7 @@ static RASN1Object *asn1_parse_header(const ut8 *buffer_base, const ut8 *buffer,
R_LOG_DEBUG ("Truncated object");
goto out_error;
}
#if R2_600
obj->headerlength = obj->sector - buffer;
#endif
return obj;
out_error:
free (obj);
Expand Down Expand Up @@ -336,38 +334,6 @@ static RASN1String* asn1_hexdump(RASN1Object *obj, ut32 depth, int fmtmode) {
}
return as;
}
#if R2_600
/* Remove if adding header_len to RASN1Object and adapting asn1_parse_header() */
#else
ut8 asn1_compute_header_length (ut8 klass, ut8 form, ut8 tag, ut32 content_length) {
ut8 identifier_length;
if (tag < 31) {
identifier_length = 1;
} else {
identifier_length = 1;
ut8 tag_octets = 0;
while (tag > 0) {
tag_octets++;
tag >>= 7;
}
identifier_length += tag_octets;
}
ut8 length_field_length;
if (content_length <= 127) {
length_field_length = 1;
} else {
length_field_length = 1;
ut8 length_octets = 0;
while (content_length > 0) {
length_octets++;
content_length >>= 8;
}
length_field_length += length_octets;
}
ut8 header_length = identifier_length + length_field_length;
return header_length;
}
#endif

// XXX this function signature is confusing
R_API char *r_asn1_object_tostring(RASN1Object *obj, ut32 depth, RStrBuf *sb, PJ *pj, int fmtmode) {
Expand All @@ -381,11 +347,6 @@ R_API char *r_asn1_object_tostring(RASN1Object *obj, ut32 depth, RStrBuf *sb, PJ
}
char temp_name[4096] = {0};
ut32 i;
#if R2_600
// hlen can be replaced by obj->headerlength
#else
ut8 hlen = 0;
#endif
// this shall not be freed. it's a pointer into the buffer.
RASN1String* asn1str = NULL;
const char* name = "";
Expand Down Expand Up @@ -539,12 +500,6 @@ R_API char *r_asn1_object_tostring(RASN1Object *obj, ut32 depth, RStrBuf *sb, PJ
string = asn1str->string;
}

#if R2_600
// hlen can be replaced by obj->headerlength
#else
// Compute header length
hlen = asn1_compute_header_length (obj->klass, obj->form, obj->tag, obj->length);
#endif
// Adapt size for BITSTRING
if (obj->tag == TAG_BITSTRING) {
obj->length++;
Expand Down Expand Up @@ -603,11 +558,7 @@ R_API char *r_asn1_object_tostring(RASN1Object *obj, ut32 depth, RStrBuf *sb, PJ
r_strbuf_append (sb, "└── ");
}
}
#if R2_600
r_strbuf_appendf (sb, " [@ 0x%" PFMT64x "](0x%x + 0x%x)", obj->offset, obj->headerlength, obj->length);
#else
r_strbuf_appendf (sb, " [@ 0x%" PFMT64x "](0x%x + 0x%x)", obj->offset, hlen, obj->length);
#endif
if (obj->tag == TAG_BITSTRING || obj->tag == TAG_INTEGER || obj->tag == TAG_GENERALSTRING) {
asn1_hexstring (obj, temp_name, sizeof (temp_name), depth, fmtmode);
if (strlen (temp_name) > 100) {
Expand Down Expand Up @@ -642,11 +593,7 @@ R_API char *r_asn1_object_tostring(RASN1Object *obj, ut32 depth, RStrBuf *sb, PJ
r_strbuf_appendf (sb, "%8s %4s %s %6s %5s %4s %-20s: %s", "OFFSET", "HDR", "+", "OBJ", "DEPTH", "FORM", "NAME", "VALUE\n");
}
r_strbuf_appendf (sb, "%#8" PFMT64x, obj->offset);
#if R2_600
r_strbuf_appendf (sb, " %#4x + %#6x %5d %4s %-20s: ", obj->headerlength, obj->length, depth, obj->form? "cons": "prim", name);
#else
r_strbuf_appendf (sb, " %#4x + %#6x %5d %4s %-20s: ", hlen, obj->length, depth, obj->form? "cons": "prim", name);
#endif
if (obj->tag == TAG_BITSTRING || obj->tag == TAG_INTEGER || obj->tag == TAG_GENERALSTRING) {
asn1_hexstring (obj, temp_name, sizeof (temp_name), depth, fmtmode);
if (strlen (temp_name) > 100) {
Expand Down
Loading