Example #1
0
        internal float Serialize(StreamMarshaler streamBuffer, bool isLast, uint arcIndex)
        {
            CfgArc cfgArc = default(CfgArc);

            cfgArc.LastArc           = isLast;
            cfgArc.HasSemanticTag    = (SemanticTagCount > 0);
            cfgArc.NextStartArcIndex = (uint)((_end != null) ? _end.SerializeId : 0);
            if (_ruleRef != null)
            {
                cfgArc.RuleRef         = true;
                cfgArc.TransitionIndex = (uint)_ruleRef._iSerialize;
            }
            else
            {
                cfgArc.RuleRef = false;
                if (_specialTransitionIndex != 0)
                {
                    cfgArc.TransitionIndex = (uint)_specialTransitionIndex;
                }
                else
                {
                    cfgArc.TransitionIndex = (uint)_iWord;
                }
            }
            cfgArc.LowConfRequired  = (_confidence < 0);
            cfgArc.HighConfRequired = (_confidence > 0);
            cfgArc.MatchMode        = (uint)_matchMode;
            _iSerialize             = arcIndex;
            streamBuffer.WriteStream(cfgArc);
            return(_flWeight);
        }
 private static StringBlob LoadStringBlob(StreamMarshaler streamHelper, uint iPos, int c)
 {
     char[] array = new char[c];
     streamHelper.Position = iPos;
     streamHelper.ReadArrayChar(array, c);
     return(new StringBlob(array));
 }
Example #3
0
        //
        //  This helper converts a serialized CFG grammar header into an in-memory header
        //
        internal static ScriptRef[] LoadScriptRefs(StreamMarshaler streamHelper, CfgSerializedHeader pFH)
        {
            //
            //  Because in 64-bit code, pointers != sizeof(ULONG) we copy each member explicitly.
            //
            if (pFH.FormatId != CfgGrammar._SPGDF_ContextFree)
            {
                return(null);
            }

            //We know that in SAPI 5.0 grammar format pszWords follows header immediately.
            if (pFH.pszWords < Marshal.SizeOf <CfgSerializedHeader>())
            {
                // Must be SAPI 6.0 or above to hold a .NET script
                return(null);
            }

            // Get the symbols
            StringBlob symbols = LoadStringBlob(streamHelper, pFH.pszSymbols, pFH.cchSymbols);

            // Get the script refs
            CfgScriptRef[] cfgScripts = Load <CfgScriptRef>(streamHelper, pFH.pScripts, pFH.cScripts);

            // Convert the CFG script reference to ScriptRef
            ScriptRef[] scripts = new ScriptRef[cfgScripts.Length];
            for (int i = 0; i < cfgScripts.Length; i++)
            {
                CfgScriptRef cfgScript = cfgScripts[i];
                scripts[i] = new ScriptRef(symbols[cfgScript._idRule], symbols[cfgScript._idMethod], cfgScript._method);
            }

            return(scripts);
        }
Example #4
0
 private static MemoryStream CombineCfg(string rule, Stream stream, SrgsRule[] extraRules)
 {
     using (MemoryStream memoryStream = new MemoryStream())
     {
         SrgsDocument srgsDocument = new SrgsDocument();
         srgsDocument.TagFormat = SrgsTagFormat.KeyValuePairs;
         foreach (SrgsRule srgsRule in extraRules)
         {
             srgsDocument.Rules.Add(srgsRule);
         }
         SrgsGrammarCompiler.Compile(srgsDocument, memoryStream);
         using (StreamMarshaler streamHelper = new StreamMarshaler(stream))
         {
             long    position = stream.Position;
             Backend org      = new Backend(streamHelper);
             stream.Position       = position;
             memoryStream.Position = 0L;
             MemoryStream memoryStream2 = new MemoryStream();
             using (StreamMarshaler streamHelper2 = new StreamMarshaler(memoryStream))
             {
                 Backend extra   = new Backend(streamHelper2);
                 Backend backend = Backend.CombineGrammar(rule, org, extra);
                 using (StreamMarshaler streamBuffer = new StreamMarshaler(memoryStream2))
                 {
                     backend.Commit(streamBuffer);
                     memoryStream2.Position = 0L;
                     return(memoryStream2);
                 }
             }
         }
     }
 }
 internal static ScriptRef[] LoadIL(Stream stream)
 {
     using (StreamMarshaler streamMarshaler = new StreamMarshaler(stream))
     {
         CfgSerializedHeader cfgSerializedHeader = new CfgSerializedHeader();
         streamMarshaler.ReadStream(cfgSerializedHeader);
         return(LoadScriptRefs(streamMarshaler, cfgSerializedHeader));
     }
 }
