aboutsummaryrefslogtreecommitdiff
path: root/libbutl/openssl.cxx
blob: f9df2e7b245e9b26ac0a8219107455e662516f1e (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
// file      : libbutl/openssl.cxx -*- C++ -*-
// license   : MIT; see accompanying LICENSE file

#include <libbutl/openssl.hxx>

#include <cassert>
#include <utility> // move()

using namespace std;

namespace butl
{
  process::pipe openssl::
  map_in (nullfd_t, io_data& d)
  {
    d.pipe.in = fdopen_null (); // /dev/null
    return pipe (d.pipe);
  }

  process::pipe openssl::
  map_in (const path& f, io_data& d)
  {
    pipe r;
    if (f.string () == "-")
    {
      // Note: no need for any options, openssl reads from stdin by default.
      //
      d.pipe = fdopen_pipe (fdopen_mode::binary);
      r = pipe (d.pipe);

      out.open (move (d.pipe.out));
    }
    else
    {
      d.options.push_back ("-in");
      d.options.push_back (f.string ().c_str ());
      d.pipe.in = fdopen_null (); // /dev/null
      r = pipe (d.pipe);
    }

    return r;
  }

  process::pipe openssl::
  map_in (fdstream_mode m, io_data& d)
  {
    assert (m == fdstream_mode::text || m == fdstream_mode::binary);

    // Note: no need for any options, openssl reads from stdin by default.
    //
    d.pipe = fdopen_pipe (m == fdstream_mode::binary
                          ? fdopen_mode::binary
                          : fdopen_mode::none);
    pipe r (d.pipe);

    out.open (move (d.pipe.out));
    return r;
  }

  process::pipe openssl::
  map_out (nullfd_t, io_data& d)
  {
    d.pipe.out = fdopen_null ();
    return pipe (d.pipe); // /dev/null
  }

  process::pipe openssl::
  map_out (const path& f, io_data& d)
  {
    pipe r;
    if (f.string () == "-")
    {
      // Note: no need for any options, openssl writes to stdout by default.
      //
      d.pipe = fdopen_pipe (fdopen_mode::binary);
      r = pipe (d.pipe);

      in.open (move (d.pipe.in), fdstream_mode::skip);
    }
    else
    {
      d.options.push_back ("-out");
      d.options.push_back (f.string ().c_str ());
      d.pipe.out = fdopen_null (); // /dev/null
      r = pipe (d.pipe);
    }

    return r;
  }

  process::pipe openssl::
  map_out (fdstream_mode m, io_data& d)
  {
    assert (m == fdstream_mode::text || m == fdstream_mode::binary);

    // Note: no need for any options, openssl writes to stdout by default.
    //
    d.pipe = fdopen_pipe (m == fdstream_mode::binary
                          ? fdopen_mode::binary
                          : fdopen_mode::none);
    pipe r (d.pipe);

    in.open (move (d.pipe.in), fdstream_mode::skip);
    return r;
  }
}