diff options
Diffstat (limited to 'src/libmdsserver/util.c')
-rw-r--r-- | src/libmdsserver/util.c | 145 |
1 files changed, 17 insertions, 128 deletions
diff --git a/src/libmdsserver/util.c b/src/libmdsserver/util.c index 09d996a..5a7450a 100644 --- a/src/libmdsserver/util.c +++ b/src/libmdsserver/util.c @@ -1,6 +1,6 @@ /** * mds — A micro-display server - * Copyright © 2014, 2015, 2016, 2017 Mattias Andrée (maandree@kth.se) + * Copyright © 2014, 2015, 2016, 2017 Mattias Andrée (m@maandree.se) * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -19,19 +19,7 @@ #include "config.h" #include "macros.h" -#include <alloca.h> -#include <stdlib.h> -#include <unistd.h> -#include <limits.h> -#include <string.h> -#include <signal.h> -#include <sys/socket.h> -#include <errno.h> -#include <ctype.h> -#include <time.h> -#include <sys/wait.h> -#include <stdint.h> -#include <inttypes.h> +#include <libsimple.h> @@ -68,20 +56,6 @@ parse_client_id(const char *str) /** - * Read an environment variable, but handle it as undefined if empty - * - * @param var The environment variable's name - * @return The environment variable's value, `NULL` if empty or not defined - */ -char * -getenv_nonempty(const char *var) -{ - char *rc = getenv(var); - return (rc && *rc) ? rc : NULL; -} - - -/** * Prepare the server so that it can reexec into * a newer version of the executed file. * @@ -197,7 +171,7 @@ send_message(int socket, const char *message, size_t length) errno = 0; while (length > 0) { - if ((just_sent = send(socket, message + sent, min(block_size, length), MSG_NOSIGNAL)) < 0) { + if ((just_sent = send(socket, message + sent, MIN(block_size, length), MSG_NOSIGNAL)) < 0) { if (errno == EPIPE) errno = ECONNRESET; if (errno == EMSGSIZE) { @@ -275,17 +249,13 @@ strict_atoj(const char *str, intmax_t *value, intmax_t min, intmax_t max) return -1; while ((c = *str)) { - if ('0' <= c && c <= '9') { - if (r > INTMAX_MAX / 10) { - return -1; - } else if (r == INTMAX_MAX / 10) { - if ((c & 15) > INTMAX_MAX % 10) - return -1; - } - r = r * 10 + (c & 15); - } else { + if (!isdigit(c)) return -1; - } + if (r > INTMAX_MAX / 10) + return -1; + if (r == INTMAX_MAX / 10 && (c & 15) > INTMAX_MAX % 10) + return -1; + r = r * 10 + (c & 15); } if (neg) @@ -326,17 +296,13 @@ strict_atouj(const char *str, uintmax_t *value, uintmax_t min, uintmax_t max) return -1; while ((c = *str)) { - if ('0' <= c && c <= '9') { - if (r > INTMAX_MAX / 10) { - return -1; - } else if (r == INTMAX_MAX / 10) { - if ((c & 15) > INTMAX_MAX % 10) - return -1; - } - r = r * 10 + (c & 15); - } else { + if (!isdigit(c)) return -1; - } + if (r > INTMAX_MAX / 10) + return -1; + if (r == INTMAX_MAX / 10 && (c & 15) > INTMAX_MAX % 10) + return -1; + r = r * 10 + (c & 15); } if (r < min || r > max) @@ -588,8 +554,8 @@ full_write(int fd, const char *buffer, size_t length) errno = 0; wrote = write(fd, buffer, length); fail_if (errno && errno != EINTR); - length -= (size_t)max(wrote, 0); - buffer += (size_t)max(wrote, 0); + length -= (size_t)MAX(wrote, 0); + buffer += (size_t)MAX(wrote, 0); } return 0; fail: @@ -686,11 +652,9 @@ startswith_n(const char *haystack, const char *needle, size_t haystack_n, size_t size_t i; if (haystack_n < needle_n) return 0; - for (i = 0; i < needle_n; i++) if (haystack[i] != needle[i]) return 0; - return 1; } @@ -732,81 +696,6 @@ fail: /** - * Check whether a NUL-terminated string is encoded in UTF-8 - * - * @param string The string - * @param allow_modified_nul Whether Modified UTF-8 is allowed, which allows a two-byte encoding for NUL - * @return Zero if good, -1 on encoding error - */ -int -verify_utf8(const char *string, int allow_modified_nul) -{ - static long BYTES_TO_MIN_BITS[] = {0, 0, 8, 12, 17, 22, 37}; - static long BYTES_TO_MAX_BITS[] = {0, 7, 11, 16, 21, 26, 31}; - long bytes = 0, read_bytes = 0, bits = 0, c, character; - - /* min bits max bits - 0....... 0 7 - 110..... 10...... 8 11 - 1110.... 10...... 10...... 12 16 - 11110... 10...... 10...... 10...... 17 21 - 111110.. 10...... 10...... 10...... 10...... 22 26 - 1111110. 10...... 10...... 10...... 10...... 10...... 27 31 - */ - - while ((c = (long)(*string++))) { - if (read_bytes == 0) { - /* First byte of the character. */ - - if (!(c & 0x80)) - /* Single-byte character. */ - continue; - - if ((c & 0xC0) == 0x80) - /* Single-byte character marked as multibyte, or - a non-first byte in a multibyte character. */ - return -1; - - /* Multibyte character. */ - while ((c & 0x80)) - bytes++, c <<= 1; - read_bytes = 1; - character = c & 0x7F; - if (bytes > 6) - /* 31-bit characters can be encoded with 6-bytes, - and UTF-8 does not cover higher code points. */ - return -1; - } else { - /* Not first byte of the character. */ - - if ((c & 0xC0) != 0x80) - /* Beginning of new character before a - multibyte character has ended. */ - return -1; - - character = (character << 6) | (c & 0x7F); - - if (++read_bytes < bytes) - /* Not at last byte yet. */ - continue; - - /* Check that the character is not unnecessarily long. */ - while (character) - character >>= 1, bits++; - bits = ((bits == 0) && (bytes == 2) && allow_modified_nul) ? 8 : bits; - if ((bits < BYTES_TO_MIN_BITS[bytes]) || (BYTES_TO_MAX_BITS[bytes] < bits)) - return -1; - - read_bytes = bytes = bits = 0; - } - } - - /* Make sure we did not stop at the middle of a multibyte character. */ - return read_bytes == 0 ? 0 : -1; -} - - -/** * Construct an error message to be sent to a client * * @param recv_client_id The client ID attached on the message that was received, must not be `NULL` |