The repository formerly known as dotfiles
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

xx.c 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /* Copyright (c) 2017, Curtis McEnroe <programble@gmail.com>
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU Affero General Public License as published by
  5. * the Free Software Foundation, either version 3 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU Affero General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Affero General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include <ctype.h>
  17. #include <err.h>
  18. #include <stdbool.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <sysexits.h>
  22. #include <unistd.h>
  23. typedef unsigned char byte;
  24. static bool zero(const byte *ptr, size_t size) {
  25. for (size_t i = 0; i < size; ++i) {
  26. if (ptr[i]) return false;
  27. }
  28. return true;
  29. }
  30. static struct {
  31. size_t cols;
  32. size_t group;
  33. bool ascii;
  34. bool offset;
  35. bool skip;
  36. } options = { 16, 8, true, true, false };
  37. static void dump(FILE *file) {
  38. bool skip = false;
  39. byte buf[options.cols];
  40. size_t offset = 0;
  41. for (
  42. size_t size;
  43. (size = fread(buf, 1, sizeof(buf), file));
  44. offset += size
  45. ) {
  46. if (options.skip) {
  47. if (zero(buf, size)) {
  48. if (!skip) printf("*\n");
  49. skip = true;
  50. continue;
  51. } else {
  52. skip = false;
  53. }
  54. }
  55. if (options.offset) {
  56. printf("%08zX: ", offset);
  57. }
  58. for (size_t i = 0; i < sizeof(buf); ++i) {
  59. if (options.group) {
  60. if (i && !(i % options.group)) {
  61. printf(" ");
  62. }
  63. }
  64. if (i < size) {
  65. printf("%02hhX ", buf[i]);
  66. } else {
  67. printf(" ");
  68. }
  69. }
  70. if (options.ascii) {
  71. printf(" ");
  72. for (size_t i = 0; i < size; ++i) {
  73. if (options.group) {
  74. if (i && !(i % options.group)) {
  75. printf(" ");
  76. }
  77. }
  78. printf("%c", isprint(buf[i]) ? buf[i] : '.');
  79. }
  80. }
  81. printf("\n");
  82. }
  83. }
  84. static void undump(FILE *file) {
  85. byte c;
  86. int match;
  87. while (0 < (match = fscanf(file, " %hhx", &c))) {
  88. printf("%c", c);
  89. }
  90. if (!match) errx(EX_DATAERR, "invalid input");
  91. }
  92. int main(int argc, char *argv[]) {
  93. bool reverse = false;
  94. const char *path = NULL;
  95. int opt;
  96. while (0 < (opt = getopt(argc, argv, "ac:g:rsz"))) {
  97. switch (opt) {
  98. break; case 'a': options.ascii ^= true;
  99. break; case 'c': options.cols = strtoul(optarg, NULL, 0);
  100. break; case 'g': options.group = strtoul(optarg, NULL, 0);
  101. break; case 'r': reverse = true;
  102. break; case 's': options.offset ^= true;
  103. break; case 'z': options.skip ^= true;
  104. break; default: return EX_USAGE;
  105. }
  106. }
  107. if (argc > optind) path = argv[optind];
  108. if (!options.cols) return EX_USAGE;
  109. FILE *file = path ? fopen(path, "r") : stdin;
  110. if (!file) err(EX_NOINPUT, "%s", path);
  111. if (reverse) {
  112. undump(file);
  113. } else {
  114. dump(file);
  115. }
  116. if (ferror(file)) err(EX_IOERR, "%s", path);
  117. return EX_OK;
  118. }