consdiff.h 4.1 KB

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