class: UninterpretedBytes
authorClaus Gittinger <cg@exept.de>
Fri, 24 Jul 2015 13:07:56 +0200
changeset 18633 3173e69ba4a4
parent 18632 5f99981d6418
child 18634 76a2b07859f7
class: UninterpretedBytes comment/format in: #fromPackedString:
UninterpretedBytes.st
--- a/UninterpretedBytes.st	Fri Jul 24 12:22:51 2015 +0200
+++ b/UninterpretedBytes.st	Fri Jul 24 13:07:56 2015 +0200
@@ -1,5 +1,3 @@
-"{ Encoding: utf8 }"
-
 "
  COPYRIGHT (c) 1993 by Claus Gittinger
 	      All Rights Reserved
@@ -196,12 +194,15 @@
 fromPackedString:aString
     "ST-80 compatibility: decode a byteArray from a packed string in which
      6bits are encoded per character. The argument, aString must be a multiple
-     of 4 in size (since 24 is the lcm of 6 and 8). This is somewhat like
-     the radix-encoding used in good old PDP11 times ;-)
+     of 4 in size (since 24 is the lcm of 6 and 8).
+     Every 6 bit packet is encoded as a character in 32..95.
+     Characters below 32 are ignored (so line breaks can be inserted at any place).
+     An addition final byte defines how many bytes of the last triple are valid. 
+     This is somewhat like the radix-encoding used in good old PDP11 times ;-)
      ST-80 uses this encoding for Images ...
      This is a base64 encoding, very similar (but not equal) to the algorithm used in RFC1421.
      PS: It took a while to figure that one out ...
-     I don't like it ;-)"
+     PPS: I don't like it ;-)"
 
     |index    "{ Class: SmallInteger }"
      dstIndex "{ Class: SmallInteger }"
@@ -221,47 +222,47 @@
 
     last := aString last codePoint.
     last > 96 ifTrue:[
-	stop := stop - 3 + (last - 96)
+        stop := stop - 3 + (last - 96)
     ].
     bytes := self new:stop.
 
     index := 1. dstIndex := 1.
     [dstIndex <= stop] whileTrue:[
-	"/ take 4 characters ...
-	"/ allow a line break before each group of 4
-	sixBits := (aString at:index) codePoint.
-	[sixBits < 32] whileTrue:[
-	    index := index + 1.
-	    sixBits := (aString at:index) codePoint.
-	].
-	sixBits := sixBits bitAnd:16r3F.
-	n := sixBits.
-
-	"/ self assert:(aString at:index+1) codePoint >= 32.
-	sixBits := (aString at:index+1) codePoint bitAnd:16r3F.
-	n := (n bitShift:6) + sixBits.
-
-	"/ self assert:(aString at:index+2) codePoint >= 32.
-	sixBits := (aString at:index+2) codePoint bitAnd:16r3F.
-	n := (n bitShift:6) + sixBits.
-
-	"/ self assert:(aString at:index+3) codePoint >= 32.
-	sixBits := (aString at:index+3) codePoint bitAnd:16r3F.
-	n := (n bitShift:6) + sixBits.
-
-	index := index + 4.
-
-	"/ now have 24 bits in n
-
-	bytes at:dstIndex put:(n bitShift:-16).
-
-	dstIndex < stop ifTrue:[
-	    bytes at:dstIndex+1 put:((n bitShift:-8) bitAnd:16rFF).
-	    dstIndex+2 <= stop ifTrue:[
-		bytes at:dstIndex+2 put:(n bitAnd:16rFF).
-	    ]
-	].
-	dstIndex := dstIndex + 3.
+        "/ take 4 characters ...
+        "/ allow a line break before each group of 4
+        sixBits := (aString at:index) codePoint.
+        [sixBits < 32] whileTrue:[
+            index := index + 1.
+            sixBits := (aString at:index) codePoint.
+        ].
+        sixBits := sixBits bitAnd:16r3F.
+        n := sixBits.
+
+        "/ self assert:(aString at:index+1) codePoint >= 32.
+        sixBits := (aString at:index+1) codePoint bitAnd:16r3F.
+        n := (n bitShift:6) + sixBits.
+
+        "/ self assert:(aString at:index+2) codePoint >= 32.
+        sixBits := (aString at:index+2) codePoint bitAnd:16r3F.
+        n := (n bitShift:6) + sixBits.
+
+        "/ self assert:(aString at:index+3) codePoint >= 32.
+        sixBits := (aString at:index+3) codePoint bitAnd:16r3F.
+        n := (n bitShift:6) + sixBits.
+
+        index := index + 4.
+
+        "/ now have 24 bits in n
+
+        bytes at:dstIndex put:(n bitShift:-16).
+
+        dstIndex < stop ifTrue:[
+            bytes at:dstIndex+1 put:((n bitShift:-8) bitAnd:16rFF).
+            dstIndex+2 <= stop ifTrue:[
+                bytes at:dstIndex+2 put:(n bitAnd:16rFF).
+            ]
+        ].
+        dstIndex := dstIndex + 3.
     ].
     ^ bytes
 
@@ -271,6 +272,23 @@
      ByteArray fromPackedString:(#[1 1 1 1 1 1] asPackedString)
      ByteArray fromPackedString:(#[1 1 1 1 1 1 1] asPackedString)
      ByteArray fromPackedString:(#[1 1 1 1 1 1 1 1] asPackedString)
+     ByteArray fromPackedString:((ByteArray new:256) asPackedString)
+     ByteArray fromPackedString:((ByteArray new:128) asPackedString)
+     ByteArray fromPackedString:((ByteArray new:129) asPackedString)
+     ByteArray fromPackedString:((ByteArray new:130) asPackedString)
+     ByteArray fromPackedString:((ByteArray new:131) asPackedString)
+     ByteArray fromPackedString:((ByteArray new:132) asPackedString)
+     ByteArray fromPackedString:((ByteArray new:64) asPackedString)
+
+     0 to:256 do:[:l |
+        |orig copy|
+
+        0 to:255 do:[:fill |
+            orig := ByteArray new:l withAll:fill.
+            copy := ByteArray fromPackedString:(orig asPackedString).
+            self assert:(orig = copy).
+         ]
+     ]
     "
 
     "Modified: / 6.3.1997 / 15:28:52 / cg"