Base64Coder.st
author Claus Gittinger <cg@exept.de>
Wed, 20 Mar 2019 21:23:46 +0100
changeset 4891 d8c52483ab4c
parent 4754 95158fac7fde
child 4894 206d7a1d926b
permissions -rw-r--r--
#TUNING by cg class: Base64Coder changed: #flush
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4743
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
     1
"{ Encoding: utf8 }"
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
     2
1308
d7bea6d0b3b6 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1122
diff changeset
     3
"
d7bea6d0b3b6 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1122
diff changeset
     4
 COPYRIGHT (c) 2002 by eXept Software AG
d7bea6d0b3b6 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1122
diff changeset
     5
              All Rights Reserved
d7bea6d0b3b6 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1122
diff changeset
     6
d7bea6d0b3b6 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1122
diff changeset
     7
 This software is furnished under a license and may be used
d7bea6d0b3b6 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1122
diff changeset
     8
 only in accordance with the terms of that license and with the
d7bea6d0b3b6 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1122
diff changeset
     9
 inclusion of the above copyright notice.   This software may not
d7bea6d0b3b6 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1122
diff changeset
    10
 be provided or otherwise made available to, or used by, any
d7bea6d0b3b6 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1122
diff changeset
    11
 other person.  No title to or ownership of the software is
d7bea6d0b3b6 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1122
diff changeset
    12
 hereby transferred.
d7bea6d0b3b6 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1122
diff changeset
    13
"
1027
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    14
"{ Package: 'stx:libbasic2' }"
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    15
3478
ce272a495f41 class: Base64Coder
Claus Gittinger <cg@exept.de>
parents: 3210
diff changeset
    16
"{ NameSpace: Smalltalk }"
ce272a495f41 class: Base64Coder
Claus Gittinger <cg@exept.de>
parents: 3210
diff changeset
    17
1964
671b01812775 New BaseNCoder
Stefan Vogel <sv@exept.de>
parents: 1888
diff changeset
    18
BaseNCoder subclass:#Base64Coder
671b01812775 New BaseNCoder
Stefan Vogel <sv@exept.de>
parents: 1888
diff changeset
    19
	instanceVariableNames:''
1027
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    20
	classVariableNames:'Base64Mapping Base64ReverseMapping'
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    21
	poolDictionaries:''
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    22
	category:'System-Storage'
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    23
!
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    24
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    25
!Base64Coder class methodsFor:'documentation'!
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    26
1308
d7bea6d0b3b6 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1122
diff changeset
    27
copyright
d7bea6d0b3b6 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1122
diff changeset
    28
"
d7bea6d0b3b6 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1122
diff changeset
    29
 COPYRIGHT (c) 2002 by eXept Software AG
d7bea6d0b3b6 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1122
diff changeset
    30
              All Rights Reserved
d7bea6d0b3b6 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1122
diff changeset
    31
d7bea6d0b3b6 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1122
diff changeset
    32
 This software is furnished under a license and may be used
d7bea6d0b3b6 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1122
diff changeset
    33
 only in accordance with the terms of that license and with the
d7bea6d0b3b6 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1122
diff changeset
    34
 inclusion of the above copyright notice.   This software may not
d7bea6d0b3b6 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1122
diff changeset
    35
 be provided or otherwise made available to, or used by, any
d7bea6d0b3b6 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1122
diff changeset
    36
 other person.  No title to or ownership of the software is
d7bea6d0b3b6 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1122
diff changeset
    37
 hereby transferred.
d7bea6d0b3b6 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1122
diff changeset
    38
"
d7bea6d0b3b6 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1122
diff changeset
    39
!
d7bea6d0b3b6 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1122
diff changeset
    40
1027
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    41
documentation
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    42
"
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    43
    Instances of this class perform Base64 en- and decoding as defined in RFC 2045
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    44
    3 bytes are mapped to 4 characters, representing 6 bits each.
2068
486ce2d49df0 comment
sr
parents: 1964
diff changeset
    45
    The encoded string consists only of characters from the set:
486ce2d49df0 comment
sr
parents: 1964
diff changeset
    46
        'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='
1027
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    47
4520
d2ea4e714e1f #DOCUMENTATION by mawalch
mawalch
parents: 4507
diff changeset
    48
    The main entry point API is
4279
bb907803ee15 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 3763
diff changeset
    49
        Base64Coder encode:aStringOrBytes
bb907803ee15 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 3763
diff changeset
    50
    and
bb907803ee15 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 3763
diff changeset
    51
        Base64Coder decode:aString
bb907803ee15 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 3763
diff changeset
    52
