Branch data Line data Source code
1 : : /* GLIB - Library of useful routines for C programming
2 : : * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3 : : *
4 : : * giochannel.c: IO Channel abstraction
5 : : * Copyright 1998 Owen Taylor
6 : : *
7 : : * SPDX-License-Identifier: LGPL-2.1-or-later
8 : : *
9 : : * This library is free software; you can redistribute it and/or
10 : : * modify it under the terms of the GNU Lesser General Public
11 : : * License as published by the Free Software Foundation; either
12 : : * version 2.1 of the License, or (at your option) any later version.
13 : : *
14 : : * This library is distributed in the hope that it will be useful,
15 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 : : * Lesser General Public License for more details.
18 : : *
19 : : * You should have received a copy of the GNU Lesser General Public
20 : : * License along with this library; if not, see <http://www.gnu.org/licenses/>.
21 : : */
22 : :
23 : : /*
24 : : * Modified by the GLib Team and others 1997-2000. See the AUTHORS
25 : : * file for a list of people on the GLib Team. See the ChangeLog
26 : : * files for a list of changes. These files are distributed with
27 : : * GLib at ftp://ftp.gtk.org/pub/gtk/.
28 : : */
29 : :
30 : : /*
31 : : * MT safe
32 : : */
33 : :
34 : : #include "config.h"
35 : :
36 : : #include <string.h>
37 : : #include <errno.h>
38 : :
39 : : #include "giochannel.h"
40 : :
41 : : #include "gstrfuncs.h"
42 : : #include "gtestutils.h"
43 : : #include "glibintl.h"
44 : :
45 : :
46 : : /**
47 : : * GIOChannel:
48 : : *
49 : : * The `GIOChannel` data type aims to provide a portable method for
50 : : * using file descriptors, pipes, and sockets, and integrating them
51 : : * into the main event loop (see [struct@GLib.MainContext]). Currently,
52 : : * full support is available on UNIX platforms; support for Windows
53 : : * is only partially complete.
54 : : *
55 : : * To create a new `GIOChannel` on UNIX systems use
56 : : * [ctor@GLib.IOChannel.unix_new]. This works for plain file descriptors,
57 : : * pipes and sockets. Alternatively, a channel can be created for a
58 : : * file in a system independent manner using [ctor@GLib.IOChannel.new_file].
59 : : *
60 : : * Once a `GIOChannel` has been created, it can be used in a generic
61 : : * manner with the functions [method@GLib.IOChannel.read_chars],
62 : : * [method@GLib.IOChannel.write_chars], [method@GLib.IOChannel.seek_position],
63 : : * and [method@GLib.IOChannel.shutdown].
64 : : *
65 : : * To add a `GIOChannel` to the main event loop, use [func@GLib.io_add_watch] or
66 : : * [func@GLib.io_add_watch_full]. Here you specify which events you are
67 : : * interested in on the `GIOChannel`, and provide a function to be called
68 : : * whenever these events occur.
69 : : *
70 : : * `GIOChannel` instances are created with an initial reference count of 1.
71 : : * [method@GLib.IOChannel.ref] and [method@GLib.IOChannel.unref] can be used to
72 : : * increment or decrement the reference count respectively. When the
73 : : * reference count falls to 0, the `GIOChannel` is freed. (Though it
74 : : * isn’t closed automatically, unless it was created using
75 : : * [ctor@GLib.IOChannel.new_file].) Using [func@GLib.io_add_watch] or
76 : : * [func@GLib.io_add_watch_full] increments a channel’s reference count.
77 : : *
78 : : * The new functions [method@GLib.IOChannel.read_chars],
79 : : * [method@GLib.IOChannel.read_line], [method@GLib.IOChannel.read_line_string],
80 : : * [method@GLib.IOChannel.read_to_end], [method@GLib.IOChannel.write_chars],
81 : : * [method@GLib.IOChannel.seek_position], and [method@GLib.IOChannel.flush]
82 : : * should not be mixed with the deprecated functions
83 : : * [method@GLib.IOChannel.read], [method@GLib.IOChannel.write], and
84 : : * [method@GLib.IOChannel.seek] on the same channel.
85 : : **/
86 : :
87 : : /**
88 : : * GIOFuncs:
89 : : * @io_read: reads raw bytes from the channel. This is called from
90 : : * various functions such as g_io_channel_read_chars() to
91 : : * read raw bytes from the channel. Encoding and buffering
92 : : * issues are dealt with at a higher level.
93 : : * @io_write: writes raw bytes to the channel. This is called from
94 : : * various functions such as g_io_channel_write_chars() to
95 : : * write raw bytes to the channel. Encoding and buffering
96 : : * issues are dealt with at a higher level.
97 : : * @io_seek: (optional): seeks the channel. This is called from
98 : : * g_io_channel_seek() on channels that support it.
99 : : * @io_close: closes the channel. This is called from
100 : : * g_io_channel_close() after flushing the buffers.
101 : : * @io_create_watch: creates a watch on the channel. This call
102 : : * corresponds directly to g_io_create_watch().
103 : : * @io_free: called from g_io_channel_unref() when the channel needs to
104 : : * be freed. This function must free the memory associated
105 : : * with the channel, including freeing the #GIOChannel
106 : : * structure itself. The channel buffers have been flushed
107 : : * and possibly @io_close has been called by the time this
108 : : * function is called.
109 : : * @io_set_flags: sets the #GIOFlags on the channel. This is called
110 : : * from g_io_channel_set_flags() with all flags except
111 : : * for %G_IO_FLAG_APPEND and %G_IO_FLAG_NONBLOCK masked
112 : : * out.
113 : : * @io_get_flags: gets the #GIOFlags for the channel. This function
114 : : * need only return the %G_IO_FLAG_APPEND and
115 : : * %G_IO_FLAG_NONBLOCK flags; g_io_channel_get_flags()
116 : : * automatically adds the others as appropriate.
117 : : *
118 : : * A table of functions used to handle different types of #GIOChannel
119 : : * in a generic way.
120 : : **/
121 : :
122 : : /**
123 : : * GIOStatus:
124 : : * @G_IO_STATUS_ERROR: An error occurred.
125 : : * @G_IO_STATUS_NORMAL: Success.
126 : : * @G_IO_STATUS_EOF: End of file.
127 : : * @G_IO_STATUS_AGAIN: Resource temporarily unavailable.
128 : : *
129 : : * Statuses returned by most of the #GIOFuncs functions.
130 : : **/
131 : :
132 : : /**
133 : : * GIOError:
134 : : * @G_IO_ERROR_NONE: no error
135 : : * @G_IO_ERROR_AGAIN: an EAGAIN error occurred
136 : : * @G_IO_ERROR_INVAL: an EINVAL error occurred
137 : : * @G_IO_ERROR_UNKNOWN: another error occurred
138 : : *
139 : : * #GIOError is only used by the deprecated functions
140 : : * g_io_channel_read(), g_io_channel_write(), and g_io_channel_seek().
141 : : **/
142 : :
143 : : #define G_IO_NICE_BUF_SIZE 1024
144 : :
145 : : /* This needs to be as wide as the largest character in any possible encoding */
146 : : #define MAX_CHAR_SIZE 10
147 : :
148 : : /* Some simplifying macros, which reduce the need to worry whether the
149 : : * buffers have been allocated. These also make USE_BUF () an lvalue,
150 : : * which is used in g_io_channel_read_to_end ().
151 : : */
152 : : #define USE_BUF(channel) ((channel)->encoding ? (channel)->encoded_read_buf \
153 : : : (channel)->read_buf)
154 : : #define BUF_LEN(string) ((string) ? (string)->len : 0)
155 : :
156 : : static GIOError g_io_error_get_from_g_error (GIOStatus status,
157 : : GError *err);
158 : : static void g_io_channel_purge (GIOChannel *channel);
159 : : static GIOStatus g_io_channel_fill_buffer (GIOChannel *channel,
160 : : GError **err);
161 : : static GIOStatus g_io_channel_read_line_backend (GIOChannel *channel,
162 : : gsize *length,
163 : : gsize *terminator_pos,
164 : : GError **error);
165 : :
166 : : /**
167 : : * g_io_channel_init:
168 : : * @channel: a #GIOChannel
169 : : *
170 : : * Initializes a #GIOChannel struct.
171 : : *
172 : : * This is called by each of the above functions when creating a
173 : : * #GIOChannel, and so is not often needed by the application
174 : : * programmer (unless you are creating a new type of #GIOChannel).
175 : : */
176 : : void
177 : 1863 : g_io_channel_init (GIOChannel *channel)
178 : : {
179 : 1863 : channel->ref_count = 1;
180 : 1863 : channel->encoding = g_strdup ("UTF-8");
181 : 1863 : channel->line_term = NULL;
182 : 1863 : channel->line_term_len = 0;
183 : 1863 : channel->buf_size = G_IO_NICE_BUF_SIZE;
184 : 1863 : channel->read_cd = (GIConv) -1;
185 : 1863 : channel->write_cd = (GIConv) -1;
186 : 1863 : channel->read_buf = NULL; /* Lazy allocate buffers */
187 : 1863 : channel->encoded_read_buf = NULL;
188 : 1863 : channel->write_buf = NULL;
189 : 1863 : channel->partial_write_buf[0] = '\0';
190 : 1863 : channel->use_buffer = TRUE;
191 : 1863 : channel->do_encode = FALSE;
192 : 1863 : channel->close_on_unref = FALSE;
193 : 1863 : }
194 : :
195 : : /**
196 : : * g_io_channel_ref:
197 : : * @channel: a #GIOChannel
198 : : *
199 : : * Increments the reference count of a #GIOChannel.
200 : : *
201 : : * Returns: the @channel that was passed in (since 2.6)
202 : : */
203 : : GIOChannel *
204 : 2002 : g_io_channel_ref (GIOChannel *channel)
205 : : {
206 : 2002 : g_return_val_if_fail (channel != NULL, NULL);
207 : :
208 : 2002 : g_atomic_int_inc (&channel->ref_count);
209 : :
210 : 2002 : return channel;
211 : 1137 : }
212 : :
213 : : /**
214 : : * g_io_channel_unref:
215 : : * @channel: a #GIOChannel
216 : : *
217 : : * Decrements the reference count of a #GIOChannel.
218 : : */
219 : : void
220 : 3826 : g_io_channel_unref (GIOChannel *channel)
221 : : {
222 : : gboolean is_zero;
223 : :
224 : 3826 : g_return_if_fail (channel != NULL);
225 : :
226 : 3826 : is_zero = g_atomic_int_dec_and_test (&channel->ref_count);
227 : :
228 : 3826 : if (G_UNLIKELY (is_zero))
229 : : {
230 : 1825 : if (channel->close_on_unref)
231 : 1317 : g_io_channel_shutdown (channel, TRUE, NULL);
232 : : else
233 : 508 : g_io_channel_purge (channel);
234 : 1825 : g_free (channel->encoding);
235 : 1825 : if (channel->read_cd != (GIConv) -1)
236 : 2 : g_iconv_close (channel->read_cd);
237 : 1825 : if (channel->write_cd != (GIConv) -1)
238 : 0 : g_iconv_close (channel->write_cd);
239 : 1825 : g_free (channel->line_term);
240 : 1825 : if (channel->read_buf)
241 : 328 : g_string_free (channel->read_buf, TRUE);
242 : 1825 : if (channel->write_buf)
243 : 67 : g_string_free (channel->write_buf, TRUE);
244 : 1825 : if (channel->encoded_read_buf)
245 : 211 : g_string_free (channel->encoded_read_buf, TRUE);
246 : 1825 : channel->funcs->io_free (channel);
247 : 716 : }
248 : 1852 : }
249 : :
250 : : static GIOError
251 : 493 : g_io_error_get_from_g_error (GIOStatus status,
252 : : GError *err)
253 : : {
254 : 493 : switch (status)
255 : : {
256 : 414 : case G_IO_STATUS_NORMAL:
257 : : case G_IO_STATUS_EOF:
258 : 493 : return G_IO_ERROR_NONE;
259 : 0 : case G_IO_STATUS_AGAIN:
260 : 0 : return G_IO_ERROR_AGAIN;
261 : 0 : case G_IO_STATUS_ERROR:
262 : 0 : g_return_val_if_fail (err != NULL, G_IO_ERROR_UNKNOWN);
263 : :
264 : 0 : if (err->domain != G_IO_CHANNEL_ERROR)
265 : 0 : return G_IO_ERROR_UNKNOWN;
266 : 0 : switch (err->code)
267 : : {
268 : 0 : case G_IO_CHANNEL_ERROR_INVAL:
269 : 0 : return G_IO_ERROR_INVAL;
270 : 0 : default:
271 : 0 : return G_IO_ERROR_UNKNOWN;
272 : : }
273 : 0 : default:
274 : : g_assert_not_reached ();
275 : 0 : }
276 : 79 : }
277 : :
278 : : /**
279 : : * g_io_channel_read:
280 : : * @channel: a #GIOChannel
281 : : * @buf: a buffer to read the data into (which should be at least
282 : : * count bytes long)
283 : : * @count: the number of bytes to read from the #GIOChannel
284 : : * @bytes_read: returns the number of bytes actually read
285 : : *
286 : : * Reads data from a #GIOChannel.
287 : : *
288 : : * Returns: %G_IO_ERROR_NONE if the operation was successful.
289 : : *
290 : : * Deprecated:2.2: Use g_io_channel_read_chars() instead.
291 : : **/
292 : : GIOError
293 : 493 : g_io_channel_read (GIOChannel *channel,
294 : : gchar *buf,
295 : : gsize count,
296 : : gsize *bytes_read)
297 : : {
298 : 493 : GError *err = NULL;
299 : : GIOError error;
300 : : GIOStatus status;
301 : :
302 : 493 : g_return_val_if_fail (channel != NULL, G_IO_ERROR_UNKNOWN);
303 : 493 : g_return_val_if_fail (bytes_read != NULL, G_IO_ERROR_UNKNOWN);
304 : :
305 : 493 : if (count == 0)
306 : : {
307 : 0 : if (bytes_read)
308 : 0 : *bytes_read = 0;
309 : 0 : return G_IO_ERROR_NONE;
310 : : }
311 : :
312 : 493 : g_return_val_if_fail (buf != NULL, G_IO_ERROR_UNKNOWN);
313 : :
314 : 493 : status = channel->funcs->io_read (channel, buf, count, bytes_read, &err);
315 : :
316 : 493 : error = g_io_error_get_from_g_error (status, err);
317 : :
318 : 493 : if (err)
319 : 0 : g_error_free (err);
320 : :
321 : 493 : return error;
322 : 79 : }
323 : :
324 : : /**
325 : : * g_io_channel_write:
326 : : * @channel: a #GIOChannel
327 : : * @buf: the buffer containing the data to write
328 : : * @count: the number of bytes to write
329 : : * @bytes_written: the number of bytes actually written
330 : : *
331 : : * Writes data to a #GIOChannel.
332 : : *
333 : : * Returns: %G_IO_ERROR_NONE if the operation was successful.
334 : : *
335 : : * Deprecated:2.2: Use g_io_channel_write_chars() instead.
336 : : **/
337 : : GIOError
338 : 0 : g_io_channel_write (GIOChannel *channel,
339 : : const gchar *buf,
340 : : gsize count,
341 : : gsize *bytes_written)
342 : : {
343 : 0 : GError *err = NULL;
344 : : GIOError error;
345 : : GIOStatus status;
346 : :
347 : 0 : g_return_val_if_fail (channel != NULL, G_IO_ERROR_UNKNOWN);
348 : 0 : g_return_val_if_fail (bytes_written != NULL, G_IO_ERROR_UNKNOWN);
349 : :
350 : 0 : status = channel->funcs->io_write (channel, buf, count, bytes_written, &err);
351 : :
352 : 0 : error = g_io_error_get_from_g_error (status, err);
353 : :
354 : 0 : if (err)
355 : 0 : g_error_free (err);
356 : :
357 : 0 : return error;
358 : 0 : }
359 : :
360 : : /**
361 : : * g_io_channel_seek:
362 : : * @channel: a #GIOChannel
363 : : * @offset: an offset, in bytes, which is added to the position specified
364 : : * by @type
365 : : * @type: the position in the file, which can be %G_SEEK_CUR (the current
366 : : * position), %G_SEEK_SET (the start of the file), or %G_SEEK_END
367 : : * (the end of the file)
368 : : *
369 : : * Sets the current position in the #GIOChannel, similar to the standard
370 : : * library function fseek().
371 : : *
372 : : * Returns: %G_IO_ERROR_NONE if the operation was successful.
373 : : *
374 : : * Deprecated:2.2: Use g_io_channel_seek_position() instead.
375 : : **/
376 : : GIOError
377 : 0 : g_io_channel_seek (GIOChannel *channel,
378 : : gint64 offset,
379 : : GSeekType type)
380 : : {
381 : 0 : GError *err = NULL;
382 : : GIOError error;
383 : : GIOStatus status;
384 : :
385 : 0 : g_return_val_if_fail (channel != NULL, G_IO_ERROR_UNKNOWN);
386 : 0 : g_return_val_if_fail (channel->is_seekable, G_IO_ERROR_UNKNOWN);
387 : :
388 : 0 : switch (type)
389 : : {
390 : 0 : case G_SEEK_CUR:
391 : : case G_SEEK_SET:
392 : : case G_SEEK_END:
393 : 0 : break;
394 : 0 : default:
395 : 0 : g_warning ("g_io_channel_seek: unknown seek type");
396 : 0 : return G_IO_ERROR_UNKNOWN;
397 : : }
398 : :
399 : 0 : status = channel->funcs->io_seek (channel, offset, type, &err);
400 : :
401 : 0 : error = g_io_error_get_from_g_error (status, err);
402 : :
403 : 0 : if (err)
404 : 0 : g_error_free (err);
405 : :
406 : 0 : return error;
407 : 0 : }
408 : :
409 : : /* The function g_io_channel_new_file() is prototyped in both
410 : : * giounix.c and giowin32.c, so we stick its documentation here.
411 : : */
412 : :
413 : : /**
414 : : * g_io_channel_new_file:
415 : : * @filename: (type filename): A string containing the name of a file
416 : : * @mode: One of "r", "w", "a", "r+", "w+", "a+". These have
417 : : * the same meaning as in fopen()
418 : : * @error: A location to return an error of type %G_FILE_ERROR
419 : : *
420 : : * Open a file @filename as a #GIOChannel using mode @mode. This
421 : : * channel will be closed when the last reference to it is dropped,
422 : : * so there is no need to call g_io_channel_close() (though doing
423 : : * so will not cause problems, as long as no attempt is made to
424 : : * access the channel after it is closed).
425 : : *
426 : : * Returns: A #GIOChannel on success, %NULL on failure.
427 : : **/
428 : :
429 : : /**
430 : : * g_io_channel_close:
431 : : * @channel: A #GIOChannel
432 : : *
433 : : * Close an IO channel. Any pending data to be written will be
434 : : * flushed, ignoring errors. The channel will not be freed until the
435 : : * last reference is dropped using g_io_channel_unref().
436 : : *
437 : : * Deprecated:2.2: Use g_io_channel_shutdown() instead.
438 : : **/
439 : : void
440 : 0 : g_io_channel_close (GIOChannel *channel)
441 : : {
442 : 0 : GError *err = NULL;
443 : :
444 : 0 : g_return_if_fail (channel != NULL);
445 : :
446 : 0 : g_io_channel_purge (channel);
447 : :
448 : 0 : channel->funcs->io_close (channel, &err);
449 : :
450 : 0 : if (err)
451 : : { /* No way to return the error */
452 : 0 : g_warning ("Error closing channel: %s", err->message);
453 : 0 : g_error_free (err);
454 : 0 : }
455 : :
456 : 0 : channel->close_on_unref = FALSE; /* Because we already did */
457 : 0 : channel->is_readable = FALSE;
458 : 0 : channel->is_writeable = FALSE;
459 : 0 : channel->is_seekable = FALSE;
460 : 0 : }
461 : :
462 : : /**
463 : : * g_io_channel_shutdown:
464 : : * @channel: a #GIOChannel
465 : : * @flush: if %TRUE, flush pending
466 : : * @err: location to store a #GIOChannelError
467 : : *
468 : : * Close an IO channel. Any pending data to be written will be
469 : : * flushed if @flush is %TRUE. The channel will not be freed until the
470 : : * last reference is dropped using g_io_channel_unref().
471 : : *
472 : : * Returns: the status of the operation.
473 : : **/
474 : : GIOStatus
475 : 1444 : g_io_channel_shutdown (GIOChannel *channel,
476 : : gboolean flush,
477 : : GError **err)
478 : : {
479 : : GIOStatus status, result;
480 : 1444 : GError *tmperr = NULL;
481 : :
482 : 1444 : g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
483 : 1444 : g_return_val_if_fail (err == NULL || *err == NULL, G_IO_STATUS_ERROR);
484 : :
485 : 1444 : if (channel->write_buf && channel->write_buf->len > 0)
486 : : {
487 : 2 : if (flush)
488 : : {
489 : : GIOFlags flags;
490 : :
491 : : /* Set the channel to blocking, to avoid a busy loop
492 : : */
493 : 2 : flags = g_io_channel_get_flags (channel);
494 : : /* Ignore any errors here, they're irrelevant */
495 : 2 : g_io_channel_set_flags (channel, flags & ~G_IO_FLAG_NONBLOCK, NULL);
496 : :
497 : 2 : result = g_io_channel_flush (channel, &tmperr);
498 : 1 : }
499 : : else
500 : 0 : result = G_IO_STATUS_NORMAL;
501 : :
502 : 2 : g_string_truncate(channel->write_buf, 0);
503 : 1 : }
504 : : else
505 : 1442 : result = G_IO_STATUS_NORMAL;
506 : :
507 : 1444 : if (channel->partial_write_buf[0] != '\0')
508 : : {
509 : 0 : if (flush)
510 : 0 : g_warning ("Partial character at end of write buffer not flushed.");
511 : 0 : channel->partial_write_buf[0] = '\0';
512 : 0 : }
513 : :
514 : 1444 : status = channel->funcs->io_close (channel, err);
515 : :
516 : 1444 : channel->close_on_unref = FALSE; /* Because we already did */
517 : 1444 : channel->is_readable = FALSE;
518 : 1444 : channel->is_writeable = FALSE;
519 : 1444 : channel->is_seekable = FALSE;
520 : :
521 : 1444 : if (status != G_IO_STATUS_NORMAL)
522 : : {
523 : 0 : g_clear_error (&tmperr);
524 : 0 : return status;
525 : : }
526 : 1444 : else if (result != G_IO_STATUS_NORMAL)
527 : : {
528 : 0 : g_propagate_error (err, tmperr);
529 : 0 : return result;
530 : : }
531 : : else
532 : 1444 : return G_IO_STATUS_NORMAL;
533 : 528 : }
534 : :
535 : : /* This function is used for the final flush on close or unref */
536 : : static void
537 : 508 : g_io_channel_purge (GIOChannel *channel)
538 : : {
539 : 508 : GError *err = NULL;
540 : : GIOStatus status G_GNUC_UNUSED;
541 : :
542 : 508 : g_return_if_fail (channel != NULL);
543 : :
544 : 508 : if (channel->write_buf && channel->write_buf->len > 0)
545 : : {
546 : : GIOFlags flags;
547 : :
548 : : /* Set the channel to blocking, to avoid a busy loop
549 : : */
550 : 63 : flags = g_io_channel_get_flags (channel);
551 : 63 : g_io_channel_set_flags (channel, flags & ~G_IO_FLAG_NONBLOCK, NULL);
552 : :
553 : 63 : status = g_io_channel_flush (channel, &err);
554 : :
555 : 63 : if (err)
556 : : { /* No way to return the error */
557 : 0 : g_warning ("Error flushing string: %s", err->message);
558 : 0 : g_error_free (err);
559 : 0 : }
560 : 30 : }
561 : :
562 : : /* Flush these in case anyone tries to close without unrefing */
563 : :
564 : 508 : if (channel->read_buf)
565 : 318 : g_string_truncate (channel->read_buf, 0);
566 : 508 : if (channel->write_buf)
567 : 63 : g_string_truncate (channel->write_buf, 0);
568 : 508 : if (channel->encoding)
569 : : {
570 : 217 : if (channel->encoded_read_buf)
571 : 205 : g_string_truncate (channel->encoded_read_buf, 0);
572 : :
573 : 217 : if (channel->partial_write_buf[0] != '\0')
574 : : {
575 : 0 : g_warning ("Partial character at end of write buffer not flushed.");
576 : 0 : channel->partial_write_buf[0] = '\0';
577 : 0 : }
578 : 14 : }
579 : 188 : }
580 : :
581 : : /**
582 : : * g_io_create_watch:
583 : : * @channel: a #GIOChannel to watch
584 : : * @condition: conditions to watch for
585 : : *
586 : : * Creates a #GSource that's dispatched when @condition is met for the
587 : : * given @channel. For example, if condition is %G_IO_IN, the source will
588 : : * be dispatched when there's data available for reading.
589 : : *
590 : : * The callback function invoked by the #GSource should be added with
591 : : * g_source_set_callback(), but it has type #GIOFunc (not #GSourceFunc).
592 : : *
593 : : * g_io_add_watch() is a simpler interface to this same functionality, for
594 : : * the case where you want to add the source to the default main loop context
595 : : * at the default priority.
596 : : *
597 : : * On Windows, polling a #GSource created to watch a channel for a socket
598 : : * puts the socket in non-blocking mode. This is a side-effect of the
599 : : * implementation and unavoidable.
600 : : *
601 : : * Returns: a new #GSource
602 : : */
603 : : GSource *
604 : 1402 : g_io_create_watch (GIOChannel *channel,
605 : : GIOCondition condition)
606 : : {
607 : 1402 : g_return_val_if_fail (channel != NULL, NULL);
608 : :
609 : 1402 : return channel->funcs->io_create_watch (channel, condition);
610 : 538 : }
611 : :
612 : : /**
613 : : * g_io_add_watch_full: (rename-to g_io_add_watch)
614 : : * @channel: a #GIOChannel
615 : : * @priority: the priority of the #GIOChannel source
616 : : * @condition: the condition to watch for
617 : : * @func: the function to call when the condition is satisfied
618 : : * @user_data: user data to pass to @func
619 : : * @notify: the function to call when the source is removed
620 : : *
621 : : * Adds the #GIOChannel into the default main loop context
622 : : * with the given priority.
623 : : *
624 : : * This internally creates a main loop source using g_io_create_watch()
625 : : * and attaches it to the main loop context with g_source_attach().
626 : : * You can do these steps manually if you need greater control.
627 : : *
628 : : * Returns: the event source id
629 : : */
630 : : guint
631 : 17 : g_io_add_watch_full (GIOChannel *channel,
632 : : gint priority,
633 : : GIOCondition condition,
634 : : GIOFunc func,
635 : : gpointer user_data,
636 : : GDestroyNotify notify)
637 : : {
638 : : GSource *source;
639 : : guint id;
640 : :
641 : 17 : g_return_val_if_fail (channel != NULL, 0);
642 : :
643 : 17 : source = g_io_create_watch (channel, condition);
644 : :
645 : 17 : if (priority != G_PRIORITY_DEFAULT)
646 : 1 : g_source_set_priority (source, priority);
647 : 17 : g_source_set_callback (source, (GSourceFunc)func, user_data, notify);
648 : :
649 : 17 : id = g_source_attach (source, NULL);
650 : 17 : g_source_unref (source);
651 : :
652 : 17 : return id;
653 : 6 : }
654 : :
655 : : /**
656 : : * g_io_add_watch:
657 : : * @channel: a #GIOChannel
658 : : * @condition: the condition to watch for
659 : : * @func: the function to call when the condition is satisfied
660 : : * @user_data: user data to pass to @func
661 : : *
662 : : * Adds the #GIOChannel into the default main loop context
663 : : * with the default priority.
664 : : *
665 : : * Returns: the event source id
666 : : */
667 : : /**
668 : : * GIOFunc:
669 : : * @source: the #GIOChannel event source
670 : : * @condition: the condition which has been satisfied
671 : : * @data: user data set in g_io_add_watch() or g_io_add_watch_full()
672 : : *
673 : : * Specifies the type of function passed to g_io_add_watch() or
674 : : * g_io_add_watch_full(), which is called when the requested condition
675 : : * on a #GIOChannel is satisfied.
676 : : *
677 : : * Returns: the function should return %FALSE if the event source
678 : : * should be removed
679 : : **/
680 : : /**
681 : : * GIOCondition:
682 : : * @G_IO_IN: There is data to read.
683 : : * @G_IO_OUT: Data can be written (without blocking).
684 : : * @G_IO_PRI: There is urgent data to read.
685 : : * @G_IO_ERR: Error condition.
686 : : * @G_IO_HUP: Hung up (the connection has been broken, usually for
687 : : * pipes and sockets).
688 : : * @G_IO_NVAL: Invalid request. The file descriptor is not open.
689 : : *
690 : : * A bitwise combination representing a condition to watch for on an
691 : : * event source.
692 : : **/
693 : : guint
694 : 9 : g_io_add_watch (GIOChannel *channel,
695 : : GIOCondition condition,
696 : : GIOFunc func,
697 : : gpointer user_data)
698 : : {
699 : 9 : return g_io_add_watch_full (channel, G_PRIORITY_DEFAULT, condition, func, user_data, NULL);
700 : : }
701 : :
702 : : /**
703 : : * g_io_channel_get_buffer_condition:
704 : : * @channel: A #GIOChannel
705 : : *
706 : : * This function returns a #GIOCondition depending on whether there
707 : : * is data to be read/space to write data in the internal buffers in
708 : : * the #GIOChannel. Only the flags %G_IO_IN and %G_IO_OUT may be set.
709 : : *
710 : : * Returns: A #GIOCondition
711 : : **/
712 : : GIOCondition
713 : 23561 : g_io_channel_get_buffer_condition (GIOChannel *channel)
714 : : {
715 : 23561 : GIOCondition condition = 0;
716 : :
717 : 23561 : if (channel->encoding)
718 : : {
719 : 3181 : if (channel->encoded_read_buf && (channel->encoded_read_buf->len > 0))
720 : 0 : condition |= G_IO_IN; /* Only return if we have full characters */
721 : 321 : }
722 : : else
723 : : {
724 : 20380 : if (channel->read_buf && (channel->read_buf->len > 0))
725 : 0 : condition |= G_IO_IN;
726 : : }
727 : :
728 : 23561 : if (channel->write_buf && (channel->write_buf->len < channel->buf_size))
729 : 0 : condition |= G_IO_OUT;
730 : :
731 : 23561 : return condition;
732 : : }
733 : :
734 : : /**
735 : : * g_io_channel_error_from_errno:
736 : : * @en: an `errno` error number, e.g. `EINVAL`
737 : : *
738 : : * Converts an `errno` error number to a #GIOChannelError.
739 : : *
740 : : * Returns: a #GIOChannelError error number, e.g.
741 : : * %G_IO_CHANNEL_ERROR_INVAL.
742 : : **/
743 : : GIOChannelError
744 : 0 : g_io_channel_error_from_errno (gint en)
745 : : {
746 : : #ifdef EAGAIN
747 : 0 : g_return_val_if_fail (en != EAGAIN, G_IO_CHANNEL_ERROR_FAILED);
748 : : #endif
749 : :
750 : 0 : switch (en)
751 : : {
752 : : #ifdef EBADF
753 : 0 : case EBADF:
754 : 0 : g_warning ("Invalid file descriptor.");
755 : 0 : return G_IO_CHANNEL_ERROR_FAILED;
756 : : #endif
757 : :
758 : : #ifdef EFAULT
759 : 0 : case EFAULT:
760 : 0 : g_warning ("Buffer outside valid address space.");
761 : 0 : return G_IO_CHANNEL_ERROR_FAILED;
762 : : #endif
763 : :
764 : : #ifdef EFBIG
765 : 0 : case EFBIG:
766 : 0 : return G_IO_CHANNEL_ERROR_FBIG;
767 : : #endif
768 : :
769 : : #ifdef EINTR
770 : : /* In general, we should catch EINTR before we get here,
771 : : * but close() is allowed to return EINTR by POSIX, so
772 : : * we need to catch it here; EINTR from close() is
773 : : * unrecoverable, because it's undefined whether
774 : : * the fd was actually closed or not, so we just return
775 : : * a generic error code.
776 : : */
777 : 0 : case EINTR:
778 : 0 : return G_IO_CHANNEL_ERROR_FAILED;
779 : : #endif
780 : :
781 : : #ifdef EINVAL
782 : 0 : case EINVAL:
783 : 0 : return G_IO_CHANNEL_ERROR_INVAL;
784 : : #endif
785 : :
786 : : #ifdef EIO
787 : 0 : case EIO:
788 : 0 : return G_IO_CHANNEL_ERROR_IO;
789 : : #endif
790 : :
791 : : #ifdef EISDIR
792 : 0 : case EISDIR:
793 : 0 : return G_IO_CHANNEL_ERROR_ISDIR;
794 : : #endif
795 : :
796 : : #ifdef ENOSPC
797 : 0 : case ENOSPC:
798 : 0 : return G_IO_CHANNEL_ERROR_NOSPC;
799 : : #endif
800 : :
801 : : #ifdef ENXIO
802 : 0 : case ENXIO:
803 : 0 : return G_IO_CHANNEL_ERROR_NXIO;
804 : : #endif
805 : :
806 : : #ifdef EOVERFLOW
807 : : #if EOVERFLOW != EFBIG
808 : 0 : case EOVERFLOW:
809 : 0 : return G_IO_CHANNEL_ERROR_OVERFLOW;
810 : : #endif
811 : : #endif
812 : :
813 : : #ifdef EPIPE
814 : 0 : case EPIPE:
815 : 0 : return G_IO_CHANNEL_ERROR_PIPE;
816 : : #endif
817 : :
818 : 0 : default:
819 : 0 : return G_IO_CHANNEL_ERROR_FAILED;
820 : : }
821 : 0 : }
822 : :
823 : : /**
824 : : * g_io_channel_set_buffer_size:
825 : : * @channel: a #GIOChannel
826 : : * @size: the size of the buffer, or 0 to let GLib pick a good size
827 : : *
828 : : * Sets the buffer size.
829 : : **/
830 : : void
831 : 4 : g_io_channel_set_buffer_size (GIOChannel *channel,
832 : : gsize size)
833 : : {
834 : 4 : g_return_if_fail (channel != NULL);
835 : :
836 : 4 : if (size == 0)
837 : 0 : size = G_IO_NICE_BUF_SIZE;
838 : :
839 : 4 : if (size < MAX_CHAR_SIZE)
840 : 0 : size = MAX_CHAR_SIZE;
841 : :
842 : 4 : channel->buf_size = size;
843 : 2 : }
844 : :
845 : : /**
846 : : * g_io_channel_get_buffer_size:
847 : : * @channel: a #GIOChannel
848 : : *
849 : : * Gets the buffer size.
850 : : *
851 : : * Returns: the size of the buffer.
852 : : **/
853 : : gsize
854 : 2 : g_io_channel_get_buffer_size (GIOChannel *channel)
855 : : {
856 : 2 : g_return_val_if_fail (channel != NULL, 0);
857 : :
858 : 2 : return channel->buf_size;
859 : 1 : }
860 : :
861 : : /**
862 : : * g_io_channel_set_line_term:
863 : : * @channel: a #GIOChannel
864 : : * @line_term: (nullable): The line termination string. Use %NULL for
865 : : * autodetect. Autodetection breaks on "\n", "\r\n", "\r", "\0",
866 : : * and the Unicode paragraph separator. Autodetection should not be
867 : : * used for anything other than file-based channels.
868 : : * @length: The length of the termination string. If -1 is passed, the
869 : : * string is assumed to be nul-terminated. This option allows
870 : : * termination strings with embedded nuls.
871 : : *
872 : : * This sets the string that #GIOChannel uses to determine
873 : : * where in the file a line break occurs.
874 : : **/
875 : : void
876 : 6 : g_io_channel_set_line_term (GIOChannel *channel,
877 : : const gchar *line_term,
878 : : gint length)
879 : : {
880 : 6 : g_return_if_fail (channel != NULL);
881 : 6 : g_return_if_fail (line_term == NULL || length != 0); /* Disallow "" */
882 : :
883 : 6 : g_free (channel->line_term);
884 : :
885 : 6 : if (line_term == NULL)
886 : : {
887 : 0 : channel->line_term = NULL;
888 : 0 : channel->line_term_len = 0;
889 : 0 : }
890 : 6 : else if (length >= 0)
891 : : {
892 : : /* We store the value nul-terminated even if the input is not */
893 : 4 : channel->line_term = g_malloc0 (length + 1);
894 : 4 : memcpy (channel->line_term, line_term, length);
895 : 4 : channel->line_term_len = (guint) length;
896 : 2 : }
897 : : else
898 : : {
899 : : /* We’re constrained by line_term_len being a guint here */
900 : 2 : gsize length_size = strlen (line_term);
901 : 2 : g_return_if_fail (length_size <= G_MAXUINT);
902 : 2 : channel->line_term = g_strdup (line_term);
903 : 2 : channel->line_term_len = (guint) length_size;
904 : : }
905 : 3 : }
906 : :
907 : : /**
908 : : * g_io_channel_get_line_term:
909 : : * @channel: a #GIOChannel
910 : : * @length: (out) (optional): a location to return the length of the line terminator
911 : : *
912 : : * This returns the string that #GIOChannel uses to determine
913 : : * where in the file a line break occurs. A value of %NULL
914 : : * indicates autodetection. Since 2.84, the return value is always
915 : : * nul-terminated.
916 : : *
917 : : * Returns: The line termination string. This value
918 : : * is owned by GLib and must not be freed.
919 : : **/
920 : : const gchar *
921 : 8 : g_io_channel_get_line_term (GIOChannel *channel,
922 : : gint *length)
923 : : {
924 : 8 : g_return_val_if_fail (channel != NULL, NULL);
925 : :
926 : 8 : if (length)
927 : 6 : *length = channel->line_term_len;
928 : :
929 : 8 : return channel->line_term;
930 : 4 : }
931 : :
932 : : /**
933 : : * g_io_channel_set_flags:
934 : : * @channel: a #GIOChannel
935 : : * @flags: the flags to set on the IO channel
936 : : * @error: A location to return an error of type #GIOChannelError
937 : : *
938 : : * Sets the (writeable) flags in @channel to (@flags & %G_IO_FLAG_SET_MASK).
939 : : *
940 : : * Returns: the status of the operation.
941 : : **/
942 : : GIOStatus
943 : 76 : g_io_channel_set_flags (GIOChannel *channel,
944 : : GIOFlags flags,
945 : : GError **error)
946 : : {
947 : 76 : g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
948 : 76 : g_return_val_if_fail ((error == NULL) || (*error == NULL),
949 : : G_IO_STATUS_ERROR);
950 : :
951 : 112 : return (*channel->funcs->io_set_flags) (channel,
952 : 76 : flags & G_IO_FLAG_SET_MASK,
953 : 36 : error);
954 : 36 : }
955 : :
956 : : /**
957 : : * g_io_channel_get_flags:
958 : : * @channel: a #GIOChannel
959 : : *
960 : : * Gets the current flags for a #GIOChannel, including read-only
961 : : * flags such as %G_IO_FLAG_IS_READABLE.
962 : : *
963 : : * The values of the flags %G_IO_FLAG_IS_READABLE and %G_IO_FLAG_IS_WRITABLE
964 : : * are cached for internal use by the channel when it is created.
965 : : * If they should change at some later point (e.g. partial shutdown
966 : : * of a socket with the UNIX shutdown() function), the user
967 : : * should immediately call g_io_channel_get_flags() to update
968 : : * the internal values of these flags.
969 : : *
970 : : * Returns: the flags which are set on the channel
971 : : **/
972 : : GIOFlags
973 : 65 : g_io_channel_get_flags (GIOChannel *channel)
974 : : {
975 : : GIOFlags flags;
976 : :
977 : 65 : g_return_val_if_fail (channel != NULL, 0);
978 : :
979 : 65 : flags = (* channel->funcs->io_get_flags) (channel);
980 : :
981 : : /* Cross implementation code */
982 : :
983 : 65 : if (channel->is_seekable)
984 : 9 : flags |= G_IO_FLAG_IS_SEEKABLE;
985 : 65 : if (channel->is_readable)
986 : 2 : flags |= G_IO_FLAG_IS_READABLE;
987 : 65 : if (channel->is_writeable)
988 : 65 : flags |= G_IO_FLAG_IS_WRITABLE;
989 : :
990 : 65 : return flags;
991 : 31 : }
992 : :
993 : : /**
994 : : * g_io_channel_set_close_on_unref:
995 : : * @channel: a #GIOChannel
996 : : * @do_close: Whether to close the channel on the final unref of
997 : : * the GIOChannel data structure.
998 : : *
999 : : * Whether to close the channel on the final unref of the #GIOChannel
1000 : : * data structure. The default value of this is %TRUE for channels
1001 : : * created by g_io_channel_new_file (), and %FALSE for all other channels.
1002 : : *
1003 : : * Setting this flag to %TRUE for a channel you have already closed
1004 : : * can cause problems when the final reference to the #GIOChannel is dropped.
1005 : : **/
1006 : : void
1007 : 1397 : g_io_channel_set_close_on_unref (GIOChannel *channel,
1008 : : gboolean do_close)
1009 : : {
1010 : 1397 : g_return_if_fail (channel != NULL);
1011 : :
1012 : 1397 : channel->close_on_unref = do_close;
1013 : 520 : }
1014 : :
1015 : : /**
1016 : : * g_io_channel_get_close_on_unref:
1017 : : * @channel: a #GIOChannel.
1018 : : *
1019 : : * Returns whether the file/socket/whatever associated with @channel
1020 : : * will be closed when @channel receives its final unref and is
1021 : : * destroyed. The default value of this is %TRUE for channels created
1022 : : * by g_io_channel_new_file (), and %FALSE for all other channels.
1023 : : *
1024 : : * Returns: %TRUE if the channel will be closed, %FALSE otherwise.
1025 : : **/
1026 : : gboolean
1027 : 2 : g_io_channel_get_close_on_unref (GIOChannel *channel)
1028 : : {
1029 : 2 : g_return_val_if_fail (channel != NULL, FALSE);
1030 : :
1031 : 2 : return channel->close_on_unref;
1032 : 1 : }
1033 : :
1034 : : /**
1035 : : * g_io_channel_seek_position:
1036 : : * @channel: a #GIOChannel
1037 : : * @offset: The offset in bytes from the position specified by @type
1038 : : * @type: a #GSeekType. The type %G_SEEK_CUR is only allowed in those
1039 : : * cases where a call to g_io_channel_set_encoding ()
1040 : : * is allowed. See the documentation for
1041 : : * g_io_channel_set_encoding () for details.
1042 : : * @error: A location to return an error of type #GIOChannelError
1043 : : *
1044 : : * Replacement for g_io_channel_seek() with the new API.
1045 : : *
1046 : : * Returns: the status of the operation.
1047 : : **/
1048 : : /**
1049 : : * GSeekType:
1050 : : * @G_SEEK_CUR: the current position in the file.
1051 : : * @G_SEEK_SET: the start of the file.
1052 : : * @G_SEEK_END: the end of the file.
1053 : : *
1054 : : * An enumeration specifying the base position for a
1055 : : * g_io_channel_seek_position() operation.
1056 : : **/
1057 : : GIOStatus
1058 : 0 : g_io_channel_seek_position (GIOChannel *channel,
1059 : : gint64 offset,
1060 : : GSeekType type,
1061 : : GError **error)
1062 : : {
1063 : : GIOStatus status;
1064 : :
1065 : : /* For files, only one of the read and write buffers can contain data.
1066 : : * For sockets, both can contain data.
1067 : : */
1068 : :
1069 : 0 : g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
1070 : 0 : g_return_val_if_fail ((error == NULL) || (*error == NULL),
1071 : : G_IO_STATUS_ERROR);
1072 : 0 : g_return_val_if_fail (channel->is_seekable, G_IO_STATUS_ERROR);
1073 : :
1074 : 0 : switch (type)
1075 : : {
1076 : 0 : case G_SEEK_CUR: /* The user is seeking relative to the head of the buffer */
1077 : 0 : if (channel->use_buffer)
1078 : : {
1079 : 0 : if (channel->do_encode && channel->encoded_read_buf
1080 : 0 : && channel->encoded_read_buf->len > 0)
1081 : : {
1082 : 0 : g_warning ("Seek type G_SEEK_CUR not allowed for this"
1083 : : " channel's encoding.");
1084 : 0 : return G_IO_STATUS_ERROR;
1085 : : }
1086 : 0 : if (channel->read_buf)
1087 : 0 : offset -= channel->read_buf->len;
1088 : 0 : if (channel->encoded_read_buf)
1089 : : {
1090 : 0 : g_assert (channel->encoded_read_buf->len == 0 || !channel->do_encode);
1091 : :
1092 : : /* If there's anything here, it's because the encoding is UTF-8,
1093 : : * so we can just subtract the buffer length, the same as for
1094 : : * the unencoded data.
1095 : : */
1096 : :
1097 : 0 : offset -= channel->encoded_read_buf->len;
1098 : 0 : }
1099 : 0 : }
1100 : 0 : break;
1101 : 0 : case G_SEEK_SET:
1102 : : case G_SEEK_END:
1103 : 0 : break;
1104 : 0 : default:
1105 : 0 : g_warning ("g_io_channel_seek_position: unknown seek type");
1106 : 0 : return G_IO_STATUS_ERROR;
1107 : : }
1108 : :
1109 : 0 : if (channel->use_buffer)
1110 : : {
1111 : 0 : status = g_io_channel_flush (channel, error);
1112 : 0 : if (status != G_IO_STATUS_NORMAL)
1113 : 0 : return status;
1114 : 0 : }
1115 : :
1116 : 0 : status = channel->funcs->io_seek (channel, offset, type, error);
1117 : :
1118 : 0 : if ((status == G_IO_STATUS_NORMAL) && (channel->use_buffer))
1119 : : {
1120 : 0 : if (channel->read_buf)
1121 : 0 : g_string_truncate (channel->read_buf, 0);
1122 : :
1123 : : /* Conversion state no longer matches position in file */
1124 : 0 : if (channel->read_cd != (GIConv) -1)
1125 : 0 : g_iconv (channel->read_cd, NULL, NULL, NULL, NULL);
1126 : 0 : if (channel->write_cd != (GIConv) -1)
1127 : 0 : g_iconv (channel->write_cd, NULL, NULL, NULL, NULL);
1128 : :
1129 : 0 : if (channel->encoded_read_buf)
1130 : : {
1131 : 0 : g_assert (channel->encoded_read_buf->len == 0 || !channel->do_encode);
1132 : 0 : g_string_truncate (channel->encoded_read_buf, 0);
1133 : 0 : }
1134 : :
1135 : 0 : if (channel->partial_write_buf[0] != '\0')
1136 : : {
1137 : 0 : g_warning ("Partial character at end of write buffer not flushed.");
1138 : 0 : channel->partial_write_buf[0] = '\0';
1139 : 0 : }
1140 : 0 : }
1141 : :
1142 : 0 : return status;
1143 : 0 : }
1144 : :
1145 : : /**
1146 : : * g_io_channel_flush:
1147 : : * @channel: a #GIOChannel
1148 : : * @error: location to store an error of type #GIOChannelError
1149 : : *
1150 : : * Flushes the write buffer for the GIOChannel.
1151 : : *
1152 : : * Returns: the status of the operation: One of
1153 : : * %G_IO_STATUS_NORMAL, %G_IO_STATUS_AGAIN, or
1154 : : * %G_IO_STATUS_ERROR.
1155 : : **/
1156 : : GIOStatus
1157 : 252 : g_io_channel_flush (GIOChannel *channel,
1158 : : GError **error)
1159 : : {
1160 : : GIOStatus status;
1161 : 252 : gsize this_time = 1, bytes_written = 0;
1162 : :
1163 : 252 : g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
1164 : 252 : g_return_val_if_fail ((error == NULL) || (*error == NULL), G_IO_STATUS_ERROR);
1165 : :
1166 : 252 : if (channel->write_buf == NULL || channel->write_buf->len == 0)
1167 : 0 : return G_IO_STATUS_NORMAL;
1168 : :
1169 : 32 : do
1170 : : {
1171 : 252 : g_assert (this_time > 0);
1172 : :
1173 : 284 : status = channel->funcs->io_write (channel,
1174 : 252 : channel->write_buf->str + bytes_written,
1175 : 252 : channel->write_buf->len - bytes_written,
1176 : 32 : &this_time, error);
1177 : 252 : bytes_written += this_time;
1178 : 64 : }
1179 : 252 : while ((bytes_written < channel->write_buf->len)
1180 : 252 : && (status == G_IO_STATUS_NORMAL));
1181 : :
1182 : 252 : g_string_erase (channel->write_buf, 0, bytes_written);
1183 : :
1184 : 252 : return status;
1185 : 32 : }
1186 : :
1187 : : /**
1188 : : * g_io_channel_set_buffered:
1189 : : * @channel: a #GIOChannel
1190 : : * @buffered: whether to set the channel buffered or unbuffered
1191 : : *
1192 : : * The buffering state can only be set if the channel's encoding
1193 : : * is %NULL. For any other encoding, the channel must be buffered.
1194 : : *
1195 : : * A buffered channel can only be set unbuffered if the channel's
1196 : : * internal buffers have been flushed. Newly created channels or
1197 : : * channels which have returned %G_IO_STATUS_EOF
1198 : : * not require such a flush. For write-only channels, a call to
1199 : : * g_io_channel_flush () is sufficient. For all other channels,
1200 : : * the buffers may be flushed by a call to g_io_channel_seek_position ().
1201 : : * This includes the possibility of seeking with seek type %G_SEEK_CUR
1202 : : * and an offset of zero. Note that this means that socket-based
1203 : : * channels cannot be set unbuffered once they have had data
1204 : : * read from them.
1205 : : *
1206 : : * On unbuffered channels, it is safe to mix read and write
1207 : : * calls from the new and old APIs, if this is necessary for
1208 : : * maintaining old code.
1209 : : *
1210 : : * The default state of the channel is buffered.
1211 : : **/
1212 : : void
1213 : 1369 : g_io_channel_set_buffered (GIOChannel *channel,
1214 : : gboolean buffered)
1215 : : {
1216 : 1369 : g_return_if_fail (channel != NULL);
1217 : :
1218 : 1369 : if (channel->encoding != NULL)
1219 : : {
1220 : 0 : g_warning ("Need to have NULL encoding to set the buffering state of the "
1221 : : "channel.");
1222 : 0 : return;
1223 : : }
1224 : :
1225 : 1369 : g_return_if_fail (!channel->read_buf || channel->read_buf->len == 0);
1226 : 1369 : g_return_if_fail (!channel->write_buf || channel->write_buf->len == 0);
1227 : :
1228 : 1369 : channel->use_buffer = buffered;
1229 : 584 : }
1230 : :
1231 : : /**
1232 : : * g_io_channel_get_buffered:
1233 : : * @channel: a #GIOChannel
1234 : : *
1235 : : * Returns whether @channel is buffered.
1236 : : *
1237 : : * Return Value: %TRUE if the @channel is buffered.
1238 : : **/
1239 : : gboolean
1240 : 2 : g_io_channel_get_buffered (GIOChannel *channel)
1241 : : {
1242 : 2 : g_return_val_if_fail (channel != NULL, FALSE);
1243 : :
1244 : 2 : return channel->use_buffer;
1245 : 1 : }
1246 : :
1247 : : /**
1248 : : * g_io_channel_set_encoding:
1249 : : * @channel: a #GIOChannel
1250 : : * @encoding: (nullable): the encoding type
1251 : : * @error: location to store an error of type #GConvertError
1252 : : *
1253 : : * Sets the encoding for the input/output of the channel.
1254 : : * The internal encoding is always UTF-8. The default encoding
1255 : : * for the external file is UTF-8.
1256 : : *
1257 : : * The encoding %NULL is safe to use with binary data.
1258 : : *
1259 : : * The encoding can only be set if one of the following conditions
1260 : : * is true:
1261 : : *
1262 : : * - The channel was just created, and has not been written to or read from yet.
1263 : : *
1264 : : * - The channel is write-only.
1265 : : *
1266 : : * - The channel is a file, and the file pointer was just repositioned
1267 : : * by a call to g_io_channel_seek_position(). (This flushes all the
1268 : : * internal buffers.)
1269 : : *
1270 : : * - The current encoding is %NULL or UTF-8.
1271 : : *
1272 : : * - One of the (new API) read functions has just returned %G_IO_STATUS_EOF
1273 : : * (or, in the case of g_io_channel_read_to_end(), %G_IO_STATUS_NORMAL).
1274 : : *
1275 : : * - One of the functions g_io_channel_read_chars() or
1276 : : * g_io_channel_read_unichar() has returned %G_IO_STATUS_AGAIN or
1277 : : * %G_IO_STATUS_ERROR. This may be useful in the case of
1278 : : * %G_CONVERT_ERROR_ILLEGAL_SEQUENCE.
1279 : : * Returning one of these statuses from g_io_channel_read_line(),
1280 : : * g_io_channel_read_line_string(), or g_io_channel_read_to_end()
1281 : : * does not guarantee that the encoding can be changed.
1282 : : *
1283 : : * Channels which do not meet one of the above conditions cannot call
1284 : : * g_io_channel_seek_position() with an offset of %G_SEEK_CUR, and, if
1285 : : * they are "seekable", cannot call g_io_channel_write_chars() after
1286 : : * calling one of the API "read" functions.
1287 : : *
1288 : : * Return Value: %G_IO_STATUS_NORMAL if the encoding was successfully set
1289 : : */
1290 : : GIOStatus
1291 : 1605 : g_io_channel_set_encoding (GIOChannel *channel,
1292 : : const gchar *encoding,
1293 : : GError **error)
1294 : : {
1295 : : GIConv read_cd, write_cd;
1296 : : #ifndef G_DISABLE_ASSERT
1297 : : gboolean did_encode;
1298 : : #endif
1299 : :
1300 : 1605 : g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
1301 : 1605 : g_return_val_if_fail ((error == NULL) || (*error == NULL), G_IO_STATUS_ERROR);
1302 : :
1303 : : /* Make sure the encoded buffers are empty */
1304 : :
1305 : 1605 : g_return_val_if_fail (!channel->do_encode || !channel->encoded_read_buf ||
1306 : 0 : channel->encoded_read_buf->len == 0, G_IO_STATUS_ERROR);
1307 : :
1308 : 1605 : if (!channel->use_buffer)
1309 : : {
1310 : 0 : g_warning ("Need to set the channel buffered before setting the encoding.");
1311 : 0 : g_warning ("Assuming this is what you meant and acting accordingly.");
1312 : :
1313 : 0 : channel->use_buffer = TRUE;
1314 : 0 : }
1315 : :
1316 : 1605 : if (channel->partial_write_buf[0] != '\0')
1317 : : {
1318 : 0 : g_warning ("Partial character at end of write buffer not flushed.");
1319 : 0 : channel->partial_write_buf[0] = '\0';
1320 : 0 : }
1321 : :
1322 : : #ifndef G_DISABLE_ASSERT
1323 : 1605 : did_encode = channel->do_encode;
1324 : : #endif
1325 : :
1326 : 1605 : if (!encoding || strcmp (encoding, "UTF8") == 0 || strcmp (encoding, "UTF-8") == 0)
1327 : : {
1328 : 1603 : channel->do_encode = FALSE;
1329 : 1603 : read_cd = write_cd = (GIConv) -1;
1330 : 698 : }
1331 : : else
1332 : : {
1333 : 2 : gint err = 0;
1334 : 2 : const gchar *from_enc = NULL, *to_enc = NULL;
1335 : :
1336 : 2 : if (channel->is_readable)
1337 : : {
1338 : 2 : read_cd = g_iconv_open ("UTF-8", encoding);
1339 : :
1340 : 2 : if (read_cd == (GIConv) -1)
1341 : : {
1342 : 0 : err = errno;
1343 : 0 : from_enc = encoding;
1344 : 0 : to_enc = "UTF-8";
1345 : 0 : }
1346 : 1 : }
1347 : : else
1348 : 0 : read_cd = (GIConv) -1;
1349 : :
1350 : 2 : if (channel->is_writeable && err == 0)
1351 : : {
1352 : 0 : write_cd = g_iconv_open (encoding, "UTF-8");
1353 : :
1354 : 0 : if (write_cd == (GIConv) -1)
1355 : : {
1356 : 0 : err = errno;
1357 : 0 : from_enc = "UTF-8";
1358 : 0 : to_enc = encoding;
1359 : 0 : }
1360 : 0 : }
1361 : : else
1362 : 2 : write_cd = (GIConv) -1;
1363 : :
1364 : 2 : if (err != 0)
1365 : : {
1366 : 0 : g_assert (from_enc);
1367 : 0 : g_assert (to_enc);
1368 : :
1369 : 0 : if (err == EINVAL)
1370 : 0 : g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_NO_CONVERSION,
1371 : 0 : _("Conversion from character set “%s” to “%s” is not supported"),
1372 : 0 : from_enc, to_enc);
1373 : : else
1374 : 0 : g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED,
1375 : 0 : _("Could not open converter from “%s” to “%s”: %s"),
1376 : 0 : from_enc, to_enc, g_strerror (err));
1377 : :
1378 : 0 : if (read_cd != (GIConv) -1)
1379 : 0 : g_iconv_close (read_cd);
1380 : 0 : if (write_cd != (GIConv) -1)
1381 : 0 : g_iconv_close (write_cd);
1382 : :
1383 : 0 : return G_IO_STATUS_ERROR;
1384 : : }
1385 : :
1386 : 2 : channel->do_encode = TRUE;
1387 : : }
1388 : :
1389 : : /* The encoding is ok, so set the fields in channel */
1390 : :
1391 : 1605 : if (channel->read_cd != (GIConv) -1)
1392 : 0 : g_iconv_close (channel->read_cd);
1393 : 1605 : if (channel->write_cd != (GIConv) -1)
1394 : 0 : g_iconv_close (channel->write_cd);
1395 : :
1396 : 1605 : if (channel->encoded_read_buf && channel->encoded_read_buf->len > 0)
1397 : : {
1398 : 0 : g_assert (!did_encode); /* Encoding UTF-8, NULL doesn't use encoded_read_buf */
1399 : :
1400 : : /* This is just validated UTF-8, so we can copy it back into read_buf
1401 : : * so it can be encoded in whatever the new encoding is.
1402 : : */
1403 : :
1404 : 0 : g_string_prepend_len (channel->read_buf, channel->encoded_read_buf->str,
1405 : 0 : channel->encoded_read_buf->len);
1406 : 0 : g_string_truncate (channel->encoded_read_buf, 0);
1407 : 0 : }
1408 : :
1409 : 1605 : channel->read_cd = read_cd;
1410 : 1605 : channel->write_cd = write_cd;
1411 : :
1412 : 1605 : g_free (channel->encoding);
1413 : 1605 : channel->encoding = g_strdup (encoding);
1414 : :
1415 : 1605 : return G_IO_STATUS_NORMAL;
1416 : 699 : }
1417 : :
1418 : : /**
1419 : : * g_io_channel_get_encoding:
1420 : : * @channel: a #GIOChannel
1421 : : *
1422 : : * Gets the encoding for the input/output of the channel.
1423 : : * The internal encoding is always UTF-8. The encoding %NULL
1424 : : * makes the channel safe for binary data.
1425 : : *
1426 : : * Returns: A string containing the encoding, this string is
1427 : : * owned by GLib and must not be freed.
1428 : : **/
1429 : : const gchar *
1430 : 2 : g_io_channel_get_encoding (GIOChannel *channel)
1431 : : {
1432 : 2 : g_return_val_if_fail (channel != NULL, NULL);
1433 : :
1434 : 2 : return channel->encoding;
1435 : 1 : }
1436 : :
1437 : : static GIOStatus
1438 : 8551 : g_io_channel_fill_buffer (GIOChannel *channel,
1439 : : GError **err)
1440 : : {
1441 : : gsize read_size, cur_len, oldlen;
1442 : : GIOStatus status;
1443 : :
1444 : 8551 : if (channel->is_seekable && channel->write_buf && channel->write_buf->len > 0)
1445 : : {
1446 : 0 : status = g_io_channel_flush (channel, err);
1447 : 0 : if (status != G_IO_STATUS_NORMAL)
1448 : 0 : return status;
1449 : 0 : }
1450 : 8551 : if (channel->is_seekable && channel->partial_write_buf[0] != '\0')
1451 : : {
1452 : 0 : g_warning ("Partial character at end of write buffer not flushed.");
1453 : 0 : channel->partial_write_buf[0] = '\0';
1454 : 0 : }
1455 : :
1456 : 8551 : if (!channel->read_buf)
1457 : 328 : channel->read_buf = g_string_sized_new (channel->buf_size);
1458 : :
1459 : 8551 : cur_len = channel->read_buf->len;
1460 : :
1461 : 8551 : g_string_set_size (channel->read_buf, channel->read_buf->len + channel->buf_size);
1462 : :
1463 : 13297 : status = channel->funcs->io_read (channel, channel->read_buf->str + cur_len,
1464 : 4746 : channel->buf_size, &read_size, err);
1465 : :
1466 : 8551 : g_assert ((status == G_IO_STATUS_NORMAL) || (read_size == 0));
1467 : :
1468 : 8551 : g_string_truncate (channel->read_buf, read_size + cur_len);
1469 : :
1470 : 8662 : if ((status != G_IO_STATUS_NORMAL) &&
1471 : 337 : ((status != G_IO_STATUS_EOF) || (channel->read_buf->len == 0)))
1472 : 272 : return status;
1473 : :
1474 : 8279 : g_assert (channel->read_buf->len > 0);
1475 : :
1476 : 8279 : if (channel->encoded_read_buf)
1477 : 5677 : oldlen = channel->encoded_read_buf->len;
1478 : : else
1479 : : {
1480 : 2602 : oldlen = 0;
1481 : 2602 : if (channel->encoding)
1482 : 211 : channel->encoded_read_buf = g_string_sized_new (channel->buf_size);
1483 : : }
1484 : :
1485 : 8279 : if (channel->do_encode)
1486 : : {
1487 : : gsize errnum, inbytes_left, outbytes_left;
1488 : : gchar *inbuf, *outbuf;
1489 : : int errval;
1490 : :
1491 : 2 : g_assert (channel->encoded_read_buf);
1492 : :
1493 : 1 : reencode:
1494 : :
1495 : 2 : inbytes_left = channel->read_buf->len;
1496 : 3 : outbytes_left = MAX (channel->read_buf->len,
1497 : 1 : channel->encoded_read_buf->allocated_len
1498 : 1 : - channel->encoded_read_buf->len - 1); /* 1 for NULL */
1499 : 2 : outbytes_left = MAX (outbytes_left, 6);
1500 : :
1501 : 2 : inbuf = channel->read_buf->str;
1502 : 3 : g_string_set_size (channel->encoded_read_buf,
1503 : 2 : channel->encoded_read_buf->len + outbytes_left);
1504 : 3 : outbuf = channel->encoded_read_buf->str + channel->encoded_read_buf->len
1505 : 2 : - outbytes_left;
1506 : :
1507 : 2 : errnum = g_iconv (channel->read_cd, &inbuf, &inbytes_left,
1508 : : &outbuf, &outbytes_left);
1509 : 2 : errval = errno;
1510 : :
1511 : 2 : g_assert (inbuf + inbytes_left == channel->read_buf->str
1512 : 1 : + channel->read_buf->len);
1513 : 2 : g_assert (outbuf + outbytes_left == channel->encoded_read_buf->str
1514 : 1 : + channel->encoded_read_buf->len);
1515 : :
1516 : 3 : g_string_erase (channel->read_buf, 0,
1517 : 2 : channel->read_buf->len - inbytes_left);
1518 : 3 : g_string_truncate (channel->encoded_read_buf,
1519 : 1 : channel->encoded_read_buf->len - outbytes_left);
1520 : :
1521 : 2 : if (errnum == (gsize) -1)
1522 : : {
1523 : 0 : switch (errval)
1524 : : {
1525 : 0 : case EINVAL:
1526 : 0 : if ((oldlen == channel->encoded_read_buf->len)
1527 : 0 : && (status == G_IO_STATUS_EOF))
1528 : 0 : status = G_IO_STATUS_EOF;
1529 : : else
1530 : 0 : status = G_IO_STATUS_NORMAL;
1531 : 0 : break;
1532 : 0 : case E2BIG:
1533 : : /* Buffer size at least 6, wrote at least on character */
1534 : 0 : g_assert (inbuf != channel->read_buf->str);
1535 : 0 : goto reencode;
1536 : 0 : case EILSEQ:
1537 : 0 : if (oldlen < channel->encoded_read_buf->len)
1538 : 0 : status = G_IO_STATUS_NORMAL;
1539 : : else
1540 : : {
1541 : 0 : g_set_error_literal (err, G_CONVERT_ERROR,
1542 : : G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1543 : 0 : _("Invalid byte sequence in conversion input"));
1544 : 0 : return G_IO_STATUS_ERROR;
1545 : : }
1546 : 0 : break;
1547 : 0 : default:
1548 : 0 : g_assert (errval != EBADF); /* The converter should be open */
1549 : 0 : g_set_error (err, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED,
1550 : 0 : _("Error during conversion: %s"), g_strerror (errval));
1551 : 0 : return G_IO_STATUS_ERROR;
1552 : : }
1553 : 0 : }
1554 : 3 : g_assert ((status != G_IO_STATUS_NORMAL)
1555 : 1 : || (channel->encoded_read_buf->len > 0));
1556 : 1 : }
1557 : 8277 : else if (channel->encoding) /* UTF-8 */
1558 : : {
1559 : : gchar *nextchar, *lastchar;
1560 : :
1561 : 5886 : g_assert (channel->encoded_read_buf);
1562 : :
1563 : 5886 : nextchar = channel->read_buf->str;
1564 : 5886 : lastchar = channel->read_buf->str + channel->read_buf->len;
1565 : :
1566 : 4832763 : while (nextchar < lastchar)
1567 : : {
1568 : : gunichar val_char;
1569 : :
1570 : 4826877 : val_char = g_utf8_get_char_validated (nextchar, lastchar - nextchar);
1571 : :
1572 : 4826877 : switch (val_char)
1573 : : {
1574 : 397 : case -2:
1575 : : /* stop, leave partial character in buffer */
1576 : 794 : lastchar = nextchar;
1577 : 794 : break;
1578 : 0 : case -1:
1579 : 0 : if (oldlen < channel->encoded_read_buf->len)
1580 : 0 : status = G_IO_STATUS_NORMAL;
1581 : : else
1582 : : {
1583 : 0 : g_set_error_literal (err, G_CONVERT_ERROR,
1584 : : G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1585 : 0 : _("Invalid byte sequence in conversion input"));
1586 : 0 : status = G_IO_STATUS_ERROR;
1587 : : }
1588 : 0 : lastchar = nextchar;
1589 : 0 : break;
1590 : 2417964 : default:
1591 : 4826083 : nextchar = g_utf8_next_char (nextchar);
1592 : 4826083 : break;
1593 : : }
1594 : : }
1595 : :
1596 : 5886 : if (lastchar > channel->read_buf->str)
1597 : : {
1598 : 5886 : gint copy_len = lastchar - channel->read_buf->str;
1599 : :
1600 : 8661 : g_string_append_len (channel->encoded_read_buf, channel->read_buf->str,
1601 : 2775 : copy_len);
1602 : 5886 : g_string_erase (channel->read_buf, 0, copy_len);
1603 : 2775 : }
1604 : 2775 : }
1605 : :
1606 : 8279 : return status;
1607 : 4746 : }
1608 : :
1609 : : /**
1610 : : * g_io_channel_read_line:
1611 : : * @channel: a #GIOChannel
1612 : : * @str_return: (out) (nullable) (not optional): The line read from the #GIOChannel, including the
1613 : : * line terminator. This data should be freed with g_free()
1614 : : * when no longer needed. This is a nul-terminated string.
1615 : : * If a @length of zero is returned, this will be %NULL instead.
1616 : : * @length: (out) (optional): location to store length of the read data, or %NULL
1617 : : * @terminator_pos: (out) (optional): location to store position of line terminator, or %NULL
1618 : : * @error: A location to return an error of type #GConvertError
1619 : : * or #GIOChannelError
1620 : : *
1621 : : * Reads a line, including the terminating character(s),
1622 : : * from a #GIOChannel into a newly-allocated string.
1623 : : * @str_return will contain allocated memory if the return
1624 : : * is %G_IO_STATUS_NORMAL.
1625 : : *
1626 : : * Returns: the status of the operation.
1627 : : **/
1628 : : GIOStatus
1629 : 288 : g_io_channel_read_line (GIOChannel *channel,
1630 : : gchar **str_return,
1631 : : gsize *length,
1632 : : gsize *terminator_pos,
1633 : : GError **error)
1634 : : {
1635 : : GIOStatus status;
1636 : : gsize got_length;
1637 : :
1638 : 288 : g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
1639 : 288 : g_return_val_if_fail (str_return != NULL, G_IO_STATUS_ERROR);
1640 : 288 : g_return_val_if_fail ((error == NULL) || (*error == NULL),
1641 : : G_IO_STATUS_ERROR);
1642 : 288 : g_return_val_if_fail (channel->is_readable, G_IO_STATUS_ERROR);
1643 : :
1644 : 288 : status = g_io_channel_read_line_backend (channel, &got_length, terminator_pos, error);
1645 : :
1646 : 288 : if (length && status != G_IO_STATUS_ERROR)
1647 : 6 : *length = got_length;
1648 : :
1649 : 288 : if (status == G_IO_STATUS_NORMAL)
1650 : : {
1651 : : gchar *line;
1652 : :
1653 : : /* Copy the read bytes (including any embedded nuls) and nul-terminate.
1654 : : * `USE_BUF (channel)->str` is guaranteed to be nul-terminated as it’s a
1655 : : * #GString, so it’s safe to call g_memdup2() with +1 length to allocate
1656 : : * a nul-terminator. */
1657 : 288 : g_assert (USE_BUF (channel));
1658 : 288 : line = g_memdup2 (USE_BUF (channel)->str, got_length + 1);
1659 : 288 : line[got_length] = '\0';
1660 : 288 : *str_return = g_steal_pointer (&line);
1661 : 288 : g_string_erase (USE_BUF (channel), 0, got_length);
1662 : 3 : }
1663 : : else
1664 : 0 : *str_return = NULL;
1665 : :
1666 : 288 : return status;
1667 : 3 : }
1668 : :
1669 : : /**
1670 : : * g_io_channel_read_line_string:
1671 : : * @channel: a #GIOChannel
1672 : : * @buffer: a #GString into which the line will be written.
1673 : : * If @buffer already contains data, the old data will
1674 : : * be overwritten.
1675 : : * @terminator_pos: (nullable): location to store position of line terminator, or %NULL
1676 : : * @error: a location to store an error of type #GConvertError
1677 : : * or #GIOChannelError
1678 : : *
1679 : : * Reads a line from a #GIOChannel, using a #GString as a buffer.
1680 : : *
1681 : : * Returns: the status of the operation.
1682 : : **/
1683 : : GIOStatus
1684 : 40204 : g_io_channel_read_line_string (GIOChannel *channel,
1685 : : GString *buffer,
1686 : : gsize *terminator_pos,
1687 : : GError **error)
1688 : : {
1689 : : gsize length;
1690 : : GIOStatus status;
1691 : :
1692 : 40204 : g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
1693 : 40204 : g_return_val_if_fail (buffer != NULL, G_IO_STATUS_ERROR);
1694 : 40204 : g_return_val_if_fail ((error == NULL) || (*error == NULL),
1695 : : G_IO_STATUS_ERROR);
1696 : 40204 : g_return_val_if_fail (channel->is_readable, G_IO_STATUS_ERROR);
1697 : :
1698 : 40204 : if (buffer->len > 0)
1699 : 0 : g_string_truncate (buffer, 0); /* clear out the buffer */
1700 : :
1701 : 40204 : status = g_io_channel_read_line_backend (channel, &length, terminator_pos, error);
1702 : :
1703 : 40204 : if (status == G_IO_STATUS_NORMAL)
1704 : : {
1705 : 40200 : g_assert (USE_BUF (channel));
1706 : 40200 : g_string_append_len (buffer, USE_BUF (channel)->str, length);
1707 : 40200 : g_string_erase (USE_BUF (channel), 0, length);
1708 : 20100 : }
1709 : :
1710 : 40204 : return status;
1711 : 20102 : }
1712 : :
1713 : :
1714 : : static GIOStatus
1715 : 40492 : g_io_channel_read_line_backend (GIOChannel *channel,
1716 : : gsize *length,
1717 : : gsize *terminator_pos,
1718 : : GError **error)
1719 : : {
1720 : : GIOStatus status;
1721 : : gsize checked_to, line_term_len, line_length, got_term_len;
1722 : 40492 : gboolean first_time = TRUE;
1723 : :
1724 : 40492 : if (!channel->use_buffer)
1725 : : {
1726 : : /* Can't do a raw read in read_line */
1727 : 0 : g_set_error_literal (error, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED,
1728 : 0 : _("Can’t do a raw read in g_io_channel_read_line_string"));
1729 : 0 : return G_IO_STATUS_ERROR;
1730 : : }
1731 : :
1732 : 40492 : status = G_IO_STATUS_NORMAL;
1733 : :
1734 : 40492 : if (channel->line_term)
1735 : 4 : line_term_len = channel->line_term_len;
1736 : : else
1737 : 40488 : line_term_len = 3;
1738 : : /* This value used for setting checked_to, it's the longest of the four
1739 : : * we autodetect for.
1740 : : */
1741 : :
1742 : 40492 : checked_to = 0;
1743 : :
1744 : 22848 : while (TRUE)
1745 : 2741 : {
1746 : : gchar *nextchar, *lastchar;
1747 : : GString *use_buf;
1748 : :
1749 : 45974 : if (!first_time || (BUF_LEN (USE_BUF (channel)) == 0))
1750 : 2772 : {
1751 : 3052 : read_again:
1752 : 5824 : status = g_io_channel_fill_buffer (channel, error);
1753 : 5824 : switch (status)
1754 : : {
1755 : 3048 : case G_IO_STATUS_NORMAL:
1756 : 5816 : if (BUF_LEN (USE_BUF (channel)) == 0)
1757 : : /* Can happen when using conversion and only read
1758 : : * part of a character
1759 : : */
1760 : : {
1761 : 0 : first_time = FALSE;
1762 : 0 : continue;
1763 : : }
1764 : 5816 : break;
1765 : 4 : case G_IO_STATUS_EOF:
1766 : 8 : if (BUF_LEN (USE_BUF (channel)) == 0)
1767 : : {
1768 : 4 : if (length)
1769 : 4 : *length = 0;
1770 : :
1771 : 4 : if (channel->encoding && channel->read_buf->len != 0)
1772 : : {
1773 : 0 : g_set_error_literal (error, G_CONVERT_ERROR,
1774 : : G_CONVERT_ERROR_PARTIAL_INPUT,
1775 : 0 : _("Leftover unconverted data in "
1776 : : "read buffer"));
1777 : 0 : return G_IO_STATUS_ERROR;
1778 : : }
1779 : : else
1780 : 4 : return G_IO_STATUS_EOF;
1781 : : }
1782 : 4 : break;
1783 : 0 : default:
1784 : 0 : if (length)
1785 : 0 : *length = 0;
1786 : 0 : return status;
1787 : : }
1788 : 2770 : }
1789 : :
1790 : 45970 : g_assert (BUF_LEN (USE_BUF (channel)) != 0);
1791 : :
1792 : 45970 : use_buf = USE_BUF (channel); /* The buffer has been created by this point */
1793 : :
1794 : 45970 : first_time = FALSE;
1795 : :
1796 : 45970 : lastchar = use_buf->str + use_buf->len;
1797 : :
1798 : 4845877 : for (nextchar = use_buf->str + checked_to; nextchar < lastchar;
1799 : 4799907 : channel->encoding ? nextchar = g_utf8_next_char (nextchar) : nextchar++)
1800 : : {
1801 : 4840391 : if (channel->line_term)
1802 : : {
1803 : 4138 : if ((size_t) (lastchar - nextchar) >= line_term_len &&
1804 : 4096 : memcmp (channel->line_term, nextchar, line_term_len) == 0)
1805 : : {
1806 : 2 : line_length = nextchar - use_buf->str;
1807 : 2 : got_term_len = line_term_len;
1808 : 2 : goto done;
1809 : : }
1810 : 2068 : }
1811 : : else /* auto detect */
1812 : : {
1813 : 4836253 : switch (*nextchar)
1814 : : {
1815 : 20382 : case '\n': /* unix */
1816 : 40482 : line_length = nextchar - use_buf->str;
1817 : 40482 : got_term_len = 1;
1818 : 40482 : goto done;
1819 : 0 : case '\r': /* Warning: do not use with sockets */
1820 : 0 : line_length = nextchar - use_buf->str;
1821 : 0 : if ((nextchar == lastchar - 1) && (status != G_IO_STATUS_EOF)
1822 : 0 : && (lastchar == use_buf->str + use_buf->len))
1823 : 0 : goto read_again; /* Try to read more data */
1824 : 0 : if ((nextchar < lastchar - 1) && (*(nextchar + 1) == '\n')) /* dos */
1825 : 0 : got_term_len = 2;
1826 : : else /* mac */
1827 : 0 : got_term_len = 1;
1828 : 0 : goto done;
1829 : 50385 : case '\xe2': /* Unicode paragraph separator */
1830 : 100770 : if ((size_t) (lastchar - nextchar) >= 3 &&
1831 : 100758 : strncmp ("\xe2\x80\xa9", nextchar, 3) == 0)
1832 : : {
1833 : 0 : line_length = nextchar - use_buf->str;
1834 : 0 : got_term_len = 3;
1835 : 0 : goto done;
1836 : : }
1837 : 100770 : break;
1838 : 0 : case '\0': /* Embedded null in input */
1839 : 0 : line_length = nextchar - use_buf->str;
1840 : 0 : got_term_len = 1;
1841 : 0 : goto done;
1842 : 2352032 : default: /* no match */
1843 : 4695001 : break;
1844 : : }
1845 : : }
1846 : 2395422 : }
1847 : :
1848 : : /* If encoding != NULL, valid UTF-8, didn't overshoot */
1849 : 5486 : g_assert (nextchar == lastchar);
1850 : :
1851 : : /* Check for EOF */
1852 : :
1853 : 5486 : if (status == G_IO_STATUS_EOF)
1854 : : {
1855 : 4 : if (channel->encoding && channel->read_buf->len > 0)
1856 : : {
1857 : 0 : g_set_error_literal (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
1858 : 0 : _("Channel terminates in a partial character"));
1859 : 0 : return G_IO_STATUS_ERROR;
1860 : : }
1861 : 4 : line_length = use_buf->len;
1862 : 4 : got_term_len = 0;
1863 : 4 : break;
1864 : : }
1865 : :
1866 : 5482 : if (use_buf->len > line_term_len - 1)
1867 : 5408 : checked_to = use_buf->len - (line_term_len - 1);
1868 : : else
1869 : 74 : checked_to = 0;
1870 : : }
1871 : :
1872 : 20385 : done:
1873 : :
1874 : 40488 : if (terminator_pos)
1875 : 40289 : *terminator_pos = line_length;
1876 : :
1877 : 40488 : if (length)
1878 : 40488 : *length = line_length + got_term_len;
1879 : :
1880 : 40488 : return G_IO_STATUS_NORMAL;
1881 : 20105 : }
1882 : :
1883 : : /**
1884 : : * g_io_channel_read_to_end:
1885 : : * @channel: a #GIOChannel
1886 : : * @str_return: (out) (array length=length) (element-type guint8): Location to
1887 : : * store a pointer to a string holding the remaining data in the
1888 : : * #GIOChannel. This data should be freed with g_free() when no
1889 : : * longer needed. This data is terminated by an extra nul
1890 : : * character, but there may be other nuls in the intervening data.
1891 : : * @length: (out): location to store length of the data
1892 : : * @error: location to return an error of type #GConvertError
1893 : : * or #GIOChannelError
1894 : : *
1895 : : * Reads all the remaining data from the file.
1896 : : *
1897 : : * Returns: %G_IO_STATUS_NORMAL on success.
1898 : : * This function never returns %G_IO_STATUS_EOF.
1899 : : **/
1900 : : GIOStatus
1901 : 0 : g_io_channel_read_to_end (GIOChannel *channel,
1902 : : gchar **str_return,
1903 : : gsize *length,
1904 : : GError **error)
1905 : : {
1906 : : GIOStatus status;
1907 : :
1908 : 0 : g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
1909 : 0 : g_return_val_if_fail ((error == NULL) || (*error == NULL),
1910 : : G_IO_STATUS_ERROR);
1911 : 0 : g_return_val_if_fail (channel->is_readable, G_IO_STATUS_ERROR);
1912 : :
1913 : 0 : if (str_return)
1914 : 0 : *str_return = NULL;
1915 : 0 : if (length)
1916 : 0 : *length = 0;
1917 : :
1918 : 0 : if (!channel->use_buffer)
1919 : : {
1920 : 0 : g_set_error_literal (error, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED,
1921 : 0 : _("Can’t do a raw read in g_io_channel_read_to_end"));
1922 : 0 : return G_IO_STATUS_ERROR;
1923 : : }
1924 : :
1925 : 0 : do
1926 : 0 : status = g_io_channel_fill_buffer (channel, error);
1927 : 0 : while (status == G_IO_STATUS_NORMAL);
1928 : :
1929 : 0 : if (status != G_IO_STATUS_EOF)
1930 : 0 : return status;
1931 : :
1932 : 0 : if (channel->encoding && channel->read_buf->len > 0)
1933 : : {
1934 : 0 : g_set_error_literal (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
1935 : 0 : _("Channel terminates in a partial character"));
1936 : 0 : return G_IO_STATUS_ERROR;
1937 : : }
1938 : :
1939 : 0 : if (USE_BUF (channel) == NULL)
1940 : : {
1941 : : /* length is already set to zero */
1942 : 0 : if (str_return)
1943 : 0 : *str_return = g_strdup ("");
1944 : 0 : }
1945 : : else
1946 : : {
1947 : 0 : if (length)
1948 : 0 : *length = USE_BUF (channel)->len;
1949 : :
1950 : 0 : if (str_return)
1951 : 0 : *str_return = g_string_free (USE_BUF (channel), FALSE);
1952 : : else
1953 : 0 : g_string_free (USE_BUF (channel), TRUE);
1954 : :
1955 : 0 : if (channel->encoding)
1956 : 0 : channel->encoded_read_buf = NULL;
1957 : : else
1958 : 0 : channel->read_buf = NULL;
1959 : : }
1960 : :
1961 : 0 : return G_IO_STATUS_NORMAL;
1962 : 0 : }
1963 : :
1964 : : /**
1965 : : * g_io_channel_read_chars:
1966 : : * @channel: a #GIOChannel
1967 : : * @buf: (out caller-allocates) (array length=count) (element-type guint8):
1968 : : * a buffer to read data into
1969 : : * @count: (in): the size of the buffer. Note that the buffer may not be
1970 : : * completely filled even if there is data in the buffer if the
1971 : : * remaining data is not a complete character.
1972 : : * @bytes_read: (out) (optional): The number of bytes read. This may be
1973 : : * zero even on success if count < 6 and the channel's encoding
1974 : : * is non-%NULL. This indicates that the next UTF-8 character is
1975 : : * too wide for the buffer.
1976 : : * @error: a location to return an error of type #GConvertError
1977 : : * or #GIOChannelError.
1978 : : *
1979 : : * Replacement for g_io_channel_read() with the new API.
1980 : : *
1981 : : * Returns: the status of the operation.
1982 : : */
1983 : : GIOStatus
1984 : 3306 : g_io_channel_read_chars (GIOChannel *channel,
1985 : : gchar *buf,
1986 : : gsize count,
1987 : : gsize *bytes_read,
1988 : : GError **error)
1989 : : {
1990 : : GIOStatus status;
1991 : : gsize got_bytes;
1992 : :
1993 : 3306 : g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
1994 : 3306 : g_return_val_if_fail ((error == NULL) || (*error == NULL), G_IO_STATUS_ERROR);
1995 : 3306 : g_return_val_if_fail (channel->is_readable, G_IO_STATUS_ERROR);
1996 : :
1997 : 3306 : if (count == 0)
1998 : : {
1999 : 0 : if (bytes_read)
2000 : 0 : *bytes_read = 0;
2001 : 0 : return G_IO_STATUS_NORMAL;
2002 : : }
2003 : 3306 : g_return_val_if_fail (buf != NULL, G_IO_STATUS_ERROR);
2004 : :
2005 : 3306 : if (!channel->use_buffer)
2006 : : {
2007 : : gsize tmp_bytes;
2008 : :
2009 : 2805 : g_assert (!channel->read_buf || channel->read_buf->len == 0);
2010 : :
2011 : 2805 : status = channel->funcs->io_read (channel, buf, count, &tmp_bytes, error);
2012 : :
2013 : 2805 : if (bytes_read)
2014 : 2805 : *bytes_read = tmp_bytes;
2015 : :
2016 : 2805 : return status;
2017 : : }
2018 : :
2019 : 501 : status = G_IO_STATUS_NORMAL;
2020 : :
2021 : 3228 : while (BUF_LEN (USE_BUF (channel)) < count && status == G_IO_STATUS_NORMAL)
2022 : 2727 : status = g_io_channel_fill_buffer (channel, error);
2023 : :
2024 : : /* Only return an error if we have no data */
2025 : :
2026 : 479 : if (BUF_LEN (USE_BUF (channel)) == 0)
2027 : : {
2028 : 188 : g_assert (status != G_IO_STATUS_NORMAL);
2029 : :
2030 : 188 : if (status == G_IO_STATUS_EOF && channel->encoding
2031 : 130 : && BUF_LEN (channel->read_buf) > 0)
2032 : : {
2033 : 0 : g_set_error_literal (error, G_CONVERT_ERROR,
2034 : : G_CONVERT_ERROR_PARTIAL_INPUT,
2035 : 0 : _("Leftover unconverted data in read buffer"));
2036 : 0 : status = G_IO_STATUS_ERROR;
2037 : 0 : }
2038 : :
2039 : 188 : if (bytes_read)
2040 : 188 : *bytes_read = 0;
2041 : :
2042 : 188 : return status;
2043 : : }
2044 : :
2045 : 291 : if (status == G_IO_STATUS_ERROR)
2046 : 0 : g_clear_error (error);
2047 : :
2048 : 291 : got_bytes = MIN (count, BUF_LEN (USE_BUF (channel)));
2049 : :
2050 : 291 : g_assert (got_bytes > 0);
2051 : :
2052 : 291 : if (channel->encoding)
2053 : : /* Don't validate for NULL encoding, binary safe */
2054 : : {
2055 : : gchar *nextchar, *prevchar;
2056 : :
2057 : 78 : g_assert (USE_BUF (channel) == channel->encoded_read_buf);
2058 : :
2059 : 78 : nextchar = channel->encoded_read_buf->str;
2060 : :
2061 : 11 : do
2062 : : {
2063 : 692 : prevchar = nextchar;
2064 : 692 : nextchar = g_utf8_next_char (nextchar);
2065 : 692 : g_assert (nextchar != prevchar); /* Possible for *prevchar of -1 or -2 */
2066 : 96 : }
2067 : 692 : while (nextchar < channel->encoded_read_buf->str + got_bytes);
2068 : :
2069 : 78 : if (nextchar > channel->encoded_read_buf->str + got_bytes)
2070 : 0 : got_bytes = prevchar - channel->encoded_read_buf->str;
2071 : :
2072 : 78 : g_assert (got_bytes > 0 || count < 6);
2073 : 11 : }
2074 : :
2075 : 291 : memcpy (buf, USE_BUF (channel)->str, got_bytes);
2076 : 291 : g_string_erase (USE_BUF (channel), 0, got_bytes);
2077 : :
2078 : 291 : if (bytes_read)
2079 : 291 : *bytes_read = got_bytes;
2080 : :
2081 : 291 : return G_IO_STATUS_NORMAL;
2082 : 1388 : }
2083 : :
2084 : : /**
2085 : : * g_io_channel_read_unichar:
2086 : : * @channel: a #GIOChannel
2087 : : * @thechar: (out): a location to return a character
2088 : : * @error: a location to return an error of type #GConvertError
2089 : : * or #GIOChannelError
2090 : : *
2091 : : * Reads a Unicode character from @channel.
2092 : : * This function cannot be called on a channel with %NULL encoding.
2093 : : *
2094 : : * Returns: a #GIOStatus
2095 : : **/
2096 : : GIOStatus
2097 : 0 : g_io_channel_read_unichar (GIOChannel *channel,
2098 : : gunichar *thechar,
2099 : : GError **error)
2100 : : {
2101 : 0 : GIOStatus status = G_IO_STATUS_NORMAL;
2102 : :
2103 : 0 : g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
2104 : 0 : g_return_val_if_fail (channel->encoding != NULL, G_IO_STATUS_ERROR);
2105 : 0 : g_return_val_if_fail ((error == NULL) || (*error == NULL),
2106 : : G_IO_STATUS_ERROR);
2107 : 0 : g_return_val_if_fail (channel->is_readable, G_IO_STATUS_ERROR);
2108 : :
2109 : 0 : while (BUF_LEN (channel->encoded_read_buf) == 0 && status == G_IO_STATUS_NORMAL)
2110 : 0 : status = g_io_channel_fill_buffer (channel, error);
2111 : :
2112 : : /* Only return an error if we have no data */
2113 : :
2114 : 0 : if (BUF_LEN (USE_BUF (channel)) == 0)
2115 : : {
2116 : 0 : g_assert (status != G_IO_STATUS_NORMAL);
2117 : :
2118 : 0 : if (status == G_IO_STATUS_EOF && BUF_LEN (channel->read_buf) > 0)
2119 : : {
2120 : 0 : g_set_error_literal (error, G_CONVERT_ERROR,
2121 : : G_CONVERT_ERROR_PARTIAL_INPUT,
2122 : 0 : _("Leftover unconverted data in read buffer"));
2123 : 0 : status = G_IO_STATUS_ERROR;
2124 : 0 : }
2125 : :
2126 : 0 : if (thechar)
2127 : 0 : *thechar = (gunichar) -1;
2128 : :
2129 : 0 : return status;
2130 : : }
2131 : :
2132 : 0 : if (status == G_IO_STATUS_ERROR)
2133 : 0 : g_clear_error (error);
2134 : :
2135 : 0 : if (thechar)
2136 : 0 : *thechar = g_utf8_get_char (channel->encoded_read_buf->str);
2137 : :
2138 : 0 : g_string_erase (channel->encoded_read_buf, 0,
2139 : 0 : g_utf8_next_char (channel->encoded_read_buf->str)
2140 : 0 : - channel->encoded_read_buf->str);
2141 : :
2142 : 0 : return G_IO_STATUS_NORMAL;
2143 : 0 : }
2144 : :
2145 : : /**
2146 : : * g_io_channel_write_chars:
2147 : : * @channel: a #GIOChannel
2148 : : * @buf: (array) (element-type guint8): a buffer to write data from
2149 : : * @count: the size of the buffer. If -1, the buffer
2150 : : * is taken to be a nul-terminated string.
2151 : : * @bytes_written: (out): The number of bytes written. This can be nonzero
2152 : : * even if the return value is not %G_IO_STATUS_NORMAL.
2153 : : * If the return value is %G_IO_STATUS_NORMAL and the
2154 : : * channel is blocking, this will always be equal
2155 : : * to @count if @count >= 0.
2156 : : * @error: a location to return an error of type #GConvertError
2157 : : * or #GIOChannelError
2158 : : *
2159 : : * Replacement for g_io_channel_write() with the new API.
2160 : : *
2161 : : * On seekable channels with encodings other than %NULL or UTF-8, generic
2162 : : * mixing of reading and writing is not allowed. A call to g_io_channel_write_chars ()
2163 : : * may only be made on a channel from which data has been read in the
2164 : : * cases described in the documentation for g_io_channel_set_encoding ().
2165 : : *
2166 : : * Returns: the status of the operation.
2167 : : **/
2168 : : GIOStatus
2169 : 4496 : g_io_channel_write_chars (GIOChannel *channel,
2170 : : const gchar *buf,
2171 : : gssize count,
2172 : : gsize *bytes_written,
2173 : : GError **error)
2174 : : {
2175 : : gsize count_unsigned;
2176 : : GIOStatus status;
2177 : 4496 : gsize wrote_bytes = 0;
2178 : :
2179 : 4496 : g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
2180 : 4496 : g_return_val_if_fail (buf != NULL || count == 0, G_IO_STATUS_ERROR);
2181 : 4496 : g_return_val_if_fail ((error == NULL) || (*error == NULL),
2182 : : G_IO_STATUS_ERROR);
2183 : 4496 : g_return_val_if_fail (channel->is_writeable, G_IO_STATUS_ERROR);
2184 : :
2185 : 4496 : if (count < 0)
2186 : 185 : count_unsigned = strlen (buf);
2187 : : else
2188 : 4311 : count_unsigned = count;
2189 : :
2190 : 4496 : if (count_unsigned == 0)
2191 : : {
2192 : 0 : if (bytes_written)
2193 : 0 : *bytes_written = 0;
2194 : 0 : return G_IO_STATUS_NORMAL;
2195 : : }
2196 : :
2197 : 4496 : g_assert (count_unsigned > 0);
2198 : :
2199 : : /* Raw write case */
2200 : :
2201 : 4496 : if (!channel->use_buffer)
2202 : : {
2203 : : gsize tmp_bytes;
2204 : :
2205 : 0 : g_assert (!channel->write_buf || channel->write_buf->len == 0);
2206 : 0 : g_assert (channel->partial_write_buf[0] == '\0');
2207 : :
2208 : 0 : status = channel->funcs->io_write (channel, buf, count_unsigned,
2209 : 0 : &tmp_bytes, error);
2210 : :
2211 : 0 : if (bytes_written)
2212 : 0 : *bytes_written = tmp_bytes;
2213 : :
2214 : 0 : return status;
2215 : : }
2216 : :
2217 : : /* General case */
2218 : :
2219 : 6545 : if (channel->is_seekable && (( BUF_LEN (channel->read_buf) > 0)
2220 : 4105 : || (BUF_LEN (channel->encoded_read_buf) > 0)))
2221 : : {
2222 : 0 : if (channel->do_encode && BUF_LEN (channel->encoded_read_buf) > 0)
2223 : : {
2224 : 0 : g_warning ("Mixed reading and writing not allowed on encoded files");
2225 : 0 : return G_IO_STATUS_ERROR;
2226 : : }
2227 : 0 : status = g_io_channel_seek_position (channel, 0, G_SEEK_CUR, error);
2228 : 0 : if (status != G_IO_STATUS_NORMAL)
2229 : : {
2230 : 0 : if (bytes_written)
2231 : 0 : *bytes_written = 0;
2232 : 0 : return status;
2233 : : }
2234 : 0 : }
2235 : :
2236 : 4496 : if (!channel->write_buf)
2237 : 99 : channel->write_buf = g_string_sized_new (channel->buf_size);
2238 : :
2239 : 8992 : while (wrote_bytes < count_unsigned)
2240 : : {
2241 : : gsize space_in_buf;
2242 : :
2243 : : /* If the buffer is full, try a write immediately. In
2244 : : * the nonblocking case, this prevents the user from
2245 : : * writing just a little bit to the buffer every time
2246 : : * and never receiving an EAGAIN.
2247 : : */
2248 : :
2249 : 4496 : if (channel->write_buf->len >= channel->buf_size - MAX_CHAR_SIZE)
2250 : : {
2251 : 154 : gsize did_write = 0, this_time;
2252 : :
2253 : 77 : do
2254 : : {
2255 : 231 : status = channel->funcs->io_write (channel, channel->write_buf->str
2256 : 154 : + did_write, channel->write_buf->len
2257 : 77 : - did_write, &this_time, error);
2258 : 154 : did_write += this_time;
2259 : 154 : }
2260 : 154 : while (status == G_IO_STATUS_NORMAL &&
2261 : 154 : did_write < MIN (channel->write_buf->len, MAX_CHAR_SIZE));
2262 : :
2263 : 154 : g_string_erase (channel->write_buf, 0, did_write);
2264 : :
2265 : 154 : if (status != G_IO_STATUS_NORMAL)
2266 : : {
2267 : 0 : if (status == G_IO_STATUS_AGAIN && wrote_bytes > 0)
2268 : 0 : status = G_IO_STATUS_NORMAL;
2269 : 0 : if (bytes_written)
2270 : 0 : *bytes_written = wrote_bytes;
2271 : 0 : return status;
2272 : : }
2273 : 77 : }
2274 : :
2275 : 4496 : space_in_buf = MAX (channel->buf_size, channel->write_buf->allocated_len - 1)
2276 : 4496 : - channel->write_buf->len; /* 1 for NULL */
2277 : :
2278 : : /* This is only true because g_io_channel_set_buffer_size ()
2279 : : * ensures that channel->buf_size >= MAX_CHAR_SIZE.
2280 : : */
2281 : 4496 : g_assert (space_in_buf >= MAX_CHAR_SIZE);
2282 : :
2283 : 4496 : if (!channel->encoding)
2284 : : {
2285 : 4301 : gsize write_this = MIN (space_in_buf, count_unsigned - wrote_bytes);
2286 : :
2287 : : /* g_string_append_len() takes a gssize, so don’t overflow it*/
2288 : 4301 : if (write_this > G_MAXSSIZE)
2289 : 0 : write_this = G_MAXSSIZE;
2290 : :
2291 : 4301 : g_string_append_len (channel->write_buf, buf, write_this);
2292 : 4301 : buf += write_this;
2293 : 4301 : wrote_bytes += write_this;
2294 : 2149 : }
2295 : : else
2296 : : {
2297 : : const gchar *from_buf;
2298 : : gsize from_buf_len, from_buf_old_len, left_len;
2299 : : gsize err;
2300 : : gint errnum;
2301 : :
2302 : 200 : if (channel->partial_write_buf[0] != '\0')
2303 : : {
2304 : 0 : g_assert (wrote_bytes == 0);
2305 : :
2306 : 0 : from_buf = channel->partial_write_buf;
2307 : 0 : from_buf_old_len = strlen (channel->partial_write_buf);
2308 : 0 : g_assert (from_buf_old_len > 0);
2309 : 0 : from_buf_len = MIN (6, from_buf_old_len + count_unsigned);
2310 : :
2311 : 0 : memcpy (channel->partial_write_buf + from_buf_old_len, buf,
2312 : 0 : from_buf_len - from_buf_old_len);
2313 : 0 : }
2314 : : else
2315 : : {
2316 : 195 : from_buf = buf;
2317 : 195 : from_buf_len = count_unsigned - wrote_bytes;
2318 : 195 : from_buf_old_len = 0;
2319 : : }
2320 : :
2321 : 190 : reconvert:
2322 : :
2323 : 195 : if (!channel->do_encode) /* UTF-8 encoding */
2324 : : {
2325 : : const gchar *badchar;
2326 : 195 : gsize try_len = MIN (from_buf_len, space_in_buf);
2327 : :
2328 : : /* UTF-8, just validate, emulate g_iconv */
2329 : :
2330 : 195 : if (!g_utf8_validate_len (from_buf, try_len, &badchar))
2331 : : {
2332 : : gunichar try_char;
2333 : 0 : gsize incomplete_len = from_buf + try_len - badchar;
2334 : :
2335 : 0 : left_len = from_buf + from_buf_len - badchar;
2336 : :
2337 : 0 : try_char = g_utf8_get_char_validated (badchar, incomplete_len);
2338 : :
2339 : 0 : switch (try_char)
2340 : : {
2341 : 0 : case -2:
2342 : 0 : g_assert (incomplete_len < 6);
2343 : 0 : if (try_len == from_buf_len)
2344 : : {
2345 : 0 : errnum = EINVAL;
2346 : 0 : err = (gsize) -1;
2347 : 0 : }
2348 : : else
2349 : : {
2350 : 0 : errnum = 0;
2351 : 0 : err = (gsize) 0;
2352 : : }
2353 : 0 : break;
2354 : 0 : case -1:
2355 : 0 : g_warning ("Invalid UTF-8 passed to g_io_channel_write_chars().");
2356 : : /* FIXME bail here? */
2357 : 0 : errnum = EILSEQ;
2358 : 0 : err = (gsize) -1;
2359 : 0 : break;
2360 : 0 : default:
2361 : : g_assert_not_reached ();
2362 : 0 : err = (gsize) -1;
2363 : 0 : errnum = 0; /* Don't confuse the compiler */
2364 : 0 : }
2365 : 0 : }
2366 : : else
2367 : : {
2368 : 195 : err = (gsize) 0;
2369 : 195 : errnum = 0;
2370 : 195 : left_len = from_buf_len - try_len;
2371 : : }
2372 : :
2373 : 200 : g_string_append_len (channel->write_buf, from_buf,
2374 : 5 : from_buf_len - left_len);
2375 : 195 : from_buf += from_buf_len - left_len;
2376 : 5 : }
2377 : : else
2378 : : {
2379 : : gchar *outbuf;
2380 : :
2381 : 0 : left_len = from_buf_len;
2382 : 0 : g_string_set_size (channel->write_buf, channel->write_buf->len
2383 : 0 : + space_in_buf);
2384 : 0 : outbuf = channel->write_buf->str + channel->write_buf->len
2385 : 0 : - space_in_buf;
2386 : 0 : err = g_iconv (channel->write_cd, (gchar **) &from_buf, &left_len,
2387 : : &outbuf, &space_in_buf);
2388 : 0 : errnum = errno;
2389 : 0 : g_string_truncate (channel->write_buf, channel->write_buf->len
2390 : 0 : - space_in_buf);
2391 : : }
2392 : :
2393 : 195 : if (err == (gsize) -1)
2394 : : {
2395 : 0 : switch (errnum)
2396 : : {
2397 : 0 : case EINVAL:
2398 : 0 : g_assert (left_len < 6);
2399 : :
2400 : 0 : if (from_buf_old_len == 0)
2401 : : {
2402 : : /* Not from partial_write_buf */
2403 : :
2404 : 0 : memcpy (channel->partial_write_buf, from_buf, left_len);
2405 : 0 : channel->partial_write_buf[left_len] = '\0';
2406 : 0 : if (bytes_written)
2407 : 0 : *bytes_written = count_unsigned;
2408 : 0 : return G_IO_STATUS_NORMAL;
2409 : : }
2410 : :
2411 : : /* Working in partial_write_buf */
2412 : :
2413 : 0 : if (left_len == from_buf_len)
2414 : : {
2415 : : /* Didn't convert anything, must still have
2416 : : * less than a full character
2417 : : */
2418 : :
2419 : 0 : g_assert (count_unsigned == from_buf_len - from_buf_old_len);
2420 : :
2421 : 0 : channel->partial_write_buf[from_buf_len] = '\0';
2422 : :
2423 : 0 : if (bytes_written)
2424 : 0 : *bytes_written = count_unsigned;
2425 : :
2426 : 0 : return G_IO_STATUS_NORMAL;
2427 : : }
2428 : :
2429 : 0 : g_assert (from_buf_len - left_len >= from_buf_old_len);
2430 : :
2431 : : /* We converted all the old data. This is fine */
2432 : :
2433 : 0 : break;
2434 : 0 : case E2BIG:
2435 : 0 : if (from_buf_len == left_len)
2436 : : {
2437 : : /* Nothing was written, add enough space for
2438 : : * at least one character.
2439 : : */
2440 : 0 : space_in_buf += MAX_CHAR_SIZE;
2441 : 0 : goto reconvert;
2442 : : }
2443 : 0 : break;
2444 : 0 : case EILSEQ:
2445 : 0 : g_set_error_literal (error, G_CONVERT_ERROR,
2446 : : G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
2447 : 0 : _("Invalid byte sequence in conversion input"));
2448 : 0 : if (from_buf_old_len > 0 && from_buf_len == left_len)
2449 : 0 : g_warning ("Illegal sequence due to partial character "
2450 : : "at the end of a previous write.");
2451 : : else
2452 : : {
2453 : 0 : g_assert (from_buf_len >= left_len + from_buf_old_len);
2454 : 0 : wrote_bytes += from_buf_len - left_len - from_buf_old_len;
2455 : : }
2456 : 0 : if (bytes_written)
2457 : 0 : *bytes_written = wrote_bytes;
2458 : 0 : channel->partial_write_buf[0] = '\0';
2459 : 0 : return G_IO_STATUS_ERROR;
2460 : 0 : default:
2461 : 0 : g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED,
2462 : 0 : _("Error during conversion: %s"), g_strerror (errnum));
2463 : 0 : if (from_buf_len >= left_len + from_buf_old_len)
2464 : 0 : wrote_bytes += from_buf_len - left_len - from_buf_old_len;
2465 : 0 : if (bytes_written)
2466 : 0 : *bytes_written = wrote_bytes;
2467 : 0 : channel->partial_write_buf[0] = '\0';
2468 : 0 : return G_IO_STATUS_ERROR;
2469 : : }
2470 : 0 : }
2471 : :
2472 : 195 : g_assert (from_buf_len - left_len >= from_buf_old_len);
2473 : :
2474 : 195 : wrote_bytes += from_buf_len - left_len - from_buf_old_len;
2475 : :
2476 : 195 : if (from_buf_old_len > 0)
2477 : : {
2478 : : /* We were working in partial_write_buf */
2479 : :
2480 : 0 : buf += from_buf_len - left_len - from_buf_old_len;
2481 : 0 : channel->partial_write_buf[0] = '\0';
2482 : 0 : }
2483 : : else
2484 : 195 : buf = from_buf;
2485 : : }
2486 : : }
2487 : :
2488 : 4496 : if (bytes_written)
2489 : 223 : *bytes_written = count_unsigned;
2490 : :
2491 : 4496 : return G_IO_STATUS_NORMAL;
2492 : 2154 : }
2493 : :
2494 : : /**
2495 : : * g_io_channel_write_unichar:
2496 : : * @channel: a #GIOChannel
2497 : : * @thechar: a character
2498 : : * @error: location to return an error of type #GConvertError
2499 : : * or #GIOChannelError
2500 : : *
2501 : : * Writes a Unicode character to @channel.
2502 : : * This function cannot be called on a channel with %NULL encoding.
2503 : : *
2504 : : * Returns: a #GIOStatus
2505 : : **/
2506 : : GIOStatus
2507 : 0 : g_io_channel_write_unichar (GIOChannel *channel,
2508 : : gunichar thechar,
2509 : : GError **error)
2510 : : {
2511 : : GIOStatus status;
2512 : : gchar static_buf[6];
2513 : : gsize char_len, wrote_len;
2514 : :
2515 : 0 : g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
2516 : 0 : g_return_val_if_fail (channel->encoding != NULL, G_IO_STATUS_ERROR);
2517 : 0 : g_return_val_if_fail ((error == NULL) || (*error == NULL),
2518 : : G_IO_STATUS_ERROR);
2519 : 0 : g_return_val_if_fail (channel->is_writeable, G_IO_STATUS_ERROR);
2520 : :
2521 : 0 : char_len = g_unichar_to_utf8 (thechar, static_buf);
2522 : :
2523 : 0 : if (channel->partial_write_buf[0] != '\0')
2524 : : {
2525 : 0 : g_warning ("Partial character written before writing unichar.");
2526 : 0 : channel->partial_write_buf[0] = '\0';
2527 : 0 : }
2528 : :
2529 : 0 : status = g_io_channel_write_chars (channel, static_buf,
2530 : 0 : char_len, &wrote_len, error);
2531 : :
2532 : : /* We validate UTF-8, so we can't get a partial write */
2533 : :
2534 : 0 : g_assert (wrote_len == char_len || status != G_IO_STATUS_NORMAL);
2535 : :
2536 : 0 : return status;
2537 : 0 : }
2538 : :
2539 : : /**
2540 : : * G_IO_CHANNEL_ERROR:
2541 : : *
2542 : : * Error domain for #GIOChannel operations. Errors in this domain will
2543 : : * be from the #GIOChannelError enumeration. See #GError for
2544 : : * information on error domains.
2545 : : **/
2546 : : /**
2547 : : * GIOChannelError:
2548 : : * @G_IO_CHANNEL_ERROR_FBIG: File too large.
2549 : : * @G_IO_CHANNEL_ERROR_INVAL: Invalid argument.
2550 : : * @G_IO_CHANNEL_ERROR_IO: IO error.
2551 : : * @G_IO_CHANNEL_ERROR_ISDIR: File is a directory.
2552 : : * @G_IO_CHANNEL_ERROR_NOSPC: No space left on device.
2553 : : * @G_IO_CHANNEL_ERROR_NXIO: No such device or address.
2554 : : * @G_IO_CHANNEL_ERROR_OVERFLOW: Value too large for defined datatype.
2555 : : * @G_IO_CHANNEL_ERROR_PIPE: Broken pipe.
2556 : : * @G_IO_CHANNEL_ERROR_FAILED: Some other error.
2557 : : *
2558 : : * Error codes returned by #GIOChannel operations.
2559 : : **/
2560 : :
2561 : 38 : G_DEFINE_QUARK (g-io-channel-error-quark, g_io_channel_error)
|