aboutsummaryrefslogtreecommitdiff
path: root/libbutl/openssl.cxx
blob: 9159036581b362b987ca9f2c9049562bcb49cc81 (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
// file      : libbutl/openssl.cxx -*- C++ -*-
// copyright : Copyright (c) 2014-2019 Code Synthesis Ltd
// license   : MIT; see accompanying LICENSE file

#ifndef __cpp_modules_ts
#include <libbutl/openssl.mxx>
#endif

#include <cassert>

#ifndef __cpp_lib_modules_ts
#include <string>

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

// Other includes.

#ifdef __cpp_modules_ts
module butl.openssl;

// Only imports additional to interface.
#ifdef __clang__
#ifdef __cpp_lib_modules_ts
import std.core;
#endif
import butl.path;
import butl.process;
import butl.fdstream;
import butl.small_vector;
#endif

#endif

using namespace std;

namespace butl
{
  process::pipe openssl::
  map_in (nullfd_t, io_data& d)
  {
    d.pipe.in = fdnull (); // /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 = fdnull (); // /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 = fdnull ();
    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 = fdnull (); // /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;
  }
}