bb907803ee15 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 3763
diff changeset
    53
    If the decoder should return a string, use
bb907803ee15 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 3763
diff changeset
    54
        Base64Coder decodeAsString:aString.
bb907803ee15 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 3763
diff changeset
    55
1027
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    56
    [author:]
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    57
        Stefan Vogel
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    58
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    59
    [see also:]
4743
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
    60
        RFC https://tools.ietf.org/html/rfc4648
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
    61
        
1027
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    62
    [instance variables:]
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    63
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    64
    [class variables:]
4520
d2ea4e714e1f #DOCUMENTATION by mawalch
mawalch
parents: 4507
diff changeset
    65
        Base64Mapping         String   Mapping from bytes (with 6 valid bits)
1027
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    66
                                       to Base64 characters
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    67
        Base64ReverseMapping  Array    Mapping from Base64 characters to 6-bit-Bytes
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    68
"
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    69
!
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    70
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    71
examples
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    72
"
1122
9f6405dc3319 example added
Claus Gittinger <cg@exept.de>
parents: 1111
diff changeset
    73
                                                                [exBegin]
3478
ce272a495f41 class: Base64Coder
Claus Gittinger <cg@exept.de>
parents: 3210
diff changeset
    74
   (Base64Coder encode:'queen%27s%20gambit') asString = 'cXVlZW4lMjdzJTIwZ2FtYml0'
ce272a495f41 class: Base64Coder
Claus Gittinger <cg@exept.de>
parents: 3210
diff changeset
    75
                                                                [exEnd]
ce272a495f41 class: Base64Coder
Claus Gittinger <cg@exept.de>
parents: 3210
diff changeset
    76
ce272a495f41 class: Base64Coder
Claus Gittinger <cg@exept.de>
parents: 3210
diff changeset
    77
                                                                [exBegin]
ce272a495f41 class: Base64Coder
Claus Gittinger <cg@exept.de>
parents: 3210
diff changeset
    78
   (Base64Coder decode:'cXVlZW4lMjdzJTIwZ2FtYml0') asString = 'queen%27s%20gambit'
2224
3f9562af7e35 example
Claus Gittinger <cg@exept.de>
parents: 2137
diff changeset
    79
                                                                [exEnd]
3f9562af7e35 example
Claus Gittinger <cg@exept.de>
parents: 2137
diff changeset
    80
3f9562af7e35 example
Claus Gittinger <cg@exept.de>
parents: 2137
diff changeset
    81
                                                                [exBegin]
1122
9f6405dc3319 example added
Claus Gittinger <cg@exept.de>
parents: 1111
diff changeset
    82
   |data1 text data2|
9f6405dc3319 example added
Claus Gittinger <cg@exept.de>
parents: 1111
diff changeset
    83
9f6405dc3319 example added
Claus Gittinger <cg@exept.de>
parents: 1111
diff changeset
    84
   data1 := #[0 1 16r7F 16r80 16r81 16rFE 16rFF].
1639
dd7c77c44be6 Fix examples
Stefan Vogel <sv@exept.de>
parents: 1484
diff changeset
    85
   text := Base64Coder encode:data1.
dd7c77c44be6 Fix examples
Stefan Vogel <sv@exept.de>
parents: 1484
diff changeset
    86
   data2 := Base64Coder decode:text.
4520
d2ea4e714e1f #DOCUMENTATION by mawalch
mawalch
parents: 4507
diff changeset
    87
   data2
1122
9f6405dc3319 example added
Claus Gittinger <cg@exept.de>
parents: 1111
diff changeset
    88
                                                                [exEnd]
1027
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    89
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    90
                                                                [exBegin]
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    91
   |coder|
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    92
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    93
   coder := Base64Coder on:'' writeStream.
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    94
   coder nextPutAll:#[0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19].
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    95
   coder flush.
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    96
   coder contents inspect.
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    97
   coder reset.
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    98
   coder nextPut:254.
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    99
   coder contents inspect.
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   100
                                                                [exEnd]
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   101
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   102
                                                                [exBegin]
1391
530a5924e319 comment
Claus Gittinger <cg@exept.de>
parents: 1390
diff changeset
   103
   |coder decoder|
1027
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   104
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   105
   coder := Base64Coder on:'' writeStream.
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   106
   coder nextPutAll:#[0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20].
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   107
   coder flush.
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   108
1391
530a5924e319 comment
Claus Gittinger <cg@exept.de>
parents: 1390
diff changeset
   109
   decoder := Base64Coder on:(coder contents readStream).
530a5924e319 comment
Claus Gittinger <cg@exept.de>
parents: 1390
diff changeset
   110
   [decoder atEnd] whileFalse:[
530a5924e319 comment
Claus Gittinger <cg@exept.de>
parents: 1390
diff changeset
   111
      Transcript show:decoder next
1027
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   112
   ].
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   113
   Transcript cr.
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   114
                                                                [exEnd]
1389
8eb6ec86c0c7 allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents: 1315
diff changeset
   115
                                                                [exBegin]
8eb6ec86c0c7 allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents: 1315
diff changeset
   116
   |coder|
8eb6ec86c0c7 allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents: 1315
diff changeset
   117
8eb6ec86c0c7 allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents: 1315
diff changeset
   118
   coder := Base64Coder on:'' writeStream.
8eb6ec86c0c7 allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents: 1315
diff changeset
   119
   coder nextPutAll:(0 to:200) asByteArray.
8eb6ec86c0c7 allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents: 1315
diff changeset
   120
   coder flush.
8eb6ec86c0c7 allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents: 1315
diff changeset
   121
8eb6ec86c0c7 allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents: 1315
diff changeset
   122
   Transcript showCR:(coder contents).
8eb6ec86c0c7 allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents: 1315
diff changeset
   123
                                                                [exEnd]
4743
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   124
                                                                [exBegin]
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   125
   |bytes encoded decoded|
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   126
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   127
   bytes := #[0 0 0] copy.
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   128
   0 to:255 do:[:b1 |
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   129
       Transcript showCR:b1.  
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   130
       bytes at:1 put:b1.  
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   131
       0 to:255 do:[:b2 |
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   132
           bytes at:2 put:b2.  
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   133
           0 to:255 do:[:b3 |
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   134
               bytes at:3 put:b3.  
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   135
               encoded := Base64Coder encode:bytes.
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   136
               decoded := Base64Coder decode:encoded.
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   137
               self assert:(decoded = bytes).
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   138
           ]
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   139
       ]
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   140
   ].
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   141
                                                                [exEnd]
1027
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   142
"
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   143
! !
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   144
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   145
!Base64Coder class methodsFor:'initialization'!
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   146
3547
399b59b67fda initialization done lazily (to speed up startup)
Claus Gittinger <cg@exept.de>
parents: 3478
diff changeset
   147
initializeMappings
1027
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   148
    "initialize class variables"
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   149
3547
399b59b67fda initialization done lazily (to speed up startup)
Claus Gittinger <cg@exept.de>
parents: 3478
diff changeset
   150
    Base64Mapping isNil ifTrue:[
399b59b67fda initialization done lazily (to speed up startup)
Claus Gittinger <cg@exept.de>
parents: 3478
diff changeset
   151
        "65 characters representing the 6-bit values from 0-63 and one pad character"
399b59b67fda initialization done lazily (to speed up startup)
Claus Gittinger <cg@exept.de>
parents: 3478
diff changeset
   152
        Base64Mapping := 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='.
4743
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   153
        Base64ReverseMapping := self reverseMappingFor:Base64Mapping.
1027
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   154
    ].
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   155
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   156
    "
4743
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   157
     Base64Mapping := nil.
3547
399b59b67fda initialization done lazily (to speed up startup)
Claus Gittinger <cg@exept.de>
parents: 3478
diff changeset
   158
     self initializeMappings
1027
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   159
    "
4743
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   160
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   161
    "Modified (comment): / 30-09-2018 / 15:39:44 / Claus Gittinger"
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   162
!
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   163
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   164
mapping
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   165
    ^ Base64Mapping
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   166
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   167
    "Created: / 30-09-2018 / 15:30:33 / Claus Gittinger"
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   168
!
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   169
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   170
reverseMapping
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   171
    ^ Base64ReverseMapping
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   172
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   173
    "Created: / 30-09-2018 / 15:30:40 / Claus Gittinger"
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   174
! !
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   175
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   176
!Base64Coder class methodsFor:'decoding'!
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   177
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   178
decode:aStringOrStream
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   179
    "because base64 decoding is used heavily in some protocols,
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   180
     a specially tuned version is provided here for the common case of decoding a string"
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   181
4753
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   182
    aStringOrStream isString ifTrue:[
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   183
        ^ self fastDecodeString:aStringOrStream
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   184
    ].    
4743
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   185
    ^ super decode:aStringOrStream.
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   186
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   187
    "Created: / 30-09-2018 / 14:14:51 / Claus Gittinger"
4753
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   188
    "Modified: / 30-09-2018 / 16:58:21 / Claus Gittinger"
