forked from ElementsProject/lightning
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ccan: import ccan/json_out and ccan/json_escape.
These are generalized from our internal implementations. The main difference is that 'struct json_escaped' is now 'struct json_escape', so we replace that immediately. The difference between lightningd's json-writing ringbuffer and the more generic ccan/json_out is that the latter has a better API and handles escaping transparently if something slips through (though it does offer direct accessors so you can mess things up yourself!). Signed-off-by: Rusty Russell <[email protected]>
- Loading branch information
1 parent
26cdf9d
commit 220449e
Showing
45 changed files
with
1,156 additions
and
191 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../licenses/BSD-MIT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#include "config.h" | ||
#include <stdio.h> | ||
#include <string.h> | ||
|
||
/** | ||
* json_escape - Escape sequences for JSON strings | ||
* | ||
* This code helps you format strings into forms useful for JSON. | ||
* | ||
* Author: Rusty Russell <[email protected]> | ||
* License: BSD-MIT | ||
* Example: | ||
* // Print arguments as a JSON array. | ||
* #include <ccan/json_escape/json_escape.h> | ||
* | ||
* int main(int argc, char *argv[]) | ||
* { | ||
* printf("["); | ||
* for (int i = 1; i < argc; i++) { | ||
* struct json_escape *e = json_escape(NULL, argv[i]); | ||
* printf("%s\"%s\"", i == 1 ? "" : ",", e->s); | ||
* } | ||
* printf("]\n"); | ||
* return 0; | ||
* } | ||
*/ | ||
int main(int argc, char *argv[]) | ||
{ | ||
/* Expect exactly one argument */ | ||
if (argc != 2) | ||
return 1; | ||
|
||
if (strcmp(argv[1], "depends") == 0) { | ||
printf("ccan/tal\n"); | ||
return 0; | ||
} | ||
|
||
return 1; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* MIT (BSD) license - see LICENSE file for details */ | ||
#ifndef CCAN_JSON_ESCAPE_H | ||
#define CCAN_JSON_ESCAPE_H | ||
#include "config.h" | ||
#include <ccan/tal/tal.h> | ||
|
||
/* Type differentiation for a correctly-escaped JSON string */ | ||
struct json_escape { | ||
/* NUL terminated string. */ | ||
char s[1]; | ||
}; | ||
|
||
/** | ||
* json_escape - escape a valid UTF-8 string. | ||
* @ctx: tal context to allocate from. | ||
* @str: the string to escape. | ||
* | ||
* Allocates and returns a valid JSON string (without surrounding quotes). | ||
*/ | ||
struct json_escape *json_escape(const tal_t *ctx, const char *str TAKES); | ||
|
||
/* Version with @len */ | ||
struct json_escape *json_escape_len(const tal_t *ctx, | ||
const char *str TAKES, size_t len); | ||
|
||
/* @str is a valid UTF-8 string which may already contain escapes. */ | ||
struct json_escape *json_partial_escape(const tal_t *ctx, | ||
const char *str TAKES); | ||
|
||
/* Do we need to escape this str? */ | ||
bool json_escape_needed(const char *str, size_t len); | ||
|
||
/* Are two escape json strings identical? */ | ||
bool json_escape_eq(const struct json_escape *a, | ||
const struct json_escape *b); | ||
|
||
/* Internal routine for creating json_escape from bytes. */ | ||
struct json_escape *json_escape_string_(const tal_t *ctx, | ||
const void *bytes, size_t len); | ||
|
||
/* Be very careful here! Can fail! Doesn't handle \u: use UTF-8 please. */ | ||
const char *json_escape_unescape(const tal_t *ctx, | ||
const struct json_escape *esc); | ||
#endif /* CCAN_JSON_ESCAPE_H */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#include <ccan/json_escape/json_escape.h> | ||
/* Include the C files directly. */ | ||
#include <ccan/json_escape/json_escape.c> | ||
#include <ccan/tap/tap.h> | ||
|
||
int main(void) | ||
{ | ||
const tal_t *ctx = tal(NULL, char); | ||
|
||
/* This is how many tests you plan to run */ | ||
plan_tests(21); | ||
|
||
ok1(!strcmp(json_partial_escape(ctx, "\\")->s, "\\\\")); | ||
ok1(!strcmp(json_partial_escape(ctx, "\\\\")->s, "\\\\")); | ||
ok1(!strcmp(json_partial_escape(ctx, "\\\\\\")->s, "\\\\\\\\")); | ||
ok1(!strcmp(json_partial_escape(ctx, "\\\\\\\\")->s, "\\\\\\\\")); | ||
ok1(!strcmp(json_partial_escape(ctx, "\\n")->s, "\\n")); | ||
ok1(!strcmp(json_partial_escape(ctx, "\n")->s, "\\n")); | ||
ok1(!strcmp(json_partial_escape(ctx, "\\\"")->s, "\\\"")); | ||
ok1(!strcmp(json_partial_escape(ctx, "\"")->s, "\\\"")); | ||
ok1(!strcmp(json_partial_escape(ctx, "\\t")->s, "\\t")); | ||
ok1(!strcmp(json_partial_escape(ctx, "\t")->s, "\\t")); | ||
ok1(!strcmp(json_partial_escape(ctx, "\\b")->s, "\\b")); | ||
ok1(!strcmp(json_partial_escape(ctx, "\b")->s, "\\b")); | ||
ok1(!strcmp(json_partial_escape(ctx, "\\r")->s, "\\r")); | ||
ok1(!strcmp(json_partial_escape(ctx, "\r")->s, "\\r")); | ||
ok1(!strcmp(json_partial_escape(ctx, "\\f")->s, "\\f")); | ||
ok1(!strcmp(json_partial_escape(ctx, "\f")->s, "\\f")); | ||
/* You're allowed to escape / according to json.org. */ | ||
ok1(!strcmp(json_partial_escape(ctx, "\\/")->s, "\\/")); | ||
ok1(!strcmp(json_partial_escape(ctx, "/")->s, "/")); | ||
|
||
ok1(!strcmp(json_partial_escape(ctx, "\\u0FFF")->s, "\\u0FFF")); | ||
ok1(!strcmp(json_partial_escape(ctx, "\\u0FFFx")->s, "\\u0FFFx")); | ||
|
||
/* Unknown escapes should be escaped. */ | ||
ok1(!strcmp(json_partial_escape(ctx, "\\x")->s, "\\\\x")); | ||
tal_free(ctx); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#include <ccan/json_escape/json_escape.h> | ||
/* Include the C files directly. */ | ||
#include <ccan/json_escape/json_escape.c> | ||
#include <ccan/tap/tap.h> | ||
|
||
int main(void) | ||
{ | ||
const tal_t *ctx = tal(NULL, char); | ||
struct json_escape *e; | ||
char *p; | ||
|
||
/* This is how many tests you plan to run */ | ||
plan_tests(5); | ||
|
||
/* This should simply be tal_steal */ | ||
p = tal_dup_arr(NULL, char, "Hello", 6, 0); | ||
e = json_escape(ctx, take(p)); | ||
ok1(!strcmp(e->s, "Hello")); | ||
ok1((void *)e == (void *)p); | ||
ok1(tal_parent(e) == ctx); | ||
|
||
/* This can't be tal_steal, but still should be freed. */ | ||
p = tal_dup_arr(NULL, char, | ||
"\\\b\f\n\r\t\"" | ||
"\\\\\\b\\f\\n\\r\\t\\\"", 22, 0); | ||
e = json_escape(ctx, take(p)); | ||
ok1(tal_parent(e) == ctx); | ||
ok1(!strcmp(e->s, | ||
"\\\\\\b\\f\\n\\r\\t\\\"" | ||
"\\\\\\\\\\\\b\\\\f\\\\n\\\\r\\\\t\\\\\\\"")); | ||
tal_free(ctx); | ||
|
||
/* This exits depending on whether all tests passed */ | ||
return exit_status(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#include <ccan/json_escape/json_escape.h> | ||
/* Include the C files directly. */ | ||
#include <ccan/json_escape/json_escape.c> | ||
#include <ccan/tap/tap.h> | ||
|
||
int main(void) | ||
{ | ||
const tal_t *ctx = tal(NULL, char); | ||
struct json_escape *e; | ||
|
||
/* This is how many tests you plan to run */ | ||
plan_tests(6); | ||
|
||
e = json_escape(ctx, "Hello"); | ||
ok1(!strcmp(e->s, "Hello")); | ||
ok1(!strcmp(json_escape_unescape(ctx, e), | ||
"Hello")); | ||
|
||
e = json_escape(ctx, | ||
"\\\b\f\n\r\t\"" | ||
"\\\\\\b\\f\\n\\r\\t\\\""); | ||
ok1(!strcmp(e->s, | ||
"\\\\\\b\\f\\n\\r\\t\\\"" | ||
"\\\\\\\\\\\\b\\\\f\\\\n\\\\r\\\\t\\\\\\\"")); | ||
ok1(!strcmp(json_escape_unescape(ctx, e), | ||
"\\\b\f\n\r\t\"" | ||
"\\\\\\b\\f\\n\\r\\t\\\"")); | ||
|
||
/* This one doesn't escape the already-escaped chars */ | ||
e = json_partial_escape(ctx, | ||
"\\\b\f\n\r\t\"" | ||
"\\\\\\b\\f\\n\\r\\t\\\""); | ||
ok1(!strcmp(e->s, | ||
"\\\\\\b\\f\\n\\r\\t\\\"" | ||
"\\\\\\b\\f\\n\\r\\t\\\"")); | ||
ok1(!strcmp(json_escape_unescape(ctx, e), | ||
"\\\b\f\n\r\t\"" | ||
"\\\b\f\n\r\t\"")); | ||
|
||
tal_free(ctx); | ||
|
||
/* This exits depending on whether all tests passed */ | ||
return exit_status(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../licenses/BSD-MIT |
Oops, something went wrong.