libmicrohttpd2

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

mhd_read_file.c (5902B)


      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_read_file.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  * Read data from the file to the provided buffer
     64  *
     65  * @param file_fd the FD of file to read
     66  * @param buf_size the size of the @a buf buffer
     67  * @param[out] buf the buffer to fill with the read data
     68  * @param[out] size_filled the pointer to variable to get the size of the data
     69  *                         actually put to the @a buffer
     70  * @return #mhd_FILE_READ_OK if succeed (the @a size_filled gets the actual
     71  *         read size),
     72  *         error otherwise
     73  */
     74 MHD_INTERNAL MHD_FN_MUST_CHECK_RESULT_ MHD_FN_PAR_NONNULL_ALL_
     75 MHD_FN_PAR_OUT_SIZE_ (4, 3) MHD_FN_PAR_OUT_ (5) enum mhd_FileReadResult
     76 mhd_read_file (int file_fd,
     77                uint_fast64_t offset,
     78                size_t buf_size,
     79                char buf[MHD_FN_PAR_DYN_ARR_SIZE_ (buf_size)],
     80                size_t *restrict size_filled)
     81 {
     82 #if (defined(HAVE_PREAD64) || defined(HAVE_PREAD)) && ! defined(mhd_W32_NATIVE)
     83 #  ifdef HAVE_PREAD64
     84   const off64_t pos_off = (off64_t) offset;
     85 #  else  /* HAVE_PREAD */
     86   const off_t pos_off = (off_t) offset;
     87 #  endif /* HAVE_PREAD */
     88   ssize_t res;
     89 
     90   *size_filled = 0;
     91 
     92   if ((0 > pos_off) ||
     93       (offset != (uint_fast64_t) pos_off))
     94     return mhd_FILE_READ_OFFSET_TOO_LARGE;
     95 
     96   if (0 > (ssize_t) buf_size)
     97     buf_size = SSIZE_MAX; /* Larger sizes may result in undefined behaviour */
     98 
     99 #  ifdef HAVE_PREAD64
    100   res = pread64 (file_fd,
    101                  buf,
    102                  buf_size,
    103                  pos_off);
    104 #  else  /* HAVE_PREAD */
    105   res = pread (file_fd,
    106                buf,
    107                buf_size,
    108                pos_off);
    109 #  endif /* HAVE_PREAD */
    110 
    111   if (0 > res)
    112     return mhd_FILE_READ_ERROR;
    113 
    114   if (0 == res)
    115     return mhd_FILE_READ_EOF;
    116 
    117   *size_filled = (size_t) res;
    118   return mhd_FILE_READ_OK;
    119 #elif ! defined(mhd_W32_NATIVE)
    120   /* Multithread-unsafe emulation */
    121 #  ifdef HAVE_LSEEK64
    122   const off64_t pos_off = (off64_t) offset;
    123 #  else
    124   const off_t pos_off = (off_t) offset;
    125 #  endif
    126   ssize_t res;
    127 
    128   *size_filled = 0;
    129 
    130   if ((0 > pos_off) ||
    131       (offset != (uint_fast64_t) pos_off))
    132     return mhd_FILE_READ_OFFSET_TOO_LARGE;
    133 
    134   if (0 > (ssize_t) buf_size)
    135     buf_size = SSIZE_MAX; /* Larger sizes may result in undefined behaviour */
    136 
    137 #  ifdef HAVE_LSEEK64
    138   if (pos_off != lseek64 (file_fd,
    139                           pos_off,
    140                           SEEK_SET))
    141     return mhd_FILE_READ_ERROR;
    142 #  else
    143   if (pos_off != lseek (file_fd,
    144                         pos_off,
    145                         SEEK_SET))
    146     return mhd_FILE_READ_ERROR;
    147 #  endif
    148 
    149   res = read (file_fd,
    150               buf,
    151               buf_size);
    152 
    153   if (0 > res)
    154     return mhd_FILE_READ_ERROR;
    155 
    156   if (0 == res)
    157     return mhd_FILE_READ_EOF;
    158 
    159   *size_filled = (size_t) res;
    160   return mhd_FILE_READ_OK;
    161 
    162 #else  /* Native W32 */
    163   const intptr_t sys_fd = _get_osfhandle (file_fd);
    164   const HANDLE w_hndl = (HANDLE) sys_fd;
    165   OVERLAPPED ovrlp;
    166   DWORD reqReadSize;
    167   DWORD resReadSize;
    168 
    169   *size_filled = 0;
    170   if ((((intptr_t) -2) == sys_fd) ||
    171       (INVALID_HANDLE_VALUE == w_hndl))
    172     return mhd_FILE_READ_ERROR;
    173 
    174   memset (&ovrlp, 0, sizeof(ovrlp));
    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     return mhd_FILE_READ_ERROR;
    190 
    191   if (0 == resReadSize)
    192     return mhd_FILE_READ_EOF;
    193 
    194   *size_filled = resReadSize;
    195   return mhd_FILE_READ_OK;
    196 #endif /* Native W32 */
    197 }