cJSON.h
author Ryan C. Gordon <icculus@icculus.org>
Fri, 23 Jun 2017 17:28:03 -0400
changeset 58 1390348facc7
permissions -rw-r--r--
Command line tool that decrypts an OPVault keychain and dumps it to stdout.

To compile: gcc -o opvault opvault.c cJSON.c -lcrypto

Usage: ./opvault </path/to/mykeychain.opvault> <password>

This is just a proof of concept; I'll be recycling this into proper OPVault
support in 1pass later and deleting this tool.

This uses OpenSSL's libcrypto for the math instead of all the homegrown
crypto this project is otherwise using. I'll probably migrate the rest in
this direction, too, since this wasn't as bad as I expected to use and
gets you all the package-manager mojo of automatic bug fixes and security
patches and shared code, etc.

cJSON parses JSON in C. That is from https://github.com/DaveGamble/cJSON

An example OPVault keychain from AgileBits is available here:

https://cache.agilebits.com/security-kb/
icculus@58
     1
/*
icculus@58
     2
  Copyright (c) 2009-2017 Dave Gamble and cJSON contributors
icculus@58
     3
icculus@58
     4
  Permission is hereby granted, free of charge, to any person obtaining a copy
icculus@58
     5
  of this software and associated documentation files (the "Software"), to deal
icculus@58
     6
  in the Software without restriction, including without limitation the rights
icculus@58
     7
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
icculus@58
     8
  copies of the Software, and to permit persons to whom the Software is
icculus@58
     9
  furnished to do so, subject to the following conditions:
icculus@58
    10
icculus@58
    11
  The above copyright notice and this permission notice shall be included in
icculus@58
    12
  all copies or substantial portions of the Software.
icculus@58
    13
icculus@58
    14
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
icculus@58
    15
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
icculus@58
    16
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
icculus@58
    17
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
icculus@58
    18
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
icculus@58
    19
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
icculus@58
    20
  THE SOFTWARE.
icculus@58
    21
*/
icculus@58
    22
icculus@58
    23
#ifndef cJSON__h
icculus@58
    24
#define cJSON__h
icculus@58
    25
icculus@58
    26
#ifdef __cplusplus
icculus@58
    27
