Skip to content

Commit

Permalink
Initial version
Browse files Browse the repository at this point in the history
  • Loading branch information
jamersonpro committed Jan 28, 2021
1 parent 0ae941a commit e9625a2
Show file tree
Hide file tree
Showing 122 changed files with 51,696 additions and 1 deletion.
215 changes: 215 additions & 0 deletions NtfsMarkBad.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
// NtfsMarkBad.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include "stdafx.h"

#include "common.h"

#include "ulib.hxx"
#include "message.hxx"
#include "system.hxx"
#include "ifssys.hxx"
#include "ntfsvol.hxx"

#include <iostream> // std::cout
#include <string> // std::string, std::stoll
#include <algorithm>
#include <stdlib.h>
#include <errno.h>

BOOLEAN DefineClassDescriptors()
{
return UlibDefineClassDescriptors() && IfsutilDefineClassDescriptors() && UntfsDefineClassDescriptors();
}

unsigned char char_toupper(unsigned char c)
{
return toupper(c);
}

std::string str_toupper(std::string s)
{
std::transform(s.begin(), s.end(), s.begin(), char_toupper);
return s;
}

void About()
{
std::cout << "Mark clusters as bad on NTFS\n\nUsage:\nNTFSMARKBAD drive: first_drive_sector_to_mark last_drive_sector_to_mark\n";
};

// convert string to long long
__int64 stoll(const std::string& str)
{
const char* ptr = str.c_str();
char* error_ptr;
__int64 result = _strtoi64(ptr, &error_ptr, 10);

if (ptr == error_ptr)
{
throw std::out_of_range("invalid stoll argument");
}

if (errno == EINVAL)
{
throw std::out_of_range("stoll argument out of range");
}

return result;
}

int __cdecl
main(
ULONG nArgCount,
PSTR arrArguments[]
)
{
MESSAGE Message;
Message.Initialize();

Message.Out("NTFSMARKBAD ", "0.0.1",
#if defined(_M_AMD64)
" x64"
#else
" x32"
#endif
);
Message.Out("");

DefineClassDescriptors();

if (nArgCount != 4)
{
About();
return 1;
}

std::string drive = arrArguments[1];
drive = str_toupper(drive);

if (drive.length() != 2
|| drive[0] < 'A' || drive[0]>'Z'
|| drive[1] != ':')
{
Message.Out("Invalid drive.");
}

std::string firstSectorStr = arrArguments[2];
__int64 firstSector;
try
{
firstSector = ::stoll(firstSectorStr);
}
catch (const std::out_of_range&)
{
firstSector = -1;
}
if (firstSector < 0)
{
Message.Out("Invalid first sector number.");
return 1;
}

std::string lastSectorStr = arrArguments[3];
__int64 lastSector;
try
{
lastSector = ::stoll(lastSectorStr);
}
catch (const std::out_of_range&)
{
lastSector = -1;
}
if (lastSector < 0 || lastSector < firstSector)
{
Message.Out("Invalid last sector number.");
return 1;
}



DSTRING CurrentDrive;
if (!SYSTEM::QueryCurrentDosDriveName(&CurrentDrive))
{
Message.Out("Error.");
return 1;
}

DSTRING InputParamDrive;
InputParamDrive.Initialize(drive.c_str());


DSTRING NtDriveName;
NtDriveName.Initialize("\\??\\");
NtDriveName.Strcat(&InputParamDrive);


if (CurrentDrive == InputParamDrive)
{
Message.Out("Cannot lock current drive.");
return 1;
}


DSTRING ntfs_name;
if (!ntfs_name.Initialize("NTFS"))
{
Message.Out("Error.");
return 1;
}


NTSTATUS Status;
DSTRING drivename;

BOOL FsNameIsNtfs;

if (!IFS_SYSTEM::QueryFileSystemNameIsNtfs(&NtDriveName,
&FsNameIsNtfs,
&Status))
{
if (Status == STATUS_ACCESS_DENIED)
{
Message.Out("Access denied.");
}
else if (Status != STATUS_SUCCESS)
{
Message.Out("Cannot open volume for direct access.");
}
else
{
Message.Out("Cannot determine file system of drive: ", drive.c_str());
}

return 1;
}

if (!FsNameIsNtfs) //NOT NTFS
{
Message.Out("Only NTFS file system supported.");
return 1;
}


NTFS_VOL NtfsVol;
BOOLEAN Result;

Result = NtfsVol.Initialize(&NtDriveName, &Message);
if (!Result)
{
Message.Out("Failed to initialize.");
return 1;
}

Result = NtfsVol.MarkBad(firstSector, lastSector, &Message);
if (!Result)
{
Message.Out("An error has occurred.");
return 1;
}

Message.Out("Completed.");
return 0;
}


26 changes: 26 additions & 0 deletions NtfsMarkBad_2005.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

Microsoft Visual Studio Solution File, Format Version 9.00
# Visual Studio 2005
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "NtfsMarkBad_2005", "NtfsMarkBad_2005.vcproj", "{6C848416-93D5-41FC-A4DD-4651B7C590B2}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Win32 = Debug|Win32
Debug|x64 = Debug|x64
Release|Win32 = Release|Win32
Release|x64 = Release|x64
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{6C848416-93D5-41FC-A4DD-4651B7C590B2}.Debug|Win32.ActiveCfg = Debug|Win32
{6C848416-93D5-41FC-A4DD-4651B7C590B2}.Debug|Win32.Build.0 = Debug|Win32
{6C848416-93D5-41FC-A4DD-4651B7C590B2}.Debug|x64.ActiveCfg = Debug|x64
{6C848416-93D5-41FC-A4DD-4651B7C590B2}.Debug|x64.Build.0 = Debug|x64
{6C848416-93D5-41FC-A4DD-4651B7C590B2}.Release|Win32.ActiveCfg = Release|Win32
{6C848416-93D5-41FC-A4DD-4651B7C590B2}.Release|Win32.Build.0 = Release|Win32
{6C848416-93D5-41FC-A4DD-4651B7C590B2}.Release|x64.ActiveCfg = Release|x64
{6C848416-93D5-41FC-A4DD-4651B7C590B2}.Release|x64.Build.0 = Release|x64
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
Loading

0 comments on commit e9625a2

Please sign in to comment.