Open SCAP Library
/home/pvrabec/project/openscap/openscap-0.7.3/src/OVAL/probes/SEAP/generic/spb.h
00001 /*
00002  * Copyright 2009 Red Hat Inc., Durham, North Carolina.
00003  * All Rights Reserved.
00004  *
00005  * This library is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU Lesser General Public
00007  * License as published by the Free Software Foundation; either
00008  * version 2.1 of the License, or (at your option) any later version.
00009  *
00010  * This library is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  * Lesser General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU Lesser General Public
00016  * License along with this library; if not, write to the Free Software
00017  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018  *
00019  * Authors:
00020  *      "Daniel Kopecek" <dkopecek@redhat.com>
00021  */
00022 
00023 #pragma once
00024 #ifndef SPB_H
00025 #define SPB_H
00026 
00027 #include <stddef.h>
00028 #include <inttypes.h>
00029 #include <common/util.h>
00030 
00031 #define __XC(a, b) OSCAP_CONCAT(a, b)
00032 #define _sym(n) __XC(__XC(_sym, __LINE__), n)
00033 
00034 typedef uint32_t spb_flags_t;
00035 
00036 #define SPB_FLAG_FREE 0x00000001
00037 #define SPB_FLAG_JOIN 0x00000002
00038 #define SPB_FLAG_FILE 0x00000004
00039 
00040 typedef uint64_t spb_size_t;
00041 #define SPB_SZ_FMT "%"PRIo64
00042 
00043 typedef struct {
00044         void       *base; /* buffer base address */
00045         spb_size_t  gend; /* sparse buffer index of the last byte of this buffer */
00046 } spb_item_t;
00047 
00048 typedef struct {
00049         spb_item_t *buffer; /* array of buffers representing the sparse buffer */
00050         uint32_t    btotal; /* number of buffer */
00051         uint32_t    balloc; /* number of allocated slots */
00052         uint32_t    bflags; /* flags - not used at the moment */
00053 } spb_t;
00054 
00055 #define SPB_DEFAULT_BALLOC   32  /* default number of pre-allocated slots */
00056 #define SPB_BALLOC_HIGHTRESH 512 /* the number of new slots is doubled until this limit is reached */
00057 #define SPB_BALLOC_ADD       32  /* number of slot to add when we reached the high threshold */
00058 
00067 spb_t *spb_new (void *buffer, size_t buflen, uint32_t balloc);
00068 
00069 void spb_free (spb_t *spb, spb_flags_t flags);
00070 
00077 uint32_t spb_bindex (spb_t *spb, spb_size_t index);
00078 
00083 spb_size_t spb_size (spb_t *spb);
00084 
00093 #define spb_iterate_oct(spb, start, end, name) /* TODO */ while(0)
00094 
00095 #define spb_iterate(spb, start, name, icode)                            \
00096         do {                                                            \
00097                 spb_size_t _sym(istart) = (start);                      \
00098                 spb_t     *_sym(ispb)   = (spb);                        \
00099                 uint32_t   _sym(idx)    = spb_bindex(_sym(ispb), _sym(istart)); \
00100                 size_t     _sym(l_off)  = (size_t)(_sym(idx) > 0 ? start - _sym(ispb)->buffer[_sym(idx) - 1].gend - 1 : start); \
00101                                                                         \
00102                 for (; _sym(idx) < _sym(ispb)->btotal; ++_sym(idx)) {   \
00103                         register size_t   _sym(l);                      \
00104                         register uint8_t *_sym(b);                      \
00105                                                                         \
00106                         _sym(l) = (size_t)(_sym(idx) > 0 ?              \
00107                                            _sym(ispb)->buffer[_sym(idx)].gend - _sym(ispb)->buffer[_sym(idx) - 1].gend : \
00108                                            _sym(ispb)->buffer[_sym(idx)].gend + 1) - _sym(l_off); \
00109                         _sym(b) = ((uint8_t *)(_sym(ispb)->buffer[_sym(idx)].base)) + _sym(l_off); \
00110                                                                         \
00111                         for (; _sym(l) > 0; --_sym(l), ++_sym(b)) {     \
00112                                 (name) = *_sym(b);                      \
00113                                 icode;                                  \
00114                         }                                               \
00115                                                                         \
00116                         if (_sym(l) > 0)                                \
00117                                 break;                                  \
00118                                                                         \
00119                         _sym(l_off) = 0;                                \
00120                 }                                                       \
00121         } while (0)
00122 
00129 int spb_add (spb_t *spb, void *buffer, size_t buflen);
00130 
00138 int spb_pick (spb_t *spb, spb_size_t start, spb_size_t size, void *dst);
00139 
00150 int spb_pick_raw (spb_t *spb, uint32_t bindex, spb_size_t start, spb_size_t size, void *dst);
00151 
00164 int spb_pick_cb (spb_t *spb, spb_size_t start, spb_size_t size, void *cb (void *, void *, size_t), void *cbarg);
00165 
00166 spb_size_t spb_drop_head (spb_t *spb, spb_size_t size, spb_flags_t flags);
00167 
00168 uint8_t spb_octet (spb_t *spb, spb_size_t idx);
00169 const uint8_t *spb_direct (spb_t *spb, spb_size_t start, spb_size_t size);
00170 
00171 #endif /* SPB_H */