extern "C"
icculus@58
    28
{
icculus@58
    29
#endif
icculus@58
    30
icculus@58
    31
/* project version */
icculus@58
    32
#define CJSON_VERSION_MAJOR 1
icculus@58
    33
#define CJSON_VERSION_MINOR 5
icculus@58
    34
#define CJSON_VERSION_PATCH 5
icculus@58
    35
icculus@58
    36
#include <stddef.h>
icculus@58
    37
icculus@58
    38
/* cJSON Types: */
icculus@58
    39
#define cJSON_Invalid (0)
icculus@58
    40
#define cJSON_False  (1 << 0)
icculus@58
    41
#define cJSON_True   (1 << 1)
icculus@58
    42
#define cJSON_NULL   (1 << 2)
icculus@58
    43
#define cJSON_Number (1 << 3)
icculus@58
    44
#define cJSON_String (1 << 4)
icculus@58
    45
#define cJSON_Array  (1 << 5)
icculus@58
    46
#define cJSON_Object (1 << 6)
icculus@58
    47
#define cJSON_Raw    (1 << 7) /* raw json */
icculus@58
    48
icculus@58
    49
#define cJSON_IsReference 256
icculus@58
    50
#define cJSON_StringIsConst 512
icculus@58
    51
icculus@58
    52
/* The cJSON structure: */
icculus@58
    53
typedef struct cJSON
icculus@58
    54
{
icculus@58
    55
    /* next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem */
icculus@58
    56
    struct cJSON *next;
icculus@58
    57
    struct cJSON *prev;
icculus@58
    58
    /* An array or object item will have a child pointer pointing to a chain of the items in the array/object. */
icculus@58
    59
    struct cJSON *child;
icculus@58
    60
icculus@58
    61
    /* The type of the item, as above. */
icculus@58
    62
    int type;
icculus@58
    63
icculus@58
    64
    /* The item's string, if type==cJSON_String  and type == cJSON_Raw */
icculus@58
    65
    char *valuestring;
icculus@58
    66
    /* writing to valueint is DEPRECATED, use cJSON_SetNumberValue instead */
icculus@58
    67
    int valueint;
icculus@58
    68
    /* The item's number, if type==cJSON_Number */
icculus@58
    69
    double valuedouble;
icculus@58
    70
icculus@58
    71
    /* The item's name string, if this item is the child of, or is in the list of subitems of an object. */
icculus@58
    72
    char *string;
icculus@58
    73
} cJSON;
icculus@58
    74
icculus@58
    75
typedef struct cJSON_Hooks
icculus@58
    76
{
icculus@58
    77
      void *(*malloc_fn)(size_t sz);
icculus@58
    78
      void (*free_fn)(void *ptr);
icculus@58
    79
} cJSON_Hooks;
icculus@58
    80
icculus@58
    81
typedef int cJSON_bool;
icculus@58
    82
icculus@58
    83
#if !defined(__WINDOWS__) && (defined(WIN32) || defined(WIN64) || defined(_MSC_VER) || defined(_WIN32))
icculus@58
    84
#define __WINDOWS__
icculus@58
    85
#endif
icculus@58
    86
#ifdef __WINDOWS__
icculus@58
    87
icculus@58
    88
/* When compiling for windows, we specify a specific calling convention to avoid issues where we are being called from a project with a different default calling convention.  For windows you have 2 define options:
icculus@58
    89
icculus@58
    90
CJSON_HIDE_SYMBOLS - Define this in the case where you don't want to ever dllexport symbols
icculus@58
    91
CJSON_EXPORT_SYMBOLS - Define this on library build when you want to dllexport symbols (default)
icculus@58
    92
CJSON_IMPORT_SYMBOLS - Define this if you want to dllimport symbol
icculus@58
    93
icculus@58
    94
For *nix builds that support visibility attribute, you can define similar behavior by
icculus@58
    95
icculus@58
    96
setting default visibility to hidden by adding
icculus@58
    97
-fvisibility=hidden (for gcc)
icculus@58
    98
or
icculus@58
    99
-xldscope=hidden (for sun cc)
icculus@58
   100
to CFLAGS
icculus@58
   101
icculus@58
   102
then using the CJSON_API_VISIBILITY flag to "export" the same symbols the way CJSON_EXPORT_SYMBOLS does
icculus@58
   103
icculus@58
   104
*/
icculus@58
   105
icculus@58
   106
/* export symbols by default, this is necessary for copy pasting the C and header file */
icculus@58
   107
#if !defined(CJSON_HIDE_SYMBOLS) && !defined(CJSON_IMPORT_SYMBOLS) && !defined(CJSON_EXPORT_SYMBOLS)
icculus@58
   108
#define CJSON_EXPORT_SYMBOLS
icculus@58
   109
#endif
icculus@58
   110
icculus@58
   111
#if defined(CJSON_HIDE_SYMBOLS)
icculus@58
   112
#define CJSON_PUBLIC(type)   type __stdcall
icculus@58
   113
#elif defined(CJSON_EXPORT_SYMBOLS)
icculus@58
   114
#define CJSON_PUBLIC(type)   __declspec(dllexport) type __stdcall
icculus@58
   115
#elif defined(CJSON_IMPORT_SYMBOLS)
icculus@58
   116
#define CJSON_PUBLIC(type)   __declspec(dllimport) type __stdcall
icculus@58
   117
#endif
icculus@58
   118
#else /* !WIN32 */
icculus@58
   119
#if (defined(__GNUC__) || defined(__SUNPRO_CC) || defined (__SUNPRO_C)) && defined(CJSON_API_VISIBILITY)
icculus@58
   120
#define CJSON_PUBLIC(type)   __attribute__((visibility("default"))) type
icculus@58
   121
#else
icculus@58
   122
#define CJSON_PUBLIC(type) type
icculus@58
   123
#endif
icculus@58
   124
#endif
icculus@58
   125
icculus@58
   126
/* Limits how deeply nested arrays/objects can be before cJSON rejects to parse them.
icculus@58
   127
 * This is to prevent stack overflows. */
icculus@58
   128
#ifndef CJSON_NESTING_LIMIT
icculus@58
   129
#define CJSON_NESTING_LIMIT 1000
icculus@58
   130
#endif
icculus@58
   131
icculus@58
   132
/* returns the version of cJSON as a string */
icculus@58
   133
CJSON_PUBLIC(const char*) cJSON_Version(void);
icculus@58
   134
icculus@58
   135
/* Supply malloc, realloc and free functions to cJSON */
icculus@58
   136
CJSON_PUBLIC(void) cJSON_InitHooks(cJSON_Hooks* hooks);
icculus@58
   137
icculus@58
   138
/* Memory Management: the caller is always responsible to free the results from all variants of cJSON_Parse (with cJSON_Delete) and cJSON_Print (with stdlib free, cJSON_Hooks.free_fn, or cJSON_free as appropriate). The exception is cJSON_PrintPreallocated, where the caller has full responsibility of the buffer. */
icculus@58
   139
/* Supply a block of JSON, and this returns a cJSON object you can interrogate. */
icculus@58
   140
CJSON_PUBLIC(cJSON *) cJSON_Parse(const char *value);
icculus@58
   141
/* Render a cJSON entity to text for transfer/storage. */
icculus@58
   142
CJSON_PUBLIC(char *) cJSON_Print(const cJSON *item);
icculus@58
   143
/* Render a cJSON entity to text for transfer/storage without any formatting. */
icculus@58
   144
CJSON_PUBLIC(char *) cJSON_PrintUnformatted(const cJSON *item);
icculus@58
   145
/* Render a cJSON entity to text using a buffered strategy. prebuffer is a guess at the final size. guessing well reduces reallocation. fmt=0 gives unformatted, =1 gives formatted */
icculus@58
   146
CJSON_PUBLIC(char *) cJSON_PrintBuffered(const cJSON *item, int prebuffer, cJSON_bool fmt);
icculus@58
   147
/* Render a cJSON entity to text using a buffer already allocated in memory with given length. Returns 1 on success and 0 on failure. */
icculus@58
   148
/* NOTE: cJSON is not always 100% accurate in estimating how much memory it will use, so to be safe allocate 5 bytes more than you actually need */
icculus@58
   149
CJSON_PUBLIC(cJSON_bool) cJSON_PrintPreallocated(cJSON *item, char *buffer, const int length, const cJSON_bool format);
icculus@58
   150
/* Delete a cJSON entity and all subentities. */
icculus@58
   151
CJSON_PUBLIC(void) cJSON_Delete(cJSON *c);
icculus@58
   152
icculus@58
   153
/* Returns the number of items in an array (or object). */
icculus@58
   154
CJSON_PUBLIC(int) cJSON_GetArraySize(const cJSON *array);
icculus@58
   155
/* Retrieve item number "item" from array "array". Returns NULL if unsuccessful. */
icculus@58
   156
CJSON_PUBLIC(cJSON *) cJSON_GetArrayItem(const cJSON *array, int index);
icculus@58
   157
/* Get item "string" from object. Case insensitive. */
icculus@58
   158
CJSON_PUBLIC(cJSON *) cJSON_GetObjectItem(const cJSON * const object, const char * const string);
icculus@58
   159
CJSON_PUBLIC(cJSON *) cJSON_GetObjectItemCaseSensitive(const cJSON * const object, const char * const string);
icculus@58
   160
CJSON_PUBLIC(cJSON_bool) cJSON_HasObjectItem(const cJSON *object, const char *string);
icculus@58
   161
/* For analysing failed parses. This returns a pointer to the parse error. You'll probably need to look a few chars back to make sense of it. Defined when cJSON_Parse() returns 0. 0 when cJSON_Parse() succeeds. */
icculus@58
   162
CJSON_PUBLIC(const char *) cJSON_GetErrorPtr(void);
icculus@58
   163
icculus@58
   164
/* These functions check the type of an item */
icculus@58
   165
CJSON_PUBLIC(cJSON_bool) cJSON_IsInvalid(const cJSON * const item);
icculus@58
   166
CJSON_PUBLIC(cJSON_bool) cJSON_IsFalse(const cJSON * const item);
icculus@58
   167
CJSON_PUBLIC(cJSON_bool) cJSON_IsTrue(const cJSON * const item);
icculus@58
   168
CJSON_PUBLIC(cJSON_bool) cJSON_IsBool(const cJSON * const item);
icculus@58
   169
CJSON_PUBLIC(cJSON_bool) cJSON_IsNull(const cJSON * const item);
icculus@58
   170
CJSON_PUBLIC(cJSON_bool) cJSON_IsNumber(const cJSON * const item);
icculus@58
   171
CJSON_PUBLIC(cJSON_bool) cJSON_IsString(const cJSON * const item);
icculus@58
   172
CJSON_PUBLIC(cJSON_bool) cJSON_IsArray(const cJSON * const item);
icculus@58
   173
CJSON_PUBLIC(cJSON_bool) cJSON_IsObject(const cJSON * const item);
icculus@58
   174
CJSON_PUBLIC(cJSON_bool) cJSON_IsRaw(const cJSON * const item);
icculus@58
   175
icculus@58
   176
/* These calls create a cJSON item of the appropriate type. */
icculus@58
   177
CJSON_PUBLIC(cJSON *) cJSON_CreateNull(void);
icculus@58
   178
CJSON_PUBLIC(cJSON *) cJSON_CreateTrue(void);
icculus@58
   179
CJSON_PUBLIC(cJSON *) cJSON_CreateFalse(void);
icculus@58
   180
CJSON_PUBLIC(cJSON *) cJSON_CreateBool(cJSON_bool boolean);
icculus@58
   181
CJSON_PUBLIC(cJSON *) cJSON_CreateNumber(double num);
icculus@58
   182
CJSON_PUBLIC(cJSON *) cJSON_CreateString(const char *string);
icculus@58
   183
/* raw json */
icculus@58
   184
CJSON_PUBLIC(cJSON *) cJSON_CreateRaw(const char *raw);
icculus@58
   185
CJSON_PUBLIC(cJSON *) cJSON_CreateArray(void);
icculus@58
   186
CJSON_PUBLIC(cJSON *) cJSON_CreateObject(void);
icculus@58
   187
icculus@58
   188
/* These utilities create an Array of count items. */
icculus@58
   189
CJSON_PUBLIC(cJSON *) cJSON_CreateIntArray(const int *numbers, int count);
icculus@58
   190
CJSON_PUBLIC(cJSON *) cJSON_CreateFloatArray(const float *numbers, int count);
icculus@58
   191
CJSON_PUBLIC(cJSON *) cJSON_CreateDoubleArray(const double *numbers, int count);
icculus@58
   192
CJSON_PUBLIC(cJSON *) cJSON_CreateStringArray(const char **strings, int count);
icculus@58
   193
icculus@58
   194
/* Append item to the specified array/object. */
icculus@58
   195
CJSON_PUBLIC(void) cJSON_AddItemToArray(cJSON *array, cJSON *item);
icculus@58
   196
CJSON_PUBLIC(void) cJSON_AddItemToObject(cJSON *object, const char *string, cJSON *item);
icculus@58
   197
/* Use this when string is definitely const (i.e. a literal, or as good as), and will definitely survive the cJSON object.
icculus@58
   198
 * WARNING: When this function was used, make sure to always check that (item->type & cJSON_StringIsConst) is zero before
icculus@58
   199
 * writing to `item->string` */
icculus@58
   200
CJSON_PUBLIC(void) cJSON_AddItemToObjectCS(cJSON *object, const char *string, cJSON *item);
icculus@58
   201
/* Append reference to item to the specified array/object. Use this when you want to add an existing cJSON to a new cJSON, but don't want to corrupt your existing cJSON. */
icculus@58
   202
CJSON_PUBLIC(void) cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item);
icculus@58
   203
