summaryrefslogtreecommitdiffstats
path: root/src/libply/ply-buffer.c
blob: 4fd1e43bea1111ab85ac4fb7b0d43728ae54cf3a (plain)
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
/* ply-buffer.c - facilities for buffering data
 *
 * Copyright (C) 2008 Red Hat, Inc.
 *
 * 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, 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., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 *
 * Written by: Ray Strode <rstrode@redhat.com>
 */
#include "config.h"
#include "ply-buffer.h"

#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/fcntl.h>
#include <sys/file.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

#include "ply-utils.h"

#ifndef PLY_BUFFER_MAX_APPEND_SIZE
#define PLY_BUFFER_MAX_APPEND_SIZE 1024
#endif

#ifndef PLY_BUFFER_MAX_BUFFER_CAPACITY
#define PLY_BUFFER_MAX_BUFFER_CAPACITY (8 * 4096)
#endif

struct _ply_buffer
{
  char   *data;
  size_t  size;
  size_t  capacity;
};

static bool
ply_buffer_increase_capacity (ply_buffer_t *buffer)
{
  assert (buffer != NULL);

  if ((buffer->capacity * 2) > PLY_BUFFER_MAX_BUFFER_CAPACITY)
    return false;

  buffer->capacity *= 2;

  buffer->data = realloc (buffer->data, buffer->capacity);
  return true;
}

void
ply_buffer_remove_bytes (ply_buffer_t *buffer,
                         size_t        bytes_to_remove)
{
  assert (buffer != NULL);

  bytes_to_remove = MIN (buffer->size, bytes_to_remove);

  if (bytes_to_remove == buffer->size)
    buffer->size = 0;
  else
    {
      memmove (buffer->data, buffer->data + bytes_to_remove,
               buffer->size - bytes_to_remove);
      buffer->size -= bytes_to_remove;
    }
}

void
ply_buffer_remove_bytes_at_end (ply_buffer_t *buffer,
                                size_t        bytes_to_remove)
{
  assert (buffer != NULL);

  bytes_to_remove = MIN (buffer->size, bytes_to_remove);

  buffer->size -= bytes_to_remove;
}

ply_buffer_t *
ply_buffer_new (void)
{
  ply_buffer_t *buffer;

  buffer = calloc (1, sizeof (ply_buffer_t));

  buffer->capacity = 4096;
  buffer->data = calloc (1, buffer->capacity);
  buffer->size = 0;

  return buffer;
}

void
ply_buffer_free (ply_buffer_t *buffer)
{
  if (buffer == NULL)
    return;

  free (buffer->data);
  free (buffer);
}

static bool
ply_buffer_validate_format_string (ply_buffer_t   *buffer,
                                   const char     *format)
{
  char *n, *p;

  p = (char *) format;

  /* lame checks to limit the damage
   * of some potential exploits.
   */
  while ((n = strstr (p, "%n")) != NULL)
    {
      if (n == format)
          return false;

      if (n[-1] != '%')
          return false;

      p = n + 1;
    }

  return true;
}

void
ply_buffer_append_with_non_literal_format_string (ply_buffer_t   *buffer,
                                                  const char     *format,
                                                  ...)
{
  va_list args;
  size_t string_size;
  char write_buffer[PLY_BUFFER_MAX_APPEND_SIZE] = "";

  assert (buffer != NULL);

  if (!ply_buffer_validate_format_string (buffer, format))
    return;

  va_start (args, format);
  string_size = vsnprintf (write_buffer, 0, format, args) + 1;
  va_end (args);

  if (string_size > PLY_BUFFER_MAX_APPEND_SIZE)
    return;

  va_start (args, format);
  vsnprintf (write_buffer, PLY_BUFFER_MAX_APPEND_SIZE,
             format, args);
  va_end (args);

  ply_buffer_append_bytes (buffer, write_buffer, string_size - 1);
}

void
ply_buffer_append_bytes (ply_buffer_t *buffer,
                         const void   *bytes,
                         size_t        length)
{
  assert (buffer != NULL);
  assert (bytes != NULL);
  assert (length != 0);

  if ((buffer->size + length) >= buffer->capacity)
    {
      if (!ply_buffer_increase_capacity (buffer))
        {
          ply_buffer_remove_bytes (buffer, length);

          if ((buffer->size + length) >= buffer->capacity)
            if (!ply_buffer_increase_capacity (buffer))
              return;
        }
    }

  assert (buffer->size + length < buffer->capacity);

  memcpy (buffer->data + buffer->size,
          bytes, length);

  buffer->size += length;
}

void
ply_buffer_append_from_fd (ply_buffer_t *buffer,
                           int           fd)
{

  char bytes[PLY_BUFFER_MAX_APPEND_SIZE] = "";
  ssize_t bytes_read;

  assert (buffer != NULL);
  assert (fd >= 0);

  if (!ply_fd_has_data (fd))
    return;

  bytes_read = read (fd, bytes, sizeof (bytes));

  if (bytes_read > 0)
    ply_buffer_append_bytes (buffer, bytes, bytes_read);
}

const char *
ply_buffer_get_bytes (ply_buffer_t *buffer)
{
  assert (buffer != NULL);
  return buffer->data;
}

char *
ply_buffer_steal_bytes (ply_buffer_t *buffer)
{
  char *bytes;
  assert (buffer != NULL);

  bytes = buffer->data;

  buffer->data = calloc (1, buffer->capacity);
  buffer->size = 0;

  return bytes;
}

size_t
ply_buffer_get_size (ply_buffer_t *buffer)
{
  return buffer->size;
}

void
ply_buffer_clear (ply_buffer_t *buffer)
{
  buffer->size = 0;
}

#ifdef PLY_BUFFER_ENABLE_TEST
int
main (int    argc,
      char **argv)
{
  int exit_code;
  ply_buffer_t *buffer;

  exit_code = 0;
  buffer = ply_buffer_new ();

  ply_buffer_append (buffer, "yo yo yo\n");
  ply_buffer_free (buffer);

  return exit_code;
}

#endif /* PLY_BUFFER_ENABLE_TEST */
/* vim: set ts=4 sw=4 expandtab autoindent cindent cino={.5s,(0: */