Example #6
0
        internal void Serialize(StreamMarshaler streamBuffer)
        {
            // Dynamic rules and imports have no arcs
            _cfgRule.FirstArcIndex = _firstState != null && !_firstState.OutArcs.IsEmpty ? (uint)_firstState.SerializeId : 0;

            _cfgRule.DirtyRule = true;

            streamBuffer.WriteStream(_cfgRule);
        }
Example #7
0
        internal void Serialize(StringBlob symbols, StreamMarshaler streamBuffer)
        {
            CfgScriptRef cfgScriptRef = default(CfgScriptRef);

            cfgScriptRef._idRule   = symbols.Find(_rule);
            cfgScriptRef._method   = _method;
            cfgScriptRef._idMethod = _idSymbol;
            streamBuffer.WriteStream(cfgScriptRef);
        }
Example #8
0
 public void WriteToWaveStream(Stream outputStream)
 {
     Helpers.ThrowIfNull(outputStream, "outputStream");
     using (StreamMarshaler sm = new StreamMarshaler(outputStream))
     {
         WriteWaveHeader(sm);
     }
     outputStream.Write(_rawAudioData, 0, _rawAudioData.Length);
     outputStream.Flush();
 }
 private static T[] Load <T>(StreamMarshaler streamHelper, uint iPos, int c)
 {
     T[] array = null;
     array = new T[c];
     if (c > 0)
     {
         streamHelper.Position = iPos;
         streamHelper.ReadArray(array, c);
     }
     return(array);
 }
Example #10
0
        private static MemoryStream BuildCfg(Backend backend)
        {
            MemoryStream memoryStream = new MemoryStream();

            using (StreamMarshaler streamBuffer = new StreamMarshaler(memoryStream))
            {
                backend.IL  = null;
                backend.PDB = null;
                backend.Commit(streamBuffer);
                return(memoryStream);
            }
        }
Example #11
0
        internal void SerializeStateEntries(StreamMarshaler streamBuffer, bool tagsCannotSpanOverMultipleArcs, float[] pWeights, ref uint iArcOffset, ref int iOffset)
        {
            List <Arc> list = _outArcs.ToList();

            list.Sort();
            Arc arc = (list.Count > 0) ? list[list.Count - 1] : null;
            IEnumerator <Arc> enumerator = ((IEnumerable <Arc>)list).GetEnumerator();

            enumerator.MoveNext();
            uint num  = (uint)(list.Count + (int)iArcOffset);
            uint num2 = num;

            foreach (Arc item in list)
            {
                int semanticTagCount = item.SemanticTagCount;
                if (semanticTagCount > 0)
                {
                    item.SetArcIndexForTag(0, iArcOffset, tagsCannotSpanOverMultipleArcs);
                }
                if (semanticTagCount <= 1)
                {
                    pWeights[iOffset++] = item.Serialize(streamBuffer, arc == item, iArcOffset++);
                }
                else
                {
                    iArcOffset++;
                    pWeights[iOffset++] = Arc.SerializeExtraEpsilonWithTag(streamBuffer, item, arc == item, num);
                    num = (uint)((int)num + (semanticTagCount - 1));
                }
            }
            enumerator = ((IEnumerable <Arc>)list).GetEnumerator();
            enumerator.MoveNext();
            num = num2;
            foreach (Arc item2 in list)
            {
                int semanticTagCount2 = item2.SemanticTagCount;
                if (semanticTagCount2 > 1)
                {
                    for (int i = 1; i < semanticTagCount2 - 1; i++)
                    {
                        item2.SetArcIndexForTag(i, iArcOffset, tagsCannotSpanOverMultipleArcs);
                        num++;
                        pWeights[iOffset++] = Arc.SerializeExtraEpsilonWithTag(streamBuffer, item2, true, num);
                        iArcOffset++;
                    }
                    item2.SetArcIndexForTag(semanticTagCount2 - 1, iArcOffset, tagsCannotSpanOverMultipleArcs);
                    pWeights[iOffset++] = item2.Serialize(streamBuffer, true, iArcOffset++);
                    num++;
                }
            }
        }
Example #12
0
        internal static float SerializeExtraEpsilonWithTag(StreamMarshaler streamBuffer, Arc arc, bool isLast, uint arcIndex)
        {
            CfgArc cfgArc = default(CfgArc);

            cfgArc.LastArc           = isLast;
            cfgArc.HasSemanticTag    = true;
            cfgArc.NextStartArcIndex = arcIndex;
            cfgArc.TransitionIndex   = 0u;
            cfgArc.LowConfRequired   = false;
            cfgArc.HighConfRequired  = false;
            cfgArc.MatchMode         = (uint)arc._matchMode;
            streamBuffer.WriteStream(cfgArc);
            return(arc._flWeight);
        }
Example #13
0
        internal void Compile(Stream stream)
        {
            Backend       backend = new Backend();
            CustomGrammar cg      = new CustomGrammar();
            SrgsElementCompilerFactory elementFactory = new SrgsElementCompilerFactory(backend, cg);

            CreateGrammar(elementFactory);
            backend.Optimize();
            using (StreamMarshaler streamBuffer = new StreamMarshaler(stream))
            {
                backend.Commit(streamBuffer);
            }
            stream.Position = 0L;
        }
