From 70f6a42561dc1659d917c6f9ba55f8f1ee3076da Mon Sep 17 00:00:00 2001 From: Zhi Guan Date: Wed, 28 Dec 2022 23:37:30 +0800 Subject: [PATCH] Remove warnings on WIN32 --- include/gmssl/file.h | 27 +++++++++++++++++++++++++++ src/file.c | 35 +++++++++++++++++++++++++++++++++++ src/x509_cer.c | 17 +++++++++-------- tools/cmsdecrypt.c | 11 +++++++---- tools/cmsencrypt.c | 10 ++++++---- tools/cmsparse.c | 8 ++++---- tools/cmssign.c | 11 +++-------- tools/cmsverify.c | 12 +++++++----- tools/crlparse.c | 11 +++-------- tools/crlverify.c | 12 +++--------- tools/tls12_client.c | 6 ++++++ tools/tls13_client.c | 6 ++++++ 12 files changed, 116 insertions(+), 50 deletions(-) create mode 100644 include/gmssl/file.h create mode 100644 src/file.c diff --git a/include/gmssl/file.h b/include/gmssl/file.h new file mode 100644 index 00000000..4906e2ce --- /dev/null +++ b/include/gmssl/file.h @@ -0,0 +1,27 @@ +/* + * Copyright 2014-2022 The GmSSL Project. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the License); you may + * not use this file except in compliance with the License. + * + * http://www.apache.org/licenses/LICENSE-2.0 + */ + +#ifndef GMSSL_FILE_H +#define GMSSL_FILE_H + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + + +int file_size(FILE *fp, size_t *size); + + +#ifdef __cplusplus +} +#endif +#endif diff --git a/src/file.c b/src/file.c new file mode 100644 index 00000000..1e86bec4 --- /dev/null +++ b/src/file.c @@ -0,0 +1,35 @@ +/* + * Copyright 2014-2022 The GmSSL Project. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the License); you may + * not use this file except in compliance with the License. + * + * http://www.apache.org/licenses/LICENSE-2.0 + */ + + +#include +#include +#include +#include +#include +#include + + +int file_size(FILE *fp, size_t *size) +{ + int fd; + struct stat st; + +#ifdef WIN32 + fd = _fileno(fp); +#else + fd = fileno(fp); +#endif + if (fstat(fd, &st) < 0) { + error_print(); + return -1; + } + *size = st.st_size; + return 1; +} diff --git a/src/x509_cer.c b/src/x509_cer.c index 39dc0563..72b54df8 100644 --- a/src/x509_cer.c +++ b/src/x509_cer.c @@ -18,6 +18,7 @@ #include #include #include +#include #include #include #include @@ -1660,14 +1661,14 @@ int x509_cert_new_from_file(uint8_t **out, size_t *outlen, const char *file) { int ret = -1; FILE *fp = NULL; - struct stat st; + size_t fsize; uint8_t *buf = NULL; size_t buflen; if (!(fp = fopen(file, "r")) - || fstat(fileno(fp), &st) < 0 - || (buflen = (st.st_size * 3)/4 + 1) < 0 - || (buf = malloc((st.st_size * 3)/4 + 1)) == NULL) { + || file_size(fp, &fsize) != 1 + || (buflen = (fsize * 3)/4 + 1) < 0 + || (buf = malloc((fsize * 3)/4 + 1)) == NULL) { error_print(); goto end; } @@ -1688,14 +1689,14 @@ int x509_certs_new_from_file(uint8_t **out, size_t *outlen, const char *file) { int ret = -1; FILE *fp = NULL; - struct stat st; + size_t fsize; uint8_t *buf = NULL; size_t buflen; if (!(fp = fopen(file, "r")) - || fstat(fileno(fp), &st) < 0 - || (buflen = (st.st_size * 3)/4 + 1) < 0 - || (buf = malloc((st.st_size * 3)/4 + 1)) == NULL) { + || file_size(fp, &fsize) != 1 + || (buflen = (fsize * 3)/4 + 1) < 0 + || (buf = malloc((fsize * 3)/4 + 1)) == NULL) { error_print(); goto end; } diff --git a/tools/cmsdecrypt.c b/tools/cmsdecrypt.c index e9452113..d3460f2c 100644 --- a/tools/cmsdecrypt.c +++ b/tools/cmsdecrypt.c @@ -12,7 +12,7 @@ #include #include #include -#include +#include #include #include @@ -35,7 +35,7 @@ int cmsdecrypt_main(int argc, char **argv) FILE *outfp = stdout; uint8_t cert[1024]; size_t certlen; - struct stat st; + size_t inlen; uint8_t *cms = NULL; size_t cmslen, cms_maxlen; SM2_KEY key; @@ -130,8 +130,11 @@ bad: goto end; } - fstat(fileno(infp), &st); - cms_maxlen = (st.st_size * 3)/4 + 1; + if (file_size(infp, &inlen) != 1) { + fprintf(stderr, "%s: get input length failed\n", prog); + goto end; + } + cms_maxlen = (inlen * 3)/4 + 1; if (!(cms = malloc(cms_maxlen))) { fprintf(stderr, "%s: malloc failure\n", prog); goto end; diff --git a/tools/cmsencrypt.c b/tools/cmsencrypt.c index 4e013379..1d762f16 100644 --- a/tools/cmsencrypt.c +++ b/tools/cmsencrypt.c @@ -12,7 +12,7 @@ #include #include #include -#include +#include #include #include #include @@ -58,7 +58,6 @@ static int get_files_size(int argc, char **argv, const char *option, size_t *len char *prog = argv[0]; char *file = NULL; FILE *fp = NULL; - struct stat st; argc--; argv++; @@ -66,21 +65,24 @@ static int get_files_size(int argc, char **argv, const char *option, size_t *len *len = 0; while (argc > 1) { if (!strcmp(*argv, option)) { + size_t fsize; + if (--argc < 1) { fprintf(stderr, "%s: '%s' option value missing\n", prog, *argv); return -1; } file = *(++argv); + if (!(fp = fopen(file, "rb"))) { fprintf(stderr, "%s: open '%s' failed : %s\n", prog, file, strerror(errno)); return -1; } - if (fstat(fileno(fp), &st) < 0) { + if (file_size(fp, &fsize) != 1) { fprintf(stderr, "%s: access '%s' failed : %s\n", prog, file, strerror(errno)); fclose(fp); return -1; } - *len += st.st_size; + *len += fsize; fclose(fp); } argc--; diff --git a/tools/cmsparse.c b/tools/cmsparse.c index 55d4b455..204f5247 100644 --- a/tools/cmsparse.c +++ b/tools/cmsparse.c @@ -12,7 +12,7 @@ #include #include #include -#include +#include #include #include #include @@ -26,7 +26,7 @@ int cmsparse_main(int argc, char **argv) char *prog = argv[0]; char *infile = NULL; FILE *infp = stdin; - struct stat st; + size_t inlen; uint8_t *cms = NULL; size_t cms_maxlen, cmslen; @@ -66,11 +66,11 @@ bad: goto end; } - if (fstat(fileno(infp), &st) < 0) { + if (file_size(infp, &inlen) != 1) { // FIXME: infp == stdin? fprintf(stderr, "%s: access '%s' failed : %s\n", prog, infile, strerror(errno)); goto end; } - cms_maxlen = (st.st_size * 3)/4 + 1; + cms_maxlen = (inlen * 3)/4 + 1; if (!(cms = malloc(cms_maxlen))) { fprintf(stderr, "%s: malloc failure\n", prog); goto end; diff --git a/tools/cmssign.c b/tools/cmssign.c index 69762ec0..f6d1c14f 100644 --- a/tools/cmssign.c +++ b/tools/cmssign.c @@ -12,7 +12,7 @@ #include #include #include -#include +#include #include #include #include @@ -49,7 +49,6 @@ int cmssign_main(int argc, char **argv) SM2_KEY key; uint8_t cert[1024]; size_t certlen; - struct stat st; uint8_t *in = NULL; size_t inlen; uint8_t *cms = NULL; @@ -153,12 +152,8 @@ bad: cert_and_key.certs_len = certlen; cert_and_key.sign_key = &key; - if (fstat(fileno(infp), &st) < 0) { - fprintf(stderr, "%s: access file error : %s\n", prog, strerror(errno)); - goto end; - } - if ((inlen = st.st_size) <= 0) { - fprintf(stderr, "%s: invalid input length\n", prog); + if (file_size(infp, &inlen) != 1) { + fprintf(stderr, "%s: get input length failed\n", prog); goto end; } if (!(in = malloc(inlen))) { diff --git a/tools/cmsverify.c b/tools/cmsverify.c index 7d0320a4..71125112 100644 --- a/tools/cmsverify.c +++ b/tools/cmsverify.c @@ -12,7 +12,7 @@ #include #include #include -#include +#include #include #include #include @@ -29,10 +29,9 @@ int cmsverify_main(int argc, char **argv) char *outfile = NULL; FILE *infp = NULL; FILE *outfp = NULL; - struct stat st; + size_t inlen; uint8_t *cms = NULL; size_t cmslen, cms_maxlen; - int content_type; const uint8_t *content; size_t content_len; @@ -87,8 +86,11 @@ bad: fprintf(stderr, "%s: '-in' option required\n", prog); goto end; } - fstat(fileno(infp), &st); - cms_maxlen = (st.st_size * 3)/4 + 1; + if (file_size(infp, &inlen) != 1) { + fprintf(stderr, "%s: get input length failed\n", prog); + goto end; + } + cms_maxlen = (inlen * 3)/4 + 1; if (!(cms = malloc(cms_maxlen))) { fprintf(stderr, "%s: malloc failure\n", prog); goto end; diff --git a/tools/crlparse.c b/tools/crlparse.c index b44ef2a5..a2c8e2ad 100644 --- a/tools/crlparse.c +++ b/tools/crlparse.c @@ -12,7 +12,7 @@ #include #include #include -#include +#include #include #include @@ -27,7 +27,6 @@ int crlparse_main(int argc, char **argv) char *outfile = NULL; FILE *infp = stdin; FILE *outfp = stdout; - struct stat st; uint8_t *in = NULL; size_t inlen; const uint8_t *pin; @@ -76,12 +75,8 @@ bad: fprintf(stderr, "%s: '-in' option required\n", prog); goto end; } - if (fstat(fileno(infp), &st) < 0) { - fprintf(stderr, "%s: access file error : %s\n", prog, strerror(errno)); - goto end; - } - if ((inlen = st.st_size) <= 0) { - fprintf(stderr, "%s: invalid input length\n", prog); + if (file_size(infp, &inlen) != 1) { + fprintf(stderr, "%s: get input length failed\n", prog); goto end; } if (!(in = malloc(inlen))) { diff --git a/tools/crlverify.c b/tools/crlverify.c index c5d29eeb..532a7ae4 100644 --- a/tools/crlverify.c +++ b/tools/crlverify.c @@ -12,7 +12,7 @@ #include #include #include -#include +#include #include #include @@ -29,7 +29,6 @@ int crlverify_main(int argc, char **argv) FILE *cacertfp = NULL; uint8_t *in = NULL; size_t inlen; - struct stat st; const uint8_t *pin; const uint8_t *crl = NULL; size_t crllen; @@ -87,13 +86,8 @@ bad: goto end; } - - if (fstat(fileno(infp), &st) < 0) { - fprintf(stderr, "%s: access file error : %s\n", prog, strerror(errno)); - goto end; - } - if ((inlen = st.st_size) <= 0) { - fprintf(stderr, "%s: invalid input length\n", prog); + if (file_size(infp, &inlen) != 1) { + fprintf(stderr, "%s: get input length failed\n", prog); goto end; } if (!(in = malloc(inlen))) { diff --git a/tools/tls12_client.c b/tools/tls12_client.c index 495f03ea..ee0a1a78 100644 --- a/tools/tls12_client.c +++ b/tools/tls12_client.c @@ -142,7 +142,10 @@ bad: FD_ZERO(&fds); FD_SET(conn.sock, &fds); +#ifdef WIN32 +#else FD_SET(fileno(stdin), &fds); +#endif if (select((int)(conn.sock + 1), &fds, NULL, NULL, NULL) < 0) { fprintf(stderr, "%s: select failed\n", prog); @@ -165,6 +168,8 @@ bad: } } +#ifdef WIN32 +#else if (FD_ISSET(fileno(stdin), &fds)) { memset(send_buf, 0, sizeof(send_buf)); @@ -181,6 +186,7 @@ bad: goto end; } } +#endif } diff --git a/tools/tls13_client.c b/tools/tls13_client.c index 3517fdbc..50c17735 100644 --- a/tools/tls13_client.c +++ b/tools/tls13_client.c @@ -141,7 +141,10 @@ bad: FD_ZERO(&fds); FD_SET(conn.sock, &fds); +#ifdef WIN32 +#else FD_SET(fileno(stdin), &fds); +#endif if (select((int)(conn.sock + 1), // In WinSock2, select() ignore the this arg &fds, NULL, NULL, NULL) < 0) { @@ -165,6 +168,8 @@ bad: } } +#ifdef WIN32 +#else if (FD_ISSET(fileno(stdin), &fds)) { memset(send_buf, 0, sizeof(send_buf)); @@ -181,6 +186,7 @@ bad: goto end; } } +#endif } end: