This repository has been archived on 2024-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
deb-goldedplus/goldlib/uulib/uuencode.c

1797 lines
49 KiB
C
Raw Normal View History

2000-02-25 10:15:17 +00:00
/*
* This file is part of uudeview, the simple and friendly multi-part multi-
2001-11-01 17:20:18 +00:00
* file uudecoder program (c) 1994-2001 by Frank Pilhofer. The author may
* be contacted at fp@fpx.de
2000-02-25 10:15:17 +00:00
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifdef SYSTEM_WINDLL
#include <windows.h>
#endif
#ifdef SYSTEM_OS2
#include <os2.h>
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <time.h>
#ifdef STDC_HEADERS
#include <stdlib.h>
#include <string.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
2002-09-23 06:16:34 +00:00
#ifdef HAVE_IO_H
#include <io.h>
#endif
2000-02-25 10:15:17 +00:00
#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif
#include <uudeview.h>
#include <uuint.h>
#include <fptools.h>
#include <uustring.h>
2002-06-10 06:13:37 +00:00
#include <crc32.h>
2000-02-25 10:15:17 +00:00
/* for braindead systems */
#ifndef SEEK_SET
#ifdef L_BEGIN
#define SEEK_SET L_BEGIN
#else
#define SEEK_SET 0
#endif
#endif
char * uuencode_id = "$Id$";
#if 0
/*
* the End-Of-Line string. MIME enforces CRLF, so that's what we use. Some
* implementations of uudecode will complain about a missing end line, since
* they look for "end^J" but find "end^J^M". We don't care - especially be-
* cause they still decode the file properly despite this complaint.
*/
#ifndef EOLSTRING
#define EOLSTRING "\015\012"
2000-02-25 10:15:17 +00:00
#endif
#else
/*
* Argh. Some delivery software (inews) has problems with the CRLF
* line termination. Let's try native EOL and see if we run into
* any problems.
* This involves opening output files in text mode instead of binary
*/
#ifndef EOLSTRING
#define EOLSTRING "\n"
2000-02-25 10:15:17 +00:00
#endif
#endif
/*
* =========================================================================
* User-configurable settings end here. Don't spy below unless you know what
* you're doing.
* =========================================================================
*/
/*
* Define End-Of-Line sequence
*/
#ifdef EOLSTRING
2002-06-10 06:13:37 +00:00
static unsigned char *eolstring = (unsigned char *) EOLSTRING;
2000-02-25 10:15:17 +00:00
#else
2002-06-10 06:13:37 +00:00
static unsigned char *eolstring = (unsigned char *) "\012";
2000-02-25 10:15:17 +00:00
#endif
/*
* Content-Transfer-Encoding types for non-MIME encodings
*/
#define CTE_UUENC "x-uuencode"
#define CTE_XXENC "x-xxencode"
#define CTE_BINHEX "x-binhex"
#define CTE_YENC "x-yenc"
2000-02-25 10:15:17 +00:00
#define CTE_TYPE(y) (((y)==B64ENCODED) ? "Base64" : \
((y)==UU_ENCODED) ? CTE_UUENC : \
((y)==XX_ENCODED) ? CTE_XXENC : \
2001-11-01 17:20:18 +00:00
((y)==PT_ENCODED) ? "8bit" : \
((y)==QP_ENCODED) ? "quoted-printable" : \
((y)==BH_ENCODED) ? CTE_BINHEX : \
((y)==YENC_ENCODED) ? CTE_YENC : "x-oops")
2000-02-25 10:15:17 +00:00
/*
* encoding tables
*/
unsigned char UUEncodeTable[64] = {
'`', '!', '"', '#', '$', '%', '&', '\'',
'(', ')', '*', '+', ',', '-', '.', '/',
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', ':', ';', '<', '=', '>', '?',
'@', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
'X', 'Y', 'Z', '[', '\\',']', '^', '_'
};
2005-08-29 19:49:00 +00:00
2000-02-25 10:15:17 +00:00
unsigned char B64EncodeTable[64] = {
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
'w', 'x', 'y', 'z', '0', '1', '2', '3',
'4', '5', '6', '7', '8', '9', '+', '/'
};
unsigned char XXEncodeTable[64] = {
'+', '-', '0', '1', '2', '3', '4', '5',
'6', '7', '8', '9', 'A', 'B', 'C', 'D',
'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',
'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b',
'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
's', 't', 'u', 'v', 'w', 'x', 'y', 'z'
};
unsigned char BHEncodeTable[64] = {
'!', '"', '#', '$', '%', '&', '\'', '(',
')', '*', '+', ',', '-', '0', '1', '2',
2005-08-29 19:49:00 +00:00
'3', '4', '5', '6', '8', '9', '@', 'A',
'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R',
'S', 'T', 'U', 'V', 'X', 'Y', 'Z', '[',
'`', 'a', 'b', 'c', 'd', 'e', 'f', 'h',
2000-02-25 10:15:17 +00:00
'i', 'j', 'k', 'l', 'm', 'p', 'q', 'r'
2001-11-01 17:20:18 +00:00
};
unsigned char HexEncodeTable[16] = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
};
2000-02-25 10:15:17 +00:00
typedef struct {
char *extension;
char *mimetype;
} mimemap;
/*
* This table maps a file's extension into a Content-Type. The current
* official list can be downloaded as
* ftp://ftp.isi.edu/in-notes/iana/assignments/media-type
* I haven't listed any text types since it doesn't make sense to encode
* them. Everything not on the list gets mapped to application/octet-stream
*/
static mimemap mimetable[] = {
{ "gif", "image/gif" }, /* Grafics Interchange Format */
{ "jpg", "image/jpeg" }, /* JFIF encoded files */
{ "jpeg", "image/jpeg" },
{ "tif", "image/tiff" }, /* Tag Image File Format */
{ "tiff", "image/tiff" },
{ "cgm", "image/cgm" }, /* Computer Graphics Metafile */
{ "au", "audio/basic" }, /* 8kHz ulaw audio data */
{ "mov", "video/quicktime" }, /* Apple Quicktime */
{ "qt", "video/quicktime" }, /* Also infrequently used */
{ "mpeg", "video/mpeg" }, /* Motion Picture Expert Group */
{ "mpg", "video/mpeg" },
{ "mp2", "video/mpeg" }, /* dito, MPEG-2 encoded files */
{ "mp3", "audio/mpeg" }, /* dito, MPEG-3 encoded files */
{ "ps", "application/postscript" }, /* Postscript Language */
{ "zip", "application/zip" }, /* ZIP archive */
{ "doc", "application/msword"},/* assume Microsoft Word */
{ NULL, NULL }
2000-02-25 10:15:17 +00:00
};
/*
* the order of the following two tables must match the
* Encoding Types definition in uudeview.h
*/
/*
* encoded bytes per line
*/
2002-06-10 06:13:37 +00:00
static int bpl[8] = { 0, 45, 57, 45, 45, 0, 0, 128 };
2000-02-25 10:15:17 +00:00
/*
* tables
*/
static unsigned char *etables[5] = {
NULL,
UUEncodeTable,
B64EncodeTable,
XXEncodeTable,
BHEncodeTable
};
/*
* variables to malloc upon initialization
*/
char *uuestr_itemp;
char *uuestr_otemp;
/*
* Encode one part of the data stream
*/
2005-08-29 19:49:00 +00:00
static int
2002-06-10 06:13:37 +00:00
UUEncodeStream (FILE *outfile, FILE *infile, int encoding, long linperfile, crc32_t *crc, crc32_t *pcrc)
2000-02-25 10:15:17 +00:00
{
unsigned char *itemp = (unsigned char *) uuestr_itemp;
unsigned char *otemp = (unsigned char *) uuestr_otemp;
2000-02-25 10:15:17 +00:00
unsigned char *optr, *table, *tptr;
int index, count;
long line=0;
size_t llen;
if (outfile==NULL || infile==NULL ||
2001-11-01 17:20:18 +00:00
(encoding!=UU_ENCODED&&encoding!=XX_ENCODED&&encoding!=B64ENCODED&&
2002-06-10 06:13:37 +00:00
encoding!=PT_ENCODED&&encoding!=QP_ENCODED&&encoding!=YENC_ENCODED)) {
2000-02-25 10:15:17 +00:00
UUMessage (uuencode_id, __LINE__, UUMSG_ERROR,
uustring (S_PARM_CHECK), "UUEncodeStream()");
2000-02-25 10:15:17 +00:00
return UURET_ILLVAL;
}
2001-11-01 17:20:18 +00:00
/*
* Special handling for plain text and quoted printable. Text is
* read line oriented.
*/
if (encoding == PT_ENCODED || encoding == QP_ENCODED) {
while (!feof (infile) && (linperfile <= 0 || line < linperfile)) {
2002-06-10 06:13:37 +00:00
if (_FP_fgets ((char *) itemp, 255, infile) == NULL) {
break;
2001-11-01 17:20:18 +00:00
}
itemp[255] = '\0';
2002-06-10 06:13:37 +00:00
count = strlen ((char *) itemp);
2001-11-01 17:20:18 +00:00
llen = 0;
optr = otemp;
/*
* Busy Callback
*/
2005-08-29 19:49:00 +00:00
2001-11-01 17:20:18 +00:00
if (UUBUSYPOLL(ftell(infile)-progress.foffset,progress.fsize)) {
UUMessage (uuencode_id, __LINE__, UUMSG_NOTE,
uustring (S_ENCODE_CANCEL));
return UURET_CANCEL;
2001-11-01 17:20:18 +00:00
}
if (encoding == PT_ENCODED) {
/*
* If there is a line feed, replace by eolstring
*/
if (count > 0 && itemp[count-1] == '\n') {
itemp[--count] = '\0';
if (fwrite (itemp, 1, count, outfile) != (unsigned)count ||
fwrite ((char *) eolstring, 1,
strlen((char *) eolstring), outfile) != strlen ((char *) eolstring)) {
return UURET_IOERR;
}
}
else {
if (fwrite (itemp, 1, count, outfile) != llen) {
return UURET_IOERR;
}
}
2001-11-01 17:20:18 +00:00
}
else if (encoding == QP_ENCODED) {
for (index=0; index<count; index++) {
if (llen == 0 && itemp[index] == '.') {
/*
* Special rule: encode '.' at the beginning of a line, so
* that some mailers aren't confused.
*/
*optr++ = '=';
*optr++ = HexEncodeTable[itemp[index] >> 4];
*optr++ = HexEncodeTable[itemp[index] & 0x0f];
llen += 3;
}
else if ((itemp[index] >= 33 && itemp[index] <= 60) ||
(itemp[index] >= 62 && itemp[index] <= 126) ||
itemp[index] == 9 || itemp[index] == 32) {
*optr++ = itemp[index];
llen++;
}
else if (itemp[index] == '\n') {
/*
* If the last character before EOL was a space or tab,
* we must encode it. If llen > 74, there's no space to do
* that, so generate a soft line break instead.
*/
if (index>0 && (itemp[index-1] == 9 || itemp[index-1] == 32)) {
*(optr-1) = '=';
if (llen <= 74) {
*optr++ = HexEncodeTable[itemp[index-1] >> 4];
*optr++ = HexEncodeTable[itemp[index-1] & 0x0f];
llen += 2;
}
}
if (fwrite (otemp, 1, llen, outfile) != llen ||
fwrite ((char *) eolstring, 1,
strlen((char *) eolstring), outfile) != strlen ((char *) eolstring)) {
return UURET_IOERR;
}
/*
* Fix the soft line break condition from above
*/
if (index>0 && (itemp[index-1] == 9 || itemp[index-1] == 32) &&
*(optr-1) == '=') {
otemp[0] = '=';
otemp[1] = HexEncodeTable[itemp[index-1] >> 4];
otemp[2] = HexEncodeTable[itemp[index-1] & 0x0f];
if (fwrite (otemp, 1, 3, outfile) != 3 ||
fwrite ((char *) eolstring, 1,
strlen((char *) eolstring), outfile) != strlen ((char *) eolstring)) {
return UURET_IOERR;
}
}
optr = otemp;
llen = 0;
}
else {
*optr++ = '=';
*optr++ = HexEncodeTable[itemp[index] >> 4];
*optr++ = HexEncodeTable[itemp[index] & 0x0f];
llen += 3;
}
/*
* Lines must be shorter than 76 characters (not counting CRLF).
* If the line grows longer than that, we must include a soft
* line break.
*/
if (itemp[index+1] != 0 && itemp[index+1] != '\n' &&
(llen >= 75 ||
(!((itemp[index+1] >= 33 && itemp[index+1] <= 60) ||
(itemp[index+1] >= 62 && itemp[index+1] <= 126)) &&
llen >= 73))) {
*optr++ = '=';
llen++;
if (fwrite (otemp, 1, llen, outfile) != llen ||
fwrite ((char *) eolstring, 1,
strlen((char *) eolstring), outfile) != strlen ((char *) eolstring)) {
return UURET_IOERR;
}
optr = otemp;
llen = 0;
}
}
2001-11-01 17:20:18 +00:00
}
line++;
}
return UURET_OK;
}
2002-06-10 06:13:37 +00:00
/*
* Special handling for yEnc
*/
if (encoding == YENC_ENCODED) {
llen = 0;
optr = otemp;
while (!feof (infile) && (linperfile <= 0 || line < linperfile)) {
if ((count = fread (itemp, 1, 128, infile)) != 128) {
if (count == 0) {
break;
}
else if (ferror (infile)) {
return UURET_IOERR;
}
2002-06-10 06:13:37 +00:00
}
if (pcrc)
*pcrc = crc32(*pcrc, itemp, count);
2002-06-10 06:13:37 +00:00
if (crc)
*crc = crc32(*crc, itemp, count);
2002-06-10 06:13:37 +00:00
line++;
/*
* Busy Callback
*/
2005-08-29 19:49:00 +00:00
2002-06-10 06:13:37 +00:00
if (UUBUSYPOLL(ftell(infile)-progress.foffset,progress.fsize)) {
UUMessage (uuencode_id, __LINE__, UUMSG_NOTE,
uustring (S_ENCODE_CANCEL));
return UURET_CANCEL;
2002-06-10 06:13:37 +00:00
}
for (index=0; index<count; index++) {
if (llen > 127) {
if (fwrite (otemp, 1, llen, outfile) != llen ||
fwrite ((char *) eolstring, 1,
strlen((char *) eolstring), outfile) != strlen ((char *) eolstring)) {
return UURET_IOERR;
}
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;
}
2002-06-10 06:13:37 +00:00
}
}
/*
* write last line
*/
if (llen) {
if (fwrite (otemp, 1, llen, outfile) != llen ||
fwrite ((char *) eolstring, 1,
strlen((char *) eolstring), outfile) != strlen ((char *) eolstring)) {
return UURET_IOERR;
2002-06-10 06:13:37 +00:00
}
}
return UURET_OK;
}
2001-11-01 17:20:18 +00:00
/*
* Handling for binary encodings
*/
2000-02-25 10:15:17 +00:00
/*
* select charset
*/
table = etables[encoding];
if (table==NULL || bpl[encoding]==0) {
UUMessage (uuencode_id, __LINE__, UUMSG_ERROR,
2001-11-01 17:20:18 +00:00
uustring (S_PARM_CHECK), "UUEncodeStream()");
2000-02-25 10:15:17 +00:00
return UURET_ILLVAL;
}
while (!feof (infile) && (linperfile <= 0 || line < linperfile)) {
if ((count = fread (itemp, 1, bpl[encoding], infile)) != bpl[encoding]) {
if (count == 0)
break;
2000-02-25 10:15:17 +00:00
else if (ferror (infile))
return UURET_IOERR;
2000-02-25 10:15:17 +00:00
}
2001-11-01 17:20:18 +00:00
2000-02-25 10:15:17 +00:00
optr = otemp;
llen = 0;
/*
* Busy Callback
*/
if (UUBUSYPOLL(ftell(infile)-progress.foffset,progress.fsize)) {
UUMessage (uuencode_id, __LINE__, UUMSG_NOTE,
uustring (S_ENCODE_CANCEL));
2000-02-25 10:15:17 +00:00
return UURET_CANCEL;
}
/*
* for UU and XX, encode the number of bytes as first character
*/
2001-11-01 17:20:18 +00:00
2000-02-25 10:15:17 +00:00
if (encoding == UU_ENCODED || encoding == XX_ENCODED) {
*optr++ = table[count];
llen++;
}
/*
2001-11-01 17:20:18 +00:00
* Main encoding
2000-02-25 10:15:17 +00:00
*/
2001-11-01 17:20:18 +00:00
2002-06-10 06:13:37 +00:00
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];
}
2001-11-01 17:20:18 +00:00
2002-06-10 06:13:37 +00:00
/*
* Special handling for incomplete lines
*/
2001-11-01 17:20:18 +00:00
2002-06-10 06:13:37 +00:00
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;
2002-06-10 06:13:37 +00:00
}
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;
2000-02-25 10:15:17 +00:00
}
}
2001-11-01 17:20:18 +00:00
2000-02-25 10:15:17 +00:00
/*
* end of line
*/
2001-11-01 17:20:18 +00:00
2002-06-10 06:13:37 +00:00
tptr = eolstring;
2000-02-25 10:15:17 +00:00
while (*tptr)
*optr++ = *tptr++;
*optr++ = '\0';
2002-06-10 06:13:37 +00:00
llen += strlen ((char *) eolstring);
2000-02-25 10:15:17 +00:00
if (fwrite (otemp, 1, llen, outfile) != llen)
return UURET_IOERR;
line++;
}
return UURET_OK;
}
/*
* Encode as MIME multipart/mixed sub-message.
*/
int UUEXPORT
UUEncodeMulti (FILE *outfile, FILE *infile, char *infname, int encoding,
char *outfname, char *mimetype, int filemode)
2000-02-25 10:15:17 +00:00
{
mimemap *miter=mimetable;
struct stat finfo;
int res, themode;
FILE *theifile;
char *ptr;
2002-06-10 06:13:37 +00:00
crc32_t crc;
crc32_t *crcptr=NULL;
2000-02-25 10:15:17 +00:00
2005-08-29 19:49:00 +00:00
if (outfile==NULL ||
2000-02-25 10:15:17 +00:00
(infile == NULL && infname==NULL) ||
(outfname==NULL && infname==NULL) ||
2001-11-01 17:20:18 +00:00
(encoding!=UU_ENCODED&&encoding!=XX_ENCODED&&encoding!=B64ENCODED&&
2002-06-10 06:13:37 +00:00
encoding!=PT_ENCODED&&encoding!=QP_ENCODED&&encoding!=YENC_ENCODED)) {
2000-02-25 10:15:17 +00:00
UUMessage (uuencode_id, __LINE__, UUMSG_ERROR,
uustring (S_PARM_CHECK), "UUEncodeMulti()");
2000-02-25 10:15:17 +00:00
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));
2000-02-25 10:15:17 +00:00
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));
2000-02-25 10:15:17 +00:00
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;
}
2002-06-10 06:13:37 +00:00
if (progress.fsize < 0)
2000-02-25 10:15:17 +00:00
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++;
2000-02-25 10:15:17 +00:00
mimetype = miter->mimetype;
}
}
2001-11-01 17:20:18 +00:00
if (mimetype == NULL && (encoding == PT_ENCODED || encoding == QP_ENCODED)) {
mimetype = "text/plain";
}
2000-02-25 10:15:17 +00:00
/*
* print sub-header
*/
2002-06-10 06:13:37 +00:00
if (encoding != YENC_ENCODED) {
fprintf (outfile, "Content-Type: %s%s",
(mimetype)?mimetype:"Application/Octet-Stream",
(char *) eolstring);
2002-06-10 06:13:37 +00:00
fprintf (outfile, "Content-Transfer-Encoding: %s%s",
CTE_TYPE(encoding), (char *) eolstring);
2002-06-10 06:13:37 +00:00
fprintf (outfile, "Content-Disposition: attachment; filename=\"%s\"%s",
UUFNameFilter ((outfname)?outfname:infname), (char *) eolstring);
2002-06-10 06:13:37 +00:00
fprintf (outfile, "%s", (char *) eolstring);
}
2000-02-25 10:15:17 +00:00
if (encoding == UU_ENCODED || encoding == XX_ENCODED) {
fprintf (outfile, "begin %o %s%s",
(themode) ? themode : 0644,
UUFNameFilter ((outfname)?outfname:infname),
(char *) eolstring);
2000-02-25 10:15:17 +00:00
}
2002-06-10 06:13:37 +00:00
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),
(char *) eolstring);
2002-06-10 06:13:37 +00:00
}
else {
fprintf (outfile, "=ybegin line=128 size=%ld name=%s%s",
progress.fsize,
UUFNameFilter ((outfname)?outfname:infname),
(char *) eolstring);
2002-06-10 06:13:37 +00:00
}
}
if ((res = UUEncodeStream (outfile, theifile, encoding, 0, crcptr, NULL)) != UURET_OK) {
2000-02-25 10:15:17 +00:00
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));
2000-02-25 10:15:17 +00:00
}
progress.action = 0;
return res;
}
2002-06-10 06:13:37 +00:00
2000-02-25 10:15:17 +00:00
if (encoding == UU_ENCODED || encoding == XX_ENCODED) {
2005-08-29 19:49:00 +00:00
fprintf (outfile, "%c%s",
(encoding==UU_ENCODED) ? UUEncodeTable[0] : XXEncodeTable[0],
(char *) eolstring);
2002-06-10 06:13:37 +00:00
fprintf (outfile, "end%s", (char *) eolstring);
}
else if (encoding == YENC_ENCODED) {
if (progress.fsize == -1) {
fprintf (outfile, "=yend crc32=%08lx%s",
crc,
(char *) eolstring);
2002-06-10 06:13:37 +00:00
}
else {
fprintf (outfile, "=yend size=%ld crc32=%08lx%s",
progress.fsize,
crc,
(char *) eolstring);
2002-06-10 06:13:37 +00:00
}
2000-02-25 10:15:17 +00:00
}
2002-06-10 06:13:37 +00:00
2000-02-25 10:15:17 +00:00
/*
* empty line at end does no harm
*/
2002-06-10 06:13:37 +00:00
fprintf (outfile, "%s", (char *) eolstring);
2000-02-25 10:15:17 +00:00
if (infile==NULL)
fclose (theifile);
progress.action = 0;
return UURET_OK;
}
/*
* Encode as MIME message/partial
*/
int UUEXPORT
UUEncodePartial (FILE *outfile, FILE *infile,
char *infname, int encoding,
char *outfname, char *mimetype,
int filemode, int partno, long linperfile,
crc32_t *crcptr)
2000-02-25 10:15:17 +00:00
{
mimemap *miter=mimetable;
static FILE *theifile;
2002-06-10 06:13:37 +00:00
int themode, numparts = 1;
2000-02-25 10:15:17 +00:00
struct stat finfo;
long thesize;
char *ptr;
int res;
2002-06-10 06:13:37 +00:00
crc32_t pcrc;
crc32_t *pcrcptr=NULL;
2000-02-25 10:15:17 +00:00
if ((outfname==NULL&&infname==NULL) || partno<=0 ||
(infile == NULL&&infname==NULL) || outfile==NULL ||
2001-11-01 17:20:18 +00:00
(encoding!=UU_ENCODED&&encoding!=XX_ENCODED&&encoding!=B64ENCODED&&
2002-06-10 06:13:37 +00:00
encoding!=PT_ENCODED&&encoding!=QP_ENCODED&&encoding!=YENC_ENCODED)) {
2000-02-25 10:15:17 +00:00
UUMessage (uuencode_id, __LINE__, UUMSG_ERROR,
uustring (S_PARM_CHECK), "UUEncodePartial()");
2000-02-25 10:15:17 +00:00
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;
2000-02-25 10:15:17 +00:00
}
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;
2000-02-25 10:15:17 +00:00
}
if (linperfile <= 0)
numparts = 1;
2005-08-29 19:49:00 +00:00
else
numparts = (int) (((long)finfo.st_size+(linperfile*bpl[encoding]-1))/
(linperfile*bpl[encoding]));
2000-02-25 10:15:17 +00:00
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;
2000-02-25 10:15:17 +00:00
}
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;
2000-02-25 10:15:17 +00:00
}
theifile = infile;
}
_FP_strncpy (progress.curfile, (outfname)?outfname:infname, 256);
2002-06-10 06:13:37 +00:00
progress.totsize = (thesize>=0) ? thesize : -1;
2000-02-25 10:15:17 +00:00
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;
2000-02-25 10:15:17 +00:00
}
}
2001-11-01 17:20:18 +00:00
if (mimetype == NULL && (encoding==PT_ENCODED || encoding==QP_ENCODED)) {
mimetype = "text/plain";
}
2000-02-25 10:15:17 +00:00
/*
* print sub-header
*/
2002-06-10 06:13:37 +00:00
if (encoding != YENC_ENCODED) {
fprintf (outfile, "MIME-Version: 1.0%s", (char *) eolstring);
fprintf (outfile, "Content-Type: %s%s",
(mimetype)?mimetype:"Application/Octet-Stream",
(char *) eolstring);
2002-06-10 06:13:37 +00:00
fprintf (outfile, "Content-Transfer-Encoding: %s%s",
CTE_TYPE(encoding), (char *) eolstring);
2002-06-10 06:13:37 +00:00
fprintf (outfile, "Content-Disposition: attachment; filename=\"%s\"%s",
UUFNameFilter ((outfname)?outfname:infname), (char *) eolstring);
2002-06-10 06:13:37 +00:00
}
2000-02-25 10:15:17 +00:00
2002-06-10 06:13:37 +00:00
fprintf (outfile, "%s", (char *) eolstring);
2005-08-29 19:49:00 +00:00
2000-02-25 10:15:17 +00:00
/*
* for the first part of UU or XX messages, print a begin line
*/
2002-06-10 06:13:37 +00:00
2000-02-25 10:15:17 +00:00
if (encoding == UU_ENCODED || encoding == XX_ENCODED) {
fprintf (outfile, "begin %o %s%s",
(themode) ? themode : ((filemode)?filemode:0644),
UUFNameFilter ((outfname)?outfname:infname), (char *) eolstring);
2002-06-10 06:13:37 +00:00
}
}
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),
(char *) eolstring);
2002-06-10 06:13:37 +00:00
}
else {
fprintf (outfile, "=ybegin part=%d line=128 size=%ld name=%s%s",
partno,
progress.totsize,
UUFNameFilter ((outfname)?outfname:infname),
(char *) eolstring);
2002-06-10 06:13:37 +00:00
}
fprintf (outfile, "=ypart begin=%ld end=%ld%s",
(partno-1)*linperfile*128+1,
(partno*linperfile*128) < progress.totsize ?
(partno*linperfile*128) : progress.totsize,
(char *) eolstring);
2002-06-10 06:13:37 +00:00
}
else {
if (progress.totsize == -1) {
fprintf (outfile, "=ybegin line=128 name=%s%s",
UUFNameFilter ((outfname)?outfname:infname),
(char *) eolstring);
2002-06-10 06:13:37 +00:00
}
else {
fprintf (outfile, "=ybegin line=128 size=%ld name=%s%s",
progress.totsize,
UUFNameFilter ((outfname)?outfname:infname),
(char *) eolstring);
2002-06-10 06:13:37 +00:00
}
2000-02-25 10:15:17 +00:00
}
}
/*
* 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;
2002-06-10 06:13:37 +00:00
if ((res = UUEncodeStream (outfile, theifile, encoding, linperfile,
crcptr, pcrcptr)) != UURET_OK) {
2000-02-25 10:15:17 +00:00
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));
2000-02-25 10:15:17 +00:00
}
progress.action = 0;
return res;
}
2002-06-10 06:13:37 +00:00
2000-02-25 10:15:17 +00:00
/*
* print end line
*/
2002-06-10 06:13:37 +00:00
2000-02-25 10:15:17 +00:00
if (feof (theifile) &&
(encoding == UU_ENCODED || encoding == XX_ENCODED)) {
2005-08-29 19:49:00 +00:00
fprintf (outfile, "%c%s",
(encoding==UU_ENCODED) ? UUEncodeTable[0] : XXEncodeTable[0],
(char *) eolstring);
2002-06-10 06:13:37 +00:00
fprintf (outfile, "end%s", (char *) eolstring);
}
else if (encoding == YENC_ENCODED) {
if (numparts != 1) {
fprintf (outfile, "=yend size=%ld part=%d pcrc32=%08lx",
(partno*linperfile*128) < progress.totsize ?
linperfile*128 : (progress.totsize-(partno-1)*linperfile*128),
partno,
pcrc);
2002-06-10 06:13:37 +00:00
}
else {
fprintf (outfile, "=yend size=%ld",
progress.totsize);
2002-06-10 06:13:37 +00:00
}
if (feof (theifile))
fprintf (outfile, " crc32=%08lx", *crcptr);
fprintf (outfile, "%s", (char *) eolstring);
2000-02-25 10:15:17 +00:00
}
2001-11-01 17:20:18 +00:00
2000-02-25 10:15:17 +00:00
/*
* empty line at end does no harm
*/
2001-11-01 17:20:18 +00:00
if (encoding != PT_ENCODED && encoding != QP_ENCODED) {
2002-06-10 06:13:37 +00:00
fprintf (outfile, "%s", (char *) eolstring);
2001-11-01 17:20:18 +00:00
}
2000-02-25 10:15:17 +00:00
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 UUEXPORT
UUEncodeToStream (FILE *outfile, FILE *infile,
char *infname, int encoding,
char *outfname, int filemode)
2000-02-25 10:15:17 +00:00
{
struct stat finfo;
FILE *theifile;
int themode;
int res;
2002-06-10 06:13:37 +00:00
crc32_t crc;
crc32_t *crcptr=NULL;
2000-02-25 10:15:17 +00:00
if (outfile==NULL ||
(infile == NULL&&infname==NULL) ||
(outfname==NULL&&infname==NULL) ||
2001-11-01 17:20:18 +00:00
(encoding!=UU_ENCODED&&encoding!=XX_ENCODED&&encoding!=B64ENCODED&&
2002-06-10 06:13:37 +00:00
encoding!=PT_ENCODED&&encoding!=QP_ENCODED&&encoding!=YENC_ENCODED)) {
2000-02-25 10:15:17 +00:00
UUMessage (uuencode_id, __LINE__, UUMSG_ERROR,
uustring (S_PARM_CHECK), "UUEncodeToStream()");
2000-02-25 10:15:17 +00:00
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));
2000-02-25 10:15:17 +00:00
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));
2000-02-25 10:15:17 +00:00
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;
}
2002-06-10 06:13:37 +00:00
if (progress.fsize < 0)
2000-02-25 10:15:17 +00:00
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),
(char *) eolstring);
2000-02-25 10:15:17 +00:00
}
2002-06-10 06:13:37 +00:00
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),
(char *) eolstring);
2002-06-10 06:13:37 +00:00
}
else {
fprintf (outfile, "=ybegin line=128 size=%ld name=%s%s",
progress.fsize,
UUFNameFilter ((outfname)?outfname:infname),
(char *) eolstring);
2002-06-10 06:13:37 +00:00
}
}
if ((res = UUEncodeStream (outfile, theifile, encoding, 0, crcptr, NULL)) != UURET_OK) {
2000-02-25 10:15:17 +00:00
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));
2000-02-25 10:15:17 +00:00
}
progress.action = 0;
return res;
}
2002-06-10 06:13:37 +00:00
2000-02-25 10:15:17 +00:00
if (encoding == UU_ENCODED || encoding == XX_ENCODED) {
2005-08-29 19:49:00 +00:00
fprintf (outfile, "%c%s",
(encoding==UU_ENCODED) ? UUEncodeTable[0] : XXEncodeTable[0],
(char *) eolstring);
2002-06-10 06:13:37 +00:00
fprintf (outfile, "end%s", (char *) eolstring);
2000-02-25 10:15:17 +00:00
}
2002-06-10 06:13:37 +00:00
else if (encoding == YENC_ENCODED) {
if (progress.fsize == -1) {
fprintf (outfile, "=yend crc32=%08lx%s",
crc,
(char *) eolstring);
2002-06-10 06:13:37 +00:00
}
else {
fprintf (outfile, "=yend size=%ld crc32=%08lx%s",
progress.fsize,
crc,
(char *) eolstring);
2002-06-10 06:13:37 +00:00
}
}
2000-02-25 10:15:17 +00:00
/*
* empty line at end does no harm
*/
2002-06-10 06:13:37 +00:00
fprintf (outfile, "%s", (char *) eolstring);
2000-02-25 10:15:17 +00:00
if (infile==NULL) fclose (theifile);
progress.action = 0;
return UURET_OK;
}
/*
* Encode to files on disk, don't generate any headers
*/
int UUEXPORT
UUEncodeToFile (FILE *infile, char *infname, int encoding,
char *outfname, char *diskname, long linperfile)
2000-02-25 10:15:17 +00:00
{
int part, numparts, len, filemode, res;
char *oname=NULL, *optr, *ptr;
FILE *theifile, *outfile;
struct stat finfo;
2002-06-10 06:13:37 +00:00
crc32_t pcrc, crc;
crc32_t *pcrcptr=NULL, *crcptr=NULL;
2000-02-25 10:15:17 +00:00
if ((diskname==NULL&&infname==NULL) ||
(outfname==NULL&&infname==NULL) || (infile==NULL&&infname==NULL) ||
2001-11-01 17:20:18 +00:00
(encoding!=UU_ENCODED&&encoding!=XX_ENCODED&&encoding!=B64ENCODED&&
2002-06-10 06:13:37 +00:00
encoding!=PT_ENCODED&&encoding!=QP_ENCODED&&encoding!=YENC_ENCODED)) {
2000-02-25 10:15:17 +00:00
UUMessage (uuencode_id, __LINE__, UUMSG_ERROR,
uustring (S_PARM_CHECK), "UUEncodeToFile()");
2000-02-25 10:15:17 +00:00
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;
2000-02-25 10:15:17 +00:00
}
sprintf (oname, "%s", diskname);
}
else {
2005-08-29 19:49:00 +00:00
len = ((uusavepath)?strlen(uusavepath):0) + strlen (diskname)
+ ((uuencodeext)?strlen(uuencodeext):0) + 5;
2000-02-25 10:15:17 +00:00
if ((oname = malloc (len)) == NULL) {
UUMessage (uuencode_id, __LINE__, UUMSG_ERROR,
uustring (S_OUT_OF_MEMORY), len);
return UURET_NOMEM;
2000-02-25 10:15:17 +00:00
}
sprintf (oname, "%s%s", (uusavepath)?uusavepath:"", diskname);
}
}
else {
2005-08-29 19:49:00 +00:00
len = ((uusavepath) ? strlen (uusavepath) : 0) +
2000-02-25 10:15:17 +00:00
strlen(UUFNameFilter(infname)) +
((uuencodeext)?strlen(uuencodeext):0) + 5;
2000-02-25 10:15:17 +00:00
if ((oname = malloc (len)) == NULL) {
UUMessage (uuencode_id, __LINE__, UUMSG_ERROR,
uustring (S_OUT_OF_MEMORY), len);
2000-02-25 10:15:17 +00:00
return UURET_NOMEM;
}
optr = UUFNameFilter (infname);
2005-08-29 19:49:00 +00:00
sprintf (oname, "%s%s",
(uusavepath)?uusavepath:"",
(*optr=='.')?optr+1:optr);
2000-02-25 10:15:17 +00:00
}
/*
* 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));
2000-02-25 10:15:17 +00:00
_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));
2000-02-25 10:15:17 +00:00
_FP_free (oname);
return UURET_IOERR;
}
if (linperfile <= 0)
numparts = 1;
2005-08-29 19:49:00 +00:00
else
2000-02-25 10:15:17 +00:00
numparts = (int) (((long)finfo.st_size + (linperfile*bpl[encoding]-1)) /
(linperfile*bpl[encoding]));
2000-02-25 10:15:17 +00:00
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;
2000-02-25 10:15:17 +00:00
else
numparts = (int) (((long)finfo.st_size+(linperfile*bpl[encoding]-1))/
(linperfile*bpl[encoding]));
2000-02-25 10:15:17 +00:00
filemode = (int) finfo.st_mode & 0777;
progress.totsize = -1;
}
theifile = infile;
}
_FP_strncpy (progress.curfile, (outfname)?outfname:infname, 256);
2002-06-10 06:13:37 +00:00
progress.totsize = (progress.totsize<0) ? -1 : progress.totsize;
2000-02-25 10:15:17 +00:00
progress.numparts = numparts;
for (part=1; !feof (theifile); part++) {
/*
* Attach extension
*/
if (progress.numparts==1 && progress.totsize!=-1 && uuencodeext!=NULL)
strcpy (optr, uuencodeext);
2005-08-29 19:49:00 +00:00
else
2000-02-25 10:15:17 +00:00
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;
2000-02-25 10:15:17 +00:00
}
}
/*
* 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)
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));
2000-02-25 10:15:17 +00:00
if (infile==NULL) fclose (theifile);
progress.action = 0;
free (oname);
return UURET_IOERR;
}
2002-06-10 06:13:37 +00:00
if (encoding != YENC_ENCODED) {
fprintf (outfile, "%s", (char *) eolstring);
fprintf (outfile, "_=_ %s", (char *) eolstring);
if (numparts == -1)
fprintf (outfile, "_=_ Part %03d of file %s%s",
part, UUFNameFilter ((outfname)?outfname:infname),
(char *) eolstring);
2002-06-10 06:13:37 +00:00
else
fprintf (outfile, "_=_ Part %03d of %03d of file %s%s",
part, numparts,
UUFNameFilter ((outfname)?outfname:infname),
(char *) eolstring);
2002-06-10 06:13:37 +00:00
fprintf (outfile, "_=_ %s", (char *) eolstring);
fprintf (outfile, "%s", (char *) eolstring);
}
2000-02-25 10:15:17 +00:00
if (part==1 && (encoding == UU_ENCODED || encoding == XX_ENCODED)) {
fprintf (outfile, "begin %o %s%s",
(filemode)?filemode : 0644,
UUFNameFilter ((outfname)?outfname:infname),
(char *) eolstring);
2000-02-25 10:15:17 +00:00
}
2002-06-10 06:13:37 +00:00
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),
(char *) eolstring);
}
else {
fprintf (outfile, "=ybegin part=%d line=128 size=%ld name=%s%s",
part,
progress.totsize,
UUFNameFilter ((outfname)?outfname:infname),
(char *) eolstring);
}
fprintf (outfile, "=ypart begin=%ld end=%ld%s",
(part-1)*linperfile*128+1,
(part*linperfile*128) < progress.totsize ?
(part*linperfile*128) : progress.totsize,
(char *) eolstring);
2002-06-10 06:13:37 +00:00
}
else {
if (progress.totsize == -1) {
fprintf (outfile, "=ybegin line=128 name=%s%s",
UUFNameFilter ((outfname)?outfname:infname),
(char *) eolstring);
}
else {
fprintf (outfile, "=ybegin line=128 size=%ld name=%s%s",
progress.totsize,
UUFNameFilter ((outfname)?outfname:infname),
(char *) eolstring);
}
2002-06-10 06:13:37 +00:00
}
}
2000-02-25 10:15:17 +00:00
if ((res = UUEncodeStream (outfile, theifile,
encoding, linperfile, crcptr, pcrcptr)) != UURET_OK) {
2000-02-25 10:15:17 +00:00
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));
2000-02-25 10:15:17 +00:00
}
if (infile==NULL) fclose (theifile);
progress.action = 0;
fclose (outfile);
unlink (oname);
_FP_free (oname);
return res;
}
2002-06-10 06:13:37 +00:00
2000-02-25 10:15:17 +00:00
if (feof (theifile) &&
(encoding == UU_ENCODED || encoding == XX_ENCODED)) {
2005-08-29 19:49:00 +00:00
fprintf (outfile, "%c%s",
(encoding==UU_ENCODED) ? UUEncodeTable[0] : XXEncodeTable[0],
(char *) eolstring);
2002-06-10 06:13:37 +00:00
fprintf (outfile, "end%s", (char *) eolstring);
}
else if (encoding == YENC_ENCODED) {
if (numparts != 1) {
fprintf (outfile, "=yend size=%ld part=%d pcrc32=%08lx",
(part*linperfile*128) < progress.totsize ?
linperfile*128 : (progress.totsize-(part-1)*linperfile*128),
part,
pcrc);
2002-06-10 06:13:37 +00:00
}
else {
fprintf (outfile, "=yend size=%ld",
progress.totsize);
2002-06-10 06:13:37 +00:00
}
if (feof (theifile))
fprintf (outfile, " crc32=%08lx", crc);
2002-06-10 06:13:37 +00:00
fprintf (outfile, "%s", (char *) eolstring);
2000-02-25 10:15:17 +00:00
}
2002-06-10 06:13:37 +00:00
2000-02-25 10:15:17 +00:00
/*
* empty line at end does no harm
*/
2002-06-10 06:13:37 +00:00
fprintf (outfile, "%s", (char *) eolstring);
2000-02-25 10:15:17 +00:00
fclose (outfile);
}
2002-06-10 06:13:37 +00:00
2000-02-25 10:15:17 +00:00
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 UUEXPORT
UUE_PrepSingle (FILE *outfile, FILE *infile,
char *infname, int encoding,
char *outfname, int filemode,
char *destination, char *from,
char *subject, int isemail)
2001-11-01 17:20:18 +00:00
{
return UUE_PrepSingleExt (outfile, infile,
infname, encoding,
outfname, filemode,
destination, from,
subject, NULL,
isemail);
2001-11-01 17:20:18 +00:00
}
int UUEXPORT
UUE_PrepSingleExt (FILE *outfile, FILE *infile,
char *infname, int encoding,
char *outfname, int filemode,
char *destination, char *from,
char *subject, char *replyto,
int isemail)
2000-02-25 10:15:17 +00:00
{
mimemap *miter=mimetable;
char *subline, *oname;
char *mimetype, *ptr;
int res, len;
if ((outfname==NULL&&infname==NULL) || (infile==NULL&&infname==NULL) ||
2001-11-01 17:20:18 +00:00
(encoding!=UU_ENCODED&&encoding!=XX_ENCODED&&encoding!=B64ENCODED&&
2002-06-10 06:13:37 +00:00
encoding!=PT_ENCODED&&encoding!=QP_ENCODED&&encoding!=YENC_ENCODED)) {
2000-02-25 10:15:17 +00:00
UUMessage (uuencode_id, __LINE__, UUMSG_ERROR,
uustring (S_PARM_CHECK), "UUE_PrepSingle()");
2000-02-25 10:15:17 +00:00
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;
2001-11-01 17:20:18 +00:00
if (mimetype == NULL && (encoding == PT_ENCODED || encoding == QP_ENCODED)) {
mimetype = "text/plain";
}
2000-02-25 10:15:17 +00:00
if ((subline = (char *) malloc (len)) == NULL) {
UUMessage (uuencode_id, __LINE__, UUMSG_ERROR,
uustring (S_OUT_OF_MEMORY), len);
2000-02-25 10:15:17 +00:00
return UURET_NOMEM;
}
2002-06-10 06:13:37 +00:00
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);
}
2000-02-25 10:15:17 +00:00
if (from) {
2002-06-10 06:13:37 +00:00
fprintf (outfile, "From: %s%s", from, (char *) eolstring);
2000-02-25 10:15:17 +00:00
}
if (destination) {
fprintf (outfile, "%s: %s%s",
(isemail)?"To":"Newsgroups",
destination, (char *) eolstring);
2000-02-25 10:15:17 +00:00
}
2001-11-01 17:20:18 +00:00
2002-06-10 06:13:37 +00:00
fprintf (outfile, "Subject: %s%s", subline, (char *) eolstring);
2001-11-01 17:20:18 +00:00
if (replyto) {
2002-06-10 06:13:37 +00:00
fprintf (outfile, "Reply-To: %s%s", replyto, (char *) eolstring);
2001-11-01 17:20:18 +00:00
}
2002-06-10 06:13:37 +00:00
if (encoding != YENC_ENCODED) {
fprintf (outfile, "MIME-Version: 1.0%s", (char *) eolstring);
fprintf (outfile, "Content-Type: %s; name=\"%s\"%s",
(mimetype)?mimetype:"Application/Octet-Stream",
UUFNameFilter ((outfname)?outfname:infname),
(char *) eolstring);
2002-06-10 06:13:37 +00:00
fprintf (outfile, "Content-Transfer-Encoding: %s%s",
CTE_TYPE(encoding), (char *) eolstring);
2002-06-10 06:13:37 +00:00
}
fprintf (outfile, "%s", (char *) eolstring);
2000-02-25 10:15:17 +00:00
res = UUEncodeToStream (outfile, infile, infname, encoding,
outfname, filemode);
2005-08-29 19:49:00 +00:00
2000-02-25 10:15:17 +00:00
_FP_free (subline);
return res;
}
int UUEXPORT
UUE_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)
2001-11-01 17:20:18 +00:00
{
return UUE_PrepPartialExt (outfile, infile,
infname, encoding,
outfname, filemode,
partno, linperfile, filesize,
destination,
from, subject, NULL,
isemail);
2001-11-01 17:20:18 +00:00
}
int UUEXPORT
UUE_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)
2000-02-25 10:15:17 +00:00
{
static int numparts, themode;
static char mimeid[64];
static FILE *theifile;
struct stat finfo;
char *subline, *oname;
long thesize;
int res, len;
2002-06-10 06:13:37 +00:00
static crc32_t crc;
crc32_t *crcptr=NULL;
2000-02-25 10:15:17 +00:00
if ((outfname==NULL&&infname==NULL) || (infile==NULL&&infname==NULL) ||
2001-11-01 17:20:18 +00:00
(encoding!=UU_ENCODED&&encoding!=XX_ENCODED&&encoding!=B64ENCODED&&
2002-06-10 06:13:37 +00:00
encoding!=PT_ENCODED&&encoding!=QP_ENCODED&&encoding!=YENC_ENCODED)) {
2000-02-25 10:15:17 +00:00
UUMessage (uuencode_id, __LINE__, UUMSG_ERROR,
uustring (S_PARM_CHECK), "UUE_PrepPartial()");
2000-02-25 10:15:17 +00:00
return UURET_ILLVAL;
}
oname = UUFNameFilter ((outfname)?outfname:infname);
len = ((subject)?strlen(subject):0) + strlen (oname) + 40;
/*
* if first part, get information about the file
*/
2002-06-10 06:13:37 +00:00
2000-02-25 10:15:17 +00:00
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;
2000-02-25 10:15:17 +00:00
}
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;
2000-02-25 10:15:17 +00:00
}
if (linperfile <= 0)
numparts = 1;
2005-08-29 19:49:00 +00:00
else
numparts = (int) (((long)finfo.st_size+(linperfile*bpl[encoding]-1))/
(linperfile*bpl[encoding]));
2000-02-25 10:15:17 +00:00
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;
}
2000-02-25 10:15:17 +00:00
}
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;
2000-02-25 10:15:17 +00:00
}
theifile = infile;
}
2002-06-10 06:13:37 +00:00
2000-02-25 10:15:17 +00:00
/*
* if there's one part only, don't use Message/Partial
*/
if (numparts == 1) {
if (infile==NULL) fclose (theifile);
2001-11-01 17:20:18 +00:00
return UUE_PrepSingleExt (outfile, infile, infname, encoding,
outfname, filemode, destination,
from, subject, replyto, isemail);
2000-02-25 10:15:17 +00:00
}
/*
* we also need a unique ID
*/
2002-06-10 06:13:37 +00:00
2000-02-25 10:15:17 +00:00
sprintf (mimeid, "UUDV-%ld.%ld.%s",
(long) time(NULL), thesize,
(strlen(oname)>16)?"oops":oname);
2000-02-25 10:15:17 +00:00
}
if ((subline = (char *) malloc (len)) == NULL) {
UUMessage (uuencode_id, __LINE__, UUMSG_ERROR,
uustring (S_OUT_OF_MEMORY), len);
2000-02-25 10:15:17 +00:00
if (infile==NULL) fclose (theifile);
return UURET_NOMEM;
}
2002-06-10 06:13:37 +00:00
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);
2002-06-10 06:13:37 +00:00
else
sprintf (subline, "- %s - (%03d/%03d)", oname,
partno, numparts);
2002-06-10 06:13:37 +00:00
}
else {
if (subject)
2005-08-29 19:49:00 +00:00
sprintf (subline, "%s (%03d/%03d) - [ %s ]",
subject, partno, numparts, oname);
2002-06-10 06:13:37 +00:00
else
sprintf (subline, "[ %s ] (%03d/%03d)",
oname, partno, numparts);
2002-06-10 06:13:37 +00:00
}
2000-02-25 10:15:17 +00:00
if (from) {
2002-06-10 06:13:37 +00:00
fprintf (outfile, "From: %s%s", from, (char *) eolstring);
2000-02-25 10:15:17 +00:00
}
2001-11-01 17:20:18 +00:00
2000-02-25 10:15:17 +00:00
if (destination) {
fprintf (outfile, "%s: %s%s",
(isemail)?"To":"Newsgroups",
destination, (char *) eolstring);
2000-02-25 10:15:17 +00:00
}
2001-11-01 17:20:18 +00:00
2002-06-10 06:13:37 +00:00
fprintf (outfile, "Subject: %s%s", subline, (char *) eolstring);
2001-11-01 17:20:18 +00:00
if (replyto) {
2002-06-10 06:13:37 +00:00
fprintf (outfile, "Reply-To: %s%s", replyto, (char *) eolstring);
2001-11-01 17:20:18 +00:00
}
2002-06-10 06:13:37 +00:00
if (encoding != YENC_ENCODED) {
fprintf (outfile, "MIME-Version: 1.0%s", (char *) eolstring);
fprintf (outfile, "Content-Type: Message/Partial; number=%d; total=%d;%s",
partno, numparts, (char *) eolstring);
2002-06-10 06:13:37 +00:00
fprintf (outfile, "\tid=\"%s\"%s",
mimeid, (char *) eolstring);
2002-06-10 06:13:37 +00:00
}
2005-08-29 19:49:00 +00:00
2002-06-10 06:13:37 +00:00
fprintf (outfile, "%s", (char *) eolstring);
2000-02-25 10:15:17 +00:00
res = UUEncodePartial (outfile, theifile,
infname, encoding,
(outfname)?outfname:infname, NULL,
themode, partno, linperfile, crcptr);
2000-02-25 10:15:17 +00:00
_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;
}