From: Robert K. <may...@us...> - 2001-12-05 22:44:11
|
Update of /cvsroot/bitcollider/bitprint In directory usw-pr-cvs1:/tmp/cvs-serv4628 Modified Files: bitprint.c bitprint.h Added Files: btest.c Log Message: Updated the bitprint code with the latest endian stuff and the . in the new bitprint format. --- NEW FILE: btest.c --- /* (PD) 2001 The Bitzi Corporation * Please see http://bitzi.com/publicdomain for more info. * * $Id: btest.c,v 1.1 2001/12/05 22:44:07 mayhemchaos Exp $ */ /* This file is used for testing purposes only. */ #include <stdio.h> #include "bitprint.h" void convert_to_hex(const unsigned char *buffer, int size, char *hexBuffer) { int i; for(i = 0; i < size; i++) { sprintf(hexBuffer + (i * sizeof(char) * 2), "%02X", buffer[i] & 0xFF); } } int main(int argc, char *argv[]) { unsigned char bitprint[BITPRINT_RAW_LEN + 1]; unsigned char bitprint2[BITPRINT_RAW_LEN + 1]; char base32Bitprint[BITPRINT_BASE32_LEN + 1]; char base32Bitprint2[BITPRINT_BASE32_LEN + 1]; if (argc < 1) { printf("usage: btest <file>\n"); exit(0); } if (!bitziBitprintFile(argv[1], bitprint)) { printf("Cannot bitprint file %s\n", argv[1]); exit(0); } bitziBitprintToBase32(bitprint, base32Bitprint); printf("%s\n", base32Bitprint); bitziBitprintFromBase32(base32Bitprint, bitprint2); if (memcmp(bitprint, bitprint2, BITPRINT_RAW_LEN)) { char temp[100]; printf("The base32 de/encode functions are faulty!\n"); convert_to_hex(bitprint, BITPRINT_RAW_LEN, temp); printf("bitprint1: %s\n", temp); convert_to_hex(bitprint2, BITPRINT_RAW_LEN, temp); printf("bitprint2: %s\n", temp); } return 0; } Index: bitprint.c =================================================================== RCS file: /cvsroot/bitcollider/bitprint/bitprint.c,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -r1.5 -r1.6 *** bitprint.c 2001/07/16 20:36:59 1.5 --- bitprint.c 2001/12/05 22:44:07 1.6 *************** *** 29,33 **** #else ! #error Please define the endianness of the platform that will run this code. /* Notes for non-windows users: If you are using autoconf, then add the following lines to your --- 29,34 ---- #else ! #define SIZEOF_LONG 4 ! //#error Please define the endianness of the platform that will run this code. /* Notes for non-windows users: If you are using autoconf, then add the following lines to your *************** *** 55,58 **** --- 56,63 ---- #endif + #ifdef BIG_ENDIAN + #undef BIG_ENDIAN + #endif + #ifdef WORDS_BIGENDIAN # define BIG_ENDIAN 1 *************** *** 102,105 **** --- 107,111 ---- typedef unsigned long word32; + typedef unsigned short word16; typedef unsigned char byte; *************** *** 119,168 **** #define BASE32_LOOKUP_MAX 43 ! static unsigned char *base32Chars = "ABCDEFGHIJKMNPQRSTUVWXYZ23456789"; static unsigned char base32Lookup[BASE32_LOOKUP_MAX][2] = { ! '0', 0xFF, ! '1', 0xFF, ! '2', 0x18, ! '3', 0x19, ! '4', 0x1A, ! '5', 0x1B, ! '6', 0x1C, ! '7', 0x1D, ! '8', 0x1E, ! '9', 0x1F, ! ':', 0xFF, ! ';', 0xFF, ! '<', 0xFF, ! '=', 0xFF, ! '>', 0xFF, ! '?', 0xFF, ! '@', 0xFF, ! 'A', 0x00, ! 'B', 0x01, ! 'C', 0x02, ! 'D', 0x03, ! 'E', 0x04, ! 'F', 0x05, ! 'G', 0x06, ! 'H', 0x07, ! 'I', 0x08, ! 'J', 0x09, ! 'K', 0x0A, ! 'L', 0xFF, ! 'M', 0x0B, ! 'N', 0x0C, ! 'O', 0xFF, ! 'P', 0x0D, ! 'Q', 0x0E, ! 'R', 0x0F, ! 'S', 0x10, ! 'T', 0x11, ! 'U', 0x12, ! 'V', 0x13, ! 'W', 0x14, ! 'X', 0x15, ! 'Y', 0x16, ! 'Z', 0x17 }; --- 125,174 ---- #define BASE32_LOOKUP_MAX 43 ! static unsigned char *base32Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"; static unsigned char base32Lookup[BASE32_LOOKUP_MAX][2] = { ! { '0', 0xFF }, ! { '1', 0xFF }, ! { '2', 0x1A }, ! { '3', 0x1B }, ! { '4', 0x1C }, ! { '5', 0x1D }, ! { '6', 0x1E }, ! { '7', 0x1F }, ! { '8', 0xFF }, ! { '9', 0xFF }, ! { ':', 0xFF }, ! { ';', 0xFF }, ! { '<', 0xFF }, ! { '=', 0xFF }, ! { '>', 0xFF }, ! { '?', 0xFF }, ! { '@', 0xFF }, ! { 'A', 0x00 }, ! { 'B', 0x01 }, ! { 'C', 0x02 }, ! { 'D', 0x03 }, ! { 'E', 0x04 }, ! { 'F', 0x05 }, ! { 'G', 0x06 }, ! { 'H', 0x07 }, ! { 'I', 0x08 }, ! { 'J', 0x09 }, ! { 'K', 0x0A }, ! { 'L', 0x0B }, ! { 'M', 0x0C }, ! { 'N', 0x0D }, ! { 'O', 0x0E }, ! { 'P', 0x0F }, ! { 'Q', 0x10 }, ! { 'R', 0x11 }, ! { 'S', 0x12 }, ! { 'T', 0x13 }, ! { 'U', 0x14 }, ! { 'V', 0x15 }, ! { 'W', 0x16 }, ! { 'X', 0x17 }, ! { 'Y', 0x18 }, ! { 'Z', 0x19 } }; *************** *** 276,284 **** { bitziEncodeBase32(bitprint, BITPRINT_RAW_LEN, base32Bitprint); } ! void bitziBitprintFromBase32(const char *base32Bitprint, unsigned char *bitprint) { bitziDecodeBase32(base32Bitprint, BITPRINT_BASE32_LEN, bitprint); } --- 282,299 ---- { bitziEncodeBase32(bitprint, BITPRINT_RAW_LEN, base32Bitprint); + memmove(base32Bitprint + SHA_BASE32SIZE + 1, + base32Bitprint + SHA_BASE32SIZE, TIGER_BASE32SIZE + 1); + base32Bitprint[SHA_BASE32SIZE] = '.'; } ! void bitziBitprintFromBase32(const char *base32BitprintWithDot, unsigned char *bitprint) { + char base32Bitprint[BITPRINT_BASE32_LEN + 1]; + + memcpy(base32Bitprint, base32BitprintWithDot, SHA_BASE32SIZE); + memcpy(base32Bitprint + SHA_BASE32SIZE, + base32BitprintWithDot + SHA_BASE32SIZE + 1, + TIGER_BASE32SIZE + 1); bitziDecodeBase32(base32Bitprint, BITPRINT_BASE32_LEN, bitprint); } *************** *** 341,344 **** --- 356,360 ---- { lookup = toupper(base32Buffer[i]) - '0'; + /* Check to make sure that the given word falls inside a valid range */ *************** *** 361,365 **** --- 377,383 ---- } else + { buffer[offset] |= word << (8 - index); + } } else *************** *** 675,681 **** * http://www.cs.technion.ac.il/~biham/Reports/Tiger/ */ - /* Initialize the tigertree context */ ! static void tt_init(TT_CONTEXT *ctx) { ctx->count = 0; --- 693,698 ---- * http://www.cs.technion.ac.il/~biham/Reports/Tiger/ */ /* Initialize the tigertree context */ ! void tt_init(TT_CONTEXT *ctx) { ctx->count = 0; *************** *** 687,690 **** --- 704,710 ---- byte *node = ctx->top - NODESIZE; tiger((word64*)node,(word64)NODESIZE,(word64*)ctx->top); // combine two nodes + #if USE_BIG_ENDIAN + tt_endian((byte *)ctx->top); + #endif memmove(node,ctx->top,TIGERSIZE); // move up result ctx->top -= TIGERSIZE; // update top ptr *************** *** 696,699 **** --- 716,722 ---- tiger((word64*)block,(word64)ctx->index,(word64*)ctx->top); + #if USE_BIG_ENDIAN + tt_endian((byte *)ctx->top); + #endif ctx->top += TIGERSIZE; ++ctx->count; *************** *** 705,709 **** } ! static void tt_update(TT_CONTEXT *ctx, byte *buffer, word32 len) { if (ctx->index) --- 728,732 ---- } ! void tt_update(TT_CONTEXT *ctx, byte *buffer, word32 len) { if (ctx->index) *************** *** 711,727 **** unsigned left = BLOCKSIZE - ctx->index; if (len < left) ! { ! memmove(ctx->block + ctx->index, buffer, len); ! ctx->index += len; ! return; /* Finished */ ! } else ! { ! memmove(ctx->block + ctx->index, buffer, left); ! ctx->index = BLOCKSIZE; ! tt_block(ctx, ctx->block); ! buffer += left; ! len -= left; ! } } while (len >= BLOCKSIZE) --- 734,750 ---- unsigned left = BLOCKSIZE - ctx->index; if (len < left) ! { ! memmove(ctx->block + ctx->index, buffer, len); ! ctx->index += len; ! return; /* Finished */ ! } else ! { ! memmove(ctx->block + ctx->index, buffer, left); ! ctx->index = BLOCKSIZE; ! tt_block(ctx, ctx->block); ! buffer += left; ! len -= left; ! } } while (len >= BLOCKSIZE) *************** *** 746,750 **** } ! static void tt_digest(TT_CONTEXT *ctx, byte *s) { tt_final(ctx); --- 769,773 ---- } ! void tt_digest(TT_CONTEXT *ctx, byte *s) { tt_final(ctx); *************** *** 755,760 **** } // this code untested; use at own risk ! static void tt_copy(TT_CONTEXT *dest, TT_CONTEXT *src) { int i; --- 778,811 ---- } + void tt_endian(byte *s) + { + word64 *i; + byte *b, btemp; + word16 *w, wtemp; + + for(w = (word16 *)s; w < ((word16 *)s) + 12; w++) + { + b = (byte *)w; + btemp = *b; + *b = *(b + 1); + *(b + 1) = btemp; + } + + for(i = (word64 *)s; i < ((word64 *)s) + 3; i++) + { + w = (word16 *)i; + + wtemp = *w; + *w = *(w + 3); + *(w + 3) = wtemp; + + wtemp = *(w + 1); + *(w + 1) = *(w + 2); + *(w + 2) = wtemp; + } + } + // this code untested; use at own risk ! void tt_copy(TT_CONTEXT *dest, TT_CONTEXT *src) { int i; *************** *** 1587,1660 **** sha_final(digest, sha_info); } - - #if 0 - void bitziEncodeBase32(const unsigned char *buffer, - unsigned int bufLen, - char *base32Buffer) - { - unsigned int i, index; - unsigned char word; - - for(i = 0, index = 0; i < bufLen;) - { - /* Is the current word going to span a byte boundary? */ - if (index > 3) - { - word = buffer[i] >> index; - index = (index + 5) % 8; - if (i < bufLen - 1) - word |= (buffer[i + 1] & (0xFF >> (8 - index))) << (5 - index); - i++; - } - else - { - word = (buffer[i] >> index) & 0x1F; - index = (index + 5) % 8; - } - - assert(word < 32); - *(base32Buffer++) = (char)base32Chars[word]; - } - - *base32Buffer = 0; - } - - void bitziDecodeBase32(const char *base32Buffer, - unsigned int base32BufLen, - unsigned char *buffer) - { - int i, index, max, lookup, offset; - unsigned char word; - - memset(buffer, 0, bitziGetBase32DecodeLength(base32BufLen)); - max = strlen(base32Buffer); - for(i = 0, index = 0, offset = 0; i < max; i++) - { - lookup = toupper(base32Buffer[i]) - '0'; - /* Check to make sure that the given word falls inside - a valid range */ - if ( lookup < 0 && lookup >= BASE32_LOOKUP_MAX) - word = 0xFF; - else - word = base32Lookup[lookup][1]; - - /* If this word is not in the table, ignore it */ - if (word == 0xFF) - continue; - - if (index <= 3) - { - buffer[offset] |= (word << index); - index = (index + 5) % 8; - } - else - { - buffer[offset] |= (word << index) & 0xFF; - index = (index + 5) % 8; - offset++; - - buffer[offset] |= word >> (5 - index); - } - } - } - #endif --- 1638,1639 ---- Index: bitprint.h =================================================================== RCS file: /cvsroot/bitcollider/bitprint/bitprint.h,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -r1.3 -r1.4 *** bitprint.h 2001/07/16 20:24:54 1.3 --- bitprint.h 2001/12/05 22:44:07 1.4 *************** *** 11,14 **** --- 11,16 ---- #endif + #include <stdio.h> + /* =================================================================== */ /* Definitions for the SHA1 hash function */ *************** *** 75,79 **** */ #define BITPRINT_RAW_LEN 44 ! #define BITPRINT_BASE32_LEN 71 /* This is the context class for the bitziInit, bitziUpdate, bitziFinal --- 77,83 ---- */ #define BITPRINT_RAW_LEN 44 ! #define BITPRINT_BASE32_LEN 72 ! #define SHA_BASE32SIZE 32 ! #define TIGER_BASE32SIZE 39 /* This is the context class for the bitziInit, bitziUpdate, bitziFinal |