1 /* 2 * Copyright (C) 2007, 2012 Stefan Walter 3 * Copyright (C) 2012 Red Hat Inc. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 9 * * Redistributions of source code must retain the above 10 * copyright notice, this list of conditions and the 11 * following disclaimer. 12 * * Redistributions in binary form must reproduce the 13 * above copyright notice, this list of conditions and 14 * the following disclaimer in the documentation and/or 15 * other materials provided with the distribution. 16 * * The names of contributors to this software may not be 17 * used to endorse or promote products derived from this 18 * software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 24 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 27 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 30 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 31 * DAMAGE. 32 * 33 * Author: Stef Walter <stef@thewalter.net> 34 */ 35 36 #ifndef P11_BUFFER_H_ 37 #define P11_BUFFER_H_ 38 39 #include "compat.h" 40 41 enum { 42 P11_BUFFER_FAILED = 1 << 0, 43 P11_BUFFER_NULL = 1 << 1, 44 }; 45 46 typedef struct { 47 void *data; 48 size_t len; 49 50 int flags; 51 size_t size; 52 void * (* frealloc) (void *, size_t); 53 void (* ffree) (void *); 54 } p11_buffer; 55 56 bool p11_buffer_init (p11_buffer *buffer, 57 size_t size); 58 59 bool p11_buffer_init_null (p11_buffer *buffer, 60 size_t size); 61 62 void p11_buffer_init_full (p11_buffer *buffer, 63 void *data, 64 size_t len, 65 int flags, 66 void * (* frealloc) (void *, size_t), 67 void (* ffree) (void *)); 68 69 void p11_buffer_uninit (p11_buffer *buffer); 70 71 void * p11_buffer_steal (p11_buffer *buffer, 72 size_t *length); 73 74 bool p11_buffer_reset (p11_buffer *buffer, 75 size_t size); 76 77 void * p11_buffer_append (p11_buffer *buffer, 78 size_t length); 79 80 void p11_buffer_add (p11_buffer *buffer, 81 const void *data, 82 ssize_t length); 83 84 #define p11_buffer_fail(buf) \ 85 ((buf)->flags |= P11_BUFFER_FAILED) 86 87 #define p11_buffer_ok(buf) \ 88 (((buf)->flags & P11_BUFFER_FAILED) ? false : true) 89 90 #define p11_buffer_failed(buf) \ 91 (((buf)->flags & P11_BUFFER_FAILED) ? true : false) 92 93 #endif /* BUFFER_H */ 94