Code:
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
public class NFile
{
private string name { get; set; }
private int fileNumber { get; set; }
private int isDat { get; set; }
private byte[] data { get; set; }
public NFile(string _name, int _fileNumber, int _isDat, byte[] _data)
{
name = _name;
fileNumber = _fileNumber;
isDat = _isDat;
data = _data;
}
}
public static class NosTxtDecryptor
{
private static readonly byte[] cryptoArray = { 0x00, 0x20, 0x2D, 0x2E, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x0A, 0x00 };
private static byte[] decrypt(byte[] array)
{
List<byte> decryptedFile = new List<byte>();
int currindex = 0;
while (currindex < array.Length)
{
char currentByte = Convert.ToChar(array[currindex++]);
if (currentByte == 0xFF)
{
decryptedFile.Add(0xD);
continue;
}
int validate = currentByte & 0x7F;
if ((currentByte & 0x80) != 0)
{
for (; validate > 0; validate -= 2)
{
if (currindex >= array.Length)
{
break;
}
currentByte = Convert.ToChar(array[currindex++]);
decryptedFile.Add(cryptoArray[(currentByte & 0xF0) >> 4]);
if (validate <= 1)
{
break;
}
if (cryptoArray[currentByte & 0xF] == 0)
{
break;
}
decryptedFile.Add(cryptoArray[currentByte & 0xF]);
}
}
else
{
for (; validate > 0; --validate)
{
if (currindex >= array.Length)
{
break;
}
currentByte = Convert.ToChar(array[currindex++]);
decryptedFile.Add(Convert.ToByte(currentByte ^ 0x33));
}
}
}
return decryptedFile.ToArray();
}
public static List<NFile> GetNFiles(FileStream f)
{
List<NFile> retList = new List<NFile>();
f.Seek(0, SeekOrigin.Begin);
int fileAmount = f.readNextInt();
for (int i = 0; i < fileAmount; i++)
{
int fileNumber = f.readNextInt();
int stringNameSize = f.readNextInt();
string stringName = f.readString(stringNameSize);
int isDat = f.readNextInt();
int fileSize = f.readNextInt();
byte[] fileContent = f.readLength(fileSize);
retList.Add(new NFile(stringName, fileNumber, isDat, decrypt(fileContent)));
}
return retList;
}
private static int readNextInt(this FileStream fileSteam)
{
byte[] buffer = new byte[4];
fileSteam.Read(buffer, 0, 4);
return BitConverter.ToInt32(buffer, 0);
}
private static string readString(this FileStream fileStream, int count)
{
byte[] buffer = new byte[count];
fileStream.Read(buffer, 0, count);
return Encoding.Default.GetString(buffer);
}
private static byte[] readLength(this FileStream fileStream, int count)
{
byte[] buffer = new byte[count];
fileStream.Read(buffer, 0, count);
return buffer;
}
}