Merge branch 'collin' into collin+newconf+sdl
[grub2/phcoder.git] / lib / hexdump.c
blobc69cb093b7da66829d85a5d5f02cd5052c818ae1
1 /* hexdump.c - hexdump function */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2008 Free Software Foundation, Inc.
6 * GRUB is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * GRUB 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 GRUB. If not, see <http://www.gnu.org/licenses/>.
20 #include <grub/types.h>
21 #include <grub/misc.h>
22 #include <grub/lib/hexdump.h>
24 void
25 hexdump (unsigned long bse, char *buf, int len)
27 int pos;
28 char line[80];
30 while (len > 0)
32 int cnt, i;
34 pos = grub_sprintf (line, "%08lx ", bse);
35 cnt = 16;
36 if (cnt > len)
37 cnt = len;
39 for (i = 0; i < cnt; i++)
41 pos += grub_sprintf (&line[pos], "%02x ", (unsigned char) buf[i]);
42 if ((i & 7) == 7)
43 line[pos++] = ' ';
46 for (; i < 16; i++)
48 pos += grub_sprintf (&line[pos], " ");
49 if ((i & 7) == 7)
50 line[pos++] = ' ';
53 line[pos++] = '|';
55 for (i = 0; i < cnt; i++)
56 line[pos++] = ((buf[i] >= 32) && (buf[i] < 127)) ? buf[i] : '.';
58 line[pos++] = '|';
60 line[pos] = 0;
62 grub_printf ("%s\n", line);
64 /* Print only first and last line if more than 3 lines are identical. */
65 if (len >= 4 * 16
66 && ! grub_memcmp (buf, buf + 1 * 16, 16)
67 && ! grub_memcmp (buf, buf + 2 * 16, 16)
68 && ! grub_memcmp (buf, buf + 3 * 16, 16))
70 grub_printf ("*\n");
73 bse += 16;
74 buf += 16;
75 len -= 16;
77 while (len >= 3 * 16 && ! grub_memcmp (buf, buf + 2 * 16, 16));
80 bse += 16;
81 buf += 16;
82 len -= cnt;