Example #1
0
		//
		// Packs one datum `oarg' into the buffer `b', using the string format
		// in `description' at position `i'
		//
		// Returns: true if we must pick the next object from the list
		//
		static bool PackOne (PackContext b, object oarg)
		{
			int n;
			
			switch (b.description [b.i]){
			case '^':
				b.conv = BigEndian;
				return false;
			case '_':
				b.conv = LittleEndian;
				return false;
			case '%':
				b.conv = Native;
				return false;

			case '!':
				b.align = -1;
				return false;
				
			case 'x':
				b.Add (new byte [] { 0 });
				return false;
				
				// Type Conversions
			case 'i':
				b.Add (b.conv.GetBytes (Convert.ToInt32 (oarg)));
				break;
				
			case 'I':
				b.Add (b.conv.GetBytes (Convert.ToUInt32 (oarg)));
				break;
				
			case 's':
				b.Add (b.conv.GetBytes (Convert.ToInt16 (oarg)));
				break;
				
			case 'S':
				b.Add (b.conv.GetBytes (Convert.ToUInt16 (oarg)));
				break;
				
			case 'l':
				b.Add (b.conv.GetBytes (Convert.ToInt64 (oarg)));
				break;
				
			case 'L':
				b.Add (b.conv.GetBytes (Convert.ToUInt64 (oarg)));
				break;
				
			case 'f':
				b.Add (b.conv.GetBytes (Convert.ToSingle (oarg)));
				break;
				
			case 'd':
				b.Add (b.conv.GetBytes (Convert.ToDouble (oarg)));
				break;
				
			case 'b':
				b.Add (new byte [] { Convert.ToByte (oarg) });
				break;

			case 'c':
				b.Add (new byte [] { (byte) (Convert.ToSByte (oarg)) });
				break;

			case 'C':
				b.Add (new byte [] { Convert.ToByte (oarg) });
				break;

				// Repeat acount;
			case '1': case '2': case '3': case '4': case '5':
			case '6': case '7': case '8': case '9':
				b.repeat = ((short) b.description [b.i]) - ((short) '0');
				return false;

			case '*':
				b.repeat = Int32.MaxValue;
				return false;
				
			case '[':
				int count = -1, j;
				
				for (j = b.i+1; j < b.description.Length; j++){
					if (b.description [j] == ']')
						break;
					n = ((short) b.description [j]) - ((short) '0');
					if (n >= 0 && n <= 9){
						if (count == -1)
							count = n;
						else
							count = count * 10 + n;
					}
				}
				if (count == -1)
					throw new ArgumentException ("invalid size specification");
				b.i = j;
				b.repeat = count;
				return false;
				
			case '$': case 'z':
				bool add_null = b.description [b.i] == 'z';
				b.i++;
				if (b.i >= b.description.Length)
					throw new ArgumentException ("$ description needs a type specified", "description");
				char d = b.description [b.i];
				Encoding e;
				
				switch (d){
				case '8':
					e = Encoding.UTF8;
					n = 1;
					break;
				case '6':
					e = Encoding.Unicode;
					n = 2;
					break;
				case '7':
#if PCL
					e = Encoding.GetEncoding ("utf-7");
#else
					e = Encoding.UTF7;
#endif
					n = 1;
					break;
				case 'b':
					e = Encoding.BigEndianUnicode;
					n = 2;
					break;
				case '3':
#if PCL
					e = Encoding.GetEncoding ("utf-32");
#else
					e = Encoding.GetEncoding (12000);
#endif
					n = 4;
					break;
				case '4':
#if PCL
					e = Encoding.GetEncoding ("utf-32BE");
#else
					e = Encoding.GetEncoding (12001);
#endif
					n = 4;
					break;
					
				default:
					throw new ArgumentException ("Invalid format for $ specifier", "description");
				}
				if (b.align == -1)
					b.align = 4;
				b.Add (e.GetBytes (Convert.ToString (oarg)));
				if (add_null)
					b.Add (new byte [n]);
				break;
			default:
				throw new ArgumentException (String.Format ("invalid format specified `{0}'",
									    b.description [b.i]));
			}
			return true;
		}
