Browse Source

Merge branch 'gbt' into bfgminer

Luke Dashjr 13 years ago
parent
commit
9fdebf9fdb

+ 3 - 0
.gitmodules

@@ -0,0 +1,3 @@
+[submodule "libblkmaker"]
+	path = libblkmaker
+	url = git://gitorious.org/bitcoin/libblkmaker.git

+ 9 - 8
Makefile.am

@@ -1,20 +1,14 @@
 
 ACLOCAL_AMFLAGS = -I m4
 
-if WANT_JANSSON
-JANSSON_INCLUDES= -I$(top_srcdir)/compat/jansson
-else
-JANSSON_INCLUDES=
-endif
-
 EXTRA_DIST	= example.conf m4/gnulib-cache.m4 linux-usb-bfgminer \
 		  api-example.php miner.php	\
 		  API.class API.java api-example.c windows-build.txt \
 		  bitstreams/* API-README FPGA-README SCRYPT-README
 
-SUBDIRS		= lib compat ccan
+SUBDIRS		= lib ccan
 
-INCLUDES	= $(PTHREAD_FLAGS) -fno-strict-aliasing $(JANSSON_INCLUDES) $(USB_FLAGS)
+INCLUDES	= $(PTHREAD_FLAGS) -fno-strict-aliasing $(USB_FLAGS)
 
 bin_PROGRAMS	= bfgminer
 
@@ -35,6 +29,13 @@ bfgminer_SOURCES	+= elist.h miner.h compat.h bench_block.h	\
 		   util.c uthash.h logging.h			\
 		   sha2.c sha2.h api.c
 
+if NEED_LIBBLKMAKER
+SUBDIRS           += libblkmaker
+bfgminer_CPPFLAGS += -Ilibblkmaker
+bfgminer_LDFLAGS  += -Llibblkmaker/.libs -Wl,-rpath,libblkmaker/.libs
+bfgminer_LDADD    += -lblkmaker-0.1 -lblkmaker_jansson-0.1
+endif
+
 bfgminer_SOURCES	+= logging.c
 
 # GPU sources, TODO: make them selectable

+ 2 - 2
README

@@ -40,8 +40,7 @@ Dependencies:
 	pkg-config		http://www.freedesktop.org/wiki/Software/pkg-config
 	libtool			http://www.gnu.org/software/libtool/
 
-	jansson			http://www.digip.org/jansson/
-	(jansson is included in-tree and not necessary)
+	jansson 2.0+    http://www.digip.org/jansson/
 
 	yasm 1.0.1+ http://yasm.tortall.net/
 	(yasm is optional, gives assembly routines for CPU mining)
@@ -143,6 +142,7 @@ Options for both config file and command line:
 --log|-l <arg>      Interval in seconds between log output (default: 5)
 --monitor|-m <arg>  Use custom pipe cmd for output messages
 --net-delay         Impose small delays in networking to not overload slow routers
+--no-gbt            Disable getblocktemplate support
 --no-longpoll       Disable X-Long-Polling support
 --no-pool-disable   Do not automatically disable pools that continually reject shares
 --no-submit-stale   Don't submit shares if they are detected as stale

+ 1 - 1
api-example.c

@@ -8,7 +8,7 @@
  */
 
 /* Compile:
- *   gcc api-example.c -I compat/jansson -o bfgminer-api
+ *   gcc api-example.c -o bfgminer-api
  */
 
 #include "config.h"

+ 5 - 0
autogen.sh

@@ -4,6 +4,11 @@ bs_dir="$(dirname $(readlink -f $0))"
 rm -rf "${bs_dir}"/autom4te.cache
 rm -f "${bs_dir}"/aclocal.m4 "${bs_dir}"/ltmain.sh
 
+if test -z "$NOSUBMODULES" ; then
+	echo 'Getting submodules...'
+	git submodule update --init
+fi
+
 echo 'Running autoreconf -if...'
 autoreconf -if || exit 1
 if test -z "$NOCONFIGURE" ; then

+ 0 - 7
compat/Makefile.am

@@ -1,7 +0,0 @@
-
-if WANT_JANSSON
-SUBDIRS	= jansson
-else
-SUBDIRS	=
-endif
-

+ 0 - 3
compat/jansson/.gitignore

@@ -1,3 +0,0 @@
-
-libjansson.a
-

+ 0 - 19
compat/jansson/LICENSE

@@ -1,19 +0,0 @@
-Copyright (c) 2009, 2010 Petri Lehtinen <petri@digip.org>
-
-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.

+ 0 - 21
compat/jansson/Makefile.am

@@ -1,21 +0,0 @@
-
-noinst_LIBRARIES	= libjansson.a
-
-libjansson_a_SOURCES	= \
-			  config.h		\
-			  jansson_config.h	\
-			  dump.c		\
-			  hashtable.c		\
-			  hashtable.h		\
-			  jansson.h		\
-			  jansson_private.h	\
-			  load.c		\
-			  strbuffer.c		\
-			  strbuffer.h		\
-			  utf.c			\
-			  utf.h			\
-			  util.h		\
-			  value.c		\
-			  memory.c		\
-			  error.c
-

+ 0 - 73
compat/jansson/config.h

@@ -1,73 +0,0 @@
-/* config.h.  Generated from config.h.in by configure.  */
-/* config.h.in.  Generated from configure.ac by autoheader.  */
-
-/* Define to 1 if you have the <dlfcn.h> header file. */
-#define HAVE_DLFCN_H 1
-
-/* Define to 1 if you have the <inttypes.h> header file. */
-#define HAVE_INTTYPES_H 1
-
-/* Define to 1 if you have the <memory.h> header file. */
-#define HAVE_MEMORY_H 1
-
-/* Define to 1 if you have the <stdint.h> header file. */
-#define HAVE_STDINT_H 1
-
-/* Define to 1 if you have the <stdlib.h> header file. */
-#define HAVE_STDLIB_H 1
-
-/* Define to 1 if you have the <strings.h> header file. */
-#define HAVE_STRINGS_H 1
-
-/* Define to 1 if you have the <string.h> header file. */
-#define HAVE_STRING_H 1
-
-/* Define to 1 if you have the <sys/stat.h> header file. */
-#define HAVE_SYS_STAT_H 1
-
-/* Define to 1 if you have the <sys/types.h> header file. */
-#define HAVE_SYS_TYPES_H 1
-
-/* Define to 1 if you have the <unistd.h> header file. */
-#define HAVE_UNISTD_H 1
-
-/* Define to the sub-directory in which libtool stores uninstalled libraries.
-   */
-#define LT_OBJDIR ".libs/"
-
-/* Name of package */
-#define PACKAGE "jansson"
-
-/* Define to the address where bug reports for this package should be sent. */
-#define PACKAGE_BUGREPORT "petri@digip.org"
-
-/* Define to the full name of this package. */
-#define PACKAGE_NAME "jansson"
-
-/* Define to the full name and version of this package. */
-#define PACKAGE_STRING "jansson 1.3"
-
-/* Define to the one symbol short name of this package. */
-#define PACKAGE_TARNAME "jansson"
-
-/* Define to the home page for this package. */
-#define PACKAGE_URL ""
-
-/* Define to the version of this package. */
-#define PACKAGE_VERSION "1.3"
-
-/* Define to 1 if you have the ANSI C header files. */
-#define STDC_HEADERS 1
-
-/* Version number of package */
-#define VERSION "1.3"
-
-/* Define to `__inline__' or `__inline' if that's what the C compiler
-   calls it, or to nothing if 'inline' is not supported under any name.  */
-#ifndef __cplusplus
-/* #undef inline */
-#endif
-
-/* Define to the type of a signed integer type of width exactly 32 bits if
-   such a type exists and the standard includes do not define it. */
-/* #undef int32_t */

+ 0 - 465
compat/jansson/dump.c

@@ -1,465 +0,0 @@
-/*
- * Copyright (c) 2009-2011 Petri Lehtinen <petri@digip.org>
- *
- * Jansson is free software; you can redistribute it and/or modify
- * it under the terms of the MIT license. See LICENSE for details.
- */
-
-#define _GNU_SOURCE
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <assert.h>
-
-#include <jansson.h>
-#include "jansson_private.h"
-#include "strbuffer.h"
-#include "utf.h"
-
-#define MAX_INTEGER_STR_LENGTH  100
-#define MAX_REAL_STR_LENGTH     100
-
-typedef int (*dump_func)(const char *buffer, int size, void *data);
-
-struct string
-{
-    char *buffer;
-    int length;
-    int size;
-};
-
-static int dump_to_strbuffer(const char *buffer, int size, void *data)
-{
-    return strbuffer_append_bytes((strbuffer_t *)data, buffer, size);
-}
-
-static int dump_to_file(const char *buffer, int size, void *data)
-{
-    FILE *dest = (FILE *)data;
-    if(fwrite(buffer, size, 1, dest) != 1)
-        return -1;
-    return 0;
-}
-
-/* 32 spaces (the maximum indentation size) */
-static char whitespace[] = "                                ";
-
-static int dump_indent(size_t flags, int depth, int space, dump_func dump, void *data)
-{
-    if(JSON_INDENT(flags) > 0)
-    {
-        int i, ws_count = JSON_INDENT(flags);
-
-        if(dump("\n", 1, data))
-            return -1;
-
-        for(i = 0; i < depth; i++)
-        {
-            if(dump(whitespace, ws_count, data))
-                return -1;
-        }
-    }
-    else if(space && !(flags & JSON_COMPACT))
-    {
-        return dump(" ", 1, data);
-    }
-    return 0;
-}
-
-static int dump_string(const char *str, int ascii, dump_func dump, void *data)
-{
-    const char *pos, *end;
-    int32_t codepoint;
-
-    if(dump("\"", 1, data))
-        return -1;
-
-    end = pos = str;
-    while(1)
-    {
-        const char *text;
-        char seq[13];
-        int length;
-
-        while(*end)
-        {
-            end = utf8_iterate(pos, &codepoint);
-            if(!end)
-                return -1;
-
-            /* mandatory escape or control char */
-            if(codepoint == '\\' || codepoint == '"' || codepoint < 0x20)
-                break;
-
-            /* non-ASCII */
-            if(ascii && codepoint > 0x7F)
-                break;
-
-            pos = end;
-        }
-
-        if(pos != str) {
-            if(dump(str, pos - str, data))
-                return -1;
-        }
-
-        if(end == pos)
-            break;
-
-        /* handle \, ", and control codes */
-        length = 2;
-        switch(codepoint)
-        {
-            case '\\': text = "\\\\"; break;
-            case '\"': text = "\\\""; break;
-            case '\b': text = "\\b"; break;
-            case '\f': text = "\\f"; break;
-            case '\n': text = "\\n"; break;
-            case '\r': text = "\\r"; break;
-            case '\t': text = "\\t"; break;
-            default:
-            {
-                /* codepoint is in BMP */
-                if(codepoint < 0x10000)
-                {
-                    sprintf(seq, "\\u%04x", codepoint);
-                    length = 6;
-                }
-
-                /* not in BMP -> construct a UTF-16 surrogate pair */
-                else
-                {
-                    int32_t first, last;
-
-                    codepoint -= 0x10000;
-                    first = 0xD800 | ((codepoint & 0xffc00) >> 10);
-                    last = 0xDC00 | (codepoint & 0x003ff);
-
-                    sprintf(seq, "\\u%04x\\u%04x", first, last);
-                    length = 12;
-                }
-
-                text = seq;
-                break;
-            }
-        }
-
-        if(dump(text, length, data))
-            return -1;
-
-        str = pos = end;
-    }
-
-    return dump("\"", 1, data);
-}
-
-static int object_key_compare_keys(const void *key1, const void *key2)
-{
-    return strcmp((*(const object_key_t **)key1)->key,
-                  (*(const object_key_t **)key2)->key);
-}
-
-static int object_key_compare_serials(const void *key1, const void *key2)
-{
-    return (*(const object_key_t **)key1)->serial -
-           (*(const object_key_t **)key2)->serial;
-}
-
-static int do_dump(const json_t *json, size_t flags, int depth,
-                   dump_func dump, void *data)
-{
-    int ascii = flags & JSON_ENSURE_ASCII ? 1 : 0;
-
-    switch(json_typeof(json)) {
-        case JSON_NULL:
-            return dump("null", 4, data);
-
-        case JSON_TRUE:
-            return dump("true", 4, data);
-
-        case JSON_FALSE:
-            return dump("false", 5, data);
-
-        case JSON_INTEGER:
-        {
-            char buffer[MAX_INTEGER_STR_LENGTH];
-            int size;
-
-            size = snprintf(buffer, MAX_INTEGER_STR_LENGTH,
-                            "%" JSON_INTEGER_FORMAT,
-                            json_integer_value(json));
-            if(size >= MAX_INTEGER_STR_LENGTH)
-                return -1;
-
-            return dump(buffer, size, data);
-        }
-
-        case JSON_REAL:
-        {
-            char buffer[MAX_REAL_STR_LENGTH];
-            int size;
-
-            size = snprintf(buffer, MAX_REAL_STR_LENGTH, "%.17g",
-                            json_real_value(json));
-            if(size >= MAX_REAL_STR_LENGTH)
-                return -1;
-
-            /* Make sure there's a dot or 'e' in the output. Otherwise
-               a real is converted to an integer when decoding */
-            if(strchr(buffer, '.') == NULL &&
-               strchr(buffer, 'e') == NULL)
-            {
-                if(size + 2 >= MAX_REAL_STR_LENGTH) {
-                    /* No space to append ".0" */
-                    return -1;
-                }
-                buffer[size] = '.';
-                buffer[size + 1] = '0';
-                size += 2;
-            }
-
-            return dump(buffer, size, data);
-        }
-
-        case JSON_STRING:
-            return dump_string(json_string_value(json), ascii, dump, data);
-
-        case JSON_ARRAY:
-        {
-            int i;
-            int n;
-            json_array_t *array;
-
-            /* detect circular references */
-            array = json_to_array(json);
-            if(array->visited)
-                goto array_error;
-            array->visited = 1;
-
-            n = json_array_size(json);
-
-            if(dump("[", 1, data))
-                goto array_error;
-            if(n == 0) {
-                array->visited = 0;
-                return dump("]", 1, data);
-            }
-            if(dump_indent(flags, depth + 1, 0, dump, data))
-                goto array_error;
-
-            for(i = 0; i < n; ++i) {
-                if(do_dump(json_array_get(json, i), flags, depth + 1,
-                           dump, data))
-                    goto array_error;
-
-                if(i < n - 1)
-                {
-                    if(dump(",", 1, data) ||
-                       dump_indent(flags, depth + 1, 1, dump, data))
-                        goto array_error;
-                }
-                else
-                {
-                    if(dump_indent(flags, depth, 0, dump, data))
-                        goto array_error;
-                }
-            }
-
-            array->visited = 0;
-            return dump("]", 1, data);
-
-        array_error:
-            array->visited = 0;
-            return -1;
-        }
-
-        case JSON_OBJECT:
-        {
-            json_object_t *object;
-            void *iter;
-            const char *separator;
-            int separator_length;
-
-            if(flags & JSON_COMPACT) {
-                separator = ":";
-                separator_length = 1;
-            }
-            else {
-                separator = ": ";
-                separator_length = 2;
-            }
-
-            /* detect circular references */
-            object = json_to_object(json);
-            if(object->visited)
-                goto object_error;
-            object->visited = 1;
-
-            iter = json_object_iter((json_t *)json);
-
-            if(dump("{", 1, data))
-                goto object_error;
-            if(!iter) {
-                object->visited = 0;
-                return dump("}", 1, data);
-            }
-            if(dump_indent(flags, depth + 1, 0, dump, data))
-                goto object_error;
-
-            if(flags & JSON_SORT_KEYS || flags & JSON_PRESERVE_ORDER)
-            {
-                const object_key_t **keys;
-                size_t size, i;
-                int (*cmp_func)(const void *, const void *);
-
-                size = json_object_size(json);
-                keys = jsonp_malloc(size * sizeof(object_key_t *));
-                if(!keys)
-                    goto object_error;
-
-                i = 0;
-                while(iter)
-                {
-                    keys[i] = jsonp_object_iter_fullkey(iter);
-                    iter = json_object_iter_next((json_t *)json, iter);
-                    i++;
-                }
-                assert(i == size);
-
-                if(flags & JSON_SORT_KEYS)
-                    cmp_func = object_key_compare_keys;
-                else
-                    cmp_func = object_key_compare_serials;
-
-                qsort(keys, size, sizeof(object_key_t *), cmp_func);
-
-                for(i = 0; i < size; i++)
-                {
-                    const char *key;
-                    json_t *value;
-
-                    key = keys[i]->key;
-                    value = json_object_get(json, key);
-                    assert(value);
-
-                    dump_string(key, ascii, dump, data);
-                    if(dump(separator, separator_length, data) ||
-                       do_dump(value, flags, depth + 1, dump, data))
-                    {
-                        jsonp_free(keys);
-                        goto object_error;
-                    }
-
-                    if(i < size - 1)
-                    {
-                        if(dump(",", 1, data) ||
-                           dump_indent(flags, depth + 1, 1, dump, data))
-                        {
-                            jsonp_free(keys);
-                            goto object_error;
-                        }
-                    }
-                    else
-                    {
-                        if(dump_indent(flags, depth, 0, dump, data))
-                        {
-                            jsonp_free(keys);
-                            goto object_error;
-                        }
-                    }
-                }
-
-                jsonp_free(keys);
-            }
-            else
-            {
-                /* Don't sort keys */
-
-                while(iter)
-                {
-                    void *next = json_object_iter_next((json_t *)json, iter);
-
-                    dump_string(json_object_iter_key(iter), ascii, dump, data);
-                    if(dump(separator, separator_length, data) ||
-                       do_dump(json_object_iter_value(iter), flags, depth + 1,
-                               dump, data))
-                        goto object_error;
-
-                    if(next)
-                    {
-                        if(dump(",", 1, data) ||
-                           dump_indent(flags, depth + 1, 1, dump, data))
-                            goto object_error;
-                    }
-                    else
-                    {
-                        if(dump_indent(flags, depth, 0, dump, data))
-                            goto object_error;
-                    }
-
-                    iter = next;
-                }
-            }
-
-            object->visited = 0;
-            return dump("}", 1, data);
-
-        object_error:
-            object->visited = 0;
-            return -1;
-        }
-
-        default:
-            /* not reached */
-            return -1;
-    }
-}
-
-
-char *json_dumps(const json_t *json, size_t flags)
-{
-    strbuffer_t strbuff;
-    char *result;
-
-    if(!(flags & JSON_ENCODE_ANY)) {
-        if(!json_is_array(json) && !json_is_object(json))
-           return NULL;
-    }
-
-    if(strbuffer_init(&strbuff))
-        return NULL;
-
-    if(do_dump(json, flags, 0, dump_to_strbuffer, (void *)&strbuff)) {
-        strbuffer_close(&strbuff);
-        return NULL;
-    }
-
-    result = jsonp_strdup(strbuffer_value(&strbuff));
-    strbuffer_close(&strbuff);
-
-    return result;
-}
-
-int json_dumpf(const json_t *json, FILE *output, size_t flags)
-{
-    if(!(flags & JSON_ENCODE_ANY)) {
-        if(!json_is_array(json) && !json_is_object(json))
-           return -1;
-    }
-
-    return do_dump(json, flags, 0, dump_to_file, (void *)output);
-}
-
-int json_dump_file(const json_t *json, const char *path, size_t flags)
-{
-    int result;
-
-    FILE *output = fopen(path, "w");
-    if(!output)
-        return -1;
-
-    result = json_dumpf(json, output, flags);
-
-    fclose(output);
-    return result;
-}

