applink.c 3.4 KB

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