Example #14
0
 private void WriteWaveHeader(StreamMarshaler sm)
 {
     char[] array = new char[4]
     {
         'R',
         'I',
         'F',
         'F'
     };
     byte[] array2 = _audioFormat.FormatSpecificData();
     sm.WriteArray(array, array.Length);
     sm.WriteStream((uint)(_rawAudioData.Length + 38 + array2.Length));
     char[] array3 = new char[4]
     {
         'W',
         'A',
         'V',
         'E'
     };
     sm.WriteArray(array3, array3.Length);
     char[] array4 = new char[4]
     {
         'f',
         'm',
         't',
         ' '
     };
     sm.WriteArray(array4, array4.Length);
     sm.WriteStream(18 + array2.Length);
     sm.WriteStream((ushort)_audioFormat.EncodingFormat);
     sm.WriteStream((ushort)_audioFormat.ChannelCount);
     sm.WriteStream(_audioFormat.SamplesPerSecond);
     sm.WriteStream(_audioFormat.AverageBytesPerSecond);
     sm.WriteStream((ushort)_audioFormat.BlockAlign);
     sm.WriteStream((ushort)_audioFormat.BitsPerSample);
     sm.WriteStream((ushort)array2.Length);
     if (array2.Length != 0)
     {
         sm.WriteStream(array2);
     }
     char[] array5 = new char[4]
     {
         'd',
         'a',
         't',
         'a'
     };
     sm.WriteArray(array5, array5.Length);
     sm.WriteStream(_rawAudioData.Length);
 }
Example #15
0
        internal void Serialize(StringBlob symbols, StreamMarshaler streamBuffer)
        {
            CfgScriptRef script = new();

            // Get the symbol id for the rule
            script._idRule = symbols.Find(_rule);

            script._method = _method;

            script._idMethod = _idSymbol;

            System.Diagnostics.Debug.Assert(script._idRule != -1 && script._idMethod != -1);

            streamBuffer.WriteStream(script);
        }
Example #16
0
        internal static CfgHeader ConvertCfgHeader(StreamMarshaler streamHelper, bool includeAllGrammarData, bool loadSymbols, out CfgSerializedHeader cfgSerializedHeader)
        {
            cfgSerializedHeader = new CfgSerializedHeader(streamHelper.Stream);
            CfgHeader cfgHeader = default(CfgHeader);

            cfgHeader.FormatId            = cfgSerializedHeader.FormatId;
            cfgHeader.GrammarGUID         = cfgSerializedHeader.GrammarGUID;
            cfgHeader.langId              = cfgSerializedHeader.LangID;
            cfgHeader.pszGlobalTags       = cfgSerializedHeader.pszSemanticInterpretationGlobals;
            cfgHeader.cArcsInLargestState = cfgSerializedHeader.cArcsInLargestState;
            cfgHeader.rules = Load <CfgRule>(streamHelper, cfgSerializedHeader.pRules, cfgSerializedHeader.cRules);
            if (includeAllGrammarData || loadSymbols)
            {
                cfgHeader.pszSymbols = LoadStringBlob(streamHelper, cfgSerializedHeader.pszSymbols, cfgSerializedHeader.cchSymbols);
            }
            if (includeAllGrammarData)
            {
                cfgHeader.pszWords = LoadStringBlob(streamHelper, cfgSerializedHeader.pszWords, cfgSerializedHeader.cchWords);
                cfgHeader.arcs     = Load <CfgArc>(streamHelper, cfgSerializedHeader.pArcs, cfgSerializedHeader.cArcs);
                cfgHeader.tags     = Load <CfgSemanticTag>(streamHelper, cfgSerializedHeader.tags, cfgSerializedHeader.cTags);
                cfgHeader.weights  = Load <float>(streamHelper, cfgSerializedHeader.pWeights, cfgSerializedHeader.cArcs);
            }
            if (cfgSerializedHeader.pszWords < Marshal.SizeOf(typeof(CfgSerializedHeader)))
            {
                cfgHeader.ulRootRuleIndex = uint.MaxValue;
                cfgHeader.GrammarOptions  = GrammarOptions.KeyValuePairs;
                cfgHeader.BasePath        = null;
                cfgHeader.GrammarMode     = GrammarType.VoiceGrammar;
            }
            else
            {
                cfgHeader.ulRootRuleIndex = cfgSerializedHeader.ulRootRuleIndex;
                cfgHeader.GrammarOptions  = cfgSerializedHeader.GrammarOptions;
                cfgHeader.GrammarMode     = (GrammarType)cfgSerializedHeader.GrammarMode;
                if (includeAllGrammarData)
                {
                    cfgHeader.scripts = Load <CfgScriptRef>(streamHelper, cfgSerializedHeader.pScripts, cfgSerializedHeader.cScripts);
                }
                if (cfgSerializedHeader.cBasePath != 0)
                {
                    streamHelper.Stream.Position = (int)cfgSerializedHeader.pRules + cfgHeader.rules.Length * Marshal.SizeOf(typeof(CfgRule));
                    cfgHeader.BasePath           = streamHelper.ReadNullTerminatedString();
                }
            }
            CheckValidCfgFormat(cfgSerializedHeader, cfgHeader, includeAllGrammarData);
            return(cfgHeader);
        }
