Skip to content

Latest commit

 

History

History
207 lines (186 loc) · 4.77 KB

stringbuf.c

File metadata and controls

207 lines (186 loc) · 4.77 KB
 
Nov 10, 2019
Nov 10, 2019
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
stringbuf: mimicking a bit of C++ to more safely handle strings
copyright 2006-10 by the mpg123 project - free software under the terms of the LGPL 2.1
see COPYING and AUTHORS files in distribution or http://mpg123.org
initially written by Thomas Orgis
*/
#include "mpg123lib_intern.h"
#include "config.h"
#include "mpg123.h"
#include "compat.h"
#include <string.h>
#include "debug.h"
void attribute_align_arg mpg123_init_string(mpg123_string* sb)
{
/* Handing in NULL here is a fatal mistake and rightfully so. */
sb->p = NULL;
sb->size = 0;
sb->fill = 0;
}
void attribute_align_arg mpg123_free_string(mpg123_string* sb)
{
if(!sb)
return;
if(sb->p != NULL) free(sb->p);
mpg123_init_string(sb);
}
int attribute_align_arg mpg123_grow_string(mpg123_string* sb, size_t new)
{
if(!sb)
return 0;
if(sb->size < new) return mpg123_resize_string(sb, new);
else return 1;
}
int attribute_align_arg mpg123_resize_string(mpg123_string* sb, size_t new)
{
if(!sb)
return 0;
debug3("resizing string pointer %p from %lu to %lu", (void*) sb->p, (unsigned long)sb->size, (unsigned long)new);
if(new == 0)
{
if(sb->size && sb->p != NULL) free(sb->p);
mpg123_init_string(sb);
return 1;
}
if(sb->size != new)
{
char* t;
debug("really!");
t = (char*) safe_realloc(sb->p, new*sizeof(char));
debug1("safe_realloc returned %p", (void*) t);
if(t != NULL)
{
sb->p = t;
sb->size = new;
Dec 29, 2019
Dec 29, 2019
61
62
63
64
65
66
if(sb->size < sb->fill)
{
// Cut short the existing data, properly.
sb->fill = sb->size;
sb->p[sb->fill-1] = 0;
}
Nov 10, 2019
Nov 10, 2019
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
return 1;
}
else return 0;
}
else return 1; /* success */
}
int attribute_align_arg mpg123_copy_string(mpg123_string* from, mpg123_string* to)
{
size_t fill;
char *text;
debug2("called copy_string with %p -> %p", (void*)from, (void*)to);
if(to == NULL)
return 0;
if(from == NULL)
{
fill = 0;
text = NULL;
}
else
{
fill = from->fill;
text = from->p;
}
if(mpg123_resize_string(to, fill))
{
if(fill) /* Avoid memcpy(NULL, NULL, 0) */
memcpy(to->p, text, fill);
to->fill = fill;
return 1;
}
else return 0;
}
int attribute_align_arg mpg123_add_string(mpg123_string* sb, const char* stuff)
{
debug1("adding %s", stuff);
return mpg123_add_substring(sb, stuff, 0, stuff ? strlen(stuff) : 0);
}
int attribute_align_arg mpg123_add_substring(mpg123_string *sb, const char *stuff, size_t from, size_t count)
{
debug("adding a substring");
if(!sb || !stuff)
return 0;
if(sb->fill) /* includes zero byte... */
{
if( (SIZE_MAX - sb->fill >= count) /* Avoid overflow. */
&& (sb->size >= sb->fill+count || mpg123_grow_string(sb, sb->fill+count)) )
{
memcpy(sb->p+sb->fill-1, stuff+from, count);
sb->fill += count;
sb->p[sb->fill-1] = 0; /* Terminate! */
}
else return 0;
}
else
{
if( count < SIZE_MAX && mpg123_grow_string(sb, count+1) )
{
memcpy(sb->p, stuff+from, count);
sb->fill = count+1;
sb->p[sb->fill-1] = 0; /* Terminate! */
}
else return 0;
}
return 1;
}
int attribute_align_arg mpg123_set_substring(mpg123_string* sb, const char* stuff, size_t from, size_t count)
{
if(!sb)
return 0;
sb->fill = 0;
return mpg123_add_substring(sb, stuff, from, count);
}
int attribute_align_arg mpg123_set_string(mpg123_string* sb, const char* stuff)
{
if(!sb)
return 0;
sb->fill = 0;
return mpg123_add_string(sb, stuff);
}
size_t attribute_align_arg mpg123_strlen(mpg123_string *sb, int utf8)
{
size_t i;
size_t bytelen;
/* Notions of empty string. If there's only a single character, it has to be the trailing zero, and if the first is the trailing zero anyway, we got empty. */
if(!sb || sb->fill < 2 || sb->p[0] == 0) return 0;
/* Find the first non-null character from the back.
We already established that the first character is non-null
That at fill-2 has to be null, though. */
for(i=sb->fill-2; i>0; --i)
if(sb->p[i] != 0) break;
/* For simple byte strings, we are done now. */
bytelen = i+1;
if(!utf8) return bytelen;
else
{
/* Work out the actual count of UTF8 bytes.
This employs no particular encoding error checking. */
size_t len = 0;
for(i=0; i<bytelen; ++i)
{
/* Every byte that is not a continuation byte ( 0xc0 == 10xx xxxx ) stands for a character. */
if((sb->p[i] & 0xc0) != 0x80) len++;
}
return len;
}
}
int attribute_align_arg mpg123_chomp_string(mpg123_string *sb)
{
ssize_t i;
if(!sb || !sb->fill) return 0;
/* Ensure that it is zero-terminated. */
sb->p[sb->fill-1] = 0;
for(i=sb->fill-2; i>=0; --i)
{
char *c = sb->p+i;
/* Stop at the first proper character. */
if(*c && *c != '\r' && *c != '\n') break;
else *c = 0;
}
/* initial fill at least 1, so i at least -1,
+2 means nothing happened for fill=1 .
With i=0, we got one non-null character, fill shall be 2
to accomodate the trailing zero. */
sb->fill = (size_t)i+2;
return 1;
}