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

« back to all changes in this revision

Viewing changes to buffer.h

  • 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.h
 
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
#ifndef NETTLE_BUFFER_H_INCLUDED
 
27
#define NETTLE_BUFFER_H_INCLUDED
 
28
 
 
29
#include "realloc.h"
 
30
 
 
31
struct nettle_buffer
 
32
{
 
33
  uint8_t *contents;
 
34
  /* Allocated size */
 
35
  unsigned alloc;
 
36
 
 
37
  void *realloc_ctx;
 
38
  nettle_realloc_func *realloc;
 
39
 
 
40
  /* Current size */
 
41
  unsigned size;
 
42
};
 
43
 
 
44
/* Initializes a buffer that uses plain realloc */
 
45
void
 
46
nettle_buffer_init(struct nettle_buffer *buffer);
 
47
 
 
48
void
 
49
nettle_buffer_init_realloc(struct nettle_buffer *buffer,
 
50
                           void *realloc_ctx,
 
51
                           nettle_realloc_func realloc);
 
52
 
 
53
/* Initializes a buffer of fix size */
 
54
void
 
55
nettle_buffer_init_size(struct nettle_buffer *buffer,
 
56
                        unsigned length, uint8_t *space);
 
57
 
 
58
void
 
59
nettle_buffer_clear(struct nettle_buffer *buffer);
 
60
 
 
61
/* Resets the buffer, without freeing the buffer space. */
 
62
void
 
63
nettle_buffer_reset(struct nettle_buffer *buffer);
 
64
 
 
65
int
 
66
nettle_buffer_grow(struct nettle_buffer *buffer,
 
67
                   unsigned length);
 
68
 
 
69
#define NETTLE_BUFFER_PUTC(buffer, c) \
 
70
( (((buffer)->size < (buffer)->alloc) || nettle_buffer_grow((buffer), 1)) \
 
71
  && ((buffer)->contents[(buffer)->size++] = (c), 1) )
 
72
 
 
73
int
 
74
nettle_buffer_write(struct nettle_buffer *buffer,
 
75
                    unsigned length, const uint8_t *data);
 
76
 
 
77
/* Like nettle_buffer_write, but instead of copying data to the
 
78
 * buffer, it returns a pointer to the area where the caller can copy
 
79
 * the data. The pointer is valid only until the next call that can
 
80
 * reallocate the buffer. */
 
81
uint8_t *
 
82
nettle_buffer_space(struct nettle_buffer *buffer,
 
83
                    unsigned length);
 
84
 
 
85
/* Copy the contents of SRC to the end of DST. */
 
86
int
 
87
nettle_buffer_copy(struct nettle_buffer *dst,
 
88
                   const struct nettle_buffer *src);
 
89
 
 
90
#endif /* NETTLE_BUFFER_H_INCLUDED */