Base64Coder.st
author Claus Gittinger <cg@exept.de>
Fri, 22 Mar 2019 13:45:55 +0100
changeset 4894 206d7a1d926b
parent 4891 d8c52483ab4c
child 4895 301653b248c3
permissions -rw-r--r--
#TUNING by cg class: Base64Coder class added: #decodeAsString: #encode: #fastEncode: #fastEncode:asString: #fastEncode:asString:lineLimit: #lineLimit comment/format in: #documentation #examples #fastDecodeString: changed: #decode: #fastDecodeString:asString:
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
4894
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
    48
    Notice: for URLs, a slightly different encoding is used,      
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
    49
    where instead of plus and slash, minus and underline are generated (see Base64UrlCoder).
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
    50
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
    51
    The main entry point API is:
4279
bb907803ee15 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 3763
diff changeset
    52
        Base64Coder encode:aStringOrBytes
bb907803ee15 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 3763
diff changeset
    53
    and
bb907803ee15 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 3763
diff changeset
    54
        Base64Coder decode:aString
bb907803ee15 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 3763
diff changeset
    55
4894
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
    56
    Typically, binary data is encoded as base64, 
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
    57
    so the natural return value is a byte array.
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
    58
4279
bb907803ee15 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 3763
diff changeset
    59
    If the decoder should return a string, use
bb907803ee15 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 3763
diff changeset
    60
        Base64Coder decodeAsString:aString.
4894
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
    61
    otherwise, a bytearray is returned from the decode: method.
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
    62
    
1027
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    63
    [author:]
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    64
        Stefan Vogel
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    65
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    66
    [see also:]
4743
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
    67
        RFC https://tools.ietf.org/html/rfc4648
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
    68
        
1027
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    69
    [instance variables:]
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    70
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    71
    [class variables:]
4520
d2ea4e714e1f #DOCUMENTATION by mawalch
mawalch
parents: 4507
diff changeset
    72
        Base64Mapping         String   Mapping from bytes (with 6 valid bits)
1027
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    73
                                       to Base64 characters
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    74
        Base64ReverseMapping  Array    Mapping from Base64 characters to 6-bit-Bytes
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    75
"
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    76
!
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    77
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    78
examples
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    79
"
1122
9f6405dc3319 example added
Claus Gittinger <cg@exept.de>
parents: 1111
diff changeset
    80
                                                                [exBegin]
3478
ce272a495f41 class: Base64Coder
Claus Gittinger <cg@exept.de>
parents: 3210
diff changeset
    81
   (Base64Coder encode:'queen%27s%20gambit') asString = 'cXVlZW4lMjdzJTIwZ2FtYml0'
ce272a495f41 class: Base64Coder
Claus Gittinger <cg@exept.de>
parents: 3210
diff changeset
    82
                                                                [exEnd]
ce272a495f41 class: Base64Coder
Claus Gittinger <cg@exept.de>
parents: 3210
diff changeset
    83
ce272a495f41 class: Base64Coder
Claus Gittinger <cg@exept.de>
parents: 3210
diff changeset
    84
                                                                [exBegin]
ce272a495f41 class: Base64Coder
Claus Gittinger <cg@exept.de>
parents: 3210
diff changeset
    85
   (Base64Coder decode:'cXVlZW4lMjdzJTIwZ2FtYml0') asString = 'queen%27s%20gambit'
2224
3f9562af7e35 example
Claus Gittinger <cg@exept.de>
parents: 2137
diff changeset
    86
                                                                [exEnd]
3f9562af7e35 example
Claus Gittinger <cg@exept.de>
parents: 2137
diff changeset
    87
3f9562af7e35 example
Claus Gittinger <cg@exept.de>
parents: 2137
diff changeset
    88
                                                                [exBegin]
1122
9f6405dc3319 example added
Claus Gittinger <cg@exept.de>
parents: 1111
diff changeset
    89
   |data1 text data2|
9f6405dc3319 example added
Claus Gittinger <cg@exept.de>
parents: 1111
diff changeset
    90
9f6405dc3319 example added
Claus Gittinger <cg@exept.de>
parents: 1111
diff changeset
    91
   data1 := #[0 1 16r7F 16r80 16r81 16rFE 16rFF].
1639
dd7c77c44be6 Fix examples
Stefan Vogel <sv@exept.de>
parents: 1484
diff changeset
    92
   text := Base64Coder encode:data1.
dd7c77c44be6 Fix examples
Stefan Vogel <sv@exept.de>
parents: 1484
diff changeset
    93
   data2 := Base64Coder decode:text.
4520
d2ea4e714e1f #DOCUMENTATION by mawalch
mawalch
parents: 4507
diff changeset
    94
   data2
1122
9f6405dc3319 example added
Claus Gittinger <cg@exept.de>
parents: 1111
diff changeset
    95
                                                                [exEnd]
1027
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    96
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    97
                                                                [exBegin]
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    98
   |coder|
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    99
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   100
   coder := Base64Coder on:'' writeStream.
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   101
   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
   102
   coder flush.
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   103
   coder contents inspect.
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   104
   coder reset.
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   105
   coder nextPut:254.
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   106
   coder contents inspect.
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   107
                                                                [exEnd]
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   108
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   109
                                                                [exBegin]
1391
530a5924e319 comment
Claus Gittinger <cg@exept.de>
parents: 1390
diff changeset
   110
   |coder decoder|
1027
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   111
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   112
   coder := Base64Coder on:'' writeStream.
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   113
   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
   114
   coder flush.
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   115
1391
530a5924e319 comment
Claus Gittinger <cg@exept.de>
parents: 1390
diff changeset
   116
   decoder := Base64Coder on:(coder contents readStream).
530a5924e319 comment
Claus Gittinger <cg@exept.de>
parents: 1390
diff changeset
   117
   [decoder atEnd] whileFalse:[
530a5924e319 comment
Claus Gittinger <cg@exept.de>
parents: 1390
diff changeset
   118
      Transcript show:decoder next
1027
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   119
   ].
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   120
   Transcript cr.
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   121
                                                                [exEnd]