4743
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   189
!
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   190
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   191
fastDecodeString:aString
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   192
    "because base64 decoding is used heavily in some protocols,
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   193
     a specially tuned version is provided here for the common case of decoding a string"
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   194
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   195
    ^ self fastDecodeString:aString asString:false
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   196
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   197
    "
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   198
     (Base64Coder encode:'queen%27s%20gambit') asString => 'cXVlZW4lMjdzJTIwZ2FtYml0'
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   199
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   200
     (Base64Coder fastDecodeString:'cXVlZW4lMjdzJTIwZ2FtYml0') asString => 'queen%27s%20gambit'
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   201
    "
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   202
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   203
    "Created: / 30-09-2018 / 14:36:58 / Claus Gittinger"
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   204
!
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   205
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   206
fastDecodeString:aString asString:asStringBoolean
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   207
    "because base64 decoding is used heavily in some protocols,
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   208
     a specially tuned version is provided here for the common case of decoding a string"
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   209
4753
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   210
    |decoding revMapping|
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   211
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   212
    revMapping := self reverseMapping.
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   213
    revMapping isNil ifTrue:[
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   214
        self initializeMappings.
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   215
        revMapping := self reverseMapping.
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   216
    ].    
4743
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   217
%{
4753
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   218
    // overallocate by 3
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   219
#   define N_QUICKBUFFER 512
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   220
    if (__isString(aString)
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   221
     && __isByteArray(revMapping)) {
4754
95158fac7fde C;tion error with Borland
Stefan Vogel <sv@exept.de>
parents: 4753
diff changeset
   222
        unsigned char *_revMapping = __stringVal(revMapping);
4753
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   223
        int numInChars = __stringSize(aString);
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   224
        char *in = __stringVal(aString);
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   225
        unsigned char quickBuffer[N_QUICKBUFFER+3];
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   226
        unsigned char *buffer = quickBuffer;
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   227
        int bufferSize = N_QUICKBUFFER;
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   228
        int outLen = 0;
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   229
        int charBuffer = 0;
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   230
        int nBitsOut = 0;
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   231
        int i;
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   232
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   233
        for (i=0; i<numInChars; i++) {
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   234
            char ch = in[i];
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   235
            int bits = -1;
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   236
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   237
            if (ch <= 127) {
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   238
                bits = _revMapping[(ch-1) & 0x7F];
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   239
            }    
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   240
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   241
            if ((unsigned)bits <= 0x3F) { 
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   242
                charBuffer = (charBuffer << 6) | bits;
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   243
                nBitsOut += 6;
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   244
                if (nBitsOut == 24) {
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   245
                    if ((outLen + 3) > bufferSize) {
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   246
                        if (buffer == quickBuffer) {
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   247
                            // overallocate by 3
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   248
                            buffer = (unsigned char *)malloc(bufferSize*2+3);
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   249
                            memcpy(buffer, quickBuffer, bufferSize);
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   250
                        } else {
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   251
                            buffer = (unsigned char *)realloc(buffer, bufferSize*2+3);
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   252
                        }
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   253
                        bufferSize = bufferSize * 2;
4743
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   254
                    }
4753
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   255
                    buffer[outLen] = (charBuffer >> 16) & 0xFF;
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   256
                    buffer[outLen+1] = (charBuffer >> 8) & 0xFF;
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   257
                    buffer[outLen+2] = (charBuffer) & 0xFF;
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   258
                    outLen += 3;
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   259
                    charBuffer = nBitsOut = 0;
4743
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   260
                }
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   261
            } else {
4753
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   262
                if ((unsigned)bits == 0x40) {
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   263
                    // end mark
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   264
                    // because of overallocation, there is no need to check for buffer-full condition here
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   265
                    if (nBitsOut == 12) {
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   266
                        // data has been padded to 12, skip 4 bits
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   267
                        // one more byte coming
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   268
                        charBuffer >>= 4;
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   269
                        nBitsOut -= 4;
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   270
                        buffer[outLen] = (charBuffer) & 0xFF;
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   271
                        outLen += 1;
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   272
                    } else if (nBitsOut == 18) {
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   273
                        // data has been padded to 18, skip 2 bits
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   274
                        charBuffer >>= 2;
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   275
                        nBitsOut -= 2;
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   276
                        buffer[outLen] = (charBuffer >> 8) & 0xFF;
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   277
                        buffer[outLen+1] = (charBuffer) & 0xFF;
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   278
                        outLen += 2;
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   279
                    }
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   280
                } else {
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   281
                    // ignore
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   282
                }    
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   283
            }
4743
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   284
        }
4753
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   285
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   286
        if (asStringBoolean == true) {
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   287
            decoding = __MKSTRING_L(buffer, outLen);
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   288
        } else {
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   289
            decoding = __MKBYTEARRAY(buffer, outLen);
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   290
        }
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   291
        if (buffer != quickBuffer) {
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   292
            free(buffer);
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   293
        }
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   294
        RETURN(decoding);
4743
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   295
    }    
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   296
%}.
4753
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   297
    decoding := super decode:aString.
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   298
    asStringBoolean ifTrue:[
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   299
        ^ decoding asString
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   300
    ].    
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   301
    ^ decoding
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   302
    
