COM.cs 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143
  1. namespace SevenZip
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Globalization;
  6. using System.IO;
  7. using System.Runtime.InteropServices;
  8. #if NET472 || NETSTANDARD2_0
  9. using System.Security.Permissions;
  10. #endif
  11. using FILETIME = System.Runtime.InteropServices.ComTypes.FILETIME;
  12. #if UNMANAGED
  13. // ReSharper disable file ConvertToAutoProperty - For UWP compatibility.
  14. /// <summary>
  15. /// The structure to fix x64 and x32 variant size mismatch.
  16. /// </summary>
  17. [StructLayout(LayoutKind.Sequential)]
  18. internal struct PropArray
  19. {
  20. readonly uint _cElems;
  21. readonly IntPtr _pElems;
  22. }
  23. /// <summary>
  24. /// COM VARIANT structure with special interface routines.
  25. /// </summary>
  26. [StructLayout(LayoutKind.Explicit)]
  27. internal struct PropVariant
  28. {
  29. [FieldOffset(0)] private ushort _vt;
  30. /// <summary>
  31. /// FILETIME variant value.
  32. /// </summary>
  33. [FieldOffset(8)] private readonly FILETIME _fileTime;
  34. /// <summary>
  35. /// The PropArray instance to fix the variant size on x64 bit systems.
  36. /// </summary>
  37. [FieldOffset(8)]
  38. private readonly PropArray _propArray;
  39. [FieldOffset(8)] private IntPtr _value;
  40. [FieldOffset(8)] private uint _uInt32Value;
  41. [FieldOffset(8)] private int _int32Value;
  42. [FieldOffset(8)] private long _int64Value;
  43. [FieldOffset(8)] private ulong _uInt64Value;
  44. /// <summary>
  45. /// Gets or sets variant type.
  46. /// </summary>
  47. public VarEnum VarType
  48. {
  49. private get
  50. {
  51. return (VarEnum) _vt;
  52. }
  53. set
  54. {
  55. _vt = (ushort) value;
  56. }
  57. }
  58. /// <summary>
  59. /// Gets or sets the pointer value of the COM variant
  60. /// </summary>
  61. public IntPtr Value
  62. {
  63. get => _value;
  64. set => _value = value;
  65. }
  66. /// <summary>
  67. /// Gets or sets the UInt32 value of the COM variant.
  68. /// </summary>
  69. public uint UInt32Value
  70. {
  71. get => _uInt32Value;
  72. set => _uInt32Value = value;
  73. }
  74. /// <summary>
  75. /// Gets or sets the UInt32 value of the COM variant.
  76. /// </summary>
  77. public int Int32Value
  78. {
  79. get => _int32Value;
  80. set => _int32Value = value;
  81. }
  82. /// <summary>
  83. /// Gets or sets the Int64 value of the COM variant
  84. /// </summary>
  85. public long Int64Value
  86. {
  87. get => _int64Value;
  88. set => _int64Value = value;
  89. }
  90. /// <summary>
  91. /// Gets or sets the UInt64 value of the COM variant
  92. /// </summary>
  93. public ulong UInt64Value
  94. {
  95. get => _uInt64Value;
  96. set => _uInt64Value = value;
  97. }
  98. /// <summary>
  99. /// Gets the object for this PropVariant.
  100. /// </summary>
  101. /// <returns></returns>
  102. public object Object
  103. {
  104. get
  105. {
  106. #if NET472 || NETSTANDARD2_0
  107. var sp = new SecurityPermission(SecurityPermissionFlag.UnmanagedCode);
  108. sp.Demand();
  109. #endif
  110. switch (VarType)
  111. {
  112. case VarEnum.VT_BSTR:
  113. return Marshal.PtrToStringBSTR(Value);
  114. case VarEnum.VT_EMPTY:
  115. return null;
  116. case VarEnum.VT_FILETIME:
  117. try
  118. {
  119. return DateTime.FromFileTime(Int64Value);
  120. }
  121. catch (ArgumentOutOfRangeException)
  122. {
  123. return DateTime.MinValue;
  124. }
  125. default:
  126. var propHandle = GCHandle.Alloc(this, GCHandleType.Pinned);
  127. try
  128. {
  129. return Marshal.GetObjectForNativeVariant(propHandle.AddrOfPinnedObject());
  130. }
  131. catch (NotSupportedException)
  132. {
  133. switch (VarType)
  134. {
  135. case VarEnum.VT_UI8:
  136. return UInt64Value;
  137. case VarEnum.VT_UI4:
  138. return UInt32Value;
  139. case VarEnum.VT_I8:
  140. return Int64Value;
  141. case VarEnum.VT_I4:
  142. return Int32Value;
  143. default:
  144. return 0;
  145. }
  146. }
  147. finally
  148. {
  149. propHandle.Free();
  150. }
  151. }
  152. }
  153. }
  154. /// <summary>
  155. /// Determines whether the specified System.Object is equal to the current PropVariant.
  156. /// </summary>
  157. /// <param name="obj">The System.Object to compare with the current PropVariant.</param>
  158. /// <returns>true if the specified System.Object is equal to the current PropVariant; otherwise, false.</returns>
  159. public override bool Equals(object obj)
  160. {
  161. return obj is PropVariant variant && Equals(variant);
  162. }
  163. /// <summary>
  164. /// Determines whether the specified PropVariant is equal to the current PropVariant.
  165. /// </summary>
  166. /// <param name="afi">The PropVariant to compare with the current PropVariant.</param>
  167. /// <returns>true if the specified PropVariant is equal to the current PropVariant; otherwise, false.</returns>
  168. private bool Equals(PropVariant afi)
  169. {
  170. if (afi.VarType != VarType)
  171. {
  172. return false;
  173. }
  174. if (VarType != VarEnum.VT_BSTR)
  175. {
  176. return afi.Int64Value == Int64Value;
  177. }
  178. return afi.Value == Value;
  179. }
  180. /// <summary>
  181. /// Serves as a hash function for a particular type.
  182. /// </summary>
  183. /// <returns> A hash code for the current PropVariant.</returns>
  184. public override int GetHashCode()
  185. {
  186. return Value.GetHashCode();
  187. }
  188. /// <summary>
  189. /// Returns a System.String that represents the current PropVariant.
  190. /// </summary>
  191. /// <returns>A System.String that represents the current PropVariant.</returns>
  192. public override string ToString()
  193. {
  194. return "[" + Value + "] " + Int64Value.ToString(CultureInfo.CurrentCulture);
  195. }
  196. /// <summary>
  197. /// Determines whether the specified PropVariant instances are considered equal.
  198. /// </summary>
  199. /// <param name="afi1">The first PropVariant to compare.</param>
  200. /// <param name="afi2">The second PropVariant to compare.</param>
  201. /// <returns>true if the specified PropVariant instances are considered equal; otherwise, false.</returns>
  202. public static bool operator ==(PropVariant afi1, PropVariant afi2)
  203. {
  204. return afi1.Equals(afi2);
  205. }
  206. /// <summary>
  207. /// Determines whether the specified PropVariant instances are not considered equal.
  208. /// </summary>
  209. /// <param name="afi1">The first PropVariant to compare.</param>
  210. /// <param name="afi2">The second PropVariant to compare.</param>
  211. /// <returns>true if the specified PropVariant instances are not considered equal; otherwise, false.</returns>
  212. public static bool operator !=(PropVariant afi1, PropVariant afi2)
  213. {
  214. return !afi1.Equals(afi2);
  215. }
  216. }
  217. /// <summary>
  218. /// Stores file extraction modes.
  219. /// </summary>
  220. internal enum AskMode
  221. {
  222. /// <summary>
  223. /// Extraction mode
  224. /// </summary>
  225. Extract = 0,
  226. /// <summary>
  227. /// Test mode
  228. /// </summary>
  229. Test,
  230. /// <summary>
  231. /// Skip mode
  232. /// </summary>
  233. Skip
  234. }
  235. /// <summary>
  236. /// Stores operation result values
  237. /// </summary>
  238. public enum OperationResult
  239. {
  240. /// <summary>
  241. /// Success
  242. /// </summary>
  243. Ok = 0,
  244. /// <summary>
  245. /// Method is unsupported
  246. /// </summary>
  247. UnsupportedMethod,
  248. /// <summary>
  249. /// Data error has occurred
  250. /// </summary>
  251. DataError,
  252. /// <summary>
  253. /// CrcError has occurred
  254. /// </summary>
  255. CrcError,
  256. /// <summary>
  257. /// File is unavailable
  258. /// </summary>
  259. Unavailable,
  260. /// <summary>
  261. /// Unexpected end of file
  262. /// </summary>
  263. UnexpectedEnd,
  264. /// <summary>
  265. /// Data after end of archive
  266. /// </summary>
  267. DataAfterEnd,
  268. /// <summary>
  269. /// File is not archive
  270. /// </summary>
  271. IsNotArc,
  272. /// <summary>
  273. /// Archive headers error
  274. /// </summary>
  275. HeadersError,
  276. /// <summary>
  277. /// Wrong password
  278. /// </summary>
  279. WrongPassword
  280. }
  281. /// <summary>
  282. /// Codes of item properties
  283. /// </summary>
  284. internal enum ItemPropId : uint
  285. {
  286. /// <summary>
  287. /// No property
  288. /// </summary>
  289. NoProperty = 0,
  290. MainSubfile,
  291. /// <summary>
  292. /// Handler item index
  293. /// </summary>
  294. HandlerItemIndex,
  295. /// <summary>
  296. /// Item path
  297. /// </summary>
  298. Path,
  299. /// <summary>
  300. /// Item name
  301. /// </summary>
  302. Name,
  303. /// <summary>
  304. /// Item extension
  305. /// </summary>
  306. Extension,
  307. /// <summary>
  308. /// true if the item is a folder; otherwise, false
  309. /// </summary>
  310. IsDirectory,
  311. /// <summary>
  312. /// Item size
  313. /// </summary>
  314. Size,
  315. /// <summary>
  316. /// Item packed sise; usually absent
  317. /// </summary>
  318. PackedSize,
  319. /// <summary>
  320. /// Item attributes; usually absent
  321. /// </summary>
  322. Attributes,
  323. /// <summary>
  324. /// Item creation time; usually absent
  325. /// </summary>
  326. CreationTime,
  327. /// <summary>
  328. /// Item last access time; usually absent
  329. /// </summary>
  330. LastAccessTime,
  331. /// <summary>
  332. /// Item last write time
  333. /// </summary>
  334. LastWriteTime,
  335. /// <summary>
  336. /// true if the item is solid; otherwise, false
  337. /// </summary>
  338. Solid,
  339. /// <summary>
  340. /// true if the item is commented; otherwise, false
  341. /// </summary>
  342. Commented,
  343. /// <summary>
  344. /// true if the item is encrypted; otherwise, false
  345. /// </summary>
  346. Encrypted,
  347. /// <summary>
  348. /// (?)
  349. /// </summary>
  350. SplitBefore,
  351. /// <summary>
  352. /// (?)
  353. /// </summary>
  354. SplitAfter,
  355. /// <summary>
  356. /// Dictionary size(?)
  357. /// </summary>
  358. DictionarySize,
  359. /// <summary>
  360. /// Item CRC checksum
  361. /// </summary>
  362. Crc,
  363. /// <summary>
  364. /// Item type(?)
  365. /// </summary>
  366. Type,
  367. /// <summary>
  368. /// (?)
  369. /// </summary>
  370. IsAnti,
  371. /// <summary>
  372. /// Compression method
  373. /// </summary>
  374. Method,
  375. /// <summary>
  376. /// (?); usually absent
  377. /// </summary>
  378. HostOS,
  379. /// <summary>
  380. /// Item file system; usually absent
  381. /// </summary>
  382. FileSystem,
  383. /// <summary>
  384. /// Item user(?); usually absent
  385. /// </summary>
  386. User,
  387. /// <summary>
  388. /// Item group(?); usually absent
  389. /// </summary>
  390. Group,
  391. /// <summary>
  392. /// Bloack size(?)
  393. /// </summary>
  394. Block,
  395. /// <summary>
  396. /// Item comment; usually absent
  397. /// </summary>
  398. Comment,
  399. /// <summary>
  400. /// Item position
  401. /// </summary>
  402. Position,
  403. /// <summary>
  404. /// Item prefix(?)
  405. /// </summary>
  406. Prefix,
  407. /// <summary>
  408. /// Number of subdirectories
  409. /// </summary>
  410. NumSubDirs,
  411. /// <summary>
  412. /// Numbers of subfiles
  413. /// </summary>
  414. NumSubFiles,
  415. /// <summary>
  416. /// The archive legacy unpacker version
  417. /// </summary>
  418. UnpackVersion,
  419. /// <summary>
  420. /// Volume(?)
  421. /// </summary>
  422. Volume,
  423. /// <summary>
  424. /// Is a volume
  425. /// </summary>
  426. IsVolume,
  427. /// <summary>
  428. /// Offset value(?)
  429. /// </summary>
  430. Offset,
  431. /// <summary>
  432. /// Links(?)
  433. /// </summary>
  434. Links,
  435. /// <summary>
  436. /// Number of blocks
  437. /// </summary>
  438. NumBlocks,
  439. /// <summary>
  440. /// Number of volumes(?)
  441. /// </summary>
  442. NumVolumes,
  443. /// <summary>
  444. /// Time type(?)
  445. /// </summary>
  446. TimeType,
  447. /// <summary>
  448. /// 64-bit(?)
  449. /// </summary>
  450. Bit64,
  451. /// <summary>
  452. /// BigEndian
  453. /// </summary>
  454. BigEndian,
  455. /// <summary>
  456. /// Cpu(?)
  457. /// </summary>
  458. Cpu,
  459. /// <summary>
  460. /// Physical archive size
  461. /// </summary>
  462. PhysicalSize,
  463. /// <summary>
  464. /// Headers size
  465. /// </summary>
  466. HeadersSize,
  467. /// <summary>
  468. /// Archive checksum
  469. /// </summary>
  470. Checksum,
  471. Characts,
  472. Va,
  473. Id,
  474. ShortName,
  475. CreatorApp,
  476. SectorSize,
  477. PosixAttrib,
  478. SymLink,
  479. Error,
  480. /// <summary>
  481. /// (?)
  482. /// </summary>
  483. TotalSize,
  484. /// <summary>
  485. /// (?)
  486. /// </summary>
  487. FreeSpace,
  488. /// <summary>
  489. /// Cluster size(?)
  490. /// </summary>
  491. ClusterSize,
  492. /// <summary>
  493. /// Volume name(?)
  494. /// </summary>
  495. VolumeName,
  496. /// <summary>
  497. /// Local item name(?); usually absent
  498. /// </summary>
  499. LocalName,
  500. /// <summary>
  501. /// (?)
  502. /// </summary>
  503. Provider,
  504. NtSecure,
  505. IsAltStream,
  506. IsAux,
  507. IsDeleted,
  508. IsTree,
  509. Sha1,
  510. Sha256,
  511. ErrorType,
  512. NumErrors,
  513. ErrorFlags,
  514. WarningFlags,
  515. Warning,
  516. NumStreams,
  517. NumAltStreams,
  518. AltStreamsSize,
  519. VirtualSize,
  520. UnpackSize,
  521. TotalPhySize,
  522. /// <summary>
  523. /// Index of the Volume
  524. /// </summary>
  525. VolumeIndex,
  526. SubType,
  527. ShortComment,
  528. CodePage,
  529. IsNotArcType,
  530. PhySizeCantBeDetected,
  531. ZerosTailIsAllowed,
  532. TailSize,
  533. EmbeddedStubSize,
  534. NtReparse,
  535. HardLink,
  536. INode,
  537. StreamId,
  538. ReadOnly,
  539. OutName,
  540. CopyLink,
  541. NumDefined,
  542. /// <summary>
  543. /// User defined property; usually absent
  544. /// </summary>
  545. UserDefined = 0x10000
  546. }
  547. /// <summary>
  548. /// PropId string names dictionary wrapper.
  549. /// </summary>
  550. internal static class PropIdToName
  551. {
  552. /// <summary>
  553. /// PropId string names
  554. /// </summary>
  555. public static readonly Dictionary<ItemPropId, string> PropIdNames =
  556. #region Initialization
  557. new Dictionary<ItemPropId, string>(46)
  558. {
  559. {ItemPropId.Path, "Path"},
  560. {ItemPropId.Name, "Name"},
  561. {ItemPropId.IsDirectory, "Folder"},
  562. {ItemPropId.Size, "Size"},
  563. {ItemPropId.PackedSize, "Packed Size"},
  564. {ItemPropId.Attributes, "Attributes"},
  565. {ItemPropId.CreationTime, "Created"},
  566. {ItemPropId.LastAccessTime, "Accessed"},
  567. {ItemPropId.LastWriteTime, "Modified"},
  568. {ItemPropId.Solid, "Solid"},
  569. {ItemPropId.Commented, "Commented"},
  570. {ItemPropId.Encrypted, "Encrypted"},
  571. {ItemPropId.SplitBefore, "Split Before"},
  572. {ItemPropId.SplitAfter, "Split After"},
  573. {
  574. ItemPropId.DictionarySize,
  575. "Dictionary Size"
  576. },
  577. {ItemPropId.Crc, "CRC"},
  578. {ItemPropId.Type, "Type"},
  579. {ItemPropId.IsAnti, "Anti"},
  580. {ItemPropId.Method, "Method"},
  581. {ItemPropId.HostOS, "Host OS"},
  582. {ItemPropId.FileSystem, "File System"},
  583. {ItemPropId.User, "User"},
  584. {ItemPropId.Group, "Group"},
  585. {ItemPropId.Block, "Block"},
  586. {ItemPropId.Comment, "Comment"},
  587. {ItemPropId.Position, "Position"},
  588. {ItemPropId.Prefix, "Prefix"},
  589. {
  590. ItemPropId.NumSubDirs,
  591. "Number of subdirectories"
  592. },
  593. {
  594. ItemPropId.NumSubFiles,
  595. "Number of subfiles"
  596. },
  597. {
  598. ItemPropId.UnpackVersion,
  599. "Unpacker version"
  600. },
  601. {ItemPropId.VolumeIndex, "VolumeIndex"},
  602. {ItemPropId.Volume, "Volume"},
  603. {ItemPropId.IsVolume, "IsVolume"},
  604. {ItemPropId.Offset, "Offset"},
  605. {ItemPropId.Links, "Links"},
  606. {
  607. ItemPropId.NumBlocks,
  608. "Number of blocks"
  609. },
  610. {
  611. ItemPropId.NumVolumes,
  612. "Number of volumes"
  613. },
  614. {ItemPropId.TimeType, "Time type"},
  615. {ItemPropId.Bit64, "64-bit"},
  616. {ItemPropId.BigEndian, "Big endian"},
  617. {ItemPropId.Cpu, "CPU"},
  618. {
  619. ItemPropId.PhysicalSize,
  620. "Physical Size"
  621. },
  622. {ItemPropId.HeadersSize, "Headers Size"},
  623. {ItemPropId.Checksum, "Checksum"},
  624. {ItemPropId.FreeSpace, "Free Space"},
  625. {ItemPropId.ClusterSize, "Cluster Size"}
  626. };
  627. #endregion
  628. }
  629. /// <summary>
  630. /// 7-zip IArchiveOpenCallback imported interface to handle the opening of an archive.
  631. /// </summary>
  632. [ComImport]
  633. [Guid("23170F69-40C1-278A-0000-000600100000")]
  634. [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
  635. internal interface IArchiveOpenCallback
  636. {
  637. // ref ulong replaced with IntPtr because handlers often pass null value
  638. // read actual value with Marshal.ReadInt64
  639. /// <summary>
  640. /// Sets total data size
  641. /// </summary>
  642. /// <param name="files">Files pointer</param>
  643. /// <param name="bytes">Total size in bytes</param>
  644. void SetTotal(
  645. IntPtr files,
  646. IntPtr bytes);
  647. /// <summary>
  648. /// Sets completed size
  649. /// </summary>
  650. /// <param name="files">Files pointer</param>
  651. /// <param name="bytes">Completed size in bytes</param>
  652. void SetCompleted(
  653. IntPtr files,
  654. IntPtr bytes);
  655. }
  656. /// <summary>
  657. /// 7-zip ICryptoGetTextPassword imported interface to get the archive password.
  658. /// </summary>
  659. [ComImport]
  660. [Guid("23170F69-40C1-278A-0000-000500100000")]
  661. [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
  662. internal interface ICryptoGetTextPassword
  663. {
  664. /// <summary>
  665. /// Gets password for the archive
  666. /// </summary>
  667. /// <param name="password">Password for the archive</param>
  668. /// <returns>Zero if everything is OK</returns>
  669. [PreserveSig]
  670. int CryptoGetTextPassword(
  671. [MarshalAs(UnmanagedType.BStr)] out string password);
  672. }
  673. /// <summary>
  674. /// 7-zip ICryptoGetTextPassword2 imported interface for setting the archive password.
  675. /// </summary>
  676. [ComImport]
  677. [Guid("23170F69-40C1-278A-0000-000500110000")]
  678. [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
  679. internal interface ICryptoGetTextPassword2
  680. {
  681. /// <summary>
  682. /// Sets password for the archive
  683. /// </summary>
  684. /// <param name="passwordIsDefined">Specifies whether archive has a password or not (0 if not)</param>
  685. /// <param name="password">Password for the archive</param>
  686. /// <returns>Zero if everything is OK</returns>
  687. [PreserveSig]
  688. int CryptoGetTextPassword2(
  689. ref int passwordIsDefined,
  690. [MarshalAs(UnmanagedType.BStr)] out string password);
  691. }
  692. /// <summary>
  693. /// 7-zip IArchiveExtractCallback imported interface.
  694. /// </summary>
  695. [ComImport]
  696. [Guid("23170F69-40C1-278A-0000-000600200000")]
  697. [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
  698. internal interface IArchiveExtractCallback
  699. {
  700. /// <summary>
  701. /// Gives the size of the unpacked archive files
  702. /// </summary>
  703. /// <param name="total">Size of the unpacked archive files (in bytes)</param>
  704. void SetTotal(ulong total);
  705. /// <summary>
  706. /// SetCompleted 7-zip function
  707. /// </summary>
  708. /// <param name="completeValue"></param>
  709. void SetCompleted([In] ref ulong completeValue);
  710. /// <summary>
  711. /// Gets the stream for file extraction
  712. /// </summary>
  713. /// <param name="index">File index in the archive file table</param>
  714. /// <param name="outStream">Pointer to the stream</param>
  715. /// <param name="askExtractMode">Extraction mode</param>
  716. /// <returns>S_OK - OK, S_FALSE - skip this file</returns>
  717. [PreserveSig]
  718. int GetStream(
  719. uint index,
  720. [Out, MarshalAs(UnmanagedType.Interface)] out ISequentialOutStream outStream,
  721. AskMode askExtractMode);
  722. /// <summary>
  723. /// PrepareOperation 7-zip function
  724. /// </summary>
  725. /// <param name="askExtractMode">Ask mode</param>
  726. void PrepareOperation(AskMode askExtractMode);
  727. /// <summary>
  728. /// Sets the operation result
  729. /// </summary>
  730. /// <param name="operationResult">The operation result</param>
  731. void SetOperationResult(OperationResult operationResult);
  732. }
  733. /// <summary>
  734. /// 7-zip IArchiveUpdateCallback imported interface.
  735. /// </summary>
  736. [ComImport]
  737. [Guid("23170F69-40C1-278A-0000-000600800000")]
  738. [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
  739. internal interface IArchiveUpdateCallback
  740. {
  741. /// <summary>
  742. /// Gives the size of the unpacked archive files.
  743. /// </summary>
  744. /// <param name="total">Size of the unpacked archive files (in bytes)</param>
  745. void SetTotal(ulong total);
  746. /// <summary>
  747. /// SetCompleted 7-zip internal function.
  748. /// </summary>
  749. /// <param name="completeValue"></param>
  750. void SetCompleted([In] ref ulong completeValue);
  751. /// <summary>
  752. /// Gets archive update mode.
  753. /// </summary>
  754. /// <param name="index">File index</param>
  755. /// <param name="newData">1 if new, 0 if not</param>
  756. /// <param name="newProperties">1 if new, 0 if not</param>
  757. /// <param name="indexInArchive">-1 if doesn't matter</param>
  758. /// <returns></returns>
  759. [PreserveSig]
  760. int GetUpdateItemInfo(
  761. uint index, ref int newData,
  762. ref int newProperties, ref uint indexInArchive);
  763. /// <summary>
  764. /// Gets the archive item property data.
  765. /// </summary>
  766. /// <param name="index">Item index</param>
  767. /// <param name="propId">Property identifier</param>
  768. /// <param name="value">Property value</param>
  769. /// <returns>Zero if Ok</returns>
  770. [PreserveSig]
  771. int GetProperty(uint index, ItemPropId propId, ref PropVariant value);
  772. /// <summary>
  773. /// Gets the stream for reading.
  774. /// </summary>
  775. /// <param name="index">The item index.</param>
  776. /// <param name="inStream">The ISequentialInStream pointer for reading.</param>
  777. /// <returns>Zero if Ok</returns>
  778. [PreserveSig]
  779. int GetStream(
  780. uint index,
  781. [Out, MarshalAs(UnmanagedType.Interface)] out ISequentialInStream inStream);
  782. /// <summary>
  783. /// Sets the result for currently performed operation.
  784. /// </summary>
  785. /// <param name="operationResult">The result value.</param>
  786. void SetOperationResult(OperationResult operationResult);
  787. /// <summary>
  788. /// EnumProperties 7-zip internal function.
  789. /// </summary>
  790. /// <param name="enumerator">The enumerator pointer.</param>
  791. /// <returns></returns>
  792. long EnumProperties(IntPtr enumerator);
  793. }
  794. /// <summary>
  795. /// 7-zip IArchiveOpenVolumeCallback imported interface to handle archive volumes.
  796. /// </summary>
  797. [ComImport]
  798. [Guid("23170F69-40C1-278A-0000-000600300000")]
  799. [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
  800. internal interface IArchiveOpenVolumeCallback
  801. {
  802. /// <summary>
  803. /// Gets the archive property data.
  804. /// </summary>
  805. /// <param name="propId">The property identificator.</param>
  806. /// <param name="value">The property value.</param>
  807. [PreserveSig]
  808. int GetProperty(
  809. ItemPropId propId, ref PropVariant value);
  810. /// <summary>
  811. /// Gets the stream for reading the volume.
  812. /// </summary>
  813. /// <param name="name">The volume file name.</param>
  814. /// <param name="inStream">The IInStream pointer for reading.</param>
  815. /// <returns>Zero if Ok</returns>
  816. [PreserveSig]
  817. int GetStream(
  818. [MarshalAs(UnmanagedType.LPWStr)] string name,
  819. [Out, MarshalAs(UnmanagedType.Interface)] out IInStream inStream);
  820. }
  821. /// <summary>
  822. /// 7-zip ISequentialInStream imported interface
  823. /// </summary>
  824. [ComImport]
  825. [Guid("23170F69-40C1-278A-0000-000300010000")]
  826. [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
  827. internal interface ISequentialInStream
  828. {
  829. /// <summary>
  830. /// Writes data to 7-zip packer
  831. /// </summary>
  832. /// <param name="data">Array of bytes available for writing</param>
  833. /// <param name="size">Array size</param>
  834. /// <returns>S_OK if success</returns>
  835. /// <remarks>If (size > 0) and there are bytes in stream,
  836. /// this function must read at least 1 byte.
  837. /// This function is allowed to read less than "size" bytes.
  838. /// You must call Read function in loop, if you need exact amount of data.
  839. /// </remarks>
  840. int Read(
  841. [Out, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)] byte[] data,
  842. uint size);
  843. }
  844. /// <summary>
  845. /// 7-zip ISequentialOutStream imported interface
  846. /// </summary>
  847. [ComImport]
  848. [Guid("23170F69-40C1-278A-0000-000300020000")]
  849. [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
  850. internal interface ISequentialOutStream
  851. {
  852. /// <summary>
  853. /// Writes data to unpacked file stream
  854. /// </summary>
  855. /// <param name="data">Array of bytes available for reading</param>
  856. /// <param name="size">Array size</param>
  857. /// <param name="processedSize">Processed data size</param>
  858. /// <returns>S_OK if success</returns>
  859. /// <remarks>If size != 0, return value is S_OK and (*processedSize == 0),
  860. /// then there are no more bytes in stream.
  861. /// If (size > 0) and there are bytes in stream,
  862. /// this function must read at least 1 byte.
  863. /// This function is allowed to rwrite less than "size" bytes.
  864. /// You must call Write function in loop, if you need exact amount of data.
  865. /// </remarks>
  866. [PreserveSig]
  867. int Write(
  868. [In, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)] byte[] data,
  869. uint size, IntPtr processedSize);
  870. }
  871. /// <summary>
  872. /// 7-zip IInStream imported interface
  873. /// </summary>
  874. [ComImport]
  875. [Guid("23170F69-40C1-278A-0000-000300030000")]
  876. [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
  877. internal interface IInStream
  878. {
  879. /// <summary>
  880. /// Read routine
  881. /// </summary>
  882. /// <param name="data">Array of bytes to set</param>
  883. /// <param name="size">Array size</param>
  884. /// <returns>Zero if Ok</returns>
  885. int Read(
  886. [Out, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)] byte[] data,
  887. uint size);
  888. /// <summary>
  889. /// Seek routine
  890. /// </summary>
  891. /// <param name="offset">Offset value</param>
  892. /// <param name="seekOrigin">Seek origin value</param>
  893. /// <param name="newPosition">New position pointer</param>
  894. void Seek(
  895. long offset, SeekOrigin seekOrigin, IntPtr newPosition);
  896. }
  897. /// <summary>
  898. /// 7-zip IOutStream imported interface
  899. /// </summary>
  900. [ComImport]
  901. [Guid("23170F69-40C1-278A-0000-000300040000")]
  902. [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
  903. internal interface IOutStream
  904. {
  905. /// <summary>
  906. /// Write routine
  907. /// </summary>
  908. /// <param name="data">Array of bytes to get</param>
  909. /// <param name="size">Array size</param>
  910. /// <param name="processedSize">Processed size</param>
  911. /// <returns>Zero if Ok</returns>
  912. [PreserveSig]
  913. int Write(
  914. [In, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)] byte[] data,
  915. uint size,
  916. IntPtr processedSize);
  917. /// <summary>
  918. /// Seek routine
  919. /// </summary>
  920. /// <param name="offset">Offset value</param>
  921. /// <param name="seekOrigin">Seek origin value</param>
  922. /// <param name="newPosition">New position pointer</param>
  923. void Seek(
  924. long offset, SeekOrigin seekOrigin, IntPtr newPosition);
  925. /// <summary>
  926. /// Set size routine
  927. /// </summary>
  928. /// <param name="newSize">New size value</param>
  929. /// <returns>Zero if Ok</returns>
  930. [PreserveSig]
  931. int SetSize(long newSize);
  932. }
  933. /// <summary>
  934. /// 7-zip essential in archive interface
  935. /// </summary>
  936. [ComImport]
  937. [Guid("23170F69-40C1-278A-0000-000600600000")]
  938. [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
  939. internal interface IInArchive
  940. {
  941. /// <summary>
  942. /// Opens archive for reading.
  943. /// </summary>
  944. /// <param name="stream">Archive file stream</param>
  945. /// <param name="maxCheckStartPosition">Maximum start position for checking</param>
  946. /// <param name="openArchiveCallback">Callback for opening archive</param>
  947. /// <returns></returns>
  948. [PreserveSig]
  949. int Open(
  950. IInStream stream,
  951. [In] ref ulong maxCheckStartPosition,
  952. [MarshalAs(UnmanagedType.Interface)] IArchiveOpenCallback openArchiveCallback);
  953. /// <summary>
  954. /// Closes the archive.
  955. /// </summary>
  956. void Close();
  957. /// <summary>
  958. /// Gets the number of files in the archive file table .
  959. /// </summary>
  960. /// <returns>The number of files in the archive</returns>
  961. uint GetNumberOfItems();
  962. /// <summary>
  963. /// Retrieves specific property data.
  964. /// </summary>
  965. /// <param name="index">File index in the archive file table</param>
  966. /// <param name="propId">Property code</param>
  967. /// <param name="value">Property variant value</param>
  968. void GetProperty(
  969. uint index,
  970. ItemPropId propId,
  971. ref PropVariant value); // PropVariant
  972. /// <summary>
  973. /// Extracts files from the opened archive.
  974. /// </summary>
  975. /// <param name="indexes">indexes of files to be extracted (must be sorted)</param>
  976. /// <param name="numItems">0xFFFFFFFF means all files</param>
  977. /// <param name="testMode">testMode != 0 means "test files operation"</param>
  978. /// <param name="extractCallback">IArchiveExtractCallback for operations handling</param>
  979. /// <returns>0 if success</returns>
  980. [PreserveSig]
  981. int Extract(
  982. [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)] uint[] indexes,
  983. uint numItems,
  984. int testMode,
  985. [MarshalAs(UnmanagedType.Interface)] IArchiveExtractCallback extractCallback);
  986. /// <summary>
  987. /// Gets archive property data
  988. /// </summary>
  989. /// <param name="propId">Archive property identificator</param>
  990. /// <param name="value">Archive property value</param>
  991. void GetArchiveProperty(
  992. ItemPropId propId, // PROPID
  993. ref PropVariant value); // PropVariant
  994. /// <summary>
  995. /// Gets the number of properties
  996. /// </summary>
  997. /// <returns>The number of properties</returns>
  998. uint GetNumberOfProperties();
  999. /// <summary>
  1000. /// Gets property information
  1001. /// </summary>
  1002. /// <param name="index">Item index</param>
  1003. /// <param name="name">Name</param>
  1004. /// <param name="propId">Property identifier</param>
  1005. /// <param name="varType">Variant type</param>
  1006. void GetPropertyInfo(
  1007. uint index,
  1008. [MarshalAs(UnmanagedType.BStr)] out string name,
  1009. out ItemPropId propId, // PROPID
  1010. out ushort varType); //VARTYPE
  1011. /// <summary>
  1012. /// Gets the number of archive properties
  1013. /// </summary>
  1014. /// <returns>The number of archive properties</returns>
  1015. uint GetNumberOfArchiveProperties();
  1016. /// <summary>
  1017. /// Gets the archive property information
  1018. /// </summary>
  1019. /// <param name="index">Item index</param>
  1020. /// <param name="name">Name</param>
  1021. /// <param name="propId">Property identifier</param>
  1022. /// <param name="varType">Variant type</param>
  1023. void GetArchivePropertyInfo(
  1024. uint index,
  1025. [MarshalAs(UnmanagedType.BStr)] out string name,
  1026. out ItemPropId propId, // PROPID
  1027. out ushort varType); //VARTYPE
  1028. }
  1029. /// <summary>
  1030. /// 7-zip essential out archive interface
  1031. /// </summary>
  1032. [ComImport]
  1033. [Guid("23170F69-40C1-278A-0000-000600A00000")]
  1034. [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
  1035. internal interface IOutArchive
  1036. {
  1037. /// <summary>
  1038. /// Updates archive items
  1039. /// </summary>
  1040. /// <param name="outStream">The ISequentialOutStream pointer for writing the archive data</param>
  1041. /// <param name="numItems">Number of archive items</param>
  1042. /// <param name="updateCallback">The IArchiveUpdateCallback pointer</param>
  1043. /// <returns>Zero if Ok</returns>
  1044. [PreserveSig]
  1045. int UpdateItems(
  1046. [MarshalAs(UnmanagedType.Interface)] ISequentialOutStream outStream,
  1047. uint numItems,
  1048. [MarshalAs(UnmanagedType.Interface)] IArchiveUpdateCallback updateCallback);
  1049. /// <summary>
  1050. /// Gets file time type(?)
  1051. /// </summary>
  1052. /// <param name="type">Type pointer</param>
  1053. void GetFileTimeType(IntPtr type);
  1054. }
  1055. /// <summary>
  1056. /// 7-zip ISetProperties interface for setting various archive properties
  1057. /// </summary>
  1058. [ComImport]
  1059. [Guid("23170F69-40C1-278A-0000-000600030000")]
  1060. [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
  1061. internal interface ISetProperties
  1062. {
  1063. /// <summary>
  1064. /// Sets the archive properties
  1065. /// </summary>
  1066. /// <param name="names">The names of the properties</param>
  1067. /// <param name="values">The values of the properties</param>
  1068. /// <param name="numProperties">The properties count</param>
  1069. /// <returns></returns>
  1070. int SetProperties(IntPtr names, IntPtr values, int numProperties);
  1071. }
  1072. #endif
  1073. }