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