1258945Sroberto/* 2258945Sroberto * Copyright (C) 2004-2007 Internet Systems Consortium, Inc. ("ISC") 3258945Sroberto * Copyright (C) 2000, 2001, 2003 Internet Software Consortium. 4258945Sroberto * 5258945Sroberto * Permission to use, copy, modify, and/or distribute this software for any 6258945Sroberto * purpose with or without fee is hereby granted, provided that the above 7258945Sroberto * copyright notice and this permission notice appear in all copies. 8258945Sroberto * 9258945Sroberto * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH 10258945Sroberto * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY 11258945Sroberto * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, 12258945Sroberto * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM 13258945Sroberto * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE 14258945Sroberto * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 15258945Sroberto * PERFORMANCE OF THIS SOFTWARE. 16258945Sroberto */ 17258945Sroberto 18258945Sroberto/* $Id: string.h,v 1.23 2007/09/13 04:48:16 each Exp $ */ 19258945Sroberto 20258945Sroberto#ifndef ISC_STRING_H 21258945Sroberto#define ISC_STRING_H 1 22258945Sroberto 23258945Sroberto/*! \file isc/string.h */ 24258945Sroberto 25258945Sroberto#include <isc/formatcheck.h> 26258945Sroberto#include <isc/int.h> 27258945Sroberto#include <isc/lang.h> 28258945Sroberto#include <isc/platform.h> 29258945Sroberto#include <isc/types.h> 30258945Sroberto 31258945Sroberto#include <string.h> 32258945Sroberto 33258945Sroberto#ifdef ISC_PLATFORM_HAVESTRINGSH 34258945Sroberto#include <strings.h> 35258945Sroberto#endif 36258945Sroberto 37258945Sroberto#define ISC_STRING_MAGIC 0x5e 38258945Sroberto 39258945SrobertoISC_LANG_BEGINDECLS 40258945Sroberto 41258945Srobertoisc_uint64_t 42258945Srobertoisc_string_touint64(char *source, char **endp, int base); 43258945Sroberto/*%< 44258945Sroberto * Convert the string pointed to by 'source' to isc_uint64_t. 45258945Sroberto * 46258945Sroberto * On successful conversion 'endp' points to the first character 47258945Sroberto * after conversion is complete. 48258945Sroberto * 49258945Sroberto * 'base': 0 or 2..36 50258945Sroberto * 51258945Sroberto * If base is 0 the base is computed from the string type. 52258945Sroberto * 53258945Sroberto * On error 'endp' points to 'source'. 54258945Sroberto */ 55258945Sroberto 56258945Srobertoisc_result_t 57258945Srobertoisc_string_copy(char *target, size_t size, const char *source); 58258945Sroberto/* 59258945Sroberto * Copy the string pointed to by 'source' to 'target' which is a 60258945Sroberto * pointer to a string of at least 'size' bytes. 61258945Sroberto * 62258945Sroberto * Requires: 63258945Sroberto * 'target' is a pointer to a char[] of at least 'size' bytes. 64258945Sroberto * 'size' an integer > 0. 65258945Sroberto * 'source' == NULL or points to a NUL terminated string. 66258945Sroberto * 67258945Sroberto * Ensures: 68258945Sroberto * If result == ISC_R_SUCCESS 69258945Sroberto * 'target' will be a NUL terminated string of no more 70258945Sroberto * than 'size' bytes (including NUL). 71258945Sroberto * 72258945Sroberto * If result == ISC_R_NOSPACE 73258945Sroberto * 'target' is undefined. 74258945Sroberto * 75258945Sroberto * Returns: 76258945Sroberto * ISC_R_SUCCESS -- 'source' was successfully copied to 'target'. 77258945Sroberto * ISC_R_NOSPACE -- 'source' could not be copied since 'target' 78258945Sroberto * is too small. 79258945Sroberto */ 80258945Sroberto 81258945Srobertovoid 82258945Srobertoisc_string_copy_truncate(char *target, size_t size, const char *source); 83258945Sroberto/* 84258945Sroberto * Copy the string pointed to by 'source' to 'target' which is a 85258945Sroberto * pointer to a string of at least 'size' bytes. 86258945Sroberto * 87258945Sroberto * Requires: 88258945Sroberto * 'target' is a pointer to a char[] of at least 'size' bytes. 89258945Sroberto * 'size' an integer > 0. 90258945Sroberto * 'source' == NULL or points to a NUL terminated string. 91258945Sroberto * 92258945Sroberto * Ensures: 93258945Sroberto * 'target' will be a NUL terminated string of no more 94258945Sroberto * than 'size' bytes (including NUL). 95258945Sroberto */ 96258945Sroberto 97258945Srobertoisc_result_t 98258945Srobertoisc_string_append(char *target, size_t size, const char *source); 99258945Sroberto/* 100258945Sroberto * Append the string pointed to by 'source' to 'target' which is a 101258945Sroberto * pointer to a NUL terminated string of at least 'size' bytes. 102258945Sroberto * 103258945Sroberto * Requires: 104258945Sroberto * 'target' is a pointer to a NUL terminated char[] of at 105258945Sroberto * least 'size' bytes. 106258945Sroberto * 'size' an integer > 0. 107258945Sroberto * 'source' == NULL or points to a NUL terminated string. 108258945Sroberto * 109258945Sroberto * Ensures: 110258945Sroberto * If result == ISC_R_SUCCESS 111258945Sroberto * 'target' will be a NUL terminated string of no more 112258945Sroberto * than 'size' bytes (including NUL). 113258945Sroberto * 114258945Sroberto * If result == ISC_R_NOSPACE 115258945Sroberto * 'target' is undefined. 116258945Sroberto * 117258945Sroberto * Returns: 118258945Sroberto * ISC_R_SUCCESS -- 'source' was successfully appended to 'target'. 119258945Sroberto * ISC_R_NOSPACE -- 'source' could not be appended since 'target' 120258945Sroberto * is too small. 121258945Sroberto */ 122258945Sroberto 123258945Srobertovoid 124258945Srobertoisc_string_append_truncate(char *target, size_t size, const char *source); 125258945Sroberto/* 126258945Sroberto * Append the string pointed to by 'source' to 'target' which is a 127258945Sroberto * pointer to a NUL terminated string of at least 'size' bytes. 128258945Sroberto * 129258945Sroberto * Requires: 130258945Sroberto * 'target' is a pointer to a NUL terminated char[] of at 131258945Sroberto * least 'size' bytes. 132258945Sroberto * 'size' an integer > 0. 133258945Sroberto * 'source' == NULL or points to a NUL terminated string. 134258945Sroberto * 135258945Sroberto * Ensures: 136258945Sroberto * 'target' will be a NUL terminated string of no more 137258945Sroberto * than 'size' bytes (including NUL). 138258945Sroberto */ 139258945Sroberto 140258945Srobertoisc_result_t 141258945Srobertoisc_string_printf(char *target, size_t size, const char *format, ...) 142258945Sroberto ISC_FORMAT_PRINTF(3, 4); 143258945Sroberto/* 144258945Sroberto * Print 'format' to 'target' which is a pointer to a string of at least 145258945Sroberto * 'size' bytes. 146258945Sroberto * 147258945Sroberto * Requires: 148258945Sroberto * 'target' is a pointer to a char[] of at least 'size' bytes. 149258945Sroberto * 'size' an integer > 0. 150258945Sroberto * 'format' == NULL or points to a NUL terminated string. 151258945Sroberto * 152258945Sroberto * Ensures: 153258945Sroberto * If result == ISC_R_SUCCESS 154258945Sroberto * 'target' will be a NUL terminated string of no more 155258945Sroberto * than 'size' bytes (including NUL). 156258945Sroberto * 157258945Sroberto * If result == ISC_R_NOSPACE 158258945Sroberto * 'target' is undefined. 159258945Sroberto * 160258945Sroberto * Returns: 161258945Sroberto * ISC_R_SUCCESS -- 'format' was successfully printed to 'target'. 162258945Sroberto * ISC_R_NOSPACE -- 'format' could not be printed to 'target' since it 163258945Sroberto * is too small. 164258945Sroberto */ 165258945Sroberto 166258945Srobertovoid 167258945Srobertoisc_string_printf_truncate(char *target, size_t size, const char *format, ...) 168258945Sroberto ISC_FORMAT_PRINTF(3, 4); 169258945Sroberto/* 170258945Sroberto * Print 'format' to 'target' which is a pointer to a string of at least 171258945Sroberto * 'size' bytes. 172258945Sroberto * 173258945Sroberto * Requires: 174258945Sroberto * 'target' is a pointer to a char[] of at least 'size' bytes. 175258945Sroberto * 'size' an integer > 0. 176258945Sroberto * 'format' == NULL or points to a NUL terminated string. 177258945Sroberto * 178258945Sroberto * Ensures: 179258945Sroberto * 'target' will be a NUL terminated string of no more 180258945Sroberto * than 'size' bytes (including NUL). 181258945Sroberto */ 182258945Sroberto 183258945Sroberto 184258945Srobertochar * 185258945Srobertoisc_string_regiondup(isc_mem_t *mctx, const isc_region_t *source); 186258945Sroberto/* 187258945Sroberto * Copy the region pointed to by r to a NUL terminated string 188258945Sroberto * allocated from the memory context pointed to by mctx. 189258945Sroberto * 190258945Sroberto * The result should be deallocated using isc_mem_free() 191258945Sroberto * 192258945Sroberto * Requires: 193258945Sroberto * 'mctx' is a point to a valid memory context. 194258945Sroberto * 'source' is a pointer to a valid region. 195258945Sroberto * 196258945Sroberto * Returns: 197258945Sroberto * a pointer to a NUL terminated string or 198258945Sroberto * NULL if memory for the copy could not be allocated 199258945Sroberto * 200258945Sroberto */ 201258945Sroberto 202298699Sdelphijint 203298699Sdelphijisc_tsmemcmp(const void *p1, const void *p2, size_t len); 204298699Sdelphij/* 205298699Sdelphij * Lexicographic compare 'len' unsigned bytes from 'p1' and 'p2' 206298699Sdelphij * like 'memcmp()'. 207298699Sdelphij * 208298699Sdelphij * This function is safe from timing attacks as it has a runtime that 209298699Sdelphij * only depends on 'len' and has no early-out option. 210298699Sdelphij * 211298699Sdelphij * Use this to check MACs and other material that is security sensitive. 212298699Sdelphij * 213298699Sdelphij * Returns: 214298699Sdelphij * (let x be the byte offset of the first different byte) 215298699Sdelphij * -1 if (u_char)p1[x] < (u_char)p2[x] 216298699Sdelphij * 1 if (u_char)p1[x] > (u_char)p2[x] 217298699Sdelphij * 0 if byte series are equal 218298699Sdelphij */ 219298699Sdelphij 220258945Srobertochar * 221258945Srobertoisc_string_separate(char **stringp, const char *delim); 222258945Sroberto 223258945Sroberto#ifdef ISC_PLATFORM_NEEDSTRSEP 224258945Sroberto#define strsep isc_string_separate 225258945Sroberto#endif 226258945Sroberto 227258945Sroberto#ifdef ISC_PLATFORM_NEEDMEMMOVE 228258945Sroberto#define memmove(a,b,c) bcopy(b,a,c) 229258945Sroberto#endif 230258945Sroberto 231258945Srobertosize_t 232258945Srobertoisc_string_strlcpy(char *dst, const char *src, size_t size); 233258945Sroberto 234258945Sroberto 235258945Sroberto#ifdef ISC_PLATFORM_NEEDSTRLCPY 236258945Sroberto#define strlcpy isc_string_strlcpy 237258945Sroberto#endif 238258945Sroberto 239258945Sroberto 240258945Srobertosize_t 241258945Srobertoisc_string_strlcat(char *dst, const char *src, size_t size); 242258945Sroberto 243258945Sroberto#ifdef ISC_PLATFORM_NEEDSTRLCAT 244258945Sroberto#define strlcat isc_string_strlcat 245258945Sroberto#endif 246258945Sroberto 247258945SrobertoISC_LANG_ENDDECLS 248258945Sroberto 249258945Sroberto#endif /* ISC_STRING_H */ 250