+ 0 - 62
compat/jansson/error.c

@@ -1,62 +0,0 @@
-#include <string.h>
-#include "jansson_private.h"
-
-void jsonp_error_init(json_error_t *error, const char *source)
-{
-    if(error)
-    {
-        error->text[0] = '\0';
-        error->line = -1;
-        error->column = -1;
-        error->position = 0;
-        if(source)
-            jsonp_error_set_source(error, source);
-        else
-            error->source[0] = '\0';
-    }
-}
-
-void jsonp_error_set_source(json_error_t *error, const char *source)
-{
-    size_t length;
-
-    if(!error || !source)
-        return;
-
-    length = strlen(source);
-    if(length < JSON_ERROR_SOURCE_LENGTH)
-        strcpy(error->source, source);
-    else {
-        size_t extra = length - JSON_ERROR_SOURCE_LENGTH + 4;
-        strcpy(error->source, "...");
-        strcpy(error->source + 3, source + extra);
-    }
-}
-
-void jsonp_error_set(json_error_t *error, int line, int column,
-                     size_t position, const char *msg, ...)
-{
-    va_list ap;
-
-    va_start(ap, msg);
-    jsonp_error_vset(error, line, column, position, msg, ap);
-    va_end(ap);
-}
-
-void jsonp_error_vset(json_error_t *error, int line, int column,
-                      size_t position, const char *msg, va_list ap)
-{
-    if(!error)
-        return;
-
-    if(error->text[0] != '\0') {
-        /* error already set */
-        return;
-    }
-
-    error->line = line;
-    error->column = column;
-    error->position = position;
-
-    vsnprintf(error->text, JSON_ERROR_TEXT_LENGTH, msg, ap);
-}

+ 0 - 372
compat/jansson/hashtable.c

@@ -1,372 +0,0 @@
-/*
- * Copyright (c) 2009-2011 Petri Lehtinen <petri@digip.org>
- *
- * This library is free software; you can redistribute it and/or modify
- * it under the terms of the MIT license. See LICENSE for details.
- */
-
-#include <stdlib.h>
-#include <jansson_config.h>   /* for JSON_INLINE */
-#include "jansson_private.h"  /* for container_of() */
-#include "hashtable.h"
-
-typedef struct hashtable_list list_t;
-typedef struct hashtable_pair pair_t;
-typedef struct hashtable_bucket bucket_t;
-
-#define list_to_pair(list_)  container_of(list_, pair_t, list)
-
-static JSON_INLINE void list_init(list_t *list)
-{
-    list->next = list;
-    list->prev = list;
-}
-
-static JSON_INLINE void list_insert(list_t *list, list_t *node)
-{
-    node->next = list;
-    node->prev = list->prev;
-    list->prev->next = node;
-    list->prev = node;
-}
-
-static JSON_INLINE void list_remove(list_t *list)
-{
-    list->prev->next = list->next;
-    list->next->prev = list->prev;
-}
-
-static JSON_INLINE int bucket_is_empty(hashtable_t *hashtable, bucket_t *bucket)
-{
-    return bucket->first == &hashtable->list && bucket->first == bucket->last;
-}
-
-static void insert_to_bucket(hashtable_t *hashtable, bucket_t *bucket,
-                             list_t *list)
-{
-    if(bucket_is_empty(hashtable, bucket))
-    {
-        list_insert(&hashtable->list, list);
-        bucket->first = bucket->last = list;
-    }
-    else
-    {
-        list_insert(bucket->first, list);
-        bucket->first = list;
-    }
-}
-
-static size_t primes[] = {
-    5, 13, 23, 53, 97, 193, 389, 769, 1543, 3079, 6151, 12289, 24593,
-    49157, 98317, 196613, 393241, 786433, 1572869, 3145739, 6291469,
-    12582917, 25165843, 50331653, 100663319, 201326611, 402653189,
-    805306457, 1610612741
-};
-static const size_t num_primes = sizeof(primes) / sizeof(size_t);
-
-static JSON_INLINE size_t num_buckets(hashtable_t *hashtable)
-{
-    return primes[hashtable->num_buckets];
-}
-
-
-static pair_t *hashtable_find_pair(hashtable_t *hashtable, bucket_t *bucket,
-                                   const void *key, size_t hash)
-{
-    list_t *list;
-    pair_t *pair;
-
-    if(bucket_is_empty(hashtable, bucket))
-        return NULL;
-
-    list = bucket->first;
-    while(1)
-    {
-        pair = list_to_pair(list);
-        if(pair->hash == hash && hashtable->cmp_keys(pair->key, key))
-            return pair;
-
-        if(list == bucket->last)
-            break;
-
-        list = list->next;
-    }
-
-    return NULL;
-}
-
-/* returns 0 on success, -1 if key was not found */
-static int hashtable_do_del(hashtable_t *hashtable,
-                            const void *key, size_t hash)
-{
-    pair_t *pair;
-    bucket_t *bucket;
-    size_t index;
-
-    index = hash % num_buckets(hashtable);
-    bucket = &hashtable->buckets[index];
-
-    pair = hashtable_find_pair(hashtable, bucket, key, hash);
-    if(!pair)
-        return -1;
-
-    if(&pair->list == bucket->first && &pair->list == bucket->last)
-        bucket->first = bucket->last = &hashtable->list;
-
-    else if(&pair->list == bucket->first)
-        bucket->first = pair->list.next;
-
-    else if(&pair->list == bucket->last)
-        bucket->last = pair->list.prev;
-
-    list_remove(&pair->list);
-
-    if(hashtable->free_key)
-        hashtable->free_key(pair->key);
-    if(hashtable->free_value)
-        hashtable->free_value(pair->value);
-
-    jsonp_free(pair);
-    hashtable->size--;
-
-    return 0;
-}
-
-static void hashtable_do_clear(hashtable_t *hashtable)
-{
-    list_t *list, *next;
-    pair_t *pair;
-
-    for(list = hashtable->list.next; list != &hashtable->list; list = next)
-    {
-        next = list->next;
-        pair = list_to_pair(list);
-        if(hashtable->free_key)
-            hashtable->free_key(pair->key);
-        if(hashtable->free_value)
-            hashtable->free_value(pair->value);
-        jsonp_free(pair);
-    }
-}
-
-static int hashtable_do_rehash(hashtable_t *hashtable)
-{
-    list_t *list, *next;
-    pair_t *pair;
-    size_t i, index, new_size;
-
-    jsonp_free(hashtable->buckets);
-
-    hashtable->num_buckets++;
-    new_size = num_buckets(hashtable);
-
-    hashtable->buckets = jsonp_malloc(new_size * sizeof(bucket_t));
-    if(!hashtable->buckets)
-        return -1;
-
-    for(i = 0; i < num_buckets(hashtable); i++)
-    {
-        hashtable->buckets[i].first = hashtable->buckets[i].last =
-            &hashtable->list;
-    }
-
-    list = hashtable->list.next;
-    list_init(&hashtable->list);
-
-    for(; list != &hashtable->list; list = next) {
-        next = list->next;
-        pair = list_to_pair(list);
-        index = pair->hash % new_size;
-        insert_to_bucket(hashtable, &hashtable->buckets[index], &pair->list);
-    }
-
-    return 0;
-}
-
-
-hashtable_t *hashtable_create(key_hash_fn hash_key, key_cmp_fn cmp_keys,
-                              free_fn free_key, free_fn free_value)
-{
-    hashtable_t *hashtable = jsonp_malloc(sizeof(hashtable_t));
-    if(!hashtable)
-        return NULL;
-
-    if(hashtable_init(hashtable, hash_key, cmp_keys, free_key, free_value))
-    {
-        jsonp_free(hashtable);
-        return NULL;
-    }
-
-    return hashtable;
-}
-
-void hashtable_destroy(hashtable_t *hashtable)
-{
-    hashtable_close(hashtable);
-    jsonp_free(hashtable);
-}
-
-int hashtable_init(hashtable_t *hashtable,
-                   key_hash_fn hash_key, key_cmp_fn cmp_keys,
-                   free_fn free_key, free_fn free_value)
-{
-    size_t i;
-
-    hashtable->size = 0;
-    hashtable->num_buckets = 0;  /* index to primes[] */
-    hashtable->buckets = jsonp_malloc(num_buckets(hashtable) * sizeof(bucket_t));
-    if(!hashtable->buckets)
-        return -1;
-
-    list_init(&hashtable->list);
-
-    hashtable->hash_key = hash_key;
-    hashtable->cmp_keys = cmp_keys;
-    hashtable->free_key = free_key;
-    hashtable->free_value = free_value;
-
-    for(i = 0; i < num_buckets(hashtable); i++)
-    {
-        hashtable->buckets[i].first = hashtable->buckets[i].last =
-            &hashtable->list;
-    }
-
-    return 0;
-}
-
-void hashtable_close(hashtable_t *hashtable)
-{
-    hashtable_do_clear(hashtable);
-    jsonp_free(hashtable->buckets);
-}
-
-int hashtable_set(hashtable_t *hashtable, void *key, void *value)
-{
-    pair_t *pair;
-    bucket_t *bucket;
-    size_t hash, index;
-
-    /* rehash if the load ratio exceeds 1 */
-    if(hashtable->size >= num_buckets(hashtable))
-        if(hashtable_do_rehash(hashtable))
-            return -1;
-
-    hash = hashtable->hash_key(key);
-    index = hash % num_buckets(hashtable);
-    bucket = &hashtable->buckets[index];
-    pair = hashtable_find_pair(hashtable, bucket, key, hash);
-
-    if(pair)
-    {
-        if(hashtable->free_key)
-            hashtable->free_key(key);
-        if(hashtable->free_value)
-            hashtable->free_value(pair->value);
-        pair->value = value;
-    }
-    else
-    {
-        pair = jsonp_malloc(sizeof(pair_t));
-        if(!pair)
-            return -1;
-
-        pair->key = key;
-        pair->value = value;
-        pair->hash = hash;
-        list_init(&pair->list);
-
-        insert_to_bucket(hashtable, bucket, &pair->list);
-
-        hashtable->size++;
-    }
-    return 0;
-}
-
-void *hashtable_get(hashtable_t *hashtable, const void *key)
-{
-    pair_t *pair;
-    size_t hash;
-    bucket_t *bucket;
-
-    hash = hashtable->hash_key(key);
-    bucket = &hashtable->buckets[hash % num_buckets(hashtable)];
-
-    pair = hashtable_find_pair(hashtable, bucket, key, hash);
-    if(!pair)
-        return NULL;
-
-    return pair->value;
-}
-
-int hashtable_del(hashtable_t *hashtable, const void *key)
-{
-    size_t hash = hashtable->hash_key(key);
-    return hashtable_do_del(hashtable, key, hash);
-}
-
-void hashtable_clear(hashtable_t *hashtable)
-{
-    size_t i;
-
-    hashtable_do_clear(hashtable);
-
-    for(i = 0; i < num_buckets(hashtable); i++)
-    {
-        hashtable->buckets[i].first = hashtable->buckets[i].last =
-            &hashtable->list;
-    }
-
-    list_init(&hashtable->list);
-    hashtable->size = 0;
-}
-
-void *hashtable_iter(hashtable_t *hashtable)
-{
-    return hashtable_iter_next(hashtable, &hashtable->list);
-}
-
-void *hashtable_iter_at(hashtable_t *hashtable, const void *key)
-{
-    pair_t *pair;
-    size_t hash;
-    bucket_t *bucket;
-
-    hash = hashtable->hash_key(key);
-    bucket = &hashtable->buckets[hash % num_buckets(hashtable)];
-
-    pair = hashtable_find_pair(hashtable, bucket, key, hash);
-    if(!pair)
-        return NULL;
-
-    return &pair->list;
-}
-
-void *hashtable_iter_next(hashtable_t *hashtable, void *iter)
-{
-    list_t *list = (list_t *)iter;
-    if(list->next == &hashtable->list)
-        return NULL;
-    return list->next;
-}
-
-void *hashtable_iter_key(void *iter)
-{
-    pair_t *pair = list_to_pair((list_t *)iter);
-    return pair->key;
-}
-
-void *hashtable_iter_value(void *iter)
-{
-    pair_t *pair = list_to_pair((list_t *)iter);
-    return pair->value;
-}
-
-void hashtable_iter_set(hashtable_t *hashtable, void *iter, void *value)
-{
-    pair_t *pair = list_to_pair((list_t *)iter);
-
-    if(hashtable->free_value)
-        hashtable->free_value(pair->value);
-
-    pair->value = value;
-}

+ 0 - 207
compat/jansson/hashtable.h

@@ -1,207 +0,0 @@
-/*
- * Copyright (c) 2009-2011 Petri Lehtinen <petri@digip.org>
- *
- * This library is free software; you can redistribute it and/or modify
- * it under the terms of the MIT license. See LICENSE for details.
- */
-
-#ifndef HASHTABLE_H
-#define HASHTABLE_H
-
-typedef size_t (*key_hash_fn)(const void *key);
-typedef int (*key_cmp_fn)(const void *key1, const void *key2);
-typedef void (*free_fn)(void *key);
-
-struct hashtable_list {
-    struct hashtable_list *prev;
-    struct hashtable_list *next;
-};
-
-struct hashtable_pair {
-    void *key;
-    void *value;
-    size_t hash;
-    struct hashtable_list list;
-};
-
-struct hashtable_bucket {
-    struct hashtable_list *first;
-    struct hashtable_list *last;
-};
-
-typedef struct hashtable {
-    size_t size;
-    struct hashtable_bucket *buckets;
-    size_t num_buckets;  /* index to primes[] */
-    struct hashtable_list list;
-
-    key_hash_fn hash_key;
-    key_cmp_fn cmp_keys;  /* returns non-zero for equal keys */
-    free_fn free_key;
-    free_fn free_value;
-} hashtable_t;
-
-/**
- * hashtable_create - Create a hashtable object
- *
- * @hash_key: The key hashing function
- * @cmp_keys: The key compare function. Returns non-zero for equal and
- *     zero for unequal unequal keys
- * @free_key: If non-NULL, called for a key that is no longer referenced.
- * @free_value: If non-NULL, called for a value that is no longer referenced.
- *
- * Returns a new hashtable object that should be freed with
- * hashtable_destroy when it's no longer used, or NULL on failure (out
- * of memory).
- */
-hashtable_t *hashtable_create(key_hash_fn hash_key, key_cmp_fn cmp_keys,
-                              free_fn free_key, free_fn free_value);
-
-/**
- * hashtable_destroy - Destroy a hashtable object
- *
- * @hashtable: The hashtable
- *
- * Destroys a hashtable created with hashtable_create().
- */
-void hashtable_destroy(hashtable_t *hashtable);
-
-/**
- * hashtable_init - Initialize a hashtable object
- *
- * @hashtable: The (statically allocated) hashtable object
- * @hash_key: The key hashing function
- * @cmp_keys: The key compare function. Returns non-zero for equal and
- *     zero for unequal unequal keys
- * @free_key: If non-NULL, called for a key that is no longer referenced.
- * @free_value: If non-NULL, called for a value that is no longer referenced.
- *
- * Initializes a statically allocated hashtable object. The object
- * should be cleared with hashtable_close when it's no longer used.
- *
- * Returns 0 on success, -1 on error (out of memory).
- */
-int hashtable_init(hashtable_t *hashtable,
-                   key_hash_fn hash_key, key_cmp_fn cmp_keys,
-                   free_fn free_key, free_fn free_value);
-
-/**
- * hashtable_close - Release all resources used by a hashtable object
- *
- * @hashtable: The hashtable
- *
- * Destroys a statically allocated hashtable object.
- */
-void hashtable_close(hashtable_t *hashtable);
-
-/**
- * hashtable_set - Add/modify value in hashtable
- *
- * @hashtable: The hashtable object
- * @key: The key
- * @value: The value
- *
- * If a value with the given key already exists, its value is replaced
- * with the new value.
- *
- * Key and value are "stealed" in the sense that hashtable frees them
- * automatically when they are no longer used. The freeing is
- * accomplished by calling free_key and free_value functions that were
- * supplied to hashtable_new. In case one or both of the free
- * functions is NULL, the corresponding item is not "stealed".
- *
- * Returns 0 on success, -1 on failure (out of memory).
- */
-int hashtable_set(hashtable_t *hashtable, void *key, void *value);
-
-/**
- * hashtable_get - Get a value associated with a key
- *
- * @hashtable: The hashtable object
- * @key: The key
- *
- * Returns value if it is found, or NULL otherwise.
- */
-void *hashtable_get(hashtable_t *hashtable, const void *key);
-
-/**
- * hashtable_del - Remove a value from the hashtable
- *
- * @hashtable: The hashtable object
- * @key: The key
- *
- * Returns 0 on success, or -1 if the key was not found.
- */
-int hashtable_del(hashtable_t *hashtable, const void *key);
-
-/**
- * hashtable_clear - Clear hashtable
- *
- * @hashtable: The hashtable object
- *
- * Removes all items from the hashtable.
- */
-void hashtable_clear(hashtable_t *hashtable);
-
-/**
- * hashtable_iter - Iterate over hashtable
- *
- * @hashtable: The hashtable object
- *
- * Returns an opaque iterator to the first element in the hashtable.
- * The iterator should be passed to hashtable_iter_* functions.
- * The hashtable items are not iterated over in any particular order.
- *
- * There's no need to free the iterator in any way. The iterator is
- * valid as long as the item that is referenced by the iterator is not
- * deleted. Other values may be added or deleted. In particular,
- * hashtable_iter_next() may be called on an iterator, and after that
- * the key/value pair pointed by the old iterator may be deleted.
- */
-void *hashtable_iter(hashtable_t *hashtable);
-
-/**
- * hashtable_iter_at - Return an iterator at a specific key
- *
- * @hashtable: The hashtable object
- * @key: The key that the iterator should point to
- *
- * Like hashtable_iter() but returns an iterator pointing to a
- * specific key.
- */
-void *hashtable_iter_at(hashtable_t *hashtable, const void *key);
-
-/**
- * hashtable_iter_next - Advance an iterator
- *
- * @hashtable: The hashtable object
- * @iter: The iterator
- *
- * Returns a new iterator pointing to the next element in the
- * hashtable or NULL if the whole hastable has been iterated over.
- */
-void *hashtable_iter_next(hashtable_t *hashtable, void *iter);
-
-/**
- * hashtable_iter_key - Retrieve the key pointed by an iterator
- *
- * @iter: The iterator
- */
-void *hashtable_iter_key(void *iter);
-
-/**
- * hashtable_iter_value - Retrieve the value pointed by an iterator
- *
- * @iter: The iterator
- */
-void *hashtable_iter_value(void *iter);
-
-/**
- * hashtable_iter_set - Set the value pointed by an iterator
- *
- * @iter: The iterator
- * @value: The value to set
- */
-void hashtable_iter_set(hashtable_t *hashtable, void *iter, void *value);
-
-#endif

