~ubuntu-branches/ubuntu/jaunty/gimp/jaunty-security

« back to all changes in this revision

Viewing changes to libgimpbase/gimpwire.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Holbach
  • Date: 2007-05-02 16:33:03 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20070502163303-bvzhjzbpw8qglc4y
Tags: 2.3.16-1ubuntu1
* Resynchronized with Debian, remaining Ubuntu changes:
  - debian/rules: i18n magic.
* debian/control.in:
  - Maintainer: Ubuntu Core Developers <ubuntu-devel@lists.ubuntu.com>
* debian/patches/02_help-message.patch,
  debian/patches/03_gimp.desktop.in.in.patch,
  debian/patches/10_dont_show_wizard.patch: updated.
* debian/patches/04_composite-signedness.patch,
  debian/patches/05_add-letter-spacing.patch: dropped, used upstream.

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
 
20
20
#include "config.h"
21
21
 
22
 
#include <errno.h>
23
 
#include <stdio.h>
24
 
#include <stdlib.h>
25
22
#include <string.h>
26
 
#ifdef HAVE_SYS_PARAM_H
27
 
#include <sys/param.h>
28
 
#endif
29
 
#include <sys/types.h>
30
 
#ifdef HAVE_UNISTD_H
31
 
#include <unistd.h>
32
 
#endif
33
23
 
34
24
#include <glib.h>
35
25
 
36
 
#ifdef G_OS_WIN32
37
 
#include <process.h>
38
 
#include <io.h>
39
 
#endif
 
26
#include <libgimpcolor/gimpcolortypes.h>
40
27
 
41
28
#include "gimpwire.h"
42
29
 
43
30
 
44
 
typedef struct _WireHandler  WireHandler;
 
31
typedef struct _GimpWireHandler  GimpWireHandler;
45
32
 
46
 
struct _WireHandler
 
33
struct _GimpWireHandler
47
34
{
48
 
  guint32         type;
49
 
  WireReadFunc    read_func;
50
 
  WireWriteFunc   write_func;
51
 
  WireDestroyFunc destroy_func;
 
35
  guint32             type;
 
36
  GimpWireReadFunc    read_func;
 
37
  GimpWireWriteFunc   write_func;
 
38
  GimpWireDestroyFunc destroy_func;
52
39
};
53
40
 
54
41
 
55
 
static void      wire_init    (void);
56
 
static guint     wire_hash    (guint32 *key);
57
 
static gboolean  wire_compare (guint32 *a,
58
 
                               guint32 *b);
59
 
 
60
 
 
61
 
static GHashTable    *wire_ht         = NULL;
62
 
static WireIOFunc     wire_read_func  = NULL;
63
 
static WireIOFunc     wire_write_func = NULL;
64
 
static WireFlushFunc  wire_flush_func = NULL;
65
 
static gboolean       wire_error_val  = FALSE;
 
42
static GHashTable        *wire_ht         = NULL;
 
43
static GimpWireIOFunc     wire_read_func  = NULL;
 
44
static GimpWireIOFunc     wire_write_func = NULL;
 
45
static GimpWireFlushFunc  wire_flush_func = NULL;
 
46
static gboolean           wire_error_val  = FALSE;
 
47
 
 
48
 
 
49
static void  gimp_wire_init (void);
66
50
 
67
51
 
68
52
void
69
 
wire_register (guint32         type,
70
 
               WireReadFunc    read_func,
71
 
               WireWriteFunc   write_func,
72
 
               WireDestroyFunc destroy_func)
 
53
gimp_wire_register (guint32             type,
 
54
                    GimpWireReadFunc    read_func,
 
55
                    GimpWireWriteFunc   write_func,
 
56
                    GimpWireDestroyFunc destroy_func)
73
57
{
74
 
  WireHandler *handler;
 
58
  GimpWireHandler *handler;
75
59
 
76
60
  if (! wire_ht)
77
 
    wire_init ();
 
61
    gimp_wire_init ();
78
62
 
79
63
  handler = g_hash_table_lookup (wire_ht, &type);
 
64
 
80
65
  if (! handler)
81
 
    handler = g_new0 (WireHandler, 1);
 
66
    handler = g_new0 (GimpWireHandler, 1);
82
67
 
83
68
  handler->type         = type;
84
69
  handler->read_func    = read_func;
89
74
}
90
75
 
91
76
void
92
 
wire_set_reader (WireIOFunc read_func)
 
77
gimp_wire_set_reader (GimpWireIOFunc read_func)
93
78
{
94
79
  wire_read_func = read_func;
95
80
}
96
81
 
97
82
void
98
 
wire_set_writer (WireIOFunc write_func)
 
