1 // UdfHandler.cpp
2
3 #include "StdAfx.h"
4
5 #include "../../../Common/ComTry.h"
6
7 #include "../../../Windows/PropVariant.h"
8 #include "../../../Windows/TimeUtils.h"
9
10 #include "../../Common/LimitedStreams.h"
11 #include "../../Common/ProgressUtils.h"
12 #include "../../Common/RegisterArc.h"
13 #include "../../Common/StreamObjects.h"
14
15 #include "../../Compress/CopyCoder.h"
16
17 #include "UdfHandler.h"
18
19 namespace NArchive {
20 namespace NUdf {
21
UdfTimeToFileTime(const CTime & t,NWindows::NCOM::CPropVariant & prop)22 static void UdfTimeToFileTime(const CTime &t, NWindows::NCOM::CPropVariant &prop)
23 {
24 UInt64 numSecs;
25 const Byte *d = t.Data;
26 if (!NWindows::NTime::GetSecondsSince1601(t.GetYear(), d[4], d[5], d[6], d[7], d[8], numSecs))
27 return;
28 if (t.IsLocal())
29 numSecs = (UInt64)((Int64)numSecs - (Int64)((Int32)t.GetMinutesOffset() * 60));
30 const UInt32 m0 = d[9];
31 const UInt32 m1 = d[10];
32 const UInt32 m2 = d[11];
33 unsigned numDigits = 0;
34 UInt64 v = numSecs * 10000000;
35 if (m0 < 100 && m1 < 100 && m2 < 100)
36 {
37 v += m0 * 100000 + m1 * 1000 + m2 * 10;
38 numDigits = 6;
39 }
40 prop.SetAsTimeFrom_Ft64_Prec(v, k_PropVar_TimePrec_Base + numDigits);
41 }
42
43 static const Byte kProps[] =
44 {
45 kpidPath,
46 kpidIsDir,
47 kpidSize,
48 kpidPackSize,
49 kpidMTime,
50 kpidATime,
51 kpidCTime,
52 kpidChangeTime,
53 // kpidUserId,
54 // kpidGroupId,
55 // kpidPosixAttrib,
56 kpidLinks
57 };
58
59 static const Byte kArcProps[] =
60 {
61 kpidUnpackVer,
62 kpidClusterSize,
63 kpidSectorSize,
64 kpidCTime,
65 kpidMTime,
66 kpidComment
67 };
68
69 IMP_IInArchive_Props
70 IMP_IInArchive_ArcProps
71
Z7_COM7F_IMF(CHandler::GetArchiveProperty (PROPID propID,PROPVARIANT * value))72 Z7_COM7F_IMF(CHandler::GetArchiveProperty(PROPID propID, PROPVARIANT *value))
73 {
74 COM_TRY_BEGIN
75 NWindows::NCOM::CPropVariant prop;
76 switch (propID)
77 {
78 case kpidPhySize: prop = _archive.PhySize; break;
79
80 case kpidUnpackVer:
81 {
82 if (_archive.LogVols.Size() == 1)
83 {
84 UString s;
85 const CLogVol &vol = _archive.LogVols[0];
86 vol.DomainId.AddUdfVersionTo(s);
87 if (!s.IsEmpty())
88 prop = s;
89 }
90 break;
91 }
92 case kpidComment:
93 {
94 UString comment = _archive.GetComment();
95 if (!comment.IsEmpty())
96 prop = comment;
97 break;
98 }
99
100 case kpidClusterSize:
101 if (_archive.LogVols.Size() > 0)
102 {
103 UInt32 blockSize = _archive.LogVols[0].BlockSize;
104 unsigned i;
105 for (i = 1; i < _archive.LogVols.Size(); i++)
106 if (_archive.LogVols[i].BlockSize != blockSize)
107 break;
108 if (i == _archive.LogVols.Size())
109 prop = blockSize;
110 }
111 break;
112
113 case kpidSectorSize: prop = ((UInt32)1 << _archive.SecLogSize); break;
114
115 case kpidCTime:
116 if (_archive.LogVols.Size() == 1)
117 {
118 const CLogVol &vol = _archive.LogVols[0];
119 if (vol.FileSets.Size() >= 1)
120 UdfTimeToFileTime(vol.FileSets[0].RecordingTime, prop);
121 }
122 break;
123 case kpidMTime:
124 if (_archive.PrimeVols.Size() == 1)
125 {
126 const CPrimeVol &pv = _archive.PrimeVols[0];
127 UdfTimeToFileTime(pv.RecordingTime, prop);
128 }
129 break;
130
131 case kpidErrorFlags:
132 {
133 UInt32 v = 0;
134 if (!_archive.IsArc) v |= kpv_ErrorFlags_IsNotArc;
135 if (_archive.Unsupported) v |= kpv_ErrorFlags_UnsupportedFeature;
136 if (_archive.UnexpectedEnd) v |= kpv_ErrorFlags_UnexpectedEnd;
137 if (_archive.NoEndAnchor) v |= kpv_ErrorFlags_HeadersError;
138 prop = v;
139 break;
140 }
141 }
142 prop.Detach(value);
143 return S_OK;
144 COM_TRY_END
145 }
146
147 class CProgressImp Z7_final: public CProgressVirt
148 {
149 CMyComPtr<IArchiveOpenCallback> _callback;
150 UInt64 _numFiles;
151 UInt64 _numBytes;
152 public:
153 HRESULT SetTotal(UInt64 numBytes) Z7_override;
154 HRESULT SetCompleted(UInt64 numFiles, UInt64 numBytes) Z7_override;
155 HRESULT SetCompleted() Z7_override;
CProgressImp(IArchiveOpenCallback * callback)156 CProgressImp(IArchiveOpenCallback *callback): _callback(callback), _numFiles(0), _numBytes(0) {}
157 };
158
SetTotal(UInt64 numBytes)159 HRESULT CProgressImp::SetTotal(UInt64 numBytes)
160 {
161 if (_callback)
162 return _callback->SetTotal(NULL, &numBytes);
163 return S_OK;
164 }
165
SetCompleted(UInt64 numFiles,UInt64 numBytes)166 HRESULT CProgressImp::SetCompleted(UInt64 numFiles, UInt64 numBytes)
167 {
168 _numFiles = numFiles;
169 _numBytes = numBytes;
170 return SetCompleted();
171 }
172
SetCompleted()173 HRESULT CProgressImp::SetCompleted()
174 {
175 if (_callback)
176 return _callback->SetCompleted(&_numFiles, &_numBytes);
177 return S_OK;
178 }
179
Z7_COM7F_IMF(CHandler::Open (IInStream * stream,const UInt64 *,IArchiveOpenCallback * callback))180 Z7_COM7F_IMF(CHandler::Open(IInStream *stream, const UInt64 *, IArchiveOpenCallback *callback))
181 {
182 COM_TRY_BEGIN
183 {
184 Close();
185 CProgressImp progressImp(callback);
186 RINOK(_archive.Open(stream, &progressImp))
187 bool showVolName = (_archive.LogVols.Size() > 1);
188 FOR_VECTOR (volIndex, _archive.LogVols)
189 {
190 const CLogVol &vol = _archive.LogVols[volIndex];
191 bool showFileSetName = (vol.FileSets.Size() > 1);
192 // showFileSetName = true; // for debug
193 FOR_VECTOR (fsIndex, vol.FileSets)
194 {
195 const CFileSet &fs = vol.FileSets[fsIndex];
196 for (unsigned i = ((showVolName || showFileSetName) ? 0 : 1); i < fs.Refs.Size(); i++)
197 {
198 CRef2 ref2;
199 ref2.Vol = volIndex;
200 ref2.Fs = fsIndex;
201 ref2.Ref = i;
202 _refs2.Add(ref2);
203 }
204 }
205 }
206 _inStream = stream;
207 }
208 return S_OK;
209 COM_TRY_END
210 }
211
Z7_COM7F_IMF(CHandler::Close ())212 Z7_COM7F_IMF(CHandler::Close())
213 {
214 _inStream.Release();
215 _archive.Clear();
216 _refs2.Clear();
217 return S_OK;
218 }
219
Z7_COM7F_IMF(CHandler::GetNumberOfItems (UInt32 * numItems))220 Z7_COM7F_IMF(CHandler::GetNumberOfItems(UInt32 *numItems))
221 {
222 *numItems = _refs2.Size();
223 return S_OK;
224 }
225
Z7_COM7F_IMF(CHandler::GetProperty (UInt32 index,PROPID propID,PROPVARIANT * value))226 Z7_COM7F_IMF(CHandler::GetProperty(UInt32 index, PROPID propID, PROPVARIANT *value))
227 {
228 COM_TRY_BEGIN
229 NWindows::NCOM::CPropVariant prop;
230 {
231 const CRef2 &ref2 = _refs2[index];
232 const CLogVol &vol = _archive.LogVols[ref2.Vol];
233 const CRef &ref = vol.FileSets[ref2.Fs].Refs[ref2.Ref];
234 const CFile &file = _archive.Files[ref.FileIndex];
235 const CItem &item = _archive.Items[file.ItemIndex];
236 switch (propID)
237 {
238 case kpidPath: prop = _archive.GetItemPath(ref2.Vol, ref2.Fs, ref2.Ref,
239 _archive.LogVols.Size() > 1, vol.FileSets.Size() > 1); break;
240 case kpidIsDir: prop = item.IsDir(); break;
241 case kpidSize: if (!item.IsDir()) prop = (UInt64)item.Size; break;
242 case kpidPackSize: if (!item.IsDir()) prop = (UInt64)item.NumLogBlockRecorded * vol.BlockSize; break;
243 case kpidMTime: UdfTimeToFileTime(item.MTime, prop); break;
244 case kpidATime: UdfTimeToFileTime(item.ATime, prop); break;
245 case kpidCTime:
246 if (item.IsExtended)
247 UdfTimeToFileTime(item.CreateTime, prop);
248 break;
249 case kpidChangeTime: UdfTimeToFileTime(item.AttribTime, prop); break;
250 // case kpidUserId: prop = item.Uid; break;
251 // case kpidGroupId: prop = item.Gid; break;
252 // case kpidPosixAttrib: prop = (UInt32)item.Permissions; break;
253 case kpidLinks: prop = (UInt32)item.FileLinkCount; break;
254 }
255 }
256 prop.Detach(value);
257 return S_OK;
258 COM_TRY_END
259 }
260
Z7_COM7F_IMF(CHandler::GetStream (UInt32 index,ISequentialInStream ** stream))261 Z7_COM7F_IMF(CHandler::GetStream(UInt32 index, ISequentialInStream **stream))
262 {
263 *stream = NULL;
264
265 const CRef2 &ref2 = _refs2[index];
266 const CLogVol &vol = _archive.LogVols[ref2.Vol];
267 const CRef &ref = vol.FileSets[ref2.Fs].Refs[ref2.Ref];
268 const CFile &file = _archive.Files[ref.FileIndex];
269 const CItem &item = _archive.Items[file.ItemIndex];
270 UInt64 size = item.Size;
271
272 if (!item.IsRecAndAlloc() || !item.CheckChunkSizes() || ! _archive.CheckItemExtents(ref2.Vol, item))
273 return E_NOTIMPL;
274
275 if (item.IsInline)
276 {
277 Create_BufInStream_WithNewBuffer(item.InlineData, stream);
278 return S_OK;
279 }
280
281 CExtentsStream *extentStreamSpec = new CExtentsStream();
282 CMyComPtr<ISequentialInStream> extentStream = extentStreamSpec;
283
284 extentStreamSpec->Stream = _inStream;
285
286 UInt64 virtOffset = 0;
287 FOR_VECTOR (extentIndex, item.Extents)
288 {
289 const CMyExtent &extent = item.Extents[extentIndex];
290 UInt32 len = extent.GetLen();
291 if (len == 0)
292 continue;
293 if (size < len)
294 return S_FALSE;
295
296 const unsigned partitionIndex = vol.PartitionMaps[extent.PartitionRef].PartitionIndex;
297 UInt32 logBlockNumber = extent.Pos;
298 const CPartition &partition = _archive.Partitions[partitionIndex];
299 UInt64 offset = ((UInt64)partition.Pos << _archive.SecLogSize) +
300 (UInt64)logBlockNumber * vol.BlockSize;
301
302 CSeekExtent se;
303 se.Phy = offset;
304 se.Virt = virtOffset;
305 virtOffset += len;
306 extentStreamSpec->Extents.Add(se);
307
308 size -= len;
309 }
310 if (size != 0)
311 return S_FALSE;
312 CSeekExtent se;
313 se.Phy = 0;
314 se.Virt = virtOffset;
315 extentStreamSpec->Extents.Add(se);
316 extentStreamSpec->Init();
317 *stream = extentStream.Detach();
318 return S_OK;
319 }
320
Z7_COM7F_IMF(CHandler::Extract (const UInt32 * indices,UInt32 numItems,Int32 testMode,IArchiveExtractCallback * extractCallback))321 Z7_COM7F_IMF(CHandler::Extract(const UInt32 *indices, UInt32 numItems,
322 Int32 testMode, IArchiveExtractCallback *extractCallback))
323 {
324 COM_TRY_BEGIN
325 const bool allFilesMode = (numItems == (UInt32)(Int32)-1);
326 if (allFilesMode)
327 numItems = _refs2.Size();
328 if (numItems == 0)
329 return S_OK;
330 UInt64 totalSize = 0;
331 UInt32 i;
332
333 for (i = 0; i < numItems; i++)
334 {
335 const UInt32 index = (allFilesMode ? i : indices[i]);
336 const CRef2 &ref2 = _refs2[index];
337 const CRef &ref = _archive.LogVols[ref2.Vol].FileSets[ref2.Fs].Refs[ref2.Ref];
338 const CFile &file = _archive.Files[ref.FileIndex];
339 const CItem &item = _archive.Items[file.ItemIndex];
340 if (!item.IsDir())
341 totalSize += item.Size;
342 }
343 RINOK(extractCallback->SetTotal(totalSize))
344
345 UInt64 currentTotalSize = 0;
346
347 CMyComPtr2_Create<ICompressProgressInfo, CLocalProgress> lps;
348 lps->Init(extractCallback, false);
349 CMyComPtr2_Create<ICompressCoder, NCompress::CCopyCoder> copyCoder;
350 CMyComPtr2_Create<ISequentialOutStream, CLimitedSequentialOutStream> outStream;
351
352 for (i = 0;; i++)
353 {
354 lps->InSize = lps->OutSize = currentTotalSize;
355 RINOK(lps->SetCur())
356 if (i >= numItems)
357 break;
358 CMyComPtr<ISequentialOutStream> realOutStream;
359 const Int32 askMode = testMode ?
360 NExtract::NAskMode::kTest :
361 NExtract::NAskMode::kExtract;
362 const UInt32 index = allFilesMode ? i : indices[i];
363
364 RINOK(extractCallback->GetStream(index, &realOutStream, askMode))
365
366 const CRef2 &ref2 = _refs2[index];
367 const CRef &ref = _archive.LogVols[ref2.Vol].FileSets[ref2.Fs].Refs[ref2.Ref];
368 const CFile &file = _archive.Files[ref.FileIndex];
369 const CItem &item = _archive.Items[file.ItemIndex];
370
371 if (item.IsDir())
372 {
373 RINOK(extractCallback->PrepareOperation(askMode))
374 RINOK(extractCallback->SetOperationResult(NExtract::NOperationResult::kOK))
375 continue;
376 }
377 currentTotalSize += item.Size;
378
379 if (!testMode && !realOutStream)
380 continue;
381
382 RINOK(extractCallback->PrepareOperation(askMode))
383 outStream->SetStream(realOutStream);
384 realOutStream.Release();
385 outStream->Init(item.Size);
386 Int32 opRes;
387 {
388 CMyComPtr<ISequentialInStream> udfInStream;
389 const HRESULT res = GetStream(index, &udfInStream);
390 if (res == E_NOTIMPL)
391 opRes = NExtract::NOperationResult::kUnsupportedMethod;
392 else if (res != S_OK)
393 opRes = NExtract::NOperationResult::kDataError;
394 else
395 {
396 RINOK(copyCoder.Interface()->Code(udfInStream, outStream, NULL, NULL, lps))
397 opRes = outStream->IsFinishedOK() ?
398 NExtract::NOperationResult::kOK:
399 NExtract::NOperationResult::kDataError;
400 }
401 }
402 outStream->ReleaseStream();
403 RINOK(extractCallback->SetOperationResult(opRes))
404 }
405 return S_OK;
406 COM_TRY_END
407 }
408
409 static const UInt32 kIsoStartPos = 0x8000;
410
411 // 5, { 0, 'N', 'S', 'R', '0' },
412
413 static const Byte k_Signature[] =
414 {
415 8, 0, 'B', 'E', 'A', '0', '1', 1, 0,
416 6, 1, 'C', 'D', '0', '0', '1'
417 };
418
419 REGISTER_ARC_I(
420 "Udf", "udf iso img", NULL, 0xE0,
421 k_Signature,
422 kIsoStartPos,
423 NArcInfoFlags::kMultiSignature |
424 NArcInfoFlags::kStartOpen,
425 IsArc_Udf)
426
427 }}
428