libpcp  0.2.1
buffer.h
1 /*
2  This file is part of Pretty Curved Privacy (pcp1).
3 
4  Copyright (C) 2013-2014 T.v.Dein.
5 
6  This program is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 
19  You can contact me by mail: <tom AT vondein DOT org>.
20 */
21 
22 #ifndef HAVE_PCP_BUFFER_H
23 #define HAVE_PCP_BUFFER_H
24 
25 #include <stdio.h>
26 #include <stdarg.h>
27 #include "mem.h"
28 #include "util.h"
29 #include "defines.h"
30 
50 struct _pcp_buffer {
51  char *name;
52  uint8_t allocated;
53  size_t blocksize;
54  size_t size;
55  size_t offset;
56  size_t end;
57  uint8_t isstring;
58  void *buf;
59 };
60 
62 typedef struct _pcp_buffer Buffer;
63 
75 Buffer *buffer_new(size_t blocksize, char *name);
76 
86 Buffer *buffer_new_str(char *name);
87 
88 
155 Buffer *buffer_new_buf(char *name, void *data, size_t datasize);
156 
157 /* initialize buffer vars */
158 void buffer_init(Buffer *b, size_t blocksize, char *name);
159 
168 void buffer_free(Buffer *b);
169 
178 void buffer_clear(Buffer *b);
179 
187 void buffer_rewind(Buffer *b);
188 
203 void buffer_add(Buffer *b, const void *data, size_t len);
204 
217 void buffer_add_buf(Buffer *dst, Buffer *src);
218 
239 void buffer_add_str(Buffer *b, const char * fmt, ...);
240 
256 void buffer_add_hex(Buffer *b, void *data, size_t len);
257 
258 /* resize the buffer if necessary, used internally only */
259 void buffer_resize(Buffer *b, size_t len);
260 
270 int buffer_done(Buffer *b);
271 
303 size_t buffer_get_chunk(Buffer *b, void *buf, size_t len);
304 
319 unsigned char *buffer_get(Buffer *b);
320 
345 char *buffer_get_str(Buffer *b);
346 
381 unsigned char *buffer_get_remainder(Buffer *b);
382 
418 size_t buffer_extract(Buffer *b, void *buf, size_t offset, size_t len);
419 
424 void buffer_dump(const Buffer *b);
425 
430 void buffer_info(const Buffer *b);
431 
442 size_t buffer_size(const Buffer *b);
443 
472 size_t buffer_left(const Buffer *b);
473 
480 uint8_t buffer_get8(Buffer *b);
481 
488 uint16_t buffer_get16(Buffer *b);
489 
496 uint32_t buffer_get32(Buffer *b);
497 
504 uint64_t buffer_get64(Buffer *b);
505 
512 uint16_t buffer_get16na(Buffer *b);
513 
520 uint32_t buffer_get32na(Buffer *b);
521 
528 uint64_t buffer_get64na(Buffer *b);
529 
538 uint8_t buffer_last8(Buffer *b);
539 
548 uint16_t buffer_last16(Buffer *b);
549 
558 uint32_t buffer_last32(Buffer *b);
559 
568 uint64_t buffer_last64(Buffer *b);
569 
586 size_t buffer_fd_read(Buffer *b, FILE *in, size_t len);
587 
594 void buffer_add8(Buffer *b, uint8_t v);
595 
602 void buffer_add16(Buffer *b, uint16_t v);
603 
610 void buffer_add32(Buffer *b, uint32_t v);
611 
618 void buffer_add64(Buffer *b, uint64_t v);
619 
626 void buffer_add16be(Buffer *b, uint16_t v);
627 
634 void buffer_add32be(Buffer *b, uint32_t v);
635 
642 void buffer_add64be(Buffer *b, uint64_t v);
643 
644 
645 #endif // HAVE_PCP_BUFFER_H
646