Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 18 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rexim/a6636976d12f67ea530ece118a700317 to your computer and use it in GitHub Desktop.
Save rexim/a6636976d12f67ea530ece118a700317 to your computer and use it in GitHub Desktop.
TCC patch that enables including files via HTTPS using CURL
From 84c42091cbae3735c52e895221f3f95a87155756 Mon Sep 17 00:00:00 2001
From: rexim <reximkut@gmail.com>
Date: Wed, 30 Jun 2021 20:43:47 +0700
Subject: [PATCH] Make it possible to include files over https
---
Makefile | 2 +-
tcc.c | 5 +++++
tccpp.c | 43 +++++++++++++++++++++++++++++++++++++++++++
3 files changed, 49 insertions(+), 1 deletion(-)
diff --git a/Makefile b/Makefile
index 3ae466f..98de4ce 100644
--- a/Makefile
+++ b/Makefile
@@ -30,7 +30,7 @@ ifdef CONFIG_WIN32
CFGWIN = -win
NATIVE_TARGET = $(ARCH)-win$(if $(findstring arm,$(ARCH)),ce,32)
else
- LIBS=-lm
+ LIBS=-lm -lcurl
ifneq ($(CONFIG_ldl),no)
LIBS+=-ldl
endif
diff --git a/tcc.c b/tcc.c
index cd887d1..7a5e53f 100644
--- a/tcc.c
+++ b/tcc.c
@@ -23,6 +23,7 @@
# include "libtcc.c"
#endif
#include "tcctools.c"
+#include <curl/curl.h>
static const char help[] =
"Tiny C Compiler "TCC_VERSION" - Copyright (C) 2001-2006 Fabrice Bellard\n"
@@ -257,6 +258,10 @@ redo:
s = tcc_new();
opt = tcc_parse_args(s, &argc, &argv, 1);
+ if (curl_global_init(CURL_GLOBAL_DEFAULT) != 0) {
+ tcc_error("Could not initialize Global cURL context. (Yes, your C compiler is using curl Kappa)");
+ }
+
if ((n | t) == 0) {
if (opt == OPT_HELP)
return printf(help), 1;
diff --git a/tccpp.c b/tccpp.c
index 76f9e42..0b86a41 100644
--- a/tccpp.c
+++ b/tccpp.c
@@ -20,6 +20,8 @@
#include "tcc.h"
+#include <curl/curl.h>
+
/********************************************************/
/* global variables */
@@ -1798,6 +1800,47 @@ ST_FUNC void preprocess(int is_bof)
buf[len - 2] = '\0';
}
+ // Hacked by https://tsoding.github.io/
+ // Available to everyone under Public Domain
+ if (strncmp("https://", buf, 8) == 0) {
+ CURLU *url = curl_url();
+ if (curl_url_set(url, CURLUPART_URL, buf, 0)) {
+ tcc_error("Could not parse the include URL");
+ }
+
+ char *path;
+ if (curl_url_get(url, CURLUPART_PATH, &path, 0)) {
+ tcc_error("Could not extract path from the include URL");
+ }
+
+ const char *file_name = tcc_basename(path);
+
+ CURL *curl = curl_easy_init();
+ if (curl == NULL) {
+ tcc_error("Could not initialize cURL context");
+ }
+
+ FILE *download_file = fopen(file_name, "w");
+ if (download_file == NULL) {
+ tcc_error("Could not open file %s: %s", file_name, strerror(errno));
+ }
+
+ curl_easy_setopt(curl, CURLOPT_URL, buf);
+ curl_easy_setopt(curl, CURLOPT_WRITEDATA, download_file);
+ CURLcode res = curl_easy_perform(curl);
+ if (res != CURLE_OK) {
+ tcc_error("Could not download %s: %s\n", buf, curl_easy_strerror(res));
+ }
+
+ strncpy(buf, file_name, sizeof(buf) - 1);
+
+ curl_free(path);
+ curl_url_cleanup(url);
+ curl_easy_cleanup(curl);
+
+ fclose(download_file);
+ }
+
if (s1->include_stack_ptr >= s1->include_stack + INCLUDE_STACK_SIZE)
tcc_error("#include recursion too deep");
/* store current file in stack, but increment stack later below */
--
2.20.1
@theoparis
Copy link

theoparis commented Oct 19, 2023

It seems like this doesn't work with the latest tcc from https://github.com/TinyCC/tinycc 😕
Is there an updated patch

@rexim
Copy link
Author

rexim commented Oct 19, 2023

Feel free to update it yourself? That's how Open Source works.

@inRm3D
Copy link

inRm3D commented Nov 23, 2023

https://tsoding.org/ is down, better change to https://tsoding.github.io/ ?

@rexim
Copy link
Author

rexim commented Nov 24, 2023

@inRm3D done!

@alexander-vla
Copy link

@rexim
Hi there .
FYI : another approach to SUBJ: To use LD_PRELOAD + dll with hookup open/close/stat libc functions . And replace open() library-call with opened "curl " subprocess file-descriptor .
eg:
$LD_PRELOAD=./libhook_library.so tcc -run mysource_with_https_includes.c

The flow
-: tcc/gcc tries to open() filename "https://xxxxx/pppp.h"
-: library implements open() function -- "see" filename contains "https://" -- and instead of open file , returns file descriptor like : return fileno( popen("curl url ", "r") ).
(other filenames (not contains "https://") - just bypass to original libc open() call )

Example : (tested with tcc/ tcc -run/ gcc / clang ) .
https://github.com/alexander-vla/hook_up_open_curl/blob/main/libinclude_hook.c

@criskell
Copy link

criskell commented Jan 16, 2024

@13m0n4de
Copy link

@theoparis Hi. I have made some changes, and now it should be able to work on the latest TCC.
https://gist.github.com/13m0n4de/84912522cce6db31da069baf1add04f8

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment