Fun 0.41.5
The programming language that makes you have fun!
Loading...
Searching...
No Matches
curl.c
Go to the documentation of this file.
1/*
2 * This file is part of the Fun programming language.
3 * https://fun-lang.xyz/
4 *
5 * Copyright 2025 Johannes Findeisen <you@hanez.org>
6 * Licensed under the terms of the Apache-2.0 license.
7 * https://opensource.org/license/apache-2-0
8 */
9
10/**
11 * @file curl.c
12 * @brief libcurl helpers and buffers used by HTTP-related VM opcodes.
13 *
14 * Declares small buffer helpers and libcurl write callbacks that support
15 * network-related opcodes when FUN_WITH_CURL is enabled.
16 */
17
18/* Ensure libcurl headers and helpers are defined at file scope (not inside vm_run) */
19#ifdef FUN_WITH_CURL
20#include <curl/curl.h>
21
22/**
23 * @brief Simple growable buffer for libcurl write callbacks.
24 *
25 * The buffer grows via realloc as more data arrives. The content is kept
26 * NUL-terminated for convenience.
27 */
28typedef struct {
29 char *d; /**< Data pointer (NUL-terminated). */
30 size_t n; /**< Number of bytes stored (excluding NUL). */
32
33/**
34 * @brief libcurl write callback that appends data to a FunCurlBuf.
35 *
36 * Reallocates the destination buffer as needed and keeps it NUL-terminated.
37 *
38 * @param ptr Pointer to incoming data block provided by libcurl.
39 * @param sz Size of each data element.
40 * @param nm Number of elements in this block.
41 * @param ud User data; must be a FunCurlBuf*.
42 * @return Number of bytes actually handled (sz*nm) on success, 0 on failure
43 * to signal an error to libcurl.
44 */
45static size_t fun_curl_write_cb(void *ptr, size_t sz, size_t nm, void *ud) {
46 size_t add = sz * nm;
47 FunCurlBuf *b = (FunCurlBuf *)ud;
48 char *p = (char *)realloc(b->d, b->n + add + 1);
49 if (!p) return 0;
50 memcpy(p + b->n, ptr, add);
51 b->d = p;
52 b->n += add;
53 b->d[b->n] = '\0';
54 return add;
55}
56
57/**
58 * @brief libcurl write callback that writes directly to a FILE*.
59 *
60 * @param ptr Pointer to incoming data.
61 * @param sz Size of each element.
62 * @param nm Number of elements.
63 * @param ud User data; must be a FILE* opened for writing in binary mode.
64 * @return Number of elements written (as returned by fwrite).
65 */
66static size_t fun_curl_file_write_cb(void *ptr, size_t sz, size_t nm, void *ud) {
67 FILE *f = (FILE *)ud;
68 return fwrite(ptr, sz, nm, f);
69}
70#endif
uint32_t b
Definition band.c:32
static size_t fun_curl_write_cb(void *ptr, size_t sz, size_t nm, void *ud)
libcurl write callback that appends data to a FunCurlBuf.
Definition curl.c:45
static size_t fun_curl_file_write_cb(void *ptr, size_t sz, size_t nm, void *ud)
libcurl write callback that writes directly to a FILE*.
Definition curl.c:66
FILE * f
Definition read_file.c:38
const char * p
Definition read_file.c:37
long sz
Definition read_file.c:50
Simple growable buffer for libcurl write callbacks.
Definition curl.c:28
size_t n
Definition curl.c:30
char * d
Definition curl.c:29