lib/util: Add hex_encode_buf
authorVolker Lendecke <vl@samba.org>
Fri, 3 Feb 2012 02:27:19 +0000 (03:27 +0100)
committerVolker Lendecke <vl@samba.org>
Mon, 6 Feb 2012 08:15:32 +0000 (09:15 +0100)
Autobuild-User: Volker Lendecke <vl@samba.org>
Autobuild-Date: Mon Feb  6 09:15:33 CET 2012 on sn-devel-104

lib/util/samba_util.h
lib/util/util.c
source3/selftest/tests.py
source3/torture/torture.c

index 9df3ddfcd0eba66fc779f765e606b56ce54117d6..5ce2e5e53528719580efa4bbea11924fc20d797a 100644 (file)
@@ -261,6 +261,11 @@ _PUBLIC_ size_t strhex_to_str(char *p, size_t p_len, const char *strhex, size_t
  */
 _PUBLIC_ _PURE_ DATA_BLOB strhex_to_data_blob(TALLOC_CTX *mem_ctx, const char *strhex) ;
 
+/**
+ * Print a buf in hex. Assumes dst is at least (srclen*2)+1 large.
+ */
+_PUBLIC_ void hex_encode_buf(char *dst, const uint8_t *src, size_t srclen);
+
 /**
  * Routine to print a buffer as HEX digits, into an allocated string.
  */
index c4fbd0b39fcab3ff864fa630ab8d71d407051c56..867da0a3405cc4cd8fa2522595d8e06da8552eef 100644 (file)
@@ -758,20 +758,31 @@ _PUBLIC_ _PURE_ DATA_BLOB strhex_to_data_blob(TALLOC_CTX *mem_ctx, const char *s
        return ret_blob;
 }
 
+/**
+ * Print a buf in hex. Assumes dst is at least (srclen*2)+1 large.
+ */
+_PUBLIC_ void hex_encode_buf(char *dst, const uint8_t *src, size_t srclen)
+{
+       size_t i;
+       for (i=0; i<srclen; i++) {
+               snprintf(dst + i*2, 3, "%02X", src[i]);
+       }
+       /*
+        * Ensure 0-termination for 0-length buffers
+        */
+       dst[srclen*2] = '\0';
+}
 
 /**
  * Routine to print a buffer as HEX digits, into an allocated string.
  */
 _PUBLIC_ void hex_encode(const unsigned char *buff_in, size_t len, char **out_hex_buffer)
 {
-       int i;
        char *hex_buffer;
 
        *out_hex_buffer = malloc_array_p(char, (len*2)+1);
        hex_buffer = *out_hex_buffer;
-
-       for (i = 0; i < len; i++)
-               slprintf(&hex_buffer[i*2], 3, "%02X", buff_in[i]);
+       hex_encode_buf(hex_buffer, buff_in, len);
 }
 
 /**
@@ -779,17 +790,13 @@ _PUBLIC_ void hex_encode(const unsigned char *buff_in, size_t len, char **out_he
  */
 _PUBLIC_ char *hex_encode_talloc(TALLOC_CTX *mem_ctx, const unsigned char *buff_in, size_t len)
 {
-       int i;
        char *hex_buffer;
 
        hex_buffer = talloc_array(mem_ctx, char, (len*2)+1);
        if (!hex_buffer) {
                return NULL;
        }
-
-       for (i = 0; i < len; i++)
-               slprintf(&hex_buffer[i*2], 3, "%02X", buff_in[i]);
-
+       hex_encode_buf(hex_buffer, buff_in, len);
        talloc_set_name_const(hex_buffer, hex_buffer);
        return hex_buffer;
 }
index 6a3ccb9877d15f4f016a9e4019ef86b91a0acb12..ce6d964703f196bafb19268a16f8a0ac8907fe8a 100755 (executable)
@@ -107,6 +107,7 @@ local_tests=[
        "LOCAL-TEVENT-SELECT",
        "LOCAL-CONVERT-STRING",
        "LOCAL-CONV-AUTH-INFO",
+       "LOCAL-hex_encode_buf",
        "LOCAL-sprintf_append"]
 
 for t in local_tests:
index e4ce91338ff0bdc682fe8e528c11f0941904a639..b14f9ed873711da084ab73ce4aa06454a347ff16 100644 (file)
@@ -8693,6 +8693,26 @@ fail:
        return result;
 }
 
+static bool run_local_hex_encode_buf(int dummy)
+{
+       char buf[17];
+       uint8_t src[8];
+       int i;
+
+       for (i=0; i<sizeof(src); i++) {
+               src[i] = i;
+       }
+       hex_encode_buf(buf, src, sizeof(src));
+       if (strcmp(buf, "0001020304050607") != 0) {
+               return false;
+       }
+       hex_encode_buf(buf, NULL, 0);
+       if (buf[0] != '\0') {
+               return false;
+       }
+       return true;
+}
+
 static double create_procs(bool (*fn)(int), bool *result)
 {
        int i, status;
@@ -8895,6 +8915,7 @@ static struct {
        { "LOCAL-CONVERT-STRING", run_local_convert_string, 0},
        { "LOCAL-CONV-AUTH-INFO", run_local_conv_auth_info, 0},
        { "LOCAL-sprintf_append", run_local_sprintf_append, 0},
+       { "LOCAL-hex_encode_buf", run_local_hex_encode_buf, 0},
        {NULL, NULL, 0}};