예제 #1
0
        /// <summary>
        /// Retrieve the encoding parameter from the command line
        /// it throws if the encoding does not match the known ones.
        /// </summary>
        /// <returns>A System.Text.Encoding object (null if no encoding specified).</returns>
        internal static Encoding Convert(Cmdlet cmdlet, string encoding)
        {
            if (string.IsNullOrEmpty(encoding))
            {
                // no parameter passed, default to UTF8
                return(ClrFacade.GetDefaultEncoding());
            }

            Encoding foundEncoding;

            if (encodingMap.TryGetValue(encoding, out foundEncoding))
            {
                return(foundEncoding);
            }

            // error condition: unknown encoding value
            string validEncodingValues = string.Join(", ", TabCompletionResults);
            string msg = StringUtil.Format(PathUtilsStrings.OutFile_WriteToFileEncodingUnknown, encoding, validEncodingValues);

            ErrorRecord errorRecord = new ErrorRecord(
                PSTraceSource.NewArgumentException("Encoding"),
                "WriteToFileEncodingUnknown",
                ErrorCategory.InvalidArgument,
                null);

            errorRecord.ErrorDetails = new ErrorDetails(msg);
            cmdlet.ThrowTerminatingError(errorRecord);

            return(null);
        }
예제 #2
0
        internal static string ReadScript(string path)
        {
            using (FileStream readerStream = new FileStream(path, FileMode.Open, FileAccess.Read))
            {
                Encoding defaultEncoding = ClrFacade.GetDefaultEncoding();
                Microsoft.Win32.SafeHandles.SafeFileHandle safeFileHandle = readerStream.SafeFileHandle;

                using (StreamReader scriptReader = new StreamReader(readerStream, defaultEncoding))
                {
                    return(scriptReader.ReadToEnd());
                }
            }
        }
예제 #3
0
        private void ReadScriptContents()
        {
            if (_scriptContents == null)
            {
                // make sure we can actually load the script and that it's non-empty
                // before we call it.

                // Note, although we are passing ASCII as the encoding, the StreamReader
                // class still obeys the byte order marks at the beginning of the file
                // if present. If not present, then ASCII is used as the default encoding.

                try
                {
                    using (FileStream readerStream = new FileStream(_path, FileMode.Open, FileAccess.Read))
                    {
                        Encoding defaultEncoding = ClrFacade.GetDefaultEncoding();
                        Microsoft.Win32.SafeHandles.SafeFileHandle safeFileHandle = readerStream.SafeFileHandle;

                        using (StreamReader scriptReader = new StreamReader(readerStream, defaultEncoding))
                        {
                            _scriptContents   = scriptReader.ReadToEnd();
                            _originalEncoding = scriptReader.CurrentEncoding;

                            // Check if this came from a trusted path. If so, set its language mode to FullLanguage.
                            if (SystemPolicy.GetSystemLockdownPolicy() != SystemEnforcementMode.None)
                            {
                                SystemEnforcementMode scriptSpecificPolicy = SystemPolicy.GetLockdownPolicy(_path, safeFileHandle);
                                if (scriptSpecificPolicy != SystemEnforcementMode.Enforce)
                                {
                                    this.DefiningLanguageMode = PSLanguageMode.FullLanguage;
                                }
                                else
                                {
                                    this.DefiningLanguageMode = PSLanguageMode.ConstrainedLanguage;
                                }
                            }
                            else
                            {
                                if (this.Context != null)
                                {
                                    this.DefiningLanguageMode = this.Context.LanguageMode;
                                }
                            }
                        }
                    }
                }
                catch (ArgumentException e)
                {
                    // This catches PSArgumentException as well.
                    ThrowCommandNotFoundException(e);
                }
                catch (IOException e)
                {
                    ThrowCommandNotFoundException(e);
                }
                catch (NotSupportedException e)
                {
                    ThrowCommandNotFoundException(e);
                }
                catch (UnauthorizedAccessException e)
                {
                    // this is unadvertised exception thrown by the StreamReader ctor when
                    // no permission to read the script file
                    ThrowCommandNotFoundException(e);
                }
            }
        }
