DummyPRFileDesc.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /**
  2. * @file DummyPRFileDesc.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <limits.h>
  32. #include <prerror.h>
  33. #include <prmem.h>
  34. #include <misc/debug.h>
  35. #include <misc/offset.h>
  36. #include <nspr_support/DummyPRFileDesc.h>
  37. #ifndef NDEBUG
  38. int dummyprfiledesc_initialized = 0;
  39. #endif
  40. PRDescIdentity dummyprfiledesc_identity;
  41. static PRStatus method_close (PRFileDesc *fd)
  42. {
  43. return PR_SUCCESS;
  44. }
  45. static PRStatus method_getpeername (PRFileDesc *fd, PRNetAddr *addr)
  46. {
  47. PR_SetError(PR_UNKNOWN_ERROR, 0);
  48. return PR_FAILURE;
  49. }
  50. static PRIntn _PR_InvalidIntn (void)
  51. {
  52. ASSERT(0)
  53. PR_SetError(PR_INVALID_METHOD_ERROR, 0);
  54. return -1;
  55. }
  56. static PRInt16 _PR_InvalidInt16 (void)
  57. {
  58. ASSERT(0)
  59. PR_SetError(PR_INVALID_METHOD_ERROR, 0);
  60. return -1;
  61. }
  62. static PRInt32 _PR_InvalidInt32 (void)
  63. {
  64. ASSERT(0)
  65. PR_SetError(PR_INVALID_METHOD_ERROR, 0);
  66. return -1;
  67. }
  68. static PRInt64 _PR_InvalidInt64 (void)
  69. {
  70. ASSERT(0)
  71. PR_SetError(PR_INVALID_METHOD_ERROR, 0);
  72. return -1;
  73. }
  74. static PROffset32 _PR_InvalidOffset32 (void)
  75. {
  76. ASSERT(0)
  77. PR_SetError(PR_INVALID_METHOD_ERROR, 0);
  78. return -1;
  79. }
  80. static PROffset64 _PR_InvalidOffset64 (void)
  81. {
  82. ASSERT(0)
  83. PR_SetError(PR_INVALID_METHOD_ERROR, 0);
  84. return -1;
  85. }
  86. static PRStatus _PR_InvalidStatus (void)
  87. {
  88. ASSERT(0)
  89. PR_SetError(PR_INVALID_METHOD_ERROR, 0);
  90. return PR_FAILURE;
  91. }
  92. static PRFileDesc *_PR_InvalidDesc (void)
  93. {
  94. ASSERT(0)
  95. PR_SetError(PR_INVALID_METHOD_ERROR, 0);
  96. return NULL;
  97. }
  98. static PRIOMethods methods = {
  99. (PRDescType)0,
  100. method_close,
  101. (PRReadFN)_PR_InvalidInt32,
  102. (PRWriteFN)_PR_InvalidInt32,
  103. (PRAvailableFN)_PR_InvalidInt32,
  104. (PRAvailable64FN)_PR_InvalidInt64,
  105. (PRFsyncFN)_PR_InvalidStatus,
  106. (PRSeekFN)_PR_InvalidOffset32,
  107. (PRSeek64FN)_PR_InvalidOffset64,
  108. (PRFileInfoFN)_PR_InvalidStatus,
  109. (PRFileInfo64FN)_PR_InvalidStatus,
  110. (PRWritevFN)_PR_InvalidInt32,
  111. (PRConnectFN)_PR_InvalidStatus,
  112. (PRAcceptFN)_PR_InvalidDesc,
  113. (PRBindFN)_PR_InvalidStatus,
  114. (PRListenFN)_PR_InvalidStatus,
  115. (PRShutdownFN)_PR_InvalidStatus,
  116. (PRRecvFN)_PR_InvalidInt32,
  117. (PRSendFN)_PR_InvalidInt32,
  118. (PRRecvfromFN)_PR_InvalidInt32,
  119. (PRSendtoFN)_PR_InvalidInt32,
  120. (PRPollFN)_PR_InvalidInt16,
  121. (PRAcceptreadFN)_PR_InvalidInt32,
  122. (PRTransmitfileFN)_PR_InvalidInt32,
  123. (PRGetsocknameFN)_PR_InvalidStatus,
  124. method_getpeername,
  125. (PRReservedFN)_PR_InvalidIntn,
  126. (PRReservedFN)_PR_InvalidIntn,
  127. (PRGetsocketoptionFN)_PR_InvalidStatus,
  128. (PRSetsocketoptionFN)_PR_InvalidStatus,
  129. (PRSendfileFN)_PR_InvalidInt32,
  130. (PRConnectcontinueFN)_PR_InvalidStatus,
  131. (PRReservedFN)_PR_InvalidIntn,
  132. (PRReservedFN)_PR_InvalidIntn,
  133. (PRReservedFN)_PR_InvalidIntn,
  134. (PRReservedFN)_PR_InvalidIntn
  135. };
  136. int DummyPRFileDesc_GlobalInit (void)
  137. {
  138. ASSERT(!dummyprfiledesc_initialized)
  139. if ((dummyprfiledesc_identity = PR_GetUniqueIdentity("DummyPRFileDesc")) == PR_INVALID_IO_LAYER) {
  140. return 0;
  141. }
  142. #ifndef NDEBUG
  143. dummyprfiledesc_initialized = 1;
  144. #endif
  145. return 1;
  146. }
  147. void DummyPRFileDesc_Create (PRFileDesc *prfd)
  148. {
  149. ASSERT(dummyprfiledesc_initialized)
  150. memset(prfd, 0, sizeof(*prfd));
  151. prfd->methods = &methods;
  152. prfd->secret = NULL;
  153. prfd->identity = dummyprfiledesc_identity;
  154. }