1389
8eb6ec86c0c7 allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents: 1315
diff changeset
   122
                                                                [exBegin]
8eb6ec86c0c7 allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents: 1315
diff changeset
   123
   |coder|
8eb6ec86c0c7 allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents: 1315
diff changeset
   124
8eb6ec86c0c7 allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents: 1315
diff changeset
   125
   coder := Base64Coder on:'' writeStream.
8eb6ec86c0c7 allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents: 1315
diff changeset
   126
   coder nextPutAll:(0 to:200) asByteArray.
8eb6ec86c0c7 allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents: 1315
diff changeset
   127
   coder flush.
8eb6ec86c0c7 allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents: 1315
diff changeset
   128
8eb6ec86c0c7 allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents: 1315
diff changeset
   129
   Transcript showCR:(coder contents).
8eb6ec86c0c7 allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents: 1315
diff changeset
   130
                                                                [exEnd]
4743
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   131
                                                                [exBegin]
4894
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   132
   |bytes|
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   133
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   134
   bytes := ByteArray new:100000.
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   135
   Time millisecondsToRun:[    
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   136
       100 timesRepeat:[
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   137
           Base64Coder encode:bytes.
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   138
       ].        
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   139
   ].        
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   140
                                                                [exEnd]
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   141
                                                                [exBegin]
4743
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   142
   |bytes encoded decoded|
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   143
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   144
   bytes := #[0 0 0] copy.
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   145
   0 to:255 do:[:b1 |
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   146
       Transcript showCR:b1.  
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   147
       bytes at:1 put:b1.  
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   148
       0 to:255 do:[:b2 |
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   149
           bytes at:2 put:b2.  
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   150
           0 to:255 do:[:b3 |
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   151
               bytes at:3 put:b3.  
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   152
               encoded := Base64Coder encode:bytes.
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   153
               decoded := Base64Coder decode:encoded.
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   154
               self assert:(decoded = bytes).
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   155
           ]
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   156
       ]
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   157
   ].
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   158
                                                                [exEnd]
1027
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   159
"
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   160
! !
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   161
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   162
!Base64Coder class methodsFor:'initialization'!
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   163
3547
399b59b67fda initialization done lazily (to speed up startup)
Claus Gittinger <cg@exept.de>
parents: 3478
diff changeset
   164
initializeMappings
1027
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   165
    "initialize class variables"
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   166
3547
399b59b67fda initialization done lazily (to speed up startup)
Claus Gittinger <cg@exept.de>
parents: 3478
diff changeset
   167
    Base64Mapping isNil ifTrue:[
399b59b67fda initialization done lazily (to speed up startup)
Claus Gittinger <cg@exept.de>
parents: 3478
diff changeset
   168
        "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
   169
        Base64Mapping := 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='.
4743
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   170
        Base64ReverseMapping := self reverseMappingFor:Base64Mapping.
1027
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   171
    ].
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   172
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   173
    "
4743
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   174
     Base64Mapping := nil.
3547
399b59b67fda initialization done lazily (to speed up startup)
Claus Gittinger <cg@exept.de>
parents: 3478
diff changeset
   175
     self initializeMappings
1027
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   176
    "
4743
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
    "Modified (comment): / 30-09-2018 / 15:39:44 / Claus Gittinger"
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   179
!
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   180
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   181
mapping
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   182
    ^ Base64Mapping
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   183
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   184
    "Created: / 30-09-2018 / 15:30:33 / Claus Gittinger"
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   185
!
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
reverseMapping
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   188
    ^ Base64ReverseMapping
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
    "Created: / 30-09-2018 / 15:30:40 / Claus Gittinger"
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   191
! !
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   192
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   193
!Base64Coder class methodsFor:'decoding'!
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
decode:aStringOrStream
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   196
    "because base64 decoding is used heavily in some protocols,
4894
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   197
     a specially tuned version is provided here
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   198
     for the common case of decoding a string.
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   199
     This returns a byteArray."
4743
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   200
4753
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   201
    aStringOrStream isString ifTrue:[
4894
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   202
        ^ self fastDecodeString:aStringOrStream asString:false
4753
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   203
    ].    
4743
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   204
    ^ super decode:aStringOrStream.
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
    "Created: / 30-09-2018 / 14:14:51 / Claus Gittinger"
4894
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   207
    "Modified: / 21-03-2019 / 22:37:27 / Claus Gittinger"
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   208
!
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   209
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   210
decodeAsString:encodedString
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   211
    "because base64 decoding is used heavily in some protocols,
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   212
     a specially tuned version is provided here
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   213
     for the common case of decoding a string.
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   214
     This returns a string."
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   215
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   216
    encodedString isString ifTrue:[
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   217
        ^ self fastDecodeString:encodedString asString:true
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   218
    ].    
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   219
    ^ super decodeAsString:encodedString.
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   220
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   221
    "Created: / 21-03-2019 / 20:43:47 / Claus Gittinger"
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   222
    "Modified: / 21-03-2019 / 22:10:24 / Claus Gittinger"
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   223
!
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   224
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   225
encode:aStringOrStream
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   226
    "because base64 encoding is used heavily in some protocols,
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   227
     a specially tuned version is provided here 
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   228
     for the common case of encoding a string.
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   229
     A string is generated with an inserted
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   230
     newline after every 76 characters (see RFC 2045)"
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   231
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   232
    (aStringOrStream isString or:[aStringOrStream isByteArray]) ifTrue:[
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   233
        ^ self fastEncode:aStringOrStream asString:true lineLimit:(self lineLimit)
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   234
    ].    
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   235
    ^ super encode:aStringOrStream.
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   236
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   237
    "Created: / 21-03-2019 / 20:44:35 / Claus Gittinger"
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   238
    "Modified: / 21-03-2019 / 22:33:37 / Claus Gittinger"
4743
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   239
!
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   240
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   241
fastDecodeString:aString
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   242
    "because base64 decoding is used heavily in some protocols,
4894
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   243
     a specially tuned version is provided here
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   244
     for the common case of decoding a string.
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   245
     This returns a byteArray"
