| 1 | /* |
|---|
| 2 | * $Id$ |
|---|
| 3 | * Copyright 2009 The Eraser Project |
|---|
| 4 | * Original Author: Joel Low <lowjoel@users.sourceforge.net> |
|---|
| 5 | * Modified By: |
|---|
| 6 | * |
|---|
| 7 | * This file is part of Eraser. |
|---|
| 8 | * |
|---|
| 9 | * Eraser is free software: you can redistribute it and/or modify it under the |
|---|
| 10 | * terms of the GNU General Public License as published by the Free Software |
|---|
| 11 | * Foundation, either version 3 of the License, or (at your option) any later |
|---|
| 12 | * version. |
|---|
| 13 | * |
|---|
| 14 | * Eraser is distributed in the hope that it will be useful, but WITHOUT ANY |
|---|
| 15 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
|---|
| 16 | * A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
|---|
| 17 | * |
|---|
| 18 | * A copy of the GNU General Public License can be found at |
|---|
| 19 | * <http://www.gnu.org/licenses/>. |
|---|
| 20 | */ |
|---|
| 21 | |
|---|
| 22 | #include <stdafx.h> |
|---|
| 23 | #include "FatApi.h" |
|---|
| 24 | |
|---|
| 25 | using namespace System::IO; |
|---|
| 26 | using namespace System::Runtime::InteropServices; |
|---|
| 27 | |
|---|
| 28 | namespace Eraser { |
|---|
| 29 | namespace Util { |
|---|
| 30 | Fat16Api::Fat16Api(VolumeInfo^ info) : Fat12Or16Api(info) |
|---|
| 31 | { |
|---|
| 32 | //Sanity checks: check that this volume is FAT16! |
|---|
| 33 | if (IsFat12() || info->VolumeFormat == L"FAT12") |
|---|
| 34 | throw gcnew ArgumentException(L"The volume provided is not a FAT16 volume."); |
|---|
| 35 | } |
|---|
| 36 | |
|---|
| 37 | Fat16Api::Fat16Api(VolumeInfo^ info, IO::Stream^ stream) : Fat12Or16Api(info, stream) |
|---|
| 38 | { |
|---|
| 39 | //Sanity checks: check that this volume is FAT16! |
|---|
| 40 | if (IsFat12() || info->VolumeFormat == L"FAT12") |
|---|
| 41 | throw gcnew ArgumentException(L"The volume provided is not a FAT16 volume."); |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | bool Fat16Api::IsClusterAllocated(unsigned cluster) |
|---|
| 45 | { |
|---|
| 46 | unsigned short* fatPtr = reinterpret_cast<unsigned short*>(Fat); |
|---|
| 47 | if ( |
|---|
| 48 | fatPtr[cluster] <= 0x0001 || |
|---|
| 49 | (fatPtr[cluster] >= 0xFFF0 && fatPtr[cluster] <= 0xFFF6) || |
|---|
| 50 | fatPtr[cluster] == 0xFFF7 |
|---|
| 51 | ) |
|---|
| 52 | return false; |
|---|
| 53 | |
|---|
| 54 | return true; |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | unsigned Fat16Api::GetNextCluster(unsigned cluster) |
|---|
| 58 | { |
|---|
| 59 | unsigned short* fatPtr = reinterpret_cast<unsigned short*>(Fat); |
|---|
| 60 | if (fatPtr[cluster] <= 0x0001 || (fatPtr[cluster] >= 0xFFF0 && fatPtr[cluster] <= 0xFFF6)) |
|---|
| 61 | throw gcnew ArgumentException(L"Invalid FAT cluster: cluster is marked free."); |
|---|
| 62 | else if (fatPtr[cluster] == 0xFFF7) |
|---|
| 63 | throw gcnew ArgumentException(L"Invalid FAT cluster: cluster is marked bad."); |
|---|
| 64 | else if (fatPtr[cluster] >= 0xFFF8) |
|---|
| 65 | return 0xFFFFFFFF; |
|---|
| 66 | else |
|---|
| 67 | return fatPtr[cluster]; |
|---|
| 68 | } |
|---|
| 69 | |
|---|
| 70 | unsigned Fat16Api::FileSize(unsigned cluster) |
|---|
| 71 | { |
|---|
| 72 | unsigned short* fatPtr = reinterpret_cast<unsigned short*>(Fat); |
|---|
| 73 | for (unsigned result = 1; ; ++result) |
|---|
| 74 | { |
|---|
| 75 | if (fatPtr[cluster] <= 0x0001 || (fatPtr[cluster] >= 0xFFF0 && fatPtr[cluster] <= 0xFFF6)) |
|---|
| 76 | throw gcnew ArgumentException(L"Invalid FAT cluster: cluster is marked free."); |
|---|
| 77 | else if (fatPtr[cluster] == 0xFFF7) |
|---|
| 78 | throw gcnew ArgumentException(L"Invalid FAT cluster: cluster is marked bad."); |
|---|
| 79 | else if (fatPtr[cluster] >= 0xFFF8) |
|---|
| 80 | return result * ClusterSize; |
|---|
| 81 | else |
|---|
| 82 | cluster = fatPtr[cluster]; |
|---|
| 83 | } |
|---|
| 84 | } |
|---|
| 85 | } |
|---|
| 86 | } |
|---|