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 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /* Copyright (C) 2017 C. McEnroe <june@causal.agency>
  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. size_t blank;
  34. bool ascii;
  35. bool offset;
  36. bool skip;
  37. } options = { 16, 8, 0, true, true, false };
  38. static void dump(FILE *file) {
  39. bool skip = false;
  40. byte buf[options.cols];
  41. size_t offset = 0;
  42. for (
  43. size_t size;
  44. (size = fread(buf, 1, sizeof(buf), file));
  45. offset += size
  46. ) {
  47. if (options.skip) {
  48. if (zero(buf, size)) {
  49. if (!skip) printf("*\n");
  50. skip = true;
  51. continue;
  52. } else {
  53. skip = false;
  54. }
  55. }
  56. if (options.blank) {
  57. if (offset && offset % options.blank == 0) {
  58. printf("\n");
  59. }
  60. }
  61. if (options.offset) {
  62. printf("%08zX: ", offset);
  63. }
  64. for (size_t i = 0; i < sizeof(buf); ++i) {
  65. if (options.group) {
  66. if (i && !(i % options.group)) {
  67. printf(" ");
  68. }
  69. }
  70. if (i < size) {
  71. printf("%02hhX ", buf[i]);
  72. } else {
  73. printf(" ");
  74. }
  75. }
  76. if (options.ascii) {
  77. printf(" ");
  78. for (size_t i = 0; i < size; ++i) {
  79. if (options.group) {
  80. if (i && !(i % options.group)) {
  81. printf(" ");
  82. }
  83. }
  84. printf("%c", isprint(buf[i]) ? buf[i] : '.');
  85. }
  86. }
  87. printf("\n");
  88. }
  89. }
  90. static void undump(FILE *file) {
  91. byte c;
  92. int match;
  93. while (0 < (match = fscanf(file, " %hhx", &c))) {
  94. printf("%c", c);
  95. }
  96. if (!match) errx(EX_DATAERR, "invalid input");
  97. }
  98. int main(int argc, char *argv[]) {
  99. bool reverse = false;
  100. const char *path = NULL;
  101. int opt;
  102. while (0 < (opt = getopt(argc, argv, "ac:g:p:rsz"))) {
  103. switch (opt) {
  104. break; case 'a': options.ascii ^= true;
  105. break; case 'c': options.cols = strtoul(optarg, NULL, 0);
  106. break; case 'g': options.group = strtoul(optarg, NULL, 0);
  107. break; case 'p': options.blank = strtoul(optarg, NULL, 0);
  108. break; case 'r': reverse = true;
  109. break; case 's': options.offset ^= true;
  110. break; case 'z': options.skip ^= true;
  111. break; default: return EX_USAGE;
  112. }
  113. }
  114. if (argc > optind) path = argv[optind];
  115. if (!options.cols) return EX_USAGE;
  116. FILE *file = path ? fopen(path, "r") : stdin;
  117. if (!file) err(EX_NOINPUT, "%s", path);
  118. if (reverse) {
  119. undump(file);
  120. } else {
  121. dump(file);
  122. }
  123. if (ferror(file)) err(EX_IOERR, "%s", path);
  124. return EX_OK;
  125. }