Lev Walkin | a5972be | 2017-09-29 23:15:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 Lev Walkin <vlm@lionet.info>. |
| 3 | * All rights reserved. |
| 4 | * Redistribution and modifications are permitted subject to BSD license. |
| 5 | */ |
Lev Walkin | a5972be | 2017-09-29 23:15:58 -0700 | [diff] [blame] | 6 | #include <asn_internal.h> |
| 7 | #include <asn_random_fill.h> |
| 8 | #include <constr_TYPE.h> |
| 9 | |
| 10 | int |
| 11 | asn_random_fill(const struct asn_TYPE_descriptor_s *td, void **struct_ptr, |
| 12 | size_t length) { |
| 13 | |
| 14 | if(td && td->op->random_fill) { |
| 15 | asn_random_fill_result_t res = |
| 16 | td->op->random_fill(td, struct_ptr, 0, length); |
| 17 | return (res.code == ARFILL_OK) ? 0 : -1; |
| 18 | } else { |
| 19 | return -1; |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | intmax_t |
| 24 | asn_random_between(intmax_t a, intmax_t b) { |
| 25 | assert(a <= b); |
| 26 | assert((b-a) < RAND_MAX); |
| 27 | if(a == b) return a; |
Lev Walkin | 4d8b748 | 2017-10-01 14:14:20 -0700 | [diff] [blame] | 28 | return a + (random() % (b - a + 1)); |
Lev Walkin | a5972be | 2017-09-29 23:15:58 -0700 | [diff] [blame] | 29 | } |