xdecode.c
来自「Reference Implementation of G.711 standa」· C语言 代码 · 共 1,011 行 · 第 1/3 页
C
1,011 行
/* 06.Jul.99 Ver.2.3 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ XDECODE.C Description: ~~~~~~~~~~~~ Program to convert into binary an ASCII file (or a sequence of ASCII files) that follows the de-facto standard of the public-domain program uuencode.c. It has added the capability of automatically reconstucting a file that has been broken (probably because the original file was too long for tranmission through some networks; for example, BITNET only allows 64k-byte-long files) using the program xencode.c. The encoded file will be decoded by xdecode.c or any other compatible with uudecode.c; using the former, the reconstruction of the file encoded by xencode.c will be automatic (in the case that file truncation happened), and the latter, will have to be manual. uuencode.c's basic algorithm [used in the generation of the ASCII file this program treats] is to access the file char-wisely (char-by-char), to transform the chars read into printable ASCII chars but adding some error detection capabilities: reads 45 chars from the file (possibly except in the last "block" read from file, if the file size is not multiple of 45, i.e., filesize % 45 != 0.), codes the number of chars read and put into the file;then codes the samples, making 3 chars into 4, and then converting them to readable ASCII chars. The encoding function [implemented in uuencode.c] is ENC(c) = (((c) & 077) + ' ') and the decoding function [as implemented in uudecode.c], DEC(c) = (((c) - ' ') & 077) The standard implies also that a header and a tail are added to the file. The header, for uuencode.c, is the word "begin", followed by the file's access mode (in octal) and the original file name. The tail is just the word "end", followed by a carriage return (necessarily). The extension here implemented just retireve some more information from the header: the continuation file's name, inserted by xencode.c. This way, if no continuation file name is found in header, the decoding program assumes that the presently open file is the last one. In this version the CRC is calculated and saved incrementally to the end of the xencoded file (right after the "end" line). Incrementally means that, when a file is broken in parts, at the end of each part, it will be printed the CRC from the beginning of the file upto that part. Three CRCs are printed: CCITT, XMODEM and ARC. The CRC module is based on the implementation posted on the comp.sources.unix by Mark G. Mendel. Usage: ~~~~~~ $ xdecode [-options] [InpFile] where: InpFile is the name of the file to be decoded; if not specified, stdin is assumed; -t forces the mode of the output file to be "text" (ie, non-binary); -b file mode is forced to binary [Default] -C path create the output file using the path Variables: ~~~~~~~~~~ in input file; out output file; Exit values: ~~~~~~~~~~~~ -1 Wrong parameters 0 Success (all but VAX/VMS) 1 Success (only VAX/VMS) 2 Error opening input file 3 Error opening output file 4 Illegal or unknown user (for Unix) 5 Error on input file: no begin line 6 Error on input file: no end line 7 Error on input file: short line (more chars were expected) Compilation under: ~~~~~~~~~~~~~~~~~~ TurboC: tcc xdecode MS-C: cl xdecode VAX-C: cc xdecode link xdecode SunOS: cc -o xdecode xdecode.c GNU-C: gcc -o xdecode xdecode.c Known bugs: ~~~~~~~~~~~ Under VMS, may not work for ASCII files encoded under any-ENCODE, due to this version force the file be fixed-length, 512 bytes/record. This is made made to assure that binary files are properly decoded in the VMS environment. These ASCII files will truncated to the smallest multiple of 512 bytes near the actual file's size. Nevertheless, these files will be successfully decoded by uudecode, that may not work always under VMS for binary files. If this feature is not desired, run the program with the option -t: $ xdecode -t file.uue and it will work properly for non-binary files (and may not for binary ones). AGAIN, this NOTE is ONLY for VMS, and not valid for other OSs, but will work anyway. Original Author: ~~~~~~~~~~~~~~~~ Simao Ferraz de Campos Neto - CPqD/Telebras History: ~~~~~~~~ 15.Mar.91 1.0 Release of 1st implementation to UGST 08.Jun.92 1.1 Inclusion of operation option -t for forcing text mode for output files; or -b, for binary ones (the default). <tdsimao@cpqd.ansp.br> 31.Aug.92 1.2 Implemented correction of a transmission bug; see note in decode(). 01.Sep.93 2.0 Implemented CRC calculation and fix stripped trailing blanks (function decode()) <simao> 18.Sep.95 2.1 Cleaned up code for gcc warnings and for compilation error in MSDOS gcc (index() renamed: xd_index()) <simao@ctd.comsat.com> 04.Mar.97 2.2 Added option to create the decoded file into a directory other than the current one. Tested for Unix only so far. <simao> 06.Jul.99 2.3 Removed compilation warnings in xd_index() (redefinition of NULL and inconsistent prototype) <simao> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*//* Version number for XDECODE */#define XDECODE 220/* General includes */#include "ugstdemo.h" /* UGST demo macros - the 1st include */#include <stdio.h>#include <stdlib.h>#include <ctype.h> /* for tolower() */#include <string.h> /* for str...() *//* System-dependent includes */#if defined(VMS)#include types#include stat#elif defined(MSDOS)#include <sys\types.h>#include <sys\stat.h>#else /* Unix */#include <pwd.h>#include <sys/types.h>#include <sys/stat.h>#endif/* DEFINITION FOR SMART PROTOTYPES */#ifndef ARGS#if defined(MSDOS) || defined(VMS)#define ARGS(x) x#else /* Unix: no parameters in prototype! */#define ARGS(x) ()#endif#endif /* ARGS *//* General defines */#define YES 1#define NO 0/* Defines for CRC routine */#define W 16 /* CRC width */#define WTYPE unsigned short /* Basic data type */#define B 8 /* the number of bits per char *//* Local function prototypes */char *get_header ARGS((FILE *fp, char *buffer, int max_len));char *translate_destination ARGS((char *dest));int decode ARGS((FILE * in, FILE * out));int fr ARGS((FILE * fd, char *buf, int cnt));char *xd_index ARGS((register char *sp, register char c));int outdec ARGS((char *p, FILE *f, int n));WTYPE updcrc ARGS((WTYPE icrc, unsigned char *icp, int icnt, WTYPE *crctab, char swapped));/* DEC is the basic 1 character decoding function to un-make a char printing */#define DEC(c) (((c) - ' ') & 077)/* Macros related to crc calculations */#define get_ccitt_crc(crc,buf,n) updcrc(crc, buf, n, crctab_ccitt, 0)#define get_arc_crc(crc,buf,n) updcrc(crc, buf, n, crctab_arc, 1)#define get_xmodem_crc(crc,buf,n) updcrc(crc, buf, n, crctab_xmodem, 1)/* Global variables related to crc calculations */WTYPE crc_a = 0, crc_c = 0, crc_x = 0;int init_crc_a = 0L, init_crc_c = -1L, init_crc_x = 0L;/* -------------------------------------------------------------------------- display_usage() Shows program usage. History: ~~~~~~~~ 04.Mar.97 v1.0 Created <simao> --------------------------------------------------------------------------*/#define P(x) printf xvoid display_usage(){ P(("XDECODE.C - Version %1.2f of 4/Mar/1997 \n\n",(float)XDECODE/100.0)); P((" Program to decode (a) file(s) that follows the de-facto\n")); P((" standard of the public-domain program uuencode.c. It is also\n")); P((" capable of automatically reconstucting a file that has been broken\n")); P((" using the program xencode.c.\n")); P(("\n")); P((" Usage:\n")); P((" $ xdecode [-options] [InpFile]\n")); P((" where:\n")); P((" InpFile ... is the name of the file to be decoded; if not\n")); P((" specified, stdin is assumed;\n")); P((" Options:\n")); P((" -t forces the output file mode to \"text\" (non-binary)\n")); P((" -b output file mode is forced to binary [Default]\n")); P((" -C path create the output file using the given path\n")); /* Quit program */ exit(-128);}#undef P/* .................... End of display_usage() .................... *//* ************************ Begin of main() ************************* */int main(argc, argv) int argc; char *argv[];{ FILE *in, *out; int mode, status, version; int more_to_decode = YES, first_time = YES, input_is_file = YES; char dest[200], this_file[128], next_file[128]; char buf[80], mode_is_binary, crc_present = 1, bad_crc = 0; char *path; char *get_header(), *translate_destination(); unsigned long o_crc_a, o_crc_c, o_crc_x; /* CRC saved in xenc'd file */#ifdef VMS static char mrs[15] = "mrs=512"; /* for correct mrs in VMS environment */#endif /* Initializations */ mode_is_binary = 1; /* set mode as binary, as default */ path=0; /* Set default path to a NULL pointer */ /* Check is 1st par. is an option */ /* Check if 1st par. is an option */ while (argc > 1 && argv[1][0] == '-' && strlen(argv[1]) > 1) if (strcmp(argv[1], "-t") == 0) { /* set mode as text */ mode_is_binary = 0; /* Move argv over the option to the next argument */ argc--; argv++; } else if (strcmp(argv[1], "-b") == 0) { /* set mode as binary */ mode_is_binary = 1; /* Move argv over the option to the next argument */ argv++; argc--; } else if (strcmp(argv[1], "-C") == 0) { /* Create destination file in another directory */ path = argv[2]; /* Move argv over the option to the next argument */ argv+=2; argc-=2; } else if (strcmp(argv[1], "-?") == 0 || strstr(argv[1], "-help")) { /* Print help */ display_usage(); } else { fprintf(stderr, "ERROR! Invalid option \"%s\" in command line\n\n", argv[1]); display_usage(); } /* Gets/checks optional input arg */ if (argc > 1) { strcpy(next_file, argv[1]); argc--; } else { in = stdin; input_is_file = NO; } /* Warns wrong usage */ if (argc != 1) HARAKIRI("Usage: xdecode [-{t,b}] [infile]\n", -1); /* Initialize crc calculations */ crc_a = init_crc_a; crc_c = init_crc_c; crc_x = init_crc_x; /* Gets down to work! */ while (more_to_decode) { /* update next file name to be treated */ if (more_to_decode) strcpy(this_file, next_file); /* Open input file */ if (input_is_file) /* ... i.e., if not the std. input */ { if ((in = fopen(this_file, "r")) == NULL) KILL(this_file, 2); }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?