暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

Oid.cs 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. //+-------------------------------------------------------------------------------+
  2. //| Copyright (c) 2003 Liping Dai. All rights reserved. |
  3. //| Web: www.lipingshare.com |
  4. //| Email: lipingshare@yahoo.com |
  5. //| |
  6. //| Copyright and Permission Details: |
  7. //| ================================= |
  8. //| Permission is hereby granted, free of charge, to any person obtaining a copy |
  9. //| of this software and associated documentation files (the "Software"), to deal |
  10. //| in the Software without restriction, including without limitation the rights |
  11. //| to use, copy, modify, merge, publish, distribute, and/or sell copies of the |
  12. //| Software, subject to the following conditions: |
  13. //| |
  14. //| 1. Redistributions of source code must retain the above copyright notice, this|
  15. //| list of conditions and the following disclaimer. |
  16. //| |
  17. //| 2. Redistributions in binary form must reproduce the above copyright notice, |
  18. //| this list of conditions and the following disclaimer in the documentation |
  19. //| and/or other materials provided with the distribution. |
  20. //| |
  21. //| THE SOFTWARE PRODUCT IS PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND, |
  22. //| EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
  23. //| WARRANTIES OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR |
  24. //| A PARTICULAR PURPOSE. |
  25. //+-------------------------------------------------------------------------------+
  26. using System;
  27. using System.IO;
  28. using System.Collections.Specialized;
  29. namespace LipingShare.LCLib.Asn1Processor
  30. {
  31. /// <summary>
  32. /// Summary description for OID.
  33. /// This class is used to encode and decode OID strings.
  34. /// </summary>
  35. internal class Oid
  36. {
  37. /// <summary>
  38. /// Retrieve OID name by OID string.
  39. /// </summary>
  40. /// <param name="inOidStr">source OID string.</param>
  41. /// <returns>OID name.</returns>
  42. public string GetOidName(string inOidStr)
  43. {
  44. if (oidDictionary == null) //Initialize oidDictionary:
  45. {
  46. oidDictionary = new StringDictionary();
  47. // string oidStr = "";
  48. // string oidDesc = "";
  49. // bool loadOidError = false;
  50. // int dbCounter = 0;
  51. }
  52. return oidDictionary[inOidStr];
  53. }
  54. /// <summary>
  55. /// Encode OID string to byte array.
  56. /// </summary>
  57. /// <param name="oidStr">source string.</param>
  58. /// <returns>encoded array.</returns>
  59. public byte[] Encode(string oidStr)
  60. {
  61. MemoryStream ms = new MemoryStream();
  62. Encode(ms, oidStr);
  63. ms.Position = 0;
  64. byte[] retval = new byte[ms.Length];
  65. ms.Read(retval, 0, retval.Length);
  66. ms.Close();
  67. return retval;
  68. }
  69. /// <summary>
  70. /// Decode OID byte array to OID string.
  71. /// </summary>
  72. /// <param name="data">source byte array.</param>
  73. /// <returns>result OID string.</returns>
  74. public string Decode(byte[] data)
  75. {
  76. MemoryStream ms = new MemoryStream(data);
  77. ms.Position = 0;
  78. string retval = Decode(ms);
  79. ms.Close();
  80. return retval;
  81. }
  82. /// <summary>
  83. /// Encode OID string and put result into <see cref="Stream"/>
  84. /// </summary>
  85. /// <param name="bt">output stream.</param>
  86. /// <param name="oidStr">source OID string.</param>
  87. public virtual void Encode(Stream bt, string oidStr) //TODO
  88. {
  89. string[] oidList = oidStr.Split('.');
  90. if (oidList.Length < 2) throw new Exception("Invalid OID string.");
  91. ulong[] values = new ulong[oidList.Length];
  92. for (int i = 0; i < oidList.Length; i++)
  93. {
  94. values[i] = Convert.ToUInt64(oidList[i]);
  95. }
  96. bt.WriteByte((byte)(values[0] * 40 + values[1]));
  97. for (int i = 2; i < values.Length; i++)
  98. EncodeValue(bt, values[i]);
  99. }
  100. /// <summary>
  101. /// Decode OID <see cref="Stream"/> and return OID string.
  102. /// </summary>
  103. /// <param name="bt">source stream.</param>
  104. /// <returns>result OID string.</returns>
  105. public virtual string Decode(Stream bt)
  106. {
  107. string retval = "";
  108. byte b;
  109. ulong v = 0;
  110. b = (byte)bt.ReadByte();
  111. retval += Convert.ToString(b / 40);
  112. retval += "." + Convert.ToString(b % 40);
  113. while (bt.Position < bt.Length)
  114. {
  115. try
  116. {
  117. DecodeValue(bt, ref v);
  118. retval += "." + v.ToString();
  119. }
  120. catch (Exception e)
  121. {
  122. throw new Exception("Failed to decode OID value: " + e.Message);
  123. }
  124. }
  125. return retval;
  126. }
  127. /// <summary>
  128. /// OID dictionary.
  129. /// </summary>
  130. private static StringDictionary oidDictionary = null;
  131. /// <summary>
  132. /// Default constructor
  133. /// </summary>
  134. public Oid()
  135. {
  136. }
  137. /// <summary>
  138. /// Encode single OID value.
  139. /// </summary>
  140. /// <param name="bt">output stream.</param>
  141. /// <param name="v">source value.</param>
  142. protected void EncodeValue(Stream bt, ulong v)
  143. {
  144. for (int i = (Asn1Util.BitPrecision(v) - 1) / 7; i > 0; i--)
  145. {
  146. bt.WriteByte((byte)(0x80 | ((v >> (i * 7)) & 0x7f)));
  147. }
  148. bt.WriteByte((byte)(v & 0x7f));
  149. }
  150. /// <summary>
  151. /// Decode single OID value.
  152. /// </summary>
  153. /// <param name="bt">source stream.</param>
  154. /// <param name="v">output value</param>
  155. /// <returns>OID value bytes.</returns>
  156. protected int DecodeValue(Stream bt, ref ulong v)
  157. {
  158. byte b;
  159. int i = 0;
  160. v = 0;
  161. while (true)
  162. {
  163. b = (byte)bt.ReadByte();
  164. i++;
  165. v <<= 7;
  166. v += (ulong)(b & 0x7f);
  167. if ((b & 0x80) == 0)
  168. return i;
  169. }
  170. }
  171. }
  172. }