New memory-tag commands
[deliverable/binutils-gdb.git] / gdbsupport / common-utils.cc
index ed3348057c84d00c9177dfbeb956bd45a336b27a..8ce839a54ae72217d35b0965f72b38393dcb4bf2 100644 (file)
@@ -392,3 +392,52 @@ align_down (ULONGEST v, int n)
   gdb_assert (n && (n & (n-1)) == 0);
   return (v & -n);
 }
+
+/* See gdbsupport/common-utils.h.  */
+
+int
+fromhex (int a)
+{
+  if (a >= '0' && a <= '9')
+    return a - '0';
+  else if (a >= 'a' && a <= 'f')
+    return a - 'a' + 10;
+  else if (a >= 'A' && a <= 'F')
+    return a - 'A' + 10;
+  else
+    error (_("Invalid hex digit %d"), a);
+}
+
+/* See gdbsupport/common-utils.h.  */
+
+int
+hex2bin (const char *hex, gdb_byte *bin, int count)
+{
+  int i;
+
+  for (i = 0; i < count; i++)
+    {
+      if (hex[0] == 0 || hex[1] == 0)
+       {
+         /* Hex string is short, or of uneven length.
+            Return the count that has been converted so far.  */
+         return i;
+       }
+      *bin++ = fromhex (hex[0]) * 16 + fromhex (hex[1]);
+      hex += 2;
+    }
+  return i;
+}
+
+/* See gdbsupport/common-utils.h.  */
+
+gdb::byte_vector
+hex2bin (const char *hex)
+{
+  size_t bin_len = strlen (hex) / 2;
+  gdb::byte_vector bin (bin_len);
+
+  hex2bin (hex, bin.data (), bin_len);
+
+  return bin;
+}
This page took 0.023379 seconds and 4 git commands to generate.