public string ExtractBoschPartnumber(byte[] allBytes)
{
string retval = string.Empty;
try
{
int partnumberAddress = Tools.Instance.findSequence(allBytes, 0, new byte[5] { 0x45, 0x44, 0x43, 0x20, 0x20 }, new byte[5] { 1, 1, 1, 1, 1 });
if (partnumberAddress > 0)
{
// for EDC
retval = System.Text.ASCIIEncoding.ASCII.GetString(allBytes, partnumberAddress + 23, 10).Trim();
if (StripNonAscii(retval).Length < 10)
{
// try again, read from "EDC" id - 0x100 to EDC id + 100 and find 10 digit sequence
retval = FindDigits(allBytes, partnumberAddress - 0x100, partnumberAddress + 0x100, 10);
}
if (retval == string.Empty)
{
// try EDC16, other partnumber struct
retval = FindAscii(allBytes, partnumberAddress - 0x100, partnumberAddress + 0x100, 11);
if (retval.StartsWith("ECM"))
{
retval = FindAscii(allBytes, partnumberAddress - 0x100, partnumberAddress + 0x100, 19);
}
}
}
else
{
partnumberAddress = Tools.Instance.findSequence(allBytes, 0, new byte[4] { 0x30, 0x32, 0x38, 0x31}, new byte[4] { 1, 1, 1, 1 });
if (partnumberAddress > 0)
{
retval = System.Text.ASCIIEncoding.ASCII.GetString(allBytes, partnumberAddress, 10).Trim();
}
}
if (retval == string.Empty)
{
partnumberAddress = Tools.Instance.findSequence(allBytes, 0, new byte[4] { 0x30, 0x32, 0x38, 0x31 }, new byte[4] { 1, 1, 1, 1 });
if (partnumberAddress > 0)
{
retval = System.Text.ASCIIEncoding.ASCII.GetString(allBytes, partnumberAddress, 10).Trim();
}
}
if (retval == string.Empty)
{
// check 512 kB EDC17 file, audi Q7 3.0TDI
partnumberAddress = Tools.Instance.findSequence(allBytes, 0, new byte[7] { 0x45, 0x44, 0x43, 0x31, 0x37, 0x20, 0x20 }, new byte[7] { 1, 1, 1, 1, 1, 1, 1 });
if (partnumberAddress > 0)
{
retval = "EDC17" + System.Text.ASCIIEncoding.ASCII.GetString(allBytes, partnumberAddress - 68, 10).Trim();
}
if (retval == string.Empty)
{
//EDC17_CPx4 = Audi
//EDC17_CPx2 = BMW?
partnumberAddress = Tools.Instance.findSequence(allBytes, 0, Encoding.ASCII.GetBytes("EDC17_"), new byte[6] { 1, 1, 1, 1, 1, 1 });
if (partnumberAddress > 0)
{
retval = "EDC17";
}
}
}
if(retval == string.Empty) {
// 1. General lookup for EDC17-string in file
int pos = Tools.Instance.findSequence(allBytes, 0, Encoding.ASCII.GetBytes("ME(D)/EDC17"), new byte[11] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 });
if(pos > 0) {
Console.WriteLine("Found ME(D)/EDC17 in firmware!");
retval = "EDC17";
}
// 2. BMW has 0281xxxxxx (boschnumber) written at FD00 and FE33
// 3. BMW has 10373xxxxx (softwarenumber)written at 0x4001A, use this as a backup if boschnumber can't be found
StringBuilder sb1 = new StringBuilder();
StringBuilder sb2 = new StringBuilder();
bool identified = true;
// Check1
for(int offset = 0xFD00;offset < 0xFD00 + 10;offset++) {
byte b = allBytes[offset];
b -= 0x30;
if(b < 0 || b > 9)
identified = false;
sb1.Append(b.ToString());
}
for(int offset = 0xFE33;offset < 0xFE33 + 10;offset++) {
byte b = allBytes[offset];
b -= 0x30;
if(b < 0 || b > 9)
identified = false;
sb2.Append(b.ToString());
}
//Console.WriteLine("sb1=" + sb1.ToString());
//Console.WriteLine("sb2=" + sb2.ToString());
if(identified && sb1.ToString() == sb2.ToString()) {
partnumberAddress = 0xFD00;
retval = sb1.ToString();
}
StringBuilder sb3 = new StringBuilder();
identified = true;
// Check2
for(int offset = 0x4001A;offset < 0x4001A + 10;offset++) {
byte b = allBytes[offset];
b -= 0x30;
if(b < 0 || b > 9)
identified = false;
sb3.Append(b.ToString());
}
//Console.WriteLine("sb3=" + sb3.ToString());
if(identified) {
partnumberAddress = 0x4001A;
retval = sb3.ToString();
}
}
if (retval.StartsWith("EDC17"))
{
if (Tools.Instance.findSequence(allBytes, 0, Encoding.ASCII.GetBytes("EDC17_CPx4"), new byte[10] { 1, 1, 1, 1, 1, 1, 1, 1, 0, 1 }) > 0)
{
Tools.Instance.m_carMake = CarMakes.Audi;
}
else if (Tools.Instance.findSequence(allBytes, 0, Encoding.ASCII.GetBytes("EDC17_CPx2"), new byte[10] { 1, 1, 1, 1, 1, 1, 1, 1, 0, 1 }) > 0)
{
Tools.Instance.m_carMake = CarMakes.BMW;
}
else if (Tools.Instance.findSequence(allBytes, 0, Encoding.ASCII.GetBytes("EDC17_C06"), new byte[9] { 1, 1, 1, 1, 1, 1, 1, 1, 1 }) > 0)
{
Tools.Instance.m_carMake = CarMakes.BMW;
}
}
}
catch (Exception)
{
}
retval = StripNonAscii(retval);
return retval;
}