CJSON_PUBLIC(void) cJSON_AddItemReferenceToObject(cJSON *object, const char *string, cJSON *item);
icculus@58
   204
icculus@58
   205
/* Remove/Detatch items from Arrays/Objects. */
icculus@58
   206
CJSON_PUBLIC(cJSON *) cJSON_DetachItemViaPointer(cJSON *parent, cJSON * const item);
icculus@58
   207
CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromArray(cJSON *array, int which);
icculus@58
   208
CJSON_PUBLIC(void) cJSON_DeleteItemFromArray(cJSON *array, int which);
icculus@58
   209
CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObject(cJSON *object, const char *string);
icculus@58
   210
CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObjectCaseSensitive(cJSON *object, const char *string);
icculus@58
   211
CJSON_PUBLIC(void) cJSON_DeleteItemFromObject(cJSON *object, const char *string);
icculus@58
   212
CJSON_PUBLIC(void) cJSON_DeleteItemFromObjectCaseSensitive(cJSON *object, const char *string);
icculus@58
   213
icculus@58
   214
/* Update array items. */
icculus@58
   215
CJSON_PUBLIC(void) cJSON_InsertItemInArray(cJSON *array, int which, cJSON *newitem); /* Shifts pre-existing items to the right. */
icculus@58
   216
CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemViaPointer(cJSON * const parent, cJSON * const item, cJSON * replacement);
icculus@58
   217
CJSON_PUBLIC(void) cJSON_ReplaceItemInArray(cJSON *array, int which, cJSON *newitem);
icculus@58
   218
CJSON_PUBLIC(void) cJSON_ReplaceItemInObject(cJSON *object,const char *string,cJSON *newitem);
icculus@58
   219
CJSON_PUBLIC(void) cJSON_ReplaceItemInObjectCaseSensitive(cJSON *object,const char *string,cJSON *newitem);
icculus@58
   220
icculus@58
   221
/* Duplicate a cJSON item */
icculus@58
   222
CJSON_PUBLIC(cJSON *) cJSON_Duplicate(const cJSON *item, cJSON_bool recurse);
icculus@58
   223
/* Duplicate will create a new, identical cJSON item to the one you pass, in new memory that will
icculus@58
   224
need to be released. With recurse!=0, it will duplicate any children connected to the item.
icculus@58
   225
The item->next and ->prev pointers are always zero on return from Duplicate. */
icculus@58
   226
/* Recursively compare two cJSON items for equality. If either a or b is NULL or invalid, they will be considered unequal.
icculus@58
   227
 * case_sensitive determines if object keys are treated case sensitive (1) or case insensitive (0) */