Example #17
0
        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);
        }
Example #18
0
        private void WriteWaveHeader(StreamMarshaler sm)
        {
            char[] riff = new char[4] {
                'R', 'I', 'F', 'F'
            };
            byte[] formatSpecificData = _audioFormat.FormatSpecificData();
            sm.WriteArray <char>(riff, riff.Length);

            sm.WriteStream((uint)(_rawAudioData.Length + 38 + formatSpecificData.Length)); // Must be four bytes

            char[] wave = new char[4] {
                'W', 'A', 'V', 'E'
            };
            sm.WriteArray(wave, wave.Length);

            char[] fmt = new char[4] {
                'f', 'm', 't', ' '
            };
            sm.WriteArray(fmt, fmt.Length);

            sm.WriteStream(18 + formatSpecificData.Length);

            sm.WriteStream((ushort)_audioFormat.EncodingFormat);
            sm.WriteStream((ushort)_audioFormat.ChannelCount);
            sm.WriteStream(_audioFormat.SamplesPerSecond);
            sm.WriteStream(_audioFormat.AverageBytesPerSecond);
            sm.WriteStream((ushort)_audioFormat.BlockAlign);
            sm.WriteStream((ushort)_audioFormat.BitsPerSample);
            sm.WriteStream((ushort)formatSpecificData.Length);

            // write codec specific data
            if (formatSpecificData.Length > 0)
            {
                sm.WriteStream(formatSpecificData);
            }

            char[] data = new char[4] {
                'd', 'a', 't', 'a'
            };
            sm.WriteArray(data, data.Length);
            sm.WriteStream(_rawAudioData.Length);
        }
Example #19
0
        internal static ScriptRef[] LoadScriptRefs(StreamMarshaler streamHelper, CfgSerializedHeader pFH)
        {
            if (pFH.FormatId != _SPGDF_ContextFree)
            {
                return(null);
            }
            if (pFH.pszWords < Marshal.SizeOf(typeof(CfgSerializedHeader)))
            {
                return(null);
            }
            StringBlob stringBlob = LoadStringBlob(streamHelper, pFH.pszSymbols, pFH.cchSymbols);

            CfgScriptRef[] array  = Load <CfgScriptRef>(streamHelper, pFH.pScripts, pFH.cScripts);
            ScriptRef[]    array2 = new ScriptRef[array.Length];
            for (int i = 0; i < array.Length; i++)
            {
                CfgScriptRef cfgScriptRef = array[i];
                array2[i] = new ScriptRef(stringBlob[cfgScriptRef._idRule], stringBlob[cfgScriptRef._idMethod], cfgScriptRef._method);
            }
            return(array2);
        }
Example #20
0
 internal static bool LoadIL(Stream stream, out byte[] assemblyContent, out byte[] assemblyDebugSymbols, out ScriptRef[] scripts)
 {
     assemblyContent = (assemblyDebugSymbols = null);
     scripts         = null;
     using (StreamMarshaler streamMarshaler = new StreamMarshaler(stream))
     {
         CfgSerializedHeader cfgSerializedHeader = new CfgSerializedHeader();
         streamMarshaler.ReadStream(cfgSerializedHeader);
         scripts = LoadScriptRefs(streamMarshaler, cfgSerializedHeader);
         if (scripts == null)
         {
             return(false);
         }
         if (cfgSerializedHeader.cIL == 0)
         {
             return(false);
         }
         assemblyContent      = Load <byte>(streamMarshaler, cfgSerializedHeader.pIL, cfgSerializedHeader.cIL);
         assemblyDebugSymbols = ((cfgSerializedHeader.cPDB > 0) ? Load <byte>(streamMarshaler, cfgSerializedHeader.pPDB, cfgSerializedHeader.cPDB) : null);
     }
     return(true);
 }
