/** * Verify that a buffer contains a valid packet. * \param[in] h The packet handler instance data pointer. * \param[in] p A pointer to the packet buffer. * \param[in] received_len The length of data received. * \return < 0 Failure * \return > 0 Number of bytes consumed. */ int32_t PHVerifyPacket(PHInstHandle h, PHPacketHandle p, uint16_t received_len) { // Verify the packet length. // Note: The last two bytes should be the RSSI and AFC. uint16_t len = PHPacketSizeECC(p); if (received_len < (len + 2)) { DEBUG_PRINTF(1, "Packet length error %d %d\n\r", received_len, len + 2); return -1; } // Attempt to correct any errors in the packet. decode_data((unsigned char*)p, len); // Check that there were no unfixed errors. bool rx_error = check_syndrome() != 0; if(rx_error) { DEBUG_PRINTF(1, "Error in packet\n\r"); return -2; } return len + 2; }
int main (int argc, char *argv[]) { int erasures[16]; int nerasures = 0; /* Initialization the ECC library */ initialize_ecc (); /* ************** */ /* Encode data into codeword, adding NPAR parity bytes */ encode_data(msg, sizeof(msg), codeword); printf("Encoded data is: \"%s\"\n", codeword); printf("Encoded data length: \"%d\"\n", sizeof(codeword)); printf("msg length: \"%d\"\n", sizeof(msg)); int i; for (i = 0; (i < sizeof(msg)); i++) { printf("%02x", msg[i]); } printf("\n"); for (i = 0; (i < sizeof(codeword)); i++) { printf("%02x", codeword[i]); } printf("\n"); #define ML (sizeof (msg) + NPAR) /* We need to indicate the position of the erasures. Eraseure positions are indexed (1 based) from the end of the message... */ erasures[nerasures++] = ML-17; erasures[nerasures++] = ML-19; /* Now decode -- encoded codeword size must be passed */ decode_data(codeword, ML); /* check if syndrome is all zeros */ if (check_syndrome () != 0) { correct_errors_erasures (codeword, ML, nerasures, erasures); printf("Corrected codeword: \"%s\"\n", codeword); } exit(0); }
TEST_F(EncodeDecode, Recover) { unsigned char p[10] = {'a', 'b', 'c', 'd', 'e', 'f'}; encode_data(p, 6, p); unsigned char p2[10]; for (int i = 0; i < 10; i++) p2[i] = p[i]; p2[4] = 30; // verify it flags the error decode_data(p2, 6 + RS_ECC_NPARITY); EXPECT_EQ(1, check_syndrome()); // verify it is corrected EXPECT_EQ(1, correct_errors_erasures(p2, 10, 0, 0)); for (int i = 0; i < 6; i++) EXPECT_EQ(p[i], p2[i]); };
int main (int argc, char *argv[]) { int erasures[16]; int nerasures = 0; char* filename = argv[1]; char command[100]; printf("reading the original file %s\n",argv[1]); //filename = argv[1]; sprintf(command,"md5 %s",argv[1]); system(command); /* Initialization the ECC library */ initialize_ecc (); //Open file file = fopen(argv[1], "r"); if (!file) { fprintf(stderr, "Unable to open file %s", argv[1]); exit(1); } //Get file length fseek(file, 0, SEEK_END); fileLen=ftell(file); fseek(file, 0, SEEK_SET); //Allocate memory buffer=(unsigned char *)malloc(fileLen+1); if (!buffer) { fprintf(stderr, "Memory error!"); fclose(file); exit(2); } //Read file contents into buffer fread(buffer, fileLen, 1, file); fclose(file); long i = fileLen; long pos = 0; int writeFlag = TRUE; strcat(filename,".encode"); printf("----------------------------------------------\n"); printf("encoding the original file into %s\n",filename); while (i>0) { //system(command); int msgLen; if (i>256-NPAR) { msgLen = 256-NPAR; } else { msgLen = i; } unsigned char msg[msgLen]; int k; for(k=0;k<msgLen;k++) { msg[k] = buffer[pos]; pos++; } encode_data(msg, sizeof(msg), codeword); if (writeFlag) { file = fopen(filename, "w+"); writeFlag = FALSE; } else { file = fopen(filename, "a+"); } fwrite(codeword, msgLen+NPAR, 1, file); fclose(file); i = i - msgLen; } free(buffer); sprintf(command,"md5 %s",filename); system(command); file = fopen(filename, "r"); if (!file) { fprintf(stderr, "Unable to open file %s", filename); exit(1); } //Get file length fseek(file, 0, SEEK_END); fileLen=ftell(file); fseek(file, 0, SEEK_SET); //Allocate memory buffer=(unsigned char *)malloc(fileLen+1); if (!buffer) { fprintf(stderr, "Memory error!"); fclose(file); exit(2); } //Read file contents into buffer fread(buffer, fileLen, 1, file); fclose(file); i = fileLen; pos = 0; writeFlag = TRUE; strcat(filename,".corrupt"); printf("----------------------------------------------\n"); printf("making some errors and write into the file %s\n",filename); int k; for(k = 0;k<10;k++) { long h = rand()%fileLen; buffer[h] = buffer[h] % 20; } file = fopen(filename, "w+"); fwrite(buffer, fileLen, 1, file); fclose(file); free(buffer); sprintf(command,"md5 %s",filename); system(command); file = fopen(filename, "r"); if (!file) { fprintf(stderr, "Unable to open file %s", filename); exit(1); } //Get file length fseek(file, 0, SEEK_END); fileLen=ftell(file); fseek(file, 0, SEEK_SET); //Allocate memory buffer=(unsigned char *)malloc(fileLen+1); if (!buffer) { fprintf(stderr, "Memory error!"); fclose(file); exit(2); } //Read file contents into buffer fread(buffer, fileLen, 1, file); fclose(file); i = fileLen; pos = 0; writeFlag = TRUE; strcat(filename,".recover"); printf("----------------------------------------------\n"); printf("recovering corrupted file into %s\n",filename); while (i>0) { int msgLen; if (i>256) { msgLen = 256; } else { msgLen = i; } unsigned char msg[msgLen]; int k; for(k=0;k<msgLen;k++) { msg[k] = buffer[pos]; pos++; } decode_data(msg, msgLen); if (check_syndrome () != 0) { correct_errors_erasures (msg,msgLen,nerasures,erasures); } if (writeFlag) { file = fopen(filename, "w+"); writeFlag = FALSE; } else { file = fopen(filename, "a+"); } fwrite(msg, msgLen-NPAR, 1, file); fclose(file); i = i - msgLen; } sprintf(command,"md5 %s",filename); system(command); free(buffer); exit(0); }
void inner_GMD(decoded_blocks *db,unsigned char * c_in_codeword, unsigned long * indices, FILE * fp){ unsigned char c_in_message[n1*k2],temp[n2],c_out_codeword[n1],c_out_message[v*32]; int c_index=0,m_index=0,i,j,m,index=0; int erasure_index[n1]; // cin decoding printf("concatenated Cin decoding...\n"); for(j=0;j<n1;j++){ for(i=0;i<n2;i++){ temp[i]=c_in_codeword[c_index++]; } unsigned char cpytemp[n2]; memcpy(cpytemp,temp,n2); decode_data(temp, n2); if (check_syndrome () != 0) { int erasure[1]; correct_errors_erasures(temp,n2,0,erasure); } int delta_dist = 0; for(i=0;i<n2;i++){ if(temp[i]!=cpytemp[i]) delta_dist++; } double prob; if(delta_dist<d2/2) prob = 2*(double)delta_dist/d2; else prob = 1.0; srand(time(NULL)); double random_num = (double)rand() / (double)RAND_MAX; for(i=0;i<k2;i++){ if (random_num<prob) c_in_message[m_index++]=0; else c_in_message[m_index++]=temp[i]; } if (random_num<prob) erasure_index[n1] = 1; else erasure_index[n1] = 0; } //printf("display c_in_message\n"); //displayCharArray(c_in_message,sizeof(c_in_message)); printf("concatenated Cout decoding...\n"); c_index=0; for(i=0;i<v;i++){ int erasure[n1]; int num_erasure = 0; index=0; //create codeword //copy message part for(j=0;j<k1;j++){ c_out_codeword[index++]=c_in_message[i*k1+j]; } int p; //copy parity part codeword p = v*32 + (i*d1); for(j=0;j<d1;j++){ c_out_codeword[index++]=c_in_message[j+p]; } //printf("display c_out_codeword for %d\n",i); //displayCharArray(c_out_codeword,sizeof(c_out_codeword)); if(erasure_index[v]==1) { int ki; for(ki=0;ki<k2;ki++) { erasure[num_erasure] = ki; num_erasure++; } } if(erasure_index[v]==1) { int di; for(di=0;di<d2;di++) { erasure[num_erasure] = k2+di; num_erasure++; } } decode_data(c_out_codeword, n1); if (check_syndrome () != 0) { correct_errors_erasures(c_out_codeword,n1,num_erasure,erasure); } for(j=0;j<k1;j++){ c_out_message[c_index++]=c_out_codeword[j]; } } //printf("updating Di...\n"); for(i=0;i<v;i++){ //printf("indices[%d]=%lu\n",i,indices[i]); //divide codeword into 32 byte blocks int fi = indices[i]; unsigned char block[32]; for(j=0;j<32;j++){ block[j]=c_out_message[(i*32)+j]; } //check if similar codeword was already decoded and present in Di //if yes, just increase the frequency int notfound=1; for(j=0;j<alpha;j++){ int flag=1; if(db[fi].frequency[j]==0) break; for(m=0;m<32;m++){ if(db[fi].file_blocks[j][m]!=block[m]){ flag=0; break; } } if(flag){ //int freq = db[fi].frequency[j]; db[fi].frequency[j]++; notfound=0; } } //if not found, add it in di if(notfound){ for(m=0;m<32;m++){ db[fi].file_blocks[j][m] = block[m]; } db[fi].frequency[j] = 1; } /* printf("display db %d:\n",fi); displayCharArray(db[fi].file_blocks[j],32); fseek(fp,fi*32,SEEK_SET); unsigned char buffer[32]; fread(buffer, 32, 1, fp); printf("real content in the file block %d:\n",fi); displayCharArray(buffer,32); */ } }
//concatenated decoding void inner_decoding(decoded_blocks *db,unsigned char * c_in_codeword, unsigned long * indices){ int p; //for(p=0;p<v;p++) { // printf("random index #%d: %lu\n",p,indices[p]); //} unsigned char c_in_message[v*k2],temp[n2],c_out_codeword[n1],c_out_message[v*k1]; int c_index=0,m_index=0,i,j,m,index=0; int erasure[1]; // cin decoding printf("concatenated Cin decoding...\n"); for(j=0;j<w*32/n2;j++){ for(i=0;i<n2;i++){ temp[i]=c_in_codeword[c_index++]; } decode_data(temp, n2); if (check_syndrome () != 0) { correct_errors_erasures(temp,n2,0,erasure); } for(i=0;i<k2;i++){ c_in_message[m_index++]=temp[k2]; } } //cout decoding: get the file and parity part printf("concatenated Cout decoding...\n"); c_index=0; for(i=0;i<v;i++){ index=0; //create codeword //copy message part for(j=0;j<k1;j++){ c_out_codeword[index++]=c_in_message[j]; } //copy parity part codeword p = (m_index/2) + (i*d1); for(j=0;j<d1;j++){ c_out_codeword[index++]=c_in_message[j+p]; } decode_data(c_out_codeword, n1); if (check_syndrome () != 0) { correct_errors_erasures(c_out_codeword,n1,0,erasure); } for(j=0;j<k1;j++){ c_out_message[c_index++]=c_out_codeword[j]; } } //c_out_message contains v decoded blocks //divide decoded message into v blocks from f1 to fv. printf("updating Di...\n"); for(i=0;i<v;i++){ printf("indices[%d]=%lu\n",i,indices[i]); //divide codeword into 32 byte blocks int fi = indices[i]; char block[32]; for(j=0;j<32;j++){ block[j]=c_out_message[(i*32)+j]; } //check if similar codeword was already decoded and present in Di //if yes, just increase the frequency int notfound=1; for(j=0;j<alpha;j++){ int flag=1; fflush(stdout); if(db[fi].frequency[j]==0) break; for(m=0;m<32;m++){ if(db[fi].file_blocks[j][m]!=block[m]){ flag=0; break; } } if(flag){ //int freq = db[fi].frequency[j]; db[fi].frequency[j]++; notfound=0; } } //if not found, add it in di if(notfound){ for(m=0;m<32;m++){ db[fi].file_blocks[j][m] = block[m]; } db[fi].frequency[j] = 1; } } }
int outer_decoding(FILE* orifp, FILE* parifp, FILE *output, FILE* tempfp, decoded_blocks *db) { int i,j,stripes,fileLen,index; int* prp_table; int* reverse_prp_table; char message[k]; char codeword[n]; //unsigned char decodedfile[stripes][k]; int erasure[1]; //FILE* tempfp = fopen("tempfile", "w+b"); if (tempfp == NULL){ printf("couldn't open temperory file for writing.\n"); return -1; } //find the number of stripes in the file fseek(parifp,0,SEEK_END); fileLen = ftell(parifp); printf("display fileLen of parity %d\n",fileLen); //if(fileLen % n==0) stripes = fileLen/d; //else //stripes = fileLen/n+1; printf("number of stripes for outer decoding %d\n",stripes); fflush(stdout); unsigned char ** parity;//[stripes][d]; parity = (unsigned char **) malloc(stripes*sizeof(unsigned char *)); for (i = 0; i < stripes; i++) { parity[i] = (unsigned char *) malloc(d*sizeof(unsigned char)); } //call prp prp_table =malloc(sizeof(int)*stripes); reverse_prp_table = malloc(sizeof(int)*stripes); //perform reverse prp prp_table = prp(stripes, k_ecc_perm); reverseprp(stripes,prp_table,reverse_prp_table); /*for(i=0;i<stripes;i++){ printf("prp %d: %d\n",i,prp_table[i]); printf("rev prp %d: %d\n",i,reverse_prp_table[i]); }*/ //free(prp_table); printf("check1\n"); enc_init(k_ecc_enc); //decrypt parity part rewind(parifp); rewind(orifp); ///number of parity blocks = stripes/2 //read parity parts directly //int parity_start = fileLen-(stripes/2)*d; //fseek(temp_fp,parity_start,SEEK_SET); unsigned char paritybytes[stripes][d]; for (i=0;i<stripes;i++) { unsigned char ct[d]; //unsigned char pt[d]; fread(ct,sizeof(ct),1,parifp); decrypt(ct,paritybytes[prp_table[i]],sizeof(ct)); //printf("prp rev for %d: ",i); //displayCharArray(paritybytes[i],sizeof(ct)); //for(j=0;j<d;j++){ // parity[i][j]=ct[j]; //} } for (i=0;i<stripes;i++) { printf("prp rev for %d: ",i); displayCharArray(paritybytes[i],sizeof(paritybytes[i])); } free(reverse_prp_table); reverse_prp_table = malloc(sizeof(int)*m); prp_table =malloc(sizeof(int)*m); prp_table = prp(m, k_file_perm); reverseprp(m,prp_table,reverse_prp_table); //get the message and the parity, create codeword and decode it //rewind(temp_fp); FILE * prp = fopen("prp","w+b"); for(i=0;i<m;i++) { unsigned char prpbuf[BLOCK_SIZE]; fseek(orifp,prp_table[i]*32,SEEK_SET); size_t br = fread(prpbuf,1,BLOCK_SIZE,orifp); fwrite(prpbuf,1,BLOCK_SIZE,prp); } fclose(prp); prp = fopen("prp","r+b"); for(i=0;i<stripes;i++) { index=0; unsigned char code[n]; size_t br = fread(code,1,k,prp); int pad = k-br; int ii; for (ii=0;ii<pad;ii++) code[br+ii] = 0; for(j=0;j<d;j++) { code[k+j] = paritybytes[i][j]; } printf("whole code %d: ",i); displayCharArray(code,n); decode_data(code, n); int syn = check_syndrome (); printf("syndrome: %d\n",syn); if (syn != 0) { correct_errors_erasures(code,n,0,erasure); } printf("decoded code %d: ",i); displayCharArray(code,n); //calculate block index of the parity part fwrite(code,1,br,tempfp); } fclose(tempfp); for (i = 0; i < stripes; i++){ free(parity[i]); } free(parity); if ((tempfp = fopen("tempfile", "r+b")) == NULL){ printf("couldn't open temperory file for writing.\n"); return -1; } // write data to the file by applying second level of permutation //free(prp_table); //free(reverse_prp_table); //printf("display m = %lu\n",m); rewind(tempfp); rewind(output); unsigned char buf[BLOCK_SIZE]; for(i=0;i<m;i++) { readStartTime = getCPUTime(); fseek(tempfp,reverse_prp_table[i]*32,SEEK_SET); fread(buf, BLOCK_SIZE, 1, tempfp); readEndTime = getCPUTime(); readTime += readEndTime-readStartTime; writeStartTime = getCPUTime(); fwrite(buf,BLOCK_SIZE,1,output); writeEndTime = getCPUTime(); writeTime += writeEndTime-writeStartTime; } printf("check4\n"); fclose(tempfp); fclose(output); fclose(orifp); fclose(parifp); printf("check5\n"); //delete(temp_fp); return 0; }
TEST_F(EncodeDecode, PassEncode) { unsigned char p[10] = {'a', 'b', 'c', 'd', 'e', 'f'}; encode_data(p, 6, p); decode_data(p, 6 + RS_ECC_NPARITY); EXPECT_EQ(0, check_syndrome()); };