UninterpretedBytes.st
branchjv
changeset 18640 358b275dced9
parent 18617 fbfd2d411738
parent 18633 3173e69ba4a4
child 18678 a9b30d72dff9
equal deleted inserted replaced
18631:27ffa826691b 18640:358b275dced9
     1 "{ Encoding: utf8 }"
       
     2 
       
     3 "
     1 "
     4  COPYRIGHT (c) 1993 by Claus Gittinger
     2  COPYRIGHT (c) 1993 by Claus Gittinger
     5 	      All Rights Reserved
     3 	      All Rights Reserved
     6 
     4 
     7  This software is furnished under a license and may be used
     5  This software is furnished under a license and may be used
   194 !
   192 !
   195 
   193 
   196 fromPackedString:aString
   194 fromPackedString:aString
   197     "ST-80 compatibility: decode a byteArray from a packed string in which
   195     "ST-80 compatibility: decode a byteArray from a packed string in which
   198      6bits are encoded per character. The argument, aString must be a multiple
   196      6bits are encoded per character. The argument, aString must be a multiple
   199      of 4 in size (since 24 is the lcm of 6 and 8). This is somewhat like
   197      of 4 in size (since 24 is the lcm of 6 and 8).
   200      the radix-encoding used in good old PDP11 times ;-)
   198      Every 6 bit packet is encoded as a character in 32..95.
       
   199      Characters below 32 are ignored (so line breaks can be inserted at any place).
       
   200      An addition final byte defines how many bytes of the last triple are valid. 
       
   201      This is somewhat like the radix-encoding used in good old PDP11 times ;-)
   201      ST-80 uses this encoding for Images ...
   202      ST-80 uses this encoding for Images ...
   202      This is a base64 encoding, very similar (but not equal) to the algorithm used in RFC1421.
   203      This is a base64 encoding, very similar (but not equal) to the algorithm used in RFC1421.
   203      PS: It took a while to figure that one out ...
   204      PS: It took a while to figure that one out ...
   204      I don't like it ;-)"
   205      PPS: I don't like it ;-)"
   205 
   206 
   206     |index    "{ Class: SmallInteger }"
   207     |index    "{ Class: SmallInteger }"
   207      dstIndex "{ Class: SmallInteger }"
   208      dstIndex "{ Class: SmallInteger }"
   208      stop     "{ Class: SmallInteger }"
   209      stop     "{ Class: SmallInteger }"
   209      sixBits  "{ Class: SmallInteger }"
   210      sixBits  "{ Class: SmallInteger }"
   219     "the size modulo 3 is encoded in the last character, if it is in the
   220     "the size modulo 3 is encoded in the last character, if it is in the
   220      range 97 .. otherwise, its exact."
   221      range 97 .. otherwise, its exact."
   221 
   222 
   222     last := aString last codePoint.
   223     last := aString last codePoint.
   223     last > 96 ifTrue:[
   224     last > 96 ifTrue:[
   224 	stop := stop - 3 + (last - 96)
   225         stop := stop - 3 + (last - 96)
   225     ].
   226     ].
   226     bytes := self new:stop.
   227     bytes := self new:stop.
   227 
   228 
   228     index := 1. dstIndex := 1.
   229     index := 1. dstIndex := 1.
   229     [dstIndex <= stop] whileTrue:[
   230     [dstIndex <= stop] whileTrue:[
   230 	"/ take 4 characters ...
   231         "/ take 4 characters ...
   231 	"/ allow a line break before each group of 4
   232         "/ allow a line break before each group of 4
   232 	sixBits := (aString at:index) codePoint.
   233         sixBits := (aString at:index) codePoint.
   233 	[sixBits < 32] whileTrue:[
   234         [sixBits < 32] whileTrue:[
   234 	    index := index + 1.
   235             index := index + 1.
   235 	    sixBits := (aString at:index) codePoint.
   236             sixBits := (aString at:index) codePoint.
   236 	].
   237         ].
   237 	sixBits := sixBits bitAnd:16r3F.
   238         sixBits := sixBits bitAnd:16r3F.
   238 	n := sixBits.
   239         n := sixBits.
   239 
   240 
   240 	"/ self assert:(aString at:index+1) codePoint >= 32.
   241         "/ self assert:(aString at:index+1) codePoint >= 32.
   241 	sixBits := (aString at:index+1) codePoint bitAnd:16r3F.
   242         sixBits := (aString at:index+1) codePoint bitAnd:16r3F.
   242 	n := (n bitShift:6) + sixBits.
   243         n := (n bitShift:6) + sixBits.
   243 
   244 
   244 	"/ self assert:(aString at:index+2) codePoint >= 32.
   245         "/ self assert:(aString at:index+2) codePoint >= 32.
   245 	sixBits := (aString at:index+2) codePoint bitAnd:16r3F.
   246         sixBits := (aString at:index+2) codePoint bitAnd:16r3F.
   246 	n := (n bitShift:6) + sixBits.
   247         n := (n bitShift:6) + sixBits.
   247 
   248 
   248 	"/ self assert:(aString at:index+3) codePoint >= 32.
   249         "/ self assert:(aString at:index+3) codePoint >= 32.
   249 	sixBits := (aString at:index+3) codePoint bitAnd:16r3F.
   250         sixBits := (aString at:index+3) codePoint bitAnd:16r3F.
   250 	n := (n bitShift:6) + sixBits.
   251         n := (n bitShift:6) + sixBits.
   251 
   252 
   252 	index := index + 4.
   253         index := index + 4.
   253 
   254 
   254 	"/ now have 24 bits in n
   255         "/ now have 24 bits in n
   255 
   256 
   256 	bytes at:dstIndex put:(n bitShift:-16).
   257         bytes at:dstIndex put:(n bitShift:-16).
   257 
   258 
   258 	dstIndex < stop ifTrue:[
   259         dstIndex < stop ifTrue:[
   259 	    bytes at:dstIndex+1 put:((n bitShift:-8) bitAnd:16rFF).
   260             bytes at:dstIndex+1 put:((n bitShift:-8) bitAnd:16rFF).
   260 	    dstIndex+2 <= stop ifTrue:[
   261             dstIndex+2 <= stop ifTrue:[
   261 		bytes at:dstIndex+2 put:(n bitAnd:16rFF).
   262                 bytes at:dstIndex+2 put:(n bitAnd:16rFF).
   262 	    ]
   263             ]
   263 	].
   264         ].
   264 	dstIndex := dstIndex + 3.
   265         dstIndex := dstIndex + 3.
   265     ].
   266     ].
   266     ^ bytes
   267     ^ bytes
   267 
   268 
   268     "
   269     "
   269      ByteArray fromPackedString:(#[1 1 1 1] asPackedString)
   270      ByteArray fromPackedString:(#[1 1 1 1] asPackedString)
   270      ByteArray fromPackedString:(#[1 1 1 1 1] asPackedString)
   271      ByteArray fromPackedString:(#[1 1 1 1 1] asPackedString)
   271      ByteArray fromPackedString:(#[1 1 1 1 1 1] asPackedString)
   272      ByteArray fromPackedString:(#[1 1 1 1 1 1] asPackedString)
   272      ByteArray fromPackedString:(#[1 1 1 1 1 1 1] asPackedString)
   273      ByteArray fromPackedString:(#[1 1 1 1 1 1 1] asPackedString)
   273      ByteArray fromPackedString:(#[1 1 1 1 1 1 1 1] asPackedString)
   274      ByteArray fromPackedString:(#[1 1 1 1 1 1 1 1] asPackedString)
       
   275      ByteArray fromPackedString:((ByteArray new:256) asPackedString)
       
   276      ByteArray fromPackedString:((ByteArray new:128) asPackedString)
       
   277      ByteArray fromPackedString:((ByteArray new:129) asPackedString)
       
   278      ByteArray fromPackedString:((ByteArray new:130) asPackedString)
       
   279      ByteArray fromPackedString:((ByteArray new:131) asPackedString)
       
   280      ByteArray fromPackedString:((ByteArray new:132) asPackedString)
       
   281      ByteArray fromPackedString:((ByteArray new:64) asPackedString)
       
   282 
       
   283      0 to:256 do:[:l |
       
   284         |orig copy|
       
   285 
       
   286         0 to:255 do:[:fill |
       
   287             orig := ByteArray new:l withAll:fill.
       
   288             copy := ByteArray fromPackedString:(orig asPackedString).
       
   289             self assert:(orig = copy).
       
   290          ]
       
   291      ]
   274     "
   292     "
   275 
   293 
   276     "Modified: / 6.3.1997 / 15:28:52 / cg"
   294     "Modified: / 6.3.1997 / 15:28:52 / cg"
   277     "Modified: / 18.12.1997 / 17:17:11 / stefan"
   295     "Modified: / 18.12.1997 / 17:17:11 / stefan"
   278 !
   296 !