Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Escaping of control sequences during serialization #66

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 40 additions & 24 deletions src/SimpleJson/SimpleJson.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// <copyright file="SimpleJson.cs" company="The Outercurve Foundation">
// Copyright (c) 2011, The Outercurve Foundation.
//
Expand Down Expand Up @@ -515,21 +515,12 @@ static class SimpleJson
private const int TOKEN_NULL = 11;
private const int BUILDER_CAPACITY = 2000;

private static readonly char[] EscapeTable;
private static readonly char[] EscapeCharacters = new char[] { '"', '\\', '\b', '\f', '\n', '\r', '\t' };
private static readonly string EscapeCharactersString = new string(EscapeCharacters);

static SimpleJson()
{
EscapeTable = new char[93];
EscapeTable['"'] = '"';
EscapeTable['\\'] = '\\';
EscapeTable['\b'] = 'b';
EscapeTable['\f'] = 'f';
EscapeTable['\n'] = 'n';
EscapeTable['\r'] = 'r';
EscapeTable['\t'] = 't';
}
private static readonly char[] EscapeCharacters = new char[] { '"', '\\',
'\x00', '\x01', '\x02', '\x03', '\x04', '\x05', '\x06', '\x07',
'\x08', '\x09', '\x0a', '\x0b', '\x0c', '\x0d', '\x0e', '\x0f',
'\x10', '\x11', '\x12', '\x13', '\x14', '\x15', '\x16', '\x17',
'\x18', '\x19', '\x1a', '\x1b', '\x1c', '\x1d', '\x1e', '\x1f'
};

/// <summary>
/// Parses the string json into a value
Expand Down Expand Up @@ -1111,13 +1102,8 @@ static bool SerializeString(string aString, StringBuilder builder)
char c = charArray[i];

// Non ascii characters are fine, buffer them up and send them to the builder
// in larger chunks if possible. The escape table is a 1:1 translation table
// with \0 [default(char)] denoting a safe character.
if (c >= EscapeTable.Length || EscapeTable[c] == default(char))
{
safeCharacterCount++;
}
else
// in larger chunks if possible.
if (Char.IsControl(c) || c == '\"' || c == '\\')
{
if (safeCharacterCount > 0)
{
Expand All @@ -1126,7 +1112,37 @@ static bool SerializeString(string aString, StringBuilder builder)
}

builder.Append('\\');
builder.Append(EscapeTable[c]);
switch (c)
{
case '\\':
builder.Append('\\');
break;
case '\"':
builder.Append('\"');
break;
case '\b':
builder.Append('b');
break;
case '\f':
builder.Append('f');
break;
case '\r':
builder.Append('r');
break;
case '\t':
builder.Append('t');
break;
case '\n':
builder.Append('n');
break;
default:
builder.AppendFormat("u{0:X4}", (int)c);
break;
}
}
else
{
safeCharacterCount++;
}
}

Expand Down