+ 0 - 253
compat/jansson/jansson.h

@@ -1,253 +0,0 @@
-/*
- * Copyright (c) 2009-2011 Petri Lehtinen <petri@digip.org>
- *
- * Jansson is free software; you can redistribute it and/or modify
- * it under the terms of the MIT license. See LICENSE for details.
- */
-
-#ifndef JANSSON_H
-#define JANSSON_H
-
-#include <stdio.h>
-#include <stdlib.h>  /* for size_t */
-#include <stdarg.h>
-
-#include <jansson_config.h>
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-/* version */
-
-#define JANSSON_MAJOR_VERSION  2
-#define JANSSON_MINOR_VERSION  1
-#define JANSSON_MICRO_VERSION  0
-
-/* Micro version is omitted if it's 0 */
-#define JANSSON_VERSION  "2.1"
-
-/* Version as a 3-byte hex number, e.g. 0x010201 == 1.2.1. Use this
-   for numeric comparisons, e.g. #if JANSSON_VERSION_HEX >= ... */
-#define JANSSON_VERSION_HEX  ((JANSSON_MAJOR_VERSION << 16) |   \
-                              (JANSSON_MINOR_VERSION << 8)  |   \
-                              (JANSSON_MICRO_VERSION << 0))
-
-
-/* types */
-
-typedef enum {
-    JSON_OBJECT,
-    JSON_ARRAY,
-    JSON_STRING,
-    JSON_INTEGER,
-    JSON_REAL,
-    JSON_TRUE,
-    JSON_FALSE,
-    JSON_NULL
-} json_type;
-
-typedef struct {
-    json_type type;
-    size_t refcount;
-} json_t;
-
-#if JSON_INTEGER_IS_LONG_LONG && (!defined(WIN32))
-#define JSON_INTEGER_FORMAT "lld"
-typedef long long json_int_t;
-#else
-#define JSON_INTEGER_FORMAT "ld"
-typedef long json_int_t;
-#endif /* JSON_INTEGER_IS_LONG_LONG */
-
-#define json_typeof(json)      ((json)->type)
-#define json_is_object(json)   (json && json_typeof(json) == JSON_OBJECT)
-#define json_is_array(json)    (json && json_typeof(json) == JSON_ARRAY)
-#define json_is_string(json)   (json && json_typeof(json) == JSON_STRING)
-#define json_is_integer(json)  (json && json_typeof(json) == JSON_INTEGER)
-#define json_is_real(json)     (json && json_typeof(json) == JSON_REAL)
-#define json_is_number(json)   (json_is_integer(json) || json_is_real(json))
-#define json_is_true(json)     (json && json_typeof(json) == JSON_TRUE)
-#define json_is_false(json)    (json && json_typeof(json) == JSON_FALSE)
-#define json_is_boolean(json)  (json_is_true(json) || json_is_false(json))
-#define json_is_null(json)     (json && json_typeof(json) == JSON_NULL)
-
-/* construction, destruction, reference counting */
-
-json_t *json_object(void);
-json_t *json_array(void);
-json_t *json_string(const char *value);
-json_t *json_string_nocheck(const char *value);
-json_t *json_integer(json_int_t value);
-json_t *json_real(double value);
-json_t *json_true(void);
-json_t *json_false(void);
-json_t *json_null(void);
-
-static JSON_INLINE
-json_t *json_incref(json_t *json)
-{
-    if(json && json->refcount != (size_t)-1)
-        ++json->refcount;
-    return json;
-}
-
-/* do not call json_delete directly */
-void json_delete(json_t *json);
-
-static JSON_INLINE
-void json_decref(json_t *json)
-{
-    if(json && json->refcount != (size_t)-1 && --json->refcount == 0)
-        json_delete(json);
-}
-
-
-/* error reporting */
-
-#define JSON_ERROR_TEXT_LENGTH    160
-#define JSON_ERROR_SOURCE_LENGTH   80
-
-typedef struct {
-    int line;
-    int column;
-    int position;
-    char source[JSON_ERROR_SOURCE_LENGTH];
-    char text[JSON_ERROR_TEXT_LENGTH];
-} json_error_t;
-
-
-/* getters, setters, manipulation */
-
-size_t json_object_size(const json_t *object);
-json_t *json_object_get(const json_t *object, const char *key);
-int json_object_set_new(json_t *object, const char *key, json_t *value);
-int json_object_set_new_nocheck(json_t *object, const char *key, json_t *value);
-int json_object_del(json_t *object, const char *key);
-int json_object_clear(json_t *object);
-int json_object_update(json_t *object, json_t *other);
-void *json_object_iter(json_t *object);
-void *json_object_iter_at(json_t *object, const char *key);
-void *json_object_iter_next(json_t *object, void *iter);
-const char *json_object_iter_key(void *iter);
-json_t *json_object_iter_value(void *iter);
-int json_object_iter_set_new(json_t *object, void *iter, json_t *value);
-
-static JSON_INLINE
-int json_object_set(json_t *object, const char *key, json_t *value)
-{
-    return json_object_set_new(object, key, json_incref(value));
-}
-
-static JSON_INLINE
-int json_object_set_nocheck(json_t *object, const char *key, json_t *value)
-{
-    return json_object_set_new_nocheck(object, key, json_incref(value));
-}
-
-static JSON_INLINE
-int json_object_iter_set(json_t *object, void *iter, json_t *value)
-{
-    return json_object_iter_set_new(object, iter, json_incref(value));
-}
-
-size_t json_array_size(const json_t *array);
-json_t *json_array_get(const json_t *array, size_t index);
-int json_array_set_new(json_t *array, size_t index, json_t *value);
-int json_array_append_new(json_t *array, json_t *value);
-int json_array_insert_new(json_t *array, size_t index, json_t *value);
-int json_array_remove(json_t *array, size_t index);
-int json_array_clear(json_t *array);
-int json_array_extend(json_t *array, json_t *other);
-
-static JSON_INLINE
-int json_array_set(json_t *array, size_t index, json_t *value)
-{
-    return json_array_set_new(array, index, json_incref(value));
-}
-
-static JSON_INLINE
-int json_array_append(json_t *array, json_t *value)
-{
-    return json_array_append_new(array, json_incref(value));
-}
-
-static JSON_INLINE
-int json_array_insert(json_t *array, size_t index, json_t *value)
-{
-    return json_array_insert_new(array, index, json_incref(value));
-}
-
-const char *json_string_value(const json_t *string);
-json_int_t json_integer_value(const json_t *integer);
-double json_real_value(const json_t *real);
-double json_number_value(const json_t *json);
-
-int json_string_set(json_t *string, const char *value);
-int json_string_set_nocheck(json_t *string, const char *value);
-int json_integer_set(json_t *integer, json_int_t value);
-int json_real_set(json_t *real, double value);
-
-
-/* pack, unpack */
-
-json_t *json_pack(const char *fmt, ...);
-json_t *json_pack_ex(json_error_t *error, size_t flags, const char *fmt, ...);
-json_t *json_vpack_ex(json_error_t *error, size_t flags, const char *fmt, va_list ap);
-
-#define JSON_VALIDATE_ONLY  0x1
-#define JSON_STRICT         0x2
-
-int json_unpack(json_t *root, const char *fmt, ...);
-int json_unpack_ex(json_t *root, json_error_t *error, size_t flags, const char *fmt, ...);
-int json_vunpack_ex(json_t *root, json_error_t *error, size_t flags, const char *fmt, va_list ap);
-
-
-/* equality */
-
-int json_equal(json_t *value1, json_t *value2);
-
-
-/* copying */
-
-json_t *json_copy(json_t *value);
-json_t *json_deep_copy(json_t *value);
-
-
-/* decoding */
-
-#define JSON_REJECT_DUPLICATES 0x1
-#define JSON_DISABLE_EOF_CHECK 0x2
-
-json_t *json_loads(const char *input, size_t flags, json_error_t *error);
-json_t *json_loadb(const char *buffer, size_t buflen, size_t flags, json_error_t *error);
-json_t *json_loadf(FILE *input, size_t flags, json_error_t *error);
-json_t *json_load_file(const char *path, size_t flags, json_error_t *error);
-
-
-/* encoding */
-
-#define JSON_INDENT(n)      (n & 0x1F)
-#define JSON_COMPACT        0x20
-#define JSON_ENSURE_ASCII   0x40
-#define JSON_SORT_KEYS      0x80
-#define JSON_PRESERVE_ORDER 0x100
-#define JSON_ENCODE_ANY     0x200
-
-char *json_dumps(const json_t *json, size_t flags);
-int json_dumpf(const json_t *json, FILE *output, size_t flags);
-int json_dump_file(const json_t *json, const char *path, size_t flags);
-
-
-/* custom memory allocation */
-
-typedef void *(*json_malloc_t)(size_t);
-typedef void (*json_free_t)(void *);
-
-void json_set_alloc_funcs(json_malloc_t malloc_fn, json_free_t free_fn);
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif

+ 0 - 34
compat/jansson/jansson_config.h

@@ -1,34 +0,0 @@
-/*
- * Copyright (c) 2010-2011 Petri Lehtinen <petri@digip.org>
- *
- * Jansson is free software; you can redistribute it and/or modify
- * it under the terms of the MIT license. See LICENSE for details.
- *
- *
- * This file specifies a part of the site-specific configuration for
- * Jansson, namely those things that affect the public API in
- * jansson.h.
- *
- * The configure script copies this file to jansson_config.h and
- * replaces @var@ substitutions by values that fit your system. If you
- * cannot run the configure script, you can do the value substitution
- * by hand.
- */
-
-#ifndef JANSSON_CONFIG_H
-#define JANSSON_CONFIG_H
-
-/* If your compiler supports the inline keyword in C, JSON_INLINE is
-   defined to `inline', otherwise empty. In C++, the inline is always
-   supported. */
-#ifdef __cplusplus
-#define JSON_INLINE inline
-#else
-#define JSON_INLINE inline
-#endif
-
-/* If your compiler supports the `long long` type,
-   JSON_INTEGER_IS_LONG_LONG is defined to 1, otherwise to 0. */
-#define JSON_INTEGER_IS_LONG_LONG 1
-
-#endif

+ 0 - 34
compat/jansson/jansson_config.h.in

@@ -1,34 +0,0 @@
-/*
- * Copyright (c) 2010-2011 Petri Lehtinen <petri@digip.org>
- *
- * Jansson is free software; you can redistribute it and/or modify
- * it under the terms of the MIT license. See LICENSE for details.
- *
- *
- * This file specifies a part of the site-specific configuration for
- * Jansson, namely those things that affect the public API in
- * jansson.h.
- *
- * The configure script copies this file to jansson_config.h and
- * replaces @var@ substitutions by values that fit your system. If you
- * cannot run the configure script, you can do the value substitution
- * by hand.
- */
-
-#ifndef JANSSON_CONFIG_H
-#define JANSSON_CONFIG_H
-
-/* If your compiler supports the inline keyword in C, JSON_INLINE is
-   defined to `inline', otherwise empty. In C++, the inline is always
-   supported. */
-#ifdef __cplusplus
-#define JSON_INLINE inline
-#else
-#define JSON_INLINE @json_inline@
-#endif
-
-/* If your compiler supports the `long long` type,
-   JSON_INTEGER_IS_LONG_LONG is defined to 1, otherwise to 0. */
-#define JSON_INTEGER_IS_LONG_LONG @json_have_long_long@
-
-#endif

+ 0 - 91
compat/jansson/jansson_private.h

@@ -1,91 +0,0 @@
-/*
- * Copyright (c) 2009-2011 Petri Lehtinen <petri@digip.org>
- *
- * Jansson is free software; you can redistribute it and/or modify
- * it under the terms of the MIT license. See LICENSE for details.
- */
-
-#ifndef JANSSON_PRIVATE_H
-#define JANSSON_PRIVATE_H
-
-#include <stddef.h>
-#include "jansson.h"
-#include "hashtable.h"
-
-#define container_of(ptr_, type_, member_)  \
-    ((type_ *)((char *)ptr_ - offsetof(type_, member_)))
-
-/* On some platforms, max() may already be defined */
-#ifndef max
-#define max(a, b)  ((a) > (b) ? (a) : (b))
-#endif
-
-/* va_copy is a C99 feature. In C89 implementations, it's sometimes
-   available as __va_copy. If not, memcpy() should do the trick. */
-#ifndef va_copy
-#ifdef __va_copy
-#define va_copy __va_copy
-#else
-#define va_copy(a, b)  memcpy(&(a), &(b), sizeof(va_list))
-#endif
-#endif
-
-typedef struct {
-    json_t json;
-    hashtable_t hashtable;
-    size_t serial;
-    int visited;
-} json_object_t;
-
-typedef struct {
-    json_t json;
-    size_t size;
-    size_t entries;
-    json_t **table;
-    int visited;
-} json_array_t;
-
-typedef struct {
-    json_t json;
-    char *value;
-} json_string_t;
-
-typedef struct {
-    json_t json;
-    double value;
-} json_real_t;
-
-typedef struct {
-    json_t json;
-    json_int_t value;
-} json_integer_t;
-
-#define json_to_object(json_)  container_of(json_, json_object_t, json)
-#define json_to_array(json_)   container_of(json_, json_array_t, json)
-#define json_to_string(json_)  container_of(json_, json_string_t, json)
-#define json_to_real(json_)   container_of(json_, json_real_t, json)
-#define json_to_integer(json_) container_of(json_, json_integer_t, json)
-
-size_t jsonp_hash_str(const void *ptr);
-int jsonp_str_equal(const void *ptr1, const void *ptr2);
-
-typedef struct {
-    size_t serial;
-    char key[1];
-} object_key_t;
-
-const object_key_t *jsonp_object_iter_fullkey(void *iter);
-
-void jsonp_error_init(json_error_t *error, const char *source);
-void jsonp_error_set_source(json_error_t *error, const char *source);
-void jsonp_error_set(json_error_t *error, int line, int column,
-                     size_t position, const char *msg, ...);
-void jsonp_error_vset(json_error_t *error, int line, int column,
-                      size_t position, const char *msg, va_list ap);
-
-/* Wrappers for custom memory functions */
-void* jsonp_malloc(size_t size);
-void jsonp_free(void *ptr);
-char *jsonp_strdup(const char *str);
-
-#endif

+ 0 - 960
compat/jansson/load.c

