2017-03-07 15:58:30 +01:00
|
|
|
/* Copyright (c) 2014, Daniel Martí
|
|
|
|
* Copyright (c) 2014, The Tor Project, Inc. */
|
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \file consdiff.c
|
|
|
|
* \brief Consensus diff implementation, including both the generation and the
|
|
|
|
* application of diffs in a minimal ed format.
|
|
|
|
*
|
|
|
|
* The consensus diff application is done in consdiff_apply_diff, which relies
|
|
|
|
* on apply_ed_diff for the main ed diff part and on some digest helper
|
|
|
|
* functions to check the digest hashes found in the consensus diff header.
|
|
|
|
*
|
|
|
|
* The consensus diff generation is more complex. consdiff_gen_diff generates
|
|
|
|
* it, relying on gen_ed_diff to generate the ed diff and some digest helper
|
|
|
|
* functions to generate the digest hashes.
|
|
|
|
*
|
|
|
|
* gen_ed_diff is the tricky bit. In it simplest form, it will take quadratic
|
|
|
|
* time and linear space to generate an ed diff given two smartlists. As shown
|
|
|
|
* in its comment section, calling calc_changes on the entire two consensuses
|
|
|
|
* will calculate what is to be added and what is to be deleted in the diff.
|
|
|
|
* Its comment section briefly explains how it works.
|
|
|
|
*
|
|
|
|
* In our case specific to consensuses, we take advantage of the fact that
|
|
|
|
* consensuses list routers sorted by their identities. We use that
|
|
|
|
* information to avoid running calc_changes on the whole smartlists.
|
|
|
|
* gen_ed_diff will navigate through the two consensuses identity by identity
|
|
|
|
* and will send small couples of slices to calc_changes, keeping the running
|
|
|
|
* time near-linear. This is explained in more detail in the gen_ed_diff
|
|
|
|
* comments.
|
2017-03-14 20:00:39 +01:00
|
|
|
*
|
|
|
|
* The allocation strategy tries to save time and memory by avoiding needless
|
|
|
|
* copies. Instead of actually splitting the inputs into separate strings, we
|
|
|
|
* allocate cdline_t objects, each of which represents a line in the original
|
|
|
|
* object or in the output. We use memarea_t allocators to manage the
|
|
|
|
* temporary memory we use when generating or applying diffs.
|
2017-03-07 15:58:30 +01:00
|
|
|
**/
|
|
|
|
|
2017-03-07 16:22:00 +01:00
|
|
|
#define CONSDIFF_PRIVATE
|
|
|
|
|
2017-03-07 15:58:30 +01:00
|
|
|
#include "or.h"
|
|
|
|
#include "consdiff.h"
|
2017-03-14 20:00:39 +01:00
|
|
|
#include "memarea.h"
|
2017-03-07 15:58:30 +01:00
|
|
|
#include "routerparse.h"
|
|
|
|
|
|
|
|
static const char* ns_diff_version = "network-status-diff-version 1";
|
|
|
|
static const char* hash_token = "hash";
|
|
|
|
|
2017-03-07 19:45:32 +01:00
|
|
|
static char *consensus_join_lines(const smartlist_t *inp);
|
|
|
|
|
2017-03-14 20:00:39 +01:00
|
|
|
/** Return true iff a and b have the same contents. */
|
|
|
|
STATIC int
|
|
|
|
lines_eq(const cdline_t *a, const cdline_t *b)
|
|
|
|
{
|
|
|
|
return a->len == b->len && fast_memeq(a->s, b->s, a->len);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Return true iff a has the same contents as the nul-terminated string b. */
|
|
|
|
STATIC int
|
|
|
|
line_str_eq(const cdline_t *a, const char *b)
|
|
|
|
{
|
|
|
|
const size_t len = strlen(b);
|
|
|
|
tor_assert(len <= UINT32_MAX);
|
|
|
|
cdline_t bline = { b, (uint32_t)len };
|
|
|
|
return lines_eq(a, &bline);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Add a cdline_t to <b>lst</b> holding as its contents the nul-terminated
|
|
|
|
* string s. Use the provided memory area for storage. */
|
|
|
|
STATIC void
|
|
|
|
smartlist_add_linecpy(smartlist_t *lst, memarea_t *area, const char *s)
|
|
|
|
{
|
|
|
|
size_t len = strlen(s);
|
|
|
|
const char *ss = memarea_memdup(area, s, len);
|
|
|
|
cdline_t *line = memarea_alloc(area, sizeof(cdline_t));
|
|
|
|
line->s = ss;
|
|
|
|
line->len = (uint32_t)len;
|
|
|
|
smartlist_add(lst, line);
|
|
|
|
}
|
|
|
|
|
2017-03-08 00:15:25 +01:00
|
|
|
/** Compute the digest of <b>cons</b>, and store the result in
|
|
|
|
* <b>digest_out</b>. Return 0 on success, -1 on failure. */
|
|
|
|
/* This is a separate, mockable function so that we can override it when
|
|
|
|
* fuzzing. */
|
2017-03-07 19:45:32 +01:00
|
|
|
MOCK_IMPL(STATIC int,
|
|
|
|
consensus_compute_digest,(const char *cons,
|
|
|
|
consensus_digest_t *digest_out))
|
2017-03-07 19:02:00 +01:00
|
|
|
{
|
|
|
|
int r = crypto_digest256((char*)digest_out->sha3_256,
|
|
|
|
cons, strlen(cons), DIGEST_SHA3_256);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2017-03-08 00:15:25 +01:00
|
|
|
/** Return true iff <b>d1</b> and <b>d2</b> contain the same digest */
|
|
|
|
/* This is a separate, mockable function so that we can override it when
|
|
|
|
* fuzzing. */
|
2017-03-07 20:12:53 +01:00
|
|
|
MOCK_IMPL(STATIC int,
|
|
|
|
consensus_digest_eq,(const uint8_t *d1,
|
|
|
|
const uint8_t *d2))
|
|
|
|
{
|
|
|
|
return fast_memeq(d1, d2, DIGEST256_LEN);
|
|
|
|
}
|
|
|
|
|
2017-03-07 15:58:30 +01:00
|
|
|
/** Create (allocate) a new slice from a smartlist. Assumes that the start
|
|
|
|
* and the end indexes are within the bounds of the initial smartlist. The end
|
|
|
|
* element is not part of the resulting slice. If end is -1, the slice is to
|
|
|
|
* reach the end of the smartlist.
|
|
|
|
*/
|
2017-03-07 16:22:00 +01:00
|
|
|
STATIC smartlist_slice_t *
|
2017-03-07 19:11:38 +01:00
|
|
|
smartlist_slice(const smartlist_t *list, int start, int end)
|
2017-03-07 15:58:30 +01:00
|
|
|
{
|
|
|
|
int list_len = smartlist_len(list);
|
|
|
|
tor_assert(start >= 0);
|
|
|
|
tor_assert(start <= list_len);
|
|
|
|
if (end == -1) {
|
|
|
|
end = list_len;
|
|
|
|
}
|
|
|
|
tor_assert(start <= end);
|
|
|
|
|
|
|
|
smartlist_slice_t *slice = tor_malloc(sizeof(smartlist_slice_t));
|
|
|
|
slice->list = list;
|
|
|
|
slice->offset = start;
|
|
|
|
slice->len = end - start;
|
|
|
|
return slice;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Helper: Compute the longest common subsequence lengths for the two slices.
|
|
|
|
* Used as part of the diff generation to find the column at which to split
|
|
|
|
* slice2 while still having the optimal solution.
|
|
|
|
* If direction is -1, the navigation is reversed. Otherwise it must be 1.
|
|
|
|
* The length of the resulting integer array is that of the second slice plus
|
|
|
|
* one.
|
|
|
|
*/
|
2017-03-07 16:22:00 +01:00
|
|
|
STATIC int *
|
2017-03-07 19:11:38 +01:00
|
|
|
lcs_lengths(const smartlist_slice_t *slice1, const smartlist_slice_t *slice2,
|
2017-03-07 15:58:30 +01:00
|
|
|
int direction)
|
|
|
|
{
|
|
|
|
size_t a_size = sizeof(int) * (slice2->len+1);
|
|
|
|
|
|
|
|
/* Resulting lcs lengths. */
|
|
|
|
int *result = tor_malloc_zero(a_size);
|
|
|
|
/* Copy of the lcs lengths from the last iteration. */
|
|
|
|
int *prev = tor_malloc(a_size);
|
|
|
|
|
|
|
|
tor_assert(direction == 1 || direction == -1);
|
|
|
|
|
|
|
|
int si = slice1->offset;
|
|
|
|
if (direction == -1) {
|
|
|
|
si += (slice1->len-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < slice1->len; ++i, si+=direction) {
|
|
|
|
|
2017-03-14 20:00:39 +01:00
|
|
|
const cdline_t *line1 = smartlist_get(slice1->list, si);
|
2017-03-07 15:58:30 +01:00
|
|
|
/* Store the last results. */
|
|
|
|
memcpy(prev, result, a_size);
|
|
|
|
|
|
|
|
int sj = slice2->offset;
|
|
|
|
if (direction == -1) {
|
|
|
|
sj += (slice2->len-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int j = 0; j < slice2->len; ++j, sj+=direction) {
|
|
|
|
|
2017-03-14 20:00:39 +01:00
|
|
|
const cdline_t *line2 = smartlist_get(slice2->list, sj);
|
|
|
|
if (lines_eq(line1, line2)) {
|
2017-03-07 15:58:30 +01:00
|
|
|
/* If the lines are equal, the lcs is one line longer. */
|
|
|
|
result[j + 1] = prev[j] + 1;
|
|
|
|
} else {
|
|
|
|
/* If not, see what lcs parent path is longer. */
|
|
|
|
result[j + 1] = MAX(result[j], prev[j + 1]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tor_free(prev);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Helper: Trim any number of lines that are equally at the start or the end
|
|
|
|
* of both slices.
|
|
|
|
*/
|
2017-03-07 16:22:00 +01:00
|
|
|
STATIC void
|
2017-03-07 15:58:30 +01:00
|
|
|
trim_slices(smartlist_slice_t *slice1, smartlist_slice_t *slice2)
|
|
|
|
{
|
|
|
|
while (slice1->len>0 && slice2->len>0) {
|
2017-03-14 20:00:39 +01:00
|
|
|
const cdline_t *line1 = smartlist_get(slice1->list, slice1->offset);
|
|
|
|
const cdline_t *line2 = smartlist_get(slice2->list, slice2->offset);
|
|
|
|
if (!lines_eq(line1, line2)) {
|
2017-03-07 15:58:30 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
slice1->offset++; slice1->len--;
|
|
|
|
slice2->offset++; slice2->len--;
|
|
|
|
}
|
|
|
|
|
|
|
|
int i1 = (slice1->offset+slice1->len)-1;
|
|
|
|
int i2 = (slice2->offset+slice2->len)-1;
|
|
|
|
|
|
|
|
while (slice1->len>0 && slice2->len>0) {
|
2017-03-14 20:00:39 +01:00
|
|
|
const cdline_t *line1 = smartlist_get(slice1->list, i1);
|
|
|
|
const cdline_t *line2 = smartlist_get(slice2->list, i2);
|
|
|
|
if (!lines_eq(line1, line2)) {
|
2017-03-07 15:58:30 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
i1--;
|
|
|
|
slice1->len--;
|
|
|
|
i2--;
|
|
|
|
slice2->len--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-14 20:00:39 +01:00
|
|
|
/** Like smartlist_string_pos, but uses a cdline_t, and is restricted to the
|
|
|
|
* bounds of the slice.
|
2017-03-07 15:58:30 +01:00
|
|
|
*/
|
2017-03-07 16:22:00 +01:00
|
|
|
STATIC int
|
2017-03-14 20:00:39 +01:00
|
|
|
smartlist_slice_string_pos(const smartlist_slice_t *slice,
|
|
|
|
const cdline_t *string)
|
2017-03-07 15:58:30 +01:00
|
|
|
{
|
|
|
|
int end = slice->offset + slice->len;
|
|
|
|
for (int i = slice->offset; i < end; ++i) {
|
2017-03-14 20:00:39 +01:00
|
|
|
const cdline_t *el = smartlist_get(slice->list, i);
|
|
|
|
if (lines_eq(el, string)) {
|
2017-03-07 15:58:30 +01:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Helper: Set all the appropriate changed booleans to true. The first slice
|
|
|
|
* must be of length 0 or 1. All the lines of slice1 and slice2 which are not
|
|
|
|
* present in the other slice will be set to changed in their bool array.
|
|
|
|
* The two changed bool arrays are passed in the same order as the slices.
|
|
|
|
*/
|
2017-03-07 16:22:00 +01:00
|
|
|
STATIC void
|
2017-03-07 15:58:30 +01:00
|
|
|
set_changed(bitarray_t *changed1, bitarray_t *changed2,
|
2017-03-07 19:11:38 +01:00
|
|
|
const smartlist_slice_t *slice1, const smartlist_slice_t *slice2)
|
2017-03-07 15:58:30 +01:00
|
|
|
{
|
|
|
|
int toskip = -1;
|
|
|
|
tor_assert(slice1->len == 0 || slice1->len == 1);
|
|
|
|
|
|
|
|
if (slice1->len == 1) {
|
2017-03-14 20:00:39 +01:00
|
|
|
const cdline_t *line_common = smartlist_get(slice1->list, slice1->offset);
|
2017-03-07 15:58:30 +01:00
|
|
|
toskip = smartlist_slice_string_pos(slice2, line_common);
|
|
|
|
if (toskip == -1) {
|
|
|
|
bitarray_set(changed1, slice1->offset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
int end = slice2->offset + slice2->len;
|
|
|
|
for (int i = slice2->offset; i < end; ++i) {
|
|
|
|
if (i != toskip) {
|
|
|
|
bitarray_set(changed2, i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Helper: Given that slice1 has been split by half into top and bot, we want
|
|
|
|
* to fetch the column at which to split slice2 so that we are still on track
|
|
|
|
* to the optimal diff solution, i.e. the shortest one. We use lcs_lengths
|
|
|
|
* since the shortest diff is just another way to say the longest common
|
|
|
|
* subsequence.
|
|
|
|
*/
|
|
|
|
static int
|
2017-03-07 19:11:38 +01:00
|
|
|
optimal_column_to_split(const smartlist_slice_t *top,
|
|
|
|
const smartlist_slice_t *bot,
|
|
|
|
const smartlist_slice_t *slice2)
|
2017-03-07 15:58:30 +01:00
|
|
|
{
|
|
|
|
int *lens_top = lcs_lengths(top, slice2, 1);
|
|
|
|
int *lens_bot = lcs_lengths(bot, slice2, -1);
|
|
|
|
int column=0, max_sum=-1;
|
|
|
|
|
|
|
|
for (int i = 0; i < slice2->len+1; ++i) {
|
|
|
|
int sum = lens_top[i] + lens_bot[slice2->len-i];
|
|
|
|
if (sum > max_sum) {
|
|
|
|
column = i;
|
|
|
|
max_sum = sum;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tor_free(lens_top);
|
|
|
|
tor_free(lens_bot);
|
|
|
|
|
|
|
|
return column;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper: Figure out what elements are new or gone on the second smartlist
|
|
|
|
* relative to the first smartlist, and store the booleans in the bitarrays.
|
|
|
|
* True on the first bitarray means the element is gone, true on the second
|
|
|
|
* bitarray means it's new.
|
|
|
|
*
|
|
|
|
* In its base case, either of the smartlists is of length <= 1 and we can
|
|
|
|
* quickly see what elements are new or are gone. In the other case, we will
|
|
|
|
* split one smartlist by half and we'll use optimal_column_to_split to find
|
|
|
|
* the optimal column at which to split the second smartlist so that we are
|
|
|
|
* finding the smallest diff possible.
|
|
|
|
*/
|
2017-03-07 16:22:00 +01:00
|
|
|
STATIC void
|
2017-03-07 19:11:38 +01:00
|
|
|
calc_changes(smartlist_slice_t *slice1,
|
|
|
|
smartlist_slice_t *slice2,
|
2017-03-07 15:58:30 +01:00
|
|
|
bitarray_t *changed1, bitarray_t *changed2)
|
|
|
|
{
|
|
|
|
trim_slices(slice1, slice2);
|
|
|
|
|
|
|
|
if (slice1->len <= 1) {
|
|
|
|
set_changed(changed1, changed2, slice1, slice2);
|
|
|
|
|
|
|
|
} else if (slice2->len <= 1) {
|
|
|
|
set_changed(changed2, changed1, slice2, slice1);
|
|
|
|
|
|
|
|
/* Keep on splitting the slices in two. */
|
|
|
|
} else {
|
|
|
|
|
|
|
|
smartlist_slice_t *top, *bot, *left, *right;
|
|
|
|
|
|
|
|
/* Split the first slice in half. */
|
|
|
|
int mid = slice1->len/2;
|
|
|
|
top = smartlist_slice(slice1->list, slice1->offset, slice1->offset+mid);
|
|
|
|
bot = smartlist_slice(slice1->list, slice1->offset+mid,
|
|
|
|
slice1->offset+slice1->len);
|
|
|
|
|
|
|
|
/* Split the second slice by the optimal column. */
|
|
|
|
int mid2 = optimal_column_to_split(top, bot, slice2);
|
|
|
|
left = smartlist_slice(slice2->list, slice2->offset, slice2->offset+mid2);
|
|
|
|
right = smartlist_slice(slice2->list, slice2->offset+mid2,
|
|
|
|
slice2->offset+slice2->len);
|
|
|
|
|
|
|
|
calc_changes(top, left, changed1, changed2);
|
|
|
|
calc_changes(bot, right, changed1, changed2);
|
|
|
|
tor_free(top);
|
|
|
|
tor_free(bot);
|
|
|
|
tor_free(left);
|
|
|
|
tor_free(right);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This table is from crypto.c. The SP and PAD defines are different. */
|
|
|
|
#define X 255
|
|
|
|
#define SP X
|
|
|
|
#define PAD X
|
|
|
|
static const uint8_t base64_compare_table[256] = {
|
|
|
|
X, X, X, X, X, X, X, X, X, SP, SP, SP, X, SP, X, X,
|
|
|
|
X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X,
|
|
|
|
SP, X, X, X, X, X, X, X, X, X, X, 62, X, X, X, 63,
|
|
|
|
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, X, X, X, PAD, X, X,
|
|
|
|
X, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
|
|
|
|
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, X, X, X, X, X,
|
|
|
|
X, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
|
|
|
|
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, X, X, X, X, X,
|
|
|
|
X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X,
|
|
|
|
X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X,
|
|
|
|
X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X,
|
|
|
|
X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X,
|
|
|
|
X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X,
|
|
|
|
X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X,
|
|
|
|
X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X,
|
|
|
|
X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X,
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Helper: Get the identity hash from a router line, assuming that the line
|
|
|
|
* at least appears to be a router line and thus starts with "r ".
|
2017-03-14 20:00:39 +01:00
|
|
|
*
|
|
|
|
* If an identity hash is found, store it (without decoding it) in
|
|
|
|
* <b>hash_out</b>, and return 0. On failure, return -1.
|
2017-03-07 15:58:30 +01:00
|
|
|
*/
|
2017-03-14 20:00:39 +01:00
|
|
|
STATIC int
|
|
|
|
get_id_hash(const cdline_t *line, cdline_t *hash_out)
|
2017-03-07 15:58:30 +01:00
|
|
|
{
|
2017-03-14 20:00:39 +01:00
|
|
|
if (line->len < 2)
|
|
|
|
return -1;
|
2017-03-07 15:58:30 +01:00
|
|
|
|
|
|
|
/* Skip the router name. */
|
2017-03-14 20:00:39 +01:00
|
|
|
const char *hash = memchr(line->s + 2, ' ', line->len - 2);
|
2017-03-07 15:58:30 +01:00
|
|
|
if (!hash) {
|
2017-03-14 20:00:39 +01:00
|
|
|
return -1;
|
2017-03-07 15:58:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
hash++;
|
|
|
|
const char *hash_end = hash;
|
|
|
|
/* Stop when the first non-base64 character is found. Use unsigned chars to
|
|
|
|
* avoid negative indexes causing crashes.
|
|
|
|
*/
|
2017-03-14 20:00:39 +01:00
|
|
|
while (base64_compare_table[*((unsigned char*)hash_end)] != X &&
|
|
|
|
hash_end < line->s + line->len) {
|
2017-03-07 15:58:30 +01:00
|
|
|
hash_end++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Empty hash. */
|
|
|
|
if (hash_end == hash) {
|
2017-03-14 20:00:39 +01:00
|
|
|
return -1;
|
2017-03-07 15:58:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-14 20:00:39 +01:00
|
|
|
hash_out->s = hash;
|
|
|
|
/* Always true because lines are limited to this length */
|
|
|
|
tor_assert(hash_end - hash <= UINT32_MAX);
|
|
|
|
hash_out->len = (uint32_t)(hash_end - hash);
|
|
|
|
|
|
|
|
return 0;
|
2017-03-07 15:58:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Helper: Check that a line is a valid router entry. We must at least be
|
|
|
|
* able to fetch a proper identity hash from it for it to be valid.
|
|
|
|
*/
|
2017-03-07 16:22:00 +01:00
|
|
|
STATIC int
|
2017-03-14 20:00:39 +01:00
|
|
|
is_valid_router_entry(const cdline_t *line)
|
2017-03-07 15:58:30 +01:00
|
|
|
{
|
2017-03-14 20:00:39 +01:00
|
|
|
if (line->len < 2 || fast_memneq(line->s, "r ", 2))
|
2017-03-07 15:58:30 +01:00
|
|
|
return 0;
|
2017-03-14 20:00:39 +01:00
|
|
|
cdline_t tmp;
|
|
|
|
return (get_id_hash(line, &tmp) == 0);
|
2017-03-07 15:58:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Helper: Find the next router line starting at the current position.
|
|
|
|
* Assumes that cur is lower than the length of the smartlist, i.e. it is a
|
|
|
|
* line within the bounds of the consensus. The only exception is when we
|
|
|
|
* don't want to skip the first line, in which case cur will be -1.
|
|
|
|
*/
|
2017-03-07 16:22:00 +01:00
|
|
|
STATIC int
|
2017-03-07 19:11:38 +01:00
|
|
|
next_router(const smartlist_t *cons, int cur)
|
2017-03-07 15:58:30 +01:00
|
|
|
{
|
|
|
|
int len = smartlist_len(cons);
|
|
|
|
tor_assert(cur >= -1 && cur < len);
|
|
|
|
|
|
|
|
if (++cur >= len) {
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2017-03-14 20:00:39 +01:00
|
|
|
const cdline_t *line = smartlist_get(cons, cur);
|
2017-03-07 15:58:30 +01:00
|
|
|
while (!is_valid_router_entry(line)) {
|
|
|
|
if (++cur >= len) {
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
line = smartlist_get(cons, cur);
|
|
|
|
}
|
|
|
|
return cur;
|
|
|
|
}
|
|
|
|
|
2017-03-14 20:00:39 +01:00
|
|
|
/** Helper: compare two base64-encoded identity hashes, which may be of
|
2017-03-07 15:58:30 +01:00
|
|
|
* different lengths. Comparison ends when the first non-base64 char is found.
|
|
|
|
*/
|
2017-03-07 16:22:00 +01:00
|
|
|
STATIC int
|
2017-03-14 20:00:39 +01:00
|
|
|
base64cmp(const cdline_t *hash1, const cdline_t *hash2)
|
2017-03-07 15:58:30 +01:00
|
|
|
{
|
|
|
|
/* NULL is always lower, useful for last_hash which starts at NULL. */
|
2017-03-14 20:00:39 +01:00
|
|
|
if (!hash1->s && !hash2->s) {
|
2017-03-07 15:58:30 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2017-03-14 20:00:39 +01:00
|
|
|
if (!hash1->s) {
|
2017-03-07 15:58:30 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2017-03-14 20:00:39 +01:00
|
|
|
if (!hash2->s) {
|
2017-03-07 15:58:30 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Don't index with a char; char may be signed. */
|
2017-03-14 20:00:39 +01:00
|
|
|
const unsigned char *a = (unsigned char*)hash1->s;
|
|
|
|
const unsigned char *b = (unsigned char*)hash2->s;
|
|
|
|
const unsigned char *a_end = a + hash1->len;
|
|
|
|
const unsigned char *b_end = b + hash2->len;
|
2017-03-07 15:58:30 +01:00
|
|
|
while (1) {
|
|
|
|
uint8_t av = base64_compare_table[*a];
|
|
|
|
uint8_t bv = base64_compare_table[*b];
|
|
|
|
if (av == X) {
|
|
|
|
if (bv == X) {
|
|
|
|
/* Both ended with exactly the same characters. */
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
/* hash2 goes on longer than hash1 and thus hash1 is lower. */
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else if (bv == X) {
|
|
|
|
/* hash1 goes on longer than hash2 and thus hash1 is greater. */
|
|
|
|
return 1;
|
|
|
|
} else if (av < bv) {
|
|
|
|
/* The first difference shows that hash1 is lower. */
|
|
|
|
return -1;
|
|
|
|
} else if (av > bv) {
|
|
|
|
/* The first difference shows that hash1 is greater. */
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
a++;
|
|
|
|
b++;
|
2017-03-14 20:00:39 +01:00
|
|
|
if (a == a_end) {
|
|
|
|
if (b == b_end) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else if (b == b_end) {
|
|
|
|
return 1;
|
|
|
|
}
|
2017-03-07 15:58:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Generate an ed diff as a smartlist from two consensuses, also given as
|
|
|
|
* smartlists. Will return NULL if the diff could not be generated, which can
|
|
|
|
* happen if any lines the script had to add matched "." or if the routers
|
|
|
|
* were not properly ordered.
|
|
|
|
*
|
2017-03-14 20:00:39 +01:00
|
|
|
* All cdline_t objects in the resulting object are either references to lines
|
|
|
|
* in one of the inputs, or are newly allocated lines in the provided memarea.
|
|
|
|
*
|
2017-03-07 15:58:30 +01:00
|
|
|
* This implementation is consensus-specific. To generate an ed diff for any
|
|
|
|
* given input in quadratic time, you can replace all the code until the
|
|
|
|
* navigation in reverse order with the following:
|
|
|
|
*
|
|
|
|
* int len1 = smartlist_len(cons1);
|
|
|
|
* int len2 = smartlist_len(cons2);
|
|
|
|
* bitarray_t *changed1 = bitarray_init_zero(len1);
|
|
|
|
* bitarray_t *changed2 = bitarray_init_zero(len2);
|
|
|
|
* cons1_sl = smartlist_slice(cons1, 0, -1);
|
|
|
|
* cons2_sl = smartlist_slice(cons2, 0, -1);
|
|
|
|
* calc_changes(cons1_sl, cons2_sl, changed1, changed2);
|
|
|
|
*/
|
2017-03-07 16:22:00 +01:00
|
|
|
STATIC smartlist_t *
|
2017-03-14 20:00:39 +01:00
|
|
|
gen_ed_diff(const smartlist_t *cons1, const smartlist_t *cons2,
|
|
|
|
memarea_t *area)
|
2017-03-07 15:58:30 +01:00
|
|
|
{
|
|
|
|
int len1 = smartlist_len(cons1);
|
|
|
|
int len2 = smartlist_len(cons2);
|
|
|
|
smartlist_t *result = smartlist_new();
|
|
|
|
|
|
|
|
/* Initialize the changed bitarrays to zero, so that calc_changes only needs
|
|
|
|
* to set the ones that matter and leave the rest untouched.
|
|
|
|
*/
|
|
|
|
bitarray_t *changed1 = bitarray_init_zero(len1);
|
|
|
|
bitarray_t *changed2 = bitarray_init_zero(len2);
|
|
|
|
int i1=-1, i2=-1;
|
|
|
|
int start1=0, start2=0;
|
|
|
|
|
|
|
|
/* To check that hashes are ordered properly */
|
2017-03-14 20:00:39 +01:00
|
|
|
cdline_t hash1 = { NULL, 0 }, hash2 = { NULL, 0 };
|
|
|
|
cdline_t last_hash1 = { NULL, 0 }, last_hash2 = { NULL, 0 };
|
2017-03-07 15:58:30 +01:00
|
|
|
|
|
|
|
/* i1 and i2 are initialized at the first line of each consensus. They never
|
|
|
|
* reach past len1 and len2 respectively, since next_router doesn't let that
|
|
|
|
* happen. i1 and i2 are advanced by at least one line at each iteration as
|
|
|
|
* long as they have not yet reached len1 and len2, so the loop is
|
|
|
|
* guaranteed to end, and each pair of (i1,i2) will be inspected at most
|
|
|
|
* once.
|
|
|
|
*/
|
|
|
|
while (i1 < len1 || i2 < len2) {
|
|
|
|
|
|
|
|
/* Advance each of the two navigation positions by one router entry if not
|
|
|
|
* yet at the end.
|
|
|
|
*/
|
|
|
|
if (i1 < len1) {
|
|
|
|
i1 = next_router(cons1, i1);
|
|
|
|
if (i1 != len1) {
|
2017-03-14 20:00:39 +01:00
|
|
|
memcpy(&last_hash1, &hash1, sizeof(cdline_t));
|
|
|
|
if (get_id_hash(smartlist_get(cons1, i1), &hash1) < 0 ||
|
|
|
|
base64cmp(&hash1, &last_hash1) <= 0) {
|
2017-03-07 15:58:30 +01:00
|
|
|
log_warn(LD_CONSDIFF, "Refusing to generate consensus diff because "
|
|
|
|
"the base consensus doesn't have its router entries "
|
|
|
|
"sorted properly.");
|
|
|
|
goto error_cleanup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i2 < len2) {
|
|
|
|
i2 = next_router(cons2, i2);
|
|
|
|
if (i2 != len2) {
|
2017-03-14 20:00:39 +01:00
|
|
|
memcpy(&last_hash2, &hash2, sizeof(cdline_t));
|
|
|
|
if (get_id_hash(smartlist_get(cons2, i2), &hash2) < 0 ||
|
|
|
|
base64cmp(&hash2, &last_hash2) <= 0) {
|
2017-03-07 15:58:30 +01:00
|
|
|
log_warn(LD_CONSDIFF, "Refusing to generate consensus diff because "
|
|
|
|
"the target consensus doesn't have its router entries "
|
|
|
|
"sorted properly.");
|
|
|
|
goto error_cleanup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If we have reached the end of both consensuses, there is no need to
|
|
|
|
* compare hashes anymore, since this is the last iteration.
|
|
|
|
*/
|
|
|
|
if (i1 < len1 || i2 < len2) {
|
|
|
|
|
|
|
|
/* Keep on advancing the lower (by identity hash sorting) position until
|
|
|
|
* we have two matching positions. The only other possible outcome is
|
|
|
|
* that a lower position reaches the end of the consensus before it can
|
|
|
|
* reach a hash that is no longer the lower one. Since there will always
|
|
|
|
* be a lower hash for as long as the loop runs, one of the two indexes
|
|
|
|
* will always be incremented, thus assuring that the loop must end
|
|
|
|
* after a finite number of iterations. If that cannot be because said
|
|
|
|
* consensus has already reached the end, both are extended to their
|
|
|
|
* respecting ends since we are done.
|
|
|
|
*/
|
2017-03-14 20:00:39 +01:00
|
|
|
int cmp = base64cmp(&hash1, &hash2);
|
2017-03-07 15:58:30 +01:00
|
|
|
while (cmp != 0) {
|
|
|
|
if (i1 < len1 && cmp < 0) {
|
|
|
|
i1 = next_router(cons1, i1);
|
|
|
|
if (i1 == len1) {
|
|
|
|
/* We finished the first consensus, so grab all the remaining
|
|
|
|
* lines of the second consensus and finish up.
|
|
|
|
*/
|
|
|
|
i2 = len2;
|
|
|
|
break;
|
|
|
|
}
|
2017-03-14 20:00:39 +01:00
|
|
|
memcpy(&last_hash1, &hash1, sizeof(cdline_t));
|
|
|
|
if (get_id_hash(smartlist_get(cons1, i1), &hash1) < 0 ||
|
|
|
|
base64cmp(&hash1, &last_hash1) <= 0) {
|
2017-03-07 15:58:30 +01:00
|
|
|
log_warn(LD_CONSDIFF, "Refusing to generate consensus diff "
|
|
|
|
"because the base consensus doesn't have its router entries "
|
|
|
|
"sorted properly.");
|
|
|
|
goto error_cleanup;
|
|
|
|
}
|
|
|
|
} else if (i2 < len2 && cmp > 0) {
|
|
|
|
i2 = next_router(cons2, i2);
|
|
|
|
if (i2 == len2) {
|
|
|
|
/* We finished the second consensus, so grab all the remaining
|
|
|
|
* lines of the first consensus and finish up.
|
|
|
|
*/
|
|
|
|
i1 = len1;
|
|
|
|
break;
|
|
|
|
}
|
2017-03-14 20:00:39 +01:00
|
|
|
memcpy(&last_hash2, &hash2, sizeof(cdline_t));
|
|
|
|
if (get_id_hash(smartlist_get(cons2, i2), &hash2) < 0 ||
|
|
|
|
base64cmp(&hash2, &last_hash2) <= 0) {
|
2017-03-07 15:58:30 +01:00
|
|
|
log_warn(LD_CONSDIFF, "Refusing to generate consensus diff "
|
|
|
|
"because the target consensus doesn't have its router entries "
|
|
|
|
"sorted properly.");
|
|
|
|
goto error_cleanup;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
i1 = len1;
|
|
|
|
i2 = len2;
|
|
|
|
break;
|
|
|
|
}
|
2017-03-14 20:00:39 +01:00
|
|
|
cmp = base64cmp(&hash1, &hash2);
|
2017-03-07 15:58:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Make slices out of these chunks (up to the common router entry) and
|
|
|
|
* calculate the changes for them.
|
|
|
|
* Error if any of the two slices are longer than 10K lines. That should
|
|
|
|
* never happen with any pair of real consensuses. Feeding more than 10K
|
|
|
|
* lines to calc_changes would be very slow anyway.
|
|
|
|
*/
|
|
|
|
#define MAX_LINE_COUNT (10000)
|
|
|
|
if (i1-start1 > MAX_LINE_COUNT || i2-start2 > MAX_LINE_COUNT) {
|
|
|
|
log_warn(LD_CONSDIFF, "Refusing to generate consensus diff because "
|
|
|
|
"we found too few common router ids.");
|
|
|
|
goto error_cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
smartlist_slice_t *cons1_sl = smartlist_slice(cons1, start1, i1);
|
|
|
|
smartlist_slice_t *cons2_sl = smartlist_slice(cons2, start2, i2);
|
|
|
|
calc_changes(cons1_sl, cons2_sl, changed1, changed2);
|
|
|
|
tor_free(cons1_sl);
|
|
|
|
tor_free(cons2_sl);
|
|
|
|
start1 = i1, start2 = i2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Navigate the changes in reverse order and generate one ed command for
|
|
|
|
* each chunk of changes.
|
|
|
|
*/
|
|
|
|
i1=len1-1, i2=len2-1;
|
2017-03-14 20:00:39 +01:00
|
|
|
char buf[128];
|
2017-03-07 15:58:30 +01:00
|
|
|
while (i1 > 0 || i2 > 0) {
|
|
|
|
|
|
|
|
int start1x, start2x, end1, end2, added, deleted;
|
|
|
|
|
|
|
|
/* We are at a point were no changed bools are true, so just keep going. */
|
|
|
|
if (!(i1 >= 0 && bitarray_is_set(changed1, i1)) &&
|
|
|
|
!(i2 >= 0 && bitarray_is_set(changed2, i2))) {
|
|
|
|
if (i1 >= 0) {
|
|
|
|
i1--;
|
|
|
|
}
|
|
|
|
if (i2 >= 0) {
|
|
|
|
i2--;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
end1 = i1, end2 = i2;
|
|
|
|
|
|
|
|
/* Grab all contiguous changed lines */
|
|
|
|
while (i1 >= 0 && bitarray_is_set(changed1, i1)) {
|
|
|
|
i1--;
|
|
|
|
}
|
|
|
|
while (i2 >= 0 && bitarray_is_set(changed2, i2)) {
|
|
|
|
i2--;
|
|
|
|
}
|
|
|
|
|
|
|
|
start1x = i1+1, start2x = i2+1;
|
|
|
|
added = end2-i2, deleted = end1-i1;
|
|
|
|
|
|
|
|
if (added == 0) {
|
|
|
|
if (deleted == 1) {
|
2017-03-14 20:00:39 +01:00
|
|
|
tor_snprintf(buf, sizeof(buf), "%id", start1x+1);
|
|
|
|
smartlist_add_linecpy(result, area, buf);
|
2017-03-07 15:58:30 +01:00
|
|
|
} else {
|
2017-03-14 20:00:39 +01:00
|
|
|
tor_snprintf(buf, sizeof(buf), "%i,%id", start1x+1, start1x+deleted);
|
|
|
|
smartlist_add_linecpy(result, area, buf);
|
2017-03-07 15:58:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
int i;
|
|
|
|
if (deleted == 0) {
|
2017-03-14 20:00:39 +01:00
|
|
|
tor_snprintf(buf, sizeof(buf), "%ia", start1x);
|
|
|
|
smartlist_add_linecpy(result, area, buf);
|
2017-03-07 15:58:30 +01:00
|
|
|
} else if (deleted == 1) {
|
2017-03-14 20:00:39 +01:00
|
|
|
tor_snprintf(buf, sizeof(buf), "%ic", start1x+1);
|
|
|
|
smartlist_add_linecpy(result, area, buf);
|
2017-03-07 15:58:30 +01:00
|
|
|
} else {
|
2017-03-14 20:00:39 +01:00
|
|
|
tor_snprintf(buf, sizeof(buf), "%i,%ic", start1x+1, start1x+deleted);
|
|
|
|
smartlist_add_linecpy(result, area, buf);
|
2017-03-07 15:58:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = start2x; i <= end2; ++i) {
|
2017-03-14 20:00:39 +01:00
|
|
|
cdline_t *line = smartlist_get(cons2, i);
|
|
|
|
if (line_str_eq(line, ".")) {
|
2017-03-07 15:58:30 +01:00
|
|
|
log_warn(LD_CONSDIFF, "Cannot generate consensus diff because "
|
|
|
|
"one of the lines to be added is \".\".");
|
|
|
|
goto error_cleanup;
|
|
|
|
}
|
2017-03-14 20:00:39 +01:00
|
|
|
smartlist_add(result, line);
|
2017-03-07 15:58:30 +01:00
|
|
|
}
|
2017-03-14 20:00:39 +01:00
|
|
|
smartlist_add_linecpy(result, area, ".");
|
2017-03-07 15:58:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bitarray_free(changed1);
|
|
|
|
bitarray_free(changed2);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
error_cleanup:
|
|
|
|
|
|
|
|
bitarray_free(changed1);
|
|
|
|
bitarray_free(changed2);
|
|
|
|
|
|
|
|
smartlist_free(result);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-03-07 19:15:43 +01:00
|
|
|
/** Apply the ed diff, starting at <b>diff_starting_line</b>, to the consensus
|
|
|
|
* and return a new consensus, also as a line-based smartlist. Will return
|
|
|
|
* NULL if the ed diff is not properly formatted.
|
2017-03-14 20:00:39 +01:00
|
|
|
*
|
|
|
|
* All cdline_t objects in the resulting object are references to lines
|
|
|
|
* in one of the inputs; nothing is copied.
|
2017-03-07 15:58:30 +01:00
|
|
|
*/
|
2017-03-07 16:22:00 +01:00
|
|
|
STATIC smartlist_t *
|
2017-03-07 19:15:43 +01:00
|
|
|
apply_ed_diff(const smartlist_t *cons1, const smartlist_t *diff,
|
|
|
|
int diff_starting_line)
|
2017-03-07 15:58:30 +01:00
|
|
|
{
|
|
|
|
int diff_len = smartlist_len(diff);
|
|
|
|
int j = smartlist_len(cons1);
|
|
|
|
smartlist_t *cons2 = smartlist_new();
|
|
|
|
|
2017-03-07 19:15:43 +01:00
|
|
|
for (int i=diff_starting_line; i<diff_len; ++i) {
|
2017-03-14 20:00:39 +01:00
|
|
|
const cdline_t *diff_cdline = smartlist_get(diff, i);
|
|
|
|
char diff_line[128];
|
2017-03-07 15:58:30 +01:00
|
|
|
char *endptr1, *endptr2;
|
|
|
|
|
2017-03-14 20:00:39 +01:00
|
|
|
if (diff_cdline->len > sizeof(diff_line) - 1) {
|
|
|
|
log_warn(LD_CONSDIFF, "Could not apply consensus diff because "
|
|
|
|
"an ed command was far too long");
|
|
|
|
goto error_cleanup;
|
|
|
|
}
|
|
|
|
memcpy(diff_line, diff_cdline->s, diff_cdline->len);
|
|
|
|
diff_line[diff_cdline->len] = 0;
|
2017-03-07 15:58:30 +01:00
|
|
|
int start = (int)strtol(diff_line, &endptr1, 10);
|
|
|
|
int end;
|
|
|
|
if (endptr1 == diff_line) {
|
|
|
|
log_warn(LD_CONSDIFF, "Could not apply consensus diff because "
|
|
|
|
"an ed command was missing a line number.");
|
|
|
|
goto error_cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Two-item range */
|
|
|
|
if (*endptr1 == ',') {
|
|
|
|
end = (int)strtol(endptr1+1, &endptr2, 10);
|
|
|
|
if (endptr2 == endptr1+1) {
|
|
|
|
log_warn(LD_CONSDIFF, "Could not apply consensus diff because "
|
|
|
|
"an ed command was missing a range end line number.");
|
2017-03-07 17:08:18 +01:00
|
|
|
goto error_cleanup;
|
2017-03-07 15:58:30 +01:00
|
|
|
}
|
|
|
|
/* Incoherent range. */
|
|
|
|
if (end <= start) {
|
|
|
|
log_warn(LD_CONSDIFF, "Could not apply consensus diff because "
|
|
|
|
"an invalid range was found in an ed command.");
|
2017-03-07 17:08:18 +01:00
|
|
|
goto error_cleanup;
|
2017-03-07 15:58:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* We'll take <n1> as <n1>,<n1> for simplicity. */
|
|
|
|
} else {
|
|
|
|
endptr2 = endptr1;
|
|
|
|
end = start;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (end > j) {
|
|
|
|
log_warn(LD_CONSDIFF, "Could not apply consensus diff because "
|
|
|
|
"its commands are not properly sorted in reverse order.");
|
|
|
|
goto error_cleanup;
|
|
|
|
}
|
|
|
|
|
2017-03-07 17:08:18 +01:00
|
|
|
if (*endptr2 == '\0') {
|
|
|
|
log_warn(LD_CONSDIFF, "Could not apply consensus diff because "
|
|
|
|
"a line with no ed command was found");
|
|
|
|
goto error_cleanup;
|
|
|
|
}
|
|
|
|
|
2017-03-07 15:58:30 +01:00
|
|
|
if (*(endptr2+1) != '\0') {
|
|
|
|
log_warn(LD_CONSDIFF, "Could not apply consensus diff because "
|
|
|
|
"an ed command longer than one char was found.");
|
|
|
|
goto error_cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
char action = *endptr2;
|
|
|
|
|
|
|
|
switch (action) {
|
|
|
|
case 'a':
|
|
|
|
case 'c':
|
|
|
|
case 'd':
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
log_warn(LD_CONSDIFF, "Could not apply consensus diff because "
|
|
|
|
"an unrecognised ed command was found.");
|
|
|
|
goto error_cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add unchanged lines. */
|
2017-03-07 22:06:03 +01:00
|
|
|
for (; j && j > end; --j) {
|
2017-03-14 20:00:39 +01:00
|
|
|
cdline_t *cons_line = smartlist_get(cons1, j-1);
|
|
|
|
smartlist_add(cons2, cons_line);
|
2017-03-07 15:58:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Ignore removed lines. */
|
|
|
|
if (action == 'c' || action == 'd') {
|
|
|
|
while (--j >= start) {
|
|
|
|
/* Skip line */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add new lines in reverse order, since it will all be reversed at the
|
|
|
|
* end.
|
|
|
|
*/
|
|
|
|
if (action == 'a' || action == 'c') {
|
|
|
|
int added_end = i;
|
|
|
|
|
|
|
|
i++; /* Skip the line with the range and command. */
|
|
|
|
while (i < diff_len) {
|
2017-03-14 20:00:39 +01:00
|
|
|
if (line_str_eq(smartlist_get(diff, i), ".")) {
|
2017-03-07 15:58:30 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (++i == diff_len) {
|
|
|
|
log_warn(LD_CONSDIFF, "Could not apply consensus diff because "
|
|
|
|
"it has lines to be inserted that don't end with a \".\".");
|
|
|
|
goto error_cleanup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int added_i = i-1;
|
|
|
|
|
|
|
|
/* It would make no sense to add zero new lines. */
|
|
|
|
if (added_i == added_end) {
|
|
|
|
log_warn(LD_CONSDIFF, "Could not apply consensus diff because "
|
|
|
|
"it has an ed command that tries to insert zero lines.");
|
|
|
|
goto error_cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (added_i > added_end) {
|
2017-03-14 20:00:39 +01:00
|
|
|
cdline_t *added_line = smartlist_get(diff, added_i--);
|
|
|
|
smartlist_add(cons2, added_line);
|
2017-03-07 15:58:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add remaining unchanged lines. */
|
|
|
|
for (; j > 0; --j) {
|
2017-03-14 20:00:39 +01:00
|
|
|
cdline_t *cons_line = smartlist_get(cons1, j-1);
|
|
|
|
smartlist_add(cons2, cons_line);
|
2017-03-07 15:58:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Reverse the whole thing since we did it from the end. */
|
|
|
|
smartlist_reverse(cons2);
|
|
|
|
return cons2;
|
|
|
|
|
|
|
|
error_cleanup:
|
|
|
|
|
|
|
|
smartlist_free(cons2);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Generate a consensus diff as a smartlist from two given consensuses, also
|
|
|
|
* as smartlists. Will return NULL if the consensus diff could not be
|
|
|
|
* generated. Neither of the two consensuses are modified in any way, so it's
|
|
|
|
* up to the caller to free their resources.
|
|
|
|
*/
|
|
|
|
smartlist_t *
|
2017-03-14 20:00:39 +01:00
|
|
|
consdiff_gen_diff(const smartlist_t *cons1,
|
|
|
|
const smartlist_t *cons2,
|
2017-03-07 19:11:38 +01:00
|
|
|
const consensus_digest_t *digests1,
|
2017-03-14 20:00:39 +01:00
|
|
|
const consensus_digest_t *digests2,
|
|
|
|
memarea_t *area)
|
2017-03-07 15:58:30 +01:00
|
|
|
{
|
2017-03-14 20:00:39 +01:00
|
|
|
smartlist_t *ed_diff = gen_ed_diff(cons1, cons2, area);
|
2017-03-07 15:58:30 +01:00
|
|
|
/* ed diff could not be generated - reason already logged by gen_ed_diff. */
|
|
|
|
if (!ed_diff) {
|
|
|
|
goto error_cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* See that the script actually produces what we want. */
|
2017-03-07 19:15:43 +01:00
|
|
|
smartlist_t *ed_cons2 = apply_ed_diff(cons1, ed_diff, 0);
|
2017-03-07 15:58:30 +01:00
|
|
|
if (!ed_cons2) {
|
2017-03-07 17:13:40 +01:00
|
|
|
/* LCOV_EXCL_START -- impossible if diff generation is correct */
|
|
|
|
log_warn(LD_BUG|LD_CONSDIFF, "Refusing to generate consensus diff because "
|
2017-03-07 15:58:30 +01:00
|
|
|
"the generated ed diff could not be tested to successfully generate "
|
|
|
|
"the target consensus.");
|
|
|
|
goto error_cleanup;
|
2017-03-07 17:13:40 +01:00
|
|
|
/* LCOV_EXCL_STOP */
|
2017-03-07 15:58:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-14 20:00:39 +01:00
|
|
|
int cons2_eq = 1;
|
|
|
|
if (smartlist_len(cons2) == smartlist_len(ed_cons2)) {
|
|
|
|
SMARTLIST_FOREACH_BEGIN(cons2, const cdline_t *, line1) {
|
|
|
|
const cdline_t *line2 = smartlist_get(ed_cons2, line1_sl_idx);
|
|
|
|
if (! lines_eq(line1, line2) ) {
|
|
|
|
cons2_eq = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} SMARTLIST_FOREACH_END(line1);
|
|
|
|
} else {
|
|
|
|
cons2_eq = 0;
|
|
|
|
}
|
2017-03-07 15:58:30 +01:00
|
|
|
smartlist_free(ed_cons2);
|
|
|
|
if (!cons2_eq) {
|
2017-03-07 17:13:40 +01:00
|
|
|
/* LCOV_EXCL_START -- impossible if diff generation is correct. */
|
|
|
|
log_warn(LD_BUG|LD_CONSDIFF, "Refusing to generate consensus diff because "
|
2017-03-07 15:58:30 +01:00
|
|
|
"the generated ed diff did not generate the target consensus "
|
|
|
|
"successfully when tested.");
|
|
|
|
goto error_cleanup;
|
2017-03-07 17:13:40 +01:00
|
|
|
/* LCOV_EXCL_STOP */
|
2017-03-07 15:58:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
char cons1_hash_hex[HEX_DIGEST256_LEN+1];
|
|
|
|
char cons2_hash_hex[HEX_DIGEST256_LEN+1];
|
|
|
|
base16_encode(cons1_hash_hex, HEX_DIGEST256_LEN+1,
|
2017-03-07 19:02:00 +01:00
|
|
|
(const char*)digests1->sha3_256, DIGEST256_LEN);
|
2017-03-07 15:58:30 +01:00
|
|
|
base16_encode(cons2_hash_hex, HEX_DIGEST256_LEN+1,
|
2017-03-07 19:02:00 +01:00
|
|
|
(const char*)digests2->sha3_256, DIGEST256_LEN);
|
2017-03-07 15:58:30 +01:00
|
|
|
|
|
|
|
/* Create the resulting consensus diff. */
|
2017-03-14 20:00:39 +01:00
|
|
|
char buf[160];
|
2017-03-07 15:58:30 +01:00
|
|
|
smartlist_t *result = smartlist_new();
|
2017-03-14 20:00:39 +01:00
|
|
|
tor_snprintf(buf, sizeof(buf), "%s", ns_diff_version);
|
|
|
|
smartlist_add_linecpy(result, area, buf);
|
|
|
|
tor_snprintf(buf, sizeof(buf), "%s %s %s", hash_token,
|
2017-03-07 17:47:19 +01:00
|
|
|
cons1_hash_hex, cons2_hash_hex);
|
2017-03-14 20:00:39 +01:00
|
|
|
smartlist_add_linecpy(result, area, buf);
|
2017-03-07 17:47:19 +01:00
|
|
|
smartlist_add_all(result, ed_diff);
|
2017-03-07 15:58:30 +01:00
|
|
|
smartlist_free(ed_diff);
|
|
|
|
return result;
|
|
|
|
|
2017-03-07 17:13:40 +01:00
|
|
|
error_cleanup:
|
2017-03-07 15:58:30 +01:00
|
|
|
|
|
|
|
if (ed_diff) {
|
2017-03-07 17:47:19 +01:00
|
|
|
/* LCOV_EXCL_START -- ed_diff is NULL except in unreachable cases above */
|
2017-03-07 15:58:30 +01:00
|
|
|
smartlist_free(ed_diff);
|
2017-03-07 17:47:19 +01:00
|
|
|
/* LCOV_EXCL_STOP */
|
2017-03-07 15:58:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Fetch the digest of the base consensus in the consensus diff, encoded in
|
2017-03-07 17:42:24 +01:00
|
|
|
* base16 as found in the diff itself. digest1_out and digest2_out must be of
|
|
|
|
* length DIGEST256_LEN or larger if not NULL.
|
2017-03-07 15:58:30 +01:00
|
|
|
*/
|
|
|
|
int
|
2017-03-07 19:11:38 +01:00
|
|
|
consdiff_get_digests(const smartlist_t *diff,
|
2017-03-07 17:42:24 +01:00
|
|
|
char *digest1_out,
|
|
|
|
char *digest2_out)
|
2017-03-07 15:58:30 +01:00
|
|
|
{
|
|
|
|
smartlist_t *hash_words = NULL;
|
2017-03-14 20:00:39 +01:00
|
|
|
const cdline_t *format;
|
2017-03-07 15:58:30 +01:00
|
|
|
char cons1_hash[DIGEST256_LEN], cons2_hash[DIGEST256_LEN];
|
|
|
|
char *cons1_hash_hex, *cons2_hash_hex;
|
2017-03-07 17:35:50 +01:00
|
|
|
if (smartlist_len(diff) < 2) {
|
2017-03-07 15:58:30 +01:00
|
|
|
log_info(LD_CONSDIFF, "The provided consensus diff is too short.");
|
|
|
|
goto error_cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check that it's the format and version we know. */
|
|
|
|
format = smartlist_get(diff, 0);
|
2017-03-14 20:00:39 +01:00
|
|
|
if (!line_str_eq(format, ns_diff_version)) {
|
2017-03-07 15:58:30 +01:00
|
|
|
log_warn(LD_CONSDIFF, "The provided consensus diff format is not known.");
|
|
|
|
goto error_cleanup;
|
|
|
|
}
|
|
|
|
|
2017-03-07 19:02:00 +01:00
|
|
|
/* Grab the base16 digests. */
|
2017-03-07 15:58:30 +01:00
|
|
|
hash_words = smartlist_new();
|
2017-03-14 20:00:39 +01:00
|
|
|
{
|
|
|
|
const cdline_t *line2 = smartlist_get(diff, 1);
|
|
|
|
char *h = tor_memdup_nulterm(line2->s, line2->len);
|
|
|
|
smartlist_split_string(hash_words, h, " ", 0, 0);
|
|
|
|
tor_free(h);
|
|
|
|
}
|
2017-03-07 15:58:30 +01:00
|
|
|
|
|
|
|
/* There have to be three words, the first of which must be hash_token. */
|
|
|
|
if (smartlist_len(hash_words) != 3 ||
|
|
|
|
strcmp(smartlist_get(hash_words, 0), hash_token)) {
|
|
|
|
log_info(LD_CONSDIFF, "The provided consensus diff does not include "
|
2017-03-07 17:36:07 +01:00
|
|
|
"the necessary digests.");
|
2017-03-07 15:58:30 +01:00
|
|
|
goto error_cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Expected hashes as found in the consensus diff header. They must be of
|
|
|
|
* length HEX_DIGEST256_LEN, normally 64 hexadecimal characters.
|
|
|
|
* If any of the decodings fail, error to make sure that the hashes are
|
2017-03-07 19:02:00 +01:00
|
|
|
* proper base16-encoded digests.
|
2017-03-07 15:58:30 +01:00
|
|
|
*/
|
|
|
|
cons1_hash_hex = smartlist_get(hash_words, 1);
|
|
|
|
cons2_hash_hex = smartlist_get(hash_words, 2);
|
|
|
|
if (strlen(cons1_hash_hex) != HEX_DIGEST256_LEN ||
|
|
|
|
strlen(cons2_hash_hex) != HEX_DIGEST256_LEN) {
|
|
|
|
log_info(LD_CONSDIFF, "The provided consensus diff includes "
|
2017-03-07 17:36:07 +01:00
|
|
|
"base16-encoded digests of incorrect size.");
|
2017-03-07 15:58:30 +01:00
|
|
|
goto error_cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (base16_decode(cons1_hash, DIGEST256_LEN,
|
|
|
|
cons1_hash_hex, HEX_DIGEST256_LEN) != DIGEST256_LEN ||
|
|
|
|
base16_decode(cons2_hash, DIGEST256_LEN,
|
|
|
|
cons2_hash_hex, HEX_DIGEST256_LEN) != DIGEST256_LEN) {
|
|
|
|
log_info(LD_CONSDIFF, "The provided consensus diff includes "
|
2017-03-07 17:36:07 +01:00
|
|
|
"malformed digests.");
|
2017-03-07 15:58:30 +01:00
|
|
|
goto error_cleanup;
|
|
|
|
}
|
|
|
|
|
2017-03-07 17:42:24 +01:00
|
|
|
if (digest1_out) {
|
|
|
|
memcpy(digest1_out, cons1_hash, DIGEST256_LEN);
|
2017-03-07 15:58:30 +01:00
|
|
|
}
|
2017-03-07 17:42:24 +01:00
|
|
|
if (digest2_out) {
|
|
|
|
memcpy(digest2_out, cons2_hash, DIGEST256_LEN);
|
2017-03-07 15:58:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
SMARTLIST_FOREACH(hash_words, char *, cp, tor_free(cp));
|
|
|
|
smartlist_free(hash_words);
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
error_cleanup:
|
|
|
|
|
|
|
|
if (hash_words) {
|
|
|
|
SMARTLIST_FOREACH(hash_words, char *, cp, tor_free(cp));
|
|
|
|
smartlist_free(hash_words);
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Apply the consensus diff to the given consensus and return a new
|
|
|
|
* consensus, also as a line-based smartlist. Will return NULL if the diff
|
|
|
|
* could not be applied. Neither the consensus nor the diff are modified in
|
|
|
|
* any way, so it's up to the caller to free their resources.
|
|
|
|
*/
|
|
|
|
char *
|
2017-03-07 19:11:38 +01:00
|
|
|
consdiff_apply_diff(const smartlist_t *cons1,
|
|
|
|
const smartlist_t *diff,
|
|
|
|
const consensus_digest_t *digests1)
|
2017-03-07 15:58:30 +01:00
|
|
|
{
|
|
|
|
smartlist_t *cons2 = NULL;
|
|
|
|
char *cons2_str = NULL;
|
|
|
|
char e_cons1_hash[DIGEST256_LEN];
|
|
|
|
char e_cons2_hash[DIGEST256_LEN];
|
|
|
|
|
2017-03-07 17:42:24 +01:00
|
|
|
if (consdiff_get_digests(diff, e_cons1_hash, e_cons2_hash) != 0) {
|
2017-03-07 15:58:30 +01:00
|
|
|
goto error_cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* See that the consensus that was given to us matches its hash. */
|
2017-03-07 20:12:53 +01:00
|
|
|
if (!consensus_digest_eq(digests1->sha3_256,
|
|
|
|
(const uint8_t*)e_cons1_hash)) {
|
2017-03-07 15:58:30 +01:00
|
|
|
char hex_digest1[HEX_DIGEST256_LEN+1];
|
|
|
|
char e_hex_digest1[HEX_DIGEST256_LEN+1];
|
|
|
|
log_warn(LD_CONSDIFF, "Refusing to apply consensus diff because "
|
2017-03-07 17:36:07 +01:00
|
|
|
"the base consensus doesn't match the digest as found in "
|
2017-03-07 15:58:30 +01:00
|
|
|
"the consensus diff header.");
|
|
|
|
base16_encode(hex_digest1, HEX_DIGEST256_LEN+1,
|
2017-03-07 19:02:00 +01:00
|
|
|
(const char *)digests1->sha3_256, DIGEST256_LEN);
|
2017-03-07 15:58:30 +01:00
|
|
|
base16_encode(e_hex_digest1, HEX_DIGEST256_LEN+1,
|
2017-03-07 19:02:00 +01:00
|
|
|
e_cons1_hash, DIGEST256_LEN);
|
2017-03-07 16:48:03 +01:00
|
|
|
log_warn(LD_CONSDIFF, "Expected: %s; found: %s",
|
2017-03-07 15:58:30 +01:00
|
|
|
hex_digest1, e_hex_digest1);
|
|
|
|
goto error_cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Grab the ed diff and calculate the resulting consensus. */
|
2017-03-07 19:15:43 +01:00
|
|
|
/* Skip the first two lines. */
|
|
|
|
cons2 = apply_ed_diff(cons1, diff, 2);
|
2017-03-07 15:58:30 +01:00
|
|
|
|
|
|
|
/* ed diff could not be applied - reason already logged by apply_ed_diff. */
|
|
|
|
if (!cons2) {
|
|
|
|
goto error_cleanup;
|
|
|
|
}
|
|
|
|
|
2017-03-07 19:45:32 +01:00
|
|
|
cons2_str = consensus_join_lines(cons2);
|
2017-03-07 15:58:30 +01:00
|
|
|
|
2017-03-07 19:02:00 +01:00
|
|
|
consensus_digest_t cons2_digests;
|
|
|
|
if (consensus_compute_digest(cons2_str, &cons2_digests) < 0) {
|
|
|
|
/* LCOV_EXCL_START -- digest can't fail */
|
2017-03-07 15:58:30 +01:00
|
|
|
log_warn(LD_CONSDIFF, "Could not compute digests of the consensus "
|
|
|
|
"resulting from applying a consensus diff.");
|
|
|
|
goto error_cleanup;
|
2017-03-07 19:02:00 +01:00
|
|
|
/* LCOV_EXCL_STOP */
|
2017-03-07 15:58:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* See that the resulting consensus matches its hash. */
|
2017-03-07 20:12:53 +01:00
|
|
|
if (!consensus_digest_eq(cons2_digests.sha3_256,
|
|
|
|
(const uint8_t*)e_cons2_hash)) {
|
2017-03-07 15:58:30 +01:00
|
|
|
log_warn(LD_CONSDIFF, "Refusing to apply consensus diff because "
|
2017-03-07 17:36:07 +01:00
|
|
|
"the resulting consensus doesn't match the digest as found in "
|
2017-03-07 15:58:30 +01:00
|
|
|
"the consensus diff header.");
|
|
|
|
char hex_digest2[HEX_DIGEST256_LEN+1];
|
|
|
|
char e_hex_digest2[HEX_DIGEST256_LEN+1];
|
|
|
|
base16_encode(hex_digest2, HEX_DIGEST256_LEN+1,
|
2017-03-07 19:02:00 +01:00
|
|
|
(const char *)cons2_digests.sha3_256, DIGEST256_LEN);
|
2017-03-07 15:58:30 +01:00
|
|
|
base16_encode(e_hex_digest2, HEX_DIGEST256_LEN+1,
|
|
|
|
e_cons2_hash, DIGEST256_LEN);
|
2017-03-07 16:48:03 +01:00
|
|
|
log_warn(LD_CONSDIFF, "Expected: %s; found: %s",
|
2017-03-07 15:58:30 +01:00
|
|
|
hex_digest2, e_hex_digest2);
|
|
|
|
goto error_cleanup;
|
|
|
|
}
|
|
|
|
|
2017-03-07 17:35:50 +01:00
|
|
|
goto done;
|
2017-03-07 15:58:30 +01:00
|
|
|
|
2017-03-07 17:35:50 +01:00
|
|
|
error_cleanup:
|
|
|
|
tor_free(cons2_str); /* Sets it to NULL */
|
2017-03-07 15:58:30 +01:00
|
|
|
|
2017-03-07 17:35:50 +01:00
|
|
|
done:
|
2017-03-07 15:58:30 +01:00
|
|
|
if (cons2) {
|
|
|
|
smartlist_free(cons2);
|
|
|
|
}
|
|
|
|
|
2017-03-07 17:35:50 +01:00
|
|
|
return cons2_str;
|
2017-03-07 15:58:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-08 00:15:25 +01:00
|
|
|
/**
|
2017-03-14 20:00:39 +01:00
|
|
|
* Helper: For every NL-terminated line in <b>s</b>, add a cdline referring to
|
|
|
|
* that line (without trailing newline) to <b>out</b>. Return -1 if there are
|
|
|
|
* any non-NL terminated lines; 0 otherwise.
|
2017-03-08 00:43:49 +01:00
|
|
|
*
|
|
|
|
* Unlike tor_split_lines, this function avoids ambiguity on its
|
2017-03-08 00:15:25 +01:00
|
|
|
* handling of a final line that isn't NL-terminated.
|
2017-03-14 20:00:39 +01:00
|
|
|
*
|
|
|
|
* All cdline_t objects are allocated in the provided memarea. Strings
|
|
|
|
* are not copied: if <b>s</b> changes or becomes invalid, then all
|
|
|
|
* generated cdlines will become invalid.
|
2017-03-08 00:15:25 +01:00
|
|
|
*/
|
2017-03-14 20:00:39 +01:00
|
|
|
STATIC int
|
|
|
|
consensus_split_lines(smartlist_t *out, const char *s, memarea_t *area)
|
2017-03-07 19:45:32 +01:00
|
|
|
{
|
|
|
|
while (*s) {
|
2017-03-14 20:00:39 +01:00
|
|
|
const char *eol = strchr(s, '\n');
|
2017-03-07 19:45:32 +01:00
|
|
|
if (!eol) {
|
|
|
|
/* File doesn't end with newline. */
|
|
|
|
return -1;
|
|
|
|
}
|
2017-03-14 20:00:39 +01:00
|
|
|
if (eol - s > UINT32_MAX) {
|
|
|
|
/* Line is far too long. */
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
cdline_t *line = memarea_alloc(area, sizeof(cdline_t));
|
|
|
|
line->s = s;
|
|
|
|
line->len = (uint32_t)(eol - s);
|
|
|
|
smartlist_add(out, line);
|
2017-03-07 19:45:32 +01:00
|
|
|
s = eol+1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-03-14 20:00:39 +01:00
|
|
|
/** Given a list of cdline_t, return a newly allocated string containing
|
2017-03-08 00:15:25 +01:00
|
|
|
* all of the lines, terminated with NL, concatenated.
|
|
|
|
*
|
|
|
|
* Unlike smartlist_join_strings(), avoids lossy operations on empty
|
|
|
|
* lists. */
|
2017-03-07 19:45:32 +01:00
|
|
|
static char *
|
|
|
|
consensus_join_lines(const smartlist_t *inp)
|
|
|
|
{
|
|
|
|
size_t n = 0;
|
2017-03-14 20:00:39 +01:00
|
|
|
SMARTLIST_FOREACH(inp, const cdline_t *, cdline, n += cdline->len + 1);
|
2017-03-07 19:45:32 +01:00
|
|
|
n += 1;
|
|
|
|
char *result = tor_malloc(n);
|
|
|
|
char *out = result;
|
2017-03-14 20:00:39 +01:00
|
|
|
SMARTLIST_FOREACH_BEGIN(inp, const cdline_t *, cdline) {
|
|
|
|
memcpy(out, cdline->s, cdline->len);
|
|
|
|
out += cdline->len;
|
2017-03-07 19:45:32 +01:00
|
|
|
*out++ = '\n';
|
2017-03-14 20:00:39 +01:00
|
|
|
} SMARTLIST_FOREACH_END(cdline);
|
2017-03-07 19:45:32 +01:00
|
|
|
*out++ = '\0';
|
|
|
|
tor_assert(out == result+n);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-03-08 00:15:25 +01:00
|
|
|
/** Given two consensus documents, try to compute a diff between them. On
|
|
|
|
* success, retun a newly allocated string containing that diff. On failure,
|
|
|
|
* return NULL. */
|
2017-03-07 19:45:32 +01:00
|
|
|
char *
|
|
|
|
consensus_diff_generate(const char *cons1,
|
|
|
|
const char *cons2)
|
|
|
|
{
|
|
|
|
consensus_digest_t d1, d2;
|
|
|
|
smartlist_t *lines1 = NULL, *lines2 = NULL, *result_lines = NULL;
|
|
|
|
int r1, r2;
|
|
|
|
char *result = NULL;
|
|
|
|
|
|
|
|
r1 = consensus_compute_digest(cons1, &d1);
|
|
|
|
r2 = consensus_compute_digest(cons2, &d2);
|
|
|
|
if (BUG(r1 < 0 || r2 < 0))
|
|
|
|
return NULL; // LCOV_EXCL_LINE
|
|
|
|
|
2017-03-14 20:00:39 +01:00
|
|
|
memarea_t *area = memarea_new();
|
2017-03-07 19:45:32 +01:00
|
|
|
lines1 = smartlist_new();
|
|
|
|
lines2 = smartlist_new();
|
2017-03-14 20:00:39 +01:00
|
|
|
if (consensus_split_lines(lines1, cons1, area) < 0)
|
2017-03-07 19:45:32 +01:00
|
|
|
goto done;
|
2017-03-14 20:00:39 +01:00
|
|
|
if (consensus_split_lines(lines2, cons2, area) < 0)
|
2017-03-07 19:45:32 +01:00
|
|
|
goto done;
|
|
|
|
|
2017-03-14 20:00:39 +01:00
|
|
|
result_lines = consdiff_gen_diff(lines1, lines2, &d1, &d2, area);
|
2017-03-07 19:45:32 +01:00
|
|
|
|
|
|
|
done:
|
|
|
|
if (result_lines) {
|
|
|
|
result = consensus_join_lines(result_lines);
|
|
|
|
smartlist_free(result_lines);
|
|
|
|
}
|
2017-03-14 20:00:39 +01:00
|
|
|
|
|
|
|
memarea_drop_all(area);
|
|
|
|
smartlist_free(lines1);
|
|
|
|
smartlist_free(lines2);
|
|
|
|
|
2017-03-07 19:45:32 +01:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-03-08 00:15:25 +01:00
|
|
|
/** Given a consensus document and a diff, try to apply the diff to the
|
|
|
|
* consensus. On success return a newly allocated string containing the new
|
|
|
|
* consensus. On failure, return NULL. */
|
2017-03-07 19:45:32 +01:00
|
|
|
char *
|
|
|
|
consensus_diff_apply(const char *consensus,
|
|
|
|
const char *diff)
|
|
|
|
{
|
|
|
|
consensus_digest_t d1;
|
|
|
|
smartlist_t *lines1 = NULL, *lines2 = NULL;
|
|
|
|
int r1;
|
|
|
|
char *result = NULL;
|
2017-03-14 20:00:39 +01:00
|
|
|
memarea_t *area = memarea_new();
|
2017-03-07 19:45:32 +01:00
|
|
|
|
|
|
|
r1 = consensus_compute_digest(consensus, &d1);
|
|
|
|
if (BUG(r1 < 0))
|
|
|
|
return NULL; // LCOV_EXCL_LINE
|
|
|
|
|
|
|
|
lines1 = smartlist_new();
|
|
|
|
lines2 = smartlist_new();
|
2017-03-14 20:00:39 +01:00
|
|
|
if (consensus_split_lines(lines1, consensus, area) < 0)
|
2017-03-07 19:45:32 +01:00
|
|
|
goto done;
|
2017-03-14 20:00:39 +01:00
|
|
|
if (consensus_split_lines(lines2, diff, area) < 0)
|
2017-03-07 19:45:32 +01:00
|
|
|
goto done;
|
|
|
|
|
|
|
|
result = consdiff_apply_diff(lines1, lines2, &d1);
|
|
|
|
|
|
|
|
done:
|
|
|
|
smartlist_free(lines1);
|
|
|
|
smartlist_free(lines2);
|
2017-03-14 20:00:39 +01:00
|
|
|
memarea_drop_all(area);
|
2017-03-07 19:45:32 +01:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|