From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([208.118.235.92]:60653) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SWofX-0001uK-5K for qemu-devel@nongnu.org; Tue, 22 May 2012 08:58:36 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1SWofV-0001ES-5z for qemu-devel@nongnu.org; Tue, 22 May 2012 08:58:30 -0400 Received: from mx1.redhat.com ([209.132.183.28]:13597) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SWofU-0001E2-Uu for qemu-devel@nongnu.org; Tue, 22 May 2012 08:58:29 -0400 From: Orit Wasserman Date: Tue, 22 May 2012 15:57:01 +0300 Message-Id: <1337691425-6022-6-git-send-email-owasserm@redhat.com> In-Reply-To: <1337691425-6022-1-git-send-email-owasserm@redhat.com> References: <1337691425-6022-1-git-send-email-owasserm@redhat.com> Subject: [Qemu-devel] [PATCH v11 5/9] Add uleb encoding/decoding functions List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: peter.maydell@linaro.org, aliguori@us.ibm.com, quintela@redhat.com, stefanha@gmail.com, mdroth@linux.vnet.ibm.com, blauwirbel@gmail.com, Orit Wasserman , chegu_vinod@hp.com, avi@redhat.com, pbonzini@redhat.com, eblake@redhat.com Implement Unsigned Little Endian Base 128. Signed-off-by: Orit Wasserman --- cutils.c | 29 +++++++++++++++++++++++++++++ qemu-common.h | 8 ++++++++ 2 files changed, 37 insertions(+), 0 deletions(-) diff --git a/cutils.c b/cutils.c index af308cd..60fb7c8 100644 --- a/cutils.c +++ b/cutils.c @@ -549,3 +549,32 @@ 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; + g_assert(!(*in & 0x80)); + *n |= *in++ << 7; + return 2; + } +} diff --git a/qemu-common.h b/qemu-common.h index 83571e0..1162b42 100644 --- a/qemu-common.h +++ b/qemu-common.h @@ -420,4 +420,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