<p>Neels Hofmeyr has uploaded this change for <strong>review</strong>.</p><p><a href="https://gerrit.osmocom.org/12671">View Change</a></p><pre style="font-family: monospace,monospace; white-space: pre-wrap;">add osmo_hexdump_b(), osmo_hexdump_nospc_b()<br><br>Add osmo_hexdump_b() and osmo_hexdump_nospc_b() to use a second static buffer,<br>allowing more than one hexdump per printf()-like call.<br><br>Rationale: recently during patch review, a situation came up where two hexdumps<br>in a single printf would have been useful. Now I've faced a similar situation<br>again, in ongoing development. So I decided it is time to provide this API.<br><br>Naming: before, I named functions that use a secondary string buffer with a '2'<br>suffix, like osmo_plmn_name() and osmo_plmn_name2(). This time, I decided to<br>use a '_b' suffix, osmo_hexdump_b(), instead. The reason is, by now I think<br>that '2' is a bad choice for secondary-buffer functions: the '2' suffix is<br>already used for introducing a newer API version of function signatures. If we,<br>for example, introduce an osmo_hexdump() that has no final delimiter after the<br>last byte, that would qualify for osmo_hexdump2(); and that would confuse with<br>the implementation simply using a secondary buffer. Sometimes during code<br>review, I assume that the existence of a foo2() function means the patch<br>submitter should use foo2() instead of foo(), and am annoyed by my previous<br>choice of overloading the '2' suffix with a secondary meaning besides "this is<br>newer API".<br><br>Change-Id: Ia48fc6b984f06b972a6846b173b8cf8d30737460<br>---<br>M include/osmocom/core/utils.h<br>M src/utils.c<br>2 files changed, 25 insertions(+), 0 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;">git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/71/12671/1</pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/include/osmocom/core/utils.h b/include/osmocom/core/utils.h</span><br><span>index ff16cf4..ffe6cdd 100644</span><br><span>--- a/include/osmocom/core/utils.h</span><br><span>+++ b/include/osmocom/core/utils.h</span><br><span>@@ -55,7 +55,9 @@</span><br><span> </span><br><span> char *osmo_ubit_dump(const uint8_t *bits, unsigned int len);</span><br><span> char *osmo_hexdump(const unsigned char *buf, int len);</span><br><span style="color: hsl(120, 100%, 40%);">+char *osmo_hexdump_b(const unsigned char *buf, int len);</span><br><span> char *osmo_hexdump_nospc(const unsigned char *buf, int len);</span><br><span style="color: hsl(120, 100%, 40%);">+char *osmo_hexdump_nospc_b(const unsigned char *buf, int len);</span><br><span> char *osmo_hexdump_buf(char *out_buf, size_t out_buf_size, const unsigned char *buf, int len, const char *delim,</span><br><span>                    bool delim_after_last);</span><br><span> </span><br><span>diff --git a/src/utils.c b/src/utils.c</span><br><span>index c21b4ad..06c2f8a 100644</span><br><span>--- a/src/utils.c</span><br><span>+++ b/src/utils.c</span><br><span>@@ -217,6 +217,7 @@</span><br><span> }</span><br><span> </span><br><span> static char hexd_buff[4096];</span><br><span style="color: hsl(120, 100%, 40%);">+static char hexd_buff2[4096];</span><br><span> static const char hex_chars[] = "0123456789abcdef";</span><br><span> </span><br><span> /*! Convert binary sequence to hexadecimal ASCII string.</span><br><span>@@ -320,6 +321,17 @@</span><br><span>    return osmo_hexdump_buf(hexd_buff, sizeof(hexd_buff), buf, len, " ", true);</span><br><span> }</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+/*! Same as osmo_hexdump() but use a separate static buffer.</span><br><span style="color: hsl(120, 100%, 40%);">+ * Allows passing a second osmo_hexdump() as print format argument.</span><br><span style="color: hsl(120, 100%, 40%);">+ *  \param[in] buf pointer to sequence of bytes.</span><br><span style="color: hsl(120, 100%, 40%);">+ *  \param[in] len length of buf in number of bytes.</span><br><span style="color: hsl(120, 100%, 40%);">+ *  \returns pointer to zero-terminated string.</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+char *osmo_hexdump_b(const unsigned char *buf, int len)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+   return osmo_hexdump_buf(hexd_buff2, sizeof(hexd_buff2), buf, len, " ", true);</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> /*! Convert binary sequence to hexadecimal ASCII string</span><br><span>  *  \param[in] buf pointer to sequence of bytes</span><br><span>  *  \param[in] len length of buf in number of bytes</span><br><span>@@ -336,6 +348,17 @@</span><br><span>     return osmo_hexdump_buf(hexd_buff, sizeof(hexd_buff), buf, len, "", true);</span><br><span> }</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+/*! Same as osmo_hexdump_nospc() but use a separate static buffer.</span><br><span style="color: hsl(120, 100%, 40%);">+ * Allows passing a second osmo_hexdump() as print format argument.</span><br><span style="color: hsl(120, 100%, 40%);">+ *  \param[in] buf pointer to sequence of bytes</span><br><span style="color: hsl(120, 100%, 40%);">+ *  \param[in] len length of buf in number of bytes</span><br><span style="color: hsl(120, 100%, 40%);">+ *  \returns pointer to zero-terminated string</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+char *osmo_hexdump_nospc_b(const unsigned char *buf, int len)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+   return osmo_hexdump_buf(hexd_buff2, sizeof(hexd_buff2), buf, len, "", true);</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> /* Compat with previous typo to preserve abi */</span><br><span> char *osmo_osmo_hexdump_nospc(const unsigned char *buf, int len)</span><br><span> #if defined(__MACH__) && defined(__APPLE__)</span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.osmocom.org/12671">change 12671</a>. To unsubscribe, or for help writing mail filters, visit <a href="https://gerrit.osmocom.org/settings">settings</a>.</p><div itemscope itemtype="http://schema.org/EmailMessage"><div itemscope itemprop="action" itemtype="http://schema.org/ViewAction"><link itemprop="url" href="https://gerrit.osmocom.org/12671"/><meta itemprop="name" content="View Change"/></div></div>

<div style="display:none"> Gerrit-Project: libosmocore </div>
<div style="display:none"> Gerrit-Branch: master </div>
<div style="display:none"> Gerrit-MessageType: newchange </div>
<div style="display:none"> Gerrit-Change-Id: Ia48fc6b984f06b972a6846b173b8cf8d30737460 </div>
<div style="display:none"> Gerrit-Change-Number: 12671 </div>
<div style="display:none"> Gerrit-PatchSet: 1 </div>
<div style="display:none"> Gerrit-Owner: Neels Hofmeyr <nhofmeyr@sysmocom.de> </div>