2 * cryptsetup file differ check (rewritten Clemens' fileDiffer in Python)
4 * Copyright (C) 2010-2020 Red Hat, Inc. All rights reserved.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
37 enum df { OK , FAIL };
39 static void print_diff(off_t from, int max,
40 const unsigned char *o,
41 const unsigned char *n)
49 for (i = 0; i < len; i++)
50 printf(" %02x", o[from + i]);
51 printf("%s\n ", max != len ? " ..." : "");
52 for (i = 0; i < len; i++)
53 printf(" %2c", o[from + i] > ' ' ? o[from + i]: '.');
55 for (i = 0; i < len; i++)
56 printf(" %02x", n[from + i]);
57 printf("%s\n ", max != len ? " ..." : "");
58 for (i = 0; i < len; i++)
59 printf(" %2c", n[from + i] > ' ' ? n[from + i]: '.');
64 * Xfrom-to (e.g. R10-15)
66 * S - change required, semantic
67 * R - change required, random
68 * F - change forbidden
70 static enum df check(const char *range, unsigned char *o, unsigned char *n)
73 unsigned long long from, to;
76 if (sscanf(range, "%c%llu-%llu", &strict, &from, &to) != 3) {
77 printf("Unknown range format %s.\n", range);
81 switch (toupper(strict)) {
86 ret = memcmp(&o[from], &n[from], to - from + 1) != 0 ? OK : FAIL;
88 case 'R': /* FIXME - random test */
89 ret = memcmp(&o[from], &n[from], to - from + 1) != 0 ? OK : FAIL;
92 ret = memcmp(&o[from], &n[from], to - from + 1) == 0 ? OK : FAIL;
100 print_diff(from, to - from + 1, o, n);
105 static int open_mmap(struct ffile *f)
109 f->fd = open(f->name, O_RDONLY);
110 if (f->fd == -1 || fstat(f->fd, &st) == -1)
113 f->size = st.st_size;
114 f->addr = mmap(NULL, f->size, PROT_READ, MAP_PRIVATE, f->fd, 0);
116 return (f->addr == MAP_FAILED) ? 0 : 1;
119 static void close_mmap(struct ffile *f)
121 if (f->addr != MAP_FAILED && !munmap(f->addr, f->size))
122 f->addr = MAP_FAILED;
124 if (f->fd != -1 && !close(f->fd))
128 int main(int argc, char *argv[])
131 struct ffile file_old = {
135 struct ffile file_new = {
141 printf("Use: differ old_file new_file change_list.\n");
145 file_old.name = argv[1];
146 if (!open_mmap(&file_old))
149 file_new.name = argv[2];
150 if (!open_mmap(&file_new))
153 for (i = 3; i < argc; i++)
154 if (check(argv[i], file_old.addr, file_new.addr) == FAIL) {
155 printf ("FAILED for %s\n", argv[i]);
162 close_mmap(&file_new);
163 close_mmap(&file_old);