Exemple #1
0
        // Loads a strongly typed grammar from a resource in the Assembly.
        private Stream LoadCfgFromResource(bool stgInit)
        {
            // Strongly typed grammar get the Cfg data
            Assembly assembly = Assembly.GetAssembly(GetType());

            Stream stream = assembly.GetManifestResourceStream(ResourceName);

            if (stream == null)
            {
                throw new FormatException(SR.Get(SRID.RecognizerInvalidBinaryGrammar));
            }
            try
            {
                ScriptRef[] scripts = CfgGrammar.LoadIL(stream);
                if (scripts == null)
                {
                    throw new ArgumentException(SR.Get(SRID.CannotLoadDotNetSemanticCode));
                }
                _scripts = scripts;
            }
            catch (Exception e)
            {
                throw new ArgumentException(SR.Get(SRID.CannotLoadDotNetSemanticCode), e);
            }
            stream.Position = 0;

            // Update the rule name
            _ruleName = CheckRuleName(stream, GetType().Name, false, stgInit, out _sapi53Only, out _semanticTag);

            _isStg = true;
            return(stream);
        }
        private Stream LoadCfgFromResource(bool stgInit)
        {
            Type     t        = typeof(SR);
            Assembly assembly = t.Assembly;

            Stream manifestResourceStream = assembly.GetManifestResourceStream(t.Namespace + "." + this.ResourceName);

            if (manifestResourceStream == null)
            {
                throw new FormatException(SR.Get(SRID.RecognizerInvalidBinaryGrammar));
            }
            try
            {
                ScriptRef[] array = CfgGrammar.LoadIL(manifestResourceStream);
                if (array == null)
                {
                    throw new ArgumentException(SR.Get(SRID.CannotLoadDotNetSemanticCode));
                }
                _scripts = array;
            }
            catch (Exception innerException)
            {
                throw new ArgumentException(SR.Get(SRID.CannotLoadDotNetSemanticCode), innerException);
            }
            manifestResourceStream.Position = 0L;
            _ruleName = CheckRuleName(manifestResourceStream, GetType().Name, false, stgInit, out _sapi53Only, out _semanticTag);
            _isStg    = true;
            return(manifestResourceStream);
        }
Exemple #3
0
        // Pulls the required data out of a stream containing a cfg.
        // Stream must point to start of cfg on entry and is reset to same point on exit.
        private static string CheckRuleName(Stream stream, string rulename, bool isImportedGrammar, bool stgInit, out bool sapi53Only, out GrammarOptions grammarOptions)
        {
            sapi53Only = false;
            long initialPosition = stream.Position;

            CfgGrammar.CfgHeader header;
            using (StreamMarshaler streamHelper = new(stream)) // Use StreamMarshaler which helps deserialize certain data types
            {
                CfgGrammar.CfgSerializedHeader serializedHeader = null;
                header = CfgGrammar.ConvertCfgHeader(streamHelper, false, true, out serializedHeader);

                StringBlob symbols = header.pszSymbols;

                // Calc the root rule
                string rootRule = header.ulRootRuleIndex != 0xffffffff && header.ulRootRuleIndex < header.rules.Length ? symbols.FromOffset(header.rules[header.ulRootRuleIndex]._nameOffset) : null;

                // Get if we have semantic interpretation
                sapi53Only = (header.GrammarOptions & (GrammarOptions.MssV1 | GrammarOptions.W3cV1 | GrammarOptions.STG | GrammarOptions.IpaPhoneme)) != 0;

                // Check that the rule name is valid
                if (rootRule == null && string.IsNullOrEmpty(rulename))
                {
                    throw new ArgumentException(SR.Get(SRID.SapiErrorNoRulesToActivate));
                }

                if (!string.IsNullOrEmpty(rulename))
                {
                    // Convert the CFG script reference to ScriptRef
                    bool fFoundRule = false;
                    foreach (CfgRule cfgRule in header.rules)
                    {
                        if (symbols.FromOffset(cfgRule._nameOffset) == rulename)
                        {
                            // Private rule are not allowed
                            fFoundRule = cfgRule.Export || stgInit || (!isImportedGrammar ? cfgRule.TopLevel || rulename == rootRule : false);
                            break;
                        }
                    }

                    // check that the name exists
                    if (!fFoundRule)
                    {
                        throw new ArgumentException(SR.Get(SRID.RecognizerRuleNotFoundStream, rulename));
                    }
                }
                else
                {
                    rulename = rootRule;
                }

                grammarOptions = header.GrammarOptions & GrammarOptions.TagFormat;
            }
            stream.Position = initialPosition;
            return(rulename);
        }
 private void CreateSandbox(MemoryStream stream)
 {
     stream.Position = 0L;
     byte[]      assemblyContent;
     byte[]      assemblyDebugSymbols;
     ScriptRef[] scripts;
     if (CfgGrammar.LoadIL(stream, out assemblyContent, out assemblyDebugSymbols, out scripts))
     {
         Assembly executingAssembly = Assembly.GetExecutingAssembly();
         _appDomain = AppDomain.CreateDomain("sandbox");
         _proxy     = (AppDomainGrammarProxy)_appDomain.CreateInstanceFromAndUnwrap(executingAssembly.GetName().CodeBase, "System.Speech.Internal.SrgsCompiler.AppDomainGrammarProxy");
         _proxy.Init(_ruleName, assemblyContent, assemblyDebugSymbols);
         _scripts = scripts;
     }
 }
        private static string CheckRuleName(Stream stream, string rulename, bool isImportedGrammar, bool stgInit, out bool sapi53Only, out GrammarOptions grammarOptions)
        {
            sapi53Only = false;
            long position = stream.Position;

            using (StreamMarshaler streamHelper = new StreamMarshaler(stream))
            {
                CfgGrammar.CfgSerializedHeader cfgSerializedHeader = null;
                CfgGrammar.CfgHeader           cfgHeader           = CfgGrammar.ConvertCfgHeader(streamHelper, false, true, out cfgSerializedHeader);
                StringBlob pszSymbols = cfgHeader.pszSymbols;
                string     text       = (cfgHeader.ulRootRuleIndex != uint.MaxValue && cfgHeader.ulRootRuleIndex < cfgHeader.rules.Length) ? pszSymbols.FromOffset(cfgHeader.rules[cfgHeader.ulRootRuleIndex]._nameOffset) : null;
                sapi53Only = ((cfgHeader.GrammarOptions & (GrammarOptions.MssV1 | GrammarOptions.IpaPhoneme | GrammarOptions.W3cV1 | GrammarOptions.STG)) != 0);
                if (text == null && string.IsNullOrEmpty(rulename))
                {
                    throw new ArgumentException(SR.Get(SRID.SapiErrorNoRulesToActivate));
                }
                if (!string.IsNullOrEmpty(rulename))
                {
                    bool      flag  = false;
                    CfgRule[] rules = cfgHeader.rules;
                    for (int i = 0; i < rules.Length; i++)
                    {
                        CfgRule cfgRule = rules[i];
                        if (pszSymbols.FromOffset(cfgRule._nameOffset) == rulename)
                        {
                            flag = (cfgRule.Export || stgInit || (!isImportedGrammar && (cfgRule.TopLevel || rulename == text)));
                            break;
                        }
                    }
                    if (!flag)
                    {
                        throw new ArgumentException(SR.Get(SRID.RecognizerRuleNotFoundStream, rulename));
                    }
                }
                else
                {
                    rulename = text;
                }
                grammarOptions = (cfgHeader.GrammarOptions & GrammarOptions.TagFormat);
            }
            stream.Position = position;
            return(rulename);
        }