Skip to content

Commit 075562f

Browse files
Merge branch 'master' into dev
2 parents 930b265 + 04ada58 commit 075562f

File tree

5 files changed

+22
-2
lines changed

5 files changed

+22
-2
lines changed

Makefile

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ TINYCBOR_SOURCES = \
2929
src/cborparser_dup_string.c \
3030
src/cborpretty.c \
3131
src/cbortojson.c \
32-
$(if $(open_memstream-pass),,src/open_memstream.c) \
3332
#
3433
CBORDUMP_SOURCES = tools/cbordump/cbordump.c
3534

@@ -72,6 +71,17 @@ endif
7271

7372
-include .config
7473

74+
# if open_memstream is unavailable on the system, try to implement our own
75+
# version using funopen or fopencookie
76+
ifeq ($(open_memstream-pass),)
77+
ifeq ($(funopen-pass)$(fopencookie-pass),)
78+
CFLAGS += -DWITHOUT_OPEN_MEMSTREAM
79+
$(warning warning: funopen and fopencookie unavailable, open_memstream can not be implemented and conversion to JSON will not work properly!)
80+
else
81+
TINYCBOR_SOURCES += src/open_memstream.c
82+
endif
83+
endif
84+
7585
# json2cbor depends on an external library (cJSON)
7686
ifneq ($(cjson-pass)$(system-cjson-pass),)
7787
JSON2CBOR_SOURCES = tools/json2cbor/json2cbor.c

Makefile.configure

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
ALLTESTS = open_memstream funopen gc_sections \
1+
ALLTESTS = open_memstream funopen fopencookie gc_sections \
22
system-cjson cjson
33
MAKEFILE := $(lastword $(MAKEFILE_LIST))
44
OUT :=
55

66
PROGRAM-open_memstream = extern int open_memstream(); int main() { return open_memstream(); }
77
PROGRAM-funopen = extern int funopen(); int main() { return funopen(); }
8+
PROGRAM-fopencookie = extern int fopencookie(); int main() { return fopencookie(); }
89
PROGRAM-gc_sections = int main() {}
910
CCFLAGS-gc_sections = -Wl,--gc-sections
1011

src/cbor.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ typedef enum CborError {
149149
/* errors in converting to JSON */
150150
CborErrorJsonObjectKeyIsAggregate,
151151
CborErrorJsonObjectKeyNotString,
152+
CborErrorJsonNotImplemented,
152153

153154
CborErrorOutOfMemory = ~0U / 2 + 1,
154155
CborErrorInternalError = ~0U

src/cborerrorstrings.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ const char *cbor_error_string(CborError error)
155155
case CborErrorJsonObjectKeyNotString:
156156
return _("conversion to JSON failed: key in object is not a string");
157157

158+
case CborErrorJsonNotImplemented:
159+
return _("conversion to JSON failed: open_memstream unavailable");
158160

159161
case CborErrorInternalError:
160162
return _("internal error");

src/cbortojson.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,11 @@ static CborError stringify_map_key(char **key, CborValue *it, int flags, CborTyp
402402
{
403403
(void)flags; /* unused */
404404
(void)type; /* unused */
405+
#ifdef WITHOUT_OPEN_MEMSTREAM
406+
(void)key; /* unused */
407+
(void)it; /* unused */
408+
return CborErrorJsonNotImplemented;
409+
#else
405410
size_t size;
406411

407412
FILE *memstream = open_memstream(key, &size);
@@ -412,6 +417,7 @@ static CborError stringify_map_key(char **key, CborValue *it, int flags, CborTyp
412417
if (unlikely(fclose(memstream) < 0 || *key == NULL))
413418
return CborErrorInternalError;
414419
return err;
420+
#endif
415421
}
416422

417423
static CborError array_to_json(FILE *out, CborValue *it, int flags, ConversionStatus *status)

0 commit comments

Comments
 (0)