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 change mj library to take an addi...



details:   https://anonhg.NetBSD.org/src/rev/acc5cc2d9748
branches:  trunk
changeset: 766517:acc5cc2d9748
user:      agc <agc%NetBSD.org@localhost>
date:      Sat Jun 25 00:37:44 2011 +0000

description:
change mj library to take an additional argument for a string type,
denoting its length. this allows binary strings to be encoded using
libmj.

escape magic characters in json strings in a more efficient manner.
the previous method was not scalable.

update callers to suit

bump libmj major version number

add examples to the libmj(3) man page

diffstat:

 crypto/external/bsd/netpgp/dist/src/lib/keyring.c      |    4 +-
 crypto/external/bsd/netpgp/dist/src/lib/netpgp.c       |   18 +-
 crypto/external/bsd/netpgp/dist/src/lib/packet-print.c |   24 +-
 crypto/external/bsd/netpgp/dist/src/libmj/libmj.3      |   59 +++++++-
 crypto/external/bsd/netpgp/dist/src/libmj/mj.c         |  136 +++++++++++++---
 crypto/external/bsd/netpgp/dist/src/libmj/mj.h         |   44 +++--
 crypto/external/bsd/netpgp/libmj/shlib_version         |    2 +-
 7 files changed, 217 insertions(+), 70 deletions(-)

diffs (truncated from 638 to 300 lines):

diff -r 1febad418f61 -r acc5cc2d9748 crypto/external/bsd/netpgp/dist/src/lib/keyring.c
--- a/crypto/external/bsd/netpgp/dist/src/lib/keyring.c Sat Jun 25 00:07:10 2011 +0000
+++ b/crypto/external/bsd/netpgp/dist/src/lib/keyring.c Sat Jun 25 00:37:44 2011 +0000
@@ -57,7 +57,7 @@
 
 #if defined(__NetBSD__)
 __COPYRIGHT("@(#) Copyright (c) 2009 The NetBSD Foundation, Inc. All rights reserved.");
-__RCSID("$NetBSD: keyring.c,v 1.49 2010/11/15 08:50:32 agc Exp $");
+__RCSID("$NetBSD: keyring.c,v 1.50 2011/06/25 00:37:44 agc Exp $");
 #endif
 
 #ifdef HAVE_FCNTL_H
@@ -1039,7 +1039,7 @@
        if (pgp_get_debug_level(__FILE__)) {
                char    *s;
 
-               mj_asprint(&s, obj);
+               mj_asprint(&s, obj, MJ_JSON_ENCODE);
                (void) fprintf(stderr, "pgp_keyring_json: '%s'\n", s);
                free(s);
        }
diff -r 1febad418f61 -r acc5cc2d9748 crypto/external/bsd/netpgp/dist/src/lib/netpgp.c
--- a/crypto/external/bsd/netpgp/dist/src/lib/netpgp.c  Sat Jun 25 00:07:10 2011 +0000
+++ b/crypto/external/bsd/netpgp/dist/src/lib/netpgp.c  Sat Jun 25 00:37:44 2011 +0000
@@ -34,7 +34,7 @@
 
 #if defined(__NetBSD__)
 __COPYRIGHT("@(#) Copyright (c) 2009 The NetBSD Foundation, Inc. All rights reserved.");
-__RCSID("$NetBSD: netpgp.c,v 1.89 2011/01/03 05:34:53 agc Exp $");
+__RCSID("$NetBSD: netpgp.c,v 1.90 2011/06/25 00:37:44 agc Exp $");
 #endif
 
 #include <sys/types.h>
