Home > Archive > Rexx > October 2006 > encodebase64 method of the String class in ooRexx 3.1
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
encodebase64 method of the String class in ooRexx 3.1
|
|
| Salvador Parra Camacho 2006-10-14, 7:01 pm |
|
I wrote the following scripts in Object REXX, PHP, Ruby, Perl and
Python:
-- Object REXX
say 'Hello world'~encodebase64
<?php
echo base64_encode("Hello world")."\n";
?>
# Ruby
puts ['Hello world'].pack('m')
# Perl
use MIME::Base64;
print encode_base64('Hello world');
# Python
import base64
print base64.b64encode('Hello world')
The Object Rexx version prints:
SGVsbG8gd29=bG==
all the other versions prints:
SGVsbG8gd29ybGQ=
Is this a reported bug of ooRexx 3.1 (REXX-ooRexx_3.1(MT) 6.00 15 Aug
2006)?
Best regards:
Salvador Parra Camacho
| |
|
| Hi Salvador,
Salvador Parra Camacho wrote:
> I wrote the following scripts in Object REXX, PHP, Ruby, Perl and
> Python:
>
> -- Object REXX
> say 'Hello world'~encodebase64
>
> <?php
> echo base64_encode("Hello world")."\n";
> ?>
>
> # Ruby
> puts ['Hello world'].pack('m')
>
> # Perl
> use MIME::Base64;
> print encode_base64('Hello world');
>
> # Python
> import base64
> print base64.b64encode('Hello world')
>
> The Object Rexx version prints:
>
> SGVsbG8gd29=bG==
>
> all the other versions prints:
>
> SGVsbG8gd29ybGQ=
Also doing the following rexxtry:
say "SGVsbG8gd29ybGQ="~decodeBase64
Helowold
........................................... rexxtry.rex on WindowsNT
say "Hello world"~encodebase64~decodebase64
Oooops ! ... try again. Incorrect call to method
Invalid Base 64 encoded string.
rc = 93 ................................... rexxtry.rex on WindowsNT
> Is this a reported bug of ooRexx 3.1 (REXX-ooRexx_3.1(MT) 6.00 15 Aug 2006)?
No, not that I could find an entry at <https://sourceforge.net/tracker/?gr...701&atid=684730>.
Please report that, such that it can be tackled.
Regards,
---rony
| |
| Steve Swift 2006-10-15, 4:01 am |
| I have a base64() function written in Rexx if you need that as a bypass
until Object Rexx is fixed.
Why not put it here?
/* Both way base64 */
Signal on syntax
String = strip(arg(1))
Base64Alphabet =
" ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmn
opqrstuvwxyz0123456789+/"
Say copies('-',79)
If verify(Strip(string,'T','='),Base64Alpha
bet) = 0 then say
Base642string(string)
Else say String2Base64(string)
Exit
String2Base64: Procedure expose base64alphabet
Parse Arg Input
Bits = x2b(c2x(input))
Remainder = Length(Bits) // 6
If Remainder <> 0 Then Bits = Bits || Copies("0", 6 - Remainder)
BitLength = Length(Bits)
BaseString = ""
Do Index = 1 to BitLength by 6
Cluster = SubStr(Bits, Index, 6)
BaseIndex = X2D(B2X(Cluster)) + 1
BaseString = BaseString || SubStr(Base64Alphabet, BaseIndex, 1)
End
Remainder = length(input)//3
If remainder <> 0 then basestring = basestring || copies('=',3-remainder)
Return BaseString
Base642String: Procedure expose base64alphabet
String = strip(arg(1),'T','=')
Bits = ''
Do I = 1 to length(string)
C = substr(string,i,1)
P = pos(c,base64alphabet)-1
Bits = bits''right(x2b(d2x(p)),6,0)
End
String = ''
Do I = 1 to length(bits)%8*8 by 8
Byte = substr(bits,i,8)
Hex = b2x(byte)
String = string''x2c(hex)
End
Return string
Syntax:
Say errortext(rc)
Say sigl '*-*' sourceline(sigl)
Trace ?r
Nop
Exit
--
Steve Swift
http://www.ringers.org.uk
| |
|
| Hi Steve,
Steve Swift wrote:
> I have a base64() function written in Rexx if you need that as a bypass
> until Object Rexx is fixed.
>
> Why not put it here?
Great! Thought you might be interested how an "ooRexx" version looks like, so I edited your code
slightly. It looks almost like your original, except that the base64-alphabet is now stored in the
environment named ".local" and as such can be always retrieved from any Rexx program using the
environment symbol ".base64alphabet" (note the leading dot, making the symbol an environment
symbol). Also your functions were merely changed to be public routines (public makes them visible
from outside of your program), and because of that the "signal on syntax" and the syntax exception
handling code had to be moved within each routine (each routine is regarded to be its own program
with its own scope).
This way, any Rexx program needing the base64-routines merely needs to call the enclosed program and
afterwards has access to the two public routines "string2base64()" and "base642string()".
Here's the result of a rexxtry session using the enclosed edited version of your program (stored in
a file named "b64rgf.rex"):
F:\test\orx\b64>rexxtry
REXX-ooRexx_3.1(MT) 6.00 7 Oct 2006
rexxtry.rex lets you interactively try REXX statements.
Each string is executed when you hit Enter.
Enter 'call tell' for a description of the features.
Go on - try a few... Enter 'exit' to end.
call b64rgf
........................................... rexxtry.rex on WindowsNT
a="Hello world" ;a1=string2base64(a);a2=base642string(a1
);say a"/"a1"/"a2"/"
Hello world/SGVsbG8gd29ybGQ=/Hello world/
........................................... rexxtry.rex on WindowsNT
Here is your program, taking advantage of the ooRexx local environment and the concept of public
routines:
--------------------- cut here --------------------
/*
Name: "b64rgf.rex"
Purpose: show how to turn a procedure into a public routine
source: Date: Sun, 15 Oct 2006 06:58:35 +0100
From: Steve Swift <Steve.J.Swift@gmail.com>
User-Agent: Thunderbird 1.5.0.7 (Windows/20060909)
MIME-Version: 1.0
Newsgroups: comp.lang.rexx
Subject: Re: encodebase64 method of the String class in ooRexx 3.1
editor: ---rgf
date: 2006-10-15
*/
/* initialisation: save alphabet in .local environment */
..local~Base64Alphabet = " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmn
opqrstuvwxyz0123456789+/"
::routine String2Base64 public
Parse Arg Input
Signal on syntax
Bits = x2b(c2x(input))
Remainder = Length(Bits) // 6
If Remainder <> 0 Then Bits = Bits || Copies("0", 6 - Remainder)
BitLength = Length(Bits)
BaseString = ""
Do Index = 1 to BitLength by 6
Cluster = SubStr(Bits, Index, 6)
BaseIndex = X2D(B2X(Cluster)) + 1
BaseString = BaseString || SubStr(.Base64Alphabet, BaseIndex, 1)
End
Remainder = length(input)//3
If remainder <> 0 then basestring = basestring || copies('=',3-remainder)
Return BaseString
Syntax:
Say errortext(rc)
Say sigl '*-*' sourceline(sigl)
Trace ?r
Nop
Exit
::routine Base642String public
Signal on syntax
String = strip(arg(1),'T','=')
Bits = ''
Do I = 1 to length(string)
C = substr(string,i,1)
P = pos(c,.base64alphabet)-1
Bits = bits''right(x2b(d2x(p)),6,0)
End
String = ''
Do I = 1 to length(bits)%8*8 by 8
Byte = substr(bits,i,8)
Hex = b2x(byte)
String = string''x2c(hex)
End
Return string
Syntax:
Say errortext(rc)
Say sigl '*-*' sourceline(sigl)
Trace ?r
Nop
Exit
--------------------- cut here --------------------
Again, thanks for your helping out!
;)
Regards,
---rony
| |
| Salvador Parra Camacho 2006-10-15, 7:09 pm |
| rony ha escrito:
> No, not that I could find an entry at <https://sourceforge.net/tracker/?gr...701&atid=684730>.
Thank you for the information.
> Please report that, such that it can be tackled.
The bug report is now in the SourceForge web page of the project.
Unfortunatly I forgot to attach the files. For example:
/*bin/true;exec rexx "$0" "$@";exit # REXX */
parse source source
say source
parse version version
say version
'uname -a'
d_string = 'Hello world'
e_string = 'SGVsbG8gd29ybGQ='
say 'encodebase64 should return: b =' e_string
say 'encodebase64 returns: a =' d_string~encodebase64
say 'b~decodebase64 returns:' e_string~decodebase64
say 'a~decodebase64 returns:' d_string~encodebase64~decodebase64
Best regards:
Salvador Parra Camacho
| |
| Salvador Parra Camacho 2006-10-15, 7:09 pm |
|
Steve Swift ha escrito:
> I have a base64() function written in Rexx if you need that as a bypass
> until Object Rexx is fixed.
>
> Why not put it here?
Thank you very much, Steve. I only need the function for a simple
personal program but it works fine (it is VERY slow since it must
generate a 30 kb file, but it works).
Best regards:
Salvador Parra Camacho
| |
| Salvador Parra Camacho 2006-10-15, 7:09 pm |
| rony ha escrito:
> This way, any Rexx program needing the base64-routines merely needs to call the enclosed program and
> afterwards has access to the two public routines "string2base64()" and "base642string()".
Rony, I use the same method here. :) (Except the name of the routines
and the use of the local environment to store the alphabet.)
Best regards:
Salvador Parra Camacho
| |
| Steve Swift 2006-10-16, 4:03 am |
| That's really useful for me... I'm just starting to think about using
::routine, even in my classic-rexx code (All of my environments have
object rexx installed, except my brain!) so I'll use your example as a
good starting point!
--
Steve Swift
http://www.ringers.org.uk
| |
| David Ashley 2006-10-16, 7:00 pm |
| Salvador Parra Camacho wrote:
> I wrote the following scripts in Object REXX, PHP, Ruby, Perl and
> Python:
>
> -- Object REXX
> say 'Hello world'~encodebase64
>
> <?php
> echo base64_encode("Hello world")."\n";
> ?>
>
> # Ruby
> puts ['Hello world'].pack('m')
>
> # Perl
> use MIME::Base64;
> print encode_base64('Hello world');
>
> # Python
> import base64
> print base64.b64encode('Hello world')
>
> The Object Rexx version prints:
>
> SGVsbG8gd29=bG==
>
> all the other versions prints:
>
> SGVsbG8gd29ybGQ=
>
> Is this a reported bug of ooRexx 3.1 (REXX-ooRexx_3.1(MT) 6.00 15 Aug
> 2006)?
>
> Best regards:
>
> Salvador Parra Camacho
>
I fixed this problem in the ooRexx code base. It had to do with the code
not counting the last characters in the input string properly in the
encodebase64 method. The decodebase64 method appears to be working
correctly. Look for the fix to appear in the next release of the code.
Sorry about the problem.
W. David Ashley
|
|
|
|
|