libmicrohttpd2

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

mhd_read_file.c (5433B)


      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 MHD_INTERNAL MHD_FN_MUST_CHECK_RESULT_ MHD_FN_PAR_NONNULL_ALL_
     64 MHD_FN_PAR_OUT_SIZE_ (4, 3) MHD_FN_PAR_OUT_ (5) enum mhd_FileReadResult
     65 mhd_read_file (int file_fd,
     66                uint_fast64_t offset,
     67                size_t buf_size,
     68                char buf[MHD_FN_PAR_DYN_ARR_SIZE_ (buf_size)],
     69                size_t *restrict size_filled)
     70 {
     71 #if (defined(HAVE_PREAD64) || defined(HAVE_PREAD)) && ! defined(mhd_W32_NATIVE)
     72 #  ifdef HAVE_PREAD64
     73   const off64_t pos_off = (off64_t) offset;
     74 #  else  /* HAVE_PREAD */
     75   const off_t pos_off = (off_t) offset;
     76 #  endif /* HAVE_PREAD */
     77   ssize_t res;
     78 
     79   *size_filled = 0;
     80 
     81   if ((0 > pos_off) ||
     82       (offset != (uint_fast64_t) pos_off))
     83     return mhd_FILE_READ_OFFSET_TOO_LARGE;
     84 
     85   if (0 > (ssize_t) buf_size)
     86     buf_size = SSIZE_MAX; /* Larger sizes may result in undefined behaviour */
     87 
     88 #  ifdef HAVE_PREAD64
     89   res = pread64 (file_fd,
     90                  buf,
     91                  buf_size,
     92                  pos_off);
     93 #  else  /* HAVE_PREAD */
     94   res = pread (file_fd,
     95                buf,
     96                buf_size,
     97                pos_off);
     98 #  endif /* HAVE_PREAD */
     99 
    100   if (0 > res)
    101     return mhd_FILE_READ_ERROR;
    102 
    103   if (0 == res)
    104     return mhd_FILE_READ_EOF;
    105 
    106   *size_filled = (size_t) res;
    107   return mhd_FILE_READ_OK;
    108 #elif ! defined(mhd_W32_NATIVE)
    109   /* Multithread-unsafe emulation */
    110 #  ifdef HAVE_LSEEK64
    111   const off64_t pos_off = (off64_t) offset;
    112 #  else
    113   const off_t pos_off = (off_t) offset;
    114 #  endif
    115   ssize_t res;
    116 
    117   *size_filled = 0;
    118 
    119   if ((0 > pos_off) ||
    120       (offset != (uint_fast64_t) pos_off))
    121     return mhd_FILE_READ_OFFSET_TOO_LARGE;
    122 
    123   if (0 > (ssize_t) buf_size)
    124     buf_size = SSIZE_MAX; /* Larger sizes may result in undefined behaviour */
    125 
    126 #  ifdef HAVE_LSEEK64
    127   if (pos_off != lseek64 (file_fd,
    128                           pos_off,
    129                           SEEK_SET))
    130     return mhd_FILE_READ_ERROR;
    131 #  else
    132   if (pos_off != lseek (file_fd,
    133                         pos_off,
    134                         SEEK_SET))
    135     return mhd_FILE_READ_ERROR;
    136 #  endif
    137 
    138   res = read (file_fd,
    139               buf,
    140               buf_size);
    141 
    142   if (0 > res)
    143     return mhd_FILE_READ_ERROR;
    144 
    145   if (0 == res)
    146     return mhd_FILE_READ_EOF;
    147 
    148   *size_filled = (size_t) res;
    149   return mhd_FILE_READ_OK;
    150 
    151 #else  /* Native W32 */
    152   const intptr_t sys_fd = _get_osfhandle (file_fd);
    153   const HANDLE w_hndl = (HANDLE) sys_fd;
    154   OVERLAPPED ovrlp;
    155   DWORD reqReadSize;
    156   DWORD resReadSize;
    157 
    158   *size_filled = 0;
    159   if ((((intptr_t) -2) == sys_fd) ||
    160       (INVALID_HANDLE_VALUE == w_hndl))
    161     return mhd_FILE_READ_ERROR;
    162 
    163   memset (&ovrlp, 0, sizeof(ovrlp));
    164   reqReadSize = (DWORD) buf_size;
    165   if (reqReadSize != buf_size)
    166     reqReadSize = (DWORD) (~((DWORD) 0));
    167   ovrlp.Offset = (DWORD) offset;
    168   offset >>= 32;
    169   ovrlp.OffsetHigh = (DWORD) offset;
    170   if (0 != (offset >> 32))
    171     return mhd_FILE_READ_OFFSET_TOO_LARGE;
    172 
    173   if (! ReadFile (w_hndl,
    174                   buf,
    175                   reqReadSize,
    176                   &resReadSize,
    177                   &ovrlp))
    178     return mhd_FILE_READ_ERROR;
    179 
    180   if (0 == resReadSize)
    181     return mhd_FILE_READ_EOF;
    182 
    183   *size_filled = resReadSize;
    184   return mhd_FILE_READ_OK;
    185 #endif /* Native W32 */
    186 }