void test_readwrite() { test_work_area_type * test_area = test_work_area_alloc("matrix-test"); { rng_type * rng = rng_alloc(MZRAN , INIT_DEV_URANDOM ); matrix_type * m1 = matrix_alloc(3 , 3); matrix_type * m2 = matrix_alloc(3 , 3); matrix_random_init( m1 , rng ); matrix_assign(m2 , m1); test_assert_true( matrix_equal( m1 , m2 ) ); { FILE * stream = util_fopen("m1" , "w"); matrix_fwrite( m1 , stream ); fclose( stream ); } matrix_random_init( m1 , rng ); test_assert_false( matrix_equal( m1 , m2 ) ); { FILE * stream = util_fopen("m1" , "r"); matrix_free( m1 ); m1 = matrix_alloc(1,1); printf("-----------------------------------------------------------------\n"); matrix_fread( m1 , stream ); test_assert_int_equal( matrix_get_rows(m1) , matrix_get_rows( m2)); test_assert_int_equal( matrix_get_columns(m1) , matrix_get_columns( m2)); util_fseek( stream , 0 , SEEK_SET); { matrix_type * m3 = matrix_fread_alloc( stream ); test_assert_true( matrix_equal( m2 , m3 )); matrix_free( m3 ); } fclose( stream ); } test_assert_true( matrix_equal( m1 , m2 ) ); matrix_free( m2 ); matrix_free( m1 ); rng_free( rng ); } test_work_area_free( test_area ); }
int main(int argc, char *argv[]) { double **K; byte *stream, *crypted; long n; FILE *fpkey; if (argc != NARGS) { printf("usage: %s keyfile inputfile outputfile\n", argv[PROGNAME]); return -1; } // Carregar o arquivo de chave de criptografia... fpkey = fopen(argv[KEYFILE], "r"); if (fpkey == NULL) { printf("Could not open keyfile: %s\n", argv[KEYFILE]); return -2; } K = matrix_create(ROWS, COLS); matrix_fread(fpkey, K, ROWS, COLS); fclose(fpkey); // Ler o arquivo a ser criptografado... stream = readFullFile(argv[INPUTFILE], &n); // Criptografar... crypted = crypt(stream, (int) n, K, ROWS, COLS); // Salvar em um arquivo... writeFullFile(argv[OUTPUTFILE], crypted, n*64); free(stream); free(crypted); matrix_free(K, ROWS); return 0; }
matrix_type * matrix_fread_alloc(FILE * stream) { matrix_type * matrix = matrix_alloc(1,1); matrix_fread(matrix , stream); return matrix; }