Example #21
0
File: Arc.cs Project: z77ma/runtime
        internal float Serialize(StreamMarshaler streamBuffer, bool isLast, uint arcIndex)
        {
            CfgArc A = new();

            A.LastArc           = isLast;
            A.HasSemanticTag    = SemanticTagCount > 0;
            A.NextStartArcIndex = (uint)(_end != null ? _end.SerializeId : 0);
            if (_ruleRef != null)
            {
                A.RuleRef         = true;
                A.TransitionIndex = (uint)_ruleRef._iSerialize; //_pFirstState.SerializeId;
            }
            else
            {
                A.RuleRef = false;
                if (_specialTransitionIndex != 0)
                {
                    A.TransitionIndex = (uint)_specialTransitionIndex;
                }
                else
                {
                    A.TransitionIndex = (uint)_iWord;
                }
            }

            A.LowConfRequired  = (_confidence < 0);
            A.HighConfRequired = (_confidence > 0);
            A.MatchMode        = (uint)_matchMode;

            // For new arcs SerializeId is INFINITE so we set it correctly here.
            // For existing states we preserve the index from loading,
            //  unless new states have been added in, in which case the arc index,
            //  and hence the transition id have changed. There is a workaround in ReloadCmd
            //  to invalidate rules in this case.
            _iSerialize = arcIndex;

            streamBuffer.WriteStream(A);
            return(_flWeight);
        }
Example #22
0
 internal void Serialize(StreamMarshaler streamBuffer)
 {
     _cfgRule.FirstArcIndex = (uint)((_firstState != null && !_firstState.OutArcs.IsEmpty) ? _firstState.SerializeId : 0);
     _cfgRule.DirtyRule     = true;
     streamBuffer.WriteStream(_cfgRule);
 }
Example #23
0
        internal void SerializeStateEntries(StreamMarshaler streamBuffer, bool tagsCannotSpanOverMultipleArcs, float[] pWeights, ref uint iArcOffset, ref int iOffset)
        {
            // The arcs must be sorted before being written to disk.
            List <Arc> outArcs = _outArcs.ToList();

            outArcs.Sort();
            Arc lastArc = outArcs.Count > 0 ? outArcs[outArcs.Count - 1] : null;

            IEnumerator <Arc> enumArcs = ((IEnumerable <Arc>)outArcs).GetEnumerator();

            enumArcs.MoveNext();

            uint nextAvailableArc     = (uint)outArcs.Count + iArcOffset;
            uint saveNextAvailableArc = nextAvailableArc;

            // Write the arc of the first epsilon arc with an arc has more than one semantic tag
            foreach (Arc arc in outArcs)
            {
                // Create the first arc.
                int cSemantics = arc.SemanticTagCount;

                // Set the semantic property reference for the first arc
                if (cSemantics > 0)
                {
                    arc.SetArcIndexForTag(0, iArcOffset, tagsCannotSpanOverMultipleArcs);
                }

                // Serialize the arc
                if (cSemantics <= 1)
                {
                    pWeights[iOffset++] = arc.Serialize(streamBuffer, lastArc == arc, iArcOffset++);
                }
                else
                {
                    // update the position of the current arc
                    ++iArcOffset;

                    // more than one arc, create an epsilon transition
                    pWeights[iOffset++] = Arc.SerializeExtraEpsilonWithTag(streamBuffer, arc, lastArc == arc, nextAvailableArc);

                    // reset the position of the next available slop for an arc
                    nextAvailableArc += (uint)cSemantics - 1;
                }
            }

            enumArcs = ((IEnumerable <Arc>)outArcs).GetEnumerator();
            enumArcs.MoveNext();

            // revert the position for the new arc
            nextAvailableArc = saveNextAvailableArc;

            // write the additional arcs if we have more than one semantic tag
            foreach (Arc arc in outArcs)
            {
                int cSemantics = arc.SemanticTagCount;

                if (cSemantics > 1)
                {
                    // If more than 2 arcs insert extra new epsilon states, one per semantic tag
                    for (int i = 1; i < cSemantics - 1; i++)
                    {
                        // Set the semantic property reference
                        arc.SetArcIndexForTag(i, iArcOffset, tagsCannotSpanOverMultipleArcs);

                        // reset the position of the next available slop for an arc
                        nextAvailableArc++;

                        // create an epsilon transition
                        pWeights[iOffset++] = Arc.SerializeExtraEpsilonWithTag(streamBuffer, arc, true, nextAvailableArc);

                        // update the position of the current arc
                        ++iArcOffset;
                    }

                    // Set the semantic property reference
                    arc.SetArcIndexForTag(cSemantics - 1, iArcOffset, tagsCannotSpanOverMultipleArcs);

                    // Add the real arc at the end
                    pWeights[iOffset++] = arc.Serialize(streamBuffer, true, iArcOffset++);

                    // reset the position of the next available slop for an arc
                    nextAvailableArc++;
                }
            }
        }
Example #24
0
 internal void Serialize(StreamMarshaler streamBuffer)
 {
     streamBuffer.WriteStream(_cfgTag);
 }
