| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Copyright (c) 2008 Patrick McHardy <kaber@trash.net> | ||
| 3 | * | ||
| 4 | * This program is free software; you can redistribute it and/or modify | ||
| 5 | * it under the terms of the GNU General Public License version 2 as | ||
| 6 | * published by the Free Software Foundation. | ||
| 7 | * | ||
| 8 | * Development of this code funded by Astaro AG (http://www.astaro.com/) | ||
| 9 | */ | ||
| 10 | |||
| 11 | #include <nft.h> | ||
| 12 | |||
| 13 | #include <stddef.h> | ||
| 14 | #include <stdarg.h> | ||
| 15 | #include <stdio.h> | ||
| 16 | #include <unistd.h> | ||
| 17 | |||
| 18 | #include <nftables.h> | ||
| 19 | #include <datatype.h> | ||
| 20 | #include <utils.h> | ||
| 21 | |||
| 22 | 3302158 | void mpz_bitmask(mpz_t rop, unsigned int width) | |
| 23 | { | ||
| 24 | 3302158 | mpz_set_ui(rop, 0); | |
| 25 | 3302158 | mpz_setbit(rop, width); | |
| 26 | 3302158 | mpz_sub_ui(rop, rop, 1); | |
| 27 | 3302158 | } | |
| 28 | |||
| 29 | 2668273 | void mpz_init_bitmask(mpz_t rop, unsigned int width) | |
| 30 | { | ||
| 31 | 2668273 | mpz_init2(rop, width); | |
| 32 | 2668273 | mpz_bitmask(rop, width); | |
| 33 | 2668273 | } | |
| 34 | |||
| 35 | 282187 | void mpz_prefixmask(mpz_t rop, unsigned int width, unsigned int prefix_len) | |
| 36 | { | ||
| 37 | 282187 | mpz_bitmask(rop, prefix_len); | |
| 38 | 282187 | mpz_lshift_ui(rop, width - prefix_len); | |
| 39 | 282187 | } | |
| 40 | |||
| 41 | 351946 | void mpz_lshift_ui(mpz_t rop, unsigned int n) | |
| 42 | { | ||
| 43 | 351946 | mpz_mul_2exp(rop, rop, n); | |
| 44 | 351946 | } | |
| 45 | |||
| 46 | 66453 | void mpz_rshift_ui(mpz_t rop, unsigned int n) | |
| 47 | { | ||
| 48 | 66453 | mpz_tdiv_q_2exp(rop, rop, n); | |
| 49 | 66453 | } | |
| 50 | |||
| 51 | #define mpz_get_type(type, endian, op) \ | ||
| 52 | ({ \ | ||
| 53 | type ret = 0; \ | ||
| 54 | size_t cnt; \ | ||
| 55 | mpz_export(&ret, &cnt, MPZ_LSWF, sizeof(ret), endian, 0, op); \ | ||
| 56 | assert(cnt <= 1); \ | ||
| 57 | ret; \ | ||
| 58 | }) | ||
| 59 | |||
| 60 | 296 | uint64_t mpz_get_uint64(const mpz_t op) | |
| 61 | { | ||
| 62 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 296 times.
|
296 | return mpz_get_type(uint64_t, MPZ_HOST_ENDIAN, op); |
| 63 | } | ||
| 64 | |||
| 65 | 4933 | uint32_t mpz_get_uint32(const mpz_t op) | |
| 66 | { | ||
| 67 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 4933 times.
|
4933 | return mpz_get_type(uint32_t, MPZ_HOST_ENDIAN, op); |
| 68 | } | ||
| 69 | |||
| 70 | 10826 | uint16_t mpz_get_uint16(const mpz_t op) | |
| 71 | { | ||
| 72 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 10826 times.
|
10826 | return mpz_get_type(uint16_t, MPZ_HOST_ENDIAN, op); |
| 73 | } | ||
| 74 | |||
| 75 | 21308 | uint8_t mpz_get_uint8(const mpz_t op) | |
| 76 | { | ||
| 77 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 21308 times.
|
21308 | return mpz_get_type(uint8_t, MPZ_HOST_ENDIAN, op); |
| 78 | } | ||
| 79 | |||
| 80 | 438133 | uint32_t mpz_get_be32(const mpz_t op) | |
| 81 | { | ||
| 82 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 438133 times.
|
438133 | return mpz_get_type(uint32_t, MPZ_BIG_ENDIAN, op); |
| 83 | } | ||
| 84 | |||
| 85 | 35430 | uint16_t mpz_get_be16(const mpz_t op) | |
| 86 | { | ||
| 87 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 35430 times.
|
35430 | return mpz_get_type(uint16_t, MPZ_BIG_ENDIAN, op); |
| 88 | } | ||
| 89 | |||
| 90 | − | void *__mpz_export_data(void *data, const mpz_t op, enum byteorder byteorder, | |
| 91 | unsigned int len) | ||
| 92 | { | ||
| 93 | enum mpz_word_order order; | ||
| 94 | enum mpz_byte_order endian; | ||
| 95 | |||
| 96 | − | switch (byteorder) { | |
| 97 | − | case BYTEORDER_BIG_ENDIAN: | |
| 98 | default: | ||
| 99 | − | order = MPZ_MSWF; | |
| 100 | − | endian = MPZ_BIG_ENDIAN; | |
| 101 | − | break; | |
| 102 | − | case BYTEORDER_HOST_ENDIAN: | |
| 103 | − | order = MPZ_HWO; | |
| 104 | − | endian = MPZ_HOST_ENDIAN; | |
| 105 | − | break; | |
| 106 | } | ||
| 107 | |||
| 108 | − | memset(data, 0, len); | |
| 109 | − | mpz_export(data, NULL, order, len, endian, 0, op); | |
| 110 | − | return data; | |
| 111 | } | ||
| 112 | |||
| 113 | − | void __mpz_import_data(mpz_t rop, const void *data, enum byteorder byteorder, | |
| 114 | unsigned int len) | ||
| 115 | { | ||
| 116 | enum mpz_word_order order; | ||
| 117 | enum mpz_byte_order endian; | ||
| 118 | |||
| 119 | − | switch (byteorder) { | |
| 120 | − | case BYTEORDER_BIG_ENDIAN: | |
| 121 | default: | ||
| 122 | − | order = MPZ_MSWF; | |
| 123 | − | endian = MPZ_BIG_ENDIAN; | |
| 124 | − | break; | |
| 125 | − | case BYTEORDER_HOST_ENDIAN: | |
| 126 | − | order = MPZ_HWO; | |
| 127 | − | endian = MPZ_HOST_ENDIAN; | |
| 128 | − | break; | |
| 129 | } | ||
| 130 | |||
| 131 | − | mpz_import(rop, len, order, 1, endian, 0, data); | |
| 132 | − | } | |
| 133 | |||
| 134 | − | void __mpz_switch_byteorder(mpz_t rop, unsigned int len) | |
| 135 | − | { | |
| 136 | − | char data[len]; | |
| 137 | |||
| 138 | − | __mpz_export_data(data, rop, BYTEORDER_BIG_ENDIAN, len); | |
| 139 | − | __mpz_import_data(rop, data, BYTEORDER_HOST_ENDIAN, len); | |
| 140 | − | } | |
| 141 | |||
| 142 | #ifndef HAVE_LIBGMP | ||
| 143 | /* mini-gmp doesn't have a gmp_printf so we use our own minimal | ||
| 144 | * variant here which is able to format a single mpz_t. | ||
| 145 | */ | ||
| 146 | int mpz_vfprintf(FILE *fp, const char *f, va_list args) | ||
| 147 | { | ||
| 148 | const mpz_t *value = va_arg(args, const mpz_t *); | ||
| 149 | int n = 0; | ||
| 150 | |||
| 151 | while (*f) { | ||
| 152 | if (*f != '%') { | ||
| 153 | if (fputc(*f, fp) != *f) | ||
| 154 | return -1; | ||
| 155 | |||
| 156 | ++n; | ||
| 157 | } else { | ||
| 158 | unsigned long prec = 0; | ||
| 159 | int base; | ||
| 160 | size_t len; | ||
| 161 | char *str; | ||
| 162 | bool ok; | ||
| 163 | |||
| 164 | if (*++f == '.') | ||
| 165 | prec = strtoul(++f, (char**)&f, 10); | ||
| 166 | |||
| 167 | if (*f++ != 'Z') | ||
| 168 | return -1; | ||
| 169 | |||
| 170 | if (*f == 'u') | ||
| 171 | base = 10; | ||
| 172 | else if (*f == 'x') | ||
| 173 | base = 16; | ||
| 174 | else | ||
| 175 | return -1; | ||
| 176 | |||
| 177 | len = mpz_sizeinbase(*value, base); | ||
| 178 | while (prec-- > len) { | ||
| 179 | if (fputc('0', fp) != '0') | ||
| 180 | return -1; | ||
| 181 | |||
| 182 | ++n; | ||
| 183 | } | ||
| 184 | |||
| 185 | str = mpz_get_str(NULL, base, *value); | ||
| 186 | ok = str && fwrite(str, 1, len, fp) == len; | ||
| 187 | nft_gmp_free(str); | ||
| 188 | |||
| 189 | if (!ok) | ||
| 190 | return -1; | ||
| 191 | |||
| 192 | n += len; | ||
| 193 | } | ||
| 194 | ++f; | ||
| 195 | } | ||
| 196 | return n; | ||
| 197 | } | ||
| 198 | #endif | ||
| 199 | |||
| 200 | 137 | void nft_gmp_free(void *ptr) | |
| 201 | { | ||
| 202 | void (*free_fcn)(void *, size_t); | ||
| 203 | |||
| 204 | /* When we get allocated memory from gmp, it was allocated via the | ||
| 205 | * allocator() from mp_set_memory_functions(). We should pair the free | ||
| 206 | * with the corresponding free function, which we get via | ||
| 207 | * mp_get_memory_functions(). | ||
| 208 | * | ||
| 209 | * It's not clear what the correct blk_size is. The default allocator | ||
| 210 | * function of gmp just wraps free() and ignores the extra argument. | ||
| 211 | * Assume 0 is fine. | ||
| 212 | */ | ||
| 213 | |||
| 214 | 137 | mp_get_memory_functions(NULL, NULL, &free_fcn); | |
| 215 | |||
| 216 | 137 | (*free_fcn)(ptr, 0); | |
| 217 | 137 | } | |
| 218 |