Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 

292 rindas
15 KiB

  1. /* Copyright (C) 2008-2016 Peter Palotas, Jeffrey Jangli, Alexandr Normuradov
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to deal
  5. * in the Software without restriction, including without limitation the rights
  6. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. * copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. * THE SOFTWARE.
  20. */
  21. using System;
  22. using System.IO;
  23. using System.Runtime.InteropServices;
  24. namespace Alphaleonis.Win32.Filesystem
  25. {
  26. partial class File
  27. {
  28. #region Export
  29. /// <summary>[AlphaFS] Backs up (export) encrypted files. This is one of a group of Encrypted File System (EFS) functions that is
  30. /// intended to implement backup and restore functionality, while maintaining files in their encrypted state.</summary>
  31. /// <remarks>
  32. /// <para>
  33. /// The file being backed up is not decrypted; it is backed up in its encrypted state.
  34. /// </para>
  35. /// <para>
  36. /// If the caller does not have access to the key for the file, the caller needs
  37. /// <see cref="Alphaleonis.Win32.Security.Privilege.Backup"/> to export encrypted files. See
  38. /// <see cref="Alphaleonis.Win32.Security.PrivilegeEnabler"/>.
  39. /// </para>
  40. /// <para>
  41. /// To backup an encrypted file call one of the
  42. /// <see cref="O:Alphaleonis.Win32.Filesystem.File.ExportEncryptedFileRaw"/> overloads and specify the file to backup
  43. /// along with the destination stream of the backup data.
  44. /// </para>
  45. /// <para>
  46. /// This function is intended for the backup of only encrypted files; see <see cref="BackupFileStream"/> for backup
  47. /// of unencrypted files.
  48. /// </para>
  49. /// </remarks>
  50. /// <param name="fileName">The name of the file to be backed up.</param>
  51. /// <param name="outputStream">The destination stream to which the backup data will be written.</param>
  52. /// <seealso cref="O:Alphaleonis.Win32.Filesystem.File.ImportEncryptedFileRaw"/>
  53. public static void ExportEncryptedFileRaw(string fileName, Stream outputStream)
  54. {
  55. ImportExportEncryptedFileDirectoryRawCore(true, false, outputStream, fileName, PathFormat.RelativePath, false);
  56. }
  57. /// <summary>[AlphaFS] Backs up (export) encrypted files. This is one of a group of Encrypted File System (EFS) functions that is
  58. /// intended to implement backup and restore functionality, while maintaining files in their encrypted state.</summary>
  59. /// <remarks>
  60. /// <para>
  61. /// The file being backed up is not decrypted; it is backed up in its encrypted state.
  62. /// </para>
  63. /// <para>
  64. /// If the caller does not have access to the key for the file, the caller needs
  65. /// <see cref="Alphaleonis.Win32.Security.Privilege.Backup"/> to export encrypted files. See
  66. /// <see cref="Alphaleonis.Win32.Security.PrivilegeEnabler"/>.
  67. /// </para>
  68. /// <para>
  69. /// To backup an encrypted file call one of the
  70. /// <see cref="O:Alphaleonis.Win32.Filesystem.File.ExportEncryptedFileRaw"/> overloads and specify the file to backup
  71. /// along with the destination stream of the backup data.
  72. /// </para>
  73. /// <para>
  74. /// This function is intended for the backup of only encrypted files; see <see cref="BackupFileStream"/> for backup
  75. /// of unencrypted files.
  76. /// </para>
  77. /// </remarks>
  78. /// <param name="fileName">The name of the file to be backed up.</param>
  79. /// <param name="outputStream">The destination stream to which the backup data will be written.</param>
  80. /// <param name="pathFormat">The path format of the <paramref name="fileName"/> parameter.</param>
  81. /// <seealso cref="O:Alphaleonis.Win32.Filesystem.File.ImportEncryptedFileRaw"/>
  82. public static void ExportEncryptedFileRaw(string fileName, Stream outputStream, PathFormat pathFormat)
  83. {
  84. ImportExportEncryptedFileDirectoryRawCore(true, false, outputStream, fileName, pathFormat, false);
  85. }
  86. #endregion // Export
  87. #region Import
  88. /// <summary>[AlphaFS] Restores (import) encrypted files. This is one of a group of Encrypted File System (EFS) functions that is
  89. /// intended to implement backup and restore functionality, while maintaining files in their encrypted state.</summary>
  90. /// <remarks>
  91. /// <para>
  92. /// If the caller does not have access to the key for the file, the caller needs
  93. /// <see cref="Alphaleonis.Win32.Security.Privilege.Backup"/> to restore encrypted files. See
  94. /// <see cref="Alphaleonis.Win32.Security.PrivilegeEnabler"/>.
  95. /// </para>
  96. /// <para>
  97. /// To restore an encrypted file call one of the
  98. /// <see cref="O:Alphaleonis.Win32.Filesystem.File.ImportEncryptedFileRaw"/> overloads and specify the file to restore
  99. /// along with the destination stream of the restored data.
  100. /// </para>
  101. /// <para>
  102. /// This function is intended for the restoration of only encrypted files; see <see cref="BackupFileStream"/> for
  103. /// backup of unencrypted files.
  104. /// </para>
  105. /// </remarks>
  106. /// <param name="inputStream">The stream to read previously backed up data from.</param>
  107. /// <param name="destinationFilePath">The path of the destination file to restore to.</param>
  108. /// <seealso cref="O:Alphaleonis.Win32.Filesystem.File.ExportEncryptedFileRaw"/>
  109. public static void ImportEncryptedFileRaw(Stream inputStream, string destinationFilePath)
  110. {
  111. ImportExportEncryptedFileDirectoryRawCore(false, false, inputStream, destinationFilePath, PathFormat.RelativePath, false);
  112. }
  113. /// <summary>[AlphaFS] Restores (import) encrypted files. This is one of a group of Encrypted File System (EFS) functions that is
  114. /// intended to implement backup and restore functionality, while maintaining files in their encrypted state.</summary>
  115. /// <remarks>
  116. /// <para>
  117. /// If the caller does not have access to the key for the file, the caller needs
  118. /// <see cref="Alphaleonis.Win32.Security.Privilege.Backup"/> to restore encrypted files. See
  119. /// <see cref="Alphaleonis.Win32.Security.PrivilegeEnabler"/>.
  120. /// </para>
  121. /// <para>
  122. /// To restore an encrypted file call one of the
  123. /// <see cref="O:Alphaleonis.Win32.Filesystem.File.ImportEncryptedFileRaw"/> overloads and specify the file to restore
  124. /// along with the destination stream of the restored data.
  125. /// </para>
  126. /// <para>
  127. /// This function is intended for the restoration of only encrypted files; see <see cref="BackupFileStream"/> for
  128. /// backup of unencrypted files.
  129. /// </para>
  130. /// </remarks>
  131. /// <param name="inputStream">The stream to read previously backed up data from.</param>
  132. /// <param name="destinationFilePath">The path of the destination file to restore to.</param>
  133. /// <param name="pathFormat">The path format of the <paramref name="destinationFilePath"/> parameter.</param>
  134. /// <seealso cref="O:Alphaleonis.Win32.Filesystem.File.ExportEncryptedFileRaw"/>
  135. public static void ImportEncryptedFileRaw(Stream inputStream, string destinationFilePath, PathFormat pathFormat)
  136. {
  137. ImportExportEncryptedFileDirectoryRawCore(false, false, inputStream, destinationFilePath, pathFormat, false);
  138. }
  139. /// <summary>[AlphaFS] Restores (import) encrypted files. This is one of a group of Encrypted File System (EFS) functions that is
  140. /// intended to implement backup and restore functionality, while maintaining files in their encrypted state.</summary>
  141. /// <remarks>
  142. /// <para>
  143. /// If the caller does not have access to the key for the file, the caller needs
  144. /// <see cref="Alphaleonis.Win32.Security.Privilege.Backup"/> to restore encrypted files. See
  145. /// <see cref="Alphaleonis.Win32.Security.PrivilegeEnabler"/>.
  146. /// </para>
  147. /// <para>
  148. /// To restore an encrypted file call one of the
  149. /// <see cref="O:Alphaleonis.Win32.Filesystem.File.ImportEncryptedFileRaw"/> overloads and specify the file to restore
  150. /// along with the destination stream of the restored data.
  151. /// </para>
  152. /// <para>
  153. /// This function is intended for the restoration of only encrypted files; see <see cref="BackupFileStream"/> for
  154. /// backup of unencrypted files.
  155. /// </para>
  156. /// </remarks>
  157. /// <param name="inputStream">The stream to read previously backed up data from.</param>
  158. /// <param name="destinationFilePath">The path of the destination file to restore to.</param>
  159. /// <param name="overwriteHidden">If set to <see langword="true"/> a hidden file will be overwritten on import.</param>
  160. /// <seealso cref="O:Alphaleonis.Win32.Filesystem.File.ExportEncryptedFileRaw"/>
  161. public static void ImportEncryptedFileRaw(Stream inputStream, string destinationFilePath, bool overwriteHidden)
  162. {
  163. ImportExportEncryptedFileDirectoryRawCore(false, false, inputStream, destinationFilePath, PathFormat.RelativePath, overwriteHidden);
  164. }
  165. /// <summary>[AlphaFS] Restores (import) encrypted files. This is one of a group of Encrypted File System (EFS) functions that is
  166. /// intended to implement backup and restore functionality, while maintaining files in their encrypted state.</summary>
  167. /// <remarks>
  168. /// <para>
  169. /// If the caller does not have access to the key for the file, the caller needs
  170. /// <see cref="Alphaleonis.Win32.Security.Privilege.Backup"/> to restore encrypted files. See
  171. /// <see cref="Alphaleonis.Win32.Security.PrivilegeEnabler"/>.
  172. /// </para>
  173. /// <para>
  174. /// To restore an encrypted file call one of the
  175. /// <see cref="O:Alphaleonis.Win32.Filesystem.File.ImportEncryptedFileRaw"/> overloads and specify the file to restore
  176. /// along with the destination stream of the restored data.
  177. /// </para>
  178. /// <para>
  179. /// This function is intended for the restoration of only encrypted files; see <see cref="BackupFileStream"/> for
  180. /// backup of unencrypted files.
  181. /// </para>
  182. /// </remarks>
  183. /// <param name="inputStream">The stream to read previously backed up data from.</param>
  184. /// <param name="destinationFilePath">The path of the destination file to restore to.</param>
  185. /// <param name="overwriteHidden">If set to <see langword="true"/> a hidden file will be overwritten on import.</param>
  186. /// <param name="pathFormat">The path format of the <paramref name="destinationFilePath"/> parameter.</param>
  187. /// <seealso cref="O:Alphaleonis.Win32.Filesystem.File.ExportEncryptedFileRaw"/>
  188. public static void ImportEncryptedFileRaw(Stream inputStream, string destinationFilePath, bool overwriteHidden, PathFormat pathFormat)
  189. {
  190. ImportExportEncryptedFileDirectoryRawCore(false, false, inputStream, destinationFilePath, pathFormat, overwriteHidden);
  191. }
  192. #endregion // Import
  193. internal static void ImportExportEncryptedFileDirectoryRawCore(bool isExport, bool isFolder, Stream stream, string destinationPath, PathFormat pathFormat, bool overwriteHidden)
  194. {
  195. string destinationPathLp = Path.GetExtendedLengthPathCore(null, destinationPath, pathFormat, GetFullPathOptions.FullCheck | GetFullPathOptions.TrimEnd);
  196. NativeMethods.EncryptedFileRawMode mode = isExport
  197. ? NativeMethods.EncryptedFileRawMode.CreateForExport
  198. : NativeMethods.EncryptedFileRawMode.CreateForImport;
  199. if (isFolder)
  200. mode = mode | NativeMethods.EncryptedFileRawMode.CreateForDir;
  201. if (overwriteHidden)
  202. mode = mode | NativeMethods.EncryptedFileRawMode.OverwriteHidden;
  203. // OpenEncryptedFileRaw()
  204. // In the ANSI version of this function, the name is limited to 248 characters.
  205. // To extend this limit to 32,767 wide characters, call the Unicode version of the function and prepend "\\?\" to the path.
  206. // 2015-08-02: MSDN does not confirm LongPath usage but a Unicode version of this function exists.
  207. SafeEncryptedFileRawHandle context;
  208. var lastError = NativeMethods.OpenEncryptedFileRaw(destinationPathLp, mode, out context);
  209. try
  210. {
  211. if (lastError != Win32Errors.ERROR_SUCCESS)
  212. NativeError.ThrowException(lastError, destinationPathLp);
  213. lastError = isExport
  214. ? NativeMethods.ReadEncryptedFileRaw((pbData, pvCallbackContext, length) =>
  215. {
  216. try
  217. {
  218. var data = new byte[length];
  219. Marshal.Copy(pbData, data, 0, (int) length);
  220. stream.Write(data, 0, (int) length);
  221. }
  222. catch (Exception ex)
  223. {
  224. return Marshal.GetHRForException(ex) & NativeMethods.OverflowExceptionBitShift;
  225. }
  226. return (int) Win32Errors.ERROR_SUCCESS;
  227. }, IntPtr.Zero, context)
  228. : NativeMethods.WriteEncryptedFileRaw((IntPtr pbData, IntPtr pvCallbackContext, ref uint length) =>
  229. {
  230. try
  231. {
  232. var data = new byte[length];
  233. length = (uint) stream.Read(data, 0, (int) length);
  234. if (length == 0)
  235. return (int) Win32Errors.ERROR_SUCCESS;
  236. Marshal.Copy(data, 0, pbData, (int) length);
  237. }
  238. catch (Exception ex)
  239. {
  240. return Marshal.GetHRForException(ex) & NativeMethods.OverflowExceptionBitShift;
  241. }
  242. return (int) Win32Errors.ERROR_SUCCESS;
  243. }, IntPtr.Zero, context);
  244. if (lastError != Win32Errors.ERROR_SUCCESS)
  245. NativeError.ThrowException(lastError, destinationPathLp);
  246. }
  247. finally
  248. {
  249. if (context != null)
  250. context.Dispose();
  251. }
  252. }
  253. }
  254. }