Example #25
0
        private static object CompileStream(int iCfg, ISrgsParser srgsParser, string srgsPath, string filename, Stream stream, bool fOutputCfg, StringBuilder innerCode, object cfgResources, out CultureInfo culture, string[] referencedAssemblies, string keyFile)
        {
            Backend       backend       = new Backend();
            CustomGrammar customGrammar = new CustomGrammar();
            SrgsElementCompilerFactory srgsElementCompilerFactory = (SrgsElementCompilerFactory)(srgsParser.ElementFactory = new SrgsElementCompilerFactory(backend, customGrammar));

            srgsParser.Parse();
            backend.Optimize();
            culture = ((backend.LangId == 21514) ? new CultureInfo("es-us") : new CultureInfo(backend.LangId));
            if (customGrammar._codebehind.Count > 0 && !string.IsNullOrEmpty(srgsPath))
            {
                for (int i = 0; i < customGrammar._codebehind.Count; i++)
                {
                    if (!File.Exists(customGrammar._codebehind[i]))
                    {
                        customGrammar._codebehind[i] = srgsPath + "\\" + customGrammar._codebehind[i];
                    }
                }
            }
            if (referencedAssemblies != null)
            {
                foreach (string item in referencedAssemblies)
                {
                    customGrammar._assemblyReferences.Add(item);
                }
            }
            customGrammar._keyFile = keyFile;
            backend.ScriptRefs     = customGrammar._scriptRefs;
            if (!fOutputCfg)
            {
                CustomGrammar.CfgResource cfgResource = new CustomGrammar.CfgResource();
                cfgResource.data = BuildCfg(backend).ToArray();
                cfgResource.name = iCfg.ToString(CultureInfo.InvariantCulture) + ".CFG";
                ((List <CustomGrammar.CfgResource>)cfgResources).Add(cfgResource);
                innerCode.Append(customGrammar.CreateAssembly(iCfg, filename, culture));
                return(customGrammar);
            }
            if (customGrammar._scriptRefs.Count > 0 && !customGrammar.HasScript)
            {
                XmlParser.ThrowSrgsException(SRID.NoScriptsForRules);
            }
            CreateAssembly(backend, customGrammar);
            if (!string.IsNullOrEmpty(filename))
            {
                stream = new FileStream(filename, FileMode.Create, FileAccess.Write);
            }
            try
            {
                using (StreamMarshaler streamBuffer = new StreamMarshaler(stream))
                {
                    backend.Commit(streamBuffer);
                    return(customGrammar);
                }
            }
            finally
            {
                if (!string.IsNullOrEmpty(filename))
                {
                    stream.Close();
                }
            }
        }
Example #26
0
 internal Backend(StreamMarshaler streamHelper)
 {
     InitFromBinaryGrammar(streamHelper);
 }
Example #27
0
        internal void Commit(StreamMarshaler streamBuffer)
        {
            long         position = streamBuffer.Stream.Position;
            List <State> list     = new List <State>(_states);

            _states = null;
            list.Sort();
            ValidateAndTagRules();
            CheckLeftRecursion(list);
            int num    = (_basePath != null) ? (_basePath.Length + 1) : 0;
            int idWord = 0;

            if (_globalTags.Count > 0)
            {
                StringBuilder stringBuilder = new StringBuilder();
                foreach (string globalTag in _globalTags)
                {
                    stringBuilder.Append(globalTag);
                }
                _symbols.Add(stringBuilder.ToString(), out idWord);
                idWord = _symbols.OffsetFromId(idWord);
                if (idWord > 65535)
                {
                    throw new OverflowException(SR.Get(SRID.TooManyRulesWithSemanticsGlobals));
                }
            }
            foreach (ScriptRef scriptRef in _scriptRefs)
            {
                _symbols.Add(scriptRef._sMethod, out scriptRef._idSymbol);
            }
            int cArcs;

            float[] pWeights;
            CfgGrammar.CfgSerializedHeader o = BuildHeader(list, num, (ushort)idWord, out cArcs, out pWeights);
            streamBuffer.WriteStream(o);
            streamBuffer.WriteArrayChar(_words.SerializeData(), _words.SerializeSize());
            streamBuffer.WriteArrayChar(_symbols.SerializeData(), _symbols.SerializeSize());
            foreach (Rule rule in _rules)
            {
                rule.Serialize(streamBuffer);
            }
            if (num > 0)
            {
                streamBuffer.WriteArrayChar(_basePath.ToCharArray(), _basePath.Length);
                streamBuffer.WriteArrayChar(_achZero, 1);
                streamBuffer.WriteArray(_abZero3, (num * 2) & 3);
            }
            streamBuffer.WriteStream(default(CfgArc));
            int  iOffset    = 1;
            uint iArcOffset = 1u;
            bool flag       = (GrammarOptions & GrammarOptions.MssV1) == GrammarOptions.MssV1;

            foreach (State item in list)
            {
                item.SerializeStateEntries(streamBuffer, flag, pWeights, ref iArcOffset, ref iOffset);
            }
            if (_fNeedWeightTable)
            {
                streamBuffer.WriteArray(pWeights, cArcs);
            }
            if (!flag)
            {
                foreach (State item2 in list)
                {
                    item2.SetEndArcIndexForTags();
                }
            }
            for (int num2 = _tags.Count - 1; num2 >= 0; num2--)
            {
                if (_tags[num2]._cfgTag.ArcIndex == 0)
                {
                    _tags.RemoveAt(num2);
                }
            }
            _tags.Sort();
            foreach (Tag tag in _tags)
            {
                tag.Serialize(streamBuffer);
            }
            foreach (ScriptRef scriptRef2 in _scriptRefs)
            {
                scriptRef2.Serialize(_symbols, streamBuffer);
            }
            if (_il != null && _il.Length != 0)
            {
                streamBuffer.Stream.Write(_il, 0, _il.Length);
            }
            if (_pdb != null && _pdb.Length != 0)
            {
                streamBuffer.Stream.Write(_pdb, 0, _pdb.Length);
            }
        }
