From 1250439e715fa23be7fa15c3a4f894047c8961bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Schr=C3=B6der?= Date: Fri, 9 Sep 2016 23:48:00 +0200 Subject: [PATCH] port to avr --- configure | 100 +++++++++++++++++++++++++++++++++++++++++++++++ configure.ac | 5 +++ src/blob.c | 6 +-- src/blob_json.c | 6 +-- src/blob_ujson.c | 25 +++++++----- src/ujson.h | 5 +-- src/ujsondec.c | 47 +++++++++++----------- src/ujsonenc.c | 8 +++- src/utils.h | 7 ++-- 9 files changed, 161 insertions(+), 48 deletions(-) diff --git a/configure b/configure index 9e819bd..807114a 100755 --- a/configure +++ b/configure @@ -1775,6 +1775,97 @@ $as_echo "$ac_res" >&6; } eval $as_lineno_stack; ${as_lineno_stack:+:} unset as_lineno } # ac_fn_c_check_func + +# ac_fn_c_check_header_mongrel LINENO HEADER VAR INCLUDES +# ------------------------------------------------------- +# Tests whether HEADER exists, giving a warning if it cannot be compiled using +# the include files in INCLUDES and setting the cache variable VAR +# accordingly. +ac_fn_c_check_header_mongrel () +{ + as_lineno=${as_lineno-"$1"} as_lineno_stack=as_lineno_stack=$as_lineno_stack + if eval \${$3+:} false; then : + { $as_echo "$as_me:${as_lineno-$LINENO}: checking for $2" >&5 +$as_echo_n "checking for $2... " >&6; } +if eval \${$3+:} false; then : + $as_echo_n "(cached) " >&6 +fi +eval ac_res=\$$3 + { $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_res" >&5 +$as_echo "$ac_res" >&6; } +else + # Is the header compilable? +{ $as_echo "$as_me:${as_lineno-$LINENO}: checking $2 usability" >&5 +$as_echo_n "checking $2 usability... " >&6; } +cat confdefs.h - <<_ACEOF >conftest.$ac_ext +/* end confdefs.h. */ +$4 +#include <$2> +_ACEOF +if ac_fn_c_try_compile "$LINENO"; then : + ac_header_compiler=yes +else + ac_header_compiler=no +fi +rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext +{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_header_compiler" >&5 +$as_echo "$ac_header_compiler" >&6; } + +# Is the header present? +{ $as_echo "$as_me:${as_lineno-$LINENO}: checking $2 presence" >&5 +$as_echo_n "checking $2 presence... " >&6; } +cat confdefs.h - <<_ACEOF >conftest.$ac_ext +/* end confdefs.h. */ +#include <$2> +_ACEOF +if ac_fn_c_try_cpp "$LINENO"; then : + ac_header_preproc=yes +else + ac_header_preproc=no +fi +rm -f conftest.err conftest.i conftest.$ac_ext +{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_header_preproc" >&5 +$as_echo "$ac_header_preproc" >&6; } + +# So? What about this header? +case $ac_header_compiler:$ac_header_preproc:$ac_c_preproc_warn_flag in #(( + yes:no: ) + { $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: $2: accepted by the compiler, rejected by the preprocessor!" >&5 +$as_echo "$as_me: WARNING: $2: accepted by the compiler, rejected by the preprocessor!" >&2;} + { $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: $2: proceeding with the compiler's result" >&5 +$as_echo "$as_me: WARNING: $2: proceeding with the compiler's result" >&2;} + ;; + no:yes:* ) + { $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: $2: present but cannot be compiled" >&5 +$as_echo "$as_me: WARNING: $2: present but cannot be compiled" >&2;} + { $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: $2: check for missing prerequisite headers?" >&5 +$as_echo "$as_me: WARNING: $2: check for missing prerequisite headers?" >&2;} + { $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: $2: see the Autoconf documentation" >&5 +$as_echo "$as_me: WARNING: $2: see the Autoconf documentation" >&2;} + { $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: $2: section \"Present But Cannot Be Compiled\"" >&5 +$as_echo "$as_me: WARNING: $2: section \"Present But Cannot Be Compiled\"" >&2;} + { $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: $2: proceeding with the compiler's result" >&5 +$as_echo "$as_me: WARNING: $2: proceeding with the compiler's result" >&2;} +( $as_echo "## -------------------------------------- ## +## Report this to mkschreder.uk@gmail.com ## +## -------------------------------------- ##" + ) | sed "s/^/$as_me: WARNING: /" >&2 + ;; +esac + { $as_echo "$as_me:${as_lineno-$LINENO}: checking for $2" >&5 +$as_echo_n "checking for $2... " >&6; } +if eval \${$3+:} false; then : + $as_echo_n "(cached) " >&6 +else + eval "$3=\$ac_header_compiler" +fi +eval ac_res=\$$3 + { $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_res" >&5 +$as_echo "$ac_res" >&6; } +fi + eval $as_lineno_stack; ${as_lineno_stack:+:} unset as_lineno + +} # ac_fn_c_check_header_mongrel cat >config.log <<_ACEOF This file contains any messages produced by compilers while running configure, to aid debugging if configure makes a mistake. @@ -12569,6 +12660,15 @@ fi +ac_fn_c_check_header_mongrel "$LINENO" "unistd.h" "ac_cv_header_unistd_h" "$ac_includes_default" +if test "x$ac_cv_header_unistd_h" = xyes; then : + +$as_echo "#define HAVE_UNISTD_H 1" >>confdefs.h + +fi + + + ac_config_files="$ac_config_files Makefile src/Makefile test/Makefile" cat >confcache <<\_ACEOF diff --git a/configure.ac b/configure.ac index f1021ad..83785ba 100644 --- a/configure.ac +++ b/configure.ac @@ -13,5 +13,10 @@ LT_INIT AC_PROG_CC +AC_CHECK_HEADER([unistd.h], + [AC_DEFINE([HAVE_UNISTD_H], [1], + [Define to 1 if you have .])], + []) + AC_OUTPUT(Makefile src/Makefile test/Makefile) diff --git a/src/blob.c b/src/blob.c index 20ecfa7..c97a38f 100644 --- a/src/blob.c +++ b/src/blob.c @@ -39,7 +39,7 @@ static inline blob_offset_t blob_field_to_offset(struct blob *buf, struct blob_f return (blob_offset_t)((char *)attr - (char *) buf->buf); } -static void blob_field_init(struct blob_field *attr, int id, unsigned int len){ +static void blob_field_init(struct blob_field *attr, uint32_t id, uint32_t len){ assert(attr); memset(attr, 0, sizeof(struct blob_field)); //len += sizeof(struct blob_field); @@ -213,7 +213,7 @@ blob_offset_t blob_open_array(struct blob *buf){ } void blob_close_array(struct blob *buf, blob_offset_t offset){ - if((unsigned long)offset > blob_size(buf)) return; + if((int)offset > blob_size(buf)) return; struct blob_field *attr = blob_offset_to_attr(buf, offset); int len = (buf->buf + blob_field_raw_len(blob_head(buf))) - (void*)attr; blob_field_set_raw_len(attr, len); @@ -225,7 +225,7 @@ blob_offset_t blob_open_table(struct blob *buf){ } void blob_close_table(struct blob *buf, blob_offset_t offset){ - if((unsigned long)offset > blob_size(buf)) return; + if((int)offset > blob_size(buf)) return; struct blob_field *attr = blob_offset_to_attr(buf, offset); int len = (buf->buf + blob_field_raw_len(blob_head(buf))) - (void*)attr; blob_field_set_raw_len(attr, len); diff --git a/src/blob_json.c b/src/blob_json.c index 332d9ee..d0c4c24 100644 --- a/src/blob_json.c +++ b/src/blob_json.c @@ -239,13 +239,13 @@ static void blob_format_element(struct strbuf *s, struct blob_field *attr, bool sprintf(buf, "%d", be16_to_cpu(*(uint16_t *)data)); break; case BLOB_FIELD_INT32: - sprintf(buf, "%d", (int32_t) be32_to_cpu(*(uint32_t *)data)); + sprintf(buf, "%lu", (int32_t) be32_to_cpu(*(uint32_t *)data)); break; case BLOB_FIELD_INT64: - sprintf(buf, "%" PRId64, (int64_t) be64_to_cpu(*(uint64_t *)data)); + sprintf(buf, "%llu", (int64_t) be64_to_cpu(*(uint64_t *)data)); break; case BLOB_FIELD_FLOAT32: - sprintf(buf, "%f", (float) unpack754_32(be32_to_cpu(*(uint32_t*)data))); + sprintf(buf, "%f", (double) unpack754_32(be32_to_cpu(*(uint32_t*)data))); break; case BLOB_FIELD_FLOAT64: sprintf(buf, "%Le", unpack754_64(be64_to_cpu(*(uint64_t*)data))); diff --git a/src/blob_ujson.c b/src/blob_ujson.c index 4d78d0d..442a2f0 100644 --- a/src/blob_ujson.c +++ b/src/blob_ujson.c @@ -41,9 +41,9 @@ http://www.opensource.apple.com/source/tcl/tcl-14/tcl/license.terms #include "blobpack.h" #include #include -#include -#include -#include +//#include +//#include +//#include #include "ujson.h" #define DEBUG(...) {} @@ -57,13 +57,15 @@ void Object_arrayAddItem(void *prv, JSOBJ obj, JSOBJ value){ DEBUG("new array item \n"); } -JSOBJ Object_newString(void *prv, wchar_t *start, wchar_t *end){ +JSOBJ Object_newString(void *prv, char *start, char *end){ size_t len = end - start + 1; - char *str = alloca(len); + char *str = malloc(len); memset(str, 0, len); - wcstombs(str, start, end - start); + strncpy(str, start, end - start); DEBUG("new string %s\n", str); - return blob_put_string(prv, str); + void* ret = blob_put_string(prv, str); + free(str); + return ret; } JSOBJ Object_newTrue(void *prv){ @@ -183,6 +185,7 @@ bool blob_init_from_json(struct blob *self, const char *json){ return true; } +#ifdef HAVE_UNISTD_H bool blob_put_json_from_file(struct blob *self, const char *file){ // this is a rather simplistic approach where we just load the whole file into memory and then parse the buffer. // there can probably be better ways where we parse data in place.. @@ -193,13 +196,15 @@ bool blob_put_json_from_file(struct blob *self, const char *file){ file_size = lseek(fd, SEEK_END, 0); if(file_size > 1000000UL) { close(fd); return false; } lseek(fd, SEEK_SET, 0); - char *buffer = alloca(file_size + 1); // allocate on stack + char *buffer = malloc(file_size + 1); // allocate on stack memset(buffer, 0, file_size + 1); if(file_size != read(fd, buffer, file_size)){ close(fd); return false; } close(fd); - return blob_put_json(self, buffer); + int ret = blob_put_json(self, buffer); + free(buffer); + return ret; } - +#endif diff --git a/src/ujson.h b/src/ujson.h index d0fc267..6c208da 100644 --- a/src/ujson.h +++ b/src/ujson.h @@ -54,7 +54,6 @@ tree doesn't have cyclic references. #define __ULTRAJSON_H__ #include -#include // Don't output any extra whitespaces when encoding #define JSON_NO_EXTRA_WHITESPACE @@ -77,7 +76,7 @@ tree doesn't have cyclic references. /* Dictates and limits how much stack space for buffers UltraJSON will use before resorting to provided heap functions */ #ifndef JSON_MAX_STACK_BUFFER_SIZE -#define JSON_MAX_STACK_BUFFER_SIZE 131072 +#define JSON_MAX_STACK_BUFFER_SIZE 64 #endif #ifdef _WIN32 @@ -300,7 +299,7 @@ EXPORTFUNCTION char *JSON_EncodeObject(JSOBJ obj, JSONObjectEncoder *enc, char * typedef struct __JSONObjectDecoder { - JSOBJ (*newString)(void *prv, wchar_t *start, wchar_t *end); + JSOBJ (*newString)(void *prv, char *start, char *end); void (*objectAddKey)(void *prv, JSOBJ obj, JSOBJ name, JSOBJ value); void (*arrayAddItem)(void *prv, JSOBJ obj, JSOBJ value); JSOBJ (*newTrue)(void *prv); diff --git a/src/ujsondec.c b/src/ujsondec.c index d2b8255..e09e414 100644 --- a/src/ujsondec.c +++ b/src/ujsondec.c @@ -41,7 +41,6 @@ http://www.opensource.apple.com/source/tcl/tcl-14/tcl/license.terms #include #include #include -#include #include #include @@ -57,8 +56,8 @@ struct DecoderState { char *start; char *end; - wchar_t *escStart; - wchar_t *escEnd; + char *escStart; + char *escEnd; int escHeap; int lastType; JSUINT32 objDepth; @@ -427,8 +426,8 @@ JSOBJ decode_string ( struct DecoderState *ds) JSUTF16 sur[2] = { 0 }; int iSur = 0; int index; - wchar_t *escOffset; - wchar_t *escStart; + char *escOffset; + char *escStart; size_t escLen = (ds->escEnd - ds->escStart); JSUINT8 *inputOffset; JSUINT8 oct; @@ -442,11 +441,11 @@ JSOBJ decode_string ( struct DecoderState *ds) if (ds->escHeap) { - if (newSize > (SIZE_MAX / sizeof(wchar_t))) + if (newSize > (SIZE_MAX / sizeof(char))) { return SetError(ds, -1, "Could not reserve memory block"); } - escStart = (wchar_t *)ds->dec->realloc(ds->escStart, newSize * sizeof(wchar_t)); + escStart = (char *)ds->dec->realloc(ds->escStart, newSize * sizeof(char)); if (!escStart) { ds->dec->free(ds->escStart); @@ -456,18 +455,18 @@ JSOBJ decode_string ( struct DecoderState *ds) } else { - wchar_t *oldStart = ds->escStart; - if (newSize > (SIZE_MAX / sizeof(wchar_t))) + char *oldStart = ds->escStart; + if (newSize > (SIZE_MAX / sizeof(char))) { return SetError(ds, -1, "Could not reserve memory block"); } - ds->escStart = (wchar_t *) ds->dec->malloc(newSize * sizeof(wchar_t)); + ds->escStart = (char *) ds->dec->malloc(newSize * sizeof(char)); if (!ds->escStart) { return SetError(ds, -1, "Could not reserve memory block"); } ds->escHeap = 1; - memcpy(ds->escStart, oldStart, escLen * sizeof(wchar_t)); + memcpy(ds->escStart, oldStart, escLen * sizeof(char)); } ds->escEnd = ds->escStart + newSize; @@ -563,7 +562,7 @@ JSOBJ decode_string ( struct DecoderState *ds) iSur ++; break; } - (*escOffset++) = (wchar_t) sur[iSur]; + (*escOffset++) = (char) sur[iSur]; iSur = 0; } else @@ -574,10 +573,10 @@ JSOBJ decode_string ( struct DecoderState *ds) return SetError (ds, -1, "Unpaired high surrogate when decoding 'string'"); } #if WCHAR_MAX == 0xffff - (*escOffset++) = (wchar_t) sur[0]; - (*escOffset++) = (wchar_t) sur[1]; + (*escOffset++) = (char) sur[0]; + (*escOffset++) = (char) sur[1]; #else - (*escOffset++) = (wchar_t) 0x10000 + (((sur[0] - 0xd800) << 10) | (sur[1] - 0xdc00)); + (*escOffset++) = (char) 0x10000 + (((sur[0] - 0xd800) << 10) | (sur[1] - 0xdc00)); #endif iSur = 0; } @@ -591,7 +590,7 @@ JSOBJ decode_string ( struct DecoderState *ds) case 1: { - *(escOffset++) = (wchar_t) (*inputOffset++); + *(escOffset++) = (char) (*inputOffset++); break; } @@ -605,7 +604,7 @@ JSOBJ decode_string ( struct DecoderState *ds) } ucs |= (*inputOffset++) & 0x3f; if (ucs < 0x80) return SetError (ds, -1, "Overlong 2 byte UTF-8 sequence detected when decoding 'string'"); - *(escOffset++) = (wchar_t) ucs; + *(escOffset++) = (char) ucs; break; } @@ -628,7 +627,7 @@ JSOBJ decode_string ( struct DecoderState *ds) } if (ucs < 0x800) return SetError (ds, -1, "Overlong 3 byte UTF-8 sequence detected when encoding string"); - *(escOffset++) = (wchar_t) ucs; + *(escOffset++) = (char) ucs; break; } @@ -656,15 +655,15 @@ JSOBJ decode_string ( struct DecoderState *ds) if (ucs >= 0x10000) { ucs -= 0x10000; - *(escOffset++) = (wchar_t) (ucs >> 10) + 0xd800; - *(escOffset++) = (wchar_t) (ucs & 0x3ff) + 0xdc00; + *(escOffset++) = (char) (ucs >> 10) + 0xd800; + *(escOffset++) = (char) (ucs & 0x3ff) + 0xdc00; } else { - *(escOffset++) = (wchar_t) ucs; + *(escOffset++) = (char) ucs; } #else - *(escOffset++) = (wchar_t) ucs; + *(escOffset++) = (char) ucs; #endif break; } @@ -866,14 +865,14 @@ JSOBJ JSON_DecodeObject(JSONObjectDecoder *dec, const char *buffer, size_t cbBuf /* FIXME: Base the size of escBuffer of that of cbBuffer so that the unicode escaping doesn't run into the wall each time */ struct DecoderState ds; - wchar_t escBuffer[(JSON_MAX_STACK_BUFFER_SIZE / sizeof(wchar_t))]; + char escBuffer[(JSON_MAX_STACK_BUFFER_SIZE / sizeof(char))]; JSOBJ ret; ds.start = (char *) buffer; ds.end = ds.start + cbBuffer; ds.escStart = escBuffer; - ds.escEnd = ds.escStart + (JSON_MAX_STACK_BUFFER_SIZE / sizeof(wchar_t)); + ds.escEnd = ds.escStart + (JSON_MAX_STACK_BUFFER_SIZE / sizeof(char)); ds.escHeap = 0; ds.prv = dec->prv; ds.dec = dec; diff --git a/src/ujsonenc.c b/src/ujsonenc.c index e21b8f7..afb361c 100644 --- a/src/ujsonenc.c +++ b/src/ujsonenc.c @@ -42,9 +42,13 @@ http://www.opensource.apple.com/source/tcl/tcl-14/tcl/license.terms #include #include #include - +#include #include +#ifndef HUGE_VAL +#define HUGE_VAL INT_MAX +#endif + #ifndef TRUE #define TRUE 1 #endif @@ -344,7 +348,7 @@ int Buffer_EscapeStringValidated (JSOBJ obj, JSONObjectEncoder *enc, const char memcpy(&in8, io + 2, sizeof(JSUINT8)); #ifdef __LITTLE_ENDIAN__ in = (JSUTF32) in16; - in |= in8 << 16; + in |= (JSUTF32)in8 << 16; ucs = ((in & 0x0f) << 12) | ((in & 0x3f00) >> 2) | ((in & 0x3f0000) >> 16); #else in = in16 << 8; diff --git a/src/utils.h b/src/utils.h index bdb3369..8e9bdc5 100644 --- a/src/utils.h +++ b/src/utils.h @@ -19,11 +19,9 @@ #ifndef __LIBUBOX_UTILS_H #define __LIBUBOX_UTILS_H -#include -#include #include #include -#include +#include /* * calloc_a(size_t len, [void **addr, size_t len,...], NULL) @@ -85,6 +83,9 @@ int clock_gettime(int type, struct timespec *tv); #include #define bswap_32(x) bswap32(x) #define bswap_64(x) bswap64(x) +#elif defined(__AVR) +#define __LITTLE_ENDIAN 1234 +#define __BYTE_ORDER __LITTLE_ENDIAN #else #include #define bswap_32(x) swap32(x)