Base64 partial decoding

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
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?

Re: Base64 partial decoding

Gelembjuk wrote:
Quoted text here. Click to load it

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
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

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

regards trookat

Re: Base64 partial decoding

Quoted text here. Click to load it

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,
Providenza & Boekelheide, Inc.

Re: Base64 partial decoding

Tim Roberts escribió:
Quoted text here. Click to load it

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

-- - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web:
-- Mi web de humor al baño María:

Site Timeline