]> WPIA git - cassiopeia.git/blob - src/bios.h
4ea874c811fdbde5615b289dd12b9a64d1c14efa
[cassiopeia.git] / src / bios.h
1 #pragma once
2
3 #include <openssl/bio.h>
4
5 #define BIO_TYPE_CUSTOM 0xff
6
7 class OpensslBIO {
8 public:
9     static const int typeID = BIO_TYPE_CUSTOM;
10     virtual ~OpensslBIO();
11
12     virtual int write( const char* buf, int num ) = 0;
13     virtual int read( char* buf, int size ) = 0;
14     virtual int puts( const char* str );
15     virtual int gets( char* str, int size );
16     virtual long ctrl( int cmod, long arg1, void* arg2 ) = 0;
17 };
18
19 namespace BIOWrapper {
20
21     int write( BIO* b, const char* buf, int num );
22     int read( BIO* b, char* buf, int size );
23     int puts( BIO* b, const char* str );
24     int gets( BIO* b, char* str, int size );
25     long ctrl( BIO* b, int cmod, long arg1, void* arg2 );
26
27     template <typename T>
28     int bio_new( BIO* b ) {
29         b->init = 1;
30         b->num = 0;
31         b->ptr = new T();
32         return 1;
33     }
34
35     int free( BIO* b );
36
37 }
38
39 template <typename T>
40 BIO_METHOD* toBio() {
41     static BIO_METHOD new_method = {
42         T::typeID,
43         T::getName(),
44         BIOWrapper::write,
45         BIOWrapper::read,
46         BIOWrapper::puts,
47         BIOWrapper::gets,
48         BIOWrapper::ctrl,
49         BIOWrapper::bio_new<T>,
50         BIOWrapper::free,
51         NULL,
52     };
53
54     return &new_method;
55 }