summaryrefslogtreecommitdiff
path: root/gegl/gegl-gio.c
blob: 39b3d7227eca561c8124d54cc875c7378cec24b0 (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
156
157
158
159
160
161
162
163
164
165
166
167
/* This file is part of GEGL
 *
 * GEGL 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 3 of the License, or (at your option) any later version.
 *
 * GEGL 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 GEGL; if not, see <http://www.gnu.org/licenses/>.
 *
 * Copyright 2014 Jon Nordby, The Grid <jononor@gmail.com>
 */

#include <gegl-gio-private.h>
#include <string.h>
#include <stdio.h>

#ifdef G_OS_WIN32
#include <windows.h>
#include <gio/gwin32inputstream.h>  
#else
#include <gio/gunixinputstream.h>
#endif

/**
 * datauri_parse_header:
 * @uri: URI to parse
 * @raw_data_out: (out) (transfer full): return location for pointer to raw data, following header
 * @header_items_no_out: (out) (transfer none): return location for number of items parsed from header
 *
 * Return value: (transfer full): header_item[0] is content-type, header_items[1] is encoding, free with g_strfreev()
 *
 * Note: currently private API
 */
static gchar **
datauri_parse_header(const gchar *uri, gchar **raw_data_out, gint *header_items_no_out)
{
  // Determine data format
  const gchar * header_end = g_strstr_len (uri, -1, ",");
  const gint data_prefix_len = strlen("data:");

  const gint header_len = header_end - uri - data_prefix_len;
  gchar *header = g_strndup(uri+data_prefix_len, header_len);  

  gchar **header_items = g_strsplit(header, ";", 3);
  gint header_items_no = -1;
  while (header_items[++header_items_no]) {
  }
  g_free(header);

  if (header_items_no_out)
    {
      *header_items_no_out = header_items_no;
    }

  if (raw_data_out)
    {
      *raw_data_out = (gchar *)uri+data_prefix_len+header_len;
    }
  return header_items;
}

// Supports data embedded in the URI itself
// http://en.wikipedia.org/wiki/Data_URI_scheme
// https://developer.mozilla.org/en-US/docs/Web/HTTP/data_URIs
static GInputStream *
input_stream_datauri(const gchar *uri)
{
  GInputStream *stream = NULL;
  gchar * raw_data = NULL;
  gint header_items_no = 0;
  gchar **header_items = datauri_parse_header(uri, &raw_data, &header_items_no);
  const gboolean is_base64 = header_items_no > 1 && g_strcmp0(header_items[1], "base64") == 0;

  if (is_base64) {
    gsize len = 0;
    guchar * data = g_base64_decode (raw_data, &len);
    stream = g_memory_input_stream_new_from_data (data, len, g_free);
  } else {
    gchar *data = g_strdup(raw_data);
    const gint len = strlen(data);
    stream = g_memory_input_stream_new_from_data (data, len, g_free);
  }

  g_strfreev(header_items);
  return stream;
}

gchar *
gegl_gio_datauri_get_content_type(const gchar *uri)
{
  gchar *content_type = NULL;
  gint header_items_no = 0;
  gchar **header_items = datauri_parse_header(uri, NULL, &header_items_no);
  if (header_items_no)
    content_type = g_strdup(header_items[0]);
  g_strfreev(header_items);
  return content_type;
}

gboolean
gegl_gio_uri_is_datauri(const gchar *uri)
{
    return g_str_has_prefix(uri, "data:");
}

/**
 * gegl_gio_open_input_stream:
 * @uri: (allow none) URI to open. @uri is preferred over @path if both are set
 * @path: (allow none) path to open. 
 * @out_file: (out) (transfer full): return location for GFile, if stream is for a file
 *
 * Return value: (transfer full): A new #GInputStream, free with g_object_unref()
 *
 * Note: currently private API
 */
GInputStream *
gegl_gio_open_input_stream(const gchar *uri, const gchar *path, GFile **out_file, GError **err)
{
  GFile *infile = NULL;
  GInputStream *fis = NULL;
  g_return_val_if_fail(uri || path, NULL);
  g_return_val_if_fail(out_file, NULL);

  if (path && g_strcmp0(path, "-") == 0)
    {
      const gboolean close_fd = FALSE;
      infile = NULL;
#ifdef G_OS_WIN32
      fis = g_win32_input_stream_new (GetStdHandle (STD_INPUT_HANDLE), close_fd);
#else
      fis = g_unix_input_stream_new(STDIN_FILENO, close_fd);
#endif
    }
  else if (uri && strlen(uri) > 0)
    {
      if (gegl_gio_uri_is_datauri(uri))
        {
          fis = input_stream_datauri(uri);
        }
      else
        {
          infile = g_file_new_for_uri(uri);
        }
    }
  else if (path && strlen(path) > 0)
    {
      infile = g_file_new_for_path(path);
    }
  else {
    return NULL;
  }

  if (infile)
    {
      g_assert(!fis);
      fis = G_INPUT_STREAM(g_file_read(infile, NULL, err));
      *out_file = infile;
    }

  return fis;
}