1 #ifndef EL__UTIL_STRING_H
2 #define EL__UTIL_STRING_H
4 /* To these two functions, same remark applies as to copy_string() or
10 #include "osdep/ascii.h"
11 #include "util/error.h"
12 #include "util/lists.h"
13 #include "util/memdebug.h"
14 #include "util/memory.h"
19 /* Autoallocation string constructors: */
21 /* Note that, contrary to the utilities using the string struct, these
22 * functions are NOT granular, thus you can't simply reuse strings allocated by
23 * these in add_to_string()-style functions. */
25 /* Allocates NUL terminated string with @len bytes from @src.
26 * If @src == NULL or @len < 0 only one byte is allocated and set it to 0. */
27 /* Returns the string or NULL on allocation failure. */
28 unsigned char *memacpy(const unsigned char *src
, int len
);
30 /* Allocated NUL terminated string with the content of @src. */
31 unsigned char *stracpy(const unsigned char *src
);
33 #else /* DEBUG_MEMLEAK */
35 unsigned char *debug_memacpy(const unsigned char *, int, const unsigned char *, int);
36 #define memacpy(s, l) debug_memacpy(__FILE__, __LINE__, s, l)
38 unsigned char *debug_stracpy(const unsigned char *, int, const unsigned char *);
39 #define stracpy(s) debug_stracpy(__FILE__, __LINE__, s)
41 #endif /* DEBUG_MEMLEAK */
44 /* Concatenates @src to @str. */
45 /* If reallocation of @str fails @str is not touched. */
46 void add_to_strn(unsigned char **str
, unsigned char *src
);
48 /* Inserts @seqlen chars from @seq at position @pos in the @dst string. */
49 /* If reallocation of @dst fails it is not touched and NULL is returned. */
51 insert_in_string(unsigned char **dst
, int pos
, unsigned char *seq
, int seqlen
);
53 /* Takes a list of strings where the last parameter _must_ be NULL and
54 * concatenates them. */
55 /* Returns the allocated string or NULL on allocation failure. */
57 * unsigned char *abc = straconcat("A", "B", "C", NULL);
59 * printf("%s", abc); -> print "ABC"
60 * mem_free(abc); -> free memory used by @abc */
61 unsigned char *straconcat(unsigned char *str
, ...);
64 /* Misc. utility string functions. */
66 /* Compare two strings, handling correctly @s1 or @s2 being NULL. */
67 int xstrcmp(unsigned char *s1
, unsigned char *s2
);
69 /* Copies at most @len chars into @dst. Ensures null termination of @dst. */
70 unsigned char *safe_strncpy(unsigned char *dst
, const unsigned char *src
, size_t len
);
72 /* strlcmp() is the middle child of history, everyone is using it differently.
73 * On some weird *systems* it seems to be defined (equivalent to strcasecmp()),
74 * so we'll better use our #define redir. */
76 /* This routine compares string @s1 of length @n1 with string @s2 of length
79 * This acts identically to strcmp() but for non-zero-terminated strings,
80 * rather than being similiar to strncmp(). That means, it fails if @n1 != @n2,
81 * thus you may use it for testing whether @s2 matches *full* @s1, not only its
82 * start (which can be a security hole, ie. in the cookies domain checking).
84 * @n1 or @n2 may be -1, which is same as strlen(@s[12]) but possibly more
85 * effective (in the future ;-). */
86 /* Returns zero if the strings match or undefined non-zero value if they
87 * differ. (The non-zero return value is _not_ same as for the standard
88 * strcmp() family.) */
89 #define strlcmp(a,b,c,d) (errfile = __FILE__, errline = __LINE__, elinks_strlcmp(a,b,c,d))
90 int elinks_strlcmp(const unsigned char *s1
, size_t n1
,
91 const unsigned char *s2
, size_t n2
);
93 /* Acts identically to strlcmp(), except for being case insensitive. */
94 #define strlcasecmp(a,b,c,d) (errfile = __FILE__, errline = __LINE__, elinks_strlcasecmp(a,b,c,d))
95 int elinks_strlcasecmp(const unsigned char *s1
, size_t n1
,
96 const unsigned char *s2
, size_t n2
);
99 #define skip_space(S) \
100 do { while (isspace(*(S))) (S)++; } while (0)
102 #define skip_nonspace(S) \
103 do { while (*(S) && !isspace(*(S))) (S)++; } while (0)
105 #define isquote(c) ((c) == '"' || (c) == '\'')
106 #define isasciialpha(c) (((c) >= 'A' && (c) <= 'Z') || ((c) >= 'a' && (c) <= 'z'))
107 #define isasciialnum(c) (isasciialpha(c) || isdigit(c))
108 #define isident(c) (isasciialnum(c) || (c) == '_' || (c) == '-')
110 /* Char is safe to write to the terminal screen */
111 #define isscreensafe(c) ((c) >= ' ' && (c) != ASCII_DEL)
114 /* String debugging using magic number, it may catch some errors. */
123 unsigned char *source
;
128 /* The granularity used for the struct string based utilities. */
129 #define STRING_GRANULARITY 0xFF
132 #define STRING_MAGIC 0x2E5BF271
133 #define check_string_magic(x) assertm((x)->magic == STRING_MAGIC, "String magic check failed.")
134 #define set_string_magic(x) do { (x)->magic = STRING_MAGIC; } while (0)
135 #define NULL_STRING { STRING_MAGIC, NULL, 0 }
136 #define INIT_STRING(s, l) { STRING_MAGIC, s, l }
138 #define check_string_magic(x)
139 #define set_string_magic(x)
140 #define NULL_STRING { NULL, 0 }
141 #define INIT_STRING(s, l) { s, l }
144 /* Initializes the passed string struct by preallocating the @source member. */
146 struct string
*init_string__(unsigned char *file
, int line
, struct string
*string
);
147 #define init_string(string) init_string__(__FILE__, __LINE__, string)
149 struct string
*init_string(struct string
*string
);
152 /* Resets @string and free()s the @source member. */
153 void done_string(struct string
*string
);
156 struct string
*add_to_string(struct string
*string
,
157 const unsigned char *source
);
158 struct string
*add_char_to_string(struct string
*string
, unsigned char character
);
159 struct string
*add_string_to_string(struct string
*to
, struct string
*from
);
160 struct string
*add_file_to_string(struct string
*string
, unsigned char *filename
);
161 struct string
*add_crlf_to_string(struct string
*string
);
163 /* Adds each C string to @string until a terminating NULL is met. */
164 struct string
*string_concat(struct string
*string
, ...);
166 /* Extends the string with @times number of @character. */
167 struct string
*add_xchar_to_string(struct string
*string
, unsigned char character
, int times
);
169 /* Add printf-style format string to @string. */
170 struct string
*add_format_to_string(struct string
*string
, unsigned char *format
, ...);
172 /* Get a regular newly allocated stream of bytes from @string. */
173 static unsigned char *squeezastring(struct string
*string
);
176 static inline unsigned char *
177 squeezastring(struct string
*string
)
179 return memacpy(string
->source
, string
->length
);
183 #undef realloc_string
185 #define realloc_string(str, size) \
186 mem_align_alloc(&(str)->source, (str)->length, (size) + 1, \
187 unsigned char, STRING_GRANULARITY)
191 #define add_bytes_to_string(string, bytes, length) \
192 add_bytes_to_string__(__FILE__, __LINE__, string, bytes, length)
194 #define debug_realloc_string(str, size) \
195 mem_align_alloc__(file, line, (void **) &(str)->source, (str)->length, (size) + 1, \
196 sizeof(unsigned char), STRING_GRANULARITY)
200 #define add_bytes_to_string(string, bytes, length) \
201 add_bytes_to_string__(string, bytes, length)
203 #define debug_realloc_string(str, size) realloc_string(str, size)
207 static inline struct string
*
208 add_bytes_to_string__(
210 unsigned char *file
, int line
,
212 struct string
*string
, const unsigned char *bytes
,
217 assertm(string
&& bytes
&& length
>= 0, "[add_bytes_to_string]");
218 if_assert_failed
{ return NULL
; }
220 check_string_magic(string
);
222 if (length
== 0) return string
;
224 newlength
= string
->length
+ length
;
225 if (!debug_realloc_string(string
, newlength
))
228 memcpy(string
->source
+ string
->length
, bytes
, length
);
229 string
->source
[newlength
] = 0;
230 string
->length
= newlength
;
236 struct string_list_item
{
237 LIST_HEAD(struct string_list_item
);
239 struct string string
;
242 /* Adds @string with @length chars to the list. If length is -1 it will be set
243 * to the return value of strlen(). */
245 add_to_string_list(struct list_head
*list
, const unsigned char *string
,
248 void free_string_list(struct list_head
*list
);
251 /* Returns an empty C string or @str if different from NULL. */
252 #define empty_string_or_(str) ((str) ? (unsigned char *) (str) : (unsigned char *) "")
254 /* Allocated copy if not NULL or returns NULL. */
255 #define null_or_stracpy(str) ((str) ? stracpy(str) : NULL)
257 #endif /* EL__UTIL_STRING_H */