mmap.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #include <sys/mman.h>
  2. #include <math.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <stdio.h>
  6. #include <sys/types.h>
  7. #include <fcntl.h>
  8. #include <unistd.h>
  9. #include <sys/stat.h>
  10. #include <assert.h>
  11. size_t getFilesize(const char* filename) {
  12. struct stat st;
  13. stat(filename, &st);
  14. return st.st_size;
  15. }
  16. int octToInt(char *current_char, unsigned int size) {
  17. unsigned int output = 0;
  18. while(size > 0){
  19. output = output * 8 + *current_char - '0';
  20. current_char++;
  21. size--;
  22. }
  23. return output;
  24. }
  25. char *findNextHeader(char *start, long int *actualSize) {
  26. char *header = start;
  27. char tmp[13];
  28. long int size;
  29. // printf("%d. %s\n", i, header);
  30. memset(tmp, 0, 13);
  31. strncpy(tmp, header + 124, 12); // size
  32. size = strtol(tmp, 0, 8);
  33. printf("\tbad: %s %ld\n", header, size);
  34. header += 512;
  35. long int next512 = ceil(size/512.0) * 512;
  36. header += next512; // look beyond this
  37. for (;; ++header) {
  38. if (header[0] != '.') continue;
  39. char *magic = header + 257; // ustar
  40. if (magic[0] == 'u' && magic[1] == 's' && magic[2] == 't') break;
  41. }
  42. printf("\tbingo! actual_size: %ld next_file:%s\n", (header - start + 512), header);
  43. if (actualSize) *actualSize = header - start + 512;
  44. return header;
  45. }
  46. void process(void *start, long length, int outFd) {
  47. char *header = (char *) start; // 512
  48. int i = 0;
  49. while (header < ((char *)start+length)) {
  50. char tmp[13];
  51. memset(tmp, 0, 13);
  52. strncpy(tmp, header + 124, 12); // size
  53. long int size = strtol(tmp, 0, 8);
  54. printf("%d. %s %ld\n", i++, header, size);
  55. if (size == 8589934591) { // oops, bad size
  56. printf("\tbad size detected. will find next header\n");
  57. long int actualSize = 0;
  58. header = findNextHeader(header, &actualSize);
  59. printf("\tdd skip=%ld count=%ld if=input.binary of=output.binary iflag=skip_bytes,count_bytes\n", (header - actualSize) - (char *)start, actualSize);
  60. } else {
  61. int next512 = ceil(size/512.0) * 512; // file data with padding size
  62. write(outFd, header, 512+next512);
  63. header += 512 + next512; // header size + the file data size
  64. }
  65. }
  66. printf("done\n");
  67. }
  68. int main(int argc, char** argv) {
  69. size_t filesize = getFilesize(argv[1]);
  70. //Open file
  71. int fd = open(argv[1], O_RDONLY, 0);
  72. assert(fd != -1);
  73. struct stat buf;
  74. fstat(fd, &buf);
  75. int fd2 = open(argv[2], O_CREAT | O_WRONLY, 0);
  76. assert(fd2 != -1);
  77. //Execute mmap
  78. void* mmappedData = mmap(NULL, filesize, PROT_READ, MAP_SHARED, fd, 0);
  79. assert(mmappedData != MAP_FAILED);
  80. process(mmappedData, buf.st_size, fd2);
  81. //Cleanup
  82. int rc = munmap(mmappedData, filesize);
  83. assert(rc == 0);
  84. close(fd);
  85. close(fd2);
  86. }