@@ -1,960 +0,0 @@
-/*
- * Copyright (c) 2009-2011 Petri Lehtinen <petri@digip.org>
- *
- * Jansson is free software; you can redistribute it and/or modify
- * it under the terms of the MIT license. See LICENSE for details.
- */
-
-#define _GNU_SOURCE
-#include <ctype.h>
-#include <errno.h>
-#include <limits.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <assert.h>
-
-#include <jansson.h>
-#include "jansson_private.h"
-#include "strbuffer.h"
-#include "utf.h"
-
-#define STREAM_STATE_OK        0
-#define STREAM_STATE_EOF      -1
-#define STREAM_STATE_ERROR    -2
-
-#define TOKEN_INVALID         -1
-#define TOKEN_EOF              0
-#define TOKEN_STRING         256
-#define TOKEN_INTEGER        257
-#define TOKEN_REAL           258
-#define TOKEN_TRUE           259
-#define TOKEN_FALSE          260
-#define TOKEN_NULL           261
-
-/* Read one byte from stream, convert to unsigned char, then int, and
-   return. return EOF on end of file. This corresponds to the
-   behaviour of fgetc(). */
-typedef int (*get_func)(void *data);
-
-typedef struct {
-    get_func get;
-    void *data;
-    char buffer[5];
-    int buffer_pos;
-    int state;
-    int line;
-    int column, last_column;
-    size_t position;
-} stream_t;
-
-typedef struct {
-    stream_t stream;
-    strbuffer_t saved_text;
-    int token;
-    union {
-        char *string;
-        json_int_t integer;
-        double real;
-    } value;
-} lex_t;
-
-#define stream_to_lex(stream) container_of(stream, lex_t, stream)
-
-
-/*** error reporting ***/
-
-static void error_set(json_error_t *error, const lex_t *lex,
-                      const char *msg, ...)
-{
-    va_list ap;
-    char msg_text[JSON_ERROR_TEXT_LENGTH];
-
-    int line = -1, col = -1;
-    size_t pos = 0;
-    const char *result = msg_text;
-
-    if(!error)
-        return;
-
-    va_start(ap, msg);
-    vsnprintf(msg_text, JSON_ERROR_TEXT_LENGTH, msg, ap);
-    va_end(ap);
-
-    if(lex)
-    {
-        const char *saved_text = strbuffer_value(&lex->saved_text);
-        char msg_with_context[JSON_ERROR_TEXT_LENGTH];
-
-        line = lex->stream.line;
-        col = lex->stream.column;
-        pos = lex->stream.position;
-
-        if(saved_text && saved_text[0])
-        {
-            if(lex->saved_text.length <= 20) {
-                snprintf(msg_with_context, JSON_ERROR_TEXT_LENGTH,
-                         "%s near '%s'", msg_text, saved_text);
-                result = msg_with_context;
-            }
-        }
-        else
-        {
-            if(lex->stream.state == STREAM_STATE_ERROR) {
-                /* No context for UTF-8 decoding errors */
-                result = msg_text;
-            }
-            else {
-                snprintf(msg_with_context, JSON_ERROR_TEXT_LENGTH,
-                         "%s near end of file", msg_text);
-                result = msg_with_context;
-            }
-        }
-    }
-
-    jsonp_error_set(error, line, col, pos, "%s", result);
-}
-
-
-/*** lexical analyzer ***/
-
-static void
-stream_init(stream_t *stream, get_func get, void *data)
-{
-    stream->get = get;
-    stream->data = data;
-    stream->buffer[0] = '\0';
-    stream->buffer_pos = 0;
-
-    stream->state = STREAM_STATE_OK;
-    stream->line = 1;
-    stream->column = 0;
-    stream->position = 0;
-}
-
-static int stream_get(stream_t *stream, json_error_t *error)
-{
-    int c;
-
-    if(stream->state != STREAM_STATE_OK)
-        return stream->state;
-
-    if(!stream->buffer[stream->buffer_pos])
-    {
-        c = stream->get(stream->data);
-        if(c == EOF) {
-            stream->state = STREAM_STATE_EOF;
-            return STREAM_STATE_EOF;
-        }
-
-        stream->buffer[0] = c;
-        stream->buffer_pos = 0;
-
-        if(0x80 <= c && c <= 0xFF)
-        {
-            /* multi-byte UTF-8 sequence */
-            int i, count;
-
-            count = utf8_check_first(c);
-            if(!count)
-                goto out;
-
-            assert(count >= 2);
-
-            for(i = 1; i < count; i++)
-                stream->buffer[i] = stream->get(stream->data);
-
-            if(!utf8_check_full(stream->buffer, count, NULL))
-                goto out;
-
-            stream->buffer[count] = '\0';
-        }
-        else
-            stream->buffer[1] = '\0';
-    }
-
-    c = stream->buffer[stream->buffer_pos++];
-
-    stream->position++;
-    if(c == '\n') {
-        stream->line++;
-        stream->last_column = stream->column;
-        stream->column = 0;
-    }
-    else if(utf8_check_first(c)) {
-        /* track the Unicode character column, so increment only if
-           this is the first character of a UTF-8 sequence */
-        stream->column++;
-    }
-
-    return c;
-
-out:
-    stream->state = STREAM_STATE_ERROR;
-    error_set(error, stream_to_lex(stream), "unable to decode byte 0x%x", c);
-    return STREAM_STATE_ERROR;
-}
-
-static void stream_unget(stream_t *stream, int c)
-{
-    if(c == STREAM_STATE_EOF || c == STREAM_STATE_ERROR)
-        return;
-
-    stream->position--;
-    if(c == '\n') {
-        stream->line--;
-        stream->column = stream->last_column;
-    }
-    else if(utf8_check_first(c))
-        stream->column--;
-
-    assert(stream->buffer_pos > 0);
-    stream->buffer_pos--;
-    assert(stream->buffer[stream->buffer_pos] == c);
-}
-
-
-static int lex_get(lex_t *lex, json_error_t *error)
-{
-    return stream_get(&lex->stream, error);
-}
-
-static void lex_save(lex_t *lex, int c)
-{
-    strbuffer_append_byte(&lex->saved_text, c);
-}
-
-static int lex_get_save(lex_t *lex, json_error_t *error)
-{
-    int c = stream_get(&lex->stream, error);
-    if(c != STREAM_STATE_EOF && c != STREAM_STATE_ERROR)
-        lex_save(lex, c);
-    return c;
-}
-
-static void lex_unget(lex_t *lex, int c)
-{
-    stream_unget(&lex->stream, c);
-}
-
-static void lex_unget_unsave(lex_t *lex, int c)
-{
-    if(c != STREAM_STATE_EOF && c != STREAM_STATE_ERROR) {
-        char d;
-        stream_unget(&lex->stream, c);
-        d = strbuffer_pop(&lex->saved_text);
-        assert(c == d);
-    }
-}
-
-static void lex_save_cached(lex_t *lex)
-{
-    while(lex->stream.buffer[lex->stream.buffer_pos] != '\0')
-    {
-        lex_save(lex, lex->stream.buffer[lex->stream.buffer_pos]);
-        lex->stream.buffer_pos++;
-        lex->stream.position++;
-    }
-}
-
-/* assumes that str points to 'u' plus at least 4 valid hex digits */
-static int32_t decode_unicode_escape(const char *str)
-{
-    int i;
-    int32_t value = 0;
-
-    assert(str[0] == 'u');
-
-    for(i = 1; i <= 4; i++) {
-        char c = str[i];
-        value <<= 4;
-        if(isdigit(c))
-            value += c - '0';
-        else if(islower(c))
-            value += c - 'a' + 10;
-        else if(isupper(c))
-            value += c - 'A' + 10;
-        else
-            assert(0);
-    }
-
-    return value;
-}
-
-static void lex_scan_string(lex_t *lex, json_error_t *error)
-{
-    int c;
-    const char *p;
-    char *t;
-    int i;
-
-    lex->value.string = NULL;
-    lex->token = TOKEN_INVALID;
-
-    c = lex_get_save(lex, error);
-
-    while(c != '"') {
-        if(c == STREAM_STATE_ERROR)
-            goto out;
-
-        else if(c == STREAM_STATE_EOF) {
-            error_set(error, lex, "premature end of input");
-            goto out;
-        }
-
-        else if(0 <= c && c <= 0x1F) {
-            /* control character */
-            lex_unget_unsave(lex, c);
-            if(c == '\n')
-                error_set(error, lex, "unexpected newline", c);
-            else
-                error_set(error, lex, "control character 0x%x", c);
-            goto out;
-        }
-
-        else if(c == '\\') {
-            c = lex_get_save(lex, error);
-            if(c == 'u') {
-                c = lex_get_save(lex, error);
-                for(i = 0; i < 4; i++) {
-                    if(!isxdigit(c)) {
-                        error_set(error, lex, "invalid escape");
-                        goto out;
-                    }
-                    c = lex_get_save(lex, error);
-                }
-            }
-            else if(c == '"' || c == '\\' || c == '/' || c == 'b' ||
-                    c == 'f' || c == 'n' || c == 'r' || c == 't')
-                c = lex_get_save(lex, error);
-            else {
-                error_set(error, lex, "invalid escape");
-                goto out;
-            }
-        }
-        else
-            c = lex_get_save(lex, error);
-    }
-
-    /* the actual value is at most of the same length as the source
-       string, because:
-         - shortcut escapes (e.g. "\t") (length 2) are converted to 1 byte
-         - a single \uXXXX escape (length 6) is converted to at most 3 bytes
-         - two \uXXXX escapes (length 12) forming an UTF-16 surrogate pair
-           are converted to 4 bytes
-    */
-    lex->value.string = jsonp_malloc(lex->saved_text.length + 1);
-    if(!lex->value.string) {
-        /* this is not very nice, since TOKEN_INVALID is returned */
-        goto out;
-    }
-
-    /* the target */
-    t = lex->value.string;
-
-    /* + 1 to skip the " */
-    p = strbuffer_value(&lex->saved_text) + 1;
-
-    while(*p != '"') {
-        if(*p == '\\') {
-            p++;
-            if(*p == 'u') {
-                char buffer[4];
-                int length;
-                int32_t value;
-
-                value = decode_unicode_escape(p);
-                p += 5;
-
-                if(0xD800 <= value && value <= 0xDBFF) {
-                    /* surrogate pair */
-                    if(*p == '\\' && *(p + 1) == 'u') {
-                        int32_t value2 = decode_unicode_escape(++p);
-                        p += 5;
-
-                        if(0xDC00 <= value2 && value2 <= 0xDFFF) {
-                            /* valid second surrogate */
-                            value =
-                                ((value - 0xD800) << 10) +
-                                (value2 - 0xDC00) +
-                                0x10000;
-                        }
-                        else {
-                            /* invalid second surrogate */
-                            error_set(error, lex,
-                                      "invalid Unicode '\\u%04X\\u%04X'",
-                                      value, value2);
-                            goto out;
-                        }
-                    }
-                    else {
-                        /* no second surrogate */
-                        error_set(error, lex, "invalid Unicode '\\u%04X'",
-                                  value);
-                        goto out;
-                    }
-                }
-                else if(0xDC00 <= value && value <= 0xDFFF) {
-                    error_set(error, lex, "invalid Unicode '\\u%04X'", value);
-                    goto out;
-                }
-                else if(value == 0)
-                {
-                    error_set(error, lex, "\\u0000 is not allowed");
-                    goto out;
-                }
-
-                if(utf8_encode(value, buffer, &length))
-                    assert(0);
-
-                memcpy(t, buffer, length);
-                t += length;
-            }
-            else {
-                switch(*p) {
-                    case '"': case '\\': case '/':
-                        *t = *p; break;
-                    case 'b': *t = '\b'; break;
-                    case 'f': *t = '\f'; break;
-                    case 'n': *t = '\n'; break;
-                    case 'r': *t = '\r'; break;
-                    case 't': *t = '\t'; break;
-                    default: assert(0);
-                }
-                t++;
-                p++;
-            }
-        }
-        else
-            *(t++) = *(p++);
-    }
-    *t = '\0';
-    lex->token = TOKEN_STRING;
-    return;
-
-out:
-    jsonp_free(lex->value.string);
-}
-
-#if JSON_INTEGER_IS_LONG_LONG
-#define json_strtoint     strtoll
-#else
-#define json_strtoint     strtol
-#endif
-
-static int lex_scan_number(lex_t *lex, int c, json_error_t *error)
-{
-    const char *saved_text;
-    char *end;
-    double value;
-
-    lex->token = TOKEN_INVALID;
-
-    if(c == '-')
-        c = lex_get_save(lex, error);
-
-    if(c == '0') {
-        c = lex_get_save(lex, error);
-        if(isdigit(c)) {
-            lex_unget_unsave(lex, c);
-            goto out;
-        }
-    }
-    else if(isdigit(c)) {
-        c = lex_get_save(lex, error);
-        while(isdigit(c))
-            c = lex_get_save(lex, error);
-    }
-    else {
-        lex_unget_unsave(lex, c);
-        goto out;
-    }
-
-    if(c != '.' && c != 'E' && c != 'e') {
-        json_int_t value;
-
-        lex_unget_unsave(lex, c);
-
-        saved_text = strbuffer_value(&lex->saved_text);
-
-        errno = 0;
-        value = json_strtoint(saved_text, &end, 10);
-        if(errno == ERANGE) {
-            if(value < 0)
-                error_set(error, lex, "too big negative integer");
-            else
-                error_set(error, lex, "too big integer");
-            goto out;
-        }
-
-        assert(end == saved_text + lex->saved_text.length);
-
-        lex->token = TOKEN_INTEGER;
-        lex->value.integer = value;
-        return 0;
-    }
-
-    if(c == '.') {
-        c = lex_get(lex, error);
-        if(!isdigit(c)) {
-            lex_unget(lex, c);
-            goto out;
-        }
-        lex_save(lex, c);
-
-        c = lex_get_save(lex, error);
-        while(isdigit(c))
-            c = lex_get_save(lex, error);
-    }
-
-    if(c == 'E' || c == 'e') {
-        c = lex_get_save(lex, error);
-        if(c == '+' || c == '-')
-            c = lex_get_save(lex, error);
-
-        if(!isdigit(c)) {
-            lex_unget_unsave(lex, c);
-            goto out;
-        }
-
-        c = lex_get_save(lex, error);
-        while(isdigit(c))
-            c = lex_get_save(lex, error);
-    }
-
-    lex_unget_unsave(lex, c);
-
-    saved_text = strbuffer_value(&lex->saved_text);
-    errno = 0;
-    value = strtod(saved_text, &end);
-    assert(end == saved_text + lex->saved_text.length);
-
-    if(errno == ERANGE && value != 0) {
-        error_set(error, lex, "real number overflow");
-        goto out;
-    }
-
-    lex->token = TOKEN_REAL;
-    lex->value.real = value;
-    return 0;
-
-out:
-    return -1;
-}
-
-static int lex_scan(lex_t *lex, json_error_t *error)
-{
-    int c;
-
-    strbuffer_clear(&lex->saved_text);
-
-    if(lex->token == TOKEN_STRING) {
-        jsonp_free(lex->value.string);
-        lex->value.string = NULL;
-    }
-
-    c = lex_get(lex, error);
-    while(c == ' ' || c == '\t' || c == '\n' || c == '\r')
-        c = lex_get(lex, error);
-
-    if(c == STREAM_STATE_EOF) {
-        lex->token = TOKEN_EOF;
-        goto out;
-    }
-
-    if(c == STREAM_STATE_ERROR) {
-        lex->token = TOKEN_INVALID;
-        goto out;
-    }
-
-    lex_save(lex, c);
-
-    if(c == '{' || c == '}' || c == '[' || c == ']' || c == ':' || c == ',')
-        lex->token = c;
-
-    else if(c == '"')
-        lex_scan_string(lex, error);
-
-    else if(isdigit(c) || c == '-') {
-        if(lex_scan_number(lex, c, error))
-            goto out;
-    }
-
-    else if(isupper(c) || islower(c)) {
-        /* eat up the whole identifier for clearer error messages */
-        const char *saved_text;
-
-        c = lex_get_save(lex, error);
-        while(isupper(c) || islower(c))
-            c = lex_get_save(lex, error);
-        lex_unget_unsave(lex, c);
-
-        saved_text = strbuffer_value(&lex->saved_text);
-
-        if(strcmp(saved_text, "true") == 0)
-            lex->token = TOKEN_TRUE;
-        else if(strcmp(saved_text, "false") == 0)
-            lex->token = TOKEN_FALSE;
-        else if(strcmp(saved_text, "null") == 0)
-            lex->token = TOKEN_NULL;
-        else
-            lex->token = TOKEN_INVALID;
-    }
-
-    else {
-        /* save the rest of the input UTF-8 sequence to get an error
-           message of valid UTF-8 */
-        lex_save_cached(lex);
-        lex->token = TOKEN_INVALID;
-    }
-
-out:
-    return lex->token;
-}
-
-static char *lex_steal_string(lex_t *lex)
-{
-    char *result = NULL;
-    if(lex->token == TOKEN_STRING)
-    {
-        result = lex->value.string;
-        lex->value.string = NULL;
-    }
-    return result;
-}
-
-static int lex_init(lex_t *lex, get_func get, void *data)
-{
-    stream_init(&lex->stream, get, data);
-    if(strbuffer_init(&lex->saved_text))
-        return -1;
-
-    lex->token = TOKEN_INVALID;
-    return 0;
-}
-
-static void lex_close(lex_t *lex)
-{
-    if(lex->token == TOKEN_STRING)
-        jsonp_free(lex->value.string);
-    strbuffer_close(&lex->saved_text);
-}
-
-
-/*** parser ***/
-
-static json_t *parse_value(lex_t *lex, size_t flags, json_error_t *error);
-
-static json_t *parse_object(lex_t *lex, size_t flags, json_error_t *error)
-{
-    json_t *object = json_object();
-    if(!object)
-        return NULL;
-
-    lex_scan(lex, error);
-    if(lex->token == '}')
-        return object;
-
-    while(1) {
-        char *key;
-        json_t *value;
-
-        if(lex->token != TOKEN_STRING) {
-            error_set(error, lex, "string or '}' expected");
-            goto error;
-        }
-
-        key = lex_steal_string(lex);
-        if(!key)
-            return NULL;
-
-        if(flags & JSON_REJECT_DUPLICATES) {
-            if(json_object_get(object, key)) {
-                jsonp_free(key);
-                error_set(error, lex, "duplicate object key");
-                goto error;
-            }
-        }
-
-        lex_scan(lex, error);
-        if(lex->token != ':') {
-            jsonp_free(key);
-            error_set(error, lex, "':' expected");
-            goto error;
-        }
-
-        lex_scan(lex, error);
-        value = parse_value(lex, flags, error);
-        if(!value) {
-            jsonp_free(key);
-            goto error;
-        }
-
-        if(json_object_set_nocheck(object, key, value)) {
-            jsonp_free(key);
-            json_decref(value);
-            goto error;
-        }
-
-        json_decref(value);
-        jsonp_free(key);
-
-        lex_scan(lex, error);
-        if(lex->token != ',')
-            break;
-
-        lex_scan(lex, error);
-    }
-
-    if(lex->token != '}') {
-        error_set(error, lex, "'}' expected");
-        goto error;
-    }
-
-    return object;
-
-error:
-    json_decref(object);
-    return NULL;
-}
-
-static json_t *parse_array(lex_t *lex, size_t flags, json_error_t *error)
-{
-    json_t *array = json_array();
-    if(!array)
-        return NULL;
-
-    lex_scan(lex, error);
-    if(lex->token == ']')
-        return array;
-
-    while(lex->token) {
-        json_t *elem = parse_value(lex, flags, error);
-        if(!elem)
-            goto error;
-
-        if(json_array_append(array, elem)) {
-            json_decref(elem);
-            goto error;
-        }
-        json_decref(elem);
-
-        lex_scan(lex, error);
-        if(lex->token != ',')
-            break;
-
-        lex_scan(lex, error);
-    }
-
-    if(lex->token != ']') {
-        error_set(error, lex, "']' expected");
-        goto error;
-    }
-
-    return array;
-
-error:
-    json_decref(array);
-    return NULL;
-}
-
-static json_t *parse_value(lex_t *lex, size_t flags, json_error_t *error)
-{
-    json_t *json;
-
-    switch(lex->token) {
-        case TOKEN_STRING: {
-            json = json_string_nocheck(lex->value.string);
-            break;
-        }
-
-        case TOKEN_INTEGER: {
-            json = json_integer(lex->value.integer);
-            break;
-        }
-
-        case TOKEN_REAL: {
-            json = json_real(lex->value.real);
-            break;
-        }
-
-        case TOKEN_TRUE:
-            json = json_true();
-            break;
-
-        case TOKEN_FALSE:
-            json = json_false();
-            break;
-
-        case TOKEN_NULL:
-            json = json_null();
-            break;
-
-        case '{':
-            json = parse_object(lex, flags, error);
-            break;
-
-        case '[':
-            json = parse_array(lex, flags, error);
-            break;
-
-        case TOKEN_INVALID:
-            error_set(error, lex, "invalid token");
-            return NULL;
-
-        default:
-            error_set(error, lex, "unexpected token");
-            return NULL;
-    }
-
-    if(!json)
-        return NULL;
-
-    return json;
-}
-
-static json_t *parse_json(lex_t *lex, size_t flags, json_error_t *error)
-{
-    json_t *result;
-
-    lex_scan(lex, error);
-    if(lex->token != '[' && lex->token != '{') {
-        error_set(error, lex, "'[' or '{' expected");
-        return NULL;
-    }
-
-    result = parse_value(lex, flags, error);
-    if(!result)
-        return NULL;
-
-    if(!(flags & JSON_DISABLE_EOF_CHECK)) {
-        lex_scan(lex, error);
-        if(lex->token != TOKEN_EOF) {
-            error_set(error, lex, "end of file expected");
-            json_decref(result);
-            result = NULL;
-        }
-    }
-
-    return result;
-}
-
-typedef struct
-{
-    const char *data;
-    int pos;
-} string_data_t;
-
-static int string_get(void *data)
-{
-    char c;
-    string_data_t *stream = (string_data_t *)data;
-    c = stream->data[stream->pos];
-    if(c == '\0')
-        return EOF;
-    else
-    {
-        stream->pos++;
-        return (unsigned char)c;
-    }
-}
-
-json_t *json_loads(const char *string, size_t flags, json_error_t *error)
-{
-    lex_t lex;
-    json_t *result;
-    string_data_t stream_data;
-
-    stream_data.data = string;
-    stream_data.pos = 0;
-
-    if(lex_init(&lex, string_get, (void *)&stream_data))
-        return NULL;
-
-    jsonp_error_init(error, "<string>");
-    result = parse_json(&lex, flags, error);
-
-    lex_close(&lex);
-    return result;
-}
-
-typedef struct
-{
-    const char *data;
-    size_t len;
-    size_t pos;
-} buffer_data_t;
-
-static int buffer_get(void *data)
-{
-    char c;
-    buffer_data_t *stream = data;
-    if(stream->pos >= stream->len)
-      return EOF;
-
-    c = stream->data[stream->pos];
-    stream->pos++;
-    return (unsigned char)c;
-}
-
-json_t *json_loadb(const char *buffer, size_t buflen, size_t flags, json_error_t *error)
-{
-    lex_t lex;
-    json_t *result;
-    buffer_data_t stream_data;
-
-    stream_data.data = buffer;
-    stream_data.pos = 0;
-    stream_data.len = buflen;
-
-    if(lex_init(&lex, buffer_get, (void *)&stream_data))
-        return NULL;
-
-    jsonp_error_init(error, "<buffer>");
-    result = parse_json(&lex, flags, error);
-
-    lex_close(&lex);
-    return result;
-}
-
-json_t *json_loadf(FILE *input, size_t flags, json_error_t *error)
-{
-    lex_t lex;
-    const char *source;
-    json_t *result;
-
-    if(lex_init(&lex, (get_func)fgetc, input))
-        return NULL;
-
-    if(input == stdin)
-        source = "<stdin>";
-    else
-        source = "<stream>";
-
-    jsonp_error_init(error, source);
-    result = parse_json(&lex, flags, error);
-
-    lex_close(&lex);
-    return result;
-}
-
-json_t *json_load_file(const char *path, size_t flags, json_error_t *error)
-{
-    json_t *result;
-    FILE *fp;
-
-    jsonp_error_init(error, path);
-
-    fp = fopen(path, "r");
-    if(!fp)
-    {
-        error_set(error, NULL, "unable to open %s: %s",
-                  path, strerror(errno));
-        return NULL;
-    }
-
-    result = json_loadf(fp, flags, error);
-
-    fclose(fp);
-    return result;
-}