4743
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   246
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   247
    ^ self fastDecodeString:aString asString:false
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   248
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   249
    "
4894
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   250
     (Base64Coder encode:'queen%27s%20gambit') => 'cXVlZW4lMjdzJTIwZ2FtYml0'
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   251
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   252
     (Base64Coder decode:'cXVlZW4lMjdzJTIwZ2FtYml0') => #[113 117 101 101 110 37 50 55 115 37 50 48 103 97 109 98 105 116]
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   253
     (Base64Coder decode:'cXVlZW4lMjdzJTIwZ2FtYml0') asString => 'queen%27s%20gambit'
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   254
     (Base64Coder decodeAsString:'cXVlZW4lMjdzJTIwZ2FtYml0') => 'queen%27s%20gambit' 
4743
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   255
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   256
     (Base64Coder fastDecodeString:'cXVlZW4lMjdzJTIwZ2FtYml0') asString => 'queen%27s%20gambit'
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   257
    "
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   258
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   259
    "Created: / 30-09-2018 / 14:36:58 / Claus Gittinger"
4894
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   260
    "Modified (comment): / 21-03-2019 / 22:12:07 / Claus Gittinger"
4743
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   261
!
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   262
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   263
fastDecodeString:aString asString:asStringBoolean
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   264
    "because base64 decoding is used heavily in some protocols,
4894
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   265
     a specially tuned version is provided here 
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   266
     for the common case of decoding a string.
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   267
     If the argument is true, a string is returned;
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   268
     otherwise, a bytearray"
4743
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   269
4753
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   270
    |decoding revMapping|
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   271
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   272
    revMapping := self reverseMapping.
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   273
    revMapping isNil ifTrue:[
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   274
        self initializeMappings.
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   275
        revMapping := self reverseMapping.
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   276
    ].    
