xref: /freebsd/usr.bin/dtc/string.cc (revision bbe31b709a653884e18995a1c97cdafd7392999a)
1af0dd31fSDavid Chisnall /*-
2af0dd31fSDavid Chisnall  * Copyright (c) 2013 David Chisnall
3af0dd31fSDavid Chisnall  * All rights reserved.
4af0dd31fSDavid Chisnall  *
5af0dd31fSDavid Chisnall  * This software was developed by SRI International and the University of
6af0dd31fSDavid Chisnall  * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237)
7af0dd31fSDavid Chisnall  * ("CTSRD"), as part of the DARPA CRASH research programme.
8af0dd31fSDavid Chisnall  *
9af0dd31fSDavid Chisnall  * Redistribution and use in source and binary forms, with or without
10af0dd31fSDavid Chisnall  * modification, are permitted provided that the following conditions
11af0dd31fSDavid Chisnall  * are met:
12af0dd31fSDavid Chisnall  * 1. Redistributions of source code must retain the above copyright
13af0dd31fSDavid Chisnall  *    notice, this list of conditions and the following disclaimer.
14af0dd31fSDavid Chisnall  * 2. Redistributions in binary form must reproduce the above copyright
15af0dd31fSDavid Chisnall  *    notice, this list of conditions and the following disclaimer in the
16af0dd31fSDavid Chisnall  *    documentation and/or other materials provided with the distribution.
17af0dd31fSDavid Chisnall  *
18af0dd31fSDavid Chisnall  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19af0dd31fSDavid Chisnall  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20af0dd31fSDavid Chisnall  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21af0dd31fSDavid Chisnall  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22af0dd31fSDavid Chisnall  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23af0dd31fSDavid Chisnall  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24af0dd31fSDavid Chisnall  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25af0dd31fSDavid Chisnall  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26af0dd31fSDavid Chisnall  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27af0dd31fSDavid Chisnall  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28af0dd31fSDavid Chisnall  * SUCH DAMAGE.
29af0dd31fSDavid Chisnall  *
30af0dd31fSDavid Chisnall  * $FreeBSD$
31af0dd31fSDavid Chisnall  */
32af0dd31fSDavid Chisnall 
33*bbe31b70SEd Maste #include <string>
34*bbe31b70SEd Maste #include <cstdio>
35*bbe31b70SEd Maste #include <cstdlib>
36009f7b42SDavid Chisnall #include <ctype.h>
37*bbe31b70SEd Maste #include <libgen.h>
38af0dd31fSDavid Chisnall 
39*bbe31b70SEd Maste #include "util.hh"
40af0dd31fSDavid Chisnall 
41*bbe31b70SEd Maste using std::string;
42af0dd31fSDavid Chisnall 
43af0dd31fSDavid Chisnall namespace dtc
44af0dd31fSDavid Chisnall {
45af0dd31fSDavid Chisnall 
46af0dd31fSDavid Chisnall void
47*bbe31b70SEd Maste push_string(byte_buffer &buffer, const string &s, bool escapes)
48af0dd31fSDavid Chisnall {
49*bbe31b70SEd Maste 	size_t length = s.size();
50*bbe31b70SEd Maste 	for (size_t i=0 ; i<length ; ++i)
51af0dd31fSDavid Chisnall 	{
52*bbe31b70SEd Maste 		uint8_t c = s[i];
53af0dd31fSDavid Chisnall 		if (escapes && c == '\\' && i+1 < length)
54af0dd31fSDavid Chisnall 		{
55*bbe31b70SEd Maste 			c = s[++i];
56af0dd31fSDavid Chisnall 			switch (c)
57af0dd31fSDavid Chisnall 			{
58af0dd31fSDavid Chisnall 				// For now, we just ignore invalid escape sequences.
59af0dd31fSDavid Chisnall 				default:
60af0dd31fSDavid Chisnall 				case '"':
61af0dd31fSDavid Chisnall 				case '\'':
62af0dd31fSDavid Chisnall 				case '\\':
63af0dd31fSDavid Chisnall 					break;
64af0dd31fSDavid Chisnall 				case 'a':
65af0dd31fSDavid Chisnall 					c = '\a';
66af0dd31fSDavid Chisnall 					break;
67af0dd31fSDavid Chisnall 				case 'b':
68af0dd31fSDavid Chisnall 					c = '\b';
69af0dd31fSDavid Chisnall 					break;
70af0dd31fSDavid Chisnall 				case 't':
71af0dd31fSDavid Chisnall 					c = '\t';
72af0dd31fSDavid Chisnall 					break;
73af0dd31fSDavid Chisnall 				case 'n':
74af0dd31fSDavid Chisnall 					c = '\n';
75af0dd31fSDavid Chisnall 					break;
76af0dd31fSDavid Chisnall 				case 'v':
77af0dd31fSDavid Chisnall 					c = '\v';
78af0dd31fSDavid Chisnall 					break;
79af0dd31fSDavid Chisnall 				case 'f':
80af0dd31fSDavid Chisnall 					c = '\f';
81af0dd31fSDavid Chisnall 					break;
82af0dd31fSDavid Chisnall 				case 'r':
83af0dd31fSDavid Chisnall 					c = '\r';
84af0dd31fSDavid Chisnall 					break;
85af0dd31fSDavid Chisnall 				case '0'...'7':
86af0dd31fSDavid Chisnall 				{
87af0dd31fSDavid Chisnall 					int v = digittoint(c);
88*bbe31b70SEd Maste 					if (i+1 < length && s[i+1] <= '7' && s[i+1] >= '0')
89af0dd31fSDavid Chisnall 					{
90af0dd31fSDavid Chisnall 						v <<= 3;
91*bbe31b70SEd Maste 						v |= digittoint(s[i+1]);
92af0dd31fSDavid Chisnall 						i++;
93*bbe31b70SEd Maste 						if (i+1 < length && s[i+1] <= '7' && s[i+1] >= '0')
94af0dd31fSDavid Chisnall 						{
95af0dd31fSDavid Chisnall 							v <<= 3;
96*bbe31b70SEd Maste 							v |= digittoint(s[i+1]);
97af0dd31fSDavid Chisnall 						}
98af0dd31fSDavid Chisnall 					}
99af0dd31fSDavid Chisnall 					c = (uint8_t)v;
100af0dd31fSDavid Chisnall 					break;
101af0dd31fSDavid Chisnall 				}
102af0dd31fSDavid Chisnall 				case 'x':
103af0dd31fSDavid Chisnall 				{
104af0dd31fSDavid Chisnall 					++i;
105af0dd31fSDavid Chisnall 					if (i >= length)
106af0dd31fSDavid Chisnall 					{
107af0dd31fSDavid Chisnall 						break;
108af0dd31fSDavid Chisnall 					}
109*bbe31b70SEd Maste 					int v = digittoint(s[i]);
110*bbe31b70SEd Maste 					if (i+1 < length && ishexdigit(s[i+1]))
111af0dd31fSDavid Chisnall 					{
112af0dd31fSDavid Chisnall 						v <<= 4;
113*bbe31b70SEd Maste 						v |= digittoint(s[++i]);
114af0dd31fSDavid Chisnall 					}
115af0dd31fSDavid Chisnall 					c = (uint8_t)v;
116af0dd31fSDavid Chisnall 					break;
117af0dd31fSDavid Chisnall 				}
118af0dd31fSDavid Chisnall 			}
119af0dd31fSDavid Chisnall 		}
120af0dd31fSDavid Chisnall 		buffer.push_back(c);
121af0dd31fSDavid Chisnall 	}
122af0dd31fSDavid Chisnall }
123af0dd31fSDavid Chisnall 
124*bbe31b70SEd Maste std::string dirname(const string &s)
125af0dd31fSDavid Chisnall {
126*bbe31b70SEd Maste 	if (s == string())
127*bbe31b70SEd Maste 	{
128*bbe31b70SEd Maste 		return string();
129*bbe31b70SEd Maste 	}
130*bbe31b70SEd Maste 	char *str = strdup(s.c_str());
131*bbe31b70SEd Maste 	string dn(::dirname(str));
132*bbe31b70SEd Maste 	free(str);
133*bbe31b70SEd Maste 	return dn;
134af0dd31fSDavid Chisnall }
135af0dd31fSDavid Chisnall 
136*bbe31b70SEd Maste std::string basename(const string &s)
137af0dd31fSDavid Chisnall {
138*bbe31b70SEd Maste 	if (s == string())
139*bbe31b70SEd Maste 	{
140*bbe31b70SEd Maste 		return string();
141af0dd31fSDavid Chisnall 	}
142*bbe31b70SEd Maste 	char *str = strdup(s.c_str());
143*bbe31b70SEd Maste 	string bn(::basename(str));
144*bbe31b70SEd Maste 	free(str);
145*bbe31b70SEd Maste 	return bn;
146*bbe31b70SEd Maste }
147af0dd31fSDavid Chisnall } // namespace dtc
148af0dd31fSDavid Chisnall 
149