+ 0 - 51
compat/jansson/memory.c

@@ -1,51 +0,0 @@
-/*
- * Copyright (c) 2009-2011 Petri Lehtinen <petri@digip.org>
- * Copyright (c) 2011 Basile Starynkevitch  <basile@starynkevitch.net>
- *
- * Jansson is free software; you can redistribute it and/or modify it
- * under the terms of the MIT license. See LICENSE for details.
- */
-
-#include <stdlib.h>
-#include <string.h>
-
-#include <jansson.h>
-#include "jansson_private.h"
-
-/* memory function pointers */
-static json_malloc_t do_malloc = malloc;
-static json_free_t do_free = free;
-
-void *jsonp_malloc(size_t size)
-{
-    if(!size)
-        return NULL;
-
-    return (*do_malloc)(size);
-}
-
-void jsonp_free(void *ptr)
-{
-    if(!ptr)
-        return;
-
-    (*do_free)(ptr);
-}
-
-char *jsonp_strdup(const char *str)
-{
-    char *new_str;
-
-    new_str = jsonp_malloc(strlen(str) + 1);
-    if(!new_str)
-        return NULL;
-
-    strcpy(new_str, str);
-    return new_str;
-}
-
-void json_set_alloc_funcs(json_malloc_t malloc_fn, json_free_t free_fn)
-{
-    do_malloc = malloc_fn;
-    do_free = free_fn;
-}

+ 0 - 104
compat/jansson/strbuffer.c

@@ -1,104 +0,0 @@
-/*
- * Copyright (c) 2009-2011 Petri Lehtinen <petri@digip.org>
- *
- * Jansson is free software; you can redistribute it and/or modify
- * it under the terms of the MIT license. See LICENSE for details.
- */
-
-#define _GNU_SOURCE
-#include <stdlib.h>
-#include <string.h>
-#include "jansson_private.h"
-#include "strbuffer.h"
-
-#define STRBUFFER_MIN_SIZE  16
-#define STRBUFFER_FACTOR    2
-
-int strbuffer_init(strbuffer_t *strbuff)
-{
-    strbuff->size = STRBUFFER_MIN_SIZE;
-    strbuff->length = 0;
-
-    strbuff->value = jsonp_malloc(strbuff->size);
-    if(!strbuff->value)
-        return -1;
-
-    /* initialize to empty */
-    strbuff->value[0] = '\0';
-    return 0;
-}
-
-void strbuffer_close(strbuffer_t *strbuff)
-{
-    jsonp_free(strbuff->value);
-    strbuff->size = 0;
-    strbuff->length = 0;
-    strbuff->value = NULL;
-}
-
-void strbuffer_clear(strbuffer_t *strbuff)
-{
-    strbuff->length = 0;
-    strbuff->value[0] = '\0';
-}
-
-const char *strbuffer_value(const strbuffer_t *strbuff)
-{
-    return strbuff->value;
-}
-
-char *strbuffer_steal_value(strbuffer_t *strbuff)
-{
-    char *result = strbuff->value;
-    strbuffer_init(strbuff);
-    return result;
-}
-
-int strbuffer_append(strbuffer_t *strbuff, const char *string)
-{
-    return strbuffer_append_bytes(strbuff, string, strlen(string));
-}
-
-int strbuffer_append_byte(strbuffer_t *strbuff, char byte)
-{
-    return strbuffer_append_bytes(strbuff, &byte, 1);
-}
-
-int strbuffer_append_bytes(strbuffer_t *strbuff, const char *data, int size)
-{
-    if(strbuff->length + size >= strbuff->size)
-    {
-        size_t new_size;
-        char *new_value;
-
-        new_size = max(strbuff->size * STRBUFFER_FACTOR,
-                       strbuff->length + size + 1);
-
-        new_value = jsonp_malloc(new_size);
-        if(!new_value)
-            return -1;
-
-        memcpy(new_value, strbuff->value, strbuff->length);
-
-        jsonp_free(strbuff->value);
-        strbuff->value = new_value;
-        strbuff->size = new_size;
-    }
-
-    memcpy(strbuff->value + strbuff->length, data, size);
-    strbuff->length += size;
-    strbuff->value[strbuff->length] = '\0';
-
-    return 0;
-}
-
-char strbuffer_pop(strbuffer_t *strbuff)
-{
-    if(strbuff->length > 0) {
-        char c = strbuff->value[--strbuff->length];
-        strbuff->value[strbuff->length] = '\0';
-        return c;
-    }
-    else
-        return '\0';
-}

+ 0 - 31
compat/jansson/strbuffer.h

@@ -1,31 +0,0 @@
-/*
- * Copyright (c) 2009-2011 Petri Lehtinen <petri@digip.org>
- *
- * Jansson is free software; you can redistribute it and/or modify
- * it under the terms of the MIT license. See LICENSE for details.
- */
-
-#ifndef STRBUFFER_H
-#define STRBUFFER_H
-
-typedef struct {
-    char *value;
-    int length;   /* bytes used */
-    int size;     /* bytes allocated */
-} strbuffer_t;
-
-int strbuffer_init(strbuffer_t *strbuff);
-void strbuffer_close(strbuffer_t *strbuff);
-
-void strbuffer_clear(strbuffer_t *strbuff);
-
-const char *strbuffer_value(const strbuffer_t *strbuff);
-char *strbuffer_steal_value(strbuffer_t *strbuff);
-
-int strbuffer_append(strbuffer_t *strbuff, const char *string);
-int strbuffer_append_byte(strbuffer_t *strbuff, char byte);
-int strbuffer_append_bytes(strbuffer_t *strbuff, const char *data, int size);
-
-char strbuffer_pop(strbuffer_t *strbuff);
-
-#endif

+ 0 - 190
compat/jansson/utf.c

@@ -1,190 +0,0 @@
-/*
- * Copyright (c) 2009-2011 Petri Lehtinen <petri@digip.org>
- *
- * Jansson is free software; you can redistribute it and/or modify
- * it under the terms of the MIT license. See LICENSE for details.
- */
-
-#include <string.h>
-#include "utf.h"
-
-int utf8_encode(int32_t codepoint, char *buffer, int *size)
-{
-    if(codepoint < 0)
-        return -1;
-    else if(codepoint < 0x80)
-    {
-        buffer[0] = (char)codepoint;
-        *size = 1;
-    }
-    else if(codepoint < 0x800)
-    {
-        buffer[0] = 0xC0 + ((codepoint & 0x7C0) >> 6);
-        buffer[1] = 0x80 + ((codepoint & 0x03F));
-        *size = 2;
-    }
-    else if(codepoint < 0x10000)
-    {
-        buffer[0] = 0xE0 + ((codepoint & 0xF000) >> 12);
-        buffer[1] = 0x80 + ((codepoint & 0x0FC0) >> 6);
-        buffer[2] = 0x80 + ((codepoint & 0x003F));
-        *size = 3;
-    }
-    else if(codepoint <= 0x10FFFF)
-    {
-        buffer[0] = 0xF0 + ((codepoint & 0x1C0000) >> 18);
-        buffer[1] = 0x80 + ((codepoint & 0x03F000) >> 12);
-        buffer[2] = 0x80 + ((codepoint & 0x000FC0) >> 6);
-        buffer[3] = 0x80 + ((codepoint & 0x00003F));
-        *size = 4;
-    }
-    else
-        return -1;
-
-    return 0;
-}
-
-int utf8_check_first(char byte)
-{
-    unsigned char u = (unsigned char)byte;
-
-    if(u < 0x80)
-        return 1;
-
-    if(0x80 <= u && u <= 0xBF) {
-        /* second, third or fourth byte of a multi-byte
-           sequence, i.e. a "continuation byte" */
-        return 0;
-    }
-    else if(u == 0xC0 || u == 0xC1) {
-        /* overlong encoding of an ASCII byte */
-        return 0;
-    }
-    else if(0xC2 <= u && u <= 0xDF) {
-        /* 2-byte sequence */
-        return 2;
-    }
-
-    else if(0xE0 <= u && u <= 0xEF) {
-        /* 3-byte sequence */
-        return 3;
-    }
-    else if(0xF0 <= u && u <= 0xF4) {
-        /* 4-byte sequence */
-        return 4;
-    }
-    else { /* u >= 0xF5 */
-        /* Restricted (start of 4-, 5- or 6-byte sequence) or invalid
-           UTF-8 */
-        return 0;
-    }
-}
-
-int utf8_check_full(const char *buffer, int size, int32_t *codepoint)
-{
-    int i;
-    int32_t value = 0;
-    unsigned char u = (unsigned char)buffer[0];
-
-    if(size == 2)
-    {
-        value = u & 0x1F;
-    }
-    else if(size == 3)
-    {
-        value = u & 0xF;
-    }
-    else if(size == 4)
-    {
-        value = u & 0x7;
-    }
-    else
-        return 0;
-
-    for(i = 1; i < size; i++)
-    {
-        u = (unsigned char)buffer[i];
-
-        if(u < 0x80 || u > 0xBF) {
-            /* not a continuation byte */
-            return 0;
-        }
-
-        value = (value << 6) + (u & 0x3F);
-    }
-
-    if(value > 0x10FFFF) {
-        /* not in Unicode range */
-        return 0;
-    }
-
-    else if(0xD800 <= value && value <= 0xDFFF) {
-        /* invalid code point (UTF-16 surrogate halves) */
-        return 0;
-    }
-
-    else if((size == 2 && value < 0x80) ||
-            (size == 3 && value < 0x800) ||
-            (size == 4 && value < 0x10000)) {
-        /* overlong encoding */
-        return 0;
-    }
-
-    if(codepoint)
-        *codepoint = value;
-
-    return 1;
-}
-
-const char *utf8_iterate(const char *buffer, int32_t *codepoint)
-{
-    int count;
-    int32_t value;
-
-    if(!*buffer)
-        return buffer;
-
-    count = utf8_check_first(buffer[0]);
-    if(count <= 0)
-        return NULL;
-
-    if(count == 1)
-        value = (unsigned char)buffer[0];
-    else
-    {
-        if(!utf8_check_full(buffer, count, &value))
-            return NULL;
-    }
-
-    if(codepoint)
-        *codepoint = value;
-
-    return buffer + count;
-}
-
-int utf8_check_string(const char *string, int length)
-{
-    int i;
-
-    if(length == -1)
-        length = strlen(string);
-
-    for(i = 0; i < length; i++)
-    {
-        int count = utf8_check_first(string[i]);
-        if(count == 0)
-            return 0;
-        else if(count > 1)
-        {
-            if(i + count > length)
-                return 0;
-
-            if(!utf8_check_full(&string[i], count, NULL))
-                return 0;
-
-            i += count - 1;
-        }
-    }
-
-    return 1;
-}

+ 0 - 39
compat/jansson/utf.h

@@ -1,39 +0,0 @@
-/*
- * Copyright (c) 2009-2011 Petri Lehtinen <petri@digip.org>
- *
- * Jansson is free software; you can redistribute it and/or modify
- * it under the terms of the MIT license. See LICENSE for details.
- */
-
-#ifndef UTF_H
-#define UTF_H
-
-#ifdef HAVE_CONFIG_H
-#include <config.h>
-
-#ifdef HAVE_INTTYPES_H
-/* inttypes.h includes stdint.h in a standard environment, so there's
-no need to include stdint.h separately. If inttypes.h doesn't define
-int32_t, it's defined in config.h. */
-#include <inttypes.h>
-#endif /* HAVE_INTTYPES_H */
-
-#else /* !HAVE_CONFIG_H */
-#ifdef _WIN32
-typedef int int32_t;
-#else /* !_WIN32 */
-/* Assume a standard environment */
-#include <inttypes.h>
-#endif /* _WIN32 */
-
-#endif /* HAVE_CONFIG_H */
-
-int utf8_encode(int codepoint, char *buffer, int *size);
-
-int utf8_check_first(char byte);
-int utf8_check_full(const char *buffer, int size, int32_t *codepoint);
-const char *utf8_iterate(const char *buffer, int32_t *codepoint);
-
-int utf8_check_string(const char *string, int length);
-
-#endif

+ 0 - 13
compat/jansson/util.h

@@ -1,13 +0,0 @@
-/*
- * Copyright (c) 2009, 2010 Petri Lehtinen <petri@digip.org>
- *
- * Jansson is free software; you can redistribute it and/or modify
- * it under the terms of the MIT license. See LICENSE for details.
- */
-
-#ifndef UTIL_H
-#define UTIL_H
-
-#define max(a, b)  ((a) > (b) ? (a) : (b))
-
-#endif

+ 0 - 983
compat/jansson/value.c

