/** => zzip_disk_mmap * * This function opens the given archive by name and turn the filehandle * to => zzip_disk_mmap for bringing it to main memory. If it can not * be => mmap(2)'ed then we slurp the whole file into a newly => malloc(2)'ed * memory block. Only if that fails too then we return null. Since handling * of disk->buffer is ambigous it should not be snatched away please. */ zzip__new__ ZZIP_DISK * zzip_disk_open(char *filename) { # ifndef O_BINARY # define O_BINARY 0 # endif struct stat st; if (stat(filename, &st) || ! st.st_size) return 0; ___ int fd = open(filename, O_RDONLY | O_BINARY); if (fd <= 0) return 0; ___ ZZIP_DISK *disk = zzip_disk_mmap(fd); if (disk) return disk; ___ zzip_byte_t *buffer = malloc(st.st_size); if (! buffer) return 0; if ((st.st_size == read(fd, buffer, st.st_size)) && (disk = zzip_disk_new())) { disk->buffer = buffer; disk->endbuf = buffer + st.st_size; disk->mapped = -1; disk->flags |= ZZIP_DISK_FLAGS_OWNED_BUFFER; } else { free(buffer); } return disk; ____; ____; ____; }
/** turn a filehandle into a mmapped zip disk archive handle * * This function uses the given file-descriptor to detect the length of the * file and calls the system => mmap(2) to put it in main memory. If it is * successful then a newly allocated ZZIP_DISK* is returned with * disk->buffer pointing to the mapview of the zipdisk content. */ zzip__new__ ZZIP_DISK* zzip_disk_mmap(int fd) { struct stat st; if (fstat (fd, &st) || !st.st_size) return 0; ___ ZZIP_DISK* disk = zzip_disk_new (); if (! disk) return 0; disk->buffer = _zzip_mmap (& zzip->mapped, fd, 0, st.st_size); if (disk->buffer == MAP_FAILED) { free (disk); return 0; } disk->endbuf = disk->buffer + st.st_size; return disk; ____; }
/** => zzip_disk_mmap * This function will attach a buffer with a zip image * that was acquired from another source than a file. * Note that if zzip_disk_mmap fails then zzip_disk_open * will fall back and try to read the full file to memory * wrapping a ZZIP_DISK around the memory buffer just as * this function will do. Note that this function will not * own the buffer, it will neither be written nor free()d. */ zzip__new__ ZZIP_DISK * zzip_disk_buffer(char *buffer, size_t buflen) { ZZIP_DISK *disk = zzip_disk_new(); if (disk) { disk->buffer = buffer; disk->endbuf = buffer + buflen; disk->mapped = -1; } return disk; }
/** => zzip_disk_mmap * This function will attach a buffer with a zip image * that was acquired from another source than a file. * Note that if zzip_disk_mmap fails then zzip_disk_open * will fall back and try to read the full file to memory * wrapping a ZZIP_DISK around the memory buffer just as * this function will do. Note that this function will not * own the buffer, it will neither be written nor free()d. */ zzip__new__ ZZIP_DISK * zzip_disk_buffer(void *buffer, size_t buflen) { ZZIP_DISK *disk = zzip_disk_new(); if (disk) { disk->buffer = (zzip_byte_t *) buffer; disk->endbuf = (zzip_byte_t *) buffer + buflen; disk->mapped = -1; } return disk; }