Apache Portable Runtime
Loading...
Searching...
No Matches
apr_strings.h
Go to the documentation of this file.
1/* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/* Portions of this file are covered by */
18/* -*- mode: c; c-file-style: "k&r" -*-
19
20 strnatcmp.c -- Perform 'natural order' comparisons of strings in C.
21 Copyright (C) 2000 by Martin Pool <mbp@humbug.org.au>
22
23 This software is provided 'as-is', without any express or implied
24 warranty. In no event will the authors be held liable for any damages
25 arising from the use of this software.
26
27 Permission is granted to anyone to use this software for any purpose,
28 including commercial applications, and to alter it and redistribute it
29 freely, subject to the following restrictions:
30
31 1. The origin of this software must not be misrepresented; you must not
32 claim that you wrote the original software. If you use this software
33 in a product, an acknowledgment in the product documentation would be
34 appreciated but is not required.
35 2. Altered source versions must be plainly marked as such, and must not be
36 misrepresented as being the original software.
37 3. This notice may not be removed or altered from any source distribution.
38*/
39
40#ifndef APR_STRINGS_H
41#define APR_STRINGS_H
42
43/**
44 * @file apr_strings.h
45 * @brief APR Strings library
46 */
47
48#include "apr.h"
49#include "apr_errno.h"
50#include "apr_pools.h"
51#define APR_WANT_IOVEC
52#include "apr_want.h"
53
54#if APR_HAVE_STDARG_H
55#include <stdarg.h>
56#endif
57
58#ifdef __cplusplus
59extern "C" {
60#endif /* __cplusplus */
61
62/**
63 * @defgroup apr_strings String routines
64 * @ingroup APR
65 * @{
66 */
67
68/**
69 * Do a natural order comparison of two strings.
70 * @param a The first string to compare
71 * @param b The second string to compare
72 * @return Either <0, 0, or >0. If the first string is less than the second
73 * this returns <0, if they are equivalent it returns 0, and if the
74 * first string is greater than second string it retuns >0.
75 */
76APR_DECLARE(int) apr_strnatcmp(char const *a, char const *b);
77
78/**
79 * Do a natural order comparison of two strings ignoring the case of the
80 * strings.
81 * @param a The first string to compare
82 * @param b The second string to compare
83 * @return Either <0, 0, or >0. If the first string is less than the second
84 * this returns <0, if they are equivalent it returns 0, and if the
85 * first string is greater than second string it retuns >0.
86 */
87APR_DECLARE(int) apr_strnatcasecmp(char const *a, char const *b);
88
89/**
90 * duplicate a string into memory allocated out of a pool
91 * @param p The pool to allocate out of
92 * @param s The string to duplicate
93 * @return The new string or NULL if s == NULL
94 */
95APR_DECLARE(char *) apr_pstrdup(apr_pool_t *p, const char *s);
96
97/**
98 * Create a null-terminated string by making a copy of a sequence
99 * of characters and appending a null byte
100 * @param p The pool to allocate out of
101 * @param s The block of characters to duplicate
102 * @param n The number of characters to duplicate
103 * @return The new string or NULL if s == NULL
104 * @remark This is a faster alternative to apr_pstrndup(), for use
105 * when you know that the string being duplicated really
106 * has 'n' or more characters. If the string might contain
107 * fewer characters, use apr_pstrndup().
108 */
109APR_DECLARE(char *) apr_pstrmemdup(apr_pool_t *p, const char *s, apr_size_t n)
110#if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
111 __attribute__((alloc_size(3)))
112#endif
113 ;
114
115/**
116 * Duplicate at most n characters of a string into memory allocated
117 * out of a pool; the new string will be NUL-terminated
118 * @param p The pool to allocate out of
119 * @param s The string to duplicate
120 * @param n The maximum number of characters to duplicate
121 * @return The new string or NULL if s == NULL
122 * @remark The amount of memory allocated from the pool is the length
123 * of the returned string including the NUL terminator
124 */
125APR_DECLARE(char *) apr_pstrndup(apr_pool_t *p, const char *s, apr_size_t n);
126
127/**
128 * Duplicate a block of memory.
129 *
130 * @param p The pool to allocate from
131 * @param m The memory to duplicate
132 * @param n The number of bytes to duplicate
133 * @return The new block of memory or NULL if m == NULL
134 */
135APR_DECLARE(void *) apr_pmemdup(apr_pool_t *p, const void *m, apr_size_t n)
136#if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
137 __attribute__((alloc_size(3)))
138#endif
139 ;
140
141/**
142 * Concatenate multiple strings, allocating memory out a pool
143 * @param p The pool to allocate out of
144 * @param ... The strings to concatenate. The final string must be NULL
145 * @return The new string
146 */
148#if defined(__GNUC__) && __GNUC__ >= 4
149 __attribute__((sentinel))
150#endif
151 ;
152
153/**
154 * Concatenate multiple strings specified in a writev-style vector
155 * @param p The pool from which to allocate
156 * @param vec The strings to concatenate
157 * @param nvec The number of strings to concatenate
158 * @param nbytes (output) strlen of new string (pass in NULL to omit)
159 * @return The new string
160 */
161APR_DECLARE(char *) apr_pstrcatv(apr_pool_t *p, const struct iovec *vec,
162 apr_size_t nvec, apr_size_t *nbytes);
163
164/**
165 * printf-style style printing routine. The data is output to a string
166 * allocated from a pool
167 * @param p The pool to allocate out of
168 * @param fmt The format of the string
169 * @param ap The arguments to use while printing the data
170 * @return The new string
171 */
172APR_DECLARE(char *) apr_pvsprintf(apr_pool_t *p, const char *fmt, va_list ap);
173
174/**
175 * printf-style style printing routine. The data is output to a string
176 * allocated from a pool
177 * @param p The pool to allocate out of
178 * @param fmt The format of the string
179 * @param ... The arguments to use while printing the data
180 * @return The new string
181 */
182APR_DECLARE_NONSTD(char *) apr_psprintf(apr_pool_t *p, const char *fmt, ...)
183 __attribute__((format(printf,2,3)));
184
185/**
186 * zero out the buffer provided, without being optimized out by
187 * the compiler.
188 *
189 * @param buffer buffer to zero out
190 * @param size size of the buffer to zero out
191 * @return APR_SUCCESS or an errno
192 */
193APR_DECLARE(apr_status_t) apr_memzero_explicit(void *buffer, apr_size_t size);
194
195/**
196 * Check whether two buffers of equal size have the same content, using a
197 * constant time algorithm (branch-less with regard to the content of the
198 * buffers and an execution time solely dependent on the number of bytes
199 * compared, not the bytes themselves).
200 *
201 * @param buf1 first buffer to compare
202 * @param buf2 second buffer to compare
203 * @param n number of bytes to compare
204 * @return 1 if equal, 0 otherwise
205 */
206APR_DECLARE(int) apr_memeq_timingsafe(const void *buf1, const void *buf2,
207 apr_size_t n);
208
209/**
210 * Check whether two NUL-terminated strings have the same content, using a
211 * constant time algorithm (branch-less with regard to the content of the
212 * secret string and an execution time solely dependent on the length of
213 * the non-secret string). The secret string of the two should be set in
214 * the first parameter \c sec1 to avoid leaking its length.
215 *
216 * @param sec1 first string to compare (the secret one)
217 * @param str2 second string to compare
218 * @return 1 if equal, 0 otherwise
219 * @remark The function will compare as much characters as there are in
220 * \c str2, so the length of \c str2 might leak through side channel,
221 * while the length of \c sec1 does not.
222 */
223APR_DECLARE(int) apr_streq_timingsafe(const char *sec1, const char *str2);
224
225/**
226 * Check whether two NUL-terminated strings have the same content, up to \c n
227 * characters, using a constant time algorithm (branch-less with regard to the
228 * content of the secret string and an execution time solely dependent on the
229 * length of the non-secret string or \c n). The secret string of the two
230 * should be set in the first parameter \c sec1 to avoid leaking its length.
231 *
232 * @param sec1 secret string to compare
233 * @param str2 string to compare with
234 * @param n max number of characters to compare
235 * @return 1 if equal, 0 otherwise
236 * @remark The function will compare as much characters as there are in
237 * \c str2 if it's less than \c n, so the length of \c str2 might
238 * leak through side channel, while the length of \c sec1 does not.
239 */
240APR_DECLARE(int) apr_strneq_timingsafe(const char *sec1, const char *str2,
241 apr_size_t n);
242
243/**
244 * Copy up to dst_size characters from src to dst; does not copy
245 * past a NUL terminator in src, but always terminates dst with a NUL
246 * regardless.
247 * @param dst The destination string
248 * @param src The source string
249 * @param dst_size The space available in dst; dst always receives
250 * NUL termination, so if src is longer than
251 * dst_size, the actual number of characters copied is
252 * dst_size - 1.
253 * @return Pointer to the NUL terminator of the destination string, dst
254 * @remark
255 * <PRE>
256 * Note the differences between this function and strncpy():
257 * 1) strncpy() doesn't always NUL terminate; apr_cpystrn() does.
258 * 2) strncpy() pads the destination string with NULs, which is often
259 * unnecessary; apr_cpystrn() does not.
260 * 3) strncpy() returns a pointer to the beginning of the dst string;
261 * apr_cpystrn() returns a pointer to the NUL terminator of dst,
262 * to allow a check for truncation.
263 * </PRE>
264 */
265APR_DECLARE(char *) apr_cpystrn(char *dst, const char *src,
266 apr_size_t dst_size);
267
268/**
269 * Remove all whitespace from a string
270 * @param dest The destination string. It is okay to modify the string
271 * in place. Namely dest == src
272 * @param src The string to rid the spaces from.
273 * @return A pointer to the destination string's null terminator.
274 */
275APR_DECLARE(char *) apr_collapse_spaces(char *dest, const char *src);
276
277/**
278 * Convert the arguments to a program from one string to an array of
279 * strings terminated by a NULL pointer
280 * @param arg_str The arguments to convert
281 * @param argv_out Output location. This is a pointer to an array of strings.
282 * @param token_context Pool to use.
283 */
285 char ***argv_out,
286 apr_pool_t *token_context);
287
288/**
289 * Split a string into separate null-terminated tokens. The tokens are
290 * delimited in the string by one or more characters from the sep
291 * argument.
292 * @param str The string to separate; this should be specified on the
293 * first call to apr_strtok() for a given string, and NULL
294 * on subsequent calls.
295 * @param sep The set of delimiters
296 * @param last State saved by apr_strtok() between calls.
297 * @return The next token from the string
298 * @note the 'last' state points to the trailing NUL char of the final
299 * token, otherwise it points to the character following the current
300 * token (all successive or empty occurances of sep are skiped on the
301 * subsequent call to apr_strtok). Therefore it is possible to avoid
302 * a strlen() determination, with the following logic;
303 * toklen = last - retval; if (*last) --toklen;
304 */
305APR_DECLARE(char *) apr_strtok(char *str, const char *sep, char **last);
306
307/**
308 * @defgroup APR_Strings_Snprintf snprintf implementations
309 * @warning
310 * These are snprintf implementations based on apr_vformatter().
311 *
312 * Note that various standards and implementations disagree on the return
313 * value of snprintf, and side-effects due to %n in the formatting string.
314 * apr_snprintf (and apr_vsnprintf) behaves as follows:
315 *
316 * Process the format string until the entire string is exhausted, or
317 * the buffer fills. If the buffer fills then stop processing immediately
318 * (so no further %n arguments are processed), and return the buffer
319 * length. In all cases the buffer is NUL terminated. It will return the
320 * number of characters inserted into the buffer, not including the
321 * terminating NUL. As a special case, if len is 0, apr_snprintf will
322 * return the number of characters that would have been inserted if
323 * the buffer had been infinite (in this case, *buffer can be NULL)
324 *
325 * In no event does apr_snprintf return a negative number.
326 * @{
327 */
328
329/**
330 * snprintf routine based on apr_vformatter. This means it understands the
331 * same extensions.
332 * @param buf The buffer to write to
333 * @param len The size of the buffer
334 * @param format The format string
335 * @param ... The arguments to use to fill out the format string.
336 */
337APR_DECLARE_NONSTD(int) apr_snprintf(char *buf, apr_size_t len,
338 const char *format, ...)
339 __attribute__((format(printf,3,4)));
340
341/**
342 * vsnprintf routine based on apr_vformatter. This means it understands the
343 * same extensions.
344 * @param buf The buffer to write to
345 * @param len The size of the buffer
346 * @param format The format string
347 * @param ap The arguments to use to fill out the format string.
348 */
349APR_DECLARE(int) apr_vsnprintf(char *buf, apr_size_t len, const char *format,
350 va_list ap);
351/** @} */
352
353/**
354 * create a string representation of an int, allocated from a pool
355 * @param p The pool from which to allocate
356 * @param n The number to format
357 * @return The string representation of the number
358 */
359APR_DECLARE(char *) apr_itoa(apr_pool_t *p, int n);
360
361/**
362 * create a string representation of a long, allocated from a pool
363 * @param p The pool from which to allocate
364 * @param n The number to format
365 * @return The string representation of the number
366 */
367APR_DECLARE(char *) apr_ltoa(apr_pool_t *p, long n);
368
369/**
370 * create a string representation of an apr_off_t, allocated from a pool
371 * @param p The pool from which to allocate
372 * @param n The number to format
373 * @return The string representation of the number
374 */
375APR_DECLARE(char *) apr_off_t_toa(apr_pool_t *p, apr_off_t n);
376
377/**
378 * Convert a numeric string into an apr_off_t numeric value.
379 * @param offset The value of the parsed string.
380 * @param buf The string to parse. It may contain optional whitespace,
381 * followed by an optional '+' (positive, default) or '-' (negative)
382 * character, followed by an optional '0x' prefix if base is 0 or 16,
383 * followed by numeric digits appropriate for base.
384 * @param end A pointer to the end of the valid character in buf. If
385 * not NULL, it is set to the first invalid character in buf.
386 * @param base A numeric base in the range between 2 and 36 inclusive,
387 * or 0. If base is zero, buf will be treated as base ten unless its
388 * digits are prefixed with '0x', in which case it will be treated as
389 * base 16.
390 * @bug *end breaks type safety; where *buf is const, *end needs to be
391 * declared as const in APR 2.0
392 */
393APR_DECLARE(apr_status_t) apr_strtoff(apr_off_t *offset, const char *buf,
394 char **end, int base);
395
396/**
397 * parse a numeric string into a 64-bit numeric value
398 * @param buf The string to parse. It may contain optional whitespace,
399 * followed by an optional '+' (positive, default) or '-' (negative)
400 * character, followed by an optional '0x' prefix if base is 0 or 16,
401 * followed by numeric digits appropriate for base.
402 * @param end A pointer to the end of the valid character in buf. If
403 * not NULL, it is set to the first invalid character in buf.
404 * @param base A numeric base in the range between 2 and 36 inclusive,
405 * or 0. If base is zero, buf will be treated as base ten unless its
406 * digits are prefixed with '0x', in which case it will be treated as
407 * base 16.
408 * @return The numeric value of the string. On overflow, errno is set
409 * to ERANGE. On success, errno is set to 0.
410 */
411APR_DECLARE(apr_int64_t) apr_strtoi64(const char *buf, char **end, int base);
412
413/**
414 * parse a base-10 numeric string into a 64-bit numeric value.
415 * Equivalent to apr_strtoi64(buf, (char**)NULL, 10).
416 * @param buf The string to parse
417 * @return The numeric value of the string. On overflow, errno is set
418 * to ERANGE. On success, errno is set to 0.
419 */
420APR_DECLARE(apr_int64_t) apr_atoi64(const char *buf);
421
422/**
423 * Format a binary size (magnitiudes are 2^10 rather than 10^3) from an apr_off_t,
424 * as bytes, K, M, T, etc, to a four character compacted human readable string.
425 * @param size The size to format
426 * @param buf The 5 byte text buffer (counting the trailing null)
427 * @return The buf passed to apr_strfsize()
428 * @remark All negative sizes report ' - ', apr_strfsize only formats positive values.
429 */
430APR_DECLARE(char *) apr_strfsize(apr_off_t size, char *buf);
431
432/** @} */
433
434#ifdef __cplusplus
435}
436#endif
437
438#endif /* !APR_STRINGS_H */
APR Platform Definitions.
APR Error Codes.
APR memory allocation.
APR Standard Headers Support.
int apr_snprintf(char *buf, apr_size_t len, const char *format,...)
int apr_vsnprintf(char *buf, apr_size_t len, const char *format, va_list ap)
int apr_status_t
Definition apr_errno.h:44
#define APR_DECLARE_NONSTD(type)
Definition apr.h:536
#define APR_DECLARE(type)
Definition apr.h:523
struct apr_pool_t apr_pool_t
Definition apr_pools.h:60
char * apr_strfsize(apr_off_t size, char *buf)
apr_status_t apr_strtoff(apr_off_t *offset, const char *buf, char **end, int base)
int apr_strnatcasecmp(char const *a, char const *b)
apr_int64_t apr_strtoi64(const char *buf, char **end, int base)
char * apr_pvsprintf(apr_pool_t *p, const char *fmt, va_list ap)
char * apr_pstrcatv(apr_pool_t *p, const struct iovec *vec, apr_size_t nvec, apr_size_t *nbytes)
char * apr_pstrndup(apr_pool_t *p, const char *s, apr_size_t n)
apr_status_t apr_memzero_explicit(void *buffer, apr_size_t size)
apr_status_t apr_tokenize_to_argv(const char *arg_str, char ***argv_out, apr_pool_t *token_context)
char * apr_collapse_spaces(char *dest, const char *src)
char * apr_pstrmemdup(apr_pool_t *p, const char *s, apr_size_t n)
void * apr_pmemdup(apr_pool_t *p, const void *m, apr_size_t n)
char * apr_off_t_toa(apr_pool_t *p, apr_off_t n)
int apr_strneq_timingsafe(const char *sec1, const char *str2, apr_size_t n)
char * apr_strtok(char *str, const char *sep, char **last)
char * apr_itoa(apr_pool_t *p, int n)
int apr_streq_timingsafe(const char *sec1, const char *str2)
char * apr_psprintf(apr_pool_t *p, const char *fmt,...)
int apr_strnatcmp(char const *a, char const *b)
int apr_memeq_timingsafe(const void *buf1, const void *buf2, apr_size_t n)
char * apr_cpystrn(char *dst, const char *src, apr_size_t dst_size)
char * apr_pstrcat(apr_pool_t *p,...)
char * apr_ltoa(apr_pool_t *p, long n)
apr_int64_t apr_atoi64(const char *buf)
char * apr_pstrdup(apr_pool_t *p, const char *s)