diff options
Diffstat (limited to 'usr/src/lib/libcustr/common')
-rw-r--r-- | usr/src/lib/libcustr/common/custr.c | 77 | ||||
-rw-r--r-- | usr/src/lib/libcustr/common/libcustr.h | 48 | ||||
-rw-r--r-- | usr/src/lib/libcustr/common/mapfile-vers | 6 |
3 files changed, 128 insertions, 3 deletions
diff --git a/usr/src/lib/libcustr/common/custr.c b/usr/src/lib/libcustr/common/custr.c index 8c8288ab7d..3ff67cac71 100644 --- a/usr/src/lib/libcustr/common/custr.c +++ b/usr/src/lib/libcustr/common/custr.c @@ -14,7 +14,7 @@ */ /* - * Copyright 2019 Joyent, Inc. + * Copyright 2020 Joyent, Inc. */ #include <stdlib.h> @@ -85,6 +85,81 @@ custr_reset(custr_t *cus) cus->cus_data[0] = '\0'; } +int +custr_remove(custr_t *cus, size_t idx, size_t len) +{ + size_t endidx = idx + len; + + /* + * Once gcc4 is dropped as a shadow compiler, we can migrate to + * using builtins for the overflow check. + */ + if (endidx < idx || endidx < len) { + errno = EINVAL; + return (-1); + } + + if (idx >= cus->cus_strlen || endidx > cus->cus_strlen) { + errno = EINVAL; + return (-1); + } + + if (len == 0) + return (0); + + /* The +1 will include the terminating NUL in the move */ + (void) memmove(cus->cus_data + idx, cus->cus_data + endidx, + cus->cus_strlen - endidx + 1); + cus->cus_strlen -= len; + + /* The result should be NUL */ + VERIFY0(cus->cus_data[cus->cus_strlen]); + return (0); +} + +int +custr_rremove(custr_t *cus, size_t ridx, size_t len) +{ + size_t idx; + + if (ridx >= cus->cus_strlen) { + errno = EINVAL; + return (-1); + } + + idx = cus->cus_strlen - ridx - 1; + return (custr_remove(cus, idx, len)); +} + +int +custr_trunc(custr_t *cus, size_t idx) +{ + if (idx >= cus->cus_strlen) { + errno = EINVAL; + return (-1); + } + + cus->cus_data[idx] = '\0'; + cus->cus_strlen = idx; + return (0); +} + +int +custr_rtrunc(custr_t *cus, size_t ridx) +{ + size_t idx; + + if (ridx >= cus->cus_strlen) { + errno = EINVAL; + return (-1); + } + + idx = cus->cus_strlen - ridx - 1; + cus->cus_data[idx] = '\0'; + cus->cus_strlen = idx; + return (0); +} + size_t custr_len(custr_t *cus) { diff --git a/usr/src/lib/libcustr/common/libcustr.h b/usr/src/lib/libcustr/common/libcustr.h index 8fe5fee1b7..f8f15db07b 100644 --- a/usr/src/lib/libcustr/common/libcustr.h +++ b/usr/src/lib/libcustr/common/libcustr.h @@ -10,7 +10,7 @@ */ /* - * Copyright 2019, Joyent, Inc. + * Copyright 2019 Joyent, Inc. */ #ifndef _LIBCUSTR_H @@ -142,6 +142,52 @@ size_t custr_len(custr_t *); void custr_reset(custr_t *); /* + * custr_remove(cus, idx, len) + * + * Remove len bytes from cus, starting at idx. + * + * Returns 0 on success or -1 on failure. On failure, errno will be set to: + * EINVAL Either the idx or len parameter is invalid + * + */ +int custr_remove(custr_t *, size_t, size_t); + +/* + * custr_rremove(cus, idx, len) + * + * Remove len bytes from cus, starting at idx relative to the end of cus. + * That is, 0 = last byte of cus, 1 = second to last byte of cus, ...). + * The direction of removal is always towards the end of the string. I.e. + * 'custr_rremove(cus, 1, 2)' removes the last two bytes of cus. + * + * Returns 0 on success or -1 on failure. On failure, errno will be set to: + * EINVAL Either the idx or len parameter is invalid + * + */ +int custr_rremove(custr_t *, size_t, size_t); + +/* + * custr_trunc(cus, idx) + * + * Truncate cus starting at idx. + * + * Returns 0 on success or -1 on failure. On failure, errno is set to: + * EINVAL The idx value was invalid. + */ +int custr_trunc(custr_t *, size_t); + +/* + * custr_rtrunc(cus, idx) + * + * Truncate cus starting at idx relative to the end of cus (similar to how + * the idx paramter is treated with custr_rremove()). + * + * Returns 0 on success or -1 on failure. On failure, errno is set to: + * EINVAL The idx value was invalid. + */ +int custr_rtrunc(custr_t *, size_t); + +/* * Retrieve a const pointer to a NUL-terminated string version of the contents * of the dynamic string. Storage for this string should not be freed, and * the pointer will be invalidated by any mutations to the dynamic string. diff --git a/usr/src/lib/libcustr/common/mapfile-vers b/usr/src/lib/libcustr/common/mapfile-vers index f94636b6f5..53b94164ad 100644 --- a/usr/src/lib/libcustr/common/mapfile-vers +++ b/usr/src/lib/libcustr/common/mapfile-vers @@ -10,7 +10,7 @@ # # -# Copyright 2019, Joyent, Inc. +# Copyright 2019 Joyent, Inc. # # @@ -42,7 +42,11 @@ SYMBOL_VERSION ILLUMOSprivate { custr_cstr; custr_free; custr_len; + custr_remove; custr_reset; + custr_rremove; + custr_rtrunc; + custr_trunc; custr_xalloc; custr_xalloc_buf; local: |