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 int64_t unconsumed; 44 int end_of_file; 45 }; 46 47 static int archive_read_format_raw_bid(struct archive_read *, int); 48 static int archive_read_format_raw_cleanup(struct archive_read *); 49 static int archive_read_format_raw_read_data(struct archive_read *, 50 const void **, size_t *, int64_t *); 51 static int archive_read_format_raw_read_data_skip(struct archive_read *); 52 static int archive_read_format_raw_read_header(struct archive_read *, 53 struct archive_entry *); 54 55 int 56 archive_read_support_format_raw(struct archive *_a) 57 { 58 struct raw_info *info; 59 struct archive_read *a = (struct archive_read *)_a; 60 int r; 61 62 archive_check_magic(_a, ARCHIVE_READ_MAGIC, 63 ARCHIVE_STATE_NEW, "archive_read_support_format_raw"); 64 65 info = (struct raw_info *)calloc(1, sizeof(*info)); 66 if (info == NULL) { 67 archive_set_error(&a->archive, ENOMEM, 68 "Can't allocate raw_info data"); 69 return (ARCHIVE_FATAL); 70 } 71 72 r = __archive_read_register_format(a, 73 info, 74 "raw", 75 archive_read_format_raw_bid, 76 NULL, 77 archive_read_format_raw_read_header, 78 archive_read_format_raw_read_data, 79 archive_read_format_raw_read_data_skip, 80 archive_read_format_raw_cleanup); 81 if (r != ARCHIVE_OK) 82 free(info); 83 return (r); 84 } 85 86 /* 87 * Bid 1 if this is a non-empty file. Anyone who can really support 88 * this should outbid us, so it should generally be safe to use "raw" 89 * in conjunction with other formats. But, this could really confuse 90 * folks if there are bid errors or minor file damage, so we don't 91 * include "raw" as part of support_format_all(). 92 */ 93 static int 94 archive_read_format_raw_bid(struct archive_read *a, int best_bid) 95 { 96 if (best_bid < 1 && __archive_read_ahead(a, 1, NULL) != NULL) 97 return (1); 98 return (-1); 99 } 100 101 /* 102 * Mock up a fake header. 103 */ 104 static int 105 archive_read_format_raw_read_header(struct archive_read *a, 106 struct archive_entry *entry) 107 { 108 struct raw_info *info; 109 110 info = (struct raw_info *)(a->format->data); 111 if (info->end_of_file) 112 return (ARCHIVE_EOF); 113 114 a->archive.archive_format = ARCHIVE_FORMAT_RAW; 115 a->archive.archive_format_name = "raw"; 116 archive_entry_set_pathname(entry, "data"); 117 archive_entry_set_filetype(entry, AE_IFREG); 118 archive_entry_set_perm(entry, 0644); 119 /* I'm deliberately leaving most fields unset here. */ 120 return (ARCHIVE_OK); 121 } 122 123 static int 124 archive_read_format_raw_read_data(struct archive_read *a, 125 const void **buff, size_t *size, int64_t *offset) 126 { 127 struct raw_info *info; 128 ssize_t avail; 129 130 info = (struct raw_info *)(a->format->data); 131 132 /* Consume the bytes we read last time. */ 133 if (info->unconsumed) { 134 __archive_read_consume(a, info->unconsumed); 135 info->unconsumed = 0; 136 } 137 138 if (info->end_of_file) 139 return (ARCHIVE_EOF); 140 141 /* Get whatever bytes are immediately available. */ 142 *buff = __archive_read_ahead(a, 1, &avail); 143 if (avail > 0) { 144 /* Return the bytes we just read */ 145 *size = avail; 146 *offset = info->offset; 147 info->offset += *size; 148 info->unconsumed = avail; 149 return (ARCHIVE_OK); 150 } else if (0 == avail) { 151 /* Record and return end-of-file. */ 152 info->end_of_file = 1; 153 *size = 0; 154 *offset = info->offset; 155 return (ARCHIVE_EOF); 156 } else { 157 /* Record and return an error. */ 158 *size = 0; 159 *offset = info->offset; 160 return (avail); 161 } 162 } 163 164 static int 165 archive_read_format_raw_read_data_skip(struct archive_read *a) 166 { 167 struct raw_info *info = (struct raw_info *)(a->format->data); 168 169 /* Consume the bytes we read last time. */ 170 if (info->unconsumed) { 171 __archive_read_consume(a, info->unconsumed); 172 info->unconsumed = 0; 173 } 174 info->end_of_file = 1; 175 return (ARCHIVE_OK); 176 } 177 178 static int 179 archive_read_format_raw_cleanup(struct archive_read *a) 180 { 181 struct raw_info *info; 182 183 info = (struct raw_info *)(a->format->data); 184 free(info); 185 a->format->data = NULL; 186 return (ARCHIVE_OK); 187 } 188