uuencode.c
来自「UUDeview是一个编码解码器」· C语言 代码 · 共 1,795 行 · 第 1/4 页
C
1,795 行
llen = 0; optr = otemp; } switch ((char) ((int) itemp[index] + 42)) { case '\0': case '\t': case '\n': case '\r': case '=': case '\033': *optr++ = '='; *optr++ = (char) ((int) itemp[index] + 42 + 64); llen += 2; break; case '.': if (llen == 0) { *optr++ = '='; *optr++ = (char) ((int) itemp[index] + 42 + 64); llen += 2; } else { *optr++ = (char) ((int) itemp[index] + 42); llen++; } break; default: *optr++ = (char) ((int) itemp[index] + 42); llen++; break; } } } /* * write last line */ if (llen) { if (fwrite (otemp, 1, llen, outfile) != llen || fwrite ((char *) eolstring, 1, strlen(eolstring), outfile) != strlen (eolstring)) { return UURET_IOERR; } } return UURET_OK; } /* * Handling for binary encodings */ /* * select charset */ table = etables[encoding]; if (table==NULL || bpl[encoding]==0) { UUMessage (uuencode_id, __LINE__, UUMSG_ERROR, uustring (S_PARM_CHECK), "UUEncodeStream()"); return UURET_ILLVAL; } while (!feof (infile) && (linperfile <= 0 || line < linperfile)) { if ((count = fread (itemp, 1, bpl[encoding], infile)) != bpl[encoding]) { if (count == 0) break; else if (ferror (infile)) return UURET_IOERR; } optr = otemp; llen = 0; /* * Busy Callback */ if (UUBUSYPOLL(ftell(infile)-progress.foffset,progress.fsize)) { UUMessage (uuencode_id, __LINE__, UUMSG_NOTE, uustring (S_ENCODE_CANCEL)); return UURET_CANCEL; } /* * for UU and XX, encode the number of bytes as first character */ if (encoding == UU_ENCODED || encoding == XX_ENCODED) { *optr++ = table[count]; llen++; } /* * Main encoding */ for (index=0; index<=count-3; index+=3, llen+=4) { *optr++ = table[itemp[index] >> 2]; *optr++ = table[((itemp[index ] & 0x03) << 4)|(itemp[index+1] >> 4)]; *optr++ = table[((itemp[index+1] & 0x0f) << 2)|(itemp[index+2] >> 6)]; *optr++ = table[ itemp[index+2] & 0x3f]; } /* * Special handling for incomplete lines */ if (index != count) { if (encoding == B64ENCODED) { if (count - index == 2) { *optr++ = table[itemp[index] >> 2]; *optr++ = table[((itemp[index ] & 0x03) << 4) | ((itemp[index+1] & 0xf0) >> 4)]; *optr++ = table[((itemp[index+1] & 0x0f) << 2)]; *optr++ = '='; } else if (count - index == 1) { *optr++ = table[ itemp[index] >> 2]; *optr++ = table[(itemp[index] & 0x03) << 4]; *optr++ = '='; *optr++ = '='; } llen += 4; } else if (encoding == UU_ENCODED || encoding == XX_ENCODED) { if (count - index == 2) { *optr++ = table[itemp[index] >> 2]; *optr++ = table[((itemp[index ] & 0x03) << 4) | ( itemp[index+1] >> 4)]; *optr++ = table[((itemp[index+1] & 0x0f) << 2)]; *optr++ = table[0]; } else if (count - index == 1) { *optr++ = table[ itemp[index] >> 2]; *optr++ = table[(itemp[index] & 0x03) << 4]; *optr++ = table[0]; *optr++ = table[0]; } llen += 4; } } /* * end of line */ tptr = eolstring; while (*tptr) *optr++ = *tptr++; *optr++ = '\0'; llen += strlen ((char *) eolstring); if (fwrite (otemp, 1, llen, outfile) != llen) return UURET_IOERR; line++; } return UURET_OK;}/* * Encode as MIME multipart/mixed sub-message. */int UUEXPORTUUEncodeMulti (FILE *outfile, FILE *infile, char *infname, int encoding, char *outfname, char *mimetype, int filemode){ mimemap *miter=mimetable; struct stat finfo; int res, themode; FILE *theifile; char *ptr; 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), "UUEncodeMulti()"); 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) != 0) { themode = (filemode)?filemode:0644; progress.fsize = -1; } else { themode = (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 not given from outside, select an appropriate Content-Type by * looking at the file's extension. If it is unknown, default to * Application/Octet-Stream */ if (mimetype == NULL) { if ((ptr = _FP_strrchr ((outfname)?outfname:infname, '.'))) { while (miter->extension && _FP_stricmp (ptr+1, miter->extension) != 0) miter++; mimetype = miter->mimetype; } } if (mimetype == NULL && (encoding == PT_ENCODED || encoding == QP_ENCODED)) { mimetype = "text/plain"; } /* * print sub-header */ if (encoding != YENC_ENCODED) { fprintf (outfile, "Content-Type: %s%s", (mimetype)?mimetype:"Application/Octet-Stream", eolstring); fprintf (outfile, "Content-Transfer-Encoding: %s%s", CTE_TYPE(encoding), eolstring); fprintf (outfile, "Content-Disposition: attachment; filename=\"%s\"%s", UUFNameFilter ((outfname)?outfname:infname), eolstring); fprintf (outfile, "%s", eolstring); } 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 as MIME message/partial */int UUEXPORTUUEncodePartial (FILE *outfile, FILE *infile, char *infname, int encoding, char *outfname, char *mimetype, int filemode, int partno, long linperfile, crc32_t *crcptr){ mimemap *miter=mimetable; static FILE *theifile; int themode, numparts; struct stat finfo; long thesize; char *ptr; int res; crc32_t pcrc; crc32_t *pcrcptr=NULL; if ((outfname==NULL&&infname==NULL) || partno<=0 || (infile == NULL&&infname==NULL) || outfile==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), "UUEncodePartial()"); return UURET_ILLVAL; } /* * The first part needs a set of headers */ progress.action = 0; if (partno == 1) { 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; } if (linperfile <= 0) numparts = 1; else numparts = (int) (((long)finfo.st_size+(linperfile*bpl[encoding]-1))/ (linperfile*bpl[encoding])); themode = (filemode) ? filemode : ((int) finfo.st_mode & 0777); thesize = (long) finfo.st_size; } else { if (fstat (fileno (infile), &finfo) != 0) { UUMessage (uuencode_id, __LINE__, UUMSG_WARNING, uustring (S_STAT_ONE_PART)); numparts = 1; themode = (filemode)?filemode:0644; thesize = -1; } else { if (linperfile <= 0) numparts = 1; else numparts = (int) (((long)finfo.st_size+(linperfile*bpl[encoding]-1))/ (linperfile*bpl[encoding])); themode = (int) finfo.st_mode & 0777; thesize = (long) finfo.st_size; } theifile = infile; } _FP_strncpy (progress.curfile, (outfname)?outfname:infname, 256); progress.totsize = (thesize>=0) ? thesize : -1; progress.partno = 1; progress.numparts = numparts; progress.percent = 0; progress.foffset = 0; /* * If not given from outside, select an appropriate Content-Type by * looking at the file's extension. If it is unknown, default to * Application/Octet-Stream */ if (mimetype == NULL) { if ((ptr = _FP_strrchr ((outfname)?outfname:infname, '.'))) { while (miter->extension && _FP_stricmp (ptr+1, miter->extension) != 0) miter++; mimetype = miter->mimetype; } } if (mimetype == NULL && (encoding==PT_ENCODED || encoding==QP_ENCODED)) { mimetype = "text/plain"; } /* * print sub-header */ if (encoding != YENC_ENCODED) { fprintf (outfile, "MIME-Version: 1.0%s", eolstring); fprintf (outfile, "Content-Type: %s%s", (mimetype)?mimetype:"Application/Octet-Stream", eolstring); fprintf (outfile, "Content-Transfer-Encoding: %s%s",
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?