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
61
62
63
64
65
66
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
|
/*
* Auto-sizing memory containers
* Copyright Jan Engelhardt, 2002-2011
*
* This file is part of libHX. libHX is free software; you can
* redistribute it and/or modify it under the terms of the GNU Lesser
* General Public License as published by the Free Software Foundation;
* either version 2.1 or (at your option) any later version.
*/
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libHX/string.h>
#include "internal.h"
static __inline__ size_t __HXmc_request(size_t len)
{
/* The container, data portion, and a trailing \0 */
return sizeof(struct memcont) + len + 1;
}
static __inline__ void HXmc_check(const struct memcont *c)
{
if (c->id != HXMC_IDENT)
fprintf(stderr, "libHX-mc error: not a hxmc object!\n");
}
static __inline__ struct memcont *HXmc_base(const hxmc_t *p)
{
return containerof(p, struct memcont, data);
}
EXPORT_SYMBOL hxmc_t *HXmc_strinit(const char *s)
{
hxmc_t *t = NULL;
size_t z = strlen(s);
if (z < 23 && HXmc_memcpy(&t, NULL, 23) == NULL)
return NULL;
return HXmc_memcpy(&t, s, z);
}
EXPORT_SYMBOL hxmc_t *HXmc_meminit(const void *ptr, size_t len)
{
hxmc_t *t = NULL;
return HXmc_memcpy(&t, ptr, len);
}
EXPORT_SYMBOL hxmc_t *HXmc_strcpy(hxmc_t **vp, const char *s)
{
if (s == NULL) {
HXmc_free(*vp);
*vp = NULL;
return NULL;
}
return HXmc_memcpy(vp, s, strlen(s));
}
EXPORT_SYMBOL hxmc_t *HXmc_memcpy(hxmc_t **vp, const void *ptr, size_t len)
{
struct memcont *ctx;
if (*vp != NULL) {
ctx = HXmc_base(*vp);
HXmc_check(ctx);
if (ctx->alloc < len) {
ctx = realloc(ctx, __HXmc_request(len));
if (ctx == NULL)
return NULL;
ctx->alloc = len;
}
} else {
ctx = malloc(__HXmc_request(len));
if (ctx == NULL)
return NULL;
ctx->id = HXMC_IDENT;
ctx->alloc = len;
}
if (ptr == NULL) {
ctx->length = 0;
ctx->data[0] = '\0';
// coverity[leaked_storage]
return *vp = ctx->data;
}
memcpy(ctx->data, ptr, ctx->length = len);
ctx->data[len] = '\0';
// coverity[leaked_storage]
return *vp = ctx->data;
}
EXPORT_SYMBOL size_t HXmc_length(const hxmc_t *vp)
{
const struct memcont *ctx;
if (vp == NULL)
return 0;
ctx = HXmc_base(vp);
HXmc_check(ctx);
return ctx->length;
}
EXPORT_SYMBOL hxmc_t *HXmc_setlen(hxmc_t **vp, size_t len)
{
struct memcont *ctx;
if (HXmc_trunc(vp, len) == NULL)
return NULL;
ctx = HXmc_base(*vp);
ctx->length = len;
return *vp;
}
EXPORT_SYMBOL hxmc_t *HXmc_trunc(hxmc_t **vp, size_t len)
{
struct memcont *ctx = HXmc_base(*vp);
HXmc_check(ctx);
if (len > ctx->alloc) {
ctx = realloc(ctx, __HXmc_request(len));
if (ctx == NULL)
return NULL;
ctx->alloc = len;
} else {
ctx->data[len] = '\0';
ctx->length = len;
}
// coverity[leaked_storage]
return *vp = ctx->data;
}
EXPORT_SYMBOL hxmc_t *HXmc_strcat(hxmc_t **vp, const char *s)
{
if (s == NULL)
return *vp;
return HXmc_memcat(vp, s, strlen(s));
}
EXPORT_SYMBOL hxmc_t *HXmc_memcat(hxmc_t **vp, const void *ptr, size_t len)
{
struct memcont *ctx = HXmc_base(*vp);
size_t nl = ctx->length + len;
HXmc_check(ctx);
if (nl > ctx->alloc) {
ctx = realloc(ctx, __HXmc_request(nl));
if (ctx == NULL)
return NULL;
ctx->alloc = nl;
}
if (ptr == NULL)
// coverity[leaked_storage]
return *vp = ctx->data;
memcpy(ctx->data + ctx->length, ptr, len);
ctx->length = nl;
ctx->data[nl] = '\0';
// coverity[leaked_storage]
return *vp = ctx->data;
}
EXPORT_SYMBOL hxmc_t *HXmc_strpcat(hxmc_t **vp, const char *s)
{
/* Prepend string @s to @*vp */
if (s == NULL)
return *vp;
return HXmc_memins(vp, 0, s, strlen(s));
}
EXPORT_SYMBOL hxmc_t *HXmc_mempcat(hxmc_t **vp, const void *ptr, size_t len)
{
/* Prepend memory @ptr (of length @len) to @*vp */
return HXmc_memins(vp, 0, ptr, len);
}
EXPORT_SYMBOL hxmc_t *HXmc_strins(hxmc_t **vp, size_t pos, const char *s)
{
if (s == NULL)
return *vp;
return HXmc_memins(vp, pos, s, strlen(s));
}
/*
* We naturally do not support negative positions like some
* scripting languages do, hence @pos is unsigned.
*/
EXPORT_SYMBOL hxmc_t *HXmc_memins(hxmc_t **vp, size_t pos, const void *ptr,
size_t len)
{
struct memcont *ctx = HXmc_base(*vp);
size_t nl = ctx->length + len;
HXmc_check(ctx);
if (ctx->alloc < nl) {
ctx = realloc(ctx, __HXmc_request(nl));
if (ctx == NULL)
return NULL;
ctx->alloc = nl;
}
if (ptr == NULL)
return *vp = ctx->data;
memmove(ctx->data + pos + len, ctx->data + pos, ctx->length - pos);
memcpy(ctx->data + pos, ptr, len);
ctx->length += len;
ctx->data[ctx->length] = '\0';
// coverity[leaked_storage]
return *vp = ctx->data;
}
EXPORT_SYMBOL hxmc_t *HXmc_memdel(hxmc_t *vp, size_t pos, size_t len)
{
struct memcont *ctx = HXmc_base(vp);
HXmc_check(ctx);
if (pos + len > ctx->length)
len = ctx->length - pos;
memmove(ctx->data + pos, ctx->data + pos + len,
ctx->length - pos - len);
ctx->length -= len;
ctx->data[ctx->length] = '\0';
return ctx->data;
}
EXPORT_SYMBOL void HXmc_free(hxmc_t *vp)
{
struct memcont *ctx;
if (vp == NULL)
return;
ctx = HXmc_base(vp);
HXmc_check(ctx);
free(ctx);
}
EXPORT_SYMBOL void HXmc_zvecfree(hxmc_t **args)
{
hxmc_t **travp;
for (travp = args; *travp != NULL; ++travp)
HXmc_free(*travp);
free(args);
}
|