diff --git a/2012/omoikane/.entry.json b/2012/omoikane/.entry.json index 7ed13239c..adf77d0a1 100644 --- a/2012/omoikane/.entry.json +++ b/2012/omoikane/.entry.json @@ -21,6 +21,14 @@ "display_via_github" : true, "entry_text" : "entry Makefile" }, + { + "file_path" : "binmode_main.h", + "inventory_order" : 200, + "OK_to_edit" : true, + "display_as" : "c", + "display_via_github" : true, + "entry_text" : "Preprocessor hack to set binary mode" + }, { "file_path" : "omoikane.alt.c", "inventory_order" : 40, @@ -29,14 +37,6 @@ "display_via_github" : true, "entry_text" : "alternate source code" }, - { - "file_path" : "omoikane.alt2.c", - "inventory_order" : 40, - "OK_to_edit" : true, - "display_as" : "c", - "display_via_github" : true, - "entry_text" : "second alternate source code" - }, { "file_path" : "omoikane.orig.c", "inventory_order" : 50, diff --git a/2012/omoikane/Makefile b/2012/omoikane/Makefile index 4f971bb6f..71f1eea26 100644 --- a/2012/omoikane/Makefile +++ b/2012/omoikane/Makefile @@ -62,7 +62,7 @@ CDEFINE= # Include files that are needed to compile # -CINCLUDE= +CINCLUDE=-include binmode_main.h # Optimization # @@ -146,15 +146,6 @@ ${PROG}.alt: ${PROG}.alt.c ${PROG}.alt.c: ${ENTRY}.alt.c ${CP} -f -v $? $@ -alt2: data ${ALT_TARGET}2 - @${TRUE} - -${PROG}.alt2: ${PROG}.alt2.c - ${CC} ${CFLAGS} $< -o $@ ${LDFLAGS} - -${PROG}.alt2.c: ${ENTRY}.alt2.c - ${CP} -f -v $? $@ - # data files # data: ${DATA} @@ -175,7 +166,7 @@ clean: clobber: clean ${RM} -f ${TARGET} ${ALT_TARGET} - ${RM} -f ${PROG}.c ${PROG}.alt.c ${PROG}.alt2.c + ${RM} -f ${PROG}.c ${PROG}.alt.c ${RM} -rf *.dSYM ${RM} -f output.c key.c data.c regenerated.bin data output output.2 diff --git a/2012/omoikane/README.md b/2012/omoikane/README.md index eec4cab6e..3a82a79c2 100644 --- a/2012/omoikane/README.md +++ b/2012/omoikane/README.md @@ -5,9 +5,7 @@ ``` There is an alternate version that will read from the compiled file itself, in -case you don't have a `/dev/urandom` file. There is also a version that should -theoretically work with Windows which distinguishes binary and text. See -[alternate code](#alternate-code) below. +case you don't have a `/dev/urandom` file. ## To use: @@ -43,30 +41,19 @@ perl parts of the script. The alternate version will, if no file is specified, read in the compiled binary itself rather than trying to open `/dev/urandom`, which is the default for the original entry. This is useful if your system does not have a `/dev/urandom` -file and you do not want to specify an extra file. The second alternate version -is like the first except it also should theoretically work with Windows, setting -binary mode on `stdin` and `stdout`. +file and you do not want to specify an extra file. ### Alternate build: -For the first version: - - ``` make alt ``` -For the second version: - -``` - make alt2 -``` - ### Alternate use: -Use `nyaruko.alt` or `nyaruko.alt2` as you would `nyaruko` above. +Use `nyaruko.alt` as you would `nyaruko` above. ### Alternate try: @@ -77,8 +64,6 @@ For the first alternate version: ./try.alt.sh ``` -We have no way to test the second version, sorry (tm Canada 🇨🇦 :-) )! - ## Judges' remarks: @@ -190,7 +175,17 @@ combinations: * tcc 0.9.25 on JSLinux Note that on MingW, `stdin` and `stdout` are not opened in binary mode by -default, this means `Nyaruko` may not faithfully encode files on MingW. +default, this means `Nyaruko` may not faithfully reproduce files on MingW. +As a workaround, a `binmode_main.h` is included, so it's possible to produce +an encoder that's binary-clean like this: + +``` + x86_64-w64-mingw32-gcc -include binmode_main.h nyaruko.c -o nyaruko.exe +``` + +The C code produced by the encoder will need to be compiled with the same +`-include binmode_main.h` hack if you want its output to faithfully reproduce +the original binary input. ### NOTICE to those who wish for a greater challenge: diff --git a/2012/omoikane/binmode_main.h b/2012/omoikane/binmode_main.h new file mode 100644 index 000000000..112aa054c --- /dev/null +++ b/2012/omoikane/binmode_main.h @@ -0,0 +1,39 @@ +/* Preprocessor hack to set binary mode for stdin and stdout. + + This replaces main() with an alternative function forces binary + modes. This is intended to included via compiler arguments, so we + would be able to enable binary modes without modifying the original + program source. Conceptually, it's similar to LD_PRELOAD, except + we hack it in via preprocessor. + + It only works for programs where main() occurs just once. +*/ + +#if defined(_WIN32) + #include + #include + #include + #include + + #define main \ + /* Forward declaration for the updated main(), now renamed */ \ + /* binmode_main(). Return type is omitted since this macro */ \ + /* is expanded just after the return type. */ \ + binmode_main(int argc, char **argv); \ + \ + /* Updated main(). */ \ + int main(int argc, char **argv) \ + { \ + /* Set binary modes. */ \ + setmode(STDIN_FILENO, O_BINARY); \ + setmode(STDOUT_FILENO, O_BINARY); \ + \ + /* Call the original main(). */ \ + return binmode_main(argc, argv); \ + } \ + \ + /* Definition for the original main. Function arguments are */ \ + /* omitted since this macro is expanded just before the arguments. */ \ + int binmode_main + +#endif diff --git a/2012/omoikane/omoikane.alt.c b/2012/omoikane/omoikane.alt.c index 5bb7e2927..a99f643b0 100644 --- a/2012/omoikane/omoikane.alt.c +++ b/2012/omoikane/omoikane.alt.c @@ -1,44 +1,44 @@ #include typedef unsigned int _;_ d,b, #define i(I1,Il,lI)if(Il){lI;}else{I1;} - I[256], n,y,a,r,u,k,o - ,L,l[ 256],O,K[ - /**/ #define\ - q(g) g char\ - *C, *Q,c[\ - ]= "KfW"\ - "" "Ww|"\ - /* 'UU!\ - % NYA!\ - */ "Z}"\ - ";fRo?JtJaVoRU" "Ro"\ - "nB}h@o?)d.X)NSTIUCz7@%",*s[]={c,"#en" "di"\ - "f/*}||1;\n__DATA__\40*/\n\n#ifndef\40q\n#d" "ef"\ - "ine\x20q\n#include\ntypedef\40unsign" "e"\ - "d\x20int\x20_;_\x20K[]={\n#include\40__FILE__\n#u" "n"\ - "def q","0},L,O,l[256],I[256],n,y,a,r,u,k,o;"#g"char" "*"\ - "S,s[]=\"",c,c,"\";int main(){X();for(S=s+*K;*S>37;){for" "(o"\ - "=0;o<5;o++)r=r*85+(83+*S++)%89;r","^=*x();for(o=0;o<4;o++" ")"\ - "{s[O++]=r&255;r>>=8;}}return!fwrite(s,O-*S%5,1,stdout);}\n" "#"\ - "endif",c},S[256]="#ifdef/*\n'true'\40or\40q{\nexec\40head\40" "-"\ - "8\40$0\n};for(open$O,$0;<$O>;print\40if$f){$f|=/^$/;}q{*/q",/* */z; - 256];q(_*x(){if(!L--){y+=++a;for(o=0;o<256;y=l[o++]=I[255&(k>>10 )]+u - ){n^=(o&1)?n>>(( o& 2)?16:6):n<<((o&2)?2:13);u=I[o];k=I[o]=I[255& (u>> - 2)]+(n+=I[(o+128) & 255]) +y;}L=255;}return&l[L];}_*X(){for(O=0 ;256 - >O;I[O++]=0);for(O = 0; sizeof(K)/sizeof( _)> O;O++)I[O&255] ^=K[ - O];for(n=y=a=L=O=0 ;O<1<<24;++ O)x( );r=O=0x0; return&O;})int/*^^*/ main - (int p,char**P){FILE* Z=fopen(p> (+ 1)?P[01 ] :P[0], "rb" - );i(;,Z,O=fread(K,256 ,4,Z);/*P */ fclose(Z))X();for(p=b=d =O= - 0;O<256;K[O++]=0)*K=+ 86;for(O =1;12> O;K[O++]=*x());X(); for - (C=Q=S;r-8;){i(*C++=34, (r-4&&r -5)||C- S ,;)z=Q[p++];i(;,z != - 32||r-3,i(i(C+=sprintf(( C), "%uU" ",",K[b++]);i(d=1;C =S - ;i(d=02,b-12,;),b%6,;),r-1 ,i(b= fread(c,1,4,stdin);i (p - =O=0,b,for(d=O=0;O<04;O++)d +=(c[O]&255)<<(8*O);d ^= - *x();for(p=5;p;c[--p]=O<32?O+ 95:O+6){O=d%85;d/=85;} O= - 5)i(d=0,b<4,c[O++]=b?b-1?b-2?36: 37:33:35 ;d=2)c[O]=0,r-4,i(i (d= 2 - |d,C!=S+6,*C++=(*x()%34)+93;p--),r -5,*s= C;d|=2) )),z ,i(*C++ =92 , - z-63||C [-1]-63||C>S+76,;)*C++=z))i( ;,d>1,d= d-2 ;Q=s[r] ;i( ; + I[256], n,y,a,r,u,k,o + ,L,l[ 256],O,K[ + /**/ #define\ + q(g) g char\ + *C, *Q,c[\ + ]= "KfW"\ + "" "Ww|"\ + /* 'UU!\ + % NYA!\ + */ "Z}"\ + ";fRo?JtJaVoRU" "Ro"\ + "nB}h@o?)d.X)NSTIUCz7@%",*s[]={c,"#en" "di"\ + "f/*}||1;\n__DATA__\40*/\n\n#ifndef\40q\n#d" "ef"\ + "ine\x20q\n#include\ntypedef\40unsign" "e"\ + "d\x20int\x20_;_\x20K[]={\n#include\40__FILE__\n#u" "n"\ + "def q","0},L,O,l[256],I[256],n,y,a,r,u,k,o;"#g"char" "*"\ + "S,s[]=\"",c,c,"\";int main(){X();for(S=s+*K;*S>37;){for" "(o"\ + "=0;o<5;o++)r=r*85+(83+*S++)%89;r","^=*x();for(o=0;o<4;o++" ")"\ + "{s[O++]=r&255;r>>=8;}}return!fwrite(s,O-*S%5,1,stdout);}\n" "#"\ + "endif",c},S[256]="#ifdef/*\n'true'\40or\40q{\nexec\40head\40" "-"\ + "8\40$0\n};for(open$O,$0;<$O>;print\40if$f){$f|=/^$/;}q{*/q",/* */z; + 256];q(_*x(){if(!L--){y+=++a;for(o=0;o<256;y=l[o++]=I[255&(k>>10 )]+u + ){n^=(o&1)?n>>(( o& 2)?16:6):n<<((o&2)?2:13);u=I[o];k=I[o]=I[255& (u>> + 2)]+(n+=I[(o+128) & 255]) +y;}L=255;}return&l[L];}_*X(){for(O=0 ;256 + >O;I[O++]=0);for(O = 0; sizeof(K)/sizeof( _)> O;O++)I[O&255] ^=K[ + O];for(n=y=a=L=O=0 ;O<1<<24;++ O)x( );r=O=0x0; return&O;})int/*^^*/ main + (int p,char**P){FILE* Z=fopen(p> (+ 1)?P[01 ] :/*proc/self*/*P, "rb" + );i(;,Z,O=fread(K,256 ,4,Z);/*P */ fclose(Z))X();for(p=b=d =O= + 0;O<256;K[O++]=0)*K=+ 86;for(O =1;12> O;K[O++]=*x());X(); for + (C=Q=S;r-8;){i(*C++=34, (r-4&&r -5)||C- S ,;)z=Q[p++];i(;,z != + 32||r-3,i(i(C+=sprintf(( C), "%uU" ",",K[b++]);i(d=1;C =S + ;i(d=02,b-12,;),b%6,;),r-1 ,i(b= fread(c,1,4,stdin);i (p + =O=0,b,for(d=O=0;O<04;O++)d +=(c[O]&255)<<(8*O);d ^= + *x();for(p=5;p;c[--p]=O<32?O+ 95:O+6){O=d%85;d/=85;} O= + 5)i(d=0,b<4,c[O++]=b?b-1?b-2?36: 37:33:35 ;d=2)c[O]=0,r-4,i(i (d= 2 + |d,C!=S+6,*C++=(*x()%34)+93;p--),r -5,*s= C;d|=2) )),z ,i(*C++ =92 , + z-63||C [-1]-63||C>S+76,;)*C++=z))i( ;,d>1,d= d-2 ;Q=s[r] ;i( ; ,r<3|| r>5,d=1;i(;,r-1, *C=0)C=S) i(;, r-4, p=0)++ r) i(*(C++ )= 34,r <4||r>5|| C 5 ||C - #include - #include - typedef unsigned int _;_ d,b, - #define i(I1,Il,lI)if(Il){lI;}else{I1;} - I[256], n,y,a,r,u,k,o - ,L,l[ 256],O,K[ - /**/ #define\ - q(g) g char\ - *C, *Q,c[\ - ]= "KfW"\ - "" "Ww|"\ - /* 'UU!\ - % NYA!\ - */ "Z}"\ - ";fRo?JtJaVoRU" "Ro"\ - "nB}h@o?)d.X)NSTIUCz7@%",*s[]={c,"#en" "di"\ - "f/*}||1;\n__DATA__\40*/\n\n#ifndef\40q\n#d" "ef"\ - "ine\x20q\n#include\ntypedef\40unsign" "e"\ - "d\x20int\x20_;_\x20K[]={\n#include\40__FILE__\n#u" "n"\ - "def q","0},L,O,l[256],I[256],n,y,a,r,u,k,o;"#g"char" "*"\ - "S,s[]=\"",c,c,"\";int main(){X();for(S=s+*K;*S>37;){for" "(o"\ - "=0;o<5;o++)r=r*85+(83+*S++)%89;r","^=*x();for(o=0;o<4;o++" ")"\ - "{s[O++]=r&255;r>>=8;}}return!fwrite(s,O-*S%5,1,stdout);}\n" "#"\ - "endif",c},S[256]="#ifdef/*\n'true'\40or\40q{\nexec\40head\40" "-"\ - "8\40$0\n};for(open$O,$0;<$O>;print\40if$f){$f|=/^$/;}q{*/q",/* */z; - 256];q(_*x(){if(!L--){y+=++a;for(o=0;o<256;y=l[o++]=I[255&(k>>10 )]+u - ){n^=(o&1)?n>>(( o& 2)?16:6):n<<((o&2)?2:13);u=I[o];k=I[o]=I[255& (u>> - 2)]+(n+=I[(o+128) & 255]) +y;}L=255;}return&l[L];}_*X(){for(O=0 ;256 - >O;I[O++]=0);for(O = 0; sizeof(K)/sizeof( _)> O;O++)I[O&255] ^=K[ - O];for(n=y=a=L=O=0 ;O<1<<24;++ O)x( );r=O=0x0; return&O;})int/*^^*/ main - (int p,char**P){FILE* Z=fopen(p> (+ 1)?P[01 ] :P[0], "rb" - );_setmode(_fileno(stdout), 0x8000);_setmode(_fileno(stdin), 0x8000);i( - ;,Z,O=fread(K,256 ,4,Z);/*P */ fclose(Z))X();for(p=b=d =O= - 0;O<256;K[O++]=0)*K=+ 86;for(O =1;12> O;K[O++]=*x());X(); for - (C=Q=S;r-8;){i(*C++=34, (r-4&&r -5)||C- S ,;)z=Q[p++];i(;,z != - 32||r-3,i(i(C+=sprintf(( C), "%uU" ",",K[b++]);i(d=1;C =S - ;i(d=02,b-12,;),b%6,;),r-1 ,i(b= fread(c,1,4,stdin);i (p - =O=0,b,for(d=O=0;O<04;O++)d +=(c[O]&255)<<(8*O);d ^= - *x();for(p=5;p;c[--p]=O<32?O+ 95:O+6){O=d%85;d/=85;} O= - 5)i(d=0,b<4,c[O++]=b?b-1?b-2?36: 37:33:35 ;d=2)c[O]=0,r-4,i(i (d= 2 - |d,C!=S+6,*C++=(*x()%34)+93;p--),r -5,*s= C;d|=2) )),z ,i(*C++ =92 , - z-63||C [-1]-63||C>S+76,;)*C++=z))i( ;,d>1,d= d-2 ;Q=s[r] ;i( ; - ,r<3|| r>5,d=1;i(;,r-1, *C=0)C=S) i(;, r-4, p=0)++ r) i(*(C++ )= - 34,r <4||r>5|| C -5 ||C