mutt/buffer.c -rw-r--r-- 6.0 KiB View raw
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
 * Copyright (C) 1996-2002,2010,2013 Michael R. Elkins <me@mutt.org>
 * Copyright (C) 2004 g10 Code GmbH
 * Copyright (C) 2018 Kevin J. McCarthy <kevin@8t8.us>
 *
 *     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
 *     the Free Software Foundation; either version 2 of the License, or
 *     (at your option) any later version.
 *
 *     This program is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public License
 *     along with this program; if not, write to the Free Software
 *     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#if HAVE_CONFIG_H
# include "config.h"
#endif

#include "mutt.h"
#include "buffer.h"


static size_t BufferPoolCount = 0;
static size_t BufferPoolLen  = 0;
static BUFFER **BufferPool = NULL;


/* Creates and initializes a BUFFER */
BUFFER *mutt_buffer_new (void)
{
  BUFFER *b;

  b = safe_malloc (sizeof(BUFFER));

  mutt_buffer_init (b);

  return b;
}

/* Initialize a new BUFFER */
BUFFER *mutt_buffer_init (BUFFER *b)
{
  memset (b, 0, sizeof(BUFFER));
  return b;
}

void mutt_buffer_free (BUFFER **p)
{
  if (!p || !*p)
    return;

  FREE (&(*p)->data);
  /* dptr is just an offset to data and shouldn't be freed */
  FREE (p);		/* __FREE_CHECKED__ */
}

void mutt_buffer_clear (BUFFER *b)
{
  b->dptr = b->data;
  if (b->dptr)
    *(b->dptr) = '\0';
}

/* Creates and initializes a BUFFER by copying the seed string. */
BUFFER *mutt_buffer_from (char *seed)
{
  BUFFER *b;

  if (!seed)
    return NULL;

  b = mutt_buffer_new ();
  b->data = safe_strdup (seed);
  b->dsize = mutt_strlen (seed);
  b->dptr = (char *) b->data + b->dsize;
  return b;
}

size_t mutt_buffer_len (BUFFER *buf)
{
  return buf->dptr - buf->data;
}

/* Increases the allocated size of the buffer */
void mutt_buffer_increase_size (BUFFER *buf, size_t new_size)
{
  size_t offset;

  if (buf->dsize < new_size)
  {
    offset = buf->dptr - buf->data;
    buf->dsize = new_size;
    safe_realloc (&buf->data, buf->dsize);
    buf->dptr = buf->data + offset;
    /* This ensures an initially NULL buf->data is now properly terminated. */
    *(buf->dptr) = '\0';
  }
}

/* Ensure buffer->dptr points to the end of the buffer. */
void mutt_buffer_fix_dptr (BUFFER *buf)
{
  buf->dptr = buf->data;

  if (buf->data)
  {
    buf->data[buf->dsize - 1] = '\0';
    buf->dptr = strchr (buf->data, '\0');
  }
}

static int _mutt_buffer_add_printf (BUFFER* buf, const char* fmt, va_list ap)
{
  va_list ap_retry;
  int len, blen, doff;

  va_copy (ap_retry, ap);

  if (!buf->dptr)
    buf->dptr = buf->data;

  doff = buf->dptr - buf->data;
  blen = buf->dsize - doff;
  /* solaris 9 vsnprintf barfs when blen is 0 */
  if (!blen)
  {
    blen = 128;
    mutt_buffer_increase_size (buf, buf->dsize + blen);
  }
  if ((len = vsnprintf (buf->dptr, blen, fmt, ap)) >= blen)
  {
    blen = ++len - blen;
    if (blen < 128)
      blen = 128;
    mutt_buffer_increase_size (buf, buf->dsize + blen);
    len = vsnprintf (buf->dptr, len, fmt, ap_retry);
  }
  if (len > 0)
    buf->dptr += len;

  va_end (ap_retry);

  return len;
}

int mutt_buffer_printf (BUFFER* buf, const char* fmt, ...)
{
  va_list ap;
  int rv;

  va_start (ap, fmt);
  mutt_buffer_clear (buf);
  rv = _mutt_buffer_add_printf (buf, fmt, ap);
  va_end (ap);

  return rv;
}

int mutt_buffer_add_printf (BUFFER* buf, const char* fmt, ...)
{
  va_list ap;
  int rv;

  va_start (ap, fmt);
  rv = _mutt_buffer_add_printf (buf, fmt, ap);
  va_end (ap);

  return rv;
}

/* Dynamically grows a BUFFER to accommodate s, in increments of 128 bytes.
 * Always one byte bigger than necessary for the null terminator, and
 * the buffer is always null-terminated */
void mutt_buffer_addstr_n (BUFFER* buf, const char* s, size_t len)
{
  if (buf->dptr + len + 1 > buf->data + buf->dsize)
    mutt_buffer_increase_size (buf, buf->dsize + (len < 128 ? 128 : len + 1));
  memcpy (buf->dptr, s, len);
  buf->dptr += len;
  *(buf->dptr) = '\0';
}

void mutt_buffer_addstr (BUFFER* buf, const char* s)
{
  mutt_buffer_addstr_n (buf, s, mutt_strlen (s));
}

void mutt_buffer_addch (BUFFER* buf, char c)
{
  mutt_buffer_addstr_n (buf, &c, 1);
}

void mutt_buffer_strcpy (BUFFER *buf, const char *s)
{
  mutt_buffer_clear (buf);
  mutt_buffer_addstr (buf, s);
}

void mutt_buffer_strcpy_n (BUFFER *buf, const char *s, size_t len)
{
  mutt_buffer_clear (buf);
  mutt_buffer_addstr_n (buf, s, len);
}


static void increase_buffer_pool (void)
{
  BUFFER *newbuf;

  BufferPoolLen += 5;
  safe_realloc (&BufferPool, BufferPoolLen * sizeof (BUFFER *));
  while (BufferPoolCount < 5)
  {
    newbuf = mutt_buffer_new ();
    mutt_buffer_increase_size (newbuf, LONG_STRING);
    mutt_buffer_clear (newbuf);
    BufferPool[BufferPoolCount++] = newbuf;
  }
}

void mutt_buffer_pool_init (void)
{
  increase_buffer_pool ();
}

void mutt_buffer_pool_free (void)
{
  dprint (1, (debugfile,
              "mutt_buffer_pool_free: %zu of %zu returned to pool\n",
              BufferPoolCount, BufferPoolLen));

  while (BufferPoolCount)
    mutt_buffer_free (&BufferPool[--BufferPoolCount]);
  FREE (&BufferPool);
  BufferPoolLen = 0;
}

BUFFER *mutt_buffer_pool_get (void)
{
  if (!BufferPoolCount)
    increase_buffer_pool ();
  return BufferPool[--BufferPoolCount];
}

void mutt_buffer_pool_release (BUFFER **pbuf)
{
  BUFFER *buf;

  if (!pbuf || !*pbuf)
    return;

  if (BufferPoolCount >= BufferPoolLen)
  {
    dprint (1, (debugfile, "Internal buffer pool error\n"));
    mutt_buffer_free (pbuf);
    return;
  }

  buf = *pbuf;
  if (buf->dsize > LONG_STRING*2)
  {
    buf->dsize = LONG_STRING;
    safe_realloc (&buf->data, buf->dsize);
  }
  mutt_buffer_clear (buf);
  buf->destroy = 0;
  BufferPool[BufferPoolCount++] = buf;

  *pbuf = NULL;
}