/* * Copyright (C) 2022-2025 Jordan Bancino <@jordan:synapse.telodendria.org> * with other valuable contributors. See CONTRIBUTORS.txt for the full list. * * Permission is hereby granted, free of charge, to any person * obtaining a copy of this software and associated documentation files * (the "Software"), to deal in the Software without restriction, * including without limitation the rights to use, copy, modify, merge, * publish, distribute, sublicense, and/or sell copies of the Software, * and to permit persons to whom the Software is furnished to do so, * subject to the following conditions: * * The above copyright notice and this permission notice shall be * included in all copies or portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ #include #include #include #include #include #include static int CanonicalJsonKeyCompare(void *k1, void *k2) { return strcmp((char *) k1, (char *) k2); } static int CanonicalJsonString(const char * str, Stream * out) { const unsigned char *p; int length = 0; StreamPutc(out, '"'); length++; for (p = (const unsigned char *) str; *p; p++) { switch (*p) { case '"': StreamPuts(out, "\\\""); length += 2; break; case '\\': StreamPuts(out, "\\\\"); length += 2; break; case '\b': StreamPuts(out, "\\b"); length += 2; break; case '\t': StreamPuts(out, "\\t"); length += 2; break; case '\n': StreamPuts(out, "\\n"); length += 2; break; case '\f': StreamPuts(out, "\\f"); length += 2; break; case '\r': StreamPuts(out, "\\r"); length += 2; break; default: if (*p < 0x20) { char buf[7]; snprintf(buf, sizeof(buf), "\\u%04x", *p); StreamPuts(out, buf); length += 6; } else { StreamPutc(out, *p); length++; } break; } } StreamPutc(out, '"'); length++; return length; } int CanonicalJsonEncodeValue(JsonValue * value, Stream * out) { Array *arr; size_t i, len; int length = 0; /* Override object type to encode using the canonical functions */ switch (JsonValueType(value)) { case JSON_OBJECT: length += CanonicalJsonEncode(JsonValueAsObject(value), out); break; case JSON_ARRAY: arr = JsonValueAsArray(value); len = ArraySize(arr); StreamPutc(out, '['); length++; { int first = 1; for (i = 0; i < len; i++) { JsonValue *aVal = ArrayGet(arr, i); if (JsonValueType(aVal) == JSON_FLOAT) { continue; } if (!first) { StreamPutc(out, ','); length++; } first = 0; length += CanonicalJsonEncodeValue(aVal, out); } } StreamPutc(out, ']'); length++; break; case JSON_STRING: length += CanonicalJsonString(JsonValueAsString(value), out); break; default: length += JsonEncodeValue(value, out, JSON_DEFAULT); break; } return length; } int CanonicalJsonEncode(HashMap * object, Stream * out) { char *key; JsonValue *value; Array *keys; size_t i; size_t keyCount; int length; if (!object) { return -1; } keys = ArrayCreate(); if (!keys) { return -1; } while (HashMapIterate(object, &key, (void **) &value)) { ArrayAdd(keys, key); } ArraySort(keys, CanonicalJsonKeyCompare); /* The total number of bytes written */ length = 0; StreamPutc(out, '{'); length++; keyCount = ArraySize(keys); { int first = 1; for (i = 0; i < keyCount; i++) { key = (char *) ArrayGet(keys, i); value = (JsonValue *) HashMapGet(object, key); if (JsonValueType(value) == JSON_FLOAT) { continue; } if (!first) { StreamPutc(out, ','); length++; } first = 0; length += CanonicalJsonString(key, out); StreamPutc(out, ':'); length++; length += CanonicalJsonEncodeValue(value, out); } } StreamPutc(out, '}'); length++; ArrayFree(keys); return length; }