uuencode.c
来自「UUDeview是一个编码解码器」· C语言 代码 · 共 1,795 行 · 第 1/4 页
C
1,795 行
CTE_TYPE(encoding), eolstring); fprintf (outfile, "Content-Disposition: attachment; filename=\"%s\"%s", UUFNameFilter ((outfname)?outfname:infname), eolstring); } fprintf (outfile, "%s", eolstring); /* * for the first part of UU or XX messages, print a begin line */ if (encoding == UU_ENCODED || encoding == XX_ENCODED) { fprintf (outfile, "begin %o %s%s", (themode) ? themode : ((filemode)?filemode:0644), UUFNameFilter ((outfname)?outfname:infname), eolstring); } } if (encoding == YENC_ENCODED) { pcrc = crc32(0L, Z_NULL, 0); pcrcptr = &pcrc; if (numparts != 1) { if (progress.totsize == -1) { fprintf (outfile, "=ybegin part=%d line=128 name=%s%s", partno, UUFNameFilter ((outfname)?outfname:infname), eolstring); } else { fprintf (outfile, "=ybegin part=%d line=128 size=%ld name=%s%s", partno, progress.totsize, UUFNameFilter ((outfname)?outfname:infname), eolstring); } fprintf (outfile, "=ypart begin=%d end=%d%s", (partno-1)*linperfile*128+1, (partno*linperfile*128) < progress.totsize ? (partno*linperfile*128) : progress.totsize, eolstring); } else { if (progress.totsize == -1) { fprintf (outfile, "=ybegin line=128 name=%s%s", UUFNameFilter ((outfname)?outfname:infname), eolstring); } else { fprintf (outfile, "=ybegin line=128 size=%ld name=%s%s", progress.totsize, UUFNameFilter ((outfname)?outfname:infname), eolstring); } } } /* * update progress information */ progress.partno = partno; progress.percent = 0; progress.foffset = ftell (theifile); if (progress.totsize <= 0) progress.fsize = -1; else if (linperfile <= 0) progress.fsize = progress.totsize; else if (progress.foffset+linperfile*bpl[encoding] > progress.totsize) progress.fsize = progress.totsize - progress.foffset; else progress.fsize = linperfile*bpl[encoding]; progress.action = UUACT_ENCODING; if ((res = UUEncodeStream (outfile, theifile, encoding, linperfile, crcptr, pcrcptr)) != UURET_OK) { if (infile==NULL) fclose (theifile); if (res != UURET_CANCEL) { UUMessage (uuencode_id, __LINE__, UUMSG_ERROR, uustring (S_ERR_ENCODING), UUFNameFilter ((outfname)?outfname:infname), (res==UURET_IOERR)?strerror(uu_errno):UUstrerror (res)); } progress.action = 0; return res; } /* * print end line */ if (feof (theifile) && (encoding == UU_ENCODED || encoding == XX_ENCODED)) { fprintf (outfile, "%c%s", (encoding==UU_ENCODED) ? UUEncodeTable[0] : XXEncodeTable[0], eolstring); fprintf (outfile, "end%s", eolstring); } else if (encoding == YENC_ENCODED) { if (numparts != 1) { fprintf (outfile, "=yend size=%d part=%d pcrc32=%08lx", (partno*linperfile*128) < progress.totsize ? linperfile*128 : (progress.totsize-(partno-1)*linperfile*128), partno, pcrc); } else { fprintf (outfile, "=yend size=%d", progress.totsize); } if (feof (theifile)) fprintf (outfile, " crc32=%08lx", *crcptr); fprintf (outfile, "%s", eolstring); } /* * empty line at end does no harm */ if (encoding != PT_ENCODED && encoding != QP_ENCODED) { fprintf (outfile, "%s", eolstring); } if (infile==NULL) { if (res != UURET_OK) { progress.action = 0; fclose (theifile); return res; } if (feof (theifile)) { progress.action = 0; fclose (theifile); return UURET_OK; } return UURET_CONT; } /* * leave progress.action as-is */ return UURET_OK;}/* * send output to a stream, don't do any headers at all */int UUEXPORTUUEncodeToStream (FILE *outfile, FILE *infile, char *infname, int encoding, char *outfname, int filemode){ struct stat finfo; FILE *theifile; int themode; int res; crc32_t crc; crc32_t *crcptr=NULL; if (outfile==NULL || (infile == NULL&&infname==NULL) || (outfname==NULL&&infname==NULL) || (encoding!=UU_ENCODED&&encoding!=XX_ENCODED&&encoding!=B64ENCODED&& encoding!=PT_ENCODED&&encoding!=QP_ENCODED&&encoding!=YENC_ENCODED)) { UUMessage (uuencode_id, __LINE__, UUMSG_ERROR, uustring (S_PARM_CHECK), "UUEncodeToStream()"); return UURET_ILLVAL; } progress.action = 0; if (infile==NULL) { if (stat (infname, &finfo) == -1) { UUMessage (uuencode_id, __LINE__, UUMSG_ERROR, uustring (S_NOT_STAT_FILE), infname, strerror (uu_errno=errno)); return UURET_IOERR; } if ((theifile = fopen (infname, "rb")) == NULL) { UUMessage (uuencode_id, __LINE__, UUMSG_ERROR, uustring (S_NOT_OPEN_FILE), infname, strerror (uu_errno=errno)); return UURET_IOERR; } themode = (filemode) ? filemode : ((int) finfo.st_mode & 0777); progress.fsize = (long) finfo.st_size; } else { if (fstat (fileno (infile), &finfo) == -1) { /* gotta live with it */ themode = 0644; progress.fsize = -1; } else { themode = (filemode) ? filemode : ((int) finfo.st_mode & 0777); progress.fsize = (long) finfo.st_size; } theifile = infile; } if (progress.fsize < 0) progress.fsize = -1; _FP_strncpy (progress.curfile, (outfname)?outfname:infname, 256); progress.partno = 1; progress.numparts = 1; progress.percent = 0; progress.foffset = 0; progress.action = UUACT_ENCODING; if (encoding == UU_ENCODED || encoding == XX_ENCODED) { fprintf (outfile, "begin %o %s%s", (themode) ? themode : 0644, UUFNameFilter ((outfname)?outfname:infname), eolstring); } else if (encoding == YENC_ENCODED) { crc = crc32(0L, Z_NULL, 0); crcptr = &crc; if (progress.fsize == -1) { fprintf (outfile, "=ybegin line=128 name=%s%s", UUFNameFilter ((outfname)?outfname:infname), eolstring); } else { fprintf (outfile, "=ybegin line=128 size=%ld name=%s%s", progress.fsize, UUFNameFilter ((outfname)?outfname:infname), eolstring); } } if ((res = UUEncodeStream (outfile, theifile, encoding, 0, crcptr, NULL)) != UURET_OK) { if (res != UURET_CANCEL) { UUMessage (uuencode_id, __LINE__, UUMSG_ERROR, uustring (S_ERR_ENCODING), UUFNameFilter ((infname)?infname:outfname), (res==UURET_IOERR)?strerror(uu_errno):UUstrerror (res)); } progress.action = 0; return res; } if (encoding == UU_ENCODED || encoding == XX_ENCODED) { fprintf (outfile, "%c%s", (encoding==UU_ENCODED) ? UUEncodeTable[0] : XXEncodeTable[0], eolstring); fprintf (outfile, "end%s", eolstring); } else if (encoding == YENC_ENCODED) { if (progress.fsize == -1) { fprintf (outfile, "=yend crc32=%08lx%s", crc, eolstring); } else { fprintf (outfile, "=yend size=%ld crc32=%08lx%s", progress.fsize, crc, eolstring); } } /* * empty line at end does no harm */ fprintf (outfile, "%s", eolstring); if (infile==NULL) fclose (theifile); progress.action = 0; return UURET_OK;}/* * Encode to files on disk, don't generate any headers */int UUEXPORTUUEncodeToFile (FILE *infile, char *infname, int encoding, char *outfname, char *diskname, long linperfile){ int part, numparts, len, filemode, res; char *oname=NULL, *optr, *ptr; FILE *theifile, *outfile; struct stat finfo; crc32_t pcrc, crc; crc32_t *pcrcptr=NULL, *crcptr=NULL; if ((diskname==NULL&&infname==NULL) || (outfname==NULL&&infname==NULL) || (infile==NULL&&infname==NULL) || (encoding!=UU_ENCODED&&encoding!=XX_ENCODED&&encoding!=B64ENCODED&& encoding!=PT_ENCODED&&encoding!=QP_ENCODED&&encoding!=YENC_ENCODED)) { UUMessage (uuencode_id, __LINE__, UUMSG_ERROR, uustring (S_PARM_CHECK), "UUEncodeToFile()"); return UURET_ILLVAL; } if (diskname) { if ((ptr = strchr (diskname, '/')) == NULL) ptr = strchr (diskname, '\\'); if (ptr) { len = strlen (diskname) + ((uuencodeext)?strlen(uuencodeext):3) + 5; if ((oname = malloc (len)) == NULL) { UUMessage (uuencode_id, __LINE__, UUMSG_ERROR, uustring (S_OUT_OF_MEMORY), len); return UURET_NOMEM; } sprintf (oname, "%s", diskname); } else { len = ((uusavepath)?strlen(uusavepath):0) + strlen (diskname) + ((uuencodeext)?strlen(uuencodeext):0) + 5; if ((oname = malloc (len)) == NULL) { UUMessage (uuencode_id, __LINE__, UUMSG_ERROR, uustring (S_OUT_OF_MEMORY), len); return UURET_NOMEM; } sprintf (oname, "%s%s", (uusavepath)?uusavepath:"", diskname); } } else { len = ((uusavepath) ? strlen (uusavepath) : 0) + strlen(UUFNameFilter(infname)) + ((uuencodeext)?strlen(uuencodeext):0) + 5; if ((oname = malloc (len)) == NULL) { UUMessage (uuencode_id, __LINE__, UUMSG_ERROR, uustring (S_OUT_OF_MEMORY), len); return UURET_NOMEM; } optr = UUFNameFilter (infname); sprintf (oname, "%s%s", (uusavepath)?uusavepath:"", (*optr=='.')?optr+1:optr); } /* * optr points after the last dot, so that we can print the part number * there. */ optr = _FP_strrchr (oname, '.'); if (optr==NULL || strchr (optr, '/')!=NULL || strchr (optr, '\\')!=NULL) { optr = oname + strlen (oname); *optr++ = '.'; } else if (optr==oname || *(optr-1)=='/' || *(optr-1)=='\\') { optr = oname + strlen (oname); *optr++ = '.'; } else optr++; progress.action = 0; if (infile==NULL) { if (stat (infname, &finfo) == -1) { UUMessage (uuencode_id, __LINE__, UUMSG_ERROR, uustring (S_NOT_STAT_FILE), infname, strerror (uu_errno=errno)); _FP_free (oname); return UURET_IOERR; } if ((theifile = fopen (infname, "rb")) == NULL) { UUMessage (uuencode_id, __LINE__, UUMSG_ERROR, uustring (S_NOT_OPEN_FILE), infname, strerror (uu_errno=errno)); _FP_free (oname); return UURET_IOERR; } if (linperfile <= 0) numparts = 1; else numparts = (int) (((long)finfo.st_size + (linperfile*bpl[encoding]-1)) / (linperfile*bpl[encoding])); filemode = (int) finfo.st_mode & 0777; progress.totsize = (long) finfo.st_size; } else { if (fstat (fileno (infile), &finfo) == -1) { /* gotta live with it */ filemode = 0644; numparts = -1; progress.totsize = -1; } else { if (linperfile <= 0) numparts = 1; else numparts = (int) (((long)finfo.st_size+(linperfile*bpl[encoding]-1))/ (linperfile*bpl[encoding])); filemode = (int) finfo.st_mode & 0777; progress.totsize = -1; } theifile = infile; } _FP_strncpy (progress.curfile, (outfname)?outfname:infname, 256); progress.totsize = (progress.totsize<0) ? -1 : progress.totsize; progress.numparts = numparts; for (part=1; !feof (theifile); part++) { /* * Attach extension */ if (progress.numparts==1 && progress.totsize!=-1 && uuencodeext!=NULL) strcpy (optr, uuencodeext); else sprintf (optr, "%03d", part); /* * check if target file exists */ if (!uu_overwrite) { if (stat (oname, &finfo) == 0) { UUMessage (uuencode_id, __LINE__, UUMSG_ERROR, uustring (S_TARGET_EXISTS), oname); if (infile==NULL) fclose (theifile); progress.action = 0; free (oname); return UURET_EXISTS; } } /* * update progress information */ progress.action = 0; progress.partno = part; progress.percent = 0; progress.foffset = ftell (theifile); if (progress.totsize == -1) progress.fsize = -1; else if (linperfile <= 0) progress.fsize = progress.totsize; else if (progress.foffset+linperfile*bpl[encoding] > progress.totsize)
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?