summaryrefslogtreecommitdiff
path: root/src/core/string.c
blob: d43c571f3804d49fec8d73e5cf15f51c8858f92f (plain)
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/*
 * This file is part of libdom.
 * Licensed under the MIT License,
 *                http://www.opensource.org/licenses/mit-license.php
 * Copyright 2007 John-Mark Bell <jmb@netsurf-browser.org>
 */

#include <inttypes.h>
#include <string.h>

#include <dom/core/string.h>

#include "core/document.h"
#include "utils/utils.h"

/**
 * A DOM string
 *
 * DOM strings store either a pointer to allocated data, a pointer
 * to constant data or an offset into a document buffer.
 *
 * They are reference counted so freeing is performed correctly.
 */
struct dom_string {
	enum { DOM_STRING_PTR,
	       DOM_STRING_CONST_PTR,
	       DOM_STRING_OFFSET,
	       DOM_STRING_PTR_NODOC
	} type;				/**< String type */

	union {
		uint8_t *ptr;
		const uint8_t *cptr;
		uint32_t offset;
	} data;				/**< Type-specific data */

	size_t len;			/**< Byte length of string */

	union {
		struct dom_document *doc;	/**< Owning document */
		struct {
			dom_alloc alloc;	/**< Memory (de)allocation
						 * function */
			void *pw;	/**< Client-specific data */
		} nodoc;
	} ctx;				/**< Allocation context */

	uint32_t refcnt;		/**< Reference count */
};

static struct dom_string empty_string = { 
	.type = DOM_STRING_CONST_PTR, 
	.data.ptr = NULL,
	.len = 0,
	.ctx.doc = NULL,
	.refcnt = 1
};

/**
 * Claim a reference on a DOM string
 *
 * \param str  The string to claim a reference on
 */
void dom_string_ref(struct dom_string *str)
{
	str->refcnt++;
}

/**
 * Release a reference on a DOM string
 *
 * \param str  The string to release the reference from
 *
 * If the reference count reaches zero, any memory claimed by the
 * string will be released
 */
void dom_string_unref(struct dom_string *str)
{
	if (--str->refcnt == 0) {
		if (str->type == DOM_STRING_PTR_NODOC) {
			str->ctx.nodoc.alloc(str->data.ptr, 0,
					str->ctx.nodoc.pw);

			str->ctx.nodoc.alloc(str, 0, str->ctx.nodoc.pw);
		} else {
			if (str->type == DOM_STRING_PTR) {
				dom_document_alloc(str->ctx.doc,
						str->data.ptr, 0);
			}

			dom_document_alloc(str->ctx.doc, str, 0);
		}
	}
}

/**
 * Create a DOM string from an offset into the document buffer
 *
 * \param doc  The document in which the string resides
 * \param off  Offset from start of document buffer
 * \param len  Length, in bytes, of string
 * \param str  Pointer to location to receive pointer to new string
 * \return DOM_NO_ERR on success, DOM_NO_MEM_ERR on memory exhaustion
 *
 * The returned string will already be referenced, so there is no need
 * to explicitly reference it.
 */
dom_exception dom_string_create_from_off(struct dom_document *doc,
		uint32_t off, size_t len, struct dom_string **str)
{
	struct dom_string *ret;

	ret = dom_document_alloc(doc, NULL, sizeof(struct dom_string));
	if (ret == NULL)
		return DOM_NO_MEM_ERR;

	ret->type = DOM_STRING_OFFSET;

	ret->data.offset = off;

	ret->len = len;

	ret->ctx.doc = doc;

	ret->refcnt = 1;

	*str = ret;

	return DOM_NO_ERR;
}

/**
 * Create a DOM string from a string of characters
 *
 * \param doc  The document in which the string resides
 * \param ptr  Pointer to string of characters
 * \param len  Length, in bytes, of string of characters
 * \param str  Pointer to location to receive pointer to new string
 * \return DOM_NO_ERR on success, DOM_NO_MEM_ERR on memory exhaustion
 *
 * The returned string will already be referenced, so there is no need
 * to explicitly reference it.
 *
 * The string of characters passed in will be copied for use by the
 * returned DOM string.
 */
dom_exception dom_string_create_from_ptr(struct dom_document *doc,
		const uint8_t *ptr, size_t len, struct dom_string **str)
{
	struct dom_string *ret;

	ret = dom_document_alloc(doc, NULL, sizeof(struct dom_string));
	if (ret == NULL)
		return DOM_NO_MEM_ERR;

	ret->data.ptr = dom_document_alloc(doc, NULL, len);
	if (ret->data.ptr == NULL) {
		dom_document_alloc(doc, ret, 0);
		return DOM_NO_MEM_ERR;
	}

	ret->type = DOM_STRING_PTR;

	memcpy(ret->data.ptr, ptr, len);

	ret->len = len;

	ret->ctx.doc = doc;

	ret->refcnt = 1;

	*str = ret;

	return DOM_NO_ERR;
}

/**
 * Create a DOM string from a constant string of characters
 *
 * \param doc  The document in which the string resides
 * \param ptr  Pointer to string of characters
 * \param len  Length, in bytes, of string of characters
 * \param str  Pointer to location to receive pointer to new string
 * \return DOM_NO_ERR on success, DOM_NO_MEM_ERR on memory exhaustion
 *
 * The returned string will already be referenced, so there is no need
 * to explicitly reference it.
 *
 * The string of characters passed in will _not_ be copied for use by the
 * returned DOM string.
 */
dom_exception dom_string_create_from_const_ptr(struct dom_document *doc,
		const uint8_t *ptr, size_t len, struct dom_string **str)
{
	struct dom_string *ret;

	ret = dom_document_alloc(doc, NULL, sizeof(struct dom_string));
	if (ret == NULL)
		return DOM_NO_MEM_ERR;

	ret->type = DOM_STRING_CONST_PTR;

	ret->data.cptr = ptr;

	ret->len = len;

	ret->ctx.doc = doc;

	ret->refcnt = 1;

	*str = ret;

	return DOM_NO_ERR;
}

/**
 * Create a DOM string from a string of characters that does not belong
 * to a document
 *
 * \param alloc  Memory (de)allocation function
 * \param pw     Pointer to client-specific private data
 * \param ptr    Pointer to string of characters
 * \param len    Length, in bytes, of string of characters
 * \param str    Pointer to location to receive result
 * \return DOM_NO_ERR on success, DOM_NO_MEM_ERR on memory exhaustion
 *
 * The returned string will already be referenced, so there is no need
 * to explicitly reference it.
 *
 * The string of characters passed in will be copied for use by the
 * returned DOM string.
 */
dom_exception dom_string_create_from_ptr_no_doc(dom_alloc alloc, void *pw,
		const uint8_t *ptr, size_t len, struct dom_string **str)
{
	struct dom_string *ret;

	ret = alloc(NULL, sizeof(struct dom_string), pw);
	if (ret == NULL)
		return DOM_NO_MEM_ERR;

	ret->data.ptr = alloc(NULL, len, pw);
	if (ret->data.ptr == NULL) {
		alloc(ret, 0, pw);
		return DOM_NO_MEM_ERR;
	}

	ret->type = DOM_STRING_PTR_NODOC;

	memcpy(ret->data.ptr, ptr, len);

	ret->len = len;

	ret->ctx.nodoc.alloc = alloc;
	ret->ctx.nodoc.pw = pw;

	ret->refcnt = 1;

	*str = ret;

	return DOM_NO_ERR;
}

/**
 * Get a pointer to the string of characters within a DOM string
 *
 * \param str   Pointer to DOM string to retrieve pointer from
 * \param data  Pointer to location to receive data
 * \param len   Pointer to location to receive byte length of data
 * \return DOM_NO_ERR on success
 *
 * The caller must have previously claimed a reference on the DOM string.
 * The returned pointer must not be freed.
 */
dom_exception dom_string_get_data(struct dom_string *str,
		const uint8_t **data, size_t *len)
{
	/* Assume that a NULL str pointer indicates the empty string */
	if (str == NULL)
		str = &empty_string;

	switch (str->type) {
	case DOM_STRING_PTR:
		*data = str->data.ptr;
		break;
	case DOM_STRING_CONST_PTR:
		*data = str->data.cptr;
		break;
	case DOM_STRING_OFFSET:
		*data = dom_document_get_base(str->ctx.doc) +
				str->data.offset;
		break;
	case DOM_STRING_PTR_NODOC:
		*data = str->data.ptr;
		break;
	}

	*len = str->len;

	return DOM_NO_ERR;
}

/**
 * Case sensitively compare two DOM strings
 *
 * \param s1  The first string to compare
 * \param s2  The second string to compare
 * \return 0 if strings match, non-0 otherwise
 *
 * NULL and "" will match.
 */
int dom_string_cmp(struct dom_string *s1, struct dom_string *s2)
{
	const uint8_t *d1 = NULL;
	const uint8_t *d2 = NULL;
	size_t l1, l2;
	dom_exception err;

	err = dom_string_get_data(s1, &d1, &l1);
	if (err != DOM_NO_ERR)
		return 1; /* arbitrary */

	err = dom_string_get_data(s2, &d2, &l2);
	if (err != DOM_NO_ERR)
		return 1; /* arbitrary */

	if (l1 != l2)
		return 1; /* arbitrary */

	return strncmp((const char *) d1, (const char *) d2, l1);
}

/**
 * Case insensitively compare two DOM strings
 *
 * \param s1  The first string to compare
 * \param s2  The second string to compare
 * \return 0 if strings match, non-0 otherwise
 *
 * NULL and "" will match.
 */
int dom_string_icmp(struct dom_string *s1, struct dom_string *s2)
{
	const uint8_t *d1 = NULL;
	const uint8_t *d2 = NULL;
	size_t l1, l2;
	dom_exception err;

	err = dom_string_get_data(s1, &d1, &l1);
	if (err != DOM_NO_ERR)
		return 1; /* arbitrary */

	err = dom_string_get_data(s2, &d2, &l2);
	if (err != DOM_NO_ERR)
		return 1; /* arbitrary */

	if (l1 != l2)
		return 1; /* arbitrary */

	return strncasecmp((const char *) d1, (const char *) d2, l1);
}