mirror of https://github.com/oxen-io/lokinet
replace custom bootstrap with curl to match all other platforms
remove dead codepull/825/head
parent
98284ad6e6
commit
9ca2c35824
@ -1,9 +0,0 @@
|
|||||||
*.o
|
|
||||||
mbedtls/
|
|
||||||
*.a
|
|
||||||
*.dll
|
|
||||||
*.so
|
|
||||||
*.exe
|
|
||||||
cacert.pem
|
|
||||||
*.enc
|
|
||||||
*.bin*
|
|
@ -1,17 +0,0 @@
|
|||||||
Copyright (c)2018-2019 Rick V. All rights reserved.
|
|
||||||
|
|
||||||
This software is provided 'as-is', without any express or implied
|
|
||||||
warranty. In no event will the authors be held liable for any damages
|
|
||||||
arising from the use of this software.
|
|
||||||
|
|
||||||
Permission is granted to anyone to use this software for any purpose,
|
|
||||||
including commercial applications, and to alter it and redistribute it
|
|
||||||
freely, subject to the following restrictions:
|
|
||||||
|
|
||||||
1. The origin of this software must not be misrepresented; you must not
|
|
||||||
claim that you wrote the original software. If you use this software
|
|
||||||
in a product, an acknowledgment in the product documentation would be
|
|
||||||
appreciated but is not required.
|
|
||||||
2. Altered source versions must be plainly marked as such, and must not be
|
|
||||||
misrepresented as being the original software.
|
|
||||||
3. This notice may not be removed or altered from any source distribution.
|
|
@ -1,62 +0,0 @@
|
|||||||
# makefile for libhttp
|
|
||||||
# requires mbedtls to be installed somewhere, for both host and target systems
|
|
||||||
# requires wget to be installed for ca bundle download
|
|
||||||
|
|
||||||
# to build:
|
|
||||||
# make prepare; make libhttp
|
|
||||||
|
|
||||||
# set this beforehand if you use clang
|
|
||||||
# make sure to preset CFLAGS if you use non-ix86 platform
|
|
||||||
# or non-GNU-compat C compilation system
|
|
||||||
# Uncomment these if you're on a 32-bit Linux?
|
|
||||||
# CC = cc
|
|
||||||
# CFLAGS = -Ofast -march=nocona -mfpmath=sse
|
|
||||||
|
|
||||||
# path to mbedtls headers/libs and system libs
|
|
||||||
# if you have local copies of libs in this folder,
|
|
||||||
# try LIBS=-L. (other stuff here)
|
|
||||||
#
|
|
||||||
# -lsocket -lnsl on Sun
|
|
||||||
# -lws2_32 on windows nt
|
|
||||||
#INCLUDE :=
|
|
||||||
#LIBS :=
|
|
||||||
|
|
||||||
.PHONY: download prepare all default
|
|
||||||
|
|
||||||
.c.o:
|
|
||||||
$(CC) $(INCLUDE) -fPIC -Iinclude $(CFLAGS) $< -c
|
|
||||||
|
|
||||||
zpipe: zpipe.c miniz.c
|
|
||||||
$(CC) $(INCLUDE) -Iinclude $(CFLAGS) $^ -s -static -o $@
|
|
||||||
|
|
||||||
base64enc: base64enc.c
|
|
||||||
$(CC) $(INCLUDE) -Iinclude $(CFLAGS) $^ -s -static -o $@ -lmbedx509 -lmbedtls -lmbedcrypto $(LIBS)
|
|
||||||
|
|
||||||
download:
|
|
||||||
wget -O ./cacert.pem https://curl.haxx.se/ca/cacert.pem
|
|
||||||
|
|
||||||
# I *think* this only work with GNU sed...
|
|
||||||
prepare: zpipe base64enc download
|
|
||||||
./zpipe < cacert.pem > data.enc
|
|
||||||
./base64enc < data.enc > out.bin
|
|
||||||
sed -ie "s/.\{76\}/&\n/g" out.bin
|
|
||||||
sed -i 's/.*/\"&\"/g' out.bin
|
|
||||||
sed -i '38,2228d' cacerts.c
|
|
||||||
echo ';' >> out.bin
|
|
||||||
sed -i '37r out.bin' cacerts.c
|
|
||||||
|
|
||||||
libhttp.dll: cacerts.o miniz.o libhttp.o uri.o internal.o
|
|
||||||
$(CC) -fPIC $(CFLAGS) $^ -s -shared -o $@ -static -lmbedx509 -lmbedtls -lmbedcrypto $(LIBS)
|
|
||||||
|
|
||||||
libhttp.so: cacerts.o miniz.o libhttp.o uri.o internal.o
|
|
||||||
$(CC) $^ -fPIC $(CFLAGS) -shared -o $@ $(LIBS) -lmbedx509 -lmbedtls -lmbedcrypto $(SYS_LIBS)
|
|
||||||
|
|
||||||
clean:
|
|
||||||
-@rm base64enc
|
|
||||||
-@rm zpipe
|
|
||||||
-@rm cacert.pem
|
|
||||||
-@rm data.enc
|
|
||||||
-@rm out.*
|
|
||||||
-@rm *.o
|
|
||||||
-@rm *.so
|
|
||||||
-@rm *.dll
|
|
@ -1,26 +0,0 @@
|
|||||||
# liblokiweb (libhttp)
|
|
||||||
|
|
||||||
## Building
|
|
||||||
|
|
||||||
### requirements
|
|
||||||
|
|
||||||
- mbedtls 2.13.0 or later, for both host and target (if cross-compiling)
|
|
||||||
- wget for host (to download Netscape root certificate store from cURL website)
|
|
||||||
- Also included is a patch that can be applied to the mbedtls source to enable features like AES-NI in protected mode, plus some networking fixes for win32, see `../contrib/lokinet-bootstrap-winnt/mbedtls-win32.patch`
|
|
||||||
|
|
||||||
build:
|
|
||||||
|
|
||||||
$ make prepare; make libhttp.[so|dll]
|
|
||||||
|
|
||||||
## Useful build-time variables
|
|
||||||
|
|
||||||
- INCLUDE: path to mbedtls headers
|
|
||||||
- LIBS: path to mbedtls libraries
|
|
||||||
- SYS_LIBS: system-specific link libraries (`-lsocket -lnsl` on Sun systems, `-lws2_32` [or `-lwsock32` if IPv6 is disabled] on Windows)
|
|
||||||
|
|
||||||
## Usage
|
|
||||||
|
|
||||||
- include libhttp.h in your source
|
|
||||||
- link against libhttp.[so|dll]
|
|
||||||
|
|
||||||
-rick
|
|
@ -1,64 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c)2018-2019 Rick V. All rights reserved.
|
|
||||||
*
|
|
||||||
* This software is provided 'as-is', without any express or implied
|
|
||||||
* warranty. In no event will the authors be held liable for any damages
|
|
||||||
* arising from the use of this software.
|
|
||||||
*
|
|
||||||
* Permission is granted to anyone to use this software for any purpose,
|
|
||||||
* including commercial applications, and to alter it and redistribute it
|
|
||||||
* freely, subject to the following restrictions:
|
|
||||||
*
|
|
||||||
* 1. The origin of this software must not be misrepresented; you must not
|
|
||||||
* claim that you wrote the original software. If you use this software
|
|
||||||
* in a product, an acknowledgment in the product documentation would be
|
|
||||||
* appreciated but is not required.
|
|
||||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
|
||||||
* misrepresented as being the original software.
|
|
||||||
* 3. This notice may not be removed or altered from any source distribution.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/* this is a tiny build-time utility that base64 encodes up to 512K
|
|
||||||
* of text/binary data from stdin. (On UNIX, we'd use GNU's [g]base64(1)
|
|
||||||
* to encode the stream. Can't guarantee that a windows user will have cygwin
|
|
||||||
* installed, so we bootstrap these at build-time instead.)
|
|
||||||
*
|
|
||||||
* here, it is used to encode the compressed zlib-stream of the
|
|
||||||
* Netscape root certificate trust store on behalf of the lokinet
|
|
||||||
* for NT bootstrap stubs.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include "sysconf.h"
|
|
||||||
#ifdef HAVE_SETMODE
|
|
||||||
# define SET_BINARY_MODE(handle) setmode(handle, O_BINARY)
|
|
||||||
#else
|
|
||||||
# define SET_BINARY_MODE(handle) ((void)0)
|
|
||||||
#endif
|
|
||||||
#include <mbedtls/base64.h>
|
|
||||||
#include <mbedtls/error.h>
|
|
||||||
|
|
||||||
main(argc, argv)
|
|
||||||
char** argv;
|
|
||||||
{
|
|
||||||
int size,r, inl;
|
|
||||||
unsigned char in[524288];
|
|
||||||
unsigned char out[1048576];
|
|
||||||
unsigned char err[1024];
|
|
||||||
memset(&in, 0, 524288);
|
|
||||||
memset(&out, 0, 1048576);
|
|
||||||
SET_BINARY_MODE(0);
|
|
||||||
/* Read up to 512K of data from stdin */
|
|
||||||
inl = fread(in, 1, 524288, stdin);
|
|
||||||
r = mbedtls_base64_encode(out, 1048576, &size, in, inl);
|
|
||||||
if (r)
|
|
||||||
{
|
|
||||||
mbedtls_strerror(r, err, 1024);
|
|
||||||
printf("error: %s\n", err);
|
|
||||||
return r;
|
|
||||||
}
|
|
||||||
fprintf(stdout, "%s", out);
|
|
||||||
return 0;
|
|
||||||
}
|
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -1,95 +0,0 @@
|
|||||||
/**
|
|
||||||
* sysconf.h -- system-dependent macros and settings
|
|
||||||
*
|
|
||||||
* Copyright (C) 2002-2004 Cosmin Truta.
|
|
||||||
* Permission to use and distribute freely.
|
|
||||||
* No warranty.
|
|
||||||
**/
|
|
||||||
|
|
||||||
#ifndef SYSCONF_H
|
|
||||||
#define SYSCONF_H
|
|
||||||
|
|
||||||
|
|
||||||
/*****************************************************************************/
|
|
||||||
/* Platform identifiers */
|
|
||||||
|
|
||||||
|
|
||||||
/* Detect Unix. */
|
|
||||||
#if defined(unix) || defined(__linux__) || defined(BSD) || defined(__CYGWIN__)
|
|
||||||
/* Add more systems here. */
|
|
||||||
# ifndef UNIX
|
|
||||||
# define UNIX
|
|
||||||
# endif
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/* Detect MS-DOS. */
|
|
||||||
#if defined(__MSDOS__)
|
|
||||||
# ifndef MSDOS
|
|
||||||
# define MSDOS
|
|
||||||
# endif
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/* TO DO: Detect OS/2. */
|
|
||||||
|
|
||||||
/* Detect Windows. */
|
|
||||||
#if defined(_WIN32) || defined(__WIN32__)
|
|
||||||
# ifndef WIN32
|
|
||||||
# define WIN32
|
|
||||||
# endif
|
|
||||||
#endif
|
|
||||||
#if defined(_WIN64)
|
|
||||||
# ifndef WIN64
|
|
||||||
# define WIN64
|
|
||||||
# endif
|
|
||||||
#endif
|
|
||||||
#if defined(_WINDOWS) || defined(WIN32) || defined(WIN64)
|
|
||||||
# ifndef WINDOWS
|
|
||||||
# define WINDOWS
|
|
||||||
# endif
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/* Enable POSIX-friendly symbols on Microsoft (Visual) C. */
|
|
||||||
#ifdef _MSC_VER
|
|
||||||
# define _POSIX_
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
/*****************************************************************************/
|
|
||||||
/* Library access */
|
|
||||||
|
|
||||||
|
|
||||||
#if defined(UNIX)
|
|
||||||
# include <unistd.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(_POSIX_VERSION)
|
|
||||||
# include <fcntl.h>
|
|
||||||
# ifndef HAVE_ISATTY
|
|
||||||
# define HAVE_ISATTY
|
|
||||||
# endif
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(MSDOS) || defined(OS2) || defined(WINDOWS) || defined(__CYGWIN__)
|
|
||||||
/* Add more systems here, e.g. MacOS 9 and earlier. */
|
|
||||||
# include <fcntl.h>
|
|
||||||
# include <io.h>
|
|
||||||
# ifndef HAVE_ISATTY
|
|
||||||
# define HAVE_ISATTY
|
|
||||||
# endif
|
|
||||||
# ifndef HAVE_SETMODE
|
|
||||||
# define HAVE_SETMODE
|
|
||||||
# endif
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/* Standard I/O handles. */
|
|
||||||
#define STDIN 0
|
|
||||||
#define STDOUT 1
|
|
||||||
#define STDERR 2
|
|
||||||
|
|
||||||
/* Provide a placeholder for O_BINARY, if it doesn't exist. */
|
|
||||||
#ifndef O_BINARY
|
|
||||||
# define O_BINARY 0
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
#endif /* SYSCONF_H */
|
|
@ -1,39 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c)2018-2019 Rick V. All rights reserved.
|
|
||||||
*
|
|
||||||
* This software is provided 'as-is', without any express or implied
|
|
||||||
* warranty. In no event will the authors be held liable for any damages
|
|
||||||
* arising from the use of this software.
|
|
||||||
*
|
|
||||||
* Permission is granted to anyone to use this software for any purpose,
|
|
||||||
* including commercial applications, and to alter it and redistribute it
|
|
||||||
* freely, subject to the following restrictions:
|
|
||||||
*
|
|
||||||
* 1. The origin of this software must not be misrepresented; you must not
|
|
||||||
* claim that you wrote the original software. If you use this software
|
|
||||||
* in a product, an acknowledgment in the product documentation would be
|
|
||||||
* appreciated but is not required.
|
|
||||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
|
||||||
* misrepresented as being the original software.
|
|
||||||
* 3. This notice may not be removed or altered from any source distribution.
|
|
||||||
*------------------------------------------------------------------------------
|
|
||||||
* internal utility functions for libhttp
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <stdbool.h>
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
#include "internal.h"
|
|
||||||
|
|
||||||
void *memncat(a, an, b, bn, s)
|
|
||||||
const void *a, *b;
|
|
||||||
size_t an, bn, s;
|
|
||||||
{
|
|
||||||
char *p = malloc(s * (an + bn));
|
|
||||||
memset(p, '\0', s * (an + bn));
|
|
||||||
memcpy(p, a, an*s);
|
|
||||||
memcpy(p + an*s, b, bn*s);
|
|
||||||
return p;
|
|
||||||
}
|
|
@ -1,84 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c)2018-2019 Rick V. All rights reserved.
|
|
||||||
*
|
|
||||||
* This software is provided 'as-is', without any express or implied
|
|
||||||
* warranty. In no event will the authors be held liable for any damages
|
|
||||||
* arising from the use of this software.
|
|
||||||
*
|
|
||||||
* Permission is granted to anyone to use this software for any purpose,
|
|
||||||
* including commercial applications, and to alter it and redistribute it
|
|
||||||
* freely, subject to the following restrictions:
|
|
||||||
*
|
|
||||||
* 1. The origin of this software must not be misrepresented; you must not
|
|
||||||
* claim that you wrote the original software. If you use this software
|
|
||||||
* in a product, an acknowledgment in the product documentation would be
|
|
||||||
* appreciated but is not required.
|
|
||||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
|
||||||
* misrepresented as being the original software.
|
|
||||||
* 3. This notice may not be removed or altered from any source distribution.
|
|
||||||
*------------------------------------------------------------------------------
|
|
||||||
* this file contains internal definitions of libhttp data structures
|
|
||||||
* internal.h and libhttp.h are mutually exclusive as they define the same
|
|
||||||
* data structures, library clients must use libhttp.h
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef INTERNAL_H
|
|
||||||
#define INTERNAL_H
|
|
||||||
|
|
||||||
/* PolarSSL */
|
|
||||||
#include <mbedtls/ssl.h>
|
|
||||||
#include <mbedtls/entropy.h>
|
|
||||||
#include <mbedtls/ctr_drbg.h>
|
|
||||||
#include <mbedtls/net_sockets.h>
|
|
||||||
#include <mbedtls/error.h>
|
|
||||||
#include <mbedtls/certs.h>
|
|
||||||
#include <mbedtls/base64.h>
|
|
||||||
|
|
||||||
/* function declarations */
|
|
||||||
void free_parsed_url();
|
|
||||||
parse_url();
|
|
||||||
void *memncat();
|
|
||||||
|
|
||||||
typedef struct url_parser_url
|
|
||||||
{
|
|
||||||
char *protocol;
|
|
||||||
char *host;
|
|
||||||
int port;
|
|
||||||
char *path;
|
|
||||||
char *query_string;
|
|
||||||
int host_exists;
|
|
||||||
char *host_ip;
|
|
||||||
} url_t;
|
|
||||||
|
|
||||||
typedef struct
|
|
||||||
{
|
|
||||||
char *ua; /* platform-specific user-agent string */
|
|
||||||
char *request_uri; /* last uri requested, the response corresponds to this link */
|
|
||||||
struct responseBody
|
|
||||||
{
|
|
||||||
/* the raw_data and headers point to the same place */
|
|
||||||
/* the content is an offset into the raw_data */
|
|
||||||
union
|
|
||||||
{
|
|
||||||
char* raw_data;
|
|
||||||
char* headers;
|
|
||||||
};
|
|
||||||
char* content;
|
|
||||||
};
|
|
||||||
|
|
||||||
/* not a public field */
|
|
||||||
struct
|
|
||||||
{
|
|
||||||
mbedtls_net_context server_fd;
|
|
||||||
mbedtls_entropy_context entropy;
|
|
||||||
mbedtls_ctr_drbg_context ctr_drbg;
|
|
||||||
mbedtls_ssl_context ssl;
|
|
||||||
mbedtls_ssl_config conf;
|
|
||||||
mbedtls_x509_crt cacert;
|
|
||||||
bool TLSInit;
|
|
||||||
char seed[64];
|
|
||||||
} tls_state;
|
|
||||||
} http_state;
|
|
||||||
|
|
||||||
#endif
|
|
@ -1,301 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c)2018-2019 Rick V. All rights reserved.
|
|
||||||
*
|
|
||||||
* This software is provided 'as-is', without any express or implied
|
|
||||||
* warranty. In no event will the authors be held liable for any damages
|
|
||||||
* arising from the use of this software.
|
|
||||||
*
|
|
||||||
* Permission is granted to anyone to use this software for any purpose,
|
|
||||||
* including commercial applications, and to alter it and redistribute it
|
|
||||||
* freely, subject to the following restrictions:
|
|
||||||
*
|
|
||||||
* 1. The origin of this software must not be misrepresented; you must not
|
|
||||||
* claim that you wrote the original software. If you use this software
|
|
||||||
* in a product, an acknowledgment in the product documentation would be
|
|
||||||
* appreciated but is not required.
|
|
||||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
|
||||||
* misrepresented as being the original software.
|
|
||||||
* 3. This notice may not be removed or altered from any source distribution.
|
|
||||||
*------------------------------------------------------------------------------
|
|
||||||
* libhttp, a really small HTTP 0-3 client library with TLS
|
|
||||||
*
|
|
||||||
* HTTPS only; why the hell would you serve semi-sensitive data over an
|
|
||||||
* unencrypted channel? In fact, the polarssl integration is intended to
|
|
||||||
* bypass limitations in the native TLS stack (no TLS 1.1+ on some older
|
|
||||||
* platforms, lack of high-encryption ciphersuites other than ARC4 or
|
|
||||||
* Triple-DES, etc)
|
|
||||||
* -rick
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <stdbool.h>
|
|
||||||
|
|
||||||
#ifdef _WIN32
|
|
||||||
#include <windows.h>
|
|
||||||
#include <wincrypt.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include "miniz.h"
|
|
||||||
#include "internal.h"
|
|
||||||
|
|
||||||
/* only decompress rootcerts once */
|
|
||||||
unsigned char* ca_certs = NULL;
|
|
||||||
/* netscape ca bundle */
|
|
||||||
const unsigned char *ca_cert_store_encoded;
|
|
||||||
/* imageboard ref just because */
|
|
||||||
static char userAgent[] = "NetRunner_Micro/0.1 PolarSSL/2.16.0;U;";
|
|
||||||
|
|
||||||
static void destroy_persistent_data()
|
|
||||||
{
|
|
||||||
free(ca_certs);
|
|
||||||
ca_certs = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool generateSeed(client)
|
|
||||||
http_state *client;
|
|
||||||
{
|
|
||||||
#ifdef _WIN32
|
|
||||||
HCRYPTPROV hprovider;
|
|
||||||
|
|
||||||
/* On Windows NT 4.0 or later, use CryptoAPI to grab 64 bytes of random data */
|
|
||||||
hprovider = 0;
|
|
||||||
CryptAcquireContext(&hprovider, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_SILENT);
|
|
||||||
CryptGenRandom(hprovider, 64, (BYTE*)&client->tls_state.seed);
|
|
||||||
CryptReleaseContext(hprovider, 0);
|
|
||||||
#endif
|
|
||||||
client->tls_state.seed[63] = '\0'; /* null-terminate for safety */
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool initTLS(client)
|
|
||||||
http_state *client;
|
|
||||||
{
|
|
||||||
int inf_status,r;
|
|
||||||
size_t out;
|
|
||||||
unsigned long inf_len;
|
|
||||||
unsigned char* tmp;
|
|
||||||
char str[512];
|
|
||||||
|
|
||||||
mbedtls_net_init(&client->tls_state.server_fd);
|
|
||||||
mbedtls_ssl_init(&client->tls_state.ssl);
|
|
||||||
mbedtls_ssl_config_init(&client->tls_state.conf);
|
|
||||||
mbedtls_x509_crt_init(&client->tls_state.cacert);
|
|
||||||
mbedtls_entropy_init(&client->tls_state.entropy);
|
|
||||||
mbedtls_ctr_drbg_init(&client->tls_state.ctr_drbg);
|
|
||||||
|
|
||||||
/* only decompress once */
|
|
||||||
if (!ca_certs)
|
|
||||||
{
|
|
||||||
tmp = malloc(524288);
|
|
||||||
|
|
||||||
r = strlen(ca_cert_store_encoded) - 1;
|
|
||||||
r = mbedtls_base64_decode(tmp, 524288, &out, ca_cert_store_encoded, r);
|
|
||||||
if (r)
|
|
||||||
{
|
|
||||||
mbedtls_strerror(r, (char*)tmp, 524288);
|
|
||||||
printf("decoding failed: %s\n", tmp);
|
|
||||||
free(tmp);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
/* inflate ca certs, they are still compressed */
|
|
||||||
ca_certs = malloc(524288);
|
|
||||||
inf_len = 524288;
|
|
||||||
|
|
||||||
inf_status = uncompress(ca_certs, &inf_len, tmp, out);
|
|
||||||
if (inf_status != Z_OK)
|
|
||||||
{
|
|
||||||
printf("decompression failed: %s\n", mz_error(inf_status));
|
|
||||||
free(tmp);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
free(tmp);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!generateSeed())
|
|
||||||
return false;
|
|
||||||
|
|
||||||
if (mbedtls_ctr_drbg_seed(&client->tls_state.ctr_drbg, mbedtls_entropy_func, &client->tls_state.entropy, (const unsigned char*)client->tls_state.seed, 64)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
r = mbedtls_x509_crt_parse(&client->tls_state.cacert, ca_certs, inf_len+1);
|
|
||||||
if (r < 0) {
|
|
||||||
mbedtls_strerror(r, str, 512);
|
|
||||||
printf("parse ca cert store failed\n ! mbedtls_x509_crt_parse returned: %s\n\n", str);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
client->tls_state.TLSInit = true;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* if false, library may be in an inconsistent state,
|
|
||||||
* call terminate_client()
|
|
||||||
*/
|
|
||||||
bool init_client(client)
|
|
||||||
http_state* client;
|
|
||||||
{
|
|
||||||
if (!ca_certs)
|
|
||||||
atexit(destroy_persistent_data);
|
|
||||||
if (!client)
|
|
||||||
client = calloc(1, sizeof(http_state));
|
|
||||||
initTLS(client);
|
|
||||||
|
|
||||||
return client->tls_state.TLSInit;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void ua_string(client)
|
|
||||||
http_state *client;
|
|
||||||
{
|
|
||||||
/* fill in user-agent string */
|
|
||||||
#ifdef _WIN32
|
|
||||||
DWORD version, major, minor, build;
|
|
||||||
version = GetVersion();
|
|
||||||
major = (DWORD)(LOBYTE(LOWORD(version)));
|
|
||||||
minor = (DWORD)(HIBYTE(LOWORD(version)));
|
|
||||||
if (version < 0x80000000)
|
|
||||||
build = (DWORD)(HIWORD(version));
|
|
||||||
client->ua = malloc(512);
|
|
||||||
snprintf(client->ua, 512, "%sWindows NT %d.%d", userAgent, major, minor);
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
download_https_resource(client, uri)
|
|
||||||
http_state *client;
|
|
||||||
char *uri;
|
|
||||||
{
|
|
||||||
int r, len;
|
|
||||||
char buf[1024], port[8];
|
|
||||||
char *rq, *resp;
|
|
||||||
unsigned flags;
|
|
||||||
url_t *parsed_uri;
|
|
||||||
|
|
||||||
rq = malloc(4096);
|
|
||||||
/* this string gets readjusted each time we make a request */
|
|
||||||
client->request_uri = realloc(NULL, strlen(uri)+1);
|
|
||||||
parsed_uri = malloc(sizeof(url_t));
|
|
||||||
memset(parsed_uri, 0, sizeof(url_t));
|
|
||||||
r = parse_url(uri, false, parsed_uri);
|
|
||||||
if (r)
|
|
||||||
{
|
|
||||||
printf("Invalid URI pathspec\n");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!client->tls_state.TLSInit)
|
|
||||||
{
|
|
||||||
printf("Failed to initialise polarssl\n");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* get host name, set port if blank */
|
|
||||||
if (!strcmp("https", parsed_uri->protocol) && !parsed_uri->port)
|
|
||||||
parsed_uri->port = 443;
|
|
||||||
|
|
||||||
printf("connecting to %s on port %d...",parsed_uri->host, parsed_uri->port);
|
|
||||||
sprintf(port, "%d", parsed_uri->port);
|
|
||||||
r = mbedtls_net_connect(&client->tls_state.server_fd, parsed_uri->host, port, MBEDTLS_NET_PROTO_TCP);
|
|
||||||
if (r)
|
|
||||||
{
|
|
||||||
printf("error - failed to connect to server: %d\n", r);
|
|
||||||
goto exit;
|
|
||||||
}
|
|
||||||
|
|
||||||
r = mbedtls_ssl_config_defaults(&client->tls_state.conf, MBEDTLS_SSL_IS_CLIENT, MBEDTLS_SSL_TRANSPORT_STREAM, MBEDTLS_SSL_PRESET_DEFAULT);
|
|
||||||
if (r)
|
|
||||||
{
|
|
||||||
printf("error - failed to set TLS options: %d\n", r);
|
|
||||||
goto exit;
|
|
||||||
}
|
|
||||||
mbedtls_ssl_conf_authmode(&client->tls_state.conf, MBEDTLS_SSL_VERIFY_REQUIRED);
|
|
||||||
mbedtls_ssl_conf_ca_chain(&client->tls_state.conf, &client->tls_state.cacert, NULL);
|
|
||||||
mbedtls_ssl_conf_rng(&client->tls_state.conf, mbedtls_ctr_drbg_random, &client->tls_state.ctr_drbg);
|
|
||||||
r = mbedtls_ssl_setup(&client->tls_state.ssl, &client->tls_state.conf);
|
|
||||||
if (r)
|
|
||||||
{
|
|
||||||
printf("error - failed to setup TLS session: %d\n", r);
|
|
||||||
goto exit;
|
|
||||||
}
|
|
||||||
r = mbedtls_ssl_set_hostname(&client->tls_state.ssl, parsed_uri->host);
|
|
||||||
if (r)
|
|
||||||
{
|
|
||||||
printf("error - failed to perform SNI: %d\n", r);
|
|
||||||
goto exit;
|
|
||||||
}
|
|
||||||
mbedtls_ssl_set_bio(&client->tls_state.ssl, &client->tls_state.server_fd, mbedtls_net_send, mbedtls_net_recv, NULL);
|
|
||||||
while ((r = mbedtls_ssl_handshake(&client->tls_state.ssl)) != 0)
|
|
||||||
{
|
|
||||||
if (r != MBEDTLS_ERR_SSL_WANT_READ && r != MBEDTLS_ERR_SSL_WANT_WRITE)
|
|
||||||
{
|
|
||||||
printf(" failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n", -r);
|
|
||||||
goto exit;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if ((flags = mbedtls_ssl_get_verify_result(&client->tls_state.ssl)) != 0)
|
|
||||||
{
|
|
||||||
char vrfy_buf[512];
|
|
||||||
printf(" failed\n");
|
|
||||||
mbedtls_x509_crt_verify_info(vrfy_buf, sizeof(vrfy_buf), " ! ", flags);
|
|
||||||
printf("%s\n", vrfy_buf);
|
|
||||||
goto exit;
|
|
||||||
}
|
|
||||||
printf("\nDownloading %s...", &parsed_uri->path[1]);
|
|
||||||
snprintf(rq, 512, "GET %s HTTP/1.0\r\nHost: %s\r\nUser-Agent: %s\r\n\r\n", parsed_uri->path, parsed_uri->host, client->ua);
|
|
||||||
while ((r = mbedtls_ssl_write(&client->tls_state.ssl, (unsigned char*)rq, strlen(rq))) <= 0)
|
|
||||||
{
|
|
||||||
if (r != MBEDTLS_ERR_SSL_WANT_READ && r != MBEDTLS_ERR_SSL_WANT_WRITE)
|
|
||||||
{
|
|
||||||
printf("failed! error %d\n\n", r);
|
|
||||||
goto exit;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
memset(rq, 0, 4096);
|
|
||||||
len = 0;
|
|
||||||
do {
|
|
||||||
r = mbedtls_ssl_read(&client->tls_state.ssl, (unsigned char*)buf, 1024);
|
|
||||||
if (r <= 0)
|
|
||||||
break;
|
|
||||||
else
|
|
||||||
{
|
|
||||||
rq = memncat(rq, len, buf, r, sizeof(char));
|
|
||||||
len += r;
|
|
||||||
}
|
|
||||||
} while (r);
|
|
||||||
printf("%d bytes downloaded to core.\n", len);
|
|
||||||
mbedtls_ssl_close_notify(&client->tls_state.ssl);
|
|
||||||
if (!strstr(rq, "200 OK"))
|
|
||||||
{
|
|
||||||
printf("An error occurred.\n");
|
|
||||||
printf("Server response:\n%s", rq);
|
|
||||||
goto exit;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Response body is in buf after processing */
|
|
||||||
resp = strstr(rq, "Content-Length");
|
|
||||||
r = strcspn(resp, "0123456789");
|
|
||||||
memcpy(buf, &resp[r], 4);
|
|
||||||
buf[3] = '\0';
|
|
||||||
r = atoi(buf);
|
|
||||||
resp = strstr(rq, "\r\n\r\n");
|
|
||||||
memcpy(buf, &resp[4], r);
|
|
||||||
|
|
||||||
r = 0;
|
|
||||||
|
|
||||||
exit:
|
|
||||||
free(rq);
|
|
||||||
free_parsed_url(parsed_uri);
|
|
||||||
return r;
|
|
||||||
}
|
|
||||||
|
|
||||||
void terminate_client(client)
|
|
||||||
http_state *client;
|
|
||||||
{
|
|
||||||
mbedtls_ssl_close_notify(&client->tls_state.ssl);
|
|
||||||
mbedtls_net_free(&client->tls_state.server_fd);
|
|
||||||
mbedtls_x509_crt_free(&client->tls_state.cacert);
|
|
||||||
mbedtls_ssl_free(&client->tls_state.ssl);
|
|
||||||
mbedtls_ssl_config_free(&client->tls_state.conf);
|
|
||||||
mbedtls_ctr_drbg_free(&client->tls_state.ctr_drbg);
|
|
||||||
mbedtls_entropy_free(&client->tls_state.entropy);
|
|
||||||
free(client->ua);
|
|
||||||
}
|
|
@ -1,59 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c)2018-2019 Rick V. All rights reserved.
|
|
||||||
*
|
|
||||||
* This software is provided 'as-is', without any express or implied
|
|
||||||
* warranty. In no event will the authors be held liable for any damages
|
|
||||||
* arising from the use of this software.
|
|
||||||
*
|
|
||||||
* Permission is granted to anyone to use this software for any purpose,
|
|
||||||
* including commercial applications, and to alter it and redistribute it
|
|
||||||
* freely, subject to the following restrictions:
|
|
||||||
*
|
|
||||||
* 1. The origin of this software must not be misrepresented; you must not
|
|
||||||
* claim that you wrote the original software. If you use this software
|
|
||||||
* in a product, an acknowledgment in the product documentation would be
|
|
||||||
* appreciated but is not required.
|
|
||||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
|
||||||
* misrepresented as being the original software.
|
|
||||||
* 3. This notice may not be removed or altered from any source distribution.
|
|
||||||
*------------------------------------------------------------------------------
|
|
||||||
* libhttp, a really small HTTP 0-3 client library with TLS
|
|
||||||
* public API header
|
|
||||||
* do not include this file in any of the libhttp sources itself
|
|
||||||
*
|
|
||||||
* HTTPS only; why the hell would you serve semi-sensitive data over an
|
|
||||||
* unencrypted channel? In fact, the polarssl integration is intended to
|
|
||||||
* bypass limitations in the native TLS stack (no TLS 1.1+ on some older
|
|
||||||
* platforms, lack of high-encryption ciphersuites other than ARC4 or
|
|
||||||
* Triple-DES, etc)
|
|
||||||
* -rick
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef LIBHTTP_H
|
|
||||||
#define LIBHTTP_H
|
|
||||||
|
|
||||||
/* http client object */
|
|
||||||
typedef struct
|
|
||||||
{
|
|
||||||
char *ua; /* platform-specific user-agent string */
|
|
||||||
char *request_uri; /* last uri requested, the response corresponds to this link */
|
|
||||||
struct responseBody
|
|
||||||
{
|
|
||||||
/* the raw_data and headers point to the same place */
|
|
||||||
/* the content is an offset into the raw_data */
|
|
||||||
union
|
|
||||||
{
|
|
||||||
char* raw_data;
|
|
||||||
char* headers;
|
|
||||||
};
|
|
||||||
char* content;
|
|
||||||
};
|
|
||||||
/* anonymous field, do not poke */
|
|
||||||
void *reserved;
|
|
||||||
} http_state;
|
|
||||||
|
|
||||||
/* libhttp public API */
|
|
||||||
bool init_client(http_state*);
|
|
||||||
int download_https_resource(http_state*, char*);
|
|
||||||
void terminate_client(http_state*);
|
|
||||||
#endif
|
|
File diff suppressed because it is too large
Load Diff
@ -1,138 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c)2018-2019 Rick V. All rights reserved.
|
|
||||||
*
|
|
||||||
* This software is provided 'as-is', without any express or implied
|
|
||||||
* warranty. In no event will the authors be held liable for any damages
|
|
||||||
* arising from the use of this software.
|
|
||||||
*
|
|
||||||
* Permission is granted to anyone to use this software for any purpose,
|
|
||||||
* including commercial applications, and to alter it and redistribute it
|
|
||||||
* freely, subject to the following restrictions:
|
|
||||||
*
|
|
||||||
* 1. The origin of this software must not be misrepresented; you must not
|
|
||||||
* claim that you wrote the original software. If you use this software
|
|
||||||
* in a product, an acknowledgment in the product documentation would be
|
|
||||||
* appreciated but is not required.
|
|
||||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
|
||||||
* misrepresented as being the original software.
|
|
||||||
* 3. This notice may not be removed or altered from any source distribution.
|
|
||||||
*------------------------------------------------------------------------------
|
|
||||||
* uri parsing functions
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <stdbool.h>
|
|
||||||
#include <assert.h>
|
|
||||||
|
|
||||||
#include "internal.h"
|
|
||||||
|
|
||||||
#ifdef _WIN32
|
|
||||||
#include <windows.h>
|
|
||||||
#include <wincrypt.h>
|
|
||||||
#else
|
|
||||||
#include <sys/types.h>
|
|
||||||
#include <sys/socket.h>
|
|
||||||
#include <netinet/in.h>
|
|
||||||
#include <netdb.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
void free_parsed_url(url_parsed)
|
|
||||||
url_t *url_parsed;
|
|
||||||
{
|
|
||||||
if (url_parsed->protocol)
|
|
||||||
free(url_parsed->protocol);
|
|
||||||
if (url_parsed->host)
|
|
||||||
free(url_parsed->host);
|
|
||||||
if (url_parsed->path)
|
|
||||||
free(url_parsed->path);
|
|
||||||
if (url_parsed->query_string)
|
|
||||||
free(url_parsed->query_string);
|
|
||||||
|
|
||||||
free(url_parsed);
|
|
||||||
}
|
|
||||||
|
|
||||||
parse_url(url, verify_host, parsed_url)
|
|
||||||
char *url;
|
|
||||||
bool verify_host;
|
|
||||||
url_t *parsed_url;
|
|
||||||
{
|
|
||||||
char *local_url, *token, *token_host, *host_port, *host_ip, *token_ptr;
|
|
||||||
char *host_token_ptr, *path = NULL;
|
|
||||||
|
|
||||||
/* Copy our string */
|
|
||||||
local_url = strdup(url);
|
|
||||||
|
|
||||||
token = strtok_r(local_url, ":", &token_ptr);
|
|
||||||
parsed_url->protocol = strdup(token);
|
|
||||||
|
|
||||||
/* Host:Port */
|
|
||||||
token = strtok_r(NULL, "/", &token_ptr);
|
|
||||||
if (token)
|
|
||||||
host_port = strdup(token);
|
|
||||||
else
|
|
||||||
host_port = (char *) calloc(1, sizeof(char));
|
|
||||||
|
|
||||||
token_host = strtok_r(host_port, ":", &host_token_ptr);
|
|
||||||
parsed_url->host_ip = NULL;
|
|
||||||
if (token_host) {
|
|
||||||
parsed_url->host = strdup(token_host);
|
|
||||||
|
|
||||||
if (verify_host) {
|
|
||||||
struct hostent *host;
|
|
||||||
host = gethostbyname(parsed_url->host);
|
|
||||||
if (host != NULL) {
|
|
||||||
parsed_url->host_ip = inet_ntoa(* (struct in_addr *) host->h_addr);
|
|
||||||
parsed_url->host_exists = 1;
|
|
||||||
} else {
|
|
||||||
parsed_url->host_exists = 0;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
parsed_url->host_exists = -1;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
parsed_url->host_exists = -1;
|
|
||||||
parsed_url->host = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Port */
|
|
||||||
token_host = strtok_r(NULL, ":", &host_token_ptr);
|
|
||||||
if (token_host)
|
|
||||||
parsed_url->port = atoi(token_host);
|
|
||||||
else
|
|
||||||
parsed_url->port = 0;
|
|
||||||
|
|
||||||
token_host = strtok_r(NULL, ":", &host_token_ptr);
|
|
||||||
assert(token_host == NULL);
|
|
||||||
|
|
||||||
token = strtok_r(NULL, "?", &token_ptr);
|
|
||||||
parsed_url->path = NULL;
|
|
||||||
if (token) {
|
|
||||||
path = (char *) realloc(path, sizeof(char) * (strlen(token) + 2));
|
|
||||||
memset(path, 0, sizeof(char) * (strlen(token)+2));
|
|
||||||
strcpy(path, "/");
|
|
||||||
strcat(path, token);
|
|
||||||
|
|
||||||
parsed_url->path = strdup(path);
|
|
||||||
|
|
||||||
free(path);
|
|
||||||
} else {
|
|
||||||
parsed_url->path = (char *) malloc(sizeof(char) * 2);
|
|
||||||
strcpy(parsed_url->path, "/");
|
|
||||||
}
|
|
||||||
|
|
||||||
token = strtok_r(NULL, "?", &token_ptr);
|
|
||||||
if (token) {
|
|
||||||
parsed_url->query_string = (char *) malloc(sizeof(char) * (strlen(token) + 1));
|
|
||||||
strncpy(parsed_url->query_string, token, strlen(token));
|
|
||||||
} else {
|
|
||||||
parsed_url->query_string = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
token = strtok_r(NULL, "?", &token_ptr);
|
|
||||||
assert(token == NULL);
|
|
||||||
|
|
||||||
free(local_url);
|
|
||||||
free(host_port);
|
|
||||||
return 0;
|
|
||||||
}
|
|
@ -1,209 +0,0 @@
|
|||||||
/* zpipe.c: example of proper use of zlib's inflate() and deflate()
|
|
||||||
Not copyrighted -- provided to the public domain
|
|
||||||
Version 1.4 11 December 2005 Mark Adler */
|
|
||||||
|
|
||||||
/* Version history:
|
|
||||||
1.0 30 Oct 2004 First version
|
|
||||||
1.1 8 Nov 2004 Add void casting for unused return values
|
|
||||||
Use switch statement for inflate() return values
|
|
||||||
1.2 9 Nov 2004 Add assertions to document zlib guarantees
|
|
||||||
1.3 6 Apr 2005 Remove incorrect assertion in inf()
|
|
||||||
1.4 11 Dec 2005 Add hack to avoid MSDOS end-of-line conversions
|
|
||||||
Avoid some compiler warnings for input and output buffers
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <assert.h>
|
|
||||||
#include "miniz.h"
|
|
||||||
|
|
||||||
#if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(__CYGWIN__)
|
|
||||||
# include <fcntl.h>
|
|
||||||
# include <io.h>
|
|
||||||
# define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY)
|
|
||||||
#else
|
|
||||||
# define SET_BINARY_MODE(file)
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#define CHUNK 16384
|
|
||||||
|
|
||||||
/* Compress from file source to file dest until EOF on source.
|
|
||||||
def() returns Z_OK on success, Z_MEM_ERROR if memory could not be
|
|
||||||
allocated for processing, Z_STREAM_ERROR if an invalid compression
|
|
||||||
level is supplied, Z_VERSION_ERROR if the version of zlib.h and the
|
|
||||||
version of the library linked do not match, or Z_ERRNO if there is
|
|
||||||
an error reading or writing the files. */
|
|
||||||
int def(FILE *source, FILE *dest, int level)
|
|
||||||
{
|
|
||||||
int ret, flush;
|
|
||||||
unsigned have;
|
|
||||||
z_stream strm;
|
|
||||||
unsigned char in[CHUNK];unsigned char out[CHUNK];
|
|
||||||
|
|
||||||
/* allocate deflate state */
|
|
||||||
strm.zalloc = Z_NULL;
|
|
||||||
strm.zfree = Z_NULL;
|
|
||||||
strm.opaque = Z_NULL;
|
|
||||||
ret = deflateInit(&strm, level);
|
|
||||||
if (ret != Z_OK)
|
|
||||||
return ret;
|
|
||||||
|
|
||||||
/* compress until end of file */
|
|
||||||
do {
|
|
||||||
strm.avail_in = fread(in, 1, CHUNK, source);
|
|
||||||
if (ferror(source)) {
|
|
||||||
(void)deflateEnd(&strm);
|
|
||||||
return Z_ERRNO;
|
|
||||||
}
|
|
||||||
flush = feof(source) ? Z_FINISH : Z_NO_FLUSH;
|
|
||||||
strm.next_in = in;
|
|
||||||
|
|
||||||
/* run deflate() on input until output buffer not full, finish
|
|
||||||
compression if all of source has been read in */
|
|
||||||
do {
|
|
||||||
strm.avail_out = CHUNK;
|
|
||||||
strm.next_out = out;
|
|
||||||
ret = deflate(&strm, flush); /* no bad return value */
|
|
||||||
assert(ret != Z_STREAM_ERROR); /* state not clobbered */
|
|
||||||
have = CHUNK - strm.avail_out;
|
|
||||||
if (fwrite(out, 1, have, dest) != have || ferror(dest)) {
|
|
||||||
(void)deflateEnd(&strm);
|
|
||||||
return Z_ERRNO;
|
|
||||||
}
|
|
||||||
} while (strm.avail_out == 0);
|
|
||||||
assert(strm.avail_in == 0); /* all input will be used */
|
|
||||||
|
|
||||||
/* done when last data in file processed */
|
|
||||||
} while (flush != Z_FINISH);
|
|
||||||
assert(ret == Z_STREAM_END); /* stream will be complete */
|
|
||||||
|
|
||||||
/* clean up and return */
|
|
||||||
(void)deflateEnd(&strm);
|
|
||||||
return Z_OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Decompress from file source to file dest until stream ends or EOF.
|
|
||||||
inf() returns Z_OK on success, Z_MEM_ERROR if memory could not be
|
|
||||||
allocated for processing, Z_DATA_ERROR if the deflate data is
|
|
||||||
invalid or incomplete, Z_VERSION_ERROR if the version of zlib.h and
|
|
||||||
the version of the library linked do not match, or Z_ERRNO if there
|
|
||||||
is an error reading or writing the files. */
|
|
||||||
int inf(FILE *source, FILE *dest)
|
|
||||||
{
|
|
||||||
int ret;
|
|
||||||
unsigned have;
|
|
||||||
z_stream strm;
|
|
||||||
unsigned char in[CHUNK];
|
|
||||||
unsigned char out[CHUNK];
|
|
||||||
|
|
||||||
/* allocate inflate state */
|
|
||||||
strm.zalloc = Z_NULL;
|
|
||||||
strm.zfree = Z_NULL;
|
|
||||||
strm.opaque = Z_NULL;
|
|
||||||
strm.avail_in = 0;
|
|
||||||
strm.next_in = Z_NULL;
|
|
||||||
ret = inflateInit(&strm);
|
|
||||||
if (ret != Z_OK)
|
|
||||||
return ret;
|
|
||||||
|
|
||||||
/* decompress until deflate stream ends or end of file */
|
|
||||||
do {
|
|
||||||
strm.avail_in = fread(in, 1, CHUNK, source);
|
|
||||||
if (ferror(source)) {
|
|
||||||
(void)inflateEnd(&strm);
|
|
||||||
return Z_ERRNO;
|
|
||||||
}
|
|
||||||
if (strm.avail_in == 0)
|
|
||||||
break;
|
|
||||||
strm.next_in = in;
|
|
||||||
|
|
||||||
/* run inflate() on input until output buffer not full */
|
|
||||||
do {
|
|
||||||
strm.avail_out = CHUNK;
|
|
||||||
strm.next_out = out;
|
|
||||||
ret = inflate(&strm, Z_NO_FLUSH);
|
|
||||||
assert(ret != Z_STREAM_ERROR); /* state not clobbered */
|
|
||||||
switch (ret) {
|
|
||||||
case Z_NEED_DICT:
|
|
||||||
ret = Z_DATA_ERROR; /* and fall through */
|
|
||||||
case Z_DATA_ERROR:
|
|
||||||
case Z_MEM_ERROR:
|
|
||||||
(void)inflateEnd(&strm);
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
have = CHUNK - strm.avail_out;
|
|
||||||
if (fwrite(out, 1, have, dest) != have || ferror(dest)) {
|
|
||||||
(void)inflateEnd(&strm);
|
|
||||||
return Z_ERRNO;
|
|
||||||
}
|
|
||||||
} while (strm.avail_out == 0);
|
|
||||||
|
|
||||||
/* done when inflate() says it's done */
|
|
||||||
} while (ret != Z_STREAM_END);
|
|
||||||
|
|
||||||
/* clean up and return */
|
|
||||||
(void)inflateEnd(&strm);
|
|
||||||
return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* report a zlib or i/o error */
|
|
||||||
void zerr(int ret)
|
|
||||||
{
|
|
||||||
fputs("zpipe: ", stderr);
|
|
||||||
switch (ret) {
|
|
||||||
case Z_ERRNO:
|
|
||||||
if (ferror(stdin))
|
|
||||||
fputs("error reading stdin\n", stderr);
|
|
||||||
if (ferror(stdout))
|
|
||||||
fputs("error writing stdout\n", stderr);
|
|
||||||
break;
|
|
||||||
case Z_STREAM_ERROR:
|
|
||||||
fputs("invalid compression level\n", stderr);
|
|
||||||
break;
|
|
||||||
case Z_DATA_ERROR:
|
|
||||||
fputs("invalid or incomplete deflate data\n", stderr);
|
|
||||||
break;
|
|
||||||
case Z_MEM_ERROR:
|
|
||||||
fputs("out of memory\n", stderr);
|
|
||||||
break;
|
|
||||||
case Z_VERSION_ERROR:
|
|
||||||
fputs("zlib version mismatch!\n", stderr);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* compress or decompress from stdin to stdout */
|
|
||||||
int main(int argc, char **argv)
|
|
||||||
{
|
|
||||||
int ret;
|
|
||||||
|
|
||||||
/* avoid end-of-line conversions */
|
|
||||||
SET_BINARY_MODE(stdin);
|
|
||||||
SET_BINARY_MODE(stdout);
|
|
||||||
|
|
||||||
/* do compression if no arguments */
|
|
||||||
/* Warning: Not compatible with plain libz, dial it back down to
|
|
||||||
* 9 if this is required, uber-level is 10
|
|
||||||
* since we have everything crammed in miniz, we don't depend on
|
|
||||||
* libz at all.
|
|
||||||
*/
|
|
||||||
if (argc == 1) {
|
|
||||||
ret = def(stdin, stdout, MZ_UBER_COMPRESSION);
|
|
||||||
if (ret != Z_OK)
|
|
||||||
zerr(ret);
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* do decompression if -d specified */
|
|
||||||
else if (argc == 2 && strcmp(argv[1], "-d") == 0) {
|
|
||||||
ret = inf(stdin, stdout);
|
|
||||||
if (ret != Z_OK)
|
|
||||||
zerr(ret);
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* otherwise, report usage */
|
|
||||||
else {
|
|
||||||
fputs("zpipe usage: zpipe [-d] < source > dest\n", stderr);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,2 +1,6 @@
|
|||||||
mbedtls*.tgz*
|
mbedtls*.tgz*
|
||||||
mbedtls-*/
|
mbedtls-*/
|
||||||
|
curl*.tar.xz*
|
||||||
|
curl-*/
|
||||||
|
include/
|
||||||
|
lib/
|
@ -0,0 +1,82 @@
|
|||||||
|
diff --git a/curl-7.66.0/include/curl/curl.h b/curl-patched/include/curl/curl.h
|
||||||
|
index ff0c7749..4d3fdbb5 100644
|
||||||
|
--- a/curl-7.66.0/include/curl/curl.h
|
||||||
|
+++ b/curl-patched/include/curl/curl.h
|
||||||
|
@@ -65,6 +65,7 @@
|
||||||
|
included, since they can't co-exist without problems */
|
||||||
|
#include <winsock2.h>
|
||||||
|
#include <ws2tcpip.h>
|
||||||
|
+#include <wspiapi.h>
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
diff --git a/curl-7.66.0/include/curl/system.h b/curl-patched/include/curl/system.h
|
||||||
|
index cd37c2bf..b96cfd8c 100644
|
||||||
|
--- a/curl-7.66.0/include/curl/system.h
|
||||||
|
+++ b/curl-patched/include/curl/system.h
|
||||||
|
@@ -411,6 +411,7 @@
|
||||||
|
# include <winsock2.h>
|
||||||
|
# include <windows.h>
|
||||||
|
# include <ws2tcpip.h>
|
||||||
|
+# include <wspiapi.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* CURL_PULL_SYS_TYPES_H is defined above when inclusion of header file */
|
||||||
|
diff --git a/curl-7.66.0/lib/curl_setup.h b/curl-patched/lib/curl_setup.h
|
||||||
|
index 13af8cde..a0408d5c 100644
|
||||||
|
--- a/curl-7.66.0/lib/curl_setup.h
|
||||||
|
+++ b/curl-patched/lib/curl_setup.h
|
||||||
|
@@ -255,6 +255,7 @@
|
||||||
|
# include <winsock2.h>
|
||||||
|
# ifdef HAVE_WS2TCPIP_H
|
||||||
|
# include <ws2tcpip.h>
|
||||||
|
+# include <wspiapi.h>
|
||||||
|
# endif
|
||||||
|
# else
|
||||||
|
# ifdef HAVE_WINSOCK_H
|
||||||
|
diff --git a/curl-7.66.0/lib/inet_pton.h b/curl-patched/lib/inet_pton.h
|
||||||
|
index 0209b9b7..67774fb9 100644
|
||||||
|
--- a/curl-7.66.0/lib/inet_pton.h
|
||||||
|
+++ b/curl-patched/lib/inet_pton.h
|
||||||
|
@@ -32,6 +32,7 @@ int Curl_inet_pton(int, const char *, void *);
|
||||||
|
#elif defined(HAVE_WS2TCPIP_H)
|
||||||
|
/* inet_pton() exists in Vista or later */
|
||||||
|
#include <ws2tcpip.h>
|
||||||
|
+#include <wspiapi.h>
|
||||||
|
#endif
|
||||||
|
#define Curl_inet_pton(x,y,z) inet_pton(x,y,z)
|
||||||
|
#endif
|
||||||
|
diff --git a/curl-7.66.0/src/tool_util.c b/curl-patched/src/tool_util.c
|
||||||
|
index 9990a463..8ea37f37 100644
|
||||||
|
--- a/curl-7.66.0/src/tool_util.c
|
||||||
|
+++ b/curl-patched/src/tool_util.c
|
||||||
|
@@ -40,12 +40,7 @@ struct timeval tvnow(void)
|
||||||
|
** is typically in the range of 10 milliseconds to 16 milliseconds.
|
||||||
|
*/
|
||||||
|
struct timeval now;
|
||||||
|
-#if defined(_WIN32_WINNT) && (_WIN32_WINNT >= 0x0600) && \
|
||||||
|
- (!defined(__MINGW32__) || defined(__MINGW64_VERSION_MAJOR))
|
||||||
|
- ULONGLONG milliseconds = GetTickCount64();
|
||||||
|
-#else
|
||||||
|
DWORD milliseconds = GetTickCount();
|
||||||
|
-#endif
|
||||||
|
now.tv_sec = (long)(milliseconds / 1000);
|
||||||
|
now.tv_usec = (long)((milliseconds % 1000) * 1000);
|
||||||
|
return now;
|
||||||
|
diff --git a/curl-7.66.0/tests/server/util.c b/curl-patched/tests/server/util.c
|
||||||
|
index b0613380..00d0b0c3 100644
|
||||||
|
--- a/curl-7.66.0/tests/server/util.c
|
||||||
|
+++ b/curl-patched/tests/server/util.c
|
||||||
|
@@ -415,12 +415,7 @@ static struct timeval tvnow(void)
|
||||||
|
** is typically in the range of 10 milliseconds to 16 milliseconds.
|
||||||
|
*/
|
||||||
|
struct timeval now;
|
||||||
|
-#if defined(_WIN32_WINNT) && (_WIN32_WINNT >= 0x0600) && \
|
||||||
|
- (!defined(__MINGW32__) || defined(__MINGW64_VERSION_MAJOR))
|
||||||
|
- ULONGLONG milliseconds = GetTickCount64();
|
||||||
|
-#else
|
||||||
|
DWORD milliseconds = GetTickCount();
|
||||||
|
-#endif
|
||||||
|
now.tv_sec = (long)(milliseconds / 1000);
|
||||||
|
now.tv_usec = (long)((milliseconds % 1000) * 1000);
|
||||||
|
return now;
|
Loading…
Reference in New Issue