123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- #include <sys/mman.h>
- #include <math.h>
- #include <stdlib.h>
- #include <string.h>
- #include <stdio.h>
- #include <sys/types.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <sys/stat.h>
- #include <assert.h>
- size_t getFilesize(const char* filename) {
- struct stat st;
- stat(filename, &st);
- return st.st_size;
- }
- int octToInt(char *current_char, unsigned int size) {
- unsigned int output = 0;
- while(size > 0){
- output = output * 8 + *current_char - '0';
- current_char++;
- size--;
- }
- return output;
- }
- char *findNextHeader(char *start, long int *actualSize) {
- char *header = start;
- char tmp[13];
- long int size;
- // printf("%d. %s\n", i, header);
- memset(tmp, 0, 13);
- strncpy(tmp, header + 124, 12); // size
- size = strtol(tmp, 0, 8);
- printf("\tbad: %s %ld\n", header, size);
- header += 512;
- long int next512 = ceil(size/512.0) * 512;
- header += next512; // look beyond this
- for (;; ++header) {
- if (header[0] != '.') continue;
- char *magic = header + 257; // ustar
- if (magic[0] == 'u' && magic[1] == 's' && magic[2] == 't') break;
- }
- printf("\tbingo! actual_size: %ld next_file:%s\n", (header - start + 512), header);
- if (actualSize) *actualSize = header - start + 512;
- return header;
- }
- void process(void *start, long length, int outFd) {
- char *header = (char *) start; // 512
- int i = 0;
- while (header < ((char *)start+length)) {
- char tmp[13];
- memset(tmp, 0, 13);
- strncpy(tmp, header + 124, 12); // size
- long int size = strtol(tmp, 0, 8);
- printf("%d. %s %ld\n", i++, header, size);
- if (size == 8589934591) { // oops, bad size
- printf("\tbad size detected. will find next header\n");
- long int actualSize = 0;
- header = findNextHeader(header, &actualSize);
- printf("\tdd skip=%ld count=%ld if=input.binary of=output.binary iflag=skip_bytes,count_bytes\n", (header - actualSize) - (char *)start, actualSize);
- } else {
- int next512 = ceil(size/512.0) * 512; // file data with padding size
- write(outFd, header, 512+next512);
- header += 512 + next512; // header size + the file data size
- }
- }
- printf("done\n");
- }
- int main(int argc, char** argv) {
- size_t filesize = getFilesize(argv[1]);
- //Open file
- int fd = open(argv[1], O_RDONLY, 0);
- assert(fd != -1);
- struct stat buf;
- fstat(fd, &buf);
- int fd2 = open(argv[2], O_CREAT | O_WRONLY, 0);
- assert(fd2 != -1);
- //Execute mmap
- void* mmappedData = mmap(NULL, filesize, PROT_READ, MAP_SHARED, fd, 0);
- assert(mmappedData != MAP_FAILED);
- process(mmappedData, buf.st_size, fd2);
- //Cleanup
- int rc = munmap(mmappedData, filesize);
- assert(rc == 0);
- close(fd);
- close(fd2);
- }
|