LLUDPZeroEncoder.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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 System.Text;
  29. using OpenSim.Framework;
  30. using Nini.Config;
  31. using OpenMetaverse;
  32. namespace OpenSim.Region.ClientStack.LindenUDP
  33. {
  34. public sealed class LLUDPZeroEncoder
  35. {
  36. private byte[] m_tmp = new byte[16];
  37. private byte[] m_dest;
  38. private int zerocount;
  39. private int pos;
  40. public LLUDPZeroEncoder()
  41. {
  42. }
  43. public LLUDPZeroEncoder(byte[] data)
  44. {
  45. m_dest = data;
  46. zerocount = 0;
  47. }
  48. public byte[] Data
  49. {
  50. get
  51. {
  52. return m_dest;
  53. }
  54. set
  55. {
  56. m_dest = value;
  57. }
  58. }
  59. public int ZeroCount
  60. {
  61. get
  62. {
  63. return zerocount;
  64. }
  65. set
  66. {
  67. zerocount = value;
  68. }
  69. }
  70. public int Position
  71. {
  72. get
  73. {
  74. return pos;
  75. }
  76. set
  77. {
  78. pos = value;
  79. }
  80. }
  81. public unsafe void AddZeros(int len)
  82. {
  83. zerocount += len;
  84. while (zerocount > 255)
  85. {
  86. m_dest[pos++] = 0x00;
  87. m_dest[pos++] = 0xff;
  88. zerocount -= 256;
  89. }
  90. }
  91. public unsafe int Finish()
  92. {
  93. if(zerocount > 0)
  94. {
  95. m_dest[pos++] = 0x00;
  96. m_dest[pos++] = (byte)zerocount;
  97. }
  98. return pos;
  99. }
  100. public unsafe void AddBytes(byte[] src, int srclen)
  101. {
  102. for (int i = 0; i < srclen; ++i)
  103. {
  104. if (src[i] == 0x00)
  105. {
  106. zerocount++;
  107. if (zerocount == 0)
  108. {
  109. m_dest[pos++] = 0x00;
  110. m_dest[pos++] = 0xff;
  111. zerocount++;
  112. }
  113. }
  114. else
  115. {
  116. if (zerocount != 0)
  117. {
  118. m_dest[pos++] = 0x00;
  119. m_dest[pos++] = (byte)zerocount;
  120. zerocount = 0;
  121. }
  122. m_dest[pos++] = src[i];
  123. }
  124. }
  125. }
  126. public unsafe void AddByte(byte v)
  127. {
  128. if (v == 0x00)
  129. {
  130. zerocount++;
  131. if (zerocount == 0)
  132. {
  133. m_dest[pos++] = 0x00;
  134. m_dest[pos++] = 0xff;
  135. zerocount++;
  136. }
  137. }
  138. else
  139. {
  140. if (zerocount != 0)
  141. {
  142. m_dest[pos++] = 0x00;
  143. m_dest[pos++] = (byte)zerocount;
  144. zerocount = 0;
  145. }
  146. m_dest[pos++] = v;
  147. }
  148. }
  149. public void AddInt16(short v)
  150. {
  151. if (v == 0)
  152. AddZeros(2);
  153. else
  154. {
  155. Utils.Int16ToBytes(v, m_tmp, 0);
  156. AddBytes(m_tmp, 2);
  157. }
  158. }
  159. public void AddUInt16(ushort v)
  160. {
  161. if (v == 0)
  162. AddZeros(2);
  163. else
  164. {
  165. Utils.UInt16ToBytes(v, m_tmp, 0);
  166. AddBytes(m_tmp, 2);
  167. }
  168. }
  169. public void AddInt(int v)
  170. {
  171. if (v == 0)
  172. AddZeros(4);
  173. else
  174. {
  175. Utils.IntToBytesSafepos(v, m_tmp, 0);
  176. AddBytes(m_tmp, 4);
  177. }
  178. }
  179. public unsafe void AddUInt(uint v)
  180. {
  181. if (v == 0)
  182. AddZeros(4);
  183. else
  184. {
  185. Utils.UIntToBytesSafepos(v, m_tmp, 0);
  186. AddBytes(m_tmp, 4);
  187. }
  188. }
  189. public void AddFloatToUInt16(float v, float range)
  190. {
  191. Utils.FloatToUInt16Bytes(v, range, m_tmp, 0);
  192. AddBytes(m_tmp, 2);
  193. }
  194. public void AddFloat(float v)
  195. {
  196. if (v == 0f)
  197. AddZeros(4);
  198. else
  199. {
  200. Utils.FloatToBytesSafepos(v, m_tmp, 0);
  201. AddBytes(m_tmp, 4);
  202. }
  203. }
  204. public void AddInt64(long v)
  205. {
  206. if (v == 0)
  207. AddZeros(8);
  208. else
  209. {
  210. Utils.Int64ToBytesSafepos(v, m_tmp, 0);
  211. AddBytes(m_tmp, 8);
  212. }
  213. }
  214. public void AddUInt64(ulong v)
  215. {
  216. if (v == 0)
  217. AddZeros(8);
  218. else
  219. {
  220. Utils.UInt64ToBytesSafepos(v, m_tmp, 0);
  221. AddBytes(m_tmp, 8);
  222. }
  223. }
  224. public void AddVector3(Vector3 v)
  225. {
  226. if (v == Vector3.Zero)
  227. AddZeros(12);
  228. else
  229. {
  230. v.ToBytes(m_tmp, 0);
  231. AddBytes(m_tmp, 12);
  232. }
  233. }
  234. public void AddVector4(Vector4 v)
  235. {
  236. if (v == Vector4.Zero)
  237. AddZeros(16);
  238. else
  239. {
  240. v.ToBytes(m_tmp, 0);
  241. AddBytes(m_tmp, 16);
  242. }
  243. }
  244. public void AddNormQuat(Quaternion v)
  245. {
  246. v.ToBytes(m_tmp, 0);
  247. AddBytes(m_tmp, 12);
  248. }
  249. public void AddUUID(UUID v)
  250. {
  251. v.ToBytes(m_tmp, 0);
  252. AddBytes(m_tmp, 16);
  253. }
  254. // maxlen <= 255 and includes null termination byte
  255. public void AddShortString(string str, int maxlen)
  256. {
  257. if (String.IsNullOrEmpty(str))
  258. {
  259. AddZeros(1);
  260. return;
  261. }
  262. --maxlen; // account for null term
  263. bool NullTerm = str.EndsWith("\0");
  264. byte[] data = Util.UTF8.GetBytes(str);
  265. int len = data.Length;
  266. if(NullTerm)
  267. --len;
  268. if(len <= maxlen)
  269. {
  270. AddByte((byte)(len + 1));
  271. AddBytes(data, len);
  272. AddZeros(1);
  273. return;
  274. }
  275. if ((data[maxlen] & 0x80) != 0)
  276. {
  277. while (maxlen > 0 && (data[maxlen] & 0xc0) != 0xc0)
  278. maxlen--;
  279. }
  280. AddByte((byte)(maxlen + 1));
  281. AddBytes(data, maxlen);
  282. AddZeros(1);
  283. }
  284. // maxlen <= 255 and includes null termination byte, maxchars == max len of utf8 source
  285. public void AddShortString(string str, int maxchars, int maxlen)
  286. {
  287. if (String.IsNullOrEmpty(str))
  288. {
  289. AddZeros(1);
  290. return;
  291. }
  292. --maxlen; // account for null term
  293. bool NullTerm = false;
  294. byte[] data;
  295. if (str.Length > maxchars)
  296. {
  297. data = Util.UTF8.GetBytes(str.Substring(0,maxchars));
  298. }
  299. else
  300. {
  301. NullTerm = str.EndsWith("\0");
  302. data = Util.UTF8.GetBytes(str);
  303. }
  304. int len = data.Length;
  305. if (NullTerm)
  306. --len;
  307. if (len <= maxlen)
  308. {
  309. AddByte((byte)(len + 1));
  310. AddBytes(data, len);
  311. AddZeros(1);
  312. return;
  313. }
  314. if ((data[maxlen] & 0x80) != 0)
  315. {
  316. while (maxlen > 0 && (data[maxlen] & 0xc0) != 0xc0)
  317. maxlen--;
  318. }
  319. AddByte((byte)(maxlen + 1));
  320. AddBytes(data, maxlen);
  321. AddZeros(1);
  322. }
  323. }
  324. }