uuencode.c

来自「UUDeview是一个编码解码器」· C语言 代码 · 共 1,795 行 · 第 1/4 页

C
1,795
字号
      progress.fsize = progress.totsize - progress.foffset;    else      progress.fsize = linperfile*bpl[encoding];    progress.action  = UUACT_ENCODING;    if ((outfile = fopen (oname, "w")) == NULL) {      UUMessage (uuencode_id, __LINE__, UUMSG_ERROR,		 uustring (S_NOT_OPEN_TARGET),		 oname, strerror (uu_errno = errno));      if (infile==NULL) fclose (theifile);      progress.action = 0;      free (oname);      return UURET_IOERR;    }    if (encoding != YENC_ENCODED) {      fprintf (outfile, "%s", eolstring);      fprintf (outfile, "_=_ %s", eolstring);      if (numparts == -1)	fprintf (outfile, "_=_ Part %03d of file %s%s",		 part, UUFNameFilter ((outfname)?outfname:infname),		 eolstring);      else	fprintf (outfile, "_=_ Part %03d of %03d of file %s%s",		 part, numparts,		 UUFNameFilter ((outfname)?outfname:infname),		 eolstring);      fprintf (outfile, "_=_ %s", eolstring);      fprintf (outfile, "%s", eolstring);    }    if (part==1 && (encoding == UU_ENCODED || encoding == XX_ENCODED)) {      fprintf (outfile, "begin %o %s%s",	       (filemode)?filemode : 0644,	       UUFNameFilter ((outfname)?outfname:infname), 	       eolstring);    }    else if (encoding == YENC_ENCODED) {      if (!crcptr) {        crc = crc32(0L, Z_NULL, 0);        crcptr = &crc;      }      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",		   part,		   UUFNameFilter ((outfname)?outfname:infname), 		   eolstring);	}	else {	  fprintf (outfile, "=ybegin part=%d line=128 size=%ld name=%s%s",		   part,		   progress.totsize,		   UUFNameFilter ((outfname)?outfname:infname), 		   eolstring);	}	fprintf (outfile, "=ypart begin=%d end=%d%s",		 (part-1)*linperfile*128+1,		 (part*linperfile*128) < progress.totsize ? 		 (part*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);	}      }    }    if ((res = UUEncodeStream (outfile, theifile,			       encoding, linperfile, crcptr, pcrcptr)) != 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));      }      if (infile==NULL) fclose (theifile);      progress.action = 0;      fclose (outfile);      unlink (oname);      _FP_free (oname);      return res;    }    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",		 (part*linperfile*128) < progress.totsize ? 		 linperfile*128 : (progress.totsize-(part-1)*linperfile*128),		 part,		 pcrc);      }      else {	fprintf (outfile, "=yend size=%d",		 progress.totsize);      }      if (feof (theifile))	fprintf (outfile, " crc32=%08lx", crc);       fprintf (outfile, "%s", eolstring);    }    /*     * empty line at end does no harm     */    fprintf (outfile, "%s", eolstring);    fclose  (outfile);  }  if (infile==NULL) fclose (theifile);  progress.action = 0;  _FP_free (oname);  return UURET_OK;}/* * Encode a MIME Mail message or Newsgroup posting and send to a * stream. Still needs a somewhat smart MDA, since we only gene- * rate a minimum set of headers. */int UUEXPORTUUE_PrepSingle (FILE *outfile, FILE *infile,		char *infname, int encoding,		char *outfname, int filemode,		char *destination, char *from,		char *subject, int isemail){  return UUE_PrepSingleExt (outfile, infile,			    infname, encoding,			    outfname, filemode,			    destination, from,			    subject, NULL,			    isemail);}int UUEXPORTUUE_PrepSingleExt (FILE *outfile, FILE *infile,		   char *infname, int encoding,		   char *outfname, int filemode,		   char *destination, char *from,		   char *subject, char *replyto,		   int isemail){  mimemap *miter=mimetable;  char *subline, *oname;  char *mimetype, *ptr;  int res, len;  if ((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), "UUE_PrepSingle()");    return UURET_ILLVAL;  }  oname = UUFNameFilter ((outfname)?outfname:infname);  len   = ((subject)?strlen(subject):0) + strlen(oname) + 40;  if ((ptr = _FP_strrchr (oname, '.'))) {    while (miter->extension && _FP_stricmp (ptr+1, miter->extension) != 0)      miter++;    mimetype = miter->mimetype;  }  else    mimetype = NULL;  if (mimetype == NULL && (encoding == PT_ENCODED || encoding == QP_ENCODED)) {    mimetype = "text/plain";  }  if ((subline = (char *) malloc (len)) == NULL) {    UUMessage (uuencode_id, __LINE__, UUMSG_ERROR,	       uustring (S_OUT_OF_MEMORY), len);    return UURET_NOMEM;  }  if (encoding == YENC_ENCODED) {    if (subject)      sprintf (subline, "- %s - %s (001/001)", oname, subject);    else      sprintf (subline, "- %s - (001/001)", oname);  }  else {    if (subject)      sprintf (subline, "%s (001/001) - [ %s ]", subject, oname);    else      sprintf (subline, "[ %s ] (001/001)", oname);  }  if (from) {    fprintf (outfile, "From: %s%s", from, eolstring);  }  if (destination) {    fprintf (outfile, "%s: %s%s",	     (isemail)?"To":"Newsgroups",	     destination, eolstring);  }  fprintf (outfile, "Subject: %s%s", subline, eolstring);  if (replyto) {    fprintf (outfile, "Reply-To: %s%s", replyto, eolstring);  }  if (encoding != YENC_ENCODED) {    fprintf (outfile, "MIME-Version: 1.0%s", eolstring);    fprintf (outfile, "Content-Type: %s; name=\"%s\"%s",	     (mimetype)?mimetype:"Application/Octet-Stream",	     UUFNameFilter ((outfname)?outfname:infname),	     eolstring);    fprintf (outfile, "Content-Transfer-Encoding: %s%s",	     CTE_TYPE(encoding), eolstring);  }  fprintf (outfile, "%s", eolstring);  res = UUEncodeToStream (outfile, infile, infname, encoding,			  outfname, filemode);    _FP_free (subline);  return res;}int UUEXPORTUUE_PrepPartial (FILE *outfile, FILE *infile,		 char *infname, int encoding,		 char *outfname, int filemode,		 int partno, long linperfile, long filesize,		 char *destination, char *from, char *subject,		 int isemail){  return UUE_PrepPartialExt (outfile, infile,			     infname, encoding,			     outfname, filemode,			     partno, linperfile, filesize,			     destination,			     from, subject, NULL,			     isemail);}int UUEXPORTUUE_PrepPartialExt (FILE *outfile, FILE *infile,		    char *infname, int encoding,		    char *outfname, int filemode,		    int partno, long linperfile, long filesize,		    char *destination,		    char *from, char *subject, char *replyto,		    int isemail){  static int numparts, themode;  static char mimeid[64];  static FILE *theifile;  struct stat finfo;  char *subline, *oname;  long thesize;  int res, len;  static crc32_t crc;  crc32_t *crcptr=NULL;  if ((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), "UUE_PrepPartial()");    return UURET_ILLVAL;  }  oname = UUFNameFilter ((outfname)?outfname:infname);  len   = ((subject)?strlen(subject):0) + strlen (oname) + 40;  /*   * if first part, get information about the file   */  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) {	if (filesize <= 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) ((filesize+(linperfile*bpl[encoding]-1))/			      (linperfile*bpl[encoding]));	  themode  = (filemode)?filemode:0644;	  thesize  = filesize;	}      }      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;	thesize  = (long) finfo.st_size;      }      theifile = infile;    }    /*     * if there's one part only, don't use Message/Partial     */    if (numparts == 1) {      if (infile==NULL) fclose (theifile);      return UUE_PrepSingleExt (outfile, infile, infname, encoding,				outfname, filemode, destination,				from, subject, replyto, isemail);    }    /*     * we also need a unique ID     */    sprintf (mimeid, "UUDV-%ld.%ld.%s",	     (long) time(NULL), thesize,	     (strlen(oname)>16)?"oops":oname);  }  if ((subline = (char *) malloc (len)) == NULL) {    UUMessage (uuencode_id, __LINE__, UUMSG_ERROR,	       uustring (S_OUT_OF_MEMORY), len);    if (infile==NULL) fclose (theifile);    return UURET_NOMEM;  }  if (encoding == YENC_ENCODED) {    if (partno == 1)      crc = crc32(0L, Z_NULL, 0);    crcptr = &crc;    if (subject)      sprintf (subline, "- %s - %s (%03d/%03d)", oname, subject,	       partno, numparts);    else      sprintf (subline, "- %s - (%03d/%03d)", oname,	       partno, numparts);  }  else {    if (subject)      sprintf (subline, "%s (%03d/%03d) - [ %s ]", 	       subject, partno, numparts, oname);    else      sprintf (subline, "[ %s ] (%03d/%03d)",	       oname, partno, numparts);  }  if (from) {    fprintf (outfile, "From: %s%s", from, eolstring);  }  if (destination) {    fprintf (outfile, "%s: %s%s",	     (isemail)?"To":"Newsgroups",	     destination, eolstring);  }  fprintf (outfile, "Subject: %s%s", subline, eolstring);  if (replyto) {    fprintf (outfile, "Reply-To: %s%s", replyto, eolstring);  }  if (encoding != YENC_ENCODED) {    fprintf (outfile, "MIME-Version: 1.0%s", eolstring);    fprintf (outfile, "Content-Type: Message/Partial; number=%d; total=%d;%s",	     partno, numparts, eolstring);    fprintf (outfile, "\tid=\"%s\"%s",	     mimeid, eolstring);  }      fprintf (outfile, "%s", eolstring);  res = UUEncodePartial (outfile, theifile,			 infname, encoding,			 (outfname)?outfname:infname, NULL,			 themode, partno, linperfile, crcptr);  _FP_free (subline);  if (infile==NULL) {    if (res != UURET_OK) {      fclose (theifile);      return res;    }    if (feof (theifile)) {      fclose (theifile);      return UURET_OK;    }    return UURET_CONT;  }  return res;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?