Skip to content

Commit

Permalink
Added Save Feature
Browse files Browse the repository at this point in the history
YOOOOO It works!
  • Loading branch information
felixgibtseinennachnamennichtan authored Feb 13, 2022
1 parent 7916c02 commit ca25ef4
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 59 deletions.
13 changes: 13 additions & 0 deletions WindowsFormsApp8/Form1.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

123 changes: 64 additions & 59 deletions WindowsFormsApp8/Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ private void button1_Click(object sender, EventArgs e)

using (OpenFileDialog openFileDialog = new OpenFileDialog())
{
openFileDialog.InitialDirectory = "c:\\";
openFileDialog.InitialDirectory = "C:\\Users\\Felix\\Downloads\\txtstuff";
openFileDialog.Filter = "XCP files (*.xcp)|*.xcp";
openFileDialog.FilterIndex = 1;
openFileDialog.RestoreDirectory = true;
Expand Down Expand Up @@ -123,31 +123,50 @@ void DecodeFile(byte[] fileContent) {
}

byte[] EncodeFile() {
byte checksum = 0;
List<byte> xcpFile = new List<byte>();
//Add the Header (VCP.XDATA�5f4d4353)
xcpFile.AddRange(new byte[] { 0x56, 0x43, 0x50, 0x2E, 0x58, 0x44, 0x41, 0x54, 0x41, 0x00, 0x35, 0x66, 0x34, 0x64, 0x34, 0x33, 0x35, 0x33 });

foreach (byte b in new byte[] { 0x56, 0x43, 0x50, 0x2E, 0x58, 0x44, 0x41, 0x54, 0x41, 0x00 }) {
checksum-=b;
}
checksum -= 0x5f;
checksum -= 0x4d;
checksum -= 0x43;
checksum -= 0x53;
//Add the Folderlength + the Schwarzenegger
char[] Folderlength = (richTextBox2.Text.Length+1).ToString("X4").ToCharArray();
char[] Folderlength = (richTextBox2.Text.Length+1).ToString("X2").ToCharArray();
foreach (byte b in BitConverter.GetBytes(richTextBox2.Text.Length + 1)) {
checksum -= b;
}
foreach (char c in Folderlength) {
xcpFile.Add(Convert.ToByte(c));
}
//Add the Foldername and the 0x00 Terminator
char[] Folder =richTextBox2.Text.ToCharArray();
foreach (char c in Folder) {
xcpFile.Add(Convert.ToByte(c));
checksum -= Convert.ToByte(c);
}
xcpFile.Add(0x00);
//Add the Folderlength + the Schwarzenegger
char[] Filenamelength = (richTextBox1.Text.Length + 1).ToString("X4").ToCharArray();

//Add the Filenamelength + the Schwarzenegger
char[] Filenamelength = (richTextBox1.Text.Length + 1).ToString("X2").ToCharArray();
foreach (char c in Filenamelength)
{
xcpFile.Add(Convert.ToByte(c));
}
//Add the Foldername and the 0x00 Terminator
foreach (byte b in BitConverter.GetBytes(richTextBox1.Text.Length + 1))
{
checksum -= b;
}
//Add the Filename and the 0x00 Terminator
char[] Filename = richTextBox1.Text.ToCharArray();
foreach (char c in Filename)
{
xcpFile.Add(Convert.ToByte(c));
checksum -= Convert.ToByte(c);
}
xcpFile.Add(0x00);
//add 8 Bytes of 00000031 (but the num is a String!)
Expand All @@ -156,40 +175,41 @@ byte[] EncodeFile() {
{
xcpFile.Add(Convert.ToByte(c));
}
checksum -= 0x31;
// add the foldername again, but it has to be 16 bytes and if smaller padded with 0xff
bool addedFolder = false;
for (int i = 0; i == 16; i++)

for (int i = 0; i < 16; i++)
{
if (addedFolder == false)
if (i >= Folder.Length)
{
foreach (char c in Folder)
{
xcpFile.Add(Convert.ToByte(c));
i++;
}
addedFolder = true;
xcpFile.Add(0xff);
checksum -= 0xff;
}
xcpFile.Add(0xff);
else {
xcpFile.Add(Convert.ToByte(Folder[i]));
checksum -= Convert.ToByte(Folder[i]);
}

}

bool addedFilename = false;
for (int i = 0; i == 16; i++)
//same for filename
for (int i = 0; i < 16; i++)
{
if (addedFilename == false)
if (i >= Filename.Length)
{
foreach (char c in Filename)
{
xcpFile.Add(Convert.ToByte(c));
i++;
}
addedFilename = true;
xcpFile.Add(0xff);
checksum -= 0xff;
}
else {
xcpFile.Add(Convert.ToByte(Filename[i]));
checksum -= Convert.ToByte(Filename[i]);
}
xcpFile.Add(0xff);

}

//nextup is the length of the Datablock, so i just make it now and check it like that
List<byte> DataBlock = new List<byte>();
UInt32 length = (UInt32) richTextBox3.Text.Length + 3;
DataBlock.AddRange(BitConverter.GetBytes(length).Reverse());
DataBlock.AddRange(BitConverter.GetBytes(length));
//9 Bytes of 0
DataBlock.AddRange(new Byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 });
//now we have to add the actual text
Expand All @@ -201,21 +221,30 @@ byte[] EncodeFile() {
DataBlock.Add(0x00);
DataBlock.Add(0xff);
//and a padding that makes the length value from before a multiple of 4
for (; 0 == DataBlock.Count%4;) {
for (; 0 != DataBlock.Count%4;) {
DataBlock.Add(0x00);
}
//datablock done
//now just add the length to the file
UInt32 Length = (UInt32)DataBlock.Count;
xcpFile.AddRange(BitConverter.GetBytes(Length));
foreach (byte b in BitConverter.GetBytes(Length)) {
checksum -= b;
}
xcpFile.AddRange(BitConverter.GetBytes(Length).Reverse());
//add the Vartype padded in 0xff
List<byte> vartype = new List<byte>();
vartype.AddRange(new byte[] {(byte)'G',(byte)'U',(byte)'Q'});
for (; vartype.Count == 13;) {
for (; vartype.Count < 13;) {
vartype.Add(0xff);
}
foreach (byte b in vartype) {
checksum -= b;
}
xcpFile.AddRange(vartype);
char[] lenn = Length.ToString("x16").ToCharArray();
char[] lenn = Length.ToString("x8").ToCharArray();
foreach (byte b in BitConverter.GetBytes(Length)) {
checksum -= b;
}
List<byte> len = new List<byte>();
foreach (char c in lenn) {

Expand All @@ -225,38 +254,14 @@ byte[] EncodeFile() {
xcpFile.AddRange(len);
//add the DataBlock
xcpFile.AddRange(DataBlock);
//calculate the checksum
int checksum = 0;
foreach (byte b in xcpFile) {
foreach (byte b in DataBlock) {
checksum -= b;
}
List<byte> HexAsciis = new List<byte>();
UInt64 longnum = 0x5f4d4353;
byte[]longnumber = BitConverter.GetBytes(longnum);
foreach (byte b in longnumber) {
HexAsciis.Add(b);
}
//we need to add the Foldernamelength
foreach (char c in Folderlength)
{
HexAsciis.Add(Convert.ToByte(c));
}
foreach (char c in Filenamelength)
{
HexAsciis.Add(Convert.ToByte(c));
}
HexAsciis.AddRange(new byte[] { 30, 30, 30, 30, 30, 30, 33, 31 });
HexAsciis.AddRange(len);
// now add every element of HexAsciis to the Checksum
foreach (byte b in HexAsciis) {
checksum += b;
}

foreach (byte b in HexAsciis)
{
checksum -= Int32.Parse(b.ToString());
char[] chekksum = checksum.ToString("x2").ToCharArray();
foreach (char c in chekksum) {
xcpFile.Add((byte)c);
}
xcpFile.Add(BitConverter.GetBytes(checksum)[3]);
return xcpFile.ToArray();


Expand Down
Binary file modified WindowsFormsApp8/bin/Debug/WindowsFormsApp8.exe
Binary file not shown.
Binary file modified WindowsFormsApp8/bin/Debug/WindowsFormsApp8.pdb
Binary file not shown.
Binary file added WindowsFormsApp8/bin/Debug/XCP-Viewer.exe
Binary file not shown.
Binary file not shown.

0 comments on commit ca25ef4

Please sign in to comment.