private static int GetVal(DecodeByte decFunc, char empty, string data, ref int index) { int res = NoMoreData; do { if (index >= data.Length) { break; } char curChar = data[index++]; if (curChar == empty) { return(EmptyChar); } res = decFunc(curChar); } while (res == NoMoreData); if (res < 0 && empty != Char.MinValue) { throw Ops.TypeError("Incorrect padding"); } return(res); }
private static void ProcessSuffix(CodeContext /*!*/ context, string data, DecodeByte decFunc) { for (int i = 0; i < data.Length; i++) { int current = decFunc(data[i]); if (current >= 0 || current == InvalidByte) { throw Error(context, "Trailing garbage"); } } }
private static StringBuilder DecodeWorker(string data, char empty, DecodeByte decFunc) { StringBuilder res = new StringBuilder(); int i = 0; while (i < data.Length) { int intVal; int val1 = GetVal(decFunc, empty, data, ref i); if (val1 < 0) { break; // no more bytes... } int val2 = GetVal(decFunc, empty, data, ref i); if (val1 < 0) { break; // no more bytes... } int val3 = GetVal(decFunc, empty, data, ref i); if (val3 < 0) { // 2 byte partial intVal = (val1 << 18) | (val2 << 12); res.Append((char)((intVal >> 16) & 0xff)); break; } int val4 = GetVal(decFunc, empty, data, ref i); if (val4 < 0) { // 3 byte partial intVal = (val1 << 18) | (val2 << 12) | (val3 << 6); res.Append((char)((intVal >> 16) & 0xff)); res.Append((char)((intVal >> 8) & 0xff)); break; } // full 4-bytes intVal = (val1 << 18) | (val2 << 12) | (val3 << 6) | (val4); res.Append((char)((intVal >> 16) & 0xff)); res.Append((char)((intVal >> 8) & 0xff)); res.Append((char)(intVal & 0xff)); } return(res); }
private static string RemovePrefix(CodeContext /*!*/ context, string data, DecodeByte decFunc) { int count = 0; while (count < data.Length) { int current = decFunc(data[count]); if (current == InvalidByte) { throw Error(context, "Illegal char"); } if (current >= 0) { break; } count++; } return(count == 0 ? data : data.Substring(count)); }
private static StringBuilder DecodeWorker(CodeContext /*!*/ context, string data, bool bounded, DecodeByte decFunc) { StringBuilder res = new StringBuilder(); int i = 0; while (i < data.Length) { int intVal; int val0 = GetVal(context, data, 0, bounded, ref i, decFunc); if (val0 < 0) { break; // no more bytes... } int val1 = GetVal(context, data, 1, bounded, ref i, decFunc); if (val1 < 0) { break; // no more bytes... } int val2 = GetVal(context, data, 2, bounded, ref i, decFunc); if (val2 < 0) { // 2 byte partial intVal = (val0 << 18) | (val1 << 12); res.Append((char)((intVal >> 16) & 0xff)); break; } int val3 = GetVal(context, data, 3, bounded, ref i, decFunc); if (val3 < 0) { // 3 byte partial intVal = (val0 << 18) | (val1 << 12) | (val2 << 6); res.Append((char)((intVal >> 16) & 0xff)); res.Append((char)((intVal >> 8) & 0xff)); break; } // full 4-bytes intVal = (val0 << 18) | (val1 << 12) | (val2 << 6) | (val3); res.Append((char)((intVal >> 16) & 0xff)); res.Append((char)((intVal >> 8) & 0xff)); res.Append((char)(intVal & 0xff)); } return(res); }
private static int GetVal(CodeContext /*!*/ context, string data, int align, bool bounded, ref int index, DecodeByte decFunc) { int res; while (true) { res = NextVal(context, data, ref index, decFunc); switch (res) { case PadByte: switch (align) { case 0: case 1: CountPadBytes(context, data, -1, ref index, decFunc); continue; case 2: if (CountPadBytes(context, data, 1, ref index, decFunc) > 0) { return(NoMoreBytes); } else { continue; } default: return(NoMoreBytes); } case NoMoreBytes: if (bounded || align == 0) { return(NoMoreBytes); } else { throw Error(context, "Incorrect padding"); } case EmptyByte: return(0); default: return(res); } } }
private static int CountPadBytes(CodeContext /*!*/ context, string data, int bound, ref int index, DecodeByte decFunc) { int res = PadByte; int count = 0; while ((bound < 0 || count < bound) && (res = NextVal(context, data, ref index, decFunc)) == PadByte) { count++; } // we only want NextVal() to eat PadBytes - not real data if (res != PadByte && res != NoMoreBytes) { index--; } return(count); }
private const int NoMoreBytes = -5; // signals end of stream private static int NextVal(CodeContext /*!*/ context, string data, ref int index, DecodeByte decFunc) { int res; while (index < data.Length) { res = decFunc(data[index++]); switch (res) { case EmptyByte: return(0); case InvalidByte: throw Error(context, "Illegal char"); case IgnoreByte: break; default: return(res); } } return(NoMoreBytes); }
private static StringBuilder DecodeWorker(string data, char empty, DecodeByte decFunc) { StringBuilder res = new StringBuilder(); int i = 0; while (i < data.Length) { int intVal; int val1 = GetVal(decFunc, empty, data, ref i); if (val1 < 0) break; // no more bytes... int val2 = GetVal(decFunc, empty, data, ref i); if (val1 < 0) break; // no more bytes... int val3 = GetVal(decFunc, empty, data, ref i); if (val3 < 0) { // 2 byte partial intVal = (val1 << 18) | (val2 << 12); res.Append((char)((intVal >> 16) & 0xff)); break; } int val4 = GetVal(decFunc, empty, data, ref i); if (val4 < 0) { // 3 byte partial intVal = (val1 << 18) | (val2 << 12) | (val3 << 6); res.Append((char)((intVal >> 16) & 0xff)); res.Append((char)((intVal >> 8) & 0xff)); break; } // full 4-bytes intVal = (val1 << 18) | (val2 << 12) | (val3 << 6) | (val4); res.Append((char)((intVal >> 16) & 0xff)); res.Append((char)((intVal >> 8) & 0xff)); res.Append((char)(intVal & 0xff)); } return res; }
private static int GetVal(DecodeByte decFunc, char empty, string data, ref int index) { int res = NoMoreData; do { if (index >= data.Length) break; char curChar = data[index++]; if (curChar == empty) return EmptyChar; res = decFunc(curChar); } while (res == NoMoreData); if (res < 0 && empty != Char.MinValue) throw PythonOps.TypeError("Incorrect padding"); return res; }
private static void ProcessSuffix(CodeContext/*!*/ context, string data, DecodeByte decFunc) { for (int i = 0; i < data.Length; i++) { int current = decFunc(data[i]); if (current >= 0 || current == InvalidByte) { throw Error(context, "Trailing garbage"); } } }
private static string RemovePrefix(CodeContext/*!*/ context, string data, DecodeByte decFunc) { int count = 0; while (count < data.Length) { int current = decFunc(data[count]); if (current == InvalidByte) { throw Error(context, "Illegal char"); } if (current >= 0) break; count++; } return count == 0 ? data : data.Substring(count); }
private static StringBuilder DecodeWorker(CodeContext/*!*/ context, string data, bool bounded, DecodeByte decFunc) { StringBuilder res = new StringBuilder(); int i = 0; while (i < data.Length) { int intVal; int val0 = GetVal(context, data, 0, bounded, ref i, decFunc); if (val0 < 0) break; // no more bytes... int val1 = GetVal(context, data, 1, bounded, ref i, decFunc); if (val1 < 0) break; // no more bytes... int val2 = GetVal(context, data, 2, bounded, ref i, decFunc); if (val2 < 0) { // 2 byte partial intVal = (val0 << 18) | (val1 << 12); res.Append((char)((intVal >> 16) & 0xff)); break; } int val3 = GetVal(context, data, 3, bounded, ref i, decFunc); if (val3 < 0) { // 3 byte partial intVal = (val0 << 18) | (val1 << 12) | (val2 << 6); res.Append((char)((intVal >> 16) & 0xff)); res.Append((char)((intVal >> 8) & 0xff)); break; } // full 4-bytes intVal = (val0 << 18) | (val1 << 12) | (val2 << 6) | (val3); res.Append((char)((intVal >> 16) & 0xff)); res.Append((char)((intVal >> 8) & 0xff)); res.Append((char)(intVal & 0xff)); } return res; }
private static int GetVal(CodeContext/*!*/ context, string data, int align, bool bounded, ref int index, DecodeByte decFunc) { int res; while (true) { res = NextVal(context, data, ref index, decFunc); switch (res) { case PadByte: switch (align) { case 0: case 1: CountPadBytes(context, data, -1, ref index, decFunc); continue; case 2: if (CountPadBytes(context, data, 1, ref index, decFunc) > 0) { return NoMoreBytes; } else { continue; } default: return NoMoreBytes; } case NoMoreBytes: if (bounded || align == 0) { return NoMoreBytes; } else { throw Error(context, "Incorrect padding"); } case EmptyByte: return 0; default: return res; } } }
private static int CountPadBytes(CodeContext/*!*/ context, string data, int bound, ref int index, DecodeByte decFunc) { int res = PadByte; int count = 0; while ((bound < 0 || count < bound) && (res = NextVal(context, data, ref index, decFunc)) == PadByte) { count++; } // we only want NextVal() to eat PadBytes - not real data if (res != PadByte && res != NoMoreBytes) index--; return count; }
private const int NoMoreBytes = -5; // signals end of stream private static int NextVal(CodeContext/*!*/ context, string data, ref int index, DecodeByte decFunc) { int res; while (index < data.Length) { res = decFunc(data[index++]); switch (res) { case EmptyByte: return 0; case InvalidByte: throw Error(context, "Illegal char"); case IgnoreByte: break; default: return res; } } return NoMoreBytes; }