Coverage Report

Created: 2024-05-21 06:29

/src/binutils-gdb/gas/sb.c
Line
Count
Source (jump to first uncovered line)
1
/* sb.c - string buffer manipulation routines
2
   Copyright (C) 1994-2024 Free Software Foundation, Inc.
3
4
   Written by Steve and Judy Chamberlain of Cygnus Support,
5
      sac@cygnus.com
6
7
   This file is part of GAS, the GNU Assembler.
8
9
   GAS is free software; you can redistribute it and/or modify
10
   it under the terms of the GNU General Public License as published by
11
   the Free Software Foundation; either version 3, or (at your option)
12
   any later version.
13
14
   GAS is distributed in the hope that it will be useful,
15
   but WITHOUT ANY WARRANTY; without even the implied warranty of
16
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
   GNU General Public License for more details.
18
19
   You should have received a copy of the GNU General Public License
20
   along with GAS; see the file COPYING.  If not, write to the Free
21
   Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
22
   02110-1301, USA.  */
23
24
#include "as.h"
25
#include "sb.h"
26
27
#include <limits.h>
28
#ifndef CHAR_BIT
29
#define CHAR_BIT 8
30
#endif
31
32
/* These routines are about manipulating strings.
33
34
   They are managed in things called `sb's which is an abbreviation
35
   for string buffers.  An sb has to be created, things can be glued
36
   on to it, and at the end of it's life it should be freed.  The
37
   contents should never be pointed at whilst it is still growing,
38
   since it could be moved at any time
39
40
   eg:
41
   sb_new (&foo);
42
   sb_grow... (&foo,...);
43
   use foo->ptr[*];
44
   sb_kill (&foo);  */
45
46
/* Buffers start at INIT_ALLOC size, and roughly double each time we
47
   go over the current allocation.  MALLOC_OVERHEAD is a guess at the
48
   system malloc overhead.  We aim to not waste any memory in the
49
   underlying page/chunk allocated by the system malloc.  */
50
204k
#define MALLOC_OVERHEAD (2 * sizeof (size_t))
51
163k
#define INIT_ALLOC (64 - MALLOC_OVERHEAD - 1)
52
53
static void sb_check (sb *, size_t);
54
55
/* Initializes an sb.  */
56
57
void
58
sb_build (sb *ptr, size_t size)
59
189k
{
60
189k
  ptr->ptr = XNEWVEC (char, size + 1);
61
189k
  ptr->max = size;
62
189k
  ptr->len = 0;
63
189k
}
64
65
void
66
sb_new (sb *ptr)
67
163k
{
68
163k
  sb_build (ptr, INIT_ALLOC);
69
163k
}
70
71
/* Deallocate the sb at ptr.  */
72
73
void
74
sb_kill (sb *ptr)
75
186k
{
76
186k
  free (ptr->ptr);
77
186k
}
78
79
/* Add the sb at s to the end of the sb at ptr.  */
80
81
void
82
sb_add_sb (sb *ptr, sb *s)
83
660k
{
84
660k
  sb_check (ptr, s->len);
85
660k
  memcpy (ptr->ptr + ptr->len, s->ptr, s->len);
86
660k
  ptr->len += s->len;
87
660k
}
88
89
/* Helper for sb_scrub_and_add_sb.  */
90
91
static sb *sb_to_scrub;
92
static char *scrub_position;
93
static size_t
94
scrub_from_sb (char *buf, size_t buflen)
95
17.1k
{
96
17.1k
  size_t copy;
97
17.1k
  copy = sb_to_scrub->len - (scrub_position - sb_to_scrub->ptr);
98
17.1k
  if (copy > buflen)
99
1.33k
    copy = buflen;
100
17.1k
  memcpy (buf, scrub_position, copy);
101
17.1k
  scrub_position += copy;
102
17.1k
  return copy;
103
17.1k
}
104
105
/* Run the sb at s through do_scrub_chars and add the result to the sb
106
   at ptr.  */
107
108
void
109
sb_scrub_and_add_sb (sb *ptr, sb *s)
110
18.9k
{
111
18.9k
  sb_to_scrub = s;
112
18.9k
  scrub_position = s->ptr;
113
114
  /* do_scrub_chars can expand text, for example when replacing
115
     # 123 "filename"
116
     with
117
     \t.linefile 123 "filename"
118
     or when replacing a 'c with the decimal ascii number for c.
119
     So we loop until the input S is consumed.  */
120
26.7k
  while (1)
121
26.7k
    {
122
26.7k
      size_t copy = s->len - (scrub_position - s->ptr) + do_scrub_pending ();
123
26.7k
      if (copy == 0)
124
18.9k
  break;
125
7.81k
      sb_check (ptr, copy);
126
7.81k
      ptr->len += do_scrub_chars (scrub_from_sb, ptr->ptr + ptr->len,
127
7.81k
          ptr->max - ptr->len);
128
7.81k
    }
129
130
18.9k
  sb_to_scrub = 0;
131
18.9k
  scrub_position = 0;
132
18.9k
}
133
134
/* Make sure that the sb at ptr has room for another len characters,
135
   and grow it if it doesn't.  */
