Open SCAP Library
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
spb.h
1 /*
2  * Copyright 2009 Red Hat Inc., Durham, North Carolina.
3  * All Rights Reserved.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Authors:
20  * "Daniel Kopecek" <dkopecek@redhat.com>
21  */
22 
23 #pragma once
24 #ifndef SPB_H
25 #define SPB_H
26 
27 #include <stddef.h>
28 #include <inttypes.h>
29 #include <common/util.h>
30 
31 #define __XC(a, b) OSCAP_CONCAT(a, b)
32 #define _sym(n) __XC(__XC(_sym, __LINE__), n)
33 
34 typedef uint32_t spb_flags_t;
35 
36 #define SPB_FLAG_FREE 0x00000001
37 #define SPB_FLAG_JOIN 0x00000002
38 #define SPB_FLAG_FILE 0x00000004
39 
40 typedef uint64_t spb_size_t;
41 #define SPB_SZ_FMT "%"PRIo64
42 
43 typedef struct {
44  void *base; /* buffer base address */
45  spb_size_t gend; /* sparse buffer index of the last byte of this buffer */
46 } spb_item_t;
47 
48 typedef struct {
49  spb_item_t *buffer; /* array of buffers representing the sparse buffer */
50  uint32_t btotal; /* number of buffer */
51  uint32_t balloc; /* number of allocated slots */
52  uint32_t bflags; /* flags - not used at the moment */
53 } spb_t;
54 
55 #define SPB_DEFAULT_BALLOC 32 /* default number of pre-allocated slots */
56 #define SPB_BALLOC_HIGHTRESH 512 /* the number of new slots is doubled until this limit is reached */
57 #define SPB_BALLOC_ADD 32 /* number of slot to add when we reached the high threshold */
58 
67 spb_t *spb_new (void *buffer, size_t buflen, uint32_t balloc);
68 
69 void spb_free (spb_t *spb, spb_flags_t flags);
70 
77 uint32_t spb_bindex (spb_t *spb, spb_size_t index);
78 
83 spb_size_t spb_size (spb_t *spb);
84 
93 #define spb_iterate_oct(spb, start, end, name) /* TODO */ while(0)
94 
95 #define spb_iterate(spb, start, name, icode) \
96  do { \
97  spb_size_t _sym(istart) = (start); \
98  spb_t *_sym(ispb) = (spb); \
99  uint32_t _sym(idx) = spb_bindex(_sym(ispb), _sym(istart)); \
100  size_t _sym(l_off) = (size_t)(_sym(idx) > 0 ? start - _sym(ispb)->buffer[_sym(idx) - 1].gend - 1 : start); \
101  \
102  for (; _sym(idx) < _sym(ispb)->btotal; ++_sym(idx)) { \
103  register size_t _sym(l); \
104  register uint8_t *_sym(b); \
105  \
106  _sym(l) = (size_t)(_sym(idx) > 0 ? \
107  _sym(ispb)->buffer[_sym(idx)].gend - _sym(ispb)->buffer[_sym(idx) - 1].gend : \
108  _sym(ispb)->buffer[_sym(idx)].gend + 1) - _sym(l_off); \
109  _sym(b) = ((uint8_t *)(_sym(ispb)->buffer[_sym(idx)].base)) + _sym(l_off); \
110  \
111  for (; _sym(l) > 0; --_sym(l), ++_sym(b)) { \
112  (name) = *_sym(b); \
113  icode; \
114  } \
115  \
116  if (_sym(l) > 0) \
117  break; \
118  \
119  _sym(l_off) = 0; \
120  } \
121  } while (0)
122 
129 int spb_add (spb_t *spb, void *buffer, size_t buflen);
130 
138 int spb_pick (spb_t *spb, spb_size_t start, spb_size_t size, void *dst);
139 
150 int spb_pick_raw (spb_t *spb, uint32_t bindex, spb_size_t start, spb_size_t size, void *dst);
151 
164 int spb_pick_cb (spb_t *spb, spb_size_t start, spb_size_t size, void *cb (void *, void *, size_t), void *cbarg);
165 
166 spb_size_t spb_drop_head (spb_t *spb, spb_size_t size, spb_flags_t flags);
167 
168 uint8_t spb_octet (spb_t *spb, spb_size_t idx);
169 const uint8_t *spb_direct (spb_t *spb, spb_size_t start, spb_size_t size);
170 
171 #endif /* SPB_H */
Definition: spb.h:43
Definition: spb.h:48