@@ -509,7 +509,8 @@
 static void
 pobj(FILE *fp, mj_t *obj, int depth)
 {
-       unsigned        i;
+       unsigned         i;
+       char            *s;
 
        if (obj == NULL) {
                (void) fprintf(stderr, "No object found\n");
@@ -528,7 +529,10 @@
                p(fp, obj->value.s, NULL);
                break;
        case MJ_STRING:
-               (void) fprintf(fp, "%.*s", (int)(obj->c), obj->value.s);
+               if ((i = mj_asprint(&s, obj, MJ_HUMAN)) > 2) {
+                       (void) fprintf(fp, "%.*s", (int)i - 2, &s[1]);
+                       free(s);
+               }
                break;
        case MJ_ARRAY:
                for (i = 0 ; i < obj->c ; i++) {
@@ -582,7 +586,7 @@
        int      i;
 
        if (pgp_get_debug_level(__FILE__)) {
-               mj_asprint(&s, obj);
+               mj_asprint(&s, obj, MJ_HUMAN);
                (void) fprintf(stderr, "formatobj: json is '%s'\n", s);
                free(s);
        }
@@ -919,7 +923,7 @@
        return pgp_keyring_list(netpgp->io, netpgp->pubring, psigs);
 }
 
-/* list the keys in a keyring, returning a JSON string */
+/* list the keys in a keyring, returning a JSON encoded string */
 int
 netpgp_list_keys_json(netpgp_t *netpgp, char **json, const int psigs)
 {
@@ -935,7 +939,7 @@
                (void) fprintf(stderr, "No keys in keyring\n");
                return 0;
        }
-       ret = mj_asprint(json, &obj);
+       ret = mj_asprint(json, &obj, MJ_JSON_ENCODE);
        mj_delete(&obj);
        return ret;
 }
@@ -1033,7 +1037,7 @@
                        k += 1;
                }
        } while (key != NULL);
-       ret = mj_asprint(json, &id_array);
+       ret = mj_asprint(json, &id_array, MJ_JSON_ENCODE);
        mj_delete(&id_array);
        return ret;
 }
diff -r 1febad418f61 -r acc5cc2d9748 crypto/external/bsd/netpgp/dist/src/lib/packet-print.c
--- a/crypto/external/bsd/netpgp/dist/src/lib/packet-print.c    Sat Jun 25 00:07:10 2011 +0000
+++ b/crypto/external/bsd/netpgp/dist/src/lib/packet-print.c    Sat Jun 25 00:37:44 2011 +0000
@@ -58,7 +58,7 @@
 
 #if defined(__NetBSD__)
 __COPYRIGHT("@(#) Copyright (c) 2009 The NetBSD Foundation, Inc. All rights reserved.");
-__RCSID("$NetBSD: packet-print.c,v 1.40 2010/11/15 08:50:32 agc Exp $");
+__RCSID("$NetBSD: packet-print.c,v 1.41 2011/06/25 00:37:44 agc Exp $");
 #endif
 
 #include <string.h>
@@ -493,12 +493,12 @@
        }
        (void) memset(keyjson, 0x0, sizeof(*keyjson));
        mj_create(keyjson, "object");
-       mj_append_field(keyjson, "header", "string", header);
+       mj_append_field(keyjson, "header", "string", header, -1);
        mj_append_field(keyjson, "key bits", "integer", (int64_t) numkeybits(pubkey));
-       mj_append_field(keyjson, "pka", "string", pgp_show_pka(pubkey->alg));
-       mj_append_field(keyjson, "key id", "string", strhexdump(keyid, key->sigid, PGP_KEY_ID_SIZE, ""));
+       mj_append_field(keyjson, "pka", "string", pgp_show_pka(pubkey->alg), -1);
+       mj_append_field(keyjson, "key id", "string", strhexdump(keyid, key->sigid, PGP_KEY_ID_SIZE, ""), -1);
        mj_append_field(keyjson, "fingerprint", "string",
-               strhexdump(fp, key->sigfingerprint.fingerprint, key->sigfingerprint.length, " "));
+               strhexdump(fp, key->sigfingerprint.fingerprint, key->sigfingerprint.length, " "), -1);
        mj_append_field(keyjson, "birthtime", "integer", pubkey->birthtime);
        mj_append_field(keyjson, "duration", "integer", pubkey->duration);
        for (i = 0; i < key->uidc; i++) {
@@ -508,8 +508,8 @@
                }
                (void) memset(&sub_obj, 0x0, sizeof(sub_obj));
                mj_create(&sub_obj, "array");
