// hash mining in a single thread // #include #include #include #include #include #include void error( const char *msg) { perror(msg); exit(1); } #define offset_basis 2166136261U #define FNV_prime 16777619U // FNV hash from http://www.isthe.com/chongo/tech/comp/fnv/ // unsigned int fnv1a( unsigned char *d, size_t n) { unsigned int hash = offset_basis; while( n--) { hash ^= *d++; hash *= FNV_prime; } return hash & 0xFFFFFF; // bottom 24 bits } unsigned int ans = 0xabcdef; // desired hash output void *f(void *p) { // thread worker printf( "hi from f\n"); // fflush(stdout); // goes into the pipe unsigned int start = 0, stop = UINT_MAX; // *** unsigned overflow bug here: for( unsigned int x = start; x <= stop; ++x) { char buf[BUFSIZ]; sprintf( buf, "%08x", x); // unsigned int h = fnv1a( (unsigned char *) &x, sizeof(x) ); // if( h == ans) { printf( "f x %08x ans %x\n", x, h); break; } unsigned int h = fnv1a( (unsigned char *) buf, 8 ); if( h == ans) { printf( "f x %s ans %x\n", buf, h); break; } } return 0; } int main( void) { setbuf( stdout, 0); int fd[2]; if( pipe(fd) < 0) perror("pipe"); dup2(fd[0],0); // connect stdin to pipe dup2(fd[1],1); // connect stdout to pipe close(fd[0]); close(fd[1]); // close unneeded descriptors pthread_t t; if( pthread_create( &t, 0, f, 0) != 0) error( "pthread_create"); char msg[BUFSIZ]; while( fgets(msg,BUFSIZ,stdin)) { // read from the pipe fprintf( stderr, "main got: %s", msg); // write to terminal if( strstr( msg, "ans") ) break; // *** stuck in loop here if ans never found } }