Riven
|
 |
«
Posted
2007-10-28 00:12:04 » |
|
I'm tempted to host some silly competion thingy about writing the smallest code to perform some I/O task. I coded some really silly task, and I'd like you guys to check out whether there is anything dangerous possible. If so, I'd be very grateful if you'd let me know as fast as possible. I'm renting this server, and I wouldn't really like to get my account suspended. I tried to isolate the running of tasks as much as possible, within the current JVM, but I might have to resort launching a new JVM to avoid any corruption of the HTTP-server-instance hosting this. http://213.247.55.3:8484/compileandrun/A (naive) solution for the task would be: 1 2 3 4 5 6 7 8
| while(true) { int a = in.read(); int b = in.read(); if((a|b)<0) break; out.write(b); out.write(a); } |
108 bytes, linebreaks don't count (for readability reasons), but spaces do Beat that! I didn't really raise the bar very high, did I..  Anyway, give it a try, and tell me if there's something I need to know. 
|
|
|
|
Riven
|
 |
«
Reply #1 - Posted
2007-10-28 00:20:50 » |
|
Beat 62 bytes, and don't forget to be naughty.
|
|
|
|
Riven
|
 |
«
Reply #2 - Posted
2007-10-28 00:52:37 » |
|
I disabled uppercase characters... to catch the casual tester This worked though: Gimme your tricks..
|
|
|
|
Games published by our own members! Check 'em out!
|
|
kappa
|
 |
«
Reply #3 - Posted
2007-10-28 02:12:51 » |
|
ewww, no uppercase characters  , pretty much ends it for me.
|
|
|
|
|
Riven
|
 |
«
Reply #4 - Posted
2007-10-28 02:35:24 » |
|
Hey... how else would I keep it secure... and... why would you need Classes in such tasks? I'm glad you tried it though 
|
|
|
|
Abuse
|
 |
«
Reply #5 - Posted
2007-10-28 02:36:35 » |
|
Beat 62 bytes, and don't forget to be naughty.
Got to 64bytes so far....
|
Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! 
|
|
|
Riven
|
 |
«
Reply #6 - Posted
2007-10-28 02:40:28 » |
|
Way to go!
|
|
|
|
kappa
|
 |
«
Reply #7 - Posted
2007-10-28 02:42:59 » |
|
and... why would you need Classes in such tasks?
ah sorry my mistake, thought the task was to break the program 
|
|
|
|
|
Riven
|
 |
«
Reply #8 - Posted
2007-10-28 02:45:07 » |
|
That too...  and that stops like 99.9999% of all attacks.
|
|
|
|
Abuse
|
 |
«
Reply #9 - Posted
2007-10-28 02:53:23 » |
|
Got to 64bytes so far....
Haha, I'm such a muppet - that 64byte solution was actually 62bytes =) I hadn't removed the declaration of the now unused b integer  Can't see a way of beating 62 though... atleast not yet =)
|
Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! 
|
|
|
Games published by our own members! Check 'em out!
|
|
Riven
|
 |
«
Reply #10 - Posted
2007-10-28 02:56:23 » |
|
Darn.
|
|
|
|
Riven
|
 |
«
Reply #11 - Posted
2007-10-28 03:21:05 » |
|
A dead end... but I love it! 1
| for(byte[]b={0,0,1};in.read(b,b[2]++&1,1)>0;out.write(b,0,b[2]*2&2)); |
(69 bytes)
|
|
|
|
Abuse
|
 |
«
Reply #12 - Posted
2007-10-28 03:56:17 » |
|
A dead end... but I love it! 1
| for(byte[]b={0,0,1};in.read(b,b[2]++&1,1)>0;out.write(b,0,b[2]*2&2)); |
(69 bytes) If you are going to break the defined contract of InputStream.read(byte[],int,int), by only checking for ">0" on the return type, then the 62 byte solution mentioned earlier can be trimmed to 61 bytes =) (presumably your own 6[1/2] byte solution is exactly the same as mine?) 1
| for(int a;(a=in.read())>0;out.write(in.read()),out.write(a)); |
|
Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! 
|
|
|
Riven
|
 |
«
Reply #13 - Posted
2007-10-28 03:59:37 » |
|
>0 is not breaking the contract of InputStream.read(byte[],int,int) This method blocks until input data is available, end of file is detected, or an exception is thrown. it will never ever return 0. Unless your set the thrid argument to 0 
|
|
|
|
noblemaster
|
 |
«
Reply #14 - Posted
2007-10-28 04:00:35 » |
|
actually, I got 1 byte less ( 60 bytes): 1
| for(int a;(a=in.read())>0;out.write(a))out.write(in.read()); |
ditto Abuse it's 61 bytes if you use >=0...
|
|
|
|
Riven
|
 |
«
Reply #15 - Posted
2007-10-28 04:02:53 » |
|
I guess this is good enough to turn into a bigger project  I wrote the http-server myself, so pretty much everything is possible. Again, don't forget to mention the security-holes. Thank you.
|
|
|
|
Riven
|
 |
