Skip to content

Commit 1410a50

Browse files
cborpretty.c: add an API to show the string fragments
This change also makes tst_Parser use the new API, which in turn allows us to check that the parser did actually parse the chunks as we intended. Signed-off-by: Thiago Macieira <[email protected]>
1 parent ee8617d commit 1410a50

File tree

3 files changed

+33
-19
lines changed

3 files changed

+33
-19
lines changed

src/cbor.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,9 @@ enum CborPrettyFlags {
487487
CborPrettyNumericEncodingIndicators = 0x01,
488488
CborPrettyTextualEncodingIndicators = 0,
489489

490+
CborPrettyShowStringFragments = 0x100,
491+
CborPrettyMergeStringFragments = 0,
492+
490493
CborPrettyDefaultFlags = 0
491494
};
492495

src/cborpretty.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@
118118
*
119119
* \value CborPrettyNumericEncodingIndicators Use numeric encoding indicators instead of textual for float and half-float.
120120
* \value CborPrettyTextualEncodingIndicators Use textual encoding indicators for float ("f") and half-float ("f16").
121+
* \value CborPrettyShowStringFragments If the byte or text string is transmitted in chunks, show each individually.
122+
* \value CborPrettyMergeStringFragment Merge all chunked byte or text strings and display them in a single entry.
121123
* \value CborPrettyDefaultFlags Default conversion flags.
122124
*/
123125

@@ -341,6 +343,8 @@ static CborError value_to_pretty(FILE *out, CborValue *it, int flags)
341343
case CborTextStringType: {
342344
size_t n = 0;
343345
const void *ptr;
346+
bool showingFragments = (flags & CborPrettyShowStringFragments) && !cbor_value_is_length_known(it);
347+
const char *separator = "";
344348
char close = '\'';
345349
char open[3] = "h'";
346350

@@ -349,7 +353,7 @@ static CborError value_to_pretty(FILE *out, CborValue *it, int flags)
349353
open[1] = '\0';
350354
}
351355

352-
if (fputs(open, out) < 0)
356+
if (fputs(showingFragments ? "(_ " : open, out) < 0)
353357
return CborErrorIO;
354358

355359
while (1) {
@@ -359,12 +363,19 @@ static CborError value_to_pretty(FILE *out, CborValue *it, int flags)
359363
if (!ptr)
360364
break;
361365

366+
if (showingFragments && fprintf(out, "%s%s", separator, open) < 0)
367+
return CborErrorIO;
362368
err = (type == CborByteStringType ? hexDump(out, ptr, n) : utf8EscapedDump(out, ptr, n));
363369
if (err)
364370
return err;
371+
if (showingFragments) {
372+
if (fputc(close, out) < 0)
373+
return CborErrorIO;
374+
separator = ", ";
375+
}
365376
}
366377

367-
if (fputc(close, out) < 0)
378+
if (fputc(showingFragments ? ')' : close, out) < 0)
368379
return CborErrorIO;
369380
return CborNoError;
370381
}

tests/parser/tst_parser.cpp

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ CborError parseOne(CborValue *it, QString *parsed)
101101
char *buffer;
102102
size_t size;
103103

104+
int flags = CborPrettyShowStringFragments;
105+
104106
setlocale(LC_ALL, "C");
105107
#ifdef Q_CC_MSVC
106108
// no open_memstream, so use a temporary file
@@ -110,7 +112,7 @@ CborError parseOne(CborValue *it, QString *parsed)
110112
FILE *f = fopen(QFile::encodeName(tmp.fileName()), "w+");
111113
if (!f)
112114
return CborErrorIO;
113-
err = cbor_value_to_pretty_advance(f, it);
115+
err = cbor_value_to_pretty_advance_flags(f, it, flags);
114116
size = ftell(f);
115117
rewind(f);
116118

@@ -119,7 +121,7 @@ CborError parseOne(CborValue *it, QString *parsed)
119121
fclose(f);
120122
#else
121123
FILE *f = open_memstream(&buffer, &size);
122-
err = cbor_value_to_pretty_advance(f, it);
124+
err = cbor_value_to_pretty_advance_flags(f, it, flags);
123125
fclose(f);
124126
#endif
125127

@@ -130,8 +132,6 @@ CborError parseOne(CborValue *it, QString *parsed)
130132

