コード例 #1
0
ファイル: reformime.c プロジェクト: Stanfan/packages-clib
static void do_print_section(struct rfc2045 *s, FILE *fp)
{
off_t start, end, body;
off_t nlines;
off_t nbodylines;

	rfc2045_mimepos(s, &start, &end, &body, &nlines, &nbodylines);

	if (fseek(stdin, body, SEEK_SET) == -1)
	{
		perror("fseek");
		exit(1);
	}

	rfc2045_cdecode_start(s, &decode_to_file, fp);
	while (body < end)
	{
	char	buf[BUFSIZ];
	size_t	n=sizeof(buf);

		if ((off_t)n > end-body)	n=end-body;
		n=fread(buf, 1, n, stdin);
		if (n == 0)
		{
			perror("fread");
			exit(1);
		}
		rfc2045_cdecode(s, buf, n);
		body += n;
	}
	rfc2045_cdecode_end(s);
}
コード例 #2
0
int rfc2045_decodemimesection(struct rfc2045src *src, struct rfc2045 *rfc,
			      int (*handler)(const char *, size_t, void *),
			      void *voidarg)
{
	off_t	start_pos, end_pos, start_body;
	char	buf[BUFSIZ];
	ssize_t	cnt;
	off_t	dummy;
	int	rc;

	rfc2045_mimepos(rfc, &start_pos, &end_pos, &start_body,
		&dummy, &dummy);
	if (SRC_SEEK(src, start_body) == (off_t)-1) return (-1);

        rfc2045_cdecode_start(rfc, handler, voidarg);

        while (start_body < end_pos)
        {
                cnt=sizeof(buf);
                if (cnt > end_pos-start_body)
                        cnt=end_pos-start_body;
                cnt=SRC_READ(src, buf, cnt);
                if (cnt <= 0)   break;
                if ((rc=rfc2045_cdecode(rfc, buf, cnt)) != 0)
			return (rc);
		start_body += cnt;
        }
        return (rfc2045_cdecode_end(rfc));
}
コード例 #3
0
ファイル: rfc2045tryboundary.c プロジェクト: zixia/nospam
static int try_boundary(struct rfc2045 *p, int fd)
{
int	rc;
char	buf[512];
int	n, cnt;
off_t	ps;

	if (p->firstpart)
	{
		for (p=p->firstpart; p; p=p->next)
			if ((rc=try_boundary(p, fd)) != 0)
				return (rc);
		return (0);
	}

	if (p->content_transfer_encoding &&
		strcmp(p->content_transfer_encoding, "base64") == 0)
		return (0);

	boundary_chk_flag=0;
	boundary_chk_buflen=0;

	if (lseek(fd, p->startbody, SEEK_SET) == -1)	return (-1);
	rfc2045_cdecode_start(p, boundary_chk, 0);

	ps=p->startbody;
	while (ps < p->endbody)
	{
		if (p->endbody - ps < sizeof(buf))
			cnt=p->endbody-ps;
		else	cnt=sizeof(buf);
		n=read(fd, buf, cnt);
		if (n <= 0)	return (-1);
		rfc2045_cdecode(p, buf, n);
		ps += n;
		if (boundary_chk_flag)	break;
	}
	rfc2045_cdecode_end(p);
	if (boundary_chk_buflen)
		boundary_chk("\n", 1, 0);	/* Flush out partial line */
	return (boundary_chk_flag);
}
コード例 #4
0
static int dorw(struct rfc2045 *p)
{
/* WTF STATIC??? */ int seen_mime=0;
char	buf[256];
int	c;
int	bcnt;

	if (fseek(fdin, p->startpos, SEEK_SET) == -1)	return (-1);
	if (p->parent)
	{
		seen_mime=1;
		if (rwmime(p))	return (-1);
	}
	while (fgets(buf, sizeof(buf), fdin))
	{
		if (buf[0] == '\n')	break;

		if (RFC2045_ISMIME1DEF(p->mime_version) &&
			strncasecmp(buf, "mime-version:", 13) == 0 &&
			!seen_mime)
		{
			seen_mime=1;
			rwmime(p);
			if (strchr(buf, '\n') == NULL)
				while ((c=getc(fdin)) >= 0 && c != '\n')
					;
			while ((c=getc(fdin)) >= 0 && c != '\n' && isspace(c))
				while ((c=getc(fdin)) >= 0 && c != '\n')
					;
			if (c >= 0)	ungetc(c, fdin);
			continue;
		}

		if (!RFC2045_ISMIME1DEF(p->mime_version) || (
			strncasecmp(buf, "mime-version:", 13) &&
			strncasecmp(buf, "content-type:", 13) &&
			strncasecmp(buf, "content-transfer-encoding:", 26))
			)
		{
			do
			{
				do
				{
					if (fdout_add(buf, strlen(buf)))
						return (-1);
				} while (strchr(buf, '\n') == NULL &&
					fgets(buf, sizeof(buf), fdin));

				c=getc(fdin);
				if (c >= 0)	ungetc(c, fdin);
			} while (c >= 0 && c != '\n' && isspace(c) &&
				    fgets(buf, sizeof(buf), fdin));
		}
		else
			while ( (c=getc(fdin)) >= 0 &&
				(ungetc(c, fdin), c) != '\n' && isspace(c))
			{
				while (fgets(buf, sizeof(buf), fdin) &&
					strchr(buf, '\n') == NULL)
					;
			}
	}
	if (RFC2045_ISMIME1DEF(p->mime_version))
	{
		if (!seen_mime)
			if (rwmime(p))	return (-1);

		if (!p->firstpart && p->rw_transfer_encoding)
			if (fdout_autoconverted(p->content_transfer_encoding,
				p->rw_transfer_encoding))	return (-1);
	}

	if (fdout_add("\n", 1))	return (-1);
	if (fseek(fdin, p->startbody, SEEK_SET) == -1)	return (-1);

	/* For non-multipart section, just print the body */

	if (!p->firstpart)
	{
	off_t	ps=p->startbody;
	int	convmode=0;

		if (p->rw_transfer_encoding)
		{
			if ( strcasecmp(p->rw_transfer_encoding,
				"quoted-printable") == 0)
				convmode=RFC2045_RW_7BIT;
			else
				convmode=RFC2045_RW_8BIT;
		}

		conv_err=0;
		if (convmode == RFC2045_RW_7BIT)
		{
			qpe_start();
			rfc2045_cdecode_start(p, &qpe_do, 0);
		}

		if (convmode == RFC2045_RW_8BIT)
		{
			rfc2045_cdecode_start(p, &do_8bit, 0);
		}

		while (ps < p->endbody)
		{
		int	n;

			if (p->endbody - ps > sizeof(buf))
				n=sizeof(buf);
			else	n=p->endbody-ps;
			n=fread(buf, 1, n, fdin);
			if (n <= 0)	return (-1);
			if (convmode)
				rfc2045_cdecode(p, buf, n);
			else	if (fdout_add(buf, n))	conv_err=1;
			ps += n;
			if (conv_err)	break;
		}
		if (convmode == RFC2045_RW_7BIT)
		{
			rfc2045_cdecode_end(p);
			qpe_end();
		}
		if (convmode == RFC2045_RW_8BIT)
		{
			rfc2045_cdecode_end(p);
		}
		if (conv_err)	return (-1);
		return (0);
	}

	bcnt=rw_boundary_cnt;

	/* Sam 8/30/99 fix - handle message/rfc822:

            --boundary
            Content-Type: message/rfc822

         --><-- we're here, DON'T add RFC2045MIMEMSG and rest of crap here
	*/
	if (p->firstpart->next == 0)
	{
	int	rc;

		p->firstpart->parent=0;
		rc=dorw(p->firstpart);
		p->firstpart->parent=p;
		return (rc);
	}

	if (fdout_add(RFC2045MIMEMSG, sizeof(RFC2045MIMEMSG)-1))
		return (-1);
	for (p=p->firstpart; p; p=p->next)
	{
		if (p->isdummy)	continue;
		sprintf(buf, "\n--%s-%d\n", rw_boundary_root, bcnt);
		if (fdout_add(buf, strlen(buf)))	return (-1);
		if (dorw(p) != 0)	return(-1);
	}
	sprintf(buf, "\n--%s-%d--\n", rw_boundary_root, bcnt);
	if (fdout_add(buf, strlen(buf)))	return (-1);
	return (0);
}