Skip to content

Commit c1a2a2e

Browse files
committed
Adding WindowsNotepadParser Console Application
1 parent ba59ece commit c1a2a2e

6 files changed

Lines changed: 253 additions & 10 deletions

File tree

‎NotepadStateLibrary/NotepadStateLibrary.sln‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio Version 17
44
VisualStudioVersion = 17.9.34622.214
55
MinimumVisualStudioVersion = 10.0.40219.1
6-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NotepadStateLibrary", "NotepadStateLibrary\NotepadStateLibrary.csproj", "{2A5CD065-D158-4CA5-A516-7D9333474AA1}"
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NotepadStateLibrary", "NotepadStateLibrary\NotepadStateLibrary.csproj", "{2A5CD065-D158-4CA5-A516-7D9333474AA1}"
77
EndProject
8-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestApp", "TestApp\TestApp.csproj", "{85127A37-7140-4B82-8DCF-5FE8F7A638BD}"
8+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestApp", "TestApp\TestApp.csproj", "{85127A37-7140-4B82-8DCF-5FE8F7A638BD}"
9+
EndProject
10+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WindowsNotepadParser", "WindowsNotepadParser\WindowsNotepadParser.csproj", "{D1DC00E6-9FB4-40C3-91DD-F7B5FEC49EFF}"
911
EndProject
1012
Global
1113
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -21,6 +23,10 @@ Global
2123
{85127A37-7140-4B82-8DCF-5FE8F7A638BD}.Debug|Any CPU.Build.0 = Debug|Any CPU
2224
{85127A37-7140-4B82-8DCF-5FE8F7A638BD}.Release|Any CPU.ActiveCfg = Release|Any CPU
2325
{85127A37-7140-4B82-8DCF-5FE8F7A638BD}.Release|Any CPU.Build.0 = Release|Any CPU
26+
{D1DC00E6-9FB4-40C3-91DD-F7B5FEC49EFF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
27+
{D1DC00E6-9FB4-40C3-91DD-F7B5FEC49EFF}.Debug|Any CPU.Build.0 = Debug|Any CPU
28+
{D1DC00E6-9FB4-40C3-91DD-F7B5FEC49EFF}.Release|Any CPU.ActiveCfg = Release|Any CPU
29+
{D1DC00E6-9FB4-40C3-91DD-F7B5FEC49EFF}.Release|Any CPU.Build.0 = Release|Any CPU
2430
EndGlobalSection
2531
GlobalSection(SolutionProperties) = preSolution
2632
HideSolutionNode = FALSE

‎NotepadStateLibrary/NotepadStateLibrary/NPTabState.cs‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ namespace NotepadStateLibrary
66
{
77
public class NPTabState
88
{
9+
//TODO: Reorder these to a sensical order?
10+
11+
/// <summary>
12+
///
13+
/// </summary>
14+
public string FileName { get; private set; }
915
/// <summary>
1016
/// Size of the associated bin file in bytes for the 0.bin or 1.bin file.
1117
/// </summary>
@@ -31,6 +37,10 @@ public class NPTabState
3137
/// <summary>
3238
/// Calculated CRC32
3339
/// </summary>
40+
public string ContentString
41+
{
42+
get { return Content == null ? string.Empty : Encoding.Unicode.GetString(Content).ReplaceLineEndings(); }
43+
}
3444
public byte[] CRC32Calculated { get; private set; } = null!;
3545
/// <summary>
3646
/// CRC32 from the file
@@ -115,9 +125,10 @@ public class NPTabState
115125
/// Notepad Tab State Files
116126
/// </summary>
117127
/// <param name="bytes">Bytes of the state file</param>
118-
public NPTabState(byte[] bytes)
128+
public NPTabState(byte[] bytes, string fileName)
119129
{
120130
this.bytes = bytes;
131+
this.FileName = fileName;
121132

122133
UnsavedBufferChunks = new List<UnsavedBufferChunk>();
123134

‎NotepadStateLibrary/NotepadStateLibrary/NPWindowState.cs‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ namespace NotepadStateLibrary
55
{
66
public class NPWindowState
77
{
8+
//TODO: Reorder these to a sensical order?
9+
10+
/// <summary>
11+
///
12+
/// </summary>
13+
public string FileName { get; private set; }
814
/// <summary>
915
/// Number of the active tab in Notepad. 0 based index.
1016
/// </summary>
@@ -56,9 +62,10 @@ public class NPWindowState
5662
/// Notepad Window State Files
5763
/// </summary>
5864
/// <param name="bytes">bytes of the state file</param>
59-
public NPWindowState(byte[] bytes)
65+
public NPWindowState(byte[] bytes, string fileName)
6066
{
6167
this.bytes = bytes;
68+
this.FileName = fileName;
6269

6370
Tabs = new List<byte[]>();
6471

‎NotepadStateLibrary/TestApp/Program.cs‎

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,50 @@
1111
byte[] b = File.ReadAllBytes(path);
1212
if (b.Length > 0)
1313
{
14-
NPTabState np = new NPTabState(b);
14+
NPTabState np = new NPTabState(b, Path.GetFileName(path));
15+
Console.WriteLine(Path.GetFileName(path));
16+
if (np.TypeFlag <= 1)
17+
{
18+
Console.WriteLine(Encoding.Unicode.GetString(np.Content).ReplaceLineEndings());
19+
}
1520

16-
if (np.Content != null)
21+
if (np.CRC32Calculated.SequenceEqual(np.CRC32Stored))
22+
{
23+
Console.WriteLine("OK");
24+
}
25+
else
1726
{
18-
string x = Encoding.Unicode.GetString(np.Content);
27+
Console.WriteLine("FAIL");
28+
}
29+
Console.WriteLine("*****************************************");
30+
}
31+
}
32+
33+
folder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), @"Packages\Microsoft.WindowsNotepad_8wekyb3d8bbwe\LocalState\WindowState");
1934

20-
string evil = "MALICIOUS LINE";
35+
foreach (var path in Directory.EnumerateFiles(folder, "*.bin"))
36+
{
37+
byte[] b = File.ReadAllBytes(path);
38+
if (b.Length > 0)
39+
{
2140

22-
File.WriteAllBytes(path, np.WriteContent(Encoding.Unicode.GetBytes(x + Environment.NewLine + evil)));
41+
NPWindowState np = new NPWindowState(b, Path.GetFileName(path));
42+
Console.WriteLine(Path.GetFileName(path));
43+
Console.WriteLine(np.NumberTabs.ToString());
2344

45+
if (np.CRC32Calculated.SequenceEqual(np.CRC32Stored))
46+
{
47+
Console.WriteLine("OK");
2448
}
49+
else
50+
{
51+
Console.WriteLine("FAIL");
52+
}
53+
Console.WriteLine("*****************************************");
2554
}
2655
}
2756

28-
57+
#region
2958
//NPTabState npWrite = new NPTabState(Encoding.Unicode.GetBytes("Test"), [0x0], [0x0], new List<UnsavedBufferChunk>(), [0x1]);
3059
//File.WriteAllBytes("check.bin", npWrite.bytes);
3160

@@ -45,6 +74,12 @@
4574

4675
//NPTabState npWrite1 = new NPTabState(0, [0x01], @"C:\Users\Reversing\Desktop\Test.txt", 12, [0x05], [0x01], new DateTime(2024, 4, 16, 11, 55, 24), sha, 0, 0, [0x1], [0x0], [0x0], c);
4776
//File.WriteAllBytes("saved.bin", npWrite1.bytes);
77+
#endregion
78+
79+
//NPTabState np = new NPTabState(File.ReadAllBytes(@"C:\Users\Reversing\AppData\Local\Packages\Microsoft.WindowsNotepad_8wekyb3d8bbwe\LocalState\TabState\f62c642a-29ba-443a-baef-d3185c5e7910.bin"));
80+
//Console.WriteLine(Encoding.Unicode.GetString(np.Content));
81+
//File.WriteAllBytes(@"C:\Users\Reversing\AppData\Local\Packages\Microsoft.WindowsNotepad_8wekyb3d8bbwe\LocalState\TabState\98c4291f-34a1-4ffe-b195-72e297d4ff60.bin",np.WriteContent(Encoding.Unicode.GetBytes("How are you?")));
82+
4883

4984
Console.WriteLine("********** Completed **********");
5085
Console.ReadLine();
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
using NotepadStateLibrary;
2+
using CsvHelper;
3+
using System.Globalization;
4+
using CommandLine;
5+
6+
Parser.Default.ParseArguments<Options>(args)
7+
.WithParsed(options =>
8+
{
9+
string tabStateLocation = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), @"Packages\Microsoft.WindowsNotepad_8wekyb3d8bbwe\LocalState\TabState");
10+
string windowStateLocation = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), @"Packages\Microsoft.WindowsNotepad_8wekyb3d8bbwe\LocalState\WindowState");
11+
string outputLocation = Directory.GetCurrentDirectory();
12+
13+
if (!string.IsNullOrWhiteSpace(options.tabStateLocation))
14+
{
15+
tabStateLocation = options.tabStateLocation;
16+
}
17+
if (!string.IsNullOrWhiteSpace(options.windowStateLocation))
18+
{
19+
windowStateLocation = options.windowStateLocation;
20+
}
21+
if (!string.IsNullOrWhiteSpace(options.outputLocation))
22+
{
23+
outputLocation = options.outputLocation;
24+
}
25+
26+
Console.WriteLine("********** Starting **********");
27+
Console.WriteLine("TabState Folder Location - {0}", tabStateLocation);
28+
if (!Directory.Exists(tabStateLocation))
29+
{
30+
Console.WriteLine("!! ERROR: Invalid TabState Folder Location !!");
31+
Environment.Exit(3);
32+
}
33+
34+
Console.WriteLine("WindowState Folder Location - {0}", windowStateLocation);
35+
if (!Directory.Exists(windowStateLocation))
36+
{
37+
Console.WriteLine("!! ERROR: Invalid WindowState Folder Location !!");
38+
Environment.Exit(3);
39+
}
40+
41+
Console.WriteLine("Output Folder Location - {0}", outputLocation);
42+
if (!Directory.Exists(outputLocation))
43+
{
44+
Console.WriteLine("!! ERROR: Invalid Output Folder Location !!");
45+
Environment.Exit(3);
46+
}
47+
48+
Console.WriteLine();
49+
50+
List<NPTabState> fileTabs = new List<NPTabState>();
51+
List<NPTabState> noFileTabs = new List<NPTabState>();
52+
List<NPTabState> stateTabs = new List<NPTabState>();
53+
List<NPWindowState> windowStateTabs = new List<NPWindowState>();
54+
55+
//Tabstate
56+
foreach (var path in Directory.EnumerateFiles(tabStateLocation, "*.bin"))
57+
{
58+
byte[] b = File.ReadAllBytes(path);
59+
if (b.Length > 0)
60+
{
61+
Console.WriteLine("Processing TabState - {0}", Path.GetFileName(path));
62+
NPTabState np = new NPTabState(b, Path.GetFileName(path));
63+
64+
switch (np.TypeFlag)
65+
{
66+
case 0:
67+
noFileTabs.Add(np);
68+
break;
69+
case 1:
70+
fileTabs.Add(np);
71+
break;
72+
default:
73+
stateTabs.Add(np);
74+
break;
75+
}
76+
}
77+
}
78+
79+
//Writing File Tabs
80+
using (var writer = new StreamWriter(Path.Combine(outputLocation, "FileTabs.csv")))
81+
{
82+
using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
83+
{
84+
csv.WriteHeader<NPTabState>();
85+
csv.NextRecord();
86+
csv.WriteRecords(fileTabs);
87+
}
88+
}
89+
90+
//Writing No File Tabs
91+
using (var writer = new StreamWriter(Path.Combine(outputLocation, "NoFileTabs.csv")))
92+
{
93+
using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
94+
{
95+
csv.WriteHeader<NPTabState>();
96+
csv.NextRecord();
97+
csv.WriteRecords(noFileTabs);
98+
}
99+
}
100+
101+
//Writing State Tabs
102+
using (var writer = new StreamWriter(Path.Combine(outputLocation, "StateTabs.csv")))
103+
{
104+
using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
105+
{
106+
csv.WriteHeader<NPTabState>();
107+
csv.NextRecord();
108+
csv.WriteRecords(stateTabs);
109+
}
110+
}
111+
112+
//Windowstate
113+
foreach (var path in Directory.EnumerateFiles(windowStateLocation, "*.bin"))
114+
{
115+
byte[] b = File.ReadAllBytes(path);
116+
if (b.Length > 0)
117+
{
118+
Console.WriteLine("Processing WindowState - {0}", Path.GetFileName(path));
119+
NPWindowState np = new NPWindowState(b, Path.GetFileName(path));
120+
windowStateTabs.Add(np);
121+
}
122+
}
123+
124+
//Writing Window States
125+
using (var writer = new StreamWriter(Path.Combine(outputLocation, "WindowStateTabs.csv")))
126+
{
127+
using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
128+
{
129+
csv.WriteHeader<NPWindowState>();
130+
csv.NextRecord();
131+
csv.WriteRecords(windowStateTabs);
132+
}
133+
}
134+
135+
Console.WriteLine("\r\n********** Finished **********");
136+
137+
})
138+
.WithNotParsed(errors =>
139+
{
140+
foreach (var error in errors)
141+
{
142+
if (error is HelpRequestedError || error is VersionRequestedError)
143+
{
144+
145+
}
146+
else
147+
{
148+
Console.WriteLine($"Error: {error}");
149+
}
150+
}
151+
});
152+
153+
public class Options
154+
{
155+
[Option('t', "tabstatelocation", Required = false, HelpText = "Tab State Folder Location. Default value is the system location.")]
156+
public string tabStateLocation { get; set; }
157+
158+
[Option('w', "windowstatelocation", Required = false, HelpText = "Window State Folder Location. Default value is the system location.")]
159+
public string windowStateLocation { get; set; }
160+
161+
[Option('o', "outputlocation", Required = false, HelpText = "Output Folder Location for CSV files. Default location is same folder as program.")]
162+
public string outputLocation { get; set; }
163+
164+
165+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="CommandLineParser" Version="2.9.1" />
12+
<PackageReference Include="CsvHelper" Version="33.0.1" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<ProjectReference Include="..\NotepadStateLibrary\NotepadStateLibrary.csproj" />
17+
</ItemGroup>
18+
19+
</Project>

0 commit comments

Comments
 (0)