2 * Copyright (c) 2003-2009 Tim Kientzle
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
25 #include "archive_platform.h"
26 __FBSDID("$FreeBSD$");
37 #include "archive_entry.h"
38 #include "archive_private.h"
39 #include "archive_read_private.h"
42 int64_t offset; /* Current position in the file. */
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 *);
55 archive_read_support_format_raw(struct archive *_a)
57 struct raw_info *info;
58 struct archive_read *a = (struct archive_read *)_a;
61 info = (struct raw_info *)calloc(1, sizeof(*info));
63 archive_set_error(&a->archive, ENOMEM,
64 "Can't allocate raw_info data");
65 return (ARCHIVE_FATAL);
68 r = __archive_read_register_format(a,
71 archive_read_format_raw_bid,
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);
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().
90 archive_read_format_raw_bid(struct archive_read *a)
93 if (__archive_read_ahead(a, 1, NULL) == NULL)
99 * Mock up a fake header.
102 archive_read_format_raw_read_header(struct archive_read *a,
103 struct archive_entry *entry)
105 struct raw_info *info;
107 info = (struct raw_info *)(a->format->data);
108 if (info->end_of_file)
109 return (ARCHIVE_EOF);
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. */
120 archive_read_format_raw_read_data(struct archive_read *a,
121 const void **buff, size_t *size, off_t *offset)
123 struct raw_info *info;
126 info = (struct raw_info *)(a->format->data);
127 if (info->end_of_file)
128 return (ARCHIVE_EOF);
130 /* Get whatever bytes are immediately available. */
131 *buff = __archive_read_ahead(a, 1, &avail);
133 /* Consume and return the bytes we just read */
134 __archive_read_consume(a, avail);
136 *offset = info->offset;
137 info->offset += *size;
139 } else if (0 == avail) {
140 /* Record and return end-of-file. */
141 info->end_of_file = 1;
143 *offset = info->offset;
144 return (ARCHIVE_EOF);
146 /* Record and return an error. */
148 *offset = info->offset;
154 archive_read_format_raw_read_data_skip(struct archive_read *a)
156 struct raw_info *info;
158 int64_t request = 1024 * 1024 * 1024UL; /* Skip 1 GB at a time. */
160 info = (struct raw_info *)(a->format->data);
161 if (info->end_of_file)
162 return (ARCHIVE_EOF);
163 info->end_of_file = 1;
166 bytes_skipped = __archive_read_skip_lenient(a, request);
167 if (bytes_skipped < 0)
168 return (ARCHIVE_FATAL);
169 if (bytes_skipped < request)
171 /* We skipped all the bytes we asked for. There might
172 * be more, so try again. */
177 archive_read_format_raw_cleanup(struct archive_read *a)
179 struct raw_info *info;
181 info = (struct raw_info *)(a->format->data);
183 a->format->data = NULL;