CRC32Stream.st
changeset 2702 bbfd7a7031a6
parent 2572 4d6f4419b6b5
child 2703 00e8619a241b
--- a/CRC32Stream.st	Mon Dec 26 13:43:06 2011 +0100
+++ b/CRC32Stream.st	Mon Jan 09 16:52:45 2012 +0100
@@ -92,14 +92,9 @@
                             hashValue) hexPrintString
                                                                 [exEnd]
 
-  expect 16r98DC9ED7:
-                                                                [exBegin]
-    self information:(CRC32Stream new nextPut:4711; hashValue) printString
-                                                                [exEnd]
-
   expect 16r70E46888:
                                                                 [exBegin]
-    self information:(CRC32Stream hashValueOf:#[1 2 3 4 5 6 7]) printString
+    self information:(CRC32Stream hashValueOf:#[1 2 3 4 5 6 7]) hexPrintString
                                                                 [exEnd]
 
   expect 16r8CD04C73:
@@ -107,7 +102,7 @@
     self information:((CRC32Stream hashValueOf:#[16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 
              16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF
              16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF
-             16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF]) digitBytes hexPrintString)
+             16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF]) hexPrintString)
                                                                 [exEnd]
 
   expect 16r86D7D79A:
@@ -123,7 +118,7 @@
             ].
          ].
     t := (t / 1000) asFloat.
-    Transcript show:'crc32:'; showCR: hashStream hashValue.
+    Transcript show:'crc32:'; showCR: hashStream hashValue hexPrintString.
     Transcript show:t; show:' seconds for '; show:(50*n/1024) asFloat; showCR:' Kb'.
     Transcript show:(n*50/1024 / t); showCR:' Kb/s'
                                                                 [exEnd]
@@ -254,136 +249,113 @@
 
 !CRC32Stream methodsFor:'writing'!
 
-nextPut:arg 
-    "add the hash of anObject to the computed hash so far.
-     anObject can be a Character, SmallInteger, or Character-, Byte-
-     Integer-Array"
+nextPutBytes:count from:anObject startingAt:start
+    "add the hash of anObject to the computed hash so far."
 
     | argSize "{ Class:SmallInteger }"
       byte    "{ Class:SmallInteger }" |
 
 %{
-#if 1
-    unsigned char *cp;
-    int n;
-    unsigned char c;
+    if (__bothSmallInteger(count, start)) {
+        int len, offs;
+        int objSize, nInstVars, nInstBytes;
+        unsigned char *extPtr;
 
-    if (__isStringLike(arg)) {
-        cp = __stringVal(arg);
-        n = __stringSize(arg);
-        goto doBytes;
-    }
+        len = __intVal(count);
+        offs = __intVal(start) - 1;
+
+        if (__isExternalBytesLike(anObject)) {
+            OBJ sz;
 
-    if (__isByteArrayLike(arg)) {
-        cp = __byteArrayVal(arg);
-        n = __byteArraySize(arg);
-    doBytes:
-        {
-            unsigned int _crc;
-            unsigned int *_crcTable;
-
-            if (__isSmallInteger(__INST(crc)) ) {
-                _crc = (unsigned) (__intVal( __INST(crc) ));
+            nInstBytes = 0;
+            extPtr = (char *)__externalBytesAddress(anObject);
+            sz = __externalBytesSize(anObject);
+            if (__isSmallInteger(sz)) {
+                objSize = __intVal(sz);
             } else {
-                _crc = __unsignedLongIntVal( __INST(crc) );
+                objSize = 0; /* unknown */
             }
-
-            _crcTable = __integerArrayVal( __INST(crcTable) );
-
-            while (n > 3) {
-                unsigned _idx;
+        } else {
+            OBJ oClass;
 
-                _idx = (_crc ^ cp[0]) & 0xFF;
-                _crc = _crcTable[_idx] ^ (_crc >> 8);
-                _idx = (_crc ^ cp[1]) & 0xFF;
-                _crc = _crcTable[_idx] ^ (_crc >> 8);
-                _idx = (_crc ^ cp[2]) & 0xFF;
-                _crc = _crcTable[_idx] ^ (_crc >> 8);
-                _idx = (_crc ^ cp[3]) & 0xFF;
-                _crc = _crcTable[_idx] ^ (_crc >> 8);
-                cp += 4;
-                n -= 4;
+            oClass = __Class(anObject);
+            switch (__intVal(__ClassInstPtr(oClass)->c_flags) & ARRAYMASK) {
+                case BYTEARRAY:
+                case WORDARRAY:
+                case LONGARRAY:
+                case SWORDARRAY:
+                case SLONGARRAY:
+                case FLOATARRAY:
+                case DOUBLEARRAY:
+                    break;
+                default:
+                    goto bad;
             }
-            while (n > 0) {
-                unsigned _byte = *cp++;
-                unsigned _idx;
+            nInstVars = __intVal(__ClassInstPtr(oClass)->c_ninstvars);
+            nInstBytes = __OBJS2BYTES__(nInstVars);
+            // nInstBytes is the number of bytes occupied by pointer instance variables
+            // subtract from size and add to byte-pointer
+            objSize = __Size(anObject) - OHDR_SIZE - nInstBytes;
+            extPtr = (char *)__byteArrayVal(anObject)+nInstBytes;
+        }
+
+        if ((offs >= 0) && (len >= 0) && (objSize >= (len + offs))) {
+            {
+                unsigned int _crc;
+                unsigned int *_crcTable;
+                unsigned char *cp = extPtr+offs;
+                unsigned n = len;
 
-                _idx = (_crc ^ _byte) & 0xFF;
-                _crc = _crcTable[_idx] ^ (_crc >> 8);
-                n--;
+                if (__isSmallInteger(__INST(crc)) ) {
+                    _crc = (unsigned) (__intVal( __INST(crc) ));
+                } else {
+                    _crc = __unsignedLongIntVal( __INST(crc) );
+                }
+
+                _crcTable = __integerArrayVal( __INST(crcTable) );
+
+                while (n > 3) {
+                    unsigned _idx;
+
+                    _idx = (_crc ^ cp[0]) & 0xFF;
+                    _crc = _crcTable[_idx] ^ (_crc >> 8);
+                    _idx = (_crc ^ cp[1]) & 0xFF;
+                    _crc = _crcTable[_idx] ^ (_crc >> 8);
+                    _idx = (_crc ^ cp[2]) & 0xFF;
+                    _crc = _crcTable[_idx] ^ (_crc >> 8);
+                    _idx = (_crc ^ cp[3]) & 0xFF;
+                    _crc = _crcTable[_idx] ^ (_crc >> 8);
+                    cp += 4;
+                    n -= 4;
+                }
+                while (n > 0) {
+                    unsigned _byte = *cp++;
+                    unsigned _idx;
+
+                    _idx = (_crc ^ _byte) & 0xFF;
+                    _crc = _crcTable[_idx] ^ (_crc >> 8);
+                    n--;
+                }
+                __INST(crc) = __MKUINT(_crc);
             }
-            __INST(crc) = __MKUINT(_crc);
-        }
-        RETURN (self );
-    }
-    if (__isCharacter(arg)) {
-        unsigned int value;
-
-        value = __intVal(_characterVal(arg));
-        if ((unsigned)value <= 0xFF) {
-            c = value;
-            cp = &c;
-            n = 1;
-            goto doBytes;
+            RETURN (count);
         }
     }
-#endif
+bad: ;
 %}.
-    arg isByteCollection ifTrue:[
-        argSize := arg size.
-        1 to:argSize do:[:n|
-            byte := arg byteAt:n.
-            crc := (crcTable at:((crc bitXor:byte) bitAnd:16rFF)+1) 
-                   bitXor:(crc bitShift:-8).
-        ].
-        ^ self.
-    ].
+    self error:'invalid argument'
 
-    arg isCharacter ifTrue:[
-        byte := arg codePoint.
-        byte < 256 ifTrue:[
-            crc := (crcTable at:((crc bitXor:byte) bitAnd:16rFF)+1) 
-                   bitXor:(crc bitShift:-8).
-            ^ self.
-        ].
-        byte digitBytes do:[:eachByte|
-            crc := (crcTable at:((crc bitXor:eachByte) bitAnd:16rFF)+1) 
-                   bitXor:(crc bitShift:-8).
-        ].
-        ^ self.
-    ].
-
-    arg isInteger ifTrue:[
-        arg < 256 ifTrue:[
-            crc := (crcTable at:((crc bitXor:arg) bitAnd:16rFF)+1) 
-                   bitXor:(crc bitShift:-8).
-            ^ self.
-        ].
-        arg digitBytes do:[:eachByte|
-            crc := (crcTable at:((crc bitXor:eachByte) bitAnd:16rFF)+1) 
-                   bitXor:(crc bitShift:-8).
-        ].
-        ^ self.
-    ].
-
-    arg isCollection ifTrue:[
-        arg do:[:eachElement|
-            self nextPut:eachElement
-        ].
-        ^ self.
-    ].
-
-    self error:'unsupported argument'.
+    "Created: / 09-01-2012 / 16:48:35 / cg"
 ! !
 
 !CRC32Stream class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic2/CRC32Stream.st,v 1.17 2011-05-13 15:35:54 stefan Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic2/CRC32Stream.st,v 1.18 2012-01-09 15:52:45 cg Exp $'
 !
 
 version_CVS
-    ^ '$Header: /cvs/stx/stx/libbasic2/CRC32Stream.st,v 1.17 2011-05-13 15:35:54 stefan Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic2/CRC32Stream.st,v 1.18 2012-01-09 15:52:45 cg Exp $'
 ! !
 
 CRC32Stream initialize!