Printable Version of Topic

Click here to view this topic in its original format

914World.com _ 914World Garage _ WOT: ARRGGHHH, i hate fixing someone elses code!

Posted by: SirAndy Aug 15 2005, 07:33 PM

this was supposed to be a simple stripped down BASE64 decoder done in java.

i don't (can't) use a lot of standard libraries because this App will be run on PDAs and java enabled cellphones, so SIZE does matter.

no problem, i get one of my contractors the BASE64 specs and some sample implementations ...

he's been fighting with this for 3 days now so i get his sourcefile and have a look at it this morning.
looks OK at first glance, but when you run it, it sporadically spits out wrong bytevalues while some decode just fine.
wtf?
so i spend all day going through his code (which is not that much) and i just can't figure out what's wrong.

then finally, about 15 minutes ago it dawns on me, have a look at this:

bReturn2[nn+2] = (byte)(iRes % 0xFF);
bReturn2[nn+1] = (byte)((iRes >> 8) % 0xFF);
bReturn2[nn+0] = (byte)((iRes >> 16) % 0xFF);


bReturn2 is a byte array, iRes a integer, the purpose is to roll the lower 3 bytes of the integer into 3 sequential fields in the byte array.

i had been staring at this for 6 hours and couldn't figure out what was wrong.
all the while it was right in front of me, plain sight, right there ... headbang.gif

can you find the error?
idea.gif Andy

PS: did i mention that i HATE going through someone else code?

Posted by: SpecialK Aug 15 2005, 08:02 PM

I have NFI, but I'd guess you're missing a parenthesis in the first line........but then again....I have NFI. confused24.gif

Get your broken off allen wrench out?

Posted by: Midtowner Aug 15 2005, 08:15 PM

Sounds all Japanese to me! smile.gif

Posted by: ! Aug 15 2005, 08:27 PM

Or ya got extra ones....parenthesis that is....

Posted by: Foxman Aug 15 2005, 08:29 PM

bReturn2[nn+2] = (byte)(iRes % 0xFF);

What are you calculating the remainder of? Are you dividing iRes and 0XFF somewhere else? It is hard to say without seeing the rest of the function.

Posted by: johnmhudson111 Aug 15 2005, 08:29 PM

QUOTE (Buzzard1 @ Aug 15 2005, 09:02 PM)
I'd guess you're missing a parenthesis in the first line.......

agree.gif Just a WAG since I have no coding ability, just an analyst by nature and that seems to be the missing pattern.

Posted by: SirAndy Aug 15 2005, 08:32 PM

QUOTE (johnmhudson111 @ Aug 15 2005, 07:29 PM)
Just a WAG since I have no coding ability, just an analyst by nature and that seems to be the missing pattern.

nope, nothing missing, syntax is just fine, compiles and runs without an error ...

wink.gif Andy

Posted by: SirAndy Aug 15 2005, 08:35 PM

QUOTE (Foxman @ Aug 15 2005, 07:29 PM)
What are you calculating the remainder of? Are you dividing iRes and 0XFF somewhere else? It is hard to say without seeing the rest of the function.

this takes a 4 byte integer and zeros out all bits except the lowest 8 (0xff) and then stores those 8 bit in an byte-array.

1st line has no shift to get 1st byte
2nd line has 8 bit shift to get 2nd byte
3rd line has 16 bit shift to get 3rd byte

that all is correct, but he still managed to f&%$ it up ...
dry.gif Andy

Posted by: johnmhudson111 Aug 15 2005, 08:36 PM

QUOTE (SirAndy @ Aug 15 2005, 09:32 PM)
nope, nothing missing, syntax is just fine, compiles and runs without an error ...

wink.gif Andy

bReturn2[nn+2] = (byte)((iRes >> 4) % 0xFF);
bReturn2[nn+1] = (byte)((iRes >> 8) % 0xFF);
bReturn2[nn+0] = (byte)((iRes >> 16) % 0xFF);

Nother WAG, I give up after this one happy11.gif

Posted by: jonwatts Aug 15 2005, 08:37 PM

I would have used the '&' operator instead of the '%' operator, but that's just me.


Posted by: r_towle Aug 15 2005, 08:58 PM

QUOTE (SirAndy @ Aug 15 2005, 08:33 PM)
this was supposed to be a simple stripped down BASE64 decoder done in java.

i don't (can't) use a lot of standard libraries because this App will be run on PDAs and java enabled cellphones, so SIZE does matter.

no problem, i get one of my contractors the BASE64 specs and some sample implementations ...

he's been fighting with this for 3 days now so i get his sourcefile and have a look at it this morning.
looks OK at first glance, but when you run it, it sporadically spits out wrong bytevalues while some decode just fine.
wtf?
so i spend all day going through his code (which is not that much) and i just can't figure out what's wrong.

then finally, about 15 minutes ago it dawns on me, have a look at this:

bReturn2[nn+2] = (byte)(iRes % 0xFF);
bReturn2[nn+1] = (byte)((iRes >> 8) % 0xFF);
bReturn2[nn+0] = (byte)((iRes >> 16) % 0xFF);


bReturn2 is a byte array, iRes a integer, the purpose is to roll the lower 3 bytes of the integer into 3 sequential fields in the byte array.

i had been staring at this for 6 hours and couldn't figure out what was wrong.
all the while it was right in front of me, plain sight, right there ... headbang.gif

can you find the error?
idea.gif Andy

PS: did i mention that i HATE going through someone else code?

bReturn2[nn+0] = (byte)(iRes % 0xFF);
bReturn2[nn+1] = (byte)((iRes >> 8) % 0xFF);
bReturn2[nn+2] = (byte)((iRes >> 16) % 0xFF);

Posted by: SirAndy Aug 15 2005, 09:07 PM

QUOTE (jonwatts @ Aug 15 2005, 07:37 PM)
I would have used the '&' operator instead of the '%' operator, but that's just me.

we've got a winner! smilie_pokal.gif

he used the "%" operator which is MODULO instead of "&" which is bitwise AND ...

took me 6 friggin hours to find that one!
headbang.gif Andy

here's the correct code snippet:

bReturn2[nn+2] = (byte)(iRes & 0xFF);
bReturn2[nn+1] = (byte)((iRes >> 8) & 0xFF);
bReturn2[nn+0] = (byte)((iRes >> 16) & 0xFF);

Posted by: jonwatts Aug 15 2005, 09:07 PM

Dumb question. Is your input base10 or base16? It doesn't matter to the compiler but it makes a difference to us humans.


Posted by: ematulac Aug 15 2005, 09:08 PM

edit: nevermind ... modulus looked strange to me there, but it didn't register.

Posted by: jonwatts Aug 15 2005, 09:09 PM

WOW, and I am such a suck-ass programmer too, but I do have to live in byte-stream land on a daily basis so maybe that helps.

I'd like to thank the Academy, my agent, and Andy for the beautiful 944 CV's, axles, and powder coated trailing arms (or do I just win an attaboy?).


Posted by: SirAndy Aug 15 2005, 09:10 PM

QUOTE (ematulac @ Aug 15 2005, 08:08 PM)
He's storing the bytes in the wrong order in the array.

nope, java integer is reverse byte order, so that part was correct ...

jon nailed it, see post above!
type.gif Andy

Posted by: Foxman Aug 15 2005, 09:12 PM

damn, I never thought about the input.

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)