Remove warnings on WIN32

This commit is contained in:
Zhi Guan
2022-12-28 23:37:30 +08:00
parent cdebed562a
commit 70f6a42561
12 changed files with 116 additions and 50 deletions

27
include/gmssl/file.h Normal file
View File

@@ -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 <stdio.h>
#include <string.h>
#ifdef __cplusplus
extern "C" {
#endif
int file_size(FILE *fp, size_t *size);
#ifdef __cplusplus
}
#endif
#endif

35
src/file.c Normal file
View File

@@ -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 <errno.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <gmssl/error.h>
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;
}

View File

@@ -18,6 +18,7 @@
#include <gmssl/pem.h>
#include <gmssl/asn1.h>
#include <gmssl/rsa.h>
#include <gmssl/file.h>
#include <gmssl/x509_oid.h>
#include <gmssl/x509_str.h>
#include <gmssl/x509_alg.h>
@@ -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;
}

View File

@@ -12,7 +12,7 @@
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <gmssl/file.h>
#include <gmssl/x509.h>
#include <gmssl/cms.h>
@@ -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;

View File

@@ -12,7 +12,7 @@
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <gmssl/file.h>
#include <gmssl/cms.h>
#include <gmssl/x509.h>
#include <gmssl/rand.h>
@@ -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--;

View File

@@ -12,7 +12,7 @@
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <gmssl/file.h>
#include <gmssl/cms.h>
#include <gmssl/x509.h>
#include <gmssl/rand.h>
@@ -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;

View File

@@ -12,7 +12,7 @@
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <gmssl/file.h>
#include <gmssl/x509.h>
#include <gmssl/cms.h>
#include <gmssl/error.h>
@@ -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))) {

View File

@@ -12,7 +12,7 @@
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <gmssl/file.h>
#include <gmssl/cms.h>
#include <gmssl/x509.h>
#include <gmssl/rand.h>
@@ -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;

View File

@@ -12,7 +12,7 @@
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <gmssl/file.h>
#include <gmssl/x509.h>
#include <gmssl/x509_crl.h>
@@ -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))) {

View File

@@ -12,7 +12,7 @@
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <gmssl/file.h>
#include <gmssl/x509.h>
#include <gmssl/x509_crl.h>
@@ -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))) {

View File

@@ -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
}

View File

@@ -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: