use the base64 encoder from glib.

2007-01-12  Sven Neumann  <sven@gimp.org>

	* plug-ins/common/mail.c: use the base64 encoder from glib.

svn path=/trunk/; revision=21693
This commit is contained in:
Sven Neumann 2007-01-12 14:34:09 +00:00 committed by Sven Neumann
parent 5bf710beec
commit 20d0b45f8d
2 changed files with 65 additions and 121 deletions

View File

@ -1,3 +1,7 @@
2007-01-12 Sven Neumann <sven@gimp.org>
* plug-ins/common/mail.c: use the base64 encoder from glib.
2007-01-12 Sven Neumann <sven@gimp.org> 2007-01-12 Sven Neumann <sven@gimp.org>
* configure.in * configure.in

View File

@ -106,9 +106,9 @@
#pragma align 4 (mail_icon) #pragma align 4 (mail_icon)
#endif #endif
#ifdef __GNUC__ #ifdef __GNUC__
static const guint8 mail_icon[] __attribute__ ((__aligned__ (4))) = static const guint8 mail_icon[] __attribute__ ((__aligned__ (4))) =
#else #else
static const guint8 mail_icon[] = static const guint8 mail_icon[] =
#endif #endif
{ "" { ""
/* Pixbuf magic (0x47646b50) */ /* Pixbuf magic (0x47646b50) */
@ -207,21 +207,16 @@ static void mail_entry_callback (GtkWidget *widget,
static void mesg_body_callback (GtkTextBuffer *buffer, static void mesg_body_callback (GtkTextBuffer *buffer,
gpointer data); gpointer data);
static gboolean valid_file (const gchar *filename); static gboolean valid_file (const gchar *filename);
static void create_headers (FILE *mailpipe); static void create_headers (FILE *mailpipe);
static gchar *find_extension (const gchar *filename); static gchar *find_extension (const gchar *filename);
static gint to64 (FILE *infile, static gboolean to64 (const gchar *filename,
FILE *outfile); FILE *outfile,
static void output64chunk (gint c1, GError **error);
gint c2, static gint sane_dup2 (gint fd1,
gint c3, gint fd2);
gint pads, static FILE *sendmail_pipe (gchar **cmd,
FILE *outfile); gint *pid);
static gint sane_dup2 (gint fd1,
gint fd2);
static FILE *sendmail_pipe (gchar **cmd,
gint *pid);
const GimpPlugInInfo PLUG_IN_INFO = const GimpPlugInInfo PLUG_IN_INFO =
@ -335,11 +330,16 @@ run (const gchar *name,
} }
else else
{ {
g_strlcpy (mail_info.filename, param[3].data.d_string, BUFFER_SIZE); g_strlcpy (mail_info.filename,
g_strlcpy (mail_info.receipt, param[4].data.d_string, BUFFER_SIZE); param[3].data.d_string, BUFFER_SIZE);
g_strlcpy (mail_info.from, param[5].data.d_string, BUFFER_SIZE); g_strlcpy (mail_info.receipt,
g_strlcpy (mail_info.subject, param[6].data.d_string, BUFFER_SIZE); param[4].data.d_string, BUFFER_SIZE);
g_strlcpy (mail_info.comment, param[7].data.d_string, BUFFER_SIZE); g_strlcpy (mail_info.from,
param[5].data.d_string, BUFFER_SIZE);
g_strlcpy (mail_info.subject,
param[6].data.d_string, BUFFER_SIZE);
g_strlcpy (mail_info.comment,
param[7].data.d_string, BUFFER_SIZE);
mail_info.encapsulation = param[8].data.d_int32; mail_info.encapsulation = param[8].data.d_int32;
} }
break; break;
@ -386,7 +386,6 @@ save_image (const gchar *filename,
gint wpid; gint wpid;
gint process_status; gint process_status;
FILE *mailpipe; FILE *mailpipe;
FILE *infile;
ext = find_extension (filename); ext = find_extension (filename);
@ -461,8 +460,14 @@ save_image (const gchar *filename,
else else
{ {
/* This must be MIME stuff. Base64 away... */ /* This must be MIME stuff. Base64 away... */
infile = g_fopen (tmpname, "r"); GError *error = NULL;
to64 (infile, mailpipe);
if (! to64 (tmpname, mailpipe, &error))
{
g_message (error->message);
g_error_free (error);
goto error;
}
/* close off mime */ /* close off mime */
if (mail_info.encapsulation == ENCAPSULATION_MIME) if (mail_info.encapsulation == ENCAPSULATION_MIME)
@ -788,7 +793,7 @@ create_headers (FILE *mailpipe)
if (strlen (mail_info.from) > 0) if (strlen (mail_info.from) > 0)
fprintf (mailpipe, "From: %s \n", mail_info.from); fprintf (mailpipe, "From: %s \n", mail_info.from);
fprintf (mailpipe, "X-Mailer: GIMP Useless Mail Program %s\n", GIMP_VERSION); fprintf (mailpipe, "X-Mailer: GIMP Useless Mail Plug-In %s\n", GIMP_VERSION);
if (mail_info.encapsulation == ENCAPSULATION_MIME) if (mail_info.encapsulation == ENCAPSULATION_MIME)
{ {
@ -831,108 +836,43 @@ create_headers (FILE *mailpipe)
} }
} }
/* static gboolean
* The following code taken from codes.c in the mpack-1.5 distribution to64 (const gchar *filename,
* by Carnegie Mellon University. FILE *outfile,
* GError **error)
*
* (C) Copyright 1993,1994 by Carnegie Mellon University
* All Rights Reserved.
*
* CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
* THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
* SOFTWARE.
*/
/*
Copyright (c) 1991 Bell Communications Research, Inc. (Bellcore)
Permission to use, copy, modify, and distribute this material
for any purpose and without fee is hereby granted, provided
that the above copyright notice and this permission notice
appear in all copies, and that the name of Bellcore not be
used in advertising or publicity pertaining to this
material without the specific, prior written permission
of an authorized representative of Bellcore. BELLCORE
MAKES NO REPRESENTATIONS ABOUT THE ACCURACY OR SUITABILITY
OF THIS MATERIAL FOR ANY PURPOSE. IT IS PROVIDED "AS IS",
WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. */
static gchar basis_64[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
static gint
to64 (FILE *infile,
FILE *outfile)
{ {
gint c1, c2, c3, ct = 0, written = 0; GMappedFile *infile;
const guchar *in;
gchar out[2048];
gint state = 0;
gint save = 0;
gsize len;
gsize bytes;
gsize c;
while ((c1 = getc (infile)) != EOF) infile = g_mapped_file_new (filename, FALSE, error);
if (! infile)
return FALSE;
in = (const guchar *) g_mapped_file_get_contents (infile);
len = g_mapped_file_get_length (infile);
for (c = 0; c < len;)
{ {
c2 = getc (infile); gsize step = MIN (1024, len - c);
if (c2 == EOF)
{ bytes = g_base64_encode_step (in, step, TRUE, out, &state, &save);
output64chunk (c1, 0, 0, 2, outfile); fwrite (out, 1, bytes, outfile);
}
else c += step;
{
c3 = getc (infile);
if (c3 == EOF)
{
output64chunk (c1, c2, 0, 1, outfile);
}
else
{
output64chunk (c1, c2, c3, 0, outfile);
}
}
ct += 4;
if (ct > 71)
{
putc ('\n', outfile);
written += 73;
ct = 0;
}
}
if (ct)
{
putc ('\n', outfile);
ct++;
} }
return written + ct; bytes = g_base64_encode_close (TRUE, out, &state, &save);
} fwrite (out, 1, bytes, outfile);
static void g_mapped_file_free (infile);
output64chunk (gint c1,
gint c2,
gint c3,
gint pads,
FILE *outfile)
{
putc (basis_64[c1 >> 2], outfile);
putc (basis_64[((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4)], outfile);
if (pads == 2) return TRUE;
{
putc ('=', outfile);
putc ('=', outfile);
}
else if (pads)
{
putc (basis_64[((c2 & 0xF) << 2) | ((c3 & 0xC0) >>6)], outfile);
putc ('=', outfile);
}
else
{
putc (basis_64[((c2 & 0xF) << 2) | ((c3 & 0xC0) >>6)], outfile);
putc (basis_64[c3 & 0x3F], outfile);
}
} }
static gint static gint