📄 corrupt.c
字号:
/* ******************************************************************************** * * This material contains proprietary software of Entropic Speech, Inc. * Any reproduction, distribution, or publication without the the prior * written permission of Entropic Speech, Inc. is strictly prohibited. * Any public distribution of copies of this work authorized in writing * by Entropic Speech, Inc. must bear the notice: * * "Copyright (c) 1987 Entropic Speech, Inc.; all rights reserved" * * Program: corrupt * * Written by: Jim Elliott * * Creates a corrupted FEA_2KB bitstream file by injecting errors at * a specified rate. * ******************************************************************************** *//* * SCCS program and date keywords. */#ifndef lintstatic char *sccs_id = "@(#)corrupt.c 1.1 12/3/87 ESI";#endif/* * System include files. */#include <math.h>#include <stdio.h>/* * ESPS include files. */#include <esps/esps.h>#include <esps/fea.h>#include <esps/fea2kb.h>/* * Defines. */#define BAD_PARAM( str ) \{ \ Fprintf( stderr, "corrupt: bad parameter -- %s = %s\n", str, getsym_s( str ) ); \ exit( 1 ); \}#define ERROR_EXIT( text ) \{ \ Fprintf( stderr, "corrupt: %s - exiting\n", text ); \ exit( 1 ); \}#define SYNTAX USAGE \( \ "corrupt -P param_file -S seed -h hist_file infile.fea outfile.fea" \)/* * System functions and variables. */char *ctime(), *strcpy();int atoi(), getopt(), strcmp();long time(), random(), srandom();void exit(), perror();extern optind;extern char *optarg;/* * ESPS functions and variables. */char *get_cmd_line();extern char *noyes[];/* ******************************************************************************** * Main program. ******************************************************************************** */main( argc, argv )char *argv[];int argc;{ char *cmd_line, /* String for command line */ *date = "12/3/87", *hfn = "corrupt.his", /* History file name */ *ifn = NULL, /* Input file name */ *ofn = NULL, /* Output file name */ *pfn = "params", /* Parameter file name */ *version = "1.1"; FILE *hfp = NULL, /* History file pointer */ *ifp = stdin, /* Input file pointer */ *ofp = stdout; /* Output file pointer */ int auto_seed = YES, /* Flag for automatic seed generation */ c, /* For getopt() return */ seed; /* Random number seed */ short err_rate; /* Channel error rate */ struct fea2kb *fea2kb_rec; /* FEA_2KB record */ struct header *ih, /* Input file header */ *oh; /* Output file header */ unsigned long err_mask(); /* Mask generation routine *//* * Read command line and process command line options. */ cmd_line = get_cmd_line( argc, argv ); while ( ( c = getopt( argc, argv, "P:S:h:" ) ) != EOF ) { switch ( c ) { case 'P': pfn = optarg; break; case 'S': seed = atoi( optarg ); auto_seed = NO; break; case 'h': hfn = optarg; break; default: SYNTAX; } }/* * Process file arguments. */ if ( optind < argc ) { ifn = argv[ optind++ ]; if ( strcmp( ifn, "-" ) == 0 ) ifn = "<stdin>"; else TRYOPEN( argv[0], ifn, "r", ifp ); } else { Fprintf( stderr, "corrupt: No input file specified\n" ); SYNTAX; } if ( optind < argc ) { ofn = argv[ optind++ ]; if ( strcmp( ofn, "-" ) == 0 ) ofn = "<stdout>"; else TRYOPEN( argv[0], ofn, "w", ofp ); } else { Fprintf( stderr, "corrupt: No output file specified\n" ); SYNTAX; } TRYOPEN( argv[0], hfn, "w", hfp );/* * Read parameter and common files, and write common file. */ if ( read_params( pfn, SC_CHECK_FILE, NULL ) != 0 ) ERROR_EXIT( "Error reading parameter file" ); if ( ( err_rate = lin_search2( error_rates, getsym_s( "err_rate" ) ) ) == -1 ) BAD_PARAM( "err_rate" ); if ( auto_seed ) { if ( symtype( "seed" ) == ST_UNDEF ) seed = 1; else { seed = getsym_i( "seed" ); seed++; } } (void) putsym_s( "filename", ofn ); (void) putsym_s( "prog", argv[0] ); (void) putsym_i( "seed", seed );/* * Read and check values from header of input file. */ if ( ( ih = read_header( ifp ) ) == NULL ) NOTSPS( argv[0], ifn ); if ( ih->common.type != FT_FEA ) ERROR_EXIT( "Input file is not a FEA file" ); if ( ih->hd.fea->fea_type != FEA_2KB ) ERROR_EXIT( "Input file is not FEA_2KB type" );/* * Create header for output file. */ oh = copy_header( ih ); add_source_file( oh, ifn, ih ); (void) strcpy( oh->common.prog, "corrupt" ); (void) strcpy( oh->common.vers, version ); (void) strcpy( oh->common.progdate, date ); add_comment( oh, cmd_line ); oh->common.tag = NO; oh->variable.refer = ifn; *(short *) get_genhd( "err_rate", oh ) = err_rate; write_header( oh, ofp );/* * Allocate storage for input and output data records. */ fea2kb_rec = allo_fea2kb_rec( ih );/* * Main program loop. */ while( get_fea2kb_rec( fea2kb_rec, ih, ifp ) != EOF ) { *fea2kb_rec->error_mask = (long) err_mask( err_rate, seed, version, date, ifn, ofn, hfp ); put_fea2kb_rec( fea2kb_rec, oh, ofp ); } exit( 0 );}/* ******************************************************************************** * Subroutine to create error mask for bitstream record. Also handles output * to history file. ******************************************************************************** */unsigned longerr_mask( err_rate, seed, version, date, ifn, ofn, hfp )char *date, *ifn, *ofn, *version;FILE *hfp;int seed;short err_rate;#define MAX_RAND 2147483647.0 /* Maximum value returned by random() */{ float v; /* Random variable */ int k; /* Scratch index */ long tloc; /* For date and time */ static float thresh; /* Error threshold */ static int err_cnt, /* Cumulative error count */ init = YES, /* Flag for initial entry */ rec_no; /* File record number */ struct key_tbl { short err_rate; /* Error rate */ float threshold; /* Threshold for error */ }; static struct key_tbl thresh_tbl[] = { E_3, 0.999, /* 1.0xE-3 error rate */ E_4, 0.9999, /* 1.0xE-4 error rate */ E_5, 0.99999, /* 1.0xE-5 error rate */ E_6, 0.999999, /* 1.0xE-6 error rate */ 0, 1.0 /* Zero error rate */ }; unsigned long error, /* Bitstream error field */ mask; /* Walking error bit */ if ( init ) { init = NO; tloc = time( 0 ); Fprintf( hfp, "Corrupt statistics output on %s", ctime( &tloc ) ); Fprintf( hfp, "Corrupt version %s, date %s\n\n", version, date ); Fprintf( hfp, "Input file: %s\n", ifn ); Fprintf( hfp, "Output file: %s\n\n", ofn ); Fprintf( hfp, "Error rate: %s\n", error_rates[ err_rate ] ); Fprintf( hfp, "Random seed: %d\n\n", seed ); (void) srandom( seed ); k = 0; while ( thresh_tbl[k].err_rate != err_rate ) k++; thresh = thresh_tbl[k].threshold * MAX_RAND; } if ( err_rate == NONE ) return( (unsigned long) 0 ); mask = 0x80000000; error = 0; rec_no++; while ( mask ) { v = random(); if ( v >= thresh ) { err_cnt++; error |= mask; Fprintf( hfp, "Record #%4d: Error = 0x%8lx; Error count = %d\n", rec_no, error, err_cnt ); } mask >>= 1; } return( error );}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -