示例#1
0
        /// <summary>
        /// Initializes the MIB import. This will resolve all referenced
        /// symbols.This method will be called by the MIB loader.
        /// </summary>
        /// <param name="log">The MIB Loader Log</param>
        /// <exception cref="MibException">if an error was encountered during the
        /// initialization
        /// </exception>
#pragma warning disable IDE0060 // Remove unused parameter
        public void Initialize(MibLoaderLog log)
#pragma warning restore IDE0060 // Remove unused parameter
        {
            string message;

            this.mib = this.loader.GetMib(this.name);
            if (this.mib == null)
            {
                message = "couldn't find referenced MIB '" + this.name + "'";
                throw new MibException(this.location, message);
            }

            if (this.symbols != null)
            {
                foreach (var s in this.symbols)
                {
                    if (this.mib.GetSymbol(s.ToString()) == null)
                    {
                        message = "couldn't find imported symbol '" +
                                  s + "' in MIB '" + this.name + "'";
                        throw new MibException(this.location, message);
                    }
                }
            }
        }
示例#2
0
 /// <summary>
 /// Adds all log entries from another log.
 /// </summary>
 /// <param name="log">The log to be read from</param>
 public void AddAll(MibLoaderLog log)
 {
     foreach (var entry in log.entries)
     {
         this.Add(entry);
     }
 }
示例#3
0
        /// <summary>
        /// Prints all log entries to the specified output stream.
        /// </summary>
        /// <param name="output">The output stream to use</param>
        /// <param name="margin">The margin</param>
        public void PrintTo(TextWriter output, int margin)
        {
            string str;

            foreach (var entry in this.entries)
            {
                StringBuilder buffer = new StringBuilder();
                switch (entry.Type)
                {
                case LogEntry.Error:
                    buffer.Append("Error: ");
                    break;

                case LogEntry.Warning:
                    buffer.Append("Warning: ");
                    break;

                default:
                    buffer.Append("Internal Error: ");
                    break;
                }

                buffer.Append("in ");
                buffer.Append(MibLoaderLog.RelativeFilename(entry.File));
                if (entry.LineNumber > 0)
                {
                    buffer.Append(": line ");
                    buffer.Append(entry.LineNumber);
                }

                buffer.Append(":\n");
                str = MibLoaderLog.LinebreakString(entry.Message, "    ", margin);
                buffer.Append(str);
                str = entry.ReadLine();
                if (str != null && str.Length >= entry.ColumnNumber)
                {
                    buffer.Append("\n\n");
                    buffer.Append(str);
                    buffer.Append("\n");
                    for (int j = 1; j < entry.ColumnNumber; j++)
                    {
                        if (str[j - 1] == '\t')
                        {
                            buffer.Append("\t");
                        }
                        else
                        {
                            buffer.Append(" ");
                        }
                    }

                    buffer.Append("^");
                }

                output.WriteLine(buffer.ToString());
            }

            output.Flush();
        }
示例#4
0
            /// <summary>
            /// Parses the MIB input source and returns the MIB modules
            /// found. This method will read the MIB either from file, URL
            /// or input stream.
            /// </summary>
            /// <param name="loader">The MIB loader to use for imports</param>
            /// <param name="log">The MIB log to use for errors</param>
            /// <returns>The list of MIB modules created</returns>
            /// <exception cref="IOException">If the MIB couldn't be found</exception>
            /// <exception cref="MibLoaderException">
            /// If the MIB couldn't be parsed correctly
            /// </exception>
            public IList <Mib> ParseMib(MibLoader loader, MibLoaderLog log)
            {
                string result = string.Empty;

                // Open input stream
                if (this.input != null)
                {
                    result = this.input.ReadToEnd();
                }
                else if (this.url != null)
                {
                    using (var client = new System.Net.WebClient())
                    {
                        result = client.DownloadString(this.url);
                    }
                }
                else
                {
                    using (TextReader reader = System.IO.File.OpenText(this.File))
                    {
                        result = reader.ReadToEnd();
                    }
                }

                using (this.input = new StringReader(result))
                {
                    // Parse input stream
                    MibAnalyzer analyzer = new MibAnalyzer(this.File, loader, log);
                    try
                    {
                        if (parser == null)
                        {
                            Asn1Tokenizer tokenizer = new Asn1Tokenizer(this.input);
                            parser = new Asn1Parser(tokenizer, analyzer);
                            parser.Tokenizer.UseTokenList = true;
                        }
                        else
                        {
                            parser.Reset(this.input, analyzer);
                        }

                        parser.Parse();
                        return(analyzer.Mibs.ToList());
                    }
                    catch (ParserCreationException e)
                    {
                        log.AddInternalError(this.File, "parser creation error in ASN.1 parser: " + e.Message);
                        analyzer.Reset();
                        throw new MibLoaderException(log);
                    }
                    catch (ParserLogException e)
                    {
                        log.AddAll(this.File, e);
                        analyzer.Reset();
                        throw new MibLoaderException(log);
                    }
                }
            }
 /// <summary>Initializes the MIB symbol. This will remove all levels of
 /// indirection present, such as references to types or values. No
 /// information is lost by this operation. This method may modify
 /// this object as a side-effect.
 /// NOTE: This is an internal method that should
 /// only be called by the MIB loader.
 /// </summary>
 /// <param name="log">The MIB Loader Log</param>
 public override void Initialize(MibLoaderLog log)
 {
     if (this.type != null)
     {
         try
         {
             this.type = this.type.Initialize(this, log);
         }
         catch (MibException e)
         {
             log.AddError(e.Location, e.Message);
             this.type = null;
         }
     }
 }
