consdiff.h 4.2 KB

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