cJSON.c
author Ryan C. Gordon <icculus@icculus.org>
Thu, 09 Apr 2020 02:15:46 -0400
changeset 60 a0629a9e3ee6
parent 58 1390348facc7
permissions -rw-r--r--
otp: Some base32-decoding fixes to match what Google Authenticator expects.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
58
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     1
/*
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     2
  Copyright (c) 2009-2017 Dave Gamble and cJSON contributors
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     3
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     4
  Permission is hereby granted, free of charge, to any person obtaining a copy
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     5
  of this software and associated documentation files (the "Software"), to deal
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     6
  in the Software without restriction, including without limitation the rights
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     7
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     8
  copies of the Software, and to permit persons to whom the Software is
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     9
  furnished to do so, subject to the following conditions:
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    10
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    11
  The above copyright notice and this permission notice shall be included in
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    12
  all copies or substantial portions of the Software.
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    13
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    14
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    15
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    16
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    17
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    18
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    19
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    20
  THE SOFTWARE.
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    21
*/
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    22
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    23
/* cJSON */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    24
/* JSON parser in C. */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    25
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    26
#ifdef __GNUC__
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    27
#pragma GCC visibility push(default)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    28
#endif
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    29
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    30
#include <string.h>
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    31
#include <stdio.h>
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    32
#include <math.h>
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    33
#include <stdlib.h>
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    34
#include <float.h>
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    35
#include <limits.h>
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    36
#include <ctype.h>
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    37
#include <locale.h>
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    38
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    39
#ifdef __GNUC__
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    40
#pragma GCC visibility pop
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    41
#endif
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    42
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    43
#include "cJSON.h"
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    44
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    45
/* define our own boolean type */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    46
#define true ((cJSON_bool)1)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    47
#define false ((cJSON_bool)0)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    48
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    49
typedef struct {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    50
    const unsigned char *json;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    51
    size_t position;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    52
} error;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    53
static error global_error = { NULL, 0 };
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    54
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    55
CJSON_PUBLIC(const char *) cJSON_GetErrorPtr(void)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    56
{
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    57
    return (const char*) (global_error.json + global_error.position);
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    58
}
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    59
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    60
/* This is a safeguard to prevent copy-pasters from using incompatible C and header files */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    61
#if (CJSON_VERSION_MAJOR != 1) || (CJSON_VERSION_MINOR != 5) || (CJSON_VERSION_PATCH != 5)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    62
    #error cJSON.h and cJSON.c have different versions. Make sure that both have the same.
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    63
#endif
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    64
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    65
CJSON_PUBLIC(const char*) cJSON_Version(void)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    66
{
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    67
    static char version[15];
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    68
    sprintf(version, "%i.%i.%i", CJSON_VERSION_MAJOR, CJSON_VERSION_MINOR, CJSON_VERSION_PATCH);
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    69
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    70
    return version;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    71
}
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    72
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    73
/* Case insensitive string comparison, doesn't consider two NULL pointers equal though */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    74
static int case_insensitive_strcmp(const unsigned char *string1, const unsigned char *string2)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    75
{
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    76
    if ((string1 == NULL) || (string2 == NULL))
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    77
    {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    78
        return 1;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    79
    }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    80
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    81
    if (string1 == string2)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    82
    {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    83
        return 0;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    84
    }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    85
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    86
    for(; tolower(*string1) == tolower(*string2); (void)string1++, string2++)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    87
    {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    88
        if (*string1 == '\0')
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    89
        {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    90
            return 0;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    91
        }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    92
    }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    93
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    94
    return tolower(*string1) - tolower(*string2);
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    95
}
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    96
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    97
typedef struct internal_hooks
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    98
{
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    99
    void *(*allocate)(size_t size);
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   100
    void (*deallocate)(void *pointer);
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   101
    void *(*reallocate)(void *pointer, size_t size);
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   102
} internal_hooks;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   103
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   104
static internal_hooks global_hooks = { malloc, free, realloc };
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   105
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   106
static unsigned char* cJSON_strdup(const unsigned char* string, const internal_hooks * const hooks)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   107
{
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   108
    size_t length = 0;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   109
    unsigned char *copy = NULL;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   110
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   111
    if (string == NULL)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   112
    {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   113
        return NULL;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   114
    }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   115
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   116
    length = strlen((const char*)string) + sizeof("");
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   117
    if (!(copy = (unsigned char*)hooks->allocate(length)))
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   118
    {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   119
        return NULL;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   120
    }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   121
    memcpy(copy, string, length);
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   122
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   123
    return copy;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   124
}
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   125
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   126
CJSON_PUBLIC(void) cJSON_InitHooks(cJSON_Hooks* hooks)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   127
{
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   128
    if (hooks == NULL)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   129
    {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   130
        /* Reset hooks */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   131
        global_hooks.allocate = malloc;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   132
        global_hooks.deallocate = free;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   133
        global_hooks.reallocate = realloc;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   134
        return;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   135
    }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   136
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   137
    global_hooks.allocate = malloc;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   138
    if (hooks->malloc_fn != NULL)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   139
    {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   140
        global_hooks.allocate = hooks->malloc_fn;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   141
    }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   142
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   143
    global_hooks.deallocate = free;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   144
    if (hooks->free_fn != NULL)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   145
    {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   146
        global_hooks.deallocate = hooks->free_fn;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   147
    }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   148
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   149
    /* use realloc only if both free and malloc are used */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   150
    global_hooks.reallocate = NULL;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   151
    if ((global_hooks.allocate == malloc) && (global_hooks.deallocate == free))
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   152
    {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   153
        global_hooks.reallocate = realloc;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   154
    }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   155
}
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   156
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   157
/* Internal constructor. */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   158
static cJSON *cJSON_New_Item(const internal_hooks * const hooks)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   159
{
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   160
    cJSON* node = (cJSON*)hooks->allocate(sizeof(cJSON));
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   161
    if (node)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   162
    {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   163
        memset(node, '\0', sizeof(cJSON));
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   164
    }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   165
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   166
    return node;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   167
}
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   168
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   169
/* Delete a cJSON structure. */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   170
CJSON_PUBLIC(void) cJSON_Delete(cJSON *item)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   171
{
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   172
    cJSON *next = NULL;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   173
    while (item != NULL)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   174
    {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   175
        next = item->next;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   176
        if (!(item->type & cJSON_IsReference) && (item->child != NULL))
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   177
        {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   178
            cJSON_Delete(item->child);
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   179
        }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   180
        if (!(item->type & cJSON_IsReference) && (item->valuestring != NULL))
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   181
        {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   182
            global_hooks.deallocate(item->valuestring);
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   183
        }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   184
        if (!(item->type & cJSON_StringIsConst) && (item->string != NULL))
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   185
        {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   186
            global_hooks.deallocate(item->string);
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   187
        }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   188
        global_hooks.deallocate(item);
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   189
        item = next;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   190
    }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   191
}
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   192
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   193
/* get the decimal point character of the current locale */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   194
static unsigned char get_decimal_point(void)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   195
{
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   196
    struct lconv *lconv = localeconv();
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   197
    return (unsigned char) lconv->decimal_point[0];
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   198
}
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   199
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   200
typedef struct
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   201
{
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   202
    const unsigned char *content;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   203
    size_t length;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   204
    size_t offset;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   205
    size_t depth; /* How deeply nested (in arrays/objects) is the input at the current offset. */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   206
    internal_hooks hooks;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   207
} parse_buffer;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   208
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   209
/* check if the given size is left to read in a given parse buffer (starting with 1) */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   210
#define can_read(buffer, size) ((buffer != NULL) && (((buffer)->offset + size) <= (buffer)->length))
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   211
#define cannot_read(buffer, size) (!can_read(buffer, size))
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   212
/* check if the buffer can be accessed at the given index (starting with 0) */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   213
#define can_access_at_index(buffer, index) ((buffer != NULL) && (((buffer)->offset + index) < (buffer)->length))
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   214
#define cannot_access_at_index(buffer, index) (!can_access_at_index(buffer, index))
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   215
/* get a pointer to the buffer at the position */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   216
#define buffer_at_offset(buffer) ((buffer)->content + (buffer)->offset)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   217
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   218
/* Parse the input text to generate a number, and populate the result into item. */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   219
static cJSON_bool parse_number(cJSON * const item, parse_buffer * const input_buffer)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   220
{
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   221
    double number = 0;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   222
    unsigned char *after_end = NULL;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   223
    unsigned char number_c_string[64];
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   224
    unsigned char decimal_point = get_decimal_point();
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   225
    size_t i = 0;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   226
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   227
    if ((input_buffer == NULL) || (input_buffer->content == NULL))
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   228
    {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   229
        return false;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   230
    }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   231
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   232
    /* copy the number into a temporary buffer and replace '.' with the decimal point
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   233
     * of the current locale (for strtod)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   234
     * This also takes care of '\0' not necessarily being available for marking the end of the input */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   235
    for (i = 0; (i < (sizeof(number_c_string) - 1)) && can_access_at_index(input_buffer, i); i++)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   236
    {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   237
        switch (buffer_at_offset(input_buffer)[i])
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   238
        {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   239
            case '0':
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   240
            case '1':
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   241
            case '2':
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   242
            case '3':
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   243
            case '4':
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   244
            case '5':
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   245
            case '6':
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   246
            case '7':
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   247
            case '8':
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   248
            case '9':
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   249
            case '+':
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   250
            case '-':
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   251
            case 'e':
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   252
            case 'E':
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   253
                number_c_string[i] = buffer_at_offset(input_buffer)[i];
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   254
                break;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   255
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   256
            case '.':
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   257
                number_c_string[i] = decimal_point;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   258
                break;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   259
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   260
            default:
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   261
                goto loop_end;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   262
        }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   263
    }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   264
loop_end:
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   265
    number_c_string[i] = '\0';
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   266
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   267
    number = strtod((const char*)number_c_string, (char**)&after_end);
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   268
    if (number_c_string == after_end)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   269
    {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   270
        return false; /* parse_error */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   271
    }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   272
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   273
    item->valuedouble = number;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   274
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   275
    /* use saturation in case of overflow */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   276
    if (number >= INT_MAX)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   277
    {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   278
        item->valueint = INT_MAX;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   279
    }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   280
    else if (number <= INT_MIN)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   281
    {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   282
        item->valueint = INT_MIN;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   283
    }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   284
    else
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   285
    {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   286
        item->valueint = (int)number;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   287
    }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   288
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   289
    item->type = cJSON_Number;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   290
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   291
    input_buffer->offset += (size_t)(after_end - number_c_string);
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   292
    return true;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   293
}
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   294
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   295
/* don't ask me, but the original cJSON_SetNumberValue returns an integer or double */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   296
CJSON_PUBLIC(double) cJSON_SetNumberHelper(cJSON *object, double number)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   297
{
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   298
    if (number >= INT_MAX)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   299
    {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   300
        object->valueint = INT_MAX;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   301
    }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   302
    else if (number <= INT_MIN)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   303
    {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   304
        object->valueint = INT_MIN;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   305
    }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   306
    else
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   307
    {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   308
        object->valueint = (int)number;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   309
    }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   310
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   311
    return object->valuedouble = number;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   312
}
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   313
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   314
typedef struct
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   315
{
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   316
    unsigned char *buffer;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   317
    size_t length;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   318
    size_t offset;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   319
    size_t depth; /* current nesting depth (for formatted printing) */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   320
    cJSON_bool noalloc;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   321
    cJSON_bool format; /* is this print a formatted print */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   322
    internal_hooks hooks;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   323
} printbuffer;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   324
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   325
/* realloc printbuffer if necessary to have at least "needed" bytes more */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   326
static unsigned char* ensure(printbuffer * const p, size_t needed)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   327
{
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   328
    unsigned char *newbuffer = NULL;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   329
    size_t newsize = 0;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   330
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   331
    if ((p == NULL) || (p->buffer == NULL))
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   332
    {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   333
        return NULL;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   334
    }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   335
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   336
    if ((p->length > 0) && (p->offset >= p->length))
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   337
    {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   338
        /* make sure that offset is valid */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   339
        return NULL;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   340
    }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   341
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   342
    if (needed > INT_MAX)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   343
    {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   344
        /* sizes bigger than INT_MAX are currently not supported */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   345
        return NULL;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   346
    }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   347
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   348
    needed += p->offset + 1;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   349
    if (needed <= p->length)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   350
    {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   351
        return p->buffer + p->offset;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   352
    }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   353
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   354
    if (p->noalloc) {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   355
        return NULL;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   356
    }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   357
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   358
    /* calculate new buffer size */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   359
    if (needed > (INT_MAX / 2))
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   360
    {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   361
        /* overflow of int, use INT_MAX if possible */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   362
        if (needed <= INT_MAX)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   363
        {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   364
            newsize = INT_MAX;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   365
        }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   366
        else
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   367
        {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   368
            return NULL;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   369
        }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   370
    }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   371
    else
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   372
    {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   373
        newsize = needed * 2;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   374
    }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   375
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   376
    if (p->hooks.reallocate != NULL)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   377
    {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   378
        /* reallocate with realloc if available */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   379
        newbuffer = (unsigned char*)p->hooks.reallocate(p->buffer, newsize);
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   380
    }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   381
    else
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   382
    {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   383
        /* otherwise reallocate manually */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   384
        newbuffer = (unsigned char*)p->hooks.allocate(newsize);
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   385
        if (!newbuffer)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   386
        {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   387
            p->hooks.deallocate(p->buffer);
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   388
            p->length = 0;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   389
            p->buffer = NULL;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   390
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   391
            return NULL;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   392
        }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   393
        if (newbuffer)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   394
        {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   395
            memcpy(newbuffer, p->buffer, p->offset + 1);
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   396
        }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   397
        p->hooks.deallocate(p->buffer);
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   398
    }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   399
    p->length = newsize;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   400
    p->buffer = newbuffer;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   401
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   402
    return newbuffer + p->offset;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   403
}
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   404
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   405
/* calculate the new length of the string in a printbuffer and update the offset */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   406
static void update_offset(printbuffer * const buffer)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   407
{
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   408
    const unsigned char *buffer_pointer = NULL;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   409
    if ((buffer == NULL) || (buffer->buffer == NULL))
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   410
    {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   411
        return;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   412
    }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   413
    buffer_pointer = buffer->buffer + buffer->offset;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   414
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   415
    buffer->offset += strlen((const char*)buffer_pointer);
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   416
}
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   417
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   418
/* Render the number nicely from the given item into a string. */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   419
static cJSON_bool print_number(const cJSON * const item, printbuffer * const output_buffer)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   420
{
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   421
    unsigned char *output_pointer = NULL;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   422
    double d = item->valuedouble;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   423
    int length = 0;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   424
    size_t i = 0;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   425
    unsigned char number_buffer[26]; /* temporary buffer to print the number into */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   426
    unsigned char decimal_point = get_decimal_point();
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   427
    double test;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   428
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   429
    if (output_buffer == NULL)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   430
    {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   431
        return false;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   432
    }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   433
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   434
    /* This checks for NaN and Infinity */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   435
    if ((d * 0) != 0)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   436
    {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   437
        length = sprintf((char*)number_buffer, "null");
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   438
    }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   439
    else
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   440
    {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   441
        /* Try 15 decimal places of precision to avoid nonsignificant nonzero digits */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   442
        length = sprintf((char*)number_buffer, "%1.15g", d);
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   443
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   444
        /* Check whether the original double can be recovered */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   445
        if ((sscanf((char*)number_buffer, "%lg", &test) != 1) || ((double)test != d))
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   446
        {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   447
            /* If not, print with 17 decimal places of precision */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   448
            length = sprintf((char*)number_buffer, "%1.17g", d);
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   449
        }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   450
    }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   451
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   452
    /* sprintf failed or buffer overrun occured */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   453
    if ((length < 0) || (length > (int)(sizeof(number_buffer) - 1)))
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   454
    {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   455
        return false;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   456
    }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   457
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   458
    /* reserve appropriate space in the output */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   459
    output_pointer = ensure(output_buffer, (size_t)length);
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   460
    if (output_pointer == NULL)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   461
    {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   462
        return false;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   463
    }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   464
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   465
    /* copy the printed number to the output and replace locale
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   466
     * dependent decimal point with '.' */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   467
    for (i = 0; i < ((size_t)length); i++)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   468
    {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   469
        if (number_buffer[i] == decimal_point)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   470
        {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   471
            output_pointer[i] = '.';
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   472
            continue;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   473
        }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   474
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   475
        output_pointer[i] = number_buffer[i];
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   476
    }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   477
    output_pointer[i] = '\0';
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   478
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   479
    output_buffer->offset += (size_t)length;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   480
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   481
    return true;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   482
}
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   483
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   484
/* parse 4 digit hexadecimal number */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   485
static unsigned parse_hex4(const unsigned char * const input)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   486
{
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   487
    unsigned int h = 0;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   488
    size_t i = 0;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   489
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   490
    for (i = 0; i < 4; i++)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   491
    {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   492
        /* parse digit */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   493
        if ((input[i] >= '0') && (input[i] <= '9'))
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   494
        {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   495
            h += (unsigned int) input[i] - '0';
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   496
        }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   497
        else if ((input[i] >= 'A') && (input[i] <= 'F'))
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   498
        {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   499
            h += (unsigned int) 10 + input[i] - 'A';
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   500
        }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   501
        else if ((input[i] >= 'a') && (input[i] <= 'f'))
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   502
        {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   503
            h += (unsigned int) 10 + input[i] - 'a';
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   504
        }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   505
        else /* invalid */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   506
        {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   507
            return 0;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   508
        }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   509
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   510
        if (i < 3)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   511
        {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   512
            /* shift left to make place for the next nibble */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   513
            h = h << 4;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   514
        }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   515
    }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   516
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   517
    return h;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   518
}
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   519
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   520
/* converts a UTF-16 literal to UTF-8
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   521
 * A literal can be one or two sequences of the form \uXXXX */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   522
