[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PATCH v13 05/13] Add uleb encoding/decoding functions
From: |
Orit Wasserman |
Subject: |
[Qemu-devel] [PATCH v13 05/13] Add uleb encoding/decoding functions |
Date: |
Wed, 27 Jun 2012 13:34:13 +0300 |
Implement Unsigned Little Endian Base 128.
Signed-off-by: Orit Wasserman <address@hidden>
---
cutils.c | 32 ++++++++++++++++++++++++++++++++
qemu-common.h | 8 ++++++++
2 files changed, 40 insertions(+), 0 deletions(-)
diff --git a/cutils.c b/cutils.c
index af308cd..3f81d53 100644
--- a/cutils.c
+++ b/cutils.c
@@ -549,3 +549,35 @@ int qemu_sendv(int sockfd, struct iovec *iov, int len, int
iov_offset)
return do_sendv_recvv(sockfd, iov, len, iov_offset, 1);
}
+/*
+ * Implementation of ULEB128 (http://en.wikipedia.org/wiki/LEB128)
+ * Input is limited to 14-bit numbers
+ */
+int uleb128_encode_small(uint8_t *out, uint32_t n)
+{
+ g_assert(n <= 0x3fff);
+ if (n < 0x80) {
+ *out++ = n;
+ return 1;
+ } else {
+ *out++ = (n & 0x7f) | 0x80;
+ *out++ = n >> 7;
+ return 2;
+ }
+}
+
+int uleb128_decode_small(const uint8_t *in, uint32_t *n)
+{
+ if (!(*in & 0x80)) {
+ *n = *in++;
+ return 1;
+ } else {
+ *n = *in++ & 0x7f;
+ /* we exceed 14 bit number */
+ if (*in & 0x80) {
+ return -1;
+ }
+ *n |= *in++ << 7;
+ return 2;
+ }
+}
diff --git a/qemu-common.h b/qemu-common.h
index 45b1d97..f7875ae 100644
--- a/qemu-common.h
+++ b/qemu-common.h
@@ -429,4 +429,12 @@ static inline bool is_power_of_2(int64_t value)
#include "module.h"
+/*
+ * Implementation of ULEB128 (http://en.wikipedia.org/wiki/LEB128)
+ * Input is limited to 14-bit numbers
+ */
+
+int uleb128_encode_small(uint8_t *out, uint32_t n);
+int uleb128_decode_small(const uint8_t *in, uint32_t *n);
+
#endif
--
1.7.7.6
- Re: [Qemu-devel] [PATCH v13 02/13] Add migration capabilites, (continued)
- [Qemu-devel] [PATCH v13 06/13] Add save_block_hdr function, Orit Wasserman, 2012/06/27
- [Qemu-devel] [PATCH v13 05/13] Add uleb encoding/decoding functions,
Orit Wasserman <=
- [Qemu-devel] [PATCH v13 07/13] Add debugging infrastructure, Orit Wasserman, 2012/06/27
- [Qemu-devel] [PATCH v13 08/13] Change ram_save_block to return -1 if there are no more changes, Orit Wasserman, 2012/06/27
- [Qemu-devel] [PATCH v13 11/13] Add XBZRLE to ram_save_block and ram_save_live, Orit Wasserman, 2012/06/27
- [Qemu-devel] [PATCH v13 10/13] Add xbzrle_encode_buffer and xbzrle_decode_buffer functions, Orit Wasserman, 2012/06/27