summaryrefslogtreecommitdiffstats
path: root/libmsi/libmsi-istream.c
blob: e74e8fb7b8ecf04dce60c06bbf8b1bac6e1c3801 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

#include "msipriv.h"
#include "libmsi-istream.h"

struct _LibmsiIStream
{
    GInputStream parent;

    GsfInput *input;
};

static void libmsi_seekable_iface_init (GSeekableIface  *iface);

G_DEFINE_TYPE_WITH_CODE (LibmsiIStream, libmsi_istream, G_TYPE_INPUT_STREAM,
                         G_IMPLEMENT_INTERFACE (G_TYPE_SEEKABLE,
                                                libmsi_seekable_iface_init);
                         )

static goffset
libmsi_tell (GSeekable *seekable)
{
    g_return_val_if_fail (LIBMSI_IS_ISTREAM(seekable), FALSE);

    return gsf_input_tell (LIBMSI_ISTREAM(seekable)->input);
}

static gboolean
libmsi_can_seek (GSeekable *seekable)
{
    return TRUE;
}

static gboolean
libmsi_seek (GSeekable *seekable, goffset offset,
             GSeekType type, GCancellable *cancellable, GError **error)
{
    g_return_val_if_fail (LIBMSI_IS_ISTREAM(seekable), FALSE);

    return !gsf_input_seek (LIBMSI_ISTREAM(seekable)->input, offset, type);
}

static gboolean
libmsi_can_truncate (GSeekable *seekable)
{
    return FALSE;
}

static void
libmsi_seekable_iface_init (GSeekableIface *iface)
{
    iface->tell = libmsi_tell;
    iface->can_seek = libmsi_can_seek;
    iface->seek = libmsi_seek;
    iface->can_truncate = libmsi_can_truncate;
}

static void
libmsi_istream_init (LibmsiIStream *self)
{
}

static void
libmsi_istream_finalize (GObject *object)
{
    LibmsiIStream *self = LIBMSI_ISTREAM (object);

    if (self->input)
        g_object_unref (self->input);

    G_OBJECT_CLASS (libmsi_istream_parent_class)->finalize (object);
}

static gssize
input_stream_read (GInputStream  *stream,
                   void *buffer,
                   gsize count,
                   GCancellable *cancellable,
                   GError **error)
{
    LibmsiIStream *self = LIBMSI_ISTREAM (stream);
    gssize remaining = gsf_input_remaining (self->input);

    if (remaining == 0)
        return 0;

    count = MIN(count, remaining);
    if (!gsf_input_read (self->input, count, buffer))
        return -1;

    return count;
}

static gssize
input_stream_skip (GInputStream *stream,
                   gsize count,
                   GCancellable  *cancellable,
                   GError **error)
{
    LibmsiIStream *self = LIBMSI_ISTREAM (stream);

    count = MIN (count, gsf_input_remaining (self->input));
    if (!gsf_input_seek (self->input, count, G_SEEK_CUR))
        return -1;

    return count;
}

static gboolean
input_stream_close (GInputStream *stream,
                    GCancellable *cancellable,
                    GError **error)
{
    return TRUE;
}

static void
libmsi_istream_class_init (LibmsiIStreamClass *klass)
{
    GObjectClass* object_class = G_OBJECT_CLASS (klass);
    GInputStreamClass *istream_class;

    object_class->finalize = libmsi_istream_finalize;

    istream_class = G_INPUT_STREAM_CLASS (klass);
    istream_class->read_fn = input_stream_read;
    istream_class->skip  = input_stream_skip;
    istream_class->close_fn = input_stream_close;
}

G_GNUC_INTERNAL LibmsiIStream *
libmsi_istream_new (GsfInput *input)
{
    GsfInput *dup = gsf_input_dup (input, NULL);
    g_return_val_if_fail (dup, NULL);

    LibmsiIStream *self = g_object_new (LIBMSI_TYPE_ISTREAM, NULL);
    self->input = dup;

    return self;
}