consdiff.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /* Copyright (c) 2014, Daniel Martí
  2. * Copyright (c) 2014-2019, The Tor Project, Inc. */
  3. /* See LICENSE for licensing information */
  4. #ifndef TOR_CONSDIFF_H
  5. #define TOR_CONSDIFF_H
  6. #include "core/or/or.h"
  7. char *consensus_diff_generate(const char *cons1,
  8. const char *cons2);
  9. char *consensus_diff_apply(const char *consensus,
  10. const char *diff);
  11. int looks_like_a_consensus_diff(const char *document, size_t len);
  12. #ifdef CONSDIFF_PRIVATE
  13. #include "lib/container/bitarray.h"
  14. struct memarea_t;
  15. /** Line type used for constructing consensus diffs. Each of these lines
  16. * refers to a chunk of memory allocated elsewhere, and is not necessarily
  17. * NUL-terminated: this helps us avoid copies and save memory. */
  18. typedef struct cdline_t {
  19. const char *s;
  20. uint32_t len;
  21. } cdline_t;
  22. typedef struct consensus_digest_t {
  23. uint8_t sha3_256[DIGEST256_LEN];
  24. } consensus_digest_t;
  25. STATIC smartlist_t *consdiff_gen_diff(const smartlist_t *cons1,
  26. const smartlist_t *cons2,
  27. const consensus_digest_t *digests1,
  28. const consensus_digest_t *digests2,
  29. struct memarea_t *area);
  30. STATIC char *consdiff_apply_diff(const smartlist_t *cons1,
  31. const smartlist_t *diff,
  32. const consensus_digest_t *digests1);
  33. STATIC int consdiff_get_digests(const smartlist_t *diff,
  34. char *digest1_out,
  35. char *digest2_out);
  36. /** Data structure to define a slice of a smarltist. */
  37. typedef struct smartlist_slice_t {
  38. /**
  39. * Smartlist that this slice is made from.
  40. * References the whole original smartlist that the slice was made out of.
  41. * */
  42. const smartlist_t *list;
  43. /** Starting position of the slice in the smartlist. */
  44. int offset;
  45. /** Length of the slice, i.e. the number of elements it holds. */
  46. int len;
  47. } smartlist_slice_t;
  48. STATIC smartlist_t *gen_ed_diff(const smartlist_t *cons1,
  49. const smartlist_t *cons2,
  50. struct memarea_t *area);
  51. STATIC smartlist_t *apply_ed_diff(const smartlist_t *cons1,
  52. const smartlist_t *diff,
  53. int start_line);
  54. STATIC void calc_changes(smartlist_slice_t *slice1, smartlist_slice_t *slice2,
  55. bitarray_t *changed1, bitarray_t *changed2);
  56. STATIC smartlist_slice_t *smartlist_slice(const smartlist_t *list,
  57. int start, int end);
  58. STATIC int next_router(const smartlist_t *cons, int cur);
  59. STATIC int *lcs_lengths(const smartlist_slice_t *slice1,
  60. const smartlist_slice_t *slice2,
  61. int direction);
  62. STATIC void trim_slices(smartlist_slice_t *slice1, smartlist_slice_t *slice2);
  63. STATIC int base64cmp(const cdline_t *hash1, const cdline_t *hash2);
  64. STATIC int get_id_hash(const cdline_t *line, cdline_t *hash_out);
  65. STATIC int is_valid_router_entry(const cdline_t *line);
  66. STATIC int smartlist_slice_string_pos(const smartlist_slice_t *slice,
  67. const cdline_t *string);
  68. STATIC void set_changed(bitarray_t *changed1, bitarray_t *changed2,
  69. const smartlist_slice_t *slice1,
  70. const smartlist_slice_t *slice2);
  71. STATIC int consensus_split_lines(smartlist_t *out, const char *s,
  72. struct memarea_t *area);
  73. STATIC void smartlist_add_linecpy(smartlist_t *lst, struct memarea_t *area,
  74. const char *s);
  75. STATIC int lines_eq(const cdline_t *a, const cdline_t *b);
  76. STATIC int line_str_eq(const cdline_t *a, const char *b);
  77. MOCK_DECL(STATIC int,
  78. consensus_compute_digest,(const char *cons,
  79. consensus_digest_t *digest_out));
  80. MOCK_DECL(STATIC int,
  81. consensus_compute_digest_as_signed,(const char *cons,
  82. consensus_digest_t *digest_out));
  83. MOCK_DECL(STATIC int,
  84. consensus_digest_eq,(const uint8_t *d1,
  85. const uint8_t *d2));
  86. #endif /* defined(CONSDIFF_PRIVATE) */
  87. #endif /* !defined(TOR_CONSDIFF_H) */