Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Base64 MIME

Reply
Thread Tools

Base64 MIME

 
 
Bigus
Guest
Posts: n/a
 
      12-01-2006
I am trying to convert a base64 encoded string in a plain text mailing list
archive file back into a document. I extract the string form the file and
then decode it like this:

open DC, ">$path/$filename";
print DC decode_base64($encoded);
close DC;

where $encoded is the base64 string.

It creates the file, say a jpg or doc, of what looks like about the right
size, but the images are corrupted. Is there anything obviously wrong with
this?

Bigus


 
Reply With Quote
 
 
 
 
Ben Morrow
Guest
Posts: n/a
 
      12-01-2006

Quoth "Bigus" <>:
> I am trying to convert a base64 encoded string in a plain text mailing list
> archive file back into a document. I extract the string form the file and
> then decode it like this:
>
> open DC, ">$path/$filename";


Use lexical filehandles.
Use three-arg open.
Check the return value of open.

open my $DC, '>', "$path/$filename"
or die "can't open '$path/$filename': $!";

If you are printing binary data to a filehandle you need to use binmode.
Yes, you need to use binmode on all platforms now.

binmode $DC;

If you don't care about compatibility pre-5.8, you can specify this as a
PerlIO layer in the open statement:

open my $DC, '>:raw', "$path/$filename"
or die "can't open '$path/$filename': $!";

> print DC decode_base64($encoded);
> close DC;


If you are worried about corruption or data loss, always check the
return value of close. If there is an error writing to the file (disk
full, network down, or whatever) it will show up here.

close $DC or die "writing to '$path/$filename' failed: $!";

Ben

--
Musica Dei donum optimi, trahit homines, trahit deos. |
Musica truces mollit animos, tristesque mentes erigit.|
Musica vel ipsas arbores et horridas movet feras. |
 
Reply With Quote
 
 
 
 
John Bokma
Guest
Posts: n/a
 
      12-01-2006
"Bigus" <> wrote:

> I am trying to convert a base64 encoded string in a plain text mailing
> list archive file back into a document. I extract the string form the
> file and then decode it like this:
>
> open DC, ">$path/$filename";
> print DC decode_base64($encoded);
> close DC;
>
> where $encoded is the base64 string.
>
> It creates the file, say a jpg or doc, of what looks like about the
> right size, but the images are corrupted. Is there anything obviously
> wrong with this?


Wild guess: perldoc -f binmode

--
John Experienced Perl programmer: http://castleamber.com/

Perl help, tutorials, and examples: http://johnbokma.com/perl/
 
Reply With Quote
 
Mumia W. (reading news)
Guest
Posts: n/a
 
      12-01-2006
On 12/01/2006 10:40 AM, Bigus wrote:
> I am trying to convert a base64 encoded string in a plain text mailing list
> archive file back into a document. I extract the string form the file and
> then decode it like this:
>
> open DC, ">$path/$filename";
> print DC decode_base64($encoded);
> close DC;
>
> where $encoded is the base64 string.
>
> It creates the file, say a jpg or doc, of what looks like about the right
> size, but the images are corrupted. Is there anything obviously wrong with
> this?
>
> Bigus
>
>


It's probably writing the file in text mode which will corrupt a binary
file. Right after you open file file, use the binmode command to set the
file's mode to binary (AKA :raw).

Read the document for binmode:

Start->Run->"perldoc -f binmode"


--

 
Reply With Quote
 
Bigus
Guest
Posts: n/a
 
      12-04-2006

"Ben Morrow" <> wrote in message
news:q83644-...
>
> Quoth "Bigus" <>:
>> I am trying to convert a base64 encoded string in a plain text mailing
>> list
>> archive file back into a document. I extract the string form the file and
>> then decode it like this:
>>
>> open DC, ">$path/$filename";

>
> Use lexical filehandles.
> Use three-arg open.
> Check the return value of open.
>
> open my $DC, '>', "$path/$filename"
> or die "can't open '$path/$filename': $!";
>
> If you are printing binary data to a filehandle you need to use binmode.
> Yes, you need to use binmode on all platforms now.
>
> binmode $DC;


thanks guys. binmode works a treat

WHat are the advantages of lexical filehandles and the 3-arg open?

Bigus


 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      12-04-2006
Ben Morrow wrote:
> If you are printing binary data to a filehandle you need to use binmode.
> Yes, you need to use binmode on all platforms now.


"Now"? Has something changed? In that case, where is that change documented?

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
Charlton Wilbur
Guest
Posts: n/a
 
      12-04-2006
>>>>> "GH" == Gunnar Hjalmarsson <> writes:

GH> Ben Morrow wrote:

>> If you are printing binary data to a filehandle you need to use
>> binmode. Yes, you need to use binmode on all platforms now.


GH> "Now"? Has something changed? In that case, where is that
GH> change documented?

In the past, printing binary data to files that did not have binmode
set often worked by accident. As soon as you introduce IO filters and
various encodings, that "by accident" is much less likely to happen.

The way to guarantee correct behavior is to use binmode when you are
printing binary data. That has not changed.

Charlton



--
Charlton Wilbur

 
Reply With Quote
 
Ben Morrow
Guest
Posts: n/a
 
      12-04-2006

Quoth Gunnar Hjalmarsson <>:
> Ben Morrow wrote:
> > If you are printing binary data to a filehandle you need to use binmode.
> > Yes, you need to use binmode on all platforms now.

>
> "Now"? Has something changed? In that case, where is that change documented?


It changed as of 5.8. Pre-5.8 there was no need for binmode on Unix
systems and other systems whose stdio did not have the concept of 'text
mode' and never did CRLF-"\n" translation. This change is documented,
admittedly somewhat obliquely, in perl58delta.

Ben

--
All persons, living or dead, are entirely coincidental.
Kurt Vonnegut
 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      12-04-2006
Charlton Wilbur wrote:
>>>>>>"GH" == Gunnar Hjalmarsson writes:

>
> GH> Ben Morrow wrote:
>
> >> If you are printing binary data to a filehandle you need to use
> >> binmode. Yes, you need to use binmode on all platforms now.

>
> GH> "Now"? Has something changed? In that case, where is that
> GH> change documented?
>
> In the past, printing binary data to files that did not have binmode
> set often worked by accident. As soon as you introduce IO filters and
> various encodings, that "by accident" is much less likely to happen.
>
> The way to guarantee correct behavior is to use binmode when you are
> printing binary data. That has not changed.


Okay, thanks Charlton! I wondered, since at
http://perldoc.perl.org/functions/binmode.html it still says that it's
necessary "on some systems (in general, DOS and Windows-based systems)".

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
Martijn Lievaart
Guest
Posts: n/a
 
      12-04-2006
On Mon, 04 Dec 2006 09:44:26 +0000, Bigus wrote:

> WHat are the advantages of lexical filehandles


Lexicals are scoped variables which are preferable over globals.

> and the 3-arg open?


It's cleaner, clearer, safer and supports io-layers.

M4
--
Redundancy is a great way to introduce more single points of failure.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FAQ 9.18 How do I decode a MIME/BASE64 string? PerlFAQ Server Perl Misc 0 04-07-2011 04:00 PM
tomcat 4.x : setting mime type for a directory or setting a default mime type CJ Java 1 10-29-2004 07:51 PM
how 2 convert mime to txt or extract words from mime joe Perl Misc 0 04-07-2004 12:34 AM
MIME::Base64.pm Todd Anderson Perl Misc 1 02-03-2004 08:40 AM
Receiving zip files via Mime::Parser/Mime::Decoder Jan Arickx Perl Misc 0 08-25-2003 08:24 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57