예제 #4
0
        private void ReadScriptContents()
        {
            if (_scriptContents == null)
            {
                // make sure we can actually load the script and that it's non-empty
                // before we call it.

                // Note, although we are passing ASCII as the encoding, the StreamReader
                // class still obeys the byte order marks at the beginning of the file
                // if present. If not present, then ASCII is used as the default encoding.

                try
                {
                    using (FileStream readerStream = new FileStream(_path, FileMode.Open, FileAccess.Read))
                    {
                        Encoding defaultEncoding = ClrFacade.GetDefaultEncoding();

                        using (StreamReader scriptReader = new StreamReader(readerStream, defaultEncoding))
                        {
                            _scriptContents   = scriptReader.ReadToEnd();
                            _originalEncoding = scriptReader.CurrentEncoding;

                            // Check this file against any system wide enforcement policies.
                            SystemScriptFileEnforcement filePolicyEnforcement = SystemPolicy.GetFilePolicyEnforcement(_path, readerStream);
                            switch (filePolicyEnforcement)
                            {
                            case SystemScriptFileEnforcement.None:
                                if (Context != null)
                                {
                                    DefiningLanguageMode = Context.LanguageMode;
                                }

                                break;

                            case SystemScriptFileEnforcement.Allow:
                                DefiningLanguageMode = PSLanguageMode.FullLanguage;
                                break;

                            case SystemScriptFileEnforcement.AllowConstrained:
                                DefiningLanguageMode = PSLanguageMode.ConstrainedLanguage;
                                break;

                            case SystemScriptFileEnforcement.Block:
                                throw new PSSecurityException(
                                          string.Format(
                                              Globalization.CultureInfo.CurrentUICulture,
                                              SecuritySupportStrings.ScriptFileBlockedBySystemPolicy,
                                              _path));

                            default:
                                throw new PSSecurityException(
                                          string.Format(
                                              Globalization.CultureInfo.CurrentUICulture,
                                              SecuritySupportStrings.UnknownSystemScriptFileEnforcement,
                                              filePolicyEnforcement));
                            }
                        }
                    }
                }
                catch (ArgumentException e)
                {
                    // This catches PSArgumentException as well.
                    ThrowCommandNotFoundException(e);
                }
                catch (IOException e)
                {
                    ThrowCommandNotFoundException(e);
                }
                catch (NotSupportedException e)
                {
                    ThrowCommandNotFoundException(e);
                }
                catch (UnauthorizedAccessException e)
                {
                    // this is unadvertised exception thrown by the StreamReader ctor when
                    // no permission to read the script file
                    ThrowCommandNotFoundException(e);
                }
            }
        }
예제 #5
0
        /// <summary>
        /// retrieve the encoding paramater from the command line
        /// it throws if the encoding does not match the known ones
        /// </summary>
        /// <returns>a System.Text.Encoding object (null if no encoding specified)</returns>
        internal static Encoding Convert(Cmdlet cmdlet, string encoding)
        {
            if (string.IsNullOrEmpty(encoding))
            {
                // no parameter passed, default to Unicode (OS preferred)
                return(System.Text.Encoding.Unicode);
            }

            // Default to unicode (this matches Get-Content)
            if (string.Equals(encoding, Unknown, StringComparison.OrdinalIgnoreCase))
            {
                return(System.Text.Encoding.Unicode);
            }

            if (string.Equals(encoding, String, StringComparison.OrdinalIgnoreCase))
            {
                return(System.Text.Encoding.Unicode);
            }

            // these are the encodings the CLR supports
            if (string.Equals(encoding, Unicode, StringComparison.OrdinalIgnoreCase))
            {
                return(System.Text.Encoding.Unicode);
            }

            if (string.Equals(encoding, BigEndianUnicode, StringComparison.OrdinalIgnoreCase))
            {
                return(System.Text.Encoding.BigEndianUnicode);
            }

            if (string.Equals(encoding, Utf8, StringComparison.OrdinalIgnoreCase))
            {
                return(System.Text.Encoding.UTF8);
            }

            if (string.Equals(encoding, Ascii, StringComparison.OrdinalIgnoreCase))
            {
                return(System.Text.Encoding.ASCII);
            }

            if (string.Equals(encoding, Utf7, StringComparison.OrdinalIgnoreCase))
            {
                return(System.Text.Encoding.UTF7);
            }

            if (string.Equals(encoding, Utf32, StringComparison.OrdinalIgnoreCase))
            {
                return(System.Text.Encoding.UTF32);
            }

            if (string.Equals(encoding, Default, StringComparison.OrdinalIgnoreCase))
            {
                return(ClrFacade.GetDefaultEncoding());
            }

            if (string.Equals(encoding, OEM, StringComparison.OrdinalIgnoreCase))
            {
                return(ClrFacade.GetOEMEncoding());
            }

            // error condition: unknown encoding value
            string validEncodingValues = string.Join(
                ", ",
                new string[] { Unknown, String, Unicode, BigEndianUnicode, Ascii, Utf8, Utf7, Utf32, Default, OEM });
            string msg = StringUtil.Format(PathUtilsStrings.OutFile_WriteToFileEncodingUnknown,
                                           encoding, validEncodingValues);

            ErrorRecord errorRecord = new ErrorRecord(
                PSTraceSource.NewArgumentException("Encoding"),
                "WriteToFileEncodingUnknown",
                ErrorCategory.InvalidArgument,
                null);

            errorRecord.ErrorDetails = new ErrorDetails(msg);
            cmdlet.ThrowTerminatingError(errorRecord);

            return(null);
        }