Skip to content

Instantly share code, notes, and snippets.

@yshl

yshl/crc32quine.c

Last active Aug 29, 2015
Embed
What would you like to do?
Search CRC-32 Quine
#include<stdio.h>
#include<stdint.h>
#ifdef CAPITAL
#define CHAR_A 'A'
#define FORMAT "%08X\n"
#else
#define CHAR_A 'a'
#define FORMAT "%08x\n"
#endif
#define hexchar(c) ((unsigned char)((c)<10?(c)+'0':(c)-10+CHAR_A))
#define crc32(crc, n) (crc_table[((crc)^(n))&0xff]^((crc)>>8))
#define POLYNOMIAL 0xedb88320
void crc_init(uint32_t *crc_table)
{
unsigned int i;
for(i=0; i<0x100; i++){
uint32_t crc=i;
int j;
for(j=0; j<8; j++){
uint32_t shift1=crc>>1;
crc=crc&1?shift1^POLYNOMIAL:shift1;
}
crc_table[i]=crc;
}
}
int main()
{
uint32_t crc_table[0x100];
uint32_t crc0=~0;
crc_init(crc_table);
unsigned char i1;
for(i1=0; i1<0x10; i1++){
uint32_t crc1=crc32(crc0,hexchar(i1));
unsigned char i2;
for(i2=0; i2<0x10; i2++){
uint32_t crc2=crc32(crc1,hexchar(i2));
unsigned char i3;
for(i3=0; i3<0x10; i3++){
uint32_t crc3=crc32(crc2,hexchar(i3));
unsigned char i4;
for(i4=0; i4<0x10; i4++){
uint32_t crc4=crc32(crc3,hexchar(i4));
unsigned char i5;
for(i5=0; i5<0x10; i5++){
uint32_t crc5=crc32(crc4,hexchar(i5));
unsigned char i6;
for(i6=0; i6<0x10; i6++){
uint32_t crc6=crc32(crc5,hexchar(i6));
unsigned char i7;
for(i7=0; i7<0x10; i7++){
uint32_t crc7=crc32(crc6,hexchar(i7));
unsigned char i8;
for(i8=0; i8<0x10; i8++){
uint32_t crc8=crc32(crc7,hexchar(i8));
#ifdef CR
crc8=crc32(crc8,'\r');
#endif
#ifdef LF
crc8=crc32(crc8,'\n');
#endif
uint32_t i0=(i1<<28)|(i2<<24)|(i3<<20)
|(i4<<16)|(i5<<12)|(i6<<8)|(i7<<4)|i8;
if(~crc8==i0){
printf(FORMAT, i0);
}
}
}
}
}
}
}
}
}
return 0;
}
#include<stdio.h>
#include<stdint.h>
#define crc32(crc, n) (crc_table[((crc)^(n))&0xff]^((crc)>>8))
#define POLYNOMIAL 0xedb88320
void crc_init(uint32_t *crc_table)
{
unsigned int i;
for(i=0; i<0x100; i++){
uint32_t crc=i;
int j;
for(j=0; j<8; j++){
uint32_t shift1=crc>>1;
crc=crc&1?shift1^POLYNOMIAL:shift1;
}
crc_table[i]=crc;
}
}
int main()
{
uint32_t crc_table[0x100];
uint32_t crc0=~0;
crc_init(crc_table);
unsigned int i1;
for(i1=0; i1<0x100; i1++){
uint32_t crc1=crc32(crc0,i1);
unsigned int i2;
for(i2=0; i2<0x100; i2++){
uint32_t crc2=crc32(crc1,i2);
unsigned int i3;
for(i3=0; i3<0x100; i3++){
uint32_t crc3=crc32(crc2,i3);
unsigned int i4;
for(i4=0; i4<0x100; i4++){
uint32_t crc4=crc32(crc3,i4);
uint32_t i0;
#ifdef BIG_ENDIAN
i0=(i1<<24)|(i2<<16)|(i3<<8)|i4;
#else
i0=(i4<<24)|(i3<<16)|(i2<<8)|i1;
#endif
if(~crc4==i0){
printf("%08x\n", i0);
}
}
}
}
}
return 0;
}
#!/bin/sh
src=crc32quine.c
prg=crc32quine
echo text
gcc -Wall -O3 -o $prg $src
time ./$prg
echo text LF
gcc -Wall -O3 -DLF -o $prg $src
time ./$prg
echo text CRLF
gcc -Wall -O3 -DCR -DLF -o $prg $src
time ./$prg
echo text CAPITAL
gcc -Wall -O3 -DCAPITAL -o $prg $src
time ./$prg
echo text CAPITAL LF
gcc -Wall -O3 -DCAPITAL -DLF -o $prg $src
time ./$prg
echo text CAPITAL CRLF
gcc -Wall -O3 -DCAPITAL -DCR -DLF -o $prg $src
time ./$prg
src=crc32quine_raw.c
prg=crc32quine_raw
echo binary LITTLE ENDIAN
gcc -Wall -O3 -o $prg $src
time ./$prg
echo binary BIG ENDIAN
gcc -Wall -O3 -DBIG_ENDIAN -o $prg $src
time ./$prg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment