2
0

bf_null.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Copyright 1995-2018 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_lcl.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. /* TODO: Convert to new style write function */
  26. bwrite_conv,
  27. nullf_write,
  28. /* TODO: Convert to new style read function */
  29. bread_conv,
  30. nullf_read,
  31. nullf_puts,
  32. nullf_gets,
  33. nullf_ctrl,
  34. NULL,
  35. NULL,
  36. nullf_callback_ctrl,
  37. };
  38. const BIO_METHOD *BIO_f_null(void)
  39. {
  40. return &methods_nullf;
  41. }
  42. static int nullf_read(BIO *b, char *out, int outl)
  43. {
  44. int ret = 0;
  45. if (out == NULL)
  46. return 0;
  47. if (b->next_bio == NULL)
  48. return 0;
  49. ret = BIO_read(b->next_bio, out, outl);
  50. BIO_clear_retry_flags(b);
  51. BIO_copy_next_retry(b);
  52. return ret;
  53. }
  54. static int nullf_write(BIO *b, const char *in, int inl)
  55. {
  56. int ret = 0;
  57. if ((in == NULL) || (inl <= 0))
  58. return 0;
  59. if (b->next_bio == NULL)
  60. return 0;
  61. ret = BIO_write(b->next_bio, in, inl);
  62. BIO_clear_retry_flags(b);
  63. BIO_copy_next_retry(b);
  64. return ret;
  65. }
  66. static long nullf_ctrl(BIO *b, int cmd, long num, void *ptr)
  67. {
  68. long ret;
  69. if (b->next_bio == NULL)
  70. return 0;
  71. switch (cmd) {
  72. case BIO_C_DO_STATE_MACHINE:
  73. BIO_clear_retry_flags(b);
  74. ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
  75. BIO_copy_next_retry(b);
  76. break;
  77. case BIO_CTRL_DUP:
  78. ret = 0L;
  79. break;
  80. default:
  81. ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
  82. }
  83. return ret;
  84. }
  85. static long nullf_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
  86. {
  87. long ret = 1;
  88. if (b->next_bio == NULL)
  89. return 0;
  90. switch (cmd) {
  91. default:
  92. ret = BIO_callback_ctrl(b->next_bio, cmd, fp);
  93. break;
  94. }
  95. return ret;
  96. }
  97. static int nullf_gets(BIO *bp, char *buf, int size)
  98. {
  99. if (bp->next_bio == NULL)
  100. return 0;
  101. return BIO_gets(bp->next_bio, buf, size);
  102. }
  103. static int nullf_puts(BIO *bp, const char *str)
  104. {
  105. if (bp->next_bio == NULL)
  106. return 0;
  107. return BIO_puts(bp->next_bio, str);
  108. }