Util: Return -1 if text codec encounters an error
@@ -42,7 +42,7 @@ assert_memory_equal(output, "BB", 2);
len = 0; TextCodecStartDecode(&codec, &iter); - len += TextCodecAdvance(&iter, 0x42, output + len, sizeof(output) - len); + assert_int_equal(TextCodecAdvance(&iter, 0x42, output + len, sizeof(output) - len), -1); len += TextCodecAdvance(&iter, 0x41, output + len, sizeof(output) - len); len += TextCodecFinish(&iter, output + len, sizeof(output) - len); assert_int_equal(len, 1);@@ -51,7 +51,7 @@
len = 0; TextCodecStartDecode(&codec, &iter); len += TextCodecAdvance(&iter, 0x41, output + len, sizeof(output) - len); - len += TextCodecAdvance(&iter, 0x42, output + len, sizeof(output) - len); + assert_int_equal(TextCodecAdvance(&iter, 0x42, output + len, sizeof(output) - len), -1); len += TextCodecFinish(&iter, output + len, sizeof(output) - len); assert_int_equal(len, 1); assert_memory_equal(output, "B", 1);@@ -59,7 +59,7 @@
len = 0; TextCodecStartDecode(&codec, &iter); len += TextCodecAdvance(&iter, 0x41, output + len, sizeof(output) - len); - len += TextCodecAdvance(&iter, 0x42, output + len, sizeof(output) - len); + assert_int_equal(TextCodecAdvance(&iter, 0x42, output + len, sizeof(output) - len), -1); len += TextCodecAdvance(&iter, 0x41, output + len, sizeof(output) - len); len += TextCodecFinish(&iter, output + len, sizeof(output) - len); assert_int_equal(len, 2);@@ -95,7 +95,7 @@ assert_memory_equal(output, "AA", 2);
len = 0; TextCodecStartEncode(&codec, &iter); - len += TextCodecAdvance(&iter, 'A', output + len, sizeof(output) - len); + assert_int_equal(TextCodecAdvance(&iter, 'A', output + len, sizeof(output) - len), -1); len += TextCodecAdvance(&iter, 'B', output + len, sizeof(output) - len); len += TextCodecFinish(&iter, output + len, sizeof(output) - len); assert_int_equal(len, 1);@@ -104,7 +104,7 @@
len = 0; TextCodecStartEncode(&codec, &iter); len += TextCodecAdvance(&iter, 'B', output + len, sizeof(output) - len); - len += TextCodecAdvance(&iter, 'A', output + len, sizeof(output) - len); + assert_int_equal(TextCodecAdvance(&iter, 'A', output + len, sizeof(output) - len), -1); len += TextCodecFinish(&iter, output + len, sizeof(output) - len); assert_int_equal(len, 1); assert_memory_equal(output, "A", 1);@@ -112,7 +112,7 @@
len = 0; TextCodecStartEncode(&codec, &iter); len += TextCodecAdvance(&iter, 'B', output + len, sizeof(output) - len); - len += TextCodecAdvance(&iter, 'A', output + len, sizeof(output) - len); + assert_int_equal(TextCodecAdvance(&iter, 'A', output + len, sizeof(output) - len), -1); len += TextCodecAdvance(&iter, 'B', output + len, sizeof(output) - len); len += TextCodecFinish(&iter, output + len, sizeof(output) - len); assert_int_equal(len, 2);@@ -181,7 +181,7 @@ assert_memory_equal(output, "DB", 2);
len = 0; TextCodecStartDecode(&codec, &iter); - len += TextCodecAdvance(&iter, 0x42, output + len, sizeof(output) - len); + assert_int_equal(TextCodecAdvance(&iter, 0x42, output + len, sizeof(output) - len), -1); len += TextCodecAdvance(&iter, 0x41, output + len, sizeof(output) - len); len += TextCodecFinish(&iter, output + len, sizeof(output) - len); assert_int_equal(len, 1);@@ -189,7 +189,7 @@ assert_memory_equal(output, "B", 1);
len = 0; TextCodecStartDecode(&codec, &iter); - len += TextCodecAdvance(&iter, 0x42, output + len, sizeof(output) - len); + assert_int_equal(TextCodecAdvance(&iter, 0x42, output + len, sizeof(output) - len), -1); len += TextCodecAdvance(&iter, 0x43, output + len, sizeof(output) - len); len += TextCodecFinish(&iter, output + len, sizeof(output) - len); assert_int_equal(len, 1);@@ -198,7 +198,7 @@
len = 0; TextCodecStartDecode(&codec, &iter); len += TextCodecAdvance(&iter, 0x41, output + len, sizeof(output) - len); - len += TextCodecAdvance(&iter, 0x42, output + len, sizeof(output) - len); + assert_int_equal(TextCodecAdvance(&iter, 0x42, output + len, sizeof(output) - len), -1); len += TextCodecFinish(&iter, output + len, sizeof(output) - len); assert_int_equal(len, 1); assert_memory_equal(output, "B", 1);@@ -206,7 +206,7 @@
len = 0; TextCodecStartDecode(&codec, &iter); len += TextCodecAdvance(&iter, 0x43, output + len, sizeof(output) - len); - len += TextCodecAdvance(&iter, 0x42, output + len, sizeof(output) - len); + assert_int_equal(TextCodecAdvance(&iter, 0x42, output + len, sizeof(output) - len), -1); len += TextCodecFinish(&iter, output + len, sizeof(output) - len); assert_int_equal(len, 1); assert_memory_equal(output, "D", 1);@@ -274,7 +274,7 @@ assert_memory_equal(output, "cdAb", 4);
len = 0; TextCodecStartDecode(&codec, &iter); - len += TextCodecAdvance(&iter, 3, output + len, sizeof(output) - len); + assert_int_equal(TextCodecAdvance(&iter, 3, output + len, sizeof(output) - len), -1); len += TextCodecAdvance(&iter, 1, output + len, sizeof(output) - len); len += TextCodecFinish(&iter, output + len, sizeof(output) - len); assert_int_equal(len, 2);@@ -282,7 +282,7 @@ assert_memory_equal(output, "Ab", 2);
len = 0; TextCodecStartDecode(&codec, &iter); - len += TextCodecAdvance(&iter, 3, output + len, sizeof(output) - len); + assert_int_equal(TextCodecAdvance(&iter, 3, output + len, sizeof(output) - len), -1); len += TextCodecAdvance(&iter, 2, output + len, sizeof(output) - len); len += TextCodecFinish(&iter, output + len, sizeof(output) - len); assert_int_equal(len, 2);@@ -291,7 +291,7 @@
len = 0; TextCodecStartDecode(&codec, &iter); len += TextCodecAdvance(&iter, 1, output + len, sizeof(output) - len); - len += TextCodecAdvance(&iter, 3, output + len, sizeof(output) - len); + assert_int_equal(TextCodecAdvance(&iter, 3, output + len, sizeof(output) - len), -1); len += TextCodecFinish(&iter, output + len, sizeof(output) - len); assert_int_equal(len, 2); assert_memory_equal(output, "Ab", 2);@@ -299,7 +299,7 @@
len = 0; TextCodecStartDecode(&codec, &iter); len += TextCodecAdvance(&iter, 2, output + len, sizeof(output) - len); - len += TextCodecAdvance(&iter, 3, output + len, sizeof(output) - len); + assert_int_equal(TextCodecAdvance(&iter, 3, output + len, sizeof(output) - len), -1); len += TextCodecFinish(&iter, output + len, sizeof(output) - len); assert_int_equal(len, 2); assert_memory_equal(output, "cd", 2);@@ -377,7 +377,7 @@ assert_memory_equal(output, "\2\1", 2);
len = 0; TextCodecStartEncode(&codec, &iter); - len += TextCodecAdvance(&iter, 'e', output + len, sizeof(output) - len); + assert_int_equal(TextCodecAdvance(&iter, 'e', output + len, sizeof(output) - len), -1); len += TextCodecAdvance(&iter, 'A', output + len, sizeof(output) - len); len += TextCodecAdvance(&iter, 'b', output + len, sizeof(output) - len); len += TextCodecFinish(&iter, output + len, sizeof(output) - len);@@ -386,7 +386,7 @@ assert_memory_equal(output, "\1", 1);
len = 0; TextCodecStartEncode(&codec, &iter); - len += TextCodecAdvance(&iter, 'e', output + len, sizeof(output) - len); + assert_int_equal(TextCodecAdvance(&iter, 'e', output + len, sizeof(output) - len), -1); len += TextCodecAdvance(&iter, 'c', output + len, sizeof(output) - len); len += TextCodecAdvance(&iter, 'd', output + len, sizeof(output) - len); len += TextCodecFinish(&iter, output + len, sizeof(output) - len);@@ -396,8 +396,8 @@
len = 0; TextCodecStartEncode(&codec, &iter); len += TextCodecAdvance(&iter, 'A', output + len, sizeof(output) - len); - len += TextCodecAdvance(&iter, 'e', output + len, sizeof(output) - len); - len += TextCodecAdvance(&iter, 'b', output + len, sizeof(output) - len); + assert_int_equal(TextCodecAdvance(&iter, 'e', output + len, sizeof(output) - len), -1); + assert_int_equal(TextCodecAdvance(&iter, 'b', output + len, sizeof(output) - len), -1); len += TextCodecFinish(&iter, output + len, sizeof(output) - len); assert_int_equal(len, 0);@@ -405,7 +405,7 @@ len = 0;
TextCodecStartEncode(&codec, &iter); len += TextCodecAdvance(&iter, 'A', output + len, sizeof(output) - len); len += TextCodecAdvance(&iter, 'b', output + len, sizeof(output) - len); - len += TextCodecAdvance(&iter, 'e', output + len, sizeof(output) - len); + assert_int_equal(TextCodecAdvance(&iter, 'e', output + len, sizeof(output) - len), -1); len += TextCodecFinish(&iter, output + len, sizeof(output) - len); assert_int_equal(len, 1); assert_memory_equal(output, "\1", 1);@@ -413,8 +413,8 @@
len = 0; TextCodecStartEncode(&codec, &iter); len += TextCodecAdvance(&iter, 'c', output + len, sizeof(output) - len); - len += TextCodecAdvance(&iter, 'e', output + len, sizeof(output) - len); - len += TextCodecAdvance(&iter, 'd', output + len, sizeof(output) - len); + assert_int_equal(TextCodecAdvance(&iter, 'e', output + len, sizeof(output) - len), -1); + assert_int_equal(TextCodecAdvance(&iter, 'd', output + len, sizeof(output) - len), -1); len += TextCodecFinish(&iter, output + len, sizeof(output) - len); assert_int_equal(len, 0);@@ -422,7 +422,7 @@ len = 0;
TextCodecStartEncode(&codec, &iter); len += TextCodecAdvance(&iter, 'c', output + len, sizeof(output) - len); len += TextCodecAdvance(&iter, 'd', output + len, sizeof(output) - len); - len += TextCodecAdvance(&iter, 'e', output + len, sizeof(output) - len); + assert_int_equal(TextCodecAdvance(&iter, 'e', output + len, sizeof(output) - len), -1); len += TextCodecFinish(&iter, output + len, sizeof(output) - len); assert_int_equal(len, 1); assert_memory_equal(output, "\2", 1);@@ -454,7 +454,7 @@ len = 0;
TextCodecStartDecode(&codec, &iter); len += TextCodecAdvance(&iter, 0xFF, output + len, sizeof(output) - len); len += TextCodecAdvance(&iter, 1, output + len, sizeof(output) - len); - len += TextCodecAdvance(&iter, 1, output + len, sizeof(output) - len); + assert_int_equal(TextCodecAdvance(&iter, 1, output + len, sizeof(output) - len), -1); len += TextCodecFinish(&iter, output + len, sizeof(output) - len); assert_int_equal(len, 2); assert_memory_equal(output, "Ab", 2);@@ -471,7 +471,7 @@ len = 0;
TextCodecStartDecode(&codec, &iter); len += TextCodecAdvance(&iter, 0xFF, output + len, sizeof(output) - len); len += TextCodecAdvance(&iter, 2, output + len, sizeof(output) - len); - len += TextCodecAdvance(&iter, 2, output + len, sizeof(output) - len); + assert_int_equal(TextCodecAdvance(&iter, 2, output + len, sizeof(output) - len), -1); len += TextCodecFinish(&iter, output + len, sizeof(output) - len); assert_int_equal(len, 2); assert_memory_equal(output, "Ac", 2);@@ -518,7 +518,7 @@ assert_memory_equal(output, "AcAb", 4);
len = 0; TextCodecStartDecode(&codec, &iter); - len += TextCodecAdvance(&iter, 3, output + len, sizeof(output) - len); + assert_int_equal(TextCodecAdvance(&iter, 3, output + len, sizeof(output) - len), -1); len += TextCodecAdvance(&iter, 0xFF, output + len, sizeof(output) - len); len += TextCodecAdvance(&iter, 1, output + len, sizeof(output) - len); len += TextCodecFinish(&iter, output + len, sizeof(output) - len);@@ -528,15 +528,15 @@
len = 0; TextCodecStartDecode(&codec, &iter); len += TextCodecAdvance(&iter, 0xFF, output + len, sizeof(output) - len); - len += TextCodecAdvance(&iter, 3, output + len, sizeof(output) - len); - len += TextCodecAdvance(&iter, 1, output + len, sizeof(output) - len); + assert_int_equal(TextCodecAdvance(&iter, 3, output + len, sizeof(output) - len), -1); + assert_int_equal(TextCodecAdvance(&iter, 1, output + len, sizeof(output) - len), -1); len += TextCodecFinish(&iter, output + len, sizeof(output) - len); assert_int_equal(len, 0); len = 0; TextCodecStartDecode(&codec, &iter); len += TextCodecAdvance(&iter, 0xFF, output + len, sizeof(output) - len); - len += TextCodecAdvance(&iter, 3, output + len, sizeof(output) - len); + assert_int_equal(TextCodecAdvance(&iter, 3, output + len, sizeof(output) - len), -1); len += TextCodecAdvance(&iter, 0xFF, output + len, sizeof(output) - len); len += TextCodecAdvance(&iter, 1, output + len, sizeof(output) - len); len += TextCodecFinish(&iter, output + len, sizeof(output) - len);@@ -548,7 +548,7 @@ TextCodecStartDecode(&codec, &iter);
len += TextCodecAdvance(&iter, 0xFF, output + len, sizeof(output) - len); len += TextCodecAdvance(&iter, 1, output + len, sizeof(output) - len); len += TextCodecAdvance(&iter, 0xFF, output + len, sizeof(output) - len); - len += TextCodecAdvance(&iter, 3, output + len, sizeof(output) - len); + assert_int_equal(TextCodecAdvance(&iter, 3, output + len, sizeof(output) - len), -1); len += TextCodecFinish(&iter, output + len, sizeof(output) - len); assert_int_equal(len, 2); assert_memory_equal(output, "Ab", 2);@@ -580,7 +580,7 @@ len = 0;
TextCodecStartEncode(&codec, &iter); len += TextCodecAdvance(&iter, 'A', output + len, sizeof(output) - len); len += TextCodecAdvance(&iter, 'b', output + len, sizeof(output) - len); - len += TextCodecAdvance(&iter, 'b', output + len, sizeof(output) - len); + assert_int_equal(TextCodecAdvance(&iter, 'b', output + len, sizeof(output) - len), -1); len += TextCodecFinish(&iter, output + len, sizeof(output) - len); assert_int_equal(len, 2); assert_memory_equal(output, "\xFF\1", 2);@@ -597,7 +597,7 @@ len = 0;
TextCodecStartEncode(&codec, &iter); len += TextCodecAdvance(&iter, 'A', output + len, sizeof(output) - len); len += TextCodecAdvance(&iter, 'c', output + len, sizeof(output) - len); - len += TextCodecAdvance(&iter, 'c', output + len, sizeof(output) - len); + assert_int_equal(TextCodecAdvance(&iter, 'c', output + len, sizeof(output) - len), -1); len += TextCodecFinish(&iter, output + len, sizeof(output) - len); assert_int_equal(len, 2); assert_memory_equal(output, "\xFF\2", 2);@@ -644,7 +644,7 @@ assert_memory_equal(output, "\xFF\2\xFF\1", 4);
len = 0; TextCodecStartEncode(&codec, &iter); - len += TextCodecAdvance(&iter, 'd', output + len, sizeof(output) - len); + assert_int_equal(TextCodecAdvance(&iter, 'd', output + len, sizeof(output) - len), -1); len += TextCodecAdvance(&iter, 'A', output + len, sizeof(output) - len); len += TextCodecAdvance(&iter, 'b', output + len, sizeof(output) - len); len += TextCodecFinish(&iter, output + len, sizeof(output) - len);@@ -654,15 +654,15 @@
len = 0; TextCodecStartEncode(&codec, &iter); len += TextCodecAdvance(&iter, 'A', output + len, sizeof(output) - len); - len += TextCodecAdvance(&iter, 'd', output + len, sizeof(output) - len); - len += TextCodecAdvance(&iter, 'b', output + len, sizeof(output) - len); + assert_int_equal(TextCodecAdvance(&iter, 'd', output + len, sizeof(output) - len), -1); + assert_int_equal(TextCodecAdvance(&iter, 'b', output + len, sizeof(output) - len), -1); len += TextCodecFinish(&iter, output + len, sizeof(output) - len); assert_int_equal(len, 0); len = 0; TextCodecStartEncode(&codec, &iter); len += TextCodecAdvance(&iter, 'A', output + len, sizeof(output) - len); - len += TextCodecAdvance(&iter, 'd', output + len, sizeof(output) - len); + assert_int_equal(TextCodecAdvance(&iter, 'd', output + len, sizeof(output) - len), -1); len += TextCodecAdvance(&iter, 'A', output + len, sizeof(output) - len); len += TextCodecAdvance(&iter, 'b', output + len, sizeof(output) - len); len += TextCodecFinish(&iter, output + len, sizeof(output) - len);@@ -674,7 +674,7 @@ TextCodecStartEncode(&codec, &iter);
len += TextCodecAdvance(&iter, 'A', output + len, sizeof(output) - len); len += TextCodecAdvance(&iter, 'b', output + len, sizeof(output) - len); len += TextCodecAdvance(&iter, 'A', output + len, sizeof(output) - len); - len += TextCodecAdvance(&iter, 'd', output + len, sizeof(output) - len); + assert_int_equal(TextCodecAdvance(&iter, 'd', output + len, sizeof(output) - len), -1); len += TextCodecFinish(&iter, output + len, sizeof(output) - len); assert_int_equal(len, 2); assert_memory_equal(output, "\xFF\1", 2);
@@ -146,18 +146,18 @@ iter->root = codec->reverseRoot;
iter->current = iter->root; } -static size_t _TextCodecFinishInternal(struct TextCodecNode* node, uint8_t* output, size_t outputLength) { +static ssize_t _TextCodecFinishInternal(struct TextCodecNode* node, uint8_t* output, size_t outputLength) { if (outputLength > node->leafLength) { outputLength = node->leafLength; } if (node->leafLength == 0) { - return 0; + return -1; } memcpy(output, node->leaf, outputLength); return node->leafLength; } -size_t TextCodecAdvance(struct TextCodecIterator* iter, uint8_t byte, uint8_t* output, size_t outputLength) { +ssize_t TextCodecAdvance(struct TextCodecIterator* iter, uint8_t byte, uint8_t* output, size_t outputLength) { struct TextCodecNode* node = TableLookup(&iter->current->children, byte); if (!node) { ssize_t size = _TextCodecFinishInternal(iter->current, output, outputLength);@@ -170,17 +170,28 @@ if (!outputLength) {
return size; } if (iter->current == iter->root) { - return 0; + return -1; + } + iter->current = iter->root; + ssize_t newSize = TextCodecAdvance(iter, byte, output, outputLength); + if (newSize < 0 && size > 0) { + return size; } + return newSize + size; + } + if (TableSize(&node->children) == 0) { iter->current = iter->root; - return TextCodecAdvance(iter, byte, output, outputLength) + size; + return _TextCodecFinishInternal(node, output, outputLength); } iter->current = node; return 0; } -size_t TextCodecFinish(struct TextCodecIterator* iter, uint8_t* output, size_t outputLength) { +ssize_t TextCodecFinish(struct TextCodecIterator* iter, uint8_t* output, size_t outputLength) { struct TextCodecNode* node = iter->current; iter->current = iter->root; + if (node->leafLength == 0) { + return 0; + } return _TextCodecFinishInternal(node, output, outputLength); }
@@ -26,7 +26,7 @@
void TextCodecStartDecode(struct TextCodec*, struct TextCodecIterator*); void TextCodecStartEncode(struct TextCodec*, struct TextCodecIterator*); -size_t TextCodecAdvance(struct TextCodecIterator*, uint8_t byte, uint8_t* output, size_t outputLength); -size_t TextCodecFinish(struct TextCodecIterator*, uint8_t* output, size_t outputLength); +ssize_t TextCodecAdvance(struct TextCodecIterator*, uint8_t byte, uint8_t* output, size_t outputLength); +ssize_t TextCodecFinish(struct TextCodecIterator*, uint8_t* output, size_t outputLength); #endif