1 /* 2 * Created by SharpDevelop. 3 * User: lextm 4 * Date: 2008/7/25 5 * Time: 20:41 6 * 7 * To change this template use Tools | Options | Coding | Edit Standard Headers. 8 */ 9 10 using System; 11 using System.Collections.Generic; 12 13 namespace Lextm.SharpSnmpLib.Mib.Elements.Types 14 { 15 /// <summary> 16 /// The INTEGER type represents a list of alternatives, or a range of numbers.. 17 /// Includes Integer32 as it's indistinguishable from INTEGER. 18 /// </summary> 19 /** 20 * As this type is used for Integer32 as well as INTEGER it incorrectly 21 * allows enumeration sub-typing of Integer32. This is ok as currently we 22 * do not care about detecting incorrect MIBs and this doesn't block the 23 * decoding of correct MIBs. 24 */ 25 public sealed class IntegerType : BaseType 26 { 27 public enum Types 28 { 29 Integer, 30 Integer32 31 } 32 33 private Types _type; 34 private bool _isEnumeration; 35 private ValueMap _map; 36 private ValueRanges _ranges; 37 38 /// <summary> 39 /// Creates an <see cref="IntegerType"/> instance. 40 /// </summary> 41 /// <param name="module"></param> 42 /// <param name="name"></param> 43 /// <param name="enumerator"></param> IntegerType(IModule module, string name, Symbol type, ISymbolEnumerator symbols)44 public IntegerType(IModule module, string name, Symbol type, ISymbolEnumerator symbols) 45 : base (module, name) 46 { 47 Types? t = GetExactType(type); 48 type.Assert(t.HasValue, "Unknown symbol for unsigned type!"); 49 _type = t.Value; 50 51 _isEnumeration = false; 52 53 Symbol current = symbols.NextNonEOLSymbol(); 54 if (current == Symbol.OpenBracket) 55 { 56 _isEnumeration = true; 57 symbols.PutBack(current); 58 _map = Lexer.DecodeEnumerations(symbols); 59 } 60 else if (current == Symbol.OpenParentheses) 61 { 62 symbols.PutBack(current); 63 _ranges = Lexer.DecodeRanges(symbols); 64 current.Assert(!_ranges.IsSizeDeclaration, "SIZE keyword is not allowed for ranges of integer types!"); 65 } 66 else 67 { 68 symbols.PutBack(current); 69 } 70 } 71 72 public Types Type 73 { 74 get { return _type; } 75 } 76 77 public ValueRanges Ranges 78 { 79 get { return _ranges; } 80 } 81 82 public bool IsEnumeration 83 { 84 get 85 { 86 return _isEnumeration; 87 } 88 } 89 90 public ValueMap Enumeration 91 { 92 get { return _isEnumeration ? _map : null; } 93 } 94 GetExactType(Symbol symbol)95 internal static Types? GetExactType(Symbol symbol) 96 { 97 if (symbol == Symbol.Integer) 98 { 99 // represents the ASN.1 builtin INTEGER type: 100 // may be represent any arbitrary (signed/unsigned) integer (in theory may have any size) 101 return Types.Integer; 102 } 103 else if (symbol == Symbol.Integer32) 104 { 105 // Integer32 ::= INTEGER (-2147483648..2147483647) // from SNMPv2-SMI 106 return Types.Integer32; 107 } 108 109 return null; 110 } 111 IsIntegerType(Symbol symbol)112 internal static bool IsIntegerType(Symbol symbol) 113 { 114 return GetExactType(symbol).HasValue; 115 } 116 } 117 }