| 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | * |
| 19 | * Authors: |
| 20 | * "Daniel Kopecek" <[email protected]> |
| 21 | */ |
| 22 | |
| 23 | #ifdef HAVE_CONFIG_Hcxx . . $ `<built-in>:409:9`! |
| 24 | #include <config.h>cxx . . $ `<file>/build/config.h`/ |
| 25 | #endif |
| 26 | |
| 27 | #include <stdlib.h> |
| 28 | #include <string.h> |
| 29 | #include <assert.h> |
| 30 | #include <time.h> |
| 31 | #include <sys/types.h> |
| 32 | #ifdef OS_WINDOWS |
| 33 | #include <io.h> |
| 34 | #else |
| 35 | #include <unistd.h> |
| 36 | #endif |
| 37 | |
| 38 | #include "common.h"cxx . . $ `<file>/src/OVAL/probes/SEAP/generic/common.h`/ |
| 39 | #include "bitmap.h"cxx . . $ `<file>/src/OVAL/probes/SEAP/generic/bitmap.h`/ |
| 40 | #include "common/debug_priv.h"cxx . . $ `<file>/src/common/debug_priv.h`/ |
| 41 | |
| 42 | bitmap_tcxx . . $ bitmap_t# *bitmap_newcxx . . $ bitmap_new(538d52d70ff53ed1). (bitmap_size_tcxx . . $ bitmap_size_t# size) |
| 43 | { |
| 44 | _Acxx . . $ `src/common/debug_priv.h:43:9`!(size > 0); |
| 45 | |
| 46 | bitmap_tcxx . . $ bitmap_t# *bitmap = malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(sizeof(bitmap_tcxx . . $ bitmap_t#)); |
| 47 | bitmap->sizecxx . . $ $anonymous_type_ccfb9561c43af652_0#size. = (size / BITMAP_CELLSIZEcxx . . $ `src/OVAL/probes/SEAP/generic/bitmap.h:51:9`!) + 1; |
| 48 | bitmap->realsizecxx . . $ $anonymous_type_ccfb9561c43af652_0#realsize. = 0; |
| 49 | bitmap->cellscxx . . $ $anonymous_type_ccfb9561c43af652_0#cells. = NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!; |
| 50 | bitmap->countcxx . . $ $anonymous_type_ccfb9561c43af652_0#count. = 0; |
| 51 | |
| 52 | xsrandomcxx . . $ xsrandom(2c44cd906ae4e829). ((unsigned long)clockcxx . . $ clock(da6335ded91d79c2). () ^ |
| 53 | (unsigned long)getpidcxx . . $ getpid(b126dc7c1de90089). ()); |
| 54 | |
| 55 | return (bitmap); |
| 56 | } |
| 57 | |
| 58 | int *bitmap_initcxx . . $ bitmap_init(ff0b42c5c579569b). (bitmap_tcxx . . $ bitmap_t# *bitmap, bitmap_size_tcxx . . $ bitmap_size_t# size) |
| 59 | { |
| 60 | _Acxx . . $ `src/common/debug_priv.h:43:9`!(bitmap != NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!); |
| 61 | _Acxx . . $ `src/common/debug_priv.h:43:9`!(size > 0); |
| 62 | |
| 63 | bitmap->cellscxx . . $ $anonymous_type_ccfb9561c43af652_0#cells. = NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!; |
| 64 | bitmap->sizecxx . . $ $anonymous_type_ccfb9561c43af652_0#size. = (size / BITMAP_CELLSIZEcxx . . $ `src/OVAL/probes/SEAP/generic/bitmap.h:51:9`!) + 1; |
| 65 | bitmap->realsizecxx . . $ $anonymous_type_ccfb9561c43af652_0#realsize. = 0; |
| 66 | bitmap->countcxx . . $ $anonymous_type_ccfb9561c43af652_0#count. = 0; |
| 67 | |
| 68 | xsrandomcxx . . $ xsrandom(2c44cd906ae4e829). ((unsigned long)clockcxx . . $ clock(da6335ded91d79c2). () ^ |
| 69 | (unsigned long)getpidcxx . . $ getpid(b126dc7c1de90089). ()); |
| 70 | |
| 71 | return (0); |
| 72 | } |
| 73 | |
| 74 | int *bitmap_reinitcxx . . $ bitmap_reinit(ff0b42c5c579569b). (bitmap_tcxx . . $ bitmap_t# *bitmap, bitmap_size_tcxx . . $ bitmap_size_t# size) |
| 75 | { |
| 76 | _Acxx . . $ `src/common/debug_priv.h:43:9`!(bitmap != NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!); |
| 77 | _Acxx . . $ `src/common/debug_priv.h:43:9`!(size > 0); |
| 78 | |
| 79 | freecxx . . $ free(5d22bdacc48458e8).(bitmap->cellscxx . . $ $anonymous_type_ccfb9561c43af652_0#cells.); |
| 80 | |
| 81 | bitmap->cellscxx . . $ $anonymous_type_ccfb9561c43af652_0#cells. = NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!; |
| 82 | bitmap->sizecxx . . $ $anonymous_type_ccfb9561c43af652_0#size. = (size / BITMAP_CELLSIZEcxx . . $ `src/OVAL/probes/SEAP/generic/bitmap.h:51:9`!) + 1; |
| 83 | bitmap->realsizecxx . . $ $anonymous_type_ccfb9561c43af652_0#realsize. = 0; |
| 84 | bitmap->countcxx . . $ $anonymous_type_ccfb9561c43af652_0#count. = 0; |
| 85 | |
| 86 | return (0); |
| 87 | } |
| 88 | |
| 89 | int bitmap_setcxx . . $ bitmap_set(67e573966af952d7). (bitmap_tcxx . . $ bitmap_t# *bitmap, bitmap_bitn_tcxx . . $ bitmap_bitn_t# bitn) |
| 90 | { |
| 91 | bitmap_size_tcxx . . $ bitmap_size_t# i; |
| 92 | |
| 93 | _Acxx . . $ `src/common/debug_priv.h:43:9`!(bitmap != NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!); |
| 94 | _Acxx . . $ `src/common/debug_priv.h:43:9`!((size_t)bitn < (size_t)bitmap->size * BITMAP_CELLSIZEcxx . . $ `src/OVAL/probes/SEAP/generic/bitmap.h:51:9`!); |
| 95 | |
| 96 | i = bitn/BITMAP_CELLSIZEcxx . . $ `src/OVAL/probes/SEAP/generic/bitmap.h:51:9`! + 1; |
| 97 | |
| 98 | if (i > bitmap->realsizecxx . . $ $anonymous_type_ccfb9561c43af652_0#realsize.) { |
| 99 | if (i <= bitmap->sizecxx . . $ $anonymous_type_ccfb9561c43af652_0#size.) { |
| 100 | void *new_cells = realloccxx . . $ realloc(417ab160f6baadc6).(bitmap->cellscxx . . $ $anonymous_type_ccfb9561c43af652_0#cells., sizeof(uint32_tcxx . . $ uint32_t#) * i); |
| 101 | if (new_cells == NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!) |
| 102 | return (-1); |
| 103 | bitmap->cellscxx . . $ $anonymous_type_ccfb9561c43af652_0#cells. = new_cells; |
| 104 | memsetcxx . . $ memset(d1de1e4fb215f1c6). (bitmap->cellscxx . . $ $anonymous_type_ccfb9561c43af652_0#cells. + bitmap->realsizecxx . . $ $anonymous_type_ccfb9561c43af652_0#realsize., 0, |
| 105 | sizeof (bitmap_cell_tcxx . . $ bitmap_cell_t#) * (i - bitmap->realsizecxx . . $ $anonymous_type_ccfb9561c43af652_0#realsize.)); |
| 106 | bitmap->realsizecxx . . $ $anonymous_type_ccfb9561c43af652_0#realsize. = i; |
| 107 | } else { |
| 108 | return (1); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | bitmap->cellscxx . . $ $anonymous_type_ccfb9561c43af652_0#cells.[i - 1] |= 1 << (bitn % BITMAP_CELLSIZEcxx . . $ `src/OVAL/probes/SEAP/generic/bitmap.h:51:9`!); |
| 113 | ++bitmap->countcxx . . $ $anonymous_type_ccfb9561c43af652_0#count.; |
| 114 | |
| 115 | return (0); |
| 116 | } |
| 117 | |
| 118 | int bitmap_cascxx . . $ bitmap_cas(df3fdd9eab1c426d). (bitmap_tcxx . . $ bitmap_t# *bitmap, bitmap_bitn_tcxx . . $ bitmap_bitn_t# bitn, int v) |
| 119 | { |
| 120 | _Acxx . . $ `src/common/debug_priv.h:43:9`!(bitmap != NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!); |
| 121 | _Acxx . . $ `src/common/debug_priv.h:43:9`!((size_t)bitn < (size_t)bitmap->size * BITMAP_CELLSIZEcxx . . $ `src/OVAL/probes/SEAP/generic/bitmap.h:51:9`!); |
| 122 | _Acxx . . $ `src/common/debug_priv.h:43:9`!(v == 0 || v == 1); |
| 123 | |
| 124 | abortcxx . . $ abort(8df32a62017fbbf6). (); |
| 125 | |
| 126 | return (1); |
| 127 | } |
| 128 | |
| 129 | int bitmap_unsetcxx . . $ bitmap_unset(67e573966af952d7). (bitmap_tcxx . . $ bitmap_t# *bitmap, bitmap_bitn_tcxx . . $ bitmap_bitn_t# bitn) |
| 130 | { |
| 131 | bitmap_size_tcxx . . $ bitmap_size_t# i; |
| 132 | |
| 133 | _Acxx . . $ `src/common/debug_priv.h:43:9`!(bitmap != NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!); |
| 134 | _Acxx . . $ `src/common/debug_priv.h:43:9`!((size_t)bitn < (size_t)bitmap->size * BITMAP_CELLSIZEcxx . . $ `src/OVAL/probes/SEAP/generic/bitmap.h:51:9`!); |
| 135 | |
| 136 | i = bitn/BITMAP_CELLSIZEcxx . . $ `src/OVAL/probes/SEAP/generic/bitmap.h:51:9`! + 1; |
| 137 | |
| 138 | if (i <= bitmap->realsizecxx . . $ $anonymous_type_ccfb9561c43af652_0#realsize.) { |
| 139 | bitmap->cellscxx . . $ $anonymous_type_ccfb9561c43af652_0#cells.[i - 1] &= ~(1 << (bitn % BITMAP_CELLSIZEcxx . . $ `src/OVAL/probes/SEAP/generic/bitmap.h:51:9`!)); |
| 140 | --bitmap->countcxx . . $ $anonymous_type_ccfb9561c43af652_0#count.; |
| 141 | |
| 142 | bitmap_size_tcxx . . $ bitmap_size_t# current_realsize = bitmap->realsizecxx . . $ $anonymous_type_ccfb9561c43af652_0#realsize.; |
| 143 | if (bitmap->realsizecxx . . $ $anonymous_type_ccfb9561c43af652_0#realsize. - i == 0) { |
| 144 | while (bitmap->cellscxx . . $ $anonymous_type_ccfb9561c43af652_0#cells.[bitmap->realsizecxx . . $ $anonymous_type_ccfb9561c43af652_0#realsize. - 1] == 0) |
| 145 | --bitmap->realsizecxx . . $ $anonymous_type_ccfb9561c43af652_0#realsize.; |
| 146 | |
| 147 | void *new_cells = realloccxx . . $ realloc(417ab160f6baadc6).(bitmap->cellscxx . . $ $anonymous_type_ccfb9561c43af652_0#cells., sizeof(uint32_tcxx . . $ uint32_t#) * bitmap->realsizecxx . . $ $anonymous_type_ccfb9561c43af652_0#realsize.); |
| 148 | if (new_cells == NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`! && bitmap->realsizecxx . . $ $anonymous_type_ccfb9561c43af652_0#realsize. > 0) { |
| 149 | bitmap->realsizecxx . . $ $anonymous_type_ccfb9561c43af652_0#realsize. = current_realsize; |
| 150 | return (-1); |
| 151 | } |
| 152 | bitmap->cellscxx . . $ $anonymous_type_ccfb9561c43af652_0#cells. = new_cells; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | return (0); |
| 157 | } |
| 158 | |
| 159 | int bitmap_clearcxx . . $ bitmap_clear(51a957349079d69a). (bitmap_tcxx . . $ bitmap_t# *bitmap) |
| 160 | { |
| 161 | _Acxx . . $ `src/common/debug_priv.h:43:9`!(bitmap != NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!); |
| 162 | |
| 163 | if (bitmap->realsizecxx . . $ $anonymous_type_ccfb9561c43af652_0#realsize. > 0) { |
| 164 | freecxx . . $ free(5d22bdacc48458e8).(bitmap->cellscxx . . $ $anonymous_type_ccfb9561c43af652_0#cells.); |
| 165 | bitmap->cellscxx . . $ $anonymous_type_ccfb9561c43af652_0#cells. = NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!; |
| 166 | bitmap->realsizecxx . . $ $anonymous_type_ccfb9561c43af652_0#realsize. = 0; |
| 167 | bitmap->countcxx . . $ $anonymous_type_ccfb9561c43af652_0#count. = 0; |
| 168 | } |
| 169 | |
| 170 | return (0); |
| 171 | } |
| 172 | |
| 173 | bitmap_bitn_tcxx . . $ bitmap_bitn_t# bitmap_setfreecxx . . $ bitmap_setfree(51a957349079d69a). (bitmap_tcxx . . $ bitmap_t# *bitmap) |
| 174 | { |
| 175 | bitmap_size_tcxx . . $ bitmap_size_t# i, j; |
| 176 | |
| 177 | _Acxx . . $ `src/common/debug_priv.h:43:9`!(bitmap != NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!); |
| 178 | |
| 179 | for (i = 0; i < bitmap->realsizecxx . . $ $anonymous_type_ccfb9561c43af652_0#realsize.; ++i) { |
| 180 | if (bitmap->cellscxx . . $ $anonymous_type_ccfb9561c43af652_0#cells.[i] != ~(bitmap_cell_tcxx . . $ bitmap_cell_t#)(0)) { |
| 181 | /* TODO: binary search */ |
| 182 | for (j = 0; j < BITMAP_CELLSIZEcxx . . $ `src/OVAL/probes/SEAP/generic/bitmap.h:51:9`!; ++j) { |
| 183 | if (((bitmap->cellscxx . . $ $anonymous_type_ccfb9561c43af652_0#cells.[i] >> j) & 1) == 0) { |
| 184 | |
| 185 | bitmap->cellscxx . . $ $anonymous_type_ccfb9561c43af652_0#cells.[i] |= 1 << j; |
| 186 | |
| 187 | return ((i * BITMAP_CELLSIZEcxx . . $ `src/OVAL/probes/SEAP/generic/bitmap.h:51:9`!) + j); |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | if (bitmap->realsizecxx . . $ $anonymous_type_ccfb9561c43af652_0#realsize. < bitmap->sizecxx . . $ $anonymous_type_ccfb9561c43af652_0#size.) |
| 194 | return (bitmap_setcxx . . $ bitmap_set(67e573966af952d7). (bitmap, |
| 195 | BITMAP_CELLSIZEcxx . . $ `src/OVAL/probes/SEAP/generic/bitmap.h:51:9`! * bitmap->realsizecxx . . $ $anonymous_type_ccfb9561c43af652_0#realsize.) ? -1 : (bitmap_bitn_tcxx . . $ bitmap_bitn_t#)(bitmap->realsizecxx . . $ $anonymous_type_ccfb9561c43af652_0#realsize. * BITMAP_CELLSIZEcxx . . $ `src/OVAL/probes/SEAP/generic/bitmap.h:51:9`!)); |
| 196 | else |
| 197 | return (-1); |
| 198 | } |
| 199 | |
| 200 | bitmap_bitn_tcxx . . $ bitmap_bitn_t# bitmap_getfreecxx . . $ bitmap_getfree(51a957349079d69a). (bitmap_tcxx . . $ bitmap_t# *bitmap) |
| 201 | { |
| 202 | _Acxx . . $ `src/common/debug_priv.h:43:9`!(bitmap != NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!); |
| 203 | |
| 204 | return (-1); |
| 205 | } |
| 206 | |
| 207 | void bitmap_freecxx . . $ bitmap_free(80bd7b4154c2ebf9). (bitmap_tcxx . . $ bitmap_t# *bitmap) |
| 208 | { |
| 209 | _Acxx . . $ `src/common/debug_priv.h:43:9`!(bitmap != NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!); |
| 210 | |
| 211 | if (bitmap->realsizecxx . . $ $anonymous_type_ccfb9561c43af652_0#realsize. > 0) |
| 212 | freecxx . . $ free(5d22bdacc48458e8).(bitmap->cellscxx . . $ $anonymous_type_ccfb9561c43af652_0#cells.); |
| 213 | freecxx . . $ free(5d22bdacc48458e8).(bitmap); |
| 214 | } |
| 215 | |
| 216 | #if defined(BITMAP_TEST) |
| 217 | #include <stdio.h> |
| 218 | |
| 219 | void bitmap_print (bitmap_t *bitmap) |
| 220 | { |
| 221 | bitmap_size_t i, j; |
| 222 | |
| 223 | printf ("bitmap@%p: size=%u, realsize=%u, cells@%p\n", |
| 224 | (void *)bitmap, bitmap->size, bitmap->realsize, (void *)bitmap->cells); |
| 225 | |
| 226 | for (i = 0; i < bitmap->realsize; ++i) { |
| 227 | printf ("[%u] hex: %08x | bin: ", i, bitmap->cells[i]); |
| 228 | for (j = 0; j < BITMAP_CELLSIZE; ++j) |
| 229 | printf ("%u", bitmap->cells[i] & (1 << j) ? 1 : 0); |
| 230 | printf ("\n"); |
| 231 | } |
| 232 | |
| 233 | return; |
| 234 | } |
| 235 | |
| 236 | int main (void) |
| 237 | { |
| 238 | bitmap_t *bmap1; |
| 239 | bitmap_t bmap2; |
| 240 | |
| 241 | printf ("Create, print.\n"); |
| 242 | |
| 243 | bmap1 = bitmap_new (8); |
| 244 | bitmap_print (bmap1); |
| 245 | |
| 246 | printf ("Set 1, 2, 5, 9, 56, 77, 79\n "); |
| 247 | |
| 248 | if (bitmap_set (bmap1, 1) == 0) |
| 249 | printf ("ok "); |
| 250 | if (bitmap_set (bmap1, 2) == 0) |
| 251 | printf ("ok "); |
| 252 | if (bitmap_set (bmap1, 5) == 0) |
| 253 | printf ("ok "); |
| 254 | if (bitmap_set (bmap1, 9) == 0) |
| 255 | printf ("ok "); |
| 256 | if (bitmap_set (bmap1, 56) == 0) |
| 257 | printf ("ok "); |
| 258 | if (bitmap_set (bmap1, 77) == 0) |
| 259 | printf ("ok "); |
| 260 | if (bitmap_set (bmap1, 79) == 0) |
| 261 | printf ("ok "); |
| 262 | |
| 263 | printf ("\n"); |
| 264 | |
| 265 | bitmap_print (bmap1); |
| 266 | |
| 267 | printf ("Unset 1, 9, 56, 77\n "); |
| 268 | |
| 269 | if (bitmap_unset (bmap1, 1) == 0) |
| 270 | printf ("ok "); |
| 271 | if (bitmap_unset (bmap1, 9) == 0) |
| 272 | printf ("ok "); |
| 273 | if (bitmap_unset (bmap1, 56) == 0) |
| 274 | printf ("ok "); |
| 275 | if (bitmap_unset (bmap1, 77) == 0) |
| 276 | printf ("ok "); |
| 277 | |
| 278 | printf ("\n"); |
| 279 | |
| 280 | bitmap_print (bmap1); |
| 281 | |
| 282 | printf ("Unset 79\n "); |
| 283 | |
| 284 | if (bitmap_unset (bmap1, 79) == 0) |
| 285 | printf ("ok "); |
| 286 | |
| 287 | printf ("\n"); |
| 288 | |
| 289 | bitmap_print (bmap1); |
| 290 | |
| 291 | printf ("Set 101, 119\n "); |
| 292 | |
| 293 | if (bitmap_set (bmap1, 101) == 0) |
| 294 | printf ("ok "); |
| 295 | if (bitmap_set (bmap1, 119) == 0) |
| 296 | printf ("ok "); |
| 297 | |
| 298 | printf ("\n"); |
| 299 | |
| 300 | bitmap_print (bmap1); |
| 301 | |
| 302 | printf ("setfree: %d, %d, %d\n", |
| 303 | bitmap_setfree (bmap1), bitmap_setfree (bmap1), bitmap_setfree (bmap1)); |
| 304 | |
| 305 | bitmap_print (bmap1); |
| 306 | |
| 307 | printf ("while setfree != -1\n"); |
| 308 | while (bitmap_setfree (bmap1) != -1); |
| 309 | |
| 310 | bitmap_print (bmap1); |
| 311 | |
| 312 | printf ("Clear\n"); |
| 313 | |
| 314 | bitmap_clear (bmap1); |
| 315 | |
| 316 | bitmap_print (bmap1); |
| 317 | |
| 318 | printf ("while setfree != -1\n"); |
| 319 | while (bitmap_setfree (bmap1) != -1); |
| 320 | |
| 321 | bitmap_print (bmap1); |
| 322 | |
| 323 | printf ("Free\n"); |
| 324 | |
| 325 | bitmap_free (bmap1); |
| 326 | |
| 327 | return (0); |
| 328 | } |
| 329 | #endif |