LLUDPZeroEncoder.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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 AddBytes(byte* src, int srclen)
  127. {
  128. for (int i = 0; i < srclen; ++i)
  129. {
  130. if (src[i] == 0x00)
  131. {
  132. zerocount++;
  133. if (zerocount == 0)
  134. {
  135. m_dest[pos++] = 0x00;
  136. m_dest[pos++] = 0xff;
  137. zerocount++;
  138. }
  139. }
  140. else
  141. {
  142. if (zerocount != 0)
  143. {
  144. m_dest[pos++] = 0x00;
  145. m_dest[pos++] = (byte)zerocount;
  146. zerocount = 0;
  147. }
  148. m_dest[pos++] = src[i];
  149. }
  150. }
  151. }
  152. public unsafe void AddByte(byte v)
  153. {
  154. if (v == 0x00)
  155. {
  156. zerocount++;
  157. if (zerocount == 0)
  158. {
  159. m_dest[pos++] = 0x00;
  160. m_dest[pos++] = 0xff;
  161. zerocount++;
  162. }
  163. }
  164. else
  165. {
  166. if (zerocount != 0)
  167. {
  168. m_dest[pos++] = 0x00;
  169. m_dest[pos++] = (byte)zerocount;
  170. zerocount = 0;
  171. }
  172. m_dest[pos++] = v;
  173. }
  174. }
  175. public void AddInt16(short v)
  176. {
  177. if (v == 0)
  178. AddZeros(2);
  179. else
  180. {
  181. Utils.Int16ToBytes(v, m_tmp, 0);
  182. AddBytes(m_tmp, 2);
  183. }
  184. }
  185. public void AddUInt16(ushort v)
  186. {
  187. if (v == 0)
  188. AddZeros(2);
  189. else
  190. {
  191. Utils.UInt16ToBytes(v, m_tmp, 0);
  192. AddBytes(m_tmp, 2);
  193. }
  194. }
  195. public void AddInt(int v)
  196. {
  197. if (v == 0)
  198. AddZeros(4);
  199. else
  200. {
  201. Utils.IntToBytesSafepos(v, m_tmp, 0);
  202. AddBytes(m_tmp, 4);
  203. }
  204. }
  205. public unsafe void AddUInt(uint v)
  206. {
  207. if (v == 0)
  208. AddZeros(4);
  209. else
  210. {
  211. Utils.UIntToBytesSafepos(v, m_tmp, 0);
  212. AddBytes(m_tmp, 4);
  213. }
  214. }
  215. public void AddFloatToUInt16(float v, float range)
  216. {
  217. Utils.FloatToUInt16Bytes(v, range, m_tmp, 0);
  218. AddBytes(m_tmp, 2);
  219. }
  220. public void AddFloat(float v)
  221. {
  222. if (v == 0f)
  223. AddZeros(4);
  224. else
  225. {
  226. Utils.FloatToBytesSafepos(v, m_tmp, 0);
  227. AddBytes(m_tmp, 4);
  228. }
  229. }
  230. public void AddInt64(long v)
  231. {
  232. if (v == 0)
  233. AddZeros(8);
  234. else
  235. {
  236. Utils.Int64ToBytesSafepos(v, m_tmp, 0);
  237. AddBytes(m_tmp, 8);
  238. }
  239. }
  240. public void AddUInt64(ulong v)
  241. {
  242. if (v == 0)
  243. AddZeros(8);
  244. else
  245. {
  246. Utils.UInt64ToBytesSafepos(v, m_tmp, 0);
  247. AddBytes(m_tmp, 8);
  248. }
  249. }
  250. public void AddVector3(Vector3 v)
  251. {
  252. if (v == Vector3.Zero)
  253. AddZeros(12);
  254. else
  255. {
  256. v.ToBytes(m_tmp, 0);
  257. AddBytes(m_tmp, 12);
  258. }
  259. }
  260. public void AddVector4(Vector4 v)
  261. {
  262. if (v == Vector4.Zero)
  263. AddZeros(16);
  264. else
  265. {
  266. v.ToBytes(m_tmp, 0);
  267. AddBytes(m_tmp, 16);
  268. }
  269. }
  270. public void AddNormQuat(Quaternion v)
  271. {
  272. v.ToBytes(m_tmp, 0);
  273. AddBytes(m_tmp, 12);
  274. }
  275. public void AddUUID(UUID v)
  276. {
  277. v.ToBytes(m_tmp, 0);
  278. AddBytes(m_tmp, 16);
  279. }
  280. // maxlen <= 255 and includes null termination byte
  281. public unsafe void AddShortString(string str, int maxlen)
  282. {
  283. if (String.IsNullOrEmpty(str))
  284. {
  285. AddZeros(1);
  286. return;
  287. }
  288. byte* data = stackalloc byte[maxlen];
  289. int len = Util.osUTF8Getbytes(str, data, maxlen, true);
  290. if (len == 0)
  291. {
  292. AddZeros(1);
  293. return;
  294. }
  295. AddByte((byte)(len));
  296. AddBytes(data, len);
  297. }
  298. // maxlen <= 255 and includes null termination byte, maxchars == max len of utf16 source
  299. public unsafe void AddShortString(string str, int maxchars, int maxlen)
  300. {
  301. if (String.IsNullOrEmpty(str))
  302. {
  303. AddZeros(1);
  304. return;
  305. }
  306. if (str.Length > maxchars)
  307. str = str.Substring(0, maxchars);
  308. byte* data = stackalloc byte[maxlen];
  309. int len = Util.osUTF8Getbytes(str, data, maxlen, true);
  310. if (len == 0)
  311. {
  312. AddZeros(1);
  313. return;
  314. }
  315. AddByte((byte)(len));
  316. AddBytes(data, len);
  317. }
  318. // maxlen <= 254 because null termination byte
  319. public unsafe void AddShortLimitedUTF8(osUTF8 str)
  320. {
  321. if(str == null)
  322. {
  323. AddZeros(1);
  324. return;
  325. }
  326. int len = str.Length;
  327. if (len == 0)
  328. {
  329. AddZeros(1);
  330. return;
  331. }
  332. AddByte((byte)(len + 1)); // add null
  333. AddBytes(str.GetArray(), len);
  334. AddZeros(1);
  335. }
  336. }
  337. }