-               mj_append(&sub_obj, "string", key->uids[i]);
-               mj_append(&sub_obj, "string", (r >= 0) ? "[REVOKED]" : "");
+               mj_append(&sub_obj, "string", key->uids[i], -1);
+               mj_append(&sub_obj, "string", (r >= 0) ? "[REVOKED]" : "", -1);
                mj_append_field(keyjson, "uid", "array", &sub_obj);
                mj_delete(&sub_obj);
                for (j = 0 ; j < key->subsigc ; j++) {
@@ -530,21 +530,21 @@
                                        key->subsigs[j].sig.info.type == PGP_SIG_SUBKEY) {
                                mj_append(&sub_obj, "integer", (int64_t)numkeybits(&key->enckey));
                                mj_append(&sub_obj, "string",
-                                       (const char *)pgp_show_pka(key->enckey.alg));
+                                       (const char *)pgp_show_pka(key->enckey.alg), -1);
                                mj_append(&sub_obj, "string",
-                                       strhexdump(keyid, key->encid, PGP_KEY_ID_SIZE, ""));
+                                       strhexdump(keyid, key->encid, PGP_KEY_ID_SIZE, ""), -1);
                                mj_append(&sub_obj, "integer", (int64_t)key->enckey.birthtime);
                                mj_append_field(keyjson, "encryption", "array", &sub_obj);
                                mj_delete(&sub_obj);
                        } else {
                                mj_append(&sub_obj, "string",
-                                       strhexdump(keyid, key->subsigs[j].sig.info.signer_id, PGP_KEY_ID_SIZE, ""));
+                                       strhexdump(keyid, key->subsigs[j].sig.info.signer_id, PGP_KEY_ID_SIZE, ""), -1);
                                mj_append(&sub_obj, "integer",
                                        (int64_t)(key->subsigs[j].sig.info.birthtime));
                                from = 0;
                                trustkey = pgp_getkeybyid(io, keyring, key->subsigs[j].sig.info.signer_id, &from, NULL);
                                mj_append(&sub_obj, "string",
-                                       (trustkey) ? (char *)trustkey->uids[trustkey->uid0] : "[unknown]");
+                                       (trustkey) ? (char *)trustkey->uids[trustkey->uid0] : "[unknown]", -1);
                                mj_append_field(keyjson, "sig", "array", &sub_obj);
                                mj_delete(&sub_obj);
                        }
@@ -553,7 +553,7 @@
        if (pgp_get_debug_level(__FILE__)) {
                char    *buf;
 
-               mj_asprint(&buf, keyjson);
+               mj_asprint(&buf, keyjson, 1);
                (void) fprintf(stderr, "pgp_sprint_mj: '%s'\n", buf);
                free(buf);
        }
diff -r 1febad418f61 -r acc5cc2d9748 crypto/external/bsd/netpgp/dist/src/libmj/libmj.3
--- a/crypto/external/bsd/netpgp/dist/src/libmj/libmj.3 Sat Jun 25 00:07:10 2011 +0000
+++ b/crypto/external/bsd/netpgp/dist/src/libmj/libmj.3 Sat Jun 25 00:37:44 2011 +0000
@@ -1,4 +1,4 @@
-.\" $NetBSD: libmj.3,v 1.3 2010/09/08 22:17:27 wiz Exp $
+.\" $NetBSD: libmj.3,v 1.4 2011/06/25 00:37:44 agc Exp $
 .\"
 .\" Copyright (c) 2010 Alistair Crooks <agc%NetBSD.org@localhost>
 .\" All rights reserved.
@@ -23,7 +23,7 @@
 .\" (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
+.Dd June 22, 2011
 .Dt LIBMJ 3
 .Os
 .Sh NAME
@@ -90,6 +90,10 @@
 .Fo mj_pretty
 .Fa "mj_t *atom" "void *stream" "unsigned depth" "const char *trailer"
 .Fc
+.Ft const char *
+.Fo mj_string_rep
+.Fa "mj_t *atom"
+.Fc
 .Sh DESCRIPTION
 .Nm
 is a small library interface to allow JSON text to be created and parsed.
