forked from lda/telodendria
Compare commits
1 commit
Author | SHA1 | Date | |
---|---|---|---|
95342f7ad1 |
213 changed files with 6706 additions and 29500 deletions
1
.cvsignore
Normal file
1
.cvsignore
Normal file
|
@ -0,0 +1 @@
|
||||||
|
build
|
2
.exrc
2
.exrc
|
@ -1,2 +0,0 @@
|
||||||
set tabstop=4
|
|
||||||
set expandtab
|
|
|
@ -1,49 +0,0 @@
|
||||||
name: Bug Report
|
|
||||||
about: File a bug report regarding Telodendria, its website, or its documentation.
|
|
||||||
body:
|
|
||||||
- type: markdown
|
|
||||||
attributes:
|
|
||||||
value: |
|
|
||||||
You are submitting a bug report. Please be sure to fill out the
|
|
||||||
title with a brief description of the bug.
|
|
||||||
- type: dropdown
|
|
||||||
attributes:
|
|
||||||
label: Type
|
|
||||||
description: Select the type of issue.
|
|
||||||
options:
|
|
||||||
- Memory Leak
|
|
||||||
- Crash
|
|
||||||
- Unexpected Error Message
|
|
||||||
- Documentation
|
|
||||||
- Website
|
|
||||||
- Other
|
|
||||||
validations:
|
|
||||||
required: true
|
|
||||||
- type: textarea
|
|
||||||
attributes:
|
|
||||||
label: Description
|
|
||||||
description: |
|
|
||||||
Please give a thorough and detailed description of the bug you
|
|
||||||
are reporting. Provide all the information you have, and do
|
|
||||||
some investigating to ensure you are providing a legitimate
|
|
||||||
issue report that is well thought out. **Include details on
|
|
||||||
how to reproduce the issue, or explicitly state that you were
|
|
||||||
unable to reproduce it.**
|
|
||||||
validations:
|
|
||||||
required: true
|
|
||||||
- type: textarea
|
|
||||||
attributes:
|
|
||||||
label: Log Output
|
|
||||||
description: |
|
|
||||||
Please copy and paste the relevant sections of the log output,
|
|
||||||
or the entire log if it is not unreasonably large. The logs
|
|
||||||
will be automatically formatted, no code block is necessary.
|
|
||||||
render: shell
|
|
||||||
validations:
|
|
||||||
required: true
|
|
||||||
- type: markdown
|
|
||||||
attributes:
|
|
||||||
value: |
|
|
||||||
Please attach any additional files that may aid in our
|
|
||||||
investigation of this issue, including screenshots, debugging
|
|
||||||
session stack traces and dumps, etc.
|
|
|
@ -1,7 +0,0 @@
|
||||||
blank_issues_enabled: false
|
|
||||||
contact_links:
|
|
||||||
- name: General Matrix Room
|
|
||||||
url: "https://matrix.to/#/#telodendria-general:bancino.net"
|
|
||||||
about: |
|
|
||||||
General discussion on Telodendria happens in this Matrix room. You
|
|
||||||
may get quicker feedback from there.
|
|
|
@ -1,18 +0,0 @@
|
||||||
name: Feature Request
|
|
||||||
about: Request a new feature or enhancement be added to Telodendria.
|
|
||||||
title: "[Feature] "
|
|
||||||
body:
|
|
||||||
- type: markdown
|
|
||||||
attributes:
|
|
||||||
value: |
|
|
||||||
You are submitting a feature request. Please be sure to fill
|
|
||||||
out the title with a brief description of the feature you are
|
|
||||||
requesting.
|
|
||||||
- type: textarea
|
|
||||||
attributes:
|
|
||||||
label: Description
|
|
||||||
description: |
|
|
||||||
Please give a thorough and detailed description of the feature
|
|
||||||
you are requesting.
|
|
||||||
validations:
|
|
||||||
required: true
|
|
|
@ -1,25 +0,0 @@
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
Please review the developer certificate of origin:
|
|
||||||
|
|
||||||
1. The contribution was created in whole or in part by me, and I have
|
|
||||||
the right to submit it under the open source licenses of the
|
|
||||||
Telodendria project; or
|
|
||||||
1. The contribution is based upon a previous work that, to the best of
|
|
||||||
my knowledge, is covered under an appropriate open source license and
|
|
||||||
I have the right under that license to submit that work with
|
|
||||||
modifications, whether created in whole or in part by me, under the
|
|
||||||
Telodendria project license; or
|
|
||||||
1. The contribution was provided directly to me by some other person
|
|
||||||
who certified (1), (2), or (3), and I have not modified it.
|
|
||||||
1. I understand and agree that this project and the contribution are
|
|
||||||
made public and that a record of the contribution—including all
|
|
||||||
personal information I submit with it—is maintained indefinitely
|
|
||||||
and may be redistributed consistent with this project or the open
|
|
||||||
source licenses involved.
|
|
||||||
|
|
||||||
- [ ] I have read the Telodendria Project development certificate of
|
|
||||||
origin, and I certify that I have permission to submit this patch
|
|
||||||
under the conditions specified in it.
|
|
||||||
|
|
2
.github/FUNDING.yml
vendored
2
.github/FUNDING.yml
vendored
|
@ -1,2 +0,0 @@
|
||||||
liberapay: "Telodendria"
|
|
||||||
custom: "https://donate.stripe.com/8wM29AfF5bRJc48eUU"
|
|
14
.gitignore
vendored
14
.gitignore
vendored
|
@ -1,14 +0,0 @@
|
||||||
build
|
|
||||||
data
|
|
||||||
.env
|
|
||||||
*.patch
|
|
||||||
*.log
|
|
||||||
vgcore.*
|
|
||||||
*.core
|
|
||||||
Cytoplasm/build
|
|
||||||
Cytoplasm/out
|
|
||||||
Cytoplasm/*-leaked.txt
|
|
||||||
contrib/.vagrant
|
|
||||||
src/Schema
|
|
||||||
src/include/Schema
|
|
||||||
man/mandoc.db
|
|
8
.idea/Telodendria.iml
Normal file
8
.idea/Telodendria.iml
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<module type="CPP_MODULE" version="4">
|
||||||
|
<component name="NewModuleRootManager">
|
||||||
|
<content url="file://$MODULE_DIR$" />
|
||||||
|
<orderEntry type="inheritedJdk" />
|
||||||
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
|
</component>
|
||||||
|
</module>
|
8
.idea/modules.xml
Normal file
8
.idea/modules.xml
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ProjectModuleManager">
|
||||||
|
<modules>
|
||||||
|
<module fileurl="file://$PROJECT_DIR$/.idea/Telodendria.iml" filepath="$PROJECT_DIR$/.idea/Telodendria.iml" />
|
||||||
|
</modules>
|
||||||
|
</component>
|
||||||
|
</project>
|
52
.idea/workspace.xml
Normal file
52
.idea/workspace.xml
Normal file
|
@ -0,0 +1,52 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="CMakeSettings">
|
||||||
|
<configurations>
|
||||||
|
<configuration PROFILE_NAME="Debug" ENABLED="true" CONFIG_NAME="Debug" />
|
||||||
|
</configurations>
|
||||||
|
</component>
|
||||||
|
<component name="ChangeListManager">
|
||||||
|
<list default="true" id="fb1df1f2-2a47-495d-89b9-277de6049bd1" name="Changes" comment="" />
|
||||||
|
<option name="SHOW_DIALOG" value="false" />
|
||||||
|
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
||||||
|
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
|
||||||
|
<option name="LAST_RESOLUTION" value="IGNORE" />
|
||||||
|
</component>
|
||||||
|
<component name="ClangdSettings">
|
||||||
|
<option name="formatViaClangd" value="false" />
|
||||||
|
</component>
|
||||||
|
<component name="MarkdownSettingsMigration">
|
||||||
|
<option name="stateVersion" value="1" />
|
||||||
|
</component>
|
||||||
|
<component name="ProjectId" id="2CD2pGJ1okjE4N8lOPrGKmwbXGw" />
|
||||||
|
<component name="ProjectViewState">
|
||||||
|
<option name="hideEmptyMiddlePackages" value="true" />
|
||||||
|
<option name="showLibraryContents" value="true" />
|
||||||
|
</component>
|
||||||
|
<component name="PropertiesComponent">{
|
||||||
|
"keyToString": {
|
||||||
|
"RunOnceActivity.OpenProjectViewOnStart": "true",
|
||||||
|
"RunOnceActivity.ShowReadmeOnStart": "true",
|
||||||
|
"RunOnceActivity.cidr.known.project.marker": "true",
|
||||||
|
"WebServerToolWindowFactoryState": "false",
|
||||||
|
"cf.first.check.clang-format": "false",
|
||||||
|
"cidr.known.project.marker": "true"
|
||||||
|
}
|
||||||
|
}</component>
|
||||||
|
<component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="application-level" UseSingleDictionary="true" transferred="true" />
|
||||||
|
<component name="TaskManager">
|
||||||
|
<task active="true" id="Default" summary="Default task">
|
||||||
|
<changelist id="fb1df1f2-2a47-495d-89b9-277de6049bd1" name="Changes" comment="" />
|
||||||
|
<created>1658323016759</created>
|
||||||
|
<option name="number" value="Default" />
|
||||||
|
<option name="presentableId" value="Default" />
|
||||||
|
<updated>1658323016759</updated>
|
||||||
|
<workItem from="1658323018463" duration="25597000" />
|
||||||
|
<workItem from="1658450495358" duration="6933000" />
|
||||||
|
</task>
|
||||||
|
<servers />
|
||||||
|
</component>
|
||||||
|
<component name="TypeScriptGeneratedFilesManager">
|
||||||
|
<option name="version" value="3" />
|
||||||
|
</component>
|
||||||
|
</project>
|
28
Cytoplasm/.indent.pro
vendored
28
Cytoplasm/.indent.pro
vendored
|
@ -1,28 +0,0 @@
|
||||||
-bad
|
|
||||||
-bap
|
|
||||||
-bbb
|
|
||||||
-nbc
|
|
||||||
-bl
|
|
||||||
-c36
|
|
||||||
-cd36
|
|
||||||
-ncdb
|
|
||||||
-nce
|
|
||||||
-ci8
|
|
||||||
-cli1
|
|
||||||
-d0
|
|
||||||
-di1
|
|
||||||
-ndj
|
|
||||||
-ei
|
|
||||||
-fc1
|
|
||||||
-i4
|
|
||||||
-ip
|
|
||||||
-l72
|
|
||||||
-lc72
|
|
||||||
-lp
|
|
||||||
-npcs
|
|
||||||
-psl
|
|
||||||
-sc
|
|
||||||
-nsob
|
|
||||||
-nut
|
|
||||||
-nv
|
|
||||||
|
|
|
@ -1,23 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright (C) 2022-2023 Jordan Bancino <@jordan:bancino.net>
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person
|
|
||||||
* obtaining a copy of this software and associated documentation files
|
|
||||||
* (the "Software"), to deal in the Software without restriction,
|
|
||||||
* including without limitation the rights to use, copy, modify, merge,
|
|
||||||
* publish, distribute, sublicense, and/or sell copies of the Software,
|
|
||||||
* and to permit persons to whom the Software is furnished to do so,
|
|
||||||
* subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be
|
|
||||||
* included in all copies or portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
||||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
||||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
||||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
||||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
||||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
||||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
||||||
* SOFTWARE.
|
|
||||||
*/
|
|
|
@ -1,78 +0,0 @@
|
||||||
# Cytoplasm (libcytoplasm)
|
|
||||||
|
|
||||||
Cytoplasm is a general-purpose C library and runtime stub for creating high-level (particularly networked and multi-threaded) C applications. It allows applications to take advantage of the speed, flexibility, and simplicity of the C programming language, while providing helpful code to perform various complex tasks. Cytoplasm provides high-level data structures, a basic logging facility, an HTTP client and server, and more.
|
|
||||||
|
|
||||||
Cytoplasm aims not to only do one thing well, but to do many things good enough. The primary target of Cytoplasm is simple, yet higher level C applications that have to perform relatively complex tasks, but don't want to pull in a large number of dependencies.
|
|
||||||
|
|
||||||
Cytoplasm is extremely opinionated on the way programs using it are written. It strives to create a comprehensive and tightly-integrated programming environment, while also maintaining C programming correctness. It doesn't do any macro magic or make C look like anything other than C. It is written entirely in C89, and depends only on a POSIX environment. This differentiates it from other general-purpose libraries that often require modern compilers and non-standard language and environment features. Cytoplasm is intended to be extremely portable and simple, while still providing some of the functionality expected in higher-level programming languages in a platform-agnostic manner. In the case of TLS, Cytoplasm wraps low-level TLS libraries to offer a single, unified interface to TLS so that programs do not have to care about the underlying implementation.
|
|
||||||
|
|
||||||
Cytoplasm is probably not suitable for embedded programming. It makes liberal use of the heap, and while data structures are designed to conserve memory where possible and practical, minimal memory usage is not really a design goal for Cytoplasm, although Cytoplasm takes care not to use any more memory than it absolutely needs. Cytoplasm also wraps a few standard libraries with additional logic and checking. While this ensures better runtime safetly, this inevitably adds a little overhead.
|
|
||||||
|
|
||||||
Originally a part of Telodendria (https://telodendria.io), a Matrix homeserver written in C, Cytoplasm was split off into its own project due to the desire of some Telodendria developers to use Telodendria's code in other projects. Cytoplasm is still a Telodendria project, and is maintained along side of Telodendria itself, even living in the same CVS module, but it is designed specifically to be distributed and used totally independent of Telodendria.
|
|
||||||
|
|
||||||
The name "Cytoplasm" was chosen for a few reasons. It plays off the precedent set up by the Matrix organization in naming projects after the parts of a neuron. It also speaks to the function of Cytoplasm. The cytoplasm of a cell is the supporting material. It is what gives the cell its shape, and it facilitates the movement of materials to the other cell parts. Likewise, Cytoplasm aims to provide a support mechanism for C applications that have to perform complex tasks.
|
|
||||||
|
|
||||||
Cytoplasm also starts with a C, which I think is a nice touch for C libraries. It's also fun to say and unique enough that searching for "libcytoplasm" should bring you to this project and not some other one.
|
|
||||||
|
|
||||||
## Building
|
|
||||||
|
|
||||||
If your operating system or software distribution provides a pre-built package of Cytoplasm, you should prefer to use that instead of building it from source.
|
|
||||||
|
|
||||||
Cytoplasm aims to have zero dependencies beyond what is mandated by POSIX. You only need the standard math and pthread libraries to build it. TLS support can optionally be enabled by setting the TLS_IMPL environment variable. The supported TLS implementations are as follows:
|
|
||||||
|
|
||||||
- OpenSSL
|
|
||||||
- LibreSSL
|
|
||||||
|
|
||||||
If TLS support is not enabled, all APIs that use it should fall back to non-TLS behavior in a sensible manner. For example, if TLS support is not enabled, then the HTTP client API will simply return an error if a TLS connection is requested. Cytoplasm uses a custom build script instead of Make, for the sake of portability. To build everything, just run the script:
|
|
||||||
|
|
||||||
$ sh make.sh
|
|
||||||
|
|
||||||
This will produce the following out/ directory:
|
|
||||||
|
|
||||||
```
|
|
||||||
out/
|
|
||||||
lib/
|
|
||||||
libcytoplasm.so - The Cytoplasm shared library.
|
|
||||||
libcytoplasm.a - The Cytoplasm static library.
|
|
||||||
cytoplasm.o - The Cytoplasm runtime stub.
|
|
||||||
bin/
|
|
||||||
hdoc - The documentation generator tool.
|
|
||||||
man/ - All Cytoplasm API documentation.
|
|
||||||
```
|
|
||||||
|
|
||||||
## Usage
|
|
||||||
|
|
||||||
Cytoplasm provides the typical .so and .a files, which can be used to link programs with it in the usual way. However, Cytoplasm also provides a minimal runtime environment that is intended to be used with the library. As such, it also provides a runtime stub, which is intended to be linked in with programs using Cytoplasm. This stub is responsible for setting up and tearing down some Cytoplasm APIs. While it isn't required by any means, it makes Cytoplasm a lot easier to use for programmers by abstracting out all of the common logic that most programs will want to use.
|
|
||||||
|
|
||||||
Here is the canonical Hello World written with Cytoplasm:
|
|
||||||
|
|
||||||
```c
|
|
||||||
#include <Log.h>
|
|
||||||
|
|
||||||
int Main(void)
|
|
||||||
{
|
|
||||||
Log(LOG_INFO, "Hello World!");
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
If this file is Hello.c, then you can compile it by doing something like this:
|
|
||||||
|
|
||||||
$ cc -o hello Hello.c cytoplasm.o -lcytoplasm
|
|
||||||
|
|
||||||
This command assumes that the runtime stub resides in the current working directory, and that libcytoplasm.so is in your library path. If you're using the version of Cytoplasm installed by your operating system or software distribution, consult the documentation for the location of the runtime stub. It may be located in /usr/local/libexec or some other similar location. If you've built Cytoplasm from source and wish to link to it from there, you may wish to do something like this:
|
|
||||||
|
|
||||||
$ export CYTOPLASM=/path/to/Cytoplasm/out/lib
|
|
||||||
$ cc -o hello Hello.c "${CYTOPLASM}/cytoplasm.o" \
|
|
||||||
"-L${CYTOPLASM}" -lcytoplasm
|
|
||||||
|
|
||||||
As you may have noticed, C programs using Cytoplasm's runtime stub don't write the main() function. Instead, they use Main(). The main() function is provided by the runtime stub. The full form of Main() expected by the stub is as follows:
|
|
||||||
|
|
||||||
```c
|
|
||||||
int Main(Array *args, HashMap *env);
|
|
||||||
```
|
|
||||||
|
|
||||||
The first argument is a Cytoplasm array of the command line arguments, and the second is a Cytoplasm hash map of environment variables. Most linkers will let programs omit the env argument, or both arguments if you don't need either. The return value of Main() is returned to the operating system.
|
|
||||||
|
|
||||||
Note that both arguments to Main may be treated like any other array or hash map. However, do not invoke ArrayFree() or HashMapFree() on the passed pointers, because memory is cleaned up after Main() returns.
|
|
||||||
|
|
|
@ -1,28 +0,0 @@
|
||||||
#include <Array.h>
|
|
||||||
#include <HashMap.h>
|
|
||||||
|
|
||||||
#include <Log.h>
|
|
||||||
|
|
||||||
int
|
|
||||||
Main(Array * args, HashMap * env)
|
|
||||||
{
|
|
||||||
size_t i;
|
|
||||||
char *key;
|
|
||||||
char *val;
|
|
||||||
|
|
||||||
Log(LOG_INFO, "Hello World!");
|
|
||||||
Log(LOG_INFO, "Arguments: %lu", ArraySize(args));
|
|
||||||
|
|
||||||
for (i = 0; i < ArraySize(args); i++)
|
|
||||||
{
|
|
||||||
Log(LOG_INFO, " [%ld] %s", i, ArrayGet(args, i));
|
|
||||||
}
|
|
||||||
|
|
||||||
Log(LOG_INFO, "Environment:");
|
|
||||||
while (HashMapIterate(env, &key, (void **) &val))
|
|
||||||
{
|
|
||||||
Log(LOG_INFO, " %s = %s", key, val);
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
|
@ -1,328 +0,0 @@
|
||||||
#!/usr/bin/env sh
|
|
||||||
|
|
||||||
addprefix() {
|
|
||||||
prefix="$1"
|
|
||||||
shift
|
|
||||||
for thing in "$@"; do
|
|
||||||
echo "${prefix}${thing}"
|
|
||||||
done
|
|
||||||
|
|
||||||
unset prefix
|
|
||||||
unset thing
|
|
||||||
}
|
|
||||||
|
|
||||||
: "${NAME:=Cytoplasm}"
|
|
||||||
: "${LIB_NAME:=$(echo ${NAME} | tr '[A-Z]' '[a-z]')}"
|
|
||||||
: "${VERSION:=0.4.0}"
|
|
||||||
|
|
||||||
: "${CVS_TAG:=${NAME}-$(echo ${VERSION} | sed 's/\./_/g')}"
|
|
||||||
|
|
||||||
|
|
||||||
: "${SRC:=src}"
|
|
||||||
: "${TOOLS:=tools}"
|
|
||||||
: "${EXAMPLES:=examples}"
|
|
||||||
: "${BUILD:=build}"
|
|
||||||
: "${OUT:=out}"
|
|
||||||
: "${STUB:=RtStub}"
|
|
||||||
: "${LICENSE:=LICENSE.txt}"
|
|
||||||
|
|
||||||
: "${CC:=cc}"
|
|
||||||
: "${AR:=ar}"
|
|
||||||
|
|
||||||
: "${DEFINES:=-D_DEFAULT_SOURCE -DLIB_NAME=\"${NAME}\" -DLIB_VERSION=\"${VERSION}\"}"
|
|
||||||
: "${INCLUDE:=${SRC}/include}"
|
|
||||||
|
|
||||||
: "${CFLAGS:=-Wall -Wextra -pedantic -std=c89 -O3 -pipe}"
|
|
||||||
: "${LD_EXTRA:=-flto -fdata-sections -ffunction-sections -s -Wl,-gc-sections}"
|
|
||||||
: "${LDFLAGS:=-lm -pthread}"
|
|
||||||
|
|
||||||
if [ "${DEBUG}" = "1" ]; then
|
|
||||||
CFLAGS="${CFLAGS} -O0 -g"
|
|
||||||
LD_EXTRA=""
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [ -n "${TLS_IMPL}" ]; then
|
|
||||||
case "${TLS_IMPL}" in
|
|
||||||
"LIBRESSL")
|
|
||||||
TLS_LIBS="-ltls -lcrypto -lssl"
|
|
||||||
;;
|
|
||||||
"OPENSSL")
|
|
||||||
TLS_LIBS="-lcrypto -lssl"
|
|
||||||
;;
|
|
||||||
*)
|
|
||||||
echo "Unrecognized TLS implementation: ${TLS_IMPL}"
|
|
||||||
echo "Consult Tls.h for supported implementations."
|
|
||||||
echo "Note that the TLS_ prefix is omitted in TLS_IMPL."
|
|
||||||
exit 1
|
|
||||||
;;
|
|
||||||
esac
|
|
||||||
|
|
||||||
DEFINES="${DEFINES} -DTLS_IMPL=TLS_${TLS_IMPL}"
|
|
||||||
LDFLAGS="${LDFLAGS} ${TLS_LIBS}"
|
|
||||||
fi
|
|
||||||
|
|
||||||
CFLAGS="${CFLAGS} ${DEFINES} $(addprefix -I$(pwd)/ ${INCLUDE})"
|
|
||||||
LDFLAGS="${LDFLAGS} ${LD_EXTRA}"
|
|
||||||
|
|
||||||
# Check the modificiation time of a file. This is used to do
|
|
||||||
# incremental builds; we only want to rebuild files that have
|
|
||||||
# have changed.
|
|
||||||
mod_time() {
|
|
||||||
if [ -n "$1" ] && [ -f "$1" ]; then
|
|
||||||
case "$(uname)" in
|
|
||||||
Linux | CYGWIN_NT* | Haiku)
|
|
||||||
stat -c %Y "$1"
|
|
||||||
;;
|
|
||||||
*BSD | DragonFly | Minix)
|
|
||||||
stat -f %m "$1"
|
|
||||||
;;
|
|
||||||
*)
|
|
||||||
# Platform unknown, force rebuilding the whole
|
|
||||||
# project every time.
|
|
||||||
echo "0"
|
|
||||||
;;
|
|
||||||
esac
|
|
||||||
else
|
|
||||||
echo "0"
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
# Substitute shell variables in a stream with their actual value
|
|
||||||
# in this shell.
|
|
||||||
setsubst() {
|
|
||||||
SED="/tmp/sed-$RANDOM.txt"
|
|
||||||
|
|
||||||
(
|
|
||||||
set | while IFS='=' read -r var val; do
|
|
||||||
val=$(echo "$val" | cut -d "'" -f 2- | rev | cut -d "'" -f 2- | rev)
|
|
||||||
echo "s|\\\${$var}|$val|g"
|
|
||||||
done
|
|
||||||
|
|
||||||
echo "s|\\\${[a-zA-Z_]*}||g"
|
|
||||||
echo "s|'''|'|g"
|
|
||||||
) >"$SED"
|
|
||||||
|
|
||||||
sed -f "$SED" $@
|
|
||||||
rm "$SED"
|
|
||||||
}
|
|
||||||
|
|
||||||
recipe_docs() {
|
|
||||||
export LD_LIBRARY_PATH="${OUT}/lib"
|
|
||||||
mkdir -p "${OUT}/man/man3"
|
|
||||||
|
|
||||||
for header in $(find ${INCLUDE} -name '*.h'); do
|
|
||||||
basename=$(basename "$header")
|
|
||||||
man=$(echo "${OUT}/man/man3/$basename" | sed -e 's/\.h$/\.3/')
|
|
||||||
|
|
||||||
if [ $(mod_time "$header") -ge $(mod_time "$man") ]; then
|
|
||||||
echo "DOC $basename"
|
|
||||||
if ! "${OUT}/bin/hdoc" -D "Os=${NAME}" -i "$header" -o "$man"; then
|
|
||||||
rm "$man"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
|
|
||||||
if which makewhatis 2>&1 > /dev/null; then
|
|
||||||
makewhatis "${OUT}/man"
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
recipe_libs() {
|
|
||||||
echo "-lm -pthread ${TLS_LIBS}"
|
|
||||||
}
|
|
||||||
|
|
||||||
recipe_build() {
|
|
||||||
mkdir -p "${BUILD}" "${OUT}/bin" "${OUT}/lib"
|
|
||||||
cd "${SRC}"
|
|
||||||
|
|
||||||
|
|
||||||
echo "CC = ${CC}"
|
|
||||||
echo "CFLAGS = ${CFLAGS}"
|
|
||||||
echo "LDFLAGS = ${LDFLAGS}"
|
|
||||||
echo
|
|
||||||
|
|
||||||
do_rebuild=0
|
|
||||||
objs=""
|
|
||||||
for src in $(find . -name '*.c' | cut -d '/' -f 2-); do
|
|
||||||
obj=$(echo "${BUILD}/$src" | sed -e 's/\.c$/\.o/')
|
|
||||||
|
|
||||||
if [ $(basename "$obj" .o) != "${STUB}" ]; then
|
|
||||||
objs="$objs $obj"
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [ $(mod_time "$src") -ge $(mod_time "../$obj") ]; then
|
|
||||||
echo "CC $(basename $obj)"
|
|
||||||
obj_dir=$(dirname "../$obj")
|
|
||||||
mkdir -p "$obj_dir"
|
|
||||||
if ! $CC $CFLAGS -fPIC -c -o "../$obj" "$src"; then
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
do_rebuild=1
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
|
|
||||||
cd ..
|
|
||||||
|
|
||||||
if [ $do_rebuild -eq 1 ] || [ ! -f "${OUT}/lib/lib${LIB_NAME}.a" ]; then
|
|
||||||
echo "AR lib${LIB_NAME}.a"
|
|
||||||
if ! $AR rcs "${OUT}/lib/lib${LIB_NAME}.a" $objs; then
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [ $do_rebuild -eq 1 ] || [ ! -f "${OUT}/lib/lib${LIB_NAME}.so" ]; then
|
|
||||||
echo "LD lib${LIB_NAME}.so"
|
|
||||||
if ! $CC -shared -o "${OUT}/lib/lib${LIB_NAME}.so" $objs ${LDFLAGS}; then
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
|
|
||||||
cp "${BUILD}/${STUB}.o" "${OUT}/lib/${LIB_NAME}.o"
|
|
||||||
|
|
||||||
for src in $(find "${TOOLS}" -name '*.c'); do
|
|
||||||
out=$(basename "$src" .c)
|
|
||||||
out="${OUT}/bin/$out"
|
|
||||||
|
|
||||||
if [ $(mod_time "$src") -ge $(mod_time "$out") ] || [ $do_rebuild -eq 1 ]; then
|
|
||||||
echo "CC $(basename $out)"
|
|
||||||
mkdir -p "$(dirname $out)"
|
|
||||||
if ! $CC $CFLAGS -o "$out" "$src" "${OUT}/lib/${LIB_NAME}.o" "-L${OUT}/lib" "-l${LIB_NAME}" ${LDFLAGS}; then
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
|
|
||||||
recipe_docs
|
|
||||||
}
|
|
||||||
|
|
||||||
recipe_examples() {
|
|
||||||
for src in $(find "${EXAMPLES}" -name '*.c'); do
|
|
||||||
out=$(basename "$src" .c)
|
|
||||||
out="${OUT}/bin/$out"
|
|
||||||
|
|
||||||
if [ $(mod_time "$src") -ge $(mod_time "$out") ]; then
|
|
||||||
echo "CC $(basename $out)"
|
|
||||||
mkdir -p "$(dirname $out)"
|
|
||||||
if ! $CC $CFLAGS -o "$out" "$src" "${OUT}/lib/${LIB_NAME}.o" "-L${OUT}/lib" "-l${LIB_NAME}" ${LDFLAGS}; then
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
recipe_clean() {
|
|
||||||
rm -r "${BUILD}" "${OUT}"
|
|
||||||
}
|
|
||||||
|
|
||||||
# Update copyright comments in sources and header files.
|
|
||||||
recipe_license() {
|
|
||||||
find . -name '*.[ch]' | while IFS= read -r src; do
|
|
||||||
if [ -t 1 ]; then
|
|
||||||
printf "LICENSE %s%*c\r" $(basename "$src") "16" " "
|
|
||||||
fi
|
|
||||||
srcHeader=$(grep -n -m 1 '^ \*/' "$src" | cut -d ':' -f 1)
|
|
||||||
head "-n$srcHeader" "$src" |
|
|
||||||
diff -u -p - "${LICENSE}" |
|
|
||||||
patch "$src" | grep -v "^Hmm"
|
|
||||||
done
|
|
||||||
if [ -t 1 ]; then
|
|
||||||
printf "%*c\n" "50" " "
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
# Format source code files by running indent(1) on them.
|
|
||||||
recipe_format() {
|
|
||||||
find . -name '*.c' | while IFS= read -r src; do
|
|
||||||
if [ -t 1 ]; then
|
|
||||||
printf "FMT %s%*c\r" $(basename "$src") "16" " "
|
|
||||||
fi
|
|
||||||
if indent "$src"; then
|
|
||||||
rm $(basename "$src").BAK
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
if [ -t 1 ]; then
|
|
||||||
printf "%*c\n" "50" " "
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
# Generate a release tarball, checksum and sign it, and push it to
|
|
||||||
# the web root.
|
|
||||||
recipe_release() {
|
|
||||||
# Tag the release at this point in time.
|
|
||||||
cvs tag "$CVS_TAG"
|
|
||||||
|
|
||||||
mkdir -p "${OUT}/release"
|
|
||||||
cd "${OUT}/release"
|
|
||||||
|
|
||||||
# Generate the release tarball.
|
|
||||||
cvs export "-r$CVS_TAG" "${NAME}"
|
|
||||||
mv "${NAME}" "${NAME}-v${VERSION}"
|
|
||||||
tar -czf "${NAME}-v${VERSION}.tar.gz" "${NAME}-v${VERSION}"
|
|
||||||
rm -r "${NAME}-v${VERSION}"
|
|
||||||
|
|
||||||
# Checksum the release tarball.
|
|
||||||
sha256 "${NAME}-v${VERSION}.tar.gz" > "${NAME}-v${VERSION}.tar.gz.sha256"
|
|
||||||
|
|
||||||
# Sign the release tarball.
|
|
||||||
if [ ! -z "${SIGNIFY_SECRET}" ]; then
|
|
||||||
signify -S -s "${SIGNIFY_SECRET}" \
|
|
||||||
-m "${NAME}-v${VERSION}.tar.gz" \
|
|
||||||
-x "${NAME}-v${VERSION}.tar.gz.sig"
|
|
||||||
else
|
|
||||||
echo "Warning: SIGNIFY_SECRET not net."
|
|
||||||
echo "The built tarball will not be signed."
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
recipe_patch() {
|
|
||||||
# If the user has not set their MXID, try to deduce one from
|
|
||||||
# their system.
|
|
||||||
if [ -z "$MXID" ]; then
|
|
||||||
MXID="@${USER}:$(hostname)"
|
|
||||||
fi
|
|
||||||
|
|
||||||
# If the user has not set their EDITOR, use a safe default.
|
|
||||||
# (vi should be available on any POSIX system)
|
|
||||||
if [ -z "$EDITOR" ]; then
|
|
||||||
EDITOR=vi
|
|
||||||
fi
|
|
||||||
|
|
||||||
NORMALIZED_MXID=$(echo "$MXID" | sed -e 's/@//g' -e 's/:/-/g')
|
|
||||||
PATCH_FILE="${NORMALIZED_MXID}_$(date +%s).patch"
|
|
||||||
|
|
||||||
# Generate the actual patch file
|
|
||||||
# Here we write some nice headers, and then do a cvs diff.
|
|
||||||
(
|
|
||||||
printf 'From: "%s" <%s>\n' "$DISPLAY_NAME" "$MXID"
|
|
||||||
echo "Date: $(date)"
|
|
||||||
echo "Subject: "
|
|
||||||
echo
|
|
||||||
cvs -q diff -uNp $PATCHSET | grep -v '^\? '
|
|
||||||
) >"$PATCH_FILE"
|
|
||||||
|
|
||||||
"$EDITOR" "$PATCH_FILE"
|
|
||||||
echo "$PATCH_FILE"
|
|
||||||
}
|
|
||||||
|
|
||||||
recipe_diff() {
|
|
||||||
tmp_patch="/tmp/${NAME}-$(date +%s).patch"
|
|
||||||
cvs -q diff -uNp $PATCHSET >"$tmp_patch"
|
|
||||||
if [ -z "$PAGER" ]; then
|
|
||||||
PAGER="less -F"
|
|
||||||
fi
|
|
||||||
|
|
||||||
$PAGER "$tmp_patch"
|
|
||||||
rm "$tmp_patch"
|
|
||||||
}
|
|
||||||
|
|
||||||
# Execute the user-specified recipes.
|
|
||||||
for recipe in $@; do
|
|
||||||
recipe_$recipe
|
|
||||||
done
|
|
||||||
|
|
||||||
# If no recipe was provided, run a build.
|
|
||||||
if [ -z "$1" ]; then
|
|
||||||
recipe_build
|
|
||||||
fi
|
|
|
@ -1,51 +0,0 @@
|
||||||
.Dd $Mdocdate: May 21 2023 $
|
|
||||||
.Dt HDOC 1
|
|
||||||
.Os Cytoplasm
|
|
||||||
.Sh NAME
|
|
||||||
.Nm hdoc
|
|
||||||
.Nd Generate documentation from a C header file.
|
|
||||||
.Sh SYNOPSIS
|
|
||||||
.Nm
|
|
||||||
.Op Fl i Ar file
|
|
||||||
.Op Fl o Ar file
|
|
||||||
.Op Fl D Ar key=value
|
|
||||||
.Sh DESCRIPTION
|
|
||||||
.Nm
|
|
||||||
is an extremely simple documentation generator that generates
|
|
||||||
a BSD man page from a specially-formatted C header file.
|
|
||||||
See
|
|
||||||
.Xr hdoc 5
|
|
||||||
for details on the format of this header file.
|
|
||||||
.Pp
|
|
||||||
The options are as follows:
|
|
||||||
.Bl -tag -width Ds
|
|
||||||
.It Fl i Ar file
|
|
||||||
The input C header file to read from. If this option is omitted,
|
|
||||||
then the standard input is read.
|
|
||||||
.It Fl o Ar file
|
|
||||||
The output BSD man page file to write. If this option is omitted,
|
|
||||||
then the standard output is written.
|
|
||||||
.It Fl D Ar key=value
|
|
||||||
Set the register
|
|
||||||
.Ar key
|
|
||||||
to
|
|
||||||
.Ar value .
|
|
||||||
.Nm
|
|
||||||
registers are used in various places, and can be set in either
|
|
||||||
the header file, or on the command line. Setting registers that
|
|
||||||
should be the same in all headers is best done from the command
|
|
||||||
line for maintainability purposes, but header-specific values
|
|
||||||
should be set in the header file itself.
|
|
||||||
.Pp
|
|
||||||
Registers are explained in more detail in
|
|
||||||
.Xr hdoc 5 .
|
|
||||||
.El
|
|
||||||
.Sh RETURN VALUE
|
|
||||||
.Pp
|
|
||||||
.Nm
|
|
||||||
returns a success value if the header is well-formed and the
|
|
||||||
man page is successfully generated. It returns an error code in
|
|
||||||
all other scenarios.
|
|
||||||
.Sh SEE ALSO
|
|
||||||
.Xr hdoc 5 ,
|
|
||||||
.Xr HeaderParser 3
|
|
|
@ -1,195 +0,0 @@
|
||||||
.Dd $Mdocdate: May 21 2023 $
|
|
||||||
.Dt HDOC 5
|
|
||||||
.Os Cytoplasm
|
|
||||||
.Sh NAME
|
|
||||||
.Nm hdoc
|
|
||||||
.Nd C header file format accepted by the documentation generator.
|
|
||||||
.Sh DESCRIPTION
|
|
||||||
.Pp
|
|
||||||
.Nm
|
|
||||||
uses an extremely primitive parser to generate documentation from
|
|
||||||
C header files. As such, the format accepted by
|
|
||||||
.Nm
|
|
||||||
is rather strict and may not be suitable for other projects beyond
|
|
||||||
of Cytoplasm. This document describes what
|
|
||||||
.Nm
|
|
||||||
considers to be a valid C header file, and how that header file can
|
|
||||||
be annotated to produce a nicely-formatted man page.
|
|
||||||
.Pp
|
|
||||||
At the very top level, a C header is a sequences of tokens that
|
|
||||||
represent the following:
|
|
||||||
.Bl -bullet -offset indent
|
|
||||||
.It
|
|
||||||
An ANSI C89 comment.
|
|
||||||
.It
|
|
||||||
A preprocessor directive.
|
|
||||||
.It
|
|
||||||
A typedef declaration.
|
|
||||||
.It
|
|
||||||
A constant or other global variable declaration.
|
|
||||||
.It
|
|
||||||
A function declaration.
|
|
||||||
.El
|
|
||||||
.Pp
|
|
||||||
Note that global variables and functions
|
|
||||||
.Em must
|
|
||||||
be marked with
|
|
||||||
.Ar extern ,
|
|
||||||
otherwise the parser will fail to recognize them. This is by
|
|
||||||
design; a header should make everything extern by default,
|
|
||||||
because it does not actually implement or declare anything.
|
|
||||||
.Pp
|
|
||||||
Preprocessor directives are completely ignored. Regular C
|
|
||||||
comments are also ignored.
|
|
||||||
.Nm
|
|
||||||
is primarily concerned with type declarations, global
|
|
||||||
variables, and functions. It also inspects specially-formatted
|
|
||||||
C comments, which are used to annotate these elements of the
|
|
||||||
header. The format of these comments is described here.
|
|
||||||
.Pp
|
|
||||||
There are two types of special comments recognized, the first
|
|
||||||
of which is called the ``main'' comment block, as it documents
|
|
||||||
the header itself, not the declarations contained in it. Main
|
|
||||||
comment blocks also control the behavior of the parser and the
|
|
||||||
resulting man page by setting registers. The format of the
|
|
||||||
main comment block, which typically appears only once at the
|
|
||||||
top of the header, although this is not a requirement, is as
|
|
||||||
follows:
|
|
||||||
.Bl -bullet -offset indent
|
|
||||||
.It
|
|
||||||
The comment should start with a ``triple star,'' like this:
|
|
||||||
.Bd -literal -offset indent
|
|
||||||
/***
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
.Ed
|
|
||||||
.It
|
|
||||||
Any lines that start with a ``@'' are parser directives that
|
|
||||||
set registers. The name of the register to set follows
|
|
||||||
immediately after the ``@'' sigil, and continues until the first
|
|
||||||
whitespace. The rest of the line is the value of the register.
|
|
||||||
A list of registers recognized by
|
|
||||||
.Nm
|
|
||||||
is as follows. These registers control the man page output,
|
|
||||||
and the last value set is the one that is used:
|
|
||||||
.Bl -tag -width Ds
|
|
||||||
.It \&Nm
|
|
||||||
The name register. The name of the man page will be set to the
|
|
||||||
contents of this register. It defaults to ``Unnmamed.''
|
|
||||||
.It \&Dd
|
|
||||||
The date register. The date of the man page will be set to the
|
|
||||||
contents of this register. If left unset, it defaults to the
|
|
||||||
current date.
|
|
||||||
.It \&Os
|
|
||||||
The operating system register. The Os value, which appears in
|
|
||||||
the bottom left and right corners of the man page, will be set
|
|
||||||
to the contents of this register. If left unset, it is not
|
|
||||||
output.
|
|
||||||
.It \&Nd
|
|
||||||
The description register. The description of the man page will
|
|
||||||
be set to the contents of this register. It defaults to
|
|
||||||
``No description.''
|
|
||||||
.It \&Xr
|
|
||||||
The cross reference register. The SEE ALSO section of the man
|
|
||||||
page will list the specified cross references, which are to be
|
|
||||||
separated by a single space. The section shall be omitted,
|
|
||||||
because it is set automatically to the same section that the
|
|
||||||
current man page will belong to. This limitation may be removed
|
|
||||||
in the future.
|
|
||||||
.El
|
|
||||||
.Pp
|
|
||||||
These registers control the parser itself, modifying its
|
|
||||||
behavior as soon as the appear in the file:
|
|
||||||
.Bl -tag -width Ds
|
|
||||||
.It ignore-typedefs
|
|
||||||
Don't throw an error for an undocumented type declaration.
|
|
||||||
The value doesn't matter; as soon as this register shows
|
|
||||||
up, it's set. In most cases, it should not be used, however,
|
|
||||||
it may be helpful in a few scenarios, such as when there are
|
|
||||||
multiple typedefs that do the same thing, but are controlled
|
|
||||||
by preprocessor macros.
|
|
||||||
.It suppress-warnings
|
|
||||||
Don't issue a warning for invalid or unrecognized top-level
|
|
||||||
tokens. They will instead be ignored until the next
|
|
||||||
recognized top-level token is found. The value doesn't
|
|
||||||
matter; as soon as this register shows up, it is set. In most
|
|
||||||
cases, it should not be used, however it may be helpful in a
|
|
||||||
few scenarios, such as when function declarations are generated
|
|
||||||
by preprocessor directives and thus don't follow the standard
|
|
||||||
form.
|
|
||||||
.El
|
|
||||||
.It
|
|
||||||
Any other lines in the main block are output to the DESCRIPTION
|
|
||||||
section of the main page. This description may contain mdoc
|
|
||||||
directives in it, as the lines are copied verbatim. If multiple
|
|
||||||
main comment blocks appear in a single header, their description
|
|
||||||
lines are appended in the order they appear.
|
|
||||||
.El
|
|
||||||
.Pp
|
|
||||||
Declaration documentation comments are created as follows:
|
|
||||||
.Bl -bullet -offset indent
|
|
||||||
.It
|
|
||||||
The comment should start with a ``double star,'' like this:
|
|
||||||
.Bd -literal -offset indent
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
.Ed
|
|
||||||
.It
|
|
||||||
The contents of the comment are copied verbatim into the output,
|
|
||||||
so the comment may contain mdoc directives.
|
|
||||||
.It
|
|
||||||
The comment must appear before a declaration. If multiple
|
|
||||||
documentation comments appear before a declaration, the last
|
|
||||||
one before the declaration is used.
|
|
||||||
.El
|
|
||||||
.Pp
|
|
||||||
The generated man page includes the name and description of the
|
|
||||||
header, a synopsis section that lists all of the functions in
|
|
||||||
the header, a description section that contains all the non-register
|
|
||||||
lines of the main comment blocks, and then all of the documentation
|
|
||||||
for each function, with the function prototype displayed as a
|
|
||||||
subsection header, and the documentation displayed under it.
|
|
||||||
.Sh EXAMPLES
|
|
||||||
.Pp
|
|
||||||
Consider the following simple C header:
|
|
||||||
.Bd -literal -offset indent
|
|
||||||
#include <stdio.h>
|
|
||||||
|
|
||||||
extern void SayHello(FILE *);
|
|
||||||
.Ed
|
|
||||||
.Pp
|
|
||||||
To annotate this header in the manner
|
|
||||||
.Nm
|
|
||||||
expects, do something like this:
|
|
||||||
.Bd -literal -offset indent
|
|
||||||
/***
|
|
||||||
* @Nm Hello
|
|
||||||
* @Nd Say hello.
|
|
||||||
* @Dd May 17 2023
|
|
||||||
*
|
|
||||||
* .Nm
|
|
||||||
* provides functionality to write hello world messages
|
|
||||||
* into standard C file descriptors.
|
|
||||||
*
|
|
||||||
* @Xr fputs fprintf
|
|
||||||
*/
|
|
||||||
#include <stdio.h>
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This function writes "hello world" to the given file
|
|
||||||
* descriptor.
|
|
||||||
* .Pp
|
|
||||||
* There really isn't much more to be said about it.
|
|
||||||
*/
|
|
||||||
extern void SayHello(FILE *);
|
|
||||||
.Ed
|
|
||||||
.Pp
|
|
||||||
This example shows how mdoc directives can be placed in
|
|
||||||
documentation comments. Note that the triple-star comment
|
|
||||||
documents the header itself, and the double-star comment
|
|
||||||
documents the type declaration or function definition
|
|
||||||
below it.
|
|
||||||
.Sh SEE ALSO
|
|
||||||
.Xr hdoc 5 ,
|
|
||||||
.Xr HeaderParser 3
|
|
|
@ -1,118 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright (C) 2022-2023 Jordan Bancino <@jordan:bancino.net>
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person
|
|
||||||
* obtaining a copy of this software and associated documentation files
|
|
||||||
* (the "Software"), to deal in the Software without restriction,
|
|
||||||
* including without limitation the rights to use, copy, modify, merge,
|
|
||||||
* publish, distribute, sublicense, and/or sell copies of the Software,
|
|
||||||
* and to permit persons to whom the Software is furnished to do so,
|
|
||||||
* subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be
|
|
||||||
* included in all copies or portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
||||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
||||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
||||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
||||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
||||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
||||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
||||||
* SOFTWARE.
|
|
||||||
*/
|
|
||||||
#include <Args.h>
|
|
||||||
|
|
||||||
#include <Memory.h>
|
|
||||||
#include <Log.h>
|
|
||||||
#include <Str.h>
|
|
||||||
|
|
||||||
#include <ctype.h>
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
void
|
|
||||||
ArgParseStateInit(ArgParseState * state)
|
|
||||||
{
|
|
||||||
state->optPos = 1;
|
|
||||||
state->optErr = 1;
|
|
||||||
state->optInd = 1;
|
|
||||||
state->optOpt = 0;
|
|
||||||
state->optArg = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
|
||||||
ArgParse(ArgParseState * state, Array * args, const char *optStr)
|
|
||||||
{
|
|
||||||
const char *arg;
|
|
||||||
|
|
||||||
arg = ArrayGet(args, state->optInd);
|
|
||||||
|
|
||||||
if (arg && StrEquals(arg, "--"))
|
|
||||||
{
|
|
||||||
state->optInd++;
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
else if (!arg || arg[0] != '-' || !isalnum((unsigned char) arg[1]))
|
|
||||||
{
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
const char *opt = strchr(optStr, arg[state->optPos]);
|
|
||||||
|
|
||||||
state->optOpt = arg[state->optPos];
|
|
||||||
|
|
||||||
if (!opt)
|
|
||||||
{
|
|
||||||
if (state->optErr && *optStr != ':')
|
|
||||||
{
|
|
||||||
Log(LOG_ERR, "Illegal option: %c", ArrayGet(args, 0), state->optOpt);
|
|
||||||
}
|
|
||||||
if (!arg[++state->optPos])
|
|
||||||
{
|
|
||||||
state->optInd++;
|
|
||||||
state->optPos = 1;
|
|
||||||
}
|
|
||||||
return '?';
|
|
||||||
}
|
|
||||||
else if (opt[1] == ':')
|
|
||||||
{
|
|
||||||
if (arg[state->optPos + 1])
|
|
||||||
{
|
|
||||||
state->optArg = (char *) arg + state->optPos + 1;
|
|
||||||
state->optInd++;
|
|
||||||
state->optPos = 1;
|
|
||||||
return state->optOpt;
|
|
||||||
}
|
|
||||||
else if (ArrayGet(args, state->optInd + 1))
|
|
||||||
{
|
|
||||||
state->optArg = (char *) ArrayGet(args, state->optInd + 1);
|
|
||||||
state->optInd += 2;
|
|
||||||
state->optPos = 1;
|
|
||||||
return state->optOpt;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if (state->optErr && *optStr != ':')
|
|
||||||
{
|
|
||||||
Log(LOG_ERR, "Option requires an argument: %c", state->optOpt);
|
|
||||||
}
|
|
||||||
if (!arg[++state->optPos])
|
|
||||||
{
|
|
||||||
state->optInd++;
|
|
||||||
state->optPos = 1;
|
|
||||||
}
|
|
||||||
return *optStr == ':' ? ':' : '?';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if (!arg[++state->optPos])
|
|
||||||
{
|
|
||||||
state->optInd++;
|
|
||||||
state->optPos = 1;
|
|
||||||
}
|
|
||||||
return state->optOpt;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,423 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright (C) 2022-2023 Jordan Bancino <@jordan:bancino.net>
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person
|
|
||||||
* obtaining a copy of this software and associated documentation files
|
|
||||||
* (the "Software"), to deal in the Software without restriction,
|
|
||||||
* including without limitation the rights to use, copy, modify, merge,
|
|
||||||
* publish, distribute, sublicense, and/or sell copies of the Software,
|
|
||||||
* and to permit persons to whom the Software is furnished to do so,
|
|
||||||
* subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be
|
|
||||||
* included in all copies or portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
||||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
||||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
||||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
||||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
||||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
||||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
||||||
* SOFTWARE.
|
|
||||||
*/
|
|
||||||
#include <Array.h>
|
|
||||||
|
|
||||||
#ifndef ARRAY_BLOCK
|
|
||||||
#define ARRAY_BLOCK 16
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include <stddef.h>
|
|
||||||
#include <Memory.h>
|
|
||||||
|
|
||||||
struct Array
|
|
||||||
{
|
|
||||||
void **entries; /* An array of void pointers, to
|
|
||||||
* store any data */
|
|
||||||
size_t allocated; /* Elements allocated on the heap */
|
|
||||||
size_t size; /* Elements actually filled */
|
|
||||||
};
|
|
||||||
|
|
||||||
int
|
|
||||||
ArrayAdd(Array * array, void *value)
|
|
||||||
{
|
|
||||||
if (!array)
|
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
return ArrayInsert(array, array->size, value);
|
|
||||||
}
|
|
||||||
|
|
||||||
Array *
|
|
||||||
ArrayCreate(void)
|
|
||||||
{
|
|
||||||
Array *array = Malloc(sizeof(Array));
|
|
||||||
|
|
||||||
if (!array)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
array->size = 0;
|
|
||||||
array->allocated = ARRAY_BLOCK;
|
|
||||||
array->entries = Malloc(sizeof(void *) * ARRAY_BLOCK);
|
|
||||||
|
|
||||||
if (!array->entries)
|
|
||||||
{
|
|
||||||
Free(array);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
return array;
|
|
||||||
}
|
|
||||||
|
|
||||||
void *
|
|
||||||
ArrayDelete(Array * array, size_t index)
|
|
||||||
{
|
|
||||||
size_t i;
|
|
||||||
void *element;
|
|
||||||
|
|
||||||
if (!array || array->size <= index)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
element = array->entries[index];
|
|
||||||
|
|
||||||
for (i = index; i < array->size - 1; i++)
|
|
||||||
{
|
|
||||||
array->entries[i] = array->entries[i + 1];
|
|
||||||
}
|
|
||||||
|
|
||||||
array->size--;
|
|
||||||
|
|
||||||
return element;
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
ArrayFree(Array * array)
|
|
||||||
{
|
|
||||||
if (array)
|
|
||||||
{
|
|
||||||
Free(array->entries);
|
|
||||||
Free(array);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void *
|
|
||||||
ArrayGet(Array * array, size_t index)
|
|
||||||
{
|
|
||||||
if (!array)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (index >= array->size)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
return array->entries[index];
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
extern int
|
|
||||||
ArrayInsert(Array * array, size_t index, void *value)
|
|
||||||
{
|
|
||||||
size_t i;
|
|
||||||
|
|
||||||
if (!array || !value || index > array->size)
|
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (array->size >= array->allocated)
|
|
||||||
{
|
|
||||||
void **tmp;
|
|
||||||
size_t newSize = array->allocated + ARRAY_BLOCK;
|
|
||||||
|
|
||||||
tmp = array->entries;
|
|
||||||
|
|
||||||
array->entries = Realloc(array->entries,
|
|
||||||
sizeof(void *) * newSize);
|
|
||||||
|
|
||||||
if (!array->entries)
|
|
||||||
{
|
|
||||||
array->entries = tmp;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
array->allocated = newSize;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (i = array->size; i > index; i--)
|
|
||||||
{
|
|
||||||
array->entries[i] = array->entries[i - 1];
|
|
||||||
}
|
|
||||||
|
|
||||||
array->size++;
|
|
||||||
|
|
||||||
array->entries[index] = value;
|
|
||||||
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
extern void *
|
|
||||||
ArraySet(Array * array, size_t index, void *value)
|
|
||||||
{
|
|
||||||
void *oldValue;
|
|
||||||
|
|
||||||
if (!value)
|
|
||||||
{
|
|
||||||
return ArrayDelete(array, index);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!array)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (index >= array->size)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
oldValue = array->entries[index];
|
|
||||||
array->entries[index] = value;
|
|
||||||
|
|
||||||
return oldValue;
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t
|
|
||||||
ArraySize(Array * array)
|
|
||||||
{
|
|
||||||
if (!array)
|
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
return array->size;
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
|
||||||
ArrayTrim(Array * array)
|
|
||||||
{
|
|
||||||
void **tmp;
|
|
||||||
|
|
||||||
if (!array)
|
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
tmp = array->entries;
|
|
||||||
|
|
||||||
array->entries = Realloc(array->entries,
|
|
||||||
sizeof(void *) * array->size);
|
|
||||||
|
|
||||||
if (!array->entries)
|
|
||||||
{
|
|
||||||
array->entries = tmp;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
ArraySwap(Array * array, size_t i, size_t j)
|
|
||||||
{
|
|
||||||
void *p = array->entries[i];
|
|
||||||
|
|
||||||
array->entries[i] = array->entries[j];
|
|
||||||
array->entries[j] = p;
|
|
||||||
}
|
|
||||||
|
|
||||||
static size_t
|
|
||||||
ArrayPartition(Array * array, size_t low, size_t high, int (*compare) (void *, void *))
|
|
||||||
{
|
|
||||||
void *pivot = array->entries[high];
|
|
||||||
size_t i = low - 1;
|
|
||||||
size_t j;
|
|
||||||
|
|
||||||
for (j = low; j <= high - 1; j++)
|
|
||||||
{
|
|
||||||
if (compare(array->entries[j], pivot) < 0)
|
|
||||||
{
|
|
||||||
i++;
|
|
||||||
ArraySwap(array, i, j);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
ArraySwap(array, i + 1, high);
|
|
||||||
return i + 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
ArrayQuickSort(Array * array, size_t low, size_t high, int (*compare) (void *, void *))
|
|
||||||
{
|
|
||||||
if (low < high)
|
|
||||||
{
|
|
||||||
size_t pi = ArrayPartition(array, low, high, compare);
|
|
||||||
|
|
||||||
ArrayQuickSort(array, low, pi ? pi - 1 : 0, compare);
|
|
||||||
ArrayQuickSort(array, pi + 1, high, compare);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
ArraySort(Array * array, int (*compare) (void *, void *))
|
|
||||||
{
|
|
||||||
if (!array)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
ArrayQuickSort(array, 0, array->size - 1, compare);
|
|
||||||
}
|
|
||||||
|
|
||||||
Array *
|
|
||||||
ArrayUnique(Array * array, int (*compare) (void *, void *))
|
|
||||||
{
|
|
||||||
Array *ret;
|
|
||||||
|
|
||||||
size_t i;
|
|
||||||
|
|
||||||
if (!array)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = ArrayDuplicate(array);
|
|
||||||
if (!ret)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ArraySize(ret) == 1)
|
|
||||||
{
|
|
||||||
/* There can't be any duplicates when there's only 1 value */
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
ArraySort(ret, compare);
|
|
||||||
|
|
||||||
for (i = 1; i < ArraySize(ret); i++)
|
|
||||||
{
|
|
||||||
void *cur = ret->entries[i];
|
|
||||||
void *prev = ret->entries[i - 1];
|
|
||||||
|
|
||||||
if (compare(cur, prev) == 0)
|
|
||||||
{
|
|
||||||
/* Remove the duplicate, and put i back where it was. */
|
|
||||||
ArrayDelete(ret, i--);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ArrayTrim(ret);
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/* Even though the following operations could be done using only the
|
|
||||||
* public Array API defined above, I opted for low-level struct
|
|
||||||
* manipulation because it allows much more efficient copying; we only
|
|
||||||
* allocate what we for sure need upfront, and don't have to
|
|
||||||
* re-allocate during the operation. */
|
|
||||||
|
|
||||||
Array *
|
|
||||||
ArrayReverse(Array * array)
|
|
||||||
{
|
|
||||||
Array *ret;
|
|
||||||
|
|
||||||
size_t i;
|
|
||||||
size_t size;
|
|
||||||
|
|
||||||
if (!array)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!array->size)
|
|
||||||
{
|
|
||||||
return ArrayCreate();
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = Malloc(sizeof(Array));
|
|
||||||
|
|
||||||
size = array->size;
|
|
||||||
|
|
||||||
ret->size = size;
|
|
||||||
ret->allocated = size;
|
|
||||||
ret->entries = Malloc(sizeof(void *) * size);
|
|
||||||
|
|
||||||
if (!ret->entries)
|
|
||||||
{
|
|
||||||
Free(ret);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (i = 0; i < size; i++)
|
|
||||||
{
|
|
||||||
ret->entries[i] = array->entries[size - i - 1];
|
|
||||||
}
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
Array *
|
|
||||||
ArrayFromVarArgs(size_t n, va_list ap)
|
|
||||||
{
|
|
||||||
size_t i;
|
|
||||||
Array *arr = Malloc(sizeof(Array));
|
|
||||||
|
|
||||||
if (!arr)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
arr->size = n;
|
|
||||||
arr->allocated = n;
|
|
||||||
arr->entries = Malloc(sizeof(void *) * arr->allocated);
|
|
||||||
|
|
||||||
if (!arr->entries)
|
|
||||||
{
|
|
||||||
Free(arr);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (i = 0; i < n; i++)
|
|
||||||
{
|
|
||||||
arr->entries[i] = va_arg(ap, void *);
|
|
||||||
}
|
|
||||||
|
|
||||||
return arr;
|
|
||||||
}
|
|
||||||
|
|
||||||
Array *
|
|
||||||
ArrayDuplicate(Array * arr)
|
|
||||||
{
|
|
||||||
size_t i;
|
|
||||||
Array *arr2 = Malloc(sizeof(Array));
|
|
||||||
|
|
||||||
if (!arr2)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
arr2->size = arr->size;
|
|
||||||
arr2->allocated = arr->size;
|
|
||||||
arr2->entries = Malloc(sizeof(void *) * arr->allocated);
|
|
||||||
|
|
||||||
if (!arr2->entries)
|
|
||||||
{
|
|
||||||
Free(arr2);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (i = 0; i < arr2->size; i++)
|
|
||||||
{
|
|
||||||
arr2->entries[i] = arr->entries[i];
|
|
||||||
}
|
|
||||||
|
|
||||||
return arr2;
|
|
||||||
}
|
|
|
@ -1,252 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright (C) 2022-2023 Jordan Bancino <@jordan:bancino.net>
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person
|
|
||||||
* obtaining a copy of this software and associated documentation files
|
|
||||||
* (the "Software"), to deal in the Software without restriction,
|
|
||||||
* including without limitation the rights to use, copy, modify, merge,
|
|
||||||
* publish, distribute, sublicense, and/or sell copies of the Software,
|
|
||||||
* and to permit persons to whom the Software is furnished to do so,
|
|
||||||
* subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be
|
|
||||||
* included in all copies or portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
||||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
||||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
||||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
||||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
||||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
||||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
||||||
* SOFTWARE.
|
|
||||||
*/
|
|
||||||
#include <Cron.h>
|
|
||||||
|
|
||||||
#include <UInt64.h>
|
|
||||||
#include <Array.h>
|
|
||||||
#include <Memory.h>
|
|
||||||
#include <Util.h>
|
|
||||||
|
|
||||||
#include <pthread.h>
|
|
||||||
|
|
||||||
struct Cron
|
|
||||||
{
|
|
||||||
UInt64 tick;
|
|
||||||
Array *jobs;
|
|
||||||
pthread_mutex_t lock;
|
|
||||||
volatile unsigned int stop:1;
|
|
||||||
pthread_t thread;
|
|
||||||
};
|
|
||||||
|
|
||||||
typedef struct Job
|
|
||||||
{
|
|
||||||
UInt64 interval;
|
|
||||||
UInt64 lastExec;
|
|
||||||
JobFunc *func;
|
|
||||||
void *args;
|
|
||||||
} Job;
|
|
||||||
|
|
||||||
static Job *
|
|
||||||
JobCreate(UInt32 interval, JobFunc * func, void *args)
|
|
||||||
{
|
|
||||||
Job *job;
|
|
||||||
|
|
||||||
if (!func)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
job = Malloc(sizeof(Job));
|
|
||||||
if (!job)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
job->interval = UInt64Create(0, interval);
|
|
||||||
job->lastExec = UInt64Create(0, 0);
|
|
||||||
job->func = func;
|
|
||||||
job->args = args;
|
|
||||||
|
|
||||||
return job;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void *
|
|
||||||
CronThread(void *args)
|
|
||||||
{
|
|
||||||
Cron *cron = args;
|
|
||||||
|
|
||||||
while (!cron->stop)
|
|
||||||
{
|
|
||||||
size_t i;
|
|
||||||
UInt64 ts; /* tick start */
|
|
||||||
UInt64 te; /* tick end */
|
|
||||||
|
|
||||||
pthread_mutex_lock(&cron->lock);
|
|
||||||
|
|
||||||
ts = UtilServerTs();
|
|
||||||
|
|
||||||
for (i = 0; i < ArraySize(cron->jobs); i++)
|
|
||||||
{
|
|
||||||
Job *job = ArrayGet(cron->jobs, i);
|
|
||||||
|
|
||||||
if (UInt64Gt(UInt64Sub(ts, job->lastExec), job->interval))
|
|
||||||
{
|
|
||||||
job->func(job->args);
|
|
||||||
job->lastExec = ts;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (UInt64Eq(job->interval, UInt64Create(0, 0)))
|
|
||||||
{
|
|
||||||
ArrayDelete(cron->jobs, i);
|
|
||||||
Free(job);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
te = UtilServerTs();
|
|
||||||
|
|
||||||
pthread_mutex_unlock(&cron->lock);
|
|
||||||
|
|
||||||
/* Only sleep if the jobs didn't overrun the tick */
|
|
||||||
if (UInt64Gt(cron->tick, UInt64Sub(te, ts)))
|
|
||||||
{
|
|
||||||
const UInt64 microTick = UInt64Create(0, 100);
|
|
||||||
|
|
||||||
UInt64 remainingTick = UInt64Sub(cron->tick, UInt64Sub(te, ts));
|
|
||||||
|
|
||||||
/* Only sleep for microTick ms at a time because if the job
|
|
||||||
* scheduler is supposed to stop before the tick is up, we
|
|
||||||
* don't want to be stuck in a long sleep */
|
|
||||||
while (UInt64Geq(remainingTick, microTick) && !cron->stop)
|
|
||||||
{
|
|
||||||
UtilSleepMillis(microTick);
|
|
||||||
|
|
||||||
remainingTick = UInt64Sub(remainingTick, microTick);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (UInt64Neq(remainingTick, UInt64Create(0, 0)) && !cron->stop)
|
|
||||||
{
|
|
||||||
UtilSleepMillis(remainingTick);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
Cron *
|
|
||||||
CronCreate(UInt32 tick)
|
|
||||||
{
|
|
||||||
Cron *cron = Malloc(sizeof(Cron));
|
|
||||||
|
|
||||||
if (!cron)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
cron->jobs = ArrayCreate();
|
|
||||||
if (!cron->jobs)
|
|
||||||
{
|
|
||||||
Free(cron);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
cron->tick = UInt64Create(0, tick);
|
|
||||||
cron->stop = 1;
|
|
||||||
|
|
||||||
pthread_mutex_init(&cron->lock, NULL);
|
|
||||||
|
|
||||||
return cron;
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
CronOnce(Cron * cron, JobFunc * func, void *args)
|
|
||||||
{
|
|
||||||
Job *job;
|
|
||||||
|
|
||||||
if (!cron || !func)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
job = JobCreate(0, func, args);
|
|
||||||
if (!job)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
pthread_mutex_lock(&cron->lock);
|
|
||||||
ArrayAdd(cron->jobs, job);
|
|
||||||
pthread_mutex_unlock(&cron->lock);
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
CronEvery(Cron * cron, unsigned long interval, JobFunc * func, void *args)
|
|
||||||
{
|
|
||||||
Job *job;
|
|
||||||
|
|
||||||
if (!cron || !func)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
job = JobCreate(interval, func, args);
|
|
||||||
if (!job)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
pthread_mutex_lock(&cron->lock);
|
|
||||||
ArrayAdd(cron->jobs, job);
|
|
||||||
pthread_mutex_unlock(&cron->lock);
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
CronStart(Cron * cron)
|
|
||||||
{
|
|
||||||
if (!cron || !cron->stop)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
cron->stop = 0;
|
|
||||||
|
|
||||||
pthread_create(&cron->thread, NULL, CronThread, cron);
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
CronStop(Cron * cron)
|
|
||||||
{
|
|
||||||
if (!cron || cron->stop)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
cron->stop = 1;
|
|
||||||
|
|
||||||
pthread_join(cron->thread, NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
CronFree(Cron * cron)
|
|
||||||
{
|
|
||||||
size_t i;
|
|
||||||
|
|
||||||
if (!cron)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
CronStop(cron);
|
|
||||||
|
|
||||||
pthread_mutex_lock(&cron->lock);
|
|
||||||
for (i = 0; i < ArraySize(cron->jobs); i++)
|
|
||||||
{
|
|
||||||
Free(ArrayGet(cron->jobs, i));
|
|
||||||
}
|
|
||||||
|
|
||||||
ArrayFree(cron->jobs);
|
|
||||||
pthread_mutex_unlock(&cron->lock);
|
|
||||||
pthread_mutex_destroy(&cron->lock);
|
|
||||||
|
|
||||||
Free(cron);
|
|
||||||
}
|
|
|
@ -1,969 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright (C) 2022-2023 Jordan Bancino <@jordan:bancino.net>
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person
|
|
||||||
* obtaining a copy of this software and associated documentation files
|
|
||||||
* (the "Software"), to deal in the Software without restriction,
|
|
||||||
* including without limitation the rights to use, copy, modify, merge,
|
|
||||||
* publish, distribute, sublicense, and/or sell copies of the Software,
|
|
||||||
* and to permit persons to whom the Software is furnished to do so,
|
|
||||||
* subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be
|
|
||||||
* included in all copies or portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
||||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
||||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
||||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
||||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
||||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
||||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
||||||
* SOFTWARE.
|
|
||||||
*/
|
|
||||||
#include <Db.h>
|
|
||||||
|
|
||||||
#include <UInt64.h>
|
|
||||||
#include <Memory.h>
|
|
||||||
#include <Json.h>
|
|
||||||
#include <Util.h>
|
|
||||||
#include <Str.h>
|
|
||||||
#include <Stream.h>
|
|
||||||
#include <Log.h>
|
|
||||||
|
|
||||||
#include <sys/types.h>
|
|
||||||
#include <dirent.h>
|
|
||||||
|
|
||||||
#include <pthread.h>
|
|
||||||
#include <fcntl.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
|
|
||||||
#include <errno.h>
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
struct Db
|
|
||||||
{
|
|
||||||
char *dir;
|
|
||||||
pthread_mutex_t lock;
|
|
||||||
|
|
||||||
size_t cacheSize;
|
|
||||||
size_t maxCache;
|
|
||||||
HashMap *cache;
|
|
||||||
|
|
||||||
/*
|
|
||||||
* The cache uses a double linked list (see DbRef
|
|
||||||
* below) to know which objects are most and least
|
|
||||||
* recently used. The following diagram helps me
|
|
||||||
* know what way all the pointers go, because it
|
|
||||||
* can get very confusing sometimes. For example,
|
|
||||||
* there's nothing stopping "next" from pointing to
|
|
||||||
* least recent, and "prev" from pointing to most
|
|
||||||
* recent, so hopefully this clarifies the pointer
|
|
||||||
* terminology used when dealing with the linked
|
|
||||||
* list:
|
|
||||||
*
|
|
||||||
* mostRecent leastRecent
|
|
||||||
* | prev prev | prev
|
|
||||||
* +---+ ---> +---+ ---> +---+ ---> NULL
|
|
||||||
* |ref| |ref| |ref|
|
|
||||||
* NULL <--- +---+ <--- +---+ <--- +---+
|
|
||||||
* next next next
|
|
||||||
*/
|
|
||||||
DbRef *mostRecent;
|
|
||||||
DbRef *leastRecent;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct DbRef
|
|
||||||
{
|
|
||||||
HashMap *json;
|
|
||||||
|
|
||||||
UInt64 ts;
|
|
||||||
size_t size;
|
|
||||||
|
|
||||||
Array *name;
|
|
||||||
|
|
||||||
DbRef *prev;
|
|
||||||
DbRef *next;
|
|
||||||
|
|
||||||
int fd;
|
|
||||||
Stream *stream;
|
|
||||||
};
|
|
||||||
|
|
||||||
static void
|
|
||||||
StringArrayFree(Array * arr)
|
|
||||||
{
|
|
||||||
size_t i;
|
|
||||||
|
|
||||||
for (i = 0; i < ArraySize(arr); i++)
|
|
||||||
{
|
|
||||||
Free(ArrayGet(arr, i));
|
|
||||||
}
|
|
||||||
|
|
||||||
ArrayFree(arr);
|
|
||||||
}
|
|
||||||
|
|
||||||
static ssize_t DbComputeSize(HashMap *);
|
|
||||||
|
|
||||||
static ssize_t
|
|
||||||
DbComputeSizeOfValue(JsonValue * val)
|
|
||||||
{
|
|
||||||
MemoryInfo *a;
|
|
||||||
ssize_t total = 0;
|
|
||||||
|
|
||||||
size_t i;
|
|
||||||
|
|
||||||
union
|
|
||||||
{
|
|
||||||
char *str;
|
|
||||||
Array *arr;
|
|
||||||
} u;
|
|
||||||
|
|
||||||
if (!val)
|
|
||||||
{
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
a = MemoryInfoGet(val);
|
|
||||||
if (a)
|
|
||||||
{
|
|
||||||
total += MemoryInfoGetSize(a);
|
|
||||||
}
|
|
||||||
|
|
||||||
switch (JsonValueType(val))
|
|
||||||
{
|
|
||||||
case JSON_OBJECT:
|
|
||||||
total += DbComputeSize(JsonValueAsObject(val));
|
|
||||||
break;
|
|
||||||
case JSON_ARRAY:
|
|
||||||
u.arr = JsonValueAsArray(val);
|
|
||||||
a = MemoryInfoGet(u.arr);
|
|
||||||
|
|
||||||
if (a)
|
|
||||||
{
|
|
||||||
total += MemoryInfoGetSize(a);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (i = 0; i < ArraySize(u.arr); i++)
|
|
||||||
{
|
|
||||||
total += DbComputeSizeOfValue(ArrayGet(u.arr, i));
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case JSON_STRING:
|
|
||||||
u.str = JsonValueAsString(val);
|
|
||||||
a = MemoryInfoGet(u.str);
|
|
||||||
if (a)
|
|
||||||
{
|
|
||||||
total += MemoryInfoGetSize(a);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case JSON_NULL:
|
|
||||||
case JSON_INTEGER:
|
|
||||||
case JSON_FLOAT:
|
|
||||||
case JSON_BOOLEAN:
|
|
||||||
default:
|
|
||||||
/* These don't use any extra heap space */
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
return total;
|
|
||||||
}
|
|
||||||
|
|
||||||
static ssize_t
|
|
||||||
DbComputeSize(HashMap * json)
|
|
||||||
{
|
|
||||||
char *key;
|
|
||||||
JsonValue *val;
|
|
||||||
MemoryInfo *a;
|
|
||||||
size_t total;
|
|
||||||
|
|
||||||
if (!json)
|
|
||||||
{
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
total = 0;
|
|
||||||
|
|
||||||
a = MemoryInfoGet(json);
|
|
||||||
if (a)
|
|
||||||
{
|
|
||||||
total += MemoryInfoGetSize(a);
|
|
||||||
}
|
|
||||||
|
|
||||||
while (HashMapIterate(json, &key, (void **) &val))
|
|
||||||
{
|
|
||||||
a = MemoryInfoGet(key);
|
|
||||||
if (a)
|
|
||||||
{
|
|
||||||
total += MemoryInfoGetSize(a);
|
|
||||||
}
|
|
||||||
|
|
||||||
total += DbComputeSizeOfValue(val);
|
|
||||||
}
|
|
||||||
|
|
||||||
return total;
|
|
||||||
}
|
|
||||||
|
|
||||||
static char *
|
|
||||||
DbHashKey(Array * args)
|
|
||||||
{
|
|
||||||
size_t i;
|
|
||||||
char *str = NULL;
|
|
||||||
|
|
||||||
for (i = 0; i < ArraySize(args); i++)
|
|
||||||
{
|
|
||||||
char *tmp = StrConcat(2, str, ArrayGet(args, i));
|
|
||||||
|
|
||||||
Free(str);
|
|
||||||
str = tmp;
|
|
||||||
}
|
|
||||||
|
|
||||||
return str;
|
|
||||||
}
|
|
||||||
|
|
||||||
static char *
|
|
||||||
DbDirName(Db * db, Array * args, size_t strip)
|
|
||||||
{
|
|
||||||
size_t i;
|
|
||||||
char *str = StrConcat(2, db->dir, "/");
|
|
||||||
|
|
||||||
for (i = 0; i < ArraySize(args) - strip; i++)
|
|
||||||
{
|
|
||||||
char *tmp;
|
|
||||||
|
|
||||||
tmp = StrConcat(3, str, ArrayGet(args, i), "/");
|
|
||||||
|
|
||||||
Free(str);
|
|
||||||
|
|
||||||
str = tmp;
|
|
||||||
}
|
|
||||||
|
|
||||||
return str;
|
|
||||||
}
|
|
||||||
|
|
||||||
static char *
|
|
||||||
DbFileName(Db * db, Array * args)
|
|
||||||
{
|
|
||||||
size_t i;
|
|
||||||
char *str = StrConcat(2, db->dir, "/");
|
|
||||||
|
|
||||||
for (i = 0; i < ArraySize(args); i++)
|
|
||||||
{
|
|
||||||
char *tmp;
|
|
||||||
char *arg = StrDuplicate(ArrayGet(args, i));
|
|
||||||
size_t j = 0;
|
|
||||||
|
|
||||||
/* Sanitize name to prevent directory traversal attacks */
|
|
||||||
while (arg[j])
|
|
||||||
{
|
|
||||||
switch (arg[j])
|
|
||||||
{
|
|
||||||
case '/':
|
|
||||||
arg[j] = '_';
|
|
||||||
break;
|
|
||||||
case '.':
|
|
||||||
arg[j] = '-';
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
j++;
|
|
||||||
}
|
|
||||||
|
|
||||||
tmp = StrConcat(3, str, arg,
|
|
||||||
(i < ArraySize(args) - 1) ? "/" : ".json");
|
|
||||||
|
|
||||||
Free(arg);
|
|
||||||
Free(str);
|
|
||||||
|
|
||||||
str = tmp;
|
|
||||||
}
|
|
||||||
|
|
||||||
return str;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
DbCacheEvict(Db * db)
|
|
||||||
{
|
|
||||||
DbRef *ref = db->leastRecent;
|
|
||||||
DbRef *tmp;
|
|
||||||
|
|
||||||
while (ref && db->cacheSize > db->maxCache)
|
|
||||||
{
|
|
||||||
char *hash;
|
|
||||||
|
|
||||||
JsonFree(ref->json);
|
|
||||||
|
|
||||||
hash = DbHashKey(ref->name);
|
|
||||||
HashMapDelete(db->cache, hash);
|
|
||||||
Free(hash);
|
|
||||||
|
|
||||||
StringArrayFree(ref->name);
|
|
||||||
|
|
||||||
db->cacheSize -= ref->size;
|
|
||||||
|
|
||||||
if (ref->next)
|
|
||||||
{
|
|
||||||
ref->next->prev = ref->prev;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
db->mostRecent = ref->prev;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ref->prev)
|
|
||||||
{
|
|
||||||
ref->prev->next = ref->next;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
db->leastRecent = ref->next;
|
|
||||||
}
|
|
||||||
|
|
||||||
tmp = ref->next;
|
|
||||||
Free(ref);
|
|
||||||
|
|
||||||
ref = tmp;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Db *
|
|
||||||
DbOpen(char *dir, size_t cache)
|
|
||||||
{
|
|
||||||
Db *db;
|
|
||||||
pthread_mutexattr_t attr;
|
|
||||||
|
|
||||||
if (!dir)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
db = Malloc(sizeof(Db));
|
|
||||||
if (!db)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
db->dir = dir;
|
|
||||||
db->maxCache = cache;
|
|
||||||
|
|
||||||
pthread_mutexattr_init(&attr);
|
|
||||||
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
|
|
||||||
pthread_mutex_init(&db->lock, &attr);
|
|
||||||
pthread_mutexattr_destroy(&attr);
|
|
||||||
|
|
||||||
db->mostRecent = NULL;
|
|
||||||
db->leastRecent = NULL;
|
|
||||||
db->cacheSize = 0;
|
|
||||||
|
|
||||||
if (db->maxCache)
|
|
||||||
{
|
|
||||||
db->cache = HashMapCreate();
|
|
||||||
if (!db->cache)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
db->cache = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
return db;
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
DbMaxCacheSet(Db * db, size_t cache)
|
|
||||||
{
|
|
||||||
if (!db)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
pthread_mutex_lock(&db->lock);
|
|
||||||
|
|
||||||
db->maxCache = cache;
|
|
||||||
if (db->maxCache && !db->cache)
|
|
||||||
{
|
|
||||||
db->cache = HashMapCreate();
|
|
||||||
db->cacheSize = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
DbCacheEvict(db);
|
|
||||||
|
|
||||||
pthread_mutex_unlock(&db->lock);
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
DbClose(Db * db)
|
|
||||||
{
|
|
||||||
if (!db)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
pthread_mutex_lock(&db->lock);
|
|
||||||
|
|
||||||
DbMaxCacheSet(db, 0);
|
|
||||||
DbCacheEvict(db);
|
|
||||||
HashMapFree(db->cache);
|
|
||||||
|
|
||||||
pthread_mutex_unlock(&db->lock);
|
|
||||||
pthread_mutex_destroy(&db->lock);
|
|
||||||
|
|
||||||
Free(db);
|
|
||||||
}
|
|
||||||
|
|
||||||
static DbRef *
|
|
||||||
DbLockFromArr(Db * db, Array * args)
|
|
||||||
{
|
|
||||||
char *file;
|
|
||||||
char *hash;
|
|
||||||
DbRef *ref;
|
|
||||||
struct flock lock;
|
|
||||||
|
|
||||||
int fd;
|
|
||||||
Stream *stream;
|
|
||||||
|
|
||||||
if (!db || !args)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
ref = NULL;
|
|
||||||
hash = NULL;
|
|
||||||
|
|
||||||
pthread_mutex_lock(&db->lock);
|
|
||||||
|
|
||||||
/* Check if the item is in the cache */
|
|
||||||
hash = DbHashKey(args);
|
|
||||||
ref = HashMapGet(db->cache, hash);
|
|
||||||
file = DbFileName(db, args);
|
|
||||||
|
|
||||||
fd = open(file, O_RDWR);
|
|
||||||
if (fd == -1)
|
|
||||||
{
|
|
||||||
if (ref)
|
|
||||||
{
|
|
||||||
HashMapDelete(db->cache, hash);
|
|
||||||
JsonFree(ref->json);
|
|
||||||
StringArrayFree(ref->name);
|
|
||||||
|
|
||||||
db->cacheSize -= ref->size;
|
|
||||||
|
|
||||||
if (ref->next)
|
|
||||||
{
|
|
||||||
ref->next->prev = ref->prev;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
db->mostRecent = ref->prev;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ref->prev)
|
|
||||||
{
|
|
||||||
ref->prev->next = ref->next;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
db->leastRecent = ref->next;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!db->leastRecent)
|
|
||||||
{
|
|
||||||
db->leastRecent = db->mostRecent;
|
|
||||||
}
|
|
||||||
|
|
||||||
Free(ref);
|
|
||||||
}
|
|
||||||
ref = NULL;
|
|
||||||
goto finish;
|
|
||||||
}
|
|
||||||
|
|
||||||
stream = StreamFd(fd);
|
|
||||||
|
|
||||||
lock.l_start = 0;
|
|
||||||
lock.l_len = 0;
|
|
||||||
lock.l_type = F_WRLCK;
|
|
||||||
lock.l_whence = SEEK_SET;
|
|
||||||
|
|
||||||
/* Lock the file on the disk */
|
|
||||||
if (fcntl(fd, F_SETLK, &lock) < 0)
|
|
||||||
{
|
|
||||||
StreamClose(stream);
|
|
||||||
ref = NULL;
|
|
||||||
goto finish;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ref) /* In cache */
|
|
||||||
{
|
|
||||||
UInt64 diskTs = UtilLastModified(file);
|
|
||||||
|
|
||||||
ref->fd = fd;
|
|
||||||
ref->stream = stream;
|
|
||||||
|
|
||||||
if (UInt64Gt(diskTs, ref->ts))
|
|
||||||
{
|
|
||||||
/* File was modified on disk since it was cached */
|
|
||||||
HashMap *json = JsonDecode(ref->stream);
|
|
||||||
|
|
||||||
if (!json)
|
|
||||||
{
|
|
||||||
StreamClose(ref->stream);
|
|
||||||
ref = NULL;
|
|
||||||
goto finish;
|
|
||||||
}
|
|
||||||
|
|
||||||
JsonFree(ref->json);
|
|
||||||
ref->json = json;
|
|
||||||
ref->ts = diskTs;
|
|
||||||
ref->size = DbComputeSize(ref->json);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Float this ref to mostRecent */
|
|
||||||
if (ref->next)
|
|
||||||
{
|
|
||||||
ref->next->prev = ref->prev;
|
|
||||||
|
|
||||||
if (!ref->prev)
|
|
||||||
{
|
|
||||||
db->leastRecent = ref->next;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
ref->prev->next = ref->next;
|
|
||||||
}
|
|
||||||
|
|
||||||
ref->prev = db->mostRecent;
|
|
||||||
ref->next = NULL;
|
|
||||||
if (db->mostRecent)
|
|
||||||
{
|
|
||||||
db->mostRecent->next = ref;
|
|
||||||
}
|
|
||||||
db->mostRecent = ref;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* If there is no least recent, this is the only thing in the
|
|
||||||
* cache, so it is also least recent. */
|
|
||||||
if (!db->leastRecent)
|
|
||||||
{
|
|
||||||
db->leastRecent = ref;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* The file on disk may be larger than what we have in memory,
|
|
||||||
* which may require items in cache to be evicted. */
|
|
||||||
DbCacheEvict(db);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
Array *name;
|
|
||||||
size_t i;
|
|
||||||
|
|
||||||
/* Not in cache; load from disk */
|
|
||||||
|
|
||||||
ref = Malloc(sizeof(DbRef));
|
|
||||||
if (!ref)
|
|
||||||
{
|
|
||||||
StreamClose(stream);
|
|
||||||
goto finish;
|
|
||||||
}
|
|
||||||
|
|
||||||
ref->json = JsonDecode(stream);
|
|
||||||
|
|
||||||
if (!ref->json)
|
|
||||||
{
|
|
||||||
Free(ref);
|
|
||||||
StreamClose(stream);
|
|
||||||
ref = NULL;
|
|
||||||
goto finish;
|
|
||||||
}
|
|
||||||
|
|
||||||
ref->fd = fd;
|
|
||||||
ref->stream = stream;
|
|
||||||
|
|
||||||
name = ArrayCreate();
|
|
||||||
for (i = 0; i < ArraySize(args); i++)
|
|
||||||
{
|
|
||||||
ArrayAdd(name, StrDuplicate(ArrayGet(args, i)));
|
|
||||||
}
|
|
||||||
ref->name = name;
|
|
||||||
|
|
||||||
if (db->cache)
|
|
||||||
{
|
|
||||||
ref->ts = UtilServerTs();
|
|
||||||
ref->size = DbComputeSize(ref->json);
|
|
||||||
HashMapSet(db->cache, hash, ref);
|
|
||||||
db->cacheSize += ref->size;
|
|
||||||
|
|
||||||
ref->next = NULL;
|
|
||||||
ref->prev = db->mostRecent;
|
|
||||||
if (db->mostRecent)
|
|
||||||
{
|
|
||||||
db->mostRecent->next = ref;
|
|
||||||
}
|
|
||||||
db->mostRecent = ref;
|
|
||||||
|
|
||||||
if (!db->leastRecent)
|
|
||||||
{
|
|
||||||
db->leastRecent = ref;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Adding this item to the cache may case it to grow too
|
|
||||||
* large, requiring some items to be evicted */
|
|
||||||
DbCacheEvict(db);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
finish:
|
|
||||||
if (!ref)
|
|
||||||
{
|
|
||||||
pthread_mutex_unlock(&db->lock);
|
|
||||||
}
|
|
||||||
|
|
||||||
Free(file);
|
|
||||||
Free(hash);
|
|
||||||
|
|
||||||
return ref;
|
|
||||||
}
|
|
||||||
|
|
||||||
DbRef *
|
|
||||||
DbCreate(Db * db, size_t nArgs,...)
|
|
||||||
{
|
|
||||||
Stream *fp;
|
|
||||||
char *file;
|
|
||||||
char *dir;
|
|
||||||
va_list ap;
|
|
||||||
Array *args;
|
|
||||||
DbRef *ret;
|
|
||||||
|
|
||||||
if (!db)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
va_start(ap, nArgs);
|
|
||||||
args = ArrayFromVarArgs(nArgs, ap);
|
|
||||||
va_end(ap);
|
|
||||||
|
|
||||||
if (!args)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
pthread_mutex_lock(&db->lock);
|
|
||||||
|
|
||||||
file = DbFileName(db, args);
|
|
||||||
|
|
||||||
if (UInt64Neq(UtilLastModified(file), UInt64Create(0, 0)))
|
|
||||||
{
|
|
||||||
Free(file);
|
|
||||||
ArrayFree(args);
|
|
||||||
pthread_mutex_unlock(&db->lock);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
dir = DbDirName(db, args, 1);
|
|
||||||
if (UtilMkdir(dir, 0750) < 0)
|
|
||||||
{
|
|
||||||
Free(file);
|
|
||||||
ArrayFree(args);
|
|
||||||
Free(dir);
|
|
||||||
pthread_mutex_unlock(&db->lock);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
Free(dir);
|
|
||||||
|
|
||||||
fp = StreamOpen(file, "w");
|
|
||||||
Free(file);
|
|
||||||
if (!fp)
|
|
||||||
{
|
|
||||||
ArrayFree(args);
|
|
||||||
pthread_mutex_unlock(&db->lock);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
StreamPuts(fp, "{}");
|
|
||||||
StreamClose(fp);
|
|
||||||
|
|
||||||
/* DbLockFromArr() will lock again for us */
|
|
||||||
pthread_mutex_unlock(&db->lock);
|
|
||||||
|
|
||||||
ret = DbLockFromArr(db, args);
|
|
||||||
|
|
||||||
ArrayFree(args);
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
|
||||||
DbDelete(Db * db, size_t nArgs,...)
|
|
||||||
{
|
|
||||||
va_list ap;
|
|
||||||
Array *args;
|
|
||||||
char *file;
|
|
||||||
char *hash;
|
|
||||||
int ret = 1;
|
|
||||||
DbRef *ref;
|
|
||||||
|
|
||||||
if (!db)
|
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
va_start(ap, nArgs);
|
|
||||||
args = ArrayFromVarArgs(nArgs, ap);
|
|
||||||
va_end(ap);
|
|
||||||
|
|
||||||
pthread_mutex_lock(&db->lock);
|
|
||||||
|
|
||||||
hash = DbHashKey(args);
|
|
||||||
file = DbFileName(db, args);
|
|
||||||
|
|
||||||
ref = HashMapGet(db->cache, hash);
|
|
||||||
if (ref)
|
|
||||||
{
|
|
||||||
HashMapDelete(db->cache, hash);
|
|
||||||
JsonFree(ref->json);
|
|
||||||
StringArrayFree(ref->name);
|
|
||||||
|
|
||||||
db->cacheSize -= ref->size;
|
|
||||||
|
|
||||||
if (ref->next)
|
|
||||||
{
|
|
||||||
ref->next->prev = ref->prev;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
db->mostRecent = ref->prev;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ref->prev)
|
|
||||||
{
|
|
||||||
ref->prev->next = ref->next;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
db->leastRecent = ref->next;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!db->leastRecent)
|
|
||||||
{
|
|
||||||
db->leastRecent = db->mostRecent;
|
|
||||||
}
|
|
||||||
|
|
||||||
Free(ref);
|
|
||||||
}
|
|
||||||
|
|
||||||
Free(hash);
|
|
||||||
|
|
||||||
if (UInt64Neq(UtilLastModified(file), UInt64Create(0, 0)))
|
|
||||||
{
|
|
||||||
ret = remove(file) == 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
pthread_mutex_unlock(&db->lock);
|
|
||||||
|
|
||||||
ArrayFree(args);
|
|
||||||
Free(file);
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
DbRef *
|
|
||||||
DbLock(Db * db, size_t nArgs,...)
|
|
||||||
{
|
|
||||||
va_list ap;
|
|
||||||
Array *args;
|
|
||||||
DbRef *ret;
|
|
||||||
|
|
||||||
va_start(ap, nArgs);
|
|
||||||
args = ArrayFromVarArgs(nArgs, ap);
|
|
||||||
va_end(ap);
|
|
||||||
|
|
||||||
if (!args)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = DbLockFromArr(db, args);
|
|
||||||
|
|
||||||
ArrayFree(args);
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
|
||||||
DbUnlock(Db * db, DbRef * ref)
|
|
||||||
{
|
|
||||||
int destroy;
|
|
||||||
|
|
||||||
if (!db || !ref)
|
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
lseek(ref->fd, 0L, SEEK_SET);
|
|
||||||
if (ftruncate(ref->fd, 0) < 0)
|
|
||||||
{
|
|
||||||
pthread_mutex_unlock(&db->lock);
|
|
||||||
Log(LOG_ERR, "Failed to truncate file on disk.");
|
|
||||||
Log(LOG_ERR, "Error on fd %d: %s", ref->fd, strerror(errno));
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
JsonEncode(ref->json, ref->stream, JSON_DEFAULT);
|
|
||||||
StreamClose(ref->stream);
|
|
||||||
|
|
||||||
if (db->cache)
|
|
||||||
{
|
|
||||||
char *key = DbHashKey(ref->name);
|
|
||||||
|
|
||||||
if (HashMapGet(db->cache, key))
|
|
||||||
{
|
|
||||||
db->cacheSize -= ref->size;
|
|
||||||
ref->size = DbComputeSize(ref->json);
|
|
||||||
db->cacheSize += ref->size;
|
|
||||||
|
|
||||||
/* If this ref has grown significantly since we last
|
|
||||||
* computed its size, it may have filled the cache and
|
|
||||||
* require some items to be evicted. */
|
|
||||||
DbCacheEvict(db);
|
|
||||||
|
|
||||||
destroy = 0;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
destroy = 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
Free(key);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
destroy = 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (destroy)
|
|
||||||
{
|
|
||||||
JsonFree(ref->json);
|
|
||||||
StringArrayFree(ref->name);
|
|
||||||
|
|
||||||
Free(ref);
|
|
||||||
}
|
|
||||||
|
|
||||||
pthread_mutex_unlock(&db->lock);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
|
||||||
DbExists(Db * db, size_t nArgs,...)
|
|
||||||
{
|
|
||||||
va_list ap;
|
|
||||||
Array *args;
|
|
||||||
char *file;
|
|
||||||
int ret;
|
|
||||||
|
|
||||||
va_start(ap, nArgs);
|
|
||||||
args = ArrayFromVarArgs(nArgs, ap);
|
|
||||||
va_end(ap);
|
|
||||||
|
|
||||||
if (!args)
|
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
pthread_mutex_lock(&db->lock);
|
|
||||||
|
|
||||||
file = DbFileName(db, args);
|
|
||||||
ret = UInt64Neq(UtilLastModified(file), UInt64Create(0, 0));
|
|
||||||
|
|
||||||
pthread_mutex_unlock(&db->lock);
|
|
||||||
|
|
||||||
Free(file);
|
|
||||||
ArrayFree(args);
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
Array *
|
|
||||||
DbList(Db * db, size_t nArgs,...)
|
|
||||||
{
|
|
||||||
Array *result;
|
|
||||||
Array *path;
|
|
||||||
DIR *files;
|
|
||||||
struct dirent *file;
|
|
||||||
char *dir;
|
|
||||||
va_list ap;
|
|
||||||
|
|
||||||
if (!db || !nArgs)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
result = ArrayCreate();
|
|
||||||
if (!result)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
va_start(ap, nArgs);
|
|
||||||
path = ArrayFromVarArgs(nArgs, ap);
|
|
||||||
dir = DbDirName(db, path, 0);
|
|
||||||
|
|
||||||
pthread_mutex_lock(&db->lock);
|
|
||||||
|
|
||||||
files = opendir(dir);
|
|
||||||
if (!files)
|
|
||||||
{
|
|
||||||
ArrayFree(path);
|
|
||||||
ArrayFree(result);
|
|
||||||
Free(dir);
|
|
||||||
pthread_mutex_unlock(&db->lock);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
while ((file = readdir(files)))
|
|
||||||
{
|
|
||||||
size_t namlen = strlen(file->d_name);
|
|
||||||
|
|
||||||
if (namlen > 5)
|
|
||||||
{
|
|
||||||
int nameOffset = namlen - 5;
|
|
||||||
|
|
||||||
if (StrEquals(file->d_name + nameOffset, ".json"))
|
|
||||||
{
|
|
||||||
file->d_name[nameOffset] = '\0';
|
|
||||||
ArrayAdd(result, StrDuplicate(file->d_name));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
closedir(files);
|
|
||||||
|
|
||||||
ArrayFree(path);
|
|
||||||
Free(dir);
|
|
||||||
pthread_mutex_unlock(&db->lock);
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
DbListFree(Array * arr)
|
|
||||||
{
|
|
||||||
StringArrayFree(arr);
|
|
||||||
}
|
|
||||||
|
|
||||||
HashMap *
|
|
||||||
DbJson(DbRef * ref)
|
|
||||||
{
|
|
||||||
return ref ? ref->json : NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
|
||||||
DbJsonSet(DbRef * ref, HashMap * json)
|
|
||||||
{
|
|
||||||
if (!ref || !json)
|
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
JsonFree(ref->json);
|
|
||||||
ref->json = JsonDuplicate(json);
|
|
||||||
return 1;
|
|
||||||
}
|
|
|
@ -1,347 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright (C) 2022-2023 Jordan Bancino <@jordan:bancino.net>
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person
|
|
||||||
* obtaining a copy of this software and associated documentation files
|
|
||||||
* (the "Software"), to deal in the Software without restriction,
|
|
||||||
* including without limitation the rights to use, copy, modify, merge,
|
|
||||||
* publish, distribute, sublicense, and/or sell copies of the Software,
|
|
||||||
* and to permit persons to whom the Software is furnished to do so,
|
|
||||||
* subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be
|
|
||||||
* included in all copies or portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
||||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
||||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
||||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
||||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
||||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
||||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
||||||
* SOFTWARE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <Graph.h>
|
|
||||||
|
|
||||||
#include <Memory.h>
|
|
||||||
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
struct Graph
|
|
||||||
{
|
|
||||||
size_t n;
|
|
||||||
Edge *matrix;
|
|
||||||
};
|
|
||||||
|
|
||||||
Graph *
|
|
||||||
GraphCreate(size_t n)
|
|
||||||
{
|
|
||||||
Graph *g;
|
|
||||||
|
|
||||||
if (!n)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
g = Malloc(sizeof(Graph));
|
|
||||||
if (!g)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
g->n = n;
|
|
||||||
|
|
||||||
g->matrix = Malloc((n * n) * sizeof(Edge));
|
|
||||||
if (!g->matrix)
|
|
||||||
{
|
|
||||||
Free(g);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
memset(g->matrix, 0, (n * n) * sizeof(Edge));
|
|
||||||
|
|
||||||
return g;
|
|
||||||
}
|
|
||||||
|
|
||||||
Graph *
|
|
||||||
GraphCreateWithEdges(size_t n, Edge * matrix)
|
|
||||||
{
|
|
||||||
Graph *g = GraphCreate(n);
|
|
||||||
|
|
||||||
if (!g)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
memcpy(g->matrix, matrix, (n * n) * sizeof(Edge));
|
|
||||||
|
|
||||||
return g;
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
GraphFree(Graph * g)
|
|
||||||
{
|
|
||||||
if (!g)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
Free(g->matrix);
|
|
||||||
Free(g);
|
|
||||||
}
|
|
||||||
|
|
||||||
Edge
|
|
||||||
GraphEdgeGet(Graph * g, Node n1, Node n2)
|
|
||||||
{
|
|
||||||
if (n1 >= g->n || n2 >= g->n)
|
|
||||||
{
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
return g->matrix[(g->n * n1) + n2];
|
|
||||||
}
|
|
||||||
|
|
||||||
Edge
|
|
||||||
GraphEdgeSet(Graph * g, Node n1, Node n2, Edge e)
|
|
||||||
{
|
|
||||||
int oldVal;
|
|
||||||
|
|
||||||
if (n1 >= g->n || n2 >= g->n)
|
|
||||||
{
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (e < 0)
|
|
||||||
{
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
oldVal = g->matrix[(g->n * n1) + n2];
|
|
||||||
|
|
||||||
g->matrix[(g->n * n1) + n2] = e;
|
|
||||||
|
|
||||||
return oldVal;
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t
|
|
||||||
GraphCountNodes(Graph * g)
|
|
||||||
{
|
|
||||||
return g ? g->n : 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
Node *
|
|
||||||
GraphBreadthFirstSearch(Graph * G, Node s, size_t * n)
|
|
||||||
{
|
|
||||||
Node *visited;
|
|
||||||
Node *queue;
|
|
||||||
Node *result;
|
|
||||||
size_t queueSize;
|
|
||||||
Node i;
|
|
||||||
|
|
||||||
if (!G || !n)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
*n = 0;
|
|
||||||
|
|
||||||
result = Malloc(G->n * sizeof(Node));
|
|
||||||
if (!result)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (s >= G->n)
|
|
||||||
{
|
|
||||||
Free(result);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
visited = Malloc(G->n * sizeof(Node));
|
|
||||||
memset(visited, 0, G->n * sizeof(Node));
|
|
||||||
queue = Malloc(G->n * sizeof(Node));
|
|
||||||
queueSize = 0;
|
|
||||||
|
|
||||||
visited[s] = 1;
|
|
||||||
|
|
||||||
queueSize++;
|
|
||||||
queue[queueSize - 1] = s;
|
|
||||||
|
|
||||||
while (queueSize)
|
|
||||||
{
|
|
||||||
s = queue[queueSize - 1];
|
|
||||||
queueSize--;
|
|
||||||
|
|
||||||
result[*n] = s;
|
|
||||||
(*n)++;
|
|
||||||
|
|
||||||
for (i = 0; i < G->n; i++)
|
|
||||||
{
|
|
||||||
if (GraphEdgeGet(G, s, i) && !visited[i])
|
|
||||||
{
|
|
||||||
visited[i] = 1;
|
|
||||||
|
|
||||||
queueSize++;
|
|
||||||
queue[queueSize - 1] = i;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Free(visited);
|
|
||||||
Free(queue);
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
GraphDepthFirstSearchRecursive(Graph * G, Node s, Node * result, size_t * n,
|
|
||||||
Node * visited)
|
|
||||||
{
|
|
||||||
size_t i;
|
|
||||||
|
|
||||||
visited[s] = 1;
|
|
||||||
|
|
||||||
result[*n] = s;
|
|
||||||
(*n)++;
|
|
||||||
|
|
||||||
for (i = 0; i < G->n; i++)
|
|
||||||
{
|
|
||||||
if (GraphEdgeGet(G, s, i) && !visited[i])
|
|
||||||
{
|
|
||||||
GraphDepthFirstSearchRecursive(G, i, result, n, visited);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Node *
|
|
||||||
GraphDepthFirstSearch(Graph * G, Node s, size_t * n)
|
|
||||||
{
|
|
||||||
Node *visited;
|
|
||||||
Node *result;
|
|
||||||
|
|
||||||
if (!G || !n)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
result = Malloc(G->n * sizeof(Node));
|
|
||||||
if (!result)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
*n = 0;
|
|
||||||
|
|
||||||
if (s >= G->n)
|
|
||||||
{
|
|
||||||
Free(result);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
visited = Malloc(G->n * sizeof(Node));
|
|
||||||
memset(visited, 0, G->n * sizeof(Node));
|
|
||||||
|
|
||||||
GraphDepthFirstSearchRecursive(G, s, result, n, visited);
|
|
||||||
|
|
||||||
Free(visited);
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
GraphTopologicalSortRecursive(Graph * G, Node s, Node * visited,
|
|
||||||
Node * stack, size_t * stackSize)
|
|
||||||
{
|
|
||||||
size_t i;
|
|
||||||
|
|
||||||
visited[s] = 1;
|
|
||||||
|
|
||||||
for (i = 0; i < G->n; i++)
|
|
||||||
{
|
|
||||||
if (GraphEdgeGet(G, s, i) && !visited[i])
|
|
||||||
{
|
|
||||||
GraphTopologicalSortRecursive(G, i, visited, stack, stackSize);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
stack[*stackSize] = s;
|
|
||||||
(*stackSize)++;
|
|
||||||
}
|
|
||||||
|
|
||||||
Node *
|
|
||||||
GraphTopologicalSort(Graph * G, size_t * n)
|
|
||||||
{
|
|
||||||
Node *visited;
|
|
||||||
Node *stack;
|
|
||||||
Node *result;
|
|
||||||
|
|
||||||
size_t i;
|
|
||||||
size_t stackSize;
|
|
||||||
|
|
||||||
if (!G || !n)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
*n = 0;
|
|
||||||
|
|
||||||
result = Malloc(G->n * sizeof(Node));
|
|
||||||
if (!result)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
visited = Malloc(G->n * sizeof(Node));
|
|
||||||
memset(visited, 0, G->n * sizeof(Node));
|
|
||||||
stack = Malloc(G->n * sizeof(Node));
|
|
||||||
memset(stack, 0, G->n * sizeof(Node));
|
|
||||||
|
|
||||||
stackSize = 0;
|
|
||||||
|
|
||||||
for (i = 0; i < G->n; i++)
|
|
||||||
{
|
|
||||||
if (!visited[i])
|
|
||||||
{
|
|
||||||
GraphTopologicalSortRecursive(G, i, visited, stack, &stackSize);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Free(visited);
|
|
||||||
|
|
||||||
while (stackSize)
|
|
||||||
{
|
|
||||||
stackSize--;
|
|
||||||
result[*n] = stack[stackSize];
|
|
||||||
(*n)++;
|
|
||||||
}
|
|
||||||
|
|
||||||
Free(stack);
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
Graph *
|
|
||||||
GraphTranspose(Graph * G)
|
|
||||||
{
|
|
||||||
Graph *T = Malloc(sizeof(Graph));
|
|
||||||
size_t i, j;
|
|
||||||
|
|
||||||
T->n = G->n;
|
|
||||||
T->matrix = Malloc((G->n * G->n) * sizeof(Edge));
|
|
||||||
|
|
||||||
memset(T->matrix, 0, (T->n * T->n) * sizeof(Edge));
|
|
||||||
|
|
||||||
for (i = 0; i < G->n; i++)
|
|
||||||
{
|
|
||||||
for (j = 0; j < G->n; j++)
|
|
||||||
{
|
|
||||||
if (GraphEdgeGet(G, i, j))
|
|
||||||
{
|
|
||||||
GraphEdgeSet(T, j, i, GraphEdgeGet(G, i, j));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return T;
|
|
||||||
}
|
|
|
@ -1,456 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright (C) 2022-2023 Jordan Bancino <@jordan:bancino.net>
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person
|
|
||||||
* obtaining a copy of this software and associated documentation files
|
|
||||||
* (the "Software"), to deal in the Software without restriction,
|
|
||||||
* including without limitation the rights to use, copy, modify, merge,
|
|
||||||
* publish, distribute, sublicense, and/or sell copies of the Software,
|
|
||||||
* and to permit persons to whom the Software is furnished to do so,
|
|
||||||
* subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be
|
|
||||||
* included in all copies or portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
||||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
||||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
||||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
||||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
||||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
||||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
||||||
* SOFTWARE.
|
|
||||||
*/
|
|
||||||
#include <HashMap.h>
|
|
||||||
|
|
||||||
#include <Memory.h>
|
|
||||||
#include <Str.h>
|
|
||||||
#include <Array.h>
|
|
||||||
|
|
||||||
#include <stddef.h>
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
typedef struct HashMapBucket
|
|
||||||
{
|
|
||||||
unsigned long hash;
|
|
||||||
char *key;
|
|
||||||
void *value;
|
|
||||||
} HashMapBucket;
|
|
||||||
|
|
||||||
struct HashMap
|
|
||||||
{
|
|
||||||
size_t count;
|
|
||||||
size_t capacity;
|
|
||||||
HashMapBucket **entries;
|
|
||||||
|
|
||||||
unsigned long (*hashFunc) (const char *);
|
|
||||||
|
|
||||||
float maxLoad;
|
|
||||||
size_t iterator;
|
|
||||||
};
|
|
||||||
|
|
||||||
static unsigned long
|
|
||||||
HashMapHashKey(const char *key)
|
|
||||||
{
|
|
||||||
unsigned long hash = 2166136261u;
|
|
||||||
size_t i = 0;
|
|
||||||
|
|
||||||
while (key[i])
|
|
||||||
{
|
|
||||||
hash ^= (unsigned char) key[i];
|
|
||||||
hash *= 16777619;
|
|
||||||
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
|
|
||||||
return hash;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int
|
|
||||||
HashMapGrow(HashMap * map)
|
|
||||||
{
|
|
||||||
size_t oldCapacity;
|
|
||||||
size_t i;
|
|
||||||
HashMapBucket **newEntries;
|
|
||||||
|
|
||||||
if (!map)
|
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
oldCapacity = map->capacity;
|
|
||||||
map->capacity *= 2;
|
|
||||||
|
|
||||||
newEntries = Malloc(map->capacity * sizeof(HashMapBucket *));
|
|
||||||
if (!newEntries)
|
|
||||||
{
|
|
||||||
map->capacity /= 2;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
memset(newEntries, 0, map->capacity * sizeof(HashMapBucket *));
|
|
||||||
|
|
||||||
for (i = 0; i < oldCapacity; i++)
|
|
||||||
{
|
|
||||||
/* If there is a value here, and it isn't a tombstone */
|
|
||||||
if (map->entries[i] && map->entries[i]->hash)
|
|
||||||
{
|
|
||||||
/* Copy it to the new entries array */
|
|
||||||
size_t index = map->entries[i]->hash % map->capacity;
|
|
||||||
|
|
||||||
for (;;)
|
|
||||||
{
|
|
||||||
if (newEntries[index])
|
|
||||||
{
|
|
||||||
if (!newEntries[index]->hash)
|
|
||||||
{
|
|
||||||
Free(newEntries[index]);
|
|
||||||
newEntries[index] = map->entries[i];
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
newEntries[index] = map->entries[i];
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
index = (index + 1) % map->capacity;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
/* Either NULL or a tombstone */
|
|
||||||
Free(map->entries[i]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Free(map->entries);
|
|
||||||
map->entries = newEntries;
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
HashMap *
|
|
||||||
HashMapCreate(void)
|
|
||||||
{
|
|
||||||
HashMap *map = Malloc(sizeof(HashMap));
|
|
||||||
|
|
||||||
if (!map)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
map->maxLoad = 0.75;
|
|
||||||
map->count = 0;
|
|
||||||
map->capacity = 16;
|
|
||||||
map->iterator = 0;
|
|
||||||
map->hashFunc = HashMapHashKey;
|
|
||||||
|
|
||||||
map->entries = Malloc(map->capacity * sizeof(HashMapBucket *));
|
|
||||||
if (!map->entries)
|
|
||||||
{
|
|
||||||
Free(map);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
memset(map->entries, 0, map->capacity * sizeof(HashMapBucket *));
|
|
||||||
|
|
||||||
return map;
|
|
||||||
}
|
|
||||||
|
|
||||||
void *
|
|
||||||
HashMapDelete(HashMap * map, const char *key)
|
|
||||||
{
|
|
||||||
unsigned long hash;
|
|
||||||
size_t index;
|
|
||||||
|
|
||||||
if (!map || !key)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
hash = map->hashFunc(key);
|
|
||||||
index = hash % map->capacity;
|
|
||||||
|
|
||||||
for (;;)
|
|
||||||
{
|
|
||||||
HashMapBucket *bucket = map->entries[index];
|
|
||||||
|
|
||||||
if (!bucket)
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (bucket->hash == hash)
|
|
||||||
{
|
|
||||||
bucket->hash = 0;
|
|
||||||
return bucket->value;
|
|
||||||
}
|
|
||||||
|
|
||||||
index = (index + 1) % map->capacity;
|
|
||||||
}
|
|
||||||
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
HashMapFree(HashMap * map)
|
|
||||||
{
|
|
||||||
if (map)
|
|
||||||
{
|
|
||||||
size_t i;
|
|
||||||
|
|
||||||
for (i = 0; i < map->capacity; i++)
|
|
||||||
{
|
|
||||||
if (map->entries[i])
|
|
||||||
{
|
|
||||||
Free(map->entries[i]->key);
|
|
||||||
Free(map->entries[i]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Free(map->entries);
|
|
||||||
Free(map);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void *
|
|
||||||
HashMapGet(HashMap * map, const char *key)
|
|
||||||
{
|
|
||||||
unsigned long hash;
|
|
||||||
size_t index;
|
|
||||||
|
|
||||||
if (!map || !key)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
hash = map->hashFunc(key);
|
|
||||||
index = hash % map->capacity;
|
|
||||||
|
|
||||||
for (;;)
|
|
||||||
{
|
|
||||||
HashMapBucket *bucket = map->entries[index];
|
|
||||||
|
|
||||||
if (!bucket)
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (bucket->hash == hash)
|
|
||||||
{
|
|
||||||
return bucket->value;
|
|
||||||
}
|
|
||||||
|
|
||||||
index = (index + 1) % map->capacity;
|
|
||||||
}
|
|
||||||
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
|
||||||
HashMapIterateReentrant(HashMap * map, char **key, void **value, size_t * i)
|
|
||||||
{
|
|
||||||
if (!map)
|
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (*i >= map->capacity)
|
|
||||||
{
|
|
||||||
*i = 0;
|
|
||||||
*key = NULL;
|
|
||||||
*value = NULL;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
while (*i < map->capacity)
|
|
||||||
{
|
|
||||||
HashMapBucket *bucket = map->entries[*i];
|
|
||||||
|
|
||||||
*i = *i + 1;
|
|
||||||
|
|
||||||
if (bucket && bucket->hash)
|
|
||||||
{
|
|
||||||
*key = bucket->key;
|
|
||||||
*value = bucket->value;
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
*i = 0;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
|
||||||
HashMapIterate(HashMap * map, char **key, void **value)
|
|
||||||
{
|
|
||||||
if (!map)
|
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return HashMapIterateReentrant(map, key, value, &map->iterator);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
HashMapMaxLoadSet(HashMap * map, float load)
|
|
||||||
{
|
|
||||||
if (!map || (load > 1.0 || load <= 0))
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
map->maxLoad = load;
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
HashMapFunctionSet(HashMap * map, unsigned long (*hashFunc) (const char *))
|
|
||||||
{
|
|
||||||
if (!map || !hashFunc)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
map->hashFunc = hashFunc;
|
|
||||||
}
|
|
||||||
|
|
||||||
void *
|
|
||||||
HashMapSet(HashMap * map, char *key, void *value)
|
|
||||||
{
|
|
||||||
unsigned long hash;
|
|
||||||
size_t index;
|
|
||||||
|
|
||||||
if (!map || !key || !value)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
key = StrDuplicate(key);
|
|
||||||
if (!key)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (map->count + 1 > map->capacity * map->maxLoad)
|
|
||||||
{
|
|
||||||
HashMapGrow(map);
|
|
||||||
}
|
|
||||||
|
|
||||||
hash = map->hashFunc(key);
|
|
||||||
index = hash % map->capacity;
|
|
||||||
|
|
||||||
for (;;)
|
|
||||||
{
|
|
||||||
HashMapBucket *bucket = map->entries[index];
|
|
||||||
|
|
||||||
if (!bucket)
|
|
||||||
{
|
|
||||||
bucket = Malloc(sizeof(HashMapBucket));
|
|
||||||
if (!bucket)
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
bucket->hash = hash;
|
|
||||||
bucket->key = key;
|
|
||||||
bucket->value = value;
|
|
||||||
map->entries[index] = bucket;
|
|
||||||
map->count++;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!bucket->hash)
|
|
||||||
{
|
|
||||||
bucket->hash = hash;
|
|
||||||
Free(bucket->key);
|
|
||||||
bucket->key = key;
|
|
||||||
bucket->value = value;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (bucket->hash == hash)
|
|
||||||
{
|
|
||||||
void *oldValue = bucket->value;
|
|
||||||
|
|
||||||
Free(bucket->key);
|
|
||||||
bucket->key = key;
|
|
||||||
|
|
||||||
bucket->value = value;
|
|
||||||
return oldValue;
|
|
||||||
}
|
|
||||||
|
|
||||||
index = (index + 1) % map->capacity;
|
|
||||||
}
|
|
||||||
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
HashMapIterateFree(char *key, void *value)
|
|
||||||
{
|
|
||||||
if (key)
|
|
||||||
{
|
|
||||||
Free(key);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (value)
|
|
||||||
{
|
|
||||||
Free(value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Array *
|
|
||||||
HashMapKeys(HashMap * map)
|
|
||||||
{
|
|
||||||
Array *arr;
|
|
||||||
|
|
||||||
char *key;
|
|
||||||
void *val;
|
|
||||||
|
|
||||||
if (!map)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
arr = ArrayCreate();
|
|
||||||
if (!arr)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
while (HashMapIterate(map, &key, &val))
|
|
||||||
{
|
|
||||||
ArrayAdd(arr, key);
|
|
||||||
}
|
|
||||||
|
|
||||||
return arr;
|
|
||||||
}
|
|
||||||
|
|
||||||
Array *
|
|
||||||
HashMapValues(HashMap * map)
|
|
||||||
{
|
|
||||||
Array *arr;
|
|
||||||
|
|
||||||
char *key;
|
|
||||||
void *val;
|
|
||||||
|
|
||||||
if (!map)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
arr = ArrayCreate();
|
|
||||||
if (!arr)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
while (HashMapIterate(map, &key, &val))
|
|
||||||
{
|
|
||||||
ArrayAdd(arr, val);
|
|
||||||
}
|
|
||||||
|
|
||||||
return arr;
|
|
||||||
}
|
|
|
@ -1,665 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright (C) 2022-2023 Jordan Bancino <@jordan:bancino.net>
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person
|
|
||||||
* obtaining a copy of this software and associated documentation files
|
|
||||||
* (the "Software"), to deal in the Software without restriction,
|
|
||||||
* including without limitation the rights to use, copy, modify, merge,
|
|
||||||
* publish, distribute, sublicense, and/or sell copies of the Software,
|
|
||||||
* and to permit persons to whom the Software is furnished to do so,
|
|
||||||
* subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be
|
|
||||||
* included in all copies or portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
||||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
||||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
||||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
||||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
||||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
||||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
||||||
* SOFTWARE.
|
|
||||||
*/
|
|
||||||
#include <HeaderParser.h>
|
|
||||||
|
|
||||||
#include <Memory.h>
|
|
||||||
#include <Str.h>
|
|
||||||
|
|
||||||
#include <string.h>
|
|
||||||
#include <ctype.h>
|
|
||||||
|
|
||||||
static int
|
|
||||||
HeaderConsumeWhitespace(HeaderExpr * expr)
|
|
||||||
{
|
|
||||||
int c;
|
|
||||||
|
|
||||||
while (1)
|
|
||||||
{
|
|
||||||
c = StreamGetc(expr->state.stream);
|
|
||||||
|
|
||||||
if (StreamEof(expr->state.stream) || StreamError(expr->state.stream))
|
|
||||||
{
|
|
||||||
expr->type = HP_EOF;
|
|
||||||
expr->data.error.msg = "End of stream reached.";
|
|
||||||
expr->data.error.lineNo = expr->state.lineNo;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isspace(c))
|
|
||||||
{
|
|
||||||
if (c == '\n')
|
|
||||||
{
|
|
||||||
expr->state.lineNo++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return c;
|
|
||||||
}
|
|
||||||
|
|
||||||
static char *
|
|
||||||
HeaderConsumeWord(HeaderExpr * expr)
|
|
||||||
{
|
|
||||||
char *str = Malloc(16 * sizeof(char));
|
|
||||||
int len = 16;
|
|
||||||
int i;
|
|
||||||
int c;
|
|
||||||
|
|
||||||
if (!str)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
c = HeaderConsumeWhitespace(expr);
|
|
||||||
|
|
||||||
i = 0;
|
|
||||||
str[i] = c;
|
|
||||||
i++;
|
|
||||||
|
|
||||||
while (!isspace(c = StreamGetc(expr->state.stream)))
|
|
||||||
{
|
|
||||||
if (i >= len)
|
|
||||||
{
|
|
||||||
len *= 2;
|
|
||||||
str = Realloc(str, len * sizeof(char));
|
|
||||||
}
|
|
||||||
|
|
||||||
str[i] = c;
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (i >= len)
|
|
||||||
{
|
|
||||||
len++;
|
|
||||||
str = Realloc(str, len * sizeof(char));
|
|
||||||
}
|
|
||||||
|
|
||||||
str[i] = '\0';
|
|
||||||
|
|
||||||
if (c != EOF)
|
|
||||||
{
|
|
||||||
StreamUngetc(expr->state.stream, c);
|
|
||||||
}
|
|
||||||
|
|
||||||
return str;
|
|
||||||
}
|
|
||||||
|
|
||||||
static char *
|
|
||||||
HeaderConsumeAlnum(HeaderExpr * expr)
|
|
||||||
{
|
|
||||||
char *str = Malloc(16 * sizeof(char));
|
|
||||||
int len = 16;
|
|
||||||
int i;
|
|
||||||
int c;
|
|
||||||
|
|
||||||
if (!str)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
c = HeaderConsumeWhitespace(expr);
|
|
||||||
|
|
||||||
i = 0;
|
|
||||||
str[i] = c;
|
|
||||||
i++;
|
|
||||||
|
|
||||||
while (isalnum(c = StreamGetc(expr->state.stream)))
|
|
||||||
{
|
|
||||||
if (i >= len)
|
|
||||||
{
|
|
||||||
len *= 2;
|
|
||||||
str = Realloc(str, len * sizeof(char));
|
|
||||||
}
|
|
||||||
|
|
||||||
str[i] = c;
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (i >= len)
|
|
||||||
{
|
|
||||||
len++;
|
|
||||||
str = Realloc(str, len * sizeof(char));
|
|
||||||
}
|
|
||||||
|
|
||||||
str[i] = '\0';
|
|
||||||
|
|
||||||
if (c != EOF)
|
|
||||||
{
|
|
||||||
StreamUngetc(expr->state.stream, c);
|
|
||||||
}
|
|
||||||
|
|
||||||
return str;
|
|
||||||
}
|
|
||||||
|
|
||||||
static char *
|
|
||||||
HeaderConsumeArg(HeaderExpr * expr)
|
|
||||||
{
|
|
||||||
char *str = Malloc(16 * sizeof(char));
|
|
||||||
int len = 16;
|
|
||||||
int i;
|
|
||||||
int c;
|
|
||||||
int block = 0;
|
|
||||||
|
|
||||||
if (!str)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
c = HeaderConsumeWhitespace(expr);
|
|
||||||
|
|
||||||
i = 0;
|
|
||||||
str[i] = c;
|
|
||||||
i++;
|
|
||||||
|
|
||||||
while (((c = StreamGetc(expr->state.stream)) != ',' && c != ')') || block > 0)
|
|
||||||
{
|
|
||||||
if (i >= len)
|
|
||||||
{
|
|
||||||
len *= 2;
|
|
||||||
str = Realloc(str, len * sizeof(char));
|
|
||||||
}
|
|
||||||
|
|
||||||
str[i] = c;
|
|
||||||
i++;
|
|
||||||
|
|
||||||
if (c == '(')
|
|
||||||
{
|
|
||||||
block++;
|
|
||||||
}
|
|
||||||
else if (c == ')')
|
|
||||||
{
|
|
||||||
block--;
|
|
||||||
}
|
|
||||||
else if (c == '\n')
|
|
||||||
{
|
|
||||||
expr->state.lineNo++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (i >= len)
|
|
||||||
{
|
|
||||||
len++;
|
|
||||||
str = Realloc(str, len * sizeof(char));
|
|
||||||
}
|
|
||||||
|
|
||||||
str[i] = '\0';
|
|
||||||
|
|
||||||
if (c != EOF)
|
|
||||||
{
|
|
||||||
StreamUngetc(expr->state.stream, c);
|
|
||||||
}
|
|
||||||
|
|
||||||
return str;
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
HeaderParse(Stream * stream, HeaderExpr * expr)
|
|
||||||
{
|
|
||||||
int c;
|
|
||||||
|
|
||||||
if (!expr)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!stream)
|
|
||||||
{
|
|
||||||
expr->type = HP_PARSE_ERROR;
|
|
||||||
expr->data.error.msg = "NULL pointer to stream.";
|
|
||||||
expr->data.error.lineNo = -1;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (expr->type == HP_DECLARATION && expr->data.declaration.args)
|
|
||||||
{
|
|
||||||
size_t i;
|
|
||||||
|
|
||||||
for (i = 0; i < ArraySize(expr->data.declaration.args); i++)
|
|
||||||
{
|
|
||||||
Free(ArrayGet(expr->data.declaration.args, i));
|
|
||||||
}
|
|
||||||
|
|
||||||
ArrayFree(expr->data.declaration.args);
|
|
||||||
}
|
|
||||||
|
|
||||||
expr->state.stream = stream;
|
|
||||||
if (!expr->state.lineNo)
|
|
||||||
{
|
|
||||||
expr->state.lineNo = 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
c = HeaderConsumeWhitespace(expr);
|
|
||||||
|
|
||||||
if (StreamEof(stream) || StreamError(stream))
|
|
||||||
{
|
|
||||||
expr->type = HP_EOF;
|
|
||||||
expr->data.error.msg = "End of stream reached.";
|
|
||||||
expr->data.error.lineNo = expr->state.lineNo;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (c == '/')
|
|
||||||
{
|
|
||||||
int i = 0;
|
|
||||||
|
|
||||||
c = StreamGetc(expr->state.stream);
|
|
||||||
if (c != '*')
|
|
||||||
{
|
|
||||||
expr->type = HP_SYNTAX_ERROR;
|
|
||||||
expr->data.error.msg = "Expected comment opening.";
|
|
||||||
expr->data.error.lineNo = expr->state.lineNo;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
expr->type = HP_COMMENT;
|
|
||||||
while (1)
|
|
||||||
{
|
|
||||||
if (i >= HEADER_EXPR_MAX - 1)
|
|
||||||
{
|
|
||||||
expr->type = HP_PARSE_ERROR;
|
|
||||||
expr->data.error.msg = "Memory limit exceeded while parsing comment.";
|
|
||||||
expr->data.error.lineNo = expr->state.lineNo;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
c = StreamGetc(expr->state.stream);
|
|
||||||
|
|
||||||
if (StreamEof(expr->state.stream) || StreamError(expr->state.stream))
|
|
||||||
{
|
|
||||||
expr->type = HP_SYNTAX_ERROR;
|
|
||||||
expr->data.error.msg = "Unterminated comment.";
|
|
||||||
expr->data.error.lineNo = expr->state.lineNo;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (c == '*')
|
|
||||||
{
|
|
||||||
c = StreamGetc(expr->state.stream);
|
|
||||||
if (c == '/')
|
|
||||||
{
|
|
||||||
expr->data.text[i] = '\0';
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
expr->data.text[i] = '*';
|
|
||||||
i++;
|
|
||||||
expr->data.text[i] = c;
|
|
||||||
i++;
|
|
||||||
if (c == '\n')
|
|
||||||
{
|
|
||||||
expr->state.lineNo++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
expr->data.text[i] = c;
|
|
||||||
i++;
|
|
||||||
|
|
||||||
if (c == '\n')
|
|
||||||
{
|
|
||||||
expr->state.lineNo++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (c == '#')
|
|
||||||
{
|
|
||||||
int i = 0;
|
|
||||||
char *word;
|
|
||||||
|
|
||||||
expr->type = HP_PREPROCESSOR_DIRECTIVE;
|
|
||||||
expr->data.text[i] = '#';
|
|
||||||
i++;
|
|
||||||
|
|
||||||
word = HeaderConsumeWord(expr);
|
|
||||||
|
|
||||||
strncpy(expr->data.text + i, word, HEADER_EXPR_MAX - i - 1);
|
|
||||||
i += strlen(word);
|
|
||||||
|
|
||||||
if (StrEquals(word, "include") ||
|
|
||||||
StrEquals(word, "undef") ||
|
|
||||||
StrEquals(word, "ifdef") ||
|
|
||||||
StrEquals(word, "ifndef"))
|
|
||||||
{
|
|
||||||
/* Read one more word */
|
|
||||||
Free(word);
|
|
||||||
word = HeaderConsumeWord(expr);
|
|
||||||
|
|
||||||
if (i + strlen(word) + 1 >= HEADER_EXPR_MAX)
|
|
||||||
{
|
|
||||||
expr->type = HP_PARSE_ERROR;
|
|
||||||
expr->data.error.msg = "Memory limit reached parsing preprocessor directive.";
|
|
||||||
expr->data.error.lineNo = expr->state.lineNo;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
strncpy(expr->data.text + i + 1, word, HEADER_EXPR_MAX - i - 1);
|
|
||||||
expr->data.text[i] = ' ';
|
|
||||||
}
|
|
||||||
|
|
||||||
Free(word);
|
|
||||||
}
|
|
||||||
else if (StrEquals(word, "define") ||
|
|
||||||
StrEquals(word, "if") ||
|
|
||||||
StrEquals(word, "elif") ||
|
|
||||||
StrEquals(word, "error"))
|
|
||||||
{
|
|
||||||
int pC = 0;
|
|
||||||
|
|
||||||
Free(word);
|
|
||||||
expr->data.text[i] = ' ';
|
|
||||||
i++;
|
|
||||||
|
|
||||||
while (1)
|
|
||||||
{
|
|
||||||
if (i >= HEADER_EXPR_MAX - 1)
|
|
||||||
{
|
|
||||||
expr->type = HP_PARSE_ERROR;
|
|
||||||
expr->data.error.msg = "Memory limit reached parsing preprocessor directive.";
|
|
||||||
expr->data.error.lineNo = expr->state.lineNo;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
c = StreamGetc(expr->state.stream);
|
|
||||||
|
|
||||||
if (StreamEof(expr->state.stream) || StreamError(expr->state.stream))
|
|
||||||
{
|
|
||||||
expr->type = HP_SYNTAX_ERROR;
|
|
||||||
expr->data.error.msg = "Unterminated preprocessor directive.";
|
|
||||||
expr->data.error.lineNo = expr->state.lineNo;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (c == '\n')
|
|
||||||
{
|
|
||||||
expr->state.lineNo++;
|
|
||||||
if (pC != '\\')
|
|
||||||
{
|
|
||||||
expr->data.text[i] = '\0';
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
expr->data.text[i] = c;
|
|
||||||
i++;
|
|
||||||
|
|
||||||
pC = c;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (StrEquals(word, "else") ||
|
|
||||||
StrEquals(word, "endif"))
|
|
||||||
{
|
|
||||||
/* Read no more words, that's the whole directive */
|
|
||||||
Free(word);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
Free(word);
|
|
||||||
|
|
||||||
expr->type = HP_SYNTAX_ERROR;
|
|
||||||
expr->data.error.msg = "Unknown preprocessor directive.";
|
|
||||||
expr->data.error.lineNo = expr->state.lineNo;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
char *word;
|
|
||||||
|
|
||||||
StreamUngetc(expr->state.stream, c);
|
|
||||||
word = HeaderConsumeWord(expr);
|
|
||||||
|
|
||||||
if (StrEquals(word, "typedef"))
|
|
||||||
{
|
|
||||||
int block = 0;
|
|
||||||
int i = 0;
|
|
||||||
|
|
||||||
expr->type = HP_TYPEDEF;
|
|
||||||
strncpy(expr->data.text, word, HEADER_EXPR_MAX - 1);
|
|
||||||
i += strlen(word);
|
|
||||||
expr->data.text[i] = ' ';
|
|
||||||
i++;
|
|
||||||
|
|
||||||
while (1)
|
|
||||||
{
|
|
||||||
if (i >= HEADER_EXPR_MAX - 1)
|
|
||||||
{
|
|
||||||
expr->type = HP_PARSE_ERROR;
|
|
||||||
expr->data.error.msg = "Memory limit exceeded while parsing typedef.";
|
|
||||||
expr->data.error.lineNo = expr->state.lineNo;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
c = StreamGetc(expr->state.stream);
|
|
||||||
|
|
||||||
if (StreamEof(expr->state.stream) || StreamError(expr->state.stream))
|
|
||||||
{
|
|
||||||
expr->type = HP_SYNTAX_ERROR;
|
|
||||||
expr->data.error.msg = "Unterminated typedef.";
|
|
||||||
expr->data.error.lineNo = expr->state.lineNo;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
expr->data.text[i] = c;
|
|
||||||
i++;
|
|
||||||
|
|
||||||
if (c == '{')
|
|
||||||
{
|
|
||||||
block++;
|
|
||||||
}
|
|
||||||
else if (c == '}')
|
|
||||||
{
|
|
||||||
block--;
|
|
||||||
}
|
|
||||||
else if (c == '\n')
|
|
||||||
{
|
|
||||||
expr->state.lineNo++;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (block <= 0 && c == ';')
|
|
||||||
{
|
|
||||||
expr->data.text[i] = '\0';
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (StrEquals(word, "extern"))
|
|
||||||
{
|
|
||||||
int wordLimit = sizeof(expr->data.declaration.returnType) - 8;
|
|
||||||
int wordLen;
|
|
||||||
|
|
||||||
Free(word);
|
|
||||||
|
|
||||||
word = HeaderConsumeWord(expr);
|
|
||||||
wordLen = strlen(word);
|
|
||||||
if (wordLen > wordLimit)
|
|
||||||
{
|
|
||||||
expr->type = HP_PARSE_ERROR;
|
|
||||||
expr->data.error.msg = "Return of declaration exceeds length limit.";
|
|
||||||
expr->data.error.lineNo = expr->state.lineNo;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
int i = wordLen;
|
|
||||||
|
|
||||||
expr->type = HP_GLOBAL;
|
|
||||||
strncpy(expr->data.global.type, word, wordLimit);
|
|
||||||
|
|
||||||
if (StrEquals(word, "struct") ||
|
|
||||||
StrEquals(word, "enum") ||
|
|
||||||
StrEquals(word, "const") ||
|
|
||||||
StrEquals(word, "unsigned"))
|
|
||||||
{
|
|
||||||
Free(word);
|
|
||||||
word = HeaderConsumeWord(expr);
|
|
||||||
wordLen = strlen(word);
|
|
||||||
expr->data.global.type[i] = ' ';
|
|
||||||
|
|
||||||
strncpy(expr->data.global.type + i + 1, word, wordLen + 1);
|
|
||||||
i += wordLen + 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
Free(word);
|
|
||||||
|
|
||||||
c = HeaderConsumeWhitespace(expr);
|
|
||||||
if (c == '*')
|
|
||||||
{
|
|
||||||
expr->data.global.type[i] = ' ';
|
|
||||||
|
|
||||||
i++;
|
|
||||||
expr->data.global.type[i] = '*';
|
|
||||||
|
|
||||||
i++;
|
|
||||||
while ((c = HeaderConsumeWhitespace(expr)) == '*')
|
|
||||||
{
|
|
||||||
expr->data.global.type[i] = c;
|
|
||||||
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
StreamUngetc(expr->state.stream, c);
|
|
||||||
word = HeaderConsumeAlnum(expr);
|
|
||||||
|
|
||||||
wordLen = strlen(word);
|
|
||||||
wordLimit = sizeof(expr->data.declaration.name) - 1;
|
|
||||||
|
|
||||||
if (wordLen > wordLimit)
|
|
||||||
{
|
|
||||||
expr->type = HP_SYNTAX_ERROR;
|
|
||||||
expr->data.error.msg = "Function name too long.";
|
|
||||||
expr->data.error.lineNo = expr->state.lineNo;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
strncpy(expr->data.global.name, word, wordLimit);
|
|
||||||
Free(word);
|
|
||||||
word = NULL;
|
|
||||||
|
|
||||||
c = HeaderConsumeWhitespace(expr);
|
|
||||||
|
|
||||||
if (c == ';')
|
|
||||||
{
|
|
||||||
/* That's the end of the global. */
|
|
||||||
}
|
|
||||||
else if (c == '[')
|
|
||||||
{
|
|
||||||
/* Looks like we have an array. Slurp all the
|
|
||||||
* dimensions */
|
|
||||||
int i = wordLen;
|
|
||||||
|
|
||||||
expr->data.global.name[i] = '[';
|
|
||||||
|
|
||||||
i++;
|
|
||||||
|
|
||||||
while (1)
|
|
||||||
{
|
|
||||||
if (i >= HEADER_EXPR_MAX - wordLen)
|
|
||||||
{
|
|
||||||
expr->type = HP_PARSE_ERROR;
|
|
||||||
expr->data.error.msg = "Memory limit exceeded while parsing global array.";
|
|
||||||
expr->data.error.lineNo = expr->state.lineNo;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
c = StreamGetc(expr->state.stream);
|
|
||||||
|
|
||||||
if (StreamEof(expr->state.stream) || StreamError(expr->state.stream))
|
|
||||||
{
|
|
||||||
expr->type = HP_SYNTAX_ERROR;
|
|
||||||
expr->data.error.msg = "Unterminated global array.";
|
|
||||||
expr->data.error.lineNo = expr->state.lineNo;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (c == ';')
|
|
||||||
{
|
|
||||||
expr->data.global.name[i] = '\0';
|
|
||||||
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
expr->data.global.name[i] = c;
|
|
||||||
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (c == '(')
|
|
||||||
{
|
|
||||||
expr->type = HP_DECLARATION;
|
|
||||||
expr->data.declaration.args = ArrayCreate();
|
|
||||||
do
|
|
||||||
{
|
|
||||||
word = HeaderConsumeArg(expr);
|
|
||||||
ArrayAdd(expr->data.declaration.args, word);
|
|
||||||
word = NULL;
|
|
||||||
}
|
|
||||||
while ((!StreamEof(expr->state.stream)) && ((c = HeaderConsumeWhitespace(expr)) != ')'));
|
|
||||||
|
|
||||||
if (StreamEof(expr->state.stream))
|
|
||||||
{
|
|
||||||
expr->type = HP_SYNTAX_ERROR;
|
|
||||||
expr->data.error.msg = "End of file reached before ')'.";
|
|
||||||
expr->data.error.lineNo = expr->state.lineNo;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
c = HeaderConsumeWhitespace(expr);
|
|
||||||
if (c != ';')
|
|
||||||
{
|
|
||||||
expr->type = HP_SYNTAX_ERROR;
|
|
||||||
expr->data.error.msg = "Expected ';'.";
|
|
||||||
expr->data.error.lineNo = expr->state.lineNo;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
expr->type = HP_SYNTAX_ERROR;
|
|
||||||
expr->data.error.msg = "Expected ';', '[', or '('";
|
|
||||||
expr->data.error.lineNo = expr->state.lineNo;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
/* Cope with preprocessor macro expansions at the top
|
|
||||||
* level. */
|
|
||||||
expr->type = HP_UNKNOWN;
|
|
||||||
strncpy(expr->data.text, word, HEADER_EXPR_MAX - 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
Free(word);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,302 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright (C) 2022-2023 Jordan Bancino <@jordan:bancino.net>
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person
|
|
||||||
* obtaining a copy of this software and associated documentation files
|
|
||||||
* (the "Software"), to deal in the Software without restriction,
|
|
||||||
* including without limitation the rights to use, copy, modify, merge,
|
|
||||||
* publish, distribute, sublicense, and/or sell copies of the Software,
|
|
||||||
* and to permit persons to whom the Software is furnished to do so,
|
|
||||||
* subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be
|
|
||||||
* included in all copies or portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
||||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
||||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
||||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
||||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
||||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
||||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
||||||
* SOFTWARE.
|
|
||||||
*/
|
|
||||||
#include <HttpClient.h>
|
|
||||||
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <ctype.h>
|
|
||||||
|
|
||||||
#include <unistd.h>
|
|
||||||
#include <errno.h>
|
|
||||||
#include <sys/types.h>
|
|
||||||
#include <sys/socket.h>
|
|
||||||
#include <netdb.h>
|
|
||||||
|
|
||||||
#include <Http.h>
|
|
||||||
#include <Memory.h>
|
|
||||||
#include <Util.h>
|
|
||||||
#include <Tls.h>
|
|
||||||
|
|
||||||
struct HttpClientContext
|
|
||||||
{
|
|
||||||
HashMap *responseHeaders;
|
|
||||||
Stream *stream;
|
|
||||||
};
|
|
||||||
|
|
||||||
HttpClientContext *
|
|
||||||
HttpRequest(HttpRequestMethod method, int flags, unsigned short port, char *host, char *path)
|
|
||||||
{
|
|
||||||
HttpClientContext *context;
|
|
||||||
|
|
||||||
int sd = -1;
|
|
||||||
struct addrinfo hints, *res, *res0;
|
|
||||||
int error;
|
|
||||||
|
|
||||||
char serv[8];
|
|
||||||
|
|
||||||
if (!method || !host || !path)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifndef TLS_IMPL
|
|
||||||
if (flags & HTTP_FLAG_TLS)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
if (!port)
|
|
||||||
{
|
|
||||||
if (flags & HTTP_FLAG_TLS)
|
|
||||||
{
|
|
||||||
strcpy(serv, "https");
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
strcpy(serv, "www");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
snprintf(serv, sizeof(serv), "%hu", port);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
context = Malloc(sizeof(HttpClientContext));
|
|
||||||
if (!context)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
memset(&hints, 0, sizeof(hints));
|
|
||||||
hints.ai_family = AF_UNSPEC;
|
|
||||||
hints.ai_socktype = SOCK_STREAM;
|
|
||||||
error = getaddrinfo(host, serv, &hints, &res0);
|
|
||||||
|
|
||||||
if (error)
|
|
||||||
{
|
|
||||||
Free(context);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (res = res0; res; res = res->ai_next)
|
|
||||||
{
|
|
||||||
sd = socket(res->ai_family, res->ai_socktype,
|
|
||||||
res->ai_protocol);
|
|
||||||
|
|
||||||
if (sd < 0)
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (connect(sd, res->ai_addr, res->ai_addrlen) < 0)
|
|
||||||
{
|
|
||||||
close(sd);
|
|
||||||
sd = -1;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (sd < 0)
|
|
||||||
{
|
|
||||||
Free(context);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
freeaddrinfo(res0);
|
|
||||||
|
|
||||||
#ifdef TLS_IMPL
|
|
||||||
if (flags & HTTP_FLAG_TLS)
|
|
||||||
{
|
|
||||||
context->stream = TlsClientStream(sd, host);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
context->stream = StreamFd(sd);
|
|
||||||
}
|
|
||||||
#else
|
|
||||||
context->stream = StreamFd(sd);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
if (!context->stream)
|
|
||||||
{
|
|
||||||
Free(context);
|
|
||||||
close(sd);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
StreamPrintf(context->stream, "%s %s HTTP/1.0\r\n",
|
|
||||||
HttpRequestMethodToString(method), path);
|
|
||||||
|
|
||||||
HttpRequestHeader(context, "Connection", "close");
|
|
||||||
HttpRequestHeader(context, "User-Agent", LIB_NAME "/" LIB_VERSION);
|
|
||||||
HttpRequestHeader(context, "Host", host);
|
|
||||||
|
|
||||||
return context;
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
HttpRequestHeader(HttpClientContext * context, char *key, char *val)
|
|
||||||
{
|
|
||||||
if (!context || !key || !val)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
StreamPrintf(context->stream, "%s: %s\r\n", key, val);
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
HttpRequestSendHeaders(HttpClientContext * context)
|
|
||||||
{
|
|
||||||
if (!context)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
StreamPuts(context->stream, "\r\n");
|
|
||||||
StreamFlush(context->stream);
|
|
||||||
}
|
|
||||||
|
|
||||||
HttpStatus
|
|
||||||
HttpRequestSend(HttpClientContext * context)
|
|
||||||
{
|
|
||||||
HttpStatus status = HTTP_STATUS_UNKNOWN;
|
|
||||||
|
|
||||||
char *line = NULL;
|
|
||||||
ssize_t lineLen;
|
|
||||||
size_t lineSize = 0;
|
|
||||||
char *tmp;
|
|
||||||
|
|
||||||
if (!context)
|
|
||||||
{
|
|
||||||
goto finish;
|
|
||||||
}
|
|
||||||
|
|
||||||
StreamFlush(context->stream);
|
|
||||||
|
|
||||||
lineLen = UtilGetLine(&line, &lineSize, context->stream);
|
|
||||||
|
|
||||||
while (lineLen == -1 && errno == EAGAIN)
|
|
||||||
{
|
|
||||||
StreamClearError(context->stream);
|
|
||||||
lineLen = UtilGetLine(&line, &lineSize, context->stream);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (lineLen == -1)
|
|
||||||
{
|
|
||||||
goto finish;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Line must contain at least "HTTP/x.x xxx" */
|
|
||||||
if (lineLen < 12)
|
|
||||||
{
|
|
||||||
goto finish;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!(strncmp(line, "HTTP/1.0", 8) == 0 ||
|
|
||||||
strncmp(line, "HTTP/1.1", 8) == 0))
|
|
||||||
{
|
|
||||||
goto finish;
|
|
||||||
}
|
|
||||||
|
|
||||||
tmp = line + 9;
|
|
||||||
|
|
||||||
while (isspace((unsigned char) *tmp) && *tmp != '\0')
|
|
||||||
{
|
|
||||||
tmp++;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!*tmp)
|
|
||||||
{
|
|
||||||
goto finish;
|
|
||||||
}
|
|
||||||
|
|
||||||
status = atoi(tmp);
|
|
||||||
|
|
||||||
if (!status)
|
|
||||||
{
|
|
||||||
status = HTTP_STATUS_UNKNOWN;
|
|
||||||
goto finish;
|
|
||||||
}
|
|
||||||
|
|
||||||
context->responseHeaders = HttpParseHeaders(context->stream);
|
|
||||||
if (!context->responseHeaders)
|
|
||||||
{
|
|
||||||
status = HTTP_STATUS_UNKNOWN;
|
|
||||||
goto finish;
|
|
||||||
}
|
|
||||||
|
|
||||||
finish:
|
|
||||||
Free(line);
|
|
||||||
return status;
|
|
||||||
}
|
|
||||||
|
|
||||||
HashMap *
|
|
||||||
HttpResponseHeaders(HttpClientContext * context)
|
|
||||||
{
|
|
||||||
if (!context)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
return context->responseHeaders;
|
|
||||||
}
|
|
||||||
|
|
||||||
Stream *
|
|
||||||
HttpClientStream(HttpClientContext * context)
|
|
||||||
{
|
|
||||||
if (!context)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
return context->stream;
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
HttpClientContextFree(HttpClientContext * context)
|
|
||||||
{
|
|
||||||
char *key;
|
|
||||||
void *val;
|
|
||||||
|
|
||||||
if (!context)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
while (HashMapIterate(context->responseHeaders, &key, &val))
|
|
||||||
{
|
|
||||||
Free(val);
|
|
||||||
}
|
|
||||||
|
|
||||||
HashMapFree(context->responseHeaders);
|
|
||||||
|
|
||||||
StreamClose(context->stream);
|
|
||||||
Free(context);
|
|
||||||
}
|
|
|
@ -1,300 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright (C) 2022-2023 Jordan Bancino <@jordan:bancino.net>
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person
|
|
||||||
* obtaining a copy of this software and associated documentation files
|
|
||||||
* (the "Software"), to deal in the Software without restriction,
|
|
||||||
* including without limitation the rights to use, copy, modify, merge,
|
|
||||||
* publish, distribute, sublicense, and/or sell copies of the Software,
|
|
||||||
* and to permit persons to whom the Software is furnished to do so,
|
|
||||||
* subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be
|
|
||||||
* included in all copies or portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
||||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
||||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
||||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
||||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
||||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
||||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
||||||
* SOFTWARE.
|
|
||||||
*/
|
|
||||||
#include <HttpRouter.h>
|
|
||||||
|
|
||||||
#include <Memory.h>
|
|
||||||
#include <HashMap.h>
|
|
||||||
#include <Str.h>
|
|
||||||
|
|
||||||
#include <regex.h>
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
#define REG_FLAGS (REG_EXTENDED)
|
|
||||||
#define REG_MAX_SUB 8
|
|
||||||
|
|
||||||
typedef struct RouteNode
|
|
||||||
{
|
|
||||||
HttpRouteFunc *exec;
|
|
||||||
HashMap *children;
|
|
||||||
|
|
||||||
regex_t regex;
|
|
||||||
} RouteNode;
|
|
||||||
|
|
||||||
struct HttpRouter
|
|
||||||
{
|
|
||||||
RouteNode *root;
|
|
||||||
};
|
|
||||||
|
|
||||||
static RouteNode *
|
|
||||||
RouteNodeCreate(char *regex, HttpRouteFunc * exec)
|
|
||||||
{
|
|
||||||
RouteNode *node;
|
|
||||||
|
|
||||||
if (!regex)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
node = Malloc(sizeof(RouteNode));
|
|
||||||
|
|
||||||
if (!node)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
node->children = HashMapCreate();
|
|
||||||
if (!node->children)
|
|
||||||
{
|
|
||||||
Free(node);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Force the regex to match the entire path part exactly. */
|
|
||||||
regex = StrConcat(3, "^", regex, "$");
|
|
||||||
if (!regex)
|
|
||||||
{
|
|
||||||
Free(node);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (regcomp(&node->regex, regex, REG_FLAGS) != 0)
|
|
||||||
{
|
|
||||||
HashMapFree(node->children);
|
|
||||||
Free(node);
|
|
||||||
Free(regex);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
node->exec = exec;
|
|
||||||
|
|
||||||
Free(regex);
|
|
||||||
return node;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
RouteNodeFree(RouteNode * node)
|
|
||||||
{
|
|
||||||
char *key;
|
|
||||||
RouteNode *val;
|
|
||||||
|
|
||||||
if (!node)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
while (HashMapIterate(node->children, &key, (void **) &val))
|
|
||||||
{
|
|
||||||
RouteNodeFree(val);
|
|
||||||
}
|
|
||||||
|
|
||||||
HashMapFree(node->children);
|
|
||||||
|
|
||||||
regfree(&node->regex);
|
|
||||||
|
|
||||||
Free(node);
|
|
||||||
}
|
|
||||||
|
|
||||||
HttpRouter *
|
|
||||||
HttpRouterCreate(void)
|
|
||||||
{
|
|
||||||
HttpRouter *router = Malloc(sizeof(HttpRouter));
|
|
||||||
|
|
||||||
if (!router)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
router->root = RouteNodeCreate("/", NULL);
|
|
||||||
|
|
||||||
return router;
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
HttpRouterFree(HttpRouter * router)
|
|
||||||
{
|
|
||||||
if (!router)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
RouteNodeFree(router->root);
|
|
||||||
Free(router);
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
|
||||||
HttpRouterAdd(HttpRouter * router, char *regPath, HttpRouteFunc * exec)
|
|
||||||
{
|
|
||||||
RouteNode *node;
|
|
||||||
char *pathPart;
|
|
||||||
char *tmp;
|
|
||||||
|
|
||||||
if (!router || !regPath || !exec)
|
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (StrEquals(regPath, "/"))
|
|
||||||
{
|
|
||||||
router->root->exec = exec;
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
regPath = StrDuplicate(regPath);
|
|
||||||
if (!regPath)
|
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
tmp = regPath;
|
|
||||||
node = router->root;
|
|
||||||
|
|
||||||
while ((pathPart = strtok_r(tmp, "/", &tmp)))
|
|
||||||
{
|
|
||||||
RouteNode *tNode = HashMapGet(node->children, pathPart);
|
|
||||||
|
|
||||||
if (!tNode)
|
|
||||||
{
|
|
||||||
tNode = RouteNodeCreate(pathPart, NULL);
|
|
||||||
RouteNodeFree(HashMapSet(node->children, pathPart, tNode));
|
|
||||||
}
|
|
||||||
|
|
||||||
node = tNode;
|
|
||||||
}
|
|
||||||
|
|
||||||
node->exec = exec;
|
|
||||||
|
|
||||||
Free(regPath);
|
|
||||||
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
|
||||||
HttpRouterRoute(HttpRouter * router, char *path, void *args, void **ret)
|
|
||||||
{
|
|
||||||
RouteNode *node;
|
|
||||||
char *pathPart;
|
|
||||||
char *tmp;
|
|
||||||
HttpRouteFunc *exec = NULL;
|
|
||||||
Array *matches = NULL;
|
|
||||||
size_t i;
|
|
||||||
int retval;
|
|
||||||
|
|
||||||
if (!router || !path)
|
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
matches = ArrayCreate();
|
|
||||||
if (!matches)
|
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
node = router->root;
|
|
||||||
|
|
||||||
if (StrEquals(path, "/"))
|
|
||||||
{
|
|
||||||
exec = node->exec;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
path = StrDuplicate(path);
|
|
||||||
tmp = path;
|
|
||||||
while ((pathPart = strtok_r(tmp, "/", &tmp)))
|
|
||||||
{
|
|
||||||
char *key;
|
|
||||||
RouteNode *val = NULL;
|
|
||||||
|
|
||||||
regmatch_t pmatch[REG_MAX_SUB];
|
|
||||||
|
|
||||||
i = 0;
|
|
||||||
|
|
||||||
while (HashMapIterateReentrant(node->children, &key, (void **) &val, &i))
|
|
||||||
{
|
|
||||||
if (regexec(&val->regex, pathPart, REG_MAX_SUB, pmatch, 0) == 0)
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
val = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!val)
|
|
||||||
{
|
|
||||||
exec = NULL;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
node = val;
|
|
||||||
exec = node->exec;
|
|
||||||
|
|
||||||
/* If we want to pass an arg, the match must be in parens */
|
|
||||||
if (val->regex.re_nsub)
|
|
||||||
{
|
|
||||||
/* pmatch[0] is the whole string, not the first
|
|
||||||
* subexpression */
|
|
||||||
char * substr;
|
|
||||||
regmatch_t cpmatch;
|
|
||||||
for (i = 1; i < REG_MAX_SUB; i++)
|
|
||||||
{
|
|
||||||
cpmatch = pmatch[i];
|
|
||||||
substr = StrSubstr(pathPart, cpmatch.rm_so, cpmatch.rm_eo);
|
|
||||||
if (pmatch[i].rm_so == -1)
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
ArrayAdd(matches, substr);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Free(path);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!exec)
|
|
||||||
{
|
|
||||||
retval = 0;
|
|
||||||
goto finish;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ret)
|
|
||||||
{
|
|
||||||
*ret = exec(matches, args);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
exec(matches, args);
|
|
||||||
}
|
|
||||||
|
|
||||||
retval = 1;
|
|
||||||
|
|
||||||
finish:
|
|
||||||
for (i = 0; i < ArraySize(matches); i++)
|
|
||||||
{
|
|
||||||
Free(ArrayGet(matches, i));
|
|
||||||
}
|
|
||||||
ArrayFree(matches);
|
|
||||||
|
|
||||||
return retval;
|
|
||||||
}
|
|
|
@ -1,399 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright (C) 2022-2023 Jordan Bancino <@jordan:bancino.net>
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person
|
|
||||||
* obtaining a copy of this software and associated documentation files
|
|
||||||
* (the "Software"), to deal in the Software without restriction,
|
|
||||||
* including without limitation the rights to use, copy, modify, merge,
|
|
||||||
* publish, distribute, sublicense, and/or sell copies of the Software,
|
|
||||||
* and to permit persons to whom the Software is furnished to do so,
|
|
||||||
* subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be
|
|
||||||
* included in all copies or portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
||||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
||||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
||||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
||||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
||||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
||||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
||||||
* SOFTWARE.
|
|
||||||
*/
|
|
||||||
#include <Int64.h>
|
|
||||||
|
|
||||||
#include <stddef.h>
|
|
||||||
#include <signal.h>
|
|
||||||
|
|
||||||
#include <Log.h>
|
|
||||||
|
|
||||||
#ifdef INT64_NATIVE
|
|
||||||
#define Int64Sign(x) ((int) (((UInt64) (x)) >> 63))
|
|
||||||
#else
|
|
||||||
#define Int64Sign(x) ((int) ((x).i[1] >> 31))
|
|
||||||
#endif
|
|
||||||
|
|
||||||
size_t
|
|
||||||
Int64Str(Int64 x, int base, char *out, size_t len)
|
|
||||||
{
|
|
||||||
static const char symbols[] = "0123456789ABCDEF";
|
|
||||||
|
|
||||||
size_t i = len - 1;
|
|
||||||
size_t j = 0;
|
|
||||||
|
|
||||||
int neg = Int64Sign(x);
|
|
||||||
|
|
||||||
Int64 base64 = Int64Create(0, base);
|
|
||||||
|
|
||||||
/* We only have symbols up to base 16 */
|
|
||||||
if (base < 2 || base > 16)
|
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* This algorithm doesn't work on INT64_MIN.
|
|
||||||
*
|
|
||||||
* But it works on all other integers in the range, so we
|
|
||||||
* just scoot the range in by one for now. It's a hack and
|
|
||||||
* I'm not a huge fan of it, but this function is mostly
|
|
||||||
* used in Json, which shouldn't have a range this large
|
|
||||||
* anyway (Json is limited to -2^53 -> 2^53-1).
|
|
||||||
*
|
|
||||||
* Proper fixes are always welcome.
|
|
||||||
*/
|
|
||||||
if (Int64Eq(x, Int64Create(0x80000000, 0x00000000)))
|
|
||||||
{
|
|
||||||
x = Int64Add(x, Int64Create(0, 1));
|
|
||||||
}
|
|
||||||
#if 0
|
|
||||||
else if (Int64Eq(x, Int64Create(0x7FFFFFFF, 0xFFFFFFFF)))
|
|
||||||
{
|
|
||||||
x = Int64Sub(x, Int64Create(0, 1));
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
if (base != 2 && base != 8 && base != 16 && neg)
|
|
||||||
{
|
|
||||||
x = Int64Neg(x);
|
|
||||||
}
|
|
||||||
|
|
||||||
do
|
|
||||||
{
|
|
||||||
Int64 mod = Int64Rem(x, base64);
|
|
||||||
Int32 low = Int64Low(mod);
|
|
||||||
|
|
||||||
out[i] = symbols[low];
|
|
||||||
i--;
|
|
||||||
x = Int64Div(x, base64);
|
|
||||||
} while (Int64Gt(x, Int64Create(0, 0)));
|
|
||||||
|
|
||||||
if (base != 2 && base != 8 && base != 16)
|
|
||||||
{
|
|
||||||
/*
|
|
||||||
* Binary, octal, and hexadecimal are known to
|
|
||||||
* be bit representations. Everything else (notably
|
|
||||||
* decimal) should include the negative sign.
|
|
||||||
*/
|
|
||||||
if (neg)
|
|
||||||
{
|
|
||||||
out[i] = '-';
|
|
||||||
i--;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
while (++i < len)
|
|
||||||
{
|
|
||||||
out[j++] = out[i];
|
|
||||||
}
|
|
||||||
|
|
||||||
out[j] = '\0';
|
|
||||||
|
|
||||||
return j;
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifndef INT64_NATIVE
|
|
||||||
|
|
||||||
/* No native 64-bit support, add our own */
|
|
||||||
|
|
||||||
Int64
|
|
||||||
Int64Create(UInt32 high, UInt32 low)
|
|
||||||
{
|
|
||||||
Int64 x;
|
|
||||||
|
|
||||||
x.i[0] = low;
|
|
||||||
x.i[1] = high;
|
|
||||||
|
|
||||||
return x;
|
|
||||||
}
|
|
||||||
|
|
||||||
Int64
|
|
||||||
Int64Add(Int64 x, Int64 y)
|
|
||||||
{
|
|
||||||
Int64 z = Int64Create(0, 0);
|
|
||||||
int carry;
|
|
||||||
|
|
||||||
z.i[0] = x.i[0] + y.i[0];
|
|
||||||
carry = z.i[0] < x.i[0];
|
|
||||||
z.i[1] = x.i[1] + y.i[1] + carry;
|
|
||||||
|
|
||||||
return z;
|
|
||||||
}
|
|
||||||
|
|
||||||
Int64
|
|
||||||
Int64Sub(Int64 x, Int64 y)
|
|
||||||
{
|
|
||||||
return Int64Add(x, Int64Neg(y));
|
|
||||||
}
|
|
||||||
|
|
||||||
Int64
|
|
||||||
Int64Mul(Int64 x, Int64 y)
|
|
||||||
{
|
|
||||||
Int64 z = Int64Create(0, 0);
|
|
||||||
|
|
||||||
int xneg = Int64Sign(x);
|
|
||||||
int yneg = Int64Sign(y);
|
|
||||||
|
|
||||||
if (xneg)
|
|
||||||
{
|
|
||||||
x = Int64Neg(x);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (yneg)
|
|
||||||
{
|
|
||||||
y = Int64Neg(y);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* while (y > 0) */
|
|
||||||
while (Int64Gt(y, Int64Create(0, 0)))
|
|
||||||
{
|
|
||||||
/* if (y & 1 != 0) */
|
|
||||||
if (Int64Neq(Int64And(y, Int64Create(0, 1)), Int64Create(0, 0)))
|
|
||||||
{
|
|
||||||
z = Int64Add(z, x);
|
|
||||||
}
|
|
||||||
|
|
||||||
x = Int64Sll(x, 1);
|
|
||||||
y = Int64Sra(y, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (xneg != yneg)
|
|
||||||
{
|
|
||||||
z = Int64Neg(z);
|
|
||||||
}
|
|
||||||
|
|
||||||
return z;
|
|
||||||
}
|
|
||||||
|
|
||||||
typedef struct
|
|
||||||
{
|
|
||||||
Int64 q;
|
|
||||||
Int64 r;
|
|
||||||
} Int64Ldiv;
|
|
||||||
|
|
||||||
static Int64Ldiv
|
|
||||||
Int64LongDivision(Int64 n, Int64 d)
|
|
||||||
{
|
|
||||||
Int64Ldiv o;
|
|
||||||
|
|
||||||
int i;
|
|
||||||
|
|
||||||
int nneg = Int64Sign(n);
|
|
||||||
int dneg = Int64Sign(d);
|
|
||||||
|
|
||||||
o.q = Int64Create(0, 0);
|
|
||||||
o.r = Int64Create(0, 0);
|
|
||||||
|
|
||||||
if (Int64Eq(d, Int64Create(0, 0)))
|
|
||||||
{
|
|
||||||
raise(SIGFPE);
|
|
||||||
return o;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (nneg)
|
|
||||||
{
|
|
||||||
n = Int64Neg(n);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (dneg)
|
|
||||||
{
|
|
||||||
d = Int64Neg(d);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (i = 63; i >= 0; i--)
|
|
||||||
{
|
|
||||||
Int64 bit = Int64And(Int64Sra(n, i), Int64Create(0, 1));
|
|
||||||
|
|
||||||
o.r = Int64Sll(o.r, 1);
|
|
||||||
o.r = Int64Or(o.r, bit);
|
|
||||||
|
|
||||||
if (Int64Geq(o.r, d))
|
|
||||||
{
|
|
||||||
o.r = Int64Sub(o.r, d);
|
|
||||||
o.q = Int64Or(o.q, Int64Sll(Int64Create(0, 1), i));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (nneg != dneg)
|
|
||||||
{
|
|
||||||
o.r = Int64Neg(o.r);
|
|
||||||
o.q = Int64Neg(o.q);
|
|
||||||
}
|
|
||||||
|
|
||||||
return o;
|
|
||||||
}
|
|
||||||
|
|
||||||
Int64
|
|
||||||
Int64Div(Int64 x, Int64 y)
|
|
||||||
{
|
|
||||||
return Int64LongDivision(x, y).q;
|
|
||||||
}
|
|
||||||
|
|
||||||
Int64
|
|
||||||
Int64Rem(Int64 x, Int64 y)
|
|
||||||
{
|
|
||||||
return Int64LongDivision(x, y).r;
|
|
||||||
}
|
|
||||||
|
|
||||||
Int64
|
|
||||||
Int64Sll(Int64 x, int y)
|
|
||||||
{
|
|
||||||
Int64 z;
|
|
||||||
|
|
||||||
if (!y)
|
|
||||||
{
|
|
||||||
return x;
|
|
||||||
}
|
|
||||||
|
|
||||||
z = Int64Create(0, 0);
|
|
||||||
|
|
||||||
if (y < 32)
|
|
||||||
{
|
|
||||||
z.i[1] = (x.i[0] >> (32 - y)) | (x.i[1] << y);
|
|
||||||
z.i[0] = x.i[0] << y;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
z.i[1] = x.i[0] << (y - 32);
|
|
||||||
}
|
|
||||||
|
|
||||||
return z;
|
|
||||||
}
|
|
||||||
|
|
||||||
Int64
|
|
||||||
Int64Sra(Int64 x, int y)
|
|
||||||
{
|
|
||||||
Int64 z;
|
|
||||||
|
|
||||||
int neg = Int64Sign(x);
|
|
||||||
|
|
||||||
if (!y)
|
|
||||||
{
|
|
||||||
return x;
|
|
||||||
}
|
|
||||||
|
|
||||||
z = Int64Create(0, 0);
|
|
||||||
|
|
||||||
if (y < 32)
|
|
||||||
{
|
|
||||||
z.i[0] = (x.i[1] << (32 - y)) | (x.i[0] >> y);
|
|
||||||
z.i[1] = x.i[1] >> y;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
z.i[0] = x.i[1] >> (y - 32);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (neg)
|
|
||||||
{
|
|
||||||
Int64 mask = Int64Create(0xFFFFFFFF, 0xFFFFFFFF);
|
|
||||||
|
|
||||||
z = Int64Or(Int64Sll(mask, (64 - y)), z);
|
|
||||||
}
|
|
||||||
|
|
||||||
return z;
|
|
||||||
}
|
|
||||||
|
|
||||||
Int64
|
|
||||||
Int64And(Int64 x, Int64 y)
|
|
||||||
{
|
|
||||||
return Int64Create(x.i[1] & y.i[1], x.i[0] & y.i[0]);
|
|
||||||
}
|
|
||||||
|
|
||||||
Int64
|
|
||||||
Int64Or(Int64 x, Int64 y)
|
|
||||||
{
|
|
||||||
return Int64Create(x.i[1] | y.i[1], x.i[0] | y.i[0]);
|
|
||||||
}
|
|
||||||
|
|
||||||
Int64
|
|
||||||
Int64Xor(Int64 x, Int64 y)
|
|
||||||
{
|
|
||||||
return Int64Create(x.i[1] ^ y.i[1], x.i[0] ^ y.i[0]);
|
|
||||||
}
|
|
||||||
|
|
||||||
Int64
|
|
||||||
Int64Not(Int64 x)
|
|
||||||
{
|
|
||||||
return Int64Create(~(x.i[1]), ~(x.i[0]));
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
|
||||||
Int64Eq(Int64 x, Int64 y)
|
|
||||||
{
|
|
||||||
return x.i[0] == y.i[0] && x.i[1] == y.i[1];
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
|
||||||
Int64Lt(Int64 x, Int64 y)
|
|
||||||
{
|
|
||||||
int xneg = Int64Sign(x);
|
|
||||||
int yneg = Int64Sign(y);
|
|
||||||
|
|
||||||
if (xneg != yneg)
|
|
||||||
{
|
|
||||||
return xneg > yneg;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if (xneg)
|
|
||||||
{
|
|
||||||
/* Both negative */
|
|
||||||
return x.i[1] > y.i[1] || (x.i[1] == y.i[1] && x.i[0] > y.i[0]);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
/* Both positive */
|
|
||||||
return x.i[1] < y.i[1] || (x.i[1] == y.i[1] && x.i[0] < y.i[0]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
|
||||||
Int64Gt(Int64 x, Int64 y)
|
|
||||||
{
|
|
||||||
int xneg = Int64Sign(x);
|
|
||||||
int yneg = Int64Sign(y);
|
|
||||||
|
|
||||||
if (xneg != yneg)
|
|
||||||
{
|
|
||||||
return xneg < yneg;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if (xneg)
|
|
||||||
{
|
|
||||||
/* Both negative */
|
|
||||||
return x.i[1] < y.i[1] || (x.i[1] == y.i[1] && x.i[0] < y.i[0]);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
/* Both positive */
|
|
||||||
return x.i[1] > y.i[1] || (x.i[1] == y.i[1] && x.i[0] > y.i[0]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
|
|
@ -1,212 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright (C) 2022-2023 Jordan Bancino <@jordan:bancino.net>
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person
|
|
||||||
* obtaining a copy of this software and associated documentation files
|
|
||||||
* (the "Software"), to deal in the Software without restriction,
|
|
||||||
* including without limitation the rights to use, copy, modify, merge,
|
|
||||||
* publish, distribute, sublicense, and/or sell copies of the Software,
|
|
||||||
* and to permit persons to whom the Software is furnished to do so,
|
|
||||||
* subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be
|
|
||||||
* included in all copies or portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
||||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
||||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
||||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
||||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
||||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
||||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
||||||
* SOFTWARE.
|
|
||||||
*/
|
|
||||||
#include <Io.h>
|
|
||||||
|
|
||||||
#include <Memory.h>
|
|
||||||
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <errno.h>
|
|
||||||
#include <fcntl.h>
|
|
||||||
|
|
||||||
struct Io
|
|
||||||
{
|
|
||||||
IoFunctions io;
|
|
||||||
void *cookie;
|
|
||||||
};
|
|
||||||
|
|
||||||
Io *
|
|
||||||
IoCreate(void *cookie, IoFunctions funcs)
|
|
||||||
{
|
|
||||||
Io *io;
|
|
||||||
|
|
||||||
/* Must have at least read or write */
|
|
||||||
if (!funcs.read && !funcs.write)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
io = Malloc(sizeof(Io));
|
|
||||||
|
|
||||||
if (!io)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
io->cookie = cookie;
|
|
||||||
|
|
||||||
io->io.read = funcs.read;
|
|
||||||
io->io.write = funcs.write;
|
|
||||||
io->io.seek = funcs.seek;
|
|
||||||
io->io.close = funcs.close;
|
|
||||||
|
|
||||||
return io;
|
|
||||||
}
|
|
||||||
|
|
||||||
ssize_t
|
|
||||||
IoRead(Io * io, void *buf, size_t nBytes)
|
|
||||||
{
|
|
||||||
if (!io || !io->io.read)
|
|
||||||
{
|
|
||||||
errno = EBADF;
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
return io->io.read(io->cookie, buf, nBytes);
|
|
||||||
}
|
|
||||||
|
|
||||||
ssize_t
|
|
||||||
IoWrite(Io * io, void *buf, size_t nBytes)
|
|
||||||
{
|
|
||||||
if (!io || !io->io.write)
|
|
||||||
{
|
|
||||||
errno = EBADF;
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
return io->io.write(io->cookie, buf, nBytes);
|
|
||||||
}
|
|
||||||
|
|
||||||
off_t
|
|
||||||
IoSeek(Io * io, off_t offset, int whence)
|
|
||||||
{
|
|
||||||
if (!io)
|
|
||||||
{
|
|
||||||
errno = EBADF;
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!io->io.seek)
|
|
||||||
{
|
|
||||||
errno = EINVAL;
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
return io->io.seek(io->cookie, offset, whence);
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
|
||||||
IoClose(Io * io)
|
|
||||||
{
|
|
||||||
int ret;
|
|
||||||
|
|
||||||
if (!io)
|
|
||||||
{
|
|
||||||
errno = EBADF;
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (io->io.close)
|
|
||||||
{
|
|
||||||
ret = io->io.close(io->cookie);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
ret = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
Free(io);
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
|
||||||
IoVprintf(Io * io, const char *fmt, va_list ap)
|
|
||||||
{
|
|
||||||
char *buf = NULL;
|
|
||||||
size_t bufSize = 0;
|
|
||||||
FILE *fp;
|
|
||||||
|
|
||||||
int ret;
|
|
||||||
|
|
||||||
if (!io || !fmt)
|
|
||||||
{
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
fp = open_memstream(&buf, &bufSize);
|
|
||||||
if (!fp)
|
|
||||||
{
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = vfprintf(fp, fmt, ap);
|
|
||||||
fclose(fp);
|
|
||||||
|
|
||||||
if (ret >= 0)
|
|
||||||
{
|
|
||||||
ret = IoWrite(io, buf, bufSize);
|
|
||||||
}
|
|
||||||
|
|
||||||
free(buf); /* Allocated by stdlib, not Memory
|
|
||||||
* API */
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
|
||||||
IoPrintf(Io * io, const char *fmt,...)
|
|
||||||
{
|
|
||||||
va_list ap;
|
|
||||||
int ret;
|
|
||||||
|
|
||||||
va_start(ap, fmt);
|
|
||||||
ret = IoVprintf(io, fmt, ap);
|
|
||||||
va_end(ap);
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
ssize_t
|
|
||||||
IoCopy(Io * in, Io * out)
|
|
||||||
{
|
|
||||||
ssize_t nBytes = 0;
|
|
||||||
char buf[IO_BUFFER];
|
|
||||||
ssize_t rRes;
|
|
||||||
ssize_t wRes;
|
|
||||||
|
|
||||||
if (!in || !out)
|
|
||||||
{
|
|
||||||
errno = EBADF;
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
while ((rRes = IoRead(in, &buf, IO_BUFFER)) != 0)
|
|
||||||
{
|
|
||||||
if (rRes == -1)
|
|
||||||
{
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
wRes = IoWrite(out, &buf, rRes);
|
|
||||||
|
|
||||||
if (wRes == -1)
|
|
||||||
{
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
nBytes += wRes;
|
|
||||||
}
|
|
||||||
|
|
||||||
return nBytes;
|
|
||||||
}
|
|
|
@ -1,95 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright (C) 2022-2023 Jordan Bancino <@jordan:bancino.net>
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person
|
|
||||||
* obtaining a copy of this software and associated documentation files
|
|
||||||
* (the "Software"), to deal in the Software without restriction,
|
|
||||||
* including without limitation the rights to use, copy, modify, merge,
|
|
||||||
* publish, distribute, sublicense, and/or sell copies of the Software,
|
|
||||||
* and to permit persons to whom the Software is furnished to do so,
|
|
||||||
* subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be
|
|
||||||
* included in all copies or portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
||||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
||||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
||||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
||||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
||||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
||||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
||||||
* SOFTWARE.
|
|
||||||
*/
|
|
||||||
#include <Io.h>
|
|
||||||
|
|
||||||
#include <Memory.h>
|
|
||||||
|
|
||||||
#include <fcntl.h>
|
|
||||||
|
|
||||||
static ssize_t
|
|
||||||
IoReadFd(void *cookie, void *buf, size_t nBytes)
|
|
||||||
{
|
|
||||||
int fd = *((int *) cookie);
|
|
||||||
|
|
||||||
return read(fd, buf, nBytes);
|
|
||||||
}
|
|
||||||
|
|
||||||
static ssize_t
|
|
||||||
IoWriteFd(void *cookie, void *buf, size_t nBytes)
|
|
||||||
{
|
|
||||||
int fd = *((int *) cookie);
|
|
||||||
|
|
||||||
return write(fd, buf, nBytes);
|
|
||||||
}
|
|
||||||
|
|
||||||
static off_t
|
|
||||||
IoSeekFd(void *cookie, off_t offset, int whence)
|
|
||||||
{
|
|
||||||
int fd = *((int *) cookie);
|
|
||||||
|
|
||||||
return lseek(fd, offset, whence);
|
|
||||||
}
|
|
||||||
|
|
||||||
static int
|
|
||||||
IoCloseFd(void *cookie)
|
|
||||||
{
|
|
||||||
int fd = *((int *) cookie);
|
|
||||||
|
|
||||||
Free(cookie);
|
|
||||||
return close(fd);
|
|
||||||
}
|
|
||||||
|
|
||||||
Io *
|
|
||||||
IoFd(int fd)
|
|
||||||
{
|
|
||||||
int *cookie = Malloc(sizeof(int));
|
|
||||||
IoFunctions f;
|
|
||||||
|
|
||||||
if (!cookie)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
*cookie = fd;
|
|
||||||
|
|
||||||
f.read = IoReadFd;
|
|
||||||
f.write = IoWriteFd;
|
|
||||||
f.seek = IoSeekFd;
|
|
||||||
f.close = IoCloseFd;
|
|
||||||
|
|
||||||
return IoCreate(cookie, f);
|
|
||||||
}
|
|
||||||
|
|
||||||
Io *
|
|
||||||
IoOpen(const char *path, int flags, mode_t mode)
|
|
||||||
{
|
|
||||||
int fd = open(path, flags, mode);
|
|
||||||
|
|
||||||
if (fd == -1)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
return IoFd(fd);
|
|
||||||
}
|
|
|
@ -1,91 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright (C) 2022-2023 Jordan Bancino <@jordan:bancino.net>
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person
|
|
||||||
* obtaining a copy of this software and associated documentation files
|
|
||||||
* (the "Software"), to deal in the Software without restriction,
|
|
||||||
* including without limitation the rights to use, copy, modify, merge,
|
|
||||||
* publish, distribute, sublicense, and/or sell copies of the Software,
|
|
||||||
* and to permit persons to whom the Software is furnished to do so,
|
|
||||||
* subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be
|
|
||||||
* included in all copies or portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
||||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
||||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
||||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
||||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
||||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
||||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
||||||
* SOFTWARE.
|
|
||||||
*/
|
|
||||||
#include <Io.h>
|
|
||||||
|
|
||||||
#include <stdio.h>
|
|
||||||
|
|
||||||
static ssize_t
|
|
||||||
IoReadFile(void *cookie, void *buf, size_t nBytes)
|
|
||||||
{
|
|
||||||
FILE *fp = cookie;
|
|
||||||
|
|
||||||
return fread(buf, 1, nBytes, fp);
|
|
||||||
}
|
|
||||||
|
|
||||||
static ssize_t
|
|
||||||
IoWriteFile(void *cookie, void *buf, size_t nBytes)
|
|
||||||
{
|
|
||||||
FILE *fp = cookie;
|
|
||||||
size_t res = fwrite(buf, 1, nBytes, fp);
|
|
||||||
|
|
||||||
/*
|
|
||||||
* fwrite() may be buffered on some platforms, but at this low level,
|
|
||||||
* it should not be; buffering happens in Stream, not Io.
|
|
||||||
*/
|
|
||||||
fflush(fp);
|
|
||||||
|
|
||||||
return res;
|
|
||||||
}
|
|
||||||
|
|
||||||
static off_t
|
|
||||||
IoSeekFile(void *cookie, off_t offset, int whence)
|
|
||||||
{
|
|
||||||
FILE *fp = cookie;
|
|
||||||
off_t ret = fseeko(fp, offset, whence);
|
|
||||||
|
|
||||||
if (ret > -1)
|
|
||||||
{
|
|
||||||
return ftello(fp);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static int
|
|
||||||
IoCloseFile(void *cookie)
|
|
||||||
{
|
|
||||||
FILE *fp = cookie;
|
|
||||||
|
|
||||||
return fclose(fp);
|
|
||||||
}
|
|
||||||
|
|
||||||
Io *
|
|
||||||
IoFile(FILE * fp)
|
|
||||||
{
|
|
||||||
IoFunctions f;
|
|
||||||
|
|
||||||
if (!fp)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
f.read = IoReadFile;
|
|
||||||
f.write = IoWriteFile;
|
|
||||||
f.seek = IoSeekFile;
|
|
||||||
f.close = IoCloseFile;
|
|
||||||
|
|
||||||
return IoCreate(fp, f);
|
|
||||||
}
|
|
|
@ -1,393 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright (C) 2022-2023 Jordan Bancino <@jordan:bancino.net>
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person
|
|
||||||
* obtaining a copy of this software and associated documentation files
|
|
||||||
* (the "Software"), to deal in the Software without restriction,
|
|
||||||
* including without limitation the rights to use, copy, modify, merge,
|
|
||||||
* publish, distribute, sublicense, and/or sell copies of the Software,
|
|
||||||
* and to permit persons to whom the Software is furnished to do so,
|
|
||||||
* subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be
|
|
||||||
* included in all copies or portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
||||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
||||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
||||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
||||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
||||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
||||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
||||||
* SOFTWARE.
|
|
||||||
*/
|
|
||||||
#include <Log.h>
|
|
||||||
|
|
||||||
#include <Memory.h>
|
|
||||||
#include <Util.h>
|
|
||||||
|
|
||||||
#include <string.h>
|
|
||||||
#include <time.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
#include <ctype.h>
|
|
||||||
#include <stdarg.h>
|
|
||||||
#include <pthread.h>
|
|
||||||
|
|
||||||
#define LOG_TSBUFFER 64
|
|
||||||
|
|
||||||
struct LogConfig
|
|
||||||
{
|
|
||||||
int level;
|
|
||||||
size_t indent;
|
|
||||||
Stream *out;
|
|
||||||
int flags;
|
|
||||||
char *tsFmt;
|
|
||||||
|
|
||||||
pthread_mutex_t lock;
|
|
||||||
};
|
|
||||||
|
|
||||||
LogConfig *globalConfig = NULL;
|
|
||||||
|
|
||||||
LogConfig *
|
|
||||||
LogConfigCreate(void)
|
|
||||||
{
|
|
||||||
LogConfig *config;
|
|
||||||
|
|
||||||
config = Malloc(sizeof(LogConfig));
|
|
||||||
|
|
||||||
if (!config)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
memset(config, 0, sizeof(LogConfig));
|
|
||||||
pthread_mutex_init(&config->lock, NULL);
|
|
||||||
|
|
||||||
LogConfigLevelSet(config, LOG_INFO);
|
|
||||||
LogConfigIndentSet(config, 0);
|
|
||||||
LogConfigOutputSet(config, NULL); /* Will set to stdout */
|
|
||||||
LogConfigFlagSet(config, LOG_FLAG_COLOR);
|
|
||||||
LogConfigTimeStampFormatSet(config, "%y-%m-%d %H:%M:%S");
|
|
||||||
|
|
||||||
return config;
|
|
||||||
}
|
|
||||||
|
|
||||||
LogConfig *
|
|
||||||
LogConfigGlobal(void)
|
|
||||||
{
|
|
||||||
if (!globalConfig)
|
|
||||||
{
|
|
||||||
globalConfig = LogConfigCreate();
|
|
||||||
}
|
|
||||||
|
|
||||||
return globalConfig;
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
LogConfigFlagClear(LogConfig * config, int flags)
|
|
||||||
{
|
|
||||||
if (!config)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
config->flags &= ~flags;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int
|
|
||||||
LogConfigFlagGet(LogConfig * config, int flags)
|
|
||||||
{
|
|
||||||
if (!config)
|
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
return config->flags & flags;
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
LogConfigFlagSet(LogConfig * config, int flags)
|
|
||||||
{
|
|
||||||
if (!config)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
config->flags |= flags;
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
LogConfigFree(LogConfig * config)
|
|
||||||
{
|
|
||||||
if (!config)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
pthread_mutex_destroy(&config->lock);
|
|
||||||
Free(config);
|
|
||||||
|
|
||||||
if (config == globalConfig)
|
|
||||||
{
|
|
||||||
globalConfig = NULL;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
LogConfigIndent(LogConfig * config)
|
|
||||||
{
|
|
||||||
if (config)
|
|
||||||
{
|
|
||||||
config->indent += 2;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
LogConfigIndentSet(LogConfig * config, size_t indent)
|
|
||||||
{
|
|
||||||
if (!config)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
config->indent = indent;
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
|
||||||
LogConfigLevelGet(LogConfig * config)
|
|
||||||
{
|
|
||||||
if (!config)
|
|
||||||
{
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
return config->level;
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
LogConfigLevelSet(LogConfig * config, int level)
|
|
||||||
{
|
|
||||||
if (!config)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
switch (level)
|
|
||||||
{
|
|
||||||
case LOG_ERR:
|
|
||||||
case LOG_WARNING:
|
|
||||||
case LOG_INFO:
|
|
||||||
case LOG_DEBUG:
|
|
||||||
config->level = level;
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
LogConfigOutputSet(LogConfig * config, Stream * out)
|
|
||||||
{
|
|
||||||
if (!config)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (out)
|
|
||||||
{
|
|
||||||
config->out = out;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
config->out = StreamStdout();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
LogConfigTimeStampFormatSet(LogConfig * config, char *tsFmt)
|
|
||||||
{
|
|
||||||
if (config)
|
|
||||||
{
|
|
||||||
config->tsFmt = tsFmt;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
LogConfigUnindent(LogConfig * config)
|
|
||||||
{
|
|
||||||
if (config && config->indent >= 2)
|
|
||||||
{
|
|
||||||
config->indent -= 2;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
Logv(LogConfig * config, int level, const char *msg, va_list argp)
|
|
||||||
{
|
|
||||||
size_t i;
|
|
||||||
int doColor;
|
|
||||||
char indicator;
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Only proceed if we have a config and its log level is set to a
|
|
||||||
* value that permits us to log. This is as close as we can get
|
|
||||||
* to a no-op function if we aren't logging anything, without doing
|
|
||||||
* some crazy macro magic.
|
|
||||||
*/
|
|
||||||
if (!config || level > config->level)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Misconfiguration */
|
|
||||||
if (!config->out)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
pthread_mutex_lock(&config->lock);
|
|
||||||
|
|
||||||
if (LogConfigFlagGet(config, LOG_FLAG_SYSLOG))
|
|
||||||
{
|
|
||||||
/* No further print logic is needed; syslog will handle it all
|
|
||||||
* for us. */
|
|
||||||
vsyslog(level, msg, argp);
|
|
||||||
pthread_mutex_unlock(&config->lock);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
doColor = LogConfigFlagGet(config, LOG_FLAG_COLOR)
|
|
||||||
&& isatty(StreamFileno(config->out));
|
|
||||||
|
|
||||||
if (doColor)
|
|
||||||
{
|
|
||||||
char *ansi;
|
|
||||||
|
|
||||||
switch (level)
|
|
||||||
{
|
|
||||||
case LOG_EMERG:
|
|
||||||
case LOG_ALERT:
|
|
||||||
case LOG_CRIT:
|
|
||||||
case LOG_ERR:
|
|
||||||
/* Bold Red */
|
|
||||||
ansi = "\033[1;31m";
|
|
||||||
break;
|
|
||||||
case LOG_WARNING:
|
|
||||||
/* Bold Yellow */
|
|
||||||
ansi = "\033[1;33m";
|
|
||||||
break;
|
|
||||||
case LOG_NOTICE:
|
|
||||||
/* Bold Magenta */
|
|
||||||
ansi = "\033[1;35m";
|
|
||||||
break;
|
|
||||||
case LOG_INFO:
|
|
||||||
/* Bold Green */
|
|
||||||
ansi = "\033[1;32m";
|
|
||||||
break;
|
|
||||||
case LOG_DEBUG:
|
|
||||||
/* Bold Blue */
|
|
||||||
ansi = "\033[1;34m";
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
ansi = "";
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
StreamPuts(config->out, ansi);
|
|
||||||
}
|
|
||||||
|
|
||||||
StreamPutc(config->out, '[');
|
|
||||||
|
|
||||||
if (config->tsFmt)
|
|
||||||
{
|
|
||||||
time_t timer = time(NULL);
|
|
||||||
struct tm *timeInfo = localtime(&timer);
|
|
||||||
char tsBuffer[LOG_TSBUFFER];
|
|
||||||
|
|
||||||
int tsLength = strftime(tsBuffer, LOG_TSBUFFER, config->tsFmt,
|
|
||||||
timeInfo);
|
|
||||||
|
|
||||||
if (tsLength)
|
|
||||||
{
|
|
||||||
StreamPuts(config->out, tsBuffer);
|
|
||||||
if (!isspace((unsigned char) tsBuffer[tsLength - 1]))
|
|
||||||
{
|
|
||||||
StreamPutc(config->out, ' ');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
StreamPrintf(config->out, "(%lu) ", UtilThreadNo());
|
|
||||||
|
|
||||||
switch (level)
|
|
||||||
{
|
|
||||||
case LOG_EMERG:
|
|
||||||
indicator = '#';
|
|
||||||
break;
|
|
||||||
case LOG_ALERT:
|
|
||||||
indicator = '@';
|
|
||||||
break;
|
|
||||||
case LOG_CRIT:
|
|
||||||
indicator = 'X';
|
|
||||||
break;
|
|
||||||
case LOG_ERR:
|
|
||||||
indicator = 'x';
|
|
||||||
break;
|
|
||||||
case LOG_WARNING:
|
|
||||||
indicator = '!';
|
|
||||||
break;
|
|
||||||
case LOG_NOTICE:
|
|
||||||
indicator = '~';
|
|
||||||
break;
|
|
||||||
case LOG_INFO:
|
|
||||||
indicator = '>';
|
|
||||||
break;
|
|
||||||
case LOG_DEBUG:
|
|
||||||
indicator = '*';
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
indicator = '?';
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
StreamPrintf(config->out, "%c]", indicator);
|
|
||||||
|
|
||||||
if (doColor)
|
|
||||||
{
|
|
||||||
/* ANSI Reset */
|
|
||||||
StreamPuts(config->out, "\033[0m");
|
|
||||||
}
|
|
||||||
|
|
||||||
StreamPutc(config->out, ' ');
|
|
||||||
for (i = 0; i < config->indent; i++)
|
|
||||||
{
|
|
||||||
StreamPutc(config->out, ' ');
|
|
||||||
}
|
|
||||||
|
|
||||||
StreamVprintf(config->out, msg, argp);
|
|
||||||
StreamPutc(config->out, '\n');
|
|
||||||
|
|
||||||
StreamFlush(config->out);
|
|
||||||
|
|
||||||
pthread_mutex_unlock(&config->lock);
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
LogTo(LogConfig * config, int level, const char *fmt,...)
|
|
||||||
{
|
|
||||||
va_list argp;
|
|
||||||
|
|
||||||
va_start(argp, fmt);
|
|
||||||
Logv(config, level, fmt, argp);
|
|
||||||
va_end(argp);
|
|
||||||
}
|
|
||||||
|
|
||||||
extern void
|
|
||||||
Log(int level, const char *fmt,...)
|
|
||||||
{
|
|
||||||
va_list argp;
|
|
||||||
|
|
||||||
va_start(argp, fmt);
|
|
||||||
Logv(LogConfigGlobal(), level, fmt, argp);
|
|
||||||
va_end(argp);
|
|
||||||
}
|
|
|
@ -1,174 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright (C) 2022-2023 Jordan Bancino <@jordan:bancino.net>
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person
|
|
||||||
* obtaining a copy of this software and associated documentation files
|
|
||||||
* (the "Software"), to deal in the Software without restriction,
|
|
||||||
* including without limitation the rights to use, copy, modify, merge,
|
|
||||||
* publish, distribute, sublicense, and/or sell copies of the Software,
|
|
||||||
* and to permit persons to whom the Software is furnished to do so,
|
|
||||||
* subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be
|
|
||||||
* included in all copies or portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
||||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
||||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
||||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
||||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
||||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
||||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
||||||
* SOFTWARE.
|
|
||||||
*/
|
|
||||||
#include <Rand.h>
|
|
||||||
|
|
||||||
#include <Int.h>
|
|
||||||
#include <UInt64.h>
|
|
||||||
#include <Util.h>
|
|
||||||
#include <Memory.h>
|
|
||||||
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <pthread.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
|
|
||||||
#define RAND_STATE_VECTOR_LENGTH 624
|
|
||||||
#define RAND_STATE_VECTOR_M 397
|
|
||||||
|
|
||||||
#define RAND_UPPER_MASK 0x80000000
|
|
||||||
#define RAND_LOWER_MASK 0x7FFFFFFF
|
|
||||||
#define RAND_TEMPER_B 0x9D2C5680
|
|
||||||
#define RAND_TEMPER_C 0xEFC60000
|
|
||||||
|
|
||||||
typedef struct RandState
|
|
||||||
{
|
|
||||||
UInt32 mt[RAND_STATE_VECTOR_LENGTH];
|
|
||||||
int index;
|
|
||||||
} RandState;
|
|
||||||
|
|
||||||
static void
|
|
||||||
RandSeed(RandState * state, UInt32 seed)
|
|
||||||
{
|
|
||||||
state->mt[0] = seed & 0xFFFFFFFF;
|
|
||||||
|
|
||||||
for (state->index = 1; state->index < RAND_STATE_VECTOR_LENGTH; state->index++)
|
|
||||||
{
|
|
||||||
state->mt[state->index] = (6069 * state->mt[state->index - 1]) & 0xFFFFFFFF;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static UInt32
|
|
||||||
RandGenerate(RandState * state)
|
|
||||||
{
|
|
||||||
static const UInt32 mag[2] = {0x0, 0x9908B0DF};
|
|
||||||
|
|
||||||
UInt32 result;
|
|
||||||
|
|
||||||
if (state->index >= RAND_STATE_VECTOR_LENGTH || state->index < 0)
|
|
||||||
{
|
|
||||||
int kk;
|
|
||||||
|
|
||||||
if (state->index >= RAND_STATE_VECTOR_LENGTH + 1 || state->index < 0)
|
|
||||||
{
|
|
||||||
RandSeed(state, 4357);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (kk = 0; kk < RAND_STATE_VECTOR_LENGTH - RAND_STATE_VECTOR_M; kk++)
|
|
||||||
{
|
|
||||||
result = (state->mt[kk] & RAND_UPPER_MASK) | (state->mt[kk + 1] & RAND_LOWER_MASK);
|
|
||||||
state->mt[kk] = state->mt[kk + RAND_STATE_VECTOR_M] ^ (result >> 1) ^ mag[result & 0x1];
|
|
||||||
}
|
|
||||||
|
|
||||||
for (; kk < RAND_STATE_VECTOR_LENGTH - 1; kk++)
|
|
||||||
{
|
|
||||||
result = (state->mt[kk] & RAND_UPPER_MASK) | (state->mt[kk + 1] & RAND_LOWER_MASK);
|
|
||||||
state->mt[kk] = state->mt[kk + (RAND_STATE_VECTOR_M - RAND_STATE_VECTOR_LENGTH)] ^ (result >> 1) ^ mag[result & 0x1];
|
|
||||||
}
|
|
||||||
|
|
||||||
result = (state->mt[RAND_STATE_VECTOR_LENGTH - 1] & RAND_UPPER_MASK) | (state->mt[0] & RAND_LOWER_MASK);
|
|
||||||
state->mt[RAND_STATE_VECTOR_LENGTH - 1] = state->mt[RAND_STATE_VECTOR_M - 1] ^ (result >> 1) ^ mag[result & 0x1];
|
|
||||||
state->index = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
result = state->mt[state->index++];
|
|
||||||
result ^= (result >> 11);
|
|
||||||
result ^= (result << 7) & RAND_TEMPER_B;
|
|
||||||
result ^= (result << 15) & RAND_TEMPER_C;
|
|
||||||
result ^= (result >> 18);
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
RandDestructor(void *p)
|
|
||||||
{
|
|
||||||
Free(p);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Generate random numbers using rejection sampling. The basic idea is
|
|
||||||
* to "reroll" if a number happens to be outside the range. However
|
|
||||||
* this could be extremely inefficient.
|
|
||||||
*
|
|
||||||
* Another idea would just be to "reroll" if the generated number ends up
|
|
||||||
* in the previously "biased" range, and THEN do a modulo.
|
|
||||||
*
|
|
||||||
* This would be far more efficient for small values of max, and fixes the
|
|
||||||
* bias issue. */
|
|
||||||
|
|
||||||
/* This algorithm therefore computes N random numbers generally in O(N)
|
|
||||||
* time, while being less biased. */
|
|
||||||
void
|
|
||||||
RandIntN(int *buf, size_t size, unsigned int max)
|
|
||||||
{
|
|
||||||
static pthread_key_t stateKey;
|
|
||||||
static int createdKey = 0;
|
|
||||||
|
|
||||||
/* Limit the range to banish all previously biased results */
|
|
||||||
const int allowed = RAND_MAX - RAND_MAX % max;
|
|
||||||
|
|
||||||
RandState *state;
|
|
||||||
int tmp;
|
|
||||||
size_t i;
|
|
||||||
|
|
||||||
if (!createdKey)
|
|
||||||
{
|
|
||||||
pthread_key_create(&stateKey, RandDestructor);
|
|
||||||
createdKey = 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
state = pthread_getspecific(stateKey);
|
|
||||||
|
|
||||||
if (!state)
|
|
||||||
{
|
|
||||||
/* Generate a seed from the system time, PID, and TID */
|
|
||||||
UInt64 ts = UtilServerTs();
|
|
||||||
UInt32 seed = UInt64Low(ts) ^ getpid() ^ (unsigned long) pthread_self();
|
|
||||||
|
|
||||||
state = Malloc(sizeof(RandState));
|
|
||||||
RandSeed(state, seed);
|
|
||||||
|
|
||||||
pthread_setspecific(stateKey, state);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Generate {size} random numbers. */
|
|
||||||
for (i = 0; i < size; i++)
|
|
||||||
{
|
|
||||||
/* Most of the time, this will take about 1 loop */
|
|
||||||
do
|
|
||||||
{
|
|
||||||
tmp = RandGenerate(state);
|
|
||||||
} while (tmp > allowed);
|
|
||||||
|
|
||||||
buf[i] = tmp % max;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Generate just 1 random number */
|
|
||||||
int
|
|
||||||
RandInt(unsigned int max)
|
|
||||||
{
|
|
||||||
int val = 0;
|
|
||||||
|
|
||||||
RandIntN(&val, 1, max);
|
|
||||||
return val;
|
|
||||||
}
|
|
|
@ -1,191 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright (C) 2022-2023 Jordan Bancino <@jordan:bancino.net>
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person
|
|
||||||
* obtaining a copy of this software and associated documentation files
|
|
||||||
* (the "Software"), to deal in the Software without restriction,
|
|
||||||
* including without limitation the rights to use, copy, modify, merge,
|
|
||||||
* publish, distribute, sublicense, and/or sell copies of the Software,
|
|
||||||
* and to permit persons to whom the Software is furnished to do so,
|
|
||||||
* subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be
|
|
||||||
* included in all copies or portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
||||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
||||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
||||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
||||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
||||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
||||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
||||||
* SOFTWARE.
|
|
||||||
*/
|
|
||||||
#include <Runtime.h>
|
|
||||||
#include <Array.h>
|
|
||||||
#include <HashMap.h>
|
|
||||||
#include <Stream.h>
|
|
||||||
#include <Log.h>
|
|
||||||
#include <Memory.h>
|
|
||||||
#include <Str.h>
|
|
||||||
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <pthread.h>
|
|
||||||
|
|
||||||
#define ERR_BUF_SIZE 128
|
|
||||||
|
|
||||||
/* Specified by POSIX to contain environment variables */
|
|
||||||
extern char **environ;
|
|
||||||
|
|
||||||
/* The linking program is expected to provide Main */
|
|
||||||
extern int Main(Array *, HashMap *);
|
|
||||||
|
|
||||||
/* Functions in the Memory API that are not exported via header */
|
|
||||||
extern int MemoryRuntimeInit(void);
|
|
||||||
extern int MemoryRuntimeDestroy(void);
|
|
||||||
|
|
||||||
typedef struct MainArgs
|
|
||||||
{
|
|
||||||
Array *args;
|
|
||||||
HashMap *env;
|
|
||||||
int ret;
|
|
||||||
} MainArgs;
|
|
||||||
|
|
||||||
static void *
|
|
||||||
MainThread(void *argp)
|
|
||||||
{
|
|
||||||
MainArgs *args = argp;
|
|
||||||
|
|
||||||
args = argp;
|
|
||||||
args->ret = Main(args->args, args->env);
|
|
||||||
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
|
||||||
main(int argc, char **argv)
|
|
||||||
{
|
|
||||||
pthread_t mainThread;
|
|
||||||
size_t i;
|
|
||||||
|
|
||||||
char *key;
|
|
||||||
char *val;
|
|
||||||
char **envp;
|
|
||||||
|
|
||||||
MainArgs args;
|
|
||||||
|
|
||||||
char errBuf[ERR_BUF_SIZE];
|
|
||||||
int errLen;
|
|
||||||
|
|
||||||
args.args = NULL;
|
|
||||||
args.env = NULL;
|
|
||||||
args.ret = EXIT_FAILURE;
|
|
||||||
|
|
||||||
if (!MemoryRuntimeInit())
|
|
||||||
{
|
|
||||||
errLen = snprintf(errBuf, ERR_BUF_SIZE, "Fatal: Unable to initialize Memory runtime.\n");
|
|
||||||
write(STDERR_FILENO, errBuf, errLen);
|
|
||||||
goto finish;
|
|
||||||
}
|
|
||||||
|
|
||||||
args.args = ArrayCreate();
|
|
||||||
|
|
||||||
if (!args.args)
|
|
||||||
{
|
|
||||||
errLen = snprintf(errBuf, ERR_BUF_SIZE, "Fatal: Unable to allocate heap memory for program arguments.\n");
|
|
||||||
write(STDERR_FILENO, errBuf, errLen);
|
|
||||||
goto finish;
|
|
||||||
}
|
|
||||||
|
|
||||||
args.env = HashMapCreate();
|
|
||||||
if (!args.env)
|
|
||||||
{
|
|
||||||
errLen = snprintf(errBuf, ERR_BUF_SIZE, "Fatal: Unable to allocate heap memory for program environment.\n");
|
|
||||||
write(STDERR_FILENO, errBuf, errLen);
|
|
||||||
goto finish;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (i = 0; i < (size_t) argc; i++)
|
|
||||||
{
|
|
||||||
char *arg = StrDuplicate(argv[i]);
|
|
||||||
|
|
||||||
if (!arg || !ArrayAdd(args.args, arg))
|
|
||||||
{
|
|
||||||
errLen = snprintf(errBuf, ERR_BUF_SIZE, "Fatal: Unable to allocate heap memory for program argument.\n");
|
|
||||||
write(STDERR_FILENO, errBuf, errLen);
|
|
||||||
goto finish;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
envp = environ;
|
|
||||||
while (*envp)
|
|
||||||
{
|
|
||||||
size_t valInd;
|
|
||||||
|
|
||||||
/* It is unclear whether or not envp strings are writable, so
|
|
||||||
* we make our own copy to manipulate it */
|
|
||||||
key = StrDuplicate(*envp);
|
|
||||||
|
|
||||||
if (!key)
|
|
||||||
{
|
|
||||||
errLen = snprintf(errBuf, ERR_BUF_SIZE, "Fatal: Unable to allocate heap memory for program environment variable.\n");
|
|
||||||
write(STDERR_FILENO, errBuf, errLen);
|
|
||||||
goto finish;
|
|
||||||
}
|
|
||||||
|
|
||||||
valInd = strcspn(key, "=");
|
|
||||||
|
|
||||||
key[valInd] = '\0';
|
|
||||||
val = key + valInd + 1;
|
|
||||||
HashMapSet(args.env, key, StrDuplicate(val));
|
|
||||||
Free(key);
|
|
||||||
envp++;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (pthread_create(&mainThread, NULL, MainThread, &args) != 0)
|
|
||||||
{
|
|
||||||
errLen = snprintf(errBuf, ERR_BUF_SIZE, "Fatal: Unable to create main thread.\n");
|
|
||||||
write(STDERR_FILENO, errBuf, errLen);
|
|
||||||
goto finish;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (pthread_join(mainThread, NULL) != 0)
|
|
||||||
{
|
|
||||||
errLen = snprintf(errBuf, ERR_BUF_SIZE, "Fatal: Unable to join main thread.\n");
|
|
||||||
write(STDERR_FILENO, errBuf, errLen);
|
|
||||||
args.ret = EXIT_FAILURE;
|
|
||||||
goto finish;
|
|
||||||
}
|
|
||||||
|
|
||||||
finish:
|
|
||||||
if (args.args)
|
|
||||||
{
|
|
||||||
for (i = 0; i < ArraySize(args.args); i++)
|
|
||||||
{
|
|
||||||
Free(ArrayGet(args.args, i));
|
|
||||||
}
|
|
||||||
ArrayFree(args.args);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (args.env)
|
|
||||||
{
|
|
||||||
while (HashMapIterate(args.env, &key, (void **) &val))
|
|
||||||
{
|
|
||||||
Free(val);
|
|
||||||
}
|
|
||||||
HashMapFree(args.env);
|
|
||||||
}
|
|
||||||
|
|
||||||
LogConfigFree(LogConfigGlobal());
|
|
||||||
|
|
||||||
StreamClose(StreamStdout());
|
|
||||||
StreamClose(StreamStdin());
|
|
||||||
StreamClose(StreamStderr());
|
|
||||||
|
|
||||||
GenerateMemoryReport(argc, argv);
|
|
||||||
|
|
||||||
MemoryRuntimeDestroy();
|
|
||||||
|
|
||||||
return args.ret;
|
|
||||||
}
|
|
|
@ -1,116 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright (C) 2022-2023 Jordan Bancino <@jordan:bancino.net>
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person
|
|
||||||
* obtaining a copy of this software and associated documentation files
|
|
||||||
* (the "Software"), to deal in the Software without restriction,
|
|
||||||
* including without limitation the rights to use, copy, modify, merge,
|
|
||||||
* publish, distribute, sublicense, and/or sell copies of the Software,
|
|
||||||
* and to permit persons to whom the Software is furnished to do so,
|
|
||||||
* subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be
|
|
||||||
* included in all copies or portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
||||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
||||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
||||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
||||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
||||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
||||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
||||||
* SOFTWARE.
|
|
||||||
*/
|
|
||||||
#include <Runtime.h>
|
|
||||||
#include <Array.h>
|
|
||||||
#include <Stream.h>
|
|
||||||
#include <Log.h>
|
|
||||||
#include <Memory.h>
|
|
||||||
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <time.h>
|
|
||||||
#include <libgen.h>
|
|
||||||
|
|
||||||
static void
|
|
||||||
HexDump(size_t off, char *hexBuf, char *asciiBuf, void *args)
|
|
||||||
{
|
|
||||||
FILE *report = args;
|
|
||||||
|
|
||||||
if (hexBuf && asciiBuf)
|
|
||||||
{
|
|
||||||
fprintf(report, "%04lx: %s | %s |\n", off, hexBuf, asciiBuf);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
fprintf(report, "%04lx\n", off);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static void
|
|
||||||
MemoryIterator(MemoryInfo * i, void *args)
|
|
||||||
{
|
|
||||||
FILE *report = args;
|
|
||||||
|
|
||||||
fprintf(report, "%s:%d: %lu bytes at %p\n",
|
|
||||||
MemoryInfoGetFile(i), MemoryInfoGetLine(i),
|
|
||||||
MemoryInfoGetSize(i), MemoryInfoGetPointer(i));
|
|
||||||
|
|
||||||
MemoryHexDump(i, HexDump, report);
|
|
||||||
|
|
||||||
fprintf(report, "\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
GenerateMemoryReport(int argc, char **argv)
|
|
||||||
{
|
|
||||||
char reportName[128];
|
|
||||||
char *namePtr;
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Use C standard IO instead of the Stream or Io APIs, because
|
|
||||||
* those use the Memory API, and that's exactly what we're trying
|
|
||||||
* to assess, so using it would generate false positives. None of
|
|
||||||
* this code should leak memory.
|
|
||||||
*/
|
|
||||||
FILE *report;
|
|
||||||
time_t currentTime;
|
|
||||||
struct tm *timeInfo;
|
|
||||||
char tsBuffer[1024];
|
|
||||||
int i;
|
|
||||||
|
|
||||||
if (!MemoryAllocated())
|
|
||||||
{
|
|
||||||
/* No memory leaked, no need to write the report. This is the
|
|
||||||
* ideal situation; we only want the report to show up if leaks
|
|
||||||
* occurred. */
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
snprintf(reportName, sizeof(reportName), "%s-leaked.txt", argv[0]);
|
|
||||||
/* Make sure the report goes in the current working directory. */
|
|
||||||
namePtr = basename(reportName);
|
|
||||||
report = fopen(namePtr, "a");
|
|
||||||
if (!report)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
currentTime = time(NULL);
|
|
||||||
timeInfo = localtime(¤tTime);
|
|
||||||
strftime(tsBuffer, sizeof(tsBuffer), "%c", timeInfo);
|
|
||||||
|
|
||||||
fprintf(report, "---------- Memory Report ----------\n");
|
|
||||||
fprintf(report, "Program:");
|
|
||||||
for (i = 0; i < argc; i++)
|
|
||||||
{
|
|
||||||
fprintf(report, " '%s'", argv[i]);
|
|
||||||
}
|
|
||||||
fprintf(report, "\nDate: %s\n", tsBuffer);
|
|
||||||
fprintf(report, "Total Bytes: %lu\n", MemoryAllocated());
|
|
||||||
fprintf(report, "\n");
|
|
||||||
|
|
||||||
MemoryIterate(MemoryIterator, report);
|
|
||||||
|
|
||||||
fclose(report);
|
|
||||||
}
|
|
|
@ -1,48 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright (C) 2022-2023 Jordan Bancino <@jordan:bancino.net>
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person
|
|
||||||
* obtaining a copy of this software and associated documentation files
|
|
||||||
* (the "Software"), to deal in the Software without restriction,
|
|
||||||
* including without limitation the rights to use, copy, modify, merge,
|
|
||||||
* publish, distribute, sublicense, and/or sell copies of the Software,
|
|
||||||
* and to permit persons to whom the Software is furnished to do so,
|
|
||||||
* subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be
|
|
||||||
* included in all copies or portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
||||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
||||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
||||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
||||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
||||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
||||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
||||||
* SOFTWARE.
|
|
||||||
*/
|
|
||||||
#include <Sha.h>
|
|
||||||
#include <Memory.h>
|
|
||||||
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
char *
|
|
||||||
ShaToHex(unsigned char *bytes)
|
|
||||||
{
|
|
||||||
size_t i = 0;
|
|
||||||
char *str = Malloc(((strlen((char *) bytes) * 2) + 1) * sizeof(char));
|
|
||||||
|
|
||||||
if (!str)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
while (bytes[i] != '\0')
|
|
||||||
{
|
|
||||||
snprintf(str + (2 * i), 3, "%02x", bytes[i]);
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
|
|
||||||
return str;
|
|
||||||
}
|
|
|
@ -1,267 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright (C) 2022-2023 Jordan Bancino <@jordan:bancino.net>
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person
|
|
||||||
* obtaining a copy of this software and associated documentation files
|
|
||||||
* (the "Software"), to deal in the Software without restriction,
|
|
||||||
* including without limitation the rights to use, copy, modify, merge,
|
|
||||||
* publish, distribute, sublicense, and/or sell copies of the Software,
|
|
||||||
* and to permit persons to whom the Software is furnished to do so,
|
|
||||||
* subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be
|
|
||||||
* included in all copies or portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
||||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
||||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
||||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
||||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
||||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
||||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
||||||
* SOFTWARE.
|
|
||||||
*/
|
|
||||||
#include <Sha.h>
|
|
||||||
#include <Memory.h>
|
|
||||||
#include <Int.h>
|
|
||||||
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
#include <limits.h>
|
|
||||||
|
|
||||||
#define LOAD32H(x, y) \
|
|
||||||
{ \
|
|
||||||
x = ((UInt32)((y)[0] & 255) << 24) | \
|
|
||||||
((UInt32)((y)[1] & 255) << 16) | \
|
|
||||||
((UInt32)((y)[2] & 255) << 8) | \
|
|
||||||
((UInt32)((y)[3] & 255)); \
|
|
||||||
}
|
|
||||||
|
|
||||||
#define ROL(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
|
|
||||||
|
|
||||||
#define BLK(i) (block->l[i & 15] = ROL(block->l[(i+13) & 15] ^ block->l[(i + 8) & 15] ^ block->l[(i + 2) & 15] ^ block->l[i & 15], 1))
|
|
||||||
|
|
||||||
#define R0(v, w, x, y, z, i) z += ((w & (x ^ y)) ^ y) + block->l[i] + 0x5A827999 + ROL(v, 5); w = ROL(w, 30);
|
|
||||||
#define R1(v, w, x, y, z, i) z += ((w & (x ^ y)) ^ y) + BLK(i) + 0x5A827999 + ROL(v, 5); w = ROL(w, 30);
|
|
||||||
#define R2(v, w, x, y, z, i) z += (w ^ x ^ y) + BLK(i) + 0x6ED9EBA1 + ROL(v, 5); w = ROL(w, 30);
|
|
||||||
#define R3(v, w, x, y, z, i) z += (((w | x) & y) | (w & x)) + BLK(i) + 0x8F1BBCDC + ROL(v, 5); w = ROL(w, 30);
|
|
||||||
#define R4(v, w, x, y, z, i) z += (w ^ x ^ y) + BLK(i) + 0xCA62C1D6 + ROL(v, 5); w = ROL(w, 30);
|
|
||||||
|
|
||||||
typedef union
|
|
||||||
{
|
|
||||||
UInt8 c[64];
|
|
||||||
UInt32 l[16];
|
|
||||||
} Char64Long16;
|
|
||||||
|
|
||||||
typedef struct
|
|
||||||
{
|
|
||||||
UInt32 state[5];
|
|
||||||
UInt32 count[2];
|
|
||||||
UInt8 buffer[64];
|
|
||||||
} Sha1Context;
|
|
||||||
|
|
||||||
static void
|
|
||||||
Sha1Transform(UInt32 state[5], const UInt8 buffer[64])
|
|
||||||
{
|
|
||||||
UInt32 a, b, c, d, e, i;
|
|
||||||
UInt8 workspace[64];
|
|
||||||
Char64Long16 *block = (Char64Long16 *) workspace;
|
|
||||||
|
|
||||||
for (i = 0; i < 16; i++)
|
|
||||||
{
|
|
||||||
LOAD32H(block->l[i], buffer + (i * 4));
|
|
||||||
}
|
|
||||||
|
|
||||||
a = state[0];
|
|
||||||
b = state[1];
|
|
||||||
c = state[2];
|
|
||||||
d = state[3];
|
|
||||||
e = state[4];
|
|
||||||
|
|
||||||
R0(a, b, c, d, e, 0);
|
|
||||||
R0(e, a, b, c, d, 1);
|
|
||||||
R0(d, e, a, b, c, 2);
|
|
||||||
R0(c, d, e, a, b, 3);
|
|
||||||
R0(b, c, d, e, a, 4);
|
|
||||||
R0(a, b, c, d, e, 5);
|
|
||||||
R0(e, a, b, c, d, 6);
|
|
||||||
R0(d, e, a, b, c, 7);
|
|
||||||
R0(c, d, e, a, b, 8);
|
|
||||||
R0(b, c, d, e, a, 9);
|
|
||||||
R0(a, b, c, d, e, 10);
|
|
||||||
R0(e, a, b, c, d, 11);
|
|
||||||
R0(d, e, a, b, c, 12);
|
|
||||||
R0(c, d, e, a, b, 13);
|
|
||||||
R0(b, c, d, e, a, 14);
|
|
||||||
R0(a, b, c, d, e, 15);
|
|
||||||
R1(e, a, b, c, d, 16);
|
|
||||||
R1(d, e, a, b, c, 17);
|
|
||||||
R1(c, d, e, a, b, 18);
|
|
||||||
R1(b, c, d, e, a, 19);
|
|
||||||
R2(a, b, c, d, e, 20);
|
|
||||||
R2(e, a, b, c, d, 21);
|
|
||||||
R2(d, e, a, b, c, 22);
|
|
||||||
R2(c, d, e, a, b, 23);
|
|
||||||
R2(b, c, d, e, a, 24);
|
|
||||||
R2(a, b, c, d, e, 25);
|
|
||||||
R2(e, a, b, c, d, 26);
|
|
||||||
R2(d, e, a, b, c, 27);
|
|
||||||
R2(c, d, e, a, b, 28);
|
|
||||||
R2(b, c, d, e, a, 29);
|
|
||||||
R2(a, b, c, d, e, 30);
|
|
||||||
R2(e, a, b, c, d, 31);
|
|
||||||
R2(d, e, a, b, c, 32);
|
|
||||||
R2(c, d, e, a, b, 33);
|
|
||||||
R2(b, c, d, e, a, 34);
|
|
||||||
R2(a, b, c, d, e, 35);
|
|
||||||
R2(e, a, b, c, d, 36);
|
|
||||||
R2(d, e, a, b, c, 37);
|
|
||||||
R2(c, d, e, a, b, 38);
|
|
||||||
R2(b, c, d, e, a, 39);
|
|
||||||
R3(a, b, c, d, e, 40);
|
|
||||||
R3(e, a, b, c, d, 41);
|
|
||||||
R3(d, e, a, b, c, 42);
|
|
||||||
R3(c, d, e, a, b, 43);
|
|
||||||
R3(b, c, d, e, a, 44);
|
|
||||||
R3(a, b, c, d, e, 45);
|
|
||||||
R3(e, a, b, c, d, 46);
|
|
||||||
R3(d, e, a, b, c, 47);
|
|
||||||
R3(c, d, e, a, b, 48);
|
|
||||||
R3(b, c, d, e, a, 49);
|
|
||||||
R3(a, b, c, d, e, 50);
|
|
||||||
R3(e, a, b, c, d, 51);
|
|
||||||
R3(d, e, a, b, c, 52);
|
|
||||||
R3(c, d, e, a, b, 53);
|
|
||||||
R3(b, c, d, e, a, 54);
|
|
||||||
R3(a, b, c, d, e, 55);
|
|
||||||
R3(e, a, b, c, d, 56);
|
|
||||||
R3(d, e, a, b, c, 57);
|
|
||||||
R3(c, d, e, a, b, 58);
|
|
||||||
R3(b, c, d, e, a, 59);
|
|
||||||
R4(a, b, c, d, e, 60);
|
|
||||||
R4(e, a, b, c, d, 61);
|
|
||||||
R4(d, e, a, b, c, 62);
|
|
||||||
R4(c, d, e, a, b, 63);
|
|
||||||
R4(b, c, d, e, a, 64);
|
|
||||||
R4(a, b, c, d, e, 65);
|
|
||||||
R4(e, a, b, c, d, 66);
|
|
||||||
R4(d, e, a, b, c, 67);
|
|
||||||
R4(c, d, e, a, b, 68);
|
|
||||||
R4(b, c, d, e, a, 69);
|
|
||||||
R4(a, b, c, d, e, 70);
|
|
||||||
R4(e, a, b, c, d, 71);
|
|
||||||
R4(d, e, a, b, c, 72);
|
|
||||||
R4(c, d, e, a, b, 73);
|
|
||||||
R4(b, c, d, e, a, 74);
|
|
||||||
R4(a, b, c, d, e, 75);
|
|
||||||
R4(e, a, b, c, d, 76);
|
|
||||||
R4(d, e, a, b, c, 77);
|
|
||||||
R4(c, d, e, a, b, 78);
|
|
||||||
R4(b, c, d, e, a, 79);
|
|
||||||
|
|
||||||
state[0] += a;
|
|
||||||
state[1] += b;
|
|
||||||
state[2] += c;
|
|
||||||
state[3] += d;
|
|
||||||
state[4] += e;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
Sha1Init(Sha1Context * ctx)
|
|
||||||
{
|
|
||||||
ctx->state[0] = 0x67452301;
|
|
||||||
ctx->state[1] = 0xEFCDAB89;
|
|
||||||
ctx->state[2] = 0x98BADCFE;
|
|
||||||
ctx->state[3] = 0x10325476;
|
|
||||||
ctx->state[4] = 0xC3D2E1F0;
|
|
||||||
|
|
||||||
ctx->count[0] = 0;
|
|
||||||
ctx->count[1] = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
Sha1Update(Sha1Context * ctx, const void *buf, UInt32 size)
|
|
||||||
{
|
|
||||||
UInt32 i, j;
|
|
||||||
|
|
||||||
j = (ctx->count[0] >> 3) & 63;
|
|
||||||
|
|
||||||
if ((ctx->count[0] += size << 3) < (size << 3))
|
|
||||||
{
|
|
||||||
ctx->count[1]++;
|
|
||||||
}
|
|
||||||
|
|
||||||
ctx->count[1] += (size >> 29);
|
|
||||||
|
|
||||||
if ((j + size) > 63)
|
|
||||||
{
|
|
||||||
i = 64 - j;
|
|
||||||
|
|
||||||
memcpy(&ctx->buffer[j], buf, i);
|
|
||||||
Sha1Transform(ctx->state, ctx->buffer);
|
|
||||||
|
|
||||||
for (; i + 63 < size; i += 64)
|
|
||||||
{
|
|
||||||
Sha1Transform(ctx->state, (UInt8 *) buf + i);
|
|
||||||
}
|
|
||||||
|
|
||||||
j = 0;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
i = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
memcpy(&ctx->buffer[j], &((UInt8 *) buf)[i], size - i);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
Sha1Calculate(Sha1Context * ctx, unsigned char *out)
|
|
||||||
{
|
|
||||||
UInt32 i;
|
|
||||||
UInt8 count[8];
|
|
||||||
|
|
||||||
for (i = 0; i < 8; i++)
|
|
||||||
{
|
|
||||||
count[i] = (unsigned char) ((ctx->count[(i >= 4 ? 0 : 1)]
|
|
||||||
>> ((3 - (i & 3)) * 8)) & 255);
|
|
||||||
}
|
|
||||||
|
|
||||||
Sha1Update(ctx, (UInt8 *) "\x80", 1);
|
|
||||||
while ((ctx->count[0] & 504) != 448)
|
|
||||||
{
|
|
||||||
Sha1Update(ctx, (UInt8 *) "\0", 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
Sha1Update(ctx, count, 8);
|
|
||||||
for (i = 0; i < (160 / 8); i++)
|
|
||||||
{
|
|
||||||
out[i] = (UInt8) ((ctx->state[i >> 2] >> ((3 - (i & 3)) * 8)) & 255);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned char *
|
|
||||||
Sha1(char *str)
|
|
||||||
{
|
|
||||||
Sha1Context ctx;
|
|
||||||
unsigned char *out;
|
|
||||||
|
|
||||||
if (!str)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
out = Malloc(((160 / 8) + 1) * sizeof(unsigned char));
|
|
||||||
if (!out)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
Sha1Init(&ctx);
|
|
||||||
Sha1Update(&ctx, str, strlen(str));
|
|
||||||
Sha1Calculate(&ctx, out);
|
|
||||||
|
|
||||||
out[160 / 8] = '\0';
|
|
||||||
|
|
||||||
return out;
|
|
||||||
}
|
|
|
@ -1,233 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright (C) 2022-2023 Jordan Bancino <@jordan:bancino.net>
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person
|
|
||||||
* obtaining a copy of this software and associated documentation files
|
|
||||||
* (the "Software"), to deal in the Software without restriction,
|
|
||||||
* including without limitation the rights to use, copy, modify, merge,
|
|
||||||
* publish, distribute, sublicense, and/or sell copies of the Software,
|
|
||||||
* and to permit persons to whom the Software is furnished to do so,
|
|
||||||
* subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be
|
|
||||||
* included in all copies or portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
||||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
||||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
||||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
||||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
||||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
||||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
||||||
* SOFTWARE.
|
|
||||||
*/
|
|
||||||
#include <Sha.h>
|
|
||||||
#include <Memory.h>
|
|
||||||
#include <Int.h>
|
|
||||||
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
#include <limits.h>
|
|
||||||
|
|
||||||
#define GET_UINT32(x) \
|
|
||||||
(((UInt32)(x)[0] << 24) | \
|
|
||||||
((UInt32)(x)[1] << 16) | \
|
|
||||||
((UInt32)(x)[2] << 8) | \
|
|
||||||
((UInt32)(x)[3]))
|
|
||||||
|
|
||||||
#define PUT_UINT32(dst, x) { \
|
|
||||||
(dst)[0] = (x) >> 24; \
|
|
||||||
(dst)[1] = (x) >> 16; \
|
|
||||||
(dst)[2] = (x) >> 8; \
|
|
||||||
(dst)[3] = (x); \
|
|
||||||
}
|
|
||||||
|
|
||||||
#define ROTR(x, n) (((x) >> (n)) | ((x) << (32 - (n))))
|
|
||||||
|
|
||||||
#define S0(x) (ROTR(x, 7) ^ ROTR(x, 18) ^ (x >> 3))
|
|
||||||
#define S1(x) (ROTR(x, 17) ^ ROTR(x, 19) ^ (x >> 10))
|
|
||||||
|
|
||||||
#define T0(x) (ROTR(x, 6) ^ ROTR(x, 11) ^ ROTR(x, 25))
|
|
||||||
#define T1(x) (ROTR(x, 2) ^ ROTR(x, 13) ^ ROTR(x, 22))
|
|
||||||
|
|
||||||
#define CH(a, b, c) (((a) & (b)) ^ ((~(a)) & (c)))
|
|
||||||
#define MAJ(a, b, c) (((a) & (b)) ^ ((a) & (c)) ^ ((b) & (c)))
|
|
||||||
#define WW(i) (w[i] = w[i - 16] + S0(w[i - 15]) + w[i - 7] + S1(w[i - 2]))
|
|
||||||
|
|
||||||
#define ROUND(a, b, c, d, e, f, g, h, k, w) { \
|
|
||||||
UInt32 tmp0 = h + T0(e) + CH(e, f, g) + k + w; \
|
|
||||||
UInt32 tmp1 = T1(a) + MAJ(a, b, c); \
|
|
||||||
h = tmp0 + tmp1; \
|
|
||||||
d += tmp0; \
|
|
||||||
}
|
|
||||||
|
|
||||||
typedef struct Sha256Context
|
|
||||||
{
|
|
||||||
size_t length;
|
|
||||||
UInt32 state[8];
|
|
||||||
size_t bufLen;
|
|
||||||
unsigned char buffer[64];
|
|
||||||
} Sha256Context;
|
|
||||||
|
|
||||||
static void
|
|
||||||
Sha256Chunk(Sha256Context * context, unsigned char chunk[64])
|
|
||||||
{
|
|
||||||
const UInt32 rk[64] = {
|
|
||||||
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1,
|
|
||||||
0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
|
|
||||||
0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786,
|
|
||||||
0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
|
|
||||||
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147,
|
|
||||||
0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
|
|
||||||
0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b,
|
|
||||||
0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
|
|
||||||
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a,
|
|
||||||
0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
|
|
||||||
0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
|
|
||||||
};
|
|
||||||
|
|
||||||
UInt32 w[64];
|
|
||||||
UInt32 a, b, c, d, e, f, g, h;
|
|
||||||
|
|
||||||
int i;
|
|
||||||
|
|
||||||
for (i = 0; i < 16; i++)
|
|
||||||
{
|
|
||||||
w[i] = GET_UINT32(&chunk[4 * i]);
|
|
||||||
}
|
|
||||||
|
|
||||||
a = context->state[0];
|
|
||||||
b = context->state[1];
|
|
||||||
c = context->state[2];
|
|
||||||
d = context->state[3];
|
|
||||||
e = context->state[4];
|
|
||||||
f = context->state[5];
|
|
||||||
g = context->state[6];
|
|
||||||
h = context->state[7];
|
|
||||||
|
|
||||||
for (i = 0; i < 16; i += 8)
|
|
||||||
{
|
|
||||||
ROUND(a, b, c, d, e, f, g, h, rk[i], w[i]);
|
|
||||||
ROUND(h, a, b, c, d, e, f, g, rk[i + 1], w[i + 1]);
|
|
||||||
ROUND(g, h, a, b, c, d, e, f, rk[i + 2], w[i + 2]);
|
|
||||||
ROUND(f, g, h, a, b, c, d, e, rk[i + 3], w[i + 3]);
|
|
||||||
ROUND(e, f, g, h, a, b, c, d, rk[i + 4], w[i + 4]);
|
|
||||||
ROUND(d, e, f, g, h, a, b, c, rk[i + 5], w[i + 5]);
|
|
||||||
ROUND(c, d, e, f, g, h, a, b, rk[i + 6], w[i + 6]);
|
|
||||||
ROUND(b, c, d, e, f, g, h, a, rk[i + 7], w[i + 7]);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (i = 16; i < 64; i += 8)
|
|
||||||
{
|
|
||||||
ROUND(a, b, c, d, e, f, g, h, rk[i], WW(i));
|
|
||||||
ROUND(h, a, b, c, d, e, f, g, rk[i + 1], WW(i + 1));
|
|
||||||
ROUND(g, h, a, b, c, d, e, f, rk[i + 2], WW(i + 2));
|
|
||||||
ROUND(f, g, h, a, b, c, d, e, rk[i + 3], WW(i + 3));
|
|
||||||
ROUND(e, f, g, h, a, b, c, d, rk[i + 4], WW(i + 4));
|
|
||||||
ROUND(d, e, f, g, h, a, b, c, rk[i + 5], WW(i + 5));
|
|
||||||
ROUND(c, d, e, f, g, h, a, b, rk[i + 6], WW(i + 6));
|
|
||||||
ROUND(b, c, d, e, f, g, h, a, rk[i + 7], WW(i + 7));
|
|
||||||
}
|
|
||||||
|
|
||||||
context->state[0] += a;
|
|
||||||
context->state[1] += b;
|
|
||||||
context->state[2] += c;
|
|
||||||
context->state[3] += d;
|
|
||||||
context->state[4] += e;
|
|
||||||
context->state[5] += f;
|
|
||||||
context->state[6] += g;
|
|
||||||
context->state[7] += h;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
Sha256Process(Sha256Context * context, unsigned char *data, size_t length)
|
|
||||||
{
|
|
||||||
context->length += length;
|
|
||||||
|
|
||||||
if (context->bufLen && context->bufLen + length >= 64)
|
|
||||||
{
|
|
||||||
int len = 64 - context->bufLen;
|
|
||||||
|
|
||||||
memcpy(context->buffer + context->bufLen, data, len);
|
|
||||||
Sha256Chunk(context, context->buffer);
|
|
||||||
data += len;
|
|
||||||
length -= len;
|
|
||||||
context->bufLen = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
while (length >= 64)
|
|
||||||
{
|
|
||||||
Sha256Chunk(context, data);
|
|
||||||
data += 64;
|
|
||||||
length -= 64;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (length)
|
|
||||||
{
|
|
||||||
memcpy(context->buffer + context->bufLen, data, length);
|
|
||||||
context->bufLen += length;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned char *
|
|
||||||
Sha256(char *str)
|
|
||||||
{
|
|
||||||
Sha256Context context;
|
|
||||||
size_t i;
|
|
||||||
unsigned char *out;
|
|
||||||
|
|
||||||
unsigned char fill[64];
|
|
||||||
UInt32 fillLen;
|
|
||||||
unsigned char buf[8];
|
|
||||||
UInt32 hiLen;
|
|
||||||
UInt32 loLen;
|
|
||||||
|
|
||||||
if (!str)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
out = Malloc(33 * sizeof(unsigned char));
|
|
||||||
if (!out)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
context.state[0] = 0x6a09e667;
|
|
||||||
context.state[1] = 0xbb67ae85;
|
|
||||||
context.state[2] = 0x3c6ef372;
|
|
||||||
context.state[3] = 0xa54ff53a;
|
|
||||||
context.state[4] = 0x510e527f;
|
|
||||||
context.state[5] = 0x9b05688c;
|
|
||||||
context.state[6] = 0x1f83d9ab;
|
|
||||||
context.state[7] = 0x5be0cd19;
|
|
||||||
|
|
||||||
context.bufLen = 0;
|
|
||||||
context.length = 0;
|
|
||||||
memset(context.buffer, 0, 64);
|
|
||||||
|
|
||||||
Sha256Process(&context, (unsigned char *) str, strlen(str));
|
|
||||||
|
|
||||||
memset(fill, 0, 64);
|
|
||||||
fill[0] = 0x80;
|
|
||||||
|
|
||||||
fillLen = (context.bufLen < 56) ? 56 - context.bufLen : 120 - context.bufLen;
|
|
||||||
hiLen = (UInt32) (context.length >> 29);
|
|
||||||
loLen = (UInt32) (context.length << 3);
|
|
||||||
|
|
||||||
PUT_UINT32(&buf[0], hiLen);
|
|
||||||
PUT_UINT32(&buf[4], loLen);
|
|
||||||
|
|
||||||
Sha256Process(&context, fill, fillLen);
|
|
||||||
Sha256Process(&context, buf, 8);
|
|
||||||
|
|
||||||
for (i = 0; i < 8; i++)
|
|
||||||
{
|
|
||||||
PUT_UINT32(&out[4 * i], context.state[i]);
|
|
||||||
}
|
|
||||||
|
|
||||||
out[32] = '\0';
|
|
||||||
|
|
||||||
return out;
|
|
||||||
}
|
|
|
@ -1,343 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright (C) 2022-2023 Jordan Bancino <@jordan:bancino.net>
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person
|
|
||||||
* obtaining a copy of this software and associated documentation files
|
|
||||||
* (the "Software"), to deal in the Software without restriction,
|
|
||||||
* including without limitation the rights to use, copy, modify, merge,
|
|
||||||
* publish, distribute, sublicense, and/or sell copies of the Software,
|
|
||||||
* and to permit persons to whom the Software is furnished to do so,
|
|
||||||
* subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be
|
|
||||||
* included in all copies or portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
||||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
||||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
||||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
||||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
||||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
||||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
||||||
* SOFTWARE.
|
|
||||||
*/
|
|
||||||
#include <Str.h>
|
|
||||||
|
|
||||||
#include <Memory.h>
|
|
||||||
#include <Util.h>
|
|
||||||
#include <Rand.h>
|
|
||||||
#include <Int.h>
|
|
||||||
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <ctype.h>
|
|
||||||
#include <stdarg.h>
|
|
||||||
#include <pthread.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
|
|
||||||
UInt32
|
|
||||||
StrUtf16Decode(UInt16 high, UInt16 low)
|
|
||||||
{
|
|
||||||
if (high <= 0xD7FF)
|
|
||||||
{
|
|
||||||
return high;
|
|
||||||
}
|
|
||||||
else if (high <= 0xDBFF)
|
|
||||||
{
|
|
||||||
unsigned short hS = (high - 0xD800) * 0x400;
|
|
||||||
unsigned short lS = low - 0xDC00;
|
|
||||||
|
|
||||||
return (lS | hS) + 0x10000;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
char *
|
|
||||||
StrUtf8Encode(UInt32 codepoint)
|
|
||||||
{
|
|
||||||
char *str;
|
|
||||||
|
|
||||||
str = Malloc(5 * sizeof(char));
|
|
||||||
if (!str)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (codepoint <= 0x7F && codepoint != 0) /* Plain ASCII */
|
|
||||||
{
|
|
||||||
str[0] = (char) codepoint;
|
|
||||||
str[1] = '\0';
|
|
||||||
}
|
|
||||||
else if (codepoint <= 0x07FF) /* 2-byte */
|
|
||||||
{
|
|
||||||
str[0] = (char) (((codepoint >> 6) & 0x1F) | 0xC0);
|
|
||||||
str[1] = (char) (((codepoint >> 0) & 0x3F) | 0x80);
|
|
||||||
str[2] = '\0';
|
|
||||||
}
|
|
||||||
else if (codepoint <= 0xFFFF) /* 3-byte */
|
|
||||||
{
|
|
||||||
str[0] = (char) (((codepoint >> 12) & 0x0F) | 0xE0);
|
|
||||||
str[1] = (char) (((codepoint >> 6) & 0x3F) | 0x80);
|
|
||||||
str[2] = (char) (((codepoint >> 0) & 0x3F) | 0x80);
|
|
||||||
str[3] = '\0';
|
|
||||||
}
|
|
||||||
else if (codepoint <= 0x10FFFF)/* 4-byte */
|
|
||||||
{
|
|
||||||
str[0] = (char) (((codepoint >> 18) & 0x07) | 0xF0);
|
|
||||||
str[1] = (char) (((codepoint >> 12) & 0x3F) | 0x80);
|
|
||||||
str[2] = (char) (((codepoint >> 6) & 0x3F) | 0x80);
|
|
||||||
str[3] = (char) (((codepoint >> 0) & 0x3F) | 0x80);
|
|
||||||
str[4] = '\0';
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
/* Send replacement character */
|
|
||||||
str[0] = (char) 0xEF;
|
|
||||||
str[1] = (char) 0xBF;
|
|
||||||
str[2] = (char) 0xBD;
|
|
||||||
str[3] = '\0';
|
|
||||||
}
|
|
||||||
|
|
||||||
return str;
|
|
||||||
}
|
|
||||||
|
|
||||||
char *
|
|
||||||
StrDuplicate(const char *inStr)
|
|
||||||
{
|
|
||||||
size_t len;
|
|
||||||
char *outStr;
|
|
||||||
|
|
||||||
if (!inStr)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
len = strlen(inStr);
|
|
||||||
outStr = Malloc(len + 1); /* For the null terminator */
|
|
||||||
if (!outStr)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
strncpy(outStr, inStr, len + 1);
|
|
||||||
|
|
||||||
return outStr;
|
|
||||||
}
|
|
||||||
|
|
||||||
char *
|
|
||||||
StrSubstr(const char *inStr, size_t start, size_t end)
|
|
||||||
{
|
|
||||||
size_t len;
|
|
||||||
size_t i;
|
|
||||||
size_t j;
|
|
||||||
|
|
||||||
char *outStr;
|
|
||||||
|
|
||||||
if (!inStr)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (start >= end)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
len = end - start;
|
|
||||||
outStr = Malloc(len + 1);
|
|
||||||
if (!outStr)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
j = 0;
|
|
||||||
for (i = start; i < end; i++)
|
|
||||||
{
|
|
||||||
if (inStr[i] == '\0')
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
outStr[j] = inStr[i];
|
|
||||||
j++;
|
|
||||||
}
|
|
||||||
|
|
||||||
outStr[j] = '\0';
|
|
||||||
|
|
||||||
return outStr;
|
|
||||||
}
|
|
||||||
|
|
||||||
char *
|
|
||||||
StrConcat(size_t nStr,...)
|
|
||||||
{
|
|
||||||
va_list argp;
|
|
||||||
char *str;
|
|
||||||
char *strp;
|
|
||||||
size_t strLen = 0;
|
|
||||||
size_t i;
|
|
||||||
|
|
||||||
va_start(argp, nStr);
|
|
||||||
for (i = 0; i < nStr; i++)
|
|
||||||
{
|
|
||||||
char *argStr = va_arg(argp, char *);
|
|
||||||
|
|
||||||
if (argStr)
|
|
||||||
{
|
|
||||||
strLen += strlen(argStr);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
va_end(argp);
|
|
||||||
|
|
||||||
str = Malloc(strLen + 1);
|
|
||||||
strp = str;
|
|
||||||
|
|
||||||
va_start(argp, nStr);
|
|
||||||
|
|
||||||
for (i = 0; i < nStr; i++)
|
|
||||||
{
|
|
||||||
/* Manually copy chars instead of using strcopy() so we don't
|
|
||||||
* have to call strlen() on the strings again, and we aren't
|
|
||||||
* writing useless null chars. */
|
|
||||||
|
|
||||||
char *argStr = va_arg(argp, char *);
|
|
||||||
|
|
||||||
if (argStr)
|
|
||||||
{
|
|
||||||
while (*argStr)
|
|
||||||
{
|
|
||||||
*strp = *argStr;
|
|
||||||
strp++;
|
|
||||||
argStr++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
va_end(argp);
|
|
||||||
str[strLen] = '\0';
|
|
||||||
return str;
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
|
||||||
StrBlank(const char *str)
|
|
||||||
{
|
|
||||||
int blank = 1;
|
|
||||||
size_t i = 0;
|
|
||||||
|
|
||||||
while (str[i])
|
|
||||||
{
|
|
||||||
blank &= isspace((unsigned char) str[i]);
|
|
||||||
/* No need to continue if we don't have a blank */
|
|
||||||
if (!blank)
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
return blank;
|
|
||||||
}
|
|
||||||
|
|
||||||
char *
|
|
||||||
StrRandom(size_t len)
|
|
||||||
{
|
|
||||||
static const char charset[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
|
||||||
|
|
||||||
char *str;
|
|
||||||
int *nums;
|
|
||||||
size_t i;
|
|
||||||
|
|
||||||
if (!len)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
str = Malloc(len + 1);
|
|
||||||
|
|
||||||
if (!str)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
nums = Malloc(len * sizeof(int));
|
|
||||||
if (!nums)
|
|
||||||
{
|
|
||||||
Free(str);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* TODO: This seems slow. */
|
|
||||||
RandIntN(nums, len, sizeof(charset) - 1);
|
|
||||||
for (i = 0; i < len; i++)
|
|
||||||
{
|
|
||||||
str[i] = charset[nums[i]];
|
|
||||||
}
|
|
||||||
|
|
||||||
Free(nums);
|
|
||||||
str[len] = '\0';
|
|
||||||
return str;
|
|
||||||
}
|
|
||||||
|
|
||||||
char *
|
|
||||||
StrInt(long i)
|
|
||||||
{
|
|
||||||
char *str;
|
|
||||||
int len;
|
|
||||||
|
|
||||||
len = snprintf(NULL, 0, "%ld", i);
|
|
||||||
str = Malloc(len + 1);
|
|
||||||
if (!str)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
snprintf(str, len + 1, "%ld", i);
|
|
||||||
|
|
||||||
return str;
|
|
||||||
}
|
|
||||||
|
|
||||||
char *
|
|
||||||
StrLower(char *str)
|
|
||||||
{
|
|
||||||
char *ret;
|
|
||||||
|
|
||||||
size_t len;
|
|
||||||
size_t i;
|
|
||||||
|
|
||||||
if (!str)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
len = strlen(str);
|
|
||||||
ret = Malloc(len + 1);
|
|
||||||
|
|
||||||
for (i = 0; i < len; i++)
|
|
||||||
{
|
|
||||||
ret[i] = tolower(str[i]);
|
|
||||||
}
|
|
||||||
|
|
||||||
ret[len] = '\0';
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
|
||||||
StrEquals(const char *str1, const char *str2)
|
|
||||||
{
|
|
||||||
/* Both strings are NULL, they're equal */
|
|
||||||
if (!str1 && !str2)
|
|
||||||
{
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* One or the other is NULL, they're not equal */
|
|
||||||
if (!str1 || !str2)
|
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Neither are NULL, do a regular string comparison */
|
|
||||||
return strcmp(str1, str2) == 0;
|
|
||||||
}
|
|
|
@ -1,658 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright (C) 2022-2023 Jordan Bancino <@jordan:bancino.net>
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person
|
|
||||||
* obtaining a copy of this software and associated documentation files
|
|
||||||
* (the "Software"), to deal in the Software without restriction,
|
|
||||||
* including without limitation the rights to use, copy, modify, merge,
|
|
||||||
* publish, distribute, sublicense, and/or sell copies of the Software,
|
|
||||||
* and to permit persons to whom the Software is furnished to do so,
|
|
||||||
* subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be
|
|
||||||
* included in all copies or portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
||||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
||||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
||||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
||||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
||||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
||||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
||||||
* SOFTWARE.
|
|
||||||
*/
|
|
||||||
#include <Stream.h>
|
|
||||||
|
|
||||||
#include <Io.h>
|
|
||||||
#include <Memory.h>
|
|
||||||
#include <Util.h>
|
|
||||||
#include <Int.h>
|
|
||||||
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <errno.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <sys/stat.h>
|
|
||||||
|
|
||||||
#ifndef STREAM_RETRIES
|
|
||||||
#define STREAM_RETRIES 10
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifndef STREAM_DELAY
|
|
||||||
#define STREAM_DELAY 2
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#define STREAM_EOF (1 << 0)
|
|
||||||
#define STREAM_ERR (1 << 1)
|
|
||||||
#define STREAM_TTY (1 << 2)
|
|
||||||
|
|
||||||
struct Stream
|
|
||||||
{
|
|
||||||
Io *io;
|
|
||||||
|
|
||||||
UInt8 *rBuf;
|
|
||||||
size_t rLen;
|
|
||||||
size_t rOff;
|
|
||||||
|
|
||||||
UInt8 *wBuf;
|
|
||||||
size_t wLen;
|
|
||||||
|
|
||||||
char *ugBuf;
|
|
||||||
size_t ugSize;
|
|
||||||
size_t ugLen;
|
|
||||||
|
|
||||||
int flags;
|
|
||||||
|
|
||||||
int fd;
|
|
||||||
};
|
|
||||||
|
|
||||||
Stream *
|
|
||||||
StreamIo(Io * io)
|
|
||||||
{
|
|
||||||
Stream *stream;
|
|
||||||
|
|
||||||
if (!io)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
stream = Malloc(sizeof(Stream));
|
|
||||||
if (!stream)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
memset(stream, 0, sizeof(Stream));
|
|
||||||
stream->io = io;
|
|
||||||
stream->fd = -1;
|
|
||||||
|
|
||||||
return stream;
|
|
||||||
}
|
|
||||||
|
|
||||||
Stream *
|
|
||||||
StreamFd(int fd)
|
|
||||||
{
|
|
||||||
Io *io = IoFd(fd);
|
|
||||||
Stream *stream;
|
|
||||||
|
|
||||||
if (!io)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
stream = StreamIo(io);
|
|
||||||
if (!stream)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
stream->fd = fd;
|
|
||||||
|
|
||||||
if (isatty(stream->fd))
|
|
||||||
{
|
|
||||||
stream->flags |= STREAM_TTY;
|
|
||||||
}
|
|
||||||
|
|
||||||
return stream;
|
|
||||||
}
|
|
||||||
|
|
||||||
Stream *
|
|
||||||
StreamFile(FILE * fp)
|
|
||||||
{
|
|
||||||
Io *io = IoFile(fp);
|
|
||||||
Stream *stream;
|
|
||||||
|
|
||||||
if (!io)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
stream = StreamIo(io);
|
|
||||||
if (!stream)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
stream->fd = fileno(fp);
|
|
||||||
|
|
||||||
if (isatty(stream->fd))
|
|
||||||
{
|
|
||||||
stream->flags |= STREAM_TTY;
|
|
||||||
}
|
|
||||||
|
|
||||||
return stream;
|
|
||||||
}
|
|
||||||
|
|
||||||
Stream *
|
|
||||||
StreamOpen(const char *path, const char *mode)
|
|
||||||
{
|
|
||||||
FILE *fp = fopen(path, mode);
|
|
||||||
|
|
||||||
if (!fp)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
return StreamFile(fp);
|
|
||||||
}
|
|
||||||
|
|
||||||
Stream *
|
|
||||||
StreamStdout(void)
|
|
||||||
{
|
|
||||||
static Stream *stdOut = NULL;
|
|
||||||
|
|
||||||
if (!stdOut)
|
|
||||||
{
|
|
||||||
stdOut = StreamFd(STDOUT_FILENO);
|
|
||||||
}
|
|
||||||
|
|
||||||
return stdOut;
|
|
||||||
}
|
|
||||||
|
|
||||||
Stream *
|
|
||||||
StreamStderr(void)
|
|
||||||
{
|
|
||||||
static Stream *stdErr = NULL;
|
|
||||||
|
|
||||||
if (!stdErr)
|
|
||||||
{
|
|
||||||
stdErr = StreamFd(STDERR_FILENO);
|
|
||||||
}
|
|
||||||
|
|
||||||
return stdErr;
|
|
||||||
}
|
|
||||||
|
|
||||||
Stream *
|
|
||||||
StreamStdin(void)
|
|
||||||
{
|
|
||||||
static Stream *stdIn = NULL;
|
|
||||||
|
|
||||||
if (!stdIn)
|
|
||||||
{
|
|
||||||
stdIn = StreamFd(STDIN_FILENO);
|
|
||||||
}
|
|
||||||
|
|
||||||
return stdIn;
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
|
||||||
StreamClose(Stream * stream)
|
|
||||||
{
|
|
||||||
int ret = 0;
|
|
||||||
|
|
||||||
if (!stream)
|
|
||||||
{
|
|
||||||
errno = EBADF;
|
|
||||||
return EOF;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (stream->rBuf)
|
|
||||||
{
|
|
||||||
Free(stream->rBuf);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (stream->wBuf)
|
|
||||||
{
|
|
||||||
ssize_t writeRes = IoWrite(stream->io, stream->wBuf, stream->wLen);
|
|
||||||
|
|
||||||
Free(stream->wBuf);
|
|
||||||
|
|
||||||
if (writeRes == -1)
|
|
||||||
{
|
|
||||||
ret = EOF;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (stream->ugBuf)
|
|
||||||
{
|
|
||||||
Free(stream->ugBuf);
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = IoClose(stream->io);
|
|
||||||
Free(stream);
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
|
||||||
StreamVprintf(Stream * stream, const char *fmt, va_list ap)
|
|
||||||
{
|
|
||||||
/* This might look like very similar code to IoVprintf(), but I
|
|
||||||
* chose not to defer to IoVprintf() because that would require us
|
|
||||||
* to immediately flush the buffer, since the Io API is unbuffered.
|
|
||||||
* StreamPuts() uses StreamPutc() under the hood, which is
|
|
||||||
* buffered. It therefore allows us to finish filling the buffer
|
|
||||||
* and then only flush it when necessary, preventing superfluous
|
|
||||||
* writes. */
|
|
||||||
|
|
||||||
char *buf = NULL;
|
|
||||||
size_t bufSize = 0;
|
|
||||||
FILE *fp;
|
|
||||||
|
|
||||||
int ret;
|
|
||||||
|
|
||||||
if (!fmt)
|
|
||||||
{
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
fp = open_memstream(&buf, &bufSize);
|
|
||||||
if (!fp)
|
|
||||||
{
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = vfprintf(fp, fmt, ap);
|
|
||||||
fclose(fp);
|
|
||||||
|
|
||||||
if (ret >= 0 && stream)
|
|
||||||
{
|
|
||||||
if (StreamPuts(stream, buf) < 0)
|
|
||||||
{
|
|
||||||
ret = -1;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
free(buf); /* Allocated by stdlib, not Memory
|
|
||||||
* API */
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
|
||||||
StreamPrintf(Stream * stream, const char *fmt,...)
|
|
||||||
{
|
|
||||||
int ret;
|
|
||||||
va_list ap;
|
|
||||||
|
|
||||||
va_start(ap, fmt);
|
|
||||||
ret = StreamVprintf(stream, fmt, ap);
|
|
||||||
va_end(ap);
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
|
||||||
StreamGetc(Stream * stream)
|
|
||||||
{
|
|
||||||
int c;
|
|
||||||
|
|
||||||
if (!stream)
|
|
||||||
{
|
|
||||||
errno = EBADF;
|
|
||||||
return EOF;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Empty the ungetc stack first */
|
|
||||||
if (stream->ugLen)
|
|
||||||
{
|
|
||||||
c = stream->ugBuf[stream->ugLen - 1];
|
|
||||||
stream->ugLen--;
|
|
||||||
return c;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (stream->flags & STREAM_EOF)
|
|
||||||
{
|
|
||||||
return EOF;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!stream->rBuf)
|
|
||||||
{
|
|
||||||
/* No buffer allocated yet */
|
|
||||||
stream->rBuf = Malloc(IO_BUFFER);
|
|
||||||
if (!stream->rBuf)
|
|
||||||
{
|
|
||||||
stream->flags |= STREAM_ERR;
|
|
||||||
return EOF;
|
|
||||||
}
|
|
||||||
|
|
||||||
stream->rOff = 0;
|
|
||||||
stream->rLen = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (stream->rOff >= stream->rLen)
|
|
||||||
{
|
|
||||||
/* We read through the entire buffer; get a new one */
|
|
||||||
ssize_t readRes = IoRead(stream->io, stream->rBuf, IO_BUFFER);
|
|
||||||
|
|
||||||
if (readRes == 0)
|
|
||||||
{
|
|
||||||
stream->flags |= STREAM_EOF;
|
|
||||||
return EOF;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (readRes == -1)
|
|
||||||
{
|
|
||||||
stream->flags |= STREAM_ERR;
|
|
||||||
return EOF;
|
|
||||||
}
|
|
||||||
|
|
||||||
stream->rOff = 0;
|
|
||||||
stream->rLen = readRes;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Read the character in the buffer and advance the offset */
|
|
||||||
c = stream->rBuf[stream->rOff];
|
|
||||||
stream->rOff++;
|
|
||||||
|
|
||||||
return c;
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
|
||||||
StreamUngetc(Stream * stream, int c)
|
|
||||||
{
|
|
||||||
if (!stream)
|
|
||||||
{
|
|
||||||
errno = EBADF;
|
|
||||||
return EOF;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!stream->ugBuf)
|
|
||||||
{
|
|
||||||
stream->ugSize = IO_BUFFER;
|
|
||||||
stream->ugBuf = Malloc(stream->ugSize);
|
|
||||||
|
|
||||||
if (!stream->ugBuf)
|
|
||||||
{
|
|
||||||
stream->flags |= STREAM_ERR;
|
|
||||||
return EOF;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (stream->ugLen >= stream->ugSize)
|
|
||||||
{
|
|
||||||
char *new;
|
|
||||||
|
|
||||||
stream->ugSize += IO_BUFFER;
|
|
||||||
new = Realloc(stream->ugBuf, stream->ugSize);
|
|
||||||
if (!new)
|
|
||||||
{
|
|
||||||
stream->flags |= STREAM_ERR;
|
|
||||||
Free(stream->ugBuf);
|
|
||||||
stream->ugBuf = NULL;
|
|
||||||
return EOF;
|
|
||||||
}
|
|
||||||
|
|
||||||
Free(stream->ugBuf);
|
|
||||||
stream->ugBuf = new;
|
|
||||||
}
|
|
||||||
|
|
||||||
stream->ugBuf[stream->ugLen] = c;
|
|
||||||
stream->ugLen++;
|
|
||||||
|
|
||||||
return c;
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
|
||||||
StreamPutc(Stream * stream, int c)
|
|
||||||
{
|
|
||||||
if (!stream)
|
|
||||||
{
|
|
||||||
errno = EBADF;
|
|
||||||
return EOF;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!stream->wBuf)
|
|
||||||
{
|
|
||||||
stream->wBuf = Malloc(IO_BUFFER);
|
|
||||||
if (!stream->wBuf)
|
|
||||||
{
|
|
||||||
stream->flags |= STREAM_ERR;
|
|
||||||
return EOF;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (stream->wLen == IO_BUFFER)
|
|
||||||
{
|
|
||||||
/* Buffer full; write it */
|
|
||||||
ssize_t writeRes = IoWrite(stream->io, stream->wBuf, stream->wLen);
|
|
||||||
|
|
||||||
if (writeRes == -1)
|
|
||||||
{
|
|
||||||
stream->flags |= STREAM_ERR;
|
|
||||||
return EOF;
|
|
||||||
}
|
|
||||||
|
|
||||||
stream->wLen = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
stream->wBuf[stream->wLen] = c;
|
|
||||||
stream->wLen++;
|
|
||||||
|
|
||||||
if (stream->flags & STREAM_TTY && c == '\n')
|
|
||||||
{
|
|
||||||
/* Newline encountered on a TTY; write now. This fixes some
|
|
||||||
* strange behavior on certain TTYs where a newline is written
|
|
||||||
* to the screen upon flush even when no newline exists in the
|
|
||||||
* stream. We just flush on newlines, but only if we're
|
|
||||||
* directly writing to a TTY. */
|
|
||||||
ssize_t writeRes = IoWrite(stream->io, stream->wBuf, stream->wLen);
|
|
||||||
|
|
||||||
if (writeRes == -1)
|
|
||||||
{
|
|
||||||
stream->flags |= STREAM_ERR;
|
|
||||||
return EOF;
|
|
||||||
}
|
|
||||||
|
|
||||||
stream->wLen = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
return c;
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
|
||||||
StreamPuts(Stream * stream, char *str)
|
|
||||||
{
|
|
||||||
int ret = 0;
|
|
||||||
|
|
||||||
if (!stream)
|
|
||||||
{
|
|
||||||
errno = EBADF;
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
while (*str)
|
|
||||||
{
|
|
||||||
if (StreamPutc(stream, *str) == EOF)
|
|
||||||
{
|
|
||||||
ret = -1;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
str++;
|
|
||||||
}
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
char *
|
|
||||||
StreamGets(Stream * stream, char *str, int size)
|
|
||||||
{
|
|
||||||
int i;
|
|
||||||
|
|
||||||
if (!stream)
|
|
||||||
{
|
|
||||||
errno = EBADF;
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (size <= 0)
|
|
||||||
{
|
|
||||||
errno = EINVAL;
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (i = 0; i < size - 1; i++)
|
|
||||||
{
|
|
||||||
int c = StreamGetc(stream);
|
|
||||||
|
|
||||||
if (StreamEof(stream) || StreamError(stream))
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
str[i] = c;
|
|
||||||
|
|
||||||
if (c == '\n')
|
|
||||||
{
|
|
||||||
i++;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
str[i] = '\0';
|
|
||||||
|
|
||||||
return str;
|
|
||||||
}
|
|
||||||
|
|
||||||
off_t
|
|
||||||
StreamSeek(Stream * stream, off_t offset, int whence)
|
|
||||||
{
|
|
||||||
off_t result;
|
|
||||||
|
|
||||||
if (!stream)
|
|
||||||
{
|
|
||||||
errno = EBADF;
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
result = IoSeek(stream->io, offset, whence);
|
|
||||||
if (result < 0)
|
|
||||||
{
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Successful seek; clear the buffers */
|
|
||||||
stream->rOff = 0;
|
|
||||||
stream->wLen = 0;
|
|
||||||
stream->ugLen = 0;
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
|
||||||
StreamEof(Stream * stream)
|
|
||||||
{
|
|
||||||
return stream && (stream->flags & STREAM_EOF);
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
|
||||||
StreamError(Stream * stream)
|
|
||||||
{
|
|
||||||
return stream && (stream->flags & STREAM_ERR);
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
StreamClearError(Stream * stream)
|
|
||||||
{
|
|
||||||
if (stream)
|
|
||||||
{
|
|
||||||
stream->flags &= ~STREAM_ERR;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
|
||||||
StreamFlush(Stream * stream)
|
|
||||||
{
|
|
||||||
if (!stream)
|
|
||||||
{
|
|
||||||
errno = EBADF;
|
|
||||||
return EOF;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (stream->wLen)
|
|
||||||
{
|
|
||||||
ssize_t writeRes = IoWrite(stream->io, stream->wBuf, stream->wLen);
|
|
||||||
|
|
||||||
if (writeRes == -1)
|
|
||||||
{
|
|
||||||
stream->flags |= STREAM_ERR;
|
|
||||||
return EOF;
|
|
||||||
}
|
|
||||||
|
|
||||||
stream->wLen = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
ssize_t
|
|
||||||
StreamCopy(Stream * in, Stream * out)
|
|
||||||
{
|
|
||||||
ssize_t nBytes = 0;
|
|
||||||
int c;
|
|
||||||
int tries = 0;
|
|
||||||
int readFlg = 0;
|
|
||||||
|
|
||||||
while (1)
|
|
||||||
{
|
|
||||||
c = StreamGetc(in);
|
|
||||||
|
|
||||||
if (StreamEof(in))
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (StreamError(in))
|
|
||||||
{
|
|
||||||
if (errno == EAGAIN)
|
|
||||||
{
|
|
||||||
StreamClearError(in);
|
|
||||||
tries++;
|
|
||||||
|
|
||||||
if (tries >= STREAM_RETRIES || readFlg)
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
UtilSleepMillis(UInt64Create(0, STREAM_DELAY));
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* As soon as we've successfully read a byte, treat future
|
|
||||||
* EAGAINs as EOF, because somebody might have forgotten to
|
|
||||||
* close their stream. */
|
|
||||||
|
|
||||||
readFlg = 1;
|
|
||||||
tries = 0;
|
|
||||||
|
|
||||||
StreamPutc(out, c);
|
|
||||||
nBytes++;
|
|
||||||
}
|
|
||||||
|
|
||||||
StreamFlush(out);
|
|
||||||
return nBytes;
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
|
||||||
StreamFileno(Stream * stream)
|
|
||||||
{
|
|
||||||
return stream ? stream->fd : -1;
|
|
||||||
}
|
|
|
@ -1,247 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright (C) 2022-2023 Jordan Bancino <@jordan:bancino.net>
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person
|
|
||||||
* obtaining a copy of this software and associated documentation files
|
|
||||||
* (the "Software"), to deal in the Software without restriction,
|
|
||||||
* including without limitation the rights to use, copy, modify, merge,
|
|
||||||
* publish, distribute, sublicense, and/or sell copies of the Software,
|
|
||||||
* and to permit persons to whom the Software is furnished to do so,
|
|
||||||
* subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be
|
|
||||||
* included in all copies or portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
||||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
||||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
||||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
||||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
||||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
||||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
||||||
* SOFTWARE.
|
|
||||||
*/
|
|
||||||
#include <Tls.h>
|
|
||||||
|
|
||||||
#if TLS_IMPL == TLS_LIBRESSL
|
|
||||||
|
|
||||||
#include <Memory.h>
|
|
||||||
#include <Log.h>
|
|
||||||
|
|
||||||
#include <errno.h>
|
|
||||||
|
|
||||||
#include <tls.h> /* LibreSSL TLS */
|
|
||||||
|
|
||||||
typedef struct LibreSSLCookie
|
|
||||||
{
|
|
||||||
int fd;
|
|
||||||
struct tls *ctx;
|
|
||||||
struct tls *cctx;
|
|
||||||
struct tls_config *cfg;
|
|
||||||
} LibreSSLCookie;
|
|
||||||
|
|
||||||
void *
|
|
||||||
TlsInitClient(int fd, const char *serverName)
|
|
||||||
{
|
|
||||||
LibreSSLCookie *cookie = Malloc(sizeof(LibreSSLCookie));
|
|
||||||
|
|
||||||
if (!cookie)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
cookie->ctx = tls_client();
|
|
||||||
cookie->cctx = NULL;
|
|
||||||
cookie->cfg = tls_config_new();
|
|
||||||
cookie->fd = fd;
|
|
||||||
|
|
||||||
|
|
||||||
if (!cookie->ctx || !cookie->cfg)
|
|
||||||
{
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (tls_config_set_ca_file(cookie->cfg, tls_default_ca_cert_file()) == -1)
|
|
||||||
{
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (tls_configure(cookie->ctx, cookie->cfg) == -1)
|
|
||||||
{
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (tls_connect_socket(cookie->ctx, fd, serverName) == -1)
|
|
||||||
{
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (tls_handshake(cookie->ctx) == -1)
|
|
||||||
{
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
|
|
||||||
return cookie;
|
|
||||||
|
|
||||||
error:
|
|
||||||
if (cookie->ctx)
|
|
||||||
{
|
|
||||||
if (tls_error(cookie->ctx))
|
|
||||||
{
|
|
||||||
Log(LOG_ERR, "TlsInitClient(): %s", tls_error(cookie->ctx));
|
|
||||||
}
|
|
||||||
|
|
||||||
tls_free(cookie->ctx);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (cookie->cfg)
|
|
||||||
{
|
|
||||||
tls_config_free(cookie->cfg);
|
|
||||||
}
|
|
||||||
|
|
||||||
Free(cookie);
|
|
||||||
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
void *
|
|
||||||
TlsInitServer(int fd, const char *crt, const char *key)
|
|
||||||
{
|
|
||||||
LibreSSLCookie *cookie = Malloc(sizeof(LibreSSLCookie));
|
|
||||||
|
|
||||||
if (!cookie)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
cookie->ctx = tls_server();
|
|
||||||
cookie->cctx = NULL;
|
|
||||||
cookie->cfg = tls_config_new();
|
|
||||||
cookie->fd = fd;
|
|
||||||
|
|
||||||
if (!cookie->ctx || !cookie->cfg)
|
|
||||||
{
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (tls_config_set_cert_file(cookie->cfg, crt) == -1)
|
|
||||||
{
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (tls_config_set_key_file(cookie->cfg, key) == -1)
|
|
||||||
{
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (tls_configure(cookie->ctx, cookie->cfg) == -1)
|
|
||||||
{
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (tls_accept_fds(cookie->ctx, &cookie->cctx, fd, fd) == -1)
|
|
||||||
{
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (tls_handshake(cookie->cctx) == -1)
|
|
||||||
{
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
|
|
||||||
return cookie;
|
|
||||||
|
|
||||||
error:
|
|
||||||
if (cookie->ctx)
|
|
||||||
{
|
|
||||||
if (tls_error(cookie->ctx))
|
|
||||||
{
|
|
||||||
Log(LOG_ERR, "TlsInitServer(): %s", tls_error(cookie->ctx));
|
|
||||||
}
|
|
||||||
tls_free(cookie->ctx);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (cookie->cctx)
|
|
||||||
{
|
|
||||||
if (tls_error(cookie->cctx))
|
|
||||||
{
|
|
||||||
Log(LOG_ERR, "TlsInitServer(): %s", tls_error(cookie->cctx));
|
|
||||||
}
|
|
||||||
|
|
||||||
tls_free(cookie->cctx);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (cookie->cfg)
|
|
||||||
{
|
|
||||||
tls_config_free(cookie->cfg);
|
|
||||||
}
|
|
||||||
|
|
||||||
Free(cookie);
|
|
||||||
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
ssize_t
|
|
||||||
TlsRead(void *cookie, void *buf, size_t nBytes)
|
|
||||||
{
|
|
||||||
LibreSSLCookie *tls = cookie;
|
|
||||||
struct tls *ctx = tls->cctx ? tls->cctx : tls->ctx;
|
|
||||||
ssize_t ret = tls_read(ctx, buf, nBytes);
|
|
||||||
|
|
||||||
if (ret == -1)
|
|
||||||
{
|
|
||||||
errno = EIO;
|
|
||||||
}
|
|
||||||
else if (ret == TLS_WANT_POLLIN || ret == TLS_WANT_POLLOUT)
|
|
||||||
{
|
|
||||||
errno = EAGAIN;
|
|
||||||
ret = -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
ssize_t
|
|
||||||
TlsWrite(void *cookie, void *buf, size_t nBytes)
|
|
||||||
{
|
|
||||||
LibreSSLCookie *tls = cookie;
|
|
||||||
struct tls *ctx = tls->cctx ? tls->cctx : tls->ctx;
|
|
||||||
ssize_t ret = tls_write(ctx, buf, nBytes);
|
|
||||||
|
|
||||||
if (ret == -1)
|
|
||||||
{
|
|
||||||
errno = EIO;
|
|
||||||
}
|
|
||||||
else if (ret == TLS_WANT_POLLIN || ret == TLS_WANT_POLLOUT)
|
|
||||||
{
|
|
||||||
errno = EAGAIN;
|
|
||||||
ret = -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
|
||||||
TlsClose(void *cookie)
|
|
||||||
{
|
|
||||||
LibreSSLCookie *tls = cookie;
|
|
||||||
|
|
||||||
int tlsRet = tls_close(tls->cctx ? tls->cctx : tls->ctx);
|
|
||||||
int sdRet;
|
|
||||||
|
|
||||||
if (tls->cctx)
|
|
||||||
{
|
|
||||||
tls_free(tls->cctx);
|
|
||||||
}
|
|
||||||
|
|
||||||
tls_free(tls->ctx);
|
|
||||||
tls_config_free(tls->cfg);
|
|
||||||
|
|
||||||
sdRet = close(tls->fd);
|
|
||||||
|
|
||||||
Free(tls);
|
|
||||||
|
|
||||||
return (tlsRet == -1 || sdRet == -1) ? -1 : 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
|
|
@ -1,307 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright (C) 2022-2023 Jordan Bancino <@jordan:bancino.net>
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person
|
|
||||||
* obtaining a copy of this software and associated documentation files
|
|
||||||
* (the "Software"), to deal in the Software without restriction,
|
|
||||||
* including without limitation the rights to use, copy, modify, merge,
|
|
||||||
* publish, distribute, sublicense, and/or sell copies of the Software,
|
|
||||||
* and to permit persons to whom the Software is furnished to do so,
|
|
||||||
* subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be
|
|
||||||
* included in all copies or portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
||||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
||||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
||||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
||||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
||||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
||||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
||||||
* SOFTWARE.
|
|
||||||
*/
|
|
||||||
#include <Tls.h>
|
|
||||||
|
|
||||||
#if TLS_IMPL == TLS_OPENSSL
|
|
||||||
|
|
||||||
#include <Memory.h>
|
|
||||||
#include <Log.h>
|
|
||||||
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
#include <openssl/ssl.h>
|
|
||||||
#include <openssl/err.h>
|
|
||||||
|
|
||||||
typedef struct OpenSSLCookie
|
|
||||||
{
|
|
||||||
int fd;
|
|
||||||
const SSL_METHOD *method;
|
|
||||||
SSL_CTX *ctx;
|
|
||||||
SSL *ssl;
|
|
||||||
} OpenSSLCookie;
|
|
||||||
|
|
||||||
static char *
|
|
||||||
SSLErrorString(int err)
|
|
||||||
{
|
|
||||||
switch (err)
|
|
||||||
{
|
|
||||||
case SSL_ERROR_NONE:
|
|
||||||
return "No error.";
|
|
||||||
case SSL_ERROR_ZERO_RETURN:
|
|
||||||
return "The TLS/SSL connection has been closed.";
|
|
||||||
case SSL_ERROR_WANT_READ:
|
|
||||||
case SSL_ERROR_WANT_WRITE:
|
|
||||||
case SSL_ERROR_WANT_CONNECT:
|
|
||||||
case SSL_ERROR_WANT_ACCEPT:
|
|
||||||
return "The operation did not complete.";
|
|
||||||
case SSL_ERROR_WANT_X509_LOOKUP:
|
|
||||||
return "X509 lookup failed.";
|
|
||||||
case SSL_ERROR_SYSCALL:
|
|
||||||
return "I/O Error.";
|
|
||||||
case SSL_ERROR_SSL:
|
|
||||||
return "SSL library error.";
|
|
||||||
}
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
void *
|
|
||||||
TlsInitClient(int fd, const char *serverName)
|
|
||||||
{
|
|
||||||
OpenSSLCookie *cookie;
|
|
||||||
char errorStr[256];
|
|
||||||
|
|
||||||
/*
|
|
||||||
* TODO: Seems odd that this isn't needed to make the
|
|
||||||
* connection... we should figure out how to verify the
|
|
||||||
* certificate matches the server we think we're
|
|
||||||
* connecting to.
|
|
||||||
*/
|
|
||||||
(void) serverName;
|
|
||||||
|
|
||||||
cookie = Malloc(sizeof(OpenSSLCookie));
|
|
||||||
if (!cookie)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
memset(cookie, 0, sizeof(OpenSSLCookie));
|
|
||||||
|
|
||||||
cookie->method = TLS_client_method();
|
|
||||||
cookie->ctx = SSL_CTX_new(cookie->method);
|
|
||||||
if (!cookie->ctx)
|
|
||||||
{
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
|
|
||||||
cookie->ssl = SSL_new(cookie->ctx);
|
|
||||||
if (!cookie->ssl)
|
|
||||||
{
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!SSL_set_fd(cookie->ssl, fd))
|
|
||||||
{
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (SSL_connect(cookie->ssl) <= 0)
|
|
||||||
{
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
|
|
||||||
return cookie;
|
|
||||||
|
|
||||||
error:
|
|
||||||
Log(LOG_ERR, "TlsClientInit(): %s", ERR_error_string(ERR_get_error(), errorStr));
|
|
||||||
|
|
||||||
if (cookie->ssl)
|
|
||||||
{
|
|
||||||
SSL_shutdown(cookie->ssl);
|
|
||||||
SSL_free(cookie->ssl);
|
|
||||||
}
|
|
||||||
|
|
||||||
close(cookie->fd);
|
|
||||||
|
|
||||||
if (cookie->ctx)
|
|
||||||
{
|
|
||||||
SSL_CTX_free(cookie->ctx);
|
|
||||||
}
|
|
||||||
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
void *
|
|
||||||
TlsInitServer(int fd, const char *crt, const char *key)
|
|
||||||
{
|
|
||||||
OpenSSLCookie *cookie;
|
|
||||||
char errorStr[256];
|
|
||||||
int acceptRet = 0;
|
|
||||||
|
|
||||||
cookie = Malloc(sizeof(OpenSSLCookie));
|
|
||||||
if (!cookie)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
memset(cookie, 0, sizeof(OpenSSLCookie));
|
|
||||||
|
|
||||||
cookie->method = TLS_server_method();
|
|
||||||
cookie->ctx = SSL_CTX_new(cookie->method);
|
|
||||||
if (!cookie->ctx)
|
|
||||||
{
|
|
||||||
Log(LOG_ERR, "TlsInitServer(): Unable to create SSL Context.");
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (SSL_CTX_use_certificate_file(cookie->ctx, crt, SSL_FILETYPE_PEM) <= 0)
|
|
||||||
{
|
|
||||||
Log(LOG_ERR, "TlsInitServer(): Unable to set certificate file: %s", crt);
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (SSL_CTX_use_PrivateKey_file(cookie->ctx, key, SSL_FILETYPE_PEM) <= 0)
|
|
||||||
{
|
|
||||||
Log(LOG_ERR, "TlsInitServer(): Unable to set key file: %s", key);
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
|
|
||||||
cookie->ssl = SSL_new(cookie->ctx);
|
|
||||||
if (!cookie->ssl)
|
|
||||||
{
|
|
||||||
Log(LOG_ERR, "TlsInitServer(): Unable to create SSL object.");
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!SSL_set_fd(cookie->ssl, fd))
|
|
||||||
{
|
|
||||||
Log(LOG_ERR, "TlsInitServer(): Unable to set file descriptor.");
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
|
|
||||||
while ((acceptRet = SSL_accept(cookie->ssl)) <= 0)
|
|
||||||
{
|
|
||||||
switch (SSL_get_error(cookie->ssl, acceptRet))
|
|
||||||
{
|
|
||||||
case SSL_ERROR_WANT_READ:
|
|
||||||
case SSL_ERROR_WANT_WRITE:
|
|
||||||
case SSL_ERROR_WANT_CONNECT:
|
|
||||||
case SSL_ERROR_WANT_ACCEPT:
|
|
||||||
continue;
|
|
||||||
default:
|
|
||||||
Log(LOG_ERR, "TlsInitServer(): Unable to accept connection.");
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return cookie;
|
|
||||||
|
|
||||||
error:
|
|
||||||
if (SSL_get_error(cookie->ssl, acceptRet) == SSL_ERROR_SYSCALL)
|
|
||||||
{
|
|
||||||
Log(LOG_ERR, "TlsServerInit(): System error: %s", strerror(errno));
|
|
||||||
}
|
|
||||||
Log(LOG_ERR, "TlsServerInit(): %s", SSLErrorString(SSL_get_error(cookie->ssl, acceptRet)));
|
|
||||||
Log(LOG_ERR, "TlsServerInit(): %s", ERR_error_string(ERR_get_error(), errorStr));
|
|
||||||
|
|
||||||
if (cookie->ssl)
|
|
||||||
{
|
|
||||||
SSL_shutdown(cookie->ssl);
|
|
||||||
SSL_free(cookie->ssl);
|
|
||||||
}
|
|
||||||
|
|
||||||
close(cookie->fd);
|
|
||||||
|
|
||||||
if (cookie->ctx)
|
|
||||||
{
|
|
||||||
SSL_CTX_free(cookie->ctx);
|
|
||||||
}
|
|
||||||
|
|
||||||
Free(cookie);
|
|
||||||
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
ssize_t
|
|
||||||
TlsRead(void *cookie, void *buf, size_t nBytes)
|
|
||||||
{
|
|
||||||
OpenSSLCookie *ssl = cookie;
|
|
||||||
int ret;
|
|
||||||
|
|
||||||
ret = SSL_read(ssl->ssl, buf, nBytes);
|
|
||||||
|
|
||||||
if (ret <= 0)
|
|
||||||
{
|
|
||||||
switch (SSL_get_error(ssl->ssl, ret))
|
|
||||||
{
|
|
||||||
case SSL_ERROR_WANT_READ:
|
|
||||||
case SSL_ERROR_WANT_WRITE:
|
|
||||||
case SSL_ERROR_WANT_CONNECT:
|
|
||||||
case SSL_ERROR_WANT_ACCEPT:
|
|
||||||
case SSL_ERROR_WANT_X509_LOOKUP:
|
|
||||||
errno = EAGAIN;
|
|
||||||
break;
|
|
||||||
case SSL_ERROR_ZERO_RETURN:
|
|
||||||
ret = 0;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
errno = EIO;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
ret = -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
ssize_t
|
|
||||||
TlsWrite(void *cookie, void *buf, size_t nBytes)
|
|
||||||
{
|
|
||||||
OpenSSLCookie *ssl = cookie;
|
|
||||||
int ret;
|
|
||||||
|
|
||||||
ret = SSL_write(ssl->ssl, buf, nBytes);
|
|
||||||
|
|
||||||
if (ret <= 0)
|
|
||||||
{
|
|
||||||
switch (SSL_get_error(ssl->ssl, ret))
|
|
||||||
{
|
|
||||||
case SSL_ERROR_WANT_READ:
|
|
||||||
case SSL_ERROR_WANT_WRITE:
|
|
||||||
case SSL_ERROR_WANT_CONNECT:
|
|
||||||
case SSL_ERROR_WANT_ACCEPT:
|
|
||||||
case SSL_ERROR_WANT_X509_LOOKUP:
|
|
||||||
errno = EAGAIN;
|
|
||||||
break;
|
|
||||||
case SSL_ERROR_ZERO_RETURN:
|
|
||||||
ret = 0;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
errno = EIO;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
ret = -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
|
||||||
TlsClose(void *cookie)
|
|
||||||
{
|
|
||||||
OpenSSLCookie *ssl = cookie;
|
|
||||||
|
|
||||||
while (SSL_shutdown(ssl->ssl) == 0);
|
|
||||||
SSL_free(ssl->ssl);
|
|
||||||
SSL_CTX_free(ssl->ctx);
|
|
||||||
|
|
||||||
#if 0
|
|
||||||
close(ssl->fd);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
Free(ssl);
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
|
|
@ -1,265 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright (C) 2022-2023 Jordan Bancino <@jordan:bancino.net>
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person
|
|
||||||
* obtaining a copy of this software and associated documentation files
|
|
||||||
* (the "Software"), to deal in the Software without restriction,
|
|
||||||
* including without limitation the rights to use, copy, modify, merge,
|
|
||||||
* publish, distribute, sublicense, and/or sell copies of the Software,
|
|
||||||
* and to permit persons to whom the Software is furnished to do so,
|
|
||||||
* subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be
|
|
||||||
* included in all copies or portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
||||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
||||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
||||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
||||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
||||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
||||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
||||||
* SOFTWARE.
|
|
||||||
*/
|
|
||||||
#include <UInt64.h>
|
|
||||||
|
|
||||||
#include <stddef.h>
|
|
||||||
#include <signal.h>
|
|
||||||
|
|
||||||
size_t
|
|
||||||
UInt64Str(UInt64 x, int base, char *out, size_t len)
|
|
||||||
{
|
|
||||||
static const char symbols[] = "0123456789ABCDEF";
|
|
||||||
|
|
||||||
size_t i = len - 1;
|
|
||||||
size_t j = 0;
|
|
||||||
|
|
||||||
UInt64 base64 = UInt64Create(0, base);
|
|
||||||
|
|
||||||
/* We only have symbols up to base 16 */
|
|
||||||
if (base < 2 || base > 16)
|
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
do
|
|
||||||
{
|
|
||||||
UInt64 mod = UInt64Rem(x, base64);
|
|
||||||
UInt32 low = UInt64Low(mod);
|
|
||||||
|
|
||||||
out[i] = symbols[low];
|
|
||||||
i--;
|
|
||||||
x = UInt64Div(x, base64);
|
|
||||||
} while (UInt64Gt(x, UInt64Create(0, 0)));
|
|
||||||
|
|
||||||
while (++i < len)
|
|
||||||
{
|
|
||||||
out[j++] = out[i];
|
|
||||||
}
|
|
||||||
|
|
||||||
out[j] = '\0';
|
|
||||||
|
|
||||||
return j;
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifndef UINT64_NATIVE
|
|
||||||
|
|
||||||
/* No native 64-bit support, add our own */
|
|
||||||
|
|
||||||
UInt64
|
|
||||||
UInt64Create(UInt32 high, UInt32 low)
|
|
||||||
{
|
|
||||||
UInt64 x;
|
|
||||||
|
|
||||||
x.i[0] = low;
|
|
||||||
x.i[1] = high;
|
|
||||||
|
|
||||||
return x;
|
|
||||||
}
|
|
||||||
|
|
||||||
UInt64
|
|
||||||
UInt64Add(UInt64 x, UInt64 y)
|
|
||||||
{
|
|
||||||
UInt64 z = UInt64Create(0, 0);
|
|
||||||
int carry;
|
|
||||||
|
|
||||||
z.i[0] = x.i[0] + y.i[0];
|
|
||||||
carry = z.i[0] < x.i[0];
|
|
||||||
z.i[1] = x.i[1] + y.i[1] + carry;
|
|
||||||
|
|
||||||
return z;
|
|
||||||
}
|
|
||||||
|
|
||||||
UInt64
|
|
||||||
UInt64Sub(UInt64 x, UInt64 y)
|
|
||||||
{
|
|
||||||
UInt64 twosCompl = UInt64Add(UInt64Not(y), UInt64Create(0, 1));
|
|
||||||
|
|
||||||
return UInt64Add(x, twosCompl);
|
|
||||||
}
|
|
||||||
|
|
||||||
UInt64
|
|
||||||
UInt64Mul(UInt64 x, UInt64 y)
|
|
||||||
{
|
|
||||||
UInt64 z = UInt64Create(0, 0);
|
|
||||||
|
|
||||||
/* while (y > 0) */
|
|
||||||
while (UInt64Gt(y, UInt64Create(0, 0)))
|
|
||||||
{
|
|
||||||
/* if (y & 1 != 0) */
|
|
||||||
if (UInt64Neq(UInt64And(y, UInt64Create(0, 1)), UInt64Create(0, 0)))
|
|
||||||
{
|
|
||||||
z = UInt64Add(z, x);
|
|
||||||
}
|
|
||||||
|
|
||||||
x = UInt64Sll(x, 1);
|
|
||||||
y = UInt64Srl(y, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
return z;
|
|
||||||
}
|
|
||||||
|
|
||||||
typedef struct
|
|
||||||
{
|
|
||||||
UInt64 q;
|
|
||||||
UInt64 r;
|
|
||||||
} UInt64Ldiv;
|
|
||||||
|
|
||||||
static UInt64Ldiv
|
|
||||||
UInt64LongDivision(UInt64 n, UInt64 d)
|
|
||||||
{
|
|
||||||
UInt64Ldiv o;
|
|
||||||
|
|
||||||
int i;
|
|
||||||
|
|
||||||
o.q = UInt64Create(0, 0);
|
|
||||||
o.r = UInt64Create(0, 0);
|
|
||||||
|
|
||||||
if (UInt64Eq(d, UInt64Create(0, 0)))
|
|
||||||
{
|
|
||||||
raise(SIGFPE);
|
|
||||||
return o;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (i = 63; i >= 0; i--)
|
|
||||||
{
|
|
||||||
UInt64 bit = UInt64And(UInt64Srl(n, i), UInt64Create(0, 1));
|
|
||||||
|
|
||||||
o.r = UInt64Sll(o.r, 1);
|
|
||||||
o.r = UInt64Or(o.r, bit);
|
|
||||||
|
|
||||||
if (UInt64Geq(o.r, d))
|
|
||||||
{
|
|
||||||
o.r = UInt64Sub(o.r, d);
|
|
||||||
o.q = UInt64Or(o.q, UInt64Sll(UInt64Create(0, 1), i));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return o;
|
|
||||||
}
|
|
||||||
|
|
||||||
UInt64
|
|
||||||
UInt64Div(UInt64 x, UInt64 y)
|
|
||||||
{
|
|
||||||
return UInt64LongDivision(x, y).q;
|
|
||||||
}
|
|
||||||
|
|
||||||
UInt64
|
|
||||||
UInt64Rem(UInt64 x, UInt64 y)
|
|
||||||
{
|
|
||||||
return UInt64LongDivision(x, y).r;
|
|
||||||
}
|
|
||||||
|
|
||||||
UInt64
|
|
||||||
UInt64Sll(UInt64 x, int y)
|
|
||||||
{
|
|
||||||
UInt64 z;
|
|
||||||
|
|
||||||
if (!y)
|
|
||||||
{
|
|
||||||
return x;
|
|
||||||
}
|
|
||||||
|
|
||||||
z = UInt64Create(0, 0);
|
|
||||||
|
|
||||||
if (y < 32)
|
|
||||||
{
|
|
||||||
z.i[1] = (x.i[0] >> (32 - y)) | (x.i[1] << y);
|
|
||||||
z.i[0] = x.i[0] << y;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
z.i[1] = x.i[0] << (y - 32);
|
|
||||||
}
|
|
||||||
|
|
||||||
return z;
|
|
||||||
}
|
|
||||||
|
|
||||||
UInt64
|
|
||||||
UInt64Srl(UInt64 x, int y)
|
|
||||||
{
|
|
||||||
UInt64 z;
|
|
||||||
|
|
||||||
if (!y)
|
|
||||||
{
|
|
||||||
return x;
|
|
||||||
}
|
|
||||||
|
|
||||||
z = UInt64Create(0, 0);
|
|
||||||
|
|
||||||
if (y < 32)
|
|
||||||
{
|
|
||||||
z.i[0] = (x.i[1] << (32 - y)) | (x.i[0] >> y);
|
|
||||||
z.i[1] = x.i[1] >> y;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
z.i[0] = x.i[1] >> (y - 32);
|
|
||||||
}
|
|
||||||
|
|
||||||
return z;
|
|
||||||
}
|
|
||||||
|
|
||||||
UInt64
|
|
||||||
UInt64And(UInt64 x, UInt64 y)
|
|
||||||
{
|
|
||||||
return UInt64Create(x.i[1] & y.i[1], x.i[0] & y.i[0]);
|
|
||||||
}
|
|
||||||
|
|
||||||
UInt64
|
|
||||||
UInt64Or(UInt64 x, UInt64 y)
|
|
||||||
{
|
|
||||||
return UInt64Create(x.i[1] | y.i[1], x.i[0] | y.i[0]);
|
|
||||||
}
|
|
||||||
|
|
||||||
UInt64
|
|
||||||
UInt64Xor(UInt64 x, UInt64 y)
|
|
||||||
{
|
|
||||||
return UInt64Create(x.i[1] ^ y.i[1], x.i[0] ^ y.i[0]);
|
|
||||||
}
|
|
||||||
|
|
||||||
UInt64
|
|
||||||
UInt64Not(UInt64 x)
|
|
||||||
{
|
|
||||||
return UInt64Create(~(x.i[1]), ~(x.i[0]));
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
|
||||||
UInt64Eq(UInt64 x, UInt64 y)
|
|
||||||
{
|
|
||||||
return x.i[0] == y.i[0] && x.i[1] == y.i[1];
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
|
||||||
UInt64Lt(UInt64 x, UInt64 y)
|
|
||||||
{
|
|
||||||
return x.i[1] < y.i[1] || (x.i[1] == y.i[1] && x.i[0] < y.i[0]);
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
|
||||||
UInt64Gt(UInt64 x, UInt64 y)
|
|
||||||
{
|
|
||||||
return x.i[1] > y.i[1] || (x.i[1] == y.i[1] && x.i[0] > y.i[0]);
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
|
|
@ -1,339 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright (C) 2022-2023 Jordan Bancino <@jordan:bancino.net>
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person
|
|
||||||
* obtaining a copy of this software and associated documentation files
|
|
||||||
* (the "Software"), to deal in the Software without restriction,
|
|
||||||
* including without limitation the rights to use, copy, modify, merge,
|
|
||||||
* publish, distribute, sublicense, and/or sell copies of the Software,
|
|
||||||
* and to permit persons to whom the Software is furnished to do so,
|
|
||||||
* subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be
|
|
||||||
* included in all copies or portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
||||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
||||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
||||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
||||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
||||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
||||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
||||||
* SOFTWARE.
|
|
||||||
*/
|
|
||||||
#include <Util.h>
|
|
||||||
|
|
||||||
#include <Memory.h>
|
|
||||||
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <stdarg.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <ctype.h>
|
|
||||||
#include <math.h>
|
|
||||||
#include <time.h>
|
|
||||||
|
|
||||||
#include <unistd.h>
|
|
||||||
#include <errno.h>
|
|
||||||
#include <sys/time.h>
|
|
||||||
#include <sys/stat.h>
|
|
||||||
#include <limits.h>
|
|
||||||
#include <pthread.h>
|
|
||||||
|
|
||||||
#include <UInt64.h>
|
|
||||||
|
|
||||||
#ifndef PATH_MAX
|
|
||||||
#define PATH_MAX 256
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifndef SSIZE_MAX
|
|
||||||
#define SSIZE_MAX LONG_MAX
|
|
||||||
#endif
|
|
||||||
|
|
||||||
UInt64
|
|
||||||
UtilServerTs(void)
|
|
||||||
{
|
|
||||||
struct timeval tv;
|
|
||||||
|
|
||||||
UInt64 ts;
|
|
||||||
UInt64 sec;
|
|
||||||
UInt64 usec;
|
|
||||||
|
|
||||||
gettimeofday(&tv, NULL);
|
|
||||||
|
|
||||||
/*
|
|
||||||
* POSIX defines struct timeval to be:
|
|
||||||
*
|
|
||||||
* struct timeval {
|
|
||||||
* time_t tv_sec;
|
|
||||||
* suseconds_t tv_usec;
|
|
||||||
* };
|
|
||||||
*
|
|
||||||
* Note: Although the C standard does not require that
|
|
||||||
* time_t be an integer type (it may be floating point),
|
|
||||||
* according to POSIX, time_t shall be an integer type.
|
|
||||||
* As we are targeting POSIX, not ANSI/ISO C, we can
|
|
||||||
* rely on this.
|
|
||||||
*
|
|
||||||
* The same goes for suseconds_t.
|
|
||||||
*/
|
|
||||||
if (sizeof(time_t) == sizeof(UInt64))
|
|
||||||
{
|
|
||||||
/* 64 bit time_t: convert it to a 64 bit integer */
|
|
||||||
time_t ms = tv.tv_sec * 1000;
|
|
||||||
UInt32 high = (UInt32) (ms >> 32);
|
|
||||||
UInt32 low = (UInt32) ms;
|
|
||||||
|
|
||||||
sec = UInt64Create(high, low);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
/* Assume 32 bit time_t: promote to 64 bit, then multiply, in
|
|
||||||
* case multiplication overflows 32 bits. */
|
|
||||||
sec = UInt64Create(0, tv.tv_sec);
|
|
||||||
sec = UInt64Mul(sec, UInt64Create(0, 1000));
|
|
||||||
}
|
|
||||||
|
|
||||||
usec = UInt64Create(0, tv.tv_usec / 1000);
|
|
||||||
ts = UInt64Add(sec, usec);
|
|
||||||
|
|
||||||
return ts;
|
|
||||||
}
|
|
||||||
|
|
||||||
UInt64
|
|
||||||
UtilLastModified(char *path)
|
|
||||||
{
|
|
||||||
struct stat st;
|
|
||||||
UInt64 ts = UInt64Create(0, 0);
|
|
||||||
|
|
||||||
if (stat(path, &st) == 0)
|
|
||||||
{
|
|
||||||
if (sizeof(time_t) == sizeof(UInt64))
|
|
||||||
{
|
|
||||||
/* 64 bit time_t: convert it to a 64 bit integer */
|
|
||||||
time_t ms = st.st_mtim.tv_sec * 1000;
|
|
||||||
UInt32 high = (UInt32) (ms >> 32);
|
|
||||||
UInt32 low = (UInt32) ms;
|
|
||||||
|
|
||||||
ts = UInt64Create(high, low);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
ts = UInt64Create(0, st.st_mtim.tv_sec);
|
|
||||||
ts = UInt64Mul(ts, UInt64Create(0, 1000));
|
|
||||||
}
|
|
||||||
|
|
||||||
/* nsec gauanteed to fit in 32 bits */
|
|
||||||
ts = UInt64Add(ts, UInt64Create(0, st.st_mtim.tv_nsec / 1000000));
|
|
||||||
}
|
|
||||||
|
|
||||||
return ts;
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
|
||||||
UtilMkdir(const char *dir, const mode_t mode)
|
|
||||||
{
|
|
||||||
char tmp[PATH_MAX];
|
|
||||||
char *p = NULL;
|
|
||||||
|
|
||||||
struct stat st;
|
|
||||||
|
|
||||||
size_t len;
|
|
||||||
|
|
||||||
len = strnlen(dir, PATH_MAX);
|
|
||||||
if (!len || len == PATH_MAX)
|
|
||||||
{
|
|
||||||
errno = ENAMETOOLONG;
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
memcpy(tmp, dir, len);
|
|
||||||
tmp[len] = '\0';
|
|
||||||
|
|
||||||
if (tmp[len - 1] == '/')
|
|
||||||
{
|
|
||||||
tmp[len - 1] = '\0';
|
|
||||||
}
|
|
||||||
|
|
||||||
if (stat(tmp, &st) == 0 && S_ISDIR(st.st_mode))
|
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (p = tmp + 1; *p; p++)
|
|
||||||
{
|
|
||||||
if (*p == '/')
|
|
||||||
{
|
|
||||||
*p = 0;
|
|
||||||
|
|
||||||
if (stat(tmp, &st) != 0)
|
|
||||||
{
|
|
||||||
if (mkdir(tmp, mode) < 0)
|
|
||||||
{
|
|
||||||
/* errno already set by mkdir() */
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (!S_ISDIR(st.st_mode))
|
|
||||||
{
|
|
||||||
errno = ENOTDIR;
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
*p = '/';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (stat(tmp, &st) != 0)
|
|
||||||
{
|
|
||||||
if (mkdir(tmp, mode) < 0)
|
|
||||||
{
|
|
||||||
/* errno already set by mkdir() */
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (!S_ISDIR(st.st_mode))
|
|
||||||
{
|
|
||||||
errno = ENOTDIR;
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
|
||||||
UtilSleepMillis(UInt64 ms)
|
|
||||||
{
|
|
||||||
struct timespec ts;
|
|
||||||
int res;
|
|
||||||
|
|
||||||
if (sizeof(time_t) == sizeof(UInt64))
|
|
||||||
{
|
|
||||||
ts.tv_sec = ((time_t) UInt64High(ms) << 32 | UInt64Low(ms)) / 1000;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
ts.tv_sec = UInt64Low(ms) / 1000;
|
|
||||||
}
|
|
||||||
|
|
||||||
ts.tv_nsec = UInt64Low(UInt64Rem(ms, UInt64Create(0, 1000))) * 1000000;
|
|
||||||
|
|
||||||
res = nanosleep(&ts, &ts);
|
|
||||||
|
|
||||||
return res;
|
|
||||||
}
|
|
||||||
|
|
||||||
ssize_t
|
|
||||||
UtilGetDelim(char **linePtr, size_t * n, int delim, Stream * stream)
|
|
||||||
{
|
|
||||||
char *curPos, *newLinePtr;
|
|
||||||
size_t newLinePtrLen;
|
|
||||||
int c;
|
|
||||||
|
|
||||||
if (!linePtr || !n || !stream)
|
|
||||||
{
|
|
||||||
errno = EINVAL;
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (*linePtr == NULL)
|
|
||||||
{
|
|
||||||
*n = 128;
|
|
||||||
|
|
||||||
if (!(*linePtr = Malloc(*n)))
|
|
||||||
{
|
|
||||||
errno = ENOMEM;
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
curPos = *linePtr;
|
|
||||||
|
|
||||||
while (1)
|
|
||||||
{
|
|
||||||
c = StreamGetc(stream);
|
|
||||||
|
|
||||||
if (StreamError(stream) || (c == EOF && curPos == *linePtr))
|
|
||||||
{
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (c == EOF)
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ((*linePtr + *n - curPos) < 2)
|
|
||||||
{
|
|
||||||
if (SSIZE_MAX / 2 < *n)
|
|
||||||
{
|
|
||||||
#ifdef EOVERFLOW
|
|
||||||
errno = EOVERFLOW;
|
|
||||||
#else
|
|
||||||
errno = ERANGE;
|
|
||||||
#endif
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
newLinePtrLen = *n * 2;
|
|
||||||
|
|
||||||
if (!(newLinePtr = Realloc(*linePtr, newLinePtrLen)))
|
|
||||||
{
|
|
||||||
errno = ENOMEM;
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
curPos = newLinePtr + (curPos - *linePtr);
|
|
||||||
*linePtr = newLinePtr;
|
|
||||||
*n = newLinePtrLen;
|
|
||||||
}
|
|
||||||
|
|
||||||
*curPos++ = (char) c;
|
|
||||||
|
|
||||||
if (c == delim)
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
*curPos = '\0';
|
|
||||||
return (ssize_t) (curPos - *linePtr);
|
|
||||||
}
|
|
||||||
|
|
||||||
ssize_t
|
|
||||||
UtilGetLine(char **linePtr, size_t * n, Stream * stream)
|
|
||||||
{
|
|
||||||
return UtilGetDelim(linePtr, n, '\n', stream);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
ThreadNoDestructor(void *p)
|
|
||||||
{
|
|
||||||
free(p);
|
|
||||||
}
|
|
||||||
|
|
||||||
UInt32
|
|
||||||
UtilThreadNo(void)
|
|
||||||
{
|
|
||||||
static pthread_key_t key;
|
|
||||||
static int createdKey = 0;
|
|
||||||
static unsigned long count = 0;
|
|
||||||
|
|
||||||
UInt32 *no;
|
|
||||||
|
|
||||||
if (!createdKey)
|
|
||||||
{
|
|
||||||
pthread_key_create(&key, ThreadNoDestructor);
|
|
||||||
createdKey = 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
no = pthread_getspecific(key);
|
|
||||||
if (!no)
|
|
||||||
{
|
|
||||||
no = malloc(sizeof(UInt32));
|
|
||||||
*no = count++;
|
|
||||||
pthread_setspecific(key, no);
|
|
||||||
}
|
|
||||||
|
|
||||||
return *no;
|
|
||||||
}
|
|
|
@ -1,82 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright (C) 2022-2023 Jordan Bancino <@jordan:bancino.net>
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person
|
|
||||||
* obtaining a copy of this software and associated documentation files
|
|
||||||
* (the "Software"), to deal in the Software without restriction,
|
|
||||||
* including without limitation the rights to use, copy, modify, merge,
|
|
||||||
* publish, distribute, sublicense, and/or sell copies of the Software,
|
|
||||||
* and to permit persons to whom the Software is furnished to do so,
|
|
||||||
* subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be
|
|
||||||
* included in all copies or portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
||||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
||||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
||||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
||||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
||||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
||||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
||||||
* SOFTWARE.
|
|
||||||
*/
|
|
||||||
#ifndef CYTOPLASM_ARGS_H
|
|
||||||
#define CYTOPLASM_ARGS_H
|
|
||||||
|
|
||||||
/***
|
|
||||||
* @Nm Args
|
|
||||||
* @Nd Getopt-style argument parser that operates on arrays.
|
|
||||||
* @Dd May 12 2023
|
|
||||||
* @Xr Array
|
|
||||||
*
|
|
||||||
* .Nm
|
|
||||||
* provides a simple argument parser in the style of
|
|
||||||
* .Xr getopt 3 .
|
|
||||||
* It exists because the runtime passes the program arguments as
|
|
||||||
* an Array, and it is often useful to parse the arguments to
|
|
||||||
* provide the standard command line interface.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <Array.h>
|
|
||||||
|
|
||||||
/**
|
|
||||||
* All state is stored in this structure, instead of global
|
|
||||||
* variables. This makes
|
|
||||||
* .Nm
|
|
||||||
* thread-safe and easy to reset.
|
|
||||||
*/
|
|
||||||
typedef struct ArgParseState
|
|
||||||
{
|
|
||||||
int optInd;
|
|
||||||
int optErr;
|
|
||||||
int optOpt;
|
|
||||||
char *optArg;
|
|
||||||
|
|
||||||
int optPos;
|
|
||||||
} ArgParseState;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Initialize the variables in the given parser state structure
|
|
||||||
* to their default values. This should be called before
|
|
||||||
* .Fn ArgParse
|
|
||||||
* is called with the parser state. It should also be called if
|
|
||||||
* .Fn ArgParse
|
|
||||||
* will be used again on a different array, or the same array all
|
|
||||||
* over again.
|
|
||||||
*/
|
|
||||||
extern void ArgParseStateInit(ArgParseState *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Parse command line arguments stored in the given array, using
|
|
||||||
* the given state and option string. This function behaves
|
|
||||||
* identically to the POSIX
|
|
||||||
* .Fn getopt
|
|
||||||
* function, and should be used in exactly the same way. Refer to
|
|
||||||
* your system's
|
|
||||||
* .Xr getopt 3
|
|
||||||
* page for details.
|
|
||||||
*/
|
|
||||||
extern int ArgParse(ArgParseState *, Array *, const char *);
|
|
||||||
|
|
||||||
#endif /* CYTOPLASM_ARGS_H */
|
|
|
@ -1,187 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright (C) 2022-2023 Jordan Bancino <@jordan:bancino.net>
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person
|
|
||||||
* obtaining a copy of this software and associated documentation files
|
|
||||||
* (the "Software"), to deal in the Software without restriction,
|
|
||||||
* including without limitation the rights to use, copy, modify, merge,
|
|
||||||
* publish, distribute, sublicense, and/or sell copies of the Software,
|
|
||||||
* and to permit persons to whom the Software is furnished to do so,
|
|
||||||
* subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be
|
|
||||||
* included in all copies or portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
||||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
||||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
||||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
||||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
||||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
||||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
||||||
* SOFTWARE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef CYTOPLASM_ARRAY_H
|
|
||||||
#define CYTOPLASM_ARRAY_H
|
|
||||||
|
|
||||||
/***
|
|
||||||
* @Nm Array
|
|
||||||
* @Nd A simple dynamic array data structure.
|
|
||||||
* @Dd November 24 2022
|
|
||||||
* @Xr HashMap Queue
|
|
||||||
*
|
|
||||||
* These functions implement a simple array data structure that is
|
|
||||||
* automatically resized as necessary when new values are added. This
|
|
||||||
* implementation does not actually store the values of the items in it;
|
|
||||||
* it only stores pointers to the data. As such, you will still have to
|
|
||||||
* manually maintain all your data. The advantage of this is that these
|
|
||||||
* functions don't have to copy data, and thus don't care how big the
|
|
||||||
* data is. Furthermore, arbitrary data can be stored in the array.
|
|
||||||
* .Pp
|
|
||||||
* This array implementation is optimized for storage space and
|
|
||||||
* appending. Deletions are expensive in that all the items of the list
|
|
||||||
* above a deletion are moved down to fill the hole where the deletion
|
|
||||||
* occurred. Insertions are also expensive in that all the elements
|
|
||||||
* above the given index must be shifted up to make room for the new
|
|
||||||
* element.
|
|
||||||
* .Pp
|
|
||||||
* Due to these design choices, this array implementation is best
|
|
||||||
* suited to linear writing, and then linear or random reading.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <stddef.h>
|
|
||||||
#include <stdarg.h>
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The functions in this API operate on an array structure which is
|
|
||||||
* opaque to the caller.
|
|
||||||
*/
|
|
||||||
typedef struct Array Array;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Allocate a new array. This function returns a pointer that can be
|
|
||||||
* used with the other functions in this API, or NULL if there was an
|
|
||||||
* error allocating memory for the array.
|
|
||||||
*/
|
|
||||||
extern Array *ArrayCreate(void);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Deallocate an array. Note that this function does not free any of
|
|
||||||
* the values stored in the array; it is the caller's job to manage the
|
|
||||||
* memory for each item. Typically, the caller would iterate over all
|
|
||||||
* the items in the array and free them before freeing the array.
|
|
||||||
*/
|
|
||||||
extern void ArrayFree(Array *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the size, in number of elements, of the given array.
|
|
||||||
*/
|
|
||||||
extern size_t ArraySize(Array *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the element at the specified index from the specified array.
|
|
||||||
* This function will return NULL if the array is NULL, or the index
|
|
||||||
* is out of bounds. Otherwise, it will return a pointer to a value
|
|
||||||
* put into the array using
|
|
||||||
* .Fn ArrayInsert
|
|
||||||
* or
|
|
||||||
* .Fn ArraySet .
|
|
||||||
*/
|
|
||||||
extern void *ArrayGet(Array *, size_t);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Insert the specified element at the specified index in the specified
|
|
||||||
* array. This function will shift the element currently at that index,
|
|
||||||
* and any elements after it before inserting the given element.
|
|
||||||
* .Pp
|
|
||||||
* This function returns a boolean value indicating whether or not it
|
|
||||||
* suceeded.
|
|
||||||
*/
|
|
||||||
extern int ArrayInsert(Array *, size_t, void *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Set the value at the specified index in the specified array to the
|
|
||||||
* specified value. This function will return the old value at that
|
|
||||||
* index, if any.
|
|
||||||
*/
|
|
||||||
extern void *ArraySet(Array *, size_t, void *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Append the specified element to the end of the specified array. This
|
|
||||||
* function uses
|
|
||||||
* .Fn ArrayInsert
|
|
||||||
* under the hood to insert an element at the end. It thus has the same
|
|
||||||
* return value as
|
|
||||||
* .Fn ArrayInsert .
|
|
||||||
*/
|
|
||||||
extern int ArrayAdd(Array *, void *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Remove the element at the specified index from the specified array.
|
|
||||||
* This function returns the element removed, if any.
|
|
||||||
*/
|
|
||||||
extern void *ArrayDelete(Array *, size_t);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sort the specified array using the specified sort function. The
|
|
||||||
* sort function compares two elements. It takes two void pointers as
|
|
||||||
* parameters, and returns an integer. The return value indicates to
|
|
||||||
* the sorting algorithm how the elements relate to each other. A
|
|
||||||
* return value of 0 indicates that the elements are identical. A
|
|
||||||
* return value greater than 0 indicates that the first item is
|
|
||||||
* ``bigger'' than the second item and should thus appear after it in
|
|
||||||
* the array. A return value less than 0 indicates the opposite: the
|
|
||||||
* second element should appear after the first in the array.
|
|
||||||
*/
|
|
||||||
extern void ArraySort(Array *, int (*) (void *, void *));
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Remove all duplicates from an array by using the given comparison
|
|
||||||
* function to sort the array, then remove matching values. This
|
|
||||||
* function returns a new array with all duplicates removed. The
|
|
||||||
* original array is unaffected. Note that arrays only store pointers
|
|
||||||
* to values, usually values on the heap. Thus, it is possible to lose
|
|
||||||
* pointers to duplicate values and have them leak.
|
|
||||||
* .P
|
|
||||||
* This is a relatively expensive operation. The array must first be
|
|
||||||
* duplicated. Then it is sorted, then it is iterated from beginning
|
|
||||||
* to end to remove duplicate entires. Note that the comparison
|
|
||||||
* function is executed on each element at least twice.
|
|
||||||
*/
|
|
||||||
extern Array *ArrayUnique(Array *, int (*) (void *, void *));
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Reverses the order of all elements in the array into a new array on
|
|
||||||
* the heap, to be freed using
|
|
||||||
* .Fn ArrayFree .
|
|
||||||
*/
|
|
||||||
extern Array *ArrayReverse(Array *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* If possible, reduce the amount of memory allocated to this array
|
|
||||||
* by calling
|
|
||||||
* .Fn Realloc
|
|
||||||
* on the internal structure to perfectly fit the elements in the
|
|
||||||
* array. This function is intended to be used by functions that return
|
|
||||||
* relatively read-only arrays that will be long-lived.
|
|
||||||
*/
|
|
||||||
extern int ArrayTrim(Array *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Convert a variadic arguments list into an Array. In most cases, the
|
|
||||||
* Array API is much easier to work with than
|
|
||||||
* .Fn va_arg
|
|
||||||
* and friends.
|
|
||||||
*/
|
|
||||||
extern Array *ArrayFromVarArgs(size_t, va_list);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Duplicate an existing array. Note that arrays only hold pointers to
|
|
||||||
* their data, not the data itself, so the duplicated array will point
|
|
||||||
* to the same places in memory as the original array.
|
|
||||||
*/
|
|
||||||
extern Array *ArrayDuplicate(Array *);
|
|
||||||
|
|
||||||
#endif /* CYTOPLASM_ARRAY_H */
|
|
|
@ -1,99 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright (C) 2022-2023 Jordan Bancino <@jordan:bancino.net>
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person
|
|
||||||
* obtaining a copy of this software and associated documentation files
|
|
||||||
* (the "Software"), to deal in the Software without restriction,
|
|
||||||
* including without limitation the rights to use, copy, modify, merge,
|
|
||||||
* publish, distribute, sublicense, and/or sell copies of the Software,
|
|
||||||
* and to permit persons to whom the Software is furnished to do so,
|
|
||||||
* subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be
|
|
||||||
* included in all copies or portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
||||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
||||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
||||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
||||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
||||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
||||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
||||||
* SOFTWARE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef CYTOPLASM_BASE64_H
|
|
||||||
#define CYTOPLASM_BASE64_H
|
|
||||||
|
|
||||||
/***
|
|
||||||
* @Nm Base64
|
|
||||||
* @Nd A simple base64 encoder/decoder with unpadded base64 support.
|
|
||||||
* @Dd September 30 2022
|
|
||||||
* @Xr Sha2
|
|
||||||
*
|
|
||||||
* This is an efficient yet simple base64 encoding and decoding API
|
|
||||||
* that supports regular base64, as well as the Matrix specification's
|
|
||||||
* extension to base64, called ``unpadded base64.'' This API provides
|
|
||||||
* the ability to convert between the two, instead of just implementing
|
|
||||||
* unpadded base64.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <stddef.h>
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This function computes the amount of bytes needed to store a message
|
|
||||||
* of the specified number of bytes as base64.
|
|
||||||
*/
|
|
||||||
extern size_t
|
|
||||||
Base64EncodedSize(size_t);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This function computes the amount of bytes needed to store a decoded
|
|
||||||
* representation of the encoded message. It takes a pointer to the
|
|
||||||
* encoded string because it must read a few bytes off the end in order
|
|
||||||
* to accurately compute the size.
|
|
||||||
*/
|
|
||||||
extern size_t
|
|
||||||
Base64DecodedSize(const char *, size_t);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Encode the specified number of bytes from the specified buffer as
|
|
||||||
* base64. This function returns a string on the heap that should be
|
|
||||||
* freed with
|
|
||||||
* .Fn Free ,
|
|
||||||
* or NULL if a memory allocation error ocurred.
|
|
||||||
*/
|
|
||||||
extern char *
|
|
||||||
Base64Encode(const char *, size_t);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Decode the specified number of bytes from the specified buffer of
|
|
||||||
* base64. This function returns a string on the heap that should be
|
|
||||||
* freed with
|
|
||||||
* .Fn Free ,
|
|
||||||
* or NULL if a memory allocation error occured.
|
|
||||||
*/
|
|
||||||
extern char *
|
|
||||||
Base64Decode(const char *, size_t);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Remove the padding from a specified base64 string. This function
|
|
||||||
* modifies the specified string in place. It thus has no return value
|
|
||||||
* because it cannot fail. If the passed pointer is invalid, the
|
|
||||||
* behavior is undefined.
|
|
||||||
*/
|
|
||||||
extern void
|
|
||||||
Base64Unpad(char *, size_t);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Add padding to an unpadded base64 string. This function takes a
|
|
||||||
* pointer to a pointer because it may be necessary to grow the memory
|
|
||||||
* allocated to the string. This function returns a boolean value
|
|
||||||
* indicating whether the pad operation was successful. In practice,
|
|
||||||
* this means it will only fail if a bigger string is necessary, but it
|
|
||||||
* could not be automatically allocated on the heap.
|
|
||||||
*/
|
|
||||||
extern int
|
|
||||||
Base64Pad(char **, size_t);
|
|
||||||
|
|
||||||
#endif /* CYTOPLASM_BASE64_H */
|
|
|
@ -1,140 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright (C) 2022-2023 Jordan Bancino <@jordan:bancino.net>
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person
|
|
||||||
* obtaining a copy of this software and associated documentation files
|
|
||||||
* (the "Software"), to deal in the Software without restriction,
|
|
||||||
* including without limitation the rights to use, copy, modify, merge,
|
|
||||||
* publish, distribute, sublicense, and/or sell copies of the Software,
|
|
||||||
* and to permit persons to whom the Software is furnished to do so,
|
|
||||||
* subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be
|
|
||||||
* included in all copies or portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
||||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
||||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
||||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
||||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
||||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
||||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
||||||
* SOFTWARE.
|
|
||||||
*/
|
|
||||||
#ifndef CYTOPLASM_CRON_H
|
|
||||||
#define CYTOPLASM_CRON_H
|
|
||||||
|
|
||||||
/***
|
|
||||||
* @Nm Cron
|
|
||||||
* @Nd Basic periodic job scheduler.
|
|
||||||
* @Dd December 24 2022
|
|
||||||
*
|
|
||||||
* This is an extremely basic job scheduler. So basic, in fact, that
|
|
||||||
* it currently runs all jobs on a single thread, which means that it
|
|
||||||
* is intended for short-lived jobs. In the future, it might be
|
|
||||||
* extended to support a one-thread-per-job model, but for now, jobs
|
|
||||||
* should take into consideration the fact that they are sharing their
|
|
||||||
* thread, so they should not be long-running.
|
|
||||||
* .Pp
|
|
||||||
* .Nm
|
|
||||||
* works by ``ticking'' at an interval defined by the caller of
|
|
||||||
* .Fn CronCreate .
|
|
||||||
* At each tick, all the registered jobs are queried, and if they are
|
|
||||||
* due to run again, their function is executed. As much as possible,
|
|
||||||
* .Nm
|
|
||||||
* tries to tick at constant intervals, however it is possible that one
|
|
||||||
* or more jobs may overrun the tick duration. If this happens,
|
|
||||||
* .Nm
|
|
||||||
* ticks again immediately after all the jobs for the previous tick
|
|
||||||
* have completed. This is in an effort to compensate for lost time,
|
|
||||||
* however it is important to note that when jobs overrun the tick
|
|
||||||
* interval, the interval is pushed back by the amount that it was
|
|
||||||
* overrun. Because of this,
|
|
||||||
* .Nm
|
|
||||||
* is best suited for scheduling jobs that should happen
|
|
||||||
* ``aproximately'' every so often; it is not a real-time scheduler
|
|
||||||
* by any means.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <Int.h>
|
|
||||||
|
|
||||||
/**
|
|
||||||
* All functions defined here operate on a structure opaque to the
|
|
||||||
* caller.
|
|
||||||
*/
|
|
||||||
typedef struct Cron Cron;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A job function is a function that takes a void pointer and returns
|
|
||||||
* nothing. The pointer is passed when the job is scheduled, and
|
|
||||||
* is expected to remain valid until the job is no longer registered.
|
|
||||||
* The pointer is passed each time the job executes.
|
|
||||||
*/
|
|
||||||
typedef void (JobFunc) (void *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create a new
|
|
||||||
* .Nm
|
|
||||||
* object that all other functions operate on. Like most of the other
|
|
||||||
* APIs in this project, it must be freed with
|
|
||||||
* .Fn CronFree
|
|
||||||
* when it is no longer needed.
|
|
||||||
* .Pp
|
|
||||||
* This function takes the tick interval in milliseconds.
|
|
||||||
*/
|
|
||||||
extern Cron * CronCreate(UInt32);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Schedule a one-off job to be executed only at the next tick, and
|
|
||||||
* then immediately discarded. This is useful for scheduling tasks that
|
|
||||||
* only have to happen once, or very infrequently depending on
|
|
||||||
* conditions other than the current time, but don't have to happen
|
|
||||||
* immediately. The caller simply indicates that it wishes for the task
|
|
||||||
* to execute at some time in the future. How far into the future this
|
|
||||||
* practically ends up being is determined by how long it takes for
|
|
||||||
* other registered jobs to finish, and what the tick interval is.
|
|
||||||
* .Pp
|
|
||||||
* This function takes a job function and a pointer to pass to that
|
|
||||||
* function when it is executed.
|
|
||||||
*/
|
|
||||||
extern void
|
|
||||||
CronOnce(Cron *, JobFunc *, void *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Schedule a repetitive task to be executed at aproximately the given
|
|
||||||
* interval. As stated above, this is as fuzzy interval; depending on
|
|
||||||
* the jobs being run and the tick interval, tasks may not execute at
|
|
||||||
* exactly the scheduled time, but they will eventually execute.
|
|
||||||
* .Pp
|
|
||||||
* This function takes an interval in milliseconds, a job function,
|
|
||||||
* and a pointer to pass to that function when it is executed.
|
|
||||||
*/
|
|
||||||
extern void
|
|
||||||
CronEvery(Cron *, unsigned long, JobFunc *, void *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Start ticking the clock and executing registered jobs.
|
|
||||||
*/
|
|
||||||
extern void
|
|
||||||
CronStart(Cron *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Stop ticking the clock. Jobs that are already executing will
|
|
||||||
* continue to execute, but when they are finished, no new jobs will
|
|
||||||
* be executed until
|
|
||||||
* .Fn CronStart
|
|
||||||
* is called.
|
|
||||||
*/
|
|
||||||
extern void
|
|
||||||
CronStop(Cron *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Discard all job references and free all memory associated with the
|
|
||||||
* given
|
|
||||||
* .Nm Cron
|
|
||||||
* instance.
|
|
||||||
*/
|
|
||||||
extern void
|
|
||||||
CronFree(Cron *);
|
|
||||||
|
|
||||||
#endif /* CYTOPLASM_CRON_H */
|
|
|
@ -1,169 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright (C) 2022-2023 Jordan Bancino <@jordan:bancino.net>
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person
|
|
||||||
* obtaining a copy of this software and associated documentation files
|
|
||||||
* (the "Software"), to deal in the Software without restriction,
|
|
||||||
* including without limitation the rights to use, copy, modify, merge,
|
|
||||||
* publish, distribute, sublicense, and/or sell copies of the Software,
|
|
||||||
* and to permit persons to whom the Software is furnished to do so,
|
|
||||||
* subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be
|
|
||||||
* included in all copies or portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
||||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
||||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
||||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
||||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
||||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
||||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
||||||
* SOFTWARE.
|
|
||||||
*/
|
|
||||||
#ifndef CYTOPLASM_DB_H
|
|
||||||
#define CYTOPLASM_DB_H
|
|
||||||
|
|
||||||
/***
|
|
||||||
* @Nm Db
|
|
||||||
* @Nd A minimal flat-file database with mutex locking and cache.
|
|
||||||
* @Dd April 27 2023
|
|
||||||
* @Xr Json
|
|
||||||
*
|
|
||||||
* Cytoplasm operates on a flat-file database instead of a
|
|
||||||
* traditional relational database. This greatly simplifies the
|
|
||||||
* persistent storage code, and creates a relatively basic API,
|
|
||||||
* described here.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <stddef.h>
|
|
||||||
|
|
||||||
#include <HashMap.h>
|
|
||||||
#include <Array.h>
|
|
||||||
|
|
||||||
/**
|
|
||||||
* All functions in this API operate on a database structure that is
|
|
||||||
* opaque to the caller.
|
|
||||||
*/
|
|
||||||
typedef struct Db Db;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* When an object is locked, a reference is returned. This reference
|
|
||||||
* is owned by the current thread, and the database is inaccessible to
|
|
||||||
* other threads until all references have been returned to the
|
|
||||||
* database.
|
|
||||||
* .Pp
|
|
||||||
* This reference is opaque, but can be manipulated by the functions
|
|
||||||
* defined here.
|
|
||||||
*/
|
|
||||||
typedef struct DbRef DbRef;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Open a data directory. This function takes a path to open, and a
|
|
||||||
* cache size in bytes. If the cache size is 0, then caching is
|
|
||||||
* disabled and objects are loaded off the disk every time they are
|
|
||||||
* locked. Otherwise, objects are stored in the cache, and they are
|
|
||||||
* evicted in a least-recently-used manner.
|
|
||||||
*/
|
|
||||||
extern Db * DbOpen(char *, size_t);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Close the database. This function will flush anything in the cache
|
|
||||||
* to the disk, and then close the data directory. It assumes that
|
|
||||||
* all references have been unlocked. If a reference has not been
|
|
||||||
* unlocked, undefined behavior results.
|
|
||||||
*/
|
|
||||||
extern void DbClose(Db *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Set the maximum cache size allowed before
|
|
||||||
* .Nm
|
|
||||||
* starts evicting old objects. If this is set to 0, everything in the
|
|
||||||
* cache is immediately evicted and caching is disabled. If the
|
|
||||||
* database was opened with a cache size of 0, setting this will
|
|
||||||
* initialize the cache, and subsequent calls to
|
|
||||||
* .Fn DbLock
|
|
||||||
* will begin caching objects.
|
|
||||||
*/
|
|
||||||
extern void DbMaxCacheSet(Db *, size_t);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create a new object in the database with the specified name. This
|
|
||||||
* function will fail if the object already exists in the database. It
|
|
||||||
* takes a variable number of C strings, with the exact number being
|
|
||||||
* specified by the second parameter. These C strings are used to
|
|
||||||
* generate a filesystem path at which to store the object. These paths
|
|
||||||
* ensure each object is uniquely identifiable, and provides semantic
|
|
||||||
* meaning to an object.
|
|
||||||
*/
|
|
||||||
extern DbRef * DbCreate(Db *, size_t,...);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Lock an existing object in the database. This function will fail
|
|
||||||
* if the object does not exist. It takes a variable number of C
|
|
||||||
* strings, with the exact number being specified by the second
|
|
||||||
* parameter. These C strings are used to generate the filesystem path
|
|
||||||
* at which to load the object. These paths ensure each object is
|
|
||||||
* uniquely identifiable, and provides semantic meaning to an object.
|
|
||||||
*/
|
|
||||||
extern DbRef * DbLock(Db *, size_t,...);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Immediately and permanently remove an object from the database.
|
|
||||||
* This function assumes the object is not locked, otherwise undefined
|
|
||||||
* behavior will result.
|
|
||||||
*/
|
|
||||||
extern int DbDelete(Db *, size_t,...);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Unlock an object and return it back to the database. This function
|
|
||||||
* immediately syncs the object to the filesystem. The cache is a
|
|
||||||
* read cache; writes are always immediate to ensure data integrity in
|
|
||||||
* the event of a system failure.
|
|
||||||
*/
|
|
||||||
extern int DbUnlock(Db *, DbRef *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Check the existence of the given database object in a more efficient
|
|
||||||
* manner than attempting to lock it with
|
|
||||||
* .Fn DbLock .
|
|
||||||
* This function does not lock the object, nor does it load it into
|
|
||||||
* memory if it exists.
|
|
||||||
*/
|
|
||||||
extern int DbExists(Db *, size_t,...);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* List all of the objects at a given path. Unlike the other varargs
|
|
||||||
* functions, this one does not take a path to a specific object; it
|
|
||||||
* takes a directory to be iterated, where each path part is its own
|
|
||||||
* C string. Note that the resulting list only contains the objects
|
|
||||||
* in the specified directory, it does not list any subdirectories.
|
|
||||||
* .Pp
|
|
||||||
* The array returned is an array of C strings containing the object
|
|
||||||
* name.
|
|
||||||
*/
|
|
||||||
extern Array * DbList(Db *, size_t,...);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Free the list returned by
|
|
||||||
* .Fn DbListFree .
|
|
||||||
*/
|
|
||||||
extern void DbListFree(Array *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Convert a database reference into JSON that can be manipulated.
|
|
||||||
* At this time, the database actually stores objects as JSON on the
|
|
||||||
* disk, so this function just returns an internal pointer, but in the
|
|
||||||
* future it may have to be generated by decompressing a binary blob,
|
|
||||||
* or something of that nature.
|
|
||||||
*/
|
|
||||||
extern HashMap * DbJson(DbRef *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Free the existing JSON associated with the given reference, and
|
|
||||||
* replace it with new JSON. This is more efficient than duplicating
|
|
||||||
* a separate object into the database reference.
|
|
||||||
*/
|
|
||||||
extern int DbJsonSet(DbRef *, HashMap *);
|
|
||||||
|
|
||||||
#endif
|
|
|
@ -1,175 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright (C) 2022-2023 Jordan Bancino <@jordan:bancino.net>
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person
|
|
||||||
* obtaining a copy of this software and associated documentation files
|
|
||||||
* (the "Software"), to deal in the Software without restriction,
|
|
||||||
* including without limitation the rights to use, copy, modify, merge,
|
|
||||||
* publish, distribute, sublicense, and/or sell copies of the Software,
|
|
||||||
* and to permit persons to whom the Software is furnished to do so,
|
|
||||||
* subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be
|
|
||||||
* included in all copies or portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
||||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
||||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
||||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
||||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
||||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
||||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
||||||
* SOFTWARE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef CYTOPLASM_GRAPH_H
|
|
||||||
#define CYTOPLASM_GRAPH_H
|
|
||||||
|
|
||||||
/***
|
|
||||||
* @Nm Graph
|
|
||||||
* @Nd Extremely simple graph, implemented as an adjacency matrix.
|
|
||||||
* @Dd July 15 2023
|
|
||||||
*
|
|
||||||
* .Nm
|
|
||||||
* is a basic graph data structure originally written for a computer
|
|
||||||
* science class on data structures and algorithms, in which it
|
|
||||||
* received full credit. This is an adaptation of the original
|
|
||||||
* implementation that follows the Cytoplasm style and uses Cytoplasm
|
|
||||||
* APIs when convenient.
|
|
||||||
* .P
|
|
||||||
* .Nm
|
|
||||||
* stores data in an adjacency matrix, which means the storage
|
|
||||||
* complexity is O(N^2), where N is the number of vertices (called
|
|
||||||
* Nodes in this implementation) in the graph. However, this makes the
|
|
||||||
* algorithms fast and efficient.
|
|
||||||
* .P
|
|
||||||
* Nodes are identified by index, so the first node is 0, the second
|
|
||||||
* is 1, and so on. This data structure does not support storing
|
|
||||||
* arbitrary data as nodes; rather, the intended use case is to add
|
|
||||||
* all your node data to an Array, thus giving each node an index,
|
|
||||||
* and then manipulating the graph with that index. This allows access
|
|
||||||
* to node data in O(1) time in call cases, and is the most memory
|
|
||||||
* efficient.
|
|
||||||
* .P
|
|
||||||
* .Nm
|
|
||||||
* can be used to store a variety of types of graphs, although it is
|
|
||||||
* primarily suited to directed and weighted graphs.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <stddef.h>
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The functions provided here operate on an opaque graph structure.
|
|
||||||
* This structure really just stores a matrix in a contiguous block of
|
|
||||||
* memory, as well as the number of nodes in the graph, but the
|
|
||||||
* structure is kept opaque so that it remains internally consistent.
|
|
||||||
* It also maintains the style of the Cytoplasm library.
|
|
||||||
*/
|
|
||||||
typedef struct Graph Graph;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* An Edge is really just a weight, which is easily represented by an
|
|
||||||
* integer. However, it makes sense to alias this to Edge for clarity,
|
|
||||||
* both in the documentation and in the implementation.
|
|
||||||
*/
|
|
||||||
typedef int Edge;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A Node is really just a row or column in the matrix, which is easily
|
|
||||||
* represented by an unsigned integer. However, it makes sense to alias
|
|
||||||
* this to Node for clarity, both in the documentation and the
|
|
||||||
* implementation.
|
|
||||||
*/
|
|
||||||
typedef size_t Node;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create a new graph structure with the given number of vertices.
|
|
||||||
*/
|
|
||||||
extern Graph *GraphCreate(size_t);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create a new graph data structure with the given number of vertices
|
|
||||||
* and the given adjacency matrix. The adjacency matrix is copied
|
|
||||||
* verbatim into the graph data structure without any validation.
|
|
||||||
*/
|
|
||||||
extern Graph *GraphCreateWithEdges(size_t, Edge *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Free all memory associated with the given graph. Since graphs are
|
|
||||||
* just a collection of numbers, they do not depend on each other in
|
|
||||||
* any way.
|
|
||||||
*/
|
|
||||||
extern void GraphFree(Graph *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the weight of the edge connecting the node specified first to
|
|
||||||
* the node specified second. If this is a directed graph, it does not
|
|
||||||
* necessarily follow that there is an edge from the node specified
|
|
||||||
* second to the node specified first. It also does not follow that
|
|
||||||
* such an edge, if it exists, has the same weight.
|
|
||||||
* .P
|
|
||||||
* This function will return -1 if the graph is invalid or either node
|
|
||||||
* is out of bounds. It will return 0 if there is no such edge from the
|
|
||||||
* node specified first to the node specified second.
|
|
||||||
*/
|
|
||||||
extern Edge GraphEdgeGet(Graph *, Node, Node);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Set the weight of the edge connecting the node specified first to
|
|
||||||
* the node specified second. If this is not a directed graph, this
|
|
||||||
* function will have to be called twice, the second time reversing the
|
|
||||||
* order of the nodes. To remove the edge, specify a weight of 0.
|
|
||||||
*/
|
|
||||||
extern Edge GraphEdgeSet(Graph *, Node, Node, Edge);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the number of nodes in the given graph. This operation is a
|
|
||||||
* simple memory access that happens in O(1) time.
|
|
||||||
*/
|
|
||||||
extern size_t GraphCountNodes(Graph *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Perform a breadth-first search on the given graph, starting at the
|
|
||||||
* specified node. This function returns a list of nodes in the order
|
|
||||||
* they were touched. The size of the list is stored in the unsigned
|
|
||||||
* integer pointed to by the last argument.
|
|
||||||
* .P
|
|
||||||
* If an error occurs, NULL will be returned. Otherwise, the returned
|
|
||||||
* pointer should be freed with the Memory API when it is no longer
|
|
||||||
* needed.
|
|
||||||
*/
|
|
||||||
extern Node * GraphBreadthFirstSearch(Graph *, Node, size_t *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Perform a depth-first search on the given graph, starting at the
|
|
||||||
* specified node. This function returns a list of nodes in the order
|
|
||||||
* they were touched. The size of the list is stored in the unsigned
|
|
||||||
* integer pointed to by the last argument.
|
|
||||||
* .P
|
|
||||||
* If an error occurs, NULL will be returned. Otherwise the returned
|
|
||||||
* pointer should be freed with the Memory API when it is no longer
|
|
||||||
* needed.
|
|
||||||
*/
|
|
||||||
extern Node *GraphDepthFirstSearch(Graph *, Node, size_t *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Perform a topological sort on the given graph. This function returns
|
|
||||||
* a list of nodes in topological ordering, though note that this is
|
|
||||||
* probably not the only topological ordering that exists for the
|
|
||||||
* graph. The size of the list is stored in the unsigned integer
|
|
||||||
* pointed to by the last argument. It should always be the number of
|
|
||||||
* nodes in the graph, but is provided for consistency and convenience.
|
|
||||||
* .P
|
|
||||||
* If an error occurs, NULL will be returned. Otherwise the returned
|
|
||||||
* pointer should be freed with the Memory API when it is no longer
|
|
||||||
* needed.
|
|
||||||
*/
|
|
||||||
extern Node *GraphTopologicalSort(Graph *, size_t *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Transpose the given graph, returning a brand new graph that is the
|
|
||||||
* result of the transposition.
|
|
||||||
*/
|
|
||||||
extern Graph * GraphTranspose(Graph *);
|
|
||||||
|
|
||||||
#endif /* CYTOPLASM_GRAPH_H */
|
|
|
@ -1,185 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright (C) 2022-2023 Jordan Bancino <@jordan:bancino.net>
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person
|
|
||||||
* obtaining a copy of this software and associated documentation files
|
|
||||||
* (the "Software"), to deal in the Software without restriction,
|
|
||||||
* including without limitation the rights to use, copy, modify, merge,
|
|
||||||
* publish, distribute, sublicense, and/or sell copies of the Software,
|
|
||||||
* and to permit persons to whom the Software is furnished to do so,
|
|
||||||
* subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be
|
|
||||||
* included in all copies or portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
||||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
||||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
||||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
||||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
||||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
||||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
||||||
* SOFTWARE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef CYTOPLASM_HASHMAP_H
|
|
||||||
#define CYTOPLASM_HASHMAP_H
|
|
||||||
|
|
||||||
/***
|
|
||||||
* @Nm HashMap
|
|
||||||
* @Nd A simple hash map implementation.
|
|
||||||
* @Dd October 11 2022
|
|
||||||
* @Xr Array Queue
|
|
||||||
*
|
|
||||||
* This is the public interface for Cytoplasm's hash map
|
|
||||||
* implementation. This hash map is designed to be simple,
|
|
||||||
* well-documented, and generally readable and understandable, yet also
|
|
||||||
* performant enough to be useful, because it is used extensively
|
|
||||||
* throughout the project.
|
|
||||||
* .Pp
|
|
||||||
* Fundamentally, this is an entirely generic map implementation. It
|
|
||||||
* can be used for many general purposes, but it is designed only to
|
|
||||||
* implement the features Cytoplasm needs to be functional. One
|
|
||||||
* example of a Cytoplasm-specific feature is that keys cannot be
|
|
||||||
* arbitrary data; they are NULL-terminated C strings.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <stddef.h>
|
|
||||||
|
|
||||||
#include <Array.h>
|
|
||||||
|
|
||||||
/**
|
|
||||||
* These functions operate on an opaque structure, which the caller
|
|
||||||
* has no knowledge about.
|
|
||||||
*/
|
|
||||||
typedef struct HashMap HashMap;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create a new hash map that is ready to be used with the rest of the
|
|
||||||
* functions defined here.
|
|
||||||
*/
|
|
||||||
extern HashMap * HashMapCreate(void);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Free the specified hash map such that it becomes invalid and any
|
|
||||||
* future use results in undefined behavior. Note that this function
|
|
||||||
* does not free the values stored in the hash map, but since it stores
|
|
||||||
* the keys internally, it will free the keys. You should use
|
|
||||||
* .Fn HashMapIterate
|
|
||||||
* to free the values stored in this map appropriately before calling
|
|
||||||
* this function.
|
|
||||||
*/
|
|
||||||
extern void HashMapFree(HashMap *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Control the maximum load of the hash map before it is expanded.
|
|
||||||
* When the hash map reaches the given capacity, it is grown. You
|
|
||||||
* don't want to only grow hash maps when they are full, because that
|
|
||||||
* makes them perform very poorly. The maximum load value is a
|
|
||||||
* percentage of how full the hash map is, and it should be between
|
|
||||||
* 0 and 1, where 0 means that no elements will cause the map to be
|
|
||||||
* expanded, and 1 means that the hash map must be completely full
|
|
||||||
* before it is expanded. The default maximum load on a new hash map
|
|
||||||
* is 0.75, which should be good enough for most purposes, however,
|
|
||||||
* this function exists specifically so that the maximum load can be
|
|
||||||
* fine-tuned.
|
|
||||||
*/
|
|
||||||
extern void HashMapMaxLoadSet(HashMap *, float);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Use a custom hashing function with the given hash map. New hash
|
|
||||||
* maps have a sane hashing function that should work okay for most
|
|
||||||
* use cases, but if you have a better hashing function, it can be
|
|
||||||
* specified this way. Do not change the hash function after keys have
|
|
||||||
* been added; doing so results in undefined behavior. Only set a new
|
|
||||||
* hash function immediately after constructing a new hash map, before
|
|
||||||
* anything has been added to it.
|
|
||||||
* .Pp
|
|
||||||
* The hash function takes a pointer to a C string, and is expected
|
|
||||||
* to return a fairly unique numerical hash value which will be
|
|
||||||
* converted into an array index.
|
|
||||||
*/
|
|
||||||
extern void
|
|
||||||
HashMapFunctionSet(HashMap *, unsigned long (*) (const char *));
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Set the given string key to the given value. Note that the key is
|
|
||||||
* copied into the hash map's own memory space, but the value is not.
|
|
||||||
* It is the caller's job to ensure that the value pointer remains
|
|
||||||
* valid for the life of the hash map, and are freed when no longer
|
|
||||||
* needed.
|
|
||||||
*/
|
|
||||||
extern void * HashMapSet(HashMap *, char *, void *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieve the value for the given key, or return NULL if no such
|
|
||||||
* key exists in the hash map.
|
|
||||||
*/
|
|
||||||
extern void * HashMapGet(HashMap *, const char *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Remove a value specified by the given key from the hash map, and
|
|
||||||
* return it to the caller to deal with. This function returns NULL
|
|
||||||
* if no such key exists.
|
|
||||||
*/
|
|
||||||
extern void * HashMapDelete(HashMap *, const char *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Iterate over all the keys and values of a hash map. This function
|
|
||||||
* works very similarly to
|
|
||||||
* .Xr getopt 3 ,
|
|
||||||
* where calls are repeatedly made in a while loop until there are no
|
|
||||||
* more items to go over. The difference is that this function does not
|
|
||||||
* rely on globals; it takes pointer pointers, and stores all
|
|
||||||
* necessary state inside the hash map itself.
|
|
||||||
* .Pp
|
|
||||||
* Note that this function is not thread-safe; two threads cannot be
|
|
||||||
* iterating over any given hash map at the same time, though they
|
|
||||||
* can each be iterating over different hash maps.
|
|
||||||
* .Pp
|
|
||||||
* This function can be tricky to use in some scenarios, as it
|
|
||||||
* continues where it left off on each call, until there are no more
|
|
||||||
* elements to go through in the hash map. If you are not iterating
|
|
||||||
* over the entire map in one go, and happen to break the loop, then
|
|
||||||
* the next time you attempt to iterate the hash map, you'll start
|
|
||||||
* somewhere in the middle, which is most likely not the intended
|
|
||||||
* behavior. Thus, it is always recommended to iterate over the entire
|
|
||||||
* hash map if you're going to use this function.
|
|
||||||
* .Pp
|
|
||||||
* Also note that the behavior of this function is undefined if
|
|
||||||
* insertions or deletions occur during the iteration. This
|
|
||||||
* functionality has not been tested, and will likely not work.
|
|
||||||
*/
|
|
||||||
extern int HashMapIterate(HashMap *, char **, void **);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A reentrant version of
|
|
||||||
* .Fn HashMapIterate
|
|
||||||
* that allows the caller to overcome the flaws of that function by
|
|
||||||
* storing the cursor outside of the hash map structure itself. This
|
|
||||||
* allows multiple threads to iterate over the same hash map at the
|
|
||||||
* same time, and it allows the iteration to be halted midway through
|
|
||||||
* without causing any unintended side effects.
|
|
||||||
* .Pp
|
|
||||||
* The cursor should be initialized to 0 at the start of iteration.
|
|
||||||
*/
|
|
||||||
extern int
|
|
||||||
HashMapIterateReentrant(HashMap *, char **, void **, size_t *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Collect the string keys of a hash map and return them as an array.
|
|
||||||
* The returned array holds pointers to the strings stored in the
|
|
||||||
* hash map, so the strings should NOT be freed; it is sufficient to
|
|
||||||
* free the array itself. Likewise, once the hash map is freed, the
|
|
||||||
* array elements are invalid and the array should be freed.
|
|
||||||
*/
|
|
||||||
extern Array * HashMapKeys(HashMap *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Collect the values of a hash map and return them as an array. The
|
|
||||||
* returned array holds the same pointers to the values as the hash
|
|
||||||
* map.
|
|
||||||
*/
|
|
||||||
extern Array * HashMapValues(HashMap *);
|
|
||||||
|
|
||||||
#endif /* CYTOPLASM_HASHMAP_H */
|
|
|
@ -1,125 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright (C) 2022-2023 Jordan Bancino <@jordan:bancino.net>
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person
|
|
||||||
* obtaining a copy of this software and associated documentation files
|
|
||||||
* (the "Software"), to deal in the Software without restriction,
|
|
||||||
* including without limitation the rights to use, copy, modify, merge,
|
|
||||||
* publish, distribute, sublicense, and/or sell copies of the Software,
|
|
||||||
* and to permit persons to whom the Software is furnished to do so,
|
|
||||||
* subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be
|
|
||||||
* included in all copies or portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
||||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
||||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
||||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
||||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
||||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
||||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
||||||
* SOFTWARE.
|
|
||||||
*/
|
|
||||||
#ifndef CYTOPLASM_HEADERPARSER_H
|
|
||||||
#define CYTOPLASM_HEADERPARSER_H
|
|
||||||
|
|
||||||
/***
|
|
||||||
* @Nm HeaderParser
|
|
||||||
* @Nd Parse simple C header files.
|
|
||||||
* @Dd April 29 2023
|
|
||||||
*
|
|
||||||
* .Nm
|
|
||||||
* is an extremely simple parser that lacks most of the functionality
|
|
||||||
* one would expect from a C code parser. It simply maps a stream
|
|
||||||
* of tokens into a few categories, parsing major ``milestones'' in
|
|
||||||
* a header, without actually understanding any of the details.
|
|
||||||
* .Pp
|
|
||||||
* This exists because it is used to generate man pages from headers.
|
|
||||||
* See
|
|
||||||
* .Xr hdoc 1
|
|
||||||
* for example usage of this parser.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <Stream.h>
|
|
||||||
#include <Array.h>
|
|
||||||
|
|
||||||
#define HEADER_EXPR_MAX 4096
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Headers are parsed as expressions. These are the expressions that
|
|
||||||
* this parser recognizes.
|
|
||||||
*/
|
|
||||||
typedef enum HeaderExprType
|
|
||||||
{
|
|
||||||
HP_COMMENT,
|
|
||||||
HP_PREPROCESSOR_DIRECTIVE,
|
|
||||||
HP_TYPEDEF,
|
|
||||||
HP_DECLARATION,
|
|
||||||
HP_GLOBAL,
|
|
||||||
HP_UNKNOWN,
|
|
||||||
HP_SYNTAX_ERROR,
|
|
||||||
HP_PARSE_ERROR,
|
|
||||||
HP_EOF
|
|
||||||
} HeaderExprType;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A representation of a function declaration.
|
|
||||||
*/
|
|
||||||
typedef struct HeaderDeclaration
|
|
||||||
{
|
|
||||||
char returnType[64];
|
|
||||||
char name[32]; /* Enforced by ANSI C */
|
|
||||||
Array *args;
|
|
||||||
} HeaderDeclaration;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A global variable declaration. The type must be of the same size
|
|
||||||
* as the function declaration's return type due to the way parsing
|
|
||||||
* them is implemented.
|
|
||||||
*/
|
|
||||||
typedef struct HeaderGlobal
|
|
||||||
{
|
|
||||||
char type[64];
|
|
||||||
char name[HEADER_EXPR_MAX - 64];
|
|
||||||
} HeaderGlobal;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A representation of a single header expression. Note that that state
|
|
||||||
* structure is entirely internally managed, so it should not be
|
|
||||||
* accessed or manipulated by functions outside the functions defined
|
|
||||||
* here.
|
|
||||||
* .Pp
|
|
||||||
* The type field should be used to determine which field in the data
|
|
||||||
* union is valid.
|
|
||||||
*/
|
|
||||||
typedef struct HeaderExpr
|
|
||||||
{
|
|
||||||
HeaderExprType type;
|
|
||||||
union
|
|
||||||
{
|
|
||||||
char text[HEADER_EXPR_MAX];
|
|
||||||
HeaderDeclaration declaration;
|
|
||||||
HeaderGlobal global;
|
|
||||||
struct
|
|
||||||
{
|
|
||||||
int lineNo;
|
|
||||||
char *msg;
|
|
||||||
} error;
|
|
||||||
} data;
|
|
||||||
|
|
||||||
struct
|
|
||||||
{
|
|
||||||
Stream *stream;
|
|
||||||
int lineNo;
|
|
||||||
} state;
|
|
||||||
} HeaderExpr;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Parse the next expression into the given header expression structure.
|
|
||||||
* To parse an entire C header, this function should be called in a
|
|
||||||
* loop until the type of the expression is HP_EOF.
|
|
||||||
*/
|
|
||||||
extern void HeaderParse(Stream *, HeaderExpr *);
|
|
||||||
|
|
||||||
#endif /* CYTOPLASM_HEADERPARSER_H */
|
|
|
@ -1,211 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright (C) 2022-2023 Jordan Bancino <@jordan:bancino.net>
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person
|
|
||||||
* obtaining a copy of this software and associated documentation files
|
|
||||||
* (the "Software"), to deal in the Software without restriction,
|
|
||||||
* including without limitation the rights to use, copy, modify, merge,
|
|
||||||
* publish, distribute, sublicense, and/or sell copies of the Software,
|
|
||||||
* and to permit persons to whom the Software is furnished to do so,
|
|
||||||
* subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be
|
|
||||||
* included in all copies or portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
||||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
||||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
||||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
||||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
||||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
||||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
||||||
* SOFTWARE.
|
|
||||||
*/
|
|
||||||
#ifndef CYTOPLASM_HTTP_H
|
|
||||||
#define CYTOPLASM_HTTP_H
|
|
||||||
|
|
||||||
/***
|
|
||||||
* @Nm Http
|
|
||||||
* @Nd Encode and decode various parts of the HTTP protocol.
|
|
||||||
* @Dd March 12 2023
|
|
||||||
* @Xr HttpClient HttpServer HashMap Queue Memory
|
|
||||||
*
|
|
||||||
* .Nm
|
|
||||||
* is a collection of utility functions and type definitions that are
|
|
||||||
* useful for dealing with HTTP. HTTP is not a complex protocol, but
|
|
||||||
* this API makes it a lot easier to work with.
|
|
||||||
* .Pp
|
|
||||||
* Note that this API doesn't target any particular HTTP version, but
|
|
||||||
* it is currently used with HTTP 1.0 clients and servers, and
|
|
||||||
* therefore may be lacking functionality added in later HTTP versions.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <stdio.h>
|
|
||||||
|
|
||||||
#include <HashMap.h>
|
|
||||||
#include <Stream.h>
|
|
||||||
|
|
||||||
#define HTTP_FLAG_NONE 0
|
|
||||||
#define HTTP_FLAG_TLS (1 << 0)
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The request methods defined by the HTTP standard. These numeric
|
|
||||||
* constants should be preferred to strings when building HTTP APIs
|
|
||||||
* because they are more efficient.
|
|
||||||
*/
|
|
||||||
typedef enum HttpRequestMethod
|
|
||||||
{
|
|
||||||
HTTP_METHOD_UNKNOWN,
|
|
||||||
HTTP_GET,
|
|
||||||
HTTP_HEAD,
|
|
||||||
HTTP_POST,
|
|
||||||
HTTP_PUT,
|
|
||||||
HTTP_DELETE,
|
|
||||||
HTTP_CONNECT,
|
|
||||||
HTTP_OPTIONS,
|
|
||||||
HTTP_TRACE,
|
|
||||||
HTTP_PATCH
|
|
||||||
} HttpRequestMethod;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* An enumeration that corresponds to the actual integer values of the
|
|
||||||
* valid HTTP response codes.
|
|
||||||
*/
|
|
||||||
typedef enum HttpStatus
|
|
||||||
{
|
|
||||||
HTTP_STATUS_UNKNOWN = 0,
|
|
||||||
|
|
||||||
/* Informational responses */
|
|
||||||
HTTP_CONTINUE = 100,
|
|
||||||
HTTP_SWITCHING_PROTOCOLS = 101,
|
|
||||||
HTTP_EARLY_HINTS = 103,
|
|
||||||
|
|
||||||
/* Successful responses */
|
|
||||||
HTTP_OK = 200,
|
|
||||||
HTTP_CREATED = 201,
|
|
||||||
HTTP_ACCEPTED = 202,
|
|
||||||
HTTP_NON_AUTHORITATIVE_INFORMATION = 203,
|
|
||||||
HTTP_NO_CONTENT = 204,
|
|
||||||
HTTP_RESET_CONTENT = 205,
|
|
||||||
HTTP_PARTIAL_CONTENT = 206,
|
|
||||||
|
|
||||||
/* Redirection messages */
|
|
||||||
HTTP_MULTIPLE_CHOICES = 300,
|
|
||||||
HTTP_MOVED_PERMANENTLY = 301,
|
|
||||||
HTTP_FOUND = 302,
|
|
||||||
HTTP_SEE_OTHER = 303,
|
|
||||||
HTTP_NOT_MODIFIED = 304,
|
|
||||||
HTTP_TEMPORARY_REDIRECT = 307,
|
|
||||||
HTTP_PERMANENT_REDIRECT = 308,
|
|
||||||
|
|
||||||
/* Client error messages */
|
|
||||||
HTTP_BAD_REQUEST = 400,
|
|
||||||
HTTP_UNAUTHORIZED = 401,
|
|
||||||
HTTP_FORBIDDEN = 403,
|
|
||||||
HTTP_NOT_FOUND = 404,
|
|
||||||
HTTP_METHOD_NOT_ALLOWED = 405,
|
|
||||||
HTTP_NOT_ACCEPTABLE = 406,
|
|
||||||
HTTP_PROXY_AUTH_REQUIRED = 407,
|
|
||||||
HTTP_REQUEST_TIMEOUT = 408,
|
|
||||||
HTTP_CONFLICT = 409,
|
|
||||||
HTTP_GONE = 410,
|
|
||||||
HTTP_LENGTH_REQUIRED = 411,
|
|
||||||
HTTP_PRECONDITION_FAILED = 412,
|
|
||||||
HTTP_PAYLOAD_TOO_LARGE = 413,
|
|
||||||
HTTP_URI_TOO_LONG = 414,
|
|
||||||
HTTP_UNSUPPORTED_MEDIA_TYPE = 415,
|
|
||||||
HTTP_RANGE_NOT_SATISFIABLE = 416,
|
|
||||||
HTTP_EXPECTATION_FAILED = 417,
|
|
||||||
HTTP_TEAPOT = 418,
|
|
||||||
HTTP_UPGRADE_REQUIRED = 426,
|
|
||||||
HTTP_PRECONDITION_REQUIRED = 428,
|
|
||||||
HTTP_TOO_MANY_REQUESTS = 429,
|
|
||||||
HTTP_REQUEST_HEADER_FIELDS_TOO_LARGE = 431,
|
|
||||||
HTTP_UNAVAILABLE_FOR_LEGAL_REASONS = 451,
|
|
||||||
|
|
||||||
/* Server error responses */
|
|
||||||
HTTP_INTERNAL_SERVER_ERROR = 500,
|
|
||||||
HTTP_NOT_IMPLEMENTED = 501,
|
|
||||||
HTTP_BAD_GATEWAY = 502,
|
|
||||||
HTTP_SERVICE_UNAVAILABLE = 503,
|
|
||||||
HTTP_GATEWAY_TIMEOUT = 504,
|
|
||||||
HTTP_VERSION_NOT_SUPPORTED = 505,
|
|
||||||
HTTP_VARIANT_ALSO_NEGOTIATES = 506,
|
|
||||||
HTTP_NOT_EXTENDED = 510,
|
|
||||||
HTTP_NETWORK_AUTH_REQUIRED = 511
|
|
||||||
} HttpStatus;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Convert an HTTP status enumeration value into a string description
|
|
||||||
* of the status, which is to be used in server response to a client,
|
|
||||||
* or a client response to a user. For example, calling
|
|
||||||
* .Fn HttpStatusToString "HTTP_GATEWAY_TIMEOUT"
|
|
||||||
* (or
|
|
||||||
* .Fn HttpStatusToString "504" )
|
|
||||||
* produces the string "Gateway Timeout". Note that the returned
|
|
||||||
* pointers point to static space, so their manipulation is forbidden.
|
|
||||||
*/
|
|
||||||
extern const char * HttpStatusToString(const HttpStatus);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Convert a string into a numeric code that can be used throughout
|
|
||||||
* the code of a program in an efficient manner. See the definition
|
|
||||||
* of HttpRequestMethod. This function does case-sensitive matching,
|
|
||||||
* and does not trim or otherwise process the input string.
|
|
||||||
*/
|
|
||||||
extern HttpRequestMethod HttpRequestMethodFromString(const char *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Convert a numeric code as defined by HttpRequestMethod into a
|
|
||||||
* string that can be sent to a server. Note that the returned pointers
|
|
||||||
* point to static space, so their manipulation is forbidden.
|
|
||||||
*/
|
|
||||||
extern const char * HttpRequestMethodToString(const HttpRequestMethod);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Encode a C string such that it can safely appear in a URL by
|
|
||||||
* performing the necessary percent escaping. A new string on the
|
|
||||||
* heap is returned. It should be freed with
|
|
||||||
* .Fn Free ,
|
|
||||||
* defined in the
|
|
||||||
* .Xr Memory 3
|
|
||||||
* API.
|
|
||||||
*/
|
|
||||||
extern char * HttpUrlEncode(char *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Decode a percent-encoded string into a C string, ignoring encoded
|
|
||||||
* null characters entirely, because those would do nothing but cause
|
|
||||||
* problems.
|
|
||||||
*/
|
|
||||||
extern char * HttpUrlDecode(char *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Decode an encoded parameter string in the form of
|
|
||||||
* ``key=val&key2=val2'' into a hash map whose values are C strings.
|
|
||||||
* This function properly decodes keys and values using the functions
|
|
||||||
* defined above.
|
|
||||||
*/
|
|
||||||
extern HashMap * HttpParamDecode(char *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Encode a hash map whose values are strings as an HTTP parameter
|
|
||||||
* string suitable for GET or POST requests.
|
|
||||||
*/
|
|
||||||
extern char * HttpParamEncode(HashMap *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Read HTTP headers from a stream and return a hash map whose values
|
|
||||||
* are strings. All keys are lowercased to make querying them
|
|
||||||
* consistent and not dependent on the case that was read from the
|
|
||||||
* stream. This is useful for both client and server code, since the
|
|
||||||
* headers are in the same format. This function should be used after
|
|
||||||
* parsing the HTTP status line, because it does not parse that line.
|
|
||||||
* It will stop when it encounters the first blank line, which
|
|
||||||
* indicates that the body is beginning. After this function completes,
|
|
||||||
* the body may be immediately read from the stream without any
|
|
||||||
* additional processing.
|
|
||||||
*/
|
|
||||||
extern HashMap * HttpParseHeaders(Stream *);
|
|
||||||
|
|
||||||
#endif
|
|
|
@ -1,104 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright (C) 2022-2023 Jordan Bancino <@jordan:bancino.net>
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person
|
|
||||||
* obtaining a copy of this software and associated documentation files
|
|
||||||
* (the "Software"), to deal in the Software without restriction,
|
|
||||||
* including without limitation the rights to use, copy, modify, merge,
|
|
||||||
* publish, distribute, sublicense, and/or sell copies of the Software,
|
|
||||||
* and to permit persons to whom the Software is furnished to do so,
|
|
||||||
* subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be
|
|
||||||
* included in all copies or portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
||||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
||||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
||||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
||||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
||||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
||||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
||||||
* SOFTWARE.
|
|
||||||
*/
|
|
||||||
#ifndef CYTOPLASM_HTTPCLIENT_H
|
|
||||||
#define CYTOPLASM_HTTPCLIENT_H
|
|
||||||
|
|
||||||
/***
|
|
||||||
* @Nm HttpClient
|
|
||||||
* @Nd Extremely simple HTTP client.
|
|
||||||
* @Dd April 29 2023
|
|
||||||
* @Xr Http HttpServer Tls
|
|
||||||
*
|
|
||||||
* .Nm
|
|
||||||
* HttpClient
|
|
||||||
* builds on the HTTP API to provide a simple yet functional HTTP
|
|
||||||
* client. It aims at being easy to use and minimal, yet also
|
|
||||||
* efficient.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <stdio.h>
|
|
||||||
|
|
||||||
#include <HashMap.h>
|
|
||||||
#include <Http.h>
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A server response is represented by a client context. It is
|
|
||||||
* opaque, so the functions defined in this API should be used to
|
|
||||||
* fetch data from and manipulate it.
|
|
||||||
*/
|
|
||||||
typedef struct HttpClientContext HttpClientContext;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Make an HTTP request. This function takes the request method,
|
|
||||||
* any flags defined in the HTTP API, the port, hostname, and path,
|
|
||||||
* all in that order. It returns NULL if there was an error making
|
|
||||||
* the request. Otherwise it returns a client context. Note that this
|
|
||||||
* function does not actually send any data, it simply makes the
|
|
||||||
* connection. Use
|
|
||||||
* .Fn HttpRequestHeader
|
|
||||||
* to add headers to the request. Then, send headers with
|
|
||||||
* .Fn HttpRequestSendHeaders .
|
|
||||||
* Finally, the request body, if any, can be written to the output
|
|
||||||
* stream, and then the request can be fully sent using
|
|
||||||
* .Fn HttpRequestSend .
|
|
||||||
*/
|
|
||||||
extern HttpClientContext *
|
|
||||||
HttpRequest(HttpRequestMethod, int, unsigned short, char *, char *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Set a request header to send to the server when making the
|
|
||||||
* request.
|
|
||||||
*/
|
|
||||||
extern void HttpRequestHeader(HttpClientContext *, char *, char *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Send the request headers to the server. This must be called before
|
|
||||||
* the request body can be written or a response body can be read.
|
|
||||||
*/
|
|
||||||
extern void HttpRequestSendHeaders(HttpClientContext *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Flush the request stream to the server. This function should be
|
|
||||||
* called before the response body is read.
|
|
||||||
*/
|
|
||||||
extern HttpStatus HttpRequestSend(HttpClientContext *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the headers returned by the server.
|
|
||||||
*/
|
|
||||||
extern HashMap * HttpResponseHeaders(HttpClientContext *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the stream used to write the request body and read the
|
|
||||||
* response body.
|
|
||||||
*/
|
|
||||||
extern Stream * HttpClientStream(HttpClientContext *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Free all memory associated with the client context. This closes the
|
|
||||||
* connection, if it was still open.
|
|
||||||
*/
|
|
||||||
extern void HttpClientContextFree(HttpClientContext *);
|
|
||||||
|
|
||||||
#endif /* CYTOPLASM_HTTPCLIENT_H */
|
|
|
@ -1,91 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright (C) 2022-2023 Jordan Bancino <@jordan:bancino.net>
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person
|
|
||||||
* obtaining a copy of this software and associated documentation files
|
|
||||||
* (the "Software"), to deal in the Software without restriction,
|
|
||||||
* including without limitation the rights to use, copy, modify, merge,
|
|
||||||
* publish, distribute, sublicense, and/or sell copies of the Software,
|
|
||||||
* and to permit persons to whom the Software is furnished to do so,
|
|
||||||
* subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be
|
|
||||||
* included in all copies or portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
||||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
||||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
||||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
||||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
||||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
||||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
||||||
* SOFTWARE.
|
|
||||||
*/
|
|
||||||
#ifndef CYTOPLASM_HTTPROUTER_H
|
|
||||||
#define CYTOPLASM_HTTPROUTER_H
|
|
||||||
|
|
||||||
/***
|
|
||||||
* @Nm HttpRouter
|
|
||||||
* @Nd Simple HTTP request router with regular expression support.
|
|
||||||
* @Dd April 29 2023
|
|
||||||
* @Xr HttpServer Http
|
|
||||||
*
|
|
||||||
* .Nm
|
|
||||||
* provides a simple mechanism for assigning functions to an HTTP
|
|
||||||
* request path. It is a simple tree data structure that parses the
|
|
||||||
* registered request paths and maps functions onto each part of the
|
|
||||||
* path. Then, requests can be easily routed to their appropriate
|
|
||||||
* handler functions.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <Array.h>
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The router structure is opaque and thus managed entirely by the
|
|
||||||
* functions defined in this API.
|
|
||||||
*/
|
|
||||||
typedef struct HttpRouter HttpRouter;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A function written to handle an HTTP request takes an array
|
|
||||||
* consisting of the matched path parts in the order they appear in
|
|
||||||
* the path, and a pointer to caller-provided arguments, if any.
|
|
||||||
* It returns a pointer that the caller is assumed to know how to
|
|
||||||
* handle.
|
|
||||||
*/
|
|
||||||
typedef void *(HttpRouteFunc) (Array *, void *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create a new empty routing tree.
|
|
||||||
*/
|
|
||||||
extern HttpRouter * HttpRouterCreate(void);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Free all the memory associated with the given routing tree.
|
|
||||||
*/
|
|
||||||
extern void HttpRouterFree(HttpRouter *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Register the specified route function to be executed upon requests
|
|
||||||
* for the specified HTTP path. The path is parsed by splitting at
|
|
||||||
* each path separator. Each part of the path is a regular expression
|
|
||||||
* that matches the entire path part. A regular expression cannot
|
|
||||||
* match more than one path part. This allows for paths like
|
|
||||||
* .Pa /some/path/(.*)/parts
|
|
||||||
* to work as one would expect.
|
|
||||||
*/
|
|
||||||
extern int HttpRouterAdd(HttpRouter *, char *, HttpRouteFunc *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Route the specified request path using the specified routing
|
|
||||||
* tree. This function will parse the path and match it to the
|
|
||||||
* appropriate route handler function. The return value is a boolean
|
|
||||||
* value that indicates whether or not an appropriate route function
|
|
||||||
* was found. If an appropriate function was found, then the void
|
|
||||||
* pointer is passed to it as arguments that it is expected to know
|
|
||||||
* how to handle, and the pointer to a void pointer is where the
|
|
||||||
* route function's response will be placed.
|
|
||||||
*/
|
|
||||||
extern int HttpRouterRoute(HttpRouter *, char *, void *, void **);
|
|
||||||
|
|
||||||
#endif /* CYTOPLASM_HTTPROUTER_H */
|
|
|
@ -1,234 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright (C) 2022-2023 Jordan Bancino <@jordan:bancino.net>
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person
|
|
||||||
* obtaining a copy of this software and associated documentation files
|
|
||||||
* (the "Software"), to deal in the Software without restriction,
|
|
||||||
* including without limitation the rights to use, copy, modify, merge,
|
|
||||||
* publish, distribute, sublicense, and/or sell copies of the Software,
|
|
||||||
* and to permit persons to whom the Software is furnished to do so,
|
|
||||||
* subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be
|
|
||||||
* included in all copies or portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
||||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
||||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
||||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
||||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
||||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
||||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
||||||
* SOFTWARE.
|
|
||||||
*/
|
|
||||||
#ifndef CYTOPLASM_HTTPSERVER_H
|
|
||||||
#define CYTOPLASM_HTTPSERVER_H
|
|
||||||
|
|
||||||
/***
|
|
||||||
* @Nm HttpServer
|
|
||||||
* @Nd Extremely simple HTTP server.
|
|
||||||
* @Dd December 13 2022
|
|
||||||
* @Xr Http HttpClient
|
|
||||||
*
|
|
||||||
* .Nm
|
|
||||||
* builds on the
|
|
||||||
* .Xr Http 3
|
|
||||||
* API, and provides a very simple, yet very functional API for
|
|
||||||
* creating an HTTP server. It aims at being easy to use and minimal,
|
|
||||||
* yet also efficient. It uses non-blocking I/O, is fully
|
|
||||||
* multi-threaded, and is very configurable. It can be set up in just
|
|
||||||
* two function calls and minimal supporting code.
|
|
||||||
* .Pp
|
|
||||||
* This API should be familar to those that have dealt with the HTTP
|
|
||||||
* server libraries of other programming languages, particularly Java.
|
|
||||||
* In fact, much of the terminology used in this API came from Java,
|
|
||||||
* and you'll notice that the way responses are sent and received very
|
|
||||||
* closely resembles Java.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <Http.h>
|
|
||||||
|
|
||||||
#include <stdio.h>
|
|
||||||
|
|
||||||
#include <HashMap.h>
|
|
||||||
#include <Stream.h>
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The functions on this API operate on an opaque structure.
|
|
||||||
*/
|
|
||||||
typedef struct HttpServer HttpServer;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Each request receives a context structure. It is opaque, so the
|
|
||||||
* functions defined in this API should be used to fetch data from
|
|
||||||
* it. These functions allow the handler to figure out the context of
|
|
||||||
* the request, which includes the path requested, any parameters,
|
|
||||||
* and the headers and method used to make the request. The context
|
|
||||||
* also provides the means by which the handler responds to the
|
|
||||||
* request, allowing it to set the status code, headers, and body.
|
|
||||||
*/
|
|
||||||
typedef struct HttpServerContext HttpServerContext;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The request handler function is executed when an HTTP request is
|
|
||||||
* received. It takes a request context, and a pointer as specified
|
|
||||||
* in the server configuration.
|
|
||||||
*/
|
|
||||||
typedef void (HttpHandler) (HttpServerContext *, void *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The number of arguments to
|
|
||||||
* .Fn HttpServerCreate
|
|
||||||
* has grown so large that arguments are now stuffed into a
|
|
||||||
* configuration structure, which is in turn passed to
|
|
||||||
* .Fn HttpServerCreate .
|
|
||||||
* This configuration is copied by value into the internal
|
|
||||||
* structures of the server. It is copied with very minimal
|
|
||||||
* validation, so ensure that all values are sensible. It may
|
|
||||||
* make sense to use
|
|
||||||
* .Fn memset
|
|
||||||
* to zero out everything in here before assigning values.
|
|
||||||
*/
|
|
||||||
typedef struct HttpServerConfig
|
|
||||||
{
|
|
||||||
unsigned short port;
|
|
||||||
unsigned int threads;
|
|
||||||
unsigned int maxConnections;
|
|
||||||
|
|
||||||
int flags; /* Http(3) flags */
|
|
||||||
char *tlsCert; /* File path */
|
|
||||||
char *tlsKey; /* File path */
|
|
||||||
|
|
||||||
HttpHandler *handler;
|
|
||||||
void *handlerArgs;
|
|
||||||
} HttpServerConfig;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create a new HTTP server using the specified configuration.
|
|
||||||
* This will set up all internal structures used by the server,
|
|
||||||
* and bind the socket and start listening for connections. However,
|
|
||||||
* it will not start accepting connections.
|
|
||||||
*/
|
|
||||||
extern HttpServer * HttpServerCreate(HttpServerConfig *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieve the configuration that was used to instantiate the given
|
|
||||||
* server. Note that this configuration is not necessarily the exact
|
|
||||||
* one that was provided; even though its values are the same, it
|
|
||||||
* should be treated as an entirely separate configuration with no
|
|
||||||
* connection to the original.
|
|
||||||
*/
|
|
||||||
extern HttpServerConfig * HttpServerConfigGet(HttpServer *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Free the resources associated with the given HTTP server. Note that
|
|
||||||
* the server can only be freed after it has been stopped. Calling this
|
|
||||||
* function while the server is still running results in undefined
|
|
||||||
* behavior.
|
|
||||||
*/
|
|
||||||
extern void HttpServerFree(HttpServer *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Attempt to start the HTTP server, and return immediately with the
|
|
||||||
* status. This API is fully multi-threaded and asynchronous, so the
|
|
||||||
* caller can continue working while the HTTP server is running in a
|
|
||||||
* separate thread and managing a pool of threads to handle responses.
|
|
||||||
*/
|
|
||||||
extern int HttpServerStart(HttpServer *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Typically, at some point after calling
|
|
||||||
* .Fn HttpServerStart ,
|
|
||||||
* the program will have no more work to do, so it will want to wait
|
|
||||||
* for the HTTP server to finish. This is accomplished via this
|
|
||||||
* function, which joins the HTTP worker thread to the calling thread,
|
|
||||||
* pausing the calling thread until the HTTP server has stopped.
|
|
||||||
*/
|
|
||||||
extern void HttpServerJoin(HttpServer *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Stop the HTTP server. Only the execution of this function will
|
|
||||||
* cause the proper shutdown of the HTTP server. If the main program
|
|
||||||
* is joined to the HTTP thread, then either another thread or a
|
|
||||||
* signal handler will have to stop the server using this function.
|
|
||||||
* The typical use case is to install a signal handler that executes
|
|
||||||
* this function on a global HTTP server.
|
|
||||||
*/
|
|
||||||
extern void HttpServerStop(HttpServer *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the request headers for the request represented by the given
|
|
||||||
* context. The data in the returned hash map should be treated as
|
|
||||||
* read only and should not be freed; it is managed entirely by the
|
|
||||||
* server.
|
|
||||||
*/
|
|
||||||
extern HashMap * HttpRequestHeaders(HttpServerContext *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the request method used to make the request represented by
|
|
||||||
* the given context.
|
|
||||||
*/
|
|
||||||
extern HttpRequestMethod HttpRequestMethodGet(HttpServerContext *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the request path for the request represented by the given
|
|
||||||
* context. The return value of this function should be treated as
|
|
||||||
* read-only, and should not be freed; it is managed entirely by the
|
|
||||||
* server.
|
|
||||||
*/
|
|
||||||
extern char * HttpRequestPath(HttpServerContext *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieve the parsed GET parameters for the request represented by
|
|
||||||
* the given context. The returned hash map should be treated as
|
|
||||||
* read-only, and should not be freed; it is managed entirely by the
|
|
||||||
* server.
|
|
||||||
*/
|
|
||||||
extern HashMap * HttpRequestParams(HttpServerContext *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Set a response header to return to the client. The old value for
|
|
||||||
* the given header is returned, if any, otherwise NULL is returned.
|
|
||||||
*/
|
|
||||||
extern char * HttpResponseHeader(HttpServerContext *, char *, char *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Set the response status to return to the client.
|
|
||||||
*/
|
|
||||||
extern void HttpResponseStatus(HttpServerContext *, HttpStatus);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the current response status that will be sent to the client
|
|
||||||
* making the request represented by the given context.
|
|
||||||
*/
|
|
||||||
extern HttpStatus HttpResponseStatusGet(HttpServerContext *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Send the response headers to the client that made the request
|
|
||||||
* represented by the specified context. This function must be called
|
|
||||||
* before the response body can be written, otherwise a malformed
|
|
||||||
* response will be sent.
|
|
||||||
*/
|
|
||||||
extern void HttpSendHeaders(HttpServerContext *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get a stream that is both readable and writable. Reading from the
|
|
||||||
* stream reads the request body that the client sent, if there is one.
|
|
||||||
* Note that the rquest headers have already been read, so the stream
|
|
||||||
* is correctly positioned at the beginning of the body of the request.
|
|
||||||
* .Fn HttpSendHeaders
|
|
||||||
* must be called before the stream is written, otherwise a malformed
|
|
||||||
* HTTP response will be sent. An HTTP handler should properly set all
|
|
||||||
* the headers it itends to send, send those headers, and then write
|
|
||||||
* the response body to this stream.
|
|
||||||
* .Pp
|
|
||||||
* Note that the stream does not need to be closed by the HTTP
|
|
||||||
* handler; in fact doing so results in undefined behavior. The stream
|
|
||||||
* is managed entirely by the server itself, so it will close it when
|
|
||||||
* necessary. This allows the underlying protocol to differ: for
|
|
||||||
* instance, an HTTP/1.1 connection may stay for multiple requests and
|
|
||||||
* responses.
|
|
||||||
*/
|
|
||||||
extern Stream * HttpServerStream(HttpServerContext *);
|
|
||||||
|
|
||||||
#endif /* CYTOPLASM_HTTPSERVER_H */
|
|
|
@ -1,122 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright (C) 2022-2023 Jordan Bancino <@jordan:bancino.net>
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person
|
|
||||||
* obtaining a copy of this software and associated documentation files
|
|
||||||
* (the "Software"), to deal in the Software without restriction,
|
|
||||||
* including without limitation the rights to use, copy, modify, merge,
|
|
||||||
* publish, distribute, sublicense, and/or sell copies of the Software,
|
|
||||||
* and to permit persons to whom the Software is furnished to do so,
|
|
||||||
* subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be
|
|
||||||
* included in all copies or portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
||||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
||||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
||||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
||||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
||||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
||||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
||||||
* SOFTWARE.
|
|
||||||
*/
|
|
||||||
#ifndef CYTOPLASM_INT_H
|
|
||||||
#define CYTOPLASM_INT_H
|
|
||||||
|
|
||||||
/***
|
|
||||||
* @Nm Int
|
|
||||||
* @Nd Fixed-width integer types.
|
|
||||||
* @Dd April 27 2023
|
|
||||||
*
|
|
||||||
* This header provides cross-platform, fixed-width integer types.
|
|
||||||
* Specifically, it uses preprocessor magic to define the following
|
|
||||||
* types:
|
|
||||||
* .Bl -bullet -offset indent
|
|
||||||
* .It
|
|
||||||
* Int8 and UInt8
|
|
||||||
* .It
|
|
||||||
* Int16 and UInt16
|
|
||||||
* .It
|
|
||||||
* Int32 and UInt32
|
|
||||||
* .El
|
|
||||||
* .Pp
|
|
||||||
* Note that there is no 64-bit integer type, because the ANSI C
|
|
||||||
* standard makes no guarantee that such a type will exist, even
|
|
||||||
* though it does on most platforms.
|
|
||||||
* .Pp
|
|
||||||
* The reason Cytoplasm provides its own header for this is
|
|
||||||
* because ANSI C does not define fixed-width types, and while it
|
|
||||||
* should be safe to rely on C99 fixed-width types in most cases,
|
|
||||||
* there may be cases where even that is not possible.
|
|
||||||
*
|
|
||||||
* @ignore-typedefs
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <limits.h>
|
|
||||||
|
|
||||||
#define BIT32_MAX 4294967295UL
|
|
||||||
#define BIT16_MAX 65535UL
|
|
||||||
#define BIT8_MAX 255UL
|
|
||||||
|
|
||||||
#ifndef UCHAR_MAX
|
|
||||||
#error Size of char data type is unknown. Define UCHAR_MAX.
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifndef USHRT_MAX
|
|
||||||
#error Size of short data type is unknown. Define USHRT_MAX.
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifndef UINT_MAX
|
|
||||||
#error Size of int data type is unknown. Define UINT_MAX.
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifndef ULONG_MAX
|
|
||||||
#error Size of long data type is unknown. Define ULONG_MAX.
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if UCHAR_MAX == BIT8_MAX
|
|
||||||
typedef signed char Int8;
|
|
||||||
typedef unsigned char UInt8;
|
|
||||||
|
|
||||||
#else
|
|
||||||
#error Unable to determine suitable data type for 8-bit integers.
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if UINT_MAX == BIT16_MAX
|
|
||||||
typedef signed int Int16;
|
|
||||||
typedef unsigned int UInt16;
|
|
||||||
|
|
||||||
#elif USHRT_MAX == BIT16_MAX
|
|
||||||
typedef signed short Int16;
|
|
||||||
typedef unsigned short UInt16;
|
|
||||||
|
|
||||||
#elif UCHAR_MAX == BIT16_MAX
|
|
||||||
typedef signed char Int16;
|
|
||||||
typedef unsigned char UInt16;
|
|
||||||
|
|
||||||
#else
|
|
||||||
#error Unable to determine suitable data type for 16-bit integers.
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if ULONG_MAX == BIT32_MAX
|
|
||||||
typedef signed long Int32;
|
|
||||||
typedef unsigned long UInt32;
|
|
||||||
|
|
||||||
#elif UINT_MAX == BIT32_MAX
|
|
||||||
typedef signed int Int32;
|
|
||||||
typedef unsigned int UInt32;
|
|
||||||
|
|
||||||
#elif USHRT_MAX == BIT32_MAX
|
|
||||||
typedef signed short Int32;
|
|
||||||
typedef unsigned short UInt32;
|
|
||||||
|
|
||||||
#elif UCHAR_MAX == BIT32_MAX
|
|
||||||
typedef signed char Int32;
|
|
||||||
typedef unsigned char UInt32;
|
|
||||||
|
|
||||||
#else
|
|
||||||
#error Unable to determine suitable data type for 32-bit integers.
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#endif /* CYTOPLASM_INT_H */
|
|
|
@ -1,252 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright (C) 2022-2023 Jordan Bancino <@jordan:bancino.net>
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person
|
|
||||||
* obtaining a copy of this software and associated documentation files
|
|
||||||
* (the "Software"), to deal in the Software without restriction,
|
|
||||||
* including without limitation the rights to use, copy, modify, merge,
|
|
||||||
* publish, distribute, sublicense, and/or sell copies of the Software,
|
|
||||||
* and to permit persons to whom the Software is furnished to do so,
|
|
||||||
* subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be
|
|
||||||
* included in all copies or portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
||||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
||||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
||||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
||||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
||||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
||||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
||||||
* SOFTWARE.
|
|
||||||
*/
|
|
||||||
#ifndef CYTOPLASM_INT64_H
|
|
||||||
#define CYTOPLASM_INT64_H
|
|
||||||
|
|
||||||
/***
|
|
||||||
* @Nm Int64
|
|
||||||
* @Nd Fixed-width 64 bit integers.
|
|
||||||
* @Dd August 11, 2023
|
|
||||||
*
|
|
||||||
* .Pp
|
|
||||||
* ANSI C89 (or C99 for that matter) provides no required mechanism
|
|
||||||
* for 64 bit integers. Nevertheless, many compilers provide them as
|
|
||||||
* extensions. However, since it is not a gaurantee, and to be fully
|
|
||||||
* standards-compliant and thus portable, a platform-agnostic interface
|
|
||||||
* is required. This header provides such an interface. If the platform
|
|
||||||
* has a 64 bit integer type, that is used, and native operations are
|
|
||||||
* performed by C preprocessor macro expansion. Otherwise, a
|
|
||||||
* compatibility layer is provided, which implements 64-bit
|
|
||||||
* arithmetic on an array of 2 32-bit numbers which are provided by
|
|
||||||
* .Xr Int 3 .
|
|
||||||
* .Pp
|
|
||||||
* Note that 64-bit emulation is certainly not as performant as using
|
|
||||||
* native 64-bit operations, so whenever possible, the native
|
|
||||||
* operations should be preferred. However, since C provides no required
|
|
||||||
* 64 bit integer on 32-bit and less platforms, this API can be used as
|
|
||||||
* a "good enough" fallback mechanism.
|
|
||||||
* .Pp
|
|
||||||
* Also note that this implementation, both in the native and
|
|
||||||
* non-native forms, makes some assumptions:
|
|
||||||
* .Bl -bullet -width Ds
|
|
||||||
* .It
|
|
||||||
* When a cast from a larger integer to a smaller integer is performed,
|
|
||||||
* the upper bits are truncated, not the lower bits.
|
|
||||||
* .It
|
|
||||||
* Negative numbers are represented in memory and in registers in two's
|
|
||||||
* compliment form.
|
|
||||||
* .El
|
|
||||||
* .Pp
|
|
||||||
* This API may provide unexpected output if these assumptions are
|
|
||||||
* false for a given platform.
|
|
||||||
*
|
|
||||||
* @ignore-typedefs
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <Int.h>
|
|
||||||
#include <UInt64.h>
|
|
||||||
|
|
||||||
#include <stddef.h>
|
|
||||||
|
|
||||||
#ifndef INT64_FORCE_EMULATED
|
|
||||||
|
|
||||||
#define BIT64_MAX 18446744073709551615UL
|
|
||||||
|
|
||||||
#if UINT_MAX == BIT64_MAX
|
|
||||||
typedef signed int Int64;
|
|
||||||
|
|
||||||
#define INT64_NATIVE
|
|
||||||
|
|
||||||
#elif ULONG_MAX == BIT64_MAX
|
|
||||||
typedef signed long Int64;
|
|
||||||
|
|
||||||
#define INT64_NATIVE
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#endif /* ifndef INT64_FORCE_EMULATED */
|
|
||||||
|
|
||||||
#ifdef INT64_NATIVE
|
|
||||||
|
|
||||||
#define Int64Create(high, low) ((Int64) (((UInt64) (high) << 32) | (low)))
|
|
||||||
#define Int64Neg(x) (-(x))
|
|
||||||
|
|
||||||
#define Int64Low(a) ((UInt32) (a))
|
|
||||||
#define Int64High(a) ((UInt32) ((a) >> 32))
|
|
||||||
|
|
||||||
#define Int64Add(a, b) ((a) + (b))
|
|
||||||
#define Int64Sub(a, b) ((a) - (b))
|
|
||||||
#define Int64Mul(a, b) ((a) * (b))
|
|
||||||
#define Int64Div(a, b) ((a) / (b))
|
|
||||||
#define Int64Rem(a, b) ((a) % (b))
|
|
||||||
|
|
||||||
#define Int64Sll(a, b) ((a) << (b))
|
|
||||||
#define Int64Sra(a, b) ((a) >> (b))
|
|
||||||
|
|
||||||
#define Int64And(a, b) ((a) & (b))
|
|
||||||
#define Int64Or(a, b) ((a) | (b))
|
|
||||||
#define Int64Xor(a, b) ((a) ^ (b))
|
|
||||||
#define Int64Not(a) (~(a))
|
|
||||||
|
|
||||||
#define Int64Eq(a, b) ((a) == (b))
|
|
||||||
#define Int64Lt(a, b) ((a) < (b))
|
|
||||||
#define Int64Gt(a, b) ((a) > (b))
|
|
||||||
|
|
||||||
#define Int64Neq(a, b) ((a) != (b))
|
|
||||||
#define Int64Leq(a, b) ((a) <= (b))
|
|
||||||
#define Int64Geq(a, b) ((a) >= (b))
|
|
||||||
|
|
||||||
#else
|
|
||||||
|
|
||||||
#define Int64Neg(x) (Int64Add(Int64Not(x), Int64Create(0, 1)))
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The internal bit representation of a signed integer is identical
|
|
||||||
* to an unsigned integer, the difference is in the algorithms and
|
|
||||||
* the way the bits are interpreted.
|
|
||||||
*/
|
|
||||||
typedef UInt64 Int64;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create a new signed 64 bit integer using the given high and low
|
|
||||||
* bits.
|
|
||||||
*/
|
|
||||||
extern Int64 Int64Create(UInt32, UInt32);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Add two signed 64 bit integers together.
|
|
||||||
*/
|
|
||||||
extern Int64 Int64Add(Int64, Int64);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Subtract the second 64 bit integer from the first.
|
|
||||||
*/
|
|
||||||
extern Int64 Int64Sub(Int64, Int64);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Multiply two 64 bit integers together. The non-native version of
|
|
||||||
* this function uses the Russian Peasant method of multiplication,
|
|
||||||
* which should afford more performance than a naive multiplication by
|
|
||||||
* addition, but it is still rather slow and depends on the size of
|
|
||||||
* the integers being multiplied.
|
|
||||||
*/
|
|
||||||
extern Int64 Int64Mul(Int64, Int64);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Divide the first 64 bit integer by the second and return the
|
|
||||||
* quotient. The non-native version of this function uses naive binary
|
|
||||||
* long division, which is slow, but gauranteed to finish in constant
|
|
||||||
* time.
|
|
||||||
*/
|
|
||||||
extern Int64 Int64Div(Int64, Int64);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Divide the first 64 bit integer by the second and return the
|
|
||||||
* remainder. The non-native version of this function uses naive binary
|
|
||||||
* long division, which is slow, but gauranteed to finish in constant
|
|
||||||
* time.
|
|
||||||
*/
|
|
||||||
extern Int64 Int64Rem(Int64, Int64);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Perform a left logical bit shift of a 64 bit integer. The second
|
|
||||||
* parameter is how many places to shift, and is declared as a regular
|
|
||||||
* integer because anything more than 64 does not make sense.
|
|
||||||
*/
|
|
||||||
extern Int64 Int64Sll(Int64, int);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Perform a right arithmetic bit shift of a 64 bit integer. The second
|
|
||||||
* parameter is how many places to shift, and is declared as a regular
|
|
||||||
* integer because anything more than 64 does not make sense.
|
|
||||||
* .Pp
|
|
||||||
* Note that on platforms that use the native 64-bit implementation,
|
|
||||||
* this is technically implementation-defined, and may in fact be a
|
|
||||||
* logical shift instead of an arithmetic shift. Note that typically
|
|
||||||
* this operation is not performed on signed integers.
|
|
||||||
*/
|
|
||||||
extern Int64 Int64Sra(Int64, int);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Perform a bitwise AND (&) of the provided 64 bit integers.
|
|
||||||
*/
|
|
||||||
extern Int64 Int64And(Int64, Int64);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Perform a bitwise OR (|) of the provided 64 bit integers.
|
|
||||||
*/
|
|
||||||
extern Int64 Int64Or(Int64, Int64);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Perform a bitwise XOR (^) of the provided 64 bit integers.
|
|
||||||
*/
|
|
||||||
extern Int64 Int64Xor(Int64, Int64);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Perform a bitwise NOT (~) of the provided 64 bit integer.
|
|
||||||
*/
|
|
||||||
extern Int64 Int64Not(Int64);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Perform a comparison of the provided 64 bit integers and return a C
|
|
||||||
* boolean that is true if and only if they are equal.
|
|
||||||
*/
|
|
||||||
extern int Int64Eq(Int64, Int64);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Perform a comparison of the provided 64 bit integers and return a C
|
|
||||||
* boolean that is true if and only if the second operand is strictly
|
|
||||||
* less than the first.
|
|
||||||
*/
|
|
||||||
extern int Int64Lt(Int64, Int64);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Perform a comparison of the provided 64 bit integers and return a C
|
|
||||||
* boolean that is true if and only if the second operand is strictly
|
|
||||||
* greater than the first.
|
|
||||||
*/
|
|
||||||
extern int Int64Gt(Int64, Int64);
|
|
||||||
|
|
||||||
#define Int64Low(a) ((a).i[0])
|
|
||||||
#define Int64High(a) ((a).i[1])
|
|
||||||
|
|
||||||
#define Int64Neq(a, b) (!Int64Eq(a, b))
|
|
||||||
#define Int64Leq(a, b) (Int64Eq(a, b) || Int64Lt(a, b))
|
|
||||||
#define Int64Geq(a, b) (Int64Eq(a, b) || Int64Gt(a, b))
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#define INT64_STRBUF 65 /* Base 2 representation with '\0' */
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Convert a 64 bit integer to a string in an arbitrary base
|
|
||||||
* representation specified by the second parameter, using the provided
|
|
||||||
* buffer and length specified by the third and fourth parameters. To
|
|
||||||
* guarantee that the string will fit in the buffer, allocate it of
|
|
||||||
* size INT64_STRBUF or larger. Note that a buffer size smaller than
|
|
||||||
* INT64_STRBUF will invoke undefined behavior.
|
|
||||||
*/
|
|
||||||
extern size_t Int64Str(Int64, int, char *, size_t);
|
|
||||||
|
|
||||||
#endif /* CYTOPLASM_INT64_H */
|
|
|
@ -1,222 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright (C) 2022-2023 Jordan Bancino <@jordan:bancino.net>
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person
|
|
||||||
* obtaining a copy of this software and associated documentation files
|
|
||||||
* (the "Software"), to deal in the Software without restriction,
|
|
||||||
* including without limitation the rights to use, copy, modify, merge,
|
|
||||||
* publish, distribute, sublicense, and/or sell copies of the Software,
|
|
||||||
* and to permit persons to whom the Software is furnished to do so,
|
|
||||||
* subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be
|
|
||||||
* included in all copies or portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
||||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
||||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
||||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
||||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
||||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
||||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
||||||
* SOFTWARE.
|
|
||||||
*/
|
|
||||||
#ifndef CYTOPLASM_IO_H
|
|
||||||
#define CYTOPLASM_IO_H
|
|
||||||
|
|
||||||
/***
|
|
||||||
* @Nm Io
|
|
||||||
* @Nd Source/sink-agnostic I/O for implementing custom streams.
|
|
||||||
* @Dd April 29 2023
|
|
||||||
* @Xr Stream Tls
|
|
||||||
*
|
|
||||||
* Many systems provide platform-specific means of implementing custom
|
|
||||||
* streams using file pointers. However, POSIX does not define a way
|
|
||||||
* of programmatically creating custom streams.
|
|
||||||
* .Nm
|
|
||||||
* therefore fills this gap in POSIX by mimicking all of the
|
|
||||||
* functionality of these platform-specific functions, but in pure
|
|
||||||
* POSIX C. It defines a number of callback funtions to be executed
|
|
||||||
* in place of the standard POSIX I/O functions, which are used to
|
|
||||||
* implement arbitrary streams that may not be to a file or socket.
|
|
||||||
* Additionally, streams can now be pipelined; the sink of one stream
|
|
||||||
* may be the source of another lower-level stream. Additionally, all
|
|
||||||
* streams, regardless of their source or sink, share the same API, so
|
|
||||||
* streams can be handled in a much more generic manner. This allows
|
|
||||||
* the HTTP client and server libraries to seemlessly support TLS and
|
|
||||||
* plain connections without having to handle each separately.
|
|
||||||
* .Pp
|
|
||||||
* .Nm
|
|
||||||
* was heavily inspired by GNU's
|
|
||||||
* .Fn fopencookie
|
|
||||||
* and BSD's
|
|
||||||
* .Fn funopen .
|
|
||||||
* It aims to combine the best of both of these functions into a single
|
|
||||||
* API that is intuitive and easy to use.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdarg.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
#include <sys/types.h>
|
|
||||||
|
|
||||||
#ifndef IO_BUFFER
|
|
||||||
#define IO_BUFFER 4096
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/**
|
|
||||||
* An opaque structure analogous to a POSIX file descriptor.
|
|
||||||
*/
|
|
||||||
typedef struct Io Io;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Read input from the source of a stream. This function should
|
|
||||||
* attempt to read the specified number of bytes of data from the
|
|
||||||
* given cookie into the given buffer. It should behave identically
|
|
||||||
* to the POSIX
|
|
||||||
* .Xr read 2
|
|
||||||
* system call, except instead of using an integer descriptor as the
|
|
||||||
* first parameter, a pointer to an implementation-defined cookie
|
|
||||||
* stores any information the function needs to read from the source.
|
|
||||||
*/
|
|
||||||
typedef ssize_t (IoReadFunc) (void *, void *, size_t);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Write output to a sink. This function should attempt to write the
|
|
||||||
* specified number of bytes of data from the given buffer into the
|
|
||||||
* stream described by the given cookie. It should behave identically
|
|
||||||
* to the POSIX
|
|
||||||
* .Xr write 2
|
|
||||||
* system call, except instead of using an integer descriptor as the
|
|
||||||
* first parameter, a pointer to an implementation-defined cookie
|
|
||||||
* stores any information the function needs to write to the sink.
|
|
||||||
*/
|
|
||||||
typedef ssize_t (IoWriteFunc) (void *, void *, size_t);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Repositions the offset of the stream described by the specified
|
|
||||||
* cookie. This function should behave identically to the POSIX
|
|
||||||
* .Xr lseek 2
|
|
||||||
* system call, except instead of using an integer descriptor as the
|
|
||||||
* first parameter, a pointer to an implementation-defined cookie
|
|
||||||
* stores any information the function needs to seek the stream.
|
|
||||||
*/
|
|
||||||
typedef off_t (IoSeekFunc) (void *, off_t, int);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Close the given stream, making future reads or writes result in
|
|
||||||
* undefined behavior. This function should also free all memory
|
|
||||||
* associated with the cookie. It should behave identically to the
|
|
||||||
* .Xr close 2
|
|
||||||
* system call, except instead of using an integer descriptor for the
|
|
||||||
* parameter, a pointer to an implementation-defined cookie stores any
|
|
||||||
* information the function needs to close the stream.
|
|
||||||
*/
|
|
||||||
typedef int (IoCloseFunc) (void *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A simple mechanism for grouping together a set of stream functions,
|
|
||||||
* to be passed to
|
|
||||||
* .Fn IoCreate .
|
|
||||||
*/
|
|
||||||
typedef struct IoFunctions
|
|
||||||
{
|
|
||||||
IoReadFunc *read;
|
|
||||||
IoWriteFunc *write;
|
|
||||||
IoSeekFunc *seek;
|
|
||||||
IoCloseFunc *close;
|
|
||||||
} IoFunctions;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create a new stream using the specified cookie and the specified
|
|
||||||
* I/O functions.
|
|
||||||
*/
|
|
||||||
extern Io * IoCreate(void *, IoFunctions);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Read the specified number of bytes from the specified stream into
|
|
||||||
* the specified buffer. This calls the stream's underlying IoReadFunc,
|
|
||||||
* which should behave identically to the POSIX
|
|
||||||
* .Xr read 2
|
|
||||||
* system call.
|
|
||||||
*/
|
|
||||||
extern ssize_t IoRead(Io *, void *, size_t);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Write the specified number of bytes from the specified stream into
|
|
||||||
* the specified buffer. This calls the stream's underlying
|
|
||||||
* IoWriteFunc, which should behave identically to the POSIX
|
|
||||||
* .Xr write 2
|
|
||||||
* system call.
|
|
||||||
*/
|
|
||||||
extern ssize_t IoWrite(Io *, void *, size_t);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Seek the specified stream using the specified offset and whence
|
|
||||||
* value. This calls the stream's underlying IoSeekFunc, which should
|
|
||||||
* behave identically to the POSIX
|
|
||||||
* .Xr lseek 2
|
|
||||||
* system call.
|
|
||||||
*/
|
|
||||||
extern off_t IoSeek(Io *, off_t, int);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Close the specified stream. This calls the stream's underlying
|
|
||||||
* IoCloseFunc, which should behave identically to the POSIX
|
|
||||||
* .Xr close 2
|
|
||||||
* system call.
|
|
||||||
*/
|
|
||||||
extern int IoClose(Io *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Print a formatted string to the given stream. This is a
|
|
||||||
* re-implementation of the standard library function
|
|
||||||
* .Xr vfprintf 3 ,
|
|
||||||
* and behaves identically.
|
|
||||||
*/
|
|
||||||
extern int IoVprintf(Io *, const char *, va_list);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Print a formatted string to the given stream. This is a
|
|
||||||
* re-implementation of the standard library function
|
|
||||||
* .Xr fprintf 3 ,
|
|
||||||
* and behaves identically.
|
|
||||||
*/
|
|
||||||
extern int IoPrintf(Io *, const char *,...);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Read all the bytes from the first stream and write them to the
|
|
||||||
* second stream. Neither stream is closed upon the completion of this
|
|
||||||
* function. This can be used for quick and convenient buffered
|
|
||||||
* copying of data from one stream into another.
|
|
||||||
*/
|
|
||||||
extern ssize_t IoCopy(Io *, Io *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Wrap a POSIX file descriptor to take advantage of this API. The
|
|
||||||
* common use case for this function is when a regular file descriptor
|
|
||||||
* needs to be accessed by an application that uses this API to also
|
|
||||||
* access non-POSIX streams.
|
|
||||||
*/
|
|
||||||
extern Io * IoFd(int);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Open or create a file for reading or writing. The specified file
|
|
||||||
* name is opened for reading or writing as specified by the given
|
|
||||||
* flags and mode. This function is a simple convenience wrapper around
|
|
||||||
* the POSIX
|
|
||||||
* .Xr open 2
|
|
||||||
* system call that passes the opened file descriptor into
|
|
||||||
* .Fn IoFd .
|
|
||||||
*/
|
|
||||||
extern Io * IoOpen(const char *, int, mode_t);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Wrap a standard C file pointer to take advantage of this API. The
|
|
||||||
* common use case for this function is when a regular C file pointer
|
|
||||||
* needs to be accessed by an application that uses this API to also
|
|
||||||
* access custom streams.
|
|
||||||
*/
|
|
||||||
extern Io * IoFile(FILE *);
|
|
||||||
|
|
||||||
#endif /* CYTOPLASM_IO_H */
|
|
|
@ -1,323 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright (C) 2022-2023 Jordan Bancino <@jordan:bancino.net>
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person
|
|
||||||
* obtaining a copy of this software and associated documentation files
|
|
||||||
* (the "Software"), to deal in the Software without restriction,
|
|
||||||
* including without limitation the rights to use, copy, modify, merge,
|
|
||||||
* publish, distribute, sublicense, and/or sell copies of the Software,
|
|
||||||
* and to permit persons to whom the Software is furnished to do so,
|
|
||||||
* subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be
|
|
||||||
* included in all copies or portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
||||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
||||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
||||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
||||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
||||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
||||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
||||||
* SOFTWARE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef CYTOPLASM_JSON_H
|
|
||||||
#define CYTOPLASM_JSON_H
|
|
||||||
|
|
||||||
/***
|
|
||||||
* @Nm Json
|
|
||||||
* @Nd A fully-featured JSON API.
|
|
||||||
* @Dd March 12 2023
|
|
||||||
* @Xr HashMap Array Stream
|
|
||||||
*
|
|
||||||
* .Nm
|
|
||||||
* is a fully-featured JSON API for C using the array and hash map
|
|
||||||
* APIs. It can parse JSON, ans serialize an in-memory structure to
|
|
||||||
* JSON. It build on the foundation of Array and HashMap because that's
|
|
||||||
* all JSON really is, just arrays and maps.
|
|
||||||
* .Nm
|
|
||||||
* also provides a structure for encapsulating an arbitrary value and
|
|
||||||
* identifying its type, making it easy for a strictly-typed language
|
|
||||||
* like C to work with loosely-typed JSON data.
|
|
||||||
* .Nm
|
|
||||||
* is very strict and tries to adhere as closely as possible to the
|
|
||||||
* proper definition of JSON. It will fail on syntax errors of any
|
|
||||||
* kind, which is fine for a Matrix homeserver that can just return
|
|
||||||
* M_BAD_JSON if anything in here fails, but this behavior may not be
|
|
||||||
* suitable for other purposes.
|
|
||||||
* .Pp
|
|
||||||
* This JSON implementation focuses primarily on serialization and
|
|
||||||
* deserialization to and from streams. It does not provide facilities
|
|
||||||
* for handling JSON strings; it only writes JSON to output streams,
|
|
||||||
* and reads them from input streams. Of course, you can use the
|
|
||||||
* POSIX
|
|
||||||
* .Xr fmemopen 3
|
|
||||||
* and
|
|
||||||
* .Xr open_memstream 3
|
|
||||||
* functions if you want to deal with JSON strings, but JSON is
|
|
||||||
* intended to be an exchange format. Data should be converted to JSON
|
|
||||||
* right when it is leaving the program, and converted from JSON to the
|
|
||||||
* in-memory format as soon as it is coming in.
|
|
||||||
* .Pp
|
|
||||||
* JSON objects are represented as hash maps consisting entirely of
|
|
||||||
* JsonValue structures, and arrays are represented as arrays
|
|
||||||
* consisting entirely of JsonValue structures. When generating a
|
|
||||||
* JSON object, any attempt to stuff a value into a hash map or array
|
|
||||||
* without first encoding it as a JsonValue will result in undefined
|
|
||||||
* behavior.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <HashMap.h>
|
|
||||||
#include <Array.h>
|
|
||||||
#include <Stream.h>
|
|
||||||
#include <Int64.h>
|
|
||||||
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stddef.h>
|
|
||||||
|
|
||||||
#define JSON_DEFAULT -1
|
|
||||||
#define JSON_PRETTY 0
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This opaque structure encapsulates all the possible types that can
|
|
||||||
* be stored in JSON. It is managed entirely by the functions defined
|
|
||||||
* in this API. It is important to note that strings, integers, floats,
|
|
||||||
* booleans, and the NULL value are all stored by value, but objects
|
|
||||||
* and arrays are stored by reference. That is, it doesn't store these
|
|
||||||
* itself, just pointers to them, however, the data
|
|
||||||
* .Em is
|
|
||||||
* freed when using
|
|
||||||
* .Fn JsonFree .
|
|
||||||
*/
|
|
||||||
typedef struct JsonValue JsonValue;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* These are the types that can be used to identify a JsonValue
|
|
||||||
* and act on it accordingly.
|
|
||||||
*/
|
|
||||||
typedef enum JsonType
|
|
||||||
{
|
|
||||||
JSON_NULL, /* Maps to a C NULL */
|
|
||||||
JSON_OBJECT, /* Maps to a HashMap of JsonValues */
|
|
||||||
JSON_ARRAY, /* Maps to an Array of JsonValues */
|
|
||||||
JSON_STRING, /* Maps to a null-terminated C string */
|
|
||||||
JSON_INTEGER, /* Maps to an Int64 */
|
|
||||||
JSON_FLOAT, /* Maps to a C double */
|
|
||||||
JSON_BOOLEAN /* Maps to a C integer of either 0 or 1 */
|
|
||||||
} JsonType;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Determine the type of the specified JSON value.
|
|
||||||
*/
|
|
||||||
extern JsonType JsonValueType(JsonValue *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Encode a JSON object as a JSON value that can be added to another
|
|
||||||
* object, or an array.
|
|
||||||
*/
|
|
||||||
extern JsonValue * JsonValueObject(HashMap *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Unwrap a JSON value that represents an object. This function will
|
|
||||||
* return NULL if the value is not actually an object.
|
|
||||||
*/
|
|
||||||
extern HashMap * JsonValueAsObject(JsonValue *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Encode a JSON array as a JSON value that can be added to an object
|
|
||||||
* or another array.
|
|
||||||
*/
|
|
||||||
extern JsonValue * JsonValueArray(Array *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Unwrap a JSON value that represents an array. This function will
|
|
||||||
* return NULL if the value is not actually an array.
|
|
||||||
*/
|
|
||||||
extern Array * JsonValueAsArray(JsonValue *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Encode a C string as a JSON value that can be added to an object or
|
|
||||||
* an array.
|
|
||||||
*/
|
|
||||||
extern JsonValue * JsonValueString(char *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Unwrap a JSON value that represents a string. This function will
|
|
||||||
* return NULL if the value is not actually a string.
|
|
||||||
*/
|
|
||||||
extern char * JsonValueAsString(JsonValue *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Encode a number as a JSON value that can be added to an object or
|
|
||||||
* an array.
|
|
||||||
*/
|
|
||||||
extern JsonValue * JsonValueInteger(Int64);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Unwrap a JSON value that represents a number. This function will
|
|
||||||
* return 0 if the value is not actually a number, which may be
|
|
||||||
* misleading. Check the type of the value before making assumptions
|
|
||||||
* about its value.
|
|
||||||
*/
|
|
||||||
extern Int64 JsonValueAsInteger(JsonValue *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Encode a floating point number as a JSON value that can be added
|
|
||||||
* to an object or an array.
|
|
||||||
*/
|
|
||||||
extern JsonValue * JsonValueFloat(double);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Unwrap a JSON value that represents a floating point number. This
|
|
||||||
* function will return 0 if the value is not actually a floating
|
|
||||||
* point number, which may be misleading. Check the type of the value
|
|
||||||
* before making assumptions about its type.
|
|
||||||
*/
|
|
||||||
extern double JsonValueAsFloat(JsonValue *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Encode a C integer according to the way C treats integers in boolean
|
|
||||||
* expressions as a JSON value that can be added to an object or an
|
|
||||||
* array.
|
|
||||||
*/
|
|
||||||
extern JsonValue * JsonValueBoolean(int);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Unwrap a JSON value that represents a boolean. This function will
|
|
||||||
* return 0 if the value is not actually a boolean, which may be
|
|
||||||
* misleading. Check the type of the value before making assumptions
|
|
||||||
* about its type.
|
|
||||||
*/
|
|
||||||
extern int JsonValueAsBoolean(JsonValue *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This is a special case that represents a JSON null. Because the
|
|
||||||
* Array and HashMap APIs do not accept NULL values, this function
|
|
||||||
* should be used to represent NULL in JSON. Even though a small
|
|
||||||
* amount of memory is allocated just to be a placeholder for nothing,
|
|
||||||
* this keeps the APIs clean.
|
|
||||||
*/
|
|
||||||
extern JsonValue * JsonValueNull(void);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Free the memory being used by a JSON value. Note that this will
|
|
||||||
* recursively free all Arrays, HashMaps, and other JsonValues that are
|
|
||||||
* reachable from the given value, including any strings attached to
|
|
||||||
* this value.
|
|
||||||
*/
|
|
||||||
extern void JsonValueFree(JsonValue *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Recursively duplicate the given JSON value. This returns a new
|
|
||||||
* JSON value that is completely identical to the specified value, but
|
|
||||||
* in no way connected to it.
|
|
||||||
*/
|
|
||||||
extern JsonValue * JsonValueDuplicate(JsonValue *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Recursively duplicate the given JSON object. This returns a new
|
|
||||||
* JSON object that is completely identical to the specified object,
|
|
||||||
* but in no way connect to it.
|
|
||||||
*/
|
|
||||||
extern HashMap * JsonDuplicate(HashMap *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Recursively free a JSON object by iterating over all of its values
|
|
||||||
* and freeing them using
|
|
||||||
* .Fn JsonValueFree .
|
|
||||||
*/
|
|
||||||
extern void JsonFree(HashMap *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Encode the given string in such a way that it can be safely
|
|
||||||
* embedded in a JSON stream. This entails:
|
|
||||||
* .Bl -bullet -offset indent
|
|
||||||
* .It
|
|
||||||
* Escaping quotes, backslashes, and other special characters using
|
|
||||||
* their backslash escape.
|
|
||||||
* .It
|
|
||||||
* Encoding bytes that are not UTF-8 using escapes.
|
|
||||||
* .It
|
|
||||||
* Wrapping the entire string in double quotes.
|
|
||||||
* .El
|
|
||||||
* .Pp
|
|
||||||
* This function is only provided via the public
|
|
||||||
* .Nm
|
|
||||||
* API so that it is accessible to custom JSON encoders, such as the
|
|
||||||
* CanonicalJson encoder. This will typically be used for encoding
|
|
||||||
* object keys; to encode values, just use
|
|
||||||
* .Fn JsonEncodeValue .
|
|
||||||
* .Pp
|
|
||||||
* This function returns the number of bytes written to the stream,
|
|
||||||
* or if the stream is NULL, the number of bytes that would have
|
|
||||||
* been written.
|
|
||||||
*/
|
|
||||||
extern int JsonEncodeString(const char *, Stream *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Serialize a JSON value as it would appear in JSON output. This is
|
|
||||||
* a recursive function that also encodes all child values reachable
|
|
||||||
* from the given value. This function is exposed via the public
|
|
||||||
* .Nm
|
|
||||||
* API so that it is accessible to custom JSON encoders. Normal users
|
|
||||||
* that are not writing custom encoders should in most cases just use
|
|
||||||
* .Fn JsonEncode
|
|
||||||
* to encode an entire object.
|
|
||||||
* .Pp
|
|
||||||
* The third parameter is an integer that represents the indent level
|
|
||||||
* of the value to be printed, or a negative number if pretty-printing
|
|
||||||
* should be disabled and JSON should be printed as minimized as
|
|
||||||
* possible. To pretty-print a JSON object, set this to
|
|
||||||
* .Va JSON_PRETTY .
|
|
||||||
* To get minified output, set it to
|
|
||||||
* .Va JSON_DEFAULT .
|
|
||||||
* .Pp
|
|
||||||
* This function returns the number of bytes written to the stream,
|
|
||||||
* or if the stream is NULL, the number of bytes that would have
|
|
||||||
* been written.
|
|
||||||
*/
|
|
||||||
extern int JsonEncodeValue(JsonValue *, Stream *, int);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Encode a JSON object as it would appear in JSON output, writing it
|
|
||||||
* to the given output stream. This function is recursive; it will
|
|
||||||
* serialize everything accessible from the passed object. The third
|
|
||||||
* parameter has the same behavior as described above.
|
|
||||||
* .Pp
|
|
||||||
* This function returns the number of bytes written to the stream,
|
|
||||||
* or if the stream is NULL, the number of bytes that would have
|
|
||||||
* been written.
|
|
||||||
*/
|
|
||||||
extern int JsonEncode(HashMap *, Stream *, int);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Decode a JSON object from the given input stream and parse it into
|
|
||||||
* a hash map of JSON values.
|
|
||||||
*/
|
|
||||||
extern HashMap * JsonDecode(Stream *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A convenience function that allows the caller to retrieve and
|
|
||||||
* arbitrarily deep keys within a JSON object. It takes a root JSON
|
|
||||||
* object, the number of levels deep to go, and then that number of
|
|
||||||
* keys as a varargs list. All keys must have objects as values, with
|
|
||||||
* the exception of the last one, which is the value that will be
|
|
||||||
* returned. Otherwise, NULL indicates the specified path doas not
|
|
||||||
* exist.
|
|
||||||
*/
|
|
||||||
extern JsonValue * JsonGet(HashMap *, size_t,...);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A convenience function that allows the caller to set arbitrarily
|
|
||||||
* deep keys within a JSON object. It takes a root JSON object, the
|
|
||||||
* number of levels deep to go, and then that number of keys as a
|
|
||||||
* varargs list. All keys must have object as values, with the
|
|
||||||
* exception of the last one, which is the value that will be set.
|
|
||||||
* The value currently at that key, if any, will be returned.
|
|
||||||
* This function will create any intermediate objects as necessary to
|
|
||||||
* set the proper key.
|
|
||||||
*/
|
|
||||||
extern JsonValue * JsonSet(HashMap *, JsonValue *, size_t,...);
|
|
||||||
|
|
||||||
#endif /* CYTOPLASM_JSON_H */
|
|
|
@ -1,196 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright (C) 2022-2023 Jordan Bancino <@jordan:bancino.net>
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person
|
|
||||||
* obtaining a copy of this software and associated documentation files
|
|
||||||
* (the "Software"), to deal in the Software without restriction,
|
|
||||||
* including without limitation the rights to use, copy, modify, merge,
|
|
||||||
* publish, distribute, sublicense, and/or sell copies of the Software,
|
|
||||||
* and to permit persons to whom the Software is furnished to do so,
|
|
||||||
* subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be
|
|
||||||
* included in all copies or portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
||||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
||||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
||||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
||||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
||||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
||||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
||||||
* SOFTWARE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef CYTOPLASM_LOG_H
|
|
||||||
#define CYTOPLASM_LOG_H
|
|
||||||
|
|
||||||
/***
|
|
||||||
* @Nm Log
|
|
||||||
* @Nd A simple logging framework for logging to multiple destinations.
|
|
||||||
* @Dd April 27 2023
|
|
||||||
* @Xr Stream
|
|
||||||
*
|
|
||||||
* .Nm
|
|
||||||
* is a simple C logging library that allows for colorful outputs,
|
|
||||||
* timestamps, and custom log levels. It also features the ability to
|
|
||||||
* have multiple logs open at one time, although Cytoplasm primarily
|
|
||||||
* utilizes the global log. All logs are thread safe.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stddef.h>
|
|
||||||
#include <syslog.h>
|
|
||||||
|
|
||||||
#include <Stream.h>
|
|
||||||
|
|
||||||
#define LOG_FLAG_COLOR (1 << 0)
|
|
||||||
#define LOG_FLAG_SYSLOG (1 << 1)
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A log is defined as a configuration that describes the properties
|
|
||||||
* of the log. This opaque structure can be manipulated by the
|
|
||||||
* functions defined in this API.
|
|
||||||
*/
|
|
||||||
typedef struct LogConfig LogConfig;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create a new log configuration with sane defaults that can be used
|
|
||||||
* immediately with the logging functions.
|
|
||||||
*/
|
|
||||||
extern LogConfig * LogConfigCreate(void);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the global log configuration, creating a new one with
|
|
||||||
* .Fn LogConfigCreate
|
|
||||||
* if necessary.
|
|
||||||
*/
|
|
||||||
extern LogConfig * LogConfigGlobal(void);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Free the given log configuration. Note that this does not close the
|
|
||||||
* underlying stream associated with the log, if there is one. Also
|
|
||||||
* note that to avoid memory leaks, the global log configuration must
|
|
||||||
* also be freed, but it cannot be used after it is freed.
|
|
||||||
*/
|
|
||||||
extern void LogConfigFree(LogConfig *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Set the current log level on the specified log configuration.
|
|
||||||
* This indicates that only messages at or above this level should be
|
|
||||||
* logged; all others are silently discarded. The passed log level
|
|
||||||
* should be one of the log levels defined by
|
|
||||||
* .Xr syslog 3 .
|
|
||||||
* Refer to that page for a complete list of acceptable log levels,
|
|
||||||
* and note that passing an invalid log level will result in undefined
|
|
||||||
* behavior.
|
|
||||||
*/
|
|
||||||
extern void LogConfigLevelSet(LogConfig *, int);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Cause the log output to be indented two more spaces than it was
|
|
||||||
* previously. This can be helpful when generating stack traces or
|
|
||||||
* other hierarchical output. This is a simple convenience wrapper
|
|
||||||
* around
|
|
||||||
* .Fn LogConfigIndentSet .
|
|
||||||
*/
|
|
||||||
extern void LogConfigIndent(LogConfig *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Cause the log output to be indented two less spaces than it was
|
|
||||||
* previously. This is a simple convenience wrapper around
|
|
||||||
* .Fn LogConfigIndentSet .
|
|
||||||
*/
|
|
||||||
extern void LogConfigUnindent(LogConfig *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Indent future log output using the specified config by some
|
|
||||||
* arbitrary amount.
|
|
||||||
*/
|
|
||||||
extern void LogConfigIndentSet(LogConfig *, size_t);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Set the file stream that logging output should be written to. This
|
|
||||||
* defaults to standard output, but it can be set to standard error,
|
|
||||||
* or any other arbitrary stream. Passing a NULL value for the stream
|
|
||||||
* pointer sets the log output to the standard output. Note that the
|
|
||||||
* output stream is only used if
|
|
||||||
* .Va LOG_FLAG_SYSLOG
|
|
||||||
* is not set.
|
|
||||||
*/
|
|
||||||
extern void LogConfigOutputSet(LogConfig *, Stream *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Set a number of boolean options on a log configuration. This
|
|
||||||
* function uses bitwise operators, so multiple options can be set with
|
|
||||||
* a single function call using bitwise OR operators. The flags are
|
|
||||||
* defined as preprocessor macros, and are as follows:
|
|
||||||
* .Bl -tag -width Ds
|
|
||||||
* .It LOG_FLAG_COLOR
|
|
||||||
* When set, enable color-coded output on TTYs. Note that colors are
|
|
||||||
* implemented as ANSI escape sequences, and are not written to file
|
|
||||||
* streams that are not actually connected to a TTY, to prevent those
|
|
||||||
* sequences from being written to a file.
|
|
||||||
* .Xr isatty 3
|
|
||||||
* is checked before writing any terminal sequences.
|
|
||||||
* .It LOG_FLAG_SYSLOG
|
|
||||||
* When set, log output to the syslog using
|
|
||||||
* .Xr syslog 3 ,
|
|
||||||
* instead of logging to the file set by
|
|
||||||
* .Fn LogConfigOutputSet .
|
|
||||||
* This flag always overrides the stream set by that function,
|
|
||||||
* regardless of when it was set, even if it was set after this flag
|
|
||||||
* was set.
|
|
||||||
* .El
|
|
||||||
*/
|
|
||||||
extern void LogConfigFlagSet(LogConfig *, int);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Clear a boolean flag from the specified log format. See above for
|
|
||||||
* the list of flags.
|
|
||||||
*/
|
|
||||||
extern void LogConfigFlagClear(LogConfig *, int);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Set a custom timestamp to be prepended to each message if the
|
|
||||||
* output is not going to the system log. Consult your system's
|
|
||||||
* documentation for
|
|
||||||
* .Xr strftime 3 .
|
|
||||||
* A value of NULL disables the timestamp output before messages.
|
|
||||||
*/
|
|
||||||
extern void LogConfigTimeStampFormatSet(LogConfig *, char *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This function does the actual logging of messages using a
|
|
||||||
* specified configuration. It takes the configuration, the log
|
|
||||||
* level, a format string, and then a list of arguments, all in that
|
|
||||||
* order. This function only logs messages if their level is above
|
|
||||||
* or equal to the currently configured log level, making it easy to
|
|
||||||
* turn some messages on or off.
|
|
||||||
* .Pp
|
|
||||||
* This function has the same usage as
|
|
||||||
* .Xr vprintf 3 .
|
|
||||||
* Consult that page for the list of format specifiers and their
|
|
||||||
* arguments. This function is typically not used directly, see the
|
|
||||||
* other log functions for the most common use cases.
|
|
||||||
*/
|
|
||||||
extern void Logv(LogConfig *, int, const char *, va_list);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Log a message using
|
|
||||||
* .Fn Logv .
|
|
||||||
* with the specified configuration. This function has the same usage
|
|
||||||
* as
|
|
||||||
* .Xr printf 3 .
|
|
||||||
*/
|
|
||||||
extern void LogTo(LogConfig *, int, const char *, ...);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Log a message to the global log using
|
|
||||||
* .Fn Logv .
|
|
||||||
* This function has the same usage as
|
|
||||||
* .Xr printf 3 .
|
|
||||||
*/
|
|
||||||
extern void Log(int, const char *, ...);
|
|
||||||
|
|
||||||
#endif
|
|
|
@ -1,235 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright (C) 2022-2023 Jordan Bancino <@jordan:bancino.net>
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person
|
|
||||||
* obtaining a copy of this software and associated documentation files
|
|
||||||
* (the "Software"), to deal in the Software without restriction,
|
|
||||||
* including without limitation the rights to use, copy, modify, merge,
|
|
||||||
* publish, distribute, sublicense, and/or sell copies of the Software,
|
|
||||||
* and to permit persons to whom the Software is furnished to do so,
|
|
||||||
* subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be
|
|
||||||
* included in all copies or portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
||||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
||||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
||||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
||||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
||||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
||||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
||||||
* SOFTWARE.
|
|
||||||
*/
|
|
||||||
#ifndef CYTOPLASM_MEMORY_H
|
|
||||||
#define CYTOPLASM_MEMORY_H
|
|
||||||
|
|
||||||
/***
|
|
||||||
* @Nm Memory
|
|
||||||
* @Nd Smart memory management.
|
|
||||||
* @Dd January 9 2023
|
|
||||||
*
|
|
||||||
* .Nm
|
|
||||||
* is an API that allows for smart memory management and profiling. It
|
|
||||||
* wraps the standard library functions
|
|
||||||
* .Xr malloc 3 ,
|
|
||||||
* .Xr realloc 3 ,
|
|
||||||
* and
|
|
||||||
* .Xr free 3 ,
|
|
||||||
* and offers identical semantics, while providing functionality that
|
|
||||||
* the standard library doesn't have, such as getting statistics on the
|
|
||||||
* total memory allocated on the heap, and getting the size of a block
|
|
||||||
* given a pointer. Additionally, thanks to preprocessor macros, the
|
|
||||||
* exact file and line number at which an allocation, re-allocation, or
|
|
||||||
* free occured can be obtained given a pointer. Finally, all the
|
|
||||||
* blocks allocated on the heap can be iterated and evaluated, and a
|
|
||||||
* callback function can be executed every time a memory operation
|
|
||||||
* occurs.
|
|
||||||
* .Pp
|
|
||||||
* In the future, this API could include a garbage collector that
|
|
||||||
* automatically frees memory it detects as being no longer in use.
|
|
||||||
* However, this feature does not yet exist.
|
|
||||||
* .Pp
|
|
||||||
* A number of macros are available, which make the
|
|
||||||
* .Nm
|
|
||||||
* API much easier to use. They are as follows:
|
|
||||||
* .Bl -bullet -offset indent
|
|
||||||
* .It
|
|
||||||
* .Fn Malloc "x"
|
|
||||||
* .It
|
|
||||||
* .Fn Realloc "x" "y"
|
|
||||||
* .It
|
|
||||||
* .Fn Free "x"
|
|
||||||
* .El
|
|
||||||
* .Pp
|
|
||||||
* These macros expand to
|
|
||||||
* .Fn MemoryAllocate ,
|
|
||||||
* .Fn MemoryReallocate ,
|
|
||||||
* and
|
|
||||||
* .Fn MemoryFree
|
|
||||||
* with the second and third parameters set to __FILE__ and __LINE__.
|
|
||||||
* This allows
|
|
||||||
* .Nm
|
|
||||||
* to be used exactly how the standard library functions would be
|
|
||||||
* used. In fact, the functions to which these macros expand are not
|
|
||||||
* intended to be used directly; for the best results, use these
|
|
||||||
* macros.
|
|
||||||
*/
|
|
||||||
#include <stddef.h>
|
|
||||||
|
|
||||||
/**
|
|
||||||
* These values are passed into the memory hook function to indicate
|
|
||||||
* the action that just happened.
|
|
||||||
*/
|
|
||||||
typedef enum MemoryAction
|
|
||||||
{
|
|
||||||
MEMORY_ALLOCATE,
|
|
||||||
MEMORY_REALLOCATE,
|
|
||||||
MEMORY_FREE,
|
|
||||||
MEMORY_BAD_POINTER,
|
|
||||||
MEMORY_CORRUPTED
|
|
||||||
} MemoryAction;
|
|
||||||
|
|
||||||
#define Malloc(x) MemoryAllocate(x, __FILE__, __LINE__)
|
|
||||||
#define Realloc(x, s) MemoryReallocate(x, s, __FILE__, __LINE__)
|
|
||||||
#define Free(x) MemoryFree(x, __FILE__, __LINE__)
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The memory information is opaque, but can be accessed using the
|
|
||||||
* functions defined by this API.
|
|
||||||
*/
|
|
||||||
typedef struct MemoryInfo MemoryInfo;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Allocate the specified number of bytes on the heap. This function
|
|
||||||
* has the same semantics as
|
|
||||||
* .Xr malloc 3 ,
|
|
||||||
* except that it takes the file name and line number at which the
|
|
||||||
* allocation occurred.
|
|
||||||
*/
|
|
||||||
extern void * MemoryAllocate(size_t, const char *, int);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Change the size of the object pointed to by the given pointer
|
|
||||||
* to the given number of bytes. This function has the same semantics
|
|
||||||
* as
|
|
||||||
* .Xr realloc 3 ,
|
|
||||||
* except that it takes the file name and line number at which the
|
|
||||||
* reallocation occurred.
|
|
||||||
*/
|
|
||||||
extern void * MemoryReallocate(void *, size_t, const char *, int);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Free the memory at the given pointer. This function has the same
|
|
||||||
* semantics as
|
|
||||||
* .Xr free 3 ,
|
|
||||||
* except that it takes the file name and line number at which the
|
|
||||||
* free occurred.
|
|
||||||
*/
|
|
||||||
extern void MemoryFree(void *, const char *, int);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the total number of bytes that the program has allocated on the
|
|
||||||
* heap. This operation iterates over all heap allocations made with
|
|
||||||
* .Fn MemoryAllocate
|
|
||||||
* and then returns a total count, in bytes.
|
|
||||||
*/
|
|
||||||
extern size_t MemoryAllocated(void);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Iterate over all heap allocations made with
|
|
||||||
* .Fn MemoryAllocate
|
|
||||||
* and call
|
|
||||||
* .Fn MemoryFree
|
|
||||||
* on them. This function immediately invalidates all pointers to
|
|
||||||
* blocks on the heap, and any subsequent attempt to read or write to
|
|
||||||
* data on the heap will result in undefined behavior. This is
|
|
||||||
* typically called at the end of the program, just before exit.
|
|
||||||
*/
|
|
||||||
extern void MemoryFreeAll(void);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Fetch information about an allocation. This function takes a raw
|
|
||||||
* pointer, and if
|
|
||||||
* . Nm
|
|
||||||
* knows about the pointer, it returns a structure that can be used
|
|
||||||
* to obtain information about the block of memory that the pointer
|
|
||||||
* points to.
|
|
||||||
*/
|
|
||||||
extern MemoryInfo * MemoryInfoGet(void *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the size in bytes of the block of memory represented by the
|
|
||||||
* specified memory info structure.
|
|
||||||
*/
|
|
||||||
extern size_t MemoryInfoGetSize(MemoryInfo *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the file name in which the block of memory represented by the
|
|
||||||
* specified memory info structure was allocated.
|
|
||||||
*/
|
|
||||||
extern const char * MemoryInfoGetFile(MemoryInfo *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the line number on which the block of memory represented by the
|
|
||||||
* specified memory info structure was allocated.
|
|
||||||
*/
|
|
||||||
extern int MemoryInfoGetLine(MemoryInfo *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get a pointer to the block of memory represented by the specified
|
|
||||||
* memory info structure.
|
|
||||||
*/
|
|
||||||
extern void * MemoryInfoGetPointer(MemoryInfo *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This function takes a pointer to a function that takes the memory
|
|
||||||
* info structure, as well as a void pointer for caller-provided
|
|
||||||
* arguments. It iterates over all the heap memory currently allocated
|
|
||||||
* at the time of calling, executing the function on each allocation.
|
|
||||||
*/
|
|
||||||
extern void MemoryIterate(void (*) (MemoryInfo *, void *), void *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Specify a function to be executed whenever a memory operation
|
|
||||||
* occurs. The MemoryAction argument specifies the operation that
|
|
||||||
* occurred on the block of memory represented by the memory info
|
|
||||||
* structure. The function also takes a void pointer to caller-provided
|
|
||||||
* arguments.
|
|
||||||
*/
|
|
||||||
extern void MemoryHook(void (*) (MemoryAction, MemoryInfo *, void *), void *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The default memory hook, which has sane behavior and is installed
|
|
||||||
* at runtime. This function does not use any memory on the heap,
|
|
||||||
* except for the MemoryInfo passed to it, which it assumes to be
|
|
||||||
* valid. Everything else happens on the stack only, to ensure that
|
|
||||||
* the hook doesn't make any memory problems worse.
|
|
||||||
*/
|
|
||||||
extern void MemoryDefaultHook(MemoryAction, MemoryInfo *, void *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Read over the block of memory represented by the given memory info
|
|
||||||
* structure and generate a hexadecimal and ASCII string for each
|
|
||||||
* chunk of the block. This function takes a callback function that
|
|
||||||
* takes the following parameters in order:
|
|
||||||
* .Bl -bullet -offset indent
|
|
||||||
* .It
|
|
||||||
* The current offset from the beginning of the block of memory in
|
|
||||||
* bytes.
|
|
||||||
* .It
|
|
||||||
* A null-terminated string containing the next 16 bytes of the block
|
|
||||||
* encoded as space-separated hex values.
|
|
||||||
* .It
|
|
||||||
* A null-terminated string containing the ASCII representation of the
|
|
||||||
* same 16 bytes of memory. This ASCII representation is safe to print
|
|
||||||
* to a terminal or other text device, because non-printable characters
|
|
||||||
* are encoded as a . (period).
|
|
||||||
* .It
|
|
||||||
* Caller-passed pointer.
|
|
||||||
* .El
|
|
||||||
*/
|
|
||||||
extern void
|
|
||||||
MemoryHexDump(MemoryInfo *, void (*) (size_t, char *, char *, void *), void *);
|
|
||||||
|
|
||||||
#endif
|
|
|
@ -1,105 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright (C) 2022-2023 Jordan Bancino <@jordan:bancino.net>
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person
|
|
||||||
* obtaining a copy of this software and associated documentation files
|
|
||||||
* (the "Software"), to deal in the Software without restriction,
|
|
||||||
* including without limitation the rights to use, copy, modify, merge,
|
|
||||||
* publish, distribute, sublicense, and/or sell copies of the Software,
|
|
||||||
* and to permit persons to whom the Software is furnished to do so,
|
|
||||||
* subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be
|
|
||||||
* included in all copies or portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
||||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
||||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
||||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
||||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
||||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
||||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
||||||
* SOFTWARE.
|
|
||||||
*/
|
|
||||||
#ifndef CYTOPLASM_QUEUE_H
|
|
||||||
#define CYTOPLASM_QUEUE_H
|
|
||||||
|
|
||||||
/***
|
|
||||||
* @Nm Queue
|
|
||||||
* @Nd A simple static queue data structure.
|
|
||||||
* @Dd November 25 2022
|
|
||||||
* @Xr Array HashMap
|
|
||||||
*
|
|
||||||
* .Nm
|
|
||||||
* implements a simple queue data structure that is statically sized.
|
|
||||||
* This implementation does not actually store the values of the items
|
|
||||||
* in it; it only stores pointers to the data. As such, you will have
|
|
||||||
* to manually maintain data and make sure it remains valid as long as
|
|
||||||
* it is in the queue. The advantage of this is that
|
|
||||||
* .Nm
|
|
||||||
* doesn't have to copy data, and thus doesn't care how big the data
|
|
||||||
* is. Furthermore, any arbitrary data can be stored in the queue.
|
|
||||||
* .Pp
|
|
||||||
* This queue implementation operates on the heap. It is a circular
|
|
||||||
* queue, and it does not grow as it is used. Once the size is set,
|
|
||||||
* the queue never gets any bigger.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <stddef.h>
|
|
||||||
|
|
||||||
/**
|
|
||||||
* These functions operate on a queue structure that is opaque to the
|
|
||||||
* caller.
|
|
||||||
*/
|
|
||||||
typedef struct Queue Queue;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Allocate a new queue that is able to store the specified number of
|
|
||||||
* items in it.
|
|
||||||
*/
|
|
||||||
extern Queue * QueueCreate(size_t);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Free the memory associated with the specified queue structure. Note
|
|
||||||
* that this function does not free any of the values stored in the
|
|
||||||
* queue; it is the caller's job to manage memory for each item.
|
|
||||||
* Typically, the caller would dequeue all the items in the queue and
|
|
||||||
* deal with them before freeing the queue itself.
|
|
||||||
*/
|
|
||||||
extern void QueueFree(Queue *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Push an element into the queue. This function returns a boolean
|
|
||||||
* value indicating whether or not the push succeeded. Pushing items
|
|
||||||
* into the queue will fail if the queue is full.
|
|
||||||
*/
|
|
||||||
extern int QueuePush(Queue *, void *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Pop an element out of the queue. This function returns NULL if the
|
|
||||||
* queue is empty. Otherwise, it returns a pointer to the item that is
|
|
||||||
* next up in the queue.
|
|
||||||
*/
|
|
||||||
extern void * QueuePop(Queue *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieve a pointer to the item that is next up in the queue without
|
|
||||||
* actually discarding it, such that the next call to
|
|
||||||
* .Fn QueuePeek
|
|
||||||
* or
|
|
||||||
* .Fn QueuePop
|
|
||||||
* will return the same pointer.
|
|
||||||
*/
|
|
||||||
extern void * QueuePeek(Queue *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Determine whether or not the queue is full.
|
|
||||||
*/
|
|
||||||
extern int QueueFull(Queue *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Determine whether or not the queue is empty.
|
|
||||||
*/
|
|
||||||
extern int QueueEmpty(Queue *);
|
|
||||||
|
|
||||||
#endif
|
|
|
@ -1,81 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright (C) 2022-2023 Jordan Bancino <@jordan:bancino.net>
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person
|
|
||||||
* obtaining a copy of this software and associated documentation files
|
|
||||||
* (the "Software"), to deal in the Software without restriction,
|
|
||||||
* including without limitation the rights to use, copy, modify, merge,
|
|
||||||
* publish, distribute, sublicense, and/or sell copies of the Software,
|
|
||||||
* and to permit persons to whom the Software is furnished to do so,
|
|
||||||
* subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be
|
|
||||||
* included in all copies or portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
||||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
||||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
||||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
||||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
||||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
||||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
||||||
* SOFTWARE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef CYTOPLASM_RAND_H
|
|
||||||
#define CYTOPLASM_RAND_H
|
|
||||||
|
|
||||||
/***
|
|
||||||
* @Nm Rand
|
|
||||||
* @Nd Thread-safe random numbers.
|
|
||||||
* @Dd February 16 2023
|
|
||||||
* @Xr Util
|
|
||||||
*
|
|
||||||
* .Nm
|
|
||||||
* is used for generating random numbers in a thread-safe way.
|
|
||||||
* Currently, one generator state is shared across all threads, which
|
|
||||||
* means that only one thread can generate random numbers at a time.
|
|
||||||
* This state is protected with a mutex to guarantee this behavior.
|
|
||||||
* In the future, a seed pool may be maintained to allow multiple
|
|
||||||
* threads to generate random numbers at the same time.
|
|
||||||
* .Pp
|
|
||||||
* The generator state is seeded on the first call to a function that
|
|
||||||
* needs it. The seed is determined by the current timestamp, the ID
|
|
||||||
* of the process, and the thread ID. These should all be sufficiently
|
|
||||||
* random sources, so the seed should be secure enough.
|
|
||||||
* .Pp
|
|
||||||
* .Nm
|
|
||||||
* currently uses a simple Mersenne Twister algorithm to generate
|
|
||||||
* random numbers. This algorithm was chosen because it is extremely
|
|
||||||
* popular and widespread. While it is likely not cryptographically
|
|
||||||
* secure, and does suffer some unfortunate pitfalls, this algorithm
|
|
||||||
* has stood the test of time and is simple enough to implement, so
|
|
||||||
* it was chosen over the alternatives.
|
|
||||||
* .Pp
|
|
||||||
* .Nm
|
|
||||||
* does not use any random number generator functions from the C
|
|
||||||
* standard library, since these are often flawed.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <stddef.h>
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Generate a single random integer between 0 and the passed value.
|
|
||||||
*/
|
|
||||||
extern int RandInt(unsigned int);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Generate the number of integers specified by the second argument
|
|
||||||
* storing them into the buffer pointed to in the first argument.
|
|
||||||
* Ensure that each number is between 0 and the third argument.
|
|
||||||
* .Pp
|
|
||||||
* This function allows a caller to get multiple random numbers at once
|
|
||||||
* in a more efficient manner than repeatedly calling
|
|
||||||
* .Fn RandInt ,
|
|
||||||
* since each call to these functions
|
|
||||||
* has to lock and unlock a mutex. It is therefore better to obtain
|
|
||||||
* multiple random numbers in one pass if multiple are needed.
|
|
||||||
*/
|
|
||||||
extern void RandIntN(int *, size_t, unsigned int);
|
|
||||||
|
|
||||||
#endif /* CYTOPLASM_RAND_H */
|
|
|
@ -1,53 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright (C) 2022-2023 Jordan Bancino <@jordan:bancino.net>
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person
|
|
||||||
* obtaining a copy of this software and associated documentation files
|
|
||||||
* (the "Software"), to deal in the Software without restriction,
|
|
||||||
* including without limitation the rights to use, copy, modify, merge,
|
|
||||||
* publish, distribute, sublicense, and/or sell copies of the Software,
|
|
||||||
* and to permit persons to whom the Software is furnished to do so,
|
|
||||||
* subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be
|
|
||||||
* included in all copies or portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
||||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
||||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
||||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
||||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
||||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
||||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
||||||
* SOFTWARE.
|
|
||||||
*/
|
|
||||||
#ifndef CYTOPLASM_RUNTIME_H
|
|
||||||
#define CYTOPLASM_RUNTIME_H
|
|
||||||
|
|
||||||
/***
|
|
||||||
* @Nm Runtime
|
|
||||||
* @Nd Supporting functions for the Cytoplasm runtime.
|
|
||||||
* @Dd May 23 2023
|
|
||||||
* @Xr Memory
|
|
||||||
*
|
|
||||||
* .Nm
|
|
||||||
* provides supporting functions for the Cytoplasm runtime. These
|
|
||||||
* functions are not intended to be called directly by programs,
|
|
||||||
* but are used internally. They're exposed via a header because
|
|
||||||
* the runtime stub needs to know their definitions.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <Array.h>
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Write a memory report to a file in the current directory, using
|
|
||||||
* the provided program arguments, including the program name that
|
|
||||||
* executed. This function is to be called after all memory is
|
|
||||||
* supposed to have been freed. It iterates over all remaining
|
|
||||||
* memory and generates a text file containing all of the
|
|
||||||
* recorded information about each block, including a hex dump of
|
|
||||||
* the data stored in them.
|
|
||||||
*/
|
|
||||||
extern void GenerateMemoryReport(int argc, char **argv);
|
|
||||||
|
|
||||||
#endif /* CYTOPLASM_RUNTIME_H */
|
|
|
@ -1,76 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright (C) 2022-2023 Jordan Bancino <@jordan:bancino.net>
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person
|
|
||||||
* obtaining a copy of this software and associated documentation files
|
|
||||||
* (the "Software"), to deal in the Software without restriction,
|
|
||||||
* including without limitation the rights to use, copy, modify, merge,
|
|
||||||
* publish, distribute, sublicense, and/or sell copies of the Software,
|
|
||||||
* and to permit persons to whom the Software is furnished to do so,
|
|
||||||
* subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be
|
|
||||||
* included in all copies or portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
||||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
||||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
||||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
||||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
||||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
||||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
||||||
* SOFTWARE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef CYTOPLASM_SHA_H
|
|
||||||
#define CYTOPLASM_SHA_H
|
|
||||||
|
|
||||||
/***
|
|
||||||
* @Nm Sha
|
|
||||||
* @Nd A simple implementation of a few SHA hashing functions.
|
|
||||||
* @Dd December 19 2022
|
|
||||||
* @Xr Memory Base64
|
|
||||||
*
|
|
||||||
* This API defines simple functions for computing SHA hashes.
|
|
||||||
* At the moment, it only defines
|
|
||||||
* .Fn Sha256
|
|
||||||
* and
|
|
||||||
* .Fn Sha1 ,
|
|
||||||
* which compute the SHA-256 and SHA-1 hashes of the given C string,
|
|
||||||
* respectively. It is not trivial to implement SHA-512 in ANSI C
|
|
||||||
* due to the lack of a 64-bit integer type, so that hash
|
|
||||||
* function has been omitted.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This function takes a pointer to a NULL-terminated C string, and
|
|
||||||
* returns a NULL-terminated byte buffer allocated on the heap using
|
|
||||||
* the Memory API, or NULL if there was an error allocating memory.
|
|
||||||
* The returned byte buffer should be freed when it is no longer
|
|
||||||
* needed. It is important to note that the returned buffer is not
|
|
||||||
* a printable string; to get a printable string, use
|
|
||||||
* .Fn ShaToHex .
|
|
||||||
*/
|
|
||||||
extern unsigned char * Sha256(char *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This function takes a pointer to a NULL-terminated C string, and
|
|
||||||
* returns a NULL-terminated byte buffer allocated on the heap using
|
|
||||||
* the Memory API, or NULL if there was an error allocating memory.
|
|
||||||
* The returned byte buffer should be freed when it is no longer
|
|
||||||
* needed. It is important to note that the returned buffer is not
|
|
||||||
* a printable string; to get a printable string, use
|
|
||||||
* .Fn ShaToHex .
|
|
||||||
*/
|
|
||||||
extern unsigned char * Sha1(char *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Convert a SHA byte buffer into a hex string. These hex strings
|
|
||||||
* are typically what is transmitted, stored, and compared, however
|
|
||||||
* there may be times when it is necessary to work with the raw
|
|
||||||
* bytes directly, which is why the conversion to a hex string is
|
|
||||||
* a separate step.
|
|
||||||
*/
|
|
||||||
extern char * ShaToHex(unsigned char *);
|
|
||||||
|
|
||||||
#endif /* CYTOPLASM_SHA_H */
|
|
|
@ -1,129 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright (C) 2022-2023 Jordan Bancino <@jordan:bancino.net>
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person
|
|
||||||
* obtaining a copy of this software and associated documentation files
|
|
||||||
* (the "Software"), to deal in the Software without restriction,
|
|
||||||
* including without limitation the rights to use, copy, modify, merge,
|
|
||||||
* publish, distribute, sublicense, and/or sell copies of the Software,
|
|
||||||
* and to permit persons to whom the Software is furnished to do so,
|
|
||||||
* subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be
|
|
||||||
* included in all copies or portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
||||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
||||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
||||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
||||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
||||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
||||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
||||||
* SOFTWARE.
|
|
||||||
*/
|
|
||||||
#ifndef CYTOPLASM_STR_H
|
|
||||||
#define CYTOPLASM_STR_H
|
|
||||||
|
|
||||||
/***
|
|
||||||
* @Nm Str
|
|
||||||
* @Nd Functions for creating and manipulating strings.
|
|
||||||
* @Dd February 15 2023
|
|
||||||
* @Xr Memory
|
|
||||||
*
|
|
||||||
* .Nm
|
|
||||||
* provides string-related functions. It is called
|
|
||||||
* .Nm ,
|
|
||||||
* not String, because some platforms (Windows) do not have
|
|
||||||
* case-sensitive filesystems, which poses a problem since
|
|
||||||
* .Pa string.h
|
|
||||||
* is a standard library header.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <Int.h>
|
|
||||||
|
|
||||||
#include <stddef.h>
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Convert UTF-16 into a Unicode codepoint.
|
|
||||||
*/
|
|
||||||
extern UInt32 StrUtf16Decode(UInt16, UInt16);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Take a Unicode codepoint and encode it into a string buffer containing
|
|
||||||
* between 1 and 4 bytes. The string buffer is allocated on the heap,
|
|
||||||
* so it should be freed when it is no longer needed.
|
|
||||||
*/
|
|
||||||
extern char * StrUtf8Encode(UInt32);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Duplicate a null-terminated string, returning a new string on the
|
|
||||||
* heap. This is useful when a function takes in a string that it needs
|
|
||||||
* to store for long amounts of time, even perhaps after the original
|
|
||||||
* string is gone.
|
|
||||||
*/
|
|
||||||
extern char * StrDuplicate(const char *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Extract part of a null-terminated string, returning a new string on
|
|
||||||
* the heap containing only the requested subsection. Like the
|
|
||||||
* substring functions included with most programming languages, the
|
|
||||||
* starting index is inclusive, and the ending index is exclusive.
|
|
||||||
*/
|
|
||||||
extern char * StrSubstr(const char *, size_t, size_t);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A varargs function that takes a number of null-terminated strings
|
|
||||||
* specified by the first argument, and returns a new string that
|
|
||||||
* contains their concatenation. It works similarly to
|
|
||||||
* .Xr strcat 3 ,
|
|
||||||
* but it takes care of allocating memory big enough to hold all the
|
|
||||||
* strings. Any string in the list may be NULL. If a NULL pointer is
|
|
||||||
* passed, it is treated like an empty string.
|
|
||||||
*/
|
|
||||||
extern char * StrConcat(size_t,...);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Return a boolean value indicating whether or not the null-terminated
|
|
||||||
* string consists only of blank characters, as determined by
|
|
||||||
* .Xr isblank 3 .
|
|
||||||
*/
|
|
||||||
extern int StrBlank(const char *str);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Generate a string of the specified length, containing random
|
|
||||||
* lowercase and uppercase letters.
|
|
||||||
*/
|
|
||||||
extern char * StrRandom(size_t);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Convert the specified integer into a string, returning the string
|
|
||||||
* on the heap, or NULL if there was a memory allocation error. The
|
|
||||||
* returned string should be freed by the caller after it is no longer
|
|
||||||
* needed.
|
|
||||||
*/
|
|
||||||
extern char * StrInt(long);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Converts a string into a lowercase version of it using
|
|
||||||
* .Xr tolower 3 ,
|
|
||||||
* returning the lowercase version on the heap, or NULL if there was
|
|
||||||
* a memory allocation error. The returned string should be freed by
|
|
||||||
* the caller after it is no longer needed.
|
|
||||||
*/
|
|
||||||
extern char * StrLower(char *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Compare two strings and determine whether or not they are equal.
|
|
||||||
* This is the most common use case of strcmp() in Cytoplasm, but
|
|
||||||
* strcmp() doesn't like NULL pointers, so these have to be checked
|
|
||||||
* explicitly and can cause problems if they aren't. This function,
|
|
||||||
* on the other hand, makes NULL pointers special cases. If both
|
|
||||||
* arguments are NULL, then they are considered equal. If only one
|
|
||||||
* argument is NULL, they are considered not equal. Otherwise, if
|
|
||||||
* no arguments are NULL, a regular strcmp() takes place and this
|
|
||||||
* function returns a boolean value indicating whether or not
|
|
||||||
* strcmp() returned 0.
|
|
||||||
*/
|
|
||||||
extern int StrEquals(const char *, const char *);
|
|
||||||
|
|
||||||
#endif /* CYTOPLASM_STR_H */
|
|
|
@ -1,223 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright (C) 2022-2023 Jordan Bancino <@jordan:bancino.net>
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person
|
|
||||||
* obtaining a copy of this software and associated documentation files
|
|
||||||
* (the "Software"), to deal in the Software without restriction,
|
|
||||||
* including without limitation the rights to use, copy, modify, merge,
|
|
||||||
* publish, distribute, sublicense, and/or sell copies of the Software,
|
|
||||||
* and to permit persons to whom the Software is furnished to do so,
|
|
||||||
* subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be
|
|
||||||
* included in all copies or portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
||||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
||||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
||||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
||||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
||||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
||||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
||||||
* SOFTWARE.
|
|
||||||
*/
|
|
||||||
#ifndef CYTOPLASM_STREAM_H
|
|
||||||
#define CYTOPLASM_STREAM_H
|
|
||||||
|
|
||||||
/***
|
|
||||||
* @Nm Stream
|
|
||||||
* @Nd An abstraction over the Io API that implements standard C I/O.
|
|
||||||
* @Dd April 29 2023
|
|
||||||
* @Xr Io
|
|
||||||
*
|
|
||||||
* .Nm
|
|
||||||
* implements an abstraction layer over the Io API. This layer buffers
|
|
||||||
* I/O and makes it much easier to work with, mimicking the standard
|
|
||||||
* C library and offering some more convenience features.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <Io.h>
|
|
||||||
|
|
||||||
#include <stdarg.h>
|
|
||||||
|
|
||||||
/**
|
|
||||||
* An opaque structure analogous to C's FILE pointers.
|
|
||||||
*/
|
|
||||||
typedef struct Stream Stream;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create a new stream using the specified Io for underlying I/O
|
|
||||||
* operations.
|
|
||||||
*/
|
|
||||||
extern Stream * StreamIo(Io * io);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create a new stream using the specified POSIX file descriptor.
|
|
||||||
* This is a convenience function for calling
|
|
||||||
* .Fn IoFd
|
|
||||||
* and then passing the result into
|
|
||||||
* .Fn StreamIo .
|
|
||||||
*/
|
|
||||||
extern Stream * StreamFd(int);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create a new stream using the specified C FILE pointer. This is a
|
|
||||||
* convenience function for calling
|
|
||||||
* .Fn IoFile
|
|
||||||
* and then passing the result into
|
|
||||||
* .Fn StreamIo .
|
|
||||||
*/
|
|
||||||
extern Stream * StreamFile(FILE *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create a new stream using the specified path and mode. This is a
|
|
||||||
* convenience function for calling
|
|
||||||
* .Xr fopen 3
|
|
||||||
* and then passing the result into
|
|
||||||
* .Fn StreamFile .
|
|
||||||
*/
|
|
||||||
extern Stream * StreamOpen(const char *, const char *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get a stream that writes to the standard output.
|
|
||||||
*/
|
|
||||||
extern Stream * StreamStdout(void);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get a stream that writes to the standard error.
|
|
||||||
*/
|
|
||||||
extern Stream * StreamStderr(void);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get a stream that reads from the standard input.
|
|
||||||
*/
|
|
||||||
extern Stream * StreamStdin(void);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Close the stream. This flushes the buffers and closes the underlying
|
|
||||||
* Io. It is analogous to the standard
|
|
||||||
* .Xr fclose 3
|
|
||||||
* function.
|
|
||||||
*/
|
|
||||||
extern int StreamClose(Stream *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Print a formatted string. This function is analogous to the standard
|
|
||||||
* .Xr vfprintf 3
|
|
||||||
* function.
|
|
||||||
*/
|
|
||||||
extern int StreamVprintf(Stream *, const char *, va_list);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Print a formatted string. This function is analogous to the
|
|
||||||
* standard
|
|
||||||
* .Xr fprintf 3
|
|
||||||
* function.
|
|
||||||
*/
|
|
||||||
extern int StreamPrintf(Stream *, const char *,...);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get a single character from a stream. This function is analogous to
|
|
||||||
* the standard
|
|
||||||
* .Xr fgetc 3
|
|
||||||
* function.
|
|
||||||
*/
|
|
||||||
extern int StreamGetc(Stream *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Push a character back onto the input stream. This function is
|
|
||||||
* analogous to the standard
|
|
||||||
* .Xr ungetc 3
|
|
||||||
* function.
|
|
||||||
*/
|
|
||||||
extern int StreamUngetc(Stream *, int);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Write a single character to the stream. This function is analogous
|
|
||||||
* to the standard
|
|
||||||
* .Xr fputc 3
|
|
||||||
* function.
|
|
||||||
*/
|
|
||||||
extern int StreamPutc(Stream *, int);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Write a null-terminated string to the stream. This function is
|
|
||||||
* analogous to the standard
|
|
||||||
* .Xr fputs 3
|
|
||||||
* function.
|
|
||||||
*/
|
|
||||||
extern int StreamPuts(Stream *, char *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Read at most the specified number of characters minus 1 from the
|
|
||||||
* specified stream and store them at the memory located at the
|
|
||||||
* specified pointer. This function is analogous to the standard
|
|
||||||
* .Xr fgets 3
|
|
||||||
* function.
|
|
||||||
*/
|
|
||||||
extern char * StreamGets(Stream *, char *, int);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Set the file position indicator for the specified stream. This
|
|
||||||
* function is analogous to the standard
|
|
||||||
* .Xr fseeko
|
|
||||||
* function.
|
|
||||||
*/
|
|
||||||
extern off_t StreamSeek(Stream *, off_t, int);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Test the end-of-file indicator for the given stream, returning a
|
|
||||||
* boolean value indicating whether or not it is set. This is analogous
|
|
||||||
* to the standard
|
|
||||||
* .Xr feof 3
|
|
||||||
* function.
|
|
||||||
*/
|
|
||||||
extern int StreamEof(Stream *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Test the stream for an error condition, returning a boolean value
|
|
||||||
* indicating whether or not one is present. This is analogous to the
|
|
||||||
* standard
|
|
||||||
* .Xr ferror 3
|
|
||||||
* function.
|
|
||||||
*/
|
|
||||||
extern int StreamError(Stream *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Clear the error condition associated with the given stream, allowing
|
|
||||||
* future reads or writes to potentially be successful. This functio
|
|
||||||
* is analogous to the standard
|
|
||||||
* .Xr clearerr 3
|
|
||||||
* function.
|
|
||||||
*/
|
|
||||||
extern void StreamClearError(Stream *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Flush all buffered data using the streams underlying write function.
|
|
||||||
* This function is analogous to the standard
|
|
||||||
* .Xr fflush 3
|
|
||||||
* function.
|
|
||||||
*/
|
|
||||||
extern int StreamFlush(Stream *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Read all the bytes from the first stream and write them to the
|
|
||||||
* second stream. This is analogous to
|
|
||||||
* .Fn IoCopy ,
|
|
||||||
* but it uses the internal buffers of the streams. It is probably
|
|
||||||
* less efficient than doing a
|
|
||||||
* .Fn IoCopy
|
|
||||||
* instead, but it is more convenient.
|
|
||||||
*/
|
|
||||||
extern ssize_t StreamCopy(Stream *, Stream *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the file descriptor associated with the given stream, or -1 if
|
|
||||||
* the stream is not associated with any file descriptor. This function
|
|
||||||
* is analogous to the standard
|
|
||||||
* .Xr fileno 3
|
|
||||||
* function.
|
|
||||||
*/
|
|
||||||
extern int StreamFileno(Stream *);
|
|
||||||
|
|
||||||
#endif /* CYTOPLASM_STREAM_H */
|
|
|
@ -1,109 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright (C) 2022-2023 Jordan Bancino <@jordan:bancino.net>
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person
|
|
||||||
* obtaining a copy of this software and associated documentation files
|
|
||||||
* (the "Software"), to deal in the Software without restriction,
|
|
||||||
* including without limitation the rights to use, copy, modify, merge,
|
|
||||||
* publish, distribute, sublicense, and/or sell copies of the Software,
|
|
||||||
* and to permit persons to whom the Software is furnished to do so,
|
|
||||||
* subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be
|
|
||||||
* included in all copies or portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
||||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
||||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
||||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
||||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
||||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
||||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
||||||
* SOFTWARE.
|
|
||||||
*/
|
|
||||||
#ifndef CYTOPLASM_TLS_H
|
|
||||||
#define CYTOPLASM_TLS_H
|
|
||||||
|
|
||||||
/***
|
|
||||||
* @Nm Tls
|
|
||||||
* @Nd Interface to platform-dependent TLS libraries.
|
|
||||||
* @Dd April 29 2023
|
|
||||||
* @Xr Stream Io
|
|
||||||
*
|
|
||||||
* .Nm
|
|
||||||
* provides an interface to platform-dependent TLS libraries. It allows
|
|
||||||
* Cytoplasm to support any TLS library with no changes to existing
|
|
||||||
* code. Support for additional TLS libraries is added by creating a
|
|
||||||
* new compilation unit that implements all the functions here, with
|
|
||||||
* the exception of a few, which are noted.
|
|
||||||
* .Pp
|
|
||||||
* Currently, Cytoplasm has support for the following TLS libraries:
|
|
||||||
* .Bl -bullet -offset indent
|
|
||||||
* .It
|
|
||||||
* LibreSSL
|
|
||||||
* .It
|
|
||||||
* OpenSSL
|
|
||||||
* .El
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <Stream.h>
|
|
||||||
|
|
||||||
#define TLS_LIBRESSL 2
|
|
||||||
#define TLS_OPENSSL 3
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create a new TLS client stream using the given file descriptor and
|
|
||||||
* the given server hostname. The hostname should be used to verify
|
|
||||||
* that the server actually is who it says it is.
|
|
||||||
* .Pp
|
|
||||||
* This function does not need to be implemented by the individual
|
|
||||||
* TLS support stubs.
|
|
||||||
*/
|
|
||||||
extern Stream * TlsClientStream(int, const char *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create a new TLS server stream using the given certificate and key
|
|
||||||
* file, in the format natively supported by the TLS library.
|
|
||||||
* .Pp
|
|
||||||
* This function does not need to be implemented by the individual
|
|
||||||
* TLS support stubs.
|
|
||||||
*/
|
|
||||||
extern Stream * TlsServerStream(int, const char *, const char *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Initialize a cookie that stores information about the given client
|
|
||||||
* connection. This cookie will be passed into the other functions
|
|
||||||
* defined by this API.
|
|
||||||
*/
|
|
||||||
extern void * TlsInitClient(int, const char *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Initialize a cookie that stores information about the given
|
|
||||||
* server connection. This cookie will be passed into the other
|
|
||||||
* functions defined by this API.
|
|
||||||
*/
|
|
||||||
extern void * TlsInitServer(int, const char *, const char *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Read from a TLS stream, decrypting it and storing the result in the
|
|
||||||
* specified buffer. This function takes the cookie, buffer, and
|
|
||||||
* number of decrypted bytes to read into it. See the documentation for
|
|
||||||
* .Fn IoRead .
|
|
||||||
*/
|
|
||||||
extern ssize_t TlsRead(void *, void *, size_t);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Write to a TLS stream, encrypting the buffer. This function takes
|
|
||||||
* the cookie, buffer, and number of unencrypted bytes to write to
|
|
||||||
* the stream. See the documentation for
|
|
||||||
* .Fn IoWrite .
|
|
||||||
*/
|
|
||||||
extern ssize_t TlsWrite(void *, void *, size_t);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Close the TLS stream, also freeing all memory associated with the
|
|
||||||
* cookie.
|
|
||||||
*/
|
|
||||||
extern int TlsClose(void *);
|
|
||||||
|
|
||||||
#endif /* CYTOPLASM_TLS_H */
|
|
|
@ -1,252 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright (C) 2022-2023 Jordan Bancino <@jordan:bancino.net>
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person
|
|
||||||
* obtaining a copy of this software and associated documentation files
|
|
||||||
* (the "Software"), to deal in the Software without restriction,
|
|
||||||
* including without limitation the rights to use, copy, modify, merge,
|
|
||||||
* publish, distribute, sublicense, and/or sell copies of the Software,
|
|
||||||
* and to permit persons to whom the Software is furnished to do so,
|
|
||||||
* subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be
|
|
||||||
* included in all copies or portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
||||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
||||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
||||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
||||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
||||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
||||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
||||||
* SOFTWARE.
|
|
||||||
*/
|
|
||||||
#ifndef CYTOPLASM_UINT64_H
|
|
||||||
#define CYTOPLASM_UINT64_H
|
|
||||||
|
|
||||||
/***
|
|
||||||
* @Nm UInt64
|
|
||||||
* @Nd Fixed-width 64 bit integers.
|
|
||||||
* @Dd August 11, 2023
|
|
||||||
*
|
|
||||||
* .Pp
|
|
||||||
* ANSI C89 (or C99 for that matter) provides no required mechanism
|
|
||||||
* for 64 bit integers. Nevertheless, many compilers provide them as
|
|
||||||
* extensions. However, since it is not a gaurantee, and to be fully
|
|
||||||
* standards-compliant and thus portable, a platform-agnostic interface
|
|
||||||
* is required. This header provides such an interface. If the platform
|
|
||||||
* has a 64 bit integer type, that is used, and native operations are
|
|
||||||
* performed by C preprocessor macro expansion. Otherwise, a
|
|
||||||
* compatibility layer is provided, which implements 64-bit
|
|
||||||
* arithmetic on an array of 2 32-bit numbers which are provided by
|
|
||||||
* .Xr Int 3 .
|
|
||||||
* .Pp
|
|
||||||
* Note that 64-bit emulation is certainly not as performant as using
|
|
||||||
* native 64-bit operations, so whenever possible, the native
|
|
||||||
* operations should be preferred. However, since C provides no required
|
|
||||||
* 64 bit integer on 32-bit and less platforms, this API can be used as
|
|
||||||
* a "good enough" fallback mechanism.
|
|
||||||
* .Pp
|
|
||||||
* Also note that this implementation, both in the native and
|
|
||||||
* non-native forms, makes some assumptions:
|
|
||||||
* .Bl -bullet -width Ds
|
|
||||||
* .It
|
|
||||||
* When a cast from a larger integer to a smaller integer is performed,
|
|
||||||
* the upper bits are truncated, not the lower bits.
|
|
||||||
* .It
|
|
||||||
* Negative numbers are represented in memory and in registers in two's
|
|
||||||
* compliment form.
|
|
||||||
* .El
|
|
||||||
* .Pp
|
|
||||||
* This API may provide unexpected output if these assumptions are
|
|
||||||
* false for a given platform.
|
|
||||||
*
|
|
||||||
* @ignore-typedefs
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <Int.h>
|
|
||||||
|
|
||||||
#include <stddef.h>
|
|
||||||
|
|
||||||
#ifndef INT64_FORCE_EMULATED
|
|
||||||
|
|
||||||
#define BIT64_MAX 18446744073709551615UL
|
|
||||||
|
|
||||||
#if UINT_MAX == BIT64_MAX
|
|
||||||
/* typedef signed int Int64; */
|
|
||||||
typedef unsigned int UInt64;
|
|
||||||
|
|
||||||
#define UINT64_NATIVE
|
|
||||||
|
|
||||||
#elif ULONG_MAX == BIT64_MAX
|
|
||||||
/* typedef signed int Int64; */
|
|
||||||
typedef unsigned long UInt64;
|
|
||||||
|
|
||||||
#define UINT64_NATIVE
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#endif /* ifndef INT64_FORCE_EMULATED */
|
|
||||||
|
|
||||||
#ifdef UINT64_NATIVE
|
|
||||||
|
|
||||||
#define UInt64Create(high, low) (((UInt64) (high) << 32) | (low))
|
|
||||||
#define UInt64Low(a) ((UInt32) ((a) & 0x00000000FFFFFFFF))
|
|
||||||
#define UInt64High(a) ((UInt32) ((a) >> 32))
|
|
||||||
|
|
||||||
#define UInt64Add(a, b) ((a) + (b))
|
|
||||||
#define UInt64Sub(a, b) ((a) - (b))
|
|
||||||
#define UInt64Mul(a, b) ((a) * (b))
|
|
||||||
#define UInt64Div(a, b) ((a) / (b))
|
|
||||||
#define UInt64Rem(a, b) ((a) % (b))
|
|
||||||
|
|
||||||
#define UInt64Sll(a, b) ((a) << (b))
|
|
||||||
#define UInt64Srl(a, b) ((a) >> (b))
|
|
||||||
|
|
||||||
#define UInt64And(a, b) ((a) & (b))
|
|
||||||
#define UInt64Or(a, b) ((a) | (b))
|
|
||||||
#define UInt64Xor(a, b) ((a) ^ (b))
|
|
||||||
#define UInt64Not(a) (~(a))
|
|
||||||
|
|
||||||
#define UInt64Eq(a, b) ((a) == (b))
|
|
||||||
#define UInt64Lt(a, b) ((a) < (b))
|
|
||||||
#define UInt64Gt(a, b) ((a) > (b))
|
|
||||||
|
|
||||||
#define UInt64Neq(a, b) ((a) != (b))
|
|
||||||
#define UInt64Leq(a, b) ((a) <= (b))
|
|
||||||
#define UInt64Geq(a, b) ((a) >= (b))
|
|
||||||
|
|
||||||
#else
|
|
||||||
|
|
||||||
/**
|
|
||||||
* For platforms that do not have a native integer large enough to
|
|
||||||
* store a 64 bit integer, this struct is used. i[0] contains the low
|
|
||||||
* bits of integer, and i[1] contains the high bits of the integer.
|
|
||||||
* .Pp
|
|
||||||
* This struct should not be accessed directly, because UInt64 may not
|
|
||||||
* actually be this struct, it might be an actual integer type. For
|
|
||||||
* maximum portability, only use the functions defined here to
|
|
||||||
* manipulate 64 bit integers.
|
|
||||||
*/
|
|
||||||
typedef struct
|
|
||||||
{
|
|
||||||
UInt32 i[2];
|
|
||||||
} UInt64;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create a new unsigned 64 bit integer using the given high and low
|
|
||||||
* bits.
|
|
||||||
*/
|
|
||||||
extern UInt64 UInt64Create(UInt32, UInt32);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Add two unsigned 64 bit integers together.
|
|
||||||
*/
|
|
||||||
extern UInt64 UInt64Add(UInt64, UInt64);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Subtract the second 64 bit integer from the first.
|
|
||||||
*/
|
|
||||||
extern UInt64 UInt64Sub(UInt64, UInt64);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Multiply two 64 bit integers together. The non-native version of
|
|
||||||
* this function uses the Russian Peasant method of multiplication,
|
|
||||||
* which should afford more performance than a naive multiplication by
|
|
||||||
* addition, but it is still rather slow and depends on the size of
|
|
||||||
* the integers being multiplied.
|
|
||||||
*/
|
|
||||||
extern UInt64 UInt64Mul(UInt64, UInt64);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Divide the first 64 bit integer by the second and return the
|
|
||||||
* quotient. The non-native version of this function uses naive binary
|
|
||||||
* long division, which is slow, but gauranteed to finish in constant
|
|
||||||
* time.
|
|
||||||
*/
|
|
||||||
extern UInt64 UInt64Div(UInt64, UInt64);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Divide the first 64 bit integer by the second and return the
|
|
||||||
* remainder. The non-native version of this function uses naive binary
|
|
||||||
* long division, which is slow, but gauranteed to finish in constant
|
|
||||||
* time.
|
|
||||||
*/
|
|
||||||
extern UInt64 UInt64Rem(UInt64, UInt64);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Perform a left logical bit shift of a 64 bit integer. The second
|
|
||||||
* parameter is how many places to shift, and is declared as a regular
|
|
||||||
* integer because anything more than 64 does not make sense.
|
|
||||||
*/
|
|
||||||
extern UInt64 UInt64Sll(UInt64, int);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Perform a right logical bit shift of a 64 bit integer. The second
|
|
||||||
* parameter is how many places to shift, and is declared as a regular
|
|
||||||
* integer because anything more than 64 does not make sense.
|
|
||||||
*/
|
|
||||||
extern UInt64 UInt64Srl(UInt64, int);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Perform a bitwise AND (&) of the provided 64 bit integers.
|
|
||||||
*/
|
|
||||||
extern UInt64 UInt64And(UInt64, UInt64);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Perform a bitwise OR (|) of the provided 64 bit integers.
|
|
||||||
*/
|
|
||||||
extern UInt64 UInt64Or(UInt64, UInt64);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Perform a bitwise XOR (^) of the provided 64 bit integers.
|
|
||||||
*/
|
|
||||||
extern UInt64 UInt64Xor(UInt64, UInt64);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Perform a bitwise NOT (~) of the provided 64 bit integer.
|
|
||||||
*/
|
|
||||||
extern UInt64 UInt64Not(UInt64);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Perform a comparison of the provided 64 bit integers and return a C
|
|
||||||
* boolean that is true if and only if they are equal.
|
|
||||||
*/
|
|
||||||
extern int UInt64Eq(UInt64, UInt64);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Perform a comparison of the provided 64 bit integers and return a C
|
|
||||||
* boolean that is true if and only if the second operand is strictly
|
|
||||||
* less than the first.
|
|
||||||
*/
|
|
||||||
extern int UInt64Lt(UInt64, UInt64);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Perform a comparison of the provided 64 bit integers and return a C
|
|
||||||
* boolean that is true if and only if the second operand is strictly
|
|
||||||
* greater than the first.
|
|
||||||
*/
|
|
||||||
extern int UInt64Gt(UInt64, UInt64);
|
|
||||||
|
|
||||||
#define UInt64Low(a) ((a).i[0])
|
|
||||||
#define UInt64High(a) ((a).i[1])
|
|
||||||
|
|
||||||
#define UInt64Neq(a, b) (!UInt64Eq(a, b))
|
|
||||||
#define UInt64Leq(a, b) (UInt64Eq(a, b) || UInt64Lt(a, b))
|
|
||||||
#define UInt64Geq(a, b) (UInt64Eq(a, b) || UInt64Gt(a, b))
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#define UINT64_STRBUF 65 /* Base 2 representation with '\0' */
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Convert a 64 bit integer to a string in an arbitrary base
|
|
||||||
* representation specified by the second parameter, using the provided
|
|
||||||
* buffer and length specified by the third and fourth parameters. To
|
|
||||||
* guarantee that the string will fit in the buffer, allocate it of
|
|
||||||
* size UINT64_STRBUF or larger. Note that a buffer size smaller than
|
|
||||||
* UINT64_STRBUF will invoke undefined behavior.
|
|
||||||
*/
|
|
||||||
extern size_t UInt64Str(UInt64, int, char *, size_t);
|
|
||||||
|
|
||||||
#endif /* CYTOPLASM_UINT64_H */
|
|
|
@ -1,69 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright (C) 2022-2023 Jordan Bancino <@jordan:bancino.net>
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person
|
|
||||||
* obtaining a copy of this software and associated documentation files
|
|
||||||
* (the "Software"), to deal in the Software without restriction,
|
|
||||||
* including without limitation the rights to use, copy, modify, merge,
|
|
||||||
* publish, distribute, sublicense, and/or sell copies of the Software,
|
|
||||||
* and to permit persons to whom the Software is furnished to do so,
|
|
||||||
* subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be
|
|
||||||
* included in all copies or portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
||||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
||||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
||||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
||||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
||||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
||||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
||||||
* SOFTWARE.
|
|
||||||
*/
|
|
||||||
#ifndef CYTOPLASM_URI_H
|
|
||||||
#define CYTOPLASM_URI_H
|
|
||||||
|
|
||||||
/***
|
|
||||||
* @Nm Uri
|
|
||||||
* @Nd Parse a URI. Typically used to parse HTTP(s) URLs.
|
|
||||||
* @Dd April 29 2023
|
|
||||||
* @Xr Http
|
|
||||||
*
|
|
||||||
* .Nm
|
|
||||||
* provides a simple mechanism for parsing URIs. This is an extremely
|
|
||||||
* basic parser that (ab)uses
|
|
||||||
* .Xr sscanf 3
|
|
||||||
* to parse URIs, so it may not be the most reliable, but it should
|
|
||||||
* work in most cases and on reasonable URIs that aren't too long, as
|
|
||||||
* the _MAX definitions are modest.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#define URI_PROTO_MAX 8
|
|
||||||
#define URI_HOST_MAX 128
|
|
||||||
#define URI_PATH_MAX 256
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The parsed URI is stored in this structure.
|
|
||||||
*/
|
|
||||||
typedef struct Uri
|
|
||||||
{
|
|
||||||
char proto[URI_PROTO_MAX];
|
|
||||||
char host[URI_HOST_MAX];
|
|
||||||
char path[URI_PATH_MAX];
|
|
||||||
unsigned short port;
|
|
||||||
} Uri;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Parse a URI string into the Uri structure as described above, or
|
|
||||||
* return NULL if there was a parsing error.
|
|
||||||
*/
|
|
||||||
extern Uri * UriParse(const char *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Free the memory associated with a Uri structure returned by
|
|
||||||
* .Fn UriParse .
|
|
||||||
*/
|
|
||||||
extern void UriFree(Uri *);
|
|
||||||
|
|
||||||
#endif /* CYTOPLASM_URI_H */
|
|
|
@ -1,117 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright (C) 2022-2023 Jordan Bancino <@jordan:bancino.net>
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person
|
|
||||||
* obtaining a copy of this software and associated documentation files
|
|
||||||
* (the "Software"), to deal in the Software without restriction,
|
|
||||||
* including without limitation the rights to use, copy, modify, merge,
|
|
||||||
* publish, distribute, sublicense, and/or sell copies of the Software,
|
|
||||||
* and to permit persons to whom the Software is furnished to do so,
|
|
||||||
* subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be
|
|
||||||
* included in all copies or portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
||||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
||||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
||||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
||||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
||||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
||||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
||||||
* SOFTWARE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef CYTOPLASM_UTIL_H
|
|
||||||
#define CYTOPLASM_UTIL_H
|
|
||||||
|
|
||||||
/***
|
|
||||||
* @Nm Util
|
|
||||||
* @Nd Some misc. helper functions that don't need their own headers.
|
|
||||||
* @Dd February 15 2023
|
|
||||||
*
|
|
||||||
* This header holds a number of random functions related to strings,
|
|
||||||
* time, the filesystem, and other simple tasks that don't require a
|
|
||||||
* full separate API. For the most part, the functions here are
|
|
||||||
* entirely standalone, depending only on POSIX functions, however
|
|
||||||
* there are a few that depend explicitly on a few other APIs. Those
|
|
||||||
* are noted.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stddef.h>
|
|
||||||
#include <sys/types.h>
|
|
||||||
|
|
||||||
#include <Stream.h>
|
|
||||||
#include <UInt64.h>
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the current timestamp in milliseconds since the Unix epoch. This
|
|
||||||
* uses
|
|
||||||
* .Xr gettimeofday 2
|
|
||||||
* and time_t, and converts it to a single number, which is then
|
|
||||||
* returned to the caller.
|
|
||||||
* .Pp
|
|
||||||
* A note on the 2038 problem: as long as sizeof(time_t) >= 8, that is,
|
|
||||||
* as long as the time_t type is 64 bits or more, then everything
|
|
||||||
* should be fine. On most, if not, all, 64-bit systems, time_t is 64
|
|
||||||
* bits. time_t is promoted to a 64-bit integer before it is converted
|
|
||||||
* to milliseconds, so there is no risk of overflue due to the
|
|
||||||
* multiplication by 1000. However, if time_t is only 32 bits, it will
|
|
||||||
* overflow before it even gets to this function, which will cause this
|
|
||||||
* function to produce unexpected results.
|
|
||||||
*/
|
|
||||||
extern UInt64 UtilServerTs(void);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Use
|
|
||||||
* .Xr stat 2
|
|
||||||
* to get the last modified time of the given file, or zero if there
|
|
||||||
* was an error getting the last modified time of a file. This is
|
|
||||||
* primarily useful for caching file data.
|
|
||||||
*/
|
|
||||||
extern UInt64 UtilLastModified(char *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This function behaves just like the system call
|
|
||||||
* .Xr mkdir 2 ,
|
|
||||||
* but it creates any intermediate directories as necessary, unlike
|
|
||||||
* .Xr mkdir 2 .
|
|
||||||
*/
|
|
||||||
extern int UtilMkdir(const char *, const mode_t);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sleep the calling thread for the given number of milliseconds.
|
|
||||||
* POSIX does not have a very friendly way to sleep, so this wraps
|
|
||||||
* .Xr nanosleep 2
|
|
||||||
* to make its usage much, much simpler.
|
|
||||||
*/
|
|
||||||
extern int UtilSleepMillis(UInt64);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This function works identically to the POSIX
|
|
||||||
* .Xr getdelim 3 ,
|
|
||||||
* except that it assumes pointers were allocated with the Memory API
|
|
||||||
* and it reads from a Stream instead of a file pointer.
|
|
||||||
*/
|
|
||||||
extern ssize_t UtilGetDelim(char **, size_t *, int, Stream *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This function is just a special case of
|
|
||||||
* .Fn UtilGetDelim
|
|
||||||
* that sets the delimiter to the newline character.
|
|
||||||
*/
|
|
||||||
extern ssize_t UtilGetLine(char **, size_t *, Stream *);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get a unique number associated with the current thread.
|
|
||||||
* Numbers are assigned in the order that threads call this
|
|
||||||
* function, but are guaranteed to be unique in identifying
|
|
||||||
* the thread in a more human-readable way than just casting
|
|
||||||
* the return value of
|
|
||||||
* .Fn pthread_self
|
|
||||||
* to a number.
|
|
||||||
*/
|
|
||||||
extern UInt32 UtilThreadNo(void);
|
|
||||||
|
|
||||||
#endif /* CYTOPLASM_UTIL_H */
|
|
|
@ -1,568 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright (C) 2022-2023 Jordan Bancino <@jordan:bancino.net>
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person
|
|
||||||
* obtaining a copy of this software and associated documentation files
|
|
||||||
* (the "Software"), to deal in the Software without restriction,
|
|
||||||
* including without limitation the rights to use, copy, modify, merge,
|
|
||||||
* publish, distribute, sublicense, and/or sell copies of the Software,
|
|
||||||
* and to permit persons to whom the Software is furnished to do so,
|
|
||||||
* subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be
|
|
||||||
* included in all copies or portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
||||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
||||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
||||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
||||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
||||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
||||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
||||||
* SOFTWARE.
|
|
||||||
*/
|
|
||||||
#include <HeaderParser.h>
|
|
||||||
|
|
||||||
#include <HashMap.h>
|
|
||||||
#include <Str.h>
|
|
||||||
#include <Memory.h>
|
|
||||||
#include <Args.h>
|
|
||||||
|
|
||||||
#include <string.h>
|
|
||||||
#include <ctype.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <time.h>
|
|
||||||
|
|
||||||
#include <errno.h>
|
|
||||||
|
|
||||||
typedef struct DocDecl
|
|
||||||
{
|
|
||||||
char docs[HEADER_EXPR_MAX];
|
|
||||||
HeaderDeclaration decl;
|
|
||||||
} DocDecl;
|
|
||||||
|
|
||||||
typedef struct DocTypedef
|
|
||||||
{
|
|
||||||
char docs[HEADER_EXPR_MAX];
|
|
||||||
char text[HEADER_EXPR_MAX];
|
|
||||||
} DocTypedef;
|
|
||||||
|
|
||||||
typedef struct DocGlobal
|
|
||||||
{
|
|
||||||
char docs[HEADER_EXPR_MAX];
|
|
||||||
HeaderGlobal global;
|
|
||||||
} DocGlobal;
|
|
||||||
|
|
||||||
static void
|
|
||||||
ParseMainBlock(HashMap * registers, Array * descr, char *comment)
|
|
||||||
{
|
|
||||||
char *line = strtok(comment, "\n");
|
|
||||||
|
|
||||||
while (line)
|
|
||||||
{
|
|
||||||
while (*line && (isspace(*line) || *line == '*'))
|
|
||||||
{
|
|
||||||
line++;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!*line)
|
|
||||||
{
|
|
||||||
goto next;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (*line == '@')
|
|
||||||
{
|
|
||||||
int i = 0;
|
|
||||||
|
|
||||||
line++;
|
|
||||||
|
|
||||||
while (!isspace(line[i]))
|
|
||||||
{
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
|
|
||||||
line[i] = '\0';
|
|
||||||
|
|
||||||
Free(HashMapSet(registers, line, StrDuplicate(line + i + 1)));
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
ArrayAdd(descr, StrDuplicate(line));
|
|
||||||
}
|
|
||||||
|
|
||||||
next:
|
|
||||||
line = strtok(NULL, "\n");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
|
||||||
Main(Array * args)
|
|
||||||
{
|
|
||||||
HeaderExpr expr;
|
|
||||||
size_t i;
|
|
||||||
char *key;
|
|
||||||
char *val;
|
|
||||||
int exit = EXIT_SUCCESS;
|
|
||||||
ArgParseState arg;
|
|
||||||
|
|
||||||
HashMap *registers = HashMapCreate();
|
|
||||||
Array *descr = ArrayCreate();
|
|
||||||
|
|
||||||
Array *declarations = ArrayCreate();
|
|
||||||
DocDecl *decl = NULL;
|
|
||||||
|
|
||||||
Array *typedefs = ArrayCreate();
|
|
||||||
DocTypedef *type = NULL;
|
|
||||||
|
|
||||||
Array *globals = ArrayCreate();
|
|
||||||
DocGlobal *global = NULL;
|
|
||||||
|
|
||||||
char comment[HEADER_EXPR_MAX];
|
|
||||||
int isDocumented = 0;
|
|
||||||
|
|
||||||
Stream *in = NULL;
|
|
||||||
Stream *out = NULL;
|
|
||||||
|
|
||||||
int opt;
|
|
||||||
|
|
||||||
ArgParseStateInit(&arg);
|
|
||||||
while ((opt = ArgParse(&arg, args, "i:o:D:")) != -1)
|
|
||||||
{
|
|
||||||
switch (opt)
|
|
||||||
{
|
|
||||||
case 'i':
|
|
||||||
if (in)
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (StrEquals(arg.optArg, "-"))
|
|
||||||
{
|
|
||||||
in = StreamStdin();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
int len = strlen(arg.optArg);
|
|
||||||
|
|
||||||
in = StreamOpen(arg.optArg, "r");
|
|
||||||
if (!in)
|
|
||||||
{
|
|
||||||
StreamPrintf(StreamStderr(), "Error: %s:%s",
|
|
||||||
arg.optArg, strerror(errno));
|
|
||||||
exit = EXIT_FAILURE;
|
|
||||||
goto finish;
|
|
||||||
}
|
|
||||||
|
|
||||||
while (arg.optArg[len - 1] != '.')
|
|
||||||
{
|
|
||||||
arg.optArg[len - 1] = '\0';
|
|
||||||
len--;
|
|
||||||
}
|
|
||||||
|
|
||||||
arg.optArg[len - 1] = '\0';
|
|
||||||
len--;
|
|
||||||
|
|
||||||
HashMapSet(registers, "Nm", StrDuplicate(arg.optArg));
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case 'o':
|
|
||||||
if (out)
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (StrEquals(arg.optArg, "-"))
|
|
||||||
{
|
|
||||||
out = StreamStdout();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
out = StreamOpen(arg.optArg, "w");
|
|
||||||
if (!out)
|
|
||||||
{
|
|
||||||
StreamPrintf(StreamStderr(), "Error: %s:%s",
|
|
||||||
arg.optArg, strerror(errno));
|
|
||||||
exit = EXIT_FAILURE;
|
|
||||||
goto finish;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case 'D':
|
|
||||||
val = arg.optArg;
|
|
||||||
while (*val && *val != '=')
|
|
||||||
{
|
|
||||||
val++;
|
|
||||||
}
|
|
||||||
if (!*val || *val != '=')
|
|
||||||
{
|
|
||||||
StreamPrintf(StreamStderr(), "Bad register definition: %s",
|
|
||||||
arg.optArg);
|
|
||||||
exit = EXIT_FAILURE;
|
|
||||||
goto finish;
|
|
||||||
}
|
|
||||||
|
|
||||||
*val = '\0';
|
|
||||||
val++;
|
|
||||||
HashMapSet(registers, arg.optArg, StrDuplicate(val));
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!in)
|
|
||||||
{
|
|
||||||
in = StreamStdin();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!out)
|
|
||||||
{
|
|
||||||
out = StreamStdout();
|
|
||||||
}
|
|
||||||
|
|
||||||
memset(&expr, 0, sizeof(expr));
|
|
||||||
|
|
||||||
while (1)
|
|
||||||
{
|
|
||||||
HeaderParse(in, &expr);
|
|
||||||
|
|
||||||
switch (expr.type)
|
|
||||||
{
|
|
||||||
case HP_PREPROCESSOR_DIRECTIVE:
|
|
||||||
/* Ignore */
|
|
||||||
break;
|
|
||||||
case HP_EOF:
|
|
||||||
/* Done parsing */
|
|
||||||
goto last;
|
|
||||||
case HP_PARSE_ERROR:
|
|
||||||
case HP_SYNTAX_ERROR:
|
|
||||||
StreamPrintf(StreamStderr(), "Parse Error: (line %d) %s\n",
|
|
||||||
expr.data.error.lineNo, expr.data.error.msg);
|
|
||||||
exit = EXIT_FAILURE;
|
|
||||||
goto finish;
|
|
||||||
case HP_COMMENT:
|
|
||||||
if (expr.data.text[0] != '*')
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (strncmp(expr.data.text, "**", 2) == 0)
|
|
||||||
{
|
|
||||||
ParseMainBlock(registers, descr, expr.data.text);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
strncpy(comment, expr.data.text, sizeof(comment));
|
|
||||||
isDocumented = 1;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case HP_TYPEDEF:
|
|
||||||
if (HashMapGet(registers, "ignore-typedefs"))
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!isDocumented)
|
|
||||||
{
|
|
||||||
StreamPrintf(StreamStderr(),
|
|
||||||
"Error: Undocumented typedef:\n%s\n",
|
|
||||||
expr.data.text);
|
|
||||||
exit = EXIT_FAILURE;
|
|
||||||
goto finish;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
type = Malloc(sizeof(DocTypedef));
|
|
||||||
strncpy(type->docs, comment, sizeof(type->docs));
|
|
||||||
strncpy(type->text, expr.data.text, sizeof(type->text));
|
|
||||||
ArrayAdd(typedefs, type);
|
|
||||||
isDocumented = 0;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case HP_DECLARATION:
|
|
||||||
if (!isDocumented)
|
|
||||||
{
|
|
||||||
StreamPrintf(StreamStderr(),
|
|
||||||
"Error: %s() is undocumented.\n",
|
|
||||||
expr.data.declaration.name);
|
|
||||||
exit = EXIT_FAILURE;
|
|
||||||
goto finish;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
decl = Malloc(sizeof(DocDecl));
|
|
||||||
decl->decl = expr.data.declaration;
|
|
||||||
decl->decl.args = ArrayCreate();
|
|
||||||
strncpy(decl->docs, comment, sizeof(decl->docs));
|
|
||||||
for (i = 0; i < ArraySize(expr.data.declaration.args); i++)
|
|
||||||
{
|
|
||||||
ArrayAdd(decl->decl.args, StrDuplicate(ArrayGet(expr.data.declaration.args, i)));
|
|
||||||
}
|
|
||||||
ArrayAdd(declarations, decl);
|
|
||||||
isDocumented = 0;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case HP_GLOBAL:
|
|
||||||
if (!isDocumented)
|
|
||||||
{
|
|
||||||
StreamPrintf(StreamStderr(),
|
|
||||||
"Error: Global %s is undocumented.\n",
|
|
||||||
expr.data.global.name);
|
|
||||||
exit = EXIT_FAILURE;
|
|
||||||
goto finish;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
global = Malloc(sizeof(DocGlobal));
|
|
||||||
global->global = expr.data.global;
|
|
||||||
|
|
||||||
strncpy(global->docs, comment, sizeof(global->docs));
|
|
||||||
ArrayAdd(globals, global);
|
|
||||||
isDocumented = 0;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case HP_UNKNOWN:
|
|
||||||
if (HashMapGet(registers, "suppress-warnings"))
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
StreamPrintf(StreamStderr(), "Warning: Unknown expression: %s\n",
|
|
||||||
expr.data.text);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
StreamPrintf(StreamStderr(), "Unknown header type: %d\n", expr.type);
|
|
||||||
StreamPrintf(StreamStderr(), "This is a programming error.\n");
|
|
||||||
exit = EXIT_FAILURE;
|
|
||||||
goto finish;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
last:
|
|
||||||
val = HashMapGet(registers, "Nm");
|
|
||||||
if (!val)
|
|
||||||
{
|
|
||||||
HashMapSet(registers, "Nm", StrDuplicate("Unnamed"));
|
|
||||||
}
|
|
||||||
|
|
||||||
val = HashMapGet(registers, "Dd");
|
|
||||||
if (!val)
|
|
||||||
{
|
|
||||||
time_t currentTime;
|
|
||||||
struct tm *timeInfo;
|
|
||||||
char tsBuf[1024];
|
|
||||||
|
|
||||||
currentTime = time(NULL);
|
|
||||||
timeInfo = localtime(¤tTime);
|
|
||||||
strftime(tsBuf, sizeof(tsBuf), "%B %d %Y", timeInfo);
|
|
||||||
|
|
||||||
val = tsBuf;
|
|
||||||
}
|
|
||||||
StreamPrintf(out, ".Dd $%s: %s $\n", "Mdocdate", val);
|
|
||||||
|
|
||||||
val = HashMapGet(registers, "Os");
|
|
||||||
if (val)
|
|
||||||
{
|
|
||||||
StreamPrintf(out, ".Os %s\n", val);
|
|
||||||
}
|
|
||||||
|
|
||||||
val = HashMapGet(registers, "Nm");
|
|
||||||
StreamPrintf(out, ".Dt %s 3\n", val);
|
|
||||||
StreamPrintf(out, ".Sh NAME\n");
|
|
||||||
StreamPrintf(out, ".Nm %s\n", val);
|
|
||||||
|
|
||||||
val = HashMapGet(registers, "Nd");
|
|
||||||
if (!val)
|
|
||||||
{
|
|
||||||
val = "No Description.";
|
|
||||||
}
|
|
||||||
StreamPrintf(out, ".Nd %s\n", val);
|
|
||||||
|
|
||||||
StreamPrintf(out, ".Sh SYNOPSIS\n");
|
|
||||||
val = HashMapGet(registers, "Nm");
|
|
||||||
StreamPrintf(out, ".In %s.h\n", val);
|
|
||||||
for (i = 0; i < ArraySize(declarations); i++)
|
|
||||||
{
|
|
||||||
size_t j;
|
|
||||||
|
|
||||||
decl = ArrayGet(declarations, i);
|
|
||||||
StreamPrintf(out, ".Ft %s\n", decl->decl.returnType);
|
|
||||||
StreamPrintf(out, ".Fn %s ", decl->decl.name);
|
|
||||||
for (j = 0; j < ArraySize(decl->decl.args); j++)
|
|
||||||
{
|
|
||||||
StreamPrintf(out, "\"%s\" ", ArrayGet(decl->decl.args, j));
|
|
||||||
}
|
|
||||||
StreamPutc(out, '\n');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ArraySize(globals))
|
|
||||||
{
|
|
||||||
StreamPrintf(out, ".Sh GLOBALS\n");
|
|
||||||
for (i = 0; i < ArraySize(globals); i++)
|
|
||||||
{
|
|
||||||
char *line;
|
|
||||||
global = ArrayGet(globals, i);
|
|
||||||
|
|
||||||
StreamPrintf(out, ".Ss %s %s\n", global->global.type, global->global.name);
|
|
||||||
|
|
||||||
line = strtok(global->docs, "\n");
|
|
||||||
while (line)
|
|
||||||
{
|
|
||||||
while (*line && (isspace(*line) || *line == '*'))
|
|
||||||
{
|
|
||||||
line++;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (*line)
|
|
||||||
{
|
|
||||||
StreamPrintf(out, "%s\n", line);
|
|
||||||
}
|
|
||||||
|
|
||||||
line = strtok(NULL, "\n");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ArraySize(typedefs))
|
|
||||||
{
|
|
||||||
StreamPrintf(out, ".Sh TYPE DECLARATIONS\n");
|
|
||||||
for (i = 0; i < ArraySize(typedefs); i++)
|
|
||||||
{
|
|
||||||
char *line;
|
|
||||||
|
|
||||||
type = ArrayGet(typedefs, i);
|
|
||||||
StreamPrintf(out, ".Bd -literal -offset indent\n");
|
|
||||||
StreamPrintf(out, "%s\n", type->text);
|
|
||||||
StreamPrintf(out, ".Ed\n.Pp\n");
|
|
||||||
|
|
||||||
line = strtok(type->docs, "\n");
|
|
||||||
while (line)
|
|
||||||
{
|
|
||||||
while (*line && (isspace(*line) || *line == '*'))
|
|
||||||
{
|
|
||||||
line++;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (*line)
|
|
||||||
{
|
|
||||||
StreamPrintf(out, "%s\n", line);
|
|
||||||
}
|
|
||||||
|
|
||||||
line = strtok(NULL, "\n");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
StreamPrintf(out, ".Sh DESCRIPTION\n");
|
|
||||||
for (i = 0; i < ArraySize(descr); i++)
|
|
||||||
{
|
|
||||||
StreamPrintf(out, "%s\n", ArrayGet(descr, i));
|
|
||||||
}
|
|
||||||
|
|
||||||
for (i = 0; i < ArraySize(declarations); i++)
|
|
||||||
{
|
|
||||||
size_t j;
|
|
||||||
char *line;
|
|
||||||
|
|
||||||
decl = ArrayGet(declarations, i);
|
|
||||||
StreamPrintf(out, ".Ss %s %s(",
|
|
||||||
decl->decl.returnType, decl->decl.name);
|
|
||||||
for (j = 0; j < ArraySize(decl->decl.args); j++)
|
|
||||||
{
|
|
||||||
StreamPrintf(out, "%s", ArrayGet(decl->decl.args, j));
|
|
||||||
if (j < ArraySize(decl->decl.args) - 1)
|
|
||||||
{
|
|
||||||
StreamPuts(out, ", ");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
StreamPuts(out, ")\n");
|
|
||||||
|
|
||||||
line = strtok(decl->docs, "\n");
|
|
||||||
while (line)
|
|
||||||
{
|
|
||||||
while (*line && (isspace(*line) || *line == '*'))
|
|
||||||
{
|
|
||||||
line++;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (*line)
|
|
||||||
{
|
|
||||||
StreamPrintf(out, "%s\n", line);
|
|
||||||
}
|
|
||||||
|
|
||||||
line = strtok(NULL, "\n");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
val = HashMapGet(registers, "Xr");
|
|
||||||
if (val)
|
|
||||||
{
|
|
||||||
char *xr = strtok(val, " ");
|
|
||||||
|
|
||||||
StreamPrintf(out, ".Sh SEE ALSO\n");
|
|
||||||
while (xr)
|
|
||||||
{
|
|
||||||
if (*xr)
|
|
||||||
{
|
|
||||||
StreamPrintf(out, ".Xr %s 3 ", xr);
|
|
||||||
}
|
|
||||||
|
|
||||||
xr = strtok(NULL, " ");
|
|
||||||
|
|
||||||
if (xr)
|
|
||||||
{
|
|
||||||
StreamPutc(out, ',');
|
|
||||||
}
|
|
||||||
StreamPutc(out, '\n');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
finish:
|
|
||||||
if (in != StreamStdin())
|
|
||||||
{
|
|
||||||
StreamClose(in);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (out != StreamStdout())
|
|
||||||
{
|
|
||||||
StreamClose(out);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (i = 0; i < ArraySize(declarations); i++)
|
|
||||||
{
|
|
||||||
size_t j;
|
|
||||||
|
|
||||||
decl = ArrayGet(declarations, i);
|
|
||||||
for (j = 0; j < ArraySize(decl->decl.args); j++)
|
|
||||||
{
|
|
||||||
Free(ArrayGet(decl->decl.args, j));
|
|
||||||
}
|
|
||||||
ArrayFree(decl->decl.args);
|
|
||||||
Free(decl);
|
|
||||||
}
|
|
||||||
ArrayFree(declarations);
|
|
||||||
|
|
||||||
for (i = 0; i < ArraySize(typedefs); i++)
|
|
||||||
{
|
|
||||||
Free(ArrayGet(typedefs, i));
|
|
||||||
}
|
|
||||||
ArrayFree(typedefs);
|
|
||||||
|
|
||||||
for (i = 0; i < ArraySize(globals); i++)
|
|
||||||
{
|
|
||||||
Free(ArrayGet(globals, i));
|
|
||||||
}
|
|
||||||
ArrayFree(globals);
|
|
||||||
|
|
||||||
for (i = 0; i < ArraySize(descr); i++)
|
|
||||||
{
|
|
||||||
Free(ArrayGet(descr, i));
|
|
||||||
}
|
|
||||||
ArrayFree(descr);
|
|
||||||
|
|
||||||
while (HashMapIterate(registers, &key, (void **) &val))
|
|
||||||
{
|
|
||||||
Free(val);
|
|
||||||
}
|
|
||||||
|
|
||||||
HashMapFree(registers);
|
|
||||||
|
|
||||||
return exit;
|
|
||||||
}
|
|
|
@ -1,145 +0,0 @@
|
||||||
#include <Int64.h>
|
|
||||||
|
|
||||||
#include <Log.h>
|
|
||||||
|
|
||||||
/* AssertEquals(actual, expected) */
|
|
||||||
int
|
|
||||||
AssertEquals(char *msg, Int64 x, Int64 y)
|
|
||||||
{
|
|
||||||
if (!Int64Eq(x, y))
|
|
||||||
{
|
|
||||||
Log(LOG_ERR, "%s: Expected 0x%X 0x%X, got 0x%X 0x%X", msg,
|
|
||||||
Int64High(y), Int64Low(y),
|
|
||||||
Int64High(x), Int64Low(x));
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
|
||||||
Main(void)
|
|
||||||
{
|
|
||||||
Int64 x, y;
|
|
||||||
|
|
||||||
Log(LOG_INFO, "sizeof(Int64) = %lu", sizeof(Int64));
|
|
||||||
|
|
||||||
#ifdef INT64_NATIVE
|
|
||||||
Log(LOG_INFO, "Using native 64-bit integers.");
|
|
||||||
#else
|
|
||||||
Log(LOG_INFO, "Using emulated 64-bit integers.");
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/* BSR Tests */
|
|
||||||
|
|
||||||
x = Int64Create(0x000000FF, 0x00000000);
|
|
||||||
|
|
||||||
y = Int64Sra(x, 4);
|
|
||||||
AssertEquals("x >> 4", y, Int64Create(0x0000000F, 0xF0000000));
|
|
||||||
|
|
||||||
y = Int64Sra(x, 8);
|
|
||||||
AssertEquals("x >> 8", y, Int64Create(0x00000000, 0xFF000000));
|
|
||||||
|
|
||||||
y = Int64Sra(x, 36);
|
|
||||||
AssertEquals("x >> 36", y, Int64Create(0x00000000, 0x0000000F));
|
|
||||||
|
|
||||||
x = Int64Create(0xFF000000, 0x00000000);
|
|
||||||
|
|
||||||
y = Int64Sra(x, 4);
|
|
||||||
AssertEquals("x >> 4", y, Int64Create(0xFFF00000, 0x00000000));
|
|
||||||
|
|
||||||
y = Int64Sra(x, 8);
|
|
||||||
AssertEquals("x >> 8", y, Int64Create(0xFFFF0000, 0x00000000));
|
|
||||||
|
|
||||||
y = Int64Sra(x, 63);
|
|
||||||
AssertEquals("x >> 63", y, Int64Create(0xFFFFFFFF, 0xFFFFFFFF));
|
|
||||||
|
|
||||||
/* BSL Tests */
|
|
||||||
|
|
||||||
x = Int64Create(0x00000000, 0xFF000000);
|
|
||||||
|
|
||||||
y = Int64Sll(x, 4);
|
|
||||||
AssertEquals("x << 4", y, Int64Create(0x0000000F, 0xF0000000));
|
|
||||||
|
|
||||||
y = Int64Sll(x, 8);
|
|
||||||
AssertEquals("x << 8", y, Int64Create(0x000000FF, 0x00000000));
|
|
||||||
|
|
||||||
y = Int64Sll(x, 36);
|
|
||||||
AssertEquals("x << 36", y, Int64Create(0xF0000000, 0x00000000));
|
|
||||||
|
|
||||||
/* ADD Tests */
|
|
||||||
|
|
||||||
x = Int64Create(0x00000000, 0xF0000001);
|
|
||||||
y = Int64Create(0x00000000, 0x00000002);
|
|
||||||
AssertEquals("0xF0000001 + 0x00000002", Int64Add(x, y), Int64Create(0x00000000, 0xF0000003));
|
|
||||||
|
|
||||||
x = Int64Create(0x00000000, 0xF0000000);
|
|
||||||
y = Int64Create(0x00000000, 0x10000000);
|
|
||||||
AssertEquals("0xF0000000 + 0x10000000", Int64Add(x, y), Int64Create(0x00000001, 0x00000000));
|
|
||||||
|
|
||||||
x = Int64Create(0, 5);
|
|
||||||
y = Int64Neg(Int64Create(0, 10));
|
|
||||||
AssertEquals("5 + (-10)", Int64Add(x, y), Int64Neg(Int64Create(0, 5)));
|
|
||||||
|
|
||||||
/* SUB Tests */
|
|
||||||
x = Int64Create(0x00000000, 0x00000005);
|
|
||||||
y = Int64Create(0x00000000, 0x00000002);
|
|
||||||
AssertEquals("0x00000005 - 0x00000002", Int64Sub(x, y), Int64Create(0x00000000, 0x00000003));
|
|
||||||
|
|
||||||
x = Int64Create(0x00000001, 0x00000000);
|
|
||||||
y = Int64Create(0x00000000, 0x00000001);
|
|
||||||
AssertEquals("0x00000001 0x00000000 - 0x00000001", Int64Sub(x, y), Int64Create(0x00000000, 0xFFFFFFFF));
|
|
||||||
|
|
||||||
x = Int64Create(0, 5);
|
|
||||||
y = Int64Create(0, 10);
|
|
||||||
AssertEquals("5 - 10", Int64Sub(x, y), Int64Neg(Int64Create(0, 5)));
|
|
||||||
|
|
||||||
x = Int64Create(0, 5);
|
|
||||||
y = Int64Neg(Int64Create(0, 10));
|
|
||||||
AssertEquals("5 - (-10)", Int64Sub(x, y), Int64Create(0, 15));
|
|
||||||
|
|
||||||
/* MUL Tests */
|
|
||||||
x = Int64Create(0, 18);
|
|
||||||
y = Int64Create(0, 1);
|
|
||||||
AssertEquals("18 * 1", Int64Mul(x, y), Int64Create(0, 18));
|
|
||||||
|
|
||||||
x = Int64Create(0, 20);
|
|
||||||
y = Int64Create(0, 12);
|
|
||||||
AssertEquals("20 * 12", Int64Mul(x, y), Int64Create(0, 240));
|
|
||||||
|
|
||||||
x = Int64Create(0x00000000, 0x00000005);
|
|
||||||
y = Int64Create(0x00000000, 0x00000005);
|
|
||||||
AssertEquals("0x00000005 * 0x00000005", Int64Mul(x, y), Int64Create(0x00000000, 0x00000019));
|
|
||||||
|
|
||||||
x = Int64Create(0x00000001, 0x00000000);
|
|
||||||
y = Int64Create(0x00000000, 0x00000005);
|
|
||||||
AssertEquals("0x00000001 0x00000000 * 0x00000005", Int64Mul(x, y), Int64Create(0x00000005, 0x00000000));
|
|
||||||
|
|
||||||
/* DIV Tests */
|
|
||||||
x = Int64Create(0, 12);
|
|
||||||
y = Int64Create(0, 4);
|
|
||||||
AssertEquals("12 / 4", Int64Div(x, y), Int64Create(0, 3));
|
|
||||||
|
|
||||||
/* MOD Tests */
|
|
||||||
x = Int64Create(0x000000FF, 0x00000000);
|
|
||||||
y = Int64Create(0x00000000, 0x00000010);
|
|
||||||
AssertEquals("0x000000FF 0x00000000 mod 0x00000010", Int64Rem(x, y), Int64Create(0, 0));
|
|
||||||
|
|
||||||
x = Int64Create(0x00000000, 0xFF000000);
|
|
||||||
y = Int64Create(0x00000000, 0x00000010);
|
|
||||||
AssertEquals("0x00000000 0xFF000000 mod 0x00000010", Int64Rem(x, y), Int64Create(0, 0));
|
|
||||||
|
|
||||||
x = Int64Create(0xFF000000, 0x00000000);
|
|
||||||
y = Int64Create(0x00000000, 0x00000010);
|
|
||||||
AssertEquals("0xFF000000 0x00000000 mod 0x00000010", Int64Rem(x, y), Int64Create(0, 0));
|
|
||||||
|
|
||||||
x = Int64Create(0x00000000, 0x000000F0);
|
|
||||||
y = Int64Create(0x00000000, 0x00000010);
|
|
||||||
AssertEquals("0x00000000 0x000000F0 mod 0x00000010", Int64Rem(x, y), Int64Create(0, 0));
|
|
||||||
|
|
||||||
/* TODO: Add more tests for negative multiplication, division, and
|
|
||||||
* mod */
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
File diff suppressed because it is too large
Load diff
|
@ -1,787 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright (C) 2022-2023 Jordan Bancino <@jordan:bancino.net>
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person
|
|
||||||
* obtaining a copy of this software and associated documentation files
|
|
||||||
* (the "Software"), to deal in the Software without restriction,
|
|
||||||
* including without limitation the rights to use, copy, modify, merge,
|
|
||||||
* publish, distribute, sublicense, and/or sell copies of the Software,
|
|
||||||
* and to permit persons to whom the Software is furnished to do so,
|
|
||||||
* subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be
|
|
||||||
* included in all copies or portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
||||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
||||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
||||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
||||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
||||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
||||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
||||||
* SOFTWARE.
|
|
||||||
*/
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <ctype.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <time.h>
|
|
||||||
|
|
||||||
#include <Args.h>
|
|
||||||
#include <Memory.h>
|
|
||||||
#include <Array.h>
|
|
||||||
#include <HashMap.h>
|
|
||||||
#include <Str.h>
|
|
||||||
#include <Stream.h>
|
|
||||||
|
|
||||||
#define BLOCK 64
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Anything that can be done in the language itself should; to
|
|
||||||
* keep the interpreter minimal. Note that this has the side effect
|
|
||||||
* of allowing the runtime to become corrupted or undefined which
|
|
||||||
* may lead to confusing behavior, but the language primitives
|
|
||||||
* cannot be corrupted, so things can always be corrected.
|
|
||||||
*/
|
|
||||||
static char runtime[] =
|
|
||||||
"[def let; name, value; [eval [(]def [name][;][;][value][)]]]"
|
|
||||||
"[def not; cond; [if [cond];;true]]"
|
|
||||||
"[def (); str; [(][str][)]]"
|
|
||||||
"[def #;;]"
|
|
||||||
"[def //;;]"
|
|
||||||
"[def day;; [time %d]]"
|
|
||||||
"[def month;; [time %m]]"
|
|
||||||
"[def year;; [time %Y]]"
|
|
||||||
"[def day-name;; [time %A]]"
|
|
||||||
"[def month-name;; [time %B]]"
|
|
||||||
"[def hour;; [time %I]]"
|
|
||||||
"[def minute;; [time %M]]"
|
|
||||||
"[def second;; [time %S]]"
|
|
||||||
"[def am/pm;; [time %p]]"
|
|
||||||
"[def timezone;; [time %Z]]"
|
|
||||||
"[def date;; [day-name], [month-name] [day], [year]]"
|
|
||||||
"[def timestamp;; [hour]:[minute]:[second] [am/pm] [timezone]]"
|
|
||||||
;
|
|
||||||
|
|
||||||
static Stream *err;
|
|
||||||
static int debug;
|
|
||||||
|
|
||||||
typedef struct Definition
|
|
||||||
{
|
|
||||||
Array *args;
|
|
||||||
char *body;
|
|
||||||
} Definition;
|
|
||||||
|
|
||||||
static char *
|
|
||||||
EvalExpr(char *expr, Array * stack);
|
|
||||||
|
|
||||||
static char *
|
|
||||||
ReadExpr(Stream * in)
|
|
||||||
{
|
|
||||||
size_t len = 0;
|
|
||||||
size_t size = 0;
|
|
||||||
|
|
||||||
size_t level = 1;
|
|
||||||
|
|
||||||
int c;
|
|
||||||
|
|
||||||
char *expr;
|
|
||||||
char *tmp;
|
|
||||||
|
|
||||||
size += BLOCK;
|
|
||||||
expr = Malloc(size * sizeof(char));
|
|
||||||
if (!expr)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
while ((c = StreamGetc(in)) != EOF)
|
|
||||||
{
|
|
||||||
if (len + 1 >= size)
|
|
||||||
{
|
|
||||||
size += BLOCK;
|
|
||||||
tmp = Realloc(expr, size * sizeof(char));
|
|
||||||
if (!tmp)
|
|
||||||
{
|
|
||||||
Free(expr);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
expr = tmp;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (c == ']')
|
|
||||||
{
|
|
||||||
if (level > 0)
|
|
||||||
{
|
|
||||||
level--;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!level)
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (c == '[')
|
|
||||||
{
|
|
||||||
level++;
|
|
||||||
}
|
|
||||||
|
|
||||||
expr[len] = c;
|
|
||||||
len++;
|
|
||||||
}
|
|
||||||
|
|
||||||
expr[len] = '\0';
|
|
||||||
return expr;
|
|
||||||
}
|
|
||||||
|
|
||||||
static char *
|
|
||||||
Eval(char **argp, Array * stack)
|
|
||||||
{
|
|
||||||
size_t len;
|
|
||||||
size_t size;
|
|
||||||
|
|
||||||
char *res;
|
|
||||||
char *tmp;
|
|
||||||
char *arg = *argp;
|
|
||||||
|
|
||||||
len = 0;
|
|
||||||
size = BLOCK;
|
|
||||||
res = Malloc(size * sizeof(char));
|
|
||||||
if (!res)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
while (*arg)
|
|
||||||
{
|
|
||||||
if (len + 1 >= size)
|
|
||||||
{
|
|
||||||
size += BLOCK;
|
|
||||||
tmp = Realloc(res, size * sizeof(char));
|
|
||||||
if (!tmp)
|
|
||||||
{
|
|
||||||
Free(res);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
res = tmp;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (*arg == ';')
|
|
||||||
{
|
|
||||||
res[len] = '\0';
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
else if (*arg == '[')
|
|
||||||
{
|
|
||||||
size_t level = 1;
|
|
||||||
char *endp;
|
|
||||||
char *evalRes;
|
|
||||||
char *base;
|
|
||||||
|
|
||||||
arg++;
|
|
||||||
endp = arg;
|
|
||||||
|
|
||||||
while (level && *endp)
|
|
||||||
{
|
|
||||||
if (*endp == ']')
|
|
||||||
{
|
|
||||||
if (level > 0)
|
|
||||||
{
|
|
||||||
level--;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!level)
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (*endp == '[')
|
|
||||||
{
|
|
||||||
level++;
|
|
||||||
}
|
|
||||||
|
|
||||||
endp++;
|
|
||||||
}
|
|
||||||
|
|
||||||
*endp = '\0';
|
|
||||||
|
|
||||||
evalRes = EvalExpr(arg, stack);
|
|
||||||
base = evalRes;
|
|
||||||
|
|
||||||
while (*evalRes)
|
|
||||||
{
|
|
||||||
if (len + 1 >= size)
|
|
||||||
{
|
|
||||||
size += BLOCK;
|
|
||||||
tmp = Realloc(res, size * sizeof(char));
|
|
||||||
if (!tmp)
|
|
||||||
{
|
|
||||||
Free(base);
|
|
||||||
Free(res);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
res = tmp;
|
|
||||||
}
|
|
||||||
|
|
||||||
res[len] = *evalRes;
|
|
||||||
len++;
|
|
||||||
evalRes++;
|
|
||||||
}
|
|
||||||
|
|
||||||
Free(base);
|
|
||||||
*endp = ']';
|
|
||||||
arg = endp;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
res[len] = *arg;
|
|
||||||
len++;
|
|
||||||
}
|
|
||||||
|
|
||||||
arg++;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (*arg == ';')
|
|
||||||
{
|
|
||||||
arg++;
|
|
||||||
while (isspace(*arg))
|
|
||||||
{
|
|
||||||
arg++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
res[len] = '\0';
|
|
||||||
*argp = arg;
|
|
||||||
|
|
||||||
return res;
|
|
||||||
}
|
|
||||||
|
|
||||||
static char *
|
|
||||||
EvalExpr(char *expr, Array * stack)
|
|
||||||
{
|
|
||||||
char *argv = expr;
|
|
||||||
char *op = expr;
|
|
||||||
char ops;
|
|
||||||
char *opi;
|
|
||||||
|
|
||||||
char *res;
|
|
||||||
|
|
||||||
while (*argv && !isspace(*argv))
|
|
||||||
{
|
|
||||||
argv++;
|
|
||||||
}
|
|
||||||
|
|
||||||
ops = *argv;
|
|
||||||
opi = argv;
|
|
||||||
|
|
||||||
*argv = '\0';
|
|
||||||
argv++;
|
|
||||||
|
|
||||||
|
|
||||||
if (StrEquals(op, "("))
|
|
||||||
{
|
|
||||||
res = StrDuplicate("[");
|
|
||||||
}
|
|
||||||
else if (StrEquals(op, ")"))
|
|
||||||
{
|
|
||||||
res = StrDuplicate("]");
|
|
||||||
}
|
|
||||||
else if (StrEquals(op, ";"))
|
|
||||||
{
|
|
||||||
res = StrDuplicate(";");
|
|
||||||
}
|
|
||||||
else if (StrEquals(op, "env"))
|
|
||||||
{
|
|
||||||
char *env = Eval(&argv, stack);
|
|
||||||
char *val = getenv(env);
|
|
||||||
|
|
||||||
Free(env);
|
|
||||||
|
|
||||||
if (!val)
|
|
||||||
{
|
|
||||||
val = "";
|
|
||||||
}
|
|
||||||
|
|
||||||
res = StrDuplicate(val);
|
|
||||||
}
|
|
||||||
else if (StrEquals(op, "time"))
|
|
||||||
{
|
|
||||||
char *fmt = Eval(&argv, stack);
|
|
||||||
|
|
||||||
time_t currentTime = time(NULL);
|
|
||||||
struct tm *timeInfo = localtime(¤tTime);
|
|
||||||
char *timestamp = Malloc(128 * sizeof(char));
|
|
||||||
|
|
||||||
if (strftime(timestamp, 128 * sizeof(char), fmt, timeInfo))
|
|
||||||
{
|
|
||||||
res = timestamp;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
res = StrDuplicate("");
|
|
||||||
Free(timestamp);
|
|
||||||
}
|
|
||||||
|
|
||||||
Free(fmt);
|
|
||||||
}
|
|
||||||
else if (StrEquals(op, "eval"))
|
|
||||||
{
|
|
||||||
char *expr = Eval(&argv, stack);
|
|
||||||
char *tmp = expr;
|
|
||||||
|
|
||||||
res = Eval(&tmp, stack);
|
|
||||||
Free(expr);
|
|
||||||
}
|
|
||||||
else if (StrEquals(op, "if"))
|
|
||||||
{
|
|
||||||
char *cond = Eval(&argv, stack);
|
|
||||||
|
|
||||||
/*
|
|
||||||
* If the condition is false, seek past the true argument
|
|
||||||
* without evaluating it.
|
|
||||||
*/
|
|
||||||
if (StrBlank(cond))
|
|
||||||
{
|
|
||||||
size_t level = 0;
|
|
||||||
|
|
||||||
while (*argv)
|
|
||||||
{
|
|
||||||
switch (*argv)
|
|
||||||
{
|
|
||||||
case '[':
|
|
||||||
level++;
|
|
||||||
break;
|
|
||||||
case ']':
|
|
||||||
if (level > 0)
|
|
||||||
{
|
|
||||||
level--;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case ';':
|
|
||||||
if (!level)
|
|
||||||
{
|
|
||||||
argv++;
|
|
||||||
while (isspace(*argv))
|
|
||||||
{
|
|
||||||
argv++;
|
|
||||||
}
|
|
||||||
goto end_loop;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
argv++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
end_loop:
|
|
||||||
res = Eval(&argv, stack);
|
|
||||||
Free(cond);
|
|
||||||
}
|
|
||||||
else if (StrEquals(op, "eq?"))
|
|
||||||
{
|
|
||||||
char *s1 = Eval(&argv, stack);
|
|
||||||
char *s2 = Eval(&argv, stack);
|
|
||||||
|
|
||||||
if (StrEquals(s1, s2))
|
|
||||||
{
|
|
||||||
res = StrDuplicate("true");
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
res = StrDuplicate("");
|
|
||||||
}
|
|
||||||
|
|
||||||
Free(s1);
|
|
||||||
Free(s2);
|
|
||||||
}
|
|
||||||
else if (StrEquals(op, "def?"))
|
|
||||||
{
|
|
||||||
Definition *def;
|
|
||||||
size_t i;
|
|
||||||
char *directive = Eval(&argv, stack);
|
|
||||||
|
|
||||||
for (i = 0; i < ArraySize(stack); i++)
|
|
||||||
{
|
|
||||||
HashMap *stackFrame = ArrayGet(stack, i);
|
|
||||||
|
|
||||||
def = HashMapGet(stackFrame, directive);
|
|
||||||
|
|
||||||
if (def)
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (def)
|
|
||||||
{
|
|
||||||
res = StrDuplicate("true");
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
res = StrDuplicate("");
|
|
||||||
}
|
|
||||||
|
|
||||||
Free(directive);
|
|
||||||
}
|
|
||||||
else if (StrEquals(op, "print"))
|
|
||||||
{
|
|
||||||
char *msg = Eval(&argv, stack);
|
|
||||||
|
|
||||||
StreamPrintf(err, msg);
|
|
||||||
Free(msg);
|
|
||||||
res = StrDuplicate("");
|
|
||||||
}
|
|
||||||
else if (StrEquals(op, "def"))
|
|
||||||
{
|
|
||||||
char *name = Eval(&argv, stack);
|
|
||||||
char *args = Eval(&argv, stack);
|
|
||||||
char *body = argv;
|
|
||||||
|
|
||||||
char *tok;
|
|
||||||
HashMap *stackFrame;
|
|
||||||
size_t i;
|
|
||||||
|
|
||||||
Definition *def = Malloc(sizeof(Definition));
|
|
||||||
|
|
||||||
def->body = StrDuplicate(body);
|
|
||||||
def->args = ArrayCreate();
|
|
||||||
|
|
||||||
if (debug)
|
|
||||||
{
|
|
||||||
StreamPrintf(err, "(def op = '%s', args = [", name);
|
|
||||||
}
|
|
||||||
|
|
||||||
tok = strtok(args, ",");
|
|
||||||
while (tok)
|
|
||||||
{
|
|
||||||
while (isspace(*tok))
|
|
||||||
{
|
|
||||||
tok++;
|
|
||||||
}
|
|
||||||
|
|
||||||
ArrayAdd(def->args, StrDuplicate(tok));
|
|
||||||
|
|
||||||
if (debug)
|
|
||||||
{
|
|
||||||
StreamPrintf(err, "'%s', ", tok);
|
|
||||||
}
|
|
||||||
|
|
||||||
tok = strtok(NULL, ",");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (debug)
|
|
||||||
{
|
|
||||||
StreamPrintf(err, "], body = '%s')\n", def->body);
|
|
||||||
}
|
|
||||||
|
|
||||||
Free(args);
|
|
||||||
stackFrame = ArrayGet(stack, ArraySize(stack) - 1);
|
|
||||||
def = HashMapSet(stackFrame, name, def);
|
|
||||||
|
|
||||||
if (def)
|
|
||||||
{
|
|
||||||
for (i = 0; i < ArraySize(def->args); i++)
|
|
||||||
{
|
|
||||||
Free(ArrayGet(def->args, i));
|
|
||||||
}
|
|
||||||
|
|
||||||
ArrayFree(def->args);
|
|
||||||
Free(def->body);
|
|
||||||
Free(def);
|
|
||||||
}
|
|
||||||
|
|
||||||
Free(name);
|
|
||||||
res = StrDuplicate("");
|
|
||||||
}
|
|
||||||
else if (StrEquals(op, "undef"))
|
|
||||||
{
|
|
||||||
char *name = Eval(&argv, stack);
|
|
||||||
HashMap *stackFrame = ArrayGet(stack, ArraySize(stack) - 1);
|
|
||||||
Definition *def = HashMapDelete(stackFrame, name);
|
|
||||||
|
|
||||||
Free(name);
|
|
||||||
|
|
||||||
if (def)
|
|
||||||
{
|
|
||||||
size_t i;
|
|
||||||
|
|
||||||
for (i = 0; i < ArraySize(def->args); i++)
|
|
||||||
{
|
|
||||||
Free(ArrayGet(def->args, i));
|
|
||||||
}
|
|
||||||
|
|
||||||
ArrayFree(def->args);
|
|
||||||
Free(def->body);
|
|
||||||
Free(def);
|
|
||||||
}
|
|
||||||
|
|
||||||
res = StrDuplicate("");
|
|
||||||
}
|
|
||||||
else if (StrEquals(op, "include"))
|
|
||||||
{
|
|
||||||
char *fileName = Eval(&argv, stack);
|
|
||||||
Stream *file = StreamOpen(fileName, "r");
|
|
||||||
|
|
||||||
size_t size;
|
|
||||||
size_t len;
|
|
||||||
char c;
|
|
||||||
|
|
||||||
Free(fileName);
|
|
||||||
if (!file)
|
|
||||||
{
|
|
||||||
res = StrDuplicate("");
|
|
||||||
goto finish;
|
|
||||||
}
|
|
||||||
|
|
||||||
size = BLOCK;
|
|
||||||
len = 0;
|
|
||||||
res = Malloc(size * sizeof(char));
|
|
||||||
|
|
||||||
while ((c = StreamGetc(file)) != EOF)
|
|
||||||
{
|
|
||||||
if (len + 1 >= size)
|
|
||||||
{
|
|
||||||
size += BLOCK;
|
|
||||||
res = Realloc(res, size * sizeof(char));
|
|
||||||
}
|
|
||||||
|
|
||||||
res[len] = c;
|
|
||||||
len++;
|
|
||||||
}
|
|
||||||
|
|
||||||
res[len] = '\0';
|
|
||||||
StreamClose(file);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
/* Locate macro on stack and execute it */
|
|
||||||
Definition *def;
|
|
||||||
char *body;
|
|
||||||
HashMap *stackFrame;
|
|
||||||
size_t i;
|
|
||||||
|
|
||||||
for (i = 0; i < ArraySize(stack); i++)
|
|
||||||
{
|
|
||||||
stackFrame = ArrayGet(stack, i);
|
|
||||||
def = HashMapGet(stackFrame, op);
|
|
||||||
|
|
||||||
if (def)
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!def)
|
|
||||||
{
|
|
||||||
res = StrDuplicate("");
|
|
||||||
goto finish;
|
|
||||||
}
|
|
||||||
|
|
||||||
stackFrame = HashMapCreate();
|
|
||||||
for (i = 0; i < ArraySize(def->args); i++)
|
|
||||||
{
|
|
||||||
char *argName = ArrayGet(def->args, i);
|
|
||||||
char *argVal = Eval(&argv, stack);
|
|
||||||
Definition *argDef = Malloc(sizeof(Definition));
|
|
||||||
|
|
||||||
argDef->args = NULL;
|
|
||||||
argDef->body = argVal;
|
|
||||||
HashMapSet(stackFrame, argName, argDef);
|
|
||||||
}
|
|
||||||
|
|
||||||
ArrayInsert(stack, 0, stackFrame);
|
|
||||||
body = def->body;
|
|
||||||
res = Eval(&body, stack);
|
|
||||||
|
|
||||||
ArrayDelete(stack, 0);
|
|
||||||
|
|
||||||
while (HashMapIterate(stackFrame, &body, (void **) &def))
|
|
||||||
{
|
|
||||||
for (i = 0; i < ArraySize(def->args); i++)
|
|
||||||
{
|
|
||||||
Free(ArrayGet(def->args, i));
|
|
||||||
}
|
|
||||||
ArrayFree(def->args);
|
|
||||||
Free(def->body);
|
|
||||||
Free(def);
|
|
||||||
}
|
|
||||||
HashMapFree(stackFrame);
|
|
||||||
}
|
|
||||||
|
|
||||||
finish:
|
|
||||||
*opi = ops;
|
|
||||||
return res;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int
|
|
||||||
Process(Stream * in, Stream * out, Array * stack)
|
|
||||||
{
|
|
||||||
int c;
|
|
||||||
|
|
||||||
while ((c = StreamGetc(in)) != EOF)
|
|
||||||
{
|
|
||||||
if (c == '[')
|
|
||||||
{
|
|
||||||
char *expr;
|
|
||||||
char *res;
|
|
||||||
|
|
||||||
expr = ReadExpr(in);
|
|
||||||
res = EvalExpr(expr, stack);
|
|
||||||
|
|
||||||
StreamPuts(out, res);
|
|
||||||
StreamFlush(out);
|
|
||||||
|
|
||||||
Free(expr);
|
|
||||||
Free(res);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
StreamPutc(out, c);
|
|
||||||
}
|
|
||||||
StreamFlush(out);
|
|
||||||
}
|
|
||||||
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
|
||||||
Main(Array * args)
|
|
||||||
{
|
|
||||||
ArgParseState arg;
|
|
||||||
int opt;
|
|
||||||
int ret = EXIT_FAILURE;
|
|
||||||
|
|
||||||
size_t i;
|
|
||||||
Array *stack;
|
|
||||||
|
|
||||||
char *rt;
|
|
||||||
|
|
||||||
Stream *in;
|
|
||||||
Stream *out;
|
|
||||||
Array *eval = ArrayCreate();
|
|
||||||
|
|
||||||
debug = 0;
|
|
||||||
in = StreamStdin();
|
|
||||||
out = StreamStdout();
|
|
||||||
err = StreamStderr();
|
|
||||||
|
|
||||||
ArgParseStateInit(&arg);
|
|
||||||
while ((opt = ArgParse(&arg, args, "i:o:e:d")) != -1)
|
|
||||||
{
|
|
||||||
switch (opt)
|
|
||||||
{
|
|
||||||
case 'i':
|
|
||||||
if (StrEquals(arg.optArg, "-"))
|
|
||||||
{
|
|
||||||
in = StreamStdin();
|
|
||||||
}
|
|
||||||
|
|
||||||
in = StreamOpen(arg.optArg, "r");
|
|
||||||
if (!in)
|
|
||||||
{
|
|
||||||
StreamPrintf(err, "Unable to open '%s' for reading\n", arg.optArg);
|
|
||||||
goto finish;
|
|
||||||
}
|
|
||||||
|
|
||||||
break;
|
|
||||||
case 'o':
|
|
||||||
if (StrEquals(arg.optArg, "-"))
|
|
||||||
{
|
|
||||||
out = StreamStdout();
|
|
||||||
}
|
|
||||||
|
|
||||||
out = StreamOpen(arg.optArg, "w");
|
|
||||||
if (!out)
|
|
||||||
{
|
|
||||||
StreamPrintf(err, "Unable to open '%s' for writing.\n", arg.optArg);
|
|
||||||
goto finish;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case 'e':
|
|
||||||
ArrayAdd(eval, StrDuplicate(arg.optArg));
|
|
||||||
break;
|
|
||||||
case 'd':
|
|
||||||
debug = 1;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
goto finish;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
stack = ArrayCreate();
|
|
||||||
ArrayAdd(stack, HashMapCreate());
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Evaluate the runtime environment, discarding any
|
|
||||||
* output because the purpose is only to set the initial
|
|
||||||
* state, not to produce extra output.
|
|
||||||
*/
|
|
||||||
rt = runtime;
|
|
||||||
Free(Eval(&rt, stack));
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Evaluate any expressions specified on the command line,
|
|
||||||
* interpolating them in the output if they produce anything
|
|
||||||
* because that is the expected behavior.
|
|
||||||
*/
|
|
||||||
for (i = 0; i < ArraySize(eval); i++)
|
|
||||||
{
|
|
||||||
char *expr = ArrayGet(eval, i);
|
|
||||||
char *res = Eval(&expr, stack);
|
|
||||||
|
|
||||||
StreamPuts(out, res);
|
|
||||||
Free(res);
|
|
||||||
}
|
|
||||||
|
|
||||||
Process(in, out, stack);
|
|
||||||
|
|
||||||
for (i = 0; i < ArraySize(stack); i++)
|
|
||||||
{
|
|
||||||
HashMap *stackFrame = ArrayGet(stack, i);
|
|
||||||
char *key;
|
|
||||||
Definition *val;
|
|
||||||
|
|
||||||
while (HashMapIterate(stackFrame, &key, (void **) &val))
|
|
||||||
{
|
|
||||||
size_t j;
|
|
||||||
|
|
||||||
for (j = 0; j < ArraySize(val->args); j++)
|
|
||||||
{
|
|
||||||
Free(ArrayGet(val->args, j));
|
|
||||||
}
|
|
||||||
|
|
||||||
ArrayFree(val->args);
|
|
||||||
|
|
||||||
Free(val->body);
|
|
||||||
Free(val);
|
|
||||||
}
|
|
||||||
|
|
||||||
HashMapFree(stackFrame);
|
|
||||||
}
|
|
||||||
|
|
||||||
ArrayFree(stack);
|
|
||||||
|
|
||||||
ret = EXIT_SUCCESS;
|
|
||||||
|
|
||||||
finish:
|
|
||||||
for (i = 0; i < ArraySize(eval); i++)
|
|
||||||
{
|
|
||||||
Free(ArrayGet(eval, i));
|
|
||||||
}
|
|
||||||
ArrayFree(eval);
|
|
||||||
|
|
||||||
if (in != StreamStdin())
|
|
||||||
{
|
|
||||||
StreamClose(in);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (out != StreamStdout())
|
|
||||||
{
|
|
||||||
StreamClose(out);
|
|
||||||
}
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
|
|
@ -1,119 +0,0 @@
|
||||||
#include <UInt64.h>
|
|
||||||
|
|
||||||
#include <Log.h>
|
|
||||||
|
|
||||||
/* AssertEquals(actual, expected) */
|
|
||||||
int
|
|
||||||
AssertEquals(char *msg, UInt64 x, UInt64 y)
|
|
||||||
{
|
|
||||||
if (!UInt64Eq(x, y))
|
|
||||||
{
|
|
||||||
Log(LOG_ERR, "%s: Expected 0x%X 0x%X, got 0x%X 0x%X", msg,
|
|
||||||
UInt64High(y), UInt64Low(y),
|
|
||||||
UInt64High(x), UInt64Low(x));
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
|
||||||
Main(void)
|
|
||||||
{
|
|
||||||
UInt64 x, y;
|
|
||||||
|
|
||||||
Log(LOG_INFO, "sizeof(UInt64) = %lu", sizeof(UInt64));
|
|
||||||
|
|
||||||
#ifdef UINT64_NATIVE
|
|
||||||
Log(LOG_INFO, "Using native 64-bit integers.");
|
|
||||||
#else
|
|
||||||
Log(LOG_INFO, "Using emulated 64-bit integers.");
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/* BSR Tests */
|
|
||||||
|
|
||||||
x = UInt64Create(0x000000FF, 0x00000000);
|
|
||||||
|
|
||||||
y = UInt64Srl(x, 4);
|
|
||||||
AssertEquals("x >> 4", y, UInt64Create(0x0000000F, 0xF0000000));
|
|
||||||
|
|
||||||
y = UInt64Srl(x, 8);
|
|
||||||
AssertEquals("x >> 8", y, UInt64Create(0x00000000, 0xFF000000));
|
|
||||||
|
|
||||||
y = UInt64Srl(x, 36);
|
|
||||||
AssertEquals("x >> 36", y, UInt64Create(0x00000000, 0x0000000F));
|
|
||||||
|
|
||||||
/* BSL Tests */
|
|
||||||
|
|
||||||
x = UInt64Create(0x00000000, 0xFF000000);
|
|
||||||
|
|
||||||
y = UInt64Sll(x, 4);
|
|
||||||
AssertEquals("x << 4", y, UInt64Create(0x0000000F, 0xF0000000));
|
|
||||||
|
|
||||||
y = UInt64Sll(x, 8);
|
|
||||||
AssertEquals("x << 8", y, UInt64Create(0x000000FF, 0x00000000));
|
|
||||||
|
|
||||||
y = UInt64Sll(x, 36);
|
|
||||||
AssertEquals("x << 36", y, UInt64Create(0xF0000000, 0x00000000));
|
|
||||||
|
|
||||||
/* ADD Tests */
|
|
||||||
|
|
||||||
x = UInt64Create(0x00000000, 0xF0000001);
|
|
||||||
y = UInt64Create(0x00000000, 0x00000002);
|
|
||||||
AssertEquals("0xF0000001 + 0x00000002", UInt64Add(x, y), UInt64Create(0x00000000, 0xF0000003));
|
|
||||||
|
|
||||||
x = UInt64Create(0x00000000, 0xF0000000);
|
|
||||||
y = UInt64Create(0x00000000, 0x10000000);
|
|
||||||
AssertEquals("0xF0000000 + 0x10000000", UInt64Add(x, y), UInt64Create(0x00000001, 0x00000000));
|
|
||||||
|
|
||||||
/* SUB Tests */
|
|
||||||
x = UInt64Create(0x00000000, 0x00000005);
|
|
||||||
y = UInt64Create(0x00000000, 0x00000002);
|
|
||||||
AssertEquals("0x00000005 - 0x00000002", UInt64Sub(x, y), UInt64Create(0x00000000, 0x00000003));
|
|
||||||
|
|
||||||
x = UInt64Create(0x00000001, 0x00000000);
|
|
||||||
y = UInt64Create(0x00000000, 0x00000001);
|
|
||||||
AssertEquals("0x00000001 0x00000000 - 0x00000001", UInt64Sub(x, y), UInt64Create(0x00000000, 0xFFFFFFFF));
|
|
||||||
|
|
||||||
/* MUL Tests */
|
|
||||||
x = UInt64Create(0, 18);
|
|
||||||
y = UInt64Create(0, 1);
|
|
||||||
AssertEquals("18 * 1", UInt64Mul(x, y), UInt64Create(0, 18));
|
|
||||||
|
|
||||||
x = UInt64Create(0, 20);
|
|
||||||
y = UInt64Create(0, 12);
|
|
||||||
AssertEquals("20 * 12", UInt64Mul(x, y), UInt64Create(0, 240));
|
|
||||||
|
|
||||||
x = UInt64Create(0x00000000, 0x00000005);
|
|
||||||
y = UInt64Create(0x00000000, 0x00000005);
|
|
||||||
AssertEquals("0x00000005 * 0x00000005", UInt64Mul(x, y), UInt64Create(0x00000000, 0x00000019));
|
|
||||||
|
|
||||||
x = UInt64Create(0x00000001, 0x00000000);
|
|
||||||
y = UInt64Create(0x00000000, 0x00000005);
|
|
||||||
AssertEquals("0x00000001 0x00000000 * 0x00000005", UInt64Mul(x, y), UInt64Create(0x00000005, 0x00000000));
|
|
||||||
|
|
||||||
/* DIV Tests */
|
|
||||||
x = UInt64Create(0, 12);
|
|
||||||
y = UInt64Create(0, 4);
|
|
||||||
AssertEquals("12 / 4", UInt64Div(x, y), UInt64Create(0, 3));
|
|
||||||
|
|
||||||
/* MOD Tests */
|
|
||||||
x = UInt64Create(0x000000FF, 0x00000000);
|
|
||||||
y = UInt64Create(0x00000000, 0x00000010);
|
|
||||||
AssertEquals("0x000000FF 0x00000000 mod 0x00000010", UInt64Rem(x, y), UInt64Create(0, 0));
|
|
||||||
|
|
||||||
x = UInt64Create(0x00000000, 0xFF000000);
|
|
||||||
y = UInt64Create(0x00000000, 0x00000010);
|
|
||||||
AssertEquals("0x00000000 0xFF000000 mod 0x00000010", UInt64Rem(x, y), UInt64Create(0, 0));
|
|
||||||
|
|
||||||
x = UInt64Create(0xFF000000, 0x00000000);
|
|
||||||
y = UInt64Create(0x00000000, 0x00000010);
|
|
||||||
AssertEquals("0xFF000000 0x00000000 mod 0x00000010", UInt64Rem(x, y), UInt64Create(0, 0));
|
|
||||||
|
|
||||||
x = UInt64Create(0x00000000, 0x000000F0);
|
|
||||||
y = UInt64Create(0x00000000, 0x00000010);
|
|
||||||
AssertEquals("0x00000000 0x000000F0 mod 0x00000010", UInt64Rem(x, y), UInt64Create(0, 0));
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (C) 2022-2023 Jordan Bancino <@jordan:bancino.net>
|
* Copyright (C) 2022 Jordan Bancino <@jordan:bancino.net>
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person
|
* Permission is hereby granted, free of charge, to any person
|
||||||
* obtaining a copy of this software and associated documentation files
|
* obtaining a copy of this software and associated documentation files
|
||||||
|
|
137
README.md
137
README.md
|
@ -1,136 +1,7 @@
|
||||||
<p align="center"><img src="https://telodendria.io/assets/Telodendria-500x500.png"></p>
|
# [Telodendria](https://telodendria.io)
|
||||||
<h1 align="center">Telodendria</h1>
|
|
||||||
|
|
||||||
Telodendria is an extremely powerful, yet lightweight and portable
|
**Telodendria** is an open source Matrix homeserver implementation written from scratch in ANSI C and designed to be lightweight and simple, yet functional.
|
||||||
chat server designed to be easy to install and configure. Powered by
|
|
||||||
the [Matrix](https://matrix.org) protocol, Telodendria empowers
|
|
||||||
everyone to run their own chat server on ordinary hardware, including
|
|
||||||
old and embedded devices. Whether you want a simple chat server just
|
|
||||||
for you and your friends and family, or want to talk to users on other
|
|
||||||
Matrix homeservers but don't want to go through all the hastle of
|
|
||||||
hosting a complicated, high-maintenance homeserver or joining an
|
|
||||||
existing homeserver for privacy or other reasons, then Telodendria
|
|
||||||
might be for you.
|
|
||||||
|
|
||||||
> **Note:** Telodendria still in development. See [Status](#status).
|
**Important:** This project is not developed on GitHub, or even with Git. As such, GitHub Pull Requests are not accepted. But that doesn't mean we don't want your contribution! You're more than welcome to clone this repo and use Git to make changes to the project if you'd prefer it to CVS, but when it comes time to actually submit your changes to this project, use [git format-patch](https://git-scm.com/docs/git-format-patch) to generate patch files, then submit them to the official Matrix room: [#telodendria-patches:bancino.net](https://matrix.to/#/#telodendria-patches:bancino.net).
|
||||||
|
|
||||||
## What is Matrix?
|
|
||||||
|
|
||||||
Matrix is an **open standard** for *interoperable*, *decentralized*,
|
|
||||||
*secure*, and *real-time* communication over the internet.
|
|
||||||
|
|
||||||
Matrix can be thought of as the successor to email, but it works
|
|
||||||
very similar to iMessage, Discord, or direct messaging on most
|
|
||||||
social media networks. The primary difference between Matrix and these
|
|
||||||
other services, however, is that Matrix doesn't depend on one central
|
|
||||||
authority, and is designed in such a way to respect your privacy.
|
|
||||||
Matrix has proven itself over the last few
|
|
||||||
years to be a reliable communication tool, and has only gotten more
|
|
||||||
user-friendly over the course of its development. Matrix is capable
|
|
||||||
enough that it can—and should—totally replace any other
|
|
||||||
means of digital communication, and it offers a much higher degree
|
|
||||||
of security, simplicity, and functionality.
|
|
||||||
|
|
||||||
Strictly speaking, Matrix itself is just the *protocol* by which
|
|
||||||
clients and servers communicate. In order to use Matrix, we need
|
|
||||||
implementations of both clients and servers. Telodendria is a server
|
|
||||||
implementation of the Matrix protocol.
|
|
||||||
|
|
||||||
## Why Telodendria?
|
|
||||||
|
|
||||||
- **Lightweight:** Written in the C programming language, Telodendria
|
|
||||||
is automatically lighter and faster than other self-hosted chat servers.
|
|
||||||
It has very few external dependencies and is as self-contained as
|
|
||||||
possible.
|
|
||||||
- **Fully-Featured:** Most lightweight chat solutions compromise on
|
|
||||||
features. Telodendria is built on the fully-featured Matrix protocol,
|
|
||||||
which provides a chat experience that most normal users are familiar
|
|
||||||
with.
|
|
||||||
- **Portable:** You can run Telodendria on just about everything,
|
|
||||||
including more traditional options like a personal home server or VPS,
|
|
||||||
but also more obscure platforms like Raspberry Pis or retro computers.
|
|
||||||
Telodendria can run on a broad number of operating systems, which means
|
|
||||||
that no matter which platform and OS you prefer, there is a good chance
|
|
||||||
you can add Telodendria without much difficulty. It is also extremely
|
|
||||||
easy to migrate a Telodendria instance between platforms; just copy the
|
|
||||||
data directory to a new device.
|
|
||||||
- **Simple:** Telodendria is designed to be a simple, no-frills
|
|
||||||
chat server. It is easy to install, easy to configure, and easy to
|
|
||||||
maintain.
|
|
||||||
- **Stable:** Other Matrix homeservers develop at the pace of the
|
|
||||||
Matrix specification itself, which is to say quite rapidly. Changes are
|
|
||||||
always being made, and a version shipped 6 months ago is already
|
|
||||||
incredibly outdated. Telodendria, on the other hand, aims to be stable.
|
|
||||||
It should *just work* for long periods of time between upgrades, and
|
|
||||||
you should never feel like Telodendria is going to change significantly
|
|
||||||
between upgrades.
|
|
||||||
|
|
||||||
[Read Technical Rationale →](docs/dev/rationale.md)
|
|
||||||
|
|
||||||
## Get Started
|
|
||||||
|
|
||||||
Check out the [Documentation](docs/README.md) to get started with
|
|
||||||
Telodendria.
|
|
||||||
|
|
||||||
## Status
|
|
||||||
|
|
||||||
Telodendria is in the very early stages of development. As such, it may
|
|
||||||
not yet deliver on all of its promises. Currently, Telodendria is not
|
|
||||||
ready for end-users yet. While it features very basic user
|
|
||||||
authentication, it does not actually work as a chat server yet.
|
|
||||||
|
|
||||||
We are hoping to ship Telodendria `v0.4.0` by May of 2024. This
|
|
||||||
release should be usable for communication between **local users**
|
|
||||||
only. Additional features, including federation with other Matrix
|
|
||||||
homeservers will be added in future releases.
|
|
||||||
|
|
||||||
You can help speed up development by [sponsoring](#sponsorship)
|
|
||||||
Telodendria or [getting involved](docs/CONTRIBUTING.md).
|
|
||||||
|
|
||||||
## Sponsorship
|
|
||||||
|
|
||||||
Telodendria is maintained by a loosely-knit band of volunteers. The
|
|
||||||
project currently has no sponsors and thus no source of income to
|
|
||||||
pay for infrastructure costs and developer time. To ensure
|
|
||||||
Telodendria's long-term success, please consider sponsoring the
|
|
||||||
project.
|
|
||||||
|
|
||||||
You can make a recurring donation to Telodendria using
|
|
||||||
[LiberaPay](https://bancino.net/Telodendria/donate). You can also make
|
|
||||||
one-time donations using
|
|
||||||
[Stripe](https://donate.stripe.com/8wM29AfF5bRJc48eUU). If you would
|
|
||||||
like to make a recurring donation larger than that allowed by
|
|
||||||
LiberaPay, please contact Jordan Bancino over Matrix at
|
|
||||||
`@jordan:bancino.net` or email at `jordan@bancino.net`.
|
|
||||||
|
|
||||||
### Benefits
|
|
||||||
|
|
||||||
While there are no set sponsorship tiers at this time, sponsoring
|
|
||||||
Telodendria is a mutually beneficial relationship. Depending on the
|
|
||||||
amount you donate, you can get your name, logo, and website links
|
|
||||||
on the [Sponsors](docs/SPONSORS.md) page, the project `README`, or the
|
|
||||||
main website.
|
|
||||||
|
|
||||||
## License
|
|
||||||
|
|
||||||
All of the code and documentation for Telodendria is licensed under a
|
|
||||||
modified MIT license. The MIT license is an extremely permissive
|
|
||||||
license that has very few restrictions. Please consult the
|
|
||||||
[`LICENSE.txt`](LICENSE.txt) file for the actual license text. It is
|
|
||||||
important to note that the Telodendria license text differs from the
|
|
||||||
original MIT license in the following ways:
|
|
||||||
|
|
||||||
- Where the MIT license states that the copyright notice and permission
|
|
||||||
notice shall be included in all copies or *substantial* portions of the
|
|
||||||
software, the Telodendria requires the copyright notice and
|
|
||||||
permission notice be included with *all* portions, regardless of the
|
|
||||||
size, by omitting the word *substantial*.
|
|
||||||
|
|
||||||
The Telodendria logo in all its forms, including the ASCII
|
|
||||||
representation, belongs solely to the Telodendria project. It must be
|
|
||||||
used only to represent the official Telodendria project. You are free
|
|
||||||
to use the logo in any way as long as it represents or links to the
|
|
||||||
official project. If Telodendria is forked, the logo must be removed
|
|
||||||
completely from the project, and optionally replaced by a different
|
|
||||||
one.
|
|
||||||
|
|
||||||
|
Please see the `README.txt` file for the actual project `README`, which simply details the repository structure a little bit. All of **Telodendria**'s user and developer documentation is available as `man` pages, or online.
|
||||||
|
|
27
README.txt
Normal file
27
README.txt
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
=======================================================
|
||||||
|
|_ _|__| | ___ __| | ___ _ __ __| |_ __(_) __ _
|
||||||
|
| |/ _ \ |/ _ \ / _` |/ _ \ '_ \ / _` | '__| |/ _` |
|
||||||
|
| | __/ | (_) | (_| | __/ | | | (_| | | | | (_| |
|
||||||
|
|_|\___|_|\___/ \__,_|\___|_| |_|\__,_|_| |_|\__,_|
|
||||||
|
=======================================================
|
||||||
|
Copyright (C) 2022 Jordan Bancino <@jordan:bancino.net>
|
||||||
|
|
||||||
|
This is the source code for Telodendria, a Matrix homeserver written
|
||||||
|
in C. All of the documentation is available as man pages in the
|
||||||
|
man/ directory, or online at https://telodendria.io
|
||||||
|
|
||||||
|
If information is missing from the documentation, please feel free
|
||||||
|
to reach out to #telodendria-general:bancino.net on Matrix.
|
||||||
|
|
||||||
|
This file documents the directory structure of the source code
|
||||||
|
repository.
|
||||||
|
|
||||||
|
Telodendria/
|
||||||
|
contrib/ - Supplemental files, such as example configs.
|
||||||
|
man/ - The official documentation as man pages.
|
||||||
|
site/ - The official website.
|
||||||
|
src/ - The C source code for Telodendria.
|
||||||
|
include/ - Header files for the source code.
|
||||||
|
Routes/ - Where Matrix API endpoints are implemented
|
||||||
|
tests/ - Unit and integration tests will eventually go here.
|
||||||
|
tools/ - Development environment and tools.
|
|
@ -1,58 +0,0 @@
|
||||||
{
|
|
||||||
"guard": "TELODENDRIA_SCHEMA_CLIENTEVENT_H",
|
|
||||||
"header": "Schema/ClientEvent.h",
|
|
||||||
"types": {
|
|
||||||
"ClientEventUnsignedData": {
|
|
||||||
"type": "struct",
|
|
||||||
"fields": {
|
|
||||||
"age": {
|
|
||||||
"type": "integer"
|
|
||||||
},
|
|
||||||
"prev_content": {
|
|
||||||
"type": "object"
|
|
||||||
},
|
|
||||||
"redacted_because": {
|
|
||||||
"type": "object"
|
|
||||||
},
|
|
||||||
"transaction_id": {
|
|
||||||
"type": "string"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"ClientEvent": {
|
|
||||||
"type": "struct",
|
|
||||||
"fields": {
|
|
||||||
"content": {
|
|
||||||
"type": "object",
|
|
||||||
"required": true
|
|
||||||
},
|
|
||||||
"event_id": {
|
|
||||||
"type": "string",
|
|
||||||
"required": true
|
|
||||||
},
|
|
||||||
"origin_server_ts": {
|
|
||||||
"type": "integer",
|
|
||||||
"required": true
|
|
||||||
},
|
|
||||||
"room_id": {
|
|
||||||
"type": "string",
|
|
||||||
"required": true
|
|
||||||
},
|
|
||||||
"sender": {
|
|
||||||
"type": "string",
|
|
||||||
"required": true
|
|
||||||
},
|
|
||||||
"state_key": {
|
|
||||||
"type": "string"
|
|
||||||
},
|
|
||||||
"type": {
|
|
||||||
"type": "string",
|
|
||||||
"required": true
|
|
||||||
},
|
|
||||||
"_unsigned": {
|
|
||||||
"type": "ClientEventUnsignedData"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,121 +0,0 @@
|
||||||
{
|
|
||||||
"header": "Schema\/Filter.h",
|
|
||||||
"types": {
|
|
||||||
"FilterRoom": {
|
|
||||||
"fields": {
|
|
||||||
"not_rooms": {
|
|
||||||
"type": "[string]"
|
|
||||||
},
|
|
||||||
"state": {
|
|
||||||
"type": "FilterRoomEvent"
|
|
||||||
},
|
|
||||||
"include_leave": {
|
|
||||||
"type": "boolean"
|
|
||||||
},
|
|
||||||
"timeline": {
|
|
||||||
"type": "FilterRoomEvent"
|
|
||||||
},
|
|
||||||
"account_data": {
|
|
||||||
"type": "FilterRoomEvent"
|
|
||||||
},
|
|
||||||
"rooms": {
|
|
||||||
"type": "[string]"
|
|
||||||
},
|
|
||||||
"ephemeral": {
|
|
||||||
"type": "FilterRoomEvent"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"type": "struct"
|
|
||||||
},
|
|
||||||
"FilterEventFormat": {
|
|
||||||
"fields": {
|
|
||||||
"federation": {
|
|
||||||
"name": "FILTER_FORMAT_FEDERATION"
|
|
||||||
},
|
|
||||||
"client": {
|
|
||||||
"name": "FILTER_FORMANT_CLIENT"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"type": "enum"
|
|
||||||
},
|
|
||||||
"FilterEvent": {
|
|
||||||
"fields": {
|
|
||||||
"not_senders": {
|
|
||||||
"type": "[string]"
|
|
||||||
},
|
|
||||||
"limit": {
|
|
||||||
"type": "integer"
|
|
||||||
},
|
|
||||||
"senders": {
|
|
||||||
"type": "[string]"
|
|
||||||
},
|
|
||||||
"types": {
|
|
||||||
"type": "[string]"
|
|
||||||
},
|
|
||||||
"not_types": {
|
|
||||||
"type": "[string]"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"type": "struct"
|
|
||||||
},
|
|
||||||
"Filter": {
|
|
||||||
"fields": {
|
|
||||||
"event_format": {
|
|
||||||
"type": "FilterEventFormat"
|
|
||||||
},
|
|
||||||
"presence": {
|
|
||||||
"type": "FilterEvent"
|
|
||||||
},
|
|
||||||
"account_data": {
|
|
||||||
"type": "FilterEvent"
|
|
||||||
},
|
|
||||||
"room": {
|
|
||||||
"type": "FilterRoom"
|
|
||||||
},
|
|
||||||
"event_fields": {
|
|
||||||
"type": "[string]"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"type": "struct"
|
|
||||||
},
|
|
||||||
"FilterRoomEvent": {
|
|
||||||
"fields": {
|
|
||||||
"not_rooms": {
|
|
||||||
"type": "[string]"
|
|
||||||
},
|
|
||||||
"not_senders": {
|
|
||||||
"type": "[string]"
|
|
||||||
},
|
|
||||||
"limit": {
|
|
||||||
"type": "integer"
|
|
||||||
},
|
|
||||||
"senders": {
|
|
||||||
"type": "[string]"
|
|
||||||
},
|
|
||||||
"include_redundant_members": {
|
|
||||||
"type": "boolean"
|
|
||||||
},
|
|
||||||
"types": {
|
|
||||||
"type": "[string]"
|
|
||||||
},
|
|
||||||
"rooms": {
|
|
||||||
"type": "[string]"
|
|
||||||
},
|
|
||||||
"lazy_load_members": {
|
|
||||||
"type": "boolean"
|
|
||||||
},
|
|
||||||
"not_types": {
|
|
||||||
"type": "[string]"
|
|
||||||
},
|
|
||||||
"contains_url": {
|
|
||||||
"type": "boolean"
|
|
||||||
},
|
|
||||||
"unread_thread_notifications": {
|
|
||||||
"type": "boolean"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"type": "struct"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"guard": "TELODENDRIA_SCHEMA_FILTER_H"
|
|
||||||
}
|
|
|
@ -1,81 +0,0 @@
|
||||||
{
|
|
||||||
"guard": "TELODENDRIA_SCHEMA_PDUV1_H",
|
|
||||||
"header": "Schema/PduV1.h",
|
|
||||||
"types": {
|
|
||||||
"PduV1EventHash": {
|
|
||||||
"type": "struct",
|
|
||||||
"fields": {
|
|
||||||
"sha256": {
|
|
||||||
"type": "string",
|
|
||||||
"required": true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"PduV1UnsignedData": {
|
|
||||||
"type": "struct",
|
|
||||||
"fields": {
|
|
||||||
"age": {
|
|
||||||
"type": "integer"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"PduV1": {
|
|
||||||
"type": "struct",
|
|
||||||
"fields": {
|
|
||||||
"auth_events": {
|
|
||||||
"type": "array",
|
|
||||||
"required": true
|
|
||||||
},
|
|
||||||
"content": {
|
|
||||||
"type": "object",
|
|
||||||
"required": true
|
|
||||||
},
|
|
||||||
"depth": {
|
|
||||||
"type": "integer",
|
|
||||||
"required": true
|
|
||||||
},
|
|
||||||
"event_id": {
|
|
||||||
"type": "string",
|
|
||||||
"required": true
|
|
||||||
},
|
|
||||||
"hashes": {
|
|
||||||
"type": "PduV1EventHash",
|
|
||||||
"required": true
|
|
||||||
},
|
|
||||||
"origin_server_ts": {
|
|
||||||
"type": "integer",
|
|
||||||
"required": true
|
|
||||||
},
|
|
||||||
"prev_events": {
|
|
||||||
"type": "array",
|
|
||||||
"required": true
|
|
||||||
},
|
|
||||||
"redacts": {
|
|
||||||
"type": "string"
|
|
||||||
},
|
|
||||||
"room_id": {
|
|
||||||
"type": "string",
|
|
||||||
"required": true
|
|
||||||
},
|
|
||||||
"sender": {
|
|
||||||
"type": "string",
|
|
||||||
"required": true
|
|
||||||
},
|
|
||||||
"signatures": {
|
|
||||||
"type": "object",
|
|
||||||
"required": true
|
|
||||||
},
|
|
||||||
"state_key": {
|
|
||||||
"type": "string"
|
|
||||||
},
|
|
||||||
"type": {
|
|
||||||
"type": "string",
|
|
||||||
"required": true
|
|
||||||
},
|
|
||||||
"_unsigned": {
|
|
||||||
"type": "PduV1UnsignedData"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,77 +0,0 @@
|
||||||
{
|
|
||||||
"guard": "TELODENDRIA_SCHEMA_PDUV3_H",
|
|
||||||
"header": "Schema/PduV3.h",
|
|
||||||
"types": {
|
|
||||||
"PduV3EventHash": {
|
|
||||||
"type": "struct",
|
|
||||||
"fields": {
|
|
||||||
"sha256": {
|
|
||||||
"type": "string",
|
|
||||||
"required": true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"PduV3UnsignedData": {
|
|
||||||
"type": "struct",
|
|
||||||
"fields": {
|
|
||||||
"age": {
|
|
||||||
"type": "integer"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"PduV3": {
|
|
||||||
"type": "struct",
|
|
||||||
"fields": {
|
|
||||||
"auth_events": {
|
|
||||||
"type": "[string]",
|
|
||||||
"required": true
|
|
||||||
},
|
|
||||||
"content": {
|
|
||||||
"type": "object",
|
|
||||||
"required": true
|
|
||||||
},
|
|
||||||
"depth": {
|
|
||||||
"type": "integer",
|
|
||||||
"required": true
|
|
||||||
},
|
|
||||||
"hashes": {
|
|
||||||
"type": "PduV3EventHash",
|
|
||||||
"required": true
|
|
||||||
},
|
|
||||||
"origin_server_ts": {
|
|
||||||
"type": "integer",
|
|
||||||
"required": true
|
|
||||||
},
|
|
||||||
"prev_events": {
|
|
||||||
"type": "[string]",
|
|
||||||
"required": true
|
|
||||||
},
|
|
||||||
"redacts": {
|
|
||||||
"type": "string"
|
|
||||||
},
|
|
||||||
"room_id": {
|
|
||||||
"type": "string",
|
|
||||||
"required": true
|
|
||||||
},
|
|
||||||
"sender": {
|
|
||||||
"type": "string",
|
|
||||||
"required": true
|
|
||||||
},
|
|
||||||
"signatures": {
|
|
||||||
"type": "object",
|
|
||||||
"required": true
|
|
||||||
},
|
|
||||||
"state_key": {
|
|
||||||
"type": "string"
|
|
||||||
},
|
|
||||||
"type": {
|
|
||||||
"type": "string",
|
|
||||||
"required": true
|
|
||||||
},
|
|
||||||
"_unsigned": {
|
|
||||||
"type": "PduV3UnsignedData"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,120 +0,0 @@
|
||||||
{
|
|
||||||
"guard": "TELODENDRIA_SCHEMA_ROOMCREATE_H",
|
|
||||||
"header": "Schema/RoomCreateRequest.h",
|
|
||||||
"types": {
|
|
||||||
"RoomVisibility": {
|
|
||||||
"fields": {
|
|
||||||
"public": {
|
|
||||||
"name": "ROOM_PUBLIC"
|
|
||||||
},
|
|
||||||
"private": {
|
|
||||||
"name": "ROOM_PRIVATE"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"type": "enum"
|
|
||||||
},
|
|
||||||
"RoomCreateRequest": {
|
|
||||||
"fields": {
|
|
||||||
"invite": {
|
|
||||||
"type": "[string]"
|
|
||||||
},
|
|
||||||
"room_version": {
|
|
||||||
"type": "string"
|
|
||||||
},
|
|
||||||
"invite_3pid": {
|
|
||||||
"type": "[RoomInvite3Pid]"
|
|
||||||
},
|
|
||||||
"topic": {
|
|
||||||
"type": "string"
|
|
||||||
},
|
|
||||||
"visibility": {
|
|
||||||
"type": "RoomVisibility"
|
|
||||||
},
|
|
||||||
"creation_content": {
|
|
||||||
"type": "object"
|
|
||||||
},
|
|
||||||
"is_direct": {
|
|
||||||
"type": "boolean"
|
|
||||||
},
|
|
||||||
"name": {
|
|
||||||
"type": "string"
|
|
||||||
},
|
|
||||||
"initial_state": {
|
|
||||||
"type": "[RoomStateEvent]"
|
|
||||||
},
|
|
||||||
"power_level_content_override": {
|
|
||||||
"type": "object"
|
|
||||||
},
|
|
||||||
"room_alias_name": {
|
|
||||||
"type": "string"
|
|
||||||
},
|
|
||||||
"preset": {
|
|
||||||
"type": "RoomCreatePreset"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"type": "struct"
|
|
||||||
},
|
|
||||||
"RoomInvite3Pid": {
|
|
||||||
"fields": {
|
|
||||||
"id_access_token": {
|
|
||||||
"required": true,
|
|
||||||
"type": "string"
|
|
||||||
},
|
|
||||||
"address": {
|
|
||||||
"required": true,
|
|
||||||
"type": "string"
|
|
||||||
},
|
|
||||||
"medium": {
|
|
||||||
"required": true,
|
|
||||||
"type": "Room3PidMedium"
|
|
||||||
},
|
|
||||||
"id_server": {
|
|
||||||
"required": true,
|
|
||||||
"type": "string"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"type": "struct"
|
|
||||||
},
|
|
||||||
"Room3PidMedium": {
|
|
||||||
"fields": {
|
|
||||||
"msisdn": {
|
|
||||||
"name": "ROOM_3PID_MSISDN"
|
|
||||||
},
|
|
||||||
"email": {
|
|
||||||
"name": "ROOM_3PID_EMAIL"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"type": "enum"
|
|
||||||
},
|
|
||||||
"RoomCreatePreset": {
|
|
||||||
"fields": {
|
|
||||||
"public_chat": {
|
|
||||||
"name": "ROOM_CREATE_PUBLIC"
|
|
||||||
},
|
|
||||||
"trusted_private_chat": {
|
|
||||||
"name": "ROOM_CREATE_TRUSTED"
|
|
||||||
},
|
|
||||||
"private_chat": {
|
|
||||||
"name": "ROOM_CREATE_PRIVATE"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"type": "enum"
|
|
||||||
},
|
|
||||||
"RoomStateEvent": {
|
|
||||||
"fields": {
|
|
||||||
"content": {
|
|
||||||
"required": true,
|
|
||||||
"type": "object"
|
|
||||||
},
|
|
||||||
"state_key": {
|
|
||||||
"type": "string"
|
|
||||||
},
|
|
||||||
"type": {
|
|
||||||
"required": true,
|
|
||||||
"type": "string"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"type": "struct"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
141
TODO.txt
Normal file
141
TODO.txt
Normal file
|
@ -0,0 +1,141 @@
|
||||||
|
Telodendria To-Do List
|
||||||
|
|
||||||
|
Key:
|
||||||
|
|
||||||
|
[ ] Not Started
|
||||||
|
[x] Done
|
||||||
|
[~] In Progress
|
||||||
|
[!] Won't Fix
|
||||||
|
|
||||||
|
Phase 1: Getting off the ground
|
||||||
|
|
||||||
|
[x] Name this project
|
||||||
|
[x] Set up a CVS repository
|
||||||
|
[x] Make CVS repository public
|
||||||
|
[x] Write a build script
|
||||||
|
[x] Write a coding style guide
|
||||||
|
[x] Add a license
|
||||||
|
[x] Add support and issue reporting guide
|
||||||
|
[x] Add table of contents to website
|
||||||
|
|
||||||
|
Phase 2: Building a foundation
|
||||||
|
|
||||||
|
[x] Implement an array
|
||||||
|
[x] Implement a logging facility
|
||||||
|
[x] Implement a hash map
|
||||||
|
[x] Allow custom hash functions for each hash map
|
||||||
|
[x] Combine library code files
|
||||||
|
[x] Implement configuration file parsing using hash map
|
||||||
|
[x] Base64 encoding/decoding with padded/unpadded support
|
||||||
|
[x] Write a release script
|
||||||
|
[x] UTF-8 encoder
|
||||||
|
[x] Implement a JSON library using the hash map and array
|
||||||
|
[x] Basic encoding from HashMap/Array/strings, etc.
|
||||||
|
[x] Basic decoding to HashMap/Array/strings, etc.
|
||||||
|
[x] Proper string encoding
|
||||||
|
[x] Proper string decoding
|
||||||
|
[x] Canonical JSON
|
||||||
|
[x] Keys are sorted lexiconographically
|
||||||
|
[x] Floats are not allowed (ignore any float values)
|
||||||
|
[x] Encode as UTF-8 instead of using \u escapes
|
||||||
|
[x] Decode encoded strings to UTF-8
|
||||||
|
[x] Write a function that gets the current Unix timestamp in milliseconds
|
||||||
|
[x] Parse the Telodendria config file
|
||||||
|
[x] Add license header to all files
|
||||||
|
[!] Add documentation for all public APIs (See below)
|
||||||
|
[x] Implement a simple HTTP server
|
||||||
|
[x] Implement param parser
|
||||||
|
[x] URL encoder/decoder
|
||||||
|
[~] Design server architecture
|
||||||
|
[x] Route requests
|
||||||
|
[x] Handle requests
|
||||||
|
[~] Data abstraction layer
|
||||||
|
[ ] Database upgrades/migration path
|
||||||
|
[ ] Caching and cache control
|
||||||
|
[x] Make memory info access O(1)
|
||||||
|
[x] Error generation
|
||||||
|
[x] Properly implement the command line options as stated in telodendria(8)
|
||||||
|
[x] Remove "chroot" option, just chroot into the data dir, and make the log
|
||||||
|
file live there as well.
|
||||||
|
[x] Allow logging to the syslog
|
||||||
|
[x] Fix memory leaks
|
||||||
|
[ ] Fix bug where the socket stays open after quit.
|
||||||
|
[ ] Possibly related to not closing the connections with fclose()?
|
||||||
|
(see HttpServer.c, grep for fclose)
|
||||||
|
[ ] Figure out how to write unit tests for array/hashmap/etc
|
||||||
|
[ ] Add recipe to td script to upload patches to the Matrix room
|
||||||
|
|
||||||
|
Phase 3: Welcome to Matrix
|
||||||
|
|
||||||
|
[~] client-Server API
|
||||||
|
[x] Error responses
|
||||||
|
[x] CORS headers
|
||||||
|
[x] /_matrix/client/versions
|
||||||
|
[x] Well-known URIs
|
||||||
|
[x] Make base-url optional in config
|
||||||
|
[x] Make identity-server optional in config
|
||||||
|
[ ] Client authentication
|
||||||
|
[ ] Capabilities negotiation
|
||||||
|
[ ] Server-Server API
|
||||||
|
[ ] Application Service API
|
||||||
|
[ ] Identity Service API
|
||||||
|
[ ] Push Gateway API
|
||||||
|
[ ] Room Versions
|
||||||
|
|
||||||
|
Phase 4:
|
||||||
|
|
||||||
|
[ ] Create an OpenBSD package and get it submitted to ports
|
||||||
|
[ ] Add relayd examples to contrib/
|
||||||
|
[ ] Create a command line tool to manage Telodendria
|
||||||
|
[ ] User management
|
||||||
|
[ ] Room management
|
||||||
|
[ ] Migrate from Synapse or Dendrite, whichever is more mainstream by the time we get here
|
||||||
|
|
||||||
|
Documentation
|
||||||
|
|
||||||
|
[x] Update rationale section
|
||||||
|
[x] Update project description (no longer a CGI binary)
|
||||||
|
[x] Clean up dark mode colors (tables, background, code snippets)
|
||||||
|
[x] Add logo (possibly center title?)
|
||||||
|
[x] Update code style to not include indent or line rules, but use indent(1) instead
|
||||||
|
[x] fix typo "Subitting Patches" in Table of Contents
|
||||||
|
[x] Make a note in Getting the Code that the password for the anoncvs account is just anoncvs
|
||||||
|
[x] Add contributors list
|
||||||
|
[x] Add list of make.sh recipes and what they do
|
||||||
|
[x] Improve Google Lighthouse score on website
|
||||||
|
[!] Image elements do not have explicit width and height
|
||||||
|
[x] Background and foreground colors do not have sufficient contrast ratio (msg-error div)
|
||||||
|
[x] Lists do not contain only <li> elements
|
||||||
|
[!] Add other message divs for notes and warnings
|
||||||
|
|
||||||
|
[~] Convert documentation to man pages
|
||||||
|
[x] Clean up dark mode in man page CSS (links)
|
||||||
|
[x] Synopsis table should not be styled
|
||||||
|
[x] Make available on MANPATH in tools/env.sh
|
||||||
|
[x] Convert list of man pages into a table on home page
|
||||||
|
[~] Internal API docs
|
||||||
|
[x] Array
|
||||||
|
[x] Base64
|
||||||
|
[ ] CanonicalJson
|
||||||
|
[ ] Config
|
||||||
|
[ ] API (Config.3)
|
||||||
|
[ ] File format (Config.5)
|
||||||
|
[ ] Constants
|
||||||
|
[x] HashMap
|
||||||
|
[ ] Http
|
||||||
|
[ ] HttpServer
|
||||||
|
[ ] Json
|
||||||
|
[ ] Log
|
||||||
|
[ ] Matrix
|
||||||
|
[ ] NonPosix
|
||||||
|
[x] Queue
|
||||||
|
[ ] Routes
|
||||||
|
[ ] TelodendriaConfig
|
||||||
|
[ ] Util
|
||||||
|
[ ] Memory
|
||||||
|
[ ] Db
|
||||||
|
|
||||||
|
[x] Add onboarding documentation
|
||||||
|
[x] Building from source
|
||||||
|
[!] Writing config file (see Config.5)
|
||||||
|
|
62
Telodendria.css
Normal file
62
Telodendria.css
Normal file
|
@ -0,0 +1,62 @@
|
||||||
|
body {
|
||||||
|
margin: auto;
|
||||||
|
max-width: 8.5in;
|
||||||
|
padding: 0.25in;
|
||||||
|
}
|
||||||
|
.code {
|
||||||
|
background-color: #eee;
|
||||||
|
border-radius: 5px;
|
||||||
|
display: block;
|
||||||
|
padding: 0.5em 1em 1.5em 1em;
|
||||||
|
|
||||||
|
font-family: monospace;
|
||||||
|
white-space: pre;
|
||||||
|
overflow-x: scroll;
|
||||||
|
}
|
||||||
|
kbd {
|
||||||
|
background-color: #eee;
|
||||||
|
border-radius: 3px;
|
||||||
|
border: 1px solid #b4b4b4;
|
||||||
|
box-shadow: 0 1px 1px rgba(0, 0, 0, .2), 0 2px 0 0 rgba(255, 255, 255, .7) inset;
|
||||||
|
color: #333;
|
||||||
|
display: inline-block;
|
||||||
|
font-size: .85em;
|
||||||
|
font-weight: 700;
|
||||||
|
line-height: 1;
|
||||||
|
padding: 2px 4px;
|
||||||
|
white-space: nowrap;
|
||||||
|
}
|
||||||
|
h1, h4, h5, h6 {
|
||||||
|
border-bottom: 1px dashed gray;
|
||||||
|
}
|
||||||
|
h4, h5, h6 {
|
||||||
|
width: fit-content;
|
||||||
|
}
|
||||||
|
a {
|
||||||
|
color: #0969da;
|
||||||
|
text-decoration: none;
|
||||||
|
}
|
||||||
|
a:hover {
|
||||||
|
text-decoration: underline;
|
||||||
|
}
|
||||||
|
table {
|
||||||
|
width: 100%;
|
||||||
|
border-collapse: collapse;
|
||||||
|
}
|
||||||
|
td, th {
|
||||||
|
border: 1px solid #eee;
|
||||||
|
text-align: left;
|
||||||
|
padding: 8px;
|
||||||
|
}
|
||||||
|
tr:nth-child(even) {
|
||||||
|
background-color: #eee;
|
||||||
|
}
|
||||||
|
@media (prefers-color-scheme: dark) {
|
||||||
|
body {
|
||||||
|
background-color: #212121;
|
||||||
|
color: white;
|
||||||
|
}
|
||||||
|
.code, tr:nth-child(even) {
|
||||||
|
background-color: #333333;
|
||||||
|
}
|
||||||
|
}
|
541
Telodendria.html
Normal file
541
Telodendria.html
Normal file
|
@ -0,0 +1,541 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||||
|
|
||||||
|
<meta name="author" content="Jordan Bancino">
|
||||||
|
<meta name="description"
|
||||||
|
content="Telodendria, a Matrix homeserver written in ANSI C.">
|
||||||
|
|
||||||
|
<meta property="og:title"
|
||||||
|
content="Telodendria | A Matrix Homeserver written in ANSI C.">
|
||||||
|
<meta property="og:type" content="website">
|
||||||
|
<meta property="og:url"
|
||||||
|
content="https://bancino.net/pub/telodendria/telodendria.html">
|
||||||
|
<meta property="og:description"
|
||||||
|
content="Telodendria, a Matrix homeserver written in ANSI C.">
|
||||||
|
|
||||||
|
<link rel="stylesheet" href="Telodendria.css">
|
||||||
|
|
||||||
|
<title>Telodendria | A Matrix Homeserver written in ANSI C.</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1 id="telodendria">Telodendria</h1>
|
||||||
|
<p>
|
||||||
|
<b>Telodendria:</b> The terminal branches of an axon.
|
||||||
|
</p>
|
||||||
|
<p>
|
||||||
|
<b><i>Note:</i></b> <b>Telodendria</b> is under <i>heavy</i> development.
|
||||||
|
Please see the <a href="#project-status">Project Status</a>.
|
||||||
|
</p>
|
||||||
|
<p>
|
||||||
|
<b>Telodendria</b> is a Matrix homeserver implementation written from
|
||||||
|
scratch in ANSI C. It is designed to be lightweight and simple, yet
|
||||||
|
functional. <b>Telodendria</b> differentiates itself from other Matrix
|
||||||
|
homeserver implementations because it:
|
||||||
|
<ul>
|
||||||
|
<li>
|
||||||
|
Is written in C, a stable, low-level programming language with a long
|
||||||
|
history, low build and runtime overhead, and wide compatibility.
|
||||||
|
</li>
|
||||||
|
<li>
|
||||||
|
Is written with minimalism as a primary design goal. Whenever possible
|
||||||
|
and practical, no third-party libraries are pulled in to the source
|
||||||
|
code. Everything <b>Telodendria</b> needs is custom written. As a
|
||||||
|
result, <b>Telodendria</b> depends only on a standard C compiler and
|
||||||
|
library to be built, and only a web server with CGI capabilities to
|
||||||
|
run.
|
||||||
|
</li>
|
||||||
|
<li>
|
||||||
|
Uses a flat-file directory structure to store data instead of a
|
||||||
|
database. This has a number of advantages:
|
||||||
|
<ul>
|
||||||
|
<li>It makes setup and maintenance much easier.</li>
|
||||||
|
<li>
|
||||||
|
It allows <b>Telodendria</b> to run on systems with fewer resources.
|
||||||
|
</li>
|
||||||
|
<li>
|
||||||
|
It provides both runtime and data safety and stability. Since no
|
||||||
|
database is running, there's fewer things that could go wrong because
|
||||||
|
there's a lot less code running on the system.
|
||||||
|
</li>
|
||||||
|
</ul>
|
||||||
|
</li>
|
||||||
|
<li>
|
||||||
|
Runs as a CGI application. <b>Telodendria</b> is delivered as a single
|
||||||
|
small, highly-optimized binary that can be dropped in a web server's
|
||||||
|
web root to be executed. This allows it to consume very few resources
|
||||||
|
and be very easy to set up.
|
||||||
|
</li>
|
||||||
|
</ul>
|
||||||
|
<p>
|
||||||
|
<b>Telodendria</b> is on Matrix! Check out the official Matrix rooms:
|
||||||
|
</p>
|
||||||
|
<table>
|
||||||
|
<tr>
|
||||||
|
<th>Room</th>
|
||||||
|
<th>Description</th>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td>
|
||||||
|
<code>#telodendria-releases:bancino.net</code>
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
Get notified of new releases.
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td>
|
||||||
|
<code>#telodendria-general:bancino.net</code>
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
General discussion and support for <b>Telodendria</b>.
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td>
|
||||||
|
<code>#telodendria-issues:bancino.net</code>
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
Report issues with <b>Telodendria</b>.
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td>
|
||||||
|
<code>#telodendria-patches:bancino.net</code>
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
Submit code patches to the <b>Telodendria</b> project.
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
</table>
|
||||||
|
<h2 id="table-of-contents">Table of Contents</h2>
|
||||||
|
<ul>
|
||||||
|
<li>
|
||||||
|
<a href="#telodendria">Telodendria</a>
|
||||||
|
<ul>
|
||||||
|
<li><a href="#table-of-contents">Table of Contents</a></li>
|
||||||
|
<li>
|
||||||
|
<a href="#getting-started">Getting Started</a>
|
||||||
|
<ul>
|
||||||
|
<li>
|
||||||
|
<a href="#install">Install Telodendria</a>
|
||||||
|
<ul>
|
||||||
|
<li><a href="#openbsd">OpenBSD</a></li>
|
||||||
|
<li><a href="#building-from-source">Building From Source</a></li>
|
||||||
|
</ul>
|
||||||
|
</li>
|
||||||
|
<li><a href="#configure">Configure Telodendria</a></li>
|
||||||
|
</ul>
|
||||||
|
</li>
|
||||||
|
<li>
|
||||||
|
<a href="#project-status">Project Status</a>
|
||||||
|
<ul>
|
||||||
|
<li><a href="#phase-1">Phase 1: Getting Off The Ground</a></li>
|
||||||
|
<li><a href="#phase-2">Phase 2: Building A Foundation</a></li>
|
||||||
|
<li><a href="#phase-3">Phase 3: Welcome To Matrix</a></li>
|
||||||
|
<li><a href="#phase-4">Phase 4: A Real Homeserver</a></li>
|
||||||
|
</ul>
|
||||||
|
</li>
|
||||||
|
<li><a href="#rationale">Rationale</a></li>
|
||||||
|
<li><a href="#project-goals">Project Goals</a></li>
|
||||||
|
<li><a href="#getting-support">Getting Support</a></li>
|
||||||
|
<li>
|
||||||
|
<li>
|
||||||
|
<a href="#documentation-status">Documentation Status</a>
|
||||||
|
</li>
|
||||||
|
<a href="#contributing">Contributing</a>
|
||||||
|
<ul>
|
||||||
|
<li><a href="#reporting-issues">Reporting Issues</a></li>
|
||||||
|
<li>
|
||||||
|
<a href="#Developing">Developing</a>
|
||||||
|
<ul>
|
||||||
|
<li><a href="#getting-the-code">Getting The Code</a></li>
|
||||||
|
<li><a href="#code-style">Code Style</a></li>
|
||||||
|
<li><a href="#submitting-patches">Subitting Patches</a></li>
|
||||||
|
</ul>
|
||||||
|
</li>
|
||||||
|
</ul>
|
||||||
|
</li>
|
||||||
|
<li><a href="#license">License</a></li>
|
||||||
|
<li><a href="#change-log">Change Log</a></li>
|
||||||
|
</ul>
|
||||||
|
</li>
|
||||||
|
</ul>
|
||||||
|
<h2 id="getting-started">Getting Started</h2>
|
||||||
|
<h3 id="install">Install Telodendria</h3>
|
||||||
|
<p>
|
||||||
|
If your operating system has an official package or port of
|
||||||
|
<b>Telodendria</b>, you should prefer to use that. If your operating
|
||||||
|
system's package or port is too out of date for your tastes, please
|
||||||
|
contact the package's maintainers to notify them, or offer to update
|
||||||
|
the package yourself.
|
||||||
|
</p>
|
||||||
|
<p>
|
||||||
|
If your operating system does not have an official package, see below
|
||||||
|
for instructions on building from source and use them to create one.
|
||||||
|
</p>
|
||||||
|
<h4 id="openbsd">OpenBSD</h4>
|
||||||
|
<p>
|
||||||
|
<b>Telodendria</b> is available in the ports tree and as a binary
|
||||||
|
package. You can install it with the following command:
|
||||||
|
</p>
|
||||||
|
<div class="code">
|
||||||
|
$ pkg_add telodendria
|
||||||
|
</div>
|
||||||
|
<h4 id="building-from-source">Building From Source</h4>
|
||||||
|
<p>
|
||||||
|
<b>Telodendria</b> is designed to be light enough that it can be built
|
||||||
|
from source on just about any operating system. It only has the
|
||||||
|
following requirements, all of which should be already available to
|
||||||
|
you on a sufficiently complete operating system:
|
||||||
|
</p>
|
||||||
|
<ul>
|
||||||
|
<li>
|
||||||
|
A standards-compliant C compiler with the C standard library. Because
|
||||||
|
<b>Telodendria</b> is written in ANSI C, it should compile on just about
|
||||||
|
any compiler, but the following compilers are known to work:
|
||||||
|
<ul>
|
||||||
|
<li>GCC</li>
|
||||||
|
<li>Clang</li>
|
||||||
|
<li>
|
||||||
|
Tiny C Compiler (<b>Note:</b> must edit <code>make.sh</code> and remove
|
||||||
|
<code>-Wl,-static -Wl,-gc-sections</code> from <code>LDFLAGS</code>)
|
||||||
|
</li>
|
||||||
|
</ul>
|
||||||
|
Other compilers should work as well, but you may have to play with the
|
||||||
|
flags in <code>make.sh</code>.
|
||||||
|
</li>
|
||||||
|
<li>
|
||||||
|
POSIX base utilities, including <code>find</code>, <code>stat</code>,
|
||||||
|
<code>env</code>, and compliant <code>sh</code>-like shell.
|
||||||
|
</li>
|
||||||
|
</ul>
|
||||||
|
<div class="code">
|
||||||
|
$ ./make.sh
|
||||||
|
</div>
|
||||||
|
<p>
|
||||||
|
If everything went well, that will produce
|
||||||
|
<code>telodendria.cgi</code>, which you can then place under your web
|
||||||
|
root and configure your web server to execute. You'll need to make sure
|
||||||
|
<code>/.well-known/matrix</code> and <code>/_matrix</code> and all the
|
||||||
|
paths under them actually execute <code>telodendria.cgi</code>. See the
|
||||||
|
provided OpenBSD <code>httpd.conf</code> for reference. Even if you
|
||||||
|
aren't using OpenBSD's <code>httpd(8)</code>, you should find its
|
||||||
|
configuration syntax simple enough to adequately demonstrate the proper
|
||||||
|
configuration.
|
||||||
|
</p>
|
||||||
|
<h3 id="configure">Configure Telodendria</h3>
|
||||||
|
<p>
|
||||||
|
Once you get <b>Telodendria</b> built and hooked into your web server,
|
||||||
|
you will have to write a configuration file for it. The configuration
|
||||||
|
file is just JSON, and it should be called
|
||||||
|
<code>Telodendria.json</code>.
|
||||||
|
</p>
|
||||||
|
<h2 id="project-status">Project Status</h2>
|
||||||
|
<p>
|
||||||
|
<b>Telodendria</b> is a very ambitious project. There's a lot that needs
|
||||||
|
to happen yet before it is even remotely usable. At the moment, there's
|
||||||
|
nothing that even remotely resembles a Matrix homeserver here; we're still
|
||||||
|
getting off the ground and building a foundation.
|
||||||
|
</p>
|
||||||
|
<p>
|
||||||
|
Just because there's nothing here yet doesn't mean you should go away
|
||||||
|
though! We desparately need help, so you are more than welcome to help
|
||||||
|
out if you want things to go quicker. Please see the
|
||||||
|
<a href="#contributing">Contributing</a> section for details on how you
|
||||||
|
can get involved.
|
||||||
|
</p>
|
||||||
|
<h3 id="phase-1">Phase 1: Getting Off The Ground</h3>
|
||||||
|
<ul>
|
||||||
|
<li><s>Name this project</s></li>
|
||||||
|
<li><s>Set up a CVS repository</s></li>
|
||||||
|
<li><s>Make CVS repository public</s></li>
|
||||||
|
<li><s>Write a coding style guide</s></li>
|
||||||
|
<li><s>Write a build script</s></li>
|
||||||
|
<li><s>Add a license</s></li>
|
||||||
|
<li><s>Add support and issue reporting guide</s></li>
|
||||||
|
<li><s>Add table of contents to this document</s></li>
|
||||||
|
</ul>
|
||||||
|
<h3 id="phase-2">Phase 2: Building A Foundation</h3>
|
||||||
|
<ul>
|
||||||
|
<li><s>Implement an array</s></li>
|
||||||
|
<li><s>Implement a logging facility</s></li>
|
||||||
|
<li><s>Implement argument parsing (<code>-c file -Vh</code>)</s></li>
|
||||||
|
<li><s>Implement a hash map</s></li>
|
||||||
|
<li><s>Combine library code files</s></li>
|
||||||
|
<li><s>Implement configuration file parsing using the hash map</s></li>
|
||||||
|
<li>Implement a JSON library using the hash map and array</li>
|
||||||
|
<li>Figure out how to write unit tests for array/hashmap/etc</li>
|
||||||
|
<li>Implement a simple HTTP server</li>
|
||||||
|
<li>
|
||||||
|
Design the server architecture
|
||||||
|
<ul>
|
||||||
|
<li>Route requests</li>
|
||||||
|
<li>Handle requests</li>
|
||||||
|
<li>Data abstraction layer</li>
|
||||||
|
<li>Error generation</li>
|
||||||
|
</ul>
|
||||||
|
</li>
|
||||||
|
</ul>
|
||||||
|
<h3 id="phase-3">Phase 3: Welcome To Matrix</h3>
|
||||||
|
<ul>
|
||||||
|
<li>
|
||||||
|
Implement the Client-Server API
|
||||||
|
</li>
|
||||||
|
<li>
|
||||||
|
Implement the Server-Server API
|
||||||
|
</li>
|
||||||
|
<li>
|
||||||
|
Implement the other Matrix APIs
|
||||||
|
</li>
|
||||||
|
</ul>
|
||||||
|
<h3 id="phase-4">Phase 4: A Real Homeserver</h3>
|
||||||
|
<ul>
|
||||||
|
<li>
|
||||||
|
Create an OpenBSD package and get it submitted to ports
|
||||||
|
</li>
|
||||||
|
<li>
|
||||||
|
Create a command line tool to manage Telodendria
|
||||||
|
<ul>
|
||||||
|
<li>Configuration file generation</li>
|
||||||
|
<li>User management</li>
|
||||||
|
<li>Room management</li>
|
||||||
|
</ul>
|
||||||
|
</li>
|
||||||
|
<li>
|
||||||
|
Migrate from Synapse. I run a Synapse homeserver right now, so somehow
|
||||||
|
I have to get all my data into the Telodendria format.
|
||||||
|
</li>
|
||||||
|
</ul>
|
||||||
|
<h2 id="documentation-status">Documentation Status</h2>
|
||||||
|
<p>
|
||||||
|
This documentation needs just a little work. Here's the things
|
||||||
|
on my list for that:
|
||||||
|
</p>
|
||||||
|
<ul>
|
||||||
|
<li>Update Rationale section</li>
|
||||||
|
<li>Update Project description (no longer a CGI binary)</li>
|
||||||
|
<li>Update project code requirements (ANSI C, POSIX.1c)</li>
|
||||||
|
</ul>
|
||||||
|
<h2 id="rationale">Rationale</h2>
|
||||||
|
<p>
|
||||||
|
This section explains
|
||||||
|
</p>
|
||||||
|
<p>
|
||||||
|
I want a lightweight Matrix homeserver designed for OpenBSD. I want a
|
||||||
|
homeserver that can be developed in <code>vi(1)</code> and compiled
|
||||||
|
with a C compiler. I want it to function entirely on a base OpenBSD
|
||||||
|
install without having to install any extra packages whatsoever. I've
|
||||||
|
found that the existing homeserver implementations are way
|
||||||
|
over-engineered and written in such a way that many programs and
|
||||||
|
libraries have to be pulled in to use them. I also want to learn how
|
||||||
|
Matrix works, and I want to understand the code I'm running on my
|
||||||
|
server.
|
||||||
|
</p>
|
||||||
|
<p>
|
||||||
|
So I wrote Telodendria.
|
||||||
|
</p>
|
||||||
|
<p>
|
||||||
|
Telodendria is written entirely in portable ANSI C. It depends on no
|
||||||
|
third-party C libraries other than the standard C library. The only
|
||||||
|
thing you need to run it is a web server that supports executing CGI
|
||||||
|
programs, and a directory that data can be written to. Everything
|
||||||
|
Telodendria needs to run itself is compiled into a single static
|
||||||
|
binary, and the source code can be built anywhere, right out of the
|
||||||
|
box.
|
||||||
|
</p>
|
||||||
|
<p>
|
||||||
|
Telodendria doesn't use a database like all the other homeservers.
|
||||||
|
Instead, it operates more like email: it uses a flat-file data
|
||||||
|
structure similar to maildir to store data. The advantage of this is
|
||||||
|
that it saves server maintainers from also having to maintain a
|
||||||
|
database. It greatly simplifies the process of getting a Matrix
|
||||||
|
homeserver up and running, and it makes it highly portable. It also is
|
||||||
|
extremely easy to back up and restore with base tools; just
|
||||||
|
<code>tar(1)</code> up the directory, and you're good to go.
|
||||||
|
</p>
|
||||||
|
<p>
|
||||||
|
Telodendria is developed and tested on OpenBSD, but you'll find that it
|
||||||
|
should run under any web server that supports CGI. I chose to write
|
||||||
|
Telodendria as a CGI program because anyone running an existing Matrix
|
||||||
|
server is likely running a web server acting as a reverse proxy in
|
||||||
|
front of it anyway, so why not just hook the homeserver directly into
|
||||||
|
the web server? That's one less daemon to run, which means memory and
|
||||||
|
CPU savings. CGI also allows Telodendria to remain single-threaded.
|
||||||
|
Each request that comes in is handled as its own process, and
|
||||||
|
operations are entirely isolated.
|
||||||
|
</p>
|
||||||
|
<h2 id="project-goals">Project Goals</h2>
|
||||||
|
<p>
|
||||||
|
The goals of this project are as follows:
|
||||||
|
</p>
|
||||||
|
<ul>
|
||||||
|
<li>
|
||||||
|
To be a production-ready Matrix server capable of handling a lot of
|
||||||
|
users. Telodendria should have good performance in many diverse
|
||||||
|
environments.
|
||||||
|
</li>
|
||||||
|
<li>
|
||||||
|
To have as few external build and run dependencies as possible. It
|
||||||
|
should be possible to compile Telodendria on any operating system out
|
||||||
|
of the box, and have it be totally statically linked, ready to run
|
||||||
|
under a <code>chroot(8)</code>-ed web server. You'll even notice that
|
||||||
|
the documentation is written in HTML directly, not Markdown, to remove
|
||||||
|
the dependency on a Markdown parser and renderer.
|
||||||
|
</li>
|
||||||
|
<li>
|
||||||
|
To be written in clean, elegant, and well-documented code. The goal is
|
||||||
|
to build a Matrix homeserver from the ground up, not just because I
|
||||||
|
don't the way existing homeservers are implemented, but also so I can
|
||||||
|
learn how Matrix really works, and maybe even teach others along the
|
||||||
|
way.
|
||||||
|
</li>
|
||||||
|
</ul>
|
||||||
|
<h2 id="getting-support">Getting Support</h2>
|
||||||
|
<p>
|
||||||
|
<b>Telodendria</b> is designed to be fairly straightforward, but that
|
||||||
|
doesn't mean there won't be hiccups along the way. If you are struggling
|
||||||
|
to get <b>Telodendria</b> up and running, you're more than welcome to
|
||||||
|
reach out for support. Just join the
|
||||||
|
<code>#telodendria-general:bancino.net</code> Matrix channel. Before
|
||||||
|
you do though, make sure you're running the latest version of
|
||||||
|
<b>Telodendria</b> and you've thoroughly read through all the
|
||||||
|
relevant documentation.
|
||||||
|
</p>
|
||||||
|
<h2 id="contributing">Contributing</h2>
|
||||||
|
<p>
|
||||||
|
<b>Telodendria</b> is an open source project. As such, it welcomes
|
||||||
|
contributions. There are many ways you can contribute, and any way you
|
||||||
|
can is greatly appreciated.
|
||||||
|
</p>
|
||||||
|
<h3 id="reporting-issues">Reporting Issues</h3>
|
||||||
|
<p>
|
||||||
|
If—after you've reached out to
|
||||||
|
<code>#telodendria-general:bancino.net</code>—it has been
|
||||||
|
determined that there is a problem with <b>Telodendria</b>, it should
|
||||||
|
be reported to <code>#telodendria-issues:bancino.net</code>. There it
|
||||||
|
can be discussed further. The issues channel serves as the official
|
||||||
|
issue tracker of <b>Telodendria</b>; although issues may be copied
|
||||||
|
into a <code>TODO</code> file in the CVS repository just so they
|
||||||
|
don't get lost.
|
||||||
|
</p>
|
||||||
|
<h3 id="developing">Developing</h3>
|
||||||
|
<p>
|
||||||
|
The primary language used to write <b>Telodendria</b> code is ANSI C.
|
||||||
|
Yes, that's the original C standard from 1989. The reason this standard
|
||||||
|
is chosen, and the reason that it will not be changed, is because the
|
||||||
|
original C is the most portable. Other languages you'll find in the
|
||||||
|
<b>Telodendria</b> repository are shell scripts and HTML. If you have
|
||||||
|
any experience at all with any of these languages, your contributions
|
||||||
|
are valuable. Please follow the guidelines in this section to ensure
|
||||||
|
the contribution workflow goes as smoothly as possible.
|
||||||
|
</p>
|
||||||
|
<h4 id="getting-the-code">Getting The Code</h4>
|
||||||
|
<p>
|
||||||
|
There are multiple ways to get the source code for <b>Telodendria</b>.
|
||||||
|
You can download an official release tarball from
|
||||||
|
<a href="https://bancino.net/pub/telodendria">here</a> if you would like,
|
||||||
|
but the preferred way is to check out the source code from CVS. This
|
||||||
|
makes generating patches a lot easier. If you do not have CVS, consult
|
||||||
|
your operating system's package repository to install it. CVS was the
|
||||||
|
chosen version control system for this project primarily because it is
|
||||||
|
built into OpenBSD.
|
||||||
|
</p>
|
||||||
|
<div class="code">
|
||||||
|
$ export CVSROOT=anoncvs@bancino.net:/cvs
|
||||||
|
$ cvs checkout Telodendria
|
||||||
|
$ cd Telodendria
|
||||||
|
</div>
|
||||||
|
<p>
|
||||||
|
You should now have the latest <b>Telodendria</b> source code. Follow
|
||||||
|
the <a href="#code-style">Code Style</a> as you make your changes.
|
||||||
|
</p>
|
||||||
|
<h4 id="code-style">Code Style</h4>
|
||||||
|
<p>
|
||||||
|
<b>Telodendria</b>'s code style is very unique. In general, these are
|
||||||
|
the conventions used by the code base.
|
||||||
|
</p>
|
||||||
|
<ul>
|
||||||
|
<li>
|
||||||
|
All function, enumeration, structure, and header names are
|
||||||
|
<code>CamelCase</code>. This is preferred to <code>snake_case</code>
|
||||||
|
because it is more compact.
|
||||||
|
</li>
|
||||||
|
<li>
|
||||||
|
<code>enum</code>s and <code>struct</code>s are always
|
||||||
|
<code>typedef</code>-ed to their same name. The <code>typedef</code>
|
||||||
|
occurs in the public API header, and the actual declaration occurs in
|
||||||
|
the private implementation header.
|
||||||
|
</li>
|
||||||
|
<li>
|
||||||
|
Indentation is done with spaces. This ensures that files look the same
|
||||||
|
for everyone. It also makes line wrapping rules much easier because
|
||||||
|
the indentations are the same size. Please configure your editor to
|
||||||
|
make the <kbd>Tab</kbd> key insert spaces, and if it does automatic
|
||||||
|
indentation, make sure it indents with spaces. If you cannot configure
|
||||||
|
your editor to insert spaces, then you can try running the code files
|
||||||
|
you were working on through <code>expand</code>. A unit of indentation
|
||||||
|
is 4 spaces.
|
||||||
|
</li>
|
||||||
|
<li>
|
||||||
|
Lines should not exceed 72 characters, including indentations. Some
|
||||||
|
developers use <code>vi(1)</code> in an 80x24 terminal to write code.
|
||||||
|
</li>
|
||||||
|
</ul>
|
||||||
|
<p>
|
||||||
|
This guide may be subject to change. The source code is the absolute
|
||||||
|
source of truth, so as long as you make your code look like the
|
||||||
|
code surrounding it, you should be fine.
|
||||||
|
</p>
|
||||||
|
<h4 id="submitting-patches">Submitting Patches</h4>
|
||||||
|
<p>
|
||||||
|
Submitting patches is fairly easy to do if you've got the CVS sources
|
||||||
|
checked out. Once you have made your changes, just run
|
||||||
|
<code>cvs diff</code>:
|
||||||
|
</p>
|
||||||
|
<div class="code">
|
||||||
|
$ cvs diff -uNp > your-changes.patch
|
||||||
|
</div>
|
||||||
|
<p>
|
||||||
|
Then, send the resulting patches to
|
||||||
|
<code>#telodendria-patches:bancino.net</code>, where they will be
|
||||||
|
promptly reviewed by the community.
|
||||||
|
</p>
|
||||||
|
<h2 id="license">License</h2>
|
||||||
|
<p>
|
||||||
|
All of the code and documentation for <b>Telodendria</b> is licensed
|
||||||
|
under the following terms and conditions:
|
||||||
|
</p>
|
||||||
|
<div class="code">
|
||||||
|
Copyright (C) 2022 Jordan Bancino <@jordan:bancino.net>
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a
|
||||||
|
copy of this software and associated documentation files (the
|
||||||
|
"Software"), to deal in the Software without restriction, including
|
||||||
|
without limitation the rights to use, copy, modify, merge, publish,
|
||||||
|
distribute, sublicense, and/or sell copies of the Software, and to
|
||||||
|
permit persons to whom the Software is furnished to do so, subject to
|
||||||
|
the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included
|
||||||
|
in all copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
||||||
|
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||||
|
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||||
|
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||||
|
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
||||||
|
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||||
|
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
</div>
|
||||||
|
<h2 id="change-log">Change Log</h2>
|
||||||
|
<p>
|
||||||
|
At this time, Telodendria does not have any tagged releases because it
|
||||||
|
is not yet functional as a Matrix homeserver. Please check out the <a
|
||||||
|
href="#project-status">Project Status</a> to see where things are
|
||||||
|
currently at.
|
||||||
|
</p>
|
||||||
|
</body>
|
||||||
|
</html>
|
|
@ -1,6 +0,0 @@
|
||||||
all:
|
|
||||||
sh tools/bin/td
|
|
||||||
|
|
||||||
install:
|
|
||||||
install build/telodendria $(PREFIX)/bin/telodendria
|
|
||||||
find man -name 'telodendria*\.[1-8]' -exec install {} $(PREFIX)/{} \;
|
|
|
@ -1,64 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright (C) 2022-2023 Jordan Bancino <@jordan:bancino.net>
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person
|
|
||||||
* obtaining a copy of this software and associated documentation files
|
|
||||||
* (the "Software"), to deal in the Software without restriction,
|
|
||||||
* including without limitation the rights to use, copy, modify, merge,
|
|
||||||
* publish, distribute, sublicense, and/or sell copies of the Software,
|
|
||||||
* and to permit persons to whom the Software is furnished to do so,
|
|
||||||
* subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be
|
|
||||||
* included in all copies or portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
||||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
||||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
||||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
||||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
||||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
||||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
||||||
* SOFTWARE.
|
|
||||||
*/
|
|
||||||
#include <Tls.h>
|
|
||||||
|
|
||||||
#if TLS_IMPL == TLS_TEMPLATE /* Set your TLS_* implementation
|
|
||||||
* flag here */
|
|
||||||
|
|
||||||
/*
|
|
||||||
* #include statements and any implementation structures
|
|
||||||
* needed should go here.
|
|
||||||
*/
|
|
||||||
|
|
||||||
void *
|
|
||||||
TlsInitClient(int fd, const char *serverName)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
void *
|
|
||||||
TlsInitServer(int fd, const char *crt, const char *key)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
ssize_t
|
|
||||||
TlsRead(void *cookie, void *buf, size_t nBytes)
|
|
||||||
{
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
ssize_t
|
|
||||||
TlsWrite(void *cookie, void *buf, size_t nBytes)
|
|
||||||
{
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
|
||||||
TlsClose(void *cookie)
|
|
||||||
{
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
|
34
contrib/Vagrantfile
vendored
34
contrib/Vagrantfile
vendored
|
@ -1,34 +0,0 @@
|
||||||
Vagrant.configure("2") do |config|
|
|
||||||
config.vm.box = "generic/openbsd7"
|
|
||||||
config.vm.network "forwarded_port", guest: 80, host: 80
|
|
||||||
config.vm.network "forwarded_port", guest: 443, host: 443
|
|
||||||
config.vm.network "forwarded_port", guest: 8008, host: 8008
|
|
||||||
# NOTE: This address is not within the allowed ranges.
|
|
||||||
# To allow this address, simply allow all ranges by specifying
|
|
||||||
# this in /etc/vbox/networks.conf (if you use the Virtualbox provider, or change the configured address):
|
|
||||||
# * 0.0.0.0/0 ::/0
|
|
||||||
config.vm.network "private_network", ip: "172.17.0.101"
|
|
||||||
# File watcher which syncs the project directory to /vagrant on the vm
|
|
||||||
config.vm.synced_folder "../", "/vagrant"
|
|
||||||
config.vm.provision "shell", inline: <<-EOF
|
|
||||||
cp /vagrant/contrib/relayd.conf /etc/relayd.conf
|
|
||||||
sed -i s/127.0.0.1/0.0.0.0/ /etc/relayd.conf
|
|
||||||
mkdir -p -m 0700 /etc/ssl/private
|
|
||||||
openssl req -x509 -newkey rsa:4096 \
|
|
||||||
-days 365 -nodes \
|
|
||||||
-subj '/CN=telodendria' \
|
|
||||||
-keyout /etc/ssl/private/telodendria.key \
|
|
||||||
-out /etc/ssl/telodendria.crt
|
|
||||||
relayd -n
|
|
||||||
rcctl enable relayd
|
|
||||||
rcctl restart relayd
|
|
||||||
cat /vagrant/tools/env.sh >> /home/vagrant/.bash_profile
|
|
||||||
sed -i 's#$(pwd)#/vagrant#' /home/vagrant/.bash_profile
|
|
||||||
sed -i 's#find tools/bin#find /vagrant/tools/bin#' /home/vagrant/.bash_profile
|
|
||||||
mkdir /vagrant/data
|
|
||||||
cp /vagrant/contrib/development.conf /vagrant/contrib/development.conf.bak
|
|
||||||
sed -i 's/"localhost"/"vagrant"/' /vagrant/contrib/development.conf
|
|
||||||
### If you changed the address in the config above you might want to change it here as well:
|
|
||||||
sed -i s#http://localhost:8008#https://172.17.0.101:443# /vagrant/contrib/development.conf
|
|
||||||
EOF
|
|
||||||
end
|
|
|
@ -1,18 +1,18 @@
|
||||||
{
|
#
|
||||||
"log": {
|
# Telodendria development configuration file.
|
||||||
"output": "stdout",
|
#
|
||||||
"color": true,
|
|
||||||
"timestampFormat": "none",
|
server-name "localhost";
|
||||||
"level": "debug"
|
base-url "http://localhost:8008";
|
||||||
},
|
id "jordan";
|
||||||
"listen": [
|
data-dir "./data";
|
||||||
{
|
federation "true";
|
||||||
"port": 8008,
|
registration "true";
|
||||||
"tls": false
|
log "stdout" {
|
||||||
}
|
level "debug";
|
||||||
],
|
timestampFormat "none";
|
||||||
"registration": true,
|
color "true";
|
||||||
"serverName": "localhost",
|
};
|
||||||
"baseUrl": "http:\/\/localhost:8008",
|
threads "4";
|
||||||
"federation": true
|
max-connections "32";
|
||||||
}
|
max-cache "1k";
|
||||||
|
|
9
contrib/httpd.conf
Normal file
9
contrib/httpd.conf
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
#
|
||||||
|
# httpd.conf: An OpenBSD httpd(8) configuration file for running
|
||||||
|
# Telodendria. Note that this is a development configuration that
|
||||||
|
# should be adapted using the httpd.conf(5) man page for production
|
||||||
|
# use.
|
||||||
|
#
|
||||||
|
server "matrix" {
|
||||||
|
listen on localhost port http
|
||||||
|
}
|
|
@ -1,24 +1,32 @@
|
||||||
{
|
#
|
||||||
"log": {
|
# Telodendria configuration file.
|
||||||
"output": "file"
|
#
|
||||||
},
|
# The following man pages document the configuration:
|
||||||
"listen": [
|
#
|
||||||
{
|
# - telodendria.conf(5)
|
||||||
"port": 8008,
|
# - Config(5)
|
||||||
"tls": false
|
#
|
||||||
},
|
# Alternatively, find the man pages online at the
|
||||||
{
|
# following URL:
|
||||||
"port": 8448,
|
#
|
||||||
"tls": {
|
# https://telodendria.io/#documentation
|
||||||
"cert": "telodendria.crt",
|
#
|
||||||
"key": "telodendria.key"
|
|
||||||
}
|
listen "8008";
|
||||||
}
|
|
||||||
],
|
server-name "example.com";
|
||||||
"serverName": "example.com",
|
base-url "https://matrix.example.com";
|
||||||
"identityServer": "https://identity.example.com",
|
identity-server "https://identity.example.com";
|
||||||
"baseUrl": "https://matrix.example.com",
|
|
||||||
"registration": false,
|
id "_telodendria" "_telodendria";
|
||||||
"federation": true,
|
data-dir "/var/telodendria";
|
||||||
"maxCache": 512000000
|
federation "true";
|
||||||
}
|
registration "false";
|
||||||
|
log "file" {
|
||||||
|
level "warning";
|
||||||
|
timestampFormat "default";
|
||||||
|
};
|
||||||
|
|
||||||
|
threads "4";
|
||||||
|
max-connections "32";
|
||||||
|
max-cache "512M";
|
||||||
|
|
|
@ -1,22 +0,0 @@
|
||||||
table <telodendria> { localhost }
|
|
||||||
|
|
||||||
http protocol httpproxy {
|
|
||||||
pass request quick path "/_matrix/*" forward to <telodendria>
|
|
||||||
pass request quick path "/.well-known/matrix/*" forward to <telodendria>
|
|
||||||
|
|
||||||
# You'll have to generate the following for this to work:
|
|
||||||
# /etc/ssl/telodendria.crt /etc/ssl/private/telodendria.key
|
|
||||||
tls keypair "telodendria"
|
|
||||||
|
|
||||||
block
|
|
||||||
return error
|
|
||||||
}
|
|
||||||
|
|
||||||
relay proxy {
|
|
||||||
listen on 127.0.0.1 port https tls
|
|
||||||
|
|
||||||
protocol httpproxy
|
|
||||||
|
|
||||||
forward to <telodendria> port 8008
|
|
||||||
}
|
|
||||||
|
|
13
contrib/telodendria.conf
Normal file
13
contrib/telodendria.conf
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
# Telodendria configuration file
|
||||||
|
|
||||||
|
log "/var/log/telodendria.log" {
|
||||||
|
level "message";
|
||||||
|
timestampFormat "none";
|
||||||
|
color "true";
|
||||||
|
};
|
||||||
|
|
||||||
|
threads "4";
|
||||||
|
|
||||||
|
data-dir "/var/telodendria";
|
||||||
|
|
||||||
|
federation "true";
|
|
@ -1,250 +0,0 @@
|
||||||
# Telodendria Change Log
|
|
||||||
|
|
||||||
This document contains the complete change log for every official release of Telodendria.
|
|
||||||
It is intended to be updated with every commit that makes a user-facing change worth
|
|
||||||
reporting in the change log. As such, it changes frequently between releases. Final
|
|
||||||
change log entries are published as [Releases](releases).
|
|
||||||
|
|
||||||
## v0.4.0
|
|
||||||
|
|
||||||
**Not Released Yet.**
|
|
||||||
|
|
||||||
This release brings filters, rooms, and events! The core of the Matrix protocol architecture
|
|
||||||
is not in place.
|
|
||||||
|
|
||||||
### Matrix Specification
|
|
||||||
|
|
||||||
The following endpoints were added:
|
|
||||||
|
|
||||||
- **POST** `/_matrix/client/v3/user/{userId}/filter`
|
|
||||||
- **GET** `/_matrix/client/v3/user/{userId}/filter/{filterId}`
|
|
||||||
|
|
||||||
### Bug Fixes
|
|
||||||
|
|
||||||
- Fixed a double-free in `RouteUserProfile()` that would cause errors with certain
|
|
||||||
Matrix clients. (#35)
|
|
||||||
|
|
||||||
## v0.3.0
|
|
||||||
|
|
||||||
**Saturday, June 10, 2023**
|
|
||||||
|
|
||||||
Introducing a new configuration API and Cytoplasm, a general-purpose C library that
|
|
||||||
supports source/sink-agnostic I/O, TLS, an HTTP client, and more! The third major
|
|
||||||
release of Telodendria packs a lot of architectural improvements on top of supporting
|
|
||||||
more of the Matrix specification.
|
|
||||||
|
|
||||||
### Matrix Specification
|
|
||||||
|
|
||||||
Added support for the following endpoints:
|
|
||||||
|
|
||||||
- `/_matrix/client/v3/account/whoami`
|
|
||||||
- `/_matrix/client/v3/account/password`
|
|
||||||
- `/_matrix/client/v3/account/deactivate`
|
|
||||||
- `/_matrix/client/v3/profile/*`
|
|
||||||
- `/_matrix/client/v3/capabilities`
|
|
||||||
- `/_matrix/client/v3/auth/*/fallback/web`
|
|
||||||
|
|
||||||
There is also support for token-based user registration. Note that there is as of
|
|
||||||
yet no admin-facing way to create these registration tokens, but the APIs are in
|
|
||||||
place.
|
|
||||||
|
|
||||||
### New Features
|
|
||||||
|
|
||||||
- Added a new `HttpClient` API for making HTTP requests. This will eventually be
|
|
||||||
used for federating with other Matrix homeservers.
|
|
||||||
- Added support for pretty-printing JSON in `Json`. Telodendria itself does not
|
|
||||||
pretty-print JSON, but this is useful for debugging and building useful tools.
|
|
||||||
- Added a handful of new development tools built on the Telodendria APIs. New
|
|
||||||
tools include `http`, a command line tool for making HTTP requests, similar to
|
|
||||||
`curl`, `json`, a command line tool for working with JSON, similar to `jq`, and
|
|
||||||
`http-debug-server`, a simple HTTP server that just prints requests out to standard
|
|
||||||
output and returns an empty JSON object. `http` and `json` are replacements for
|
|
||||||
`curl` and `jq` that build on the `HttpClient` and `Json` APIs. They exist mainly
|
|
||||||
to test those APIs, but also to reduce the number of dependencies that Telodendria
|
|
||||||
has. `http-debug-server` exists to test the `HttpServer` and `HttpClient` APIs.
|
|
||||||
- Replaced all usage of `jq` with the new `json` tool. `jq` is no longer a development
|
|
||||||
dependency.
|
|
||||||
- Replaced all usage of `curl` with the new `http` tool. `curl` is no longer a
|
|
||||||
required development dependency.
|
|
||||||
- Added a new `tt` script for easily making Matrix requests against Telodendria
|
|
||||||
in development.
|
|
||||||
- Added TLS support to both the HTTP client and server. Currently, Telodendria
|
|
||||||
supports LibreSSL and OpenSSL, but other TLS libraries should be extremely easy
|
|
||||||
to add support for.
|
|
||||||
- Added support for spinning up multiple HTTP servers. This is useful for having
|
|
||||||
a TLS port and a non-TLS port, for example.
|
|
||||||
- Moved all program configuration to the data directory and added an administrator
|
|
||||||
API endpoint to manage it. It is now no longer recommended to manually update the
|
|
||||||
configuration file. Consult the [Administrator API](user/admin/README.md) documentation
|
|
||||||
and the [Configuration](user/config.md) documentation.
|
|
||||||
- Added an administrator API endpoint for process control. Telodendria can now be
|
|
||||||
restarted or shutdown via API endpoint.
|
|
||||||
- Added an administrator API endpoint for getting statistics about the running
|
|
||||||
Telodendria process.
|
|
||||||
- Added support for user privileges, a way to have fine-grained control over what
|
|
||||||
users are allowed to do with the administrator API. Administrator APIs for setting
|
|
||||||
and getting privileges is now supported, and registration tokens have privileges
|
|
||||||
associated with them so that users created with a token will automatically be given
|
|
||||||
the specified privileges.
|
|
||||||
|
|
||||||
### Fixes & General Improvements
|
|
||||||
|
|
||||||
- Fixed a few warnings that were generated on some obscure compilers.
|
|
||||||
- Moved the `main()` function to its own file to make it easier to link other
|
|
||||||
programs with the Telodendria APIs.
|
|
||||||
- Fixed the development tools environment script. Apparently using a hyphen as a bullet
|
|
||||||
point is not very portable, because some shell implementations of `printf` interpret it
|
|
||||||
as a flag. Switched to an asterisk.
|
|
||||||
- Fixed some intermittent I/O errors that would occur as a result of race conditions in
|
|
||||||
`JsonConsomeWhitespace()`. This function, and a few others, expect I/O to be blocking,
|
|
||||||
but the `HttpServer` sets up I/O to be non-blocking, leading to occasional failures in
|
|
||||||
JSON parsing.
|
|
||||||
- Abstracted all I/O into the new `Io` and `Stream` APIs, which provide an input- and
|
|
||||||
output- agnostic stream processing interface. This allows for a simple implementation of
|
|
||||||
proxies, TLS, and other stream filters without having to change any of the existing
|
|
||||||
code.
|
|
||||||
- Remove all non-POSIX function calls, including the call to `chroot()` and, on
|
|
||||||
OpenBSD, `pledge()` and `unveil()`. This may seem like a downgrade in security, but
|
|
||||||
these are platform-specific system calls that should be patched in by package maintainers
|
|
||||||
if they are desired. They also caused problems when implementing other features, because
|
|
||||||
some library calls need to be able to access files on the filesystem.
|
|
||||||
- Fixed the build script to supply `LDFLAGS` after the object files when linking.
|
|
||||||
Apparently the order in which libraries are passed matters to some compilers.
|
|
||||||
- Added the response status of a request to the log output. This means that requests are
|
|
||||||
logged after they have completed, not before they are started.
|
|
||||||
- Memory allocations, reallocations, and frees are no longer loged when the log level
|
|
||||||
is set to debug in the configuration file. To enable the logging of memory operations,
|
|
||||||
pass the `-v` flag.
|
|
||||||
- Implemented a proper HTTP request router with POSIX regular expression support.
|
|
||||||
Previously, a series of nested `if`-statements were used to route requests, but this
|
|
||||||
approach quickly becamse very messy. While the HTTP request router incurs a small memory
|
|
||||||
and runtime speed penalty, the code is now much more maintainable and easier to follow.
|
|
||||||
- Fixed some memory bugs in `Db` that were related to caching data. Caching should
|
|
||||||
now work as expected.
|
|
||||||
- Fixed a major design flaw in `Db` that would cause deadlock when multiple threads
|
|
||||||
request access to the same object. Database locking is now in a per-thread basis,
|
|
||||||
instead of a per-reference basis.
|
|
||||||
- Telodendria now shuts down cleanly in response to `SIGTERM`.
|
|
||||||
- Did some general refactoring to make the source code more readable and easier
|
|
||||||
to maintain.
|
|
||||||
Fixed a number of memory-related issues, including switching out some unsafe
|
|
||||||
functions for safer versions, per the recommendations of the OpenBSD linker.
|
|
||||||
- Moved all code documentation into the C header files to make it more likely
|
|
||||||
that it will get updated. A simple header file parser and documentation generator
|
|
||||||
have been added to the code base. See the `hdoc` man pages for documentation.
|
|
||||||
- Updated the build script to provide static and shared libraries containing
|
|
||||||
the code for Telodendria to make it easier to statically and dynamically link to
|
|
||||||
other programs. The idea is that these libraries should be shipped with Telodendria,
|
|
||||||
or as a separate package, and can be used to provide a high-level programming
|
|
||||||
environment.
|
|
||||||
- Updated the `Json` API to calculate the length of a JSON object. This is
|
|
||||||
used to set the `Content-Length` header in HTTP requests and reponses.
|
|
||||||
- Added some string functions, including `StrEquals()`, which replaced almost all
|
|
||||||
uses of `strcmp()`, since `strcmp()` is used almost exclusively for equality
|
|
||||||
checking. `StrEquals()` provides a standard way to do so, because previously,
|
|
||||||
multiple different conventions could be found throughout the code base (for example:
|
|
||||||
`!strcmp(str1, str2)` vs `strcmp(str1, str2) == 0`).
|
|
||||||
|
|
||||||
... And many more!
|
|
||||||
|
|
||||||
## v0.2.1
|
|
||||||
|
|
||||||
**Monday, March 6, 2023**
|
|
||||||
|
|
||||||
This is a patch release that fixes a few typos and other minor issues.
|
|
||||||
|
|
||||||
## v0.2.0
|
|
||||||
|
|
||||||
**Monday, March 6, 2023**
|
|
||||||
|
|
||||||
This release is focused on providing a decent amount of the client authentication
|
|
||||||
API. You can now create accounts on a Telodendria homeserver, and log in to
|
|
||||||
get access tokens.
|
|
||||||
|
|
||||||
### New
|
|
||||||
|
|
||||||
- Added the basic form of the user registration API. If registration is enabled
|
|
||||||
in the configuration file, clients can now register for Matrix accounts.
|
|
||||||
- Added the basic form of the user login API. Clients can now log in to
|
|
||||||
their accounts and generate access tokens to be used to authenticate requests.
|
|
||||||
- Added the basic form of the user interactive authentication API, which can be used
|
|
||||||
by endpoints that the spec says requires it. Currently, it only implements the
|
|
||||||
dummy and password stages, but more stages, such as the registration token stage,
|
|
||||||
will be added in future releases.
|
|
||||||
- Added a simple landing page that allows those setting up Telodendria to
|
|
||||||
quickly verify that it is accessible where it needs to be.
|
|
||||||
- Added the static login page for clients that don't support regular login.
|
|
||||||
|
|
||||||
### Changes
|
|
||||||
|
|
||||||
- Improved HTTP request logging by removing unnecessary log entries and making
|
|
||||||
errors more specific.
|
|
||||||
- Leaked memory is now hexdump-ed out to the log if the log level is set to debug.
|
|
||||||
This greatly simplifies debugging, because developers can now see exactly what the
|
|
||||||
contents of the leaked memory are. Note that in some circumstances, this memory
|
|
||||||
may contain sensitive data, such as access tokens, usernames, or passwords. However,
|
|
||||||
Telodendria should not be leaking memory at all, so if you encounter any leaks,
|
|
||||||
please report them.
|
|
||||||
- Refactored a lot of the code and accompanying documentation to be more readable and
|
|
||||||
maintainable.
|
|
||||||
|
|
||||||
### Bug Fixes
|
|
||||||
|
|
||||||
- Fixed a memory leak that would occur when parsing an invalid JSON object.
|
|
||||||
- Fixed an edge case where HTTP response headers were being sent before they were
|
|
||||||
properly set, causing the server to report a status of 200 even when that wasn't the
|
|
||||||
desired status.
|
|
||||||
- Fixed a few memory leaks in the HTTP parameter decoder that would occur in some
|
|
||||||
edge cases.
|
|
||||||
- Fixed an "off-by-one" error in the HTTP server request parser that would prevent
|
|
||||||
`GET` parameters from being parsed.
|
|
||||||
- Fixed the database file name descriptor to prevent directory traversal attacks
|
|
||||||
by replacing special characters with safer ones.
|
|
||||||
- Fixed a memory leak that would occur when closing a database that contains
|
|
||||||
cached objects.
|
|
||||||
- Fixed a memory leak that would occur when deleting database objects.
|
|
||||||
- Fixed a few non-fatal memory warnings that would show up as a result of passing a
|
|
||||||
constant string into certain functions.
|
|
||||||
|
|
||||||
### Misc.
|
|
||||||
|
|
||||||
- Fixed a bug in `td` that caused `cvs` to be invoked in the wrong directory when
|
|
||||||
tagging a new release.
|
|
||||||
- Added support for environment variable substitution in all site files. This
|
|
||||||
makes it easier to release Telodendria versions.
|
|
||||||
- Fix whitespace issues in various shell scripts.
|
|
||||||
- Fixed the debug log output so that it only shows the file name, not the
|
|
||||||
entire file path in the repository.
|
|
||||||
- Updated the copyright year in the source code and compiled output.
|
|
||||||
- Switched the `-std=c89` flag to `-ansi`, as `-ansi` might be more supported.
|
|
||||||
- Fixed the `-v` flag. It now sets the log level to debug as soon as possible to
|
|
||||||
allow debugging configuration file parsing if necessary.
|
|
||||||
|
|
||||||
... And many more bug fixes and feature additions! Too much has changed to make a
|
|
||||||
comprehensive change log. A lot of things have been done under the hood to make
|
|
||||||
Telodendria easier to develop in the future. Please test the current functionality,
|
|
||||||
and report bugs.
|
|
||||||
|
|
||||||
The following platforms have been known to compile and run Telodendria:
|
|
||||||
|
|
||||||
- OpenBSD
|
|
||||||
- Linux (GNU and non-GNU)
|
|
||||||
- Windows (via Cygwin)
|
|
||||||
- FreeBSD
|
|
||||||
- NetBSD
|
|
||||||
- DragonFlyBSD
|
|
||||||
- Haiku OS
|
|
||||||
- Android (via Termux)
|
|
||||||
|
|
||||||
Telodendria is about being portable; if you compile it on an obscure operating system,
|
|
||||||
do let us know about it!
|
|
||||||
|
|
||||||
## v0.1.0
|
|
||||||
|
|
||||||
**Tuesday, December 13, 2022**
|
|
||||||
|
|
||||||
This is the first public release of Telodendria so there are no changes to report.
|
|
||||||
Future releases will have a complete change log entry here.
|
|
||||||
|
|
||||||
This is a symbolic release targeted at developers, so there's nothing useful to
|
|
||||||
ordinary users yet. Stay tuned for future releases though!
|
|
|
@ -1,163 +0,0 @@
|
||||||
# Contributing
|
|
||||||
|
|
||||||
Telodendria is a fully open source project. As such, it welcomes
|
|
||||||
contributions. There are many ways you can contribute, and any way you
|
|
||||||
can is greatly appreciated. This document details the ways you can
|
|
||||||
contribute, and how to go about contributing.
|
|
||||||
|
|
||||||
## Sponsoring Telodendria
|
|
||||||
|
|
||||||
If you would like to sponsor Telodendria, see the
|
|
||||||
[Sponsorship](../README.md#sponsorship) section on the main project
|
|
||||||
page. Donations of any size are greatly appreciated.
|
|
||||||
|
|
||||||
## Reporting Issues
|
|
||||||
|
|
||||||
An important way to get involved is to just report issues you find with
|
|
||||||
Telodendria during experimentation or normal use. To report an issue,
|
|
||||||
go to [Issues](/Telodendria/telodendria/issues) →
|
|
||||||
[New Issue](/Telodendria/telodendria/issues/new/choose) and follow the
|
|
||||||
instructions.
|
|
||||||
|
|
||||||
> **Note:** GitHub issues are not accepted. Issues may only be
|
|
||||||
> submitted to the official [Gitea](https://git.telodendria.io)
|
|
||||||
> instance.
|
|
||||||
|
|
||||||
### Feature Requests
|
|
||||||
|
|
||||||
Feature requests are allowed, but note that they are low-priority in
|
|
||||||
comparison to existing issues and features. That being said, don't
|
|
||||||
hesitate to submit feature requests. Just select the "Feature Request"
|
|
||||||
option when submitting an issue.
|
|
||||||
|
|
||||||
## Developing
|
|
||||||
|
|
||||||
If you want to write code for Telodendria, either to fix an issue or
|
|
||||||
add a new feature, you're in the right place. Please follow all the
|
|
||||||
guidelines in this document to ensure the contribution workflow goes
|
|
||||||
as smoothly as possible.
|
|
||||||
|
|
||||||
### Who can develop Telodendria?
|
|
||||||
|
|
||||||
Everyone is welcome to contribute code to Telodendria, provided that
|
|
||||||
they are willing to license their contributions under the same license
|
|
||||||
as the project itself.
|
|
||||||
|
|
||||||
The primary language used to write Telodendria code is ANSI C. Other
|
|
||||||
languages you'll find in the Telodendria repository include shell
|
|
||||||
scripts, `mdoc`, a little bit of HTML and CSS, and `Makefiles`.
|
|
||||||
Experience with any of these is preferred, but if you want to use
|
|
||||||
Telodendria to learn, that's okay too! Telodendria's code base should
|
|
||||||
hopefully be a good learning tool, and if you are serious about
|
|
||||||
submitting quality work, we'll guide you through the process and
|
|
||||||
offer suggestions.
|
|
||||||
|
|
||||||
### What do I need?
|
|
||||||
|
|
||||||
You'll need a couple of things to develop Telodendria:
|
|
||||||
|
|
||||||
- A Unix-like operating system that provides standard POSIX behavior,
|
|
||||||
or the Windows Subsystem for Linux (WSL), Cygwin, or Msys2 if you are
|
|
||||||
running Windows.
|
|
||||||
- A C compiler capable of compiling ANSI C89 code (pretty much all of
|
|
||||||
them do—pick your favorite, and if you find it doesn't work,
|
|
||||||
open an issue!).
|
|
||||||
- `make` for building the project.
|
|
||||||
- `git` for managing your changes.
|
|
||||||
|
|
||||||
Optionally, you may also find these tools helpful:
|
|
||||||
|
|
||||||
- `indent` for formatting code.
|
|
||||||
- `valgrind` for debugging particularly nasty issues.
|
|
||||||
|
|
||||||
### Getting The Code
|
|
||||||
|
|
||||||
Telodendria is developed using Git. The easiest way to contribute
|
|
||||||
changes is to fork the main repository, and then creating a pull
|
|
||||||
request to ask us to pull your changes into our repo.
|
|
||||||
|
|
||||||
1. If you don't have an account on the
|
|
||||||
[Gitea instance](https://git.telodendria.io), create one and sign in.
|
|
||||||
1. Fork this repository.
|
|
||||||
1. In your development environment, clone your fork:
|
|
||||||
```shell
|
|
||||||
git clone https://git.telodendria.io/[YOUR_USERNAME]/telodendria.git
|
|
||||||
cd telodendria
|
|
||||||
```
|
|
||||||
|
|
||||||
Please base your changes on the `master` branch. If you need help
|
|
||||||
getting started with Git, that is beyond the scope of this
|
|
||||||
document, but you can find many good tutorials on the web.
|
|
||||||
|
|
||||||
### Building & Running
|
|
||||||
|
|
||||||
Telodendria uses the `make` build system.
|
|
||||||
|
|
||||||
**TODO:** Update this section before #19 is closed. Provide quick
|
|
||||||
make, run, and install directions (maybe just redirect to Porting for
|
|
||||||
install directions), then list all the `make` recipes. Shouldn't be
|
|
||||||
as many as were in `td`.
|
|
||||||
|
|
||||||
### Pull Requests
|
|
||||||
|
|
||||||
> **Note:** Telodendria does not accept GitHub pull requests at this
|
|
||||||
> time. Please submit your pull requests via Gitea.
|
|
||||||
|
|
||||||
Telodendria follows the standard pull request procedures. Once you have
|
|
||||||
made your changes, committed them, and pushed to your fork, you should
|
|
||||||
be able to open a pull request on the main repository. When you do, you
|
|
||||||
will be prompted to write a description. Be sure to include the
|
|
||||||
related issue that you are closing in your description.
|
|
||||||
|
|
||||||
### Code Style
|
|
||||||
|
|
||||||
In general, these are the conventions used by the code base. This
|
|
||||||
guide may be slightly outdated or subject to change, but it should be
|
|
||||||
a good start. The source code itself is always the absolute source of
|
|
||||||
truth, so as long as you make your code look like the code surrounding
|
|
||||||
it, you should be fine.
|
|
||||||
|
|
||||||
- All function, enumeration, structure, and header names are
|
|
||||||
`CamelCase`. This is preferred to `snake_case` because it is more
|
|
||||||
compact.
|
|
||||||
- All variable names are `lowerCamelCase`. This is preferred to
|
|
||||||
`snake_case` because it is more compact. One exception to this rule is
|
|
||||||
if a variable name, such as a member of a struct, directly represents
|
|
||||||
a JSON key in an object specified by the Matrix specification, which
|
|
||||||
may be in `snake_case`.
|
|
||||||
- Enumerations and structures are always `typedef`-ed to their same
|
|
||||||
name. The `typedef` should occur in the public API header, and the
|
|
||||||
actual declaration should live in the implementation file, unless
|
|
||||||
the enumeration or structure is intended to be made fully public.
|
|
||||||
- A feature of the code base lives in a single C source file that has a
|
|
||||||
matching header. The header file should only export public symbols;
|
|
||||||
everything else in the C source should be static.
|
|
||||||
- Except where absolutely necessary, global variables are forbidden
|
|
||||||
to prevent problems with threads and whatnot. Every variable a
|
|
||||||
function needs should be passed to it either through a structure, or
|
|
||||||
as a separate argument.
|
|
||||||
- Anywhere that C allows curly braces to be optional, there still must
|
|
||||||
be curly braces. This makes it easier to read the code by making it
|
|
||||||
less ambiguous, and it makes it easier to add on to the code later.
|
|
||||||
|
|
||||||
As far as actually formatting the code goes, such as where to put
|
|
||||||
brackets, and whether or not to use tabs or spaces, use `indent` to
|
|
||||||
take care of that. The repository contains a `.indent.pro` that should
|
|
||||||
automatically be loaded by `indent` to set the correct rules. If you
|
|
||||||
don't have a working `indent`, then just indicate in your pull
|
|
||||||
request that I should run my `indent` on the code.
|
|
||||||
|
|
||||||
### Documentation
|
|
||||||
|
|
||||||
This project places a strong emphasis on documentation. Well-documented
|
|
||||||
code is fundamental to a successful project, so when you are writing
|
|
||||||
code, please also make sure that it is documented appropriately.
|
|
||||||
|
|
||||||
- If you are adding a header, make sure you add the necessary comments
|
|
||||||
detailing the header and the functions in it.
|
|
||||||
- If you are adding a function, make sure you add the necessary
|
|
||||||
comments to the appropriate header.
|
|
||||||
|
|
||||||
If your pull request does not also include proper documentation, it
|
|
||||||
will likely be rejected.
|
|
||||||
|
|
|
@ -1,32 +0,0 @@
|
||||||
# Telodendria Documentation
|
|
||||||
|
|
||||||
Here you will find all of the documentation for Telodendria. If you
|
|
||||||
find that some documentation is missing or incorrect, please open an
|
|
||||||
issue, and, even better, a pull request to fix the issue.
|
|
||||||
|
|
||||||
You are also welcome to join the
|
|
||||||
[`#telodendria-general:bancino.net`](https://matrix.to/#/#telodendria-general:bancino.net)
|
|
||||||
matrix room, where you can discuss Telodnedria with others and ask
|
|
||||||
questions.
|
|
||||||
|
|
||||||
## User Documentation
|
|
||||||
|
|
||||||
- [System Requirements](user/requirements.md)
|
|
||||||
- [Install](user/install.md)
|
|
||||||
- [Usage & Running](user/usage.md)
|
|
||||||
- [Initial Set Up](user/setup.md)
|
|
||||||
- [Configuration Options](user/config.md)
|
|
||||||
- [Administrator API](user/admin/README.md)
|
|
||||||
|
|
||||||
## Developer Documentation
|
|
||||||
|
|
||||||
- [Repository Structure](dev/repo.md)
|
|
||||||
- [Contributing Guidelines](CONTRIBUTING.md)
|
|
||||||
- [Porting Guidelines](dev/ports.md)
|
|
||||||
|
|
||||||
## Miscellaneous
|
|
||||||
|
|
||||||
- [Matrix Specification](https://spec.matrix.org) ([Mirror](https://telodendria.io/spec.matrix.org))
|
|
||||||
- [Change Log](CHANGELOG.md)
|
|
||||||
- [Project Road Map](ROADMAP.md)
|
|
||||||
|
|
|
@ -1,54 +0,0 @@
|
||||||
# Telodendria Matrix Specification Roadmap
|
|
||||||
|
|
||||||
This document provides a high-level overview of Telodendria's roadmap as it pertains to implementing the Matrix specification. Essentially, the Matrix specification is divided up into manageable portions amongst Telodendria releases, so that each release up until the first stable release implements a small portion of it.
|
|
||||||
|
|
||||||
**Note:** The first stable release of Telodendria will implement Matrix v1.7, no newer version. The Matrix specification changes too frequently, so I had to just pick a version in order to make this project manageable. Once v1.7 is complete, then we can move on to later specs.
|
|
||||||
|
|
||||||
This document will be updated to include more implementation details as they come up. It contains the big picture for far-out releases, and more relevant implementation details for near releases.
|
|
||||||
|
|
||||||
## Milestone v0.4.0
|
|
||||||
|
|
||||||
- [ ] Client-Server API
|
|
||||||
- [ ] **7:** Events
|
|
||||||
- [x] Compute size of JSON object in `CanonicalJson`
|
|
||||||
- [x] Rename `Sha2.h` to just `Sha.h`; add `Sha1()` function
|
|
||||||
- [x] Make `Sha256()` just return raw bytes; add function to convert to hex string.
|
|
||||||
- [ ] **8:** Rooms
|
|
||||||
- [ ] **9:** User Data
|
|
||||||
- [x] Profiles
|
|
||||||
- [ ] Directory
|
|
||||||
|
|
||||||
## Milestone v0.5.0
|
|
||||||
|
|
||||||
- [ ] Client-Server API
|
|
||||||
- [ ] Modules
|
|
||||||
- [ ] Content Repository
|
|
||||||
|
|
||||||
## Milestone v0.6.0
|
|
||||||
|
|
||||||
- [ ] Client-Server API
|
|
||||||
- [ ] Modules
|
|
||||||
- [ ] Instant Messaging
|
|
||||||
- [ ] Voice over IP
|
|
||||||
- [ ] Receipts
|
|
||||||
- [ ] Fully Read Markers
|
|
||||||
- [ ] Send-To-Device Messaging
|
|
||||||
- [ ] Security (Rate Limiting)
|
|
||||||
|
|
||||||
## Milestone v0.7.0
|
|
||||||
|
|
||||||
- [ ] Server-Server API
|
|
||||||
|
|
||||||
## Milestone v0.8.0
|
|
||||||
|
|
||||||
- [ ] Application Service API
|
|
||||||
- [ ] YAML parser?
|
|
||||||
|
|
||||||
## Milestone v0.9.0
|
|
||||||
|
|
||||||
- [ ] Identity Service API
|
|
||||||
|
|
||||||
## Milestone v1.7.0 (Stable Release!)
|
|
||||||
|
|
||||||
- [ ] Push Gateway API
|
|
||||||
- [ ] Room Versions
|
|
Some files were not shown because too many files have changed in this diff Show more
Loading…
Reference in a new issue