Пример #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
 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();
 }
Пример #3
0
        /// <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());
        }
        internal static object[] Base64ToArgsConverter(string base64)
        {
            string str = !string.IsNullOrEmpty(base64) ? new string(Encoding.Unicode.GetChars(Convert.FromBase64String(base64))) : throw StringToBase64Converter.tracer.NewArgumentNullException(nameof(base64));

            StringToBase64Converter.tracer.WriteLine(str, new object[0]);
            Deserializer deserializer = new Deserializer((XmlReader) new XmlTextReader((TextReader) new StringReader(str)));
            object       obj          = deserializer.Deserialize();

            if (!deserializer.Done())
            {
                throw StringToBase64Converter.tracer.NewArgumentException("-args");
            }
            if (!(obj is PSObject psObject))
            {
                throw StringToBase64Converter.tracer.NewArgumentException("-args");
            }
            if (!(psObject.BaseObject is ArrayList baseObject))
            {
                throw StringToBase64Converter.tracer.NewArgumentException("-args");
            }
            return(baseObject.ToArray());
        }
Пример #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
 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);
     }
 }
Пример #9
0
        /// <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();
        }
Пример #10
0
        private void ReadXml()
        {
            try
            {
                Deserializer deserializer = new Deserializer((XmlReader) new XmlTextReader((TextReader)this.streamReader));
                while (!deserializer.Done())
                {
                    string          streamName;
                    object          obj    = deserializer.Deserialize(out streamName);
                    MinishellStream stream = MinishellStream.Unknown;
                    if (streamName != null)
                    {
                        stream = StringToMinishellStreamConverter.ToMinishellStream(streamName);
                    }
                    if (stream == MinishellStream.Unknown)
                    {
                        stream = this.isOutput ? MinishellStream.Output : MinishellStream.Error;
                    }
                    if (stream == MinishellStream.Output || obj != null)
                    {
                        switch (stream)
                        {
                        case MinishellStream.Error:
                            if (obj is PSObject)
                            {
                                obj = (object)ErrorRecord.FromPSObjectForRemoting(PSObject.AsPSObject(obj));
                                break;
                            }
                            string message;
                            try
                            {
                                message = (string)LanguagePrimitives.ConvertTo(obj, typeof(string), (IFormatProvider)CultureInfo.InvariantCulture);
                            }
                            catch (PSInvalidCastException ex)
                            {
                                ProcessStreamReader.tracer.TraceException((Exception)ex);
                                continue;
                            }
                            obj = (object)new ErrorRecord((Exception) new RemoteException(message), "NativeCommandError", ErrorCategory.NotSpecified, (object)message);
                            break;

                        case MinishellStream.Verbose:
                        case MinishellStream.Warning:
                        case MinishellStream.Debug:
                            try
                            {
                                obj = LanguagePrimitives.ConvertTo(obj, typeof(string), (IFormatProvider)CultureInfo.InvariantCulture);
                                break;
                            }
                            catch (PSInvalidCastException ex)
                            {
                                ProcessStreamReader.tracer.TraceException((Exception)ex);
                                continue;
                            }
                        }
                        this.AddObjectToWriter(obj, stream);
                    }
                }
            }
            catch (XmlException ex)
            {
                ProcessStreamReader.tracer.TraceException((Exception)ex);
                this.AddObjectToWriter((object)new ErrorRecord((Exception) new XmlException(string.Format((IFormatProvider)null, ResourceManagerCache.GetResourceString("NativeCP", "CliXmlError"), (object)(MinishellStream)(this.isOutput ? 0 : 1), (object)this.processPath, (object)ex.Message), (Exception)ex), "ProcessStreamReader_CliXmlError", ErrorCategory.SyntaxError, (object)this.processPath), MinishellStream.Error);
            }
        }