@@ -208,6 +212,57 @@
 and the first match after the starting point will be returned.
 For objects, an incremental value of 2 should be used,
 and an even start value should be specified.
+.Pp
+String values should be created and appended using two parameters in
+the stdarg fields, that of the string itself, and its length in bytes
+immediately after the string.
+A value of
+.Dv -1
+may be used if the string length is not known.
+.Sh EXAMPLES
+The follow code fragment will make a JSON object
+out of the string
+.Dq Hello <USERNAME>\en
+in the
+buffer called
+.Dq buf
+where
+.Dq USERNAME
+is the name of the user taken from the runtime environment.
+The encoded text will be in an allocated buffer called
+.Dq s
+.Bd -literal -offset indent
+mj_t atom;
+char buf[BUFSIZ];
+char *s;
+int cc;
+
+(void) memset(\*[Am]atom, 0x0, sizeof(atom));
+cc = snprintf(buf, sizeof(buf), "Hello %s\en", getenv("USER"));
+mj_create(\*[Am]atom, "string", buf, cc);
+cc = mj_asprint(\*[Am]s, \*[Am]atom, MJ_JSON_ENCODE);
+.Ed
+.Pp
+and the following example will take the (binary) text which has been encoded into
+JSON and is in the buffer
+.Dq buf ,
+such as in the previous example, and re-create the original text:
+.Bd -literal -offset indent
+int from, to, tok, cc;
+char *s;
+mj_t atom;
+
+(void) memset(\*[Am]atom, 0x0, sizeof(atom));
+from = to = tok = 0;
+mj_parse(\*[Am]atom, buf, \*[Am]from, \*[Am]to, \*[Am]tok);
+cc = mj_asprint(\*[Am]s, \*[Am]atom, MJ_HUMAN);
+printf("%.*s", cc, s);
+.Ed
+.Pp
+The
+.Dv s
+pointer points to allocated storage with the original NUL-terminated string
+in it.
 .Sh SEE ALSO
 .Xr calloc 3 ,
 .Xr free 3
diff -r 1febad418f61 -r acc5cc2d9748 crypto/external/bsd/netpgp/dist/src/libmj/mj.c
--- a/crypto/external/bsd/netpgp/dist/src/libmj/mj.c    Sat Jun 25 00:07:10 2011 +0000
+++ b/crypto/external/bsd/netpgp/dist/src/libmj/mj.c    Sat Jun 25 00:37:44 2011 +0000
@@ -37,7 +37,7 @@
 
 /* save 'n' chars of 's' in malloc'd memory */
 static char *
-strnsave(const char *s, int n, unsigned esc)
+strnsave(const char *s, int n, unsigned encoded)
 {
        char    *newc;
        char    *cp;
@@ -46,16 +46,25 @@
        if (n < 0) {
                n = (int)strlen(s);
        }
-       NEWARRAY(char, cp, (n * 2) + 1, "strnsave", return NULL);
-       if (esc) {
+       NEWARRAY(char, cp, n + n + 1, "strnsave", return NULL);
+       if (encoded) {
                newc = cp;
                for (i = 0 ; i < n ; i++) {
-                       if (*s == '\\') {
-                               *newc++ = *s++;
+                       if ((uint8_t)*s == 0xac) {
+                               *newc++ = (char)0xac;
+                               *newc++ = '1';
+                               s += 1;
                        } else if (*s == '"') {
-                               *newc++ = '\\';
+                               *newc++ = (char)0xac;
+                               *newc++ = '2';
+                               s += 1;
+                       } else if (*s == 0x0) {
+                               *newc++ = (char)0xac;
+                               *newc++ = '0';
+                               s += 1;
+                       } else {
+                               *newc++ = *s++;
                        }
-                       *newc++ = *s++;
                }
                *newc = 0x0;
        } else {
@@ -87,7 +96,7 @@
 
        atom->type = MJ_NUMBER;



Home | Main Index | Thread Index | Old Index