libmicrohttpd2

HTTP server C library (MHD 2.x, alpha)
Log | Files | Refs | README | LICENSE

mhd_read_file.c (5684B)


      1 /* SPDX-License-Identifier: LGPL-2.1-or-later OR (GPL-2.0-or-later WITH eCos-exception-2.0) */
      2 /*
      3   This file is part of GNU libmicrohttpd.
      4   Copyright (C) 2024 Evgeny Grin (Karlson2k)
      5 
      6   GNU libmicrohttpd is free software; you can redistribute it and/or
      7   modify it under the terms of the GNU Lesser General Public
      8   License as published by the Free Software Foundation; either
      9   version 2.1 of the License, or (at your option) any later version.
     10 
     11   GNU libmicrohttpd is distributed in the hope that it will be useful,
     12   but WITHOUT ANY WARRANTY; without even the implied warranty of
     13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     14   Lesser General Public License for more details.
     15 
     16   Alternatively, you can redistribute GNU libmicrohttpd and/or
     17   modify it under the terms of the GNU General Public License as
     18   published by the Free Software Foundation; either version 2 of
     19   the License, or (at your option) any later version, together
     20   with the eCos exception, as follows:
     21 
     22     As a special exception, if other files instantiate templates or
     23     use macros or inline functions from this file, or you compile this
     24     file and link it with other works to produce a work based on this
     25     file, this file does not by itself cause the resulting work to be
     26     covered by the GNU General Public License. However the source code
     27     for this file must still be made available in accordance with
     28     section (3) of the GNU General Public License v2.
     29 
     30     This exception does not invalidate any other reasons why a work
     31     based on this file might be covered by the GNU General Public
     32     License.
     33 
     34   You should have received copies of the GNU Lesser General Public
     35   License and the GNU General Public License along with this library;
     36   if not, see <https://www.gnu.org/licenses/>.
     37 */
     38 
     39 /**
     40  * @file src/mhd2/mhd_read_file.c
     41  * @brief  The implementation of mhd_read_file() function
     42  * @author Karlson2k (Evgeny Grin)
     43  */
     44 
     45 #include "mhd_sys_options.h"
     46 
     47 #include "mhd_assert.h"
     48 
     49 #include <stdlib.h>
     50 #ifdef HAVE_UNISTD_H
     51 #  include <unistd.h>
     52 #endif
     53 
     54 #if !defined(mhd_W32_NATIVE)
     55 #  include "mhd_limits.h"
     56 #else
     57 /* Native W32 */
     58 #  include <windows.h>
     59 #  include <string.h> /* for memset() */
     60 #  include <io.h> /* for _get_osfhandle() */
     61 #endif
     62 
     63 #include "mhd_read_file.h"
     64 
     65 MHD_INTERNAL MHD_FN_MUST_CHECK_RESULT_ MHD_FN_PAR_NONNULL_ALL_
     66 MHD_FN_PAR_OUT_SIZE_ (4, 3) MHD_FN_PAR_OUT_ (5) enum mhd_FileReadResult
     67 mhd_read_file (int file_fd,
     68                uint_fast64_t offset,
     69                size_t buf_size,
     70                char buf[MHD_FN_PAR_DYN_ARR_SIZE_ (buf_size)],
     71                size_t *restrict size_filled)
     72 {
     73 #if (defined(HAVE_PREAD64) || defined(HAVE_PREAD)) && !defined(mhd_W32_NATIVE)
     74 #  ifdef HAVE_PREAD64
     75   const off64_t pos_off = (off64_t)offset;
     76 #  else  /* HAVE_PREAD */
     77   const off_t pos_off = (off_t)offset;
     78 #  endif /* HAVE_PREAD */
     79   ssize_t res;
     80 
     81   mhd_assert (0u != buf_size);
     82 
     83   *size_filled = 0;
     84 
     85   if ((0 > pos_off)
     86       || (offset != (uint_fast64_t)pos_off))
     87     return mhd_FILE_READ_OFFSET_TOO_LARGE;
     88 
     89   if (0 > (ssize_t)buf_size)
     90     buf_size = SSIZE_MAX; /* Larger sizes may result in undefined behaviour */
     91 
     92 #  ifdef HAVE_PREAD64
     93   res = pread64 (file_fd,
     94                  buf,
     95                  buf_size,
     96                  pos_off);
     97 #  else  /* HAVE_PREAD */
     98   res = pread (file_fd,
     99                buf,
    100                buf_size,
    101                pos_off);
    102 #  endif /* HAVE_PREAD */
    103 
    104   if (0 > res)
    105     return mhd_FILE_READ_ERROR;
    106 
    107   if (0 == res)
    108     return mhd_FILE_READ_EOF;
    109 
    110   *size_filled = (size_t)res;
    111   return mhd_FILE_READ_OK;
    112 #elif !defined(mhd_W32_NATIVE)
    113   /* Multithread-unsafe emulation */
    114 #  ifdef HAVE_LSEEK64
    115   const off64_t pos_off = (off64_t)offset;
    116 #  else
    117   const off_t pos_off = (off_t)offset;
    118 #  endif
    119   ssize_t res;
    120 
    121   mhd_assert (0u != buf_size);
    122 
    123   *size_filled = 0;
    124 
    125   if ((0 > pos_off)
    126       || (offset != (uint_fast64_t)pos_off))
    127     return mhd_FILE_READ_OFFSET_TOO_LARGE;
    128 
    129   if (0 > (ssize_t)buf_size)
    130     buf_size = SSIZE_MAX; /* Larger sizes may result in undefined behaviour */
    131 
    132 #  ifdef HAVE_LSEEK64
    133   if (pos_off != lseek64 (file_fd,
    134                           pos_off,
    135                           SEEK_SET))
    136     return mhd_FILE_READ_ERROR;
    137 #  else
    138   if (pos_off != lseek (file_fd,
    139                         pos_off,
    140                         SEEK_SET))
    141     return mhd_FILE_READ_ERROR;
    142 #  endif
    143 
    144   res = read (file_fd,
    145               buf,
    146               buf_size);
    147 
    148   if (0 > res)
    149     return mhd_FILE_READ_ERROR;
    150 
    151   if (0 == res)
    152     return mhd_FILE_READ_EOF;
    153 
    154   *size_filled = (size_t)res;
    155   return mhd_FILE_READ_OK;
    156 
    157 #else  /* Native W32 */
    158   const intptr_t sys_fd = _get_osfhandle (file_fd);
    159   const HANDLE w_hndl = (HANDLE)sys_fd;
    160   OVERLAPPED ovrlp;
    161   DWORD reqReadSize;
    162   DWORD resReadSize;
    163 
    164   mhd_assert (0u != buf_size);
    165 
    166   *size_filled = 0;
    167   if ((((intptr_t)-2) == sys_fd)
    168       || (INVALID_HANDLE_VALUE == w_hndl))
    169     return mhd_FILE_READ_ERROR;
    170 
    171   memset (&ovrlp, 0, sizeof(ovrlp));
    172 #  ifndef HAVE_NULL_PTR_ALL_ZEROS
    173   ovrlp.hEvent = NULL;
    174 #  endif /* ! HAVE_NULL_PTR_ALL_ZEROS */
    175   reqReadSize = (DWORD)buf_size;
    176   if (reqReadSize != buf_size)
    177     reqReadSize = (DWORD)(~((DWORD)0));
    178   ovrlp.Offset = (DWORD)offset;
    179   offset >>= 32;
    180   ovrlp.OffsetHigh = (DWORD)offset;
    181   if (0 != (offset >> 32))
    182     return mhd_FILE_READ_OFFSET_TOO_LARGE;
    183 
    184   if (!ReadFile (w_hndl,
    185                  buf,
    186                  reqReadSize,
    187                  &resReadSize,
    188                  &ovrlp))
    189   {
    190     return (ERROR_HANDLE_EOF == GetLastError ()) ?
    191            mhd_FILE_READ_EOF : mhd_FILE_READ_ERROR;
    192   }
    193   mhd_assert (0 != resReadSize);
    194 
    195   *size_filled = resReadSize;
    196   return mhd_FILE_READ_OK;
    197 #endif /* Native W32 */
    198 }