131133
CborError parseOneChunk(CborValue *it, QString *parsed)
132134
{
133-
// we can't use the cborpretty.c API here because it uses
134-
// cbor_value_advance_fixed and cbor_value_dup_xxxx_string
135135
CborError err;
136136
CborType ourType = cbor_value_get_type(it);
137137
if (ourType == CborByteStringType) {
@@ -397,18 +397,18 @@ void addStringsData()
397397
QTest::newRow("textstring5*8") << raw("\x7b\0\0\0\0\0\0\0\x05Hello") << "\"Hello\"";
398398

399399
// strings with undefined length
400-
QTest::newRow("_emptybytestring") << raw("\x5f\xff") << "h''";
401-
QTest::newRow("_emptytextstring") << raw("\x7f\xff") << "\"\"";
402-
QTest::newRow("_emptybytestring2") << raw("\x5f\x40\xff") << "h''";
403-
QTest::newRow("_emptytextstring2") << raw("\x7f\x60\xff") << "\"\"";
404-
QTest::newRow("_emptybytestring3") << raw("\x5f\x40\x40\xff") << "h''";
405-
QTest::newRow("_emptytextstring3") << raw("\x7f\x60\x60\xff") << "\"\"";
406-
QTest::newRow("_bytestring5*2") << raw("\x5f\x43Hel\x42lo\xff") << "h'48656c6c6f'";
407-
QTest::newRow("_textstring5*2") << raw("\x7f\x63Hel\x62lo\xff") << "\"Hello\"";
408-
QTest::newRow("_bytestring5*5") << raw("\x5f\x41H\x41""e\x41l\x41l\x41o\xff") << "h'48656c6c6f'";
409-
QTest::newRow("_textstring5*5") << raw("\x7f\x61H\x61""e\x61l\x61l\x61o\xff") << "\"Hello\"";
410-
QTest::newRow("_bytestring5*6") << raw("\x5f\x41H\x41""e\x40\x41l\x41l\x41o\xff") << "h'48656c6c6f'";
411-
QTest::newRow("_textstring5*6") << raw("\x7f\x61H\x61""e\x61l\x60\x61l\x61o\xff") << "\"Hello\"";
400+
QTest::newRow("_emptybytestring") << raw("\x5f\xff") << "(_ )";
401+
QTest::newRow("_emptytextstring") << raw("\x7f\xff") << "(_ )";
402+
QTest::newRow("_emptybytestring2") << raw("\x5f\x40\xff") << "(_ h'')";
403+
QTest::newRow("_emptytextstring2") << raw("\x7f\x60\xff") << "(_ \"\")";
404+
QTest::newRow("_emptybytestring3") << raw("\x5f\x40\x40\xff") << "(_ h'', h'')";
405+
QTest::newRow("_emptytextstring3") << raw("\x7f\x60\x60\xff") << "(_ \"\", \"\")";
406+
QTest::newRow("_bytestring5*2") << raw("\x5f\x43Hel\x42lo\xff") << "(_ h'48656c', h'6c6f')";
407+
QTest::newRow("_textstring5*2") << raw("\x7f\x63Hel\x62lo\xff") << "(_ \"Hel\", \"lo\")";
408+
QTest::newRow("_bytestring5*5") << raw("\x5f\x41H\x41""e\x41l\x41l\x41o\xff") << "(_ h'48', h'65', h'6c', h'6c', h'6f')";
409+
QTest::newRow("_textstring5*5") << raw("\x7f\x61H\x61""e\x61l\x61l\x61o\xff") << "(_ \"H\", \"e\", \"l\", \"l\", \"o\")";
410+
QTest::newRow("_bytestring5*6") << raw("\x5f\x41H\x41""e\x40\x41l\x41l\x41o\xff") << "(_ h'48', h'65', h'', h'6c', h'6c', h'6f')";
411+
QTest::newRow("_textstring5*6") << raw("\x7f\x61H\x61""e\x61l\x60\x61l\x61o\xff") << "(_ \"H\", \"e\", \"l\", \"\", \"l\", \"o\")";
412412
}
413413

414414
void tst_Parser::strings_data()
@@ -763,7 +763,7 @@ void tst_Parser::mapsAndArrays()
763763

764764
// mixed with indeterminate length strings
765765
compareOneSize(-1, "\xbf\1\x9f" + data + "\xff\x65Hello\xbf" + data + "\x7f\xff\xff\xff",
766-
"{_ 1: [_ " + expected + "], \"Hello\": {_ " + expected + ": \"\"}}");
766+
"{_ 1: [_ " + expected + "], \"Hello\": {_ " + expected + ": (_ )}}");
767767
}
768768

769769
void tst_Parser::chunkedString_data()

0 commit comments

Comments
 (0)