File: vtkURILoader.cxx

package info (click to toggle)
paraview 5.13.2%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 544,220 kB
  • sloc: cpp: 3,374,605; ansic: 1,332,409; python: 150,381; xml: 122,166; sql: 65,887; sh: 7,317; javascript: 5,262; yacc: 4,417; java: 3,977; perl: 2,363; lex: 1,929; f90: 1,397; makefile: 170; objc: 153; tcl: 59; pascal: 50; fortran: 29
file content (346 lines) | stat: -rw-r--r-- 9,013 bytes parent folder | download
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
// SPDX-License-Identifier: BSD-3-Clause
#include "vtkURILoader.h"

#include "vtkFileResourceStream.h"
#include "vtkMemoryResourceStream.h"
#include "vtkObjectFactory.h"
#include "vtkValueFromString.h"

#include <vtksys/Base64.h>
#include <vtksys/SystemTools.hxx>

#include <algorithm>
#include <array>
#include <cassert>
#include <cstring>

VTK_ABI_NAMESPACE_BEGIN

//------------------------------------------------------------------------------
struct vtkURILoader::vtkInternals
{
  vtkSmartPointer<vtkURI> BaseURI{};
};

//------------------------------------------------------------------------------
vtkStandardNewMacro(vtkURILoader);

//------------------------------------------------------------------------------
vtkURILoader::vtkURILoader()
  : Impl{ new vtkInternals{} }
{
}

//------------------------------------------------------------------------------
vtkURILoader::~vtkURILoader() = default;

//------------------------------------------------------------------------------
bool vtkURILoader::SetBaseURI(const std::string& uri)
{
  if (uri.empty()) // remove base uri
  {
    return this->SetBaseURI(nullptr);
  }

  auto parsed = vtkURI::Parse(uri);
  if (!parsed)
  {
    return false;
  }

  return this->SetBaseURI(parsed);
}

//------------------------------------------------------------------------------
bool vtkURILoader::SetBaseURI(vtkURI* uri)
{
  if (uri && !uri->IsAbsolute())
  {
    vtkErrorMacro("Base URI must be absolute or null");
    return false;
  }

  this->Impl->BaseURI = uri;
  return true;
}

//------------------------------------------------------------------------------
bool vtkURILoader::SetBaseFileName(const std::string& filepath)
{
  if (!vtksys::SystemTools::FileExists(filepath))
  {
    vtkErrorMacro("Can not find \"" << filepath << "\"");
    return false;
  }

  auto fullPath = vtksys::SystemTools::CollapseFullPath(filepath);
  if (fullPath.front() != '/') // add / for windows path
  {
    fullPath.insert(fullPath.begin(), '/');
  }

  this->Impl->BaseURI = vtkURI::Make("file", "", vtkURI::PercentEncode(fullPath));

  return this->Impl->BaseURI;
}

//------------------------------------------------------------------------------
bool vtkURILoader::SetBaseDirectory(const std::string& dirpath)
{
  if (!vtksys::SystemTools::FileExists(dirpath))
  {
    vtkErrorMacro("Can not find \"" << dirpath << "\"");
    return false;
  }

  if (!vtksys::SystemTools::FileIsDirectory(dirpath))
  {
    vtkErrorMacro("\"" << dirpath << "\" is not a directory. Use SetBaseFileName.");
    return false;
  }

  // add /. because relative URI truncate the last path component
  auto fullPath = vtksys::SystemTools::CollapseFullPath(dirpath) + "/.";
  if (fullPath.front() != '/') // add / for windows path
  {
    fullPath.insert(fullPath.begin(), '/');
  }

  this->Impl->BaseURI = vtkURI::Make("file", "", vtkURI::PercentEncode(fullPath));

  return this->Impl->BaseURI;
}

//------------------------------------------------------------------------------
vtkURI* vtkURILoader::GetBaseURI() const
{
  return this->Impl->BaseURI;
}

//------------------------------------------------------------------------------
vtkSmartPointer<vtkURI> vtkURILoader::Resolve(const vtkURI* uri)
{
  return vtkURI::Resolve(this->Impl->BaseURI, uri);
}

//------------------------------------------------------------------------------
vtkSmartPointer<vtkResourceStream> vtkURILoader::Load(const char* uri, std::size_t size)
{
  auto parsed = vtkURI::Parse(uri, size);
  if (!parsed)
  {
    return nullptr;
  }

  return this->Load(parsed);
}

//------------------------------------------------------------------------------
vtkSmartPointer<vtkResourceStream> vtkURILoader::Load(const vtkURI* uri)
{
  if (!uri->IsReference())
  {
    vtkErrorMacro("Given URI must be a valid URI reference.");
    return nullptr;
  }

  auto resolved = vtkURI::Resolve(this->Impl->BaseURI, uri);
  if (!resolved)
  {
    return nullptr;
  }

  return this->DoLoad(*resolved);
}

//------------------------------------------------------------------------------
vtkSmartPointer<vtkResourceStream> vtkURILoader::LoadResolved(const vtkURI* uri)
{
  if (!uri)
  {
    vtkErrorMacro("uri must not be null");
    return nullptr;
  }

  if (!uri->IsFull())
  {
    vtkErrorMacro("uri must be complete to be loaded");
    return nullptr;
  }

  return this->DoLoad(*uri);
}

