Example #1
0
void encode_file(char *file, int nativeBlockNum, int parityBlockNum)
{
    int chunkSize = 1;
    int totalSize;

    FILE *fp_in;
    FILE *fp_out;
    if( ( fp_in = fopen(file,"rb") ) == NULL )
    {
        printf("Can not open source file!\n");
        exit(0);
    }

    fseek(fp_in, 0L, SEEK_END);
    //ftell() get the total size of the file
    totalSize = ftell(fp_in);
    chunkSize = (totalSize / nativeBlockNum) + ( totalSize%nativeBlockNum != 0 ); 
    //	chunkSize = (ftell(fp_in) / nativeBlockNum) + ( ftell(fp_in)%nativeBlockNum != 0 ); 
    //	chunkSize = (int) (ceil( (long double) (ftell(fp_in) / nativeBlockNum)) ); 

    uint8_t *dataBuf;		//host
    uint8_t *codeBuf;		//host
    int dataSize = nativeBlockNum*chunkSize*sizeof(uint8_t);
    int codeSize = parityBlockNum*chunkSize*sizeof(uint8_t);
    dataBuf = (uint8_t*) malloc( nativeBlockNum*chunkSize*sizeof(uint8_t) );
    memset(dataBuf, 0, dataSize);
    codeBuf = (uint8_t*) malloc( parityBlockNum*chunkSize*sizeof(uint8_t) );
    memset(codeBuf, 0, codeSize);

    int i;
    for(i=0; i<nativeBlockNum; i++)
    {
        if( fseek( fp_in, i*chunkSize, SEEK_SET ) == -1 )
        {
            printf("fseek error!\n");
            exit(0);
        }

        if( fread( dataBuf+i*chunkSize, sizeof(uint8_t), chunkSize, fp_in ) == EOF )
        {
            printf("fread error!\n");
            exit(0);
        }
    }
    fclose(fp_in);

    struct timespec start, end;
    double totalTime;
    clock_gettime(CLOCK_REALTIME,&start);
    //	// setup table for GF(2^8)
    //	setup_tables(8);
    uint8_t *encodingMatrix;
    encodingMatrix = (uint8_t*) malloc( parityBlockNum*nativeBlockNum*sizeof(uint8_t) );
    gen_encoding_matrix(encodingMatrix, parityBlockNum, nativeBlockNum);
    write_metadata(totalSize, parityBlockNum, nativeBlockNum, encodingMatrix);
    encode_chunk(dataBuf, encodingMatrix, codeBuf, nativeBlockNum, parityBlockNum, chunkSize);
    //	matrix_mul(encodingMatrix, dataBuf, codeBuf, parityBlockNum, nativeBlockNum, chunkSize);
    /*
    //int i;
    int j;
    int k;
    int n=parityBlockNum;
    int p=nativeBlockNum;
    int m=chunkSize;
    for(i=0; i<n; i++)
    {
    for(j=0; j<m; j++)
    {
    codeBuf[i*m+j] = 0;
    for(k=0; k<p; k++)
    {
    //				C[i*m+j] = gf_add(C[i*m+j], gf_mul(A[i*p+k],B[k*m+j]));
    codeBuf[i*m+j] ^= gf_mul(encodingMatrix[i*p+k],dataBuf[k*m+j]);
    }
    }
    }
    */
    clock_gettime(CLOCK_REALTIME,&end);
    totalTime = (double)(end.tv_sec-start.tv_sec)*1000+(double)(end.tv_nsec-start.tv_nsec)/(double)1000000L;
    printf("Total CPU encoding time: %fms\n", totalTime);

    char output_file_name[100];
    for(i=0; i<nativeBlockNum; i++)
    {
        sprintf(output_file_name, "_%d_", i);
        strcat(output_file_name, file);
        if( ( fp_out = fopen(output_file_name, "wb") ) == NULL )
        {
            printf("Can not open source file!\n");
            exit(0);
        }
        if( fwrite(dataBuf+i*chunkSize, sizeof(uint8_t), chunkSize, fp_out ) != sizeof(uint8_t)*chunkSize )
        {
            printf("fwrite error!\n");
            exit(0);
        }
        fclose(fp_out);
    }
    for(i=0; i<parityBlockNum; i++)
    {
        sprintf(output_file_name, "_%d_", i+nativeBlockNum);
        strcat(output_file_name, file);
        if( ( fp_out = fopen(output_file_name, "wb") ) == NULL )
        {
            printf("Can not open source file!\n");
            exit(0);
        }
        if( fwrite(codeBuf+i*chunkSize, sizeof(uint8_t), chunkSize, fp_out ) != sizeof(uint8_t)*chunkSize )
        {
            printf("fwrite error!\n");
            exit(0);
        }
        fclose(fp_out);
    }

    free(dataBuf);
    free(codeBuf);

    free(encodingMatrix);

}
Example #2
0
int main(int argc, char *argv[])
{
	int nativeBlockNum = 4;
	int parityBlockNum = 2;
//	int chunkSize = sizeof(uint8_t);
	int chunkSize = 1;

//	char[] file_name_src = argv[1];
	FILE *fp_in;
	FILE *fp_out;
	int i;
	if( ( fp_in = fopen(argv[1],"rb") ) == NULL )
	{
		printf("Can not open source file!\n");
		exit(0);
	}

	fseek(fp_in, 0L, SEEK_END);
	chunkSize = ftell(fp_in)/nativeBlockNum; //ftell() get the total size of the file

	uint8_t *dataBuf;
	uint8_t *codeBuf;
	dataBuf = (unsigned char*) malloc( nativeBlockNum*chunkSize*sizeof(uint8_t) );
	codeBuf = (unsigned char*) malloc( parityBlockNum*chunkSize*sizeof(uint8_t) );

	for(i=0; i<nativeBlockNum; i++)
	{
		if( fseek(fp_in, i*chunkSize, SEEK_SET) == -1 )
		{
			printf("fseek error!\n");
			exit(0);
		}

		if( fread(dataBuf+i*chunkSize, sizeof(uint8_t), chunkSize, fp_in ) != sizeof(uint8_t)*chunkSize )
		{
			printf("fread error!\n");
			exit(0);
		}
	}
	fclose(fp_in);

	// setup table for GF(2^8)
	setup_tables(8);
	gen_encoding_matrix(parityBlockNum, nativeBlockNum);
	encode_chunk(dataBuf, encodingMatrix, codeBuf, nativeBlockNum, parityBlockNum, chunkSize);

#ifdef DEBUG
	show_code_chunk(codeBuf, parityBlockNum, chunkSize);
#endif

	char output_file_name[10];
	for(i=0; i<parityBlockNum; i++)
	{
		sprintf(output_file_name, "code_%d", i);
		if( ( fp_out = fopen(output_file_name, "wb") ) == NULL )
		{
			printf("Can not open source file!\n");
			exit(0);
		}
		if( fwrite(codeBuf+i*chunkSize, sizeof(uint8_t), chunkSize, fp_out ) != sizeof(uint8_t)*chunkSize )
		{
			printf("fwrite error!\n");
			exit(0);
		}
		fclose(fp_out);
	}



//	uint8_t testData[4] = {9,1,2,0};
//	uint8_t *testCode;
//
//	// setup table for GF(2^8)
//	setup_tables(8);
//	gen_encoding_matrix(parityBlockNum, nativeBlockNum);
//	testCode = (unsigned char*)malloc(parityBlockNum*chunkSize);
//	encode_chunk(testData, encodingMatrix, testCode, nativeBlockNum, parityBlockNum, chunkSize);
//
//#ifdef DEBUG
//showgflog();
//printf("\n");
//showgfilog();
//printf("\n");
//#endif
//	show_code_chunk(testCode, parityBlockNum, chunkSize);
//#ifdef DEBUG
//printf("\n");
//show_encoding_matrix(parityBlockNum, nativeBlockNum);
//#endif

free(encodingMatrix);

}