0% found this document useful (0 votes)
41 views

Using System

This code parses bytes from a PSD file into a CSV file. It reads bytes from the PSD file into a byte array, then loops through the bytes and extracts packet information fields like packet number, timestamp, frame control fields, RSSI, sequence number, addresses etc. It writes each packet's data as a CSV line to a output file.

Uploaded by

aleksandar7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views

Using System

This code parses bytes from a PSD file into a CSV file. It reads bytes from the PSD file into a byte array, then loops through the bytes and extracts packet information fields like packet number, timestamp, frame control fields, RSSI, sequence number, addresses etc. It writes each packet's data as a CSV line to a output file.

Uploaded by

aleksandar7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

using System;

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

private void button1_Click(object sender, EventArgs e)


{

// Specify a file to read from and to create.


string PSDSourceFile = @txtSelectPSD.Text; //"test2.psd";
// string pathNew = @"test.txt";

try
{

using (FileStream fsSource = new FileStream(PSDSourceFile, FileMode.Open,


FileAccess.Read))
{

// Read the source file into a byte array.


byte[] bytes = new byte[fsSource.Length];
byte[] bytesHex = new byte[fsSource.Length];

int numBytesToRead = (int)fsSource.Length;


int numBytesRead = 0;
while (numBytesToRead > 0)
{
// Read may return anything from 0 to numBytesToRead.
int n = fsSource.Read(bytes, numBytesRead, numBytesToRead);

// Break when the end of the file is reached.


if (n == 0)
break;

numBytesRead += n;
numBytesToRead -= n;
}
numBytesToRead = bytes.Length;

// Write the byte array to the other FileStream.


// using (FileStream fsNew = new FileStream(pathNew,
// FileMode.Create, FileAccess.Write))
// {
// fsNew.Write(bytes, 0, numBytesToRead);
// }

// string hex = BitConverter.ToString(bytes);

// using (System.IO.StreamWriter fsNew3 = new System.IO.StreamWriter("test3.txt",


true))
// {
// fsNew3.Write(hex);
// }

// using (System.IO.StreamWriter fsNew2 = new System.IO.StreamWriter("test2.txt",


true))
// {
// int PacketBytes = 0;
// for (int i = 0; i <= numBytesRead - 1; i++)
// {
// PacketBytes++;
// if (PacketBytes < 151)
// {
// fsNew2.Write(bytes[i]);
// }
// else
// {
// fsNew2.WriteLine(bytes[i]);
// PacketBytes = 0;
// }
// }
// }

// using (System.IO.StreamWriter fsNew4 = new System.IO.StreamWriter("testBits.txt",


true))
// {
//
// for (int i = 0; i <= 151; i++)
// {
// string Bits = Convert.ToString(bytes[i], 2).PadLeft(8, '0');
// fsNew4.WriteLine(Convert.ToString(i) + ',' + Bits);
// }
// }

string ParseFileName = txtFilenamePrefix.Text;

using (System.IO.StreamWriter fsNew2 = new System.IO.StreamWriter(ParseFileName +


".csv", true))
{
long PacketTimeStampPrevious = 0;
for (int i = 0; i <= numBytesRead - 1; i += 151)
{
// Extracting packet info
string PacketInfo = Convert.ToString(bytes[i], 2).PadLeft(8, '0');
string PacketInfoNotUsed = PacketInfo.Substring(0, 5);
string PacketInfoIncomplete = PacketInfo.Substring(5, 1);
string PacketInfoCorrelation = PacketInfo.Substring(6, 1);
string PacketInfoWithFCS = PacketInfo.Substring(7, 1);

// Extracting Packet Number


byte[] PacketNumberArray = { bytes[i + 1], bytes[i + 2], bytes[i + 3], bytes[i
+ 4] };
uint PacketNumberInt = BitConverter.ToUInt32(PacketNumberArray, 0);
// Debug.Print(PacketNumberInt.ToString());

// In the case when the conversion Little - Big Endian is needed


// if (BitConverter.IsLittleEndian)
// Array.Reverse(PacketNumberArray);

byte[] PacketTimeStampOffsetArray = { bytes[5], bytes[6], bytes[7], bytes[8],


bytes[9], bytes[10], bytes[11], bytes[12] };
long PacketTimeStampOffset = BitConverter.ToInt64(PacketTimeStampOffsetArray,
0);

byte[] PacketTimeStampArray = { bytes[i + 5], bytes[i + 6], bytes[i + 7],


bytes[i + 8], bytes[i + 9], bytes[i + 10], bytes[i + 11], bytes[i + 12] };
// Array.Reverse(PacketTimeStampArray);
long PacketTimeStampInt = BitConverter.ToInt64(PacketTimeStampArray, 0);
PacketTimeStampInt = (long)((PacketTimeStampInt - PacketTimeStampOffset)/32);
// According to Documentation the value is 26 not 32. Wuth 32 values are same as in RF Packet Sniffer.

long PacketTimeStampDifference = PacketTimeStampInt - PacketTimeStampPrevious;


PacketTimeStampPrevious = PacketTimeStampInt;

// Added by Zlaja - int PacketNumberIntZ = bytes[i + 1] + bytes[i + 2] * 256 +


bytes[i + 3] * 256 * 256 + bytes[i + 4] *256*256*256;

int PacketLenght = Convert.ToInt32(bytes[i+13]);

string FrameControlLeft = Convert.ToString(bytes[i+14], 2).PadLeft(8, '0');


string FrameControlRight = Convert.ToString(bytes[i+15], 2).PadLeft(8, '0');
string FrameControlField = FrameControlRight + FrameControlLeft;

string FrameControlFieldType = FrameControlField.Substring(13, 3);


string FrameControlFieldSecurity = FrameControlField.Substring(12, 1);
string FrameControlFieldFramePending = FrameControlField.Substring(11, 1);
string FrameControlFieldACK = FrameControlField.Substring(10, 1);
string FrameControlFieldIntraPAN = FrameControlField.Substring(9, 1);

// *** sta kada je vodeca cifra 0 ***//


int LQI = Convert.ToInt32(bytes[i+PacketLenght + 14 - 1]) - 128;
// *** sta kada je vodeca cifra 0 ***//

string FCS = Convert.ToString(bytes[i + PacketLenght + 14 - 1],2).PadLeft(8,


'0');
FCS = FCS.Substring(0, 1);
int RSSINew = ~bytes[i+PacketLenght + 14 - 2];

long RSSICalc;
RSSICalc = Convert.ToInt32(bytes[i+PacketLenght + 14 - 2]);
RSSICalc = RSSICalc & 0xFF;

RSSICalc = (-1)*(256 - RSSICalc)-73;

if (RSSICalc < - 256)


{
RSSICalc = RSSICalc + 256;
}

byte[] SeqNumberArray = { bytes[i + 16] };


string SeqNumber = BitConverter.ToString(SeqNumberArray).Replace("-", "");
SeqNumber = "x" + SeqNumber;

string DestPAN;
string DestAddr;
string SourceAddr;

if (PacketLenght != 5)
{
byte[] DestPANArray = { bytes[i + 17], bytes[i + 18] };
Array.Reverse(DestPANArray);
DestPAN = BitConverter.ToString(DestPANArray).Replace("-", "");
DestPAN = "x" + DestPAN;

byte[] DestAddrArray = { bytes[i + 19], bytes[i + 20] };


Array.Reverse(DestAddrArray);
DestAddr = BitConverter.ToString(DestAddrArray).Replace("-", "");
DestAddr = "x" + DestAddr;

byte[] SourceAddrArray = { bytes[i + 21], bytes[i + 22] };


Array.Reverse(SourceAddrArray);
SourceAddr = BitConverter.ToString(SourceAddrArray).Replace("-", "");
SourceAddr = "x" + SourceAddr;
}
else
{
DestAddr = "-";
SourceAddr = "-";
DestPAN = "-";
}

fsNew2.WriteLine(PacketNumberInt + "," + PacketTimeStampInt + "," +


PacketTimeStampDifference + "," + PacketInfoNotUsed + "," + PacketInfoIncomplete + "," +
PacketInfoCorrelation + "," + PacketInfoWithFCS + "," + PacketLenght + "," + FrameControlFieldType + "," +
FrameControlFieldSecurity + "," + FrameControlFieldFramePending + "," + FrameControlFieldACK + "," +
FrameControlFieldIntraPAN + "," + RSSICalc + "," + LQI + "," + FCS + "," + SeqNumber + "," + DestPAN + ","
+ DestAddr + "," + SourceAddr + "," + txtChannel.Text);
}
}
}

MessageBox.Show("Parsing is Finished.");
}
catch (FileNotFoundException ioEx)
{
Console.WriteLine(ioEx.Message);

}
}

You might also like