示例#1
0
 internal static object[] Base64ToArgsConverter(string base64)
 {
     if (string.IsNullOrEmpty(base64))
     {
         throw PSTraceSource.NewArgumentNullException("base64");
     }
     string s = new string(Encoding.Unicode.GetChars(Convert.FromBase64String(base64)));
     Deserializer deserializer = new Deserializer(XmlReader.Create(new StringReader(s), InternalDeserializer.XmlReaderSettingsForCliXml));
     object obj2 = deserializer.Deserialize();
     if (!deserializer.Done())
     {
         throw PSTraceSource.NewArgumentException("-args");
     }
     PSObject obj3 = obj2 as PSObject;
     if (obj3 == null)
     {
         throw PSTraceSource.NewArgumentException("-args");
     }
     ArrayList baseObject = obj3.BaseObject as ArrayList;
     if (baseObject == null)
     {
         throw PSTraceSource.NewArgumentException("-args");
     }
     return baseObject.ToArray();
 }
示例#2
0
		internal WrappedDeserializer(Serialization.DataFormat dataFormat, string streamName, TextReader input) : base(dataFormat, streamName)
		{
			if (dataFormat != Serialization.DataFormat.None)
			{
				this.textReader = input;
				this.firstLine = this.textReader.ReadLine();
				if (string.Compare(this.firstLine, Serialization.XmlCliTag, StringComparison.OrdinalIgnoreCase) == 0)
				{
					dataFormat = Serialization.DataFormat.XML;
				}
				Serialization.DataFormat dataFormat1 = this.format;
				switch (dataFormat1)
				{
					case Serialization.DataFormat.Text:
					{
						return;
					}
					case Serialization.DataFormat.XML:
					{
						this.xmlReader = XmlReader.Create(this.textReader);
						this.xmlDeserializer = new Deserializer(this.xmlReader);
						return;
					}
					default:
					{
						return;
					}
				}
			}
			else
			{
				return;
			}
		}
示例#3
0
 public static object[] DeserializeAsList(string source)
 {
     ArrayList list = new ArrayList();
     TextReader input = new StringReader(source);
     Deserializer deserializer = new Deserializer(XmlReader.Create(input, InternalDeserializer.XmlReaderSettingsForCliXml));
     while (!deserializer.Done())
     {
         object obj2 = deserializer.Deserialize();
         list.Add(obj2);
     }
     return list.ToArray();
 }
示例#4
0
        WrappedDeserializer(DataFormat dataFormat, string streamName, TextReader input)
            :
            base(dataFormat, streamName)
        {
            Dbg.Assert(input != null, "input should have a value");

            // If the data format is none - do nothing...
            if (dataFormat == DataFormat.None)
                return;

            textReader = input;
            _firstLine = textReader.ReadLine();
            if (String.Compare(_firstLine, Serialization.XmlCliTag, StringComparison.OrdinalIgnoreCase) == 0)
            {
                // format should be XML

                dataFormat = DataFormat.XML;
            }

            switch (format)
            {
                case DataFormat.XML:
                    _xmlReader = XmlReader.Create(textReader);
                    _xmlDeserializer = new Deserializer(_xmlReader);
                    break;
                case DataFormat.Text:
                default:
                    // do nothing; we'll just read from the TextReader

                    break;
            }
        }
