CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Encoding, binary file

 



zeno
Novice

Apr 19, 2002, 3:48 AM

Post #1 of 4 (686 views)
Encoding, binary file Can't Post

To whom: Using Perl and no other libraries, I am trying to read in a binary file and I am getting a conversion error. So, I sent the same file (word doc) through Outlook Express to myself and looked over the source code of the email and noticed that OE (in the multipart section) had encoded the file different for some characters. As a result the word doc is corrupt.

Any ideas why my routine gives me different results than Outlook express? I've tried debugging it char-by-char but that proved timely and useless. (Unfortunately, my HOST doesn't have MIME library access. I must do this in simple Perl. So the encode_base64 function I found on the web.)

MY CODE:

while (<$filename>) {
print encode_base64($_);
}


sub encode_base64 ($) {
my $res = "";
my $eol = "\n";
pos($_[0]) = 0;
while ($_[0] =~ /(.{1,45})/gs) {
$res .= substr(pack("u", $1), 1);
chop($res);
}
$res =~ tr|` -_|AA-Za-z0-9+/|;
my $padding = (3 - length($_[0]) % 3) % 3;
$res =~ s/.{$padding}$/"=" x $padding/e if $padding;
if (length $eol) {
$res =~ s/(.{1,76})/$1$eol/g;
}
$res;
}

Ever feel inches away from your destination and every inch feels like a new beginning? Help. Peter.


fashimpaur
User / Moderator

Apr 19, 2002, 4:36 AM

Post #2 of 4 (684 views)
Re: [zeno] Encoding, binary file [In reply to] Can't Post

Zeno,

Although I do not know for sure what the problem is, I was just curious. I
see that you set $eol = "\n". On Windows, isn't $eol eq "\r\n"? I wonder
if this will uncorrupt your data. Post back if that makes any difference.

Good Luck,
Dennis

$a="c323745335d3221214b364d545".
"a362532582521254c3640504c3729".
"2f493759214b3635554c3040606a0",
print unpack"u*",pack "h*",$a,"\n\n";


zeno
Novice

Apr 19, 2002, 6:51 AM

Post #3 of 4 (679 views)
Re: [zeno] Encoding, binary file [In reply to] Can't Post

I tried to do as suggested, it makes no difference. I am using a UNIX host. To be more specific about the problem: some of the characters during the encoding are different. Notice below in the first line of the base64, they are identical until it is "Sg" in OE ENCODING and "TA" in MY ENCODING. I have a feeling it has to do with escape characters, like "/" or "<" or ">" or "\". I also copied the first line of the word doc. Any ideas?

OE ENCODING:

0M8R4KGxGuEAAAAAAAAAAAAAAAAAAAAAPgADAP7/CQAGAAAAAAAAAAAAAAABAAAASgAAAAAAAAAA



MY ENCODING:

0M8R4KGxGuEAAAAAAAAAAAAAAAAAAAAAPgADAP7/CQAGAAAAAAAAAAAAAAABAAAATAAAAAAAAAAA



Original Word Doc:

ࡱ >    L  N 



Thanks again


zeno
Novice

Apr 19, 2002, 9:04 AM

Post #4 of 4 (675 views)
Re: [zeno] Encoding, binary file [In reply to] Can't Post

I think I have pinpointed the exact location of the problem: it's
in the encode_base64 code itself - on the last few lines:

sub encode_base64 ($) {
my $res = "";
my $eol = "\n";
pos($_[0]) = 0;
while ($_[0] =~ /(.{1,45})/gs) {
$res .= substr(pack("u", $1), 1);
chop($res);
}
$res =~ tr|` -_|AA-Za-z0-9+/|;
#!!! Here's where the different characters are created!!!!
# my $padding = (3 - length($_[0]) % 3) % 3;
# $res =~ s/.{$padding}$/"=" x $padding/e if $padding;
# if (length $eol) {
# $res =~ s/(.{1,76})/$1$eol/g;
# }

$res;
}

I have borrowed this code and don't know what it's trying to do - is it a standard? Since I have no MIME access on my host, and I'm using UNIX - is it possible another encoding function exists, something like uu?encode? Thanks again. Peter.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives