1 /*- 2 * Copyright (c) 2003-2009 Tim Kientzle 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR 15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, 18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 #include "archive_platform.h" 26 __FBSDID("$FreeBSD: head/lib/libarchive/archive_read_support_format_raw.c 201107 2009-12-28 03:25:33Z kientzle $"); 27 28 #ifdef HAVE_ERRNO_H 29 #include <errno.h> 30 #endif 31 #include <stdio.h> 32 #ifdef HAVE_STDLIB_H 33 #include <stdlib.h> 34 #endif 35 36 #include "archive.h" 37 #include "archive_entry.h" 38 #include "archive_private.h" 39 #include "archive_read_private.h" 40 41 struct raw_info { 42 int64_t offset; /* Current position in the file. */ 43 int end_of_file; 44 }; 45 46 static int archive_read_format_raw_bid(struct archive_read *); 47 static int archive_read_format_raw_cleanup(struct archive_read *); 48 static int archive_read_format_raw_read_data(struct archive_read *, 49 const void **, size_t *, off_t *); 50 static int archive_read_format_raw_read_data_skip(struct archive_read *); 51 static int archive_read_format_raw_read_header(struct archive_read *, 52 struct archive_entry *); 53 54 int 55 archive_read_support_format_raw(struct archive *_a) 56 { 57 struct raw_info *info; 58 struct archive_read *a = (struct archive_read *)_a; 59 int r; 60 61 info = (struct raw_info *)calloc(1, sizeof(*info)); 62 if (info == NULL) { 63 archive_set_error(&a->archive, ENOMEM, 64 "Can't allocate raw_info data"); 65 return (ARCHIVE_FATAL); 66 } 67 68 r = __archive_read_register_format(a, 69 info, 70 "raw", 71 archive_read_format_raw_bid, 72 NULL, 73 archive_read_format_raw_read_header, 74 archive_read_format_raw_read_data, 75 archive_read_format_raw_read_data_skip, 76 archive_read_format_raw_cleanup); 77 if (r != ARCHIVE_OK) 78 free(info); 79 return (r); 80 } 81 82 /* 83 * Bid 1 if this is a non-empty file. Anyone who can really support 84 * this should outbid us, so it should generally be safe to use "raw" 85 * in conjunction with other formats. But, this could really confuse 86 * folks if there are bid errors or minor file damage, so we don't 87 * include "raw" as part of support_format_all(). 88 */ 89 static int 90 archive_read_format_raw_bid(struct archive_read *a) 91 { 92 93 if (__archive_read_ahead(a, 1, NULL) == NULL) 94 return (-1); 95 return (1); 96 } 97 98 /* 99 * Mock up a fake header. 100 */ 101 static int 102 archive_read_format_raw_read_header(struct archive_read *a, 103 struct archive_entry *entry) 104 { 105 struct raw_info *info; 106 107 info = (struct raw_info *)(a->format->data); 108 if (info->end_of_file) 109 return (ARCHIVE_EOF); 110 111 a->archive.archive_format = ARCHIVE_FORMAT_RAW; 112 a->archive.archive_format_name = "Raw data"; 113 archive_entry_set_pathname(entry, "data"); 114 /* XXX should we set mode to mimic a regular file? XXX */ 115 /* I'm deliberately leaving most fields unset here. */ 116 return (ARCHIVE_OK); 117 } 118 119 static int 120 archive_read_format_raw_read_data(struct archive_read *a, 121 const void **buff, size_t *size, off_t *offset) 122 { 123 struct raw_info *info; 124 ssize_t avail; 125 126 info = (struct raw_info *)(a->format->data); 127 if (info->end_of_file) 128 return (ARCHIVE_EOF); 129 130 /* Get whatever bytes are immediately available. */ 131 *buff = __archive_read_ahead(a, 1, &avail); 132 if (avail > 0) { 133 /* Consume and return the bytes we just read */ 134 __archive_read_consume(a, avail); 135 *size = avail; 136 *offset = info->offset; 137 info->offset += *size; 138 return (ARCHIVE_OK); 139 } else if (0 == avail) { 140 /* Record and return end-of-file. */ 141 info->end_of_file = 1; 142 *size = 0; 143 *offset = info->offset; 144 return (ARCHIVE_EOF); 145 } else { 146 /* Record and return an error. */ 147 *size = 0; 148 *offset = info->offset; 149 return (avail); 150 } 151 } 152 153 static int 154 archive_read_format_raw_read_data_skip(struct archive_read *a) 155 { 156 struct raw_info *info; 157 off_t bytes_skipped; 158 int64_t request = 1024 * 1024 * 1024UL; /* Skip 1 GB at a time. */ 159 160 info = (struct raw_info *)(a->format->data); 161 if (info->end_of_file) 162 return (ARCHIVE_EOF); 163 info->end_of_file = 1; 164 165 for (;;) { 166 bytes_skipped = __archive_read_skip_lenient(a, request); 167 if (bytes_skipped < 0) 168 return (ARCHIVE_FATAL); 169 if (bytes_skipped < request) 170 return (ARCHIVE_OK); 171 /* We skipped all the bytes we asked for. There might 172 * be more, so try again. */ 173 } 174 } 175 176 static int 177 archive_read_format_raw_cleanup(struct archive_read *a) 178 { 179 struct raw_info *info; 180 181 info = (struct raw_info *)(a->format->data); 182 free(info); 183 a->format->data = NULL; 184 return (ARCHIVE_OK); 185 } 186