コード例 #1
0
ファイル: binascii.cs プロジェクト: weimingtom/IronPythonMod
        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);
        }
コード例 #2
0
ファイル: binascii.cs プロジェクト: profMagija/ironpython3
 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");
         }
     }
 }
コード例 #3
0
ファイル: binascii.cs プロジェクト: weimingtom/IronPythonMod
        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);
        }
コード例 #4
0
ファイル: binascii.cs プロジェクト: profMagija/ironpython3
        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));
        }
コード例 #5
0
ファイル: binascii.cs プロジェクト: profMagija/ironpython3
        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);
        }
コード例 #6
0
ファイル: binascii.cs プロジェクト: profMagija/ironpython3
        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);
                }
            }
        }
コード例 #7
0
ファイル: binascii.cs プロジェクト: profMagija/ironpython3
        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);
        }
コード例 #8
0
ファイル: binascii.cs プロジェクト: profMagija/ironpython3
        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);
        }
コード例 #9
0
ファイル: binascii.cs プロジェクト: jcteague/ironruby
        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;
        }
コード例 #10
0
ファイル: binascii.cs プロジェクト: jcteague/ironruby
        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;
        }
コード例 #11
0
ファイル: binascii.cs プロジェクト: joshholmes/ironruby
 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");
         }
     }
 }
コード例 #12
0
ファイル: binascii.cs プロジェクト: joshholmes/ironruby
 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);
 }
コード例 #13
0
ファイル: binascii.cs プロジェクト: joshholmes/ironruby
        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;
        }
コード例 #14
0
ファイル: binascii.cs プロジェクト: joshholmes/ironruby
 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;
         }
     }
 }
コード例 #15
0
ファイル: binascii.cs プロジェクト: joshholmes/ironruby
        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;
        }
コード例 #16
0
ファイル: binascii.cs プロジェクト: joshholmes/ironruby
        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;
        }