2 * cryptsetup file differ check (rewritten Clemens' fileDiffer in Python)
4 * Copyright (C) 2010 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 * version 2 as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
36 enum df { OK , FAIL };
38 static void print_diff(off_t from, int max,
39 const unsigned char *o,
40 const unsigned char *n)
48 for (i = 0; i < len; i++)
49 printf(" %02x", o[from + i]);
50 printf("%s\n ", max != len ? " ..." : "");
51 for (i = 0; i < len; i++)
52 printf(" %2c", o[from + i] > ' ' ? o[from + i]: '.');
54 for (i = 0; i < len; i++)
55 printf(" %02x", n[from + i]);
56 printf("%s\n ", max != len ? " ..." : "");
57 for (i = 0; i < len; i++)
58 printf(" %2c", n[from + i] > ' ' ? n[from + i]: '.');
63 * Xfrom-to (e.g. R10-15)
65 * S - change required, semantic
66 * R - change required, random
67 * F - change forbidden
69 static enum df check(const char *range, unsigned char *o, unsigned char *n)
72 unsigned long long from, to;
75 if (sscanf(range, "%c%llu-%llu", &strict, &from, &to) != 3) {
76 printf("Unknown range format %s.\n", range);
80 switch (toupper(strict)) {
85 ret = memcmp(&o[from], &n[from], to - from + 1) != 0 ? OK : FAIL;
87 case 'R': /* FIXME - random test */
88 ret = memcmp(&o[from], &n[from], to - from + 1) != 0 ? OK : FAIL;
91 ret = memcmp(&o[from], &n[from], to - from + 1) == 0 ? OK : FAIL;
99 print_diff(from, to - from + 1, o, n);
104 static int open_mmap(struct ffile *f)
108 f->fd = open(f->name, O_RDONLY);
109 if (f->fd == -1 || fstat(f->fd, &st) == -1)
112 f->size = st.st_size;
113 f->addr = mmap(NULL, f->size, PROT_READ, MAP_PRIVATE, f->fd, 0);
115 return (f->addr == MAP_FAILED) ? 0 : 1;
118 static void close_mmap(struct ffile *f)
120 if (f->addr != MAP_FAILED && !munmap(f->addr, f->size))
121 f->addr = MAP_FAILED;
123 if (f->fd != -1 && !close(f->fd))
127 int main(int argc, char *argv[])
130 struct ffile file_old = {
134 struct ffile file_new = {
140 printf("Use: differ old_file new_file change_list.\n");
144 file_old.name = argv[1];
145 if (!open_mmap(&file_old))
148 file_new.name = argv[2];
149 if (!open_mmap(&file_new))
152 for (i = 3; i < argc; i++)
153 if (check(argv[i], file_old.addr, file_new.addr) == FAIL) {
154 printf ("FAILED for %s\n", argv[i]);
161 close_mmap(&file_new);
162 close_mmap(&file_old);