1245803Stheraven/*- 2245803Stheraven * Copyright (c) 2013 David Chisnall 3245803Stheraven * All rights reserved. 4245803Stheraven * 5245803Stheraven * This software was developed by SRI International and the University of 6245803Stheraven * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237) 7245803Stheraven * ("CTSRD"), as part of the DARPA CRASH research programme. 8245803Stheraven * 9245803Stheraven * Redistribution and use in source and binary forms, with or without 10245803Stheraven * modification, are permitted provided that the following conditions 11245803Stheraven * are met: 12245803Stheraven * 1. Redistributions of source code must retain the above copyright 13245803Stheraven * notice, this list of conditions and the following disclaimer. 14245803Stheraven * 2. Redistributions in binary form must reproduce the above copyright 15245803Stheraven * notice, this list of conditions and the following disclaimer in the 16245803Stheraven * documentation and/or other materials provided with the distribution. 17245803Stheraven * 18245803Stheraven * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19245803Stheraven * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20245803Stheraven * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21245803Stheraven * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22245803Stheraven * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23245803Stheraven * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24245803Stheraven * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25245803Stheraven * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26245803Stheraven * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27245803Stheraven * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28245803Stheraven * SUCH DAMAGE. 29245803Stheraven * 30245803Stheraven * $FreeBSD$ 31245803Stheraven */ 32245803Stheraven 33245803Stheraven#ifndef _UTIL_HH_ 34245803Stheraven#define _UTIL_HH_ 35245803Stheraven 36245803Stheraven#include <vector> 37245803Stheraven 38245803Stheraven// If we aren't using C++11, then just ignore static asserts. 39245803Stheraven#if __cplusplus < 201103L 40245803Stheraven#ifndef static_assert 41245803Stheraven#define static_assert(x, y) ((void)0) 42245803Stheraven#endif 43245803Stheraven#endif 44245803Stheraven 45245803Stheravennamespace dtc { 46245803Stheraven 47245803Stheraven/** 48245803Stheraven * Type for a buffer of bytes. This is used for a lot of short-lived temporary 49245803Stheraven * variables, so may eventually be changed to something like LLVM's 50245803Stheraven * SmallVector, but currently the program runs in a tiny fraction of a second, 51245803Stheraven * so this is not an issue. 52245803Stheraven */ 53245803Stheraventypedef std::vector<uint8_t> byte_buffer; 54245803Stheraven 55245803Stheraven/** 56245803Stheraven * Helper function to push a big endian value into a byte buffer. We use 57245803Stheraven * native-endian values for all of the in-memory data structures and only 58245803Stheraven * transform them into big endian form for output. 59245803Stheraven */ 60245803Stheraventemplate<typename T> 61245803Stheraveninline void push_big_endian(byte_buffer &v, T val) 62245803Stheraven{ 63245803Stheraven static_assert(sizeof(T) > 1, 64245803Stheraven "Big endian doesn't make sense for single-byte values"); 65245803Stheraven for (int bit=(sizeof(T) - 1)*8 ; bit>=0 ; bit-= 8) 66245803Stheraven { 67245803Stheraven v.push_back((val >> bit) & 0xff); 68245803Stheraven } 69245803Stheraven} 70245803Stheraven 71245803Stheraven/** 72245803Stheraven * Simple inline non-locale-aware check that this is a valid ASCII 73245803Stheraven * digit. 74245803Stheraven */ 75245803Stheraveninline bool isdigit(char c) 76245803Stheraven{ 77245803Stheraven return (c >= '0') && (c <= '9'); 78245803Stheraven} 79245803Stheraven 80245803Stheraven/** 81245803Stheraven * Simple inline non-locale-aware check that this is a valid ASCII 82245803Stheraven * hex digit. 83245803Stheraven */ 84245803Stheraveninline bool ishexdigit(char c) 85245803Stheraven{ 86245803Stheraven return ((c >= '0') && (c <= '9')) || ((c >= 'a') && (c <= 'f')) || 87245803Stheraven ((c >= 'A') && (c <= 'Z')); 88245803Stheraven} 89245803Stheraven 90245803Stheraven}// namespace dtc 91245803Stheraven 92245803Stheraven#endif // !_UTIL_HH_ 93