Tor  0.4.7.0-alpha-dev
consdiff.h
Go to the documentation of this file.
1 /* Copyright (c) 2014, Daniel Martí
2  * Copyright (c) 2014-2021, The Tor Project, Inc. */
3 /* See LICENSE for licensing information */
4 
5 /**
6  * @file consdiff.h
7  * @brief Header for consdiff.c
8  **/
9 
10 #ifndef TOR_CONSDIFF_H
11 #define TOR_CONSDIFF_H
12 
13 #include "core/or/or.h"
14 
15 char *consensus_diff_generate(const char *cons1, size_t cons1len,
16  const char *cons2, size_t cons2len);
17 char *consensus_diff_apply(const char *consensus, size_t consensus_len,
18  const char *diff, size_t diff_len);
19 
20 int looks_like_a_consensus_diff(const char *document, size_t len);
21 
22 #ifdef CONSDIFF_PRIVATE
23 #include "lib/container/bitarray.h"
24 
25 struct memarea_t;
26 
27 /** Line type used for constructing consensus diffs. Each of these lines
28  * refers to a chunk of memory allocated elsewhere, and is not necessarily
29  * NUL-terminated: this helps us avoid copies and save memory. */
30 typedef struct cdline_t {
31  const char *s;
32  uint32_t len;
33 } cdline_t;
34 
35 typedef struct consensus_digest_t {
36  uint8_t sha3_256[DIGEST256_LEN];
37 } consensus_digest_t;
38 
40  const smartlist_t *cons2,
41  const consensus_digest_t *digests1,
42  const consensus_digest_t *digests2,
43  struct memarea_t *area);
44 STATIC char *consdiff_apply_diff(const smartlist_t *cons1,
45  const smartlist_t *diff,
46  const consensus_digest_t *digests1);
48  char *digest1_out,
49  char *digest2_out);
50 
51 /** Data structure to define a slice of a smarltist. */
52 typedef struct smartlist_slice_t {
53  /**
54  * Smartlist that this slice is made from.
55  * References the whole original smartlist that the slice was made out of.
56  * */
57  const smartlist_t *list;
58  /** Starting position of the slice in the smartlist. */
59  int offset;
60  /** Length of the slice, i.e. the number of elements it holds. */
61  int len;
62 } smartlist_slice_t;
64  const smartlist_t *cons2,
65  struct memarea_t *area);
67  const smartlist_t *diff,
68  int start_line);
69 STATIC void calc_changes(smartlist_slice_t *slice1, smartlist_slice_t *slice2,
70  bitarray_t *changed1, bitarray_t *changed2);
71 STATIC smartlist_slice_t *smartlist_slice(const smartlist_t *list,
72  int start, int end);
73 STATIC int next_router(const smartlist_t *cons, int cur);
74 STATIC int *lcs_lengths(const smartlist_slice_t *slice1,
75  const smartlist_slice_t *slice2,
76  int direction);
77 STATIC void trim_slices(smartlist_slice_t *slice1, smartlist_slice_t *slice2);
78 STATIC int base64cmp(const cdline_t *hash1, const cdline_t *hash2);
79 STATIC int get_id_hash(const cdline_t *line, cdline_t *hash_out);
80 STATIC int is_valid_router_entry(const cdline_t *line);
81 STATIC int smartlist_slice_string_pos(const smartlist_slice_t *slice,
82  const cdline_t *string);
83 STATIC void set_changed(bitarray_t *changed1, bitarray_t *changed2,
84  const smartlist_slice_t *slice1,
85  const smartlist_slice_t *slice2);
87  const char *s, size_t len,
88  struct memarea_t *area);
89 STATIC void smartlist_add_linecpy(smartlist_t *lst, struct memarea_t *area,
90  const char *s);
91 STATIC int lines_eq(const cdline_t *a, const cdline_t *b);
92 STATIC int line_str_eq(const cdline_t *a, const char *b);
93 
94 MOCK_DECL(STATIC int,
95  consensus_compute_digest,(const char *cons, size_t len,
96  consensus_digest_t *digest_out));
97 MOCK_DECL(STATIC int,
98  consensus_compute_digest_as_signed,(const char *cons, size_t len,
99  consensus_digest_t *digest_out));
100 MOCK_DECL(STATIC int,
101  consensus_digest_eq,(const uint8_t *d1,
102  const uint8_t *d2));
103 #endif /* defined(CONSDIFF_PRIVATE) */
104 
105 #endif /* !defined(TOR_CONSDIFF_H) */
Implements a variable-sized (but non-resizeable) bit-array.
unsigned int bitarray_t
Definition: bitarray.h:30
STATIC void smartlist_add_linecpy(smartlist_t *lst, memarea_t *area, const char *s)
Definition: consdiff.c:94
STATIC int line_str_eq(const cdline_t *a, const char *b)
Definition: consdiff.c:60
STATIC void trim_slices(smartlist_slice_t *slice1, smartlist_slice_t *slice2)
Definition: consdiff.c:213
STATIC int * lcs_lengths(const smartlist_slice_t *slice1, const smartlist_slice_t *slice2, int direction)
Definition: consdiff.c:165
STATIC void set_changed(bitarray_t *changed1, bitarray_t *changed2, const smartlist_slice_t *slice1, const smartlist_slice_t *slice2)
Definition: consdiff.c:264
STATIC smartlist_t * apply_ed_diff(const smartlist_t *cons1, const smartlist_t *diff, int diff_starting_line)
Definition: consdiff.c:859
smartlist_t * consdiff_gen_diff(const smartlist_t *cons1, const smartlist_t *cons2, const consensus_digest_t *digests1, const consensus_digest_t *digests2, memarea_t *area)
Definition: consdiff.c:1026
STATIC int consensus_digest_eq(const uint8_t *d1, const uint8_t *d2)
Definition: consdiff.c:129
STATIC int base64cmp(const cdline_t *hash1, const cdline_t *hash2)
Definition: consdiff.c:472
STATIC int next_router(const smartlist_t *cons, int cur)
Definition: consdiff.c:449
STATIC smartlist_slice_t * smartlist_slice(const smartlist_t *list, int start, int end)
Definition: consdiff.c:140
STATIC void calc_changes(smartlist_slice_t *slice1, smartlist_slice_t *slice2, bitarray_t *changed1, bitarray_t *changed2)
Definition: consdiff.c:327
STATIC int consensus_compute_digest_as_signed(const char *cons, size_t len, consensus_digest_t *digest_out)
Definition: consdiff.c:118
STATIC int is_valid_router_entry(const cdline_t *line)
Definition: consdiff.c:435
char * consdiff_apply_diff(const smartlist_t *cons1, const smartlist_t *diff, const consensus_digest_t *digests1)
Definition: consdiff.c:1192
STATIC int consensus_split_lines(smartlist_t *out, const char *s, size_t len, memarea_t *area)
Definition: consdiff.c:1289
STATIC smartlist_t * gen_ed_diff(const smartlist_t *cons1_orig, const smartlist_t *cons2, memarea_t *area)
Definition: consdiff.c:625
STATIC int get_id_hash(const cdline_t *line, cdline_t *hash_out)
Definition: consdiff.c:395
STATIC int lines_eq(const cdline_t *a, const cdline_t *b)
Definition: consdiff.c:53
STATIC int smartlist_slice_string_pos(const smartlist_slice_t *slice, const cdline_t *string)
Definition: consdiff.c:245
int consdiff_get_digests(const smartlist_t *diff, char *digest1_out, char *digest2_out)
Definition: consdiff.c:1106
STATIC int consensus_compute_digest(const char *cons, size_t len, consensus_digest_t *digest_out)
Definition: consdiff.c:105
char * consensus_diff_apply(const char *consensus, size_t consensus_len, const char *diff, size_t diff_len)
Definition: consdiff.c:1381
char * consensus_diff_generate(const char *cons1, size_t cons1len, const char *cons2, size_t cons2len)
Definition: consdiff.c:1341
int looks_like_a_consensus_diff(const char *document, size_t len)
Definition: consdiff.c:1416
#define DIGEST256_LEN
Definition: digest_sizes.h:23
Master header file for Tor-specific functionality.
#define STATIC
Definition: testsupport.h:32
#define MOCK_DECL(rv, funcname, arglist)
Definition: testsupport.h:127