xref: /freebsd/crypto/openssl/ms/applink.c (revision e0c4386e7e71d93b0edc0c8fa156263fc4a8b0b6)
1*e0c4386eSCy Schubert /*
2*e0c4386eSCy Schubert  * Copyright 2004-2021 The OpenSSL Project Authors. All Rights Reserved.
3*e0c4386eSCy Schubert  *
4*e0c4386eSCy Schubert  * Licensed under the Apache License 2.0 (the "License").  You may not use
5*e0c4386eSCy Schubert  * this file except in compliance with the License.  You can obtain a copy
6*e0c4386eSCy Schubert  * in the file LICENSE in the source distribution or at
7*e0c4386eSCy Schubert  * https://www.openssl.org/source/license.html
8*e0c4386eSCy Schubert  */
9*e0c4386eSCy Schubert 
10*e0c4386eSCy Schubert #define APPLINK_STDIN   1
11*e0c4386eSCy Schubert #define APPLINK_STDOUT  2
12*e0c4386eSCy Schubert #define APPLINK_STDERR  3
13*e0c4386eSCy Schubert #define APPLINK_FPRINTF 4
14*e0c4386eSCy Schubert #define APPLINK_FGETS   5
15*e0c4386eSCy Schubert #define APPLINK_FREAD   6
16*e0c4386eSCy Schubert #define APPLINK_FWRITE  7
17*e0c4386eSCy Schubert #define APPLINK_FSETMOD 8
18*e0c4386eSCy Schubert #define APPLINK_FEOF    9
19*e0c4386eSCy Schubert #define APPLINK_FCLOSE  10      /* should not be used */
20*e0c4386eSCy Schubert 
21*e0c4386eSCy Schubert #define APPLINK_FOPEN   11      /* solely for completeness */
22*e0c4386eSCy Schubert #define APPLINK_FSEEK   12
23*e0c4386eSCy Schubert #define APPLINK_FTELL   13
24*e0c4386eSCy Schubert #define APPLINK_FFLUSH  14
25*e0c4386eSCy Schubert #define APPLINK_FERROR  15
26*e0c4386eSCy Schubert #define APPLINK_CLEARERR 16
27*e0c4386eSCy Schubert #define APPLINK_FILENO  17      /* to be used with below */
28*e0c4386eSCy Schubert 
29*e0c4386eSCy Schubert #define APPLINK_OPEN    18      /* formally can't be used, as flags can vary */
30*e0c4386eSCy Schubert #define APPLINK_READ    19
31*e0c4386eSCy Schubert #define APPLINK_WRITE   20
32*e0c4386eSCy Schubert #define APPLINK_LSEEK   21
33*e0c4386eSCy Schubert #define APPLINK_CLOSE   22
34*e0c4386eSCy Schubert #define APPLINK_MAX     22      /* always same as last macro */
35*e0c4386eSCy Schubert 
36*e0c4386eSCy Schubert #ifndef APPMACROS_ONLY
37*e0c4386eSCy Schubert # include <stdio.h>
38*e0c4386eSCy Schubert # include <io.h>
39*e0c4386eSCy Schubert # include <fcntl.h>
40*e0c4386eSCy Schubert 
41*e0c4386eSCy Schubert # ifdef __BORLANDC__
42*e0c4386eSCy Schubert    /* _lseek in <io.h> is a function-like macro so we can't take its address */
43*e0c4386eSCy Schubert #  undef _lseek
44*e0c4386eSCy Schubert #  define _lseek lseek
45*e0c4386eSCy Schubert # endif
46*e0c4386eSCy Schubert 
app_stdin(void)47*e0c4386eSCy Schubert static void *app_stdin(void)
48*e0c4386eSCy Schubert {
49*e0c4386eSCy Schubert     return stdin;
50*e0c4386eSCy Schubert }
51*e0c4386eSCy Schubert 
app_stdout(void)52*e0c4386eSCy Schubert static void *app_stdout(void)
53*e0c4386eSCy Schubert {
54*e0c4386eSCy Schubert     return stdout;
55*e0c4386eSCy Schubert }
56*e0c4386eSCy Schubert 
app_stderr(void)57*e0c4386eSCy Schubert static void *app_stderr(void)
58*e0c4386eSCy Schubert {
59*e0c4386eSCy Schubert     return stderr;
60*e0c4386eSCy Schubert }
61*e0c4386eSCy Schubert 
app_feof(FILE * fp)62*e0c4386eSCy Schubert static int app_feof(FILE *fp)
63*e0c4386eSCy Schubert {
64*e0c4386eSCy Schubert     return feof(fp);
65*e0c4386eSCy Schubert }
66*e0c4386eSCy Schubert 
app_ferror(FILE * fp)67*e0c4386eSCy Schubert static int app_ferror(FILE *fp)
68*e0c4386eSCy Schubert {
69*e0c4386eSCy Schubert     return ferror(fp);
70*e0c4386eSCy Schubert }
71*e0c4386eSCy Schubert 
app_clearerr(FILE * fp)72*e0c4386eSCy Schubert static void app_clearerr(FILE *fp)
73*e0c4386eSCy Schubert {
74*e0c4386eSCy Schubert     clearerr(fp);
75*e0c4386eSCy Schubert }
76*e0c4386eSCy Schubert 
app_fileno(FILE * fp)77*e0c4386eSCy Schubert static int app_fileno(FILE *fp)
78*e0c4386eSCy Schubert {
79*e0c4386eSCy Schubert     return _fileno(fp);
80*e0c4386eSCy Schubert }
81*e0c4386eSCy Schubert 
app_fsetmod(FILE * fp,char mod)82*e0c4386eSCy Schubert static int app_fsetmod(FILE *fp, char mod)
83*e0c4386eSCy Schubert {
84*e0c4386eSCy Schubert     return _setmode(_fileno(fp), mod == 'b' ? _O_BINARY : _O_TEXT);
85*e0c4386eSCy Schubert }
86*e0c4386eSCy Schubert 
87*e0c4386eSCy Schubert #ifdef __cplusplus
88*e0c4386eSCy Schubert extern "C" {
89*e0c4386eSCy Schubert #endif
90*e0c4386eSCy Schubert 
91*e0c4386eSCy Schubert __declspec(dllexport)
92*e0c4386eSCy Schubert void **
93*e0c4386eSCy Schubert # if defined(__BORLANDC__)
94*e0c4386eSCy Schubert /*
95*e0c4386eSCy Schubert  * __stdcall appears to be the only way to get the name
96*e0c4386eSCy Schubert  * decoration right with Borland C. Otherwise it works
97*e0c4386eSCy Schubert  * purely incidentally, as we pass no parameters.
98*e0c4386eSCy Schubert  */
99*e0c4386eSCy Schubert __stdcall
100*e0c4386eSCy Schubert # else
101*e0c4386eSCy Schubert __cdecl
102*e0c4386eSCy Schubert # endif
OPENSSL_Applink(void)103*e0c4386eSCy Schubert OPENSSL_Applink(void)
104*e0c4386eSCy Schubert {
105*e0c4386eSCy Schubert     static int once = 1;
106*e0c4386eSCy Schubert     static void *OPENSSL_ApplinkTable[APPLINK_MAX + 1] =
107*e0c4386eSCy Schubert         { (void *)APPLINK_MAX };
108*e0c4386eSCy Schubert 
109*e0c4386eSCy Schubert     if (once) {
110*e0c4386eSCy Schubert         OPENSSL_ApplinkTable[APPLINK_STDIN] = app_stdin;
111*e0c4386eSCy Schubert         OPENSSL_ApplinkTable[APPLINK_STDOUT] = app_stdout;
112*e0c4386eSCy Schubert         OPENSSL_ApplinkTable[APPLINK_STDERR] = app_stderr;
113*e0c4386eSCy Schubert         OPENSSL_ApplinkTable[APPLINK_FPRINTF] = fprintf;
114*e0c4386eSCy Schubert         OPENSSL_ApplinkTable[APPLINK_FGETS] = fgets;
115*e0c4386eSCy Schubert         OPENSSL_ApplinkTable[APPLINK_FREAD] = fread;
116*e0c4386eSCy Schubert         OPENSSL_ApplinkTable[APPLINK_FWRITE] = fwrite;
117*e0c4386eSCy Schubert         OPENSSL_ApplinkTable[APPLINK_FSETMOD] = app_fsetmod;
118*e0c4386eSCy Schubert         OPENSSL_ApplinkTable[APPLINK_FEOF] = app_feof;
119*e0c4386eSCy Schubert         OPENSSL_ApplinkTable[APPLINK_FCLOSE] = fclose;
120*e0c4386eSCy Schubert 
121*e0c4386eSCy Schubert         OPENSSL_ApplinkTable[APPLINK_FOPEN] = fopen;
122*e0c4386eSCy Schubert         OPENSSL_ApplinkTable[APPLINK_FSEEK] = fseek;
123*e0c4386eSCy Schubert         OPENSSL_ApplinkTable[APPLINK_FTELL] = ftell;
124*e0c4386eSCy Schubert         OPENSSL_ApplinkTable[APPLINK_FFLUSH] = fflush;
125*e0c4386eSCy Schubert         OPENSSL_ApplinkTable[APPLINK_FERROR] = app_ferror;
126*e0c4386eSCy Schubert         OPENSSL_ApplinkTable[APPLINK_CLEARERR] = app_clearerr;
127*e0c4386eSCy Schubert         OPENSSL_ApplinkTable[APPLINK_FILENO] = app_fileno;
128*e0c4386eSCy Schubert 
129*e0c4386eSCy Schubert         OPENSSL_ApplinkTable[APPLINK_OPEN] = _open;
130*e0c4386eSCy Schubert         OPENSSL_ApplinkTable[APPLINK_READ] = _read;
131*e0c4386eSCy Schubert         OPENSSL_ApplinkTable[APPLINK_WRITE] = _write;
132*e0c4386eSCy Schubert         OPENSSL_ApplinkTable[APPLINK_LSEEK] = _lseek;
133*e0c4386eSCy Schubert         OPENSSL_ApplinkTable[APPLINK_CLOSE] = _close;
134*e0c4386eSCy Schubert 
135*e0c4386eSCy Schubert         once = 0;
136*e0c4386eSCy Schubert     }
137*e0c4386eSCy Schubert 
138*e0c4386eSCy Schubert     return OPENSSL_ApplinkTable;
139*e0c4386eSCy Schubert }
140*e0c4386eSCy Schubert 
141*e0c4386eSCy Schubert #ifdef __cplusplus
142*e0c4386eSCy Schubert }
143*e0c4386eSCy Schubert #endif
144*e0c4386eSCy Schubert #endif
145