示例#6
0
        /// <summary>
        /// Loads a MIB. This method will also load all imported MIB:s if
        /// not previously loaded by this loader. Note that if the source
        /// contains several MIB modules, this method will only return the
        /// first one (although all are loaded).
        /// </summary>
        /// <param name="src">The MIB source</param>
        /// <returns>The first MIB Module loaded</returns>
        /// <exception cref="IOException">If the MIB couldn't be found</exception>
        /// <exception cref="MibLoaderException">
        /// If the MIB file couldn't be loaded correctly
        /// </exception>
        private Mib Load(MibSource src)
        {
            this.sourceQueue.Clear();
            this.sourceQueue.Add(src);

            int position = mibs.Count;

            MibLoaderLog log = this.LoadQueue();

            if (log.ErrorCount > 0)
            {
                throw new MibLoaderException(log);
            }

            return(this.mibs[position]);
        }
示例#7
0
        /// <summary>
        /// Initializes the MIB symbol. This will remove all levels of
        /// indirection present, such as references to types or values. No
        /// information is lost by this operation. This method may modify
        /// this object as a side-effect.
        /// </summary>
        /// NOTE: This is an internal method that should
        /// only be called by the MIB loader.
        /// <param name="log">The MIB loader log</param>
        /// <exception cref="MibException">
        /// If an error was encountered during the initialization
        /// </exception>
        public override void Initialize(MibLoaderLog log)
        {
            ObjectIdentifierValue oid;

            if (this.type != null)
            {
                try
                {
                    this.type = this.type.Initialize(this, log);
                }
                catch (MibException e)
                {
                    log.AddError(e.Location, e.Message);
                    this.type = null;
                }
            }

            if (this.value != null)
            {
                try
                {
                    this.value = this.value.Initialize(log, this.type);
                }
                catch (MibException e)
                {
                    log.AddError(e.Location, e.Message);
                    this.value = null;
                }
            }

            if (this.type != null && this.value != null && !this.type.IsCompatible(this.value))
            {
                log.AddError(this.Location, "value is not compatible with type");
            }

            if (this.value is ObjectIdentifierValue)
            {
                oid = (ObjectIdentifierValue)this.value;
                if (oid.Symbol == null)
                {
                    oid.Symbol = this;
                }
            }
        }
示例#8
0
        /// <summary>
        /// Clears and prepares this MIB for garbage collection. This method
        /// will recursively clear all associated symbols, making sure that
        /// no data structures references symbols from this MIB. Obviously,
        /// this method shouldn't be called unless all dependant MIBs have
        /// been cleared first.
        /// </summary>
        public void Clear()
        {
            this.loader = null;
            this.log    = null;

            if (this.imports != null)
            {
                this.imports.Clear();
            }

            this.imports = null;
            if (this.symbolList != null)
            {
                foreach (MibSymbol symbol in this.symbolList)
                {
                    symbol.Clear();
                }

                this.symbolList.Clear();
            }

            this.symbolList = null;

            if (this.symbolNameMap != null)
            {
                this.symbolNameMap.Clear();
            }

            this.symbolNameMap = null;

            if (this.symbolValueMap != null)
            {
                this.symbolValueMap.Clear();
            }

            this.symbolValueMap = null;
        }
示例#9
0
 /// <summary>
 /// Initializes the MIB value. This will remove all levels of
 /// indirection present, such as references to other values. No
 /// value information is lost by this operation. This method may
 /// modify this object as a side-effect, and will return the basic
 /// value.
 /// NOTE: This is an internal method that should
 /// only be called by the MIB loader.
 /// </summary>
 /// <param name="log">The MIB loader log</param>
 /// <param name="type">The value type</param>
 /// <returns>The basic MIB value</returns>
 /// <exception cref="MibException">
 /// If an error was encountered during the initialization
 /// </exception>
 public abstract MibValue Initialize(MibLoaderLog log, MibType type);
