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: head/lib/libarchive/archive_read_support_format_raw.c 201107 2009-12-28 03:25:33Z kientzle $");
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. */
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 *);
56 archive_read_support_format_raw(struct archive *_a)
58 struct raw_info *info;
59 struct archive_read *a = (struct archive_read *)_a;
62 archive_check_magic(_a, ARCHIVE_READ_MAGIC,
63 ARCHIVE_STATE_NEW, "archive_read_support_format_raw");
65 info = (struct raw_info *)calloc(1, sizeof(*info));
67 archive_set_error(&a->archive, ENOMEM,
68 "Can't allocate raw_info data");
69 return (ARCHIVE_FATAL);
72 r = __archive_read_register_format(a,
75 archive_read_format_raw_bid,
77 archive_read_format_raw_read_header,
78 archive_read_format_raw_read_data,
79 archive_read_format_raw_read_data_skip,
81 archive_read_format_raw_cleanup);
88 * Bid 1 if this is a non-empty file. Anyone who can really support
89 * this should outbid us, so it should generally be safe to use "raw"
90 * in conjunction with other formats. But, this could really confuse
91 * folks if there are bid errors or minor file damage, so we don't
92 * include "raw" as part of support_format_all().
95 archive_read_format_raw_bid(struct archive_read *a, int best_bid)
97 if (best_bid < 1 && __archive_read_ahead(a, 1, NULL) != NULL)
103 * Mock up a fake header.
106 archive_read_format_raw_read_header(struct archive_read *a,
107 struct archive_entry *entry)
109 struct raw_info *info;
111 info = (struct raw_info *)(a->format->data);
112 if (info->end_of_file)
113 return (ARCHIVE_EOF);
115 a->archive.archive_format = ARCHIVE_FORMAT_RAW;
116 a->archive.archive_format_name = "raw";
117 archive_entry_set_pathname(entry, "data");
118 archive_entry_set_filetype(entry, AE_IFREG);
119 archive_entry_set_perm(entry, 0644);
120 /* I'm deliberately leaving most fields unset here. */
125 archive_read_format_raw_read_data(struct archive_read *a,
126 const void **buff, size_t *size, int64_t *offset)
128 struct raw_info *info;
131 info = (struct raw_info *)(a->format->data);
133 /* Consume the bytes we read last time. */
134 if (info->unconsumed) {
135 __archive_read_consume(a, info->unconsumed);
136 info->unconsumed = 0;
139 if (info->end_of_file)
140 return (ARCHIVE_EOF);
142 /* Get whatever bytes are immediately available. */
143 *buff = __archive_read_ahead(a, 1, &avail);
145 /* Return the bytes we just read */
147 *offset = info->offset;
148 info->offset += *size;
149 info->unconsumed = avail;
151 } else if (0 == avail) {
152 /* Record and return end-of-file. */
153 info->end_of_file = 1;
155 *offset = info->offset;
156 return (ARCHIVE_EOF);
158 /* Record and return an error. */
160 *offset = info->offset;
166 archive_read_format_raw_read_data_skip(struct archive_read *a)
168 struct raw_info *info = (struct raw_info *)(a->format->data);
170 /* Consume the bytes we read last time. */
171 if (info->unconsumed) {
172 __archive_read_consume(a, info->unconsumed);
173 info->unconsumed = 0;
175 info->end_of_file = 1;
180 archive_read_format_raw_cleanup(struct archive_read *a)
182 struct raw_info *info;
184 info = (struct raw_info *)(a->format->data);
186 a->format->data = NULL;