83
gimp_wire_set_writer (GimpWireIOFunc write_func)
99
84
{
100
85
  wire_write_func = write_func;
101
86
}
102
87
 
103
88
void
104
 
wire_set_flusher (WireFlushFunc flush_func)
 
89
gimp_wire_set_flusher (GimpWireFlushFunc flush_func)
105
90
{
106
91
  wire_flush_func = flush_func;
107
92
}
108
93
 
109
94
gboolean
110
 
wire_read (GIOChannel *channel,
111
 
           guint8     *buf,
112
 
           gsize       count,
113
 
           gpointer    user_data)
 
95
gimp_wire_read (GIOChannel *channel,
 
96
                guint8     *buf,
 
97
                gsize       count,
 
98
                gpointer    user_data)
114
99
{
115
100
  g_return_val_if_fail (count >= 0, FALSE);
116
101
 
139
124
                                                &bytes,
140
125
                                                &error);
141
126
            }
142
 
          while (status == G_IO_STATUS_AGAIN);
 
127
          while (G_UNLIKELY (status == G_IO_STATUS_AGAIN));
143
128
 
144
 
          if (status != G_IO_STATUS_NORMAL)
 
129
          if (G_UNLIKELY (status != G_IO_STATUS_NORMAL))
145
130
            {
146
131
              if (error)
147
132
                {
159
144
              return FALSE;
160
145
            }
161
146
 
162
 
          if (bytes == 0)
 
147
          if (G_UNLIKELY (bytes == 0))
163
148
            {
164
149
              g_warning ("%s: wire_read(): unexpected EOF", g_get_prgname ());
165
150
              wire_error_val = TRUE;
175
160
}
176
161
 
177
162
gboolean
178
 
wire_write (GIOChannel *channel,
179
 
            guint8     *buf,
180
 
            gsize       count,
181
 
            gpointer    user_data)
 
163
gimp_wire_write (GIOChannel   *channel,
 
164
                 const guint8 *buf,
 
165
                 gsize         count,
 
166
                 gpointer      user_data)
182
167
{
183
168
  g_return_val_if_fail (count >= 0, FALSE);
184
169
 
185
170
  if (wire_write_func)
186
171
    {
187
 
      if (!(* wire_write_func) (channel, buf, count, user_data))
 
172
      if (!(* wire_write_func) (channel, (guint8 *) buf, count, user_data))
188
173
        {
189
174
          g_warning ("%s: wire_write: error", g_get_prgname ());
190
175
          wire_error_val = TRUE;
203
188
            {
204
189
              bytes = 0;
205
190
              status = g_io_channel_write_chars (channel,
206
 
                                                 (gchar *) buf, count,
 
191
                                                 (const gchar *) buf, count,
207
192
                                                 &bytes,
208
193
                                                 &error);
209
194
            }
210
 
          while (status == G_IO_STATUS_AGAIN);
 
195
          while (G_UNLIKELY (status == G_IO_STATUS_AGAIN));
211
196
 
212
 
          if (status != G_IO_STATUS_NORMAL)
 
197
          if (G_UNLIKELY (status != G_IO_STATUS_NORMAL))
213
198
            {
214
199
              if (error)
215
200
                {
236
221
}
237
222
 
238
223
gboolean
239
 
wire_flush (GIOChannel *channel,
240
 
            gpointer    user_data)
 
224
gimp_wire_flush (GIOChannel *channel,
 
225
                 gpointer    user_data)
241
226
{
242
227
  if (wire_flush_func)
243
228
    return (* wire_flush_func) (channel, user_data);
246
231
}
247
232
 
248
233
gboolean
249
 
wire_error (void)
 
234
gimp_wire_error (void)
250
235
{
251
236
  return wire_error_val;
252
237
}
253
238
 
254
239
void
255
 
wire_clear_error (void)
 
240
gimp_wire_clear_error (void)
256
241
{
257
242
  wire_error_val = FALSE;
258
243
}
259
244
 
260
245
gboolean
261
 
wire_read_msg (GIOChannel  *channel,
262
 
               WireMessage *msg,
263
 
               gpointer     user_data)
 
246
gimp_wire_read_msg (GIOChannel      *channel,
 
247
                    GimpWireMessage *msg,
 
248
                    gpointer         user_data)
264
249
{
265
 
  WireHandler *handler;
 
250
  GimpWireHandler *handler;
266
251
 
267
252
  if (wire_error_val)
268
253
    return !wire_error_val;
269
254
 
270
 
  if (! wire_read_int32 (channel, &msg->type, 1, user_data))
 
255
  if (! _gimp_wire_read_int32 (channel, &msg->type, 1, user_data))
271
256
    return FALSE;
272
257
 
273
258
  handler = g_hash_table_lookup (wire_ht, &msg->type);
274
 
  if (!handler)
 
259
  if (! handler)
275
260
    g_error ("could not find handler for message: %d", msg->type);
276
261
 
277
262
  (* handler->read_func) (channel, msg, user_data);
280
265
}
281
266
 
282
267
gboolean
283
 
wire_write_msg (GIOChannel  *channel,
284
 
                WireMessage *msg,
285
 
                gpointer     user_data)
 
268
gimp_wire_write_msg (GIOChannel      *channel,
 
269
                     GimpWireMessage *msg,
 
270
                     gpointer         user_data)
286
271
{
287
 
  WireHandler *handler;
 
272
  GimpWireHandler *handler;
288
273
 
289
274
  if (wire_error_val)
290
275
    return !wire_error_val;
291
276
 
292
277
  handler = g_hash_table_lookup (wire_ht, &msg->type);
293
 
  if (!handler)
 
278
  if (! handler)
294
279
    g_error ("could not find handler for message: %d", msg->type);
295
280
 
296
 
  if (! wire_write_int32 (channel, &msg->type, 1, user_data))
 
281
  if (! _gimp_wire_write_int32 (channel, &msg->type, 1, user_data))
297
282
    return FALSE;
298
283
 
299
284
  (* handler->write_func) (channel, msg, user_data);
302
287
}
303
288
 
304
289
void
305
 
wire_destroy (WireMessage *msg)
 
290
gimp_wire_destroy (GimpWireMessage *msg)
306
291
{
307
 
  WireHandler *handler;
 
292
  GimpWireHandler *handler;
308
293
 
309
294
  handler = g_hash_table_lookup (wire_ht, &msg->type);
310
295
  if (!handler)
314
299
}
315
300
 
316
301
gboolean
317
 
wire_read_int32 (GIOChannel *channel,
318
 
                 guint32    *data,
319
 
                 gint        count,
320
 
                 gpointer    user_data)
 
302
_gimp_wire_read_int32 (GIOChannel *channel,
 
303
                       guint32    *data,
 
304
                       gint        count,
 
305
                       gpointer    user_data)
321
306
{
322
307
  g_return_val_if_fail (count >= 0, FALSE);
323
308
 
324
309
  if (count > 0)
325
310
    {
326
 
      if (! wire_read_int8 (channel, (guint8 *) data, count * 4, user_data))
 
311
      if (! _gimp_wire_read_int8 (channel,
 
312
                                  (guint8 *) data, count * 4, user_data))
327
313
        return FALSE;
328
314
 
329
315
      while (count--)
337
323
}
338
324
 
339
325
gboolean
340
 
wire_read_int16 (GIOChannel *channel,
341
 
                 guint16    *data,
342
 
                 gint        count,
343
 
                 gpointer    user_data)
 
326
_gimp_wire_read_int16 (GIOChannel *channel,
 
327
                       guint16    *data,
 
328
                       gint        count,
 
329
                       gpointer    user_data)
344
330
{
345
331
  g_return_val_if_fail (count >= 0, FALSE);
346
332
 
347
333
  if (count > 0)
348
334
    {
349
 
      if (! wire_read_int8 (channel, (guint8 *) data, count * 2, user_data))
 
335
      if (! _gimp_wire_read_int8 (channel,
 
336
                                  (guint8 *) data, count * 2, user_data))
350
337
        return FALSE;
351
338
 
352
339
      while (count--)
360
347
}
361
348
 
362
349
gboolean
363
 
wire_read_int8 (GIOChannel *channel,
364
 
                guint8     *data,
365
 
                gint        count,
366
 
                gpointer    user_data)
 
350
_gimp_wire_read_int8 (GIOChannel *channel,
 
351
                      guint8     *data,
 
352
                      gint        count,
 
353
                      gpointer    user_data)
367
354
{
368
355
  g_return_val_if_fail (count >= 0, FALSE);
369
356
 
370
 
  return wire_read (channel, data, count, user_data);
 
357
  return gimp_wire_read (channel, data, count, user_data);
371
358
}
372
359
 
373
360
gboolean
374
 
wire_read_double (GIOChannel *channel,
375
 
                  gdouble    *data,
376
 
                  gint        count,
377
 
                  gpointer    user_data)
 
361
_gimp_wire_read_double (GIOChannel *channel,
 
362
                        gdouble    *data,
 
363
                        gint        count,
 
364
                        gpointer    user_data)
378
365
{
379
366
  gdouble *t;
380
367
  guint8   tmp[8];
381
368
  gint     i;
382
369
#if (G_BYTE_ORDER == G_LITTLE_ENDIAN)
383
370
  gint     j;
384
 
  guint8   swap;
385
371
#endif
386
372
 
387
373
  g_return_val_if_fail (count >= 0, FALSE);
390
376
 
391
377
  for (i = 0; i < count; i++)
392
378
    {
393
 
      if (! wire_read_int8 (channel, tmp, 8, user_data))
 
379
      if (! _gimp_wire_read_int8 (channel, tmp, 8, user_data))
394
380
        return FALSE;
395
381
 
396
382
#if (G_BYTE_ORDER == G_LITTLE_ENDIAN)
397
383
      for (j = 0; j < 4; j++)
398
384
        {
399
 
          swap = tmp[j];
400
 
          tmp[j] = tmp[7 - j];
 
385
          guint8 swap;
 
386
 
 
387
          swap       = tmp[j];
 
388
          tmp[j]     = tmp[7 - j];
401
389
          tmp[7 - j] = swap;
402
390
        }
403
391
#endif
409
397
}
410
398
 
411
399
gboolean
412
 
wire_read_string (GIOChannel  *channel,
413
 
                  gchar      **data,
414
 
                  gint         count,
415
 
                  gpointer     user_data)
 
400
_gimp_wire_read_string (GIOChannel  *channel,
 
401
                        gchar      **data,
 
402
                        gint         count,
 
403
                        gpointer     user_data)
416
404
{
417
405
  guint32 tmp;
418
406
  gint    i;
421
409
 
422
410
  for (i = 0; i < count; i++)
423
411
    {
424
 
      if (!wire_read_int32 (channel, &tmp, 1, user_data))
 
412
      if (! _gimp_wire_read_int32 (channel, &tmp, 1, user_data))
425
413
        return FALSE;
426
414
 
427
415
      if (tmp > 0)
428
416
        {
429
417
          data[i] = g_new (gchar, tmp);
430
 
          if (! wire_read_int8 (channel, (guint8 *) data[i], tmp, user_data))
 
418
          if (! _gimp_wire_read_int8 (channel,
 
419
                                      (guint8 *) data[i], tmp, user_data))
431
420
            {
432
421
              g_free (data[i]);
433
422
              return FALSE;
443
432
}
444
433
 
445
434
gboolean
446
 
wire_write_int32 (GIOChannel *channel,
447
 
                  guint32    *data,
448
 
                  gint        count,
449
 
                  gpointer    user_data)
 
435
_gimp_wire_read_color (GIOChannel *channel,
 
436
                       GimpRGB    *data,
 
437
                       gint        count,
 
438
                       gpointer    user_data)
 
439
{
 
440
  g_return_val_if_fail (count >= 0, FALSE);
 
441
 
 
442
  return _gimp_wire_read_double (channel,
 
443
                                 (gdouble *) data, 4 * count, user_data);
 
444
}
 
445
 
 
446
gboolean
 
447
_gimp_wire_write_int32 (GIOChannel    *channel,
 
448
                        const guint32 *data,
 
449
                        gint           count,
 
450
                        gpointer       user_data)
450
451
{
451
452
  guint32 tmp;
452
453
  gint    i;
458
459
      for (i = 0; i < count; i++)
459
460
        {
460
461
          tmp = g_htonl (data[i]);
461
 
          if (! wire_write_int8 (channel, (guint8 *) &tmp, 4, user_data))
 
462
          if (! _gimp_wire_write_int8 (channel,
 
463
                                       (const guint8 *) &tmp, 4, user_data))
462
464
            return FALSE;
463
465
        }
464
466
    }
467
469
}
468
470
 
469
471
gboolean
470
 
wire_write_int16 (GIOChannel *channel,
471
 
                  guint16    *data,
472
 
                  gint        count,
473
 
                  gpointer    user_data)
 
472
_gimp_wire_write_int16 (GIOChannel    *channel,
 
473
                        const guint16 *data,
 
474
                        gint           count,
 
475
                        gpointer       user_data)
474
476
{
475
477
  guint16 tmp;
476
478
  gint    i;
482
484
      for (i = 0; i < count; i++)
483
485
        {
484
486
          tmp = g_htons (data[i]);
485
 
          if (! wire_write_int8 (channel, (guint8 *) &tmp, 2, user_data))
 
487
          if (! _gimp_wire_write_int8 (channel,
 
488
                                       (const guint8 *) &tmp, 2, user_data))
486
489
            return FALSE;
487
490
        }
488
491
    }
491
494
}
492
495
 
493
496
gboolean
494
 
wire_write_int8 (GIOChannel *channel,
495
 
                 guint8     *data,
496
 
                 gint        count,
497
 
                 gpointer    user_data)
 
497
_gimp_wire_write_int8 (GIOChannel   *channel,
 
498
                       const guint8 *data,
 
499
                       gint          count,
 
500
                       gpointer      user_data)
498
501
{
499
502
  g_return_val_if_fail (count >= 0, FALSE);
500
503
 
501
 
  return wire_write (channel, data, count, user_data);
 
504
  return gimp_wire_write (channel, data, count, user_data);
502
505
}
503
506
 
504
507
gboolean
505
 
wire_write_double (GIOChannel *channel,
506
 
                   gdouble    *data,
507
 
                   gint        count,
508
 
                   gpointer    user_data)
 
508
_gimp_wire_write_double (GIOChannel    *channel,
 
509
                         const gdouble *data,
 
510
                         gint           count,
 
511
                         gpointer       user_data)
509
512
{
510
513
  gdouble *t;
511
514
  guint8   tmp[8];
512
515
  gint     i;
513
516
#if (G_BYTE_ORDER == G_LITTLE_ENDIAN)
514
517
  gint     j;
515
 
  guint8   swap;
516
518
#endif
517
519
 
518
520
  g_return_val_if_fail (count >= 0, FALSE);
526
528
#if (G_BYTE_ORDER == G_LITTLE_ENDIAN)
527
529
      for (j = 0; j < 4; j++)
528
530
        {
529
 
          swap = tmp[j];
530
 
          tmp[j] = tmp[7 - j];
 
531
          guint8 swap;
 
532
 
 
533
          swap       = tmp[j];
 
534
          tmp[j]     = tmp[7 - j];
531
535
          tmp[7 - j] = swap;
532
536
        }
533
537
#endif
534
538
 
535
 
      if (! wire_write_int8 (channel, tmp, 8, user_data))
 
539
      if (! _gimp_wire_write_int8 (channel, tmp, 8, user_data))
536
540
        return FALSE;
537
541
 
538
542
#if 0
553
557
}
554
558
 
555
559
gboolean
556
 
wire_write_string (GIOChannel  *channel,
557
 
                   gchar      **data,
558
 
                   gint         count,
559
 
                   gpointer     user_data)
 
560
_gimp_wire_write_string (GIOChannel  *channel,
 
561
                         gchar      **data,
 
562
                         gint         count,
 
563
                         gpointer     user_data)
560
564
{
561
565
  guint32 tmp;
562
566
  gint    i;
570
574
      else
571
575
        tmp = 0;
572
576
 
573
 
      if (! wire_write_int32 (channel, &tmp, 1, user_data))
 
577
      if (! _gimp_wire_write_int32 (channel, &tmp, 1, user_data))
574
578
        return FALSE;
 
579
 
575
580
      if (tmp > 0)
576
 
        if (! wire_write_int8 (channel, (guint8 *) data[i], tmp, user_data))
 
581
        if (! _gimp_wire_write_int8 (channel,
 
582
                                     (const guint8 *) data[i], tmp, user_data))
577
583
          return FALSE;
578
584
    }
579
585
 
580
586
  return TRUE;
581
587
}
582
588
 
583
 
static void
584
 
wire_init (void)
 
589
gboolean
 
590
_gimp_wire_write_color (GIOChannel    *channel,
 
591
                        const GimpRGB *data,
 
592
                        gint           count,
 
593
                        gpointer       user_data)
585
594
{
586
 
  if (! wire_ht)
587
 
    wire_ht = g_hash_table_new ((GHashFunc) wire_hash,
588
 
                                (GCompareFunc) wire_compare);
 
595
  g_return_val_if_fail (count >= 0, FALSE);
 
596
 
 
597
  return _gimp_wire_write_double (channel,
 
598
                                  (gdouble *) data, 4 * count, user_data);
589
599
}
590
600
 
591
601
static guint
592
 
wire_hash (guint32 *key)
 
602
gimp_wire_hash (const guint32 *key)
593
603
{
594
604
  return *key;
595
605
}
596
606
 
597
607
static gboolean
598
 
wire_compare (guint32 *a,
599
 
              guint32 *b)
 
608
gimp_wire_compare (const guint32 *a,
 
609
                   const guint32 *b)
600
610
{
601
611
  return (*a == *b);
602
612
}
 
613
 
 
614
static void
 
615
gimp_wire_init (void)
 
616
{
 
617
  if (! wire_ht)
 
618
    wire_ht = g_hash_table_new ((GHashFunc) gimp_wire_hash,
 
619
                                (GCompareFunc) gimp_wire_compare);
 
620
}