io.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #include "io.h"
  2. #include <util/generic/singleton.h>
  3. #include <util/generic/yexception.h>
  4. #include <util/system/compiler.h>
  5. #include <util/system/yassert.h>
  6. namespace {
  7. using NOpenSSL::TAbstractIO;
  8. TAbstractIO* IO(BIO* bio) noexcept {
  9. void* ptr = BIO_get_data(bio);
  10. Y_ABORT_UNLESS(ptr);
  11. return static_cast<TAbstractIO*>(ptr);
  12. }
  13. template<class T, class Callable, class... Args>
  14. T ExceptionBoundary(BIO* bio, Callable&& f, T err, Args&&... args) noexcept {
  15. try {
  16. return (IO(bio)->*f)(args...);
  17. } catch (...) {
  18. return err;
  19. }
  20. }
  21. int Write(BIO* bio, const char* data, int dlen) noexcept {
  22. return ExceptionBoundary(bio, &TAbstractIO::WriteOld, -1, data, dlen);
  23. }
  24. int Read(BIO* bio, char* data, int dlen) noexcept {
  25. return ExceptionBoundary(bio, &TAbstractIO::ReadOld, -1, data, dlen);
  26. }
  27. int Puts(BIO* bio, const char* buf) noexcept {
  28. return ExceptionBoundary(bio, &TAbstractIO::Puts, -1, buf);
  29. }
  30. int Gets(BIO* bio, char* buf, int size) noexcept {
  31. return ExceptionBoundary(bio, &TAbstractIO::Gets, -1, buf, size);
  32. }
  33. long Ctrl(BIO* bio, int cmd, long larg, void* parg) noexcept {
  34. return ExceptionBoundary(bio, &TAbstractIO::Ctrl, -1, cmd, larg, parg);
  35. }
  36. int Create(BIO* bio) noexcept {
  37. BIO_set_data(bio, nullptr);
  38. BIO_set_init(bio, 1);
  39. return 1;
  40. }
  41. int Destroy(BIO* bio) noexcept {
  42. BIO_set_data(bio, nullptr);
  43. BIO_set_init(bio, 0);
  44. return 1;
  45. }
  46. NOpenSSL::TBioMethod* Method() {
  47. return SingletonWithPriority<NOpenSSL::TBioMethod, 32768>(
  48. BIO_get_new_index() | BIO_TYPE_SOURCE_SINK,
  49. "AbstractIO",
  50. Write,
  51. Read,
  52. Puts,
  53. Gets,
  54. Ctrl,
  55. Create,
  56. Destroy,
  57. nullptr
  58. );
  59. }
  60. }
  61. namespace NOpenSSL {
  62. TAbstractIO::TAbstractIO()
  63. : Bio(BIO_new(*Method())) {
  64. if (Y_UNLIKELY(!Bio)) {
  65. throw std::bad_alloc();
  66. }
  67. BIO_set_data(Bio, this);
  68. }
  69. TAbstractIO::~TAbstractIO() {
  70. BIO_free(Bio);
  71. }
  72. int TAbstractIO::WriteOld(const char* data, int dlen) {
  73. size_t written = 0;
  74. int ret = Write(data, dlen, &written);
  75. if (ret <= 0) {
  76. return ret;
  77. }
  78. return written;
  79. }
  80. int TAbstractIO::ReadOld(char* data, int dlen) {
  81. size_t readbytes = 0;
  82. int ret = Read(data, dlen, &readbytes);
  83. if (ret <= 0) {
  84. return ret;
  85. }
  86. return readbytes;
  87. }
  88. long TAbstractIO::Ctrl(int cmd, long larg, void* parg) {
  89. Y_UNUSED(larg);
  90. Y_UNUSED(parg);
  91. if (cmd == BIO_CTRL_FLUSH) {
  92. Flush();
  93. return 1;
  94. }
  95. return 0;
  96. }
  97. } // namespace NOpenSSL