Adjusted alternate version to preserve formatting

The first alt version added some whitespaces that broke the outline, so
I simply filled those up.  I also expanded tabs to spaces since there is
no size limit restriction for the alternate version.

The second alt version attempted to force binary mode with _setmode()
and _fileno() calls, plus some additional headers.  Rather than doing it
this way, I hack in the binary mode adjustments via preprocessor.

Actually I did a similar trick for IOCCC 2011, but I guess I didn't
submit that version:
https://uguu.org/archive/ioccc2011/akari/source/binmode_main.h

The file above uses _setmode() and _fileno().  The leading underscore
indicates that they were nonstandard extensions.  For the binmode_main.h
in this change, I have opted for setmode() and STDIN_FILENO instead,
which are more widely available.
This commit is contained in:
omoikane committed 2025-05-15 17:51:12 -07:00
1 parent 376f51c489
commit aff78be8f9
5 files changed
+93 -117

No files matched your search

+2 -11
View File
@@ -62,7 +62,7 @@ CDEFINE=
# Include files that are needed to compile # Include files that are needed to compile
# #
CINCLUDE= CINCLUDE=-include binmode_main.h
# Optimization # Optimization
# #
@@ -146,15 +146,6 @@ ${PROG}.alt: ${PROG}.alt.c
${PROG}.alt.c: ${ENTRY}.alt.c ${PROG}.alt.c: ${ENTRY}.alt.c
${CP} -f -v $? $@ ${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 files
# #
data: ${DATA} data: ${DATA}
@@ -175,7 +166,7 @@ clean:
clobber: clean clobber: clean
${RM} -f ${TARGET} ${ALT_TARGET} ${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} -rf *.dSYM
${RM} -f output.c key.c data.c regenerated.bin data output output.2 ${RM} -f output.c key.c data.c regenerated.bin data output output.2
+14 -19
View File
@@ -5,9 +5,7 @@
``` ```
There is an alternate version that will read from the compiled file itself, in 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 case you don't have a `/dev/urandom` file.
theoretically work with Windows which distinguishes binary and text. See
[alternate code](#alternate-code) below.
## To use: ## 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 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 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` 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 file and you do not want to specify an extra file.
is like the first except it also should theoretically work with Windows, setting
binary mode on `stdin` and `stdout`.
### Alternate build: ### Alternate build:
For the first version:
``` <!---sh--> ``` <!---sh-->
make alt make alt
``` ```
For the second version:
``` <!---sh-->
make alt2
```
### Alternate use: ### Alternate use:
Use `nyaruko.alt` or `nyaruko.alt2` as you would `nyaruko` above. Use `nyaruko.alt` as you would `nyaruko` above.
### Alternate try: ### Alternate try:
@@ -77,8 +64,6 @@ For the first alternate version:
./try.alt.sh ./try.alt.sh
``` ```
We have no way to test the second version, sorry (tm Canada 🇨🇦 :-) )!
## Judges' remarks: ## Judges' remarks:
@@ -190,7 +175,17 @@ combinations:
* tcc 0.9.25 on JSLinux * tcc 0.9.25 on JSLinux
Note that on MingW, `stdin` and `stdout` are not opened in binary mode by 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:
``` <!---sh-->
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: ### NOTICE to those who wish for a greater challenge:
+39
View File
@@ -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<stdio.h>
#include<fcntl.h>
#include<io.h>
#include<unistd.h>
#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
+38 -38
View File
@@ -1,44 +1,44 @@
#include<stdio.h> #include<stdio.h>
typedef unsigned int _;_ d,b, typedef unsigned int _;_ d,b,
#define i(I1,Il,lI)if(Il){lI;}else{I1;} #define i(I1,Il,lI)if(Il){lI;}else{I1;}
I[256], n,y,a,r,u,k,o I[256], n,y,a,r,u,k,o
,L,l[ 256],O,K[ ,L,l[ 256],O,K[
/**/ #define\ /**/ #define\
q(g) g char\ q(g) g char\
*C, *Q,c[\ *C, *Q,c[\
]= "KfW"\ ]= "KfW"\
"" "Ww|"\ "" "Ww|"\
/* 'UU!\ /* 'UU!\
% NYA!\ % NYA!\
*/ "Z}"\ */ "Z}"\
";fRo?JtJaV<x4@*?R?&JV1" ".s"\ ";fRo?JtJaV<x4@*?R?&JV1" ".s"\
"{Fyj2_;khB1xQ5oxm~mS@B|(pa>oRU" "Ro"\ "{Fyj2_;khB1xQ5oxm~mS@B|(pa>oRU" "Ro"\
"nB}h@o?)d.X)NSTIUCz7@%",*s[]={c,"#en" "di"\ "nB}h@o?)d.X)NSTIUCz7@%",*s[]={c,"#en" "di"\
"f/*}||1;\n__DATA__\40*/\n\n#ifndef\40q\n#d" "ef"\ "f/*}||1;\n__DATA__\40*/\n\n#ifndef\40q\n#d" "ef"\
"ine\x20q\n#include<stdio.h>\ntypedef\40unsign" "e"\ "ine\x20q\n#include<stdio.h>\ntypedef\40unsign" "e"\
"d\x20int\x20_;_\x20K[]={\n#include\40__FILE__\n#u" "n"\ "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" "*"\ "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"\ "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++" ")"\ "=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" "#"\ "{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" "-"\ "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; "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 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>> ){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 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;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 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" (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= );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 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 != (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 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 ;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 ^= =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= *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 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 , |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( ; 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++ )= ,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<S+ 78,;)i (*C++=0;d=1; C=S ,r<3 || r> 34,r <4||r>5|| C<S+ 78,;)i (*C++=0;d=1; C=S ,r<3 || r>
5 ||C<S+ 79,; )i(;,d, puts ( 5 ||C<S+ 79,; )i(;,d, puts (
-49
View File
@@ -1,49 +0,0 @@
#include<stdio.h>
#include<io.h>
#include<fcntl.h>
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?JtJaV<x4@*?R?&JV1" ".s"\
"{Fyj2_;khB1xQ5oxm~mS@B|(pa>oRU" "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<stdio.h>\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<S+ 78,;)i (*C++=0;d=1; C=S ,r<3 || r>
5 ||C<S+ 79,; )i(;,d, puts (
S); d=0 )} return
0;}