Debian Codegraph @

src/tools/file/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
30 #include <string.h>
31 #include <iostream>
32 #include <memory>
33
34 #include <algorithm>
35
36 #include "file_internal.h"
37 #include "input.h"
38 #include "xattr.h"
39
40 #include <sys/stat.h>
41 #if WITH_EXTENDED_FS
42 # include <sys/types.h>
43 # include <sys/xattr.h>
44 #endif
45
46 namespace libmwaw_tools
47 {
48 InputStream *XAttr::getStream(const char *attr) const
49 {
50 if (m_fName.empty() || !attr)
51 return 0;
52 #if WITH_EXTENDED_FS==1
53 # define MWAW_EXTENDED_FS , 0, XATTR_SHOWCOMPRESSION
54 #else
55 # define MWAW_EXTENDED_FS
56 #endif
57
58 #if WITH_EXTENDED_FS
59 ssize_t sz=getxattr(m_fName.c_str(), attr, 0, 0 MWAW_EXTENDED_FS);
60 if (sz>0) {
61 std::unique_ptr<char[]> buffer{new char[size_t(sz)]};
62 if (buffer==0) {
63 MWAW_DEBUG_MSG(("XAttr::getStream: can not alloc buffer\n"));
64 return 0;
65 }
66 if (getxattr(m_fName.c_str(), attr, buffer.get(), size_t(sz) MWAW_EXTENDED_FS) != sz) {
67 MWAW_DEBUG_MSG(("XAttr::getStream: getxattr can read attribute\n"));
68 return 0;
69 }
70
71 InputStream *res=new StringStream(reinterpret_cast<unsigned char *>(buffer.get()),static_cast<unsigned long>(sz));
72 return res;
73 }
74 #endif
75
76 std::unique_ptr<InputStream> auxi{getAuxillarInput()};
77 if (auxi) {
78 InputStream *res = unMacMIME(auxi.get(), attr);
79 return res;
80 }
81
82 return getUsingFinderDat(attr);
83 }
84
85 InputStream *XAttr::getAuxillarInput() const
86 {
87 if (m_fName.empty())
88 return 0;
89
90 /** look for file ._NAME or __MACOSX/._NAME
91 Fixme: Must be probably changed to works on Windows */
92 size_t sPos=m_fName.rfind('/');
93 std::string folder(""), file("");
94 if (sPos==std::string::npos)
95 file = m_fName;
96 else {
97 folder=m_fName.substr(0,sPos+1);
98 file=m_fName.substr(sPos+1);
99 }
100 std::string name=folder+"._"+file;
101
102 struct stat status;
103 if (stat(name.c_str(), &status)==0 && S_ISREG(status.st_mode))
104 ;
105 else {
106 name=folder+"__MACOSX/._"+file;
107 if (stat(name.c_str(), &status) || !S_ISREG(status.st_mode))
108 return 0;
109 }
110
111 std::unique_ptr<FileStream> res{new FileStream(name.c_str())};
112 if (res->ok()) {
113 MWAW_DEBUG_MSG(("XAttr::getAuxillarInput: find an auxilliary file: %s\n",name.c_str()));
114 return res.release();
115 }
116 return 0;
117 }
118
119 /* os9 way to save resource on a Fat,... disk */
120 InputStream *XAttr::getUsingFinderDat(char const *what) const
121 {
122 if (m_fName.empty())
123 return 0;
124
125 bool lookForResourceFork=true;
126 if (strcmp("com.apple.FinderInfo",what)==0)
127 lookForResourceFork=false;
128 else if (strcmp("com.apple.ResourceFork",what))
129 return 0;
130
131 /** look for file FINDER.DAT */
132 size_t sPos=m_fName.rfind('/');
133 std::string folder(""), file("");
134 if (sPos==std::string::npos)
135 file = m_fName;
136 else {
137 folder=m_fName.substr(0,sPos+1);
138 file=m_fName.substr(sPos+1);
139 }
140 std::string name=folder+"FINDER.DAT";
141 struct stat status;
142 if (stat(name.c_str(), &status)!=0 || !S_ISREG(status.st_mode))
143 return 0;
144
145 std::unique_ptr<FileStream> input{new FileStream(name.c_str())};
146 if (!input || !input->ok()) {
147 return 0;
148 }
149
150 input->seek(0, InputStream::SK_SET);
151 try {
152 int num=0;
153 while (!input->atEOS()) {
154 if (++num==23) { // must realign read on 2048
155 if (input->seek(24, InputStream::SK_CUR))
156 break;
157 num=0;
158 }
159 long pos=input->tell();
160 if (input->seek(pos+92, InputStream::SK_SET))
161 break;
162 input->seek(pos, InputStream::SK_SET);
163 auto nSz=static_cast<int>(input->readU8());
164 if (nSz<=0 || nSz>31) {
165 MWAW_DEBUG_MSG(("XAttr::readFinderDat: file size seems bad %d\n", nSz));
166 input->seek(pos+92, InputStream::SK_SET);
167 continue;
168 }
169 std::string fName("");
170 for (int i=0; i<nSz; ++i)
171 fName+=char(input->readU8());
172 if (fName!=file) {
173 input->seek(pos+92, InputStream::SK_SET);
174 continue;
175 }
176 // fInfo or fInfo
177 if (!lookForResourceFork) {
178 input->seek(pos+32, InputStream::SK_SET);
179 unsigned long numBytesRead = 0;
180 const unsigned char *data = input->read(16, numBytesRead);
181 if (numBytesRead != 16 || !data) {
182 MWAW_DEBUG_MSG(("XAttr::readFinderDat: can not read fileinfo\n"));
183 break;
184 }
185 return new StringStream(data,static_cast<unsigned long>(16));
186 }
187
188 input->seek(pos+80, InputStream::SK_SET);
189 std::string rsrcName("");
190 for (int i=0; i<8; ++i) {
191 auto c=char(input->readU8());
192 if (!c) break;
193 rsrcName += c;
194 }
195 for (int i=0; i <3; ++i) { // extension
196 auto c=char(input->readU8());
197 if (!c) break;
198 if (i==0) rsrcName += '.';
199 rsrcName += c;
200 }
201 name=folder+"RESOURCE.FRK/"+rsrcName;
202 if (stat(name.c_str(), &status)!=0 || !S_ISREG(status.st_mode))
203 break;
204 input.reset(new FileStream(name.c_str()));
205 if (!input || !input->ok())
206 break;
207 return input.release();
208 }
209 }
210 catch (...) {
211 }
212 return 0;
213 }
214
215 /* freely inspired from http://tools.ietf.org/html/rfc1740#appendix-A
216 */
217 InputStream *XAttr::unMacMIME(InputStream *inp, char const *what) const
218 {
219 if (!inp || !what) return 0;
220 int lookForId=0;
221 if (strcmp("com.apple.FinderInfo",what)==0)
222 lookForId=9;
223 else if (strcmp("com.apple.ResourceFork",what)==0)
224 lookForId=2;
225 else if (strcmp("com.apple.DataFork",what)==0)
226 lookForId=1;
227 if (lookForId==0) return 0;
228
229 try {
230 inp->seek(0, InputStream::SK_SET);
231 auto magicNumber = long(inp->readU32());
232 if (magicNumber != 0x00051600 && magicNumber != 0x00051607)
233 return 0;
234 auto version = long(inp->readU32());
235 if (version != 0x20000) {
236 MWAW_DEBUG_MSG(("XAttr::unMacMIME: unknown version: %lx\n", static_cast<long unsigned int>(version)));
237 return 0;
238 }
239 inp->seek(16, InputStream::SK_CUR); // filename
240 auto numEntries = long(inp->readU16());
241 if (inp->atEOS() || numEntries <= 0) {
242 MWAW_DEBUG_MSG(("XAttr::unMacMIME: can not read number of entries\n"));
243 return 0;
244 }
245 for (long i = 0; i < numEntries; i++) {
246 long pos = inp->tell();
247 auto wh = static_cast<int>(inp->readU32());
248 if (wh <= 0 || wh >= 16 || inp->atEOS()) {
249 MWAW_DEBUG_MSG(("XAttr::unMacMIME: find unknown id: %d\n", wh));
250 return 0;
251 }
252 MWAW_DEBUG_MSG(("XAttr::unMacMIME: find entry %d\n", wh));
253 if (wh != lookForId) {
254 inp->seek(8, InputStream::SK_CUR);
255 continue;
256 }
257 auto entryPos = long(inp->readU32());
258 auto entrySize = static_cast<unsigned long>(inp->readU32());
259 if (entrySize == 0) {
260 MWAW_DEBUG_MSG(("XAttr::unMacMIME: entry %d is empty\n", wh));
261 return 0;
262 }
263 if (entryPos <= pos) {
264 MWAW_DEBUG_MSG(("XAttr::unMacMIME: find bad entry pos\n"));
265 return 0;
266 }
267 /* try to read the data */
268 inp->seek(entryPos, InputStream::SK_SET);
269 if (inp->tell() != entryPos) {
270 MWAW_DEBUG_MSG(("XAttr::unMacMIME: can not seek entry pos %lx\n", static_cast<long unsigned int>(entryPos)));
271 return 0;
272 }
273 unsigned long numBytesRead = 0;
274 const unsigned char *data = inp->read(entrySize, numBytesRead);
275 if (numBytesRead != entrySize || !data) {
276 MWAW_DEBUG_MSG(("XAttr::unMacMIME: can not read %lX byte\n", static_cast<long unsigned int>(entryPos)));
277 return 0;
278 }
279 return new StringStream(data,static_cast<unsigned long>(entrySize));
280 }
281 }
282 catch (...) {
283 return 0;
284 }
285 return 0;
286 }
287 }
288 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: