From patchwork Fri Jul 13 07:23:38 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Juan Quintela X-Patchwork-Id: 170812 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 A8C962C0353 for ; Fri, 13 Jul 2012 17:57:28 +1000 (EST) Received: from localhost ([::1]:54930 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SpaFS-0001Le-CL for incoming@patchwork.ozlabs.org; Fri, 13 Jul 2012 03:25:10 -0400 Received: from eggs.gnu.org ([208.118.235.92]:45840) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SpaEW-0008Ci-Uz for qemu-devel@nongnu.org; Fri, 13 Jul 2012 03:24:18 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1SpaES-0004RH-IL for qemu-devel@nongnu.org; Fri, 13 Jul 2012 03:24:12 -0400 Received: from mx1.redhat.com ([209.132.183.28]:1884) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SpaES-0004R7-9q for qemu-devel@nongnu.org; Fri, 13 Jul 2012 03:24:08 -0400 Received: from int-mx01.intmail.prod.int.phx2.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id q6D7O72e017713 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Fri, 13 Jul 2012 03:24:07 -0400 Received: from trasno.mitica (ovpn-116-28.ams2.redhat.com [10.36.116.28]) by int-mx01.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id q6D7NjR6014276; Fri, 13 Jul 2012 03:24:06 -0400 From: Juan Quintela To: qemu-devel@nongnu.org Date: Fri, 13 Jul 2012 09:23:38 +0200 Message-Id: <1342164224-32709-17-git-send-email-quintela@redhat.com> In-Reply-To: <1342164224-32709-1-git-send-email-quintela@redhat.com> References: <1342164224-32709-1-git-send-email-quintela@redhat.com> X-Scanned-By: MIMEDefang 2.67 on 10.5.11.11 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 209.132.183.28 Cc: Orit Wasserman Subject: [Qemu-devel] [PATCH 16/22] 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 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 b0bdd4b..700f943 100644 --- a/cutils.c +++ b/cutils.c @@ -384,3 +384,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 195bab5..3188bdd 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