stream cipher: Allow in-place encryption.
[paraslash.git] / string.c
1 /*
2 * Copyright (C) 2004-2011 Andre Noll <maan@systemlinux.org>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7 /** \file string.c Memory allocation and string handling functions. */
8
9 #include <sys/time.h> /* gettimeofday */
10 #include <pwd.h>
11 #include <sys/utsname.h> /* uname() */
12 #include <string.h>
13 #include <regex.h>
14
15 #include "para.h"
16 #include "string.h"
17 #include "error.h"
18
19 /**
20 * Paraslash's version of realloc().
21 *
22 * \param p Pointer to the memory block, may be \p NULL.
23 * \param size The desired new size.
24 *
25 * A wrapper for realloc(3). It calls \p exit(\p EXIT_FAILURE) on errors,
26 * i.e. there is no need to check the return value in the caller.
27 *
28 * \return A pointer to the newly allocated memory, which is suitably aligned
29 * for any kind of variable and may be different from \a p.
30 *
31 * \sa realloc(3).
32 */
33 __must_check __malloc void *para_realloc(void *p, size_t size)
34 {
35 /*
36 * No need to check for NULL pointers: If p is NULL, the call
37 * to realloc is equivalent to malloc(size)
38 */
39 assert(size);
40 if (!(p = realloc(p, size))) {
41 PARA_EMERG_LOG("realloc failed (size = %zu), aborting\n",
42 size);
43 exit(EXIT_FAILURE);
44 }
45 return p;
46 }
47
48 /**
49 * Paraslash's version of malloc().
50 *
51 * \param size The desired new size.
52 *
53 * A wrapper for malloc(3) which exits on errors.
54 *
55 * \return A pointer to the allocated memory, which is suitably aligned for any
56 * kind of variable.
57 *
58 * \sa malloc(3).
59 */
60 __must_check __malloc void *para_malloc(size_t size)
61 {
62 void *p;
63
64 assert(size);
65 p = malloc(size);
66 if (!p) {
67 PARA_EMERG_LOG("malloc failed (size = %zu), aborting\n",
68 size);
69 exit(EXIT_FAILURE);
70 }
71 return p;
72 }
73
74 /**
75 * Paraslash's version of calloc().
76 *
77 * \param size The desired new size.
78 *
79 * A wrapper for calloc(3) which exits on errors.
80 *
81 * \return A pointer to the allocated and zeroed-out memory, which is suitably
82 * aligned for any kind of variable.
83 *
84 * \sa calloc(3)
85 */
86 __must_check __malloc void *para_calloc(size_t size)
87 {
88 void *ret = para_malloc(size);
89
90 memset(ret, 0, size);
91 return ret;
92 }
93
94 /**
95 * Paraslash's version of strdup().
96 *
97 * \param s The string to be duplicated.
98 *
99 * A wrapper for strdup(3). It calls \p exit(EXIT_FAILURE) on errors, i.e.
100 * there is no need to check the return value in the caller.
101 *
102 * \return A pointer to the duplicated string. If \a s was the \p NULL pointer,
103 * an pointer to an empty string is returned.
104 *
105 * \sa strdup(3)
106 */
107 __must_check __malloc char *para_strdup(const char *s)
108 {
109 char *ret;
110
111 if ((ret = strdup(s? s: "")))
112 return ret;
113 PARA_EMERG_LOG("strdup failed, aborting\n");
114 exit(EXIT_FAILURE);
115 }
116
117 /**
118 * Allocate a sufficiently large string and print into it.
119 *
120 * \param fmt A usual format string.
121 *
122 * Produce output according to \p fmt. No artificial bound on the length of the
123 * resulting string is imposed.
124 *
125 * \return This function either returns a pointer to a string that must be
126 * freed by the caller or aborts without returning.
127 *
128 * \sa printf(3).
129 */
130 __must_check __printf_1_2 __malloc char *make_message(const char *fmt, ...)
131 {
132 char *msg;
133
134 PARA_VSPRINTF(fmt, msg);
135 return msg;
136 }
137
138 /**
139 * Free the content of a pointer and set it to \p NULL.
140 *
141 * This is equivalent to "free(*arg); *arg = NULL;".
142 *
143 * \param arg The pointer whose content should be freed.
144 */
145 void freep(void *arg)
146 {
147 void **ptr = (void **)arg;
148 free(*ptr);
149 *ptr = NULL;
150 }
151
152 /**
153 * Paraslash's version of strcat().
154 *
155 * \param a String to be appended to.
156 * \param b String to append.
157 *
158 * Append \p b to \p a.
159 *
160 * \return If \a a is \p NULL, return a pointer to a copy of \a b, i.e.
161 * para_strcat(NULL, b) is equivalent to para_strdup(b). If \a b is \p NULL,
162 * return \a a without making a copy of \a a. Otherwise, construct the
163 * concatenation \a c, free \a a (but not \a b) and return \a c.
164 *
165 * \sa strcat(3)
166 */
167 __must_check __malloc char *para_strcat(char *a, const char *b)
168 {
169 char *tmp;
170
171 if (!a)
172 return para_strdup(b);
173 if (!b)
174 return a;
175 tmp = make_message("%s%s", a, b);
176 free(a);
177 return tmp;
178 }
179
180 /**
181 * Paraslash's version of dirname().
182 *
183 * \param name Pointer to the full path.
184 *
185 * Compute the directory component of \p name.
186 *
187 * \return If \a name is \p NULL or the empty string, return \p NULL.
188 * Otherwise, Make a copy of \a name and return its directory component. Caller
189 * is responsible to free the result.
190 */
191 __must_check __malloc char *para_dirname(const char *name)
192 {
193 char *p, *ret;
194
195 if (!name || !*name)
196 return NULL;
197 ret = para_strdup(name);
198 p = strrchr(ret, '/');
199 if (!p)
200 *ret = '\0';
201 else
202 *p = '\0';
203 return ret;
204 }
205
206 /**
207 * Paraslash's version of basename().
208 *
209 * \param name Pointer to the full path.
210 *
211 * Compute the filename component of \a name.
212 *
213 * \return \p NULL if (a) \a name is the empty string or \p NULL, or (b) name
214 * ends with a slash. Otherwise, a pointer within \a name is returned. Caller
215 * must not free the result.
216 */
217 __must_check char *para_basename(const char *name)
218 {
219 char *ret;
220
221 if (!name || !*name)
222 return NULL;
223 ret = strrchr(name, '/');
224 if (!ret)
225 return (char *)name;
226 ret++;
227 return ret;
228 }
229
230 /**
231 * Cut trailing newline.
232 *
233 * \param buf The string to be chopped.
234 *
235 * Replace the last character in \p buf by zero if it is equal to
236 * the newline character.
237 */
238 void chop(char *buf)
239 {
240 int n = strlen(buf);
241
242 if (!n)
243 return;
244 if (buf[n - 1] == '\n')
245 buf[n - 1] = '\0';
246 }
247
248 /**
249 * Get the logname of the current user.
250 *
251 * \return A dynamically allocated string that must be freed by the caller. On
252 * errors, the string "unknown_user" is returned, i.e. this function never
253 * returns \p NULL.
254 *
255 * \sa getpwuid(3).
256 */
257 __must_check __malloc char *para_logname(void)
258 {
259 struct passwd *pw = getpwuid(getuid());
260 return para_strdup(pw? pw->pw_name : "unknown_user");
261 }
262
263 /**
264 * Get the home directory of the current user.
265 *
266 * \return A dynamically allocated string that must be freed by the caller. If
267 * the home directory could not be found, this function returns "/tmp".
268 */
269 __must_check __malloc char *para_homedir(void)
270 {
271 struct passwd *pw = getpwuid(getuid());
272 return para_strdup(pw? pw->pw_dir : "/tmp");
273 }
274
275 /**
276 * Get the own hostname.
277 *
278 * \return A dynamically allocated string containing the hostname.
279 *
280 * \sa uname(2).
281 */
282 __malloc char *para_hostname(void)
283 {
284 struct utsname u;
285
286 uname(&u);
287 return para_strdup(u.nodename);
288 }
289
290 /**
291 * Used to distinguish between read-only and read-write mode.
292 *
293 * \sa for_each_line(), for_each_line_ro().
294 */
295 enum for_each_line_modes{
296 /** Activate read-only mode. */
297 LINE_MODE_RO,
298 /** Activate read-write mode. */
299 LINE_MODE_RW
300 };
301
302 static int for_each_complete_line(enum for_each_line_modes mode, char *buf,
303 size_t size, line_handler_t *line_handler, void *private_data)
304 {
305 char *start = buf, *end;
306 int ret, i, num_lines = 0;
307
308 // PARA_NOTICE_LOG("buf: %s\n", buf);
309 while (start < buf + size) {
310 char *next_null;
311 char *next_cr;
312
313 next_cr = memchr(start, '\n', buf + size - start);
314 next_null = memchr(start, '\0', buf + size - start);
315 if (!next_cr && !next_null)
316 break;
317 if (next_cr && next_null) {
318 end = next_cr < next_null? next_cr : next_null;
319 } else if (next_null) {
320 end = next_null;
321 } else
322 end = next_cr;
323 num_lines++;
324 if (!line_handler) {
325 start = ++end;
326 continue;
327 }
328 if (mode == LINE_MODE_RO) {
329 size_t s = end - start;
330 char *b = para_malloc(s + 1);
331 memcpy(b, start, s);
332 b[s] = '\0';
333 // PARA_NOTICE_LOG("b: %s, start: %s\n", b, start);
334 ret = line_handler(b, private_data);
335 free(b);
336 } else {
337 *end = '\0';
338 ret = line_handler(start, private_data);
339 }
340 if (ret < 0)
341 return ret;
342 start = ++end;
343 }
344 if (!line_handler || mode == LINE_MODE_RO)
345 return num_lines;
346 i = buf + size - start;
347 if (i && i != size)
348 memmove(buf, start, i);
349 return i;
350 }
351
352 /**
353 * Call a custom function for each complete line.
354 *
355 * \param buf The buffer containing data separated by newlines.
356 * \param size The number of bytes in \a buf.
357 * \param line_handler The custom function.
358 * \param private_data Pointer passed to \a line_handler.
359 *
360 * If \p line_handler is \p NULL, the function returns the number of complete
361 * lines in \p buf. Otherwise, \p line_handler is called for each complete
362 * line in \p buf. The first argument to \p line_handler is the current line,
363 * and \p private_data is passed as the second argument. The function returns
364 * if \p line_handler returns a negative value or no more lines are in the
365 * buffer. The rest of the buffer (last chunk containing an incomplete line)
366 * is moved to the beginning of the buffer.
367 *
368 * \return If \p line_handler is not \p NULL, this function returns the number
369 * of bytes not handled to \p line_handler on success, or the negative return
370 * value of the \p line_handler on errors.
371 *
372 * \sa for_each_line_ro().
373 */
374 int for_each_line(char *buf, size_t size, line_handler_t *line_handler,
375 void *private_data)
376 {
377 return for_each_complete_line(LINE_MODE_RW, buf, size, line_handler,
378 private_data);
379 }
380
381 /**
382 * Call a custom function for each complete line.
383 *
384 * \param buf Same meaning as in \p for_each_line().
385 * \param size Same meaning as in \p for_each_line().
386 * \param line_handler Same meaning as in \p for_each_line().
387 * \param private_data Same meaning as in \p for_each_line().
388 *
389 * This function behaves like \p for_each_line(), but \a buf is left unchanged.
390 *
391 * \return On success, the function returns the number of complete lines in \p
392 * buf, otherwise the (negative) return value of \p line_handler is returned.
393 *
394 * \sa for_each_line().
395 */
396 int for_each_line_ro(char *buf, size_t size, line_handler_t *line_handler,
397 void *private_data)
398 {
399 return for_each_complete_line(LINE_MODE_RO, buf, size, line_handler,
400 private_data);
401 }
402
403 /** Return the hex characters of the lower 4 bits. */
404 #define hex(a) (hexchar[(a) & 15])
405
406 static void write_size_header(char *buf, int n)
407 {
408 static char hexchar[] = "0123456789abcdef";
409
410 buf[0] = hex(n >> 12);
411 buf[1] = hex(n >> 8);
412 buf[2] = hex(n >> 4);
413 buf[3] = hex(n);
414 buf[4] = ' ';
415 }
416
417 /**
418 * Read a four-byte hex-number and return its value.
419 *
420 * Each status item sent by para_server is prefixed with such a hex number in
421 * ASCII which describes the size of the status item.
422 *
423 * \param buf The buffer which must be at least four bytes long.
424 *
425 * \return The value of the hex number on success, \p -E_SIZE_PREFIX if the
426 * buffer did not contain only hex digits.
427 */
428 int read_size_header(const char *buf)
429 {
430 int i, len = 0;
431
432 for (i = 0; i < 4; i++) {
433 unsigned char c = buf[i];
434 len <<= 4;
435 if (c >= '0' && c <= '9') {
436 len += c - '0';
437 continue;
438 }
439 if (c >= 'a' && c <= 'f') {
440 len += c - 'a' + 10;
441 continue;
442 }
443 return -E_SIZE_PREFIX;
444 }
445 if (buf[4] != ' ')
446 return -E_SIZE_PREFIX;
447 return len;
448 }
449
450 /**
451 * Safely print into a buffer at a given offset.
452 *
453 * \param b Determines the buffer, its size, and the offset.
454 * \param fmt The format string.
455 *
456 * This function prints into the buffer given by \a b at the offset which is
457 * also given by \a b. If there is not enough space to hold the result, the
458 * buffer size is doubled until the underlying call to vsnprintf() succeeds
459 * or the size of the buffer exceeds the maximal size specified in \a b.
460 *
461 * In the latter case the unmodified \a buf and \a offset values as well as the
462 * private_data pointer of \a b are passed to the \a max_size_handler of \a b.
463 * If this function succeeds, i.e. returns a non-negative value, the offset of
464 * \a b is reset to zero and the given data is written to the beginning of the
465 * buffer. If \a max_size_handler() returns a negative value, this value is
466 * returned by \a para_printf().
467 *
468 * Upon return, the offset of \a b is adjusted accordingly so that subsequent
469 * calls to this function append data to what is already contained in the
470 * buffer.
471 *
472 * It's OK to call this function with \p b->buf being \p NULL. In this case, an
473 * initial buffer is allocated.
474 *
475 * \return The number of bytes printed into the buffer (not including the
476 * terminating \p NULL byte) on success, negative on errors. If there is no
477 * size-bound on \a b, i.e. if \p b->max_size is zero, this function never
478 * fails.
479 *
480 * \sa make_message(), vsnprintf(3).
481 */
482 __printf_2_3 int para_printf(struct para_buffer *b, const char *fmt, ...)
483 {
484 int ret, sz_off = (b->flags & PBF_SIZE_PREFIX)? 5 : 0;
485
486 if (!b->buf) {
487 b->buf = para_malloc(128);
488 b->size = 128;
489 b->offset = 0;
490 }
491 while (1) {
492 char *p = b->buf + b->offset;
493 size_t size = b->size - b->offset;
494 va_list ap;
495
496 if (size > sz_off) {
497 va_start(ap, fmt);
498 ret = vsnprintf(p + sz_off, size - sz_off, fmt, ap);
499 va_end(ap);
500 if (ret > -1 && ret < size - sz_off) { /* success */
501 b->offset += ret + sz_off;
502 if (sz_off)
503 write_size_header(p, ret);
504 return ret + sz_off;
505 }
506 }
507 /* check if we may grow the buffer */
508 if (!b->max_size || 2 * b->size < b->max_size) { /* yes */
509 /* try again with more space */
510 b->size *= 2;
511 b->buf = para_realloc(b->buf, b->size);
512 continue;
513 }
514 /* can't grow buffer */
515 if (!b->offset || !b->max_size_handler) /* message too large */
516 return -ERRNO_TO_PARA_ERROR(ENOSPC);
517 ret = b->max_size_handler(b->buf, b->offset, b->private_data);
518 if (ret < 0)
519 return ret;
520 b->offset = 0;
521 }
522 }
523
524 /** \cond LLONG_MAX and LLONG_MIN might not be defined. */
525 #ifndef LLONG_MAX
526 #define LLONG_MAX 9223372036854775807LL
527 #endif
528 #ifndef LLONG_MIN
529 #define LLONG_MIN (-LLONG_MAX - 1LL)
530 #endif
531 /** \endcond */
532
533 /**
534 * Convert a string to a 64-bit signed integer value.
535 *
536 * \param str The string to be converted.
537 * \param value Result pointer.
538 *
539 * \return Standard.
540 *
541 * \sa para_atoi32(), strtol(3), atoi(3).
542 */
543 int para_atoi64(const char *str, int64_t *value)
544 {
545 char *endptr;
546 long long tmp;
547
548 errno = 0; /* To distinguish success/failure after call */
549 tmp = strtoll(str, &endptr, 10);
550 if (errno == ERANGE && (tmp == LLONG_MAX || tmp == LLONG_MIN))
551 return -E_ATOI_OVERFLOW;
552 if (errno != 0 && tmp == 0) /* other error */
553 return -E_STRTOLL;
554 if (endptr == str)
555 return -E_ATOI_NO_DIGITS;
556 if (*endptr != '\0') /* Further characters after number */
557 return -E_ATOI_JUNK_AT_END;
558 *value = tmp;
559 return 1;
560 }
561
562 /**
563 * Convert a string to a 32-bit signed integer value.
564 *
565 * \param str The string to be converted.
566 * \param value Result pointer.
567 *
568 * \return Standard.
569 *
570 * \sa para_atoi64().
571 */
572 int para_atoi32(const char *str, int32_t *value)
573 {
574 int64_t tmp;
575 int ret;
576 const int32_t max = 2147483647;
577
578 ret = para_atoi64(str, &tmp);
579 if (ret < 0)
580 return ret;
581 if (tmp > max || tmp < -max - 1)
582 return -E_ATOI_OVERFLOW;
583 *value = tmp;
584 return 1;
585 }
586
587 static inline int loglevel_equal(const char *arg, const char * const ll)
588 {
589 return !strncasecmp(arg, ll, strlen(ll));
590 }
591
592 /**
593 * Compute the loglevel number from its name.
594 *
595 * \param txt The name of the loglevel (debug, info, ...).
596 *
597 * \return The numeric representation of the loglevel name.
598 */
599 int get_loglevel_by_name(const char *txt)
600 {
601 if (loglevel_equal(txt, "debug"))
602 return LL_DEBUG;
603 if (loglevel_equal(txt, "info"))
604 return LL_INFO;
605 if (loglevel_equal(txt, "notice"))
606 return LL_NOTICE;
607 if (loglevel_equal(txt, "warning"))
608 return LL_WARNING;
609 if (loglevel_equal(txt, "error"))
610 return LL_ERROR;
611 if (loglevel_equal(txt, "crit"))
612 return LL_CRIT;
613 if (loglevel_equal(txt, "emerg"))
614 return LL_EMERG;
615 return -1;
616 }
617
618 static int get_next_word(const char *buf, const char *delim, char **word)
619 {
620 enum line_state_flags {LSF_HAVE_WORD = 1, LSF_BACKSLASH = 2,
621 LSF_SINGLE_QUOTE = 4, LSF_DOUBLE_QUOTE = 8};
622 const char *in;
623 char *out;
624 int ret, state = 0;
625
626 out = para_malloc(strlen(buf) + 1);
627 *out = '\0';
628 *word = out;
629 for (in = buf; *in; in++) {
630 const char *p;
631
632 switch (*in) {
633 case '\\':
634 if (state & LSF_BACKSLASH) /* \\ */
635 goto copy_char;
636 state |= LSF_BACKSLASH;
637 state |= LSF_HAVE_WORD;
638 continue;
639 case 'n':
640 case 't':
641 if (state & LSF_BACKSLASH) { /* \n or \t */
642 *out++ = (*in == 'n')? '\n' : '\t';
643 state &= ~LSF_BACKSLASH;
644 continue;
645 }
646 goto copy_char;
647 case '"':
648 if (state & LSF_BACKSLASH) /* \" */
649 goto copy_char;
650 if (state & LSF_SINGLE_QUOTE) /* '" */
651 goto copy_char;
652 if (state & LSF_DOUBLE_QUOTE) {
653 state &= ~LSF_DOUBLE_QUOTE;
654 continue;
655 }
656 state |= LSF_HAVE_WORD;
657 state |= LSF_DOUBLE_QUOTE;
658 continue;
659 case '\'':
660 if (state & LSF_BACKSLASH) /* \' */
661 goto copy_char;
662 if (state & LSF_DOUBLE_QUOTE) /* "' */
663 goto copy_char;
664 if (state & LSF_SINGLE_QUOTE) {
665 state &= ~LSF_SINGLE_QUOTE;
666 continue;
667 }
668 state |= LSF_HAVE_WORD;
669 state |= LSF_SINGLE_QUOTE;
670 continue;
671 }
672 for (p = delim; *p; p++) {
673 if (*in != *p)
674 continue;
675 if (state & LSF_BACKSLASH)
676 goto copy_char;
677 if (state & LSF_SINGLE_QUOTE)
678 goto copy_char;
679 if (state & LSF_DOUBLE_QUOTE)
680 goto copy_char;
681 if (state & LSF_HAVE_WORD)
682 goto success;
683 break;
684 }
685 if (*p) /* ignore delimiter at the beginning */
686 continue;
687 copy_char:
688 state |= LSF_HAVE_WORD;
689 *out++ = *in;
690 state &= ~LSF_BACKSLASH;
691 }
692 ret = 0;
693 if (!(state & LSF_HAVE_WORD))
694 goto out;
695 ret = -ERRNO_TO_PARA_ERROR(EINVAL);
696 if (state & LSF_BACKSLASH) {
697 PARA_ERROR_LOG("trailing backslash\n");
698 goto out;
699 }
700 if ((state & LSF_SINGLE_QUOTE) || (state & LSF_DOUBLE_QUOTE)) {
701 PARA_ERROR_LOG("unmatched quote character\n");
702 goto out;
703 }
704 success:
705 *out = '\0';
706 return in - buf;
707 out:
708 free(*word);
709 *word = NULL;
710 return ret;
711 }
712
713 /**
714 * Free an array of words created by create_argv().
715 *
716 * \param argv A pointer previously obtained by \ref create_argv().
717 */
718 void free_argv(char **argv)
719 {
720 int i;
721
722 for (i = 0; argv[i]; i++)
723 free(argv[i]);
724 free(argv);
725 }
726
727 /**
728 * Split a buffer into words.
729 *
730 * This parser honors single and double quotes, backslash-escaped characters
731 * and special characters like \p \\n. The result contains pointers to copies
732 * of the words contained in \a buf and has to be freed by using \ref
733 * free_argv().
734 *
735 * \param buf The buffer to be split.
736 * \param delim Each character in this string is treated as a separator.
737 * \param result The array of words is returned here.
738 *
739 * \return Number of words in \a buf, negative on errors.
740 */
741 int create_argv(const char *buf, const char *delim, char ***result)
742 {
743 char *word, **argv = para_malloc(2 * sizeof(char *));
744 const char *p;
745 int ret, num_words;
746
747 for (p = buf, num_words = 0; ; p += ret, num_words++) {
748 ret = get_next_word(p, delim, &word);
749 if (ret < 0)
750 goto err;
751 if (!ret)
752 break;
753 argv = para_realloc(argv, (num_words + 2) * sizeof(char*));
754 argv[num_words] = word;
755 }
756 argv[num_words] = NULL;
757 *result = argv;
758 return num_words;
759 err:
760 while (num_words > 0)
761 free(argv[--num_words]);
762 free(argv);
763 return ret;
764 }
765
766 /**
767 * Compile a regular expression.
768 *
769 * This simple wrapper calls regcomp() and logs a message on errors.
770 *
771 * \param preg See regcomp(3).
772 * \param regex See regcomp(3).
773 * \param cflags See regcomp(3).
774 *
775 * \return Standard.
776 */
777 int para_regcomp(regex_t *preg, const char *regex, int cflags)
778 {
779 char *buf;
780 size_t size;
781 int ret = regcomp(preg, regex, cflags);
782
783 if (ret == 0)
784 return 1;
785 size = regerror(ret, preg, NULL, 0);
786 buf = para_malloc(size);
787 regerror(ret, preg, buf, size);
788 PARA_ERROR_LOG("%s\n", buf);
789 free(buf);
790 return -E_REGEX;
791 }