diff options
author | Christian Grothoff <christian@grothoff.org> | 2021-02-26 21:23:11 +0100 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2021-02-26 21:23:11 +0100 |
commit | b55c4031d49904148139660dea231f37f2afc878 (patch) | |
tree | 116de0c1d27bd252fd33acd75b782e9aca493304 /src/microhttpd/response.c | |
parent | 2275296815b6f90f4b9fc313cb5c46879aae434d (diff) | |
download | libmicrohttpd-b55c4031d49904148139660dea231f37f2afc878.tar.gz libmicrohttpd-b55c4031d49904148139660dea231f37f2afc878.zip |
-misc style fixes, no semantic changes
Diffstat (limited to 'src/microhttpd/response.c')
-rw-r--r-- | src/microhttpd/response.c | 217 |
1 files changed, 105 insertions, 112 deletions
diff --git a/src/microhttpd/response.c b/src/microhttpd/response.c index 3dbcd245..14e036c8 100644 --- a/src/microhttpd/response.c +++ b/src/microhttpd/response.c | |||
@@ -1,6 +1,6 @@ | |||
1 | /* | 1 | /* |
2 | This file is part of libmicrohttpd | 2 | This file is part of libmicrohttpd |
3 | Copyright (C) 2007, 2009, 2010, 2016, 2017 Daniel Pittman and Christian Grothoff | 3 | Copyright (C) 2007-2021 Daniel Pittman and Christian Grothoff |
4 | 4 | ||
5 | This library is free software; you can redistribute it and/or | 5 | This library is free software; you can redistribute it and/or |
6 | modify it under the terms of the GNU Lesser General Public | 6 | modify it under the terms of the GNU Lesser General Public |
@@ -861,135 +861,128 @@ MHD_create_response_from_buffer_with_free_callback (size_t size, | |||
861 | */ | 861 | */ |
862 | _MHD_EXTERN struct MHD_Response * | 862 | _MHD_EXTERN struct MHD_Response * |
863 | MHD_create_response_from_iovec (const struct MHD_IoVec *iov, | 863 | MHD_create_response_from_iovec (const struct MHD_IoVec *iov, |
864 | int iovcnt, | 864 | unsigned int iovcnt, |
865 | MHD_ContentReaderFreeCallback free_cb, | 865 | MHD_ContentReaderFreeCallback free_cb, |
866 | void *cls) | 866 | void *cls) |
867 | { | 867 | { |
868 | struct MHD_Response *response; | 868 | struct MHD_Response *response; |
869 | unsigned int i; | ||
870 | int i_cp = 0; /**< Index in the copy of iov */ | ||
871 | uint64_t total_size = 0; | ||
872 | const void *last_valid_buffer = NULL; | ||
869 | 873 | ||
870 | if ((NULL == iov) && (0 < iovcnt)) | 874 | if ((NULL == iov) && (0 < iovcnt)) |
871 | return NULL; | 875 | return NULL; |
872 | 876 | ||
873 | response = MHD_calloc_ (1, sizeof (struct MHD_Response)); | 877 | response = MHD_calloc_ (1, sizeof (struct MHD_Response)); |
874 | if (NULL != response) | 878 | if (NULL == response) |
879 | return NULL; | ||
880 | if (! MHD_mutex_init_ (&response->mutex)) | ||
881 | { | ||
882 | free (response); | ||
883 | return NULL; | ||
884 | } | ||
885 | /* Calculate final size, number of valid elements, and check 'iov' */ | ||
886 | for (i = 0; i < iovcnt; ++i) | ||
875 | { | 887 | { |
876 | if (MHD_mutex_init_ (&response->mutex)) | 888 | if (0 == iov[i].iov_len) |
889 | continue; /* skip zero-sized elements */ | ||
890 | if (NULL == iov[i].iov_base) | ||
877 | { | 891 | { |
878 | int i; | 892 | i_cp = -1; /* error */ |
879 | int i_cp; /**< Index in the copy of iov */ | 893 | break; |
880 | uint64_t total_size; | 894 | } |
881 | void *last_valid_buffer; | 895 | if ( (total_size > (total_size + iov[i].iov_len)) || |
882 | 896 | (INT_MAX == i_cp) || | |
883 | i_cp = 0; | 897 | (SSIZE_MAX < iov[i].iov_len) ) |
884 | total_size = 0; | 898 | { |
885 | last_valid_buffer = NULL; | 899 | i_cp = -1; /* overflow */ |
886 | /* Calculate final size, number of valid elements, and check 'iov' */ | 900 | break; |
887 | for (i = 0; iovcnt > i; ++i) | 901 | } |
888 | { | 902 | last_valid_buffer = iov[i].iov_base; |
889 | #if defined(MHD_WINSOCK_SOCKETS) && defined(_WIN64) | 903 | total_size += iov[i].iov_len; |
890 | int64_t i_add; | ||
891 | #endif /* ! MHD_WINSOCK_SOCKETS && _WIN64 */ | ||
892 | if (0 == iov[i].iov_len) | ||
893 | continue; /* skip zero-sized elements */ | ||
894 | |||
895 | if (NULL == iov[i].iov_base) | ||
896 | { | ||
897 | i_cp = -1; /* error */ | ||
898 | break; | ||
899 | } | ||
900 | if ( (total_size > (total_size + iov[i].iov_len)) || | ||
901 | (INT_MAX == i_cp) || | ||
902 | (SSIZE_MAX < iov[i].iov_len) ) | ||
903 | { | ||
904 | i_cp = -1; /* overflow */ | ||
905 | break; | ||
906 | } | ||
907 | last_valid_buffer = iov[i].iov_base; | ||
908 | total_size += iov[i].iov_len; | ||
909 | #if defined(MHD_POSIX_SOCKETS) || ! defined(_WIN64) | 904 | #if defined(MHD_POSIX_SOCKETS) || ! defined(_WIN64) |
910 | i_cp++; | 905 | i_cp++; |
911 | #else /* ! MHD_POSIX_SOCKETS && _WIN64 */ | 906 | #else /* ! MHD_POSIX_SOCKETS && _WIN64 */ |
912 | i_add = iov[i].iov_len / ULONG_MAX; | 907 | { |
913 | if (0 != iov[i].iov_len % ULONG_MAX) | 908 | int64_t i_add; |
914 | i_add++; | 909 | |
915 | if (INT_MAX < (i_add + i_cp)) | 910 | i_add = iov[i].iov_len / ULONG_MAX; |
916 | { | 911 | if (0 != iov[i].iov_len % ULONG_MAX) |
917 | i_cp = -1; /* overflow */ | 912 | i_add++; |
918 | break; | 913 | if (INT_MAX < (i_add + i_cp)) |
919 | } | ||
920 | i_cp += (int) i_add; | ||
921 | #endif /* ! MHD_POSIX_SOCKETS && _WIN64 */ | ||
922 | } | ||
923 | if (0 <= i_cp) | ||
924 | { | 914 | { |
925 | response->fd = -1; | 915 | i_cp = -1; /* overflow */ |
926 | response->reference_count = 1; | 916 | break; |
927 | response->total_size = total_size; | ||
928 | response->crc_cls = cls; | ||
929 | response->crfc = free_cb; | ||
930 | if (1 < i_cp) | ||
931 | { | ||
932 | MHD_iovec_ *iov_copy; | ||
933 | int num_copy_elements = i_cp; | ||
934 | |||
935 | iov_copy = MHD_calloc_ (num_copy_elements, sizeof(MHD_iovec_)); | ||
936 | if (NULL != iov_copy) | ||
937 | { | ||
938 | i_cp = 0; | ||
939 | for (i = 0; iovcnt > i; ++i) | ||
940 | { | ||
941 | size_t element_size; | ||
942 | uint8_t *buf; | ||
943 | |||
944 | if (0 == iov[i].iov_len) | ||
945 | continue; /* skip zero-sized elements */ | ||
946 | |||
947 | buf = (uint8_t*) iov[i].iov_base; | ||
948 | element_size = iov[i].iov_len; | ||
949 | #if defined(MHD_WINSOCK_SOCKETS) && defined(_WIN64) | ||
950 | while (ULONG_MAX < element_size) | ||
951 | { | ||
952 | iov_copy[i_cp].iov_base = (void*) buf; | ||
953 | iov_copy[i_cp].iov_len = ULONG_MAX; | ||
954 | buf += ULONG_MAX; | ||
955 | element_size -= ULONG_MAX; | ||
956 | i_cp++; | ||
957 | } | ||
958 | #endif /* MHD_WINSOCK_SOCKETS && _WIN64 */ | ||
959 | iov_copy[i_cp].iov_base = (void*) buf; | ||
960 | iov_copy[i_cp].iov_len = element_size; | ||
961 | i_cp++; | ||
962 | } | ||
963 | |||
964 | mhd_assert (num_copy_elements == i_cp); | ||
965 | response->data_iov = iov_copy; | ||
966 | response->data_iovcnt = i_cp; | ||
967 | |||
968 | return response; | ||
969 | } | ||
970 | |||
971 | } | ||
972 | else if (1 == i_cp) | ||
973 | { | ||
974 | mhd_assert (NULL != last_valid_buffer); | ||
975 | response->data = last_valid_buffer; | ||
976 | response->data_size = total_size; | ||
977 | |||
978 | return response; | ||
979 | } | ||
980 | else /* if (0 == i_nz) */ | ||
981 | { | ||
982 | mhd_assert (0 == total_size); | ||
983 | |||
984 | return response; | ||
985 | } | ||
986 | } | 917 | } |
987 | /* Some error condition */ | 918 | i_cp += (int) i_add; |
988 | MHD_mutex_destroy_chk_ (&response->mutex); | ||
989 | } | 919 | } |
920 | #endif /* ! MHD_POSIX_SOCKETS && _WIN64 */ | ||
921 | } | ||
922 | if (-1 == i_cp) | ||
923 | { | ||
924 | /* Some error condition */ | ||
925 | MHD_mutex_destroy_chk_ (&response->mutex); | ||
990 | free (response); | 926 | free (response); |
927 | return NULL; | ||
928 | } | ||
929 | response->fd = -1; | ||
930 | response->reference_count = 1; | ||
931 | response->total_size = total_size; | ||
932 | response->crc_cls = cls; | ||
933 | response->crfc = free_cb; | ||
934 | if (0 == i_cp) | ||
935 | { | ||
936 | mhd_assert (0 == total_size); | ||
937 | return response; | ||
938 | } | ||
939 | if (1 == i_cp) | ||
940 | { | ||
941 | mhd_assert (NULL != last_valid_buffer); | ||
942 | response->data = (void *) last_valid_buffer; | ||
943 | response->data_size = total_size; | ||
944 | return response; | ||
945 | } | ||
946 | mhd_assert (1 < i_cp); | ||
947 | { | ||
948 | MHD_iovec_ *iov_copy; | ||
949 | int num_copy_elements = i_cp; | ||
950 | |||
951 | iov_copy = MHD_calloc_ (num_copy_elements, | ||
952 | sizeof(MHD_iovec_)); | ||
953 | if (NULL == iov_copy) | ||
954 | { | ||
955 | MHD_mutex_destroy_chk_ (&response->mutex); | ||
956 | free (response); | ||
957 | return NULL; | ||
958 | } | ||
959 | i_cp = 0; | ||
960 | for (i = 0; i < iovcnt; ++i) | ||
961 | { | ||
962 | size_t element_size = iov[i].iov_len; | ||
963 | const void *buf = iov[i].iov_base; | ||
964 | |||
965 | if (0 == element_size) | ||
966 | continue; /* skip zero-sized elements */ | ||
967 | #if defined(MHD_WINSOCK_SOCKETS) && defined(_WIN64) | ||
968 | while (ULONG_MAX < element_size) | ||
969 | { | ||
970 | iov_copy[i_cp].iov_base = buf; | ||
971 | iov_copy[i_cp].iov_len = ULONG_MAX; | ||
972 | buf += ULONG_MAX; | ||
973 | element_size -= ULONG_MAX; | ||
974 | i_cp++; | ||
975 | } | ||
976 | #endif /* MHD_WINSOCK_SOCKETS && _WIN64 */ | ||
977 | iov_copy[i_cp].iov_base = (void *) buf; | ||
978 | iov_copy[i_cp].iov_len = element_size; | ||
979 | i_cp++; | ||
980 | } | ||
981 | mhd_assert (num_copy_elements == i_cp); | ||
982 | response->data_iov = iov_copy; | ||
983 | response->data_iovcnt = i_cp; | ||
984 | return response; | ||
991 | } | 985 | } |
992 | return NULL; | ||
993 | } | 986 | } |
994 | 987 | ||
995 | 988 | ||