dev-python/conda-libmamba-solver: bump version to 24.11.1
[gentoo-zh.git] / media-sound / netease-cloud-music / files / patch.c
blobf34e989190cd7c2ab69459ee136ed42cc57a0163
1 // SPDX-License-Identifier: BSD-3-Clause
2 /**
3 * Copyright (c) 2021 kXuan. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
6 * following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
9 * disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
11 * following disclaimer in the documentation and/or other materials provided with the distribution.
12 * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
13 * products derived from this software without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
16 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
18 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
20 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
21 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 #define _GNU_SOURCE
26 #include <dlfcn.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <strings.h>
30 #include <unistd.h>
31 #include <vlc/vlc.h>
32 #include <vlc/plugins/vlc_common.h>
33 #include <vlc/plugins/vlc_stream.h>
35 static void *load_sym(void *override_func, const char *name)
37 void *ptr = dlsym(RTLD_NEXT, name);
38 if (ptr == NULL) {
39 fprintf(stderr, "Cannot load symbol '%s' %s. Please report bug on AUR.\n", name, dlerror());
40 exit(1);
42 if (ptr == override_func) {
43 fprintf(stderr, "circular reference '%s'. Please report bug on AUR.\n", name);
44 exit(1);
46 return ptr;
49 static int is_flac(const char *url)
51 static const char suffix[] = ".flac";
52 if (!url) {
53 return 0;
55 size_t len = strlen(url);
56 if (len < sizeof(suffix)) {
57 return 0;
60 return strcasecmp(url + len - sizeof(suffix) + 1, suffix) == 0;
63 /**
64 * The netease cloud music server set the Content-Type of .flac format to mpeg/audio, which results vlc unable to decode
65 * the music and play.
67 * To make vlc works correctly, we rewrite the Content-Type to audio/flac.
69 * @param s
70 * @param query
71 * @param args
72 * @return
74 int vlc_stream_vaControl(stream_t *s, int query, va_list args)
76 static typeof(vlc_stream_vaControl) *orig_fn;
77 if (orig_fn == NULL) {
78 orig_fn = load_sym(vlc_stream_vaControl, __func__);
80 if (query == STREAM_GET_CONTENT_TYPE && is_flac(s->psz_url)) {
81 *va_arg(args, char **) = strdup("audio/flac");
82 return VLC_SUCCESS;
83 } else {
84 return orig_fn(s, query, args);
88 struct string_with_len {
89 const char *s;
90 size_t len;
92 #define STRING_WITH_LEN_INIT(s) {s, sizeof(s)-1}
94 /**
95 * drop all library fixes.
97 * Library fixes should only be applied to the netease-cloud-music main process. Sometime, netease-cloud-music execute
98 * external program, such as `xdg-open`, `kde-open5`. Those external programs use newer version qt and other new
99 * libraries. If we don't drop these environment variables, those programs may not work, because some symbol may not
100 * exist in the bundled old library.
102 * @param path
103 * @param argv
104 * @param envp
105 * @return
107 int execve(const char *path, char *const argv[], char *const envp[])
109 static typeof(execve) *orig_fn;
110 if (orig_fn == NULL) {
111 orig_fn = load_sym(execve, "execve");
114 static struct string_with_len drop_env[] = {
115 STRING_WITH_LEN_INIT("LD_LIBRARY_PATH="),
116 STRING_WITH_LEN_INIT("LD_PRELOAD="),
117 STRING_WITH_LEN_INIT("QT_PLUGIN_PATH="),
118 STRING_WITH_LEN_INIT("QT_QPA_PLATFORM_PLUGIN_PATH="),
119 STRING_WITH_LEN_INIT("QT_QPA_PLATFORM="),
122 size_t nenv;
123 for (nenv = 0; envp[nenv]; nenv++);
125 char *new_envp[nenv + 1];
126 char *const *src = envp;
127 char **dst = new_envp;
129 while (*src) {
130 for (int i = 0; i < sizeof(drop_env) / sizeof(*drop_env); ++i) {
131 if (strncmp(drop_env[i].s, *src, drop_env[i].len) == 0) {
132 goto next_env;
135 *dst = *src;
136 ++dst;
137 next_env:
138 ++src;
141 *dst = NULL;
142 return orig_fn(path, argv, new_envp);