Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/crypto/external/bsd/netpgp/dist/src/libmj Move the minimalis...



details:   https://anonhg.NetBSD.org/src/rev/a90636917847
branches:  trunk
changeset: 756882:a90636917847
user:      agc <agc%NetBSD.org@localhost>
date:      Sat Aug 07 04:13:57 2010 +0000

description:
Move the minimalist JSON routines into their own library

diffstat:

 crypto/external/bsd/netpgp/dist/src/libmj/Makefile.am |   14 +
 crypto/external/bsd/netpgp/dist/src/libmj/defs.h      |   95 +++
 crypto/external/bsd/netpgp/dist/src/libmj/libmj.3     |  207 +++++++
 crypto/external/bsd/netpgp/dist/src/libmj/mj.c        |  521 ++++++++++++++++++
 crypto/external/bsd/netpgp/dist/src/libmj/mj.h        |   68 ++
 5 files changed, 905 insertions(+), 0 deletions(-)

diffs (truncated from 925 to 300 lines):

diff -r 07a1e176b270 -r a90636917847 crypto/external/bsd/netpgp/dist/src/libmj/Makefile.am
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/crypto/external/bsd/netpgp/dist/src/libmj/Makefile.am     Sat Aug 07 04:13:57 2010 +0000
@@ -0,0 +1,14 @@
+## $NetBSD: Makefile.am,v 1.1 2010/08/07 04:13:57 agc Exp $
+
+AM_CFLAGS              = $(WARNCFLAGS)
+
+lib_LTLIBRARIES                = libmj.la
+
+libmj_la_CPPFLAGS      = -I$(top_srcdir)/include
+
+libmj_la_SOURCES       = \
+       mj.c \
+
+man3_MANS              = mj.3
+
+dist_man_MANS          = mj.3
diff -r 07a1e176b270 -r a90636917847 crypto/external/bsd/netpgp/dist/src/libmj/defs.h
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/crypto/external/bsd/netpgp/dist/src/libmj/defs.h  Sat Aug 07 04:13:57 2010 +0000
@@ -0,0 +1,95 @@
+/* $NetBSD: defs.h,v 1.1 2010/08/07 04:13:57 agc Exp $ */
+
+/*-
+ * Copyright (c) 2009 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Alistair Crooks (agc%NetBSD.org@localhost)
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+#ifndef DEFS_H_
+#define DEFS_H_
+
+#include <sys/types.h>
+#include <sys/param.h>
+
+#ifdef HAVE_INTTYPES_H
+#include <inttypes.h>
+#endif
+
+#ifdef HAVE_STDINT_H
+#include <stdint.h>
+#endif
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#define NEWARRAY(type,ptr,size,where,action) do {                      \
+       if ((ptr = calloc(sizeof(type), (unsigned)(size))) == NULL) {   \
+               (void) fprintf(stderr, "%s: can't allocate %lu bytes\n", \
+                       where, (unsigned long)(size * sizeof(type)));   \
+               action;                                                 \
+       }                                                               \
+} while( /* CONSTCOND */ 0)
+
+#define RENEW(type, _ptr, _size, _newsize, where, action) do {         \
+       type *_newptr;                                                  \
+       _newptr = realloc(_ptr, (size_t)(_newsize) * sizeof(type));     \
+       if (_newptr == NULL) {                                          \
+               (void) fprintf(stderr, "%s: can't realloc %lu bytes\n", \
+                       where, (unsigned long)((_newsize) * sizeof(type))); \
+               action;                                                 \
+       } else {                                                        \
+               (void) memset(&_newptr[_size], 0x0,                     \
+                       (_newsize - _size) * sizeof(type));             \
+               _ptr = _newptr;                                         \
+               _size = _newsize;                                       \
+       }                                                               \
+} while( /* CONSTCOND */ 0)
+
+#define NEW(type, ptr, where, action)  NEWARRAY(type, ptr, 1, where, action)
+
+#define FREE(ptr)      free(ptr)
+
+#define ALLOC(type, v, size, c, init, incr, where, action) do {                \
+       uint32_t        _newsize = size;                                \
+       if (size == 0) {                                                \
+               _newsize = init;                                        \
+               NEWARRAY(type, v, _newsize, where ": new", action);     \
+       } else if (c == size) {                                         \
+               _newsize = size + incr;                                 \
+               RENEW(type, v, size, _newsize, where ": renew", action); \
+       }                                                               \
+       size = _newsize;                                                \
+} while( /* CONSTCOND */ 0)
+
+#define DEFINE_ARRAY(name, type)                                       \
+typedef struct name {                                                  \
+       uint32_t        c;                                              \
+       uint32_t        size;                                           \
+       type           *v;                                              \
+} name
+
+#endif /* !DEFS_H_ */
diff -r 07a1e176b270 -r a90636917847 crypto/external/bsd/netpgp/dist/src/libmj/libmj.3
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/crypto/external/bsd/netpgp/dist/src/libmj/libmj.3 Sat Aug 07 04:13:57 2010 +0000
@@ -0,0 +1,207 @@
+.\"
+.\" Copyright (c) 2010 Alistair Crooks <agc%NetBSD.org@localhost>
+.\" All rights reserved.
+.\"
+.\" Redistribution and use in source and binary forms, with or without
+.\" modification, are permitted provided that the following conditions
+.\" are met:
+.\" 1. Redistributions of source code must retain the above copyright
+.\"    notice, this list of conditions and the following disclaimer.
+.\" 2. Redistributions in binary form must reproduce the above copyright
+.\"    notice, this list of conditions and the following disclaimer in the
+.\"    documentation and/or other materials provided with the distribution.
+.\"
+.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+.\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+.\"
+.Dd August 5, 2010
+.Dt LIBMJ 3
+.Os
+.Sh NAME
+.Nm libmj
+.Nd minimalist JSON lightweight data interchange library
+.Sh LIBRARY
+.Lb libmj
+.Sh SYNOPSIS
+.In mj.h
+.Ft int
+.Fo mj_create
+.Fa "mj_t *atom" "const char *text" "..."
+.Fc
+.Ft int
+.Fo mj_parse
+.Fa "mj_t *atom" "const char *text" "int *tokfrom" "int *tokto" "int *toktype"
+.Fc
+.Ft int
+.Fo mj_append
+.Fa "mj_t *atom" "const char *text" "..."
+.Fc
+.Ft int
+.Fo mj_append_field
+.Fa "mj_t *atom" "const char *fieldname" "const char *text" "..."
+.Fc
+.Ft int
+.Fo mj_deepcopy
+.Fa "mj_t *dest" "mj_t *src"
+.Fc
+.Ft void
+.Fo mj_delete
+.Fa "mj_t *atom"
+.Fc
+.Pp
+Access to objects and array entries is made using the following functions:
+.Ft int
+.Fo mj_arraycount
+.Fa "mj_t *atom"
+.Fc
+.Ft int
+.Fo mj_object_find
+.Fa "mj_t *atom" "const char *name" "const unsigned startpoint"
+.Fa "const unsigned incr"
+.Fc
+.Ft mj_t *
+.Fo mj_get_atom
+.Fa "mj_t *atom" "..."
+.Fc
+.Pp
+JSON object output functions:
+.Ft int
+.Fo mj_snprint
+.Fa "char *buffer" "size_t size" "mj_t *atom"
+.Fc
+.Ft int
+.Fo mj_asprint
+.Fa "char **buffer" "mj_t *atom"
+.Fc
+.Ft int
+.Fo mj_string_size
+.Fa "mj_t *atom"
+.Fc
+.Sh DESCRIPTION
+.Nm
+is a small library interface to allow JSON text to be created and parsed.
+JSON is the Java Script Object Notation,
+a lightweight data-interchange format, standardised in the ECMA standard.
+The library name
+.Nm
+is derived from a further acronym of
+.Dq minimalist JSON .
+.\" Hey, Mary!
+.Pp
+The
+.Nm
+library can be used to create a string in memory which contains a textutal
+representation of a number of objects, arbitrarily structured.
+The library can also be used to reconstruct the structure.
+Data can thus be serialised easily and efficiently, and data structures
+rebuilt to produce the original structure of the data.
+.Pp
+JSON contains basic units called atoms, the two
+basic atoms being strings and numbers.
+Three other useful atomic values are provided,
+.Dq null ,
+.Dq false ,
+and
+.Dq true .
+Atoms can be grouped together as key/value pairs in an
+.Dq object ,
+and as individual, ordered atoms, in an
+.Dq array .
+.Pp
+To create a new object, the
+.Fn mj_create
+is used.
+It can be deleted using the
+.Fn mj_delete
+function.
+.Pp
+Atoms, objects and arrays can be appended
+to arrays and objects using the
+.Fn mj_append
+function.
+.Pp
+Objects can be printed out
+by using the
+.Fn mj_snprint
+function.
+The size of a string of JSON text can be calculated
+using the
+.Fn mj_string_size
+function.
+A utility function
+.Fn mj_asprint
+is provided which will allocate space dynamically,
+using
+.Xr calloc 3 ,
+and the JSON serialised text is copied into it.
+This memory can later be de-allocated using
+.Xr free 3 .
+.Pp
+The
+.Fa type
+argument given to the
+.Fn mj_create ,
+.Fn mj_append and
+.Fn mj_append_field
+functions is taken from a list of
+.Dq false
+.Dq true
+.Dq null
+.Dq number
+.Dq integer
+.Dq string
+.Dq array
+and
+.Dq object
+types.
+An integer differs from a number in that it cannot take on
+any floating point values.
+It is implemented internally using a signed 64-bit integer type.
+This restriction of values for an integer type may be removed at a later date.
+.Pp
+Within a JSON object, the key values can be iterated over using an integer
+index to access the individual
+JSON objects.
+The index can also be found using the
+.Fn mj_object_index
+function, and the object using
+the
+.Fn mj_object_find
+function.
+.Pp



Home | Main Index | Thread Index | Old Index