#include "libavutil/attributes.h"
#include "libavutil/avassert.h"
#include "libavutil/avstring.h"
#include "libavutil/crc.h"
#include "libavutil/hash.h"
#include "libavutil/file_open.h"
#include "libavutil/mem.h"
#include "libavutil/opt.h"
#include "libavutil/time.h"
#include "url.h"
#include <errno.h>
#include <fcntl.h>
#include <stdatomic.h>
#include <sys/file.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
Go to the source code of this file.
|
| static int | hash_uri (uint8_t hash[HASH_SIZE], const char *uri) |
| |
| static uint16_t | get_block_crc (const uint8_t *block, size_t block_size) |
| |
| static int | shared_close (URLContext *h) |
| |
| static int | cache_map (URLContext *h, int64_t filesize) |
| |
| static int | spacemap_init (URLContext *h, const uint8_t hash[HASH_SIZE]) |
| |
| static int | spacemap_grow (URLContext *h, int64_t block) |
| |
| static int64_t | get_filesize (URLContext *h) |
| |
| static int | set_filesize (URLContext *h, int64_t new_size) |
| |
| static int | shared_open (URLContext *h, const char *arg, int flags, AVDictionary **options) |
| |
| static int | spacemap_remap (URLContext *h, size_t map_size) |
| |
| static int | read_cache (SharedContext *s, uint8_t *buf, size_t size, off_t offset) |
| |
| static int | write_cache (SharedContext *s, const uint8_t *buf, size_t size, off_t offset) |
| |
| static size_t | clamp_size (URLContext *h, size_t size, int64_t pos) |
| |
| static int | shared_read (URLContext *h, unsigned char *buf, int size) |
| |
| static int64_t | shared_seek (URLContext *h, int64_t pos, int whence) |
| |
| static int | shared_get_file_handle (URLContext *h) |
| |
| static int | shared_get_short_seek (URLContext *h) |
| |
◆ HASH_METHOD
| #define HASH_METHOD "SHA512/256" |
This hash should be resistant against collision attacks, so that an attacker could not generate e.g.
two different URIs that map to the same cache file. This requires at least 64 bits of collision resistance in practice (i.e. 128 bits = 16 bytes of hash size). However, we can be conservative by computing e.g. a 256 bit hash and storing it inside the file header for verification.
Note that due to the way we use atomics, we should avoid zero bytes in the resulting hash; hence we tweak the input slightly to avoid this. The resulting loss in hash strength is negligible, since 32 bytes is already much more than needed.
Definition at line 57 of file shared.c.
◆ HASH_SIZE
◆ HEADER_MAGIC
| #define HEADER_MAGIC MKTAG(u'\xFF', 'S', 'h', '$') |
◆ HEADER_VERSION
◆ DEF_SET_ONCE
| #define DEF_SET_ONCE |
( |
|
ctype, |
|
|
|
atype |
|
) |
| |
Value: static int set_once_##atype(atomic_##atype *
const ptr,
const ctype value) \
{ \
ctype prev = 0; \
av_assert1(
value != 0); \
ptr, &prev,
value, memory_order_release, memory_order_relaxed)) \
return 1; \
else
if (prev ==
value) \
return 0; \
else \
}
Definition at line 122 of file shared.c.
◆ OFFSET
◆ BlockState
| Enumerator |
|---|
| BLOCK_NONE | block is not cached
|
| BLOCK_PENDING | a thread is currently trying to write this block
|
| BLOCK_FAILED | the underlying I/O source failed to read this block
|
Definition at line 81 of file shared.c.
◆ hash_uri()
| static int hash_uri |
( |
uint8_t |
hash[HASH_SIZE], |
|
|
const char * |
uri |
|
) |
| |
|
static |
◆ get_block_crc()
| static uint16_t get_block_crc |
( |
const uint8_t * |
block, |
|
|
size_t |
block_size |
|
) |
| |
|
static |
◆ shared_close()
◆ cache_map()
◆ spacemap_init()
| static int spacemap_init |
( |
URLContext * |
h, |
|
|
const uint8_t |
hash[HASH_SIZE] |
|
) |
| |
|
static |
◆ spacemap_grow()
◆ get_filesize()
◆ set_filesize()
◆ shared_open()
◆ spacemap_remap()
| static int spacemap_remap |
( |
URLContext * |
h, |
|
|
size_t |
map_size |
|
) |
| |
|
static |
◆ read_cache()
| static int read_cache |
( |
SharedContext * |
s, |
|
|
uint8_t * |
buf, |
|
|
size_t |
size, |
|
|
off_t |
offset |
|
) |
| |
|
static |
◆ write_cache()
| static int write_cache |
( |
SharedContext * |
s, |
|
|
const uint8_t * |
buf, |
|
|
size_t |
size, |
|
|
off_t |
offset |
|
) |
| |
|
static |
◆ clamp_size()
◆ shared_read()
| static int shared_read |
( |
URLContext * |
h, |
|
|
unsigned char * |
buf, |
|
|
int |
size |
|
) |
| |
|
static |
◆ shared_seek()
◆ shared_get_file_handle()
| static int shared_get_file_handle |
( |
URLContext * |
h | ) |
|
|
static |
◆ shared_get_short_seek()
| static int shared_get_short_seek |
( |
URLContext * |
h | ) |
|
|
static |
◆ options
Initial value:= {
{
"block_shift",
"Set the base 2 logarithm of the block size",
OFFSET(block_shift),
AV_OPT_TYPE_INT, {.i64 = 15}, 9, 30, .flags =
D },
{
"read_only",
"Don't write data to the cache, only read from it",
OFFSET(read_only),
AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, .flags =
D },
{
"cache_verify",
"Verify correctness of the cache against the source",
OFFSET(verify),
AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, .flags =
D },
{
"cache_timeout",
"Time in us to wait before re-fetching pending blocks",
OFFSET(timeout),
AV_OPT_TYPE_INT64, {.i64 = 0}, 0, INT64_MAX, .flags =
D },
{
"retry_errors",
"Re-request blocks even if they previously failed",
OFFSET(retry_errors),
AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, .flags =
D },
{0},
}
Definition at line 855 of file shared.c.
◆ shared_context_class
Initial value:= {
.class_name = "shared",
}
Definition at line 865 of file shared.c.
◆ ff_shared_protocol
Initial value:
Definition at line 872 of file shared.c.