Received: from eggs.gnu.org ([208.118.235.92]:52104) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SwdNQ-0002aR-1j for qemu-devel@nongnu.org; Wed, 01 Aug 2012 14:10:33 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1SwdNL-0007iy-3T for qemu-devel@nongnu.org; Wed, 01 Aug 2012 14:10:31 -0400 Received: from mx1.redhat.com ([209.132.183.28]:29232) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SwdNK-0007ip-Rc for qemu-devel@nongnu.org; Wed, 01 Aug 2012 14:10:27 -0400 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id q71IA7pS018408 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Wed, 1 Aug 2012 14:10:26 -0400 From: Juan Quintela Date: Wed, 1 Aug 2012 20:01:13 +0200 Message-Id: <1343844079-2949-6-git-send-email-quintela@redhat.com> In-Reply-To: <1343844079-2949-1-git-send-email-quintela@redhat.com> References: <1343844079-2949-1-git-send-email-quintela@redhat.com> Subject: [Qemu-devel] [PATCH 05/11] Add uleb encoding/decoding functions List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: Orit Wasserman From: Orit Wasserman Implement Unsigned Little Endian Base 128. Signed-off-by: Orit Wasserman Signed-off-by: Juan Quintela --- cutils.c | 33 +++++++++++++++++++++++++++++++++ qemu-common.h | 8 ++++++++ 2 files changed, 41 insertions(+) diff --git a/cutils.c b/cutils.c index 35e2e2b..ee4614d 100644 --- a/cutils.c +++ b/cutils.c @@ -391,3 +391,36 @@ int64_t pow2floor(int64_t value) } return value; } + +/* + * 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 432545b..b906b21 100644 --- a/qemu-common.h +++ b/qemu-common.h @@ -426,4 +426,12 @@ int64_t pow2floor(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.11.2