xdecode.c
来自「Reference Implementation of G.711 standa」· C语言 代码 · 共 1,011 行 · 第 1/3 页
C
1,011 行
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ char *get_header (FILE *fp, char *buffer, int max_len); ~~~~~~~~~~~ Description: ~~~~~~~~~~~~ Sistematically reads lines from "fp" until one checks with the standard expected ty uudecode algorithm. Important to skip additional lines added by e-mail protocols (as BSMTP) for routing information, and that have no information as far as the original file is concerned. Variables: ~~~~~~~~~~ fp pointer to encoded file buffer buffer where the header line is returned max_len maximum length for header. Exit values: ~~~~~~~~~~~~ Pointer to header's buffer; NULL if no header found in file. Author: Simao Ferraz de Campos Neto -- CPqD/Telebras ~~~~~~~ <tdsimao@venus.cpqd.ansp.br> Date: 19/Mar/91-16:30 ~~~~~ Version: 1.00 ~~~~~~~~ Revision/Date/Author: none (yet) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/char *get_header(fp, buffer, max_len) FILE *fp; char *buffer; int max_len;{ for (;;) { if (fgets(buffer, max_len, fp) == NULL) return (NULL); if (strncmp(buffer, "begin ", 6) == 0) break; } return (buffer);}/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ char *translate_destination (char *dest); ~~~~~~~~~~~~~~~~~~~~~~ Description: ~~~~~~~~~~~~ Handle ~user/file format, converting to present system's names (root, directory, user, etc) IF not under MSDOS or VAX-VMS (ie, Unix); if one of the former, no modification on "dest" is performed. Variables: ~~~~~~~~~~ dest i/o buffer where the translation is performed Exit values: ~~~~~~~~~~~~ Pointer to translated buffer Author: unknown ~~~~~~~ Revision/Date/Author: Simao Ferraz de Campos Neto -- CPqD/Telebras ~~~~~~~~~~~~~~~~~~~~~ <tdsimao@venus.cpqd.ansp.br> 19/Mar/91-16:30~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/char *translate_destination(dest) char *dest;{#ifndef MSDOS#ifndef VMS char *sl; struct passwd *getpwnam(); struct passwd *user; char dnbuf[100]; sl = xd_index(dest, '/'); if (sl == NULL) return (NULL); *sl++ = 0; user = getpwnam(dest + 1); if (user == NULL) return (NULL); strcpy(dnbuf, user->pw_dir); strcat(dnbuf, "/"); strcat(dnbuf, sl); strcpy(dest, dnbuf);#endif /* VMS */#endif /* MSDOS */ return (dest);}/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ decode (FILE *in, FILE *out); ~~~~~~~ Description: ~~~~~~~~~~~~ Copy from in to out, encoding as it goes along, using the basic decoding scheme: DEC(c) = (((c) - ' ') & 077) Variables: ~~~~~~~~~~ in pointer to file from which samples are taken out pointer to file to which encoded samples are put Return values: ~~~~~~~~~~~~~~ 1, if success; 0, if EOF while expecting to have more data; -1, on other file read failures; 2, if a bug was found. Routines: - outdec ~~~~~~~~~ - DEC Author: original, unknown; got from PD sources. ~~~~~~~ Revision/Date/Author: ~~~~~~~~~~~~~~~~~~~~~ 19.Mar.91 v1.0 Documentation of the public domain source for decode(); <tdsimao@venus.cpqd.ansp.br> 31.Aug.92 v1.1 Corrected bug in dealing with lines that, originally with a space as the very last character in the line, had this space stripped-off by the e-mailing system (somewhere!). Here, a new blank is put at the end of the line, replacing the CR character normally present. <tdsimao@venus.cpqd.ansp.br>~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/int decode(in, out) FILE *in; FILE *out;{ char buf[80]; char *bp; int n, i, expected; static unsigned long lineno; int out_code = 1; for (;; ++lineno) { /* for each input line */ if (fgets(buf, sizeof buf, in) == NULL) { /* An error occurred */ if (feof(in)) return (0); else return (-1); } n = DEC(buf[0]); /* Break if no chars to read */ if ((n <= 0) || (buf[0] == '\n')) break; /* Calculate expected # of chars and pad if necessary */ expected = ((n + 2) / 3) << 2; for (i = strlen(buf) - 1; i <= expected; i++) buf[i] = ' '; /* Move pointer to start of data */ bp = &buf[1]; while (n > 0) { outdec(bp, out, n); bp += 4; n -= 3; } } return (out_code);}/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ int outdec (char *p, FILE *f, int n); ~~~~~~ Description: ~~~~~~~~~~~~ Output a group of 3 bytes (4 input characters). The input chars are pointed to by p, they are to be output to file f. n is used to tell us not to output all of them at the end of the file. The basic decoding algorithm is: DEC(c) = (((c) - ' ') & 077) Variables: ~~~~~~~~~~ p pointer to array of 4 [printable] bytes to be decoded and saved as 3 binary bytes; f pointer to file to which decoded samples are put n expected number of chars in the line Author: unknown ~~~~~~~ Revision/Date/Author: Simao Ferraz de Campos Neto -- CPqD/Telebras ~~~~~~~~~~~~~~~~~~~~~ <tdsimao@venus.cpqd.ansp.br> -- 19/Mar/91-16:30~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/int outdec(p, f, n) char *p; FILE *f; int n;{ char c[3]; /* Decode chars */ c[0] = DEC(*p) << 2 | DEC(p[1]) >> 4; c[1] = DEC(p[1]) << 4 | DEC(p[2]) >> 2; c[2] = DEC(p[2]) << 6 | DEC(p[3]); /* Save decoded bytes on file */ if (n >= 1) putc(c[0], f); if (n >= 2) putc(c[1], f); if (n >= 3) putc(c[2], f); /* Update CRCs */ if (n > 3) n = 3; crc_a = get_arc_crc(crc_a, (unsigned char *) c, n); crc_c = get_ccitt_crc(crc_c, (unsigned char *) c, n); crc_x = get_xmodem_crc(crc_x, (unsigned char *) c, n); /* Return OK */ return 0;}/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ int fr (FILE *fd, char *buf, int cnt); ~~ Description: ~~~~~~~~~~~~ Read upto cnt bytes from stream (file/device) pointed by fd and save in buffer buf. Similar to fread. Variables: ~~~~~~~~~~ fd pointer to file from which chars are read buf pointer to char cnt maximum number of chars to be read Author: unknown ~~~~~~~ Revision/Date/Author: Simao Ferraz de Campos Neto -- CPqD/Telebras ~~~~~~~~~~~~~~~~~~~~~ <tdsimao@venus.cpqd.ansp.br> -- 19/Mar/91-16:30~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/int fr(fd, buf, cnt) FILE *fd; char *buf; int cnt;{ int c, i; for (i = 0; i < cnt; i++) { c = getc(fd); if (c == EOF) return (i); buf[i] = c; } return (cnt);}/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ char *xd_index (register char *sp, register char c); ~~~~~~ Description: ~~~~~~~~~~~~ Locates the first occurence of c in stream sp and returns it; if c is not found, returns NULL. Variables: ~~~~~~~~~~ sp pointer to first ocurrence of c in ptr c character to be located; Author: unknown ~~~~~~~ Revision/Date/Author: Simao Ferraz de Campos Neto -- CPqD/Telebras ~~~~~~~~~~~~~~~~~~~~~ <tdsimao@venus.cpqd.ansp.br> -- 19/Mar/91-16:30~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/#ifdef NULL#undef NULL#endif#define NULL 0char *xd_index(sp, c) register char *sp; register char c;{ do { if (*sp == c) return (sp); } while (*sp++); return (NULL);}/* ....................... End of xd_index() ................................ */
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?