Changeset 299

Show
Ignore:
Timestamp:
10/13/06 17:59:32 (2 years ago)
Author:
krobillard
Message:

Thune - Added 'sha1 checksum.

Location:
trunk/thune
Files:
1 added
4 modified

Legend:

Unmodified
Added
Removed
  • trunk/thune/doc/UserManual

    r294 r299  
    459459to-hex       (int -- int)            Flag integer to display in hexadecimal. 
    460460to-dec       (int -- int)            Flag integer to display in decimal. 
     461checksum     (bin type -- digest)    Compute 'crc16 or 'sha1 checksum. 
    461462===========  ======================  ====================== 
    462463 
  • trunk/thune/files.c

    r279 r299  
    744744 
    745745 
    746 // (binary -- int) 
     746#define SHA1HANDSOFF    1 
     747#include <support/sha1.c> 
     748 
     749 
     750// (binary type -- int) 
    747751UR_CALL( uc_checksum ) 
    748752{ 
     753    UCell* res; 
    749754    char* cpA; 
    750755    char* cpB; 
    751756 
    752     UR_CALL_UNUSED_TH 
    753  
    754     if( ur_binarySlice( tos, &cpA, &cpB ) ) 
    755     { 
    756         ur_initType(tos, UT_INT); 
    757         ur_int(tos) = checksum_crc16( (uint8_t*) cpA, cpB - cpA ); 
     757    if( ! ur_isAWord( tos ) ) 
     758        goto typeErr; 
     759 
     760    UR_S_DROP; 
     761    res = UR_TOS; 
     762 
     763    if( ur_binarySlice( res, &cpA, &cpB ) ) 
     764    { 
     765        switch( ur_atom(tos) ) 
     766        { 
     767            case UR_ATOM_CRC16: 
     768                ur_initType(res, UT_INT); 
     769                ur_int(res) = checksum_crc16( (uint8_t*) cpA, cpB - cpA ); 
     770                return; 
     771 
     772            case UR_ATOM_SHA1: 
     773            { 
     774                SHA1_CTX context; 
     775                UIndex binN; 
     776                UBinary* bin; 
     777 
     778                binN = ur_makeBinary( 20 ); 
     779                bin = ur_binPtr( binN ); 
     780                bin->used = 20; 
     781 
     782                SHA1Init( &context ); 
     783                SHA1Update( &context, (uint8_t*) cpA, cpB - cpA ); 
     784                SHA1Final( bin->ptr.b, &context ); 
     785                 
     786                ur_initType(res, UT_BINARY); 
     787                ur_setSeries(res, binN, 0); 
     788            } 
     789                return; 
     790        } 
     791        goto typeErr; 
    758792    } 
    759793    else 
    760794    { 
    761         ur_setNone(tos); 
    762     } 
     795        ur_throwErr( UR_ERR_DATATYPE, "checksum expected binary!" ); 
     796        return; 
     797    } 
     798 
     799typeErr: 
     800 
     801    ur_throwErr( UR_ERR_DATATYPE, "checksum expected type 'crc16 or 'sha1" ); 
    763802} 
    764803 
  • trunk/thune/urlan.c

    r276 r299  
    269269    FIXED_ATOM( "append", 6, UR_ATOM_APPEND ) 
    270270 
     271    // Checksum types 
     272    FIXED_ATOM( "crc16",  5, UR_ATOM_CRC16 ) 
     273    FIXED_ATOM( "sha1",   4, UR_ATOM_SHA1 ) 
     274 
    271275    FIXED_ATOM( "reader-macros", 13, UR_ATOM_READER_MACROS ) 
    272276 
  • trunk/thune/urlan_atoms.h

    r276 r299  
    4242#define UR_ATOM_WRITE           105 
    4343#define UR_ATOM_APPEND          106 
    44 #define UR_ATOM_READER_MACROS           107 
     44#define UR_ATOM_CRC16           107 
     45#define UR_ATOM_SHA1            108 
     46#define UR_ATOM_READER_MACROS           109