bf_null.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include <errno.h>
  11. #include "bio_local.h"
  12. #include "internal/cryptlib.h"
  13. /*
  14. * BIO_put and BIO_get both add to the digest, BIO_gets returns the digest
  15. */
  16. static int nullf_write(BIO *h, const char *buf, int num);
  17. static int nullf_read(BIO *h, char *buf, int size);
  18. static int nullf_puts(BIO *h, const char *str);
  19. static int nullf_gets(BIO *h, char *str, int size);
  20. static long nullf_ctrl(BIO *h, int cmd, long arg1, void *arg2);
  21. static long nullf_callback_ctrl(BIO *h, int cmd, BIO_info_cb *fp);
  22. static const BIO_METHOD methods_nullf = {
  23. BIO_TYPE_NULL_FILTER,
  24. "NULL filter",
  25. bwrite_conv,
  26. nullf_write,
  27. bread_conv,
  28. nullf_read,
  29. nullf_puts,
  30. nullf_gets,
  31. nullf_ctrl,
  32. NULL,
  33. NULL,
  34. nullf_callback_ctrl,
  35. };
  36. const BIO_METHOD *BIO_f_null(void)
  37. {
  38. return &methods_nullf;
  39. }
  40. static int nullf_read(BIO *b, char *out, int outl)
  41. {
  42. int ret = 0;
  43. if (out == NULL)
  44. return 0;
  45. if (b->next_bio == NULL)
  46. return 0;
  47. ret = BIO_read(b->next_bio, out, outl);
  48. BIO_clear_retry_flags(b);
  49. BIO_copy_next_retry(b);
  50. return ret;
  51. }
  52. static int nullf_write(BIO *b, const char *in, int inl)
  53. {
  54. int ret = 0;
  55. if ((in == NULL) || (inl <= 0))
  56. return 0;
  57. if (b->next_bio == NULL)
  58. return 0;
  59. ret = BIO_write(b->next_bio, in, inl);
  60. BIO_clear_retry_flags(b);
  61. BIO_copy_next_retry(b);
  62. return ret;
  63. }
  64. static long nullf_ctrl(BIO *b, int cmd, long num, void *ptr)
  65. {
  66. long ret;
  67. if (b->next_bio == NULL)
  68. return 0;
  69. switch (cmd) {
  70. case BIO_C_DO_STATE_MACHINE:
  71. BIO_clear_retry_flags(b);
  72. ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
  73. BIO_copy_next_retry(b);
  74. break;
  75. case BIO_CTRL_DUP:
  76. ret = 0L;
  77. break;
  78. default:
  79. ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
  80. }
  81. return ret;
  82. }
  83. static long nullf_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
  84. {
  85. if (b->next_bio == NULL)
  86. return 0;
  87. return BIO_callback_ctrl(b->next_bio, cmd, fp);
  88. }
  89. static int nullf_gets(BIO *bp, char *buf, int size)
  90. {
  91. if (bp->next_bio == NULL)
  92. return 0;
  93. return BIO_gets(bp->next_bio, buf, size);
  94. }
  95. static int nullf_puts(BIO *bp, const char *str)
  96. {
  97. if (bp->next_bio == NULL)
  98. return 0;
  99. return BIO_puts(bp->next_bio, str);
  100. }