di_ops.h (2951B)
1 /* Copyright (c) 2003-2004, Roger Dingledine 2 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson. 3 * Copyright (c) 2007-2021, The Tor Project, Inc. */ 4 /* See LICENSE for licensing information */ 5 6 /** 7 * \file di_ops.h 8 * \brief Headers for di_ops.c 9 **/ 10 11 #ifndef TOR_DI_OPS_H 12 #define TOR_DI_OPS_H 13 14 #include "orconfig.h" 15 #include "lib/cc/torint.h" 16 17 int tor_memcmp(const void *a, const void *b, size_t sz); 18 int tor_memeq(const void *a, const void *b, size_t sz); 19 /** Perform a constant-time comparison of the <b>sz</b> bytes at <b>a</b> and 20 * <b>b</b>, yielding true if they are different, and false otherwise. */ 21 #define tor_memneq(a,b,sz) (!tor_memeq((a),(b),(sz))) 22 23 /** Alias for the platform's memcmp() function. This function is 24 * <em>not</em> data-independent: we define this alias so that we can 25 * mark cases where we are deliberately using a data-dependent memcmp() 26 * implementation. 27 */ 28 #define fast_memcmp(a,b,c) (memcmp((a),(b),(c))) 29 /** Alias for the platform's memcmp() function, for use in testing equality. 30 * 31 * This function is <em>not</em> data-independent: we define this alias so 32 * that we can mark cases where we are deliberately using a data-dependent 33 * memcmp() implementation. 34 */ 35 #define fast_memeq(a,b,c) (0==memcmp((a),(b),(c))) 36 /** Alias for the platform's memcmp() function, for use in testing inequality. 37 * 38 * This function is <em>not</em> data-independent: we define this alias so 39 * that we can mark cases where we are deliberately using a data-dependent 40 * memcmp() implementation. 41 */ 42 #define fast_memneq(a,b,c) (0!=memcmp((a),(b),(c))) 43 44 int safe_mem_is_zero(const void *mem, size_t sz); 45 46 /** A type for a map from DIGEST256_LEN-byte blobs to void*, such that 47 * data lookups take an amount of time proportional only to the size 48 * of the map, and not to the position or presence of the item in the map. 49 * 50 * Not efficient for large maps! */ 51 typedef struct di_digest256_map_t di_digest256_map_t; 52 /** 53 * Type for a function used to free members of a di_digest256_map_t. 54 **/ 55 typedef void (*dimap_free_fn)(void *); 56 57 void dimap_free_(di_digest256_map_t *map, dimap_free_fn free_fn); 58 /** 59 * @copydoc dimap_free_ 60 * 61 * Additionally, set the pointer <b>map</b> to NULL. 62 **/ 63 #define dimap_free(map, free_fn) \ 64 do { \ 65 dimap_free_((map), (free_fn)); \ 66 (map) = NULL; \ 67 } while (0) 68 void dimap_add_entry(di_digest256_map_t **map, 69 const uint8_t *key, void *val); 70 void *dimap_search(const di_digest256_map_t *map, const uint8_t *key, 71 void *dflt_val); 72 int select_array_member_cumulative_timei(const uint64_t *entries, 73 int n_entries, 74 uint64_t total, uint64_t rand_val); 75 76 void memcpy_if_true_timei(bool s, void *dest, const void *src, size_t n); 77 78 #endif /* !defined(TOR_DI_OPS_H) */