@@ -1,983 +0,0 @@
-/*
- * Copyright (c) 2009-2011 Petri Lehtinen <petri@digip.org>
- *
- * Jansson is free software; you can redistribute it and/or modify
- * it under the terms of the MIT license. See LICENSE for details.
- */
-
-#define _GNU_SOURCE
-
-#include <stddef.h>
-#include <stdlib.h>
-#include <string.h>
-
-#include <jansson.h>
-#include "hashtable.h"
-#include "jansson_private.h"
-#include "utf.h"
-
-
-static JSON_INLINE void json_init(json_t *json, json_type type)
-{
-    json->type = type;
-    json->refcount = 1;
-}
-
-
-/*** object ***/
-
-/* From http://www.cse.yorku.ca/~oz/hash.html */
-size_t jsonp_hash_str(const void *ptr)
-{
-    const char *str = (const char *)ptr;
-
-    size_t hash = 5381;
-    size_t c;
-
-    while((c = (size_t)*str))
-    {
-        hash = ((hash << 5) + hash) + c;
-        str++;
-    }
-
-    return hash;
-}
-
-int jsonp_str_equal(const void *ptr1, const void *ptr2)
-{
-    return strcmp((const char *)ptr1, (const char *)ptr2) == 0;
-}
-
-/* This macro just returns a pointer that's a few bytes backwards from
-   string. This makes it possible to pass a pointer to object_key_t
-   when only the string inside it is used, without actually creating
-   an object_key_t instance. */
-#define string_to_key(string)  container_of(string, object_key_t, key)
-
-static size_t hash_key(const void *ptr)
-{
-    return jsonp_hash_str(((const object_key_t *)ptr)->key);
-}
-
-static int key_equal(const void *ptr1, const void *ptr2)
-{
-    return jsonp_str_equal(((const object_key_t *)ptr1)->key,
-                           ((const object_key_t *)ptr2)->key);
-}
-
-static void value_decref(void *value)
-{
-    json_decref((json_t *)value);
-}
-
-json_t *json_object(void)
-{
-    json_object_t *object = jsonp_malloc(sizeof(json_object_t));
-    if(!object)
-        return NULL;
-    json_init(&object->json, JSON_OBJECT);
-
-    if(hashtable_init(&object->hashtable,
-                      hash_key, key_equal,
-                      jsonp_free, value_decref))
-    {
-        jsonp_free(object);
-        return NULL;
-    }
-
-    object->serial = 0;
-    object->visited = 0;
-
-    return &object->json;
-}
-
-static void json_delete_object(json_object_t *object)
-{
-    hashtable_close(&object->hashtable);
-    jsonp_free(object);
-}
-
-size_t json_object_size(const json_t *json)
-{
-    json_object_t *object;
-
-    if(!json_is_object(json))
-        return 0;
-
-    object = json_to_object(json);
-    return object->hashtable.size;
-}
-
-json_t *json_object_get(const json_t *json, const char *key)
-{
-    json_object_t *object;
-
-    if(!json_is_object(json))
-        return NULL;
-
-    object = json_to_object(json);
-    return hashtable_get(&object->hashtable, string_to_key(key));
-}
-
-int json_object_set_new_nocheck(json_t *json, const char *key, json_t *value)
-{
-    json_object_t *object;
-    object_key_t *k;
-
-    if(!key || !value)
-        return -1;
-
-    if(!json_is_object(json) || json == value)
-    {
-        json_decref(value);
-        return -1;
-    }
-    object = json_to_object(json);
-
-    /* offsetof(...) returns the size of object_key_t without the
-       last, flexible member. This way, the correct amount is
-       allocated. */
-    k = jsonp_malloc(offsetof(object_key_t, key) + strlen(key) + 1);
-    if(!k)
-    {
-        json_decref(value);
-        return -1;
-    }
-
-    k->serial = object->serial++;
-    strcpy(k->key, key);
-
-    if(hashtable_set(&object->hashtable, k, value))
-    {
-        json_decref(value);
-        return -1;
-    }
-
-    return 0;
-}
-
-int json_object_set_new(json_t *json, const char *key, json_t *value)
-{
-    if(!key || !utf8_check_string(key, -1))
-    {
-        json_decref(value);
-        return -1;
-    }
-
-    return json_object_set_new_nocheck(json, key, value);
-}
-
-int json_object_del(json_t *json, const char *key)
-{
-    json_object_t *object;
-
-    if(!json_is_object(json))
-        return -1;
-
-    object = json_to_object(json);
-    return hashtable_del(&object->hashtable, string_to_key(key));
-}
-
-int json_object_clear(json_t *json)
-{
-    json_object_t *object;
-
-    if(!json_is_object(json))
-        return -1;
-
-    object = json_to_object(json);
-    hashtable_clear(&object->hashtable);
-
-    return 0;
-}
-
-int json_object_update(json_t *object, json_t *other)
-{
-    void *iter;
-
-    if(!json_is_object(object) || !json_is_object(other))
-        return -1;
-
-    iter = json_object_iter(other);
-    while(iter) {
-        const char *key;
-        json_t *value;
-
-        key = json_object_iter_key(iter);
-        value = json_object_iter_value(iter);
-
-        if(json_object_set_nocheck(object, key, value))
-            return -1;
-
-        iter = json_object_iter_next(other, iter);
-    }
-
-    return 0;
-}
-
-void *json_object_iter(json_t *json)
-{
-    json_object_t *object;
-
-    if(!json_is_object(json))
-        return NULL;
-
-    object = json_to_object(json);
-    return hashtable_iter(&object->hashtable);
-}
-
-void *json_object_iter_at(json_t *json, const char *key)
-{
-    json_object_t *object;
-
-    if(!key || !json_is_object(json))
-        return NULL;
-
-    object = json_to_object(json);
-    return hashtable_iter_at(&object->hashtable, string_to_key(key));
-}
-
-void *json_object_iter_next(json_t *json, void *iter)
-{
-    json_object_t *object;
-
-    if(!json_is_object(json) || iter == NULL)
-        return NULL;
-
-    object = json_to_object(json);
-    return hashtable_iter_next(&object->hashtable, iter);
-}
-
-const object_key_t *jsonp_object_iter_fullkey(void *iter)
-{
-    if(!iter)
-        return NULL;
-
-    return hashtable_iter_key(iter);
-}
-
-const char *json_object_iter_key(void *iter)
-{
-    if(!iter)
-        return NULL;
-
-    return jsonp_object_iter_fullkey(iter)->key;
-}
-
-json_t *json_object_iter_value(void *iter)
-{
-    if(!iter)
-        return NULL;
-
-    return (json_t *)hashtable_iter_value(iter);
-}
-
-int json_object_iter_set_new(json_t *json, void *iter, json_t *value)
-{
-    json_object_t *object;
-
-    if(!json_is_object(json) || !iter || !value)
-        return -1;
-
-    object = json_to_object(json);
-    hashtable_iter_set(&object->hashtable, iter, value);
-
-    return 0;
-}
-
-static int json_object_equal(json_t *object1, json_t *object2)
-{
-    void *iter;
-
-    if(json_object_size(object1) != json_object_size(object2))
-        return 0;
-
-    iter = json_object_iter(object1);
-    while(iter)
-    {
-        const char *key;
-        json_t *value1, *value2;
-
-        key = json_object_iter_key(iter);
-        value1 = json_object_iter_value(iter);
-        value2 = json_object_get(object2, key);
-
-        if(!json_equal(value1, value2))
-            return 0;
-
-        iter = json_object_iter_next(object1, iter);
-    }
-
-    return 1;
-}
-
-static json_t *json_object_copy(json_t *object)
-{
-    json_t *result;
-    void *iter;
-
-    result = json_object();
-    if(!result)
-        return NULL;
-
-    iter = json_object_iter(object);
-    while(iter)
-    {
-        const char *key;
-        json_t *value;
-
-        key = json_object_iter_key(iter);
-        value = json_object_iter_value(iter);
-        json_object_set_nocheck(result, key, value);
-
-        iter = json_object_iter_next(object, iter);
-    }
-
-    return result;
-}
-
-static json_t *json_object_deep_copy(json_t *object)
-{
-    json_t *result;
-    void *iter;
-
-    result = json_object();
-    if(!result)
-        return NULL;
-
-    iter = json_object_iter(object);
-    while(iter)
-    {
-        const char *key;
-        json_t *value;
-
-        key = json_object_iter_key(iter);
-        value = json_object_iter_value(iter);
-        json_object_set_new_nocheck(result, key, json_deep_copy(value));
-
-        iter = json_object_iter_next(object, iter);
-    }
-
-    return result;
-}
-
-
-/*** array ***/
-
-json_t *json_array(void)
-{
-    json_array_t *array = jsonp_malloc(sizeof(json_array_t));
-    if(!array)
-        return NULL;
-    json_init(&array->json, JSON_ARRAY);
-
-    array->entries = 0;
-    array->size = 8;
-
-    array->table = jsonp_malloc(array->size * sizeof(json_t *));
-    if(!array->table) {
-        jsonp_free(array);
-        return NULL;
-    }
-
-    array->visited = 0;
-
-    return &array->json;
-}
-
-static void json_delete_array(json_array_t *array)
-{
-    size_t i;
-
-    for(i = 0; i < array->entries; i++)
-        json_decref(array->table[i]);
-
-    jsonp_free(array->table);
-    jsonp_free(array);
-}
-
-size_t json_array_size(const json_t *json)
-{
-    if(!json_is_array(json))
-        return 0;
-
-    return json_to_array(json)->entries;
-}
-
-json_t *json_array_get(const json_t *json, size_t index)
-{
-    json_array_t *array;
-    if(!json_is_array(json))
-        return NULL;
-    array = json_to_array(json);
-
-    if(index >= array->entries)
-        return NULL;
-
-    return array->table[index];
-}
-
-int json_array_set_new(json_t *json, size_t index, json_t *value)
-{
-    json_array_t *array;
-
-    if(!value)
-        return -1;
-
-    if(!json_is_array(json) || json == value)
-    {
-        json_decref(value);
-        return -1;
-    }
-    array = json_to_array(json);
-
-    if(index >= array->entries)
-    {
-        json_decref(value);
-        return -1;
-    }
-
-    json_decref(array->table[index]);
-    array->table[index] = value;
-
-    return 0;
-}
-
-static void array_move(json_array_t *array, size_t dest,
-                       size_t src, size_t count)
-{
-    memmove(&array->table[dest], &array->table[src], count * sizeof(json_t *));
-}
-
-static void array_copy(json_t **dest, size_t dpos,
-                       json_t **src, size_t spos,
-                       size_t count)
-{
-    memcpy(&dest[dpos], &src[spos], count * sizeof(json_t *));
-}
-
-static json_t **json_array_grow(json_array_t *array,
-                                size_t amount,
-                                int copy)
-{
-    size_t new_size;
-    json_t **old_table, **new_table;
-
-    if(array->entries + amount <= array->size)
-        return array->table;
-
-    old_table = array->table;
-
-    new_size = max(array->size + amount, array->size * 2);
-    new_table = jsonp_malloc(new_size * sizeof(json_t *));
-    if(!new_table)
-        return NULL;
-
-    array->size = new_size;
-    array->table = new_table;
-
-    if(copy) {
-        array_copy(array->table, 0, old_table, 0, array->entries);
-        jsonp_free(old_table);
-        return array->table;
-    }
-
-    return old_table;
-}
-
-int json_array_append_new(json_t *json, json_t *value)
-{
-    json_array_t *array;
-
-    if(!value)
-        return -1;
-
-    if(!json_is_array(json) || json == value)
-    {
-        json_decref(value);
-        return -1;
-    }
-    array = json_to_array(json);
-
-    if(!json_array_grow(array, 1, 1)) {
-        json_decref(value);
-        return -1;
-    }
-
-    array->table[array->entries] = value;
-    array->entries++;
-
-    return 0;
-}
-
-int json_array_insert_new(json_t *json, size_t index, json_t *value)
-{
-    json_array_t *array;
-    json_t **old_table;
-
-    if(!value)
-        return -1;
-
-    if(!json_is_array(json) || json == value) {
-        json_decref(value);
-        return -1;
-    }
-    array = json_to_array(json);
-
-    if(index > array->entries) {
-        json_decref(value);
-        return -1;
-    }
-
-    old_table = json_array_grow(array, 1, 0);
-    if(!old_table) {
-        json_decref(value);
-        return -1;
-    }
-
-    if(old_table != array->table) {
-        array_copy(array->table, 0, old_table, 0, index);
-        array_copy(array->table, index + 1, old_table, index,
-                   array->entries - index);
-        jsonp_free(old_table);
-    }
-    else
-        array_move(array, index + 1, index, array->entries - index);
-
-    array->table[index] = value;
-    array->entries++;
-
-    return 0;
-}
-
-int json_array_remove(json_t *json, size_t index)
-{
-    json_array_t *array;
-
-    if(!json_is_array(json))
-        return -1;
-    array = json_to_array(json);
-
-    if(index >= array->entries)
-        return -1;
-
-    json_decref(array->table[index]);
-
-    array_move(array, index, index + 1, array->entries - index);
-    array->entries--;
-
-    return 0;
-}
-
-int json_array_clear(json_t *json)
-{
-    json_array_t *array;
-    size_t i;
-
-    if(!json_is_array(json))
-        return -1;
-    array = json_to_array(json);
-
-    for(i = 0; i < array->entries; i++)
-        json_decref(array->table[i]);
-
-    array->entries = 0;
-    return 0;
-}
-
-int json_array_extend(json_t *json, json_t *other_json)
-{
-    json_array_t *array, *other;
-    size_t i;
-
-    if(!json_is_array(json) || !json_is_array(other_json))
-        return -1;
-    array = json_to_array(json);
-    other = json_to_array(other_json);
-
-    if(!json_array_grow(array, other->entries, 1))
-        return -1;
-
-    for(i = 0; i < other->entries; i++)
-        json_incref(other->table[i]);
-
-    array_copy(array->table, array->entries, other->table, 0, other->entries);
-
-    array->entries += other->entries;
-    return 0;
-}
-
-static int json_array_equal(json_t *array1, json_t *array2)
-{
-    size_t i, size;
-
-    size = json_array_size(array1);
-    if(size != json_array_size(array2))
-        return 0;
-
-    for(i = 0; i < size; i++)
-    {
-        json_t *value1, *value2;
-
-        value1 = json_array_get(array1, i);
-        value2 = json_array_get(array2, i);
-
-        if(!json_equal(value1, value2))
-            return 0;
-    }
-
-    return 1;
-}
-
-static json_t *json_array_copy(json_t *array)
-{
-    json_t *result;
-    size_t i;
-
-    result = json_array();
-    if(!result)
-        return NULL;
-
-    for(i = 0; i < json_array_size(array); i++)
-        json_array_append(result, json_array_get(array, i));
-
-    return result;
-}
-
-static json_t *json_array_deep_copy(json_t *array)
-{
-    json_t *result;
-    size_t i;
-
-    result = json_array();
-    if(!result)
-        return NULL;
-
-    for(i = 0; i < json_array_size(array); i++)
-        json_array_append_new(result, json_deep_copy(json_array_get(array, i)));
-
-    return result;
-}
-
-/*** string ***/
-
-json_t *json_string_nocheck(const char *value)
-{
-    json_string_t *string;
-
-    if(!value)
-        return NULL;
-
-    string = jsonp_malloc(sizeof(json_string_t));
-    if(!string)
-        return NULL;
-    json_init(&string->json, JSON_STRING);
-
-    string->value = jsonp_strdup(value);
-    if(!string->value) {
-        jsonp_free(string);
-        return NULL;
-    }
-
-    return &string->json;
-}
-
-json_t *json_string(const char *value)
-{
-    if(!value || !utf8_check_string(value, -1))
-        return NULL;
-
-    return json_string_nocheck(value);
-}
-
-const char *json_string_value(const json_t *json)
-{
-    if(!json_is_string(json))
-        return NULL;
-
-    return json_to_string(json)->value;
-}
-
-int json_string_set_nocheck(json_t *json, const char *value)
-{
-    char *dup;
-    json_string_t *string;
-
-    dup = jsonp_strdup(value);
-    if(!dup)
-        return -1;
-
-    string = json_to_string(json);
-    jsonp_free(string->value);
-    string->value = dup;
-
-    return 0;
-}
-
-int json_string_set(json_t *json, const char *value)
-{
-    if(!value || !utf8_check_string(value, -1))
-        return -1;
-
-    return json_string_set_nocheck(json, value);
-}
-
-static void json_delete_string(json_string_t *string)
-{
-    jsonp_free(string->value);
-    jsonp_free(string);
-}
-
-static int json_string_equal(json_t *string1, json_t *string2)
-{
-    return strcmp(json_string_value(string1), json_string_value(string2)) == 0;
-}
-
-static json_t *json_string_copy(json_t *string)
-{
-    return json_string_nocheck(json_string_value(string));
-}
-
-
-/*** integer ***/
-
-json_t *json_integer(json_int_t value)
-{
-    json_integer_t *integer = jsonp_malloc(sizeof(json_integer_t));
-    if(!integer)
-        return NULL;
-    json_init(&integer->json, JSON_INTEGER);
-
-    integer->value = value;
-    return &integer->json;
-}
-
-json_int_t json_integer_value(const json_t *json)
-{
-    if(!json_is_integer(json))
-        return 0;
-
-    return json_to_integer(json)->value;
-}
-
-int json_integer_set(json_t *json, json_int_t value)
-{
-    if(!json_is_integer(json))
-        return -1;
-
-    json_to_integer(json)->value = value;
-
-    return 0;
-}
-
-static void json_delete_integer(json_integer_t *integer)
-{
-    jsonp_free(integer);
-}
-
-static int json_integer_equal(json_t *integer1, json_t *integer2)
-{
-    return json_integer_value(integer1) == json_integer_value(integer2);
-}
-
-static json_t *json_integer_copy(json_t *integer)
-{
-    return json_integer(json_integer_value(integer));
-}
-
-
-/*** real ***/
-
-json_t *json_real(double value)
-{
-    json_real_t *real = jsonp_malloc(sizeof(json_real_t));
-    if(!real)
-        return NULL;
-    json_init(&real->json, JSON_REAL);
-
-    real->value = value;
-    return &real->json;
-}
-
-double json_real_value(const json_t *json)
-{
-    if(!json_is_real(json))
-        return 0;
-
-    return json_to_real(json)->value;
-}
-
-int json_real_set(json_t *json, double value)
-{
-    if(!json_is_real(json))
-        return 0;
-
-    json_to_real(json)->value = value;
-
-    return 0;
-}
-
-static void json_delete_real(json_real_t *real)
-{
-    jsonp_free(real);
-}
-
-static int json_real_equal(json_t *real1, json_t *real2)
-{
-    return json_real_value(real1) == json_real_value(real2);
-}
-
-static json_t *json_real_copy(json_t *real)
-{
-    return json_real(json_real_value(real));
-}
-
-
-/*** number ***/
-
-double json_number_value(const json_t *json)
-{
-    if(json_is_integer(json))
-        return json_integer_value(json);
-    else if(json_is_real(json))
-        return json_real_value(json);
-    else
-        return 0.0;
-}
-
-
-/*** simple values ***/
-
-json_t *json_true(void)
-{
-    static json_t the_true = {JSON_TRUE, (size_t)-1};
-    return &the_true;
-}
-
-
-json_t *json_false(void)
-{
-    static json_t the_false = {JSON_FALSE, (size_t)-1};
-    return &the_false;
-}
-
-
-json_t *json_null(void)
-{
-    static json_t the_null = {JSON_NULL, (size_t)-1};
-    return &the_null;
-}
-
-
-/*** deletion ***/
-
-void json_delete(json_t *json)
-{
-    if(json_is_object(json))
-        json_delete_object(json_to_object(json));
-
-    else if(json_is_array(json))
-        json_delete_array(json_to_array(json));
-
-    else if(json_is_string(json))
-        json_delete_string(json_to_string(json));
-
-    else if(json_is_integer(json))
-        json_delete_integer(json_to_integer(json));
-
-    else if(json_is_real(json))
-        json_delete_real(json_to_real(json));
-
-    /* json_delete is not called for true, false or null */
-}
-
-
-/*** equality ***/
-
-int json_equal(json_t *json1, json_t *json2)
-{
-    if(!json1 || !json2)
-        return 0;
-
-    if(json_typeof(json1) != json_typeof(json2))
-        return 0;
-
-    /* this covers true, false and null as they are singletons */
-    if(json1 == json2)
-        return 1;
-
-    if(json_is_object(json1))
-        return json_object_equal(json1, json2);
-
-    if(json_is_array(json1))
-        return json_array_equal(json1, json2);
-
-    if(json_is_string(json1))
-        return json_string_equal(json1, json2);
-
-    if(json_is_integer(json1))
-        return json_integer_equal(json1, json2);
-
-    if(json_is_real(json1))
-        return json_real_equal(json1, json2);
-
-    return 0;
-}
-
-
-/*** copying ***/
-
-json_t *json_copy(json_t *json)
-{
-    if(!json)
-        return NULL;
-
-    if(json_is_object(json))
-        return json_object_copy(json);
-
-    if(json_is_array(json))
-        return json_array_copy(json);
-
-    if(json_is_string(json))
-        return json_string_copy(json);
-
-    if(json_is_integer(json))
-        return json_integer_copy(json);
-
-    if(json_is_real(json))
-        return json_real_copy(json);
-
-    if(json_is_true(json) || json_is_false(json) || json_is_null(json))
-        return json;
-
-    return NULL;
-}
-
-json_t *json_deep_copy(json_t *json)
-{
-    if(!json)
-        return NULL;
-
-    if(json_is_object(json))
-        return json_object_deep_copy(json);
-
-    if(json_is_array(json))
-        return json_array_deep_copy(json);
-
-    /* for the rest of the types, deep copying doesn't differ from
-       shallow copying */
-
-    if(json_is_string(json))
-        return json_string_copy(json);
-
-    if(json_is_integer(json))
-        return json_integer_copy(json);
-
-    if(json_is_real(json))
-        return json_real_copy(json);
-
-    if(json_is_true(json) || json_is_false(json) || json_is_null(json))
-        return json;
-
-    return NULL;
-}

