xref: /titanic_44/usr/src/cmd/sgs/libelf/misc/String.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate /*
34*7c478bd9Sstevel@tonic-gate  * C++ Demangler Source Code
35*7c478bd9Sstevel@tonic-gate  * @(#)master	1.5
36*7c478bd9Sstevel@tonic-gate  * 7/27/88 13:54:37
37*7c478bd9Sstevel@tonic-gate  */
38*7c478bd9Sstevel@tonic-gate #include <stdio.h>
39*7c478bd9Sstevel@tonic-gate #include <setjmp.h>
40*7c478bd9Sstevel@tonic-gate #include <assert.h>
41*7c478bd9Sstevel@tonic-gate #include <string.h>
42*7c478bd9Sstevel@tonic-gate #include <malloc.h>
43*7c478bd9Sstevel@tonic-gate #include "elf_dem.h"
44*7c478bd9Sstevel@tonic-gate #include "String.h"
45*7c478bd9Sstevel@tonic-gate 
46*7c478bd9Sstevel@tonic-gate /*
47*7c478bd9Sstevel@tonic-gate  * This code emulates the C++ String package
48*7c478bd9Sstevel@tonic-gate  * in a crude way.
49*7c478bd9Sstevel@tonic-gate  */
50*7c478bd9Sstevel@tonic-gate 
51*7c478bd9Sstevel@tonic-gate jmp_buf jbuf;
52*7c478bd9Sstevel@tonic-gate 
53*7c478bd9Sstevel@tonic-gate /*
54*7c478bd9Sstevel@tonic-gate  * This function will expand the space
55*7c478bd9Sstevel@tonic-gate  * available to a String so that more data
56*7c478bd9Sstevel@tonic-gate  * can be appended to it
57*7c478bd9Sstevel@tonic-gate  */
58*7c478bd9Sstevel@tonic-gate static String *
grow(s)59*7c478bd9Sstevel@tonic-gate grow(s)
60*7c478bd9Sstevel@tonic-gate String *s;
61*7c478bd9Sstevel@tonic-gate {
62*7c478bd9Sstevel@tonic-gate 	String *ns;
63*7c478bd9Sstevel@tonic-gate 	int sz = s->sg.max * 2;
64*7c478bd9Sstevel@tonic-gate 	assert(sz > 0);
65*7c478bd9Sstevel@tonic-gate #ifdef ELF
66*7c478bd9Sstevel@tonic-gate 	if ((ns = (String *)malloc(sz + sizeof (StringGuts) + 1)) == NULL)
67*7c478bd9Sstevel@tonic-gate 		longjmp(jbuf, 1);
68*7c478bd9Sstevel@tonic-gate 	(void) memcpy(ns, s, s->sg.max + sizeof (StringGuts) + 1);
69*7c478bd9Sstevel@tonic-gate 	free(s);
70*7c478bd9Sstevel@tonic-gate #else
71*7c478bd9Sstevel@tonic-gate 	if ((ns = (String *)realloc(s, sz + sizeof (StringGuts) + 1)) == NULL)
72*7c478bd9Sstevel@tonic-gate 		longjmp(jbuf, 1);
73*7c478bd9Sstevel@tonic-gate #endif
74*7c478bd9Sstevel@tonic-gate 	ns->sg.max = sz;
75*7c478bd9Sstevel@tonic-gate 	return (ns);
76*7c478bd9Sstevel@tonic-gate }
77*7c478bd9Sstevel@tonic-gate 
78*7c478bd9Sstevel@tonic-gate /*
79*7c478bd9Sstevel@tonic-gate  * This function will expand the space
80*7c478bd9Sstevel@tonic-gate  * available to a String so that more data
81*7c478bd9Sstevel@tonic-gate  * can be prepended to it.
82*7c478bd9Sstevel@tonic-gate  */
83*7c478bd9Sstevel@tonic-gate static String *
ror(s,n)84*7c478bd9Sstevel@tonic-gate ror(s, n)
85*7c478bd9Sstevel@tonic-gate String *s;
86*7c478bd9Sstevel@tonic-gate int n;
87*7c478bd9Sstevel@tonic-gate {
88*7c478bd9Sstevel@tonic-gate 	assert(s != 0);
89*7c478bd9Sstevel@tonic-gate 	while (s->sg.end + n > s->sg.max)
90*7c478bd9Sstevel@tonic-gate 		s = grow(s);
91*7c478bd9Sstevel@tonic-gate #ifdef __STDC__
92*7c478bd9Sstevel@tonic-gate 	assert(n >= 0);
93*7c478bd9Sstevel@tonic-gate 	assert(s->sg.end >= s->sg.start);
94*7c478bd9Sstevel@tonic-gate 	(void) memmove(s->data + n, s->data, s->sg.end - s->sg.start);
95*7c478bd9Sstevel@tonic-gate #else
96*7c478bd9Sstevel@tonic-gate 	{
97*7c478bd9Sstevel@tonic-gate 		int i;
98*7c478bd9Sstevel@tonic-gate 		for (i = s->sg.end - 1; i >= s->sg.start; i--)
99*7c478bd9Sstevel@tonic-gate 			s->data[i+n] = s->data[i];
100*7c478bd9Sstevel@tonic-gate 	}
101*7c478bd9Sstevel@tonic-gate #endif
102*7c478bd9Sstevel@tonic-gate 	s->sg.end += n;
103*7c478bd9Sstevel@tonic-gate 	s->sg.start += n;
104*7c478bd9Sstevel@tonic-gate 	s->data[s->sg.end] = 0;
105*7c478bd9Sstevel@tonic-gate 	return (s);
106*7c478bd9Sstevel@tonic-gate }
107*7c478bd9Sstevel@tonic-gate 
108*7c478bd9Sstevel@tonic-gate /*
109*7c478bd9Sstevel@tonic-gate  * This function will prepend c
110*7c478bd9Sstevel@tonic-gate  * to s
111*7c478bd9Sstevel@tonic-gate  */
112*7c478bd9Sstevel@tonic-gate String *
prep_String(c,s)113*7c478bd9Sstevel@tonic-gate prep_String(c, s)
114*7c478bd9Sstevel@tonic-gate char *c;
115*7c478bd9Sstevel@tonic-gate String *s;
116*7c478bd9Sstevel@tonic-gate {
117*7c478bd9Sstevel@tonic-gate 	return (nprep_String(c, s, ID_NAME_MAX));
118*7c478bd9Sstevel@tonic-gate }
119*7c478bd9Sstevel@tonic-gate 
120*7c478bd9Sstevel@tonic-gate /*
121*7c478bd9Sstevel@tonic-gate  * This function will prepend the
122*7c478bd9Sstevel@tonic-gate  * first n characters of c to s
123*7c478bd9Sstevel@tonic-gate  */
124*7c478bd9Sstevel@tonic-gate String *
nprep_String(c,s,n)125*7c478bd9Sstevel@tonic-gate nprep_String(c, s, n)
126*7c478bd9Sstevel@tonic-gate const char *c;
127*7c478bd9Sstevel@tonic-gate String *s;
128*7c478bd9Sstevel@tonic-gate int n;
129*7c478bd9Sstevel@tonic-gate {
130*7c478bd9Sstevel@tonic-gate 	int len = strlen(c);
131*7c478bd9Sstevel@tonic-gate 	assert(s != 0);
132*7c478bd9Sstevel@tonic-gate 	if (len > n)
133*7c478bd9Sstevel@tonic-gate 		len = n;
134*7c478bd9Sstevel@tonic-gate 	if (len > s->sg.start)
135*7c478bd9Sstevel@tonic-gate 		s = ror(s, len - s->sg.start);
136*7c478bd9Sstevel@tonic-gate 	s->sg.start -= len;
137*7c478bd9Sstevel@tonic-gate 	(void) memcpy(s->data + s->sg.start, c, len);
138*7c478bd9Sstevel@tonic-gate 	return (s);
139*7c478bd9Sstevel@tonic-gate }
140*7c478bd9Sstevel@tonic-gate 
141*7c478bd9Sstevel@tonic-gate /*
142*7c478bd9Sstevel@tonic-gate  * This function will append
143*7c478bd9Sstevel@tonic-gate  * c to s.
144*7c478bd9Sstevel@tonic-gate  */
145*7c478bd9Sstevel@tonic-gate String *
app_String(s,c)146*7c478bd9Sstevel@tonic-gate app_String(s, c)
147*7c478bd9Sstevel@tonic-gate String *s;
148*7c478bd9Sstevel@tonic-gate const char *c;
149*7c478bd9Sstevel@tonic-gate {
150*7c478bd9Sstevel@tonic-gate 	return (napp_String(s, c, ID_NAME_MAX));
151*7c478bd9Sstevel@tonic-gate }
152*7c478bd9Sstevel@tonic-gate 
153*7c478bd9Sstevel@tonic-gate /*
154*7c478bd9Sstevel@tonic-gate  * This function will append the
155*7c478bd9Sstevel@tonic-gate  * first n characters of c to s
156*7c478bd9Sstevel@tonic-gate  */
157*7c478bd9Sstevel@tonic-gate String *
napp_String(String * s,const char * c,int n)158*7c478bd9Sstevel@tonic-gate napp_String(String *s, const char *c, int n)
159*7c478bd9Sstevel@tonic-gate {
160*7c478bd9Sstevel@tonic-gate 	int len = strlen(c);
161*7c478bd9Sstevel@tonic-gate 	int catlen;
162*7c478bd9Sstevel@tonic-gate 	assert(s != 0);
163*7c478bd9Sstevel@tonic-gate 	if (n < len)
164*7c478bd9Sstevel@tonic-gate 		len = n;
165*7c478bd9Sstevel@tonic-gate 	catlen = s->sg.end + len;
166*7c478bd9Sstevel@tonic-gate 	while (catlen > s->sg.max)
167*7c478bd9Sstevel@tonic-gate 		s = grow(s);
168*7c478bd9Sstevel@tonic-gate 	(void) memcpy(s->data + s->sg.end, c, len);
169*7c478bd9Sstevel@tonic-gate 	s->sg.end += len;
170*7c478bd9Sstevel@tonic-gate 	s->data[s->sg.end] = '\0';
171*7c478bd9Sstevel@tonic-gate 	return (s);
172*7c478bd9Sstevel@tonic-gate }
173*7c478bd9Sstevel@tonic-gate 
174*7c478bd9Sstevel@tonic-gate /*
175*7c478bd9Sstevel@tonic-gate  * This function initializes a
176*7c478bd9Sstevel@tonic-gate  * String.  It returns its argument if
177*7c478bd9Sstevel@tonic-gate  * its argument is non-zero.
178*7c478bd9Sstevel@tonic-gate  * This prevents the same string
179*7c478bd9Sstevel@tonic-gate  * from being re-initialized.
180*7c478bd9Sstevel@tonic-gate  */
181*7c478bd9Sstevel@tonic-gate String *
mk_String(s)182*7c478bd9Sstevel@tonic-gate mk_String(s)
183*7c478bd9Sstevel@tonic-gate String *s;
184*7c478bd9Sstevel@tonic-gate {
185*7c478bd9Sstevel@tonic-gate 	if (s)
186*7c478bd9Sstevel@tonic-gate 		return (s);
187*7c478bd9Sstevel@tonic-gate 	s = (String *)malloc(STRING_START + sizeof (StringGuts) + 1);
188*7c478bd9Sstevel@tonic-gate 	if (s == NULL)
189*7c478bd9Sstevel@tonic-gate 		longjmp(jbuf, 1);
190*7c478bd9Sstevel@tonic-gate 	s->sg.start = s->sg.end = STRING_START/2;
191*7c478bd9Sstevel@tonic-gate 	s->sg.max = STRING_START;
192*7c478bd9Sstevel@tonic-gate 	s->data[s->sg.end] = '\0';
193*7c478bd9Sstevel@tonic-gate 	return (s);
194*7c478bd9Sstevel@tonic-gate }
195*7c478bd9Sstevel@tonic-gate 
196*7c478bd9Sstevel@tonic-gate void
free_String(s)197*7c478bd9Sstevel@tonic-gate free_String(s)
198*7c478bd9Sstevel@tonic-gate String *s;
199*7c478bd9Sstevel@tonic-gate {
200*7c478bd9Sstevel@tonic-gate 	if (s)
201*7c478bd9Sstevel@tonic-gate 		free(s);
202*7c478bd9Sstevel@tonic-gate }
203*7c478bd9Sstevel@tonic-gate 
204*7c478bd9Sstevel@tonic-gate /*
205*7c478bd9Sstevel@tonic-gate  * This function copies
206*7c478bd9Sstevel@tonic-gate  * c into s.
207*7c478bd9Sstevel@tonic-gate  * Used for initialization.
208*7c478bd9Sstevel@tonic-gate  */
209*7c478bd9Sstevel@tonic-gate String *
set_String(s,c)210*7c478bd9Sstevel@tonic-gate set_String(s, c)
211*7c478bd9Sstevel@tonic-gate String *s;
212*7c478bd9Sstevel@tonic-gate char *c;
213*7c478bd9Sstevel@tonic-gate {
214*7c478bd9Sstevel@tonic-gate 	int len = strlen(c)*2;
215*7c478bd9Sstevel@tonic-gate 	while (len > s->sg.max)
216*7c478bd9Sstevel@tonic-gate 		s = grow(s);
217*7c478bd9Sstevel@tonic-gate 	s->sg.start = s->sg.end = s->sg.max / 2;
218*7c478bd9Sstevel@tonic-gate 	s = app_String(s, c);
219*7c478bd9Sstevel@tonic-gate 	return (s);
220*7c478bd9Sstevel@tonic-gate }
221*7c478bd9Sstevel@tonic-gate 
222*7c478bd9Sstevel@tonic-gate /*
223*7c478bd9Sstevel@tonic-gate  * Chop n characters off the end of a string.
224*7c478bd9Sstevel@tonic-gate  * Return the truncated string.
225*7c478bd9Sstevel@tonic-gate  */
226*7c478bd9Sstevel@tonic-gate String *
trunc_String(String * s,int n)227*7c478bd9Sstevel@tonic-gate trunc_String(String *s, int n)
228*7c478bd9Sstevel@tonic-gate {
229*7c478bd9Sstevel@tonic-gate 	assert(n <= s->sg.end - s->sg.start);
230*7c478bd9Sstevel@tonic-gate 	s->sg.end -= n;
231*7c478bd9Sstevel@tonic-gate 	s->data[s->sg.end] = '\0';
232*7c478bd9Sstevel@tonic-gate 	return (s);
233*7c478bd9Sstevel@tonic-gate }
234