- initial import

git-svn-id: http://moon:8086/svn/mips@1 a8ebac50-d88d-4704-bea3-6648445a41b3
This commit is contained in:
2014-07-20 15:01:37 +00:00
commit 26291bca3d
1549 changed files with 3997969 additions and 0 deletions
+74
View File
@@ -0,0 +1,74 @@
/*
* bogomips.c -- Program to measure bogomips... this program will probably go
* totally wacky with cpufreq enabled.
*
* Copyright (C) 2005 Darrick Wong.
*/
#include <stdio.h>
#include <time.h>
/* this should be approx 2 Bo*oMips to start (note initial shift), and will
* still work even if initially too large, it will just take slightly longer */
unsigned long loops_per_jiffy = (1<<12);
/* This is the number of bits of precision for the loops_per_jiffy. Each
* bit takes on average 1.5/HZ seconds. This (like the original) is a little
* better than 1% */
#define LPS_PREC 8
extern int HZ;
extern void delay(unsigned int loops);
//plagiarized straight from the 2.4 sources.
void calibrate_delay(void)
{
unsigned long ticks, loopbit;
int lps_precision = LPS_PREC;
loops_per_jiffy = (1<<12);
printf("Calibrating delay loop... ");
while (loops_per_jiffy <<= 1) {
/* wait for "start of" clock tick */
ticks = clock();
while (ticks == clock())
/* nothing */;
/* Go .. */
ticks = clock();
delay(loops_per_jiffy);
ticks = clock() - ticks;
if (ticks)
break;
}
/* Do a binary approximation to get loops_per_jiffy set to equal one clock
(up to lps_precision bits) */
loops_per_jiffy >>= 1;
loopbit = loops_per_jiffy;
while ( lps_precision-- && (loopbit >>= 1) ) {
loops_per_jiffy |= loopbit;
ticks = clock();
while (ticks == clock());
ticks = clock();
delay(loops_per_jiffy);
if (clock() != ticks) /* longer than 1 tick */
loops_per_jiffy &= ~loopbit;
}
/* Round the value and print it */
printf("%lu.%02lu BogoMIPS\n",
loops_per_jiffy/(500000/HZ),
(loops_per_jiffy/(5000/HZ)) % 100);
}
int main(int c, char *v[]) {
if(c > 1) {
while(1) {calibrate_delay();}
}
calibrate_delay();
return 0;
}
+20
View File
@@ -0,0 +1,20 @@
//386 delay.
int HZ = 100;
/*
* Non TSC based delay loop for 386, 486, MediaGX
*/
void __inline__ delay(unsigned long loops)
{
int d0;
__asm__ __volatile__(
"\tjmp 1f\n"
".align 16\n"
"1:\tjmp 2f\n"
".align 16\n"
"2:\tdecl %0\n\tjns 2b"
:"=&a" (d0)
:"0" (loops));
}
+20
View File
@@ -0,0 +1,20 @@
//Pentiums-with-TSC delay.
int HZ = 100;
#define rdtscl(low) \
__asm__ __volatile__ ("rdtsc" : "=a" (low) : : "edx")
//This delay() is the one used on x86's with TSC after 2.2.14.
//It won't work on a non TSC x86, period.
void __inline__ delay(unsigned long loops)
{
unsigned long bclock, now;
rdtscl(bclock);
do
{
rdtscl(now);
}
while((now-bclock) < loops);
}
+9
View File
@@ -0,0 +1,9 @@
//portable delay.
int HZ = 100;
void delay(int loops)
{
long i;
for (i = loops; i >= 0 ; i--) ;
}
File diff suppressed because it is too large Load Diff
Binary file not shown.
File diff suppressed because it is too large Load Diff
Binary file not shown.
+349
View File
@@ -0,0 +1,349 @@
/********************************************************************\
*
* FILE: hashtest.c
*
* CONTENTS: test file for sample C-implementation of
* RIPEMD-160 and RIPEMD128
* * command line arguments:
* filename -- compute hash code of file read binary
* -sstring -- print string & hashcode
* -t -- perform time trial
* -x -- execute standard test suite, ASCII input
* * for linkage with rmd128.c: define RMDsize as 128
* for linkage with rmd160.c: define RMDsize as 160 (default)
* TARGET: any computer with an ANSI C compiler
*
* AUTHOR: Antoon Bosselaers, ESAT-COSIC
* DATE: 18 April 1996
* VERSION: 1.1
* HISTORY: bug in RMDonemillion() corrected
*
* Copyright (c) Katholieke Universiteit Leuven
* 1996, All Rights Reserved
*
* Conditions for use of the RIPEMD-160 Software
*
* The RIPEMD-160 software is freely available for use under the terms and
* conditions described hereunder, which shall be deemed to be accepted by
* any user of the software and applicable on any use of the software:
*
* 1. K.U.Leuven Department of Electrical Engineering-ESAT/COSIC shall for
* all purposes be considered the owner of the RIPEMD-160 software and of
* all copyright, trade secret, patent or other intellectual property
* rights therein.
* 2. The RIPEMD-160 software is provided on an "as is" basis without
* warranty of any sort, express or implied. K.U.Leuven makes no
* representation that the use of the software will not infringe any
* patent or proprietary right of third parties. User will indemnify
* K.U.Leuven and hold K.U.Leuven harmless from any claims or liabilities
* which may arise as a result of its use of the software. In no
* circumstances K.U.Leuven R&D will be held liable for any deficiency,
* fault or other mishappening with regard to the use or performance of
* the software.
* 3. User agrees to give due credit to K.U.Leuven in scientific publications
* or communications in relation with the use of the RIPEMD-160 software
* as follows: RIPEMD-160 software written by Antoon Bosselaers,
* available at http://www.esat.kuleuven.be/~cosicart/ps/AB-9601/.
*
\********************************************************************/
#ifndef RMDsize
#define RMDsize 160
#endif
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#if RMDsize == 128
#include "rmd128.h"
#elif RMDsize == 160
#include "rmd160.h"
#endif
#define TEST_BLOCK_SIZE 8000
#define TEST_BLOCKS 12500
#define TEST_BYTES ((long)TEST_BLOCK_SIZE * (long)TEST_BLOCKS)
/********************************************************************/
byte *RMD(byte *message)
/*
* returns RMD(message)
* message should be a string terminated by '\0'
*/
{
dword MDbuf[RMDsize/32]; /* contains (A, B, C, D(, E)) */
static byte hashcode[RMDsize/8]; /* for final hash-value */
dword X[16]; /* current 16-word chunk */
unsigned int i; /* counter */
dword length; /* length in bytes of message */
dword nbytes; /* # of bytes not yet processed */
/* initialize */
MDinit(MDbuf);
length = (dword)strlen((char *)message);
/* process message in 16-word chunks */
for (nbytes=length; nbytes > 63; nbytes-=64) {
for (i=0; i<16; i++) {
X[i] = BYTES_TO_DWORD(message);
message += 4;
}
compress(MDbuf, X);
} /* length mod 64 bytes left */
/* finish: */
MDfinish(MDbuf, message, length, 0);
for (i=0; i<RMDsize/8; i+=4) {
hashcode[i] = MDbuf[i>>2]; /* implicit cast to byte */
hashcode[i+1] = (MDbuf[i>>2] >> 8); /* extracts the 8 least */
hashcode[i+2] = (MDbuf[i>>2] >> 16); /* significant bits. */
hashcode[i+3] = (MDbuf[i>>2] >> 24);
}
return (byte *)hashcode;
}
/********************************************************************/
byte *RMDbinary(char *fname)
/*
* returns RMD(message in file fname)
* fname is read as binary data.
*/
{
FILE *mf; /* pointer to file <fname> */
byte data[1024]; /* contains current mess. block */
dword nbytes; /* length of this block */
dword MDbuf[RMDsize/32]; /* contains (A, B, C, D(, E)) */
static byte hashcode[RMDsize/8]; /* for final hash-value */
dword X[16]; /* current 16-word chunk */
unsigned int i, j; /* counters */
dword length[2]; /* length in bytes of message */
dword offset; /* # of unprocessed bytes at */
/* call of MDfinish */
/* initialize */
if ((mf = fopen(fname, "rb")) == NULL) {
fprintf(stderr, "\nRMDbinary: cannot open file \"%s\".\n",
fname);
exit(1);
}
MDinit(MDbuf);
length[0] = 0;
length[1] = 0;
while ((nbytes = fread(data, 1, 1024, mf)) != 0) {
/* process all complete blocks */
for (i=0; i<(nbytes>>6); i++) {
for (j=0; j<16; j++)
X[j] = BYTES_TO_DWORD(data+64*i+4*j);
compress(MDbuf, X);
}
/* update length[] */
if (length[0] + nbytes < length[0])
length[1]++; /* overflow to msb of length */
length[0] += nbytes;
}
/* finish: */
offset = length[0] & 0x3C0; /* extract bytes 6 to 10 inclusive */
MDfinish(MDbuf, data+offset, length[0], length[1]);
for (i=0; i<RMDsize/8; i+=4) {
hashcode[i] = MDbuf[i>>2];
hashcode[i+1] = (MDbuf[i>>2] >> 8);
hashcode[i+2] = (MDbuf[i>>2] >> 16);
hashcode[i+3] = (MDbuf[i>>2] >> 24);
}
fclose(mf);
return (byte *)hashcode;
}
/********************************************************************/
void speedtest(void)
/*
* A time trial routine, to measure the speed of ripemd.
* Measures processor time required to process TEST_BLOCKS times
* a message of TEST_BLOCK_SIZE characters.
*/
{
clock_t t0, t1;
byte *data;
byte hashcode[RMDsize/8];
dword X[16];
dword MDbuf[RMDsize/32];
unsigned int i, j, k;
srand(time(NULL));
/* allocate and initialize test data */
if ((data = (byte*)malloc(TEST_BLOCK_SIZE)) == NULL) {
fprintf(stderr, "speedtest: allocation error\n");
exit(1);
}
for (i=0; i<TEST_BLOCK_SIZE; i++)
data[i] = (byte)(rand() >> 7);
/* start timer */
printf("\n\nRIPEMD-%u time trial. Processing %ld characters...\n",
RMDsize, TEST_BYTES);
t0 = clock();
/* process data */
MDinit(MDbuf);
for (i=0; i<TEST_BLOCKS; i++) {
for (j=0; j<TEST_BLOCK_SIZE; j+=64) {
for (k=0; k<16; k++)
X[k] = BYTES_TO_DWORD(data+j+4*k);
compress(MDbuf, X);
}
}
MDfinish(MDbuf, data, TEST_BYTES, 0);
/* stop timer, get time difference */
t1 = clock();
printf("\nTest input processed in %g seconds.\n",
(double)(t1-t0)/(double)CLOCKS_PER_SEC);
printf("Characters processed per second: %g\n",
(double)CLOCKS_PER_SEC*TEST_BYTES/((double)t1-t0));
for (i=0; i<RMDsize/8; i+=4) {
hashcode[i] = MDbuf[i>>2];
hashcode[i+1] = (MDbuf[i>>2] >> 8);
hashcode[i+2] = (MDbuf[i>>2] >> 16);
hashcode[i+3] = (MDbuf[i>>2] >> 24);
}
printf("\nhashcode: ");
for (i=0; i<RMDsize/8; i++)
printf("%02x", hashcode[i]);
printf("\n");
free(data);
return;
}
/********************************************************************/
void RMDonemillion(void)
/*
* returns RMD() of message consisting of 1 million 'a' characters
*/
{
dword MDbuf[RMDsize/32]; /* contains (A, B, C, D(, E)) */
static byte hashcode[RMDsize/8]; /* for final hash-value */
dword X[16]; /* current 16-word chunk */
unsigned int i; /* counter */
MDinit(MDbuf);
memcpy(X, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 32);
memcpy(X+8, X, 32);
for (i=15625; i>0; i--)
compress(MDbuf, X);
MDfinish(MDbuf, NULL, 1000000UL, 0);
for (i=0; i<RMDsize/8; i+=4) {
hashcode[i] = MDbuf[i>>2];
hashcode[i+1] = (MDbuf[i>>2] >> 8);
hashcode[i+2] = (MDbuf[i>>2] >> 16);
hashcode[i+3] = (MDbuf[i>>2] >> 24);
}
printf("\n* message: 1 million times \"a\"\n hashcode: ");
for (i=0; i<RMDsize/8; i++)
printf("%02x", hashcode[i]);
}
/********************************************************************/
void RMDstring(char *message, char *print)
{
unsigned int i;
byte *hashcode;
hashcode = RMD((byte *)message);
printf("\n* message: %s\n hashcode: ", print);
for (i=0; i<RMDsize/8; i++)
printf("%02x", hashcode[i]);
}
/********************************************************************/
void testsuite (void)
/*
* standard test suite
*/
{
printf("\n\nRIPEMD-%u test suite results (ASCII):\n", RMDsize);
RMDstring("", "\"\" (empty string)");
RMDstring("a", "\"a\"");
RMDstring("abc", "\"abc\"");
RMDstring("message digest", "\"message digest\"");
RMDstring("abcdefghijklmnopqrstuvwxyz", "\"abcdefghijklmnopqrstuvwxyz\"");
RMDstring("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
"\"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq\"");
RMDstring("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
"\"A...Za...z0...9\"");
RMDstring("1234567890123456789012345678901234567890"
"1234567890123456789012345678901234567890",
"8 times \"1234567890\"");
RMDonemillion();
printf("\n");
return;
}
/********************************************************************/
main (int argc, char *argv[])
/*
* main program. calls one or more of the test routines depending
* on command line arguments. see the header of this file.
*
*/
{
unsigned int i, j;
byte *hashcode;
if (argc == 1) {
printf("For each command line argument in turn:\n");
printf(" filename -- compute hash code of file binary read\n");
printf(" -sstring -- print string & hashcode\n");
printf(" -t -- perform time trial\n");
printf(" -x -- execute standard test suite, ASCII input\n");
}
else {
for (i = 1; i < argc; i++) {
if (argv[i][0] == '-' && argv[i][1] == 's') {
printf("\n\nmessage: %s", argv[i]+2);
hashcode = RMD((byte *)argv[i] + 2);
printf("\nhashcode: ");
for (j=0; j<RMDsize/8; j++)
printf("%02x", hashcode[j]);
printf("\n");
}
else if (strcmp (argv[i], "-t") == 0)
speedtest ();
else if (strcmp (argv[i], "-x") == 0)
testsuite ();
else {
hashcode = RMDbinary (argv[i]);
printf("\n\nmessagefile (binary): %s", argv[i]);
printf("\nhashcode: ");
for (j=0; j<RMDsize/8; j++)
printf("%02x", hashcode[j]);
printf("\n");
}
}
}
printf("\n");
return 0;
}
/********************** end of file hashtest.c **********************/
+13
View File
@@ -0,0 +1,13 @@
#include <stdio.h>
#define A 0.5
#define B 0.55
_Sat long _Fract sat_add3 (_Sat long _Fract a, _Sat long _Fract b)
{
return a + b;
}
int main()
{
printf("hello %X + %X = %X", (_Sat long _Fract)A, (_Sat long _Fract)B, sat_add3(A, B));
}
Binary file not shown.
+294
View File
@@ -0,0 +1,294 @@
/********************************************************************\
*
* FILE: rmd160.c
*
* CONTENTS: A sample C-implementation of the RIPEMD-160
* hash-function.
* TARGET: any computer with an ANSI C compiler
*
* AUTHOR: Antoon Bosselaers, ESAT-COSIC
* DATE: 1 March 1996
* VERSION: 1.0
*
* Copyright (c) Katholieke Universiteit Leuven
* 1996, All Rights Reserved
*
* Conditions for use of the RIPEMD-160 Software
*
* The RIPEMD-160 software is freely available for use under the terms and
* conditions described hereunder, which shall be deemed to be accepted by
* any user of the software and applicable on any use of the software:
*
* 1. K.U.Leuven Department of Electrical Engineering-ESAT/COSIC shall for
* all purposes be considered the owner of the RIPEMD-160 software and of
* all copyright, trade secret, patent or other intellectual property
* rights therein.
* 2. The RIPEMD-160 software is provided on an "as is" basis without
* warranty of any sort, express or implied. K.U.Leuven makes no
* representation that the use of the software will not infringe any
* patent or proprietary right of third parties. User will indemnify
* K.U.Leuven and hold K.U.Leuven harmless from any claims or liabilities
* which may arise as a result of its use of the software. In no
* circumstances K.U.Leuven R&D will be held liable for any deficiency,
* fault or other mishappening with regard to the use or performance of
* the software.
* 3. User agrees to give due credit to K.U.Leuven in scientific publications
* or communications in relation with the use of the RIPEMD-160 software
* as follows: RIPEMD-160 software written by Antoon Bosselaers,
* available at http://www.esat.kuleuven.be/~cosicart/ps/AB-9601/.
*
\********************************************************************/
/* header files */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "rmd160.h"
/********************************************************************/
void MDinit(dword *MDbuf)
{
MDbuf[0] = 0x67452301UL;
MDbuf[1] = 0xefcdab89UL;
MDbuf[2] = 0x98badcfeUL;
MDbuf[3] = 0x10325476UL;
MDbuf[4] = 0xc3d2e1f0UL;
return;
}
/********************************************************************/
void compress(dword *MDbuf, dword *X)
{
dword aa = MDbuf[0], bb = MDbuf[1], cc = MDbuf[2],
dd = MDbuf[3], ee = MDbuf[4];
dword aaa = MDbuf[0], bbb = MDbuf[1], ccc = MDbuf[2],
ddd = MDbuf[3], eee = MDbuf[4];
/* round 1 */
FF(aa, bb, cc, dd, ee, X[ 0], 11);
FF(ee, aa, bb, cc, dd, X[ 1], 14);
FF(dd, ee, aa, bb, cc, X[ 2], 15);
FF(cc, dd, ee, aa, bb, X[ 3], 12);
FF(bb, cc, dd, ee, aa, X[ 4], 5);
FF(aa, bb, cc, dd, ee, X[ 5], 8);
FF(ee, aa, bb, cc, dd, X[ 6], 7);
FF(dd, ee, aa, bb, cc, X[ 7], 9);
FF(cc, dd, ee, aa, bb, X[ 8], 11);
FF(bb, cc, dd, ee, aa, X[ 9], 13);
FF(aa, bb, cc, dd, ee, X[10], 14);
FF(ee, aa, bb, cc, dd, X[11], 15);
FF(dd, ee, aa, bb, cc, X[12], 6);
FF(cc, dd, ee, aa, bb, X[13], 7);
FF(bb, cc, dd, ee, aa, X[14], 9);
FF(aa, bb, cc, dd, ee, X[15], 8);
/* round 2 */
GG(ee, aa, bb, cc, dd, X[ 7], 7);
GG(dd, ee, aa, bb, cc, X[ 4], 6);
GG(cc, dd, ee, aa, bb, X[13], 8);
GG(bb, cc, dd, ee, aa, X[ 1], 13);
GG(aa, bb, cc, dd, ee, X[10], 11);
GG(ee, aa, bb, cc, dd, X[ 6], 9);
GG(dd, ee, aa, bb, cc, X[15], 7);
GG(cc, dd, ee, aa, bb, X[ 3], 15);
GG(bb, cc, dd, ee, aa, X[12], 7);
GG(aa, bb, cc, dd, ee, X[ 0], 12);
GG(ee, aa, bb, cc, dd, X[ 9], 15);
GG(dd, ee, aa, bb, cc, X[ 5], 9);
GG(cc, dd, ee, aa, bb, X[ 2], 11);
GG(bb, cc, dd, ee, aa, X[14], 7);
GG(aa, bb, cc, dd, ee, X[11], 13);
GG(ee, aa, bb, cc, dd, X[ 8], 12);
/* round 3 */
HH(dd, ee, aa, bb, cc, X[ 3], 11);
HH(cc, dd, ee, aa, bb, X[10], 13);
HH(bb, cc, dd, ee, aa, X[14], 6);
HH(aa, bb, cc, dd, ee, X[ 4], 7);
HH(ee, aa, bb, cc, dd, X[ 9], 14);
HH(dd, ee, aa, bb, cc, X[15], 9);
HH(cc, dd, ee, aa, bb, X[ 8], 13);
HH(bb, cc, dd, ee, aa, X[ 1], 15);
HH(aa, bb, cc, dd, ee, X[ 2], 14);
HH(ee, aa, bb, cc, dd, X[ 7], 8);
HH(dd, ee, aa, bb, cc, X[ 0], 13);
HH(cc, dd, ee, aa, bb, X[ 6], 6);
HH(bb, cc, dd, ee, aa, X[13], 5);
HH(aa, bb, cc, dd, ee, X[11], 12);
HH(ee, aa, bb, cc, dd, X[ 5], 7);
HH(dd, ee, aa, bb, cc, X[12], 5);
/* round 4 */
II(cc, dd, ee, aa, bb, X[ 1], 11);
II(bb, cc, dd, ee, aa, X[ 9], 12);
II(aa, bb, cc, dd, ee, X[11], 14);
II(ee, aa, bb, cc, dd, X[10], 15);
II(dd, ee, aa, bb, cc, X[ 0], 14);
II(cc, dd, ee, aa, bb, X[ 8], 15);
II(bb, cc, dd, ee, aa, X[12], 9);
II(aa, bb, cc, dd, ee, X[ 4], 8);
II(ee, aa, bb, cc, dd, X[13], 9);
II(dd, ee, aa, bb, cc, X[ 3], 14);
II(cc, dd, ee, aa, bb, X[ 7], 5);
II(bb, cc, dd, ee, aa, X[15], 6);
II(aa, bb, cc, dd, ee, X[14], 8);
II(ee, aa, bb, cc, dd, X[ 5], 6);
II(dd, ee, aa, bb, cc, X[ 6], 5);
II(cc, dd, ee, aa, bb, X[ 2], 12);
/* round 5 */
JJ(bb, cc, dd, ee, aa, X[ 4], 9);
JJ(aa, bb, cc, dd, ee, X[ 0], 15);
JJ(ee, aa, bb, cc, dd, X[ 5], 5);
JJ(dd, ee, aa, bb, cc, X[ 9], 11);
JJ(cc, dd, ee, aa, bb, X[ 7], 6);
JJ(bb, cc, dd, ee, aa, X[12], 8);
JJ(aa, bb, cc, dd, ee, X[ 2], 13);
JJ(ee, aa, bb, cc, dd, X[10], 12);
JJ(dd, ee, aa, bb, cc, X[14], 5);
JJ(cc, dd, ee, aa, bb, X[ 1], 12);
JJ(bb, cc, dd, ee, aa, X[ 3], 13);
JJ(aa, bb, cc, dd, ee, X[ 8], 14);
JJ(ee, aa, bb, cc, dd, X[11], 11);
JJ(dd, ee, aa, bb, cc, X[ 6], 8);
JJ(cc, dd, ee, aa, bb, X[15], 5);
JJ(bb, cc, dd, ee, aa, X[13], 6);
/* parallel round 1 */
JJJ(aaa, bbb, ccc, ddd, eee, X[ 5], 8);
JJJ(eee, aaa, bbb, ccc, ddd, X[14], 9);
JJJ(ddd, eee, aaa, bbb, ccc, X[ 7], 9);
JJJ(ccc, ddd, eee, aaa, bbb, X[ 0], 11);
JJJ(bbb, ccc, ddd, eee, aaa, X[ 9], 13);
JJJ(aaa, bbb, ccc, ddd, eee, X[ 2], 15);
JJJ(eee, aaa, bbb, ccc, ddd, X[11], 15);
JJJ(ddd, eee, aaa, bbb, ccc, X[ 4], 5);
JJJ(ccc, ddd, eee, aaa, bbb, X[13], 7);
JJJ(bbb, ccc, ddd, eee, aaa, X[ 6], 7);
JJJ(aaa, bbb, ccc, ddd, eee, X[15], 8);
JJJ(eee, aaa, bbb, ccc, ddd, X[ 8], 11);
JJJ(ddd, eee, aaa, bbb, ccc, X[ 1], 14);
JJJ(ccc, ddd, eee, aaa, bbb, X[10], 14);
JJJ(bbb, ccc, ddd, eee, aaa, X[ 3], 12);
JJJ(aaa, bbb, ccc, ddd, eee, X[12], 6);
/* parallel round 2 */
III(eee, aaa, bbb, ccc, ddd, X[ 6], 9);
III(ddd, eee, aaa, bbb, ccc, X[11], 13);
III(ccc, ddd, eee, aaa, bbb, X[ 3], 15);
III(bbb, ccc, ddd, eee, aaa, X[ 7], 7);
III(aaa, bbb, ccc, ddd, eee, X[ 0], 12);
III(eee, aaa, bbb, ccc, ddd, X[13], 8);
III(ddd, eee, aaa, bbb, ccc, X[ 5], 9);
III(ccc, ddd, eee, aaa, bbb, X[10], 11);
III(bbb, ccc, ddd, eee, aaa, X[14], 7);
III(aaa, bbb, ccc, ddd, eee, X[15], 7);
III(eee, aaa, bbb, ccc, ddd, X[ 8], 12);
III(ddd, eee, aaa, bbb, ccc, X[12], 7);
III(ccc, ddd, eee, aaa, bbb, X[ 4], 6);
III(bbb, ccc, ddd, eee, aaa, X[ 9], 15);
III(aaa, bbb, ccc, ddd, eee, X[ 1], 13);
III(eee, aaa, bbb, ccc, ddd, X[ 2], 11);
/* parallel round 3 */
HHH(ddd, eee, aaa, bbb, ccc, X[15], 9);
HHH(ccc, ddd, eee, aaa, bbb, X[ 5], 7);
HHH(bbb, ccc, ddd, eee, aaa, X[ 1], 15);
HHH(aaa, bbb, ccc, ddd, eee, X[ 3], 11);
HHH(eee, aaa, bbb, ccc, ddd, X[ 7], 8);
HHH(ddd, eee, aaa, bbb, ccc, X[14], 6);
HHH(ccc, ddd, eee, aaa, bbb, X[ 6], 6);
HHH(bbb, ccc, ddd, eee, aaa, X[ 9], 14);
HHH(aaa, bbb, ccc, ddd, eee, X[11], 12);
HHH(eee, aaa, bbb, ccc, ddd, X[ 8], 13);
HHH(ddd, eee, aaa, bbb, ccc, X[12], 5);
HHH(ccc, ddd, eee, aaa, bbb, X[ 2], 14);
HHH(bbb, ccc, ddd, eee, aaa, X[10], 13);
HHH(aaa, bbb, ccc, ddd, eee, X[ 0], 13);
HHH(eee, aaa, bbb, ccc, ddd, X[ 4], 7);
HHH(ddd, eee, aaa, bbb, ccc, X[13], 5);
/* parallel round 4 */
GGG(ccc, ddd, eee, aaa, bbb, X[ 8], 15);
GGG(bbb, ccc, ddd, eee, aaa, X[ 6], 5);
GGG(aaa, bbb, ccc, ddd, eee, X[ 4], 8);
GGG(eee, aaa, bbb, ccc, ddd, X[ 1], 11);
GGG(ddd, eee, aaa, bbb, ccc, X[ 3], 14);
GGG(ccc, ddd, eee, aaa, bbb, X[11], 14);
GGG(bbb, ccc, ddd, eee, aaa, X[15], 6);
GGG(aaa, bbb, ccc, ddd, eee, X[ 0], 14);
GGG(eee, aaa, bbb, ccc, ddd, X[ 5], 6);
GGG(ddd, eee, aaa, bbb, ccc, X[12], 9);
GGG(ccc, ddd, eee, aaa, bbb, X[ 2], 12);
GGG(bbb, ccc, ddd, eee, aaa, X[13], 9);
GGG(aaa, bbb, ccc, ddd, eee, X[ 9], 12);
GGG(eee, aaa, bbb, ccc, ddd, X[ 7], 5);
GGG(ddd, eee, aaa, bbb, ccc, X[10], 15);
GGG(ccc, ddd, eee, aaa, bbb, X[14], 8);
/* parallel round 5 */
FFF(bbb, ccc, ddd, eee, aaa, X[12] , 8);
FFF(aaa, bbb, ccc, ddd, eee, X[15] , 5);
FFF(eee, aaa, bbb, ccc, ddd, X[10] , 12);
FFF(ddd, eee, aaa, bbb, ccc, X[ 4] , 9);
FFF(ccc, ddd, eee, aaa, bbb, X[ 1] , 12);
FFF(bbb, ccc, ddd, eee, aaa, X[ 5] , 5);
FFF(aaa, bbb, ccc, ddd, eee, X[ 8] , 14);
FFF(eee, aaa, bbb, ccc, ddd, X[ 7] , 6);
FFF(ddd, eee, aaa, bbb, ccc, X[ 6] , 8);
FFF(ccc, ddd, eee, aaa, bbb, X[ 2] , 13);
FFF(bbb, ccc, ddd, eee, aaa, X[13] , 6);
FFF(aaa, bbb, ccc, ddd, eee, X[14] , 5);
FFF(eee, aaa, bbb, ccc, ddd, X[ 0] , 15);
FFF(ddd, eee, aaa, bbb, ccc, X[ 3] , 13);
FFF(ccc, ddd, eee, aaa, bbb, X[ 9] , 11);
FFF(bbb, ccc, ddd, eee, aaa, X[11] , 11);
/* combine results */
ddd += cc + MDbuf[1]; /* final result for MDbuf[0] */
MDbuf[1] = MDbuf[2] + dd + eee;
MDbuf[2] = MDbuf[3] + ee + aaa;
MDbuf[3] = MDbuf[4] + aa + bbb;
MDbuf[4] = MDbuf[0] + bb + ccc;
MDbuf[0] = ddd;
return;
}
/********************************************************************/
void MDfinish(dword *MDbuf, byte *strptr, dword lswlen, dword mswlen)
{
unsigned int i; /* counter */
dword X[16]; /* message words */
memset(X, 0, 16*sizeof(dword));
/* put bytes from strptr into X */
for (i=0; i<(lswlen&63); i++) {
/* byte i goes into word X[i div 4] at pos. 8*(i mod 4) */
X[i>>2] ^= (dword) *strptr++ << (8 * (i&3));
}
/* append the bit m_n == 1 */
X[(lswlen>>2)&15] ^= (dword)1 << (8*(lswlen&3) + 7);
if ((lswlen & 63) > 55) {
/* length goes to next block */
compress(MDbuf, X);
memset(X, 0, 16*sizeof(dword));
}
/* append length in bits*/
X[14] = lswlen << 3;
X[15] = (lswlen >> 29) | (mswlen << 3);
compress(MDbuf, X);
return;
}
/************************ end of file rmd160.c **********************/
+154
View File
@@ -0,0 +1,154 @@
/********************************************************************\
*
* FILE: rmd160.h
*
* CONTENTS: Header file for a sample C-implementation of the
* RIPEMD-160 hash-function.
* TARGET: any computer with an ANSI C compiler
*
* AUTHOR: Antoon Bosselaers, ESAT-COSIC
* DATE: 1 March 1996
* VERSION: 1.0
*
* Copyright (c) Katholieke Universiteit Leuven
* 1996, All Rights Reserved
*
* Conditions for use of the RIPEMD-160 Software
*
* The RIPEMD-160 software is freely available for use under the terms and
* conditions described hereunder, which shall be deemed to be accepted by
* any user of the software and applicable on any use of the software:
*
* 1. K.U.Leuven Department of Electrical Engineering-ESAT/COSIC shall for
* all purposes be considered the owner of the RIPEMD-160 software and of
* all copyright, trade secret, patent or other intellectual property
* rights therein.
* 2. The RIPEMD-160 software is provided on an "as is" basis without
* warranty of any sort, express or implied. K.U.Leuven makes no
* representation that the use of the software will not infringe any
* patent or proprietary right of third parties. User will indemnify
* K.U.Leuven and hold K.U.Leuven harmless from any claims or liabilities
* which may arise as a result of its use of the software. In no
* circumstances K.U.Leuven R&D will be held liable for any deficiency,
* fault or other mishappening with regard to the use or performance of
* the software.
* 3. User agrees to give due credit to K.U.Leuven in scientific publications
* or communications in relation with the use of the RIPEMD-160 software
* as follows: RIPEMD-160 software written by Antoon Bosselaers,
* available at http://www.esat.kuleuven.be/~cosicart/ps/AB-9601/.
*
\********************************************************************/
#ifndef RMD160H /* make sure this file is read only once */
#define RMD160H
/********************************************************************/
/* typedef 8 and 32 bit types, resp. */
/* adapt these, if necessary,
for your operating system and compiler */
typedef unsigned char byte;
typedef unsigned long dword;
/********************************************************************/
/* macro definitions */
/* collect four bytes into one word: */
#define BYTES_TO_DWORD(strptr) \
(((dword) *((strptr)+3) << 24) | \
((dword) *((strptr)+2) << 16) | \
((dword) *((strptr)+1) << 8) | \
((dword) *(strptr)))
/* ROL(x, n) cyclically rotates x over n bits to the left */
/* x must be of an unsigned 32 bits type and 0 <= n < 32. */
#define ROL(x, n) (((x) << (n)) | ((x) >> (32-(n))))
/* the five basic functions F(), G() and H() */
#define F(x, y, z) ((x) ^ (y) ^ (z))
#define G(x, y, z) (((x) & (y)) | (~(x) & (z)))
#define H(x, y, z) (((x) | ~(y)) ^ (z))
#define I(x, y, z) (((x) & (z)) | ((y) & ~(z)))
#define J(x, y, z) ((x) ^ ((y) | ~(z)))
/* the ten basic operations FF() through III() */
#define FF(a, b, c, d, e, x, s) {\
(a) += F((b), (c), (d)) + (x);\
(a) = ROL((a), (s)) + (e);\
(c) = ROL((c), 10);\
}
#define GG(a, b, c, d, e, x, s) {\
(a) += G((b), (c), (d)) + (x) + 0x5a827999UL;\
(a) = ROL((a), (s)) + (e);\
(c) = ROL((c), 10);\
}
#define HH(a, b, c, d, e, x, s) {\
(a) += H((b), (c), (d)) + (x) + 0x6ed9eba1UL;\
(a) = ROL((a), (s)) + (e);\
(c) = ROL((c), 10);\
}
#define II(a, b, c, d, e, x, s) {\
(a) += I((b), (c), (d)) + (x) + 0x8f1bbcdcUL;\
(a) = ROL((a), (s)) + (e);\
(c) = ROL((c), 10);\
}
#define JJ(a, b, c, d, e, x, s) {\
(a) += J((b), (c), (d)) + (x) + 0xa953fd4eUL;\
(a) = ROL((a), (s)) + (e);\
(c) = ROL((c), 10);\
}
#define FFF(a, b, c, d, e, x, s) {\
(a) += F((b), (c), (d)) + (x);\
(a) = ROL((a), (s)) + (e);\
(c) = ROL((c), 10);\
}
#define GGG(a, b, c, d, e, x, s) {\
(a) += G((b), (c), (d)) + (x) + 0x7a6d76e9UL;\
(a) = ROL((a), (s)) + (e);\
(c) = ROL((c), 10);\
}
#define HHH(a, b, c, d, e, x, s) {\
(a) += H((b), (c), (d)) + (x) + 0x6d703ef3UL;\
(a) = ROL((a), (s)) + (e);\
(c) = ROL((c), 10);\
}
#define III(a, b, c, d, e, x, s) {\
(a) += I((b), (c), (d)) + (x) + 0x5c4dd124UL;\
(a) = ROL((a), (s)) + (e);\
(c) = ROL((c), 10);\
}
#define JJJ(a, b, c, d, e, x, s) {\
(a) += J((b), (c), (d)) + (x) + 0x50a28be6UL;\
(a) = ROL((a), (s)) + (e);\
(c) = ROL((c), 10);\
}
/********************************************************************/
/* function prototypes */
void MDinit(dword *MDbuf);
/*
* initializes MDbuffer to "magic constants"
*/
void compress(dword *MDbuf, dword *X);
/*
* the compression function.
* transforms MDbuf using message bytes X[0] through X[15]
*/
void MDfinish(dword *MDbuf, byte *strptr, dword lswlen, dword mswlen);
/*
* puts bytes from strptr into X and pad out; appends length
* and finally, compresses the last block(s)
* note: length in bits == 8 * (lswlen + 2^32 mswlen).
* note: there are (lswlen mod 64) bytes left in strptr.
*/
#endif /* RMD160H */
/*********************** end of file rmd160.h ***********************/
+148
View File
@@ -0,0 +1,148 @@
*asm:
%{G*} %(endian_spec) %{mips1} %{mips2} %{mips3} %{mips4} %{mips32} %{mips32r2} %{mips64} %{mips16} %{mno-mips16:-no-mips16} %{mips3d} %{mno-mips3d:-no-mips3d} %{mdmx} %{mno-mdmx:-no-mdmx} %{mdsp} %{mno-dsp} %{mdspr2} %{mno-dspr2} %{msmartmips} %{mno-smartmips} %{mmt} %{mno-mt} %{mfix-vr4120} %{mfix-vr4130} %(subtarget_asm_optimizing_spec) %(subtarget_asm_debugging_spec) %{mabi=*} %{!mabi*: %(asm_abi_default_spec)} %{mgp32} %{mgp64} %{march=*} %{mxgot:-xgot} %{mfp32} %{mfp64} %{mshared} %{mno-shared} %{msym32} %{mno-sym32} %{mtune=*} %{v} %(subtarget_asm_spec)
*asm_debug:
%{gstabs*:--gstabs}%{!gstabs*:%{g*:--gdwarf2}} %{fdebug-prefix-map=*:--debug-prefix-map %*}
*asm_final:
*asm_options:
%{--target-help:%:print-asm-header()} %a %Y %{c:%W{o*}%{!o*:-o %w%b%O}}%{!c:-o %d%w%u%O}
*invoke_as:
%{!S:-o %|.s |
as %(asm_options) %m.s %A }
*cpp:
%(subtarget_cpp_spec)
*cpp_options:
%(cpp_unique_options) %1 %{m*} %{std*&ansi&trigraphs} %{W*&pedantic*} %{w} %{f*} %{g*:%{!g0:%{!fno-working-directory:-fworking-directory}}} %{O*} %{undef} %{save-temps:-fpch-preprocess}
*cpp_debug_options:
%{d*}
*cpp_unique_options:
%{C|CC:%{!E:%eGCC does not support -C or -CC without -E}} %{!Q:-quiet} %{nostdinc*} %{C} %{CC} %{v} %{I*&F*} %{P} %I %{MD:-MD %{!o:%b.d}%{o*:%.d%*}} %{MMD:-MMD %{!o:%b.d}%{o*:%.d%*}} %{M} %{MM} %{MF*} %{MG} %{MP} %{MQ*} %{MT*} %{!E:%{!M:%{!MM:%{!MT:%{!MQ:%{MD|MMD:%{o*:-MQ %*}}}}}}} %{remap} %{g3|ggdb3|gstabs3|gcoff3|gxcoff3|gvms3:-dD} %{H} %C %{D*&U*&A*} %{i*} %Z %i %{fmudflap:-D_MUDFLAP -include mf-runtime.h} %{fmudflapth:-D_MUDFLAP -D_MUDFLAPTH -include mf-runtime.h} %{E|M|MM:%W{o*}}
*trad_capable_cpp:
cc1 -E %{traditional|ftraditional|traditional-cpp:-traditional-cpp}
*cc1:
%{gline:%{!g:%{!g0:%{!g1:%{!g2: -g1}}}}} %{G*} %{EB:-meb} %{EL:-mel} %{EB:%{EL:%emay not use both -EB and -EL}} %{save-temps: } %(subtarget_cc1_spec)
*cc1_options:
%{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}} %1 %{!Q:-quiet} -dumpbase %B %{d*} %{m*} %{a*} %{c|S:%{o*:-auxbase-strip %*}%{!o*:-auxbase %b}}%{!c:%{!S:-auxbase %b}} %{g*} %{O*} %{W*&pedantic*} %{w} %{std*&ansi&trigraphs} %{v:-version} %{pg:-p} %{p} %{f*} %{undef} %{Qn:-fno-ident} %{--help:--help} %{--target-help:--target-help} %{--help=*:--help=%(VALUE)} %{!fsyntax-only:%{S:%W{o*}%{!o*:-o %b.s}}} %{fsyntax-only:-o %j} %{-param*} %{fmudflap|fmudflapth:-fno-builtin -fno-merge-constants} %{coverage:-fprofile-arcs -ftest-coverage}
*cc1plus:
*link_gcc_c_sequence:
%G %L %G
*link_ssp:
%{fstack-protector|fstack-protector-all:-lssp_nonshared -lssp}
*endfile:
crtend%O%s crtn%O%s
*link:
%(endian_spec) %{G*} %{mips1} %{mips2} %{mips3} %{mips4} %{mips32} %{mips32r2} %{mips64} %{bestGnum} %{shared} %{non_shared}
*lib:
*mfwrap:
%{static: %{fmudflap|fmudflapth: --wrap=malloc --wrap=free --wrap=calloc --wrap=realloc --wrap=mmap --wrap=munmap --wrap=alloca} %{fmudflapth: --wrap=pthread_create}} %{fmudflap|fmudflapth: --wrap=main}
*mflib:
%{fmudflap|fmudflapth: -export-dynamic}
*link_gomp:
*libgcc:
-lgcc
*startfile:
crti%O%s crtbegin%O%s
*switches_need_spaces:
*cross_compile:
1
*version:
4.3.3
*multilib:
. !msoft-float !EL !EB;soft-float msoft-float !EL !EB;el !msoft-float EL !EB;eb !msoft-float !EL EB;soft-float/el msoft-float EL !EB;soft-float/eb msoft-float !EL EB;
*multilib_defaults:
EL mips1 mabi=32
*multilib_extra:
*multilib_matches:
mel EL;meb EB;m4650 msingle-float;msoft-float msoft-float;EL EL;EB EB;
*multilib_exclusions:
*multilib_options:
msoft-float EL/EB
*linker:
collect2
*link_libgcc:
%D
*md_exec_prefix:
*md_startfile_prefix:
*md_startfile_prefix_1:
*startfile_prefix_spec:
*sysroot_spec:
--sysroot=%R
*sysroot_suffix_spec:
*sysroot_hdrs_suffix_spec:
*subtarget_cc1_spec:
*subtarget_cpp_spec:
*subtarget_asm_optimizing_spec:
%{noasmopt:-O0} %{!noasmopt:%{O:-O2} %{O1:-O2} %{O2:-O2} %{O3:-O3}}
*subtarget_asm_debugging_spec:
%{g} %{g0} %{g1} %{g2} %{g3} %{ggdb:-g} %{ggdb0:-g0} %{ggdb1:-g1} %{ggdb2:-g2} %{ggdb3:-g3} %{gstabs:-g} %{gstabs0:-g0} %{gstabs1:-g1} %{gstabs2:-g2} %{gstabs3:-g3} %{gstabs+:-g} %{gstabs+0:-g0} %{gstabs+1:-g1} %{gstabs+2:-g2} %{gstabs+3:-g3} %{gcoff:-g} %{gcoff0:-g0} %{gcoff1:-g1} %{gcoff2:-g2} %{gcoff3:-g3} %{gcoff*:-mdebug} %{!gcoff*:-no-mdebug}
*subtarget_asm_spec:
*asm_abi_default_spec:
-mabi=32
*endian_spec:
%{!EB:%{!meb:-EL}} %{EB|meb:-EB}
*link_command:
%{!fsyntax-only:%{!c:%{!M:%{!MM:%{!E:%{!S: %(linker) %l %{pie:-pie} %X %{o*} %{A} %{d} %{e*} %{m} %{N} %{n} %{r} %{s} %{t} %{u*} %{x} %{z} %{Z} %{!A:%{!nostdlib:%{!nostartfiles:%S}}} %{static:} %{L*} %(mfwrap) %(link_libgcc) %o %{fopenmp|ftree-parallelize-loops=*:%:include(libgomp.spec)%(link_gomp)} %(mflib) %{fprofile-arcs|fprofile-generate|coverage:-lgcov} %{!nostdlib:%{!nodefaultlibs:%(link_ssp) %(link_gcc_c_sequence)}} %{!A:%{!nostdlib:%{!nostartfiles:%E}}} %{T*} }}}}}}
Binary file not shown.
+882
View File
@@ -0,0 +1,882 @@
/*
* T T C P . C
*
* Test TCP connection. Makes a connection on port 5001
* and transfers fabricated buffers or data copied from stdin.
*
* Usable on 4.2, 4.3, and 4.1a systems by defining one of
* BSD42 BSD43 (BSD41a)
* Machines using System V with BSD sockets should define SYSV.
*
* Modified for operation under 4.2BSD, 18 Dec 84
* T.C. Slattery, USNA
* Minor improvements, Mike Muuss and Terry Slattery, 16-Oct-85.
* Modified in 1989 at Silicon Graphics, Inc.
* catch SIGPIPE to be able to print stats when receiver has died
* for tcp, don't look for sentinel during reads to allow small transfers
* increased default buffer size to 8K, nbuf to 2K to transfer 16MB
* moved default port to 5001, beyond IPPORT_USERRESERVED
* make sinkmode default because it is more popular,
* -s now means don't sink/source
* count number of read/write system calls to see effects of
* blocking from full socket buffers
* for tcp, -D option turns off buffered writes (sets TCP_NODELAY sockopt)
* buffer alignment options, -A and -O
* print stats in a format that's a bit easier to use with grep & awk
* for SYSV, mimic BSD routines to use most of the existing timing code
* Modified by Steve Miller of the University of Maryland, College Park
* -b sets the socket buffer size (SO_SNDBUF/SO_RCVBUF)
* Modified Sept. 1989 at Silicon Graphics, Inc.
* restored -s sense at request of tcs@brl
* Modified Oct. 1991 at Silicon Graphics, Inc.
* use getopt(3) for option processing, add -f and -T options.
* SGI IRIX 3.3 and 4.0 releases don't need #define SYSV.
*
* Distribution Status -
* Public Domain. Distribution Unlimited.
*
* $Id: ttcp.c,v 1.7 2008/08/21 16:40:18 joel Exp $
*/
#ifndef lint
/* static char RCSid[] = "ttcp.c $Revision: 1.7 $"; */
#endif
#define BSD43
/* #define BSD42 */
/* #define BSD41a */
/* #define SYSV */ /* required on SGI IRIX releases before 3.3 */
#define ENABLE_NANOSLEEP_DELAY
#include <stdio.h>
#include <signal.h>
#include <ctype.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <string.h>
#include <sys/time.h> /* struct timeval */
#include <unistd.h>
#include <stdlib.h>
#if defined(SYSV)
#include <sys/times.h>
#include <sys/param.h>
struct rusage {
struct timeval ru_utime, ru_stime;
};
#define RUSAGE_SELF 0
#else
#include <sys/resource.h>
#endif
struct sockaddr_in sinme;
struct sockaddr_in sinhim;
struct sockaddr_in frominet;
/* these make it easier to avoid warnings */
struct sockaddr *sinhim_p = (struct sockaddr *) &sinhim;
struct sockaddr *sinme_p = (struct sockaddr *) &sinme;
struct sockaddr *frominet_p = (struct sockaddr *) &frominet;
int domain;
socklen_t fromlen;
int fd; /* fd of network socket */
int buflen = 8 * 1024; /* length of buffer */
char *buf; /* ptr to dynamic buffer */
int nbuf = 2 * 1024; /* number of buffers to send in sinkmode */
int bufoffset = 0; /* align buffer to this */
int bufalign = 16*1024; /* modulo this */
int udp = 0; /* 0 = tcp, !0 = udp */
int options = 0; /* socket options */
int one = 1; /* for 4.3 BSD style setsockopt() */
short port = 5001; /* TCP port number */
char *host; /* ptr to name of host */
int trans; /* 0=receive, !0=transmit mode */
int sinkmode = 0; /* 0=normal I/O, !0=sink/source mode */
int verbose = 0; /* 0=print basic info, 1=print cpu rate, proc
* resource usage. */
int nodelay = 0; /* set TCP_NODELAY socket option */
int b_flag = 0; /* use mread() */
int sockbufsize = 0; /* socket buffer size to use */
char fmt = 'K'; /* output format: k = kilobits, K = kilobytes,
* m = megabits, M = megabytes,
* g = gigabits, G = gigabytes */
int touchdata = 0; /* access data after reading */
long milliseconds = 0; /* delay in milliseconds */
struct hostent *addr;
extern int errno;
extern int optind;
extern char *optarg;
char Usage[] = "\
Usage: ttcp -t [-options] host [ < in ]\n\
ttcp -r [-options > out]\n\
Common options:\n\
-l ## length of bufs read from or written to network (default 8192)\n\
-u use UDP instead of TCP\n\
-p ## port number to send to or listen at (default 5001)\n\
-s -t: source a pattern to network\n\
-r: sink (discard) all data from network\n\
-A align the start of buffers to this modulus (default 16384)\n\
-O start buffers at this offset from the modulus (default 0)\n\
-v verbose: print more statistics\n\
-d set SO_DEBUG socket option\n\
-b ## set socket buffer size (if supported)\n\
-f X format for rate: k,K = kilo{bit,byte}; m,M = mega; g,G = giga\n\
Options specific to -t:\n\
-n## number of source bufs written to network (default 2048)\n\
-D don't buffer TCP writes (sets TCP_NODELAY socket option)\n\
Options specific to -r:\n\
-B for -s, only output full blocks as specified by -l (for TAR)\n\
-T \"touch\": access each byte as it's read\n\
-m ## delay for specified milliseconds between each write\n\
";
char stats[128];
double nbytes; /* bytes on net */
unsigned long numCalls; /* # of I/O system calls */
double cput, realt; /* user, real time (seconds) */
void err();
void mes();
void pattern();
void prep_timer();
double read_timer();
int Nread();
int Nwrite();
void delay();
int mread();
char *outfmt();
void
sigpipe()
{
}
void millisleep(long msec)
{
#if defined(ENABLE_NANOSLEEP_DELAY)
struct timespec req;
req.tv_sec = msec / 1000;
req.tv_nsec = (msec % 1000) * 1000000;
nanosleep( &req, NULL );
#endif
}
int main(argc,argv)
int argc;
char **argv;
{
unsigned long addr_tmp;
int c;
if (argc < 2) goto usage;
while ((c = getopt(argc, argv, "drstuvBDTb:f:l:m:n:p:A:O:")) != -1) {
switch (c) {
case 'B':
b_flag = 1;
break;
case 't':
trans = 1;
break;
case 'r':
trans = 0;
break;
case 'd':
options |= SO_DEBUG;
break;
case 'D':
#ifdef TCP_NODELAY
nodelay = 1;
#else
fprintf(stderr,
"ttcp: -D option ignored: TCP_NODELAY socket option not supported\n");
#endif
break;
case 'm':
milliseconds = atoi(optarg);
#if !defined(ENABLE_NANOSLEEP_DELAY)
fprintf(stderr, "millisecond delay disabled\n");
#endif
break;
case 'n':
nbuf = atoi(optarg);
break;
case 'l':
buflen = atoi(optarg);
break;
case 's':
sinkmode = !sinkmode;
break;
case 'p':
port = atoi(optarg);
break;
case 'u':
udp = 1;
break;
case 'v':
verbose = 1;
break;
case 'A':
bufalign = atoi(optarg);
break;
case 'O':
bufoffset = atoi(optarg);
break;
case 'b':
#if defined(SO_SNDBUF) || defined(SO_RCVBUF)
sockbufsize = atoi(optarg);
#else
fprintf(stderr,
"ttcp: -b option ignored: SO_SNDBUF/SO_RCVBUF socket options not supported\n");
#endif
break;
case 'f':
fmt = *optarg;
break;
case 'T':
touchdata = 1;
break;
default:
goto usage;
}
}
if(trans) {
/* xmitr */
if (optind == argc)
goto usage;
bzero((char *)&sinhim, sizeof(sinhim));
host = argv[optind];
if (atoi(host) > 0 ) {
/* Numeric */
sinhim.sin_family = AF_INET;
#if defined(cray)
addr_tmp = inet_addr(host);
sinhim.sin_addr = addr_tmp;
#else
sinhim.sin_addr.s_addr = inet_addr(host);
#endif
} else {
if ((addr=gethostbyname(host)) == NULL)
err("bad hostname");
sinhim.sin_family = addr->h_addrtype;
bcopy(addr->h_addr,(char*)&addr_tmp, addr->h_length);
#if defined(cray)
sinhim.sin_addr = addr_tmp;
#else
sinhim.sin_addr.s_addr = addr_tmp;
#endif /* cray */
}
sinhim.sin_port = htons(port);
sinme.sin_port = 0; /* free choice */
} else {
/* rcvr */
sinme.sin_port = htons(port);
}
if (udp && buflen < 5) {
buflen = 5; /* send more than the sentinel size */
}
if ( (buf = (char *)malloc(buflen+bufalign)) == (char *)NULL)
err("malloc");
if (bufalign != 0)
buf +=(bufalign - ((int)buf % bufalign) + bufoffset) % bufalign;
if (trans) {
fprintf(stdout,
"ttcp-t: buflen=%d, nbuf=%d, align=%d/%d, port=%d",
buflen, nbuf, bufalign, bufoffset, port);
if (sockbufsize)
fprintf(stdout, ", sockbufsize=%d", sockbufsize);
fprintf(stdout, " %s -> %s\n", udp?"udp":"tcp", host);
} else {
fprintf(stdout,
"ttcp-r: buflen=%d, nbuf=%d, align=%d/%d, port=%d",
buflen, nbuf, bufalign, bufoffset, port);
if (sockbufsize)
fprintf(stdout, ", sockbufsize=%d", sockbufsize);
fprintf(stdout, " %s\n", udp?"udp":"tcp");
}
if ((fd = socket(AF_INET, udp?SOCK_DGRAM:SOCK_STREAM, 0)) < 0)
err("socket");
mes("socket");
if (bind(fd, sinme_p, sizeof(sinme)) < 0)
err("bind");
#if defined(SO_SNDBUF) || defined(SO_RCVBUF)
if (sockbufsize) {
if (trans) {
if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &sockbufsize,
sizeof sockbufsize) < 0)
err("setsockopt: sndbuf");
mes("sndbuf");
} else {
if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &sockbufsize,
sizeof sockbufsize) < 0)
err("setsockopt: rcvbuf");
mes("rcvbuf");
}
}
#endif
if (!udp) {
#if !defined(__rtems__)
signal(SIGPIPE, sigpipe);
#endif
if (trans) {
/* We are the client if transmitting */
if (options) {
#if defined(BSD42)
if( setsockopt(fd, SOL_SOCKET, options, 0, 0) < 0)
#else /* BSD43 */
if( setsockopt(fd, SOL_SOCKET, options, &one, sizeof(one)) < 0)
#endif
err("setsockopt");
}
#ifdef TCP_NODELAY
if (nodelay) {
struct protoent *p;
p = getprotobyname("tcp");
if( p && setsockopt(fd, p->p_proto, TCP_NODELAY,
&one, sizeof(one)) < 0)
err("setsockopt: nodelay");
mes("nodelay");
}
#endif
if(connect(fd, sinhim_p, sizeof(sinhim) ) < 0)
err("connect");
mes("connect");
} else {
/* otherwise, we are the server and
* should listen for the connections
*/
#if defined(ultrix) || defined(sgi)
listen(fd,1); /* workaround for alleged u4.2 bug */
#else
listen(fd,0); /* allow a queue of 0 */
#endif
if(options) {
#if defined(BSD42)
if( setsockopt(fd, SOL_SOCKET, options, 0, 0) < 0)
#else /* BSD43 */
if( setsockopt(fd, SOL_SOCKET, options, &one, sizeof(one)) < 0)
#endif
err("setsockopt");
}
fromlen = sizeof(frominet);
domain = AF_INET;
if((fd=accept(fd, frominet_p, &fromlen) ) < 0)
err("accept");
{ struct sockaddr_in peer;
socklen_t peerlen = sizeof(peer);
if (getpeername(fd, (struct sockaddr *) &peer,
&peerlen) < 0) {
err("getpeername");
}
fprintf(stderr,"ttcp-r: accept from %s\n",
inet_ntoa(peer.sin_addr));
}
}
}
prep_timer();
errno = 0;
if (sinkmode) {
register int cnt;
if (trans) {
pattern( buf, buflen );
if(udp) (void)Nwrite( fd, buf, 4 ); /* rcvr start */
while (nbuf-- && Nwrite(fd,buf,buflen) == buflen) {
nbytes += buflen;
millisleep( milliseconds );
}
if(udp) (void)Nwrite( fd, buf, 4 ); /* rcvr end */
} else {
if (udp) {
while ((cnt=Nread(fd,buf,buflen)) > 0) {
static int going = 0;
if( cnt <= 4 ) {
if( going )
break; /* "EOF" */
going = 1;
prep_timer();
} else {
nbytes += cnt;
}
}
} else {
while ((cnt=Nread(fd,buf,buflen)) > 0) {
nbytes += cnt;
}
}
}
} else {
register int cnt;
if (trans) {
while((cnt=read(0,buf,buflen)) > 0 &&
Nwrite(fd,buf,cnt) == cnt)
nbytes += cnt;
} else {
while((cnt=Nread(fd,buf,buflen)) > 0 &&
write(1,buf,cnt) == cnt)
nbytes += cnt;
}
}
if(errno) err("IO");
(void)read_timer(stats,sizeof(stats));
if(udp&&trans) {
(void)Nwrite( fd, buf, 4 ); /* rcvr end */
(void)Nwrite( fd, buf, 4 ); /* rcvr end */
(void)Nwrite( fd, buf, 4 ); /* rcvr end */
(void)Nwrite( fd, buf, 4 ); /* rcvr end */
}
if( cput <= 0.0 ) cput = 0.001;
if( realt <= 0.0 ) realt = 0.001;
fprintf(stdout,
"ttcp%s: %.0f bytes in %.2f real seconds = %s/sec +++\n",
trans?"-t":"-r",
nbytes, realt, outfmt(nbytes/realt));
if (verbose) {
fprintf(stdout,
"ttcp%s: %.0f bytes in %.2f CPU seconds = %s/cpu sec\n",
trans?"-t":"-r",
nbytes, cput, outfmt(nbytes/cput));
}
fprintf(stdout,
"ttcp%s: %ld I/O calls, msec/call = %.2f, calls/sec = %.2f\n",
trans?"-t":"-r",
numCalls,
1024.0 * realt/((double)numCalls),
((double)numCalls)/realt);
fprintf(stdout,"ttcp%s: %s\n", trans?"-t":"-r", stats);
if (verbose) {
fprintf(stdout,
"ttcp%s: buffer address %p\n",
trans?"-t":"-r",
buf);
}
exit(0);
usage:
fprintf(stderr,Usage);
exit(1);
return 0;
}
void
err(s)
char *s;
{
fprintf(stderr,"ttcp%s: ", trans?"-t":"-r");
perror(s);
fprintf(stderr,"errno=%d\n",errno);
exit(1);
}
void
mes(s)
char *s;
{
fprintf(stderr,"ttcp%s: %s\n", trans?"-t":"-r", s);
}
void pattern( cp, cnt )
register char *cp;
register int cnt;
{
register char c;
c = 0;
while( cnt-- > 0 ) {
while( !isprint((c&0x7F)) ) c++;
*cp++ = (c++&0x7F);
}
}
char *
outfmt(b)
double b;
{
static char obuf[50];
switch (fmt) {
case 'G':
sprintf(obuf, "%.2f GB", b / 1024.0 / 1024.0 / 1024.0);
break;
default:
case 'K':
sprintf(obuf, "%.2f KB", b / 1024.0);
break;
case 'M':
sprintf(obuf, "%.2f MB", b / 1024.0 / 1024.0);
break;
case 'g':
sprintf(obuf, "%.2f Gbit", b * 8.0 / 1024.0 / 1024.0 / 1024.0);
break;
case 'k':
sprintf(obuf, "%.2f Kbit", b * 8.0 / 1024.0);
break;
case 'm':
sprintf(obuf, "%.2f Mbit", b * 8.0 / 1024.0 / 1024.0);
break;
}
return obuf;
}
static struct timeval time0; /* Time at which timing started */
static struct rusage ru0; /* Resource utilization at the start */
static void prusage();
static void tvadd();
static void tvsub();
static void psecs();
#if defined(SYSV)
/*ARGSUSED*/
static
getrusage(ignored, ru)
int ignored;
register struct rusage *ru;
{
struct tms buf;
times(&buf);
/* Assumption: HZ <= 2147 (LONG_MAX/1000000) */
ru->ru_stime.tv_sec = buf.tms_stime / HZ;
ru->ru_stime.tv_usec = ((buf.tms_stime % HZ) * 1000000) / HZ;
ru->ru_utime.tv_sec = buf.tms_utime / HZ;
ru->ru_utime.tv_usec = ((buf.tms_utime % HZ) * 1000000) / HZ;
}
/*ARGSUSED*/
static
gettimeofday(tp, zp)
struct timeval *tp;
struct timezone *zp;
{
tp->tv_sec = time(0);
tp->tv_usec = 0;
}
#endif /* SYSV */
/*
* P R E P _ T I M E R
*/
void
prep_timer()
{
gettimeofday(&time0, (struct timezone *)0);
getrusage(RUSAGE_SELF, &ru0);
}
/*
* R E A D _ T I M E R
*
*/
double
read_timer(str,len)
char *str;
int len;
{
struct timeval timedol;
struct rusage ru1;
struct timeval td;
struct timeval tend, tstart;
char line[132];
getrusage(RUSAGE_SELF, &ru1);
gettimeofday(&timedol, (struct timezone *)0);
prusage(&ru0, &ru1, &timedol, &time0, line);
(void)strncpy( str, line, len );
/* Get real time */
tvsub( &td, &timedol, &time0 );
realt = td.tv_sec + ((double)td.tv_usec) / 1000000;
/* Get CPU time (user+sys) */
tvadd( &tend, &ru1.ru_utime, &ru1.ru_stime );
tvadd( &tstart, &ru0.ru_utime, &ru0.ru_stime );
tvsub( &td, &tend, &tstart );
cput = td.tv_sec + ((double)td.tv_usec) / 1000000;
if( cput < 0.00001 ) cput = 0.00001;
return( cput );
}
static void
prusage(r0, r1, e, b, outp)
register struct rusage *r0, *r1;
struct timeval *e, *b;
char *outp;
{
struct timeval tdiff;
register time_t t;
register char *cp;
register int i;
int ms;
t = (r1->ru_utime.tv_sec-r0->ru_utime.tv_sec)*100+
(r1->ru_utime.tv_usec-r0->ru_utime.tv_usec)/10000+
(r1->ru_stime.tv_sec-r0->ru_stime.tv_sec)*100+
(r1->ru_stime.tv_usec-r0->ru_stime.tv_usec)/10000;
ms = (e->tv_sec-b->tv_sec)*100 + (e->tv_usec-b->tv_usec)/10000;
#define END(x) {while(*x) x++;}
#if defined(SYSV)
cp = "%Uuser %Ssys %Ereal %P";
#else
#if defined(sgi) /* IRIX 3.3 will show 0 for %M,%F,%R,%C */
cp = "%Uuser %Ssys %Ereal %P %Mmaxrss %F+%Rpf %Ccsw";
#else
cp = "%Uuser %Ssys %Ereal %P %Xi+%Dd %Mmaxrss %F+%Rpf %Ccsw";
#endif
#endif
for (; *cp; cp++) {
if (*cp != '%')
*outp++ = *cp;
else if (cp[1]) switch(*++cp) {
case 'U':
tvsub(&tdiff, &r1->ru_utime, &r0->ru_utime);
sprintf(outp,"%ld.%01ld", tdiff.tv_sec, tdiff.tv_usec/100000);
END(outp);
break;
case 'S':
tvsub(&tdiff, &r1->ru_stime, &r0->ru_stime);
sprintf(outp,"%ld.%01ld", tdiff.tv_sec, tdiff.tv_usec/100000);
END(outp);
break;
case 'E':
psecs(ms / 100, outp);
END(outp);
break;
case 'P':
sprintf(outp,"%d%%", (int) (t*100 / ((ms ? ms : 1))));
END(outp);
break;
#if !defined(SYSV)
case 'W':
i = r1->ru_nswap - r0->ru_nswap;
sprintf(outp,"%d", i);
END(outp);
break;
case 'X':
sprintf(outp,"%ld", t == 0 ? 0 : (r1->ru_ixrss-r0->ru_ixrss)/t);
END(outp);
break;
case 'D':
sprintf(outp,"%ld", t == 0 ? 0 :
(r1->ru_idrss+r1->ru_isrss-(r0->ru_idrss+r0->ru_isrss))/t);
END(outp);
break;
case 'K':
sprintf(outp,"%ld", t == 0 ? 0 :
((r1->ru_ixrss+r1->ru_isrss+r1->ru_idrss) -
(r0->ru_ixrss+r0->ru_idrss+r0->ru_isrss))/t);
END(outp);
break;
case 'M':
sprintf(outp,"%d", r1->ru_maxrss/2);
END(outp);
break;
case 'F':
sprintf(outp,"%d", r1->ru_majflt-r0->ru_majflt);
END(outp);
break;
case 'R':
sprintf(outp,"%d", r1->ru_minflt-r0->ru_minflt);
END(outp);
break;
case 'I':
sprintf(outp,"%d", r1->ru_inblock-r0->ru_inblock);
END(outp);
break;
case 'O':
sprintf(outp,"%d", r1->ru_oublock-r0->ru_oublock);
END(outp);
break;
case 'C':
sprintf(outp,"%d+%d", r1->ru_nvcsw-r0->ru_nvcsw,
r1->ru_nivcsw-r0->ru_nivcsw );
END(outp);
break;
#endif /* !SYSV */
}
}
*outp = '\0';
}
static void
tvadd(tsum, t0, t1)
struct timeval *tsum, *t0, *t1;
{
tsum->tv_sec = t0->tv_sec + t1->tv_sec;
tsum->tv_usec = t0->tv_usec + t1->tv_usec;
if (tsum->tv_usec > 1000000)
tsum->tv_sec++, tsum->tv_usec -= 1000000;
}
static void
tvsub(tdiff, t1, t0)
struct timeval *tdiff, *t1, *t0;
{
tdiff->tv_sec = t1->tv_sec - t0->tv_sec;
tdiff->tv_usec = t1->tv_usec - t0->tv_usec;
if (tdiff->tv_usec < 0)
tdiff->tv_sec--, tdiff->tv_usec += 1000000;
}
static void
psecs(l,cp)
long l;
register char *cp;
{
register int i;
i = l / 3600;
if (i) {
sprintf(cp,"%d:", i);
END(cp);
i = l % 3600;
sprintf(cp,"%d%d", (i/60) / 10, (i/60) % 10);
END(cp);
} else {
i = l;
sprintf(cp,"%d", i / 60);
END(cp);
}
i %= 60;
*cp++ = ':';
sprintf(cp,"%d%d", i / 10, i % 10);
}
/*
* N R E A D
*/
int Nread( fd, buf, count )
int fd;
void *buf;
int count;
{
struct sockaddr_in from;
socklen_t len = sizeof(from);
register int cnt;
if( udp ) {
cnt = recvfrom( fd, buf, count, 0, (struct sockaddr *)&from, &len );
numCalls++;
} else {
if( b_flag )
cnt = mread( fd, buf, count ); /* fill buf */
else {
cnt = read( fd, buf, count );
numCalls++;
}
if (touchdata && cnt > 0) {
register int c = cnt, sum;
register char *b = buf;
while (c--)
sum += *b++;
}
}
return(cnt);
}
/*
* N W R I T E
*/
int Nwrite( fd, buf, count )
int fd;
void *buf;
int count;
{
register int cnt;
if( udp ) {
again:
cnt = sendto( fd, buf, count, 0, sinhim_p, sizeof(sinhim) );
numCalls++;
if( cnt<0 && errno == ENOBUFS ) {
printf("ttcp: out of buffers -- delaying\n"); /*JRS*/
delay(18000);
errno = 0;
goto again;
}
} else {
cnt = write( fd, buf, count );
numCalls++;
}
return(cnt);
}
void
delay(int us)
{
struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = us;
(void)select( 1, (fd_set *)0, (fd_set *)0, (fd_set *)0, &tv );
}
/*
* M R E A D
*
* This function performs the function of a read(II) but will
* call read(II) multiple times in order to get the requested
* number of characters. This can be necessary because
* network connections don't deliver data with the same
* grouping as it is written with. Written by Robert S. Miles, BRL.
*/
int
mread(fd, bufp, n)
int fd;
register char *bufp;
unsigned n;
{
register unsigned count = 0;
register int nread;
do {
nread = read(fd, bufp, n-count);
numCalls++;
if(nread < 0) {
perror("ttcp_mread");
return(-1);
}
if(nread == 0)
return((int)count);
count += (unsigned)nread;
bufp += nread;
} while(count < n);
return((int)count);
}