daily pastebin goal
70%
SHARE
TWEET

Untitled

a guest Aug 12th, 2017 368 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. int
  2. main(int argc, char *argv[])
  3. {
  4.     off_t size;
  5.     uint32_t tmp;
  6.     uint8_t *buf, crc[4], crc128k[4], *rgb;
  7.     int fd, ofd, rgbfd;
  8.  
  9.     if (argc != 4) {
  10.         fprintf(stderr, "usage: %s lk.rom rgb565le.raw lknew.rom\n",
  11.             argv[0]);
  12.         exit(EXIT_FAILURE);
  13.     }
  14.  
  15.     memset(crc, 0, sizeof(crc));
  16.     memset(crc128k, 0, sizeof(crc128k));
  17.  
  18.     if ((ofd = open(argv[3], O_RDWR | O_CREAT | O_TRUNC, 0666)) == -1)
  19.         err(EXIT_FAILURE, "%s", argv[3]);
  20.  
  21.     if ((fd = open(argv[1], O_RDONLY)) == -1)
  22.         err(EXIT_FAILURE, "%s", argv[1]);
  23.     if ((size = lseek(fd, 0, SEEK_END)) == -1)
  24.         err(EXIT_FAILURE, "%s", argv[1]);
  25.     if ((buf = mmap(NULL, size, PROT_READ, MAP_SHARED | MAP_FILE, fd, 0))
  26.         == MAP_FAILED)
  27.         err(EXIT_FAILURE, "%s", argv[1]);
  28.  
  29.     if ((rgbfd = open(argv[2], O_RDONLY)) == -1)
  30.         err(EXIT_FAILURE, "%s", argv[2]);
  31.     if ((rgb = mmap(NULL, 768000, PROT_READ, MAP_SHARED | MAP_FILE,
  32.         rgbfd, 0)) == MAP_FAILED)
  33.         err(EXIT_FAILURE, "%s", argv[2]);
  34.  
  35.     size -= 774716;
  36.  
  37.     tmp = 0;
  38.     TCCCRC(tmp, buf, 16);
  39.     TCCCRC(tmp, crc, 4);
  40.     TCCCRC(tmp, buf + 28, size - 28);
  41.     TCCCRC(tmp, rgb, 768000);
  42.     TCCCRC(tmp, buf + size + 768000, 774716 - 768000);
  43.     crc[0] = (tmp >> 0) & 0xff;
  44.     crc[1] = (tmp >> 8) & 0xff;
  45.     crc[2] = (tmp >> 16) & 0xff;
  46.     crc[3] = (tmp >> 24) & 0xff;
  47.  
  48.     tmp = 0;
  49.     TCCCRC(tmp, buf, 16);
  50.     TCCCRC(tmp, crc, 4);
  51.     TCCCRC(tmp, buf + 28, 131072 - 28);
  52.     crc128k[0] = (tmp >> 0) & 0xff;
  53.     crc128k[1] = (tmp >> 8) & 0xff;
  54.     crc128k[2] = (tmp >> 16) & 0xff;
  55.     crc128k[3] = (tmp >> 24) & 0xff;
  56.  
  57.     tmp = 0;
  58.  
  59.     write(ofd, buf, 16);
  60.     write(ofd, crc128k, 4);
  61.     write(ofd, &tmp, 4);
  62.     write(ofd, crc, 4);
  63.     write(ofd, buf + 28, size - 28);
  64.     write(ofd, rgb, 768000);
  65.     write(ofd, buf + size + 768000, 774716 - 768000);
  66.  
  67.     size += 774716;
  68.  
  69.     munmap(rgb, 768000);
  70.     close(rgbfd);
  71.     munmap(buf, size);
  72.     close(fd);
  73.     close(ofd);
  74.  
  75.     return EXIT_SUCCESS;
  76. }
RAW Paste Data
Top