LLUDPZeroEncoder.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using OpenSim.Framework;
  29. using Nini.Config;
  30. using OpenMetaverse;
  31. namespace OpenSim.Region.ClientStack.LindenUDP
  32. {
  33. public sealed class LLUDPZeroEncoder
  34. {
  35. private byte[] m_tmp = new byte[16];
  36. private byte[] m_dest;
  37. private int zerocount;
  38. private int pos;
  39. public LLUDPZeroEncoder()
  40. {
  41. }
  42. public LLUDPZeroEncoder(byte[] data)
  43. {
  44. m_dest = data;
  45. zerocount = 0;
  46. }
  47. public byte[] Data
  48. {
  49. get
  50. {
  51. return m_dest;
  52. }
  53. set
  54. {
  55. m_dest = value;
  56. }
  57. }
  58. public int ZeroCount
  59. {
  60. get
  61. {
  62. return zerocount;
  63. }
  64. set
  65. {
  66. zerocount = value;
  67. }
  68. }
  69. public int Position
  70. {
  71. get
  72. {
  73. return pos;
  74. }
  75. set
  76. {
  77. pos = value;
  78. }
  79. }
  80. public unsafe void AddZeros(int len)
  81. {
  82. zerocount += len;
  83. while (zerocount > 255)
  84. {
  85. m_dest[pos++] = 0x00;
  86. m_dest[pos++] = 0xff;
  87. zerocount -= 256;
  88. }
  89. }
  90. public unsafe int Finish()
  91. {
  92. if(zerocount > 0)
  93. {
  94. m_dest[pos++] = 0x00;
  95. m_dest[pos++] = (byte)zerocount;
  96. }
  97. return pos;
  98. }
  99. public unsafe void AddBytes(byte[] src, int srclen)
  100. {
  101. for (int i = 0; i < srclen; ++i)
  102. {
  103. if (src[i] == 0x00)
  104. {
  105. zerocount++;
  106. if (zerocount == 0)
  107. {
  108. m_dest[pos++] = 0x00;
  109. m_dest[pos++] = 0xff;
  110. zerocount++;
  111. }
  112. }
  113. else
  114. {
  115. if (zerocount != 0)
  116. {
  117. m_dest[pos++] = 0x00;
  118. m_dest[pos++] = (byte)zerocount;
  119. zerocount = 0;
  120. }
  121. m_dest[pos++] = src[i];
  122. }
  123. }
  124. }
  125. public unsafe void AddByte(byte v)
  126. {
  127. if (v == 0x00)
  128. {
  129. zerocount++;
  130. if (zerocount == 0)
  131. {
  132. m_dest[pos++] = 0x00;
  133. m_dest[pos++] = 0xff;
  134. zerocount++;
  135. }
  136. }
  137. else
  138. {
  139. if (zerocount != 0)
  140. {
  141. m_dest[pos++] = 0x00;
  142. m_dest[pos++] = (byte)zerocount;
  143. zerocount = 0;
  144. }
  145. m_dest[pos++] = v;
  146. }
  147. }
  148. public void AddInt16(short v)
  149. {
  150. if (v == 0)
  151. AddZeros(2);
  152. else
  153. {
  154. Utils.Int16ToBytes(v, m_tmp, 0);
  155. AddBytes(m_tmp, 2);
  156. }
  157. }
  158. public void AddUInt16(ushort v)
  159. {
  160. if (v == 0)
  161. AddZeros(2);
  162. else
  163. {
  164. Utils.UInt16ToBytes(v, m_tmp, 0);
  165. AddBytes(m_tmp, 2);
  166. }
  167. }
  168. public void AddInt(int v)
  169. {
  170. if (v == 0)
  171. AddZeros(4);
  172. else
  173. {
  174. Utils.IntToBytesSafepos(v, m_tmp, 0);
  175. AddBytes(m_tmp, 4);
  176. }
  177. }
  178. public unsafe void AddUInt(uint v)
  179. {
  180. if (v == 0)
  181. AddZeros(4);
  182. else
  183. {
  184. Utils.UIntToBytesSafepos(v, m_tmp, 0);
  185. AddBytes(m_tmp, 4);
  186. }
  187. }
  188. public void AddFloatToUInt16(float v, float range)
  189. {
  190. Utils.FloatToUInt16Bytes(v, range, m_tmp, 0);
  191. AddBytes(m_tmp, 2);
  192. }
  193. public void AddFloat(float v)
  194. {
  195. if (v == 0f)
  196. AddZeros(4);
  197. else
  198. {
  199. Utils.FloatToBytesSafepos(v, m_tmp, 0);
  200. AddBytes(m_tmp, 4);
  201. }
  202. }
  203. public void AddInt64(long v)
  204. {
  205. if (v == 0)
  206. AddZeros(8);
  207. else
  208. {
  209. Utils.Int64ToBytesSafepos(v, m_tmp, 0);
  210. AddBytes(m_tmp, 8);
  211. }
  212. }
  213. public void AddUInt64(ulong v)
  214. {
  215. if (v == 0)
  216. AddZeros(8);
  217. else
  218. {
  219. Utils.UInt64ToBytesSafepos(v, m_tmp, 0);
  220. AddBytes(m_tmp, 8);
  221. }
  222. }
  223. public void AddVector3(Vector3 v)
  224. {
  225. if (v == Vector3.Zero)
  226. AddZeros(12);
  227. else
  228. {
  229. v.ToBytes(m_tmp, 0);
  230. AddBytes(m_tmp, 12);
  231. }
  232. }
  233. public void AddVector4(Vector4 v)
  234. {
  235. if (v == Vector4.Zero)
  236. AddZeros(16);
  237. else
  238. {
  239. v.ToBytes(m_tmp, 0);
  240. AddBytes(m_tmp, 16);
  241. }
  242. }
  243. public void AddNormQuat(Quaternion v)
  244. {
  245. v.ToBytes(m_tmp, 0);
  246. AddBytes(m_tmp, 12);
  247. }
  248. public void AddUUID(UUID v)
  249. {
  250. v.ToBytes(m_tmp, 0);
  251. AddBytes(m_tmp, 16);
  252. }
  253. }
  254. }