xref: /illumos-gate/usr/src/cmd/sgs/libelf/misc/String.c (revision 2a8bcb4e)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
28*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate /*
31*7c478bd9Sstevel@tonic-gate  * C++ Demangler Source Code
32*7c478bd9Sstevel@tonic-gate  * @(#)master	1.5
33*7c478bd9Sstevel@tonic-gate  * 7/27/88 13:54:37
34*7c478bd9Sstevel@tonic-gate  */
35*7c478bd9Sstevel@tonic-gate #include <stdio.h>
36*7c478bd9Sstevel@tonic-gate #include <setjmp.h>
37*7c478bd9Sstevel@tonic-gate #include <assert.h>
38*7c478bd9Sstevel@tonic-gate #include <string.h>
39*7c478bd9Sstevel@tonic-gate #include <malloc.h>
40*7c478bd9Sstevel@tonic-gate #include "elf_dem.h"
41*7c478bd9Sstevel@tonic-gate #include "String.h"
42*7c478bd9Sstevel@tonic-gate 
43*7c478bd9Sstevel@tonic-gate /*
44*7c478bd9Sstevel@tonic-gate  * This code emulates the C++ String package
45*7c478bd9Sstevel@tonic-gate  * in a crude way.
46*7c478bd9Sstevel@tonic-gate  */
47*7c478bd9Sstevel@tonic-gate 
48*7c478bd9Sstevel@tonic-gate jmp_buf jbuf;
49*7c478bd9Sstevel@tonic-gate 
50*7c478bd9Sstevel@tonic-gate /*
51*7c478bd9Sstevel@tonic-gate  * This function will expand the space
52*7c478bd9Sstevel@tonic-gate  * available to a String so that more data
53*7c478bd9Sstevel@tonic-gate  * can be appended to it
54*7c478bd9Sstevel@tonic-gate  */
55*7c478bd9Sstevel@tonic-gate static String *
grow(s)56*7c478bd9Sstevel@tonic-gate grow(s)
57*7c478bd9Sstevel@tonic-gate String *s;
58*7c478bd9Sstevel@tonic-gate {
59*7c478bd9Sstevel@tonic-gate 	String *ns;
60*7c478bd9Sstevel@tonic-gate 	int sz = s->sg.max * 2;
61*7c478bd9Sstevel@tonic-gate 	assert(sz > 0);
62*7c478bd9Sstevel@tonic-gate #ifdef ELF
63*7c478bd9Sstevel@tonic-gate 	if ((ns = (String *)malloc(sz + sizeof (StringGuts) + 1)) == NULL)
64*7c478bd9Sstevel@tonic-gate 		longjmp(jbuf, 1);
65*7c478bd9Sstevel@tonic-gate 	(void) memcpy(ns, s, s->sg.max + sizeof (StringGuts) + 1);
66*7c478bd9Sstevel@tonic-gate 	free(s);
67*7c478bd9Sstevel@tonic-gate #else
68*7c478bd9Sstevel@tonic-gate 	if ((ns = (String *)realloc(s, sz + sizeof (StringGuts) + 1)) == NULL)
69*7c478bd9Sstevel@tonic-gate 		longjmp(jbuf, 1);
70*7c478bd9Sstevel@tonic-gate #endif
71*7c478bd9Sstevel@tonic-gate 	ns->sg.max = sz;
72*7c478bd9Sstevel@tonic-gate 	return (ns);
73*7c478bd9Sstevel@tonic-gate }
74*7c478bd9Sstevel@tonic-gate 
75*7c478bd9Sstevel@tonic-gate /*
76*7c478bd9Sstevel@tonic-gate  * This function will expand the space
77*7c478bd9Sstevel@tonic-gate  * available to a String so that more data
78*7c478bd9Sstevel@tonic-gate  * can be prepended to it.
79*7c478bd9Sstevel@tonic-gate  */
80*7c478bd9Sstevel@tonic-gate static String *
ror(s,n)81*7c478bd9Sstevel@tonic-gate ror(s, n)
82*7c478bd9Sstevel@tonic-gate String *s;
83*7c478bd9Sstevel@tonic-gate int n;
84*7c478bd9Sstevel@tonic-gate {
85*7c478bd9Sstevel@tonic-gate 	assert(s != 0);
86*7c478bd9Sstevel@tonic-gate 	while (s->sg.end + n > s->sg.max)
87*7c478bd9Sstevel@tonic-gate 		s = grow(s);
88*7c478bd9Sstevel@tonic-gate #ifdef __STDC__
89*7c478bd9Sstevel@tonic-gate 	assert(n >= 0);
90*7c478bd9Sstevel@tonic-gate 	assert(s->sg.end >= s->sg.start);
91*7c478bd9Sstevel@tonic-gate 	(void) memmove(s->data + n, s->data, s->sg.end - s->sg.start);
92*7c478bd9Sstevel@tonic-gate #else
93*7c478bd9Sstevel@tonic-gate 	{
94*7c478bd9Sstevel@tonic-gate 		int i;
95*7c478bd9Sstevel@tonic-gate 		for (i = s->sg.end - 1; i >= s->sg.start; i--)
96*7c478bd9Sstevel@tonic-gate 			s->data[i+n] = s->data[i];
97*7c478bd9Sstevel@tonic-gate 	}
98*7c478bd9Sstevel@tonic-gate #endif
99*7c478bd9Sstevel@tonic-gate 	s->sg.end += n;
100*7c478bd9Sstevel@tonic-gate 	s->sg.start += n;
101*7c478bd9Sstevel@tonic-gate 	s->data[s->sg.end] = 0;
102*7c478bd9Sstevel@tonic-gate 	return (s);
103*7c478bd9Sstevel@tonic-gate }
104*7c478bd9Sstevel@tonic-gate 
105*7c478bd9Sstevel@tonic-gate /*
106*7c478bd9Sstevel@tonic-gate  * This function will prepend c
107*7c478bd9Sstevel@tonic-gate  * to s
108*7c478bd9Sstevel@tonic-gate  */
109*7c478bd9Sstevel@tonic-gate String *
prep_String(c,s)110*7c478bd9Sstevel@tonic-gate prep_String(c, s)
111*7c478bd9Sstevel@tonic-gate char *c;
112*7c478bd9Sstevel@tonic-gate String *s;
113*7c478bd9Sstevel@tonic-gate {
114*7c478bd9Sstevel@tonic-gate 	return (nprep_String(c, s, ID_NAME_MAX));
115*7c478bd9Sstevel@tonic-gate }
116*7c478bd9Sstevel@tonic-gate 
117*7c478bd9Sstevel@tonic-gate /*
118*7c478bd9Sstevel@tonic-gate  * This function will prepend the
119*7c478bd9Sstevel@tonic-gate  * first n characters of c to s
120*7c478bd9Sstevel@tonic-gate  */
121*7c478bd9Sstevel@tonic-gate String *
nprep_String(c,s,n)122*7c478bd9Sstevel@tonic-gate nprep_String(c, s, n)
123*7c478bd9Sstevel@tonic-gate const char *c;
124*7c478bd9Sstevel@tonic-gate String *s;
125*7c478bd9Sstevel@tonic-gate int n;
126*7c478bd9Sstevel@tonic-gate {
127*7c478bd9Sstevel@tonic-gate 	int len = strlen(c);
128*7c478bd9Sstevel@tonic-gate 	assert(s != 0);
129*7c478bd9Sstevel@tonic-gate 	if (len > n)
130*7c478bd9Sstevel@tonic-gate 		len = n;
131*7c478bd9Sstevel@tonic-gate 	if (len > s->sg.start)
132*7c478bd9Sstevel@tonic-gate 		s = ror(s, len - s->sg.start);
133*7c478bd9Sstevel@tonic-gate 	s->sg.start -= len;
134*7c478bd9Sstevel@tonic-gate 	(void) memcpy(s->data + s->sg.start, c, len);
135*7c478bd9Sstevel@tonic-gate 	return (s);
136*7c478bd9Sstevel@tonic-gate }
137*7c478bd9Sstevel@tonic-gate 
138*7c478bd9Sstevel@tonic-gate /*
139*7c478bd9Sstevel@tonic-gate  * This function will append
140*7c478bd9Sstevel@tonic-gate  * c to s.
141*7c478bd9Sstevel@tonic-gate  */
142*7c478bd9Sstevel@tonic-gate String *
app_String(s,c)143*7c478bd9Sstevel@tonic-gate app_String(s, c)
144*7c478bd9Sstevel@tonic-gate String *s;
145*7c478bd9Sstevel@tonic-gate const char *c;
146*7c478bd9Sstevel@tonic-gate {
147*7c478bd9Sstevel@tonic-gate 	return (napp_String(s, c, ID_NAME_MAX));
148*7c478bd9Sstevel@tonic-gate }
149*7c478bd9Sstevel@tonic-gate 
150*7c478bd9Sstevel@tonic-gate /*
151*7c478bd9Sstevel@tonic-gate  * This function will append the
152*7c478bd9Sstevel@tonic-gate  * first n characters of c to s
153*7c478bd9Sstevel@tonic-gate  */
154*7c478bd9Sstevel@tonic-gate String *
napp_String(String * s,const char * c,int n)155*7c478bd9Sstevel@tonic-gate napp_String(String *s, const char *c, int n)
156*7c478bd9Sstevel@tonic-gate {
157*7c478bd9Sstevel@tonic-gate 	int len = strlen(c);
158*7c478bd9Sstevel@tonic-gate 	int catlen;
159*7c478bd9Sstevel@tonic-gate 	assert(s != 0);
160*7c478bd9Sstevel@tonic-gate 	if (n < len)
161*7c478bd9Sstevel@tonic-gate 		len = n;
162*7c478bd9Sstevel@tonic-gate 	catlen = s->sg.end + len;
163*7c478bd9Sstevel@tonic-gate 	while (catlen > s->sg.max)
164*7c478bd9Sstevel@tonic-gate 		s = grow(s);
165*7c478bd9Sstevel@tonic-gate 	(void) memcpy(s->data + s->sg.end, c, len);
166*7c478bd9Sstevel@tonic-gate 	s->sg.end += len;
167*7c478bd9Sstevel@tonic-gate 	s->data[s->sg.end] = '\0';
168*7c478bd9Sstevel@tonic-gate 	return (s);
169*7c478bd9Sstevel@tonic-gate }
170*7c478bd9Sstevel@tonic-gate 
171*7c478bd9Sstevel@tonic-gate /*
172*7c478bd9Sstevel@tonic-gate  * This function initializes a
173*7c478bd9Sstevel@tonic-gate  * String.  It returns its argument if
174*7c478bd9Sstevel@tonic-gate  * its argument is non-zero.
175*7c478bd9Sstevel@tonic-gate  * This prevents the same string
176*7c478bd9Sstevel@tonic-gate  * from being re-initialized.
177*7c478bd9Sstevel@tonic-gate  */
178*7c478bd9Sstevel@tonic-gate String *
mk_String(s)179*7c478bd9Sstevel@tonic-gate mk_String(s)
180*7c478bd9Sstevel@tonic-gate String *s;
181*7c478bd9Sstevel@tonic-gate {
182*7c478bd9Sstevel@tonic-gate 	if (s)
183*7c478bd9Sstevel@tonic-gate 		return (s);
184*7c478bd9Sstevel@tonic-gate 	s = (String *)malloc(STRING_START + sizeof (StringGuts) + 1);
185*7c478bd9Sstevel@tonic-gate 	if (s == NULL)
186*7c478bd9Sstevel@tonic-gate 		longjmp(jbuf, 1);
187*7c478bd9Sstevel@tonic-gate 	s->sg.start = s->sg.end = STRING_START/2;
188*7c478bd9Sstevel@tonic-gate 	s->sg.max = STRING_START;
189*7c478bd9Sstevel@tonic-gate 	s->data[s->sg.end] = '\0';
190*7c478bd9Sstevel@tonic-gate 	return (s);
191*7c478bd9Sstevel@tonic-gate }
192*7c478bd9Sstevel@tonic-gate 
193*7c478bd9Sstevel@tonic-gate void
free_String(s)194*7c478bd9Sstevel@tonic-gate free_String(s)
195*7c478bd9Sstevel@tonic-gate String *s;
196*7c478bd9Sstevel@tonic-gate {
197*7c478bd9Sstevel@tonic-gate 	if (s)
198*7c478bd9Sstevel@tonic-gate 		free(s);
199*7c478bd9Sstevel@tonic-gate }
200*7c478bd9Sstevel@tonic-gate 
201*7c478bd9Sstevel@tonic-gate /*
202*7c478bd9Sstevel@tonic-gate  * This function copies
203*7c478bd9Sstevel@tonic-gate  * c into s.
204*7c478bd9Sstevel@tonic-gate  * Used for initialization.
205*7c478bd9Sstevel@tonic-gate  */
206*7c478bd9Sstevel@tonic-gate String *
set_String(s,c)207*7c478bd9Sstevel@tonic-gate set_String(s, c)
208*7c478bd9Sstevel@tonic-gate String *s;
209*7c478bd9Sstevel@tonic-gate char *c;
210*7c478bd9Sstevel@tonic-gate {
211*7c478bd9Sstevel@tonic-gate 	int len = strlen(c)*2;
212*7c478bd9Sstevel@tonic-gate 	while (len > s->sg.max)
213*7c478bd9Sstevel@tonic-gate 		s = grow(s);
214*7c478bd9Sstevel@tonic-gate 	s->sg.start = s->sg.end = s->sg.max / 2;
215*7c478bd9Sstevel@tonic-gate 	s = app_String(s, c);
216*7c478bd9Sstevel@tonic-gate 	return (s);
217*7c478bd9Sstevel@tonic-gate }
218*7c478bd9Sstevel@tonic-gate 
219*7c478bd9Sstevel@tonic-gate /*
220*7c478bd9Sstevel@tonic-gate  * Chop n characters off the end of a string.
221*7c478bd9Sstevel@tonic-gate  * Return the truncated string.
222*7c478bd9Sstevel@tonic-gate  */
223*7c478bd9Sstevel@tonic-gate String *
trunc_String(String * s,int n)224*7c478bd9Sstevel@tonic-gate trunc_String(String *s, int n)
225*7c478bd9Sstevel@tonic-gate {
226*7c478bd9Sstevel@tonic-gate 	assert(n <= s->sg.end - s->sg.start);
227*7c478bd9Sstevel@tonic-gate 	s->sg.end -= n;
228*7c478bd9Sstevel@tonic-gate 	s->data[s->sg.end] = '\0';
229*7c478bd9Sstevel@tonic-gate 	return (s);
230*7c478bd9Sstevel@tonic-gate }
231