1999-03-07 20:56:03 +08:00
|
|
|
/* LIBGIMP - The GIMP Library
|
|
|
|
* Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
|
1998-01-25 18:26:47 +08:00
|
|
|
*
|
2009-01-18 06:28:01 +08:00
|
|
|
* This library is free software: you can redistribute it and/or
|
1999-11-18 05:13:50 +08:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
1998-01-25 18:26:47 +08:00
|
|
|
* License as published by the Free Software Foundation; either
|
2009-01-18 06:28:01 +08:00
|
|
|
* version 3 of the License, or (at your option) any later version.
|
2000-05-14 20:09:43 +08:00
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
1999-03-07 20:56:03 +08:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
1998-01-25 18:26:47 +08:00
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
1999-11-18 05:13:50 +08:00
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2009-01-18 06:28:01 +08:00
|
|
|
* License along with this library. If not, see
|
2018-07-12 05:27:07 +08:00
|
|
|
* <https://www.gnu.org/licenses/>.
|
1999-03-07 20:56:03 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
#include <string.h>
|
1999-03-07 20:56:03 +08:00
|
|
|
|
2012-05-03 06:54:21 +08:00
|
|
|
#include <glib-object.h>
|
2001-07-16 04:47:03 +08:00
|
|
|
|
2006-02-23 21:53:32 +08:00
|
|
|
#include <libgimpcolor/gimpcolortypes.h>
|
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
#include "gimpwire.h"
|
|
|
|
|
|
|
|
|
2005-12-13 22:11:56 +08:00
|
|
|
typedef struct _GimpWireHandler GimpWireHandler;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2005-12-13 22:11:56 +08:00
|
|
|
struct _GimpWireHandler
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2005-12-13 22:11:56 +08:00
|
|
|
guint32 type;
|
|
|
|
GimpWireReadFunc read_func;
|
|
|
|
GimpWireWriteFunc write_func;
|
|
|
|
GimpWireDestroyFunc destroy_func;
|
1997-11-25 06:05:25 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2005-12-13 22:11:56 +08:00
|
|
|
static GHashTable *wire_ht = NULL;
|
|
|
|
static GimpWireIOFunc wire_read_func = NULL;
|
|
|
|
static GimpWireIOFunc wire_write_func = NULL;
|
|
|
|
static GimpWireFlushFunc wire_flush_func = NULL;
|
|
|
|
static gboolean wire_error_val = FALSE;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
|
2005-12-13 22:11:56 +08:00
|
|
|
static void gimp_wire_init (void);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
|
|
|
|
void
|
2005-12-13 22:11:56 +08:00
|
|
|
gimp_wire_register (guint32 type,
|
|
|
|
GimpWireReadFunc read_func,
|
|
|
|
GimpWireWriteFunc write_func,
|
|
|
|
GimpWireDestroyFunc destroy_func)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2005-12-13 22:11:56 +08:00
|
|
|
GimpWireHandler *handler;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2004-10-04 21:25:33 +08:00
|
|
|
if (! wire_ht)
|
2005-12-13 22:11:56 +08:00
|
|
|
gimp_wire_init ();
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
handler = g_hash_table_lookup (wire_ht, &type);
|
2005-12-13 22:11:56 +08:00
|
|
|
|
2004-10-04 21:25:33 +08:00
|
|
|
if (! handler)
|
2007-12-11 16:39:10 +08:00
|
|
|
handler = g_slice_new0 (GimpWireHandler);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2000-05-14 20:09:43 +08:00
|
|
|
handler->type = type;
|
|
|
|
handler->read_func = read_func;
|
|
|
|
handler->write_func = write_func;
|
1997-11-25 06:05:25 +08:00
|
|
|
handler->destroy_func = destroy_func;
|
|
|
|
|
|
|
|
g_hash_table_insert (wire_ht, &handler->type, handler);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2005-12-13 22:11:56 +08:00
|
|
|
gimp_wire_set_reader (GimpWireIOFunc read_func)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
|
|
|
wire_read_func = read_func;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2005-12-13 22:11:56 +08:00
|
|
|
gimp_wire_set_writer (GimpWireIOFunc write_func)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
|
|
|
wire_write_func = write_func;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2005-12-13 22:11:56 +08:00
|
|
|
gimp_wire_set_flusher (GimpWireFlushFunc flush_func)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
|
|
|
wire_flush_func = flush_func;
|
|
|
|
}
|
|
|
|
|
2000-05-14 20:09:43 +08:00
|
|
|
gboolean
|
2005-12-13 22:11:56 +08:00
|
|
|
gimp_wire_read (GIOChannel *channel,
|
|
|
|
guint8 *buf,
|
|
|
|
gsize count,
|
|
|
|
gpointer user_data)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
|
|
|
if (wire_read_func)
|
|
|
|
{
|
2002-05-17 01:41:38 +08:00
|
|
|
if (!(* wire_read_func) (channel, buf, count, user_data))
|
2004-10-04 21:25:33 +08:00
|
|
|
{
|
2010-07-02 03:15:33 +08:00
|
|
|
/* Gives a confusing error message most of the time, disable:
|
2007-06-25 18:42:52 +08:00
|
|
|
g_warning ("%s: gimp_wire_read: error", g_get_prgname ());
|
2010-07-02 03:15:33 +08:00
|
|
|
*/
|
2004-10-04 21:25:33 +08:00
|
|
|
wire_error_val = TRUE;
|
|
|
|
return FALSE;
|
|
|
|
}
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2001-08-30 01:48:28 +08:00
|
|
|
GIOStatus status;
|
|
|
|
GError *error = NULL;
|
|
|
|
gsize bytes;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
while (count > 0)
|
2004-10-04 21:25:33 +08:00
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
|
|
|
bytes = 0;
|
|
|
|
status = g_io_channel_read_chars (channel,
|
|
|
|
(gchar *) buf, count,
|
|
|
|
&bytes,
|
|
|
|
&error);
|
|
|
|
}
|
2006-03-30 16:43:34 +08:00
|
|
|
while (G_UNLIKELY (status == G_IO_STATUS_AGAIN));
|
2004-10-04 21:25:33 +08:00
|
|
|
|
2024-03-05 15:40:19 +08:00
|
|
|
if (G_UNLIKELY (bytes == 0 && status == G_IO_STATUS_EOF))
|
|
|
|
{
|
|
|
|
g_warning ("%s: gimp_wire_read(): unexpected EOF",
|
|
|
|
g_get_prgname ());
|
|
|
|
wire_error_val = TRUE;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
else if (G_UNLIKELY (status != G_IO_STATUS_NORMAL))
|
2004-10-04 21:25:33 +08:00
|
|
|
{
|
|
|
|
if (error)
|
|
|
|
{
|
2007-06-25 18:42:52 +08:00
|
|
|
g_warning ("%s: gimp_wire_read(): error: %s",
|
2004-10-04 21:25:33 +08:00
|
|
|
g_get_prgname (), error->message);
|
|
|
|
g_error_free (error);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-06-25 18:42:52 +08:00
|
|
|
g_warning ("%s: gimp_wire_read(): error",
|
2004-10-04 21:25:33 +08:00
|
|
|
g_get_prgname ());
|
|
|
|
}
|
|
|
|
|
|
|
|
wire_error_val = TRUE;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
count -= bytes;
|
|
|
|
buf += bytes;
|
|
|
|
}
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2000-05-14 20:09:43 +08:00
|
|
|
gboolean
|
2005-12-13 22:11:56 +08:00
|
|
|
gimp_wire_write (GIOChannel *channel,
|
|
|
|
const guint8 *buf,
|
|
|
|
gsize count,
|
|
|
|
gpointer user_data)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
|
|
|
if (wire_write_func)
|
|
|
|
{
|
2005-12-13 22:11:56 +08:00
|
|
|
if (!(* wire_write_func) (channel, (guint8 *) buf, count, user_data))
|
2004-10-04 21:25:33 +08:00
|
|
|
{
|
2007-06-25 18:42:52 +08:00
|
|
|
g_warning ("%s: gimp_wire_write: error", g_get_prgname ());
|
2004-10-04 21:25:33 +08:00
|
|
|
wire_error_val = TRUE;
|
|
|
|
return FALSE;
|
|
|
|
}
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2001-08-30 01:48:28 +08:00
|
|
|
GIOStatus status;
|
|
|
|
GError *error = NULL;
|
|
|
|
gsize bytes;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
while (count > 0)
|
2004-10-04 21:25:33 +08:00
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
|
|
|
bytes = 0;
|
|
|
|
status = g_io_channel_write_chars (channel,
|
2005-12-13 22:11:56 +08:00
|
|
|
(const gchar *) buf, count,
|
2004-10-04 21:25:33 +08:00
|
|
|
&bytes,
|
|
|
|
&error);
|
|
|
|
}
|
2006-03-30 16:43:34 +08:00
|
|
|
while (G_UNLIKELY (status == G_IO_STATUS_AGAIN));
|
2004-10-04 21:25:33 +08:00
|
|
|
|
2006-03-30 16:43:34 +08:00
|
|
|
if (G_UNLIKELY (status != G_IO_STATUS_NORMAL))
|
2004-10-04 21:25:33 +08:00
|
|
|
{
|
|
|
|
if (error)
|
|
|
|
{
|
2007-06-25 18:42:52 +08:00
|
|
|
g_warning ("%s: gimp_wire_write(): error: %s",
|
2004-10-04 21:25:33 +08:00
|
|
|
g_get_prgname (), error->message);
|
|
|
|
g_error_free (error);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-06-25 18:42:52 +08:00
|
|
|
g_warning ("%s: gimp_wire_write(): error",
|
2004-10-04 21:25:33 +08:00
|
|
|
g_get_prgname ());
|
|
|
|
}
|
|
|
|
|
|
|
|
wire_error_val = TRUE;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
count -= bytes;
|
|
|
|
buf += bytes;
|
|
|
|
}
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2000-05-14 20:09:43 +08:00
|
|
|
gboolean
|
2005-12-13 22:11:56 +08:00
|
|
|
gimp_wire_flush (GIOChannel *channel,
|
|
|
|
gpointer user_data)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
|
|
|
if (wire_flush_func)
|
2002-05-17 01:41:38 +08:00
|
|
|
return (* wire_flush_func) (channel, user_data);
|
2000-05-14 20:09:43 +08:00
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2000-05-14 20:09:43 +08:00
|
|
|
gboolean
|
2005-12-13 22:11:56 +08:00
|
|
|
gimp_wire_error (void)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
|
|
|
return wire_error_val;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2005-12-13 22:11:56 +08:00
|
|
|
gimp_wire_clear_error (void)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
|
|
|
wire_error_val = FALSE;
|
|
|
|
}
|
|
|
|
|
2000-05-14 20:09:43 +08:00
|
|
|
gboolean
|
2005-12-13 22:11:56 +08:00
|
|
|
gimp_wire_read_msg (GIOChannel *channel,
|
|
|
|
GimpWireMessage *msg,
|
|
|
|
gpointer user_data)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2005-12-13 22:11:56 +08:00
|
|
|
GimpWireHandler *handler;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2007-06-25 18:42:52 +08:00
|
|
|
if (G_UNLIKELY (! wire_ht))
|
|
|
|
g_error ("gimp_wire_read_msg: the wire protocol has not been initialized");
|
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
if (wire_error_val)
|
|
|
|
return !wire_error_val;
|
|
|
|
|
2005-12-13 22:11:56 +08:00
|
|
|
if (! _gimp_wire_read_int32 (channel, &msg->type, 1, user_data))
|
1997-11-25 06:05:25 +08:00
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
handler = g_hash_table_lookup (wire_ht, &msg->type);
|
2007-06-25 18:42:52 +08:00
|
|
|
|
|
|
|
if (G_UNLIKELY (! handler))
|
|
|
|
g_error ("gimp_wire_read_msg: could not find handler for message: %d",
|
|
|
|
msg->type);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2002-05-17 01:41:38 +08:00
|
|
|
(* handler->read_func) (channel, msg, user_data);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
return !wire_error_val;
|
|
|
|
}
|
|
|
|
|
2000-05-14 20:09:43 +08:00
|
|
|
gboolean
|
2005-12-13 22:11:56 +08:00
|
|
|
gimp_wire_write_msg (GIOChannel *channel,
|
|
|
|
GimpWireMessage *msg,
|
|
|
|
gpointer user_data)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2005-12-13 22:11:56 +08:00
|
|
|
GimpWireHandler *handler;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2007-06-25 18:42:52 +08:00
|
|
|
if (G_UNLIKELY (! wire_ht))
|
|
|
|
g_error ("gimp_wire_write_msg: the wire protocol has not been initialized");
|
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
if (wire_error_val)
|
|
|
|
return !wire_error_val;
|
|
|
|
|
|
|
|
handler = g_hash_table_lookup (wire_ht, &msg->type);
|
2007-06-25 18:42:52 +08:00
|
|
|
|
|
|
|
if (G_UNLIKELY (! handler))
|
|
|
|
g_error ("gimp_wire_write_msg: could not find handler for message: %d",
|
|
|
|
msg->type);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2005-12-13 22:11:56 +08:00
|
|
|
if (! _gimp_wire_write_int32 (channel, &msg->type, 1, user_data))
|
1997-11-25 06:05:25 +08:00
|
|
|
return FALSE;
|
|
|
|
|
2002-05-17 01:41:38 +08:00
|
|
|
(* handler->write_func) (channel, msg, user_data);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
return !wire_error_val;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2005-12-13 22:11:56 +08:00
|
|
|
gimp_wire_destroy (GimpWireMessage *msg)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2005-12-13 22:11:56 +08:00
|
|
|
GimpWireHandler *handler;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2007-06-25 18:42:52 +08:00
|
|
|
if (G_UNLIKELY (! wire_ht))
|
|
|
|
g_error ("gimp_wire_destroy: the wire protocol has not been initialized");
|
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
handler = g_hash_table_lookup (wire_ht, &msg->type);
|
2007-06-25 18:42:52 +08:00
|
|
|
|
|
|
|
if (G_UNLIKELY (! handler))
|
|
|
|
g_error ("gimp_wire_destroy: could not find handler for message: %d\n",
|
|
|
|
msg->type);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
(* handler->destroy_func) (msg);
|
|
|
|
}
|
|
|
|
|
2018-11-20 05:06:28 +08:00
|
|
|
gboolean
|
|
|
|
_gimp_wire_read_int64 (GIOChannel *channel,
|
|
|
|
guint64 *data,
|
|
|
|
gint count,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (count >= 0, FALSE);
|
|
|
|
|
|
|
|
if (count > 0)
|
|
|
|
{
|
|
|
|
if (! _gimp_wire_read_int8 (channel,
|
|
|
|
(guint8 *) data, count * 8, user_data))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
while (count--)
|
|
|
|
{
|
|
|
|
*data = GUINT64_FROM_BE (*data);
|
|
|
|
data++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2000-05-14 20:09:43 +08:00
|
|
|
gboolean
|
2005-12-13 22:11:56 +08:00
|
|
|
_gimp_wire_read_int32 (GIOChannel *channel,
|
|
|
|
guint32 *data,
|
|
|
|
gint count,
|
|
|
|
gpointer user_data)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2004-10-04 21:25:33 +08:00
|
|
|
g_return_val_if_fail (count >= 0, FALSE);
|
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
if (count > 0)
|
|
|
|
{
|
2005-12-13 22:11:56 +08:00
|
|
|
if (! _gimp_wire_read_int8 (channel,
|
|
|
|
(guint8 *) data, count * 4, user_data))
|
2004-10-04 21:25:33 +08:00
|
|
|
return FALSE;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
while (count--)
|
2004-10-04 21:25:33 +08:00
|
|
|
{
|
|
|
|
*data = g_ntohl (*data);
|
|
|
|
data++;
|
|
|
|
}
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2000-05-14 20:09:43 +08:00
|
|
|
gboolean
|
2005-12-13 22:11:56 +08:00
|
|
|
_gimp_wire_read_int16 (GIOChannel *channel,
|
|
|
|
guint16 *data,
|
|
|
|
gint count,
|
|
|
|
gpointer user_data)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2004-10-04 21:25:33 +08:00
|
|
|
g_return_val_if_fail (count >= 0, FALSE);
|
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
if (count > 0)
|
|
|
|
{
|
2005-12-13 22:11:56 +08:00
|
|
|
if (! _gimp_wire_read_int8 (channel,
|
|
|
|
(guint8 *) data, count * 2, user_data))
|
2004-10-04 21:25:33 +08:00
|
|
|
return FALSE;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
while (count--)
|
2004-10-04 21:25:33 +08:00
|
|
|
{
|
|
|
|
*data = g_ntohs (*data);
|
|
|
|
data++;
|
|
|
|
}
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2000-05-14 20:09:43 +08:00
|
|
|
gboolean
|
2005-12-13 22:11:56 +08:00
|
|
|
_gimp_wire_read_int8 (GIOChannel *channel,
|
|
|
|
guint8 *data,
|
|
|
|
gint count,
|
|
|
|
gpointer user_data)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2004-10-04 21:25:33 +08:00
|
|
|
g_return_val_if_fail (count >= 0, FALSE);
|
|
|
|
|
2005-12-13 22:11:56 +08:00
|
|
|
return gimp_wire_read (channel, data, count, user_data);
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
|
2000-05-14 20:09:43 +08:00
|
|
|
gboolean
|
2005-12-13 22:11:56 +08:00
|
|
|
_gimp_wire_read_double (GIOChannel *channel,
|
|
|
|
gdouble *data,
|
|
|
|
gint count,
|
|
|
|
gpointer user_data)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2002-11-25 18:12:02 +08:00
|
|
|
gdouble *t;
|
|
|
|
guint8 tmp[8];
|
|
|
|
gint i;
|
2001-07-16 04:47:03 +08:00
|
|
|
|
2004-10-04 21:25:33 +08:00
|
|
|
g_return_val_if_fail (count >= 0, FALSE);
|
|
|
|
|
2002-11-25 18:12:02 +08:00
|
|
|
t = (gdouble *) tmp;
|
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
for (i = 0; i < count; i++)
|
|
|
|
{
|
2007-07-30 23:44:56 +08:00
|
|
|
#if (G_BYTE_ORDER == G_LITTLE_ENDIAN)
|
|
|
|
gint j;
|
|
|
|
#endif
|
|
|
|
|
2005-12-13 22:11:56 +08:00
|
|
|
if (! _gimp_wire_read_int8 (channel, tmp, 8, user_data))
|
2002-11-25 18:12:02 +08:00
|
|
|
return FALSE;
|
2001-07-16 04:47:03 +08:00
|
|
|
|
|
|
|
#if (G_BYTE_ORDER == G_LITTLE_ENDIAN)
|
2002-11-25 18:12:02 +08:00
|
|
|
for (j = 0; j < 4; j++)
|
|
|
|
{
|
2006-03-30 16:43:34 +08:00
|
|
|
guint8 swap;
|
|
|
|
|
|
|
|
swap = tmp[j];
|
|
|
|
tmp[j] = tmp[7 - j];
|
2002-11-25 18:12:02 +08:00
|
|
|
tmp[7 - j] = swap;
|
|
|
|
}
|
2001-07-16 04:47:03 +08:00
|
|
|
#endif
|
|
|
|
|
2002-11-25 18:12:02 +08:00
|
|
|
data[i] = *t;
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2000-05-14 20:09:43 +08:00
|
|
|
gboolean
|
2005-12-13 22:11:56 +08:00
|
|
|
_gimp_wire_read_string (GIOChannel *channel,
|
|
|
|
gchar **data,
|
|
|
|
gint count,
|
|
|
|
gpointer user_data)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2007-07-30 23:44:56 +08:00
|
|
|
gint i;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2004-10-04 21:25:33 +08:00
|
|
|
g_return_val_if_fail (count >= 0, FALSE);
|
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
for (i = 0; i < count; i++)
|
|
|
|
{
|
2007-07-30 23:44:56 +08:00
|
|
|
guint32 tmp;
|
|
|
|
|
2005-12-13 22:11:56 +08:00
|
|
|
if (! _gimp_wire_read_int32 (channel, &tmp, 1, user_data))
|
2004-10-04 21:25:33 +08:00
|
|
|
return FALSE;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
if (tmp > 0)
|
2004-10-04 21:25:33 +08:00
|
|
|
{
|
2007-12-11 17:36:38 +08:00
|
|
|
data[i] = g_try_new (gchar, tmp);
|
|
|
|
|
|
|
|
if (! data[i])
|
|
|
|
{
|
|
|
|
g_printerr ("%s: failed to allocate %u bytes\n", G_STRFUNC, tmp);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2007-12-11 15:16:04 +08:00
|
|
|
|
2005-12-13 22:11:56 +08:00
|
|
|
if (! _gimp_wire_read_int8 (channel,
|
|
|
|
(guint8 *) data[i], tmp, user_data))
|
2004-10-04 21:25:33 +08:00
|
|
|
{
|
|
|
|
g_free (data[i]);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2007-12-11 15:16:04 +08:00
|
|
|
|
|
|
|
/* make sure that the string is NULL-terminated */
|
|
|
|
data[i][tmp - 1] = '\0';
|
2004-10-04 21:25:33 +08:00
|
|
|
}
|
1997-11-25 06:05:25 +08:00
|
|
|
else
|
2004-10-04 21:25:33 +08:00
|
|
|
{
|
|
|
|
data[i] = NULL;
|
|
|
|
}
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2023-11-21 04:38:11 +08:00
|
|
|
gboolean
|
|
|
|
_gimp_wire_read_gegl_color (GIOChannel *channel,
|
|
|
|
GBytes **pixel_data,
|
|
|
|
GBytes **icc_data,
|
|
|
|
gchar **encoding,
|
|
|
|
gint count,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
|
|
|
gint i;
|
|
|
|
|
|
|
|
g_return_val_if_fail (count >= 0, FALSE);
|
|
|
|
|
|
|
|
for (i = 0; i < count; i++)
|
|
|
|
{
|
|
|
|
guint32 size;
|
|
|
|
guint8 pixel[40];
|
|
|
|
guint32 icc_length;
|
|
|
|
|
|
|
|
if (! _gimp_wire_read_int32 (channel,
|
|
|
|
&size, 1,
|
|
|
|
user_data) ||
|
|
|
|
size > 40 ||
|
|
|
|
! _gimp_wire_read_int8 (channel, pixel, size, user_data) ||
|
|
|
|
! _gimp_wire_read_string (channel, &(encoding[i]), 1, user_data) ||
|
|
|
|
! _gimp_wire_read_int32 (channel, &icc_length, 1, user_data))
|
|
|
|
{
|
|
|
|
g_clear_pointer (&(encoding[i]), g_free);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2024-02-13 23:07:24 +08:00
|
|
|
pixel_data[i] = (size > 0 ? g_bytes_new (pixel, size) : NULL);
|
2023-11-21 04:38:11 +08:00
|
|
|
|
|
|
|
/* Read space (profile data). */
|
|
|
|
|
|
|
|
icc_data[i] = NULL;
|
|
|
|
if (icc_length > 0)
|
|
|
|
{
|
|
|
|
guint8 *icc;
|
|
|
|
|
|
|
|
icc = g_new0 (guint8, icc_length);
|
|
|
|
|
|
|
|
if (! _gimp_wire_read_int8 (channel, icc, icc_length, user_data))
|
|
|
|
{
|
|
|
|
g_clear_pointer (&(encoding[i]), g_free);
|
|
|
|
g_clear_pointer (&icc, g_free);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2024-02-13 23:07:24 +08:00
|
|
|
icc_data[i] = g_bytes_new_take (icc, icc_length);
|
2023-11-21 04:38:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2018-11-20 05:06:28 +08:00
|
|
|
gboolean
|
|
|
|
_gimp_wire_write_int64 (GIOChannel *channel,
|
|
|
|
const guint64 *data,
|
|
|
|
gint count,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (count >= 0, FALSE);
|
|
|
|
|
|
|
|
if (count > 0)
|
|
|
|
{
|
|
|
|
gint i;
|
|
|
|
|
|
|
|
for (i = 0; i < count; i++)
|
|
|
|
{
|
|
|
|
guint64 tmp = GUINT64_TO_BE (data[i]);
|
|
|
|
|
|
|
|
if (! _gimp_wire_write_int8 (channel,
|
|
|
|
(const guint8 *) &tmp, 8, user_data))
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2000-05-14 20:09:43 +08:00
|
|
|
gboolean
|
2005-12-13 22:11:56 +08:00
|
|
|
_gimp_wire_write_int32 (GIOChannel *channel,
|
|
|
|
const guint32 *data,
|
|
|
|
gint count,
|
|
|
|
gpointer user_data)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2004-10-04 21:25:33 +08:00
|
|
|
g_return_val_if_fail (count >= 0, FALSE);
|
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
if (count > 0)
|
|
|
|
{
|
2007-07-30 23:44:56 +08:00
|
|
|
gint i;
|
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
for (i = 0; i < count; i++)
|
2004-10-04 21:25:33 +08:00
|
|
|
{
|
2007-07-30 23:44:56 +08:00
|
|
|
guint32 tmp = g_htonl (data[i]);
|
|
|
|
|
2005-12-13 22:11:56 +08:00
|
|
|
if (! _gimp_wire_write_int8 (channel,
|
|
|
|
(const guint8 *) &tmp, 4, user_data))
|
2004-10-04 21:25:33 +08:00
|
|
|
return FALSE;
|
|
|
|
}
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2000-05-14 20:09:43 +08:00
|
|
|
gboolean
|
2005-12-13 22:11:56 +08:00
|
|
|
_gimp_wire_write_int16 (GIOChannel *channel,
|
|
|
|
const guint16 *data,
|
|
|
|
gint count,
|
|
|
|
gpointer user_data)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2004-10-04 21:25:33 +08:00
|
|
|
g_return_val_if_fail (count >= 0, FALSE);
|
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
if (count > 0)
|
|
|
|
{
|
2007-07-30 23:44:56 +08:00
|
|
|
gint i;
|
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
for (i = 0; i < count; i++)
|
2004-10-04 21:25:33 +08:00
|
|
|
{
|
2007-07-30 23:44:56 +08:00
|
|
|
guint16 tmp = g_htons (data[i]);
|
|
|
|
|
2005-12-13 22:11:56 +08:00
|
|
|
if (! _gimp_wire_write_int8 (channel,
|
|
|
|
(const guint8 *) &tmp, 2, user_data))
|
2004-10-04 21:25:33 +08:00
|
|
|
return FALSE;
|
|
|
|
}
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2000-05-14 20:09:43 +08:00
|
|
|
gboolean
|
2005-12-13 22:11:56 +08:00
|
|
|
_gimp_wire_write_int8 (GIOChannel *channel,
|
|
|
|
const guint8 *data,
|
|
|
|
gint count,
|
|
|
|
gpointer user_data)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2004-10-04 21:25:33 +08:00
|
|
|
g_return_val_if_fail (count >= 0, FALSE);
|
|
|
|
|
2005-12-13 22:11:56 +08:00
|
|
|
return gimp_wire_write (channel, data, count, user_data);
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
|
2000-05-14 20:09:43 +08:00
|
|
|
gboolean
|
2005-12-13 22:11:56 +08:00
|
|
|
_gimp_wire_write_double (GIOChannel *channel,
|
|
|
|
const gdouble *data,
|
|
|
|
gint count,
|
|
|
|
gpointer user_data)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2002-11-25 18:12:02 +08:00
|
|
|
gdouble *t;
|
|
|
|
guint8 tmp[8];
|
2001-07-16 04:47:03 +08:00
|
|
|
gint i;
|
|
|
|
#if (G_BYTE_ORDER == G_LITTLE_ENDIAN)
|
2002-11-25 18:12:02 +08:00
|
|
|
gint j;
|
2001-07-16 04:47:03 +08:00
|
|
|
#endif
|
|
|
|
|
2004-10-04 21:25:33 +08:00
|
|
|
g_return_val_if_fail (count >= 0, FALSE);
|
|
|
|
|
2002-11-25 18:12:02 +08:00
|
|
|
t = (gdouble *) tmp;
|
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
for (i = 0; i < count; i++)
|
|
|
|
{
|
2002-11-25 18:12:02 +08:00
|
|
|
*t = data[i];
|
2001-07-16 04:47:03 +08:00
|
|
|
|
|
|
|
#if (G_BYTE_ORDER == G_LITTLE_ENDIAN)
|
2002-11-25 18:12:02 +08:00
|
|
|
for (j = 0; j < 4; j++)
|
|
|
|
{
|
2006-03-30 16:43:34 +08:00
|
|
|
guint8 swap;
|
|
|
|
|
|
|
|
swap = tmp[j];
|
|
|
|
tmp[j] = tmp[7 - j];
|
2002-11-25 18:12:02 +08:00
|
|
|
tmp[7 - j] = swap;
|
|
|
|
}
|
2001-07-16 04:47:03 +08:00
|
|
|
#endif
|
|
|
|
|
2005-12-13 22:11:56 +08:00
|
|
|
if (! _gimp_wire_write_int8 (channel, tmp, 8, user_data))
|
2004-10-04 21:25:33 +08:00
|
|
|
return FALSE;
|
2001-07-16 04:47:03 +08:00
|
|
|
|
|
|
|
#if 0
|
|
|
|
{
|
2004-10-04 21:25:33 +08:00
|
|
|
gint k;
|
2001-07-16 04:47:03 +08:00
|
|
|
|
2004-10-04 21:25:33 +08:00
|
|
|
g_print ("Wire representation of %f:\t", data[i]);
|
2001-07-16 04:47:03 +08:00
|
|
|
|
2004-10-04 21:25:33 +08:00
|
|
|
for (k = 0; k < 8; k++)
|
|
|
|
g_print ("%02x ", tmp[k]);
|
2001-07-16 04:47:03 +08:00
|
|
|
|
2004-10-04 21:25:33 +08:00
|
|
|
g_print ("\n");
|
2001-07-16 04:47:03 +08:00
|
|
|
}
|
|
|
|
#endif
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2000-05-14 20:09:43 +08:00
|
|
|
gboolean
|
2005-12-13 22:11:56 +08:00
|
|
|
_gimp_wire_write_string (GIOChannel *channel,
|
|
|
|
gchar **data,
|
|
|
|
gint count,
|
|
|
|
gpointer user_data)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2007-07-30 23:44:56 +08:00
|
|
|
gint i;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2004-10-04 21:25:33 +08:00
|
|
|
g_return_val_if_fail (count >= 0, FALSE);
|
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
for (i = 0; i < count; i++)
|
|
|
|
{
|
2007-07-30 23:44:56 +08:00
|
|
|
guint32 tmp;
|
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
if (data[i])
|
2004-10-04 21:25:33 +08:00
|
|
|
tmp = strlen (data[i]) + 1;
|
1997-11-25 06:05:25 +08:00
|
|
|
else
|
2004-10-04 21:25:33 +08:00
|
|
|
tmp = 0;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2005-12-13 22:11:56 +08:00
|
|
|
if (! _gimp_wire_write_int32 (channel, &tmp, 1, user_data))
|
2004-10-04 21:25:33 +08:00
|
|
|
return FALSE;
|
2005-12-13 22:11:56 +08:00
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
if (tmp > 0)
|
2005-12-13 22:11:56 +08:00
|
|
|
if (! _gimp_wire_write_int8 (channel,
|
|
|
|
(const guint8 *) data[i], tmp, user_data))
|
2004-10-04 21:25:33 +08:00
|
|
|
return FALSE;
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2023-11-21 04:38:11 +08:00
|
|
|
gboolean
|
|
|
|
_gimp_wire_write_gegl_color (GIOChannel *channel,
|
|
|
|
GBytes **pixel_data,
|
|
|
|
GBytes **icc_data,
|
|
|
|
gchar **encoding,
|
|
|
|
gint count,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
|
|
|
gint i;
|
|
|
|
|
|
|
|
g_return_val_if_fail (count >= 0, FALSE);
|
|
|
|
|
|
|
|
for (i = 0; i < count; i++)
|
|
|
|
{
|
2024-02-13 23:07:24 +08:00
|
|
|
const guint8 *pixel = NULL;
|
|
|
|
gsize bpp = 0;
|
|
|
|
const guint8 *icc = NULL;
|
|
|
|
gsize icc_length = 0;
|
|
|
|
|
|
|
|
if (pixel_data[i])
|
|
|
|
pixel = g_bytes_get_data (pixel_data[i], &bpp);
|
|
|
|
if (icc_data[i])
|
|
|
|
icc = g_bytes_get_data (icc_data[i], &icc_length);
|
|
|
|
|
|
|
|
if (! _gimp_wire_write_int32 (channel, (const guint32 *) &bpp, 1, user_data))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (bpp > 0 && ! _gimp_wire_write_int8 (channel, pixel, bpp, user_data))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (! _gimp_wire_write_string (channel, &(encoding[i]), 1, user_data) ||
|
|
|
|
! _gimp_wire_write_int32 (channel, (const guint32 *) &icc_length, 1, user_data))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (icc_length > 0 && ! _gimp_wire_write_int8 (channel, icc, icc_length, user_data))
|
2023-11-21 04:38:11 +08:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
static guint
|
2005-12-13 22:11:56 +08:00
|
|
|
gimp_wire_hash (const guint32 *key)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
|
|
|
return *key;
|
|
|
|
}
|
|
|
|
|
2000-05-14 20:09:43 +08:00
|
|
|
static gboolean
|
2005-12-13 22:11:56 +08:00
|
|
|
gimp_wire_compare (const guint32 *a,
|
|
|
|
const guint32 *b)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
|
|
|
return (*a == *b);
|
|
|
|
}
|
2005-12-13 22:11:56 +08:00
|
|
|
|
|
|
|
static void
|
|
|
|
gimp_wire_init (void)
|
|
|
|
{
|
|
|
|
if (! wire_ht)
|
|
|
|
wire_ht = g_hash_table_new ((GHashFunc) gimp_wire_hash,
|
|
|
|
(GCompareFunc) gimp_wire_compare);
|
|
|
|
}
|