PublicRegionHelper.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /* Copyright (C) 2014-2016 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
  2. *
  3. * You can redistribute this program and/or modify it under the terms of
  4. * the GNU Lesser Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. */
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Text;
  10. namespace DiskAccessLibrary.LogicalDiskManager
  11. {
  12. public class PublicRegionHelper
  13. {
  14. /// <summary>
  15. /// LBA values in extent / volume / component records refer to logical 512-byte blocks within the public region of the disk, regardless of the actual block size of the disk.
  16. /// We have to translate those values in order to support disks with 4K sectors.
  17. /// </summary>
  18. public const int BytesPerPublicRegionSector = 512;
  19. public static long TranslateFromPublicRegionLBA(long publicRegionOffsetLBA, long publicRegionStartLBA, int bytesPerDiskSector)
  20. {
  21. return publicRegionStartLBA + publicRegionOffsetLBA * BytesPerPublicRegionSector / bytesPerDiskSector;
  22. }
  23. public static long TranslateFromPublicRegionSizeLBA(long sectorCount, int bytesPerDiskSector)
  24. {
  25. return sectorCount * BytesPerPublicRegionSector / bytesPerDiskSector;
  26. }
  27. public static long TranslateToPublicRegionLBA(long sectorIndex, PrivateHeader privateHeader)
  28. {
  29. return TranslateToPublicRegionLBA(sectorIndex, (long)privateHeader.PublicRegionStartLBA, (int)privateHeader.BytesPerSector);
  30. }
  31. public static long TranslateToPublicRegionLBA(long sectorIndex, long publicRegionStartLBA, int bytesPerDiskSector)
  32. {
  33. return (sectorIndex - publicRegionStartLBA) * bytesPerDiskSector / BytesPerPublicRegionSector;
  34. }
  35. public static long TranslateToPublicRegionSizeLBA(long sectorCount, PrivateHeader privateHeader)
  36. {
  37. return TranslateToPublicRegionSizeLBA(sectorCount, (int)privateHeader.BytesPerSector);
  38. }
  39. public static long TranslateToPublicRegionSizeLBA(long sectorCount, int bytesPerDiskSector)
  40. {
  41. return sectorCount * bytesPerDiskSector / BytesPerPublicRegionSector;
  42. }
  43. }
  44. }