136
137
static void
138
sb_check (sb *ptr, size_t len)
139
22.7M
{
140
22.7M
  size_t want = ptr->len + len;
141
142
22.7M
  if (want > ptr->max)
143
20.6k
    {
144
20.6k
      size_t max;
145
146
20.6k
      want += MALLOC_OVERHEAD + 1;
147
20.6k
      if ((ssize_t) want < 0)
148
0
  as_fatal ("string buffer overflow");
149
20.6k
#if GCC_VERSION >= 3004
150
20.6k
      max = (size_t) 1 << (CHAR_BIT * sizeof (want)
151
20.6k
         - (sizeof (want) <= sizeof (long)
152
20.6k
            ? __builtin_clzl ((long) want)
153
20.6k
            : __builtin_clzll ((long long) want)));
154
#else
155
      max = 128;
156
      while (want > max)
157
  max <<= 1;
158
#endif
159
20.6k
      max -= MALLOC_OVERHEAD + 1;
160
20.6k
      ptr->max = max;
161
20.6k
      ptr->ptr = XRESIZEVEC (char, ptr->ptr, max + 1);
162
20.6k
    }
163
22.7M
}
164
165
/* Make the sb at ptr point back to the beginning.  */
166
167
void
168
sb_reset (sb *ptr)
169
722k
{
170
722k
  ptr->len = 0;
171
722k
}
172
173
/* Add character c to the end of the sb at ptr.  */
174
175
void
176
sb_add_char (sb *ptr, size_t c)
177
21.9M
{
178
21.9M
  sb_check (ptr, 1);
179
21.9M
  ptr->ptr[ptr->len++] = c;
180
21.9M
}
181
182
/* Add null terminated string s to the end of sb at ptr.  */
183
184
void
185
sb_add_string (sb *ptr, const char *s)
186
10.6k
{
187
10.6k
  size_t len = strlen (s);
188
10.6k
  sb_check (ptr, len);
189
10.6k
  memcpy (ptr->ptr + ptr->len, s, len);
190
10.6k
  ptr->len += len;
191
10.6k
}
192
193
/* Add string at s of length len to sb at ptr */
194
195
void
196
sb_add_buffer (sb *ptr, const char *s, size_t len)
197
112k
{
198
112k
  sb_check (ptr, len);
199
112k
  memcpy (ptr->ptr + ptr->len, s, len);
200
112k
  ptr->len += len;
201
112k
}
202
203
/* Write terminating NUL and return string.  */
204
205
char *
206
sb_terminate (sb *in)
207
655k
{
208
655k
  in->ptr[in->len] = 0;
209
655k
  return in->ptr;
210
655k
}
211
212
/* Start at the index idx into the string in sb at ptr and skip
213
   whitespace. return the index of the first non whitespace character.  */
214
215
size_t
216
sb_skip_white (size_t idx, sb *ptr)
217
96.9k
{
218
97.8k
  while (idx < ptr->len
219
97.8k
   && (ptr->ptr[idx] == ' '
220
91.2k
       || ptr->ptr[idx] == '\t'))
221
912
    idx++;
222
96.9k
  return idx;
223
96.9k
}
224
225
/* Start at the index idx into the sb at ptr. skips whitespace,
226
   a comma and any following whitespace. returns the index of the
227
   next character.  */
228
229
size_t
230
sb_skip_comma (size_t idx, sb *ptr)
231
62.4k
{
232
68.9k
  while (idx < ptr->len
233
68.9k
   && (ptr->ptr[idx] == ' '
234
66.8k
       || ptr->ptr[idx] == '\t'))
235
6.54k
    idx++;
236
237
62.4k
  if (idx < ptr->len
238
62.4k
      && ptr->ptr[idx] == ',')
239
52.8k
    idx++;
240
241
62.4k
  while (idx < ptr->len
242
62.4k
   && (ptr->ptr[idx] == ' '
243
59.5k
       || ptr->ptr[idx] == '\t'))
244
1
    idx++;
245
246
62.4k
  return idx;
247
62.4k
}