diff options
author | nelsonb%netscape.com <devnull@localhost> | 2001-03-20 04:23:38 +0000 |
---|---|---|
committer | nelsonb%netscape.com <devnull@localhost> | 2001-03-20 04:23:38 +0000 |
commit | 252c6ac9df28d2383a4f58c6a8780029e57c9bf2 (patch) | |
tree | a93a2e8be4a7e5e39ebfc4957bdfc6f086fa4a20 | |
parent | 24a46542e4476e45f26e02600e1d75e0edbf08cb (diff) | |
download | nss-hg-252c6ac9df28d2383a4f58c6a8780029e57c9bf2.tar.gz |
Add function SECU_PrintBuf() which prints a buffer in hex and ASCII.
-rw-r--r-- | security/nss/cmd/lib/secutil.c | 53 | ||||
-rw-r--r-- | security/nss/cmd/lib/secutil.h | 3 |
2 files changed, 56 insertions, 0 deletions
diff --git a/security/nss/cmd/lib/secutil.c b/security/nss/cmd/lib/secutil.c index a36947fb5..78a914210 100644 --- a/security/nss/cmd/lib/secutil.c +++ b/security/nss/cmd/lib/secutil.c @@ -698,6 +698,59 @@ SECU_PrintAsHex(FILE *out, SECItem *data, char *m, int level) } } +static const char *hex = "0123456789abcdef"; + +static const char printable[257] = { + "................" /* 0x */ + "................" /* 1x */ + " !\"#$%&'()*+,-./" /* 2x */ + "0123456789:;<=>?" /* 3x */ + "@ABCDEFGHIJKLMNO" /* 4x */ + "PQRSTUVWXYZ[\\]^_" /* 5x */ + "`abcdefghijklmno" /* 6x */ + "pqrstuvwxyz{|}~." /* 7x */ + "................" /* 8x */ + "................" /* 9x */ + "................" /* ax */ + "................" /* bx */ + "................" /* cx */ + "................" /* dx */ + "................" /* ex */ + "................" /* fx */ +}; + +void +SECU_PrintBuf(FILE *out, const char *msg, const void *vp, int len) +{ + const unsigned char *cp = (const unsigned char *)vp; + char buf[80]; + char *bp; + char *ap; + + fprintf(out, "%s [Len: %d]\n", msg, len); + memset(buf, ' ', sizeof buf); + bp = buf; + ap = buf + 50; + while (--len >= 0) { + unsigned char ch = *cp++; + *bp++ = hex[(ch >> 4) & 0xf]; + *bp++ = hex[ch & 0xf]; + *bp++ = ' '; + *ap++ = printable[ch]; + if (ap - buf >= 66) { + *ap = 0; + fprintf(out, " %s\n", buf); + memset(buf, ' ', sizeof buf); + bp = buf; + ap = buf + 50; + } + } + if (bp > buf) { + *ap = 0; + fprintf(out, " %s\n", buf); + } +} + void SECU_PrintInteger(FILE *out, SECItem *i, char *m, int level) { diff --git a/security/nss/cmd/lib/secutil.h b/security/nss/cmd/lib/secutil.h index 81267e592..b42d457bb 100644 --- a/security/nss/cmd/lib/secutil.h +++ b/security/nss/cmd/lib/secutil.h @@ -215,6 +215,9 @@ extern void SECU_PrintAlgorithmID(FILE *out, SECAlgorithmID *a, char *m, /* Print SECItem as hex */ extern void SECU_PrintAsHex(FILE *out, SECItem *i, char *m, int level); +/* dump a buffer in hex and ASCII */ +extern void SECU_PrintBuf(FILE *out, const char *msg, const void *vp, int len); + /* * Format and print the UTC Time "t". If the tag message "m" is not NULL, * do indent formatting based on "level" and add a newline afterward; |