4743
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   303
    "
4753
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   304
     (Base64Coder encode:'queen%27s%20gambit') => 'cXVlZW4lMjdzJTIwZ2FtYml0'
4743
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   305
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   306
     (Base64Coder decode:'cXVlZW4lMjdzJTIwZ2FtYml0') asString => 'queen%27s%20gambit'
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   307
     (Base64Coder fastDecodeString:'cXVlZW4lMjdzJTIwZ2FtYml0') asString => 'queen%27s%20gambit'
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   308
     (Base64Coder fastDecodeString:'cXVlZW4lMjdzJTIwZ2FtYml0' asString:true) => 'queen%27s%20gambit'
4753
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   309
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   310
     (Base64Coder encode:'a') => 'YQ=='
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   311
     (Base64Coder fastDecodeString:'YQ==' asString:true) => 'a'
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   312
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   313
     (Base64Coder encode:'aa') => 'YWE='
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   314
     (Base64Coder fastDecodeString:'YWE=' asString:true) => 'aa'
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   315
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   316
     |data encoded|
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   317
     data := ByteArray new:100000.
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   318
     encoded := Base64Coder encode:data.
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   319
     Time millisecondsToRun:[
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   320
        10 timesRepeat:[
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   321
            Base64Coder decode:encoded.
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   322
        ]
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   323
     ] 
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   324
     
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   325
     |data encoded|
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   326
     data := ByteArray new:100000.
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   327
     encoded := Base64Coder encode:data.
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   328
     Time millisecondsToRun:[
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   329
        10 timesRepeat:[
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   330
            Base64Coder fastDecodeString:encoded.
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   331
        ]
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   332
     ]
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   333
4743
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   334
    "
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   335
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   336
    "Created: / 30-09-2018 / 14:35:05 / Claus Gittinger"
4753
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   337
    "Modified (comment): / 30-09-2018 / 16:57:52 / Claus Gittinger"
1027
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   338
! !
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   339
1063
144a9e513f3e #emptyWriteStream
Stefan Vogel <sv@exept.de>
parents: 1027
diff changeset
   340
!Base64Coder methodsFor:'encoding'!
144a9e513f3e #emptyWriteStream
Stefan Vogel <sv@exept.de>
parents: 1027
diff changeset
   341
3763
0d73959c77f3 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 3547
diff changeset
   342
nextPutByte:aByte
1027
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   343
    "encode aByte on the output stream"
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   344
4507
e85ee316a1b2 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4279
diff changeset
   345
    |b1 "{ Class: SmallInteger }"
e85ee316a1b2 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4279
diff changeset
   346
     b2 "{ Class: SmallInteger }" 
e85ee316a1b2 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4279
diff changeset
   347
     b3 "{ Class: SmallInteger }"
e85ee316a1b2 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4279
diff changeset
   348
     b4 "{ Class: SmallInteger }" 
e85ee316a1b2 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4279
diff changeset
   349
     bufferedBytes "{ Class: SmallInteger }" |
1027
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   350
3763
0d73959c77f3 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 3547
diff changeset
   351
    buffer := (buffer bitShift:8) bitOr:aByte.
