applink.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * Copyright 2004-2021 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. # include <stdio.h>
  34. # include <io.h>
  35. # include <fcntl.h>
  36. # ifdef __BORLANDC__
  37. /* _lseek in <io.h> is a function-like macro so we can't take its address */
  38. # undef _lseek
  39. # define _lseek lseek
  40. # endif
  41. static void *app_stdin(void)
  42. {
  43. return stdin;
  44. }
  45. static void *app_stdout(void)
  46. {
  47. return stdout;
  48. }
  49. static void *app_stderr(void)
  50. {
  51. return stderr;
  52. }
  53. static int app_feof(FILE *fp)
  54. {
  55. return feof(fp);
  56. }
  57. static int app_ferror(FILE *fp)
  58. {
  59. return ferror(fp);
  60. }
  61. static void app_clearerr(FILE *fp)
  62. {
  63. clearerr(fp);
  64. }
  65. static int app_fileno(FILE *fp)
  66. {
  67. return _fileno(fp);
  68. }
  69. static int app_fsetmod(FILE *fp, char mod)
  70. {
  71. return _setmode(_fileno(fp), mod == 'b' ? _O_BINARY : _O_TEXT);
  72. }
  73. #ifdef __cplusplus
  74. extern "C" {
  75. #endif
  76. __declspec(dllexport)
  77. void **
  78. # if defined(__BORLANDC__)
  79. /*
  80. * __stdcall appears to be the only way to get the name
  81. * decoration right with Borland C. Otherwise it works
  82. * purely incidentally, as we pass no parameters.
  83. */
  84. __stdcall
  85. # else
  86. __cdecl
  87. # endif
  88. OPENSSL_Applink(void)
  89. {
  90. static int once = 1;
  91. static void *OPENSSL_ApplinkTable[APPLINK_MAX + 1] =
  92. { (void *)APPLINK_MAX };
  93. if (once) {
  94. OPENSSL_ApplinkTable[APPLINK_STDIN] = app_stdin;
  95. OPENSSL_ApplinkTable[APPLINK_STDOUT] = app_stdout;
  96. OPENSSL_ApplinkTable[APPLINK_STDERR] = app_stderr;
  97. OPENSSL_ApplinkTable[APPLINK_FPRINTF] = fprintf;
  98. OPENSSL_ApplinkTable[APPLINK_FGETS] = fgets;
  99. OPENSSL_ApplinkTable[APPLINK_FREAD] = fread;
  100. OPENSSL_ApplinkTable[APPLINK_FWRITE] = fwrite;
  101. OPENSSL_ApplinkTable[APPLINK_FSETMOD] = app_fsetmod;
  102. OPENSSL_ApplinkTable[APPLINK_FEOF] = app_feof;
  103. OPENSSL_ApplinkTable[APPLINK_FCLOSE] = fclose;
  104. OPENSSL_ApplinkTable[APPLINK_FOPEN] = fopen;
  105. OPENSSL_ApplinkTable[APPLINK_FSEEK] = fseek;
  106. OPENSSL_ApplinkTable[APPLINK_FTELL] = ftell;
  107. OPENSSL_ApplinkTable[APPLINK_FFLUSH] = fflush;
  108. OPENSSL_ApplinkTable[APPLINK_FERROR] = app_ferror;
  109. OPENSSL_ApplinkTable[APPLINK_CLEARERR] = app_clearerr;
  110. OPENSSL_ApplinkTable[APPLINK_FILENO] = app_fileno;
  111. OPENSSL_ApplinkTable[APPLINK_OPEN] = _open;
  112. OPENSSL_ApplinkTable[APPLINK_READ] = _read;
  113. OPENSSL_ApplinkTable[APPLINK_WRITE] = _write;
  114. OPENSSL_ApplinkTable[APPLINK_LSEEK] = _lseek;
  115. OPENSSL_ApplinkTable[APPLINK_CLOSE] = _close;
  116. once = 0;
  117. }
  118. return OPENSSL_ApplinkTable;
  119. }
  120. #ifdef __cplusplus
  121. }
  122. #endif
  123. #endif