Example #2
0
		//
		// Packs one datum `oarg' into the buffer `b', using the string format
		// in `description' at position `i'
		//
		// Returns: true if we must pick the next object from the list
		//
		static bool PackOne (PackContext b, object oarg)
		{
			int n;
			
			switch (b.description [b.i]){
			case '^':
				b.conv = BigEndian;
				return false;
			case '_':
				b.conv = LittleEndian;
				return false;
			case '%':
				b.conv = Native;
				return false;

			case '!':
				b.align = -1;
				return false;
				
			case 'x':
				b.Add (new byte [] { 0 });
				return false;
				
				// Type Conversions
			case 'i':
				b.Add (b.conv.GetBytes (Convert.ToInt32 (oarg)));
				break;
				
			case 'I':
				b.Add (b.conv.GetBytes (Convert.ToUInt32 (oarg)));
				break;
				
			case 's':
				b.Add (b.conv.GetBytes (Convert.ToInt16 (oarg)));
				break;
				
			case 'S':
				b.Add (b.conv.GetBytes (Convert.ToUInt16 (oarg)));
				break;
				
			case 'l':
				b.Add (b.conv.GetBytes (Convert.ToInt64 (oarg)));
				break;
				
			case 'L':
				b.Add (b.conv.GetBytes (Convert.ToUInt64 (oarg)));
				break;
				
			case 'f':
				b.Add (b.conv.GetBytes (Convert.ToSingle (oarg)));
				break;
				
			case 'd':
				b.Add (b.conv.GetBytes (Convert.ToDouble (oarg)));
				break;
				
			case 'b':
				b.Add (new byte [] { Convert.ToByte (oarg) });
				break;

			case 'c':
				b.Add (new byte [] { (byte) (Convert.ToSByte (oarg)) });
				break;

			case 'C':
				b.Add (new byte [] { Convert.ToByte (oarg) });
				break;

				// Repeat acount;
			case '1': case '2': case '3': case '4': case '5':
			case '6': case '7': case '8': case '9':
				b.repeat = ((short) b.description [b.i]) - ((short) '0');
				return false;

			case '*':
				b.repeat = Int32.MaxValue;
				return false;
				
			case '[':
				int count = -1, j;
				
				for (j = b.i+1; j < b.description.Length; j++){
					if (b.description [j] == ']')
						break;
					n = ((short) b.description [j]) - ((short) '0');
					if (n >= 0 && n <= 9){
						if (count == -1)
							count = n;
						else
							count = count * 10 + n;
					}
				}
				if (count == -1)
					throw new ArgumentException ("invalid size specification");
				b.i = j;
				b.repeat = count;
				return false;
				
			case '$': case 'z':
				bool add_null = b.description [b.i] == 'z';
				b.i++;
				if (b.i >= b.description.Length)
					throw new ArgumentException ("$ description needs a type specified", "description");
				char d = b.description [b.i];
				Encoding e;
				
				switch (d){
				case '8':
					e = Encoding.UTF8;
					n = 1;
					break;
				case '6':
					e = Encoding.Unicode;
					n = 2;
					break;
				case '7':
					e = Encoding.UTF7;
					n = 1;
					break;
				case 'b':
					e = Encoding.BigEndianUnicode;
					n = 2;
					break;
				case '3':
					e = Encoding.GetEncoding (12000);
					n = 4;
					break;
				case '4':
					e = Encoding.GetEncoding (12001);
					n = 4;
					break;
					
				default:
					throw new ArgumentException ("Invalid format for $ specifier", "description");
				}
				if (b.align == -1)
					b.align = 4;
				b.Add (e.GetBytes (Convert.ToString (oarg)));
				if (add_null)
					b.Add (new byte [n]);
				break;
			default:
				throw new ArgumentException (String.Format ("invalid format specified `{0}'",
									    b.description [b.i]));
			}
			return true;
		}