4743
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   277
%{
4753
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   278
    // overallocate by 3
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   279
#   define N_QUICKBUFFER 512
4894
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   280
    if (__isStringLike(aString)
4753
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   281
     && __isByteArray(revMapping)) {
4754
95158fac7fde C;tion error with Borland
Stefan Vogel <sv@exept.de>
parents: 4753
diff changeset
   282
        unsigned char *_revMapping = __stringVal(revMapping);
4753
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   283
        int numInChars = __stringSize(aString);
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   284
        char *in = __stringVal(aString);
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   285
        unsigned char quickBuffer[N_QUICKBUFFER+3];
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   286
        unsigned char *buffer = quickBuffer;
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   287
        int bufferSize = N_QUICKBUFFER;
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   288
        int outLen = 0;
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   289
        int charBuffer = 0;
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   290
        int nBitsOut = 0;
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   291
        int i;
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   292
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   293
        for (i=0; i<numInChars; i++) {
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   294
            char ch = in[i];
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   295
            int bits = -1;
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   296
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   297
            if (ch <= 127) {
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   298
                bits = _revMapping[(ch-1) & 0x7F];
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   299
            }    
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
            if ((unsigned)bits <= 0x3F) { 
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   302
                charBuffer = (charBuffer << 6) | bits;
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   303
                nBitsOut += 6;
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   304
                if (nBitsOut == 24) {
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   305
                    if ((outLen + 3) > bufferSize) {
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   306
                        if (buffer == quickBuffer) {
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   307
                            // overallocate by 3
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   308
                            buffer = (unsigned char *)malloc(bufferSize*2+3);
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   309
                            memcpy(buffer, quickBuffer, bufferSize);
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   310
                        } else {
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   311
                            buffer = (unsigned char *)realloc(buffer, bufferSize*2+3);
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
                        bufferSize = bufferSize * 2;
4743
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   314
                    }
4753
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   315
                    buffer[outLen] = (charBuffer >> 16) & 0xFF;
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   316
                    buffer[outLen+1] = (charBuffer >> 8) & 0xFF;
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   317
                    buffer[outLen+2] = (charBuffer) & 0xFF;
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   318
                    outLen += 3;
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   319
                    charBuffer = nBitsOut = 0;
4743
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   320
                }
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   321
            } else {
4753
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   322
                if ((unsigned)bits == 0x40) {
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   323
                    // end mark
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   324
                    // 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
   325
                    if (nBitsOut == 12) {
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   326
                        // data has been padded to 12, skip 4 bits
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   327
                        // one more byte coming
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   328
                        charBuffer >>= 4;
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   329
                        nBitsOut -= 4;
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   330
                        buffer[outLen] = (charBuffer) & 0xFF;
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   331
                        outLen += 1;
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   332
                    } else if (nBitsOut == 18) {
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   333
                        // data has been padded to 18, skip 2 bits
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   334
                        charBuffer >>= 2;
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   335
                        nBitsOut -= 2;
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   336
                        buffer[outLen] = (charBuffer >> 8) & 0xFF;
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   337
                        buffer[outLen+1] = (charBuffer) & 0xFF;
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   338
                        outLen += 2;
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   339
                    }
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   340
                } else {
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   341
                    // ignore
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   342
                }    
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   343
            }
4743
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   344
        }
4753
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   345
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   346
        if (asStringBoolean == true) {
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   347
            decoding = __MKSTRING_L(buffer, outLen);
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   348
        } else {
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   349
            decoding = __MKBYTEARRAY(buffer, outLen);
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   350
        }
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   351
        if (buffer != quickBuffer) {
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   352
            free(buffer);
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   353
        }
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   354
        RETURN(decoding);
4743
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   355
    }    
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   356
%}.
4753
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   357
    decoding := super decode:aString.
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   358
    asStringBoolean ifTrue:[
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   359
        ^ decoding asString
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   360
    ].    
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   361
    ^ decoding
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   362
    
4743
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   363
    "
4753
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   364
     (Base64Coder encode:'queen%27s%20gambit') => 'cXVlZW4lMjdzJTIwZ2FtYml0'
4743
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   365
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   366
     (Base64Coder decode:'cXVlZW4lMjdzJTIwZ2FtYml0') asString => 'queen%27s%20gambit'
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   367
     (Base64Coder fastDecodeString:'cXVlZW4lMjdzJTIwZ2FtYml0') asString => 'queen%27s%20gambit'
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   368
     (Base64Coder fastDecodeString:'cXVlZW4lMjdzJTIwZ2FtYml0' asString:true) => 'queen%27s%20gambit'
4753
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   369
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   370
     (Base64Coder encode:'a') => 'YQ=='
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   371
     (Base64Coder fastDecodeString:'YQ==' asString:true) => 'a'
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   372
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   373
     (Base64Coder encode:'aa') => 'YWE='
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   374
     (Base64Coder fastDecodeString:'YWE=' asString:true) => 'aa'
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   375
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   376
     |data encoded|
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   377
     data := ByteArray new:100000.
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   378
     encoded := Base64Coder encode:data.
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   379
     Time millisecondsToRun:[
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   380
        10 timesRepeat:[
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   381
            Base64Coder decode:encoded.
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   382
        ]
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   383
     ] 
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   384
     
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   385
     |data encoded|
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   386
     data := ByteArray new:100000.
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   387
     encoded := Base64Coder encode:data.
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   388
     Time millisecondsToRun:[
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   389
        10 timesRepeat:[
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   390
            Base64Coder fastDecodeString:encoded.
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   391
        ]
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   392
     ]
5b849ae3a1a1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4743
diff changeset
   393
4743
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   394
    "
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   395
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   396
    "Created: / 30-09-2018 / 14:35:05 / Claus Gittinger"
4894
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   397
    "Modified: / 21-03-2019 / 22:34:49 / Claus Gittinger"
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   398
!
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   399
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   400
fastEncode:aStringOrByteArray 
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   401
    "because base64 encoding is used heavily in some protocols,
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   402
     a specially tuned version is provided here
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   403
     for the common case of encoding a string or bytearray.
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   404
     A string is generated with an inserted
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   405
     newline after every 76 characters (see RFC 2045)"
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   406
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   407
    ^ self fastEncode:aStringOrByteArray asString:true lineLimit:(self lineLimit)
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   408
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   409
    "Created: / 21-03-2019 / 20:43:07 / Claus Gittinger"
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   410
    "Modified (comment): / 21-03-2019 / 22:14:45 / Claus Gittinger"
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   411
!
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   412
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   413
fastEncode:aStringOrByteArray asString:asStringBoolean
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   414
    "because base64 encoding is used heavily in some protocols,
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   415
     a specially tuned version is provided here,
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   416
     for the common case of encoding a string or bytearray.
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   417
     If asStringBoolean is true, a string is generated; otherwise, a bytearray is returned.
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   418
     A newline is inserted after every 76 characters (see RFC 2045)"
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   419
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   420
    ^ self fastEncode:aStringOrByteArray asString:asStringBoolean lineLimit:(self lineLimit)
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   421
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   422
    "Created: / 01-10-2018 / 09:19:35 / Claus Gittinger"
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   423
    "Modified (comment): / 21-03-2019 / 22:14:17 / Claus Gittinger"
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   424
!
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   425
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   426
fastEncode:aStringOrByteArray asString:asStringBoolean lineLimit:lineLimitOrNil
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   427
    "because base64 encoding is used heavily in some protocols,
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   428
     a specially tuned version is provided here 
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   429
     for the common case of encoding a string.
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   430
     If asStringBoolean is true, a string is generated; otherwise, a bytearray is returned.
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   431
     If lineLimitOrNil is non-nil, a newline is inserted after every such number of characters"
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   432
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   433
    |encoding mapping|
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   434
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   435
    mapping := self mapping.
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   436
    mapping isNil ifTrue:[
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   437
        self initializeMappings.
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   438
        mapping := self mapping.
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   439
    ].    
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   440
%{
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   441
    // overallocate by 5
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   442
#   define N_QUICKBUFFER 512
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   443
    int argIsString = __isStringLike(aStringOrByteArray);
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   444
    
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   445
    if ((argIsString || __isByteArray(aStringOrByteArray))
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   446
     && __isStringLike(mapping)) {
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   447
        unsigned char *_mapping = __stringVal(mapping);;
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   448
        int numInChars;
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   449
        unsigned char *in;
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   450
        unsigned char quickBuffer[N_QUICKBUFFER+5];
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   451
        unsigned char *buffer = quickBuffer;
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   452
        int bufferSize = N_QUICKBUFFER;
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   453
        int outLen = 0;
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   454
        int nBitsOut = 0;
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   455
        int i;
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   456
        int numInCharsMinus3;
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   457
        unsigned int lineLimit = ~0;
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   458
        int lineLength = 0;
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   459
        int restLength = 0;
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   460
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   461
        if (__isSmallInteger(lineLimitOrNil)) {
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   462
            lineLimit = __intVal(lineLimitOrNil);
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   463
        } 
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   464
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   465
        if (argIsString) {
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   466
            numInChars = __stringSize(aStringOrByteArray);
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   467
            in = __stringVal(aStringOrByteArray);
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   468
        } else {
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   469
            numInChars = __byteArraySize(aStringOrByteArray);
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   470
            in = __byteArrayVal(aStringOrByteArray);
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   471
        }
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   472
        // fprintf(stderr, "%d\n", numInChars);
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   473
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   474
        lineLength = 0;
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   475
        numInCharsMinus3 = numInChars-3;
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   476
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   477
        for (i=0; i<=numInCharsMinus3; i+=3) {
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   478
            int charBuffer;
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   479
            
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   480
            if (lineLength >= lineLimit) {
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   481
                buffer[outLen++] = '\n';
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   482
                lineLength = 0;
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   483
            }
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   484
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   485
            charBuffer = (in[i]) << 16;    
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   486
            charBuffer |= ((in[i+1]) << 8);    
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   487
            charBuffer |= (in[i+2]);
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   488
            
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   489
            if ((outLen + 5) > bufferSize) {
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   490
                if (buffer == quickBuffer) {
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   491
                    // overallocate by 5
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   492
                    buffer = (unsigned char *)malloc(bufferSize*2+5);
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   493
                    memcpy(buffer, quickBuffer, bufferSize);
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   494
                } else {
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   495
                    buffer = (unsigned char *)realloc(buffer, bufferSize*2+5);
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   496
                }
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   497
                bufferSize = bufferSize * 2;
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   498
            }
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   499
#ifdef __LSBFIRST__
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   500
            {
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   501
                unsigned int out = _mapping[(charBuffer >> 18) & 0x3F];
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   502
                out |= (_mapping[(charBuffer >> 12) & 0x3F]) << 8;
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   503
                out |= (_mapping[(charBuffer >> 6) & 0x3F]) << 16;
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   504
                out |= (_mapping[(charBuffer) & 0x3F]) << 24;
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   505
                ((unsigned int*)(&buffer[outLen]))[0] = out;
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   506
            }
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   507
#else
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   508
            buffer[outLen] = _mapping[(charBuffer >> 18) & 0x3F];
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   509
            buffer[outLen+1] = _mapping[(charBuffer >> 12) & 0x3F];
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   510
            buffer[outLen+2] = _mapping[(charBuffer >> 6) & 0x3F];
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   511
            buffer[outLen+3] = _mapping[(charBuffer) & 0x3F];
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   512
#endif
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   513
            outLen += 4;
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   514
            lineLength += 4;
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   515
        }
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   516
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   517
        restLength = numInChars-i;
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   518
        // fprintf(stderr, "rest: %d\n", restLength);
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   519
        if (restLength) {
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   520
            if (lineLength >= lineLimit) {
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   521
                buffer[outLen++] = '\n';
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   522
                lineLength = 0;
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   523
            }
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   524
            if (restLength == 1) {
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   525
                unsigned int charBuffer;
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   526
                
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   527
                // pad with '=='
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   528
                charBuffer = (in[i]) << 4;    
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   529
                buffer[outLen] = _mapping[(charBuffer >> 6) & 0x3F];
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   530
                buffer[outLen+1] = _mapping[(charBuffer) & 0x3F];
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   531
                buffer[outLen+2] = _mapping[64];
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   532
                buffer[outLen+3] = _mapping[64];
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   533
                outLen += 4;
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   534
            } else {
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   535
                unsigned int charBuffer;
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   536
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   537
                // restLength == 2
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   538
                // pad with '='
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   539
                charBuffer = (in[i]) << 8;    
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   540
                charBuffer |= (in[i+1]); 
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   541
                charBuffer <<= 2;
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   542
                buffer[outLen] = _mapping[(charBuffer >> 12) & 0x3F];
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   543
                buffer[outLen+1] = _mapping[(charBuffer >> 6) & 0x3F];
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   544
                buffer[outLen+2] = _mapping[(charBuffer) & 0x3F];
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   545
                buffer[outLen+3] = _mapping[64];
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   546
                outLen += 4;
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   547
            }    
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   548
        }
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   549
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   550
        if (asStringBoolean == true) {
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   551
            encoding = __MKSTRING_L(buffer, outLen);
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   552
        } else {
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   553
            encoding = __MKBYTEARRAY(buffer, outLen);
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   554
        }
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   555
        if (buffer != quickBuffer) {
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   556
            free(buffer);
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   557
        }
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   558
        RETURN(encoding);
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   559
    }    
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   560
%}.
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   561
    encoding := super encode:aStringOrByteArray with:lineLimitOrNil.
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   562
    asStringBoolean ifTrue:[
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   563
        ^ encoding asString
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   564
    ].    
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   565
    ^ encoding
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   566
    
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   567
    "
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   568
     (Base64Coder encode:'queen%27s%20gambit') => 'cXVlZW4lMjdzJTIwZ2FtYml0'
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   569
     (Base64Coder fastEncode:'queen%27s%20gambit' asString:true) => 'cXVlZW4lMjdzJTIwZ2FtYml0'
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   570
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   571
     (Base64Coder decode:'cXVlZW4lMjdzJTIwZ2FtYml0') asString => 'queen%27s%20gambit'
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   572
     (Base64Coder fastDecodeString:'cXVlZW4lMjdzJTIwZ2FtYml0') asString => 'queen%27s%20gambit'
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   573
     (Base64Coder fastDecodeString:'cXVlZW4lMjdzJTIwZ2FtYml0' asString:true) => 'queen%27s%20gambit'
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   574
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   575
     (Base64Coder encode:'a') => 'YQ=='
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   576
     (Base64Coder fastEncode:'a' asString:true) => 'YQ=='
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   577
     (Base64Coder fastDecodeString:'YQ==' asString:true) => 'a'
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   578
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   579
     (Base64Coder encode:'aa') => 'YWE='
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   580
     (Base64Coder fastEncode:'aa' asString:true) => 'YWE='
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   581
     (Base64Coder fastDecodeString:'YWE=' asString:true) => 'aa'
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   582
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   583
     |data|
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   584
     data := ByteArray new:1000.
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   585
     Time millisecondsToRun:[
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   586
        10000 timesRepeat:[self halt.
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   587
            Base64Coder encode:data.
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   588
        ]
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   589
     ]
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   590
     
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   591
     |data|
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   592
     data := ByteArray new:1000.
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   593
     Base64Coder fastEncode:data asString:true lineLimit:20.
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   594
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   595
     |data|
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   596
     data := ByteArray new:1000.
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   597
     Base64Coder fastEncode:data asString:true lineLimit:nil.
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   598
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   599
     |data|
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   600
     data := ByteArray new:1000.
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   601
     Time millisecondsToRun:[
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   602
        10000 timesRepeat:[
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   603
            Base64Coder fastEncode:data.
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   604
        ]
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   605
     ]
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   606
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   607
     self assert:((Base64Coder fastEncode:'abc' asString:true)
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   608
                  = 'abc' base64Encoded).
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   609
     self assert:((Base64Coder fastEncode:'a' asString:true)
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   610
                  = 'a' base64Encoded).
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   611
     self assert:((Base64Coder fastEncode:'ab' asString:true)
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   612
                  = 'ab' base64Encoded).
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   613
     self assert:((Base64Coder fastEncode:'abcd' asString:true)
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   614
                  = 'abcd' base64Encoded).
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   615
     self assert:((Base64Coder fastEncode:'abcde' asString:true)
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   616
                  = 'abcde' base64Encoded).
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   617
     self assert:((Base64Coder fastEncode:'abcdef' asString:true)
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   618
                  = 'abcdef' base64Encoded).
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   619
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   620
     self assert:((Base64Coder fastEncode:#'parseMethod:onError:rememberNodes:nodeGenerationCallback:' asString:true)
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   621
                  = #'parseMethod:onError:rememberNodes:nodeGenerationCallback:' base64Encoded).
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   622
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   623
     self assert:((Base64Coder fastEncode:'_INVOKESTATIC_R:' asString:true)
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   624
                  = '_INVOKESTATIC_R:' base64Encoded).
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   625
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   626
     self assert:((Base64Coder fastEncode:#'_INVOKESTATIC_R:' asString:true)
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   627
                  = #'_INVOKESTATIC_R:' base64Encoded) 
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   628
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   629
     self assert:((Base64Coder fastEncode:'_INVOKESTATIC_R:' asString:true)
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   630
                  = (Base64Coder fastEncode:#'_INVOKESTATIC_R:' asString:true)).
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   631
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   632
     self assert:((#'_INVOKESTATIC_R:' base64Encoded)
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   633
                  = ('_INVOKESTATIC_R:' base64Encoded)).
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   634
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   635
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   636
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   637
     self assert:((Base64Coder fastEncode:'_INVOKESTATIC_R:_:' asString:true)
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   638
                  = '_INVOKESTATIC_R:_:' base64Encoded).
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   639
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   640
     self assert:((Base64Coder fastEncode:#'_INVOKESTATIC_R:_:' asString:true)
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   641
                  = #'_INVOKESTATIC_R:_:' base64Encoded) 
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   642
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   643
     self assert:((Base64Coder fastEncode:'_INVOKESTATIC_R:_:' asString:true)
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   644
                  = (Base64Coder fastEncode:#'_INVOKESTATIC_R:_:' asString:true)).
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   645
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   646
     self assert:((#'_INVOKESTATIC_R:_:' base64Encoded)
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   647
                  = ('_INVOKESTATIC_R:_:' base64Encoded)).
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   648
                  
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   649
     Symbol allInstancesDo:[:each |
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   650
         self assert:((Base64Coder fastEncode:each asString:true)
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   651
                     =  (Base64Coder encode:each with:nil)) 
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   652
     ]                
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   653
    "
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   654
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   655
    "Created: / 21-03-2019 / 21:58:59 / Claus Gittinger"
206d7a1d926b #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4891
diff changeset
   656
    "Modified (comment): / 21-03-2019 / 23:11:50 / Claus Gittinger"
1027
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   657
! !
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   658
1063
144a9e513f3e #emptyWriteStream
Stefan Vogel <sv@exept.de>
parents: 1027
diff changeset
   659
!Base64Coder methodsFor:'encoding'!
144a9e513f3e #emptyWriteStream
Stefan Vogel <sv@exept.de>
parents: 1027
diff changeset
   660
3763
0d73959c77f3 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 3547
diff changeset
   661
nextPutByte:aByte
1027
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   662
    "encode aByte on the output stream"
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   663
4507
e85ee316a1b2 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4279
diff changeset
   664
    |b1 "{ Class: SmallInteger }"
e85ee316a1b2 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4279
diff changeset
   665
     b2 "{ Class: SmallInteger }" 
e85ee316a1b2 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4279
diff changeset
   666
     b3 "{ Class: SmallInteger }"
e85ee316a1b2 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4279
diff changeset
   667
     b4 "{ Class: SmallInteger }" 
e85ee316a1b2 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4279
diff changeset
   668
     bufferedBytes "{ Class: SmallInteger }" |
1027
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   669
3763
0d73959c77f3 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 3547
diff changeset
   670
    buffer := (buffer bitShift:8) bitOr:aByte.
1027
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   671
    bits := bits + 8.
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   672
    bits == 24 ifTrue:[
1389
8eb6ec86c0c7 allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents: 1315
diff changeset
   673
        "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
   674
        (lineLimit notNil and:[charCount >= lineLimit]) ifTrue:[
8eb6ec86c0c7 allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents: 1315
diff changeset
   675
            stream cr.
8eb6ec86c0c7 allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents: 1315
diff changeset
   676
            charCount := 0.
8eb6ec86c0c7 allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents: 1315
diff changeset
   677
        ].
4507
e85ee316a1b2 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4279
diff changeset
   678
        bufferedBytes := buffer.
e85ee316a1b2 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4279
diff changeset
   679
        
e85ee316a1b2 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4279
diff changeset
   680
        b4 := bufferedBytes bitAnd:16r3F.
e85ee316a1b2 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4279
diff changeset
   681
        b3 := (bufferedBytes bitShift:-6)  bitAnd:16r3F.
e85ee316a1b2 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4279
diff changeset
   682
        b2 := (bufferedBytes bitShift:-12) bitAnd:16r3F.
e85ee316a1b2 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4279
diff changeset
   683
        b1 := (bufferedBytes bitShift:-18) bitAnd:16r3F.
1027
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   684
        buffer := bits := 0.
4507
e85ee316a1b2 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4279
diff changeset
   685
        
4743
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   686
        stream nextPut:(mapping at:b1+1);
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   687
               nextPut:(mapping at:b2+1);
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   688
               nextPut:(mapping at:b3+1);
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   689
               nextPut:(mapping at:b4+1).
1027
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   690
1389
8eb6ec86c0c7 allow for lineLimit (see RFC2045) to be adjusted
Claus Gittinger <cg@exept.de>
parents: 1315
diff changeset
   691
        charCount := charCount + 4.
1027
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   692
    ].
4507
e85ee316a1b2 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4279
diff changeset
   693
e85ee316a1b2 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4279
diff changeset
   694
    "Modified: / 26-08-2017 / 12:35:17 / cg"
4743
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   695
    "Modified: / 30-09-2018 / 15:15:14 / Claus Gittinger"
1027
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   696
! !
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   697
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   698
!Base64Coder methodsFor:'misc'!
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   699
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   700
flush
1390
410841d24a09 formatting
Claus Gittinger <cg@exept.de>
parents: 1389
diff changeset
   701
    "flush the remaining bits of buffer. 
410841d24a09 formatting
Claus Gittinger <cg@exept.de>
parents: 1389
diff changeset
   702
     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
   703
     the buffer and signal that padding has been done via $= characters."
1027
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   704
4891
d8c52483ab4c #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4754
diff changeset
   705
    |tempBuffer "{Class: SmallInteger}"
d8c52483ab4c #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4754
diff changeset
   706
     b1 b2 b3 b4|
1390
410841d24a09 formatting
Claus Gittinger <cg@exept.de>
parents: 1389
diff changeset
   707
        
410841d24a09 formatting
Claus Gittinger <cg@exept.de>
parents: 1389
diff changeset
   708
    bits == 0 ifTrue:[
1027
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   709
        "buffer is empty, nothing to do"
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   710
        ^ self.
1390
410841d24a09 formatting
Claus Gittinger <cg@exept.de>
parents: 1389
diff changeset
   711
    ].
410841d24a09 formatting
Claus Gittinger <cg@exept.de>
parents: 1389
diff changeset
   712
            
4891
d8c52483ab4c #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4754
diff changeset
   713
    tempBuffer := buffer.
1390
410841d24a09 formatting
Claus Gittinger <cg@exept.de>
parents: 1389
diff changeset
   714
    bits == 8 ifTrue:[
4891
d8c52483ab4c #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4754
diff changeset
   715
        tempBuffer := tempBuffer bitShift:4.
1027
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   716
        b4 := b3 := 64. "pad with '=='"
4891
d8c52483ab4c #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4754
diff changeset
   717
        b1 := (tempBuffer bitShift:-6) bitAnd:16r3F.
d8c52483ab4c #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4754
diff changeset
   718
        b2 := tempBuffer bitAnd:16r3F.
1390
410841d24a09 formatting
Claus Gittinger <cg@exept.de>
parents: 1389
diff changeset
   719
    ] ifFalse:[
410841d24a09 formatting
Claus Gittinger <cg@exept.de>
parents: 1389
diff changeset
   720
        bits = 16 ifTrue:[
4891
d8c52483ab4c #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4754
diff changeset
   721
            tempBuffer := tempBuffer bitShift:2.
1390
410841d24a09 formatting
Claus Gittinger <cg@exept.de>
parents: 1389
diff changeset
   722
            b4 := 64.        "pad with '='"
4891
d8c52483ab4c #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4754
diff changeset
   723
            b3 := tempBuffer bitAnd:16r3F.
d8c52483ab4c #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4754
diff changeset
   724
            b2 := (tempBuffer bitShift:-6)  bitAnd:16r3F.
d8c52483ab4c #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4754
diff changeset
   725
            b1 := (tempBuffer bitShift:-12) bitAnd:16r3F.
1390
410841d24a09 formatting
Claus Gittinger <cg@exept.de>
parents: 1389
diff changeset
   726
        ]
410841d24a09 formatting
Claus Gittinger <cg@exept.de>
parents: 1389
diff changeset
   727
    ].
410841d24a09 formatting
Claus Gittinger <cg@exept.de>
parents: 1389
diff changeset
   728
    bits := buffer := 0.
1027
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   729
1391
530a5924e319 comment
Claus Gittinger <cg@exept.de>
parents: 1390
diff changeset
   730
    "RFC 2045 says: max 76 characters in one line"
530a5924e319 comment
Claus Gittinger <cg@exept.de>
parents: 1390
diff changeset
   731
    (lineLimit notNil and:[charCount >= lineLimit]) ifTrue:[
530a5924e319 comment
Claus Gittinger <cg@exept.de>
parents: 1390
diff changeset
   732
        stream cr.
530a5924e319 comment
Claus Gittinger <cg@exept.de>
parents: 1390
diff changeset
   733
        charCount := 0.
530a5924e319 comment
Claus Gittinger <cg@exept.de>
parents: 1390
diff changeset
   734
    ].
530a5924e319 comment
Claus Gittinger <cg@exept.de>
parents: 1390
diff changeset
   735
4743
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   736
    stream nextPut:(mapping at:b1+1);
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   737
           nextPut:(mapping at:b2+1);
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   738
           nextPut:(mapping at:b3+1);
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   739
           nextPut:(mapping at:b4+1).
1391
530a5924e319 comment
Claus Gittinger <cg@exept.de>
parents: 1390
diff changeset
   740
    charCount := charCount + 4.
4743
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   741
4891
d8c52483ab4c #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4754
diff changeset
   742
    "Modified: / 20-03-2019 / 21:22:39 / Claus Gittinger"
1484
9772f2d67819 Optimize #upToEnd
Stefan Vogel <sv@exept.de>
parents: 1458
diff changeset
   743
! !
9772f2d67819 Optimize #upToEnd
Stefan Vogel <sv@exept.de>
parents: 1458
diff changeset
   744
9772f2d67819 Optimize #upToEnd
Stefan Vogel <sv@exept.de>
parents: 1458
diff changeset
   745
!Base64Coder methodsFor:'private'!
9772f2d67819 Optimize #upToEnd
Stefan Vogel <sv@exept.de>
parents: 1458
diff changeset
   746
9772f2d67819 Optimize #upToEnd
Stefan Vogel <sv@exept.de>
parents: 1458
diff changeset
   747
fillBuffer
4743
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   748
    "fill buffer with next 4 characters each representing 6 bits.
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   749
     Used when decoding."
1484
9772f2d67819 Optimize #upToEnd
Stefan Vogel <sv@exept.de>
parents: 1458
diff changeset
   750
4743
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   751
    |b  
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   752
     tempBuffer "{Class: SmallInteger}"
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   753
     _bits      "{Class: SmallInteger}" |
1484
9772f2d67819 Optimize #upToEnd
Stefan Vogel <sv@exept.de>
parents: 1458
diff changeset
   754
9772f2d67819 Optimize #upToEnd
Stefan Vogel <sv@exept.de>
parents: 1458
diff changeset
   755
    tempBuffer := 0.
4743
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   756
    _bits := 0.
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   757
3210
6e7ac9b78369 [true] whileTrue: -> #loop
Stefan Vogel <sv@exept.de>
parents: 2325
diff changeset
   758
    [
1484
9772f2d67819 Optimize #upToEnd
Stefan Vogel <sv@exept.de>
parents: 1458
diff changeset
   759
        "read next valid Base64 character, skip invalid characters"
2137
2d1af00702fe slightly tuned loop
Claus Gittinger <cg@exept.de>
parents: 2068
diff changeset
   760
        b := 255.
2d1af00702fe slightly tuned loop
Claus Gittinger <cg@exept.de>
parents: 2068
diff changeset
   761
        [b == 255] whileTrue:[
1484
9772f2d67819 Optimize #upToEnd
Stefan Vogel <sv@exept.de>
parents: 1458
diff changeset
   762
            b := stream next.
9772f2d67819 Optimize #upToEnd
Stefan Vogel <sv@exept.de>
parents: 1458
diff changeset
   763
            b isNil ifTrue:[ "end of stream"
1964
671b01812775 New BaseNCoder
Stefan Vogel <sv@exept.de>
parents: 1888
diff changeset
   764
                b := 64.     "simulate end-mark"
1484
9772f2d67819 Optimize #upToEnd
Stefan Vogel <sv@exept.de>
parents: 1458
diff changeset
   765
            ] ifFalse:[
4743
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   766
                b := reverseMapping at:b codePoint ifAbsent:255.
1484
9772f2d67819 Optimize #upToEnd
Stefan Vogel <sv@exept.de>
parents: 1458
diff changeset
   767
            ]
2137
2d1af00702fe slightly tuned loop
Claus Gittinger <cg@exept.de>
parents: 2068
diff changeset
   768
        ].
1964
671b01812775 New BaseNCoder
Stefan Vogel <sv@exept.de>
parents: 1888
diff changeset
   769
1484
9772f2d67819 Optimize #upToEnd
Stefan Vogel <sv@exept.de>
parents: 1458
diff changeset
   770
        b == 64 ifTrue:[
9772f2d67819 Optimize #upToEnd
Stefan Vogel <sv@exept.de>
parents: 1458
diff changeset
   771
            "got $=, end of Base64 string has been reached"
9772f2d67819 Optimize #upToEnd
Stefan Vogel <sv@exept.de>
parents: 1458
diff changeset
   772
            atEnd := true.
4743
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   773
            _bits == 12 ifTrue:[
1484
9772f2d67819 Optimize #upToEnd
Stefan Vogel <sv@exept.de>
parents: 1458
diff changeset
   774
                "data has been padded to 12, skip 4 bits"
4743
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   775
                tempBuffer := tempBuffer bitShift:-4.
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   776
                _bits := _bits - 4.
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   777
            ] ifFalse:[_bits == 18 ifTrue:[
1484
9772f2d67819 Optimize #upToEnd
Stefan Vogel <sv@exept.de>
parents: 1458
diff changeset
   778
                "data has been padded to 18, skip 2 bits"
4743
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   779
                tempBuffer := tempBuffer bitShift:-2.
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   780
                _bits := _bits - 2.
1484
9772f2d67819 Optimize #upToEnd
Stefan Vogel <sv@exept.de>
parents: 1458
diff changeset
   781
            ]].
9772f2d67819 Optimize #upToEnd
Stefan Vogel <sv@exept.de>
parents: 1458
diff changeset
   782
        ] ifFalse:[
9772f2d67819 Optimize #upToEnd
Stefan Vogel <sv@exept.de>
parents: 1458
diff changeset
   783
            "got valid Base64 character, append to buffer"
9772f2d67819 Optimize #upToEnd
Stefan Vogel <sv@exept.de>
parents: 1458
diff changeset
   784
            tempBuffer := (tempBuffer bitShift:6) bitOr:b.
4743
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   785
            _bits := _bits + 6.
1484
9772f2d67819 Optimize #upToEnd
Stefan Vogel <sv@exept.de>
parents: 1458
diff changeset
   786
        ].
4743
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   787
        (_bits == 24 or:[atEnd]) ifTrue:[
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   788
            bits := _bits.
2137
2d1af00702fe slightly tuned loop
Claus Gittinger <cg@exept.de>
parents: 2068
diff changeset
   789
            buffer := tempBuffer.
2d1af00702fe slightly tuned loop
Claus Gittinger <cg@exept.de>
parents: 2068
diff changeset
   790
            ^ self.
2d1af00702fe slightly tuned loop
Claus Gittinger <cg@exept.de>
parents: 2068
diff changeset
   791
        ].
3210
6e7ac9b78369 [true] whileTrue: -> #loop
Stefan Vogel <sv@exept.de>
parents: 2325
diff changeset
   792
    ] loop.
4743
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   793
6bdaef8ec48a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4520
diff changeset
   794
    "Modified: / 30-09-2018 / 15:16:19 / Claus Gittinger"
1027
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   795
! !
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   796
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   797
!Base64Coder class methodsFor:'documentation'!
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   798
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   799
version
3763
0d73959c77f3 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 3547
diff changeset
   800
    ^ '$Header$'
2325
fa4a6a5fd3e1 changed: #nextPut:
Stefan Vogel <sv@exept.de>
parents: 2224
diff changeset
   801
!
fa4a6a5fd3e1 changed: #nextPut:
Stefan Vogel <sv@exept.de>
parents: 2224
diff changeset
   802
fa4a6a5fd3e1 changed: #nextPut:
Stefan Vogel <sv@exept.de>
parents: 2224
diff changeset
   803
version_CVS
3763
0d73959c77f3 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 3547
diff changeset
   804
    ^ '$Header$'
1027
1c8e7e7785ab initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   805
! !
1111
2a64f0fe418a more protocol
Claus Gittinger <cg@exept.de>
parents: 1084
diff changeset
   806