icculus@58
   228
CJSON_PUBLIC(cJSON_bool) cJSON_Compare(const cJSON * const a, const cJSON * const b, const cJSON_bool case_sensitive);
icculus@58
   229
icculus@58
   230
icculus@58
   231
/* ParseWithOpts allows you to require (and check) that the JSON is null terminated, and to retrieve the pointer to the final byte parsed. */
icculus@58
   232
/* If you supply a ptr in return_parse_end and parsing fails, then return_parse_end will contain a pointer to the error. If not, then cJSON_GetErrorPtr() does the job. */
icculus@58
   233
CJSON_PUBLIC(cJSON *) cJSON_ParseWithOpts(const char *value, const char **return_parse_end, cJSON_bool require_null_terminated);
icculus@58
   234
icculus@58
   235
CJSON_PUBLIC(void) cJSON_Minify(char *json);
icculus@58
   236
icculus@58
   237
/* Macros for creating things quickly. */
icculus@58
   238
#define cJSON_AddNullToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateNull())
icculus@58
   239
#define cJSON_AddTrueToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateTrue())
icculus@58
   240
#define cJSON_AddFalseToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateFalse())
icculus@58
   241
#define cJSON_AddBoolToObject(object,name,b) cJSON_AddItemToObject(object, name, cJSON_CreateBool(b))
icculus@58
   242
#define cJSON_AddNumberToObject(object,name,n) cJSON_AddItemToObject(object, name, cJSON_CreateNumber(n))
icculus@58
   243
#define cJSON_AddStringToObject(object,name,s) cJSON_AddItemToObject(object, name, cJSON_CreateString(s))
icculus@58
   244
#define cJSON_AddRawToObject(object,name,s) cJSON_AddItemToObject(object, name, cJSON_CreateRaw(s))
icculus@58
   245
icculus@58
   246
/* When assigning an integer value, it needs to be propagated to valuedouble too. */
icculus@58
   247
#define cJSON_SetIntValue(object, number) ((object) ? (object)->valueint = (object)->valuedouble = (number) : (number))
icculus@58
   248
/* helper for the cJSON_SetNumberValue macro */
icculus@58
   249
CJSON_PUBLIC(double) cJSON_SetNumberHelper(cJSON *object, double number);
icculus@58
   250
#define cJSON_SetNumberValue(object, number) ((object != NULL) ? cJSON_SetNumberHelper(object, (double)number) : (number))
icculus@58
   251
icculus@58
   252
/* Macro for iterating over an array or object */
icculus@58
   253
#define cJSON_ArrayForEach(element, array) for(element = (array != NULL) ? (array)->child : NULL; element != NULL; element = element->next)
icculus@58
   254
icculus@58
   255
/* malloc/free objects using the malloc/free functions that have been set with cJSON_InitHooks */
icculus@58
   256
CJSON_PUBLIC(void *) cJSON_malloc(size_t size);
icculus@58
   257
CJSON_PUBLIC(void) cJSON_free(void *object);
icculus@58
   258
icculus@58
   259
#ifdef __cplusplus
icculus@58
   260
}
icculus@58
   261
#endif
icculus@58
   262
icculus@58
   263
#endif