Example #28
0
        internal static CfgHeader ConvertCfgHeader(StreamMarshaler streamHelper, bool includeAllGrammarData, bool loadSymbols, out CfgSerializedHeader cfgSerializedHeader)
        {
            cfgSerializedHeader = new CfgSerializedHeader(streamHelper.Stream);

            //
            //  Because in 64-bit code, pointers != sizeof(ULONG) we copy each member explicitly.
            //

            CfgHeader header = new();

            header.FormatId            = cfgSerializedHeader.FormatId;
            header.GrammarGUID         = cfgSerializedHeader.GrammarGUID;
            header.langId              = cfgSerializedHeader.LangID;
            header.pszGlobalTags       = cfgSerializedHeader.pszSemanticInterpretationGlobals;
            header.cArcsInLargestState = cfgSerializedHeader.cArcsInLargestState;

            // read all the common fields
            header.rules = Load <CfgRule>(streamHelper, cfgSerializedHeader.pRules, cfgSerializedHeader.cRules);

            if (includeAllGrammarData || loadSymbols)
            {
                header.pszSymbols = LoadStringBlob(streamHelper, cfgSerializedHeader.pszSymbols, cfgSerializedHeader.cchSymbols);
            }

            if (includeAllGrammarData)
            {
                header.pszWords = LoadStringBlob(streamHelper, cfgSerializedHeader.pszWords, cfgSerializedHeader.cchWords);
                header.arcs     = Load <CfgArc>(streamHelper, cfgSerializedHeader.pArcs, cfgSerializedHeader.cArcs);
                header.tags     = Load <CfgSemanticTag>(streamHelper, cfgSerializedHeader.tags, cfgSerializedHeader.cTags);
                header.weights  = Load <float>(streamHelper, cfgSerializedHeader.pWeights, cfgSerializedHeader.cArcs);
            }

            //We know that in SAPI 5.0 grammar format pszWords follows header immediately.
            if (cfgSerializedHeader.pszWords < Marshal.SizeOf <CfgSerializedHeader>())
            {
                //This is SAPI 5.0 and SAPI 5.1 grammar format
                header.ulRootRuleIndex = 0xFFFFFFFF;
                header.GrammarOptions  = GrammarOptions.KeyValuePairs;
                header.BasePath        = null;
                header.GrammarMode     = GrammarType.VoiceGrammar;
            }
            else
            {
                //This is SAPI 5.2 and beyond grammar format
                header.ulRootRuleIndex = cfgSerializedHeader.ulRootRuleIndex;
                header.GrammarOptions  = cfgSerializedHeader.GrammarOptions;
                header.GrammarMode     = (GrammarType)cfgSerializedHeader.GrammarMode;
                if (includeAllGrammarData)
                {
                    header.scripts = Load <CfgScriptRef>(streamHelper, cfgSerializedHeader.pScripts, cfgSerializedHeader.cScripts);
                }
                // The BasePath string is written after the rules - no offset is provided
                // Get the chars and build the string
                if (cfgSerializedHeader.cBasePath > 0)
                {
                    streamHelper.Stream.Position = (int)cfgSerializedHeader.pRules + (header.rules.Length * Marshal.SizeOf <CfgRule>());
                    header.BasePath = streamHelper.ReadNullTerminatedString();
                }
            }

            // Check the content - should be valid for both SAPI 5.0 and SAPI 5.2 grammars
            CheckValidCfgFormat(cfgSerializedHeader, header, includeAllGrammarData);

            return(header);
        }
Example #29
0
        //
        //  This helper converts a serialized CFG grammar header into an in-memory header
        //
        internal static CfgHeader ConvertCfgHeader(StreamMarshaler streamHelper)
        {
            CfgSerializedHeader cfgSerializedHeader = null;

            return(ConvertCfgHeader(streamHelper, true, true, out cfgSerializedHeader));
        }