示例#5
0
        private void ReadXml()
        {
            try
            {
                XmlReader xmlReader = XmlReader.Create(_streamReader, InternalDeserializer.XmlReaderSettingsForCliXml);
                Deserializer des = new Deserializer(xmlReader);
                while (!des.Done())
                {
                    string streamName;
                    object obj = des.Deserialize(out streamName);

                    //Decide the stream to which data belongs
                    MinishellStream stream = MinishellStream.Unknown;
                    if (streamName != null)
                    {
                        stream = StringToMinishellStreamConverter.ToMinishellStream(streamName);
                    }
                    if (stream == MinishellStream.Unknown)
                    {
                        stream = _isOutput ? MinishellStream.Output : MinishellStream.Error;
                    }

                    //Null is allowed only in output stream
                    if (stream != MinishellStream.Output && obj == null)
                    {
                        continue;
                    }

                    if (stream == MinishellStream.Error)
                    {
                        if (obj is PSObject)
                        {
                            obj = ErrorRecord.FromPSObjectForRemoting(PSObject.AsPSObject(obj));
                        }
                        else
                        {
                            string errorMessage = null;
                            try
                            {
                                errorMessage = (string)LanguagePrimitives.ConvertTo(obj, typeof(string), CultureInfo.InvariantCulture);
                            }
                            catch (PSInvalidCastException)
                            {
                                continue;
                            }
                            obj = new ErrorRecord(new RemoteException(errorMessage),
                                                "NativeCommandError", ErrorCategory.NotSpecified, errorMessage);
                        }
                    }
                    else if (stream == MinishellStream.Information)
                    {
                        if (obj is PSObject)
                        {
                            obj = InformationRecord.FromPSObjectForRemoting(PSObject.AsPSObject(obj));
                        }
                        else
                        {
                            string messageData = null;
                            try
                            {
                                messageData = (string)LanguagePrimitives.ConvertTo(obj, typeof(string), CultureInfo.InvariantCulture);
                            }
                            catch (PSInvalidCastException)
                            {
                                continue;
                            }

                            obj = new InformationRecord(messageData, null);
                        }
                    }
                    else if (stream == MinishellStream.Debug ||
                             stream == MinishellStream.Verbose ||
                             stream == MinishellStream.Warning)
                    {
                        //Convert to string
                        try
                        {
                            obj = LanguagePrimitives.ConvertTo(obj, typeof(string), CultureInfo.InvariantCulture);
                        }
                        catch (PSInvalidCastException)
                        {
                            continue;
                        }
                    }
                    AddObjectToWriter(obj, stream);
                }
            }
            catch (XmlException originalException)
            {
                string template = NativeCP.CliXmlError;
                string message = string.Format(
                    null,
                    template,
                    _isOutput ? MinishellStream.Output : MinishellStream.Error,
                    _processPath,
                    originalException.Message);
                XmlException newException = new XmlException(
                    message,
                    originalException);

                ErrorRecord error = new ErrorRecord(
                    newException,
                    "ProcessStreamReader_CliXmlError",
                    ErrorCategory.SyntaxError,
                    _processPath);
                AddObjectToWriter(error, MinishellStream.Error);
            }
        }
示例#6
0
 private void ReadXml()
 {
     try
     {
         Deserializer deserializer = new Deserializer(XmlReader.Create(this.streamReader, InternalDeserializer.XmlReaderSettingsForCliXml));
         while (!deserializer.Done())
         {
             string str;
             object obj2 = deserializer.Deserialize(out str);
             MinishellStream unknown = MinishellStream.Unknown;
             if (str != null)
             {
                 unknown = StringToMinishellStreamConverter.ToMinishellStream(str);
             }
             if (unknown == MinishellStream.Unknown)
             {
                 unknown = this.isOutput ? MinishellStream.Output : MinishellStream.Error;
             }
             if ((unknown == MinishellStream.Output) || (obj2 != null))
             {
                 if (unknown == MinishellStream.Error)
                 {
                     if (obj2 is PSObject)
                     {
                         obj2 = ErrorRecord.FromPSObjectForRemoting(PSObject.AsPSObject(obj2));
                     }
                     else
                     {
                         string targetObject = null;
                         try
                         {
                             targetObject = (string) LanguagePrimitives.ConvertTo(obj2, typeof(string), CultureInfo.InvariantCulture);
                         }
                         catch (PSInvalidCastException)
                         {
                             continue;
                         }
                         obj2 = new ErrorRecord(new RemoteException(targetObject), "NativeCommandError", ErrorCategory.NotSpecified, targetObject);
                     }
                 }
                 else if (((unknown == MinishellStream.Debug) || (unknown == MinishellStream.Verbose)) || (unknown == MinishellStream.Warning))
                 {
                     try
                     {
                         obj2 = LanguagePrimitives.ConvertTo(obj2, typeof(string), CultureInfo.InvariantCulture);
                     }
                     catch (PSInvalidCastException)
                     {
                         continue;
                     }
                 }
                 this.AddObjectToWriter(obj2, unknown);
             }
         }
     }
     catch (XmlException exception)
     {
         string cliXmlError = NativeCP.CliXmlError;
         XmlException exception2 = new XmlException(string.Format(null, cliXmlError, new object[] { this.isOutput ? MinishellStream.Output : MinishellStream.Error, this.processPath, exception.Message }), exception);
         ErrorRecord data = new ErrorRecord(exception2, "ProcessStreamReader_CliXmlError", ErrorCategory.SyntaxError, this.processPath);
         this.AddObjectToWriter(data, MinishellStream.Error);
     }
 }
