~ubuntu-branches/ubuntu/saucy/nettle/saucy-proposed

« back to all changes in this revision

Viewing changes to buffer.c

  • Committer: Bazaar Package Importer
  • Author(s): Marek Habersack
  • Date: 2004-05-04 15:56:02 UTC
  • Revision ID: james.westby@ubuntu.com-20040504155602-7jbhw5mabvwksl3j
Tags: upstream-1.10
Import upstream version 1.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* buffer.c
 
2
 *
 
3
 * A bare-bones string stream.
 
4
 */
 
5
 
 
6
/* nettle, low-level cryptographics library
 
7
 *
 
8
 * Copyright (C) 2002 Niels M�ller
 
9
 *  
 
10
 * The nettle library is free software; you can redistribute it and/or modify
 
11
 * it under the terms of the GNU Lesser General Public License as published by
 
12
 * the Free Software Foundation; either version 2.1 of the License, or (at your
 
13
 * option) any later version.
 
14
 * 
 
15
 * The nettle library is distributed in the hope that it will be useful, but
 
16
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 
17
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
 
18
 * License for more details.
 
19
 * 
 
20
 * You should have received a copy of the GNU Lesser General Public License
 
21
 * along with the nettle library; see the file COPYING.LIB.  If not, write to
 
22
 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
 
23
 * MA 02111-1307, USA.
 
24
 */
 
25
 
 
26
#if HAVE_CONFIG_H
 
27
# include "config.h"
 
28
#endif
 
29
 
 
30
#include <assert.h>
 
31
#include <stdlib.h>
 
32
#include <string.h>
 
33
 
 
34
#include "buffer.h"
 
35
 
 
36
int
 
37
nettle_buffer_grow(struct nettle_buffer *buffer,
 
38
                   unsigned length)
 
39
{
 
40
  assert(buffer->size <= buffer->alloc);
 
41
  
 
42
  if (buffer->size + length > buffer->alloc)
 
43
    {
 
44
      unsigned alloc;
 
45
      uint8_t *p;
 
46
      
 
47
      if (!buffer->realloc)
 
48
        return 0;
 
49
      
 
50
      alloc = buffer->alloc * 2 + length + 100;
 
51
      p = buffer->realloc(buffer->realloc_ctx, buffer->contents, alloc);
 
52
      if (!p)
 
53
        return 0;
 
54
      
 
55
      buffer->contents = p;
 
56
      buffer->alloc = alloc;
 
57
    }
 
58
  return 1;
 
59
}
 
60
 
 
61
void
 
62
nettle_buffer_init_realloc(struct nettle_buffer *buffer,
 
63
                           void *realloc_ctx,
 
64
                           nettle_realloc_func realloc)
 
65
{
 
66
  buffer->contents = NULL;
 
67
  buffer->alloc = 0;
 
68
  buffer->realloc = realloc;
 
69
  buffer->realloc_ctx = realloc_ctx;
 
70
  buffer->size = 0;
 
71
}
 
72
 
 
73
void
 
74
nettle_buffer_init_size(struct nettle_buffer *buffer,
 
75
                        unsigned length, uint8_t *space)
 
76
{
 
77
  buffer->contents = space;
 
78
  buffer->alloc = length;
 
79
  buffer->realloc = NULL;
 
80
  buffer->realloc_ctx = NULL;
 
81
  buffer->size = 0;
 
82
}
 
83
 
 
84
void
 
85
nettle_buffer_clear(struct nettle_buffer *buffer)
 
86
{
 
87
  if (buffer->realloc)
 
88
    buffer->realloc(buffer->realloc_ctx, buffer->contents, 0);
 
89
 
 
90
  buffer->contents = NULL;
 
91
  buffer->alloc = 0;
 
92
  buffer->size = 0;
 
93
}
 
94
 
 
95
void
 
96
nettle_buffer_reset(struct nettle_buffer *buffer)
 
97
{
 
98
  buffer->size = 0;
 
99
}
 
100
 
 
101
uint8_t *
 
102
nettle_buffer_space(struct nettle_buffer *buffer,
 
103
                    unsigned length)
 
104
{
 
105
  uint8_t *p;
 
106
 
 
107
  if (!nettle_buffer_grow(buffer, length))
 
108
    return NULL;
 
109
 
 
110
  p = buffer->contents + buffer->size;
 
111
  buffer->size += length;
 
112
  return p;
 
113
}
 
114
     
 
115
int
 
116
nettle_buffer_write(struct nettle_buffer *buffer,
 
117
                    unsigned length, const uint8_t *data)
 
118
{
 
119
  uint8_t *p = nettle_buffer_space(buffer, length);
 
120
  if (p)
 
121
    {
 
122
      memcpy(p, data, length);
 
123
      return 1;
 
124
    }
 
125
  else
 
126
    return 0;
 
127
}
 
128
 
 
129
int
 
130
nettle_buffer_copy(struct nettle_buffer *dst,
 
131
                   const struct nettle_buffer *src)
 
132
{
 
133
  return nettle_buffer_write(dst, src->size, src->contents);
 
134
}