⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 che.c

📁 program for changing endianness of firmware dumps
💻 C
字号:
/* * (c) petter wahlman, badeip@binary-art.net * */#include <stdlib.h>#include <stdio.h>#include <string.h>#include <unistd.h>#include <fcntl.h>#include <errno.h>#include <getopt.h>#include <sys/types.h>#include <sys/stat.h>#include <che.h>void print_usage(void){   printf("usage: che [OPTIONS] <source> <dest>\n"          "           --help      this info\n"          "           --type #num where num is one of the following:\n"          "                  0    dword: 0,1,2,3\n"          "                  0    dword: 1,0,3,2\n"          "                  0    dword: 2,3,0,1\n"          "                  0    dword: 3,2,1,0\n"   );   exit(1);}int main(int argc, char **argv){   char *source, *dest;   char *buf;   struct stat st;   unsigned int i;   size_t nr;   u32 type;   int fd, fdd;   int rc = 1;   if (argc < 2) {      print_usage();      return rc;   }   while (1) {      u32 c;      s32 option_index = 0;      static struct option long_options[] = {          { "help",    0, 0, 'h' },           { "type",    1, 0, 't' },           { NULL,      0, 0,  0  }      };        c = getopt_long(argc, argv, "ht:", long_options, &option_index);      if (-1 == c)         break;      switch (c) {         case 'h':            print_usage();            break;         case 't':            type = strtoul(optarg, NULL, 0x0);            break;         default:            print_usage();            break;       }   }   source = argv[optind++];   fd = open(source, O_RDONLY);   if (-1 == fd) {      perror(source);      return rc;   }   fdd = fileno(stdout);   dest = argv[optind++];   if (dest) {      fdd = open(dest, O_WRONLY | O_CREAT, 0644);      if (-1 == fdd) {         perror(dest);         close(fd);         return rc;      }   }   fstat(fd, &st);   buf = malloc(st.st_size);   if (!buf) {      perror("malloc");      return rc;   }   if (isatty(1)) {      printf("source: %s\n", source);      printf("dest:   %s\n", dest ? dest : "stdout");      printf("size:   0x%08lx\n", st.st_size);   }   nr = read(fd, buf, st.st_size);   if (nr != st.st_size) {      fprintf(stderr, "error, incorrect number of bytes read: %s\n", strerror(errno));      return rc;   }   for (i = 0; i < nr; i+=4) {      unsigned int val;      unsigned char b[4];      val = *(unsigned int *) &buf[i];      switch(type) {         case 0:            b[0] = val & 0xff;            b[1] = (val >>  8) & 0xff;            b[2] = (val >> 16) & 0xff;            b[3] = (val >> 24) & 0xff;            break;         case 1:            b[1] = val & 0xff;            b[0] = (val >>  8) & 0xff;            b[3] = (val >> 16) & 0xff;            b[2] = (val >> 24) & 0xff;            break;         case 2:            b[2] = val & 0xff;            b[3] = (val >>  8) & 0xff;            b[0] = (val >> 16) & 0xff;            b[1] = (val >> 24) & 0xff;            break;         case 3:            b[3] = val & 0xff;            b[2] = (val >>  8) & 0xff;            b[1] = (val >> 16) & 0xff;            b[0] = (val >> 24) & 0xff;            break;      }      memcpy(&buf[i], b, sizeof(b));   }   write(fdd, buf, st.st_size);   free(buf);   close(fd);   close(fdd);   return 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -