From 0e0f7f0b7c6c658259f09152ff8326f05b81b271 Mon Sep 17 00:00:00 2001 From: "Eric S. Raymond" Date: Fri, 17 Jun 2011 11:37:39 -0400 Subject: Improve the code for making sensor data and command strings visible... ...and remove some const declarations that could have spelled trouble in the future. --- hex.c | 43 ++++++++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 19 deletions(-) (limited to 'hex.c') diff --git a/hex.c b/hex.c index 5c6d9a38..04eae847 100644 --- a/hex.c +++ b/hex.c @@ -3,34 +3,39 @@ * BSD terms apply: see the file COPYING in the distribution root for details. */ #include +#include #include "gpsd.h" -char /*@ observer @*/ *gpsd_hexdump(const void *binbuf, size_t binbuflen) +char /*@ observer @*/ *gpsd_hexdump(char *binbuf, size_t binbuflen) { - static char hexbuf[MAX_PACKET_LENGTH * 2 + 1]; + if (isprint(binbuf[binbuflen-1])) + return binbuf; + else { + 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; + } } int gpsd_hexpack( /*@in@*/ const char *src, /*@out@ */ char *dst, size_t len) -- cgit v1.2.1