From 6f17053e6c75e5c44eaee9d4da0a4c80470274d0 Mon Sep 17 00:00:00 2001 From: Marton Balint Date: Tue, 26 Aug 2025 22:57:40 +0200 Subject: [PATCH] avformat/urldecode: add ff_urldecode_len function This will be used later to decode partial strings. Signed-off-by: Marton Balint --- libavformat/urldecode.c | 18 ++++++++++++++++++ libavformat/urldecode.h | 18 ++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/libavformat/urldecode.c b/libavformat/urldecode.c index e7fa27b3fa..fdaa41784f 100644 --- a/libavformat/urldecode.c +++ b/libavformat/urldecode.c @@ -28,6 +28,8 @@ #include +#include "libavutil/error.h" +#include "libavutil/macros.h" #include "libavutil/mem.h" #include "libavutil/avstring.h" #include "urldecode.h" @@ -93,3 +95,19 @@ char *ff_urldecode(const char *url, int decode_plus_sign) return dest; } + +int ff_urldecode_len(char *dest, size_t dest_len, const char *url, size_t url_max_len, int decode_plus_sign) +{ + size_t written_bytes; + size_t url_len = strlen(url); + + url_len = FFMIN(url_len, url_max_len); + + if (dest_len <= url_len) + return AVERROR(EINVAL); + + written_bytes = urldecode(dest, url, url_len, decode_plus_sign); + dest[written_bytes] = '\0'; + + return written_bytes; +} diff --git a/libavformat/urldecode.h b/libavformat/urldecode.h index 80b11c3428..246d9c9fda 100644 --- a/libavformat/urldecode.h +++ b/libavformat/urldecode.h @@ -19,6 +19,8 @@ #ifndef AVFORMAT_URLDECODE_H #define AVFORMAT_URLDECODE_H +#include + /** * Decodes an URL from its percent-encoded form back into normal * representation. This function returns the decoded URL in a string. @@ -33,4 +35,20 @@ */ char *ff_urldecode(const char *url, int decode_plus_sign); +/** + * Decodes an URL from its percent-encoded form back into normal + * representation. This function returns the decoded URL in a string. + * The URL to be decoded does not necessarily have to be encoded but + * in that case the original string is duplicated. + * + * @param dest the destination buffer. + * @param dest_len the maximum available space in the destination buffer. + * Must be bigger than FFMIN(strlen(url), url_max_len) to avoid + * an AVERROR(EINVAL) result + * @param url_max_len the maximum number of chars to read from url + * @param decode_plus_sign if nonzero plus sign is decoded to space + * @return the number of written bytes to dest excluding the zero terminator, + * negative on error + */ +int ff_urldecode_len(char *dest, size_t dest_len, const char *url, size_t url_max_len, int decode_plus_sign); #endif /* AVFORMAT_URLDECODE_H */