+ 6 - 10
configure.ac

@@ -134,7 +134,8 @@ AC_CHECK_LIB(pthread, pthread_create, ,
         AC_MSG_ERROR([Could not find pthread library - please install libpthread]))
 PTHREAD_LIBS=-lpthread
 
-AC_CHECK_LIB(jansson, json_loads, request_jansson=false, request_jansson=true)
+AC_CHECK_LIB(jansson, json_loads, ,
+	AC_MSG_ERROR([Could not find jansson library]))
 
 if test "x$opencl" = xyes; then
 	adl="yes"
@@ -248,19 +249,16 @@ else
 fi
 
 
+AC_CONFIG_SUBDIRS([libblkmaker])
+
+AM_CONDITIONAL([NEED_LIBBLKMAKER], [true])
 AM_CONDITIONAL([NEED_FPGAUTILS], [test x$icarus$bitforce$modminer$ztex != xnononono])
 AM_CONDITIONAL([HAS_SCRYPT], [test x$scrypt = xyes])
 AM_CONDITIONAL([HAVE_CURSES], [test x$curses = xyes])
-AM_CONDITIONAL([WANT_JANSSON], [test x$request_jansson = xtrue])
 AM_CONDITIONAL([HAVE_WINDOWS], [test x$have_win32 = xtrue])
 AM_CONDITIONAL([HAVE_x86_64], [test x$have_x86_64 = xtrue])
 
-if test x$request_jansson = xtrue
-then
-	JANSSON_LIBS="compat/jansson/libjansson.a"
-else
-	JANSSON_LIBS=-ljansson
-fi
+JANSSON_LIBS=-ljansson
 
 dnl Find YASM
 has_yasm=false
@@ -384,8 +382,6 @@ AC_SUBST(USB_FLAGS)
 
 AC_CONFIG_FILES([
 	Makefile
-	compat/Makefile
-	compat/jansson/Makefile
 	x86_64/Makefile
 	x86_32/Makefile
 	ccan/Makefile

+ 1 - 0
libblkmaker

@@ -0,0 +1 @@
+Subproject commit 243684e40d203bed04a04d665bacab701860a282

+ 8 - 0
make-release

@@ -18,6 +18,7 @@ cp -v \
 	pdcurses.dll \
 	libcurl-4.dll \
 	pthreadGC2.dll \
+	libjansson-4.dll \
 	libusb-1.0.dll \
 	zlib1.dll \
 	"$OUTDIR/"
@@ -25,8 +26,14 @@ git branch TMP "$tag"
 git clone . "$TMPDIR" -b TMP --depth 1
 git branch -D TMP
 cd "$TMPDIR"
+git submodule update --init
 git archive --prefix "$sw"/ --format tar "$tag" | tar xvp
+(
+	cd libblkmaker
+	git archive --prefix "libblkmaker/" --format tar HEAD | tar -C "../$sw" -xvp
+)
 cd "$sw"
+NOSUBMODULES=1 \
 NOCONFIGURE=1 \
 ./autogen.sh
 cd ..
@@ -55,6 +62,7 @@ NOCONFIGURE=1 \
 make -j4
 cp -v \
 	bfgminer.exe \
+	libblkmaker/.libs/*.dll \
 	*.cl \
 	example.conf \
 	windows-build.txt \

+ 321 - 45
miner.c

@@ -33,6 +33,9 @@
 
 #ifndef WIN32
 #include <sys/resource.h>
+#include <sys/socket.h>
+#else
+#include <winsock2.h>
 #endif
 #include <ccan/opt/opt.h>
 #include <jansson.h>
@@ -40,6 +43,10 @@
 #include <libgen.h>
 #include <sha2.h>
 
+#include <blkmaker.h>
+#include <blkmaker_jansson.h>
+#include <blktemplate.h>
+
 #include "compat.h"
 #include "miner.h"
 #include "findnonce.h"
@@ -88,6 +95,7 @@ static char packagename[255];
 bool opt_protocol;
 static bool opt_benchmark;
 static bool want_longpoll = true;
+static bool want_gbt = true;
 bool have_longpoll;
 bool want_per_device_stats;
 bool use_syslog;
@@ -422,6 +430,8 @@ static struct pool *add_pool(void)
 
 	pool->rpc_proxy = NULL;
 
+	pool->lp_socket = CURL_SOCKET_BAD;
+
 	pools = realloc(pools, sizeof(struct pool *) * (total_pools + 2));
 	pools[total_pools++] = pool;
 
@@ -1034,6 +1044,9 @@ static struct opt_table opt_config_table[] = {
 			opt_hidden
 #endif
 	),
+	OPT_WITHOUT_ARG("--no-gbt",
+			opt_set_invbool, &want_gbt,
+			"Disable getblocktemplate support"),
 	OPT_WITHOUT_ARG("--no-longpoll",
 			opt_set_invbool, &want_longpoll,
 			"Disable X-Long-Polling support"),
@@ -1445,6 +1458,41 @@ static bool work_decode(const json_t *val, struct work *work)
 			detect_algo = 2;
 	}
 	
+	if (work->tmpl) {
+		const char *err = blktmpl_add_jansson(work->tmpl, val, time(NULL));
+		if (err) {
+			applog(LOG_ERR, "blktmpl error: %s", err);
+			goto err_out;
+		}
+		work->rolltime = blkmk_time_left(work->tmpl, time(NULL));
+		if (blkmk_get_data(work->tmpl, work->data, 80, time(NULL), NULL, &work->dataid) < 76)
+			goto err_out;
+		swap32yes(work->data, work->data, 80 / 4);
+		memcpy(&work->data[80], "\0\0\0\x80\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\x80\x02\0\0", 48);
+
+		const struct blktmpl_longpoll_req *lp;
+		struct pool *pool = work->pool;
+		if ((lp = blktmpl_get_longpoll(work->tmpl)) && ((!pool->lp_id) || strcmp(lp->id, pool->lp_id))) {
+			free(pool->lp_id);
+			pool->lp_id = strdup(lp->id);
+
+#if 0  /* This just doesn't work :( */
+			curl_socket_t sock = pool->lp_socket;
+			if (sock != CURL_SOCKET_BAD) {
+				pool->lp_socket = CURL_SOCKET_BAD;
+				applog(LOG_WARNING, "Pool %u long poll request hanging, reconnecting", pool->pool_no);
+				shutdown(sock,
+#ifndef WIN32
+				         SHUT_RDWR
+#else
+				         SD_BOTH
+#endif
+				);
+			}
+#endif
+		}
+	}
+	else
 	if (unlikely(!jobj_binary(val, "data", work->data, sizeof(work->data), true))) {
 		applog(LOG_ERR, "JSON inval data");
 		goto err_out;
@@ -1465,6 +1513,15 @@ static bool work_decode(const json_t *val, struct work *work)
 		applog(LOG_ERR, "JSON inval target");
 		goto err_out;
 	}
