| 1 |
/* stringbuf.h - Inline functions for building strings. |
| 2 |
* Copyright (C) 2001, 2002, 2007, 2008, 2012, 2020 g10 Code GmbH |
| 3 |
* |
| 4 |
* This file is part of KSBA. |
| 5 |
* |
| 6 |
* KSBA is free software; you can redistribute it and/or modify |
| 7 |
* it under the terms of either |
| 8 |
* |
| 9 |
* - the GNU Lesser General Public License as published by the Free |
| 10 |
* Software Foundation; either version 3 of the License, or (at |
| 11 |
* your option) any later version. |
| 12 |
* |
| 13 |
* or |
| 14 |
* |
| 15 |
* - the GNU General Public License as published by the Free |
| 16 |
* Software Foundation; either version 2 of the License, or (at |
| 17 |
* your option) any later version. |
| 18 |
* |
| 19 |
* or both in parallel, as here. |
| 20 |
* |
| 21 |
* KSBA is distributed in the hope that it will be useful, but WITHOUT |
| 22 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 23 |
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public |
| 24 |
* License for more details. |
| 25 |
* |
| 26 |
* You should have received a copies of the GNU General Public License |
| 27 |
* and the GNU Lesser General Public License along with this program; |
| 28 |
* if not, see <http://www.gnu.org/licenses/>. |
| 29 |
*/ |
| 30 |
|
| 31 |
#ifndef STRINGBUF_H |
| 32 |
#define STRINGBUF_H 1 |
| 33 |
|
| 34 |
#include "util.h" |
| 35 |
#include "errno.h" |
| 36 |
|
| 37 |
struct stringbuf |
| 38 |
{ |
| 39 |
size_t len; |
| 40 |
size_t size; |
| 41 |
char *buf; |
| 42 |
gpg_error_t out_of_core; |
| 43 |
}; |
| 44 |
|
| 45 |
|
| 46 |
static inline void |
| 47 |
init_stringbuf (struct stringbuf *sb, int initiallen) |
| 48 |
{ |
| 49 |
sb->len = 0; |
| 50 |
sb->size = initiallen; |
| 51 |
sb->out_of_core = 0; |
| 52 |
/* allocate one more, so that get_stringbuf can append a nul */ |
| 53 |
sb->buf = xtrymalloc (initiallen+1); |
| 54 |
if (!sb->buf) |
| 55 |
sb->out_of_core = errno? errno : ENOMEM; |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
static inline void |
| 60 |
deinit_stringbuf (struct stringbuf *sb) |
| 61 |
{ |
| 62 |
xfree (sb->buf); |
| 63 |
sb->buf = NULL; |
| 64 |
sb->out_of_core = ENOMEM; /* make sure the caller does an init before reuse */ |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
static inline void |
| 69 |
put_stringbuf_mem (struct stringbuf *sb, const char *text, size_t n) |
| 70 |
{ |
| 71 |
if (sb->out_of_core) |
| 72 |
return; |
| 73 |
|
| 74 |
if (sb->len + n >= sb->size) |
| 75 |
{ |
| 76 |
char *p; |
| 77 |
|
| 78 |
sb->size += n + 100; |
| 79 |
p = xtryrealloc (sb->buf, sb->size + 1); |
| 80 |
if (!p) |
| 81 |
{ |
| 82 |
sb->out_of_core = errno? errno : ENOMEM; |
| 83 |
return; |
| 84 |
} |
| 85 |
sb->buf = p; |
| 86 |
} |
| 87 |
memcpy (sb->buf+sb->len, text, n); |
| 88 |
sb->len += n; |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
static inline void |
| 93 |
put_stringbuf_mem_skip (struct stringbuf *sb, const char *text, size_t n, |
| 94 |
int skip) |
| 95 |
{ |
| 96 |
char *p; |
| 97 |
|
| 98 |
if (!skip) |
| 99 |
{ |
| 100 |
put_stringbuf_mem (sb, text, n); |
| 101 |
return; |
| 102 |
} |
| 103 |
if (sb->out_of_core) |
| 104 |
return; |
| 105 |
|
| 106 |
if (sb->len + n >= sb->size) |
| 107 |
{ |
| 108 |
/* Note: we allocate too much here, but we don't care. */ |
| 109 |
sb->size += n + 100; |
| 110 |
p = xtryrealloc (sb->buf, sb->size + 1); |
| 111 |
if ( !p) |
| 112 |
{ |
| 113 |
sb->out_of_core = errno? errno : ENOMEM; |
| 114 |
return; |
| 115 |
} |
| 116 |
sb->buf = p; |
| 117 |
} |
| 118 |
p = sb->buf+sb->len; |
| 119 |
while (n > skip) |
| 120 |
{ |
| 121 |
text += skip; |
| 122 |
n -= skip; |
| 123 |
*p++ = *text++; |
| 124 |
n--; |
| 125 |
sb->len++; |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
static inline void |
| 131 |
put_stringbuf (struct stringbuf *sb, const char *text) |
| 132 |
{ |
| 133 |
put_stringbuf_mem (sb, text,strlen (text)); |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
static inline void |
| 138 |
put_stringbuf_mem_sexp (struct stringbuf *sb, const char *text, size_t length) |
| 139 |
{ |
| 140 |
char buf[20]; |
| 141 |
sprintf (buf,"%u:", (unsigned int)length); |
| 142 |
put_stringbuf (sb, buf); |
| 143 |
put_stringbuf_mem (sb, text, length); |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
static inline void |
| 148 |
put_stringbuf_sexp (struct stringbuf *sb, const char *text) |
| 149 |
{ |
| 150 |
put_stringbuf_mem_sexp (sb, text, strlen (text)); |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
static inline void |
| 155 |
put_stringbuf_uint (struct stringbuf *sb, unsigned int value) |
| 156 |
{ |
| 157 |
char buf[35]; |
| 158 |
snprintf (buf, sizeof buf, "%u", (unsigned int)value); |
| 159 |
put_stringbuf_sexp (sb, buf); |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
static inline char * |
| 164 |
get_stringbuf (struct stringbuf *sb) |
| 165 |
{ |
| 166 |
char *p; |
| 167 |
|
| 168 |
if (sb->out_of_core) |
| 169 |
{ |
| 170 |
xfree (sb->buf); sb->buf = NULL; |
| 171 |
gpg_err_set_errno (sb->out_of_core); |
| 172 |
return NULL; |
| 173 |
} |
| 174 |
|
| 175 |
sb->buf[sb->len] = 0; |
| 176 |
p = sb->buf; |
| 177 |
sb->buf = NULL; |
| 178 |
sb->out_of_core = ENOMEM; /* make sure the caller does an init before reuse */ |
| 179 |
return p; |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
#endif /*STRINGBUF_H*/ |