Qt Cryptographic Architecture
qpipe.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2003-2007 Justin Karneges <justin@affinix.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301 USA
18  *
19  */
20 
31 #ifndef QPIPE_H
32 #define QPIPE_H
33 
34 #ifndef DOXYGEN_SHOULD_SKIP_THIS
35 
36 #ifndef QPIPE_NO_SECURE
37 # define QPIPE_SECURE
38 #endif
39 
40 #ifdef QPIPE_SECURE
41 # include "QtCrypto"
42 #else
43 # define QCA_EXPORT
44 #endif
45 
46 // defs adapted qprocess_p.h
47 #ifdef Q_OS_WIN
48 #include <windows.h>
49 typedef HANDLE Q_PIPE_ID;
50 #define INVALID_Q_PIPE_ID INVALID_HANDLE_VALUE
51 #else
52 typedef int Q_PIPE_ID;
53 #define INVALID_Q_PIPE_ID -1
54 #endif
55 
56 #endif
57 
58 // Note: for Windows console, I/O must be in UTF-8. Reads are guaranteed to
59 // to completely decode (no partial characters). Likewise, writes must
60 // not contain partial characters.
61 
62 namespace QCA {
63 
64 
75 class QCA_EXPORT QPipeDevice : public QObject
76 {
77  Q_OBJECT
78 public:
82  enum Type
83  {
84  Read,
85  Write
86  };
87 
93  QPipeDevice(QObject *parent = nullptr);
94  ~QPipeDevice() override;
95 
99  Type type() const;
100 
104  bool isValid() const;
105 
115  Q_PIPE_ID id() const;
116 
124  int idAsInt() const;
125 
132  void take(Q_PIPE_ID id, Type t);
133 
137  void enable();
138 
142  void close();
143 
147  void release();
148 
156  bool setInheritable(bool enabled);
157 
161  int bytesAvailable() const;
162 
171  int read(char *data, int maxsize);
172 
183  int write(const char *data, int size);
184 
193  int writeResult(int *written) const;
194 
195 Q_SIGNALS:
199  void notify();
200 
201 private:
202  Q_DISABLE_COPY(QPipeDevice)
203 
204  class Private;
205  friend class Private;
206  Private *d;
207 };
208 
218 class QCA_EXPORT QPipeEnd : public QObject
219 {
220  Q_OBJECT
221 public:
222 
226  enum Error
227  {
229  ErrorBroken
230  };
231 
237  QPipeEnd(QObject *parent = nullptr);
238 
239  ~QPipeEnd() override;
240 
244  void reset();
245 
250 
257  bool isValid() const;
258 
262  Q_PIPE_ID id() const;
263 
267  int idAsInt() const;
268 
275  void take(Q_PIPE_ID id, QPipeDevice::Type t);
276 
277 #ifdef QPIPE_SECURE
278 
286  void setSecurityEnabled(bool secure);
287 #endif
288 
295  void enable();
296 
302  void close();
303 
310  void release();
311 
320  bool setInheritable(bool enabled);
321 
325  void finalize();
326 
331 
340  int bytesAvailable() const;
341 
350  int bytesToWrite() const;
351 
362  QByteArray read(int bytes = -1);
363 
373  void write(const QByteArray &a);
374 
375 #ifdef QPIPE_SECURE
376 
386  SecureArray readSecure(int bytes = -1);
387 
397  void writeSecure(const SecureArray &a);
398 #endif
399 
406  QByteArray takeBytesToWrite();
407 
408 #ifdef QPIPE_SECURE
409 
416 #endif
417 
418 Q_SIGNALS:
425  void readyRead();
426 
433  void bytesWritten(int bytes);
434 
446  void closed();
447 
455 
456 private:
457  Q_DISABLE_COPY(QPipeEnd)
458 
459  class Private;
460  friend class Private;
461  Private *d;
462 };
463 
480 class QCA_EXPORT QPipe
481 {
482 public:
490  QPipe(QObject *parent = nullptr);
491 
492  ~QPipe();
493 
500  void reset();
501 
502 #ifdef QPIPE_SECURE
503 
508  bool create(bool secure = false);
509 #else
510 
513  bool create();
514 #endif
515 
519  QPipeEnd & readEnd() { return i; }
520 
524  QPipeEnd & writeEnd() { return o; }
525 
526 private:
527  Q_DISABLE_COPY(QPipe)
528 
529  QPipeEnd i, o;
530 };
531 
532 }
533 
534 #endif
QObject
QCA::QPipeEnd::setSecurityEnabled
void setSecurityEnabled(bool secure)
Sets whether the pipe uses secure memory for read/write.
QCA::QPipeDevice
Unbuffered direct pipe.
Definition: qpipe.h:76
QCA::QPipeDevice::close
void close()
Close the pipe end.
QCA::QPipeDevice::setInheritable
bool setInheritable(bool enabled)
Set the pipe end to be inheritable.
QCA::QPipeDevice::Type
Type
The type of device.
Definition: qpipe.h:83
QCA::QPipeEnd::take
void take(Q_PIPE_ID id, QPipeDevice::Type t)
Take over an existing pipe handle.
QCA::QPipeEnd::id
Q_PIPE_ID id() const
Pipe identification.
QCA::QPipe
A FIFO buffer (named pipe) abstraction.
Definition: qpipe.h:481
QCA::QPipeEnd::type
QPipeDevice::Type type() const
The type of pipe end (either read or write)
QCA::QPipeEnd::reset
void reset()
Reset the pipe end to an inactive state.
QCA::QPipeDevice::write
int write(const char *data, int size)
Write to the pipe end.
QCA::QPipe::reset
void reset()
Reset the pipe.
QCA::QPipeEnd::error
void error(QCA::QPipeEnd::Error e)
Emitted when the pipe encounters an error trying to read or write, or if the other end of the pipe ha...
QCA::QPipeDevice::writeResult
int writeResult(int *written) const
The result of a write operation.
QCA::QPipeDevice::isValid
bool isValid() const
Test whether this object corresponds to a valid pipe.
QCA
QCA - the Qt Cryptographic Architecture.
Definition: qca_basic.h:41
QCA::QPipeDevice::id
Q_PIPE_ID id() const
The low level identification for this pipe.
QCA::QPipeDevice::enable
void enable()
Enable the pipe for reading or writing (depending on Type)
QCA::QPipeEnd::takeBytesToWriteSecure
SecureArray takeBytesToWriteSecure()
Returns any unsent bytes queued for writing.
QCA::QPipeEnd::release
void release()
Let go of the active pipe handle, but don't close it.
QCA::QPipeEnd::readSecure
SecureArray readSecure(int bytes=-1)
Read bytes from the pipe.
QCA::QPipeDevice::release
void release()
Release the pipe end, but do not close it.
QCA::QPipeEnd::ErrorEOF
@ ErrorEOF
End of file error.
Definition: qpipe.h:228
QCA::QPipe::readEnd
QPipeEnd & readEnd()
The read end of the pipe.
Definition: qpipe.h:519
QCA::QPipeEnd::bytesWritten
void bytesWritten(int bytes)
Emitted when bytes have been written to the write end of the pipe.
QCA::QPipeEnd::finalizeAndRelease
void finalizeAndRelease()
Clear the contents of the pipe, and release the pipe.
QCA::QPipeEnd
A buffered higher-level pipe end.
Definition: qpipe.h:219
QCA::QPipeDevice::take
void take(Q_PIPE_ID id, Type t)
Take over an existing pipe id, closing the old pipe if any.
QCA::QPipeEnd::bytesToWrite
int bytesToWrite() const
Returns the number of bytes pending to write.
QCA::QPipeDevice::idAsInt
int idAsInt() const
The low level identification for this pipe, returned as an integer.
QCA::QPipeDevice::QPipeDevice
QPipeDevice(QObject *parent=nullptr)
Standard constructor.
QCA::QPipeEnd::idAsInt
int idAsInt() const
Pipe identification.
QCA::QPipeEnd::close
void close()
Close the end of the pipe.
QCA::SecureArray
Secure array of bytes.
Definition: qca_tools.h:317
QCA::QPipeEnd::enable
void enable()
Enable the endpoint for the pipe.
QCA::QPipeEnd::read
QByteArray read(int bytes=-1)
Read bytes from the pipe.
QCA::QPipeDevice::type
Type type() const
The Type of the pipe device (that is, read or write)
QCA::QPipeDevice::Read
@ Read
The pipe end can be read from.
Definition: qpipe.h:84
QCA::QPipeEnd::bytesAvailable
int bytesAvailable() const
Determine how many bytes are available to be read.
QCA::QPipeEnd::closed
void closed()
Emitted when this end of the pipe is closed as a result of calling close()
QCA::QPipeDevice::notify
void notify()
Emitted when the pipe end can be read from or written to (depending on its Type).
QCA::QPipe::QPipe
QPipe(QObject *parent=nullptr)
Standard constructor.
QCA::QPipeEnd::write
void write(const QByteArray &a)
Write bytes to the pipe.
QCA::QPipe::writeEnd
QPipeEnd & writeEnd()
The write end of the pipe.
Definition: qpipe.h:524
QCA::QPipeDevice::read
int read(char *data, int maxsize)
Read from the pipe end.
QCA::QPipeEnd::QPipeEnd
QPipeEnd(QObject *parent=nullptr)
Standard constructor.
QCA::QPipeEnd::finalize
void finalize()
Clear the contents of the pipe, and invalidate the pipe.
QCA::QPipeEnd::takeBytesToWrite
QByteArray takeBytesToWrite()
Returns any unsent bytes queued for writing.
QCA::QPipeEnd::Error
Error
The type of error.
Definition: qpipe.h:227
QCA::QPipeEnd::readyRead
void readyRead()
Emitted when there are bytes available to be read from the read end of the pipe.
QCA::QPipeDevice::bytesAvailable
int bytesAvailable() const
Obtain the number of bytes available to be read.
QCA::QPipeEnd::writeSecure
void writeSecure(const SecureArray &a)
Write bytes to the pipe.
QCA::QPipeEnd::isValid
bool isValid() const
Determine whether the pipe end is valid.
QCA::QPipe::create
bool create(bool secure=false)
Create the pipe.
QCA::QPipeEnd::setInheritable
bool setInheritable(bool enabled)
Sets whether the pipe should be inheritable to child processes.