Debian Codegraph @

src/memcache.c

raw symbols SCIP
1 /*
2 * HTTPDirFS - HTTP Directory Filesystem
3 *
4 * Copyright (C) 2020-2026 Fufu Fang <[email protected]>
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * In addition, as a special exception, the copyright holders give
20 * permission to link the code of portions of this program with the OpenSSL
21 * library.
22 */
23
24 /**
25 * \file memcache.c
26 * \brief Memory cache and data transfer definitions implementation
27 */
28
29 #include "memcache.h"
30
31 #include "cache.h"
32 #include "log.h"
33 #include "util.h"
34
35 #include <stdlib.h>
36 #include <string.h>
37
38 size_t write_memory_callback(void *recv_data, size_t size, size_t nmemb,
39 void *userp)
40 {
41 TransferStruct *ts = (TransferStruct *)userp;
42
43 if (size != 0 && nmemb > (SIZE_MAX - ts->curr_size - 1) / size) {
44 lprintf(fatal, "Response buffer size overflow!\n");
45 }
46 size_t recv_size = size * nmemb;
47
48 if (ts->cache_ptr) {
49 PTHREAD_MUTEX_LOCK(&ts->cache_ptr->dl_lock);
50 }
51
52 void *new_data = REALLOC(ts->data, ts->curr_size + recv_size + 1);
53 ts->data = new_data;
54
55 memmove(&ts->data[ts->curr_size], recv_data, recv_size);
56 ts->curr_size += recv_size;
57 ts->data[ts->curr_size] = '\0';
58
59 if (ts->cache_ptr) {
60 if (ts->ad_ptr) {
61 PTHREAD_COND_BROADCAST(&ts->ad_ptr->cond);
62 }
63 PTHREAD_MUTEX_UNLOCK(&ts->cache_ptr->dl_lock);
64 }
65
66 return recv_size;
67 }