Example #3
0
        //
        // Packs one datum `oarg' into the buffer `b', using the string format
        // in `description' at position `i'
        //
        // Returns: true if we must pick the next object from the list
        //
        private static bool PackOne(PackContext b, object oarg)
        {
            int n;

            switch (b.description[b.i])
            {
            case '^':
                b.conv = BigEndian;
                return(false);

            case '_':
                b.conv = LittleEndian;
                return(false);

            case '%':
                b.conv = Native;
                return(false);

            case '!':
                return(false);

            case 'x':
                b.Add(new byte[] { 0 });
                return(false);

            // Type Conversions
            case 'i':
                b.Add(b.conv.GetBytes(Convert.ToInt32(oarg)));
                break;

            case 'I':
                b.Add(b.conv.GetBytes(Convert.ToUInt32(oarg)));
                break;

            case 's':
                b.Add(b.conv.GetBytes(Convert.ToInt16(oarg)));
                break;

            case 'S':
                b.Add(b.conv.GetBytes(Convert.ToUInt16(oarg)));
                break;

            case 'l':
                b.Add(b.conv.GetBytes(Convert.ToInt64(oarg)));
                break;

            case 'L':
                b.Add(b.conv.GetBytes(Convert.ToUInt64(oarg)));
                break;

            case 'b':
                b.Add(new[] { Convert.ToByte(oarg) });
                break;

            case 'c':
                b.Add(new[] { (byte)(Convert.ToSByte(oarg)) });
                break;

            case 'C':
                b.Add(new[] { Convert.ToByte(oarg) });
                break;

            case 'A':
                b.Add(b.conv.GetBytes((byte[])oarg));
                break;

            // Repeat acount;
            case '1':
            case '2':
            case '3':
            case '4':
            case '5':
            case '6':
            case '7':
            case '8':
            case '9':
                b.repeat = ((short)b.description[b.i]) - ((short)'0');
                return(false);

            case '*':
                b.repeat = Int32.MaxValue;
                return(false);

            case '[':
                int count = -1, j;

                for (j = b.i + 1; j < b.description.Length; j++)
                {
                    if (b.description[j] == ']')
                    {
                        break;
                    }
                    n = ((short)b.description[j]) - ((short)'0');
                    if (n >= 0 && n <= 9)
                    {
                        if (count == -1)
                        {
                            count = n;
                        }
                        else
                        {
                            count = count * 10 + n;
                        }
                    }
                }
                if (count == -1)
                {
                    throw new ArgumentException("invalid size specification");
                }
                b.i      = j;
                b.repeat = count;
                return(false);

            case '$':
            case 'z':
                var add_null = b.description[b.i] == 'z';
                b.i++;
                if (b.i >= b.description.Length)
                {
                    throw new ArgumentException("$ description needs a type specified", "description");
                }
                char d = b.description[b.i];
                System.Text.Encoding e;

                switch (d)
                {
                case '8':
                    e = System.Text.Encoding.UTF8;
                    n = 1;
                    break;

                case '6':
                    e = System.Text.Encoding.Unicode;
                    n = 2;
                    break;

                case '7':
                    e = System.Text.Encoding.UTF7;
                    n = 1;
                    break;

                case 'b':
                    e = System.Text.Encoding.BigEndianUnicode;
                    n = 2;
                    break;

                case '3':
                    e = System.Text.Encoding.GetEncoding(12000);
                    n = 4;
                    break;

                case '4':
                    e = System.Text.Encoding.GetEncoding(12001);
                    n = 4;
                    break;

                default:
                    throw new ArgumentException("Invalid format for $ specifier", "description");
                }
                b.Add(e.GetBytes(Convert.ToString(oarg)));
                if (add_null)
                {
                    b.Add(new byte[n]);
                }
                break;

            default:
                throw new ArgumentException($"invalid format specified '{b.description[b.i]}'", "description");
            }
            return(true);
        }