-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit cc44aa9
Showing
3 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
all: | ||
gcc -Wall crctrace.c | ||
./a.out | ||
ruby crctrace.rb |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#include <stdio.h> | ||
#include <string.h> | ||
|
||
static unsigned short crctracecrc; | ||
void crctraceout(FILE* fp) | ||
{ | ||
fprintf(fp, "%04x\n", crctracecrc); | ||
} | ||
|
||
void crctraceimpl(char* filename, int lineno) | ||
{ | ||
unsigned char c = (unsigned char)lineno; | ||
unsigned short crc = crctracecrc; | ||
int i = 0; | ||
int l = strlen(filename); | ||
for (; i < l; i++) c ^= filename[i]; | ||
for (i = 0; i < 8; i++) { | ||
unsigned char a = c; | ||
if (crc & 1) a++; | ||
crc >>= 1; | ||
if (a & 1) crc ^= 0xa001; | ||
c >>= 1; | ||
} | ||
crctracecrc = crc; | ||
} | ||
#define crctrace() crctraceimpl(__FILE__, __LINE__) | ||
|
||
void sub(int i) { | ||
crctrace(); | ||
if (i < 10) { | ||
crctrace(); | ||
} | ||
} | ||
|
||
int main(int argc, char **argv) | ||
{ | ||
crctrace(); | ||
sub(7); | ||
crctrace(); | ||
sub(17); | ||
crctraceout(stdout); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#!/usr/bin/ruby | ||
def checkpointid(filename, lineno) | ||
filename.each_byte.inject(lineno){|a,b|a^b} & 0xff | ||
end | ||
|
||
def crc16(ptn) | ||
crc = 0 | ||
ptn.each do |c| | ||
8.times do | ||
a = c | ||
a += 1 if (crc & 1) == 1 | ||
crc >>= 1 | ||
crc ^= 0xa001 if (a & 1) == 1 | ||
c >>= 1 | ||
end | ||
end | ||
crc | ||
end | ||
|
||
files = Dir.glob('*.c') | ||
checkpoints = {} | ||
files.each do |filename| | ||
body = File.open(filename, 'r'){|fd|fd.read} | ||
body.split("\n").each_with_index do |line, l| | ||
if /crctrace\(\);/ === line | ||
id = checkpointid(filename, l + 1) | ||
checkpoints[id] = "#{filename}:#{l + 1}:1:" | ||
end | ||
end | ||
end | ||
|
||
ucheckpoints = checkpoints.keys.uniq | ||
if checkpoints.size != ucheckpoints.size | ||
puts 'error: not unique' | ||
exit | ||
end | ||
|
||
5.times do | ||
(1..6).each do |len| | ||
u = ucheckpoints * len | ||
try = ucheckpoints.size * len * 100 | ||
try.times do | ||
ptn = u.take(len) | ||
if crc16(ptn) == 0xfbc3 # TODO | ||
puts "found: %s" % ptn.join(', ') | ||
ptn.each do |id| | ||
puts checkpoints[id] | ||
end | ||
exit | ||
end | ||
u.shuffle! | ||
end | ||
end | ||
end |