Example #30
0
        internal void InitFromBinaryGrammar(StreamMarshaler streamHelper)
        {
            CfgGrammar.CfgHeader header = CfgGrammar.ConvertCfgHeader(streamHelper);
            _words          = header.pszWords;
            _symbols        = header.pszSymbols;
            _grammarOptions = header.GrammarOptions;
            State[] array = new State[header.arcs.Length];
            SortedDictionary <int, Rule> sortedDictionary = new SortedDictionary <int, Rule>();
            int count = _rules.Count;

            BuildRulesFromBinaryGrammar(header, array, sortedDictionary, count);
            Arc[]  array2 = new Arc[header.arcs.Length];
            bool   flag   = true;
            CfgArc cfgArc = default(CfgArc);
            State  state  = null;
            IEnumerator <KeyValuePair <int, Rule> > enumerator = sortedDictionary.GetEnumerator();

            if (enumerator.MoveNext())
            {
                KeyValuePair <int, Rule> current = enumerator.Current;
                Rule value = current.Value;
                for (int i = 1; i < header.arcs.Length; i++)
                {
                    CfgArc cfgArc2 = header.arcs[i];
                    if (cfgArc2.RuleRef)
                    {
                        value._listRules.Add(_rules[(int)cfgArc2.TransitionIndex]);
                    }
                    if (current.Key == i)
                    {
                        value = current.Value;
                        if (enumerator.MoveNext())
                        {
                            current = enumerator.Current;
                        }
                    }
                    if (flag || cfgArc.LastArc)
                    {
                        if (array[i] == null)
                        {
                            uint nextHandle = CfgGrammar.NextHandle;
                            array[i] = new State(value, nextHandle, i);
                            AddState(array[i]);
                        }
                        state = array[i];
                    }
                    int   nextStartArcIndex = (int)cfgArc2.NextStartArcIndex;
                    State end = null;
                    if (state != null && nextStartArcIndex != 0)
                    {
                        if (array[nextStartArcIndex] == null)
                        {
                            uint nextHandle2 = CfgGrammar.NextHandle;
                            array[nextStartArcIndex] = new State(value, nextHandle2, nextStartArcIndex);
                            AddState(array[nextStartArcIndex]);
                        }
                        end = array[nextStartArcIndex];
                    }
                    float flWeight = (header.weights != null) ? header.weights[i] : 1f;
                    Arc   arc;
                    if (cfgArc2.RuleRef)
                    {
                        Rule ruleRef = _rules[(int)cfgArc2.TransitionIndex];
                        arc = new Arc(null, ruleRef, _words, flWeight, 0, null, MatchMode.AllWords, ref _fNeedWeightTable);
                    }
                    else
                    {
                        int transitionIndex = (int)cfgArc2.TransitionIndex;
                        int num             = (transitionIndex == 4194302 || transitionIndex == 4194301 || transitionIndex == 4194303) ? transitionIndex : 0;
                        arc = new Arc((int)((num == 0) ? cfgArc2.TransitionIndex : 0), flWeight, cfgArc2.LowConfRequired ? (-1) : (cfgArc2.HighConfRequired ? 1 : 0), num, MatchMode.AllWords, ref _fNeedWeightTable);
                    }
                    arc.Start = state;
                    arc.End   = end;
                    AddArc(arc);
                    array2[i] = arc;
                    flag      = false;
                    cfgArc    = cfgArc2;
                }
            }
            int j = 1;
            int k = 0;

            for (; j < header.arcs.Length; j++)
            {
                CfgArc cfgArc3 = header.arcs[j];
                if (!cfgArc3.HasSemanticTag)
                {
                    continue;
                }
                for (; k < header.tags.Length && header.tags[k].StartArcIndex == j; k++)
                {
                    CfgSemanticTag cfgTag = header.tags[k];
                    Tag            tag    = new Tag(this, cfgTag);
                    _tags.Add(tag);
                    array2[tag._cfgTag.StartArcIndex].AddStartTag(tag);
                    array2[tag._cfgTag.EndArcIndex].AddEndTag(tag);
                    if (cfgTag._nameOffset > 0)
                    {
                        tag._cfgTag._nameOffset = _symbols.OffsetFromId(_symbols.Find(_symbols.FromOffset(cfgTag._nameOffset)));
                    }
                    else
                    {
                        tag._cfgTag._valueOffset = _symbols.OffsetFromId(_symbols.Find(_symbols.FromOffset(cfgTag._valueOffset)));
                    }
                }
            }
            _fNeedWeightTable = true;
            if (header.BasePath != null)
            {
                SetBasePath(header.BasePath);
            }
            _guid              = header.GrammarGUID;
            _langId            = header.langId;
            _grammarMode       = header.GrammarMode;
            _fLoadedFromBinary = true;
        }