1027
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   352
    bits := bits + 8.
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   353
    bits == 24 ifTrue:[
1389
8eb6ec86c0c7 allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents: 1315
diff changeset
   354
        "RFC 2045 says: max 76 characters in one line"
8eb6ec86c0c7 allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents: 1315
diff changeset
   355
        (lineLimit notNil and:[charCount >= lineLimit]) ifTrue:[
8eb6ec86c0c7 allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents: 1315
diff changeset
   356
            stream cr.
8eb6ec86c0c7 allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents: 1315
diff changeset
   357
            charCount := 0.
8eb6ec86c0c7 allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents: 1315
diff changeset
   358
        ].
4507
e85ee316a1b2 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4279
diff changeset
   359
        bufferedBytes := buffer.
e85ee316a1b2 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4279
diff changeset
   360
        
e85ee316a1b2 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4279
diff changeset
   361
        b4 := bufferedBytes bitAnd:16r3F.
e85ee316a1b2 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4279
diff changeset
   362
        b3 := (bufferedBytes bitShift:-6)  bitAnd:16r3F.
e85ee316a1b2 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4279
diff changeset
   363
        b2 := (bufferedBytes bitShift:-12) bitAnd:16r3F.
e85ee316a1b2 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4279
diff changeset
   364
        b1 := (bufferedBytes bitShift:-18) bitAnd:16r3F.
1027
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   365
        buffer := bits := 0.
4507
e85ee316a1b2 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4279
diff changeset
   366
        
4743
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   367
        stream nextPut:(mapping at:b1+1);
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   368
               nextPut:(mapping at:b2+1);
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   369
               nextPut:(mapping at:b3+1);
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   370
               nextPut:(mapping at:b4+1).
1027
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   371
1389
8eb6ec86c0c7 allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents: 1315
diff changeset
   372
        charCount := charCount + 4.
1027
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   373
    ].
4507
e85ee316a1b2 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4279
diff changeset
   374
e85ee316a1b2 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4279
diff changeset
   375
    "Modified: / 26-08-2017 / 12:35:17 / cg"
4743
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   376
    "Modified: / 30-09-2018 / 15:15:14 / Claus Gittinger"
1027
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   377
! !
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   378
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   379
!Base64Coder methodsFor:'misc'!
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   380
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   381
flush
1390
410841d24a09 formatting
Claus Gittinger <cg@exept.de>
parents: 1389
diff changeset
   382
    "flush the remaining bits of buffer. 
410841d24a09 formatting
Claus Gittinger <cg@exept.de>
parents: 1389
diff changeset
   383
     The number of bits in buffer is not a multiple of 6, so we pad
410841d24a09 formatting
Claus Gittinger <cg@exept.de>
parents: 1389
diff changeset
   384
     the buffer and signal that padding has been done via $= characters."
1027
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   385
4891
d8c52483ab4c #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4754
diff changeset
   386
    |tempBuffer "{Class: SmallInteger}"
d8c52483ab4c #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4754
diff changeset
   387
     b1 b2 b3 b4|
1390
410841d24a09 formatting
Claus Gittinger <cg@exept.de>
parents: 1389
diff changeset
   388
        
410841d24a09 formatting
Claus Gittinger <cg@exept.de>
parents: 1389
diff changeset
   389
    bits == 0 ifTrue:[
1027
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   390
        "buffer is empty, nothing to do"
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   391
        ^ self.
1390
410841d24a09 formatting
Claus Gittinger <cg@exept.de>
parents: 1389
diff changeset
   392
    ].
410841d24a09 formatting
Claus Gittinger <cg@exept.de>
parents: 1389
diff changeset
   393
            
4891
d8c52483ab4c #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4754
diff changeset
   394
    tempBuffer := buffer.
1390
410841d24a09 formatting
Claus Gittinger <cg@exept.de>
parents: 1389
diff changeset
   395
    bits == 8 ifTrue:[
4891
d8c52483ab4c #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4754
diff changeset
   396
        tempBuffer := tempBuffer bitShift:4.
1027
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   397
        b4 := b3 := 64. "pad with '=='"
4891
d8c52483ab4c #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4754
diff changeset
   398
        b1 := (tempBuffer bitShift:-6) bitAnd:16r3F.
d8c52483ab4c #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4754
diff changeset
   399
        b2 := tempBuffer bitAnd:16r3F.
1390
410841d24a09 formatting
Claus Gittinger <cg@exept.de>
parents: 1389
diff changeset
   400
    ] ifFalse:[
410841d24a09 formatting
Claus Gittinger <cg@exept.de>
parents: 1389
diff changeset
   401
        bits = 16 ifTrue:[
4891
d8c52483ab4c #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4754
diff changeset
   402
            tempBuffer := tempBuffer bitShift:2.
1390
410841d24a09 formatting
Claus Gittinger <cg@exept.de>
parents: 1389
diff changeset
   403
            b4 := 64.        "pad with '='"
4891
d8c52483ab4c #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4754
diff changeset
   404
            b3 := tempBuffer bitAnd:16r3F.
d8c52483ab4c #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4754
diff changeset
   405
            b2 := (tempBuffer bitShift:-6)  bitAnd:16r3F.
d8c52483ab4c #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4754
diff changeset
   406
            b1 := (tempBuffer bitShift:-12) bitAnd:16r3F.
1390
410841d24a09 formatting
Claus Gittinger <cg@exept.de>
parents: 1389
diff changeset
   407
        ]
410841d24a09 formatting
Claus Gittinger <cg@exept.de>
parents: 1389
diff changeset
   408
    ].