示例#7
0
        internal void Import()
        {
            CreateFileStream();
            _deserializer = new Deserializer(_xr);
            // If total count has been requested, return a dummy object with zero confidence
            if (_cmdlet.PagingParameters.IncludeTotalCount)
            {
                PSObject totalCount = _cmdlet.PagingParameters.NewTotalCount(0, 0);
                _cmdlet.WriteObject(totalCount);
            }


            ulong skip = _cmdlet.PagingParameters.Skip;
            ulong first = _cmdlet.PagingParameters.First;

            // if paging is not specified then keep the old V2 behavior
            if (skip == 0 && first == ulong.MaxValue)
            {
                ulong item = 0;
                while (!_deserializer.Done())
                {
                    object result = _deserializer.Deserialize();
                    if (item++ < skip)
                        continue;
                    if (first == 0)
                        break;

                    _cmdlet.WriteObject(result);
                    first--;
                }
            }
            // else try to flatten the output if possible
            else
            {
                ulong skipped = 0;
                ulong count = 0;
                while (!_deserializer.Done() && count < first)
                {
                    object result = _deserializer.Deserialize();
                    PSObject psObject = result as PSObject;

                    if (psObject == null && skipped++ >= skip)
                    {
                        count++;
                        _cmdlet.WriteObject(result);
                        continue;
                    }

                    ICollection c = psObject.BaseObject as ICollection;
                    if (c != null)
                    {
                        foreach (object o in c)
                        {
                            if (count >= first)
                                break;

                            if (skipped++ >= skip)
                            {
                                count++;
                                _cmdlet.WriteObject(o);
                            }
                        }
                    }
                    else
                    {
                        if (skipped++ >= skip)
                        {
                            count++;
                            _cmdlet.WriteObject(result);
                        }
                    }
                }
            }
        }
示例#8
0
 internal void Import()
 {
     this.CreateFileStream();
     this._deserializer = new Deserializer(this._xr);
     if (this._cmdlet.PagingParameters.IncludeTotalCount != 0)
     {
         PSObject sendToPipeline = this._cmdlet.PagingParameters.NewTotalCount(0L, 0.0);
         this._cmdlet.WriteObject(sendToPipeline);
     }
     ulong skip = this._cmdlet.PagingParameters.Skip;
     ulong first = this._cmdlet.PagingParameters.First;
     if ((skip == 0L) && (first == ulong.MaxValue))
     {
         ulong num3 = 0L;
         while (!this._deserializer.Done())
         {
             object obj3 = this._deserializer.Deserialize();
             num3 += (ulong) 1L;
             if (num3 >= skip)
             {
                 if (first == 0L)
                 {
                     return;
                 }
                 this._cmdlet.WriteObject(obj3);
                 first -= (ulong) 1L;
             }
         }
     }
     else
     {
         ulong num4 = 0L;
         ulong num5 = 0L;
         while (!this._deserializer.Done() && (num5 < first))
         {
             object obj4 = this._deserializer.Deserialize();
             PSObject obj5 = obj4 as PSObject;
             if (obj5 == null)
             {
                 num4 += (ulong) 1L;
                 if (num4 >= skip)
                 {
                     num5 += (ulong) 1L;
                     this._cmdlet.WriteObject(obj4);
                     continue;
                 }
             }
             ICollection baseObject = obj5.BaseObject as ICollection;
             if (baseObject != null)
             {
                 foreach (object obj6 in baseObject)
                 {
                     if (num5 >= first)
                     {
                         continue;
                     }
                     num4 += (ulong) 1L;
                     if (num4 >= skip)
                     {
                         num5 += (ulong) 1L;
                         this._cmdlet.WriteObject(obj6);
                     }
                 }
             }
             else
             {
                 num4 += (ulong) 1L;
                 if (num4 >= skip)
                 {
                     num5 += (ulong) 1L;
                     this._cmdlet.WriteObject(obj4);
                 }
             }
         }
     }
 }
示例#9
0
文件: PsUtils.cs 项目: 40a/PowerShell
        /// <summary>
        /// Decodes base64 encoded string in to args array
        /// </summary>
        /// <param name="base64"></param>
        /// <returns></returns>
        internal static object[] Base64ToArgsConverter(string base64)
        {
            if (string.IsNullOrEmpty(base64))
            {
                throw PSTraceSource.NewArgumentNullException("base64");
            }
            string decoded = new string(Encoding.Unicode.GetChars(Convert.FromBase64String(base64)));

            //Deserialize string
            XmlReader reader = XmlReader.Create(new StringReader(decoded), InternalDeserializer.XmlReaderSettingsForCliXml);
            object dso;
            Deserializer deserializer = new Deserializer(reader);
            dso = deserializer.Deserialize();
            if (deserializer.Done() == false)
            {
                //This helper function should move to host and it should provide appropriate
                //error message there.
                throw PSTraceSource.NewArgumentException(MinishellParameterBinderController.ArgsParameter);
            }

            PSObject mo = dso as PSObject;
            if (mo == null)
            {
                //This helper function should move the host. Provide appropriate error message.
                //Format of args parameter is not correct.
                throw PSTraceSource.NewArgumentException(MinishellParameterBinderController.ArgsParameter);
            }

            var argsList = mo.BaseObject as ArrayList;
            if (argsList == null)
            {
                //This helper function should move the host. Provide appropriate error message.
                //Format of args parameter is not correct.
                throw PSTraceSource.NewArgumentException(MinishellParameterBinderController.ArgsParameter);
            }

            return argsList.ToArray();
        }