1 /* $NetBSD: vstring.h,v 1.2 2017/02/14 01:16:49 christos Exp $ */ 2 3 #ifndef _VSTRING_H_INCLUDED_ 4 #define _VSTRING_H_INCLUDED_ 5 6 /*++ 7 /* NAME 8 /* vstring 3h 9 /* SUMMARY 10 /* arbitrary-length string manager 11 /* SYNOPSIS 12 /* #include "vstring.h" 13 /* DESCRIPTION 14 /* .nf 15 16 /* 17 * System library. 18 */ 19 #include <stdarg.h> 20 21 /* 22 * Utility library. 23 */ 24 #include <vbuf.h> 25 #include <check_arg.h> 26 27 /* 28 * We can't allow bare VBUFs in the interface, because VSTRINGs have a 29 * specific initialization and destruction sequence. 30 */ 31 typedef struct VSTRING { 32 VBUF vbuf; 33 ssize_t maxlen; 34 } VSTRING; 35 36 extern VSTRING *vstring_alloc(ssize_t); 37 extern void vstring_ctl(VSTRING *,...); 38 extern VSTRING *vstring_truncate(VSTRING *, ssize_t); 39 extern VSTRING *vstring_free(VSTRING *); 40 extern VSTRING *vstring_strcpy(VSTRING *, const char *); 41 extern VSTRING *vstring_strncpy(VSTRING *, const char *, ssize_t); 42 extern VSTRING *vstring_strcat(VSTRING *, const char *); 43 extern VSTRING *vstring_strncat(VSTRING *, const char *, ssize_t); 44 extern VSTRING *vstring_memcpy(VSTRING *, const char *, ssize_t); 45 extern VSTRING *vstring_memcat(VSTRING *, const char *, ssize_t); 46 extern char *vstring_memchr(VSTRING *, int); 47 extern VSTRING *vstring_insert(VSTRING *, ssize_t, const char *, ssize_t); 48 extern VSTRING *vstring_prepend(VSTRING *, const char *, ssize_t); 49 extern VSTRING *PRINTFLIKE(2, 3) vstring_sprintf(VSTRING *, const char *,...); 50 extern VSTRING *PRINTFLIKE(2, 3) vstring_sprintf_append(VSTRING *, const char *,...); 51 extern VSTRING *PRINTFLIKE(2, 3) vstring_sprintf_prepend(VSTRING *, const char *,...); 52 extern char *vstring_export(VSTRING *); 53 extern VSTRING *vstring_import(char *); 54 55 /* Legacy API: constant plus type-unchecked argument. */ 56 #define VSTRING_CTL_MAXLEN 1 57 #define VSTRING_CTL_END 0 58 59 /* Safer API: type-checked arguments. */ 60 #define CA_VSTRING_CTL_END VSTRING_CTL_END 61 #define CA_VSTRING_CTL_MAXLEN(val) VSTRING_CTL_MAXLEN, CHECK_VAL(VSTRING_CTL, ssize_t, (val)) 62 63 CHECK_VAL_HELPER_DCL(VSTRING_CTL, ssize_t); 64 65 /* 66 * Macros. Unsafe macros have UPPERCASE names. 67 */ 68 #define VSTRING_SPACE(vp, len) ((vp)->vbuf.space(&(vp)->vbuf, (len))) 69 #define vstring_str(vp) ((char *) (vp)->vbuf.data) 70 #define VSTRING_LEN(vp) ((ssize_t) ((vp)->vbuf.ptr - (vp)->vbuf.data)) 71 #define vstring_end(vp) ((char *) (vp)->vbuf.ptr) 72 #define VSTRING_TERMINATE(vp) do { \ 73 if ((vp)->vbuf.cnt <= 0) \ 74 VSTRING_SPACE((vp),1); \ 75 *(vp)->vbuf.ptr = 0; \ 76 } while (0) 77 #define VSTRING_RESET(vp) do { \ 78 (vp)->vbuf.ptr = (vp)->vbuf.data; \ 79 (vp)->vbuf.cnt = (vp)->vbuf.len; \ 80 } while (0) 81 #define VSTRING_ADDCH(vp, ch) VBUF_PUT(&(vp)->vbuf, ch) 82 #define VSTRING_SKIP(vp) do { \ 83 while ((vp)->vbuf.cnt > 0 && *(vp)->vbuf.ptr) \ 84 (vp)->vbuf.ptr++, (vp)->vbuf.cnt--; \ 85 } while (0) 86 #define vstring_avail(vp) ((vp)->vbuf.cnt) 87 88 /* 89 * The following macro is not part of the public interface, because it can 90 * really screw up a buffer by positioning past allocated memory. 91 */ 92 #define VSTRING_AT_OFFSET(vp, offset) do { \ 93 (vp)->vbuf.ptr = (vp)->vbuf.data + (offset); \ 94 (vp)->vbuf.cnt = (vp)->vbuf.len - (offset); \ 95 } while (0) 96 97 extern VSTRING *vstring_vsprintf(VSTRING *, const char *, va_list); 98 extern VSTRING *vstring_vsprintf_append(VSTRING *, const char *, va_list); 99 100 /* BUGS 101 /* Auto-resizing may change the address of the string data in 102 /* a vstring structure. Beware of dangling pointers. 103 /* HISTORY 104 /* .ad 105 /* .fi 106 /* A vstring module appears in the UNPROTO software by Wietse Venema. 107 /* LICENSE 108 /* .ad 109 /* .fi 110 /* The Secure Mailer license must be distributed with this software. 111 /* AUTHOR(S) 112 /* Wietse Venema 113 /* IBM T.J. Watson Research 114 /* P.O. Box 704 115 /* Yorktown Heights, NY 10598, USA 116 /*--*/ 117 118 #endif 119