From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 26027 invoked by alias); 17 May 2012 18:29:48 -0000 Received: (qmail 25810 invoked by uid 22791); 17 May 2012 18:29:44 -0000 X-SWARE-Spam-Status: No, hits=-4.4 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-lpp01m010-f73.google.com (HELO mail-lpp01m010-f73.google.com) (209.85.215.73) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Thu, 17 May 2012 18:29:23 +0000 Received: by lahe6 with SMTP id e6so107515lah.0 for ; Thu, 17 May 2012 11:29:21 -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=3kXYOTJ0CqRbeyrG7ETcu3md//WXPkN/E4X+LyBe0PU=; b=MNHKuLkWyos11G3ueBYdKq34hLHRS8Bdm8YTkHWjbm/3GrYambZAt5ZBGnRx/TqCh/ cGu1d1Mcz5L9CkBPotCBE8GRCj4PuD9+1MR5lV+3l2nVl3X2vtlSL2KOhClE/p7T2L/M /JJQFPwUqCRB2CayJm+aQr0lBFLhbp/Ix4IMBliNcMZSPLb0mynrA/nLHWjpFMKSLDXx 7ZJKD7OVcT4NanIF24F7h4BouIcIXL+B/fhpFkm+YD6W0WYW+1YZr/VCw+f0a45MGZw6 eYK1lyi8Mn1GuR6a6qflgYQ6H4F7yU17HIM8HLvshm53F8A2hv0OaAiC1XnfCi5sG7r/ aRMw== Received: by 10.14.185.140 with SMTP id u12mr1715618eem.0.1337279361609; Thu, 17 May 2012 11:29:21 -0700 (PDT) Received: by 10.14.185.140 with SMTP id u12mr1715611eem.0.1337279361508; Thu, 17 May 2012 11:29:21 -0700 (PDT) Received: from hpza10.eem.corp.google.com ([74.125.121.33]) by gmr-mx.google.com with ESMTPS id d52si6285442eei.1.2012.05.17.11.29.21 (version=TLSv1/SSLv3 cipher=AES128-SHA); Thu, 17 May 2012 11:29:21 -0700 (PDT) Received: from ruffy.mtv.corp.google.com (ruffy.mtv.corp.google.com [172.18.110.50]) by hpza10.eem.corp.google.com (Postfix) with ESMTP id 20F7220004E; Thu, 17 May 2012 11:29:21 -0700 (PDT) Received: by ruffy.mtv.corp.google.com (Postfix, from userid 67641) id 64FAE2461B2; Thu, 17 May 2012 11:29:20 -0700 (PDT) To: gcc-patches@gcc.gnu.org, binutils@sourceware.org cc: gdb-patches@sourceware.org Subject: Re: [RFA] leb128.h: New file. Message-Id: <20120517182920.64FAE2461B2@ruffy.mtv.corp.google.com> Date: Thu, 17 May 2012 18:29:00 -0000 From: dje@google.com (Doug Evans) X-Gm-Message-State: ALoCoQmcAfI9diZWRPMzz67ypR4lPSX8Y/RW1YJXp36cEelCtv2aqySiDHbSoLhSy/DBKbd+le834qNNwWeS50tQwHmHYlOnTFScm6rawDceWc/hONEs02Dkg13/wa0m1qwVE8WnYrTa63B6m4QoRl1QBw4XwcrI4A== 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/msg00661.txt.bz2 Hi. This is a slightly modified version of my previous patch. ref: http://gcc.gnu.org/ml/gcc-patches/2012-05/msg00962.html The only change is to make the result of the functions an int instead of a const pointer. This lets them be used in places where the code is using non-const pointers without having to apply ugly casts. Ok to check in? 2012-05-17 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 17 May 2012 18:23:29 -0000 @@ -0,0 +1,130 @@ +/* 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 number of bytes read. + If we read off the end of the buffer, zero is returned, + and nothing is stored in R. + + Note: The result is an int instead of a pointer to the next byte to be + read to avoid const-vs-non-const problems. */ + +static inline int +read_uleb128 (const unsigned char *buf, const unsigned char *buf_end, + uint64_t *r) +{ + const unsigned char *p = buf; + unsigned int shift = 0; + uint64_t result = 0; + unsigned char byte; + + while (1) + { + if (p >= buf_end) + return 0; + + byte = *p++; + result |= ((uint64_t) (byte & 0x7f)) << shift; + if ((byte & 0x80) == 0) + break; + shift += 7; + } + + *r = result; + return p - buf; +} + +/* Decode the signed LEB128 constant at BUF into the variable pointed to + by R, and return the number of bytes read. + If we read off the end of the buffer, zero is returned, + and nothing is stored in R. + + Note: The result is an int instead of a pointer to the next byte to be + read to avoid const-vs-non-const problems. */ + +static inline int +read_sleb128 (const unsigned char *buf, const unsigned char *buf_end, + int64_t *r) +{ + const unsigned char *p = buf; + unsigned int shift = 0; + int64_t result = 0; + unsigned char byte; + + while (1) + { + if (p >= buf_end) + return 0; + + byte = *p++; + 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 p - buf; +} + +/* Return the number of bytes to read to skip past an LEB128 number in BUF. + If the end isn't found before reaching BUF_END, return zero. + + Note: The result is an int instead of a pointer to the next byte to be + read to avoid const-vs-non-const problems. */ + +static inline int +skip_leb128 (const unsigned char *buf, const unsigned char *buf_end) +{ + const unsigned char *p = buf; + unsigned char byte; + + while (1) + { + if (p == buf_end) + return 0; + + byte = *p++; + if ((byte & 128) == 0) + return p - buf; + } +} + +#endif /* LEB128_H */