Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
No results found
Show changes
Showing
with 602 additions and 559 deletions
#include "common.h"
int main(void)
{
const char *strings_file = "test/test-strings";
const char *strings_reencoded_file = "test/test-strings-reencoded";
FILE *f, *f2;
char buffer[1024], buffer2[1024];
plan_tests(90);
f = fopen(strings_file, "rb");
if (f == NULL) {
diag("Could not open %s: %s", strings_file, strerror(errno));
return 1;
}
f2 = fopen(strings_reencoded_file, "rb");
if (f2 == NULL) {
diag("Could not open %s: %s", strings_reencoded_file, strerror(errno));
return 1;
}
while (fgets(buffer, sizeof(buffer), f)) {
const char *s = chomp(buffer);
bool valid;
JsonNode *node;
if (expect_literal(&s, "valid ")) {
valid = true;
} else if (expect_literal(&s, "invalid ")) {
valid = false;
} else {
fail("Invalid line in test-strings: %s", buffer);
continue;
}
node = json_decode(s);
if (valid) {
char *reencoded;
char errmsg[256];
if (node == NULL) {
fail("%s is valid, but json_decode returned NULL", s);
continue;
}
if (!json_check(node, errmsg)) {
fail("Corrupt tree produced by json_decode: %s", errmsg);
continue;
}
reencoded = json_encode(node);
if (!fgets(buffer2, sizeof(buffer2), f2)) {
fail("test-strings-reencoded is missing this line: %s", reencoded);
continue;
}
chomp(buffer2);
ok(strcmp(reencoded, buffer2) == 0, "re-encode %s -> %s", s, reencoded);
free(reencoded);
json_delete(node);
} else if (node != NULL) {
fail("%s is invalid, but json_decode returned non-NULL", s);
continue;
}
}
if (ferror(f) || fclose(f) != 0 || ferror(f2) || fclose(f2) != 0) {
diag("I/O error reading test data.");
return 1;
}
return exit_status();
}
#include "common.h"
static char buf1[256], buf2[256];
/* Used for pass and fail messages */
static char *quote_string(const char *str, char buf[256])
{
char *out = buf;
*out++ = '"';
for (; *str != 0; str++) {
if (out - buf > 256 - 5) {
/* String is too long. End it with `...' */
out = buf + 256 - 5;
*out++ = '.';
*out++ = '.';
*out++ = '.';
break;
}
switch (*str) {
case '\t':
*out++ = '\\';
*out++ = 't';
break;
case '\n':
*out++ = '\\';
*out++ = 'n';
break;
case '"':
*out++ = '\\';
*out++ = '"';
break;
case '\\':
*out++ = '\\';
*out++ = '\\';
break;
default:
*out++ = *str;
break;
}
}
*out++ = '"';
*out = 0;
return buf;
}
static void test_stringify(const char *input, const char *expected)
{
JsonNode *node = NULL;
char *enc = NULL;
char *strn = NULL;
char *str = NULL;
node = json_decode(input);
if (node == NULL) {
fail("Failed to decode %s", input);
goto end;
}
enc = json_encode(node);
if (strcmp(enc, input) != 0) {
fail("%s re-encodes to %s. Either encode/decode is broken, or the input string needs to be normalized", input, enc);
goto end;
}
strn = json_stringify(node, NULL);
if (strcmp(strn, enc) != 0) {
fail("json_stringify with NULL space produced a different string than json_encode");
goto end;
}
str = json_stringify(node, "\t");
if (strcmp(str, expected) != 0) {
fail("Expected %s, but json_stringify produced %s",
quote_string(expected, buf1), quote_string(str, buf2));
goto end;
}
pass("stringify %s", input);
end:
json_delete(node);
free(enc);
free(strn);
free(str);
}
int main(void)
{
(void) chomp;
plan_tests(9);
test_stringify("[]", "[]");
test_stringify("[1]", "[\n\t1\n]");
test_stringify("[1,2,3]", "[\n\t1,\n\t2,\n\t3\n]");
test_stringify("[[]]", "[\n\t[]\n]");
test_stringify("[[1,2],[3,4]]", "[\n\t[\n\t\t1,\n\t\t2\n\t],\n\t[\n\t\t3,\n\t\t4\n\t]\n]");
test_stringify("{}", "{}");
test_stringify("{\"one\":1}", "{\n\t\"one\": 1\n}");
test_stringify("{\"one\":1,\"t*\":[2,3,10]}", "{\n\t\"one\": 1,\n\t\"t*\": [\n\t\t2,\n\t\t3,\n\t\t10\n\t]\n}");
test_stringify("{\"a\":{\"1\":1,\"2\":2},\"b\":{\"3\":[null,false,true,\"\\f\"]}}",
"{\n\t\"a\": {\n\t\t\"1\": 1,\n\t\t\"2\": 2\n\t},\n\t\"b\": {\n\t\t\"3\": [\n\t\t\tnull,\n\t\t\tfalse,\n\t\t\ttrue,\n\t\t\t\"\\f\"\n\t\t]\n\t}\n}");
return exit_status();
}
#include "common.h"
int main(void)
{
const char *strings_file = "test/test-strings";
FILE *f;
char buffer[1024];
plan_tests(224);
f = fopen(strings_file, "rb");
if (f == NULL) {
diag("Could not open %s: %s", strings_file, strerror(errno));
return 1;
}
while (fgets(buffer, sizeof(buffer), f)) {
const char *s = chomp(buffer);
bool valid;
if (expect_literal(&s, "valid ")) {
valid = true;
} else if (expect_literal(&s, "invalid ")) {
valid = false;
} else {
fail("Invalid line in test-strings: %s", buffer);
continue;
}
if (strcmp(s, "\"1\\u2\"") == 0)
puts("here");
if (json_validate(s) == valid) {
pass("%s %s", valid ? "valid" : "invalid", s);
} else {
fail("%s is %s, but json_validate returned %s",
s,
valid ? "valid" : "invalid",
valid ? "false" : "true");
}
}
if (ferror(f) || fclose(f) != 0) {
diag("I/O error reading test strings.");
return 1;
}
return exit_status();
}
invalid
invalid
invalid "
invalid [,]
invalid [)
invalid []]
invalid [}
invalid {,}
invalid {]
invalid ["1":2]
invalid [1,2,]
invalid [1:2}
invalid {"1":2,}
invalid {1:2}
invalid {"1":2, "2.5" : [3, 4, {}, {"5": ["6"], [7 ]}]}
invalid {"1":2, "2.5" : [3, 4, {}, {"5": ["6"], [7]}]}
invalid {"1":2, "2.5" : [3, 4, {}, {"5": ["6"], "7" :[8 ]}]
invalid {"1":2, "2.5" : [3, 4, {}, {"5": ["6"], "7" :[8 ]}]]
invalid {"1":2, "3":4
invalid "1\u2"
invalid [,2]
invalid "3
invalid "3" "4"
invalid [3[4]
invalid [3[4]]
invalid [3, [4, [5], 6] 7, 8 9]
invalid [3, [4, [5], 6] 7, 8, 9]
invalid [3, [4, [5], 6], 7, 8 9]
invalid {"hello":true, "bye":false, null}
invalid {"hello":true, "bye":false, null:null}
invalid "hi
invalid "hi"""
invalid {"hi": "bye"]
invalid "\uD800\uD800"
invalid "\uD800\uDBFF"
invalid "\UD834\UDD1E"
invalid "\uDB00"
invalid "\uDB00\uDBFF"
valid "\uFFFE"
valid "\uFFFF"
invalid .
valid ""
valid []
valid {}
invalid +.
valid 0.5
invalid 0.e1
valid {"1":{}}
valid {"1":2}
valid {"1":2, "2.5" : [3, 4, {}, {"5": ["6"]}]}
valid {"1":2, "2.5" : [3, 4, {}, {"5": ["6"], "7" :[8 ]}]}
valid 1234
valid -1234
valid {"1":2, "3":4}
invalid +1234
invalid ++1234
valid 123.456e142
valid 123.456e-142
valid 123.456e+142
invalid 123.e-142
valid "1\u2000"
valid "1\u20001"
valid 2
invalid .246e-142
invalid .2e-142
valid 3
invalid .3
valid "3"
valid [3]
invalid +3.
valid 3.2e+1
valid [3, [4]]
valid [3, [4, [5]]]
valid [3, [4, [5], 6]]
valid [3, [4, [5], 6], 7]
valid [3, [4, [5], 6], 7, 8]
valid [3, [4, [5], 6], 7, 8, 9]
invalid +3.5
invalid .3e
invalid .3e1
invalid .3e-1
invalid .3e+1
invalid 3.e1
invalid 3.e+1
valid 3e+1
invalid .5
invalid +.5
invalid .5e+1
valid [ 7]
valid [7 ]
valid [7]
invalid .e-14234
valid "hello"
valid ["hello"]
valid ["hello", "bye"]
valid ["hello", "bye\n"]
valid ["hello", "bye\n\r\t"]
valid ["hello", "bye\n\r\t\b"]
valid ["hello", "bye\n\r\t\b",true]
valid ["hello", "bye\n\r\t\b",true , false]
valid ["hello", "bye\n\r\t\b",true , false, null]
invalid ["hello", "bye\n\r\t\v"]
valid {"hello":true}
valid {"hello":true, "bye":false}
valid {"hello":true, "bye":false, "foo":["one","two","three"]}
valid "hi"
valid ["hi"]
valid ["hi", "bye"]
valid {"hi": "bye"}
valid ["hi", "bye", 3]
valid ["hi", "bye[", 3]
valid "\u0007"
valid "\u0008"
valid "\u0009"
valid "\u0010"
valid "\u0020"
valid "\u10000"
valid "\u1234"
valid "\u99999"
valid "\ud800\udc00"
valid "\uD800\uDC00"
valid "\uD834\uDD1E"
valid "\uDBFF\uDFFF"
valid "\uFFFD"
valid "\uFFFF"
invalid hello
valid [32, 1]
invalid [32,
valid "\uD800\uDC00"
valid "\n"
valid "hello"
valid "hello\u0009world"
valid "hello"
valid "hello\n"
valid "hello"
valid 3
invalid 3.
invalid .3
valid 0.3
invalid 0.3e
invalid 0.3e+
valid 0.3e+5
valid 0.3e-5
valid 0.3e5
valid "hello"
invalid +3
valid -3
invalid -3.
valid -3.1
invalid .5
invalid 5.
invalid 5.e1
valid 0.5
invalid .3e1
invalid .3e+1
invalid .3e-1
invalid .3e-1 .5
invalid .3e-1.5
invalid .3e+1.5
invalid .3e+.
invalid .3e+.5
invalid .3e+1.5
invalid 9.3e+1.5
invalid 9.e+1.5
invalid 9.e+
invalid 9.e+1
valid "\""
valid "\"3.5"
valid "\"."
invalid "\".".
valid "\"....."
invalid "\"\"\"\"""
invalid ["\"\"\"\"", .5]
invalid [.5]
valid ["\"\"\"\"", 0.5]
invalid ["\"\"\"\"", .5]
invalid ["\"\"\"\"",.5]
invalid ["\"",.5]
invalid ["\".5",.5]
invalid ["\".5",".5\"".5]
invalid ["\".5",".5\"", .5]
invalid ["\".5",".5\"",.5]
valid ["\".5",".5\"",0.5]
invalid {"key":/*comment*/"value"}
invalid {"key":/*comment"value"}
invalid {"key":"value"}/*
invalid {"key":"value"}/**/
invalid {"key":"value"}/***/
invalid {"key":"value"}/**//
invalid {"key":"value"}/**///
invalid {"key":"value"}/**///----
invalid {"key":"value"}#
invalid {"key":"value"}#{
invalid {"key":"value"}#{}
invalid {"key":"value"}#,
invalid {"key":"value"/**/, "k2":"v2"}
valid "\u0027"
invalid "hello\'"
invalid 'hello\''
invalid 'hello'
invalid 'hell\'o'
invalid '\'hello'
invalid '\'hello\''
invalid \'hello\'
invalid 'hello\'
invalid ['hello\']
invalid ['hello\'']
invalid ['hello"']
invalid ['hello\"']
invalid ['hello"o']
invalid ['"']
invalid '"'
invalid '"hello"'
invalid '"hello'
invalid '"hi"'
valid [ 1 , 2 , 3 ]
invalid nil
invalid fals
invalid falsify
invalid falsetto
invalid truism
invalid {"key"
invalid {"key","key2":value}
invalid "\u0000"
"￾"
"￿"
""
[]
{}
0.5
{"1":{}}
{"1":2}
{"1":2,"2.5":[3,4,{},{"5":["6"]}]}
{"1":2,"2.5":[3,4,{},{"5":["6"],"7":[8]}]}
1234
-1234
{"1":2,"3":4}
1.23456e+144
1.23456e-140
1.23456e+144
"1 "
"1 1"
2
3
"3"
[3]
32
[3,[4]]
[3,[4,[5]]]
[3,[4,[5],6]]
[3,[4,[5],6],7]
[3,[4,[5],6],7,8]
[3,[4,[5],6],7,8,9]
30
[7]
[7]
[7]
"hello"
["hello"]
["hello","bye"]
["hello","bye\n"]
["hello","bye\n\r\t"]
["hello","bye\n\r\t\b"]
["hello","bye\n\r\t\b",true]
["hello","bye\n\r\t\b",true,false]
["hello","bye\n\r\t\b",true,false,null]
{"hello":true}
{"hello":true,"bye":false}
{"hello":true,"bye":false,"foo":["one","two","three"]}
"hi"
["hi"]
["hi","bye"]
{"hi":"bye"}
["hi","bye",3]
["hi","bye[",3]
"\u0007"
"\b"
"\t"
"\u0010"
" "
"က0"
"ሴ"
"香9"
"𐀀"
"𐀀"
"𝄞"
"􏿿"
"�"
"￿"
[32,1]
"𐀀"
"\n"
"hello"
"hello\tworld"
"hello"
"hello\n"
"hello"
3
0.3
30000
3e-06
30000
"hello"
-3
-3.1
0.5
"\""
"\"3.5"
"\"."
"\"....."
["\"\"\"\"",0.5]
["\".5",".5\"",0.5]
"'"
[1,2,3]
/* Copyright (C) CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
* SPDX-License-Identifier: GPL-3.0-or-later
*/
/**
* Cleanup attributes.
* @cond internal
*/
#pragma once
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#define auto_free __attribute__((cleanup(_cleanup_free)))
static inline void _cleanup_free(const void *p) {
free(*(char **)p);
}
#define auto_close __attribute__((cleanup(_cleanup_close)))
static inline void _cleanup_close(int *p) {
if (*p != -1) close(*p);
}
#define auto_fclose __attribute__((cleanup(_cleanup_fclose)))
static inline void _cleanup_fclose(FILE **p) {
if (*p) fclose(*p);
}
/* @endcond */
/* Dummy file, no real configuration here */
/* Dummy file, no real configuration here
* SPDX-License-Identifier: GPL-3.0-or-later */
#define HAVE_ATTRIBUTE_COLD 1
#define HAVE_ATTRIBUTE_NORETURN 1
#define HAVE_ATTRIBUTE_PURE 1
#define HAVE_ATTRIBUTE_UNUSED 1
\ No newline at end of file
#define HAVE_ATTRIBUTE_UNUSED 1
#define HAVE_ATTRIBUTE_NONNULL 1
SPDX-License-Identifier: MIT
SPDX-URL: https://spdx.org/licenses/MIT.html
License-Text:
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
......
SPDX-License-Identifier: CC0-1.0
SPDX-URL: https://spdx.org/licenses/CC0-1.0.html
License-Text:
Statement of Purpose
The laws of most jurisdictions throughout the world automatically confer exclusive Copyright and Related Rights (defined below) upon the creator and subsequent owner(s) (each and all, an "owner") of an original work of authorship and/or a database (each, a "Work").
......
SPDX-License-Identifier: LGPL-2.1-or-later
SPDX-URL: http://spdx.org/licenses/LGPL-2.1
License-Text:
GNU LESSER GENERAL PUBLIC LICENSE
Version 2.1, February 1999
......
/* Copyright (C) CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include "contrib/mempattern.h"
#include "contrib/ucw/mempool.h"
static void mm_nofree(void *p)
{
/* nop */
}
static void *mm_malloc(void *ctx, size_t n)
{
(void)ctx;
return malloc(n);
}
void *mm_alloc(knot_mm_t *mm, size_t size)
{
if (mm) {
return mm->alloc(mm->ctx, size);
} else {
return malloc(size);
}
}
void *mm_calloc(knot_mm_t *mm, size_t nmemb, size_t size)
{
if (nmemb == 0 || size == 0) {
return NULL;
}
if (mm) {
size_t total_size = nmemb * size;
if (total_size / nmemb != size) { // Overflow check
return NULL;
}
void *mem = mm_alloc(mm, total_size);
if (mem == NULL) {
return NULL;
}
return memset(mem, 0, total_size);
} else {
return calloc(nmemb, size);
}
}
void *mm_realloc(knot_mm_t *mm, void *what, size_t size, size_t prev_size)
{
if (mm) {
void *p = mm->alloc(mm->ctx, size);
if (p == NULL) {
return NULL;
} else {
if (what) {
memcpy(p, what,
prev_size < size ? prev_size : size);
}
mm_free(mm, what);
return p;
}
} else {
return realloc(what, size);
}
}
char *mm_strdup(knot_mm_t *mm, const char *s)
{
if (s == NULL) {
return NULL;
}
if (mm) {
size_t len = strlen(s) + 1;
void *mem = mm_alloc(mm, len);
if (mem == NULL) {
return NULL;
}
return memcpy(mem, s, len);
} else {
return strdup(s);
}
}
void mm_free(knot_mm_t *mm, void *what)
{
if (mm) {
if (mm->free) {
mm->free(what);
}
} else {
free(what);
}
}
void mm_ctx_init(knot_mm_t *mm)
{
mm->ctx = NULL;
mm->alloc = mm_malloc;
mm->free = free;
}
void mm_ctx_mempool(knot_mm_t *mm, size_t chunk_size)
{
mm->ctx = mp_new(chunk_size);
mm->alloc = (knot_mm_alloc_t)mp_alloc;
mm->free = mm_nofree;
}
/* Code in addition to Knot's mempattern. */
void *mm_malloc_aligned(void *ctx, size_t n)
{
size_t alignment = (uintptr_t)ctx;
void *res;
int err = posix_memalign(&res, alignment, n);
if (err == 0) {
return res;
} else {
assert(err == -1 && errno == ENOMEM);
return NULL;
}
}
knot_mm_t * mm_ctx_mempool2(size_t chunk_size)
{
knot_mm_t pool_tmp;
mm_ctx_mempool(&pool_tmp, chunk_size);
knot_mm_t *pool = mm_alloc(&pool_tmp, sizeof(*pool));
if (!pool) {
mp_delete(pool_tmp.ctx);
return NULL;
}
memcpy(pool, &pool_tmp, sizeof(*pool));
return pool;
}
/* Copyright (C) CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/*!
* \brief Memory allocation related functions.
*/
#pragma once
#include <libknot/mm_ctx.h>
#include "contrib/ucw/mempool.h"
#include "lib/defines.h"
#include <assert.h>
#include <stdint.h>
/*! \brief Default memory block size. */
#define MM_DEFAULT_BLKSIZE 4096
/*! \brief Allocs using 'mm' if any, uses system malloc() otherwise. */
KR_EXPORT
void *mm_alloc(knot_mm_t *mm, size_t size);
/*! \brief Callocs using 'mm' if any, uses system calloc() otherwise. */
void *mm_calloc(knot_mm_t *mm, size_t nmemb, size_t size);
/*! \brief Reallocs using 'mm' if any, uses system realloc() otherwise. */
KR_EXPORT
void *mm_realloc(knot_mm_t *mm, void *what, size_t size, size_t prev_size);
/*! \brief Strdups using 'mm' if any, uses system strdup() otherwise. */
char *mm_strdup(knot_mm_t *mm, const char *s);
/*! \brief Free using 'mm' if any, uses system free() otherwise. */
KR_EXPORT
void mm_free(knot_mm_t *mm, void *what);
/*! \brief Initialize default memory allocation context. */
void mm_ctx_init(knot_mm_t *mm);
/*! \brief Memory pool context. */
void mm_ctx_mempool(knot_mm_t *mm, size_t chunk_size);
/* API in addition to Knot's mempattern. */
/*! \brief New memory pool context, allocated on itself. */
KR_EXPORT knot_mm_t * mm_ctx_mempool2(size_t chunk_size);
/*! \brief Delete a memory pool. OK to call on a non-pool. */
static inline void mm_ctx_delete(knot_mm_t *mm)
{
/* The mp_alloc comparison bears a risk of missing the private symbol from knot. */
if (mm && mm->ctx && mm->alloc == (knot_mm_alloc_t)mp_alloc)
mp_delete(mm->ctx);
}
/*! \brief Readability: avoid const-casts in code. */
static inline void free_const(const void *what)
{
free((void *)what);
}
/*! \brief posix_memalign() wrapper. */
void *mm_malloc_aligned(void *ctx, size_t n);
/*! \brief Initialize mm with malloc+free with specified alignment (a power of two). */
static inline void mm_ctx_init_aligned(knot_mm_t *mm, size_t alignment)
{
assert(__builtin_popcount(alignment) == 1);
mm_ctx_init(mm);
mm->ctx = (void *)(uintptr_t)alignment; /*< roundabout to satisfy linters */
/* posix_memalign() doesn't allow alignment < sizeof(void*),
* and there's no point in using it for small values anyway,
* as plain malloc() guarantees at least max_align_t. */
if (alignment > sizeof(max_align_t)) {
mm->alloc = mm_malloc_aligned;
}
}
# contrib
# SPDX-License-Identifier: GPL-3.0-or-later
contrib_src = files([
'ccan/asprintf/asprintf.c',
'ccan/json/json.c',
'ucw/mempool.c',
'ucw/mempool-fmt.c',
'mempattern.c',
'murmurhash3/murmurhash3.c',
'base32hex.c',
'base64.c',
'base64url.c',
'openbsd/siphash.c',
])
contrib_inc = include_directories('.', '..')
contrib_lib = static_library(
'contrib',
contrib_src,
include_directories: contrib_inc,
dependencies: libknot,
)
contrib_dep = declare_dependency(
include_directories: contrib_inc,
link_with: contrib_lib,
)
/* This is MurmurHash3. The original C++ code was placed in the public domain
* by its author, Austin Appleby. */
* by its author, Austin Appleby.
*
* SPDX-License-Identifier: CC0-1.0
* Source: https://github.com/aappleby/smhasher/blob/master/src/MurmurHash3.cpp */
#include "murmurhash3.h"
#include "string.h"
static inline uint32_t fmix(uint32_t h)
{
......@@ -32,12 +36,10 @@ uint32_t hash(const char* data, size_t len_)
//----------
// body
const uint32_t * blocks = (const uint32_t*) (data + nblocks * 4);
int i;
for(i = -nblocks; i; i++)
for(int i = 0; i < nblocks; ++i)
{
uint32_t k1 = blocks[i];
uint32_t k1;
memcpy(&k1, data + i * sizeof(k1), sizeof(k1));
k1 *= c1;
k1 = rotl32(k1, 15);
......
/* SPDX-License-Identifier: CC0-1.0
* Source: https://github.com/aappleby/smhasher/blob/master/src/MurmurHash3.cpp */
#pragma once
#include <stdlib.h>
......
SPDXVersion: SPDX-2.1
DataLicense: CC0-1.0
SPDXID: SPDXRef-DOCUMENT
DocumentName: ccan-json
DocumentNamespace: http://spdx.org/spdxdocs/spdx-v2.1-d9b4db4c-062f-4add-89b6-f603224f5a2c
PackageName: json
PackageDownloadLocation: git+https://github.com/aappleby/smhasher.git@73e075b203d9c76cd1e20d6c8907c2983d653f33#MurmurHash3.cpp
PackageOriginator: Person: Austin Appleby (aappleby@gmail.com)
PackageLicenseDeclared: CC0-1.0
/* $OpenBSD: siphash.c,v 1.6 2017/04/12 17:41:49 deraadt Exp $ */
/*-
* Copyright (c) 2013 Andre Oppermann <andre@FreeBSD.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* SipHash is a family of PRFs SipHash-c-d where the integer parameters c and d
* are the number of compression rounds and the number of finalization rounds.
* A compression round is identical to a finalization round and this round
* function is called SipRound. Given a 128-bit key k and a (possibly empty)
* byte string m, SipHash-c-d returns a 64-bit value SipHash-c-d(k; m).
*
* Implemented from the paper "SipHash: a fast short-input PRF", 2012.09.18,
* by Jean-Philippe Aumasson and Daniel J. Bernstein,
* Permanent Document ID b9a943a805fbfc6fde808af9fc0ecdfa
* https://131002.net/siphash/siphash.pdf
* https://131002.net/siphash/
*/
#include <string.h>
#include "libknot/endian.h"
#include "contrib/openbsd/siphash.h"
static void SipHash_CRounds(SIPHASH_CTX *, int);
static void SipHash_Rounds(SIPHASH_CTX *, int);
void
SipHash_Init(SIPHASH_CTX *ctx, const SIPHASH_KEY *key)
{
uint64_t k0, k1;
k0 = le64toh(key->k0);
k1 = le64toh(key->k1);
ctx->v[0] = 0x736f6d6570736575ULL ^ k0;
ctx->v[1] = 0x646f72616e646f6dULL ^ k1;
ctx->v[2] = 0x6c7967656e657261ULL ^ k0;
ctx->v[3] = 0x7465646279746573ULL ^ k1;
memset(ctx->buf, 0, sizeof(ctx->buf));
ctx->bytes = 0;
}
void
SipHash_Update(SIPHASH_CTX *ctx, int rc, int rf, const void *src, size_t len)
{
const uint8_t *ptr = src;
size_t left, used;
if (len == 0)
return;
used = ctx->bytes % sizeof(ctx->buf);
ctx->bytes += len;
if (used > 0) {
left = sizeof(ctx->buf) - used;
if (len >= left) {
memcpy(&ctx->buf[used], ptr, left);
SipHash_CRounds(ctx, rc);
len -= left;
ptr += left;
} else {
memcpy(&ctx->buf[used], ptr, len);
return;
}
}
while (len >= sizeof(ctx->buf)) {
memcpy(ctx->buf, ptr, sizeof(ctx->buf));
SipHash_CRounds(ctx, rc);
len -= sizeof(ctx->buf);
ptr += sizeof(ctx->buf);
}
if (len > 0)
memcpy(&ctx->buf, ptr, len);
}
uint64_t
SipHash_End(SIPHASH_CTX *ctx, int rc, int rf)
{
uint64_t r;
size_t left, used;
used = ctx->bytes % sizeof(ctx->buf);
left = sizeof(ctx->buf) - used;
memset(&ctx->buf[used], 0, left - 1);
ctx->buf[7] = ctx->bytes;
SipHash_CRounds(ctx, rc);
ctx->v[2] ^= 0xff;
SipHash_Rounds(ctx, rf);
r = (ctx->v[0] ^ ctx->v[1]) ^ (ctx->v[2] ^ ctx->v[3]);
// we do not clean up the context
return htole64(r);
}
uint64_t
SipHash(const SIPHASH_KEY *key, int rc, int rf, const void *src, size_t len)
{
SIPHASH_CTX ctx;
SipHash_Init(&ctx, key);
SipHash_Update(&ctx, rc, rf, src, len);
return (SipHash_End(&ctx, rc, rf));
}
#define SIP_ROTL(x, b) ((x) << (b)) | ( (x) >> (64 - (b)))
static void
SipHash_Rounds(SIPHASH_CTX *ctx, int rounds)
{
while (rounds--) {
ctx->v[0] += ctx->v[1];
ctx->v[2] += ctx->v[3];
ctx->v[1] = SIP_ROTL(ctx->v[1], 13);
ctx->v[3] = SIP_ROTL(ctx->v[3], 16);
ctx->v[1] ^= ctx->v[0];
ctx->v[3] ^= ctx->v[2];
ctx->v[0] = SIP_ROTL(ctx->v[0], 32);
ctx->v[2] += ctx->v[1];
ctx->v[0] += ctx->v[3];
ctx->v[1] = SIP_ROTL(ctx->v[1], 17);
ctx->v[3] = SIP_ROTL(ctx->v[3], 21);
ctx->v[1] ^= ctx->v[2];
ctx->v[3] ^= ctx->v[0];
ctx->v[2] = SIP_ROTL(ctx->v[2], 32);
}
}
static void
SipHash_CRounds(SIPHASH_CTX *ctx, int rounds)
{
uint64_t tmp;
memcpy(&tmp, ctx->buf, sizeof(tmp));
uint64_t m = le64toh(tmp);
ctx->v[3] ^= m;
SipHash_Rounds(ctx, rounds);
ctx->v[0] ^= m;
}
/*-
* Copyright (c) 2013 Andre Oppermann <andre@FreeBSD.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $OpenBSD: siphash.h,v 1.3 2015/02/20 11:51:03 tedu Exp $
*/
/*
* SipHash is a family of pseudorandom functions (a.k.a. keyed hash functions)
* optimized for speed on short messages returning a 64bit hash/digest value.
*
* The number of rounds is defined during the initialization:
* SipHash24_Init() for the fast and reasonable strong version
* SipHash48_Init() for the strong version (half as fast)
*
* struct SIPHASH_CTX ctx;
* SipHash24_Init(&ctx);
* SipHash_SetKey(&ctx, "16bytes long key");
* SipHash_Update(&ctx, pointer_to_string, length_of_string);
* SipHash_End(&ctx);
*/
#ifndef _SIPHASH_H_
#define _SIPHASH_H_
#include <stddef.h>
#include <stdint.h>
#define SIPHASH_BLOCK_LENGTH 8
#define SIPHASH_KEY_LENGTH 16
#define SIPHASH_DIGEST_LENGTH 8
typedef struct _SIPHASH_CTX {
uint64_t v[4];
uint8_t buf[SIPHASH_BLOCK_LENGTH];
uint32_t bytes;
} SIPHASH_CTX;
typedef struct {
uint64_t k0;
uint64_t k1;
} SIPHASH_KEY;
void SipHash_Init(SIPHASH_CTX *, const SIPHASH_KEY *);
void SipHash_Update(SIPHASH_CTX *, int, int, const void *, size_t);
uint64_t SipHash_End(SIPHASH_CTX *, int, int);
uint64_t SipHash(const SIPHASH_KEY *, int, int, const void *, size_t);
#define SipHash24_Init(_c, _k) SipHash_Init((_c), (_k))
#define SipHash24_Update(_c, _p, _l) SipHash_Update((_c), 2, 4, (_p), (_l))
#define SipHash24_End(_d) SipHash_End((_d), 2, 4)
#define SipHash24(_k, _p, _l) SipHash((_k), 2, 4, (_p), (_l))
#define SipHash48_Init(_c, _k) SipHash_Init((_c), (_k))
#define SipHash48_Update(_c, _p, _l) SipHash_Update((_c), 4, 8, (_p), (_l))
#define SipHash48_End(_d) SipHash_End((_d), 4, 8)
#define SipHash48(_k, _p, _l) SipHash((_k), 4, 8, (_p), (_l))
#endif /* _SIPHASH_H_ */
......@@ -2,6 +2,8 @@
* UCW Library -- Generic allocators
*
* (c) 2014 Martin Mares <mj@ucw.cz>
* SPDX-License-Identifier: LGPL-2.1-or-later
* Source: https://www.ucw.cz/libucw/
*/
#ifndef _UCW_ALLOC_H
......@@ -22,7 +24,7 @@ struct ucw_allocator {
/**
* [[std]]
* This allocator uses <<basics:xmalloc()>>, <<basics:xrealloc()>> and <<basics:xfree()>>. The memory
* it allocates is left unitialized.
* it allocates is left uninitialized.
**/
extern struct ucw_allocator ucw_allocator_std;
......
......@@ -4,8 +4,8 @@
* (c) 1997--2012 Martin Mares <mj@ucw.cz>
* (c) 2006 Robert Spalek <robert@ucw.cz>
*
* This software may be freely distributed and used according to the terms
* of the GNU Lesser General Public License.
* SPDX-License-Identifier: LGPL-2.1-or-later
* Source: https://www.ucw.cz/libucw/
*/
#ifndef _UCW_CONFIG_H
......@@ -38,7 +38,12 @@ typedef int32_t s32; /** Exactly 32 bits, signed **/
typedef uint64_t u64; /** Exactly 64 bits, unsigned **/
typedef int64_t s64; /** Exactly 64 bits, signed **/
#ifndef uint /* Redefining typedef is a C11 feature. */
typedef unsigned int uint; /** A better pronounceable alias for `unsigned int` **/
#define uint uint
#endif
typedef s64 timestamp_t; /** Milliseconds since an unknown epoch **/
// FIXME: This should be removed soon
......