From 2c2efaa91fe977cf761e9d6e9b42d2112697d686 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Wed, 14 Aug 2019 11:21:35 +0200 Subject: [PATCH] lib/xiph/VorbisComments: pass struct vorbis_comment instead of char** Use the "comments" attribute instead of relying on the nullptr terminator. --- src/decoder/plugins/VorbisDecoderPlugin.cxx | 12 +++--- src/lib/xiph/VorbisComments.cxx | 46 ++++++++++++++------- src/lib/xiph/VorbisComments.hxx | 8 ++-- 3 files changed, 43 insertions(+), 23 deletions(-) diff --git a/src/decoder/plugins/VorbisDecoderPlugin.cxx b/src/decoder/plugins/VorbisDecoderPlugin.cxx index dfb38c2b5..28ee8594f 100644 --- a/src/decoder/plugins/VorbisDecoderPlugin.cxx +++ b/src/decoder/plugins/VorbisDecoderPlugin.cxx @@ -157,10 +157,10 @@ VorbisDecoder::OnOggBeginning(const ogg_packet &_packet) } static void -vorbis_send_comments(DecoderClient &client, InputStream &is, - char **comments) +SubmitVorbisComment(DecoderClient &client, InputStream &is, + const vorbis_comment &vc) { - auto tag = vorbis_comments_to_tag(comments); + auto tag = VorbisCommentToTag(vc); if (!tag) return; @@ -269,10 +269,10 @@ VorbisDecoder::OnOggPacket(const ogg_packet &_packet) } else SubmitInit(); - vorbis_send_comments(client, input_stream, vc.user_comments); + SubmitVorbisComment(client, input_stream, vc); ReplayGainInfo rgi; - if (vorbis_comments_to_replay_gain(rgi, vc.user_comments)) + if (VorbisCommentToReplayGain(rgi, vc)) client.SubmitReplayGain(&rgi); } else { if (!dsp_initialized) { @@ -404,7 +404,7 @@ vorbis_scan_stream(InputStream &is, TagHandler &handler) noexcept /* visit the Vorbis comments we just read */ - vorbis_comments_scan(vc.user_comments, handler); + VorbisCommentScan(vc, handler); /* check the song duration by locating the e_o_s packet */ diff --git a/src/lib/xiph/VorbisComments.cxx b/src/lib/xiph/VorbisComments.cxx index d2315e708..f170b7462 100644 --- a/src/lib/xiph/VorbisComments.cxx +++ b/src/lib/xiph/VorbisComments.cxx @@ -27,20 +27,38 @@ #include "tag/ReplayGain.hxx" #include "ReplayGainInfo.hxx" #include "util/StringView.hxx" +#include "config.h" + +#ifndef HAVE_TREMOR +#include +#else +#include +#endif /* HAVE_TREMOR */ + +template +static void +ForEachUserComment(const vorbis_comment &vc, F &&f) +{ + const char *const*const user_comments = vc.user_comments; + const int*const comment_lengths = vc.comment_lengths; + + const size_t n = vc.comments; + for (size_t i = 0; i < n; ++i) + f(StringView{user_comments[i], size_t(comment_lengths[i])}); +} bool -vorbis_comments_to_replay_gain(ReplayGainInfo &rgi, char **comments) noexcept +VorbisCommentToReplayGain(ReplayGainInfo &rgi, + const vorbis_comment &vc) noexcept { rgi.Clear(); bool found = false; - while (*comments) { - if (ParseReplayGainVorbis(rgi, *comments)) + ForEachUserComment(vc, [&](StringView s){ + if (ParseReplayGainVorbis(rgi, s.data)) found = true; - - comments++; - } + }); return found; } @@ -64,10 +82,10 @@ vorbis_copy_comment(StringView comment, } static void -vorbis_scan_comment(const char *comment, TagHandler &handler) noexcept +vorbis_scan_comment(StringView comment, TagHandler &handler) noexcept { if (handler.WantPair()) { - const auto split = StringView(comment).Split('='); + const auto split = comment.Split('='); if (!split.first.empty() && !split.second.IsNull()) handler.OnPair(split.first, split.second); } @@ -85,19 +103,19 @@ vorbis_scan_comment(const char *comment, TagHandler &handler) noexcept } void -vorbis_comments_scan(char **comments, TagHandler &handler) noexcept +VorbisCommentScan(const vorbis_comment &vc, TagHandler &handler) noexcept { - while (*comments) - vorbis_scan_comment(*comments++, handler); - + ForEachUserComment(vc, [&](StringView s){ + vorbis_scan_comment(s, handler); + }); } std::unique_ptr -vorbis_comments_to_tag(char **comments) noexcept +VorbisCommentToTag(const vorbis_comment &vc) noexcept { TagBuilder tag_builder; AddTagHandler h(tag_builder); - vorbis_comments_scan(comments, h); + VorbisCommentScan(vc, h); return tag_builder.empty() ? nullptr : tag_builder.CommitNew(); diff --git a/src/lib/xiph/VorbisComments.hxx b/src/lib/xiph/VorbisComments.hxx index 6a82a351e..5287d3376 100644 --- a/src/lib/xiph/VorbisComments.hxx +++ b/src/lib/xiph/VorbisComments.hxx @@ -22,17 +22,19 @@ #include +struct vorbis_comment; struct ReplayGainInfo; class TagHandler; struct Tag; bool -vorbis_comments_to_replay_gain(ReplayGainInfo &rgi, char **comments) noexcept; +VorbisCommentToReplayGain(ReplayGainInfo &rgi, + const vorbis_comment &vc) noexcept; void -vorbis_comments_scan(char **comments, TagHandler &handler) noexcept; +VorbisCommentScan(const vorbis_comment &vc, TagHandler &handler) noexcept; std::unique_ptr -vorbis_comments_to_tag(char **comments) noexcept; +VorbisCommentToTag(const vorbis_comment &vc) noexcept; #endif