xref: /PHP-8.1/main/streams/mmap.c (revision 01b3fc03)
1 /*
2   +----------------------------------------------------------------------+
3   | Copyright (c) The PHP Group                                          |
4   +----------------------------------------------------------------------+
5   | This source file is subject to version 3.01 of the PHP license,      |
6   | that is bundled with this package in the file LICENSE, and is        |
7   | available through the world-wide-web at the following url:           |
8   | https://www.php.net/license/3_01.txt                                 |
9   | If you did not receive a copy of the PHP license and are unable to   |
10   | obtain it through the world-wide-web, please send a note to          |
11   | license@php.net so we can mail you a copy immediately.               |
12   +----------------------------------------------------------------------+
13   | Author: Wez Furlong <wez@thebrainroom.com>                           |
14   +----------------------------------------------------------------------+
15 */
16 
17 /* Memory Mapping interface for streams */
18 #include "php.h"
19 #include "php_streams_int.h"
20 
_php_stream_mmap_range(php_stream * stream,size_t offset,size_t length,php_stream_mmap_access_t mode,size_t * mapped_len)21 PHPAPI char *_php_stream_mmap_range(php_stream *stream, size_t offset, size_t length, php_stream_mmap_access_t mode, size_t *mapped_len)
22 {
23 	php_stream_mmap_range range;
24 
25 	range.offset = offset;
26 	range.length = length;
27 	range.mode = mode;
28 	range.mapped = NULL;
29 
30 	if (PHP_STREAM_OPTION_RETURN_OK == php_stream_set_option(stream, PHP_STREAM_OPTION_MMAP_API, PHP_STREAM_MMAP_MAP_RANGE, &range)) {
31 		if (mapped_len) {
32 			*mapped_len = range.length;
33 		}
34 		return range.mapped;
35 	}
36 	return NULL;
37 }
38 
_php_stream_mmap_unmap(php_stream * stream)39 PHPAPI int _php_stream_mmap_unmap(php_stream *stream)
40 {
41 	return php_stream_set_option(stream, PHP_STREAM_OPTION_MMAP_API, PHP_STREAM_MMAP_UNMAP, NULL) == PHP_STREAM_OPTION_RETURN_OK;
42 }
43 
_php_stream_mmap_unmap_ex(php_stream * stream,zend_off_t readden)44 PHPAPI int _php_stream_mmap_unmap_ex(php_stream *stream, zend_off_t readden)
45 {
46 	int ret = 1;
47 
48 	if (php_stream_seek(stream, readden, SEEK_CUR) != 0) {
49 		ret = 0;
50 	}
51 	if (php_stream_mmap_unmap(stream) == 0) {
52 		ret = 0;
53 	}
54 
55 	return ret;
56 }
57