static unsigned char utf16_literal_to_utf8(const unsigned char * const input_pointer, const unsigned char * const input_end, unsigned char **output_pointer)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   523
{
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   524
    long unsigned int codepoint = 0;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   525
    unsigned int first_code = 0;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   526
    const unsigned char *first_sequence = input_pointer;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   527
    unsigned char utf8_length = 0;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   528
    unsigned char utf8_position = 0;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   529
    unsigned char sequence_length = 0;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   530
    unsigned char first_byte_mark = 0;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   531
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   532
    if ((input_end - first_sequence) < 6)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   533
    {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   534
        /* input ends unexpectedly */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   535
        goto fail;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   536
    }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   537
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   538
    /* get the first utf16 sequence */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   539
    first_code = parse_hex4(first_sequence + 2);
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   540
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   541
    /* check that the code is valid */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   542
    if (((first_code >= 0xDC00) && (first_code <= 0xDFFF)))
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   543
    {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   544
        goto fail;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   545
    }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   546
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   547
    /* UTF16 surrogate pair */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   548
    if ((first_code >= 0xD800) && (first_code <= 0xDBFF))
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   549
    {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   550
        const unsigned char *second_sequence = first_sequence + 6;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   551
        unsigned int second_code = 0;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   552
        sequence_length = 12; /* \uXXXX\uXXXX */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   553
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   554
        if ((input_end - second_sequence) < 6)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   555
        {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   556
            /* input ends unexpectedly */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   557
            goto fail;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   558
        }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   559
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   560
        if ((second_sequence[0] != '\\') || (second_sequence[1] != 'u'))
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   561
        {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   562
            /* missing second half of the surrogate pair */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   563
            goto fail;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   564
        }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   565
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   566
        /* get the second utf16 sequence */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   567
        second_code = parse_hex4(second_sequence + 2);
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   568
        /* check that the code is valid */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   569
        if ((second_code < 0xDC00) || (second_code > 0xDFFF))
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   570
        {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   571
            /* invalid second half of the surrogate pair */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   572
            goto fail;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   573
        }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   574
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   575
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   576
        /* calculate the unicode codepoint from the surrogate pair */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   577
        codepoint = 0x10000 + (((first_code & 0x3FF) << 10) | (second_code & 0x3FF));
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   578
    }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   579
    else
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   580
    {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   581
        sequence_length = 6; /* \uXXXX */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   582
        codepoint = first_code;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   583
    }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   584
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   585
    /* encode as UTF-8
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   586
     * takes at maximum 4 bytes to encode:
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   587
     * 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   588
    if (codepoint < 0x80)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   589
    {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   590
        /* normal ascii, encoding 0xxxxxxx */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   591
        utf8_length = 1;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   592
    }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   593
    else if (codepoint < 0x800)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   594
    {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   595
        /* two bytes, encoding 110xxxxx 10xxxxxx */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   596
        utf8_length = 2;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   597
        first_byte_mark = 0xC0; /* 11000000 */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   598
    }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   599
    else if (codepoint < 0x10000)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   600
    {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   601
        /* three bytes, encoding 1110xxxx 10xxxxxx 10xxxxxx */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   602
        utf8_length = 3;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   603
        first_byte_mark = 0xE0; /* 11100000 */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   604
    }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   605
    else if (codepoint <= 0x10FFFF)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   606
    {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   607
        /* four bytes, encoding 1110xxxx 10xxxxxx 10xxxxxx 10xxxxxx */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   608
        utf8_length = 4;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   609
        first_byte_mark = 0xF0; /* 11110000 */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   610
    }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   611
    else
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   612
    {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   613
        /* invalid unicode codepoint */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   614
        goto fail;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   615
    }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   616
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   617
    /* encode as utf8 */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   618
    for (utf8_position = (unsigned char)(utf8_length - 1); utf8_position > 0; utf8_position--)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   619
    {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   620
        /* 10xxxxxx */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   621
        (*output_pointer)[utf8_position] = (unsigned char)((codepoint | 0x80) & 0xBF);
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   622
        codepoint >>= 6;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   623
    }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   624
    /* encode first byte */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   625
    if (utf8_length > 1)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   626
    {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   627
        (*output_pointer)[0] = (unsigned char)((codepoint | first_byte_mark) & 0xFF);
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   628
    }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   629
    else
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   630
    {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   631
        (*output_pointer)[0] = (unsigned char)(codepoint & 0x7F);
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   632
    }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   633
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   634
    *output_pointer += utf8_length;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   635
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   636
    return sequence_length;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   637
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   638
fail:
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   639
    return 0;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   640
}
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   641
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   642
/* Parse the input text into an unescaped cinput, and populate item. */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   643
static cJSON_bool parse_string(cJSON * const item, parse_buffer * const input_buffer)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   644
{
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   645
    const unsigned char *input_pointer = buffer_at_offset(input_buffer) + 1;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   646
    const unsigned char *input_end = buffer_at_offset(input_buffer) + 1;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   647
    unsigned char *output_pointer = NULL;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   648
    unsigned char *output = NULL;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   649
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   650
    /* not a string */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   651
    if (buffer_at_offset(input_buffer)[0] != '\"')
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   652
    {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   653
        goto fail;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   654
    }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   655
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   656
    {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   657
        /* calculate approximate size of the output (overestimate) */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   658
        size_t allocation_length = 0;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   659
        size_t skipped_bytes = 0;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   660
        while (((size_t)(input_end - input_buffer->content) < input_buffer->length) && (*input_end != '\"'))
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   661
        {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   662
            /* is escape sequence */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   663
            if (input_end[0] == '\\')
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   664
            {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   665
                if ((size_t)(input_end + 1 - input_buffer->content) >= input_buffer->length)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   666
                {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   667
                    /* prevent buffer overflow when last input character is a backslash */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   668
                    goto fail;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   669
                }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   670
                skipped_bytes++;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   671
                input_end++;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   672
            }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   673
            input_end++;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   674
        }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   675
        if (((size_t)(input_end - input_buffer->content) >= input_buffer->length) || (*input_end != '\"'))
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   676
        {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   677
            goto fail; /* string ended unexpectedly */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   678
        }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   679
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   680
        /* This is at most how much we need for the output */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   681
        allocation_length = (size_t) (input_end - buffer_at_offset(input_buffer)) - skipped_bytes;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   682
        output = (unsigned char*)input_buffer->hooks.allocate(allocation_length + sizeof(""));
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   683
        if (output == NULL)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   684
        {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   685
            goto fail; /* allocation failure */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   686
        }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   687
    }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   688
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   689
    output_pointer = output;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   690
    /* loop through the string literal */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   691
    while (input_pointer < input_end)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   692
    {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   693
        if (*input_pointer != '\\')
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   694
        {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   695
            *output_pointer++ = *input_pointer++;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   696
        }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   697
        /* escape sequence */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   698
        else
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   699
        {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   700
            unsigned char sequence_length = 2;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   701
            if ((input_end - input_pointer) < 1)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   702
            {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   703
                goto fail;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   704
            }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   705
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   706
            switch (input_pointer[1])
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   707
            {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   708
                case 'b':
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   709
                    *output_pointer++ = '\b';
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   710
                    break;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   711
                case 'f':
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   712
                    *output_pointer++ = '\f';
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   713
                    break;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   714
                case 'n':
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   715
                    *output_pointer++ = '\n';
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   716
                    break;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   717
                case 'r':
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   718
                    *output_pointer++ = '\r';
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   719
                    break;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   720
                case 't':
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   721
                    *output_pointer++ = '\t';
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   722
                    break;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   723
                case '\"':
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   724
                case '\\':
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   725
                case '/':
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   726
                    *output_pointer++ = input_pointer[1];
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   727
                    break;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   728
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   729
                /* UTF-16 literal */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   730
                case 'u':
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   731
                    sequence_length = utf16_literal_to_utf8(input_pointer, input_end, &output_pointer);
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   732
                    if (sequence_length == 0)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   733
                    {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   734
                        /* failed to convert UTF16-literal to UTF-8 */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   735
                        goto fail;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   736
                    }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   737
                    break;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   738
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   739
                default:
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   740
                    goto fail;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   741
            }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   742
            input_pointer += sequence_length;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   743
        }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   744
    }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   745
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   746
    /* zero terminate the output */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   747
    *output_pointer = '\0';
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   748
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   749
    item->type = cJSON_String;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   750
    item->valuestring = (char*)output;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   751
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   752
    input_buffer->offset = (size_t) (input_end - input_buffer->content);
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   753
    input_buffer->offset++;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   754
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   755
    return true;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   756
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   757
fail:
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   758
    if (output != NULL)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   759
    {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   760
        input_buffer->hooks.deallocate(output);
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   761
    }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   762
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   763
    if (input_pointer != NULL)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   764
    {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   765
        input_buffer->offset = (size_t)(input_pointer - input_buffer->content);
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   766
    }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   767
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   768
    return false;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   769
}
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   770
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   771
/* Render the cstring provided to an escaped version that can be printed. */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   772
static cJSON_bool print_string_ptr(const unsigned char * const input, printbuffer * const output_buffer)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   773
{
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   774
    const unsigned char *input_pointer = NULL;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   775
    unsigned char *output = NULL;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   776
    unsigned char *output_pointer = NULL;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   777
    size_t output_length = 0;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   778
    /* numbers of additional characters needed for escaping */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   779
    size_t escape_characters = 0;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   780
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   781
    if (output_buffer == NULL)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   782
    {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   783
        return false;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   784
    }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   785
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   786
    /* empty string */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   787
    if (input == NULL)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   788
    {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   789
        output = ensure(output_buffer, sizeof("\"\""));
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   790
        if (output == NULL)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   791
        {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   792
            return false;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   793
        }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   794
        strcpy((char*)output, "\"\"");
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   795
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   796
        return true;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   797
    }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   798
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   799
    /* set "flag" to 1 if something needs to be escaped */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   800
    for (input_pointer = input; *input_pointer; input_pointer++)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   801
    {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   802
        switch (*input_pointer)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   803
        {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   804
            case '\"':
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   805
            case '\\':
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   806
            case '\b':
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   807
            case '\f':
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   808
            case '\n':
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   809
            case '\r':
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   810
            case '\t':
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   811
                /* one character escape sequence */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   812
                escape_characters++;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   813
                break;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   814
            default:
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   815
                if (*input_pointer < 32)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   816
                {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   817
                    /* UTF-16 escape sequence uXXXX */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   818
                    escape_characters += 5;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   819
                }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   820
                break;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   821
        }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   822
    }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   823
    output_length = (size_t)(input_pointer - input) + escape_characters;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   824
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   825
    output = ensure(output_buffer, output_length + sizeof("\"\""));
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   826
    if (output == NULL)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   827
    {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   828
        return false;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   829
    }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   830
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   831
    /* no characters have to be escaped */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   832
    if (escape_characters == 0)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   833
    {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   834
        output[0] = '\"';
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   835
        memcpy(output + 1, input, output_length);
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   836
        output[output_length + 1] = '\"';
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   837
        output[output_length + 2] = '\0';
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   838
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   839
        return true;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   840
    }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   841
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   842
    output[0] = '\"';
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   843
    output_pointer = output + 1;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   844
    /* copy the string */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   845
    for (input_pointer = input; *input_pointer != '\0'; (void)input_pointer++, output_pointer++)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   846
    {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   847
        if ((*input_pointer > 31) && (*input_pointer != '\"') && (*input_pointer != '\\'))
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   848
        {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   849
            /* normal character, copy */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   850
            *output_pointer = *input_pointer;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   851
        }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   852
        else
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   853
        {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   854
            /* character needs to be escaped */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   855
            *output_pointer++ = '\\';
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   856
            switch (*input_pointer)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   857
            {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   858
                case '\\':
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   859
                    *output_pointer = '\\';
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   860
                    break;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   861
                case '\"':
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   862
                    *output_pointer = '\"';
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   863
                    break;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   864
                case '\b':
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   865
                    *output_pointer = 'b';
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   866
                    break;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   867
                case '\f':
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   868
                    *output_pointer = 'f';
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   869
                    break;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   870
                case '\n':
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   871
                    *output_pointer = 'n';
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   872
                    break;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   873
                case '\r':
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   874
                    *output_pointer = 'r';
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   875
                    break;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   876
                case '\t':
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   877
                    *output_pointer = 't';
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   878
                    break;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   879
                default:
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   880
                    /* escape and print as unicode codepoint */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   881
                    sprintf((char*)output_pointer, "u%04x", *input_pointer);
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   882
                    output_pointer += 4;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   883
                    break;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   884
            }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   885
        }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   886
    }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   887
    output[output_length + 1] = '\"';
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   888
    output[output_length + 2] = '\0';
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   889
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   890
    return true;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   891
}
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   892
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   893
/* Invoke print_string_ptr (which is useful) on an item. */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   894
static cJSON_bool print_string(const cJSON * const item, printbuffer * const p)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   895
{
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   896
    return print_string_ptr((unsigned char*)item->valuestring, p);
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   897
}
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   898
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   899
/* Predeclare these prototypes. */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   900
static cJSON_bool parse_value(cJSON * const item, parse_buffer * const input_buffer);
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   901
static cJSON_bool print_value(const cJSON * const item, printbuffer * const output_buffer);
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   902
static cJSON_bool parse_array(cJSON * const item, parse_buffer * const input_buffer);
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   903
static cJSON_bool print_array(const cJSON * const item, printbuffer * const output_buffer);
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   904
static cJSON_bool parse_object(cJSON * const item, parse_buffer * const input_buffer);
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   905
static cJSON_bool print_object(const cJSON * const item, printbuffer * const output_buffer);
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   906
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   907
/* Utility to jump whitespace and cr/lf */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   908
static parse_buffer *buffer_skip_whitespace(parse_buffer * const buffer)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   909
{
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   910
    if ((buffer == NULL) || (buffer->content == NULL))
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   911
    {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   912
        return NULL;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   913
    }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   914
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   915
    while (can_access_at_index(buffer, 0) && (buffer_at_offset(buffer)[0] <= 32))
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   916
    {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   917
       buffer->offset++;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   918
    }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   919
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   920
    if (buffer->offset == buffer->length)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   921
    {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   922
        buffer->offset--;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   923
    }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   924
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   925
    return buffer;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   926
}
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   927
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   928
/* Parse an object - create a new root, and populate. */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   929
CJSON_PUBLIC(cJSON *) cJSON_ParseWithOpts(const char *value, const char **return_parse_end, cJSON_bool require_null_terminated)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   930
{
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   931
    parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0 } };
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   932
    cJSON *item = NULL;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   933
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   934
    /* reset error position */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   935
    global_error.json = NULL;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   936
    global_error.position = 0;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   937
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   938
    if (value == NULL)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   939
    {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   940
        goto fail;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   941
    }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   942
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   943
    buffer.content = (const unsigned char*)value;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   944
    buffer.length = strlen((const char*)value) + sizeof("");
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   945
    buffer.offset = 0;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   946
    buffer.hooks = global_hooks;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   947
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   948
    item = cJSON_New_Item(&global_hooks);
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   949
    if (item == NULL) /* memory fail */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   950
    {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   951
        goto fail;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   952
    }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   953
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   954
    if (!parse_value(item, buffer_skip_whitespace(&buffer)))
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   955
    {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   956
        /* parse failure. ep is set. */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   957
        goto fail;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   958
    }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   959
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   960
    /* if we require null-terminated JSON without appended garbage, skip and then check for a null terminator */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   961
    if (require_null_terminated)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   962
    {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   963
        buffer_skip_whitespace(&buffer);
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   964
        if ((buffer.offset >= buffer.length) || buffer_at_offset(&buffer)[0] != '\0')
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   965
        {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   966
            goto fail;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   967
        }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   968
    }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   969
    if (return_parse_end)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   970
    {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   971
        *return_parse_end = (const char*)buffer_at_offset(&buffer);
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   972
    }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   973
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   974
    return item;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   975
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   976
fail:
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   977
    if (item != NULL)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   978
    {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   979
        cJSON_Delete(item);
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   980
    }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   981
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   982
    if (value != NULL)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   983
    {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   984
        error local_error;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   985
        local_error.json = (const unsigned char*)value;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   986
        local_error.position = 0;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   987
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   988
        if (buffer.offset < buffer.length)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   989
        {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   990
            local_error.position = buffer.offset;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   991
        }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   992
        else if (buffer.length > 0)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   993
        {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   994
            local_error.position = buffer.length - 1;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   995
        }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   996
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   997
        if (return_parse_end != NULL)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   998
        {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   999
            *return_parse_end = (const char*)local_error.json + local_error.position;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1000
        }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1001
        else
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1002
        {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1003
            global_error = local_error;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1004
        }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1005
    }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1006
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1007
    return NULL;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1008
}
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1009
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1010
/* Default options for cJSON_Parse */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1011
CJSON_PUBLIC(cJSON *) cJSON_Parse(const char *value)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1012
{
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1013
    return cJSON_ParseWithOpts(value, 0, 0);
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1014
}
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1015
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1016
#define cjson_min(a, b) ((a < b) ? a : b)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1017
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1018
static unsigned char *print(const cJSON * const item, cJSON_bool format, const internal_hooks * const hooks)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1019
{
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1020
    printbuffer buffer[1];
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1021
    unsigned char *printed = NULL;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1022
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1023
    memset(buffer, 0, sizeof(buffer));
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1024
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1025
    /* create buffer */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1026
    buffer->buffer = (unsigned char*) hooks->allocate(256);
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1027
    buffer->format = format;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1028
    buffer->hooks = *hooks;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1029
    if (buffer->buffer == NULL)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1030
    {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1031
        goto fail;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1032
    }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1033
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1034
    /* print the value */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1035
    if (!print_value(item, buffer))
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1036
    {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1037
        goto fail;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1038
    }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1039
    update_offset(buffer);
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1040
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1041
    /* check if reallocate is available */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1042
    if (hooks->reallocate != NULL)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1043
    {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1044
        printed = (unsigned char*) hooks->reallocate(buffer->buffer, buffer->length);
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1045
        buffer->buffer = NULL;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1046
        if (printed == NULL) {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1047
            goto fail;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1048
        }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1049
    }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1050
    else /* otherwise copy the JSON over to a new buffer */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1051
    {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1052
        printed = (unsigned char*) hooks->allocate(buffer->offset + 1);
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1053
        if (printed == NULL)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1054
        {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1055
            goto fail;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1056
        }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1057
        memcpy(printed, buffer->buffer, cjson_min(buffer->length, buffer->offset + 1));
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1058
        printed[buffer->offset] = '\0'; /* just to be sure */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1059
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1060
        /* free the buffer */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1061
        hooks->deallocate(buffer->buffer);
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1062
    }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1063
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1064
    return printed;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1065
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1066
fail:
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1067
    if (buffer->buffer != NULL)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1068
    {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1069
        hooks->deallocate(buffer->buffer);
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1070
    }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1071
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1072
    if (printed != NULL)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1073
    {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1074
        hooks->deallocate(printed);
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1075
    }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1076
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1077
    return NULL;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1078
}
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1079
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1080
/* Render a cJSON item/entity/structure to text. */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1081
CJSON_PUBLIC(char *) cJSON_Print(const cJSON *item)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1082
{
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1083
    return (char*)print(item, true, &global_hooks);
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1084
}
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1085
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1086
CJSON_PUBLIC(char *) cJSON_PrintUnformatted(const cJSON *item)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1087
{
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1088
    return (char*)print(item, false, &global_hooks);
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1089
}
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1090
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1091
CJSON_PUBLIC(char *) cJSON_PrintBuffered(const cJSON *item, int prebuffer, cJSON_bool fmt)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1092
{
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1093
    printbuffer p = { 0, 0, 0, 0, 0, 0, { 0, 0, 0 } };
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1094
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1095
    if (prebuffer < 0)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1096
    {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1097
        return NULL;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1098
    }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1099
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1100
    p.buffer = (unsigned char*)global_hooks.allocate((size_t)prebuffer);
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1101
    if (!p.buffer)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1102
    {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1103
        return NULL;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1104
    }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1105
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1106
    p.length = (size_t)prebuffer;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1107
    p.offset = 0;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1108
    p.noalloc = false;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1109
    p.format = fmt;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1110
    p.hooks = global_hooks;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1111
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1112
    if (!print_value(item, &p))
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1113
    {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1114
        return NULL;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1115
    }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1116
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1117
    return (char*)p.buffer;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1118
}
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1119
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1120
CJSON_PUBLIC(cJSON_bool) cJSON_PrintPreallocated(cJSON *item, char *buf, const int len, const cJSON_bool fmt)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1121
{
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1122
    printbuffer p = { 0, 0, 0, 0, 0, 0, { 0, 0, 0 } };
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1123
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1124
    if (len < 0)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1125
    {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1126
        return false;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1127
    }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1128
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1129
    p.buffer = (unsigned char*)buf;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1130
    p.length = (size_t)len;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1131
    p.offset = 0;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1132
    p.noalloc = true;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1133
    p.format = fmt;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1134
    p.hooks = global_hooks;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1135
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1136
    return print_value(item, &p);
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1137
}
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1138
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1139
/* Parser core - when encountering text, process appropriately. */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1140
static cJSON_bool parse_value(cJSON * const item, parse_buffer * const input_buffer)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1141
{
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1142
    if ((input_buffer == NULL) || (input_buffer->content == NULL))
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1143
    {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1144
        return false; /* no input */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1145
    }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1146
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1147
    /* parse the different types of values */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1148
    /* null */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1149
    if (can_read(input_buffer, 4) && (strncmp((const char*)buffer_at_offset(input_buffer), "null", 4) == 0))
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1150
    {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1151
        item->type = cJSON_NULL;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1152
        input_buffer->offset += 4;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1153
        return true;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1154
    }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1155
    /* false */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1156
    if (can_read(input_buffer, 5) && (strncmp((const char*)buffer_at_offset(input_buffer), "false", 5) == 0))
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1157
    {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1158
        item->type = cJSON_False;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1159
        input_buffer->offset += 5;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1160
        return true;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1161
    }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1162
    /* true */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1163
    if (can_read(input_buffer, 4) && (strncmp((const char*)buffer_at_offset(input_buffer), "true", 4) == 0))
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1164
    {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1165
        item->type = cJSON_True;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1166
        item->valueint = 1;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1167
        input_buffer->offset += 4;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1168
        return true;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1169
    }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1170
    /* string */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1171
    if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == '\"'))
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1172
    {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1173
        return parse_string(item, input_buffer);
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1174
    }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1175
    /* number */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1176
    if (can_access_at_index(input_buffer, 0) && ((buffer_at_offset(input_buffer)[0] == '-') || ((buffer_at_offset(input_buffer)[0] >= '0') && (buffer_at_offset(input_buffer)[0] <= '9'))))
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1177
    {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1178
        return parse_number(item, input_buffer);
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1179
    }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1180
    /* array */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1181
    if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == '['))
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1182
    {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1183
        return parse_array(item, input_buffer);
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1184
    }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1185
    /* object */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1186
    if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == '{'))
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1187
    {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1188
        return parse_object(item, input_buffer);
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1189
    }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1190
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1191
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1192
    return false;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1193
}
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1194
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1195
/* Render a value to text. */
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1196
static cJSON_bool print_value(const cJSON * const item, printbuffer * const output_buffer)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1197
{
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1198
    unsigned char *output = NULL;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1199
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1200
    if ((item == NULL) || (output_buffer == NULL))
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1201
    {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1202
        return false;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1203
    }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1204
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1205
    switch ((item->type) & 0xFF)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1206
    {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1207
        case cJSON_NULL:
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1208
            output = ensure(output_buffer, 5);
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1209
            if (output == NULL)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1210
            {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1211
                return false;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1212
            }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1213
            strcpy((char*)output, "null");
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1214
            return true;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1215
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1216
        case cJSON_False:
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1217
            output = ensure(output_buffer, 6);
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1218
            if (output == NULL)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1219
            {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1220
                return false;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1221
            }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1222
            strcpy((char*)output, "false");
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1223
            return true;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1224
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1225
        case cJSON_True:
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1226
            output = ensure(output_buffer, 5);
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1227
            if (output == NULL)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1228
            {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1229
                return false;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1230
            }
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1231
            strcpy((char*)output, "true");
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1232
            return true;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1233
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1234
        case cJSON_Number:
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1235
            return print_number(item, output_buffer);
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1236
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1237
        case cJSON_Raw:
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1238
        {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1239
            size_t raw_length = 0;
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1240
            if (item->valuestring == NULL)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1241
            {
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1242
                if (!output_buffer->noalloc)
1390348facc7 Command line tool that decrypts an OPVault keychain and dumps it to stdout.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1243
                {