+	if (work->tmpl) {
+		for (size_t i = 0; i < sizeof(work->target) / 2; ++i)
+		{
+			int p = (sizeof(work->target) - 1) - i;
+			unsigned char c = work->target[i];
+			work->target[i] = work->target[p];
+			work->target[p] = c;
+		}
+	}
 	
 	for (i = 32; i--; )
 	{
@@ -2033,7 +2090,7 @@ static bool submit_upstream_work(const struct work *work, CURL *curl, bool resub
 {
 	char *hexstr = NULL;
 	json_t *val, *res;
-	char s[345], sd[345];
+	char *s, *sd;
 	bool rc = false;
 	int thr_id = work->thr_id;
 	struct cgpu_info *cgpu = thr_info[thr_id].cgpu;
@@ -2042,6 +2099,16 @@ static bool submit_upstream_work(const struct work *work, CURL *curl, bool resub
 	uint32_t *hash32;
 	char hashshow[64+1] = "";
 
+	if (work->tmpl) {
+		unsigned char data[76];
+		swap32yes(data, work->data, 76);
+		json_t *req = blkmk_submit_jansson(work->tmpl, data, work->dataid, *((uint32_t*)&work->data[76]));
+		s = json_dumps(req, 0);
+		sd = bin2hex(data, 80);
+	} else {
+		s  = malloc(345);
+		sd = malloc(345);
+
 	/* build hex string */
 	hexstr = bin2hex(work->data, sizeof(work->data));
 	if (unlikely(!hexstr)) {
@@ -2057,10 +2124,16 @@ static bool submit_upstream_work(const struct work *work, CURL *curl, bool resub
 	      "{\"method\": \"getwork\", \"params\": [ \"%s\" ], \"id\":1}",
 		hexstr);
 
+	}
+
 	applog(LOG_DEBUG, "DBG: sending %s submit RPC call: %s", pool->rpc_url, sd);
 
 	/* issue JSON-RPC request */
 	val = json_rpc_call(curl, pool->rpc_url, pool->rpc_userpass, s, false, false, &rolltime, pool, true);
+
+	free(s);
+	free(sd);
+
 	if (unlikely(!val)) {
 		applog(LOG_INFO, "submit_upstream_work json_rpc_call failed");
 		if (!pool_tset(pool, &pool->submit_fail)) {
@@ -2087,7 +2160,7 @@ static bool submit_upstream_work(const struct work *work, CURL *curl, bool resub
 	/* Theoretically threads could race when modifying accepted and
 	 * rejected values but the chance of two submits completing at the
 	 * same time is zero so there is no point adding extra locking */
-	if (json_is_true(res)) {
+	if (json_is_null(res) || json_is_true(res)) {
 		cgpu->accepted++;
 		cgpu->accepted_weighed += work->difficulty;
 		total_accepted++;
@@ -2138,6 +2211,7 @@ static bool submit_upstream_work(const struct work *work, CURL *curl, bool resub
 			else
 				strcpy(where, "");
 
+			if (!json_is_string(res))
 			res = json_object_get(val, "reject-reason");
 			if (res) {
 				const char *reasontmp = json_string_value(res);
@@ -2199,7 +2273,7 @@ out_nofree:
 	return rc;
 }
 
-static const char *rpc_req =
+static const char *getwork_rpc_req =
 	"{\"method\": \"getwork\", \"params\": [], \"id\":0}\r\n";
 
 /* In balanced mode, the amount of diff1 solutions per pool is monitored as a
@@ -2269,6 +2343,70 @@ static void get_benchmark_work(struct work *work)
 	work->pool = pools[0];
 }
 
+static char *prepare_rpc_req(struct work *work, enum pool_protocol proto, const char *lpid)
+{
+	char *rpc_req;
+
+	switch (proto) {
+		case PLP_GETWORK:
+			work->tmpl = NULL;
+			return strdup(getwork_rpc_req);
+		case PLP_GETBLOCKTEMPLATE:
+			work->tmpl_refcount = malloc(sizeof(*work->tmpl_refcount));
+			if (!work->tmpl_refcount)
+				return NULL;
+			work->tmpl = blktmpl_create();
+			if (!work->tmpl)
+				goto gbtfail2;
+			*work->tmpl_refcount = 1;
+			gbt_capabilities_t caps = blktmpl_addcaps(work->tmpl);
+			if (!caps)
+				goto gbtfail;
+			caps |= GBT_LONGPOLL;
+			json_t *req = blktmpl_request_jansson(caps, lpid);
+			if (!req)
+				goto gbtfail;
+			rpc_req = json_dumps(req, 0);
+			if (!rpc_req)
+				goto gbtfail;
+			json_decref(req);
+			return rpc_req;
+		default:
+			return NULL;
+	}
+	return NULL;
+
+gbtfail:
+	blktmpl_free(work->tmpl);
+	work->tmpl = NULL;
+gbtfail2:
+	free(work->tmpl_refcount);
+	work->tmpl_refcount = NULL;
+	return NULL;
+}
+
+static const char *pool_protocol_name(enum pool_protocol proto)
+{
+	switch (proto) {
+		case PLP_GETBLOCKTEMPLATE:
+			return "getblocktemplate";
+		case PLP_GETWORK:
+			return "getwork";
+		default:
+			return "UNKNOWN";
+	}
+}
+
+static enum pool_protocol pool_protocol_fallback(enum pool_protocol proto)
+{
+	switch (proto) {
+		case PLP_GETBLOCKTEMPLATE:
+			return PLP_GETWORK;
+		default:
+			return PLP_NONE;
+	}
+}
+
 static bool get_upstream_work(struct work *work, CURL *curl)
 {
 	struct pool *pool = work->pool;
@@ -2277,6 +2415,14 @@ static bool get_upstream_work(struct work *work, CURL *curl)
 	json_t *val = NULL;
 	bool rc = false;
 	char *url;
+	enum pool_protocol proto;
+
+	char *rpc_req;
+
+tryagain:
+	rpc_req = prepare_rpc_req(work, pool->proto, NULL);
+	if (!rpc_req)
+		return false;
 
 	applog(LOG_DEBUG, "DBG: sending %s get RPC call: %s", pool->rpc_url, rpc_req);
 
@@ -2288,10 +2434,16 @@ static bool get_upstream_work(struct work *work, CURL *curl)
 			    false, &work->rolltime, pool, false);
 	pool_stats->getwork_attempts++;
 
+	free(rpc_req);
+
 	if (likely(val)) {
 		rc = work_decode(json_object_get(val, "result"), work);
 		if (unlikely(!rc))
 			applog(LOG_DEBUG, "Failed to decode work in get_upstream_work");
+	} else if (PLP_NONE != (proto = pool_protocol_fallback(pool->proto))) {
+		applog(LOG_WARNING, "Pool %u failed getblocktemplate request; falling back to getwork protocol", pool->pool_no);
+		pool->proto = proto;
+		goto tryagain;
 	} else
 		applog(LOG_DEBUG, "Failed json_rpc_call in get_upstream_work");
 
@@ -2336,6 +2488,16 @@ static struct work *make_work(void)
 
 static void free_work(struct work *work)
 {
+	if (work->tmpl) {
+		struct pool *pool = work->pool;
+		mutex_lock(&pool->pool_lock);
+		bool free_tmpl = !--*work->tmpl_refcount;
+		mutex_unlock(&pool->pool_lock);
+		if (free_tmpl) {
+			blktmpl_free(work->tmpl);
+			free(work->tmpl_refcount);
+		}
+	}
 	free(work);
 }
 
@@ -2597,12 +2759,27 @@ static inline bool should_roll(struct work *work)
  * reject blocks as invalid. */
 static inline bool can_roll(struct work *work)
 {
-	return (work->pool && work->rolltime && !work->clone &&
+	if (!(work->pool && !work->clone))
+		return false;
+	if (work->tmpl) {
+		if (stale_work(work, false))
+			return false;
+		return blkmk_work_left(work->tmpl);
+	}
+	return (work->rolltime &&
 		work->rolls < 7000 && !stale_work(work, false));
 }
 
 static void roll_work(struct work *work)
 {
+	if (work->tmpl) {
+		if (blkmk_get_data(work->tmpl, work->data, 80, time(NULL), NULL, &work->dataid) < 76)
+			applog(LOG_ERR, "Failed to get next data from template; spinning wheels!");
+		swap32yes(work->data, work->data, 80 / 4);
+		calc_midstate(work);
+		applog(LOG_DEBUG, "Successfully rolled extranonce to dataid %u", work->dataid);
+	} else {
+
 	uint32_t *work_ntime;
 	uint32_t ntime;
 
@@ -2610,21 +2787,36 @@ static void roll_work(struct work *work)
 	ntime = be32toh(*work_ntime);
 	ntime++;
 	*work_ntime = htobe32(ntime);
+
+		applog(LOG_DEBUG, "Successfully rolled time header in work");
+	}
+
 	local_work++;
 	work->rolls++;
 	work->blk.nonce = 0;
-	applog(LOG_DEBUG, "Successfully rolled work");
 
 	/* This is now a different work item so it needs a different ID for the
 	 * hashtable */
 	work->id = total_work++;
 }
 
+static void workcpy(struct work *dest, const struct work *work)
+{
+	memcpy(dest, work, sizeof(*dest));
+
+	if (work->tmpl) {
+		struct pool *pool = work->pool;
+		mutex_lock(&pool->pool_lock);
+		++*work->tmpl_refcount;
+		mutex_unlock(&pool->pool_lock);
+	}
+}
+
 static struct work *make_clone(struct work *work)
 {
 	struct work *work_clone = make_work();
 
-	memcpy(work_clone, work, sizeof(struct work));
+	workcpy(work_clone, work);
 	work_clone->clone = true;
 	work_clone->longpoll = false;
 	work_clone->mandatory = false;
@@ -3458,7 +3650,7 @@ static void display_pool_summary(struct pool *pool)
 		wlog("Pool: %s\n", pool->rpc_url);
 		if (pool->solved)
 			wlog("SOLVED %d BLOCK%s!\n", pool->solved, pool->solved > 1 ? "S" : "");
-		wlog("%s own long-poll support\n", pool->hdr_path ? "Has" : "Does not have");
+		wlog("%s own long-poll support\n", pool->lp_url ? "Has" : "Does not have");
 		wlog(" Queued work requests: %d\n", pool->getwork_requested);
 		wlog(" Share submissions: %d\n", pool->accepted + pool->rejected);
 		wlog(" Accepted shares: %d\n", pool->accepted);
@@ -3749,17 +3941,32 @@ updated:
 		wlogprint("%d: ", pool->pool_no);
 		switch (pool->enabled) {
 			case POOL_ENABLED:
-				wlogprint("Enabled ");
+				wlogprint("Enabled  ");
 				break;
 			case POOL_DISABLED:
 				wlogprint("Disabled ");
 				break;
 			case POOL_REJECTING:
-				wlogprint("Rejecting ");
+				wlogprint("Rejectin ");
 				break;
 		}
-		wlogprint("%s Priority %d: %s  User:%s\n",
-			pool->idle? "Dead" : "Alive",
+		if (pool->idle)
+			wlogprint("Dead ");
+		else
+		if (pool->lp_url && pool->proto != pool->lp_proto)
+			wlogprint("Mixed");
+		else
+			switch (pool->proto) {
+				case PLP_GETBLOCKTEMPLATE:
+					wlogprint(" GBT ");
+					break;
+				case PLP_GETWORK:
+					wlogprint("GWork");
+					break;
+				default:
+					wlogprint("Alive");
+			}
+		wlogprint(" Priority %d: %s  User:%s\n",
 			pool->prio,
 			pool->rpc_url, pool->rpc_user);
 		wattroff(logwin, A_BOLD | A_DIM);
@@ -4321,12 +4528,39 @@ out_unlock:
 
 static void *longpoll_thread(void *userdata);
 
+// NOTE: This assumes reference URI is a root
+static char *absolute_uri(char *uri, const char *ref)
+{
+	if (strstr(uri, "://"))
+		return strdup(uri);
+
+	char *copy_start, *abs;
+	bool need_slash = false;
+
+	copy_start = (uri[0] == '/') ? &uri[1] : uri;
+	if (ref[strlen(ref) - 1] != '/')
+		need_slash = true;
+
+	abs = malloc(strlen(ref) + strlen(copy_start) + 2);
+	if (!abs) {
+		applog(LOG_ERR, "Malloc failure in absolute_uri");
+		return NULL;
+	}
+
+	sprintf(abs, "%s%s%s", ref, need_slash ? "/" : "", copy_start);
+
+	return abs;
+}
+
 static bool pool_active(struct pool *pool, bool pinging)
 {
 	bool ret = false;
 	json_t *val;
 	CURL *curl;
 	int rolltime;
+	char *rpc_req;
+	struct work *work;
+	enum pool_protocol proto;
 
 	curl = curl_easy_init();
 	if (unlikely(!curl)) {
@@ -4334,15 +4568,28 @@ static bool pool_active(struct pool *pool, bool pinging)
 		return false;
 	}
 
+	work = make_work();
+	work->pool = pool;
+	proto = want_gbt ? PLP_GETBLOCKTEMPLATE : PLP_GETWORK;
+
+tryagain:
+	rpc_req = prepare_rpc_req(work, proto, NULL);
+	if (!rpc_req)
+		return false;
+
 	applog(LOG_INFO, "Testing pool %s", pool->rpc_url);
 	val = json_rpc_call(curl, pool->rpc_url, pool->rpc_userpass, rpc_req,
 			true, false, &rolltime, pool, false);
 
 	if (val) {
-		struct work *work = make_work();
 		bool rc;
+		json_t *res;
 
-		rc = work_decode(json_object_get(val, "result"), work);
+		res = json_object_get(val, "result");
+		if ((!json_is_object(res)) || (proto == PLP_GETBLOCKTEMPLATE && !json_object_get(res, "bits")))
+			goto badwork;
+
+		rc = work_decode(res, work);
 		if (rc) {
 			applog(LOG_DEBUG, "Successfully retrieved and deciphered work from pool %u %s",
 			       pool->pool_no, pool->rpc_url);
@@ -4356,38 +4603,40 @@ static bool pool_active(struct pool *pool, bool pinging)
 			ret = true;
 			gettimeofday(&pool->tv_idle, NULL);
 		} else {
+badwork:
 			applog(LOG_DEBUG, "Successfully retrieved but FAILED to decipher work from pool %u %s",
 			       pool->pool_no, pool->rpc_url);
+			if (PLP_NONE != (proto = pool_protocol_fallback(proto)))
+				goto tryagain;
 			free_work(work);
+			goto out;
 		}
 		json_decref(val);
 
+		if (proto != pool->proto) {
+			pool->proto = proto;
+			applog(LOG_INFO, "Selected %s protocol for pool %u", pool_protocol_name(proto), pool->pool_no);
+		}
+
 		if (pool->lp_url)
 			goto out;
 
 		/* Decipher the longpoll URL, if any, and store it in ->lp_url */
-		if (pool->hdr_path) {
-			char *copy_start, *hdr_path;
-			bool need_slash = false;
-
-			hdr_path = pool->hdr_path;
-			if (strstr(hdr_path, "://")) {
-				pool->lp_url = hdr_path;
-				hdr_path = NULL;
-			} else {
-				/* absolute path, on current server */
-				copy_start = (*hdr_path == '/') ? (hdr_path + 1) : hdr_path;
-				if (pool->rpc_url[strlen(pool->rpc_url) - 1] != '/')
-					need_slash = true;
-
-				pool->lp_url = malloc(strlen(pool->rpc_url) + strlen(copy_start) + 2);
-				if (!pool->lp_url) {
-					applog(LOG_ERR, "Malloc failure in pool_active");
-					return false;
-				}
 
-				sprintf(pool->lp_url, "%s%s%s", pool->rpc_url, need_slash ? "/" : "", copy_start);
-			}
+		const struct blktmpl_longpoll_req *lp;
+		if (work->tmpl && (lp = blktmpl_get_longpoll(work->tmpl))) {
+			// NOTE: work_decode takes care of lp id
+			pool->lp_url = lp->uri ? absolute_uri(lp->uri, pool->rpc_url) : pool->rpc_url;
+			if (!pool->lp_url)
+				return false;
+			pool->lp_proto = PLP_GETBLOCKTEMPLATE;
+		}
+		else
+		if (pool->hdr_path) {
+			pool->lp_url = absolute_uri(pool->hdr_path, pool->rpc_url);
+			if (!pool->lp_url)
+				return false;
+			pool->lp_proto = PLP_GETWORK;
 		} else
 			pool->lp_url = NULL;
 
@@ -4396,7 +4645,10 @@ static bool pool_active(struct pool *pool, bool pinging)
 			if (unlikely(pthread_create(&pool->longpoll_thread, NULL, longpoll_thread, (void *)pool)))
 				quit(1, "Failed to create pool longpoll thread");
 		}
+	} else if (PLP_NONE != (proto = pool_protocol_fallback(proto))) {
+		goto tryagain;
 	} else {
+		free_work(work);
 		applog(LOG_DEBUG, "FAILED to retrieve work from pool %u %s",
 		       pool->pool_no, pool->rpc_url);
 		if (!pinging)
@@ -4634,7 +4886,7 @@ keepwaiting:
 	}
 
 	memcpy(work, work_heap, sizeof(struct work));
-	free_work(work_heap);
+	free(work_heap);
 
 out:
 	work->thr_id = thr_id;
@@ -4656,7 +4908,7 @@ bool submit_work_sync(struct thr_info *thr, const struct work *work_in)
 	wc->work = make_work();
 	wc->cmd = WC_SUBMIT_WORK;
 	wc->thr = thr;
-	memcpy(wc->work, work_in, sizeof(*work_in));
+	workcpy(wc->work, work_in);
 	wc->work->share_found_time = time(NULL);
 
 	applog(LOG_DEBUG, "Pushing submit work to work thread");
@@ -4944,13 +5196,10 @@ enum {
 };
 
 /* Stage another work item from the work returned in a longpoll */
-static void convert_to_work(json_t *val, int rolltime, struct pool *pool)
+static void convert_to_work(json_t *val, int rolltime, struct pool *pool, struct work *work)
 {
-	struct work *work;
 	bool rc;
 
-	work = make_work();
-
 	rc = work_decode(json_object_get(val, "result"), work);
 	if (unlikely(!rc)) {
 		applog(LOG_ERR, "Could not convert longpoll data to work");
@@ -4996,12 +5245,12 @@ static struct pool *select_longpoll_pool(struct pool *cp)
 {
 	int i;
 
-	if (cp->hdr_path)
+	if (cp->lp_url)
 		return cp;
 	for (i = 0; i < total_pools; i++) {
 		struct pool *pool = pools[i];
 
-		if (pool->hdr_path)
+		if (pool->lp_url)
 			return pool;
 	}
 	return NULL;
@@ -5022,6 +5271,13 @@ static void wait_lpcurrent(struct pool *pool)
 	}
 }
 
+static curl_socket_t save_curl_socket(void *vpool, __maybe_unused curlsocktype purpose, struct curl_sockaddr *addr) {
+	struct pool *pool = vpool;
+	curl_socket_t sock = socket(addr->family, addr->socktype, addr->protocol);
+	pool->lp_socket = sock;
+	return sock;
+}
+
 static void *longpoll_thread(void *userdata)
 {
 	struct pool *cp = (struct pool *)userdata;
@@ -5057,13 +5313,20 @@ retry_pool:
 	wait_lpcurrent(cp);
 
 	if (cp == pool)
-		applog(LOG_WARNING, "Long-polling activated for %s", pool->lp_url);
+		applog(LOG_WARNING, "Long-polling activated via %s (%s)", pool->lp_url, pool_protocol_name(pool->lp_proto));
 	else
-		applog(LOG_WARNING, "Long-polling activated for pool %s via %s", cp->rpc_url, pool->lp_url);
+		applog(LOG_WARNING, "Long-polling activated for pool %s via %s (%s)", cp->rpc_url, pool->lp_url, pool_protocol_name(pool->lp_proto));
 
 	while (42) {
 		json_t *val, *soval;
 
+		struct work *work = make_work();
+		work->pool = pool;
+		const char *rpc_req;
+		rpc_req = prepare_rpc_req(work, pool->lp_proto, pool->lp_id);
+		if (!rpc_req)
+			goto lpfail;
+
 		wait_lpcurrent(cp);
 
 		gettimeofday(&start, NULL);
@@ -5073,15 +5336,18 @@ retry_pool:
 		 * so always establish a fresh connection instead of relying on
 		 * a persistent one. */
 		curl_easy_setopt(curl, CURLOPT_FRESH_CONNECT, 1);
+		curl_easy_setopt(curl, CURLOPT_OPENSOCKETFUNCTION, save_curl_socket);
+		curl_easy_setopt(curl, CURLOPT_OPENSOCKETDATA, pool);
 		val = json_rpc_call(curl, pool->lp_url, pool->rpc_userpass, rpc_req,
 				    false, true, &rolltime, pool, false);
+		pool->lp_socket = CURL_SOCKET_BAD;
 		if (likely(val)) {
 			soval = json_object_get(json_object_get(val, "result"), "submitold");
 			if (soval)
 				pool->submit_old = json_is_true(soval);
 			else
 				pool->submit_old = false;
-			convert_to_work(val, rolltime, pool);
+			convert_to_work(val, rolltime, pool, work);
 			failures = 0;
 			json_decref(val);
 		} else {
@@ -5090,10 +5356,12 @@ retry_pool:
 			 * immediately and just restart it the rest of the
 			 * time. */
 			gettimeofday(&end, NULL);
+			free_work(work);
 			if (end.tv_sec - start.tv_sec > 30)
 				continue;
 			if (failures == 1)
 				applog(LOG_WARNING, "longpoll failed for %s, retrying every 30s", pool->lp_url);
+lpfail:
 			sleep(30);
 		}
 		if (pool != cp) {
@@ -5828,6 +6096,12 @@ bool add_cgpu(struct cgpu_info*cgpu)
 	return true;
 }
 
+static bool my_blkmaker_sha256_callback(void *digest, const void *buffer, size_t length)
+{
+	sha2(buffer, length, digest, false);
+	return true;
+}
+
 int main(int argc, char *argv[])
 {
 	struct block *block, *tmpblock;
@@ -5839,6 +6113,8 @@ int main(int argc, char *argv[])
 	unsigned int k;
 	int i, j;
 
+	blkmk_sha256_impl = my_blkmaker_sha256_callback;
+
 	/* This dangerous functions tramples random dynamically allocated
 	 * variables so do it before anything at all */
 	if (unlikely(curl_global_init(CURL_GLOBAL_ALL)))

+ 19 - 0
miner.h

@@ -9,6 +9,10 @@
 #include <pthread.h>
 #include <jansson.h>
 #include <curl/curl.h>
+
+#include <blkmaker.h>
+#include <blktemplate.h>
+
 #include "elist.h"
 #include "uthash.h"
 #include "logging.h"
@@ -791,6 +795,12 @@ enum pool_enable {
 	POOL_REJECTING,
 };
 
+enum pool_protocol {
+	PLP_NONE,
+	PLP_GETWORK,
+	PLP_GETBLOCKTEMPLATE,
+};
+
 struct pool {
 	int pool_no;
 	int prio;
@@ -813,8 +823,13 @@ struct pool {
 	unsigned char	work_restart_id;
 	uint32_t	block_id;
 
+	enum pool_protocol proto;
+
 	char *hdr_path;
 	char *lp_url;
+	char *lp_id;
+	enum pool_protocol lp_proto;
+	curl_socket_t lp_socket;
 
 	unsigned int getwork_requested;
 	unsigned int stale_shares;
@@ -885,6 +900,10 @@ struct work {
 	float		difficulty;
 
 	time_t share_found_time;
+
+	blktemplate_t	*tmpl;
+	int		*tmpl_refcount;
+	unsigned int	dataid;
 };
 
 extern void get_datestamp(char *, struct timeval *);

+ 16 - 2
util.c

@@ -56,6 +56,7 @@ struct timeval nettime;
 struct data_buffer {
 	void		*buf;
 	size_t		len;
+	curl_socket_t	*idlemarker;
 };
 
 struct upload_buffer {
@@ -96,6 +97,16 @@ static size_t all_data_cb(const void *ptr, size_t size, size_t nmemb,
 	void *newmem;
 	static const unsigned char zero = 0;
 
+	if (db->idlemarker) {
+		const unsigned char *cptr = ptr;
+		for (size_t i = 0; i < len; ++i)
+			if (!(isspace(cptr[i]) || cptr[i] == '{')) {
+				*db->idlemarker = CURL_SOCKET_BAD;
+				db->idlemarker = NULL;
+				break;
+			}
+	}
+
 	oldlen = db->len;
 	newlen = oldlen + len;
 
@@ -273,7 +284,7 @@ json_t *json_rpc_call(CURL *curl, const char *url,
 		      struct pool *pool, bool share)
 {
 	long timeout = longpoll ? (60 * 60) : 60;
-	struct data_buffer all_data = {NULL, 0};
+	struct data_buffer all_data = {NULL, 0, NULL};
 	struct header_info hi = {NULL, 0, NULL, false, false, false};
 	char len_hdr[64], user_agent_hdr[128];
 	char curl_err_str[CURL_ERROR_SIZE];
@@ -286,6 +297,9 @@ json_t *json_rpc_call(CURL *curl, const char *url,
 
 	memset(&err, 0, sizeof(err));
 
+	if (longpoll)
+		all_data.idlemarker = &pool->lp_socket;
+
 	/* it is assumed that 'curl' is freshly [re]initialized at this pt */
 
 	if (probe)
@@ -438,7 +452,7 @@ json_t *json_rpc_call(CURL *curl, const char *url,
 	res_val = json_object_get(val, "result");
 	err_val = json_object_get(val, "error");
 
-	if (!res_val || json_is_null(res_val) ||
+	if (!res_val ||
 	    (err_val && !json_is_null(err_val))) {
 		char *s;