Debian Codegraph @

src/tools/zip/xattr.cpp

raw
1 /* -*- Mode: C++; c-default-style: "k&r"; indent-tabs-mode: nil; tab-width: 2; c-basic-offset: 2 -*- */
2
3 /* libmwaw: tools
4 * Version: MPL 2.0 / LGPLv2+
5 *
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 2.0 (the "License"); you may not use this file except in compliance with
8 * the License or as specified alternatively below. You may obtain a copy of
9 * the License at http://www.mozilla.org/MPL/
10 *
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
15 *
16 * Major Contributor(s):
17 * Copyright (C) 2011, 2012 Alonso Laurent ([email protected])
18 *
19 *
20 * All Rights Reserved.
21 *
22 * For minor contributions see the git repository.
23 *
24 * Alternatively, the contents of this file may be used under the terms of
25 * the GNU Lesser General Public License Version 2 or later (the "LGPLv2+"),
26 * in which case the provisions of the LGPLv2+ are applicable
27 * instead of those above.
28 */
29 #include <string.h>
30 #include <iostream>
31 #include <memory>
32
33 #include <algorithm>
34
35 #include "input.h"
36 #include "xattr.h"
37 #include "zip_internal.h"
38
39 #include <sys/stat.h>
40 #if WITH_EXTENDED_FS
41 # include <sys/types.h>
42 # include <sys/xattr.h>
43 #endif
44
45 #ifndef __BIG_ENDIAN__
46 static void addShort(unsigned short x, char *&buff)
47 {
48 *(buff++)=char(x>>8);
49 *(buff++)=char(x&0xFF);
50 }
51 static void addLong(unsigned int x, char *&buff)
52 {
53 *(buff++)=char((x>>24)&0xFF);
54 *(buff++)=char((x>>16)&0xFF);
55 *(buff++)=char((x>>8)&0xFF);
56 *(buff++)=char(x&0xFF);
57 }
58 #else
59 static void addShort(unsigned short x, char *&buff)
60 {
61 *(buff++)=char(x&0xFF);
62 *(buff++)=char(x>>8);
63 }
64 static void addLong(unsigned int x, char *&buff)
65 {
66 *(buff++)=char(x&0xFF);
67 *(buff++)=char((x>>8)&0xFF);
68 *(buff++)=char((x>>16)&0xFF);
69 *(buff++)=char((x>>24)&0xFF);
70 }
71 #endif
72
73 namespace libmwaw_zip
74 {
75 std::shared_ptr<InputStream> XAttr::getStream() const
76 {
77 std::shared_ptr<InputStream> res;
78 if (m_fName.length()==0)
79 return res;
80 #if WITH_EXTENDED_FS==0
81 return res;
82 #else
83
84 # if WITH_EXTENDED_FS==1
85 # define MWAW_EXTENDED_FS , 0, XATTR_SHOWCOMPRESSION
86 # else
87 # define MWAW_EXTENDED_FS
88 # endif
89 ssize_t sz[2];
90 static char const *attr[2] = { "com.apple.FinderInfo", "com.apple.ResourceFork" };
91 int find=0;
92 for (int i = 0; i < 2; i++) {
93 sz[i]=getxattr(m_fName.c_str(), attr[i], 0, 0 MWAW_EXTENDED_FS);
94 if (sz[i]) find++;
95 }
96 if (!find) return res;
97
98 //ok we must create a MIME stream
99 ssize_t totalSz= sz[0]+sz[1]+find*12+30;
100 std::unique_ptr<char[]> buffer{new char[size_t(totalSz)]};
101 if (buffer==0) {
102 MWAW_DEBUG_MSG(("XAttr::getStream: can not alloc buffer\n"));
103 return res;
104 }
105 char *bufferPtr=buffer.get();
106 addLong(0x00051607, bufferPtr); // signature
107 addLong(0x20000, bufferPtr); // version
108 static char defaultName[]="Mac OS X ";
109 for (int i = 0; i < 16; i++)
110 *(bufferPtr++) = defaultName[i];
111 addShort(static_cast<unsigned short>(find), bufferPtr); // num of data
112
113 // the data header
114 unsigned int offset=30+static_cast<unsigned int>(find)*12;
115 unsigned int const wh[2]= {9,2};
116 for (int i=0; i < 2; i++) {
117 if (!sz[i]) continue;
118 addLong(wh[i],bufferPtr); // id
119 addLong(offset,bufferPtr); // offset
120 addLong(static_cast<unsigned int>(sz[i]),bufferPtr); // size
121 offset+=static_cast<unsigned int>(sz[i]);
122 }
123 addLong(0, bufferPtr);
124 // the data
125 for (int i=0; i < 2; i++) {
126 if (!sz[i]) continue;
127 if (getxattr(m_fName.c_str(), attr[i], bufferPtr, size_t(sz[i]) MWAW_EXTENDED_FS) != sz[i]) {
128 MWAW_DEBUG_MSG(("XAttr::getStream: getxattr can read attribute\n"));
129 return res;
130 }
131 bufferPtr+=sz[i];
132 }
133
134 res.reset(new StringStream(reinterpret_cast<unsigned char *>(buffer.get()),static_cast<unsigned long>(totalSz)));
135 return res;
136 #endif
137 }
138
139 std::shared_ptr<InputStream> XAttr::getClassicStream() const
140 {
141 std::shared_ptr<InputStream> res;
142 if (m_fName.length()==0)
143 return res;
144
145 /** look for file FINDER.DAT */
146 size_t sPos=m_fName.rfind('/');
147 std::string folder(""), file("");
148 if (sPos==std::string::npos)
149 file = m_fName;
150 else {
151 folder=m_fName.substr(0,sPos+1);
152 file=m_fName.substr(sPos+1);
153 }
154 std::string name=folder+"FINDER.DAT";
155 struct stat status;
156 if (stat(name.c_str(), &status)!=0 || !S_ISREG(status.st_mode))
157 return res;
158
159 std::shared_ptr<FileStream> input(new FileStream(name.c_str()));
160 if (!input || !input->ok())
161 return res;
162 input->seek(0, InputStream::SK_SET);
163 try {
164 int num=0;
165 while (!input->atEOS()) {
166 if (++num==23) { // must realign read on 2048
167 if (input->seek(24, InputStream::SK_CUR))
168 break;
169 num=0;
170 }
171 long pos=input->tell();
172 if (input->seek(pos+92, InputStream::SK_SET))
173 break;
174 input->seek(pos, InputStream::SK_SET);
175 auto nSz=static_cast<int>(input->readU8());
176 if (nSz<=0 || nSz>31) {
177 MWAW_DEBUG_MSG(("XAttr::getClassicStream: file size seems bad %d\n", nSz));
178 input->seek(pos+92, InputStream::SK_SET);
179 continue;
180 }
181 std::string fName("");
182 for (int i=0; i<nSz; ++i)
183 fName+=char(input->readU8());
184 if (fName!=file) {
185 input->seek(pos+92, InputStream::SK_SET);
186 continue;
187 }
188 int const finderInfoSize=32;
189 ssize_t sz[2]= {finderInfoSize,0};
190 // try to find the ressource fork
191 input->seek(pos+80, InputStream::SK_SET);
192 std::string rsrcName("");
193 for (int i=0; i<8; ++i) {
194 auto c=char(input->readU8());
195 if (!c) break;
196 rsrcName += c;
197 }
198 for (int i=0; i <3; ++i) { // extension
199 auto c=char(input->readU8());
200 if (!c) break;
201 if (i==0) rsrcName += '.';
202 rsrcName += c;
203 }
204 name=folder+"RESOURCE.FRK/"+rsrcName;
205 if (stat(name.c_str(), &status)==0 && S_ISREG(status.st_mode))
206 sz[1]=ssize_t(status.st_size);
207 int find=sz[1] ? 2 : 1;
208
209 //ok we must create a MIME stream
210 ssize_t totalSz= sz[0]+sz[1]+find*12+30;
211 std::unique_ptr<char[]> buffer{new char[size_t(totalSz)]};
212 if (buffer==0) {
213 MWAW_DEBUG_MSG(("XAttr::getClassicStream: can not alloc buffer\n"));
214 return res;
215 }
216 char *bufferPtr=buffer.get();
217 addLong(0x00051607, bufferPtr); // signature
218 addLong(0x20000, bufferPtr); // version
219 memcpy(bufferPtr,"Mac OS X ",16); // defaultName
220 bufferPtr+=16;
221 addShort(static_cast<unsigned short>(find), bufferPtr); // num of data
222
223 // the data header
224 unsigned int offset=30+static_cast<unsigned int>(find)*12;
225 unsigned int const wh[2]= {9,2};
226 for (int i=0; i < 2; i++) {
227 if (!sz[i]) continue;
228 addLong(wh[i],bufferPtr); // id
229 addLong(offset,bufferPtr); // offset
230 addLong(static_cast<unsigned int>(sz[i]),bufferPtr); // size
231 offset+=static_cast<unsigned int>(sz[i]);
232 }
233 addLong(0, bufferPtr);
234
235 // the data
236
237 // fInfo or fInfo
238 input->seek(pos+32, InputStream::SK_SET);
239 unsigned long numBytesRead = 0;
240 const unsigned char *data = input->read(finderInfoSize, numBytesRead);
241 if (numBytesRead != static_cast<unsigned long>(finderInfoSize) || !data) {
242 MWAW_DEBUG_MSG(("XAttr::getClassicStream::readFinderDat: can not read fileinfo\n"));
243 return res;
244 }
245 memcpy(bufferPtr, &data[0], finderInfoSize);
246 bufferPtr+=finderInfoSize;
247
248 if (sz[1]) {
249 input.reset(new FileStream(name.c_str()));
250 if (!input || !input->ok()) {
251 return res;
252 }
253 numBytesRead = 0;
254 data = input->read(static_cast<unsigned long>(sz[1]), numBytesRead);
255 if (numBytesRead != static_cast<unsigned long>(sz[1]) || !data) {
256 MWAW_DEBUG_MSG(("XAttr::getClassicStream::readFinderDat: can not read resource fork\n"));
257 return res;
258 }
259 memcpy(bufferPtr, &data[0], size_t(sz[1]));
260 }
261
262 res.reset(new StringStream(reinterpret_cast<unsigned char *>(buffer.get()),
263 static_cast<unsigned long>(totalSz)));
264 return res;
265 }
266 }
267 catch (...) {
268 }
269 return res;
270 }
271
272 }
273 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: