applink.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * Copyright 2004-2023 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. #define APPLINK_STDIN 1
  10. #define APPLINK_STDOUT 2
  11. #define APPLINK_STDERR 3
  12. #define APPLINK_FPRINTF 4
  13. #define APPLINK_FGETS 5
  14. #define APPLINK_FREAD 6
  15. #define APPLINK_FWRITE 7
  16. #define APPLINK_FSETMOD 8
  17. #define APPLINK_FEOF 9
  18. #define APPLINK_FCLOSE 10 /* should not be used */
  19. #define APPLINK_FOPEN 11 /* solely for completeness */
  20. #define APPLINK_FSEEK 12
  21. #define APPLINK_FTELL 13
  22. #define APPLINK_FFLUSH 14
  23. #define APPLINK_FERROR 15
  24. #define APPLINK_CLEARERR 16
  25. #define APPLINK_FILENO 17 /* to be used with below */
  26. #define APPLINK_OPEN 18 /* formally can't be used, as flags can vary */
  27. #define APPLINK_READ 19
  28. #define APPLINK_WRITE 20
  29. #define APPLINK_LSEEK 21
  30. #define APPLINK_CLOSE 22
  31. #define APPLINK_MAX 22 /* always same as last macro */
  32. #ifndef APPMACROS_ONLY
  33. /*
  34. * Normally, do not define APPLINK_NO_INCLUDES. Define it if you are using
  35. * symbol preprocessing and do not want the preprocessing to affect the
  36. * following included header files. You will need to put these
  37. * include lines somewhere in the file that is including applink.c.
  38. */
  39. # ifndef APPLINK_NO_INCLUDES
  40. # include <stdio.h>
  41. # include <io.h>
  42. # include <fcntl.h>
  43. # endif
  44. # ifdef __BORLANDC__
  45. /* _lseek in <io.h> is a function-like macro so we can't take its address */
  46. # undef _lseek
  47. # define _lseek lseek
  48. # endif
  49. static void *app_stdin(void)
  50. {
  51. return stdin;
  52. }
  53. static void *app_stdout(void)
  54. {
  55. return stdout;
  56. }
  57. static void *app_stderr(void)
  58. {
  59. return stderr;
  60. }
  61. static int app_feof(FILE *fp)
  62. {
  63. return feof(fp);
  64. }
  65. static int app_ferror(FILE *fp)
  66. {
  67. return ferror(fp);
  68. }
  69. static void app_clearerr(FILE *fp)
  70. {
  71. clearerr(fp);
  72. }
  73. static int app_fileno(FILE *fp)
  74. {
  75. return _fileno(fp);
  76. }
  77. static int app_fsetmod(FILE *fp, char mod)
  78. {
  79. return _setmode(_fileno(fp), mod == 'b' ? _O_BINARY : _O_TEXT);
  80. }
  81. #ifdef __cplusplus
  82. extern "C" {
  83. #endif
  84. __declspec(dllexport)
  85. void **
  86. # if defined(__BORLANDC__)
  87. /*
  88. * __stdcall appears to be the only way to get the name
  89. * decoration right with Borland C. Otherwise it works
  90. * purely incidentally, as we pass no parameters.
  91. */
  92. __stdcall
  93. # else
  94. __cdecl
  95. # endif
  96. OPENSSL_Applink(void)
  97. {
  98. static int once = 1;
  99. static void *OPENSSL_ApplinkTable[APPLINK_MAX + 1] =
  100. { (void *)APPLINK_MAX };
  101. if (once) {
  102. OPENSSL_ApplinkTable[APPLINK_STDIN] = app_stdin;
  103. OPENSSL_ApplinkTable[APPLINK_STDOUT] = app_stdout;
  104. OPENSSL_ApplinkTable[APPLINK_STDERR] = app_stderr;
  105. OPENSSL_ApplinkTable[APPLINK_FPRINTF] = fprintf;
  106. OPENSSL_ApplinkTable[APPLINK_FGETS] = fgets;
  107. OPENSSL_ApplinkTable[APPLINK_FREAD] = fread;
  108. OPENSSL_ApplinkTable[APPLINK_FWRITE] = fwrite;
  109. OPENSSL_ApplinkTable[APPLINK_FSETMOD] = app_fsetmod;
  110. OPENSSL_ApplinkTable[APPLINK_FEOF] = app_feof;
  111. OPENSSL_ApplinkTable[APPLINK_FCLOSE] = fclose;
  112. OPENSSL_ApplinkTable[APPLINK_FOPEN] = fopen;
  113. OPENSSL_ApplinkTable[APPLINK_FSEEK] = fseek;
  114. OPENSSL_ApplinkTable[APPLINK_FTELL] = ftell;
  115. OPENSSL_ApplinkTable[APPLINK_FFLUSH] = fflush;
  116. OPENSSL_ApplinkTable[APPLINK_FERROR] = app_ferror;
  117. OPENSSL_ApplinkTable[APPLINK_CLEARERR] = app_clearerr;
  118. OPENSSL_ApplinkTable[APPLINK_FILENO] = app_fileno;
  119. OPENSSL_ApplinkTable[APPLINK_OPEN] = _open;
  120. OPENSSL_ApplinkTable[APPLINK_READ] = _read;
  121. OPENSSL_ApplinkTable[APPLINK_WRITE] = _write;
  122. OPENSSL_ApplinkTable[APPLINK_LSEEK] = _lseek;
  123. OPENSSL_ApplinkTable[APPLINK_CLOSE] = _close;
  124. once = 0;
  125. }
  126. return OPENSSL_ApplinkTable;
  127. }
  128. #ifdef __cplusplus
  129. }
  130. #endif
  131. #endif