"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 | * |
Rob Landley | ea224be | 2006-06-18 20:20:07 +0000 | [diff] [blame] | 9 | * Licensed under GPLv2 or later, see file License in this tarball for details. |
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 | |
| 17 | |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame^] | 18 | static void bb_dump_addfile(dumper_t *dumper, char *name) |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 19 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 20 | char *p; |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 21 | FILE *fp; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 22 | char *buf; |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 23 | |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 24 | fp = xfopen(name, "r"); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 25 | |
Denis Vlasenko | 8ee649a | 2008-03-26 20:04:27 +0000 | [diff] [blame] | 26 | while ((buf = xmalloc_fgetline(fp)) != NULL) { |
Rob Landley | ea224be | 2006-06-18 20:20:07 +0000 | [diff] [blame] | 27 | p = skip_whitespace(buf); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 28 | |
| 29 | if (*p && (*p != '#')) { |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame^] | 30 | bb_dump_add(dumper, p); |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 31 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 32 | free(buf); |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 33 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 34 | fclose(fp); |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 35 | } |
| 36 | |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 37 | static const char *const add_strings[] = { |
Denis Vlasenko | 2dbeaa9 | 2006-09-23 13:31:46 +0000 | [diff] [blame] | 38 | "\"%07.7_ax \" 16/1 \"%03o \" \"\\n\"", /* b */ |
| 39 | "\"%07.7_ax \" 16/1 \"%3_c \" \"\\n\"", /* c */ |
| 40 | "\"%07.7_ax \" 8/2 \" %05u \" \"\\n\"", /* d */ |
| 41 | "\"%07.7_ax \" 8/2 \" %06o \" \"\\n\"", /* o */ |
| 42 | "\"%07.7_ax \" 8/2 \" %04x \" \"\\n\"", /* x */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 43 | }; |
| 44 | |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 45 | static const char add_first[] ALIGN1 = "\"%07.7_Ax\n\""; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 46 | |
Denis Vlasenko | fbe5f39 | 2007-11-18 05:36:50 +0000 | [diff] [blame] | 47 | static const char hexdump_opts[] ALIGN1 = "bcdoxCe:f:n:s:v" USE_FEATURE_HEXDUMP_REVERSE("R"); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 48 | |
| 49 | static const struct suffix_mult suffixes[] = { |
Denis Vlasenko | f868963 | 2007-07-27 15:06:25 +0000 | [diff] [blame] | 50 | { "b", 512 }, |
| 51 | { "k", 1024 }, |
| 52 | { "m", 1024*1024 }, |
| 53 | { } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 54 | }; |
| 55 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 56 | int hexdump_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 57 | int hexdump_main(int argc, char **argv) |
| 58 | { |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame^] | 59 | dumper_t *dumper = alloc_dumper(); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 60 | const char *p; |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 61 | int ch; |
Denis Vlasenko | fbe5f39 | 2007-11-18 05:36:50 +0000 | [diff] [blame] | 62 | #if ENABLE_FEATURE_HEXDUMP_REVERSE |
| 63 | FILE *fp; |
| 64 | smallint rdump = 0; |
| 65 | #endif |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 66 | |
Denis Vlasenko | fbe5f39 | 2007-11-18 05:36:50 +0000 | [diff] [blame] | 67 | if (ENABLE_HD && !applet_name[2]) { /* we are "hd" */ |
| 68 | ch = 'C'; |
| 69 | goto hd_applet; |
| 70 | } |
| 71 | |
| 72 | /* We cannot use getopt32: in hexdump options are cumulative. |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 73 | * E.g. "hexdump -C -C file" should dump each line twice */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 74 | while ((ch = getopt(argc, argv, hexdump_opts)) > 0) { |
Denis Vlasenko | 4c196a8 | 2006-09-23 15:53:01 +0000 | [diff] [blame] | 75 | p = strchr(hexdump_opts, ch); |
Denis Vlasenko | 2dbeaa9 | 2006-09-23 13:31:46 +0000 | [diff] [blame] | 76 | if (!p) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 77 | bb_show_usage(); |
Denis Vlasenko | 2dbeaa9 | 2006-09-23 13:31:46 +0000 | [diff] [blame] | 78 | if ((p - hexdump_opts) < 5) { |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame^] | 79 | bb_dump_add(dumper, add_first); |
| 80 | bb_dump_add(dumper, add_strings[(int)(p - hexdump_opts)]); |
Denis Vlasenko | fbe5f39 | 2007-11-18 05:36:50 +0000 | [diff] [blame] | 81 | } |
| 82 | /* Save a little bit of space below by omitting the 'else's. */ |
| 83 | if (ch == 'C') { |
| 84 | hd_applet: |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame^] | 85 | bb_dump_add(dumper, "\"%08.8_Ax\n\""); |
| 86 | bb_dump_add(dumper, "\"%08.8_ax \" 8/1 \"%02x \" \" \" 8/1 \"%02x \" "); |
| 87 | bb_dump_add(dumper, "\" |\" 16/1 \"%_p\" \"|\\n\""); |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 88 | } |
Denis Vlasenko | fbe5f39 | 2007-11-18 05:36:50 +0000 | [diff] [blame] | 89 | if (ch == 'e') { |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame^] | 90 | bb_dump_add(dumper, optarg); |
Denis Vlasenko | fbe5f39 | 2007-11-18 05:36:50 +0000 | [diff] [blame] | 91 | } /* else */ |
| 92 | if (ch == 'f') { |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame^] | 93 | bb_dump_addfile(dumper, optarg); |
Denis Vlasenko | fbe5f39 | 2007-11-18 05:36:50 +0000 | [diff] [blame] | 94 | } /* else */ |
| 95 | if (ch == 'n') { |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame^] | 96 | dumper->dump_length = xatoi_u(optarg); |
Denis Vlasenko | fbe5f39 | 2007-11-18 05:36:50 +0000 | [diff] [blame] | 97 | } /* else */ |
| 98 | if (ch == 's') { |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame^] | 99 | dumper->dump_skip = xatoul_range_sfx(optarg, 0, LONG_MAX, suffixes); |
Denis Vlasenko | fbe5f39 | 2007-11-18 05:36:50 +0000 | [diff] [blame] | 100 | } /* else */ |
| 101 | if (ch == 'v') { |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame^] | 102 | dumper->dump_vflag = ALL; |
Denis Vlasenko | fbe5f39 | 2007-11-18 05:36:50 +0000 | [diff] [blame] | 103 | } |
| 104 | #if ENABLE_FEATURE_HEXDUMP_REVERSE |
| 105 | if (ch == 'R') { |
| 106 | rdump = 1; |
| 107 | } |
| 108 | #endif |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame^] | 111 | if (!dumper->fshead) { |
| 112 | bb_dump_add(dumper, add_first); |
| 113 | bb_dump_add(dumper, "\"%07.7_ax \" 8/2 \"%04x \" \"\\n\""); |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | argv += optind; |
| 117 | |
Denis Vlasenko | fbe5f39 | 2007-11-18 05:36:50 +0000 | [diff] [blame] | 118 | #if !ENABLE_FEATURE_HEXDUMP_REVERSE |
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 | #else |
| 121 | if (!rdump) { |
Denis Vlasenko | 55f7912 | 2008-07-16 11:00:16 +0000 | [diff] [blame^] | 122 | return bb_dump_dump(dumper, argv); |
Denis Vlasenko | fbe5f39 | 2007-11-18 05:36:50 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | /* -R: reverse of 'hexdump -Cv' */ |
| 126 | fp = stdin; |
| 127 | if (!*argv) { |
| 128 | argv--; |
| 129 | goto jump_in; |
| 130 | } |
| 131 | |
| 132 | do { |
| 133 | char *buf; |
| 134 | fp = xfopen(*argv, "r"); |
| 135 | jump_in: |
Denis Vlasenko | 8ee649a | 2008-03-26 20:04:27 +0000 | [diff] [blame] | 136 | while ((buf = xmalloc_fgetline(fp)) != NULL) { |
Denis Vlasenko | fbe5f39 | 2007-11-18 05:36:50 +0000 | [diff] [blame] | 137 | p = buf; |
| 138 | while (1) { |
| 139 | /* skip address or previous byte */ |
| 140 | while (isxdigit(*p)) p++; |
| 141 | while (*p == ' ') p++; |
| 142 | /* '|' char will break the line */ |
| 143 | if (!isxdigit(*p) || sscanf(p, "%x ", &ch) != 1) |
| 144 | break; |
| 145 | putchar(ch); |
| 146 | } |
| 147 | free(buf); |
| 148 | } |
| 149 | fclose(fp); |
| 150 | } while (*++argv); |
| 151 | |
| 152 | fflush_stdout_and_exit(EXIT_SUCCESS); |
| 153 | #endif |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 154 | } |