help-smalltalk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-smalltalk] Re: base64 en/decoding helper


From: Joachim Jaeckel
Subject: Re: [Help-smalltalk] Re: base64 en/decoding helper
Date: Fri, 26 Jun 2009 20:39:36 +0200
User-agent: Mozilla-Thunderbird 2.0.0.19 (X11/20090103)

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
    ]
]

reply via email to

[Prev in Thread] Current Thread [Next in Thread]