410841d24a09 formatting
Claus Gittinger <cg@exept.de>
parents: 1389
diff changeset
   409
    bits := buffer := 0.
1027
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   410
1391
530a5924e319 comment
Claus Gittinger <cg@exept.de>
parents: 1390
diff changeset
   411
    "RFC 2045 says: max 76 characters in one line"
530a5924e319 comment
Claus Gittinger <cg@exept.de>
parents: 1390
diff changeset
   412
    (lineLimit notNil and:[charCount >= lineLimit]) ifTrue:[
530a5924e319 comment
Claus Gittinger <cg@exept.de>
parents: 1390
diff changeset
   413
        stream cr.
530a5924e319 comment
Claus Gittinger <cg@exept.de>
parents: 1390
diff changeset
   414
        charCount := 0.
530a5924e319 comment
Claus Gittinger <cg@exept.de>
parents: 1390
diff changeset
   415
    ].
530a5924e319 comment
Claus Gittinger <cg@exept.de>
parents: 1390
diff changeset
   416
4743
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   417
    stream nextPut:(mapping at:b1+1);
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   418
           nextPut:(mapping at:b2+1);
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   419
           nextPut:(mapping at:b3+1);
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   420
           nextPut:(mapping at:b4+1).
1391
530a5924e319 comment
Claus Gittinger <cg@exept.de>
parents: 1390
diff changeset
   421
    charCount := charCount + 4.
4743
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   422
4891
d8c52483ab4c #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4754
diff changeset
   423
    "Modified: / 20-03-2019 / 21:22:39 / Claus Gittinger"
1484
9772f2d67819 Optimize #upToEnd
Stefan Vogel <sv@exept.de>
parents: 1458
diff changeset
   424
! !
9772f2d67819 Optimize #upToEnd
Stefan Vogel <sv@exept.de>
parents: 1458
diff changeset
   425
9772f2d67819 Optimize #upToEnd
Stefan Vogel <sv@exept.de>
parents: 1458
diff changeset
   426
!Base64Coder methodsFor:'private'!
9772f2d67819 Optimize #upToEnd
Stefan Vogel <sv@exept.de>
parents: 1458
diff changeset
   427
9772f2d67819 Optimize #upToEnd
Stefan Vogel <sv@exept.de>
parents: 1458
diff changeset
   428
fillBuffer
4743
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   429
    "fill buffer with next 4 characters each representing 6 bits.
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   430
     Used when decoding."
1484
9772f2d67819 Optimize #upToEnd
Stefan Vogel <sv@exept.de>
parents: 1458
diff changeset
   431
4743
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   432
    |b  
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   433
     tempBuffer "{Class: SmallInteger}"
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   434
     _bits      "{Class: SmallInteger}" |
1484
9772f2d67819 Optimize #upToEnd
Stefan Vogel <sv@exept.de>
parents: 1458
diff changeset
   435
9772f2d67819 Optimize #upToEnd
Stefan Vogel <sv@exept.de>
parents: 1458
diff changeset
   436
    tempBuffer := 0.