示例#10
0
 /// <summary>
 /// Initializes the MIB type. This will remove all levels of
 /// indirection present, such as references to types or values. No
 /// information is lost by this operation. This method may modify
 /// this object as a side-effect, and will return the basic
 /// type.
 /// NOTE: This is an internal method that should
 /// only be called by the MIB loader.
 /// </summary>
 /// <param name="symbol">The MIB symbol containing this type</param>
 /// <param name="log">The MIB Loader Log</param>
 /// <returns>The basic MIB type</returns>
 /// <exception cref="MibException">If an error was encountered
 /// during initialization</exception>
 public abstract MibType Initialize(MibSymbol symbol, MibLoaderLog log);
示例#11
0
 /// <summary>
 /// Initializes the MIB symbol. This will remove all levels of
 /// indirection present, such as references to types or values.No
 /// information is lost by this operation.This method may modify
 /// this object as a side-effect.
 /// </summary>
 /// <param name="log">The MibLoaderLog</param>
 public override void Initialize(MibLoaderLog log)
 {
     // Nothing to be initialized
 }
示例#12
0
        /// <summary>
        /// Loads all MIB files in the loader queue. New entries may be
        /// added to the queue while loading a MIB, as a result of
        /// importing other MIB files. This method will either load all
        /// MIB files in the queue or none (if errors were encountered).
        /// </summary>
        /// <returns>The loader log for the whole queue</returns>
        /// <exception cref="MibLoaderException">
        /// If the MIB file in the queue couldn't be
        /// found
        /// </exception>
        private MibLoaderLog LoadQueue()
        {
            MibLoaderLog log       = new MibLoaderLog();
            IList <Mib>  processed = new List <Mib>();

            IList <Mib> list;

            foreach (var msrc in this.sourceQueue)
            {
                if (this.GetMib(msrc.File) == null)
                {
                    list = msrc.ParseMib(this, log);
                    foreach (var mib in list)
                    {
                        mib.Loaded = true;
                    }

                    this.mibs = this.mibs.Concat(list).ToList();
                    processed = processed.Concat(list).ToList();
                }
            }

            this.sourceQueue.Clear();

            for (int i = 0; i < nameQueue.Count; i++)
            {
                string name = nameQueue[i];
                if (!this.Locate(name, out MibSource src))
                {
                    continue;
                }

                if (this.GetMib(src.File) == null)
                {
                    list = src.ParseMib(this, log);
                    foreach (var mib in list)
                    {
                        mib.Loaded = false;
                    }

                    this.mibs = this.mibs.Concat(list).ToList();
                    processed = processed.Concat(list).ToList();
                }
            }

            this.nameQueue.Clear();

            // Initialize all parsed MIB files in reverse order
            foreach (var mib in processed.Reverse())
            {
                try
                {
                    mib.Initialize();
                }
                catch (MibLoaderException)
                {
                    // Do nothing, errors are already in the log
                }
            }

            // Validate all parsed MIB files in reverse order
            foreach (var mib in processed.Reverse())
            {
                try
                {
                    mib.Validate();
                }
                catch (MibLoaderException)
                {
                    // Do nothing, errors are already in the log
                }
            }

            // Handle errors
            if (log.ErrorCount > 0)
            {
                foreach (var mib in processed)
                {
                    this.mibs.Remove(mib);
                }
            }

            return(log);
        }
示例#13
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Mib"/> class.
 /// This will NOT read the actual MIB file, but only creates
 /// an empty container. The symbols are then added during the
 /// first analysis pass (of two), leaving symbols in the MIB
 /// possibly containing unresolved references.
 /// A separate call to Initialize() must be made once all
 /// referenced MIB modules have also been loaded.
 /// </summary>
 /// <param name="file">The MIB file name</param>
 /// <param name="loader">The MIB loader to use for imports</param>
 /// <param name="log">The MIB log to use for errors</param>
 /// <see cref="Initialize"/>
 public Mib(string file, MibLoader loader, MibLoaderLog log)
 {
     this.file   = file;
     this.loader = loader;
     this.log    = log;
 }
示例#14
0
 protected MibLoaderException(SerializationInfo info, StreamingContext context)
     : base(info, context)
 {
     this.log = (MibLoaderLog)info.GetValue("Log", typeof(MibLoaderLog));
 }
示例#15
0
 /// <summary>
 /// Initializes a new instance of the <see cref="MibLoaderException"/> class.
 /// The specified message will
 /// be added to a new MIB loader log as an error.
 /// </summary>
 /// <param name="file">The MIB file for which an exception was raised during loading</param>
 /// <param name="message">The detailed error message</param>
 public MibLoaderException(string file, string message)
 {
     this.log = new MibLoaderLog();
     this.log.AddError(file, -1, -1, message);
 }
示例#16
0
 /// <summary>
 /// Initializes a new instance of the <see cref="MibLoaderException"/> class.
 /// </summary>
 /// <param name="log">The MIB loader log</param>
 public MibLoaderException(MibLoaderLog log)
 {
     this.log = log;
 }