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