diff options
Diffstat (limited to 'src/lib/response_from_fd.c')
-rw-r--r-- | src/lib/response_from_fd.c | 85 |
1 files changed, 42 insertions, 43 deletions
diff --git a/src/lib/response_from_fd.c b/src/lib/response_from_fd.c index ac0a54d1..ffeb2b91 100644 --- a/src/lib/response_from_fd.c +++ b/src/lib/response_from_fd.c | |||
@@ -57,34 +57,34 @@ file_reader (void *cls, | |||
57 | size_t max) | 57 | size_t max) |
58 | { | 58 | { |
59 | struct MHD_Response *response = cls; | 59 | struct MHD_Response *response = cls; |
60 | #if !defined(_WIN32) || defined(__CYGWIN__) | 60 | #if ! defined(_WIN32) || defined(__CYGWIN__) |
61 | ssize_t n; | 61 | ssize_t n; |
62 | #else /* _WIN32 && !__CYGWIN__ */ | 62 | #else /* _WIN32 && !__CYGWIN__ */ |
63 | const HANDLE fh = (HANDLE) _get_osfhandle (response->fd); | 63 | const HANDLE fh = (HANDLE) _get_osfhandle (response->fd); |
64 | #endif /* _WIN32 && !__CYGWIN__ */ | 64 | #endif /* _WIN32 && !__CYGWIN__ */ |
65 | const int64_t offset64 = (int64_t)(pos + response->fd_off); | 65 | const int64_t offset64 = (int64_t) (pos + response->fd_off); |
66 | 66 | ||
67 | if (offset64 < 0) | 67 | if (offset64 < 0) |
68 | return MHD_CONTENT_READER_END_WITH_ERROR; /* seek to required position is not possible */ | 68 | return MHD_CONTENT_READER_END_WITH_ERROR; /* seek to required position is not possible */ |
69 | 69 | ||
70 | #if !defined(_WIN32) || defined(__CYGWIN__) | 70 | #if ! defined(_WIN32) || defined(__CYGWIN__) |
71 | if (max > SSIZE_MAX) | 71 | if (max > SSIZE_MAX) |
72 | max = SSIZE_MAX; /* Clamp to maximum return value. */ | 72 | max = SSIZE_MAX; /* Clamp to maximum return value. */ |
73 | 73 | ||
74 | #if defined(HAVE_PREAD64) | 74 | #if defined(HAVE_PREAD64) |
75 | n = pread64 (response->fd, | 75 | n = pread64 (response->fd, |
76 | buf, | 76 | buf, |
77 | max, | 77 | max, |
78 | offset64); | 78 | offset64); |
79 | #elif defined(HAVE_PREAD) | 79 | #elif defined(HAVE_PREAD) |
80 | if ( (sizeof(off_t) < sizeof (uint64_t)) && | 80 | if ( (sizeof(off_t) < sizeof (uint64_t)) && |
81 | (offset64 > (uint64_t)INT32_MAX) ) | 81 | (offset64 > (uint64_t) INT32_MAX) ) |
82 | return MHD_CONTENT_READER_END_WITH_ERROR; /* Read at required position is not possible. */ | 82 | return MHD_CONTENT_READER_END_WITH_ERROR; /* Read at required position is not possible. */ |
83 | 83 | ||
84 | n = pread (response->fd, | 84 | n = pread (response->fd, |
85 | buf, | 85 | buf, |
86 | max, | 86 | max, |
87 | (off_t) offset64); | 87 | (off_t) offset64); |
88 | #else /* ! HAVE_PREAD */ | 88 | #else /* ! HAVE_PREAD */ |
89 | #if defined(HAVE_LSEEK64) | 89 | #if defined(HAVE_LSEEK64) |
90 | if (lseek64 (response->fd, | 90 | if (lseek64 (response->fd, |
@@ -93,7 +93,7 @@ file_reader (void *cls, | |||
93 | return MHD_CONTENT_READER_END_WITH_ERROR; /* can't seek to required position */ | 93 | return MHD_CONTENT_READER_END_WITH_ERROR; /* can't seek to required position */ |
94 | #else /* ! HAVE_LSEEK64 */ | 94 | #else /* ! HAVE_LSEEK64 */ |
95 | if ( (sizeof(off_t) < sizeof (uint64_t)) && | 95 | if ( (sizeof(off_t) < sizeof (uint64_t)) && |
96 | (offset64 > (uint64_t)INT32_MAX) ) | 96 | (offset64 > (uint64_t) INT32_MAX) ) |
97 | return MHD_CONTENT_READER_END_WITH_ERROR; /* seek to required position is not possible */ | 97 | return MHD_CONTENT_READER_END_WITH_ERROR; /* seek to required position is not possible */ |
98 | 98 | ||
99 | if (lseek (response->fd, | 99 | if (lseek (response->fd, |
@@ -115,25 +115,25 @@ file_reader (void *cls, | |||
115 | if (INVALID_HANDLE_VALUE == fh) | 115 | if (INVALID_HANDLE_VALUE == fh) |
116 | return MHD_CONTENT_READER_END_WITH_ERROR; /* Value of 'response->fd' is not valid. */ | 116 | return MHD_CONTENT_READER_END_WITH_ERROR; /* Value of 'response->fd' is not valid. */ |
117 | else | 117 | else |
118 | { | 118 | { |
119 | OVERLAPPED f_ol = {0, 0, {{0, 0}}, 0}; /* Initialize to zero. */ | 119 | OVERLAPPED f_ol = {0, 0, {{0, 0}}, 0}; /* Initialize to zero. */ |
120 | ULARGE_INTEGER pos_uli; | 120 | ULARGE_INTEGER pos_uli; |
121 | DWORD toRead = (max > INT32_MAX) ? INT32_MAX : (DWORD) max; | 121 | DWORD toRead = (max > INT32_MAX) ? INT32_MAX : (DWORD) max; |
122 | DWORD resRead; | 122 | DWORD resRead; |
123 | 123 | ||
124 | pos_uli.QuadPart = (uint64_t) offset64; /* Simple transformation 64bit -> 2x32bit. */ | 124 | pos_uli.QuadPart = (uint64_t) offset64; /* Simple transformation 64bit -> 2x32bit. */ |
125 | f_ol.Offset = pos_uli.LowPart; | 125 | f_ol.Offset = pos_uli.LowPart; |
126 | f_ol.OffsetHigh = pos_uli.HighPart; | 126 | f_ol.OffsetHigh = pos_uli.HighPart; |
127 | if (! ReadFile (fh, | 127 | if (! ReadFile (fh, |
128 | (void*)buf, | 128 | (void*) buf, |
129 | toRead, | 129 | toRead, |
130 | &resRead, | 130 | &resRead, |
131 | &f_ol)) | 131 | &f_ol)) |
132 | return MHD_CONTENT_READER_END_WITH_ERROR; /* Read error. */ | 132 | return MHD_CONTENT_READER_END_WITH_ERROR; /* Read error. */ |
133 | if (0 == resRead) | 133 | if (0 == resRead) |
134 | return MHD_CONTENT_READER_END_OF_STREAM; | 134 | return MHD_CONTENT_READER_END_OF_STREAM; |
135 | return (ssize_t) resRead; | 135 | return (ssize_t) resRead; |
136 | } | 136 | } |
137 | #endif /* _WIN32 && !__CYGWIN__ */ | 137 | #endif /* _WIN32 && !__CYGWIN__ */ |
138 | } | 138 | } |
139 | 139 | ||
@@ -174,18 +174,18 @@ free_callback (void *cls) | |||
174 | */ | 174 | */ |
175 | struct MHD_Response * | 175 | struct MHD_Response * |
176 | MHD_response_from_fd (enum MHD_HTTP_StatusCode sc, | 176 | MHD_response_from_fd (enum MHD_HTTP_StatusCode sc, |
177 | int fd, | 177 | int fd, |
178 | uint64_t offset, | 178 | uint64_t offset, |
179 | uint64_t size) | 179 | uint64_t size) |
180 | { | 180 | { |
181 | struct MHD_Response *response; | 181 | struct MHD_Response *response; |
182 | 182 | ||
183 | mhd_assert (-1 != fd); | 183 | mhd_assert (-1 != fd); |
184 | #if !defined(HAVE___LSEEKI64) && !defined(HAVE_LSEEK64) | 184 | #if ! defined(HAVE___LSEEKI64) && ! defined(HAVE_LSEEK64) |
185 | if ( (sizeof (uint64_t) > sizeof (off_t)) && | 185 | if ( (sizeof (uint64_t) > sizeof (off_t)) && |
186 | ( (size > (uint64_t)INT32_MAX) || | 186 | ( (size > (uint64_t) INT32_MAX) || |
187 | (offset > (uint64_t)INT32_MAX) || | 187 | (offset > (uint64_t) INT32_MAX) || |
188 | ((size + offset) >= (uint64_t)INT32_MAX) ) ) | 188 | ((size + offset) >= (uint64_t) INT32_MAX) ) ) |
189 | return NULL; | 189 | return NULL; |
190 | #endif | 190 | #endif |
191 | if ( ((int64_t) size < 0) || | 191 | if ( ((int64_t) size < 0) || |
@@ -194,11 +194,11 @@ MHD_response_from_fd (enum MHD_HTTP_StatusCode sc, | |||
194 | return NULL; | 194 | return NULL; |
195 | 195 | ||
196 | response = MHD_response_from_callback (sc, | 196 | response = MHD_response_from_callback (sc, |
197 | size, | 197 | size, |
198 | MHD_FILE_READ_BLOCK_SIZE, | 198 | MHD_FILE_READ_BLOCK_SIZE, |
199 | &file_reader, | 199 | &file_reader, |
200 | NULL, | 200 | NULL, |
201 | &free_callback); | 201 | &free_callback); |
202 | if (NULL == response) | 202 | if (NULL == response) |
203 | return NULL; | 203 | return NULL; |
204 | response->fd = fd; | 204 | response->fd = fd; |
@@ -208,4 +208,3 @@ MHD_response_from_fd (enum MHD_HTTP_StatusCode sc, | |||
208 | } | 208 | } |
209 | 209 | ||
210 | /* end of response_from_fd.c */ | 210 | /* end of response_from_fd.c */ |
211 | |||