4743
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   437
    _bits := 0.
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   438
3210
6e7ac9b78369 [true] whileTrue: -> #loop
Stefan Vogel <sv@exept.de>
parents: 2325
diff changeset
   439
    [
1484
9772f2d67819 Optimize #upToEnd
Stefan Vogel <sv@exept.de>
parents: 1458
diff changeset
   440
        "read next valid Base64 character, skip invalid characters"
2137
2d1af00702fe slightly tuned loop
Claus Gittinger <cg@exept.de>
parents: 2068
diff changeset
   441
        b := 255.
2d1af00702fe slightly tuned loop
Claus Gittinger <cg@exept.de>
parents: 2068
diff changeset
   442
        [b == 255] whileTrue:[
1484
9772f2d67819 Optimize #upToEnd
Stefan Vogel <sv@exept.de>
parents: 1458
diff changeset
   443
            b := stream next.
9772f2d67819 Optimize #upToEnd
Stefan Vogel <sv@exept.de>
parents: 1458
diff changeset
   444
            b isNil ifTrue:[ "end of stream"
1964
671b01812775 New BaseNCoder
Stefan Vogel <sv@exept.de>
parents: 1888
diff changeset
   445
                b := 64.     "simulate end-mark"
1484
9772f2d67819 Optimize #upToEnd
Stefan Vogel <sv@exept.de>
parents: 1458
diff changeset
   446
            ] ifFalse:[
4743
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   447
                b := reverseMapping at:b codePoint ifAbsent:255.
1484
9772f2d67819 Optimize #upToEnd
Stefan Vogel <sv@exept.de>
parents: 1458
diff changeset
   448
            ]
2137
2d1af00702fe slightly tuned loop
Claus Gittinger <cg@exept.de>
parents: 2068
diff changeset
   449
        ].
1964
671b01812775 New BaseNCoder
Stefan Vogel <sv@exept.de>
parents: 1888
diff changeset
   450
1484
9772f2d67819 Optimize #upToEnd
Stefan Vogel <sv@exept.de>
parents: 1458
diff changeset
   451
        b == 64 ifTrue:[
9772f2d67819 Optimize #upToEnd
Stefan Vogel <sv@exept.de>
parents: 1458
diff changeset
   452
            "got $=, end of Base64 string has been reached"
9772f2d67819 Optimize #upToEnd
Stefan Vogel <sv@exept.de>
parents: 1458
diff changeset
   453
            atEnd := true.
4743
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   454
            _bits == 12 ifTrue:[
1484
9772f2d67819 Optimize #upToEnd
Stefan Vogel <sv@exept.de>
parents: 1458
diff changeset
   455
                "data has been padded to 12, skip 4 bits"
4743
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   456
                tempBuffer := tempBuffer bitShift:-4.
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   457
                _bits := _bits - 4.
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   458
            ] ifFalse:[_bits == 18 ifTrue:[
1484
9772f2d67819 Optimize #upToEnd
Stefan Vogel <sv@exept.de>
parents: 1458
diff changeset
   459
                "data has been padded to 18, skip 2 bits"
4743
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   460
                tempBuffer := tempBuffer bitShift:-2.
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   461
                _bits := _bits - 2.
1484
9772f2d67819 Optimize #upToEnd
Stefan Vogel <sv@exept.de>
parents: 1458
diff changeset
   462
            ]].
9772f2d67819 Optimize #upToEnd
Stefan Vogel <sv@exept.de>
parents: 1458
diff changeset
   463
        ] ifFalse:[
9772f2d67819 Optimize #upToEnd
Stefan Vogel <sv@exept.de>
parents: 1458
diff changeset
   464
            "got valid Base64 character, append to buffer"
9772f2d67819 Optimize #upToEnd
Stefan Vogel <sv@exept.de>
parents: 1458
diff changeset
   465
            tempBuffer := (tempBuffer bitShift:6) bitOr:b.
4743
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   466
            _bits := _bits + 6.
1484
9772f2d67819 Optimize #upToEnd
Stefan Vogel <sv@exept.de>
parents: 1458
diff changeset
   467
        ].
4743
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   468
        (_bits == 24 or:[atEnd]) ifTrue:[
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   469
            bits := _bits.
2137
2d1af00702fe slightly tuned loop
Claus Gittinger <cg@exept.de>
parents: 2068
diff changeset
   470
            buffer := tempBuffer.
2d1af00702fe slightly tuned loop
Claus Gittinger <cg@exept.de>
parents: 2068
diff changeset
   471
            ^ self.
2d1af00702fe slightly tuned loop
Claus Gittinger <cg@exept.de>
parents: 2068
diff changeset
   472
        ].
3210
6e7ac9b78369 [true] whileTrue: -> #loop
Stefan Vogel <sv@exept.de>
parents: 2325
diff changeset
   473
    ] loop.
4743
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   474
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   475
    "Modified: / 30-09-2018 / 15:16:19 / Claus Gittinger"
1027
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   476
! !
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   477
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   478
!Base64Coder class methodsFor:'documentation'!
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   479
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   480
version
3763
0d73959c77f3 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 3547
diff changeset
   481
    ^ '$Header$'
2325
fa4a6a5fd3e1 changed: #nextPut:
Stefan Vogel <sv@exept.de>
parents: 2224
diff changeset
   482
!
fa4a6a5fd3e1 changed: #nextPut:
Stefan Vogel <sv@exept.de>
parents: 2224
diff changeset
   483
fa4a6a5fd3e1 changed: #nextPut:
Stefan Vogel <sv@exept.de>
parents: 2224
diff changeset
   484
version_CVS
3763
0d73959c77f3 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 3547
diff changeset
   485
    ^ '$Header$'
1027
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   486
! !
1111
2a64f0fe418a more protocol
Claus Gittinger <cg@exept.de>
parents: 1084
diff changeset
   487