base64 en/decoding helper

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

base64 en/decoding helper

Joachim Jaeckel
Hello,

just to make sure, that I didn't overlook something!

There's no base64 en- de-coding method somewhere in the packages?

I only found
NetClients.MIME.MimeEncodedWordCoDec decodeBase64From:to:in:

but that throws every time a "did not understand #digitAt...
(and is only a decode method.)

Best regards,
Joachim.


_______________________________________________
help-smalltalk mailing list
[hidden email]
http://lists.gnu.org/mailman/listinfo/help-smalltalk
Reply | Threaded
Open this post in threaded view
|

Re: base64 en/decoding helper

Paolo Bonzini-2
Joachim Jaeckel wrote:
> Hello,
>
> just to make sure, that I didn't overlook something!
>
> There's no base64 en- de-coding method somewhere in the packages?

Nope.

> I only found
> NetClients.MIME.MimeEncodedWordCoDec decodeBase64From:to:in:

It's unused, which is why it doesn't work.

BTW

Integer extend [
     digitAt: i [
         ^(self bitShift: 8 - (8*i)) bitAnd: 255
     ]
]

Paolo


_______________________________________________
help-smalltalk mailing list
[hidden email]
http://lists.gnu.org/mailman/listinfo/help-smalltalk
Reply | Threaded
Open this post in threaded view
|

Re: base64 en/decoding helper

Joachim Jaeckel
> BTW
>
> Integer extend [
>     digitAt: i [
>         ^(self bitShift: 8 - (8*i)) bitAnd: 255
>     ]
> ]

Great, thanks :-)

decoding works...



_______________________________________________
help-smalltalk mailing list
[hidden email]
http://lists.gnu.org/mailman/listinfo/help-smalltalk
Reply | Threaded
Open this post in threaded view
|

Re: Re: base64 en/decoding helper

Joachim Jaeckel
Hello,

just for the case if it's usefull to somebody else, or maybe Paolo would
like to integrate it into the gst sources (if the problem of base64 is
not to trivial)

...and...

I'd think, that there is some potential in finetune the code for the
longer smalltalker (and I'd be happy to learn from some improvements).

I have created a base64 encoding method (and I will work on a decoding,
too, because some encoded strings could not be decoded through the
former mentioned NetClient.MIME method...) -> (and for the test, these
strings could be decoded through Perl's MIME::Base64...)

So find attached my first version...

And please again one question... how do I represent a '\n' in Smalltalk?
(Every 76 characters, normaly a '\n' has to be inserted into the
result-string...)

Best regards,
Joachim.

Object subclass: Base64 [

    Base64 class >> encode: aString [
        | chars i j tripple pads c1 c2 c3 c4 b64string |
        chars := 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'.
        b64string := String new.

        pads := 0.
        i := 1.

        [i <= aString size] whileTrue: [
            j := i +2.
            (j > aString size) ifTrue: [j := aString size].
            tripple := aString copyFrom: i to: j.

            (tripple size < 3) ifTrue: [
                pads := 3 - tripple size.
                1 to: pads do: [ :n |
                    tripple growBy: 1.
                    tripple at: (tripple size) put: (0 asCharacter).
                ]
            ].

            b64string growBy: 4.
            c1 := (tripple at: 1) asInteger bitShift: -2.
            b64string at: (b64string size -3) put: (chars at: c1 +1).

            c2 := (((tripple at: 1) asInteger bitAnd: 3) bitShift: 4) bitOr: ((tripple at: 2) asInteger bitShift: -4).
            b64string at: (b64string size -2) put: (chars at: c2 +1).

            c3 := (((tripple at: 2) asInteger bitAnd: 15) bitShift: 2) bitOr: ((tripple at: 3) asInteger bitShift: -6).
            b64string at: (b64string size -1) put: (chars at: c3 +1).

            c4 := (tripple at: 3) asInteger bitAnd: 63.
            b64string at: (b64string size) put: (chars at: c4 +1).

            i := i +3.
        ].

        (pads > 0) ifTrue: [
            1 to: pads do: [ :n |
                b64string at: (b64string size -n +1) put: $=.
            ]
        ].

        ^b64string
    ]
]

_______________________________________________
help-smalltalk mailing list
[hidden email]
http://lists.gnu.org/mailman/listinfo/help-smalltalk
Reply | Threaded
Open this post in threaded view
|

Re: Re: base64 en/decoding helper

Stefan Schmiedl
On Fri, 26 Jun 2009 20:39:36 +0200
Joachim Jaeckel <[hidden email]> wrote:

> And please again one question... how do I represent a '\n' in
> Smalltalk? (Every 76 characters, normaly a '\n' has to be inserted
> into the result-string...)

"Character class inspect" gives a lot of useful info here.

s.


_______________________________________________
help-smalltalk mailing list
[hidden email]
http://lists.gnu.org/mailman/listinfo/help-smalltalk
Reply | Threaded
Open this post in threaded view
|

Re: Re: base64 en/decoding helper

Joachim Jaeckel
Ahh :-)

Thanks! So even my (0 asCharacter) could be replaced by 'Character nul'

Best regards,
Joachim.



Stefan Schmiedl schrieb:

> On Fri, 26 Jun 2009 20:39:36 +0200
> Joachim Jaeckel <[hidden email]> wrote:
>
>> And please again one question... how do I represent a '\n' in
>> Smalltalk? (Every 76 characters, normaly a '\n' has to be inserted
>> into the result-string...)
>
> "Character class inspect" gives a lot of useful info here.
>
> s.
>
>
> _______________________________________________
> help-smalltalk mailing list
> [hidden email]
> http://lists.gnu.org/mailman/listinfo/help-smalltalk
>
>



_______________________________________________
help-smalltalk mailing list
[hidden email]
http://lists.gnu.org/mailman/listinfo/help-smalltalk
Reply | Threaded
Open this post in threaded view
|

Re: Re: base64 en/decoding helper

Stefan Schmiedl
In reply to this post by Joachim Jaeckel
I'll reply to a part that I think I understood:

On Fri, 26 Jun 2009 20:39:36 +0200
Joachim Jaeckel <[hidden email]> wrote:

> pads := 0.
> i := 1.
>
> [i <= aString size] whileTrue: [
>    j := i +2.
>    (j > aString size) ifTrue: [j := aString size].
>    tripple := aString copyFrom: i to: j.
>
>    (tripple size < 3) ifTrue: [
> pads := 3 - tripple size.
> 1 to: pads do: [ :n |
>    tripple growBy: 1.
>    tripple at: (tripple size) put: (0 asCharacter).
> ]
>    ].

Some quick reflex actions on the above part:

If the string size does not change, put it into a temporary variable.

j already shows if you got three characters, no need to ask tripple
(triplet?) for it's size.

iterate over "aString , '\0\0\0'", you'll get your padding at the end
for free without extra checks

gotta go now,
s.

You're repeatedly grabbing triplets from the string and are looking
every time if you really got three characters, right?

--
Stefan Schmiedl
EDV-Beratung Schmiedl, Berghangstr. 5, D-93413 Cham
im Büro: 09971 9966 989, am Handy: 0160 9981 6278


_______________________________________________
help-smalltalk mailing list
[hidden email]
http://lists.gnu.org/mailman/listinfo/help-smalltalk
Reply | Threaded
Open this post in threaded view
|

Re: Re: base64 en/decoding helper

Joachim Jaeckel
> Some quick reflex actions on the above part:
>
> If the string size does not change, put it into a temporary variable.
>
> j already shows if you got three characters, no need to ask tripple
> (triplet?) for it's size.

Yes, that's right!

> iterate over "aString , '\0\0\0'", you'll get your padding at the end
> for free without extra checks

Sorry, but I didn't get that... do you mean to initialize 'tripple' with
'\0\0\0'?

> You're repeatedly grabbing triplets from the string and are looking
> every time if you really got three characters, right?

Yes, because the last one could end with less...

But I see some potential for improvement...

Thanks! :-)


_______________________________________________
help-smalltalk mailing list
[hidden email]
http://lists.gnu.org/mailman/listinfo/help-smalltalk
Reply | Threaded
Open this post in threaded view
|

Re: Re: base64 en/decoding helper

Stefan Schmiedl
On Fri, 26 Jun 2009 21:19:04 +0200
Joachim Jaeckel <[hidden email]> wrote:

> > iterate over "aString , '\0\0\0'", you'll get your padding at the
> > end for free without extra checks
>
> Sorry, but I didn't get that... do you mean to initialize 'tripple'
> with '\0\0\0'?

First of all, I don't know how a string with three nuls would have
to be written correctly. That said, the rest is valid:

'abcde' , '...'  gives you 'abcde...'
which becomes 'abc' and 'de.'.
The loop ends when j >= aString size (original string).

The best solution to a problem is avoiding it :-)

s.


_______________________________________________
help-smalltalk mailing list
[hidden email]
http://lists.gnu.org/mailman/listinfo/help-smalltalk