«
Reply #16 - Posted
2007-10-28 04:04:44 » |
|
1
| for(int a;(a=in.read())>0;out.write(a))out.write(in.read()); |
I totally love the simplicity.
|
|
|
|
Abuse
|
 |
«
Reply #17 - Posted
2007-10-28 04:06:57 » |
|
oh yeah, nice spot 
|
Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! 
|
|
|
Abuse
|
 |
«
Reply #18 - Posted
2007-10-28 04:08:38 » |
|
I totally love the simplicity.
I still think your 69byte solution is by far the most complicated =S
|
Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! 
|
|
|
Matzon
|
 |
«
Reply #19 - Posted
2007-10-28 08:52:32 » |
|
isn't this flawed, since it only checks the validity of the first in.read? -edit- ok, the string has pairs of bytes.
|
|
|
|
Riven
|
 |
«
Reply #20 - Posted
2007-10-28 16:07:04 » |
|
|
|
|
|
Riven
|
 |
«
Reply #21 - Posted
2007-10-28 17:28:15 » |
|
I'm adding a database now, to make accounts, and compete against eachother...
|
|
|
|
markush
Senior Newbie 
Plöp!
|
 |
«
Reply #22 - Posted
2007-10-28 20:04:40 » |
|
72 bytes! muhahaha  To achieve it, you can replace the only space with a line break, and since that doesn't count its one byte off 
|
|
|
|
|
Riven
|
 |
«
Reply #23 - Posted
2007-10-28 20:12:55 » |
|
Hehe... then I get 72 bytes too... and I got 2 solutions: 1 2 3
| for(int r=0,i=0,b=0;r>-1;b=b<<1|(r=in.read())&1) if(i++%8==0)out.write(b); |
1 2 3 4 5 6
| for(int r,i=0,b=0;(r=in.read())>0;) { b=b<<1|r&1; if(++i%8==0)out.write(b); } |
The first is extremely not-done.
|
|
|
|
Riven
|
 |
«
Reply #24 - Posted
2007-10-28 20:15:33 » |
|
It's a nice loophole though, and I'll make sure I'll 'fix' that.
Like... only linebreaks allowed after a '{'
Could you show me your solution?
|
|
|
|
markush
Senior Newbie 
Plöp!
|
 |
«
Reply #25 - Posted
2007-10-28 20:24:54 » |
|
Hehe... then I get 72 bytes too... and I got 2 solutions: 1 2 3
| for(int r=0,i=0,b=0;r>-1;b=b<<1|(r=in.read())&1) if(i++%8==0)out.write(b); |
1 2 3 4 5 6
| for(int r,i=0,b=0;(r=in.read())>0;) { b=b<<1|r&1; if(++i%8==0)out.write(b); } |
The first is extremely not-done. My is a bit different: 1 2 3 4 5
| for(int c=0,i=0,x;(x=in.read())>0;c<<=1){ c|=x&1; if(++i%8<1)out.write(c); } |
And 71 bytes with that: 1 2 3
| for(int r=1,i=0,b=0;r>0;b=b<<1|(r=in.read())&1) if(i++%8==0)out.write(b); |
|
|
|
|
|
Abuse
|
 |
«
Reply #26 - Posted
2007-10-28 20:56:46 » |
|
erm, am I missing something? 2 of the solutions above don't work? They output a leading 0 to the output stream. 1 2 3
| for(int r=1,i=0,b=0;r>0;b=b<<1|(r=in.read())&1) if(i++%8==0)out.write(b); |
and 1 2 3
| for(int r=0,i=0,b=0;r>-1;b=b<<1|(r=in.read())&1) if(i++%8==0)out.write(b); |
|
Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! 
|
|
|
Martin Strand
|
 |
«
Reply #27 - Posted
2007-10-28 21:13:20 » |
|
This doesn't output a leading zero but all ascii values are 0x100 too high. It looks ok on that web page though.  (70 bytes) 1 2 3 4 5 6 7 8 9
| for(int r=1,b=1; r>0; b=b<<1|(r=in.read())&1) if(b>255) { out.write(b); b=1; } |
|
|
|
|
|
Abuse
|
 |
«
Reply #28 - Posted
2007-10-28 21:29:01 » |
|
This doesn't output a leading zero but all ascii values are 0x100 too high. It looks ok on that web page though.  (70 bytes) 1 2 3 4 5 6 7 8 9
| for(int r=1,b=1; r>0; b=b<<1|(r=in.read())&1) if(b>255) { out.write(b); b=1; } |
That's perfectly ok - the contract of OutputStream.write(int) dictates that only the bottom 8 bits are used anyway, so using the upper bits for signalling is pretty damn clever =)
|
Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! 
|
|
|
Abuse
|
 |
«
Reply #29 - Posted
2007-10-28 21:37:19 » |
|
An adaption of the above (66 bytes!) :- for(int b=1; (b=b<<1|(in.read()-48))>0;) if(b>255){ out.write(b); b=1; } And there is even a redundant ';' in the for, which I'm sure can be optimised away with some restructuring?
|
Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! 
|
|
|
|