Base64 partial decoding

This is a discussion on Base64 partial decoding within the PHP server side scripting (comp.lang.php) forum.

Base64 partial decoding

Postby Gelembjuk on Tue Dec 02, 2008 11:47 pm

Hello.
I have data encoded with base64. I need to decode it but i must to do
it part by part.
I read bytes from stream. I must read some part (8000 bytes or any
other count that is not too big, less then 10000) then i need to
decode this part of data and write to another stream.

I found that if i just read part of data and decode it with
base64_decode then final decoded data are not correct.
If there a way to decode base64 encoded data part by part?
Gelembjuk
 
Posts: 3
Joined: Tue Dec 02, 2008 11:47 pm

Re: Base64 partial decoding

Postby Trookat on Wed Dec 03, 2008 4:17 am

Gelembjuk wrote:
> Hello.
> I have data encoded with base64. I need to decode it but i must to do
> it part by part.
> I read bytes from stream. I must read some part (8000 bytes or any
> other count that is not too big, less then 10000) then i need to
> decode this part of data and write to another stream.
>
> I found that if i just read part of data and decode it with
> base64_decode then final decoded data are not correct.
> If there a way to decode base64 encoded data part by part?

Interesting problem you have, I can understand why you want to decode
only sections of a base64 encoded stream and not the entire lot .

base64 encoded string "hello world" gets turned into aGVsbG8gd29ybGQ=

now its been a long time since ive done bitwise encoding ( back in
asm/pascal graphic programming days for dos ) so i had to look up how
base64 encoding works
found a nice article on how it works
http://email.about.com/cs/standards/a/base64_encoding.htm
its mostly about email but its a good start


if you miss grab the wrong parts you can get erroneous results.
Now assuming if its practical and you know the exact structure of the
stream you could use functions like chunk_split ,unpack or even preg
functions to get the correct data.

personally i would do a base64_decode and work on the resulted string
thereafter.

Without knowing exactly what your source or your code is thats the best
I can offer


regards trookat
Trookat
 
Posts: 85
Joined: Sat Aug 23, 2003 4:54 am

Re: Base64 partial decoding

Postby Tim Roberts on Thu Dec 04, 2008 11:13 pm

Gelembjuk wrote:
>
>I have data encoded with base64. I need to decode it but i must to do
>it part by part.
>I read bytes from stream. I must read some part (8000 bytes or any
>other count that is not too big, less then 10000) then i need to
>decode this part of data and write to another stream.
>
>I found that if i just read part of data and decode it with
>base64_decode then final decoded data are not correct.
>If there a way to decode base64 encoded data part by part?

Base64 takes 3 bytes at a time and converts them to 4 characters. You must
decode those 4 characters as a unit; you can't start at any arbitrary byte.
That's also why you sometimes get equal signs (=) at the end; they are
padding the encoded string out to a multiple of 4 characters.

So, just make sure each chunk you decode is a multiple of 4 characters, and
you'll be golden.
--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.
Tim Roberts
 
Posts: 329
Joined: Sat Sep 04, 2004 10:31 pm

Re: Base64 partial decoding

Postby =?ISO-8859-1?Q?=22=C1lvaro_G=2E_Vicario=22?= on Fri Dec 05, 2008 1:59 am

Tim Roberts escribió:
> Base64 takes 3 bytes at a time and converts them to 4 characters. You must
> decode those 4 characters as a unit; you can't start at any arbitrary byte.
> That's also why you sometimes get equal signs (=) at the end; they are
> padding the encoded string out to a multiple of 4 characters.
>
> So, just make sure each chunk you decode is a multiple of 4 characters, and
> you'll be golden.

I think this can be very useful so I've posted a note about it in the
PHP manual.


--
-- http://alvaro.es/ - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://bits.demogracia.com/
-- Mi web de humor al baño María: http://www.demogracia.com/
--
=?ISO-8859-1?Q?=22=C1lvaro_G=2E_Vicario=22?=
 
Posts: 213
Joined: Thu Aug 09, 2007 1:32 pm


Return to PHP server side scripting (comp.lang.php)

Who is online

Users browsing this forum: No registered users and 0 guests