-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathread_sgo.php
138 lines (114 loc) · 3.54 KB
/
read_sgo.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
echo "file: ".$argv[1]."\n";
//pasring_based on
//https://ideone.com/0YAVfY
//https://forum.skodahome.cz/topic/123830-podtlakova-trubka-brzdoveho-valca/
//https://strserg.com/VAG_dataflash/
$dump_file = false;
if(isset($argv[2]) && $argv[2] == 'dump'){
$dump_file = true;
}
$fh = fopen($argv[1], 'r');
$fdump = false;
if($dump_file){
$fdump = fopen($argv[1].'.bin', 'w');
}
$header = array();
$header['sgoLabel'] = fread($fh, 17);
$header['SGO_COMPATIBILITY'] = read_int($fh);
$header['checksum'] = read_int($fh);
//Where in the file are diffrent structures located
$header['indexIDENT'] = read_int($fh);
$header['indexbaudrate_2000'] = read_int($fh);
$header['indexkwp_2000_rei'] = read_int($fh);
$header['indexkwp_2000_acp'] = read_int($fh);
$header['indexkwp_2000_sa2'] = read_int($fh);
$header['index_DATA_BLOCKS'] = read_int($fh);
//Read Ident data
fseek($fh, $header['indexIDENT']);
$header['cnt_file'] = (xorstr(fread($fh, 260)));
$header['sw_version_kurz_String'] = xorstr((fread($fh, 5)));
$header['sw_version_kurz_DWord'] = read_int($fh);
//Read sa2 data
fseek($fh, $header['indexkwp_2000_sa2']);
$header['SGO_KWP_2000_SA2_nritems'] = read_int($fh);
$header['SGO_KWP_2000_SA2'] = strToHex((fread($fh, $header['SGO_KWP_2000_SA2_nritems'])));
//Read acctual data blocks header
fseek($fh, $header['index_DATA_BLOCKS']);
$header['SGO_DATENBLOCKE_nritems'] = read_int($fh);
$header['SGO_DATENBLOCKE'] = array();
for($i=0;$i<$header['SGO_DATENBLOCKE_nritems'];$i++){
$header['SGO_DATENBLOCKE'][] = read_int($fh);
}
$data_blocks = array();
//Read acctual data blocks
foreach($header['SGO_DATENBLOCKE'] AS $block_index){
fseek($fh, $block_index);
$block = array();
$block['start_adr'] = read_3byte_adr($fh);
$block['data_block_format'] = strToHex((fread($fh, 1)));
$block['size_after_decompression'] = read_3byte_adr($fh);
$block['SGO_LOESCH_BEREICH_start_adr'] = read_3byte_adr($fh);
$block['SGO_LOESCH_BEREICH_end_adr'] = read_3byte_adr($fh);
$block['SGO_DATENBLOCK_CHECK_start_adr'] = read_3byte_adr($fh);
$block['SGO_DATENBLOCK_CHECK_end_adr'] = read_3byte_adr($fh);
$block['SGO_DATENBLOCK_CHECK_checksum'] = strToHex((fread($fh, 2)));
$block['SGO_DATENBLOCK_datenblock_daten_size'] = read_int($fh);
$data = fread($fh, $block['SGO_DATENBLOCK_datenblock_daten_size']);
//$block['SGO_DATENBLOCK_data'] = strToHex($data);
if($block['size_after_decompression'] != $block['SGO_DATENBLOCK_datenblock_daten_size']){
throw new Exception("commrepssion used");
}
if($fdump){
$sp = fseek($fdump, $block['start_adr']);
if($sp != 0){
throw new Exception("failed to seek");
}
fwrite($fdump, xordat($data));
}
//$block[''] = strToHex((fread($fh, 1)));
$data_blocks[] = $block;
}
var_dump($header);
var_dump($data_blocks);
if($fdump){
fclose($fdump);
echo "dumped file\n";
}else{
}
function read_3byte_adr($fh){
$start_adr = fread($fh, 3);
$start_adr = $start_adr[2].$start_adr[1].$start_adr[0]."\0";
return unpack('V', $start_adr)[1];
}
function read_int($fh){
return unpack('V', fread($fh, 4))[1];
}
function xordat($string){
$str = '';
for ($i=0; $i<strlen($string); $i++){
$c = ord($string[$i]);
$str .= chr($c^0xff);
}
return $str;
}
function xorstr($string){
$str = '';
for ($i=0; $i<strlen($string); $i++){
$c = ord($string[$i]);
if($c == 0){
break;
}
$str .= chr($c^0xff);
}
return $str;
}
function strToHex($string){
$hex = '';
for ($i=0; $i<strlen($string); $i++){
$ord = ord($string[$i]);
$hexCode = dechex($ord);
$hex .= substr('0'.$hexCode, -2).' ';
}
return strToUpper($hex);
}