From patchwork Mon Jul 9 18:22:45 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Orit Wasserman X-Patchwork-Id: 169914 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 0C82F2C0201 for ; Tue, 10 Jul 2012 04:23:30 +1000 (EST) Received: from localhost ([::1]:35970 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SoIcJ-0000wp-Ql for incoming@patchwork.ozlabs.org; Mon, 09 Jul 2012 14:23:27 -0400 Received: from eggs.gnu.org ([208.118.235.92]:50816) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SoIc7-0000qG-2G for qemu-devel@nongnu.org; Mon, 09 Jul 2012 14:23:16 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1SoIc4-0005A0-Ob for qemu-devel@nongnu.org; Mon, 09 Jul 2012 14:23:14 -0400 Received: from mx1.redhat.com ([209.132.183.28]:54681) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SoIc4-00059m-GN for qemu-devel@nongnu.org; Mon, 09 Jul 2012 14:23:12 -0400 Received: from int-mx02.intmail.prod.int.phx2.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id q69IN6Vs010742 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Mon, 9 Jul 2012 14:23:06 -0400 Received: from dhcp-1-120.tlv.redhat.com (vpn-200-236.tlv.redhat.com [10.35.200.236]) by int-mx02.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id q69IMg9f011568; Mon, 9 Jul 2012 14:23:02 -0400 From: Orit Wasserman To: qemu-devel@nongnu.org Date: Mon, 9 Jul 2012 21:22:45 +0300 Message-Id: <1341858170-1903-5-git-send-email-owasserm@redhat.com> In-Reply-To: <1341858170-1903-1-git-send-email-owasserm@redhat.com> References: <1341858170-1903-1-git-send-email-owasserm@redhat.com> X-Scanned-By: MIMEDefang 2.67 on 10.5.11.12 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 209.132.183.28 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 Subject: [Qemu-devel] [PATCH v16 4/9] Add uleb encoding/decoding functions X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Implement Unsigned Little Endian Base 128. Signed-off-by: Orit Wasserman --- cutils.c | 33 +++++++++++++++++++++++++++++++++ qemu-common.h | 8 ++++++++ 2 files changed, 41 insertions(+), 0 deletions(-) diff --git a/cutils.c b/cutils.c index 02d5bd0..7d99fe0 100644 --- a/cutils.c +++ b/cutils.c @@ -557,3 +557,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 a6987d7..ae2b850 100644 --- a/qemu-common.h +++ b/qemu-common.h @@ -432,4 +432,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