From 7f5851d906449063c6cc653e7967549e861a2fea Mon Sep 17 00:00:00 2001 From: "Eric S. Raymond" Date: Wed, 20 Jun 2012 09:00:33 -0400 Subject: Split gpsd_hexdump() so AIS dumping will be invertible. --- hex.c | 47 ++++++++++++++++++++++++++--------------------- 1 file changed, 26 insertions(+), 21 deletions(-) (limited to 'hex.c') diff --git a/hex.c b/hex.c index ef67f06b..c4130a66 100644 --- a/hex.c +++ b/hex.c @@ -8,7 +8,7 @@ #include "gpsd.h" -const char /*@ observer @*/ *gpsd_hexdump(char *binbuf, size_t binbuflen) +const char /*@ observer @*/ *gpsd_packetdump(char *binbuf, size_t binbuflen) { char *cp; bool printable = true; @@ -19,31 +19,36 @@ const char /*@ observer @*/ *gpsd_hexdump(char *binbuf, size_t binbuflen) printable = false; if (printable) return binbuf; - else { - static char hexbuf[MAX_PACKET_LENGTH * 2 + 1]; + else + return gpsd_hexdump(binbuf, binbuflen); +} + +const char /*@ observer @*/ *gpsd_hexdump(char *binbuf, size_t binbuflen) +{ + /* FIXME: this isn't thead-safe! */ + static char hexbuf[MAX_PACKET_LENGTH * 2 + 1]; #ifndef SQUELCH_ENABLE - size_t i, j = 0; - size_t len = - (size_t) ((binbuflen > - MAX_PACKET_LENGTH) ? MAX_PACKET_LENGTH : binbuflen); - const char *ibuf = (const char *)binbuf; - const char *hexchar = "0123456789abcdef"; + size_t i, j = 0; + size_t len = + (size_t) ((binbuflen > + MAX_PACKET_LENGTH) ? MAX_PACKET_LENGTH : binbuflen); + const char *ibuf = (const char *)binbuf; + const char *hexchar = "0123456789abcdef"; - if (NULL == binbuf || 0 == binbuflen) - return ""; + if (NULL == binbuf || 0 == binbuflen) + return ""; - /*@ -shiftimplementation @*/ - for (i = 0; i < len; i++) { - hexbuf[j++] = hexchar[(ibuf[i] & 0xf0) >> 4]; - hexbuf[j++] = hexchar[ibuf[i] & 0x0f]; - } - /*@ +shiftimplementation @*/ - hexbuf[j] = '\0'; + /*@ -shiftimplementation @*/ + for (i = 0; i < len; i++) { + hexbuf[j++] = hexchar[(ibuf[i] & 0xf0) >> 4]; + hexbuf[j++] = hexchar[ibuf[i] & 0x0f]; + } + /*@ +shiftimplementation @*/ + hexbuf[j] = '\0'; #else /* SQUELCH defined */ - hexbuf[0] = '\0'; + hexbuf[0] = '\0'; #endif /* SQUELCH_ENABLE */ - return hexbuf; - } + return hexbuf; } /*@ +charint -shiftimplementation @*/ -- cgit v1.2.1