From 60664bc00b8ba18c514e9194399aa753e795fdd7 Mon Sep 17 00:00:00 2001 From: wm4 Date: Wed, 20 Apr 2016 18:00:20 +0200 Subject: stream_memory: add hex:// protocol Completely useless, expect for some special purposes. --- DOCS/man/mpv.rst | 3 +++ stream/stream_memory.c | 37 +++++++++++++++++++++++++++++++++++-- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/DOCS/man/mpv.rst b/DOCS/man/mpv.rst index ea0681c5ae..bdde369672 100644 --- a/DOCS/man/mpv.rst +++ b/DOCS/man/mpv.rst @@ -715,6 +715,9 @@ PROTOCOLS ``memory://data`` Use the ``data`` part as source data. +``hex://data`` + Like ``memory://``, but the string is interpreted as hexdump. + PSEUDO GUI MODE =============== diff --git a/stream/stream_memory.c b/stream/stream_memory.c index 4bcb860c49..84947b5aeb 100644 --- a/stream/stream_memory.c +++ b/stream/stream_memory.c @@ -55,6 +55,32 @@ static int control(stream_t *s, int cmd, void *arg) return STREAM_UNSUPPORTED; } +static int h_to_i(unsigned char c) +{ + if (c >= '0' && c <= '9') + return c - '0'; + if (c >= 'a' && c <= 'f') + return c - 'a' + 10; + if (c >= 'A' && c <= 'F') + return c - 'A' + 10; + return -1; +} + +static bool bstr_to_hex_inplace(bstr *h) +{ + if (h->len % 2) + return false; + for (int n = 0; n < h->len / 2; n++) { + int hi = h_to_i(h->start[n * 2 + 0]); + int lo = h_to_i(h->start[n * 2 + 1]); + if (hi < 0 || lo < 0) + return false; + h->start[n] = (hi << 4) | lo; + } + h->len /= 2; + return true; +} + static int open_f(stream_t *stream) { stream->fill_buffer = fill_buffer; @@ -68,14 +94,21 @@ static int open_f(stream_t *stream) // Initial data bstr data = bstr0(stream->url); - bstr_eatstart0(&data, "memory://"); + bool use_hex = bstr_eatstart0(&data, "hex://"); + if (!use_hex) + bstr_eatstart0(&data, "memory://"); stream_control(stream, STREAM_CTRL_SET_CONTENTS, &data); + if (use_hex && !bstr_to_hex_inplace(&p->data)) { + MP_FATAL(stream, "Invalid data.\n"); + return STREAM_ERROR; + } + return STREAM_OK; } const stream_info_t stream_info_memory = { .name = "memory", .open = open_f, - .protocols = (const char*const[]){ "memory", NULL }, + .protocols = (const char*const[]){ "memory", "hex", NULL }, }; -- cgit v1.2.3