1af0dd31fSDavid Chisnall /*-
2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
31de7b4b8SPedro F. Giffuni *
4af0dd31fSDavid Chisnall * Copyright (c) 2013 David Chisnall
5af0dd31fSDavid Chisnall * All rights reserved.
6af0dd31fSDavid Chisnall *
7af0dd31fSDavid Chisnall * This software was developed by SRI International and the University of
8af0dd31fSDavid Chisnall * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237)
9af0dd31fSDavid Chisnall * ("CTSRD"), as part of the DARPA CRASH research programme.
10af0dd31fSDavid Chisnall *
11af0dd31fSDavid Chisnall * Redistribution and use in source and binary forms, with or without
12af0dd31fSDavid Chisnall * modification, are permitted provided that the following conditions
13af0dd31fSDavid Chisnall * are met:
14af0dd31fSDavid Chisnall * 1. Redistributions of source code must retain the above copyright
15af0dd31fSDavid Chisnall * notice, this list of conditions and the following disclaimer.
16af0dd31fSDavid Chisnall * 2. Redistributions in binary form must reproduce the above copyright
17af0dd31fSDavid Chisnall * notice, this list of conditions and the following disclaimer in the
18af0dd31fSDavid Chisnall * documentation and/or other materials provided with the distribution.
19af0dd31fSDavid Chisnall *
20af0dd31fSDavid Chisnall * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21af0dd31fSDavid Chisnall * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22af0dd31fSDavid Chisnall * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23af0dd31fSDavid Chisnall * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24af0dd31fSDavid Chisnall * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25af0dd31fSDavid Chisnall * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26af0dd31fSDavid Chisnall * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27af0dd31fSDavid Chisnall * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28af0dd31fSDavid Chisnall * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29af0dd31fSDavid Chisnall * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30af0dd31fSDavid Chisnall * SUCH DAMAGE.
31af0dd31fSDavid Chisnall */
32af0dd31fSDavid Chisnall
33af0dd31fSDavid Chisnall #ifndef _UTIL_HH_
34af0dd31fSDavid Chisnall #define _UTIL_HH_
35af0dd31fSDavid Chisnall
3667b60a1bSKyle Evans #include <memory>
3767b60a1bSKyle Evans #include <stdint.h>
3867b60a1bSKyle Evans #include <string>
39af0dd31fSDavid Chisnall #include <vector>
40af0dd31fSDavid Chisnall
41af0dd31fSDavid Chisnall // If we aren't using C++11, then just ignore static asserts.
42af0dd31fSDavid Chisnall #if __cplusplus < 201103L
43af0dd31fSDavid Chisnall #ifndef static_assert
44af0dd31fSDavid Chisnall #define static_assert(x, y) ((void)0)
45af0dd31fSDavid Chisnall #endif
46af0dd31fSDavid Chisnall #endif
47af0dd31fSDavid Chisnall
48d37eb02eSKyle Evans #ifdef MISSING_DIGITTOINT
49d37eb02eSKyle Evans namespace
50d37eb02eSKyle Evans {
51d37eb02eSKyle Evans /**
52d37eb02eSKyle Evans * Glibc doesn't have a definition of digittoint, so provide our own.
53d37eb02eSKyle Evans */
digittoint(int c)54d37eb02eSKyle Evans inline int digittoint(int c)
55d37eb02eSKyle Evans {
56d37eb02eSKyle Evans switch (c)
57d37eb02eSKyle Evans {
58d37eb02eSKyle Evans default:
59d37eb02eSKyle Evans case '0': return 0;
60d37eb02eSKyle Evans case '1': return 1;
61d37eb02eSKyle Evans case '2': return 2;
62d37eb02eSKyle Evans case '3': return 3;
63d37eb02eSKyle Evans case '4': return 4;
64d37eb02eSKyle Evans case '5': return 5;
65d37eb02eSKyle Evans case '6': return 6;
66d37eb02eSKyle Evans case '7': return 7;
67d37eb02eSKyle Evans case '8': return 8;
68d37eb02eSKyle Evans case '9': return 9;
69d37eb02eSKyle Evans case 'a': return 10;
70d37eb02eSKyle Evans case 'b': return 11;
71d37eb02eSKyle Evans case 'c': return 12;
72d37eb02eSKyle Evans case 'd': return 13;
73d37eb02eSKyle Evans case 'e': return 14;
74d37eb02eSKyle Evans case 'f': return 15;
75d37eb02eSKyle Evans }
76d37eb02eSKyle Evans }
77d37eb02eSKyle Evans }
78d37eb02eSKyle Evans #endif
79d37eb02eSKyle Evans
80af0dd31fSDavid Chisnall namespace dtc {
81af0dd31fSDavid Chisnall
82af0dd31fSDavid Chisnall /**
83af0dd31fSDavid Chisnall * Type for a buffer of bytes. This is used for a lot of short-lived temporary
84af0dd31fSDavid Chisnall * variables, so may eventually be changed to something like LLVM's
85af0dd31fSDavid Chisnall * SmallVector, but currently the program runs in a tiny fraction of a second,
86af0dd31fSDavid Chisnall * so this is not an issue.
87af0dd31fSDavid Chisnall */
88af0dd31fSDavid Chisnall typedef std::vector<uint8_t> byte_buffer;
89af0dd31fSDavid Chisnall
90af0dd31fSDavid Chisnall /**
91af0dd31fSDavid Chisnall * Helper function to push a big endian value into a byte buffer. We use
92af0dd31fSDavid Chisnall * native-endian values for all of the in-memory data structures and only
93af0dd31fSDavid Chisnall * transform them into big endian form for output.
94af0dd31fSDavid Chisnall */
95af0dd31fSDavid Chisnall template<typename T>
push_big_endian(byte_buffer & v,T val)96af0dd31fSDavid Chisnall inline void push_big_endian(byte_buffer &v, T val)
97af0dd31fSDavid Chisnall {
98af0dd31fSDavid Chisnall static_assert(sizeof(T) > 1,
99af0dd31fSDavid Chisnall "Big endian doesn't make sense for single-byte values");
100af0dd31fSDavid Chisnall for (int bit=(sizeof(T) - 1)*8 ; bit>=0 ; bit-= 8)
101af0dd31fSDavid Chisnall {
102af0dd31fSDavid Chisnall v.push_back((val >> bit) & 0xff);
103af0dd31fSDavid Chisnall }
104af0dd31fSDavid Chisnall }
105af0dd31fSDavid Chisnall
106bbe31b70SEd Maste void push_string(byte_buffer &v, const std::string &s, bool escapes=false);
107bbe31b70SEd Maste
108af0dd31fSDavid Chisnall /**
109af0dd31fSDavid Chisnall * Simple inline non-locale-aware check that this is a valid ASCII
110af0dd31fSDavid Chisnall * digit.
111af0dd31fSDavid Chisnall */
isdigit(char c)112af0dd31fSDavid Chisnall inline bool isdigit(char c)
113af0dd31fSDavid Chisnall {
114af0dd31fSDavid Chisnall return (c >= '0') && (c <= '9');
115af0dd31fSDavid Chisnall }
116af0dd31fSDavid Chisnall
117af0dd31fSDavid Chisnall /**
118af0dd31fSDavid Chisnall * Simple inline non-locale-aware check that this is a valid ASCII
119af0dd31fSDavid Chisnall * hex digit.
120af0dd31fSDavid Chisnall */
ishexdigit(char c)121af0dd31fSDavid Chisnall inline bool ishexdigit(char c)
122af0dd31fSDavid Chisnall {
123af0dd31fSDavid Chisnall return ((c >= '0') && (c <= '9')) || ((c >= 'a') && (c <= 'f')) ||
124bbe31b70SEd Maste ((c >= 'A') && (c <= 'F'));
125af0dd31fSDavid Chisnall }
126af0dd31fSDavid Chisnall
127bbe31b70SEd Maste /**
128bbe31b70SEd Maste * Simple inline non-locale-aware check that this is a valid ASCII
129bbe31b70SEd Maste * letter.
130bbe31b70SEd Maste */
isalpha(char c)131bbe31b70SEd Maste inline bool isalpha(char c)
132bbe31b70SEd Maste {
133bbe31b70SEd Maste return ((c >= 'a') && (c <= 'z')) || ((c >= 'A') && (c <= 'Z'));
134bbe31b70SEd Maste }
135bbe31b70SEd Maste
136bbe31b70SEd Maste /**
137bbe31b70SEd Maste * A wrapper around dirname(3) that handles inconsistencies relating to memory
138bbe31b70SEd Maste * management between platforms and provides a std::string interface.
139bbe31b70SEd Maste */
140bbe31b70SEd Maste std::string dirname(const std::string&);
141bbe31b70SEd Maste
142bbe31b70SEd Maste /**
143bbe31b70SEd Maste * A wrapper around basename(3) that handles inconsistencies relating to memory
144bbe31b70SEd Maste * management between platforms and provides a std::string interface.
145bbe31b70SEd Maste */
146bbe31b70SEd Maste std::string basename(const std::string&);
147bbe31b70SEd Maste
148af0dd31fSDavid Chisnall }// namespace dtc
149af0dd31fSDavid Chisnall
150af0dd31fSDavid Chisnall #endif // !_UTIL_HH_
151