From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 25927 invoked by alias); 15 May 2012 05:02:59 -0000 Received: (qmail 25802 invoked by uid 22791); 15 May 2012 05:02:56 -0000 X-SWARE-Spam-Status: No, hits=-4.1 required=5.0 tests=AWL,BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,KHOP_RCVD_TRUST,RCVD_IN_DNSWL_LOW,RCVD_IN_HOSTKARMA_YE,T_RP_MATCHES_RCVD X-Spam-Check-By: sourceware.org Received: from mail-vb0-f73.google.com (HELO mail-vb0-f73.google.com) (209.85.212.73) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Tue, 15 May 2012 05:02:30 +0000 Received: by vbbfd1 with SMTP id fd1so742370vbb.0 for ; Mon, 14 May 2012 22:02:29 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=to:cc:subject:message-id:date:from:x-gm-message-state; bh=pUJM+4y/kZIxgrOksVhgpElsg8evEctJkAgOAXVqJ70=; b=FMArNpyp8HOUSMJUKRzQ/037xJ8LuzDmTwiyIjs9FCt3RONs1NJoAmrbih5JQ9vQ1A FvchS7h/qOEuk03YhpOxvQW6ez5mLKZ+zzeYLt1Z+CCm1BYtlC4vS7VV9hwgpdLjMUX1 PY+LSYH3Rrj9reNQmgj51ZrreKMXIejEmK1zGT5jVjQ2r3EOlmsw3buIjf9cfjkW5rlZ s53SNV+vSblx9UiyEPxhczg9yHnnxMYe1hlE89R3qbwD4Xt06NXoLjG61WY760aYccIc KBqAGUZJ//YbFm/db7QaZndtsHOULQNHTP3CnGDVX3rBZF+hIV5eK0LSt/Eex+FW6Tyv gcdA== Received: by 10.101.2.30 with SMTP id e30mr5032766ani.27.1337058149093; Mon, 14 May 2012 22:02:29 -0700 (PDT) Received: by 10.101.2.30 with SMTP id e30mr5032752ani.27.1337058148944; Mon, 14 May 2012 22:02:28 -0700 (PDT) Received: from wpzn4.hot.corp.google.com (216-239-44-65.google.com [216.239.44.65]) by gmr-mx.google.com with ESMTPS id v25si20664455yhm.0.2012.05.14.22.02.28 (version=TLSv1/SSLv3 cipher=AES128-SHA); Mon, 14 May 2012 22:02:28 -0700 (PDT) Received: from ruffy.mtv.corp.google.com (ruffy.mtv.corp.google.com [172.18.110.50]) by wpzn4.hot.corp.google.com (Postfix) with ESMTP id 8C3F51E004D; Mon, 14 May 2012 22:02:28 -0700 (PDT) Received: by ruffy.mtv.corp.google.com (Postfix, from userid 67641) id 3CAD62461B0; Mon, 14 May 2012 22:02:28 -0700 (PDT) To: gcc-patches@gcc.gnu.org, binutils@sourceware.org cc: gdb-patches@sourceware.org Subject: [RFA] leb128.h: New file. Message-Id: <20120515050228.3CAD62461B0@ruffy.mtv.corp.google.com> Date: Tue, 15 May 2012 05:02:00 -0000 From: dje@google.com (Doug Evans) X-Gm-Message-State: ALoCoQnmHvqdBPk4C1+vx0HAad0zU+cYwzQhPjiJrYqyqBXCWWOqsX0fvvKAwHKPogCOPPQmuMLHql3o27sOMQXEEZSoqV1Evt5JKUJtBUi/tXnvTLXQsGo1D2Ur8AncdTszADkPPA/sC+73ZtY3NXuoXf6ZqufSoQ== X-IsSubscribed: yes Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org X-SW-Source: 2012-05/txt/msg00550.txt.bz2 Hi. This patch creates leb128.h which contains a few functions for reading leb128 values. They're in a header as static inlines because they can be speed critical. I've only included what I need in GDB. Ok to check in? 2012-05-14 Doug Evans * leb128.h: New file. Index: leb128.h =================================================================== RCS file: leb128.h diff -N leb128.h --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ leb128.h 15 May 2012 04:56:33 -0000 @@ -0,0 +1,116 @@ +/* Utilities for reading leb128 values. + Copyright (C) 2012 Free Software Foundation, Inc. + +This file is part of the libiberty library. +Libiberty is free software; you can redistribute it and/or +modify it under the terms of the GNU Library General Public +License as published by the Free Software Foundation; either +version 2 of the License, or (at your option) any later version. + +Libiberty is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Library General Public License for more details. + +You should have received a copy of the GNU Library General Public +License along with libiberty; see the file COPYING.LIB. If not, write +to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor, +Boston, MA 02110-1301, USA. */ + +/* The functions defined here can be speed critical. + Since they are all pretty small we keep things simple and just define + them all as "static inline". */ + +#ifndef LEB128_H +#define LEB128_H + +#include "ansidecl.h" + +/* Get a definition for NULL. */ +#include + +#ifdef HAVE_STDINT_H +#include +#endif +#ifdef HAVE_INTTYPES_H +#include +#endif + +/* Decode the unsigned LEB128 constant at BUF into the variable pointed to + by R, and return the new value of BUF. + If we read off the end of the buffer, NULL is returned. */ + +static inline const unsigned char * +read_uleb128 (const unsigned char *buf, const unsigned char *buf_end, + uint64_t *r) +{ + unsigned int shift = 0; + uint64_t result = 0; + unsigned char byte; + + while (1) + { + if (buf >= buf_end) + return NULL; + + byte = *buf++; + result |= ((uint64_t) (byte & 0x7f)) << shift; + if ((byte & 0x80) == 0) + break; + shift += 7; + } + + *r = result; + return buf; +} + +/* Decode the signed LEB128 constant at BUF into the variable pointed to + by R, and return the new value of BUF. + If we read off the end of the buffer, NULL is returned. */ + +static inline const unsigned char * +read_sleb128 (const unsigned char *buf, const unsigned char *buf_end, + int64_t *r) +{ + unsigned int shift = 0; + int64_t result = 0; + unsigned char byte; + + while (1) + { + if (buf >= buf_end) + return NULL; + + byte = *buf++; + result |= ((uint64_t) (byte & 0x7f)) << shift; + shift += 7; + if ((byte & 0x80) == 0) + break; + } + if (shift < (sizeof (*r) * 8) && (byte & 0x40) != 0) + result |= -(((uint64_t) 1) << shift); + + *r = result; + return buf; +} + +/* Return a pointer to just past the end of an LEB128 number in BUF. + If the end isn't found before reaching BUF_END, return NULL. */ + +static inline const unsigned char * +skip_leb128 (const unsigned char *buf, const unsigned char *buf_end) +{ + unsigned char byte; + + while (1) + { + if (buf == buf_end) + return NULL; + + byte = *buf++; + if ((byte & 128) == 0) + return buf; + } +} + +#endif /* LEB128_H */