]> xenbits.xensource.com Git - libvirt.git/commitdiff
util: Introduce virReadBufInt16LE and virReadBufInt16BE
authorJohn Ferlan <jferlan@redhat.com>
Tue, 21 Jun 2016 16:57:30 +0000 (12:57 -0400)
committerJohn Ferlan <jferlan@redhat.com>
Fri, 24 Jun 2016 17:23:02 +0000 (13:23 -0400)
In order to read 16 bits of data in the native format and convert add
the 16 bit macros to match existing 32 and 64 bit code.

Signed-off-by: John Ferlan <jferlan@redhat.com>
src/util/virendian.h
tests/virendiantest.c

index eefe48ca6f1f92392948f29b45cc778855ec95ef..97940bd33a52ba280da0b643a21800bc1d5e2f5e 100644 (file)
      ((uint32_t)(uint8_t)((buf)[2]) << 16) |             \
      ((uint32_t)(uint8_t)((buf)[3]) << 24))
 
+/**
+ * virReadBufInt16BE:
+ * @buf: byte to start reading at (can be 'char*' or 'unsigned char*');
+ *       evaluating buf must not have any side effects
+ *
+ * Read 2 bytes at BUF as a big-endian 16-bit number.  Caller is
+ * responsible to avoid reading beyond array bounds.
+ */
+# define virReadBufInt16BE(buf)                          \
+    (((uint16_t)(uint8_t)((buf)[0]) << 8) |              \
+     (uint16_t)(uint8_t)((buf)[1]))
+
+/**
+ * virReadBufInt16LE:
+ * @buf: byte to start reading at (can be 'char*' or 'unsigned char*');
+ *       evaluating buf must not have any side effects
+ *
+ * Read 2 bytes at BUF as a little-endian 16-bit number.  Caller is
+ * responsible to avoid reading beyond array bounds.
+ */
+# define virReadBufInt16LE(buf)                          \
+    ((uint16_t)(uint8_t)((buf)[0]) |                     \
+     ((uint16_t)(uint8_t)((buf)[1]) << 8))
+
 #endif /* __VIR_ENDIAN_H__ */
index 407250710e4819200d572f4004d59cc6fc0fc3d1..f858e5cb754341b38756a61e3447810043e53e9c 100644 (file)
@@ -50,6 +50,15 @@ test1(const void *data ATTRIBUTE_UNUSED)
     if (virReadBufInt32LE(array + 9) != 0x8d8c8b8aU)
         goto cleanup;
 
+    if (virReadBufInt16BE(array) != 0x0102U)
+        goto cleanup;
+    if (virReadBufInt16BE(array + 11) != 0x8c8dU)
+        goto cleanup;
+    if (virReadBufInt16LE(array) != 0x0201U)
+        goto cleanup;
+    if (virReadBufInt16LE(array + 11) != 0x8d8cU)
+        goto cleanup;
+
     ret = 0;
  cleanup:
     return ret;
@@ -81,6 +90,15 @@ test2(const void *data ATTRIBUTE_UNUSED)
     if (virReadBufInt32LE(array + 9) != 0x8d8c8b8aU)
         goto cleanup;
 
+    if (virReadBufInt16BE(array) != 0x0102U)
+        goto cleanup;
+    if (virReadBufInt16BE(array + 11) != 0x8c8dU)
+        goto cleanup;
+    if (virReadBufInt16LE(array) != 0x0201U)
+        goto cleanup;
+    if (virReadBufInt16LE(array + 11) != 0x8d8cU)
+        goto cleanup;
+
     ret = 0;
  cleanup:
     return ret;