twpear has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to convert a windows .ico file into Base64 (so that I can inline it into some code, then Base64 decode it to a file and use it elsewhere). I have played with various open pragma and binmode (including none), but the decoded file is not 100% replica of the original ico file. There are some 0x0d0x0a where the original only has 0x0a, very suggestive of line-sending problems, but to date nothing has worked. I have switched to UUEncode and UUDecode to get my program working, but I am concerned that MIME::Base64 is not generating a faithful rendering.
use MIME::Base64 qw(encode_base64 decode_base64); #ENCODE open(INBIN, "<:bytes","in.ico") or die "$!"; open(OUTB64, ">esc.b64") or die "$!"; binmode(INBIN); while (read(INBIN, $buf, 60*57)) { print OUTB64 encode_base64($buf); } close (INBIN); close (OUTB64); #DECODE open(OUTBIN, ">:bytes","out.ico") or die "$!"; open(INB64, "<esc.b64") or die "$!"; binmode(OUTBIN); while (<INB64>) { print OUTBIN decode_base64($_); } close (OUTBIN); close (INB64);

Replies are listed 'Best First'.
Re: MIME::Base64 on win32 (Activestate 5.10) broken ?
by ikegami (Patriarch) on Jan 30, 2009 at 17:01 UTC

    There's nothing wrong with your program, and I can't replicate your problem. ( Also using ActiveState 5.10.0 )

    >debug in.ico -rcx CX 0015 : -d100 l15 0B12:0100 61 62 63 0D 64 65 66 0D-0A 67 68 69 0A 6A 6B 6C abc.def.. +ghi.jkl 0B12:0110 0D 0A 0A 0D 0D ..... -q >c:\progs\perl5100\bin\perl 740238.pl >fc /b in.ico out.ico Comparing files in.ico and OUT.ICO FC: no differences encountered >debug out.ico -rcx CX 0015 : -d100 l15 0B12:0100 61 62 63 0D 64 65 66 0D-0A 67 68 69 0A 6A 6B 6C abc.def.. +ghi.jkl 0B12:0110 0D 0A 0A 0D 0D ..... -q

    I suspect some compatibility layer outside of Perl is messing with Perl's IO.

Re: MIME::Base64 on win32 (Activestate 5.10) broken ?
by mr_mischief (Monsignor) on Jan 30, 2009 at 17:28 UTC
    As ikegami notes, I get no problem data from your program. I tried 5.8.8 and 5.10.0 on Linux, Strawberry 5.10 on Windows, and ActiveState 5.8.8 and 5.10.0 on Windows. In every case, the generated file is identical to the source file. I also tried it with a PNG without the encoding changes on the file handles with the same results:

    use MIME::Base64 qw( encode_base64 decode_base64 ); open( INBIN, '<', 'file.png' ) or die "$!"; open( OUTB64, '>', 'file.b64' ) or die "$!"; binmode( INBIN ); while ( read( INBIN, $buf, 60 * 57 ) ) { print OUTB64 encode_base64( $buf ); } close ( INBIN ); close ( OUTB64 ); open( OUTBIN, '>', 'out.png' ) or die "$!"; open( INB64, '<', 'file.b64' ) or die "$!"; binmode( OUTBIN ); while ( <INB64> ) { print OUTBIN decode_base64( $_ ); } close ( OUTBIN ); close ( INB64 );

    I am curious what exactly is different about your ActiveState 5.10.0 and these other installations. What's your MIME::Base64 version? What's your Windows version? Are you using anything else in the mix like Windows-to-Unix compatibility tools or anything?