summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAnton Luka Šijanec <anton@sijanec.eu>2023-02-12 20:22:32 +0100
committerAnton Luka Šijanec <anton@sijanec.eu>2023-02-12 20:22:32 +0100
commitd7f3721ac6fa2acebc5005170591c05f6a569930 (patch)
tree176b2d98ac6f340bdead4cff1b8e250b272f3fdb
parentinsert.php error reporting, correct hashes (diff)
downloadtravnik-d7f3721ac6fa2acebc5005170591c05f6a569930.tar
travnik-d7f3721ac6fa2acebc5005170591c05f6a569930.tar.gz
travnik-d7f3721ac6fa2acebc5005170591c05f6a569930.tar.bz2
travnik-d7f3721ac6fa2acebc5005170591c05f6a569930.tar.lz
travnik-d7f3721ac6fa2acebc5005170591c05f6a569930.tar.xz
travnik-d7f3721ac6fa2acebc5005170591c05f6a569930.tar.zst
travnik-d7f3721ac6fa2acebc5005170591c05f6a569930.zip
-rw-r--r--src/bencoding.c18
1 files changed, 10 insertions, 8 deletions
diff --git a/src/bencoding.c b/src/bencoding.c
index a46380e..9ff9f89 100644
--- a/src/bencoding.c
+++ b/src/bencoding.c
@@ -1,3 +1,5 @@
+#include <stdbool.h>
+
/**
* enum of all possible bencoding types and some options to use
* to check a type, use ORing, not direct comparison, as bdecoded structs inherit opts from bdecode function in their ->types
@@ -8,7 +10,8 @@ enum benc {
num = 1 << 1,
list = 1 << 2,
dict = 1 << 3,
- replace = 1 << 4 /**< replace existing element with same key when using binsert() instead of prepending the new element before the old. see binsert() docs. */
+ replace = 1 << 4, /**< replace existing element with same key when using binsert() instead of prepending the new element before the old. see binsert() docs. */
+ nonascii2dot = 1 << 5 /**< see b2json() */
};
/**
@@ -197,22 +200,19 @@ int b2json_charsize (unsigned char a) {
return 2;
if (a < ' ')
return 6;
- if (a > 127)
- return 1;
return 1;
}
/**
* write a string representation of a character in a JSON string
*
- * non-ASCII characters are replaced with a '.'
- *
* @param dest [out] destination
* @param a [in] the character in question
+ * @param na2d [in] true if you want to convert non-ascii characters to . or false to output them literally (the latter is non-standard)
* @return the destination pointer, incremented for the number of bytes written
*/
-char * b2json_charrepr (char * dest, unsigned char a) {
+char * b2json_charrepr (char * dest, unsigned char a, bool na2d) {
switch (a) {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wstringop-truncation"
@@ -243,7 +243,7 @@ char * b2json_charrepr (char * dest, unsigned char a) {
sprintf(buf, "\\u00%02x", a);
strncpy(dest, buf, 6);
return dest+6;
- } else if (a > 127) {
+ } else if (a > 127 && na2d) {
*dest++ = '.';
return dest;
} else {
@@ -297,6 +297,8 @@ int b2json_length (struct bencoding * b) {
*
* writes false when struct has an incorrect type and null when NULL pointer is passed, this is in ordnung with b2json_length.
*
+ * you can set bencoding opt nonascii2dot to convert values above 127 to '.'
+ *
* @param dest [in] destination
* @param b [in] bencoding structure of a bdecoded element
* @return the destination pointer, incremented for the number of bytes written
@@ -316,7 +318,7 @@ char * b2json (char * dest, struct bencoding * b) {
if (b->type & string) {
*dest++ = '"';
for (size_t i = 0; i < b->valuelen; i++)
- dest = b2json_charrepr(dest, b->value[i]);
+ dest = b2json_charrepr(dest, b->value[i], b->type & nonascii2dot);
*dest++ = '"';
return dest;
}