//------------------------------------------------------------------------------
vtkSmartPointer<vtkResourceStream> vtkURILoader::DoLoad(const vtkURI& uri)
{
  // Case insensitive
  const auto scheme = vtksys::SystemTools::LowerCase(uri.GetScheme().GetValue());
  if (scheme == "file")
  {
    return this->LoadFile(uri);
  }
  else if (scheme == "data")
  {
    return this->LoadData(uri);
  }

  vtkErrorMacro("Unknown URI scheme for \"" << uri.ToString() << "\"");
  return nullptr;
}

//------------------------------------------------------------------------------
vtkSmartPointer<vtkResourceStream> vtkURILoader::LoadFile(const vtkURI& uri)
{
  // Decode
  auto decodedPath = vtkURI::PercentDecode(uri.GetPath().GetValue());

  auto cpath = decodedPath.c_str();
#if defined(_WIN32)
  if (*cpath == '/') // discard first / on Windows
  {
    ++cpath;
  }
#endif

  auto stream = vtkSmartPointer<vtkFileResourceStream>::New();
  if (!stream->Open(cpath))
  {
    vtkErrorMacro("Failed to open file \"" << cpath << "\"");
    return nullptr;
  }

  return stream;
}

namespace
{

// https://datatracker.ietf.org/doc/html/rfc2397#section-3
// Data URI "header" info
struct DataURIInfo
{
  std::string Type{};  // <mediatype>
  bool base64{};       // true if ;base64 has been specified at the end of parameters
  const char* Begin{}; // pointer to the first character of the data stream (raw or base64)
  const char* End{};   // pointer one past the last character of the data stream
};

// Helper to extract data URI header
DataURIInfo ExtractDataURI(const char* begin, const char* end)
{
  DataURIInfo output{};

  if (begin == end)
  {
    vtkErrorWithObjectMacro(nullptr, "Empty data URI");
    return output;
  }

  const auto typeEnd = std::find_if(begin, end, [](char c) { return c == ';' || c == ','; });

  if (typeEnd == end)
  {
    vtkErrorWithObjectMacro(nullptr, "No ',' in data URI");
    return output;
  }

  if (begin != typeEnd) // type specified
  {
    auto type = std::string{ begin, typeEnd };
    begin = typeEnd;

    output.Type = std::move(type);
  }
  else
  {
    output.Type = "text/plain;charset=US-ASCII";
  }

  while (*begin == ';')
  {
    ++begin; // discard ;
    const auto paramEnd = std::find_if(begin, end, [](char c) { return c == ';' || c == ','; });

    if (paramEnd == end)
    {
      vtkErrorWithObjectMacro(nullptr, "Truncated data URI header");
      return output;
    }

    if (*paramEnd == ',')
    {
      auto param = std::string{ begin, paramEnd };
      if (param == "base64")
      {
        output.base64 = true;
      }

      // Parameters aren't stored since unused, but if needed its where it should be done
      begin = paramEnd;
      break;
    }

    // Parameters aren't stored since unused, but if needed its where it should be done
    begin = paramEnd;
  }

  if (*begin != ',')
  {
    vtkErrorWithObjectMacro(nullptr, "Incomplete data URI, missing ','");
    return output;
  }

  output.Begin = begin + 1;
  output.End = end;

  return output;
}

}

//------------------------------------------------------------------------------
vtkSmartPointer<vtkResourceStream> vtkURILoader::LoadData(const vtkURI& uri)
{
  const auto& path = uri.GetPath().GetValue();
  const auto info = ExtractDataURI(path.data(), path.data() + path.size());

  if (info.base64)
  {
    const auto size = static_cast<std::size_t>(std::distance(info.Begin, info.End));
    if (size % 4 != 0)
    {
      vtkErrorMacro("Truncated base64 data. " << size << " is not a multiple of 4.");
      return nullptr;
    }

    std::vector<unsigned char> data;
    data.resize(size / 4 * 3); // 4 base64 value -> 3 bytes
    const auto decoded = vtksysBase64_Decode(
      reinterpret_cast<const unsigned char*>(info.Begin), data.size(), data.data(), size);
    data.resize(decoded);

    auto stream = vtkSmartPointer<vtkMemoryResourceStream>::New();
    stream->SetBuffer(std::move(data));

    return stream;
  }

  // raw data: convert %xx in string if any
  auto data = vtkURI::PercentDecode(info.Begin, std::distance(info.Begin, info.End));

  auto stream = vtkSmartPointer<vtkMemoryResourceStream>::New();
  stream->SetBuffer(std::move(data));

  return stream;
}

//------------------------------------------------------------------------------
void vtkURILoader::PrintSelf(ostream& os, vtkIndent indent)
{
  os << indent << "Base URI: " << (this->HasBaseURI() ? this->GetBaseURI()->ToString() : "None")
     << std::endl;
  Superclass::PrintSelf(os, indent.GetNextIndent());
}
VTK_ABI_NAMESPACE_END