Skip to content

Commit

Permalink
add crctrace
Browse files Browse the repository at this point in the history
  • Loading branch information
ohac committed Dec 6, 2012
0 parents commit cc44aa9
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 0 deletions.
4 changes: 4 additions & 0 deletions crctrace/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
all:
gcc -Wall crctrace.c
./a.out
ruby crctrace.rb
43 changes: 43 additions & 0 deletions crctrace/crctrace.c
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;
}
54 changes: 54 additions & 0 deletions crctrace/crctrace.rb
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

0 comments on commit cc44aa9

Please sign in to comment.