Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdio>
- #include <pthread.h>
- #include <vector>
- #include <unistd.h>
- #include <arpa/inet.h>
- #include <algorithm>
- using namespace std;
- #define MAXINT 0xFFFFFFFF
- #define CALC_STEP 1000000
- #define N_THREADS 8
- #define CRC16 0x8005
- pthread_mutex_t mtx_count = PTHREAD_MUTEX_INITIALIZER;
- pthread_mutex_t mtx_found = PTHREAD_MUTEX_INITIALIZER;
- vector<uint32_t> found;
- uint32_t my_count = 0;
- void hd(uint8_t* data, size_t size){ //HEX DUMP
- if(nullptr == data || size == 0)
- return;
- printf("Dumping \t%u bytes:\t", size);
- for(size_t i = 0; i < size; ++i){
- printf("%02X ", data[i]);
- }
- printf("\n");
- }
- uint16_t crc16_ARC(const uint8_t *data, size_t size) {
- uint16_t out = 0;
- int bits_read = 0, bit_flag;
- /* Sanity check: */
- if(data == NULL)
- return 0;
- while(size > 0) {
- bit_flag = out >> 15;
- /* Get next bit: */
- out <<= 1;
- out |= (*data >> bits_read) & 1; // item a) work from the least significant bits
- /* Increment bit counter: */
- bits_read++;
- if(bits_read > 7) {
- bits_read = 0;
- data++;
- size--;
- }
- /* Cycle check: */
- if(bit_flag)
- out ^= CRC16;
- }
- // item b) "push out" the last 16 bits
- int i;
- for (i = 0; i < 16; ++i) {
- bit_flag = out >> 15;
- out <<= 1;
- if(bit_flag)
- out ^= CRC16;
- }
- // item c) reverse the bits
- uint16_t crc = 0;
- i = 0x8000;
- int j = 0x0001;
- for (; i != 0; i >>=1, j <<= 1) {
- if (i & out) crc |= j;
- }
- return crc;
- }
- uint16_t crc16_CCITT_FALSE(const uint8_t* data_p, size_t length) {
- uint8_t x;
- uint16_t crc = 0xFFFF;
- while (length--){
- x = crc >> 8 ^ *data_p++;
- x ^= x>>4;
- crc = (crc << 8) ^ ((uint16_t)(x << 12)) ^ ((uint16_t)(x <<5)) ^ ((uint16_t)x);
- }
- return crc;
- }
- struct u32_tuple{
- uint32_t a;
- uint32_t b;
- };
- void chk_rng(u32_tuple&rng, vector<uint32_t>&local_found){
- // vector<uint32_t> local_found;
- uint8_t* d8;
- uint8_t* c8;
- uint32_t d32;
- uint32_t dn32;
- uint16_t crc;
- if(!(rng.b > rng.a)) {
- cerr << "Invalid range!" << endl;
- }
- d8 = (uint8_t*)(&dn32);
- c8 = (uint8_t*)(&crc);
- for(d32 = rng.a; d32 < rng.b; ++d32){
- dn32 = htonl(d32);
- crc = crc16_ARC(d8, 4);
- if(c8[0] == d8[3] && c8[1] == d8[2]){
- local_found.push_back(d32);
- }
- }
- }
- void *th_fun(void*){
- u32_tuple t;
- vector<uint32_t> v;
- bool roll = true;
- while (roll) {
- pthread_mutex_lock(&mtx_count);
- if(MAXINT == my_count){
- roll = false;
- }else if((MAXINT - my_count) < CALC_STEP){
- t.a = my_count;
- my_count = MAXINT;
- t.b = my_count;
- }else{
- t.a = my_count;
- my_count += 100000;
- t.b = my_count;
- }
- pthread_mutex_unlock(&mtx_count);
- if(false == roll){
- continue;
- }
- v.clear();
- chk_rng(t, v);
- pthread_mutex_lock(&mtx_found);
- found.insert( found.end(), v.begin(), v.end() );
- pthread_mutex_unlock(&mtx_found);
- }
- return nullptr;
- }
- inline float percent_of(float small, float big){
- return (100*small)/big;
- }
- int main() {
- // char s1[10] = "123456789";
- // uint32_t s = htonl(0x8962FAFF); // data FFFA6289 -- 8962FAFF
- // size_t size = sizeof(s);
- // uint16_t crc16 = crc16_ARC((uint8_t*)&s, size);
- //// uint16_t crc16_cf = crc16_CCITT_FALSE((uint8_t*)&s, size);
- // hd((uint8_t*)&s1, 9);
- // hd((uint8_t*)&s,size);
- // hd((uint8_t*)&crc16,2);
- // cout << "CRC16/ARC = 0x" << std::hex << crc16 << endl;
- //// cout << "CRC16/CCITT-FALSE = 0x" << std::hex << crc16_cf << endl;
- ///////////////////////////////////////////////////////////////////////////////
- FILE * pFile;
- pthread_t th[N_THREADS];
- uint32_t cnt_local;
- uint32_t fnd_local;
- for(int i = 0; i < N_THREADS; ++i){
- if(pthread_create(&th[i], NULL, th_fun, NULL)) {
- fprintf(stderr, "Error creating thread %d;\n", i);
- return 1;
- }
- }
- printf(" \% \tCOUNT\t\tFOUND\n");
- while (my_count < MAXINT) {
- pthread_mutex_lock(&mtx_count);
- cnt_local = my_count;
- pthread_mutex_unlock(&mtx_count);
- pthread_mutex_lock(&mtx_found);
- fnd_local = found.size();
- pthread_mutex_unlock(&mtx_found);
- printf("\r[%.1f]\t%08X\t%d", percent_of((float)cnt_local, (float)MAXINT), cnt_local, fnd_local);
- fflush(stdout);
- sleep(1);
- }
- for(int i = 0; i < N_THREADS; ++i){
- if(pthread_join(th[i], NULL)) {
- fprintf(stderr, "Error joining thread %d;\n", i);
- return 2;
- }
- }
- printf("\nSorting data...\n");
- std::sort(found.begin(), found.end());
- pFile = fopen ("crc16_ARC.txt","w");
- printf("Saving to file...\n");
- for(size_t i = 0; i < found.size(); ++i){
- // printf("0x%08X\n", found[i]);
- fprintf(pFile, "0x%08X\n", found[i]);
- }
- fclose(pFile);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement