"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 2 | /* |
| 3 | * hexdump implementation for busybox |
| 4 | * Based on code from util-linux v 2.11l |
| 5 | * |
| 6 | * Copyright (c) 1989 |
| 7 | * The Regents of the University of California. All rights reserved. |
| 8 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame^] | 9 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 10 | */ |
| 11 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 12 | #include "libbb.h" |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 13 | #include "dump.h" |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 14 | |
Denis Vlasenko | 99912ca | 2007-04-10 15:43:37 +0000 | [diff] [blame] | 15 | /* This is a NOEXEC applet. Be very careful! */ |
| 16 | |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame] | 17 | static void bb_dump_addfile(dumper_t *dumper, char *name) |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 18 | { |
Denis Vlasenko | 084266e | 2008-07-26 23:08:31 +0000 | [diff] [blame] | 19 | char *p; |
| 20 | FILE *fp; |
| 21 | char *buf; |
| 22 | |
| 23 | fp = xfopen_for_read(name); |
| 24 | while ((buf = xmalloc_fgetline(fp)) != NULL) { |
| 25 | p = skip_whitespace(buf); |
| 26 | if (*p && (*p != '#')) { |
| 27 | bb_dump_add(dumper, p); |
| 28 | } |
| 29 | free(buf); |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 30 | } |
Denis Vlasenko | 084266e | 2008-07-26 23:08:31 +0000 | [diff] [blame] | 31 | fclose(fp); |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 32 | } |
| 33 | |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 34 | static const char *const add_strings[] = { |
Denis Vlasenko | 2dbeaa9 | 2006-09-23 13:31:46 +0000 | [diff] [blame] | 35 | "\"%07.7_ax \" 16/1 \"%03o \" \"\\n\"", /* b */ |
| 36 | "\"%07.7_ax \" 16/1 \"%3_c \" \"\\n\"", /* c */ |
| 37 | "\"%07.7_ax \" 8/2 \" %05u \" \"\\n\"", /* d */ |
| 38 | "\"%07.7_ax \" 8/2 \" %06o \" \"\\n\"", /* o */ |
| 39 | "\"%07.7_ax \" 8/2 \" %04x \" \"\\n\"", /* x */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 40 | }; |
| 41 | |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 42 | static const char add_first[] ALIGN1 = "\"%07.7_Ax\n\""; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 43 | |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 44 | static const char hexdump_opts[] ALIGN1 = "bcdoxCe:f:n:s:v" IF_FEATURE_HEXDUMP_REVERSE("R"); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 45 | |
| 46 | static const struct suffix_mult suffixes[] = { |
Denis Vlasenko | f868963 | 2007-07-27 15:06:25 +0000 | [diff] [blame] | 47 | { "b", 512 }, |
| 48 | { "k", 1024 }, |
| 49 | { "m", 1024*1024 }, |
Denys Vlasenko | 043b1e5 | 2009-09-06 12:47:55 +0200 | [diff] [blame] | 50 | { "", 0 } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 51 | }; |
| 52 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 53 | int hexdump_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 54 | int hexdump_main(int argc, char **argv) |
| 55 | { |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame] | 56 | dumper_t *dumper = alloc_dumper(); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 57 | const char *p; |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 58 | int ch; |
Denis Vlasenko | fbe5f39 | 2007-11-18 05:36:50 +0000 | [diff] [blame] | 59 | #if ENABLE_FEATURE_HEXDUMP_REVERSE |
| 60 | FILE *fp; |
| 61 | smallint rdump = 0; |
| 62 | #endif |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 63 | |
Denis Vlasenko | fbe5f39 | 2007-11-18 05:36:50 +0000 | [diff] [blame] | 64 | if (ENABLE_HD && !applet_name[2]) { /* we are "hd" */ |
| 65 | ch = 'C'; |
| 66 | goto hd_applet; |
| 67 | } |
| 68 | |
| 69 | /* We cannot use getopt32: in hexdump options are cumulative. |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 70 | * E.g. "hexdump -C -C file" should dump each line twice */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 71 | while ((ch = getopt(argc, argv, hexdump_opts)) > 0) { |
Denis Vlasenko | 4c196a8 | 2006-09-23 15:53:01 +0000 | [diff] [blame] | 72 | p = strchr(hexdump_opts, ch); |
Denis Vlasenko | 2dbeaa9 | 2006-09-23 13:31:46 +0000 | [diff] [blame] | 73 | if (!p) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 74 | bb_show_usage(); |
Denis Vlasenko | 2dbeaa9 | 2006-09-23 13:31:46 +0000 | [diff] [blame] | 75 | if ((p - hexdump_opts) < 5) { |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame] | 76 | bb_dump_add(dumper, add_first); |
| 77 | bb_dump_add(dumper, add_strings[(int)(p - hexdump_opts)]); |
Denis Vlasenko | fbe5f39 | 2007-11-18 05:36:50 +0000 | [diff] [blame] | 78 | } |
| 79 | /* Save a little bit of space below by omitting the 'else's. */ |
| 80 | if (ch == 'C') { |
| 81 | hd_applet: |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame] | 82 | bb_dump_add(dumper, "\"%08.8_Ax\n\""); |
| 83 | bb_dump_add(dumper, "\"%08.8_ax \" 8/1 \"%02x \" \" \" 8/1 \"%02x \" "); |
| 84 | bb_dump_add(dumper, "\" |\" 16/1 \"%_p\" \"|\\n\""); |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 85 | } |
Denis Vlasenko | fbe5f39 | 2007-11-18 05:36:50 +0000 | [diff] [blame] | 86 | if (ch == 'e') { |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame] | 87 | bb_dump_add(dumper, optarg); |
Denis Vlasenko | fbe5f39 | 2007-11-18 05:36:50 +0000 | [diff] [blame] | 88 | } /* else */ |
| 89 | if (ch == 'f') { |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame] | 90 | bb_dump_addfile(dumper, optarg); |
Denis Vlasenko | fbe5f39 | 2007-11-18 05:36:50 +0000 | [diff] [blame] | 91 | } /* else */ |
| 92 | if (ch == 'n') { |
Denys Vlasenko | 7783248 | 2010-08-12 14:14:45 +0200 | [diff] [blame] | 93 | dumper->dump_length = xatoi_positive(optarg); |
Denis Vlasenko | fbe5f39 | 2007-11-18 05:36:50 +0000 | [diff] [blame] | 94 | } /* else */ |
Denys Vlasenko | 1854bc1 | 2010-04-06 23:33:28 +0200 | [diff] [blame] | 95 | if (ch == 's') { /* compat: -s accepts hex numbers too */ |
| 96 | dumper->dump_skip = xstrtoul_range_sfx(optarg, /*base:*/ 0, /*lo:*/ 0, /*hi:*/ LONG_MAX, suffixes); |
Denis Vlasenko | fbe5f39 | 2007-11-18 05:36:50 +0000 | [diff] [blame] | 97 | } /* else */ |
| 98 | if (ch == 'v') { |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame] | 99 | dumper->dump_vflag = ALL; |
Denis Vlasenko | fbe5f39 | 2007-11-18 05:36:50 +0000 | [diff] [blame] | 100 | } |
| 101 | #if ENABLE_FEATURE_HEXDUMP_REVERSE |
| 102 | if (ch == 'R') { |
| 103 | rdump = 1; |
| 104 | } |
| 105 | #endif |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame] | 108 | if (!dumper->fshead) { |
| 109 | bb_dump_add(dumper, add_first); |
| 110 | bb_dump_add(dumper, "\"%07.7_ax \" 8/2 \"%04x \" \"\\n\""); |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | argv += optind; |
| 114 | |
Denis Vlasenko | fbe5f39 | 2007-11-18 05:36:50 +0000 | [diff] [blame] | 115 | #if !ENABLE_FEATURE_HEXDUMP_REVERSE |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame] | 116 | return bb_dump_dump(dumper, argv); |
Denis Vlasenko | fbe5f39 | 2007-11-18 05:36:50 +0000 | [diff] [blame] | 117 | #else |
| 118 | if (!rdump) { |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame] | 119 | return bb_dump_dump(dumper, argv); |
Denis Vlasenko | fbe5f39 | 2007-11-18 05:36:50 +0000 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | /* -R: reverse of 'hexdump -Cv' */ |
| 123 | fp = stdin; |
| 124 | if (!*argv) { |
| 125 | argv--; |
| 126 | goto jump_in; |
| 127 | } |
| 128 | |
| 129 | do { |
| 130 | char *buf; |
Denis Vlasenko | 5415c85 | 2008-07-21 23:05:26 +0000 | [diff] [blame] | 131 | fp = xfopen_for_read(*argv); |
Denis Vlasenko | fbe5f39 | 2007-11-18 05:36:50 +0000 | [diff] [blame] | 132 | jump_in: |
Denis Vlasenko | 8ee649a | 2008-03-26 20:04:27 +0000 | [diff] [blame] | 133 | while ((buf = xmalloc_fgetline(fp)) != NULL) { |
Denis Vlasenko | fbe5f39 | 2007-11-18 05:36:50 +0000 | [diff] [blame] | 134 | p = buf; |
| 135 | while (1) { |
| 136 | /* skip address or previous byte */ |
| 137 | while (isxdigit(*p)) p++; |
| 138 | while (*p == ' ') p++; |
| 139 | /* '|' char will break the line */ |
| 140 | if (!isxdigit(*p) || sscanf(p, "%x ", &ch) != 1) |
| 141 | break; |
| 142 | putchar(ch); |
| 143 | } |
| 144 | free(buf); |
| 145 | } |
| 146 | fclose(fp); |
| 147 | } while (*++argv); |
| 148 | |
| 149 | fflush_stdout_and_exit(EXIT_SUCCESS); |
| 150 | #endif |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 151 | } |