gpsd-dev
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[gpsd-dev] [PATCH 2/5] gpsd_hexdump: Always dump in hex mode


From: chris
Subject: [gpsd-dev] [PATCH 2/5] gpsd_hexdump: Always dump in hex mode
Date: Tue, 5 Jun 2012 17:26:21 +0100

From: Christian Gagneraud <address@hidden>

This function doesn't always dump the data in hex form.
The problem is that some users on it to always do hex dumps.
For example, the AIS json code use it to dump undecoded data. Depending on the
data it will have either text dump or hex dump, which breaks the json
idempotency.

* This patch doesn't bring any regression. *
scons check and splint passed

Signed-off-by: Christian Gagneraud <address@hidden>
---
 hex.c |   47 ++++++++++++++++++-----------------------------
 1 files changed, 18 insertions(+), 29 deletions(-)

diff --git a/hex.c b/hex.c
index ef67f06..0171f7b 100644
--- a/hex.c
+++ b/hex.c
@@ -10,40 +10,29 @@
 
 const char /*@ observer @*/ *gpsd_hexdump(char *binbuf, size_t binbuflen)
 {
-    char *cp;
-    bool printable = true;
-
-    assert(binbuf != NULL);
-    for (cp = binbuf; cp < binbuf + binbuflen; cp++)
-       if (!isprint(*cp) && !isspace(*cp))
-           printable = false;
-    if (printable)
-       return binbuf;
-    else {
-       static char hexbuf[MAX_PACKET_LENGTH * 2 + 1];
+    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 @*/
-- 
1.7.7




reply via email to

[Prev in Thread] Current Thread [Next in Thread]