从raknet上剥下来的

比较适用于前后端通讯,可以对BitStream进行二次封装,方便使用.

BitStream.h:

  1. #ifndef __BITSTREAM_H
  2. #define __BITSTREAM_H
  3.  
  4. #ifdef _WIN32
  5. #if defined (_INTEGRAL_MAX_BITS) && _INTEGRAL_MAX_BITS >= 64
  6. typedef signed __int64 int64_t;
  7. typedef unsigned __int64 uint64_t;
  8. #define HAS_INT64
  9. #endif
  10. #else
  11. #include <stdint.h>
  12. #define HAS_INT64
  13.  
  14. #endif
  15.  
  16. #include <string>
  17. #include "share.h"
  18.  
  19. // Arbitrary size, just picking something likely to be larger than most packets
  20. #define BITSTREAM_STACK_ALLOCATION_SIZE 1024*2
  21.  
  22. /** \note If you want the default network byte stream to be
  23. in Network Byte Order (Big Endian) then #define __BITSTREAM_BIG_END
  24. otherwise the default is 'Little Endian'. If your CPU has the same
  25. Byte Order as your network stream, you can cut out some overheads
  26. using #define __BITSTREAM_NATIVE_END --- if this is defined,
  27. the __BITSTREAM_BIG_END flag becomes ineffective.
  28. */
  29.  
  30. namespace dhpnet
  31. {
  32. /**
  33. * This macro transforms a bit in byte
  34. * @param x Transform a bit to a byte
  35. */
  36. #define BITS_TO_BYTES(x) (((x)+7)>>3)
  37.  
  38. #define BYTES_TO_BITS(x) (x<<3)
  39.  
  40. /**
  41. * @brief Packets encoding and decoding facilities
  42. *
  43. * Helper class to encode and decode packets.
  44. *
  45. */
  46.  
  47. class BitStream
  48. {
  49.  
  50. public:
  51. /**
  52. * Default Constructor
  53. */
  54. BitStream();
  55. /**
  56. * Preallocate some memory for the construction of the packet
  57. * @param initialBytesToAllocate the amount of byte to pre-allocate.
  58. */
  59. BitStream( int initialBytesToAllocate );
  60.  
  61. /**
  62. * Initialize the BitStream object using data from the network.
  63. * Set _copyData to true if you want to make an internal copy of
  64. * the data you are passing. You can then Write and do all other
  65. * operations Set it to false if you want to just use a pointer to
  66. * the data you are passing, in order to save memory and speed.
  67. * You should only then do read operations.
  68. * @param _data An array of bytes.
  69. * @param lengthInBytes Size of the @em _data.
  70. * @param _copyData Does a copy of the input data.
  71. */
  72. BitStream( unsigned char* _data, unsigned int lengthInBytes, bool _copyData );
  73. //
  74. BitStream( unsigned char* _data, unsigned int lengthInBytes, unsigned int datasize);
  75. /**
  76. * Destructor
  77. */
  78. ~BitStream();
  79. /**
  80. * Reset the bitstream for reuse
  81. */
  82. void Reset( void );
  83. void SetBuffer(char* _data, unsigned int lengthInBytes, unsigned int datasize);
  84. void ClearBuffer();
  85. //
  86.  
  87. /**
  88. * Write the native types to the end of the buffer
  89. * without any compression mecanism.
  90. * @param input The data
  91. */
  92. void WriteBool( const bool input );
  93. /**
  94. * Write the native types to the end of the buffer
  95. * without any compression mecanism.
  96. * @param input The data
  97. */
  98. void WriteUInt8( const uint8 input );
  99. //
  100.  
  101. /**
  102. * Write the native types to the end of the buffer
  103. * without any compression mecanism.
  104. * @param input The data
  105. */
  106. void WriteInt8( const int8 input );
  107. /**
  108. * Write the native types to the end of the buffer
  109. * without any compression mecanism.
  110. * @param input The data
  111. */
  112. void WriteUInt16( const uint16 input );
  113. /**
  114. * Write the native types to the end of the buffer
  115. * without any compression mecanism.
  116. * @param input The data
  117. */
  118. void WriteInt16( const int16 input );
  119. /**
  120. * Write the native types to the end of the buffer
  121. * without any compression mecanism.
  122. * @param input The data
  123. */
  124. void WriteUInt32( const uint32 input );
  125. /**
  126. * Write the native types to the end of the buffer
  127. * without any compression mecanism.
  128. * @param input The data
  129. */
  130. void WriteInt32( const int32 input );
  131.  
  132. #ifdef HAS_INT64
  133. /**
  134. * Write the native types to the end of the buffer
  135. * without any compression mecanism.
  136. * @param input The data
  137. */
  138. void WriteUInt64( const uint64 input );
  139. /**
  140. * Write the native types to the end of the buffer
  141. * without any compression mecanism.
  142. * @param input The data
  143. */
  144. void WriteInt64( const int64 input );
  145. #endif
  146.  
  147. /**
  148. * Write the native types to the end of the buffer
  149. * without any compression mecanism.
  150. * @param input The data
  151. */
  152. void WriteFloat( const float input );
  153. /**
  154. * Write the native types to the end of the buffer
  155. * without any compression mechanism.
  156. * @param input The data
  157. */
  158. void WriteDouble( const double input );
  159. /**
  160. * Write an array or casted stream. It is supposed to
  161. * be raw data. It is also not possible to deal with endian problem
  162. * @param input a byte buffer
  163. * @param numberOfBytes the size of the byte buffer
  164. */
  165. void WriteBytes( const char* input, const int numberOfBytes );
  166. /**
  167. * write multi bytes string
  168. * @param input
  169. */
  170. void WriteStr(char input[]);
  171. /**
  172. * write standard string
  173. * @param input
  174. */
  175. void WriteStr( const std::string& input );
  176. /**
  177. * Copy from another bitstream
  178. * @bitStream the bitstream to copy from
  179. */
  180. void WriteBS( const BitStream *bitStream );
  181.  
  182. /**
  183. * Write the native types with simple compression.
  184. * Best used with negatives and positives close to 0
  185. * @param input The data.
  186. */
  187. void WriteCompUInt8( const uint8 input );
  188. /**
  189. * Write the native types with simple compression.
  190. * Best used with negatives and positives close to 0
  191. * @param input The data.
  192. */
  193. void WriteCompInt8( const int8 input );
  194. /**
  195. * Write the native types with simple compression.
  196. * Best used with negatives and positives close to 0
  197. * @param input The data.
  198. */
  199. void WriteCompUInt16( const uint16 input );
  200. /**
  201. * Write the native types with simple compression.
  202. * Best used with negatives and positives close to 0
  203. * @param input The data.
  204. */
  205. void WriteCompInt16( const int16 input );
  206. /**
  207. * Write the native types with simple compression.
  208. * Best used with negatives and positives close to 0
  209. * @param input The data.
  210. */
  211. void WriteCompUInt32( const uint32 input );
  212. /**
  213. * Write the native types with simple compression.
  214. * Best used with negatives and positives close to 0
  215. * @param input The data.
  216. */
  217. void WriteCompInt32( const int32 input );
  218.  
  219. #ifdef HAS_INT64
  220. /**
  221. * Write the native types with simple compression.
  222. * Best used with negatives and positives close to 0
  223. * @param input The data.
  224. */
  225. void WriteCompUInt64( const uint64 input );
  226. /**
  227. * Write the native types with simple compression.
  228. * Best used with negatives and positives close to 0
  229. * @param input The data.
  230. */
  231. void WriteCompInt64( const int64 input );
  232. #endif
  233. /**
  234. * Write the native types with simple compression.
  235. * Best used with negatives and positives close to 0
  236. * @param input The data.
  237. */
  238. void WriteCompFloat( const float input );
  239.  
  240. /**
  241. * Write the native types with simple compression.
  242. * Best used with negatives and positives close to 0
  243. * @param input The data.
  244. */
  245. void WriteCompDouble( const double input );
  246.  
  247. /**
  248. * Write a normalized 3D vector, using (at most) 4 bytes + 3 bits instead of 12 bytes. Will further compress y or z axis aligned vectors.
  249. * Accurate to 1/32767.5.
  250. * @param x x
  251. * @param y y
  252. * @param z z
  253. */
  254. void WriteNormVector( float x, float y, float z );
  255.  
  256. /**
  257. * Write a vector, using 10 bytes instead of 12.
  258. * Loses accuracy to about 3/10ths and only saves 2 bytes, so only use if accuracy is not important.
  259. * @param x x
  260. * @param y y
  261. * @param z z
  262. */
  263. void WriteVector( float x, float y, float z );
  264.  
  265. /**
  266. * Write a normalized quaternion in 6 bytes + 4 bits instead of 16 bytes. Slightly lossy.
  267. * @param w w
  268. * @param x x
  269. * @param y y
  270. * @param z z
  271. */
  272. void WriteNormQuat( float w, float x, float y, float z);
  273.  
  274. /**
  275. * Write an orthogonal matrix by creating a quaternion, and writing 3 components of the quaternion in 2 bytes each
  276. * for 6 bytes instead of 36
  277. */
  278. void WriteOrthMatrix(
  279. float m00, float m01, float m02,
  280. float m10, float m11, float m12,
  281. float m20, float m21, float m22 );
  282. /**
  283. * Read the native types from the front of the buffer
  284. * @param output The readed value.
  285. * @return true on success false otherwise. The result of a reading
  286. * can only be wrong in the case we reach the end of the BitStream
  287. * with some missing bits.
  288. */
  289. bool ReadBool();
  290. /**
  291. * Read the native types from the front of the buffer
  292. * @param output The readed value.
  293. * @return true on success false otherwise. The result of a reading
  294. * can only be wrong in the case we reach the end of the BitStream
  295. * with some missing bits.
  296. */
  297. uint8 ReadUInt8();
  298. /**
  299. * Read the native types from the front of the buffer
  300. * @param output The readed value.
  301. * @return true on success false otherwise. The result of a reading
  302. * can only be wrong in the case we reach the end of the BitStream
  303. * with some missing bits.
  304. */
  305. int8 ReadInt8();
  306. /**
  307. * Read the native types from the front of the buffer
  308. * @param output The readed value.
  309. * @return true on success false otherwise. The result of a reading
  310. * can only be wrong in the case we reach the end of the BitStream
  311. * with some missing bits.
  312. */
  313. uint16 ReadUInt16();
  314. /**
  315. * Read the native types from the front of the buffer
  316. * @param output The readed value.
  317. * @return true on success false otherwise. The result of a reading
  318. * can only be wrong in the case we reach the end of the BitStream
  319. * with some missing bits.
  320. */
  321. int16 ReadInt16();
  322. /**
  323. * Read the native types from the front of the buffer
  324. * @param output The readed value.
  325. * @return true on success false otherwise. The result of a reading
  326. * can only be wrong in the case we reach the end of the BitStream
  327. * with some missing bits.
  328. */
  329. uint32 ReadUInt32();
  330. /**
  331. * Read the native types from the front of the buffer
  332. * @param output The readed value.
  333. * @return true on success false otherwise. The result of a reading
  334. * can only be wrong in the case we reach the end of the BitStream
  335. * with some missing bits.
  336. */
  337. int32 ReadInt32();
  338.  
  339. #ifdef HAS_INT64
  340. /**
  341. * Read the native types from the front of the buffer
  342. * @param output The readed value.
  343. * @return true on success false otherwise. The result of a reading
  344. * can only be wrong in the case we reach the end of the BitStream
  345. * with some missing bits.
  346. */
  347. uint64 ReadUInt64();
  348. /**
  349. * Read the native types from the front of the buffer
  350. * @param output The readed value.
  351. * @return true on success false otherwise. The result of a reading
  352. * can only be wrong in the case we reach the end of the BitStream
  353. * with some missing bits.
  354. */
  355. int64 ReadInt64();
  356. #endif
  357. /**
  358. * Read the native types from the front of the buffer
  359. * @param output The readed value.
  360. * @return true on success false otherwise. The result of a reading
  361. * can only be wrong in the case we reach the end of the BitStream
  362. * with some missing bits.
  363. */
  364. float ReadFloat();
  365. /**
  366. * Read the native types from the front of the buffer
  367. * @param output The readed value.
  368. * @return true on success false otherwise. The result of a reading
  369. * can only be wrong in the case we reach the end of the BitStream
  370. * with some missing bits.
  371. */
  372. double ReadDouble();
  373. /**
  374. * Read an array or casted stream of byte. The array
  375. * is raw data. There is no automatic conversion on
  376. * big endian arch
  377. * @param output The result byte array. It should be larger than @em numberOfBytes.
  378. * @param numberOfBytes The number of byte to read
  379. * @return true on success false if there is some missing bytes.
  380. */
  381. bool ReadBytes( char* output, const int numberOfBytes );
  382. /**
  383. * Read multi bytes string
  384. * @return
  385. */
  386. bool ReadStr(char output[], int size);
  387. /**
  388. * Read standard string
  389. * @return
  390. */
  391. std::string ReadStr();
  392. /**
  393. * Read the types you wrote with WriteCompressed
  394. * @param output The read value
  395. * @return true on success, false on not enough data to read
  396. */
  397. uint8 ReadCompUInt8();
  398. /**
  399. * Read the types you wrote with WriteCompressed
  400. * @param output The read value
  401. * @return true on success, false on not enough data to read
  402. */
  403. int8 ReadCompInt8();
  404. /**
  405. * Read the types you wrote with WriteCompressed
  406. * @param output The read value
  407. * @return true on success, false on not enough data to read
  408. */
  409. uint16 ReadCompUInt16();
  410. /**
  411. * Read the types you wrote with WriteCompressed
  412. * @param output The read value
  413. * @return true on success, false on not enough data to read
  414. */
  415. int16 ReadCompInt16();
  416. /**
  417. * Read the types you wrote with WriteCompressed
  418. * @param output The read value
  419. * @return true on success, false on not enough data to read
  420. */
  421. uint32 ReadCompUInt32();
  422. /**
  423. * Read the types you wrote with WriteCompressed
  424. * @param output The read value
  425. * @return true on success, false on not enough data to read
  426. */
  427. int32 ReadCompInt32();
  428.  
  429. #ifdef HAS_INT64
  430. /**
  431. * Read the types you wrote with WriteCompressed
  432. * @param output The read value
  433. * @return true on success, false on not enough data to read
  434. */
  435. uint64 ReadCompUInt64();
  436. /**
  437. * Read the types you wrote with WriteCompressed
  438. * @param output The read value
  439. * @return true on success, false on not enough data to read
  440. */
  441. int64 ReadCompInt64();
  442. #endif
  443. /**
  444. * Read the types you wrote with WriteCompressed
  445. * @param output The read value
  446. * @return true on success, false on not enough data to read
  447. */
  448. float ReadCompFloat();
  449.  
  450. /**
  451. * Read the types you wrote with WriteCompressed
  452. * @param output The read value
  453. * @return true on success, false on not enough data to read
  454. */
  455. double ReadCompDouble();
  456.  
  457. /**
  458. * Read a normalized 3D vector, using (at most) 4 bytes + 3 bits instead of 12 bytes. Will further compress y or z axis aligned vectors.
  459. * Accurate to 1/32767.5.
  460. * @param x x
  461. * @param y y
  462. * @param z z
  463. */
  464. bool ReadNormVector( float &x, float &y, float &z );
  465.  
  466. /**
  467. * Read 3 floats, using 10 bytes, where those floats comprise a vector
  468. * Loses accuracy to about 3/10ths and only saves 2 bytes, so only use if accuracy is not important.
  469. * @param x x
  470. * @param y y
  471. * @param z z
  472. */
  473. bool ReadVector( float &x, float &y, float &z );
  474. /**
  475. * Read a normalized quaternion in 6 bytes + 4 bits instead of 16 bytes. Slightly lossy.
  476. * @param w w
  477. * @param x x
  478. * @param y y
  479. * @param z z
  480. */
  481. bool ReadNormQuat( float &w, float &x, float &y, float &z);
  482. /**
  483. * Read an orthogonal matrix from a quaternion, reading 3 components of the quaternion in 2 bytes each and extrapolatig the 4th.
  484. * for 6 bytes instead of 36
  485. */
  486. bool ReadOrthMatrix(
  487. float &m00, float &m01, float &m02,
  488. float &m10, float &m11, float &m12,
  489. float &m20, float &m21, float &m22 );
  490.  
  491. /**
  492. * Sets the read pointer back to the beginning of your data.
  493. */
  494. void ResetReadPointer( void );
  495. /**
  496. * Sets the write pointer back to the beginning of your data.
  497. */
  498. void ResetWritePointer( void );
  499. /**
  500. * This is good to call when you are done with the stream to make
  501. * sure you didn't leave any data left over void
  502. */
  503. void AssertStreamEmpty( void );
  504. /**
  505. * print to the standard output the state of the stream bit by bit
  506. */
  507. void PrintBits( void ) const;
  508.  
  509. /**
  510. * Ignore data we don't intend to read
  511. * @param numberOfBits The number of bits to ignore
  512. */
  513. void IgnoreBits( const int numberOfBits );
  514.  
  515. /**
  516. * Move the write pointer to a position on the array.
  517. * @param offset the offset from the start of the array.
  518. * @attention
  519. * Dangerous if you don't know what you are doing!
  520. *
  521. */
  522. void SetWriteOffset( const int offset );
  523. /**
  524. * Returns the length in bits of the stream
  525. */
  526. int GetWriteOffset( void ) const;
  527. /**
  528. * Returns the length in bytes of the stream
  529. */
  530. int GetNumberOfBytesUsed( void ) const;
  531.  
  532. int GetNumberOfBytesRead(void)const;
  533.  
  534. /**
  535. * Move the read pointer to a position on the array.
  536. * @param offset
  537. */
  538. void SetReadOffset( const int offset );
  539.  
  540. void SetByteReadOffSet(const int offset);
  541. /**
  542. * Returns the number of bits into the stream that we have read
  543. */
  544. int GetReadOffset( void ) const;
  545.  
  546. /**
  547. * Returns the number of bits left in the stream that haven't been read
  548. */
  549. int GetNumberOfUnreadBits( void ) const;
  550. /**
  551. * Makes a copy of the internal data for you Data will point to
  552. * the stream. Returns the length in bits of the stream. Partial
  553. * bytes are left aligned
  554. * @param _data the resulting byte copy of the internal state.
  555. */
  556. int CopyData( unsigned char** _data ) const;
  557. /**
  558. * Set the stream to some initial data. For internal use
  559. * Partial bytes are left aligned
  560. * @param input The data
  561. * @param numberOfBits the number of bits set in the data buffer
  562. */
  563. void SetData( const unsigned char* input, const int numberOfBits );
  564. /**
  565. * Exposes the internal data.
  566. * Partial bytes are left aligned.
  567. * @return A pointer to the internal state
  568. */
  569. unsigned char* GetData( void ) const;
  570. /**
  571. * Write numberToWrite bits from the input source Right aligned
  572. * data means in the case of a partial byte, the bits are aligned
  573. * from the right (bit 0) rather than the left (as in the normal
  574. * internal representation) You would set this to true when
  575. * writing user data, and false when copying bitstream data, such
  576. * as writing one bitstream to another
  577. * @param input The data
  578. * @param numberOfBitsToWrite The number of bits to write
  579. * @param rightAlignedBits if true data will be right aligned
  580. */
  581. void WriteBits( const unsigned char* input,
  582. int numberOfBitsToWrite, const bool rightAlignedBits = true );
  583. /**
  584. * Align the bitstream to the byte boundary and then write the
  585. * specified number of bits. This is faster than WriteBits but
  586. * wastes the bits to do the alignment and requires you to call
  587. * ReadAlignedBits at the corresponding read position.
  588. * @param input The data
  589. * @param numberOfBytesToWrite The size of data.
  590. */
  591. void WriteAlignedBytes( const unsigned char* input,
  592. const int numberOfBytesToWrite );
  593. /**
  594. * Read bits, starting at the next aligned bits. Note that the
  595. * modulus 8 starting offset of the sequence must be the same as
  596. * was used with WriteBits. This will be a problem with packet
  597. * coalescence unless you byte align the coalesced packets.
  598. * @param output The byte array larger than @em numberOfBytesToRead
  599. * @param numberOfBytesToRead The number of byte to read from the internal state
  600. * @return true if there is enough byte.
  601. */
  602. bool ReadAlignedBytes( unsigned char* output,
  603. const int numberOfBytesToRead );
  604. /**
  605. * Align the next write and/or read to a byte boundary. This can
  606. * be used to 'waste' bits to byte align for efficiency reasons It
  607. * can also be used to force coalesced bitstreams to start on byte
  608. * boundaries so so WriteAlignedBits and ReadAlignedBits both
  609. * calculate the same offset when aligning.
  610. */
  611. void AlignWriteToByteBoundary( void );
  612. /**
  613. * Align the next write and/or read to a byte boundary. This can
  614. * be used to 'waste' bits to byte align for efficiency reasons It
  615. * can also be used to force coalesced bitstreams to start on byte
  616. * boundaries so so WriteAlignedBits and ReadAlignedBits both
  617. * calculate the same offset when aligning.
  618. */
  619. void AlignReadToByteBoundary( void );
  620.  
  621. /**
  622. * Read numberOfBitsToRead bits to the output source
  623. * alignBitsToRight should be set to true to convert internal
  624. * bitstream data to userdata It should be false if you used
  625. * WriteBits with rightAlignedBits false
  626. * @param output The resulting bits array
  627. * @param numberOfBitsToRead The number of bits to read
  628. * @param alignsBitsToRight if true bits will be right aligned.
  629. * @return true if there is enough bits to read
  630. */
  631. bool ReadBits( unsigned char* output, int numberOfBitsToRead,
  632. const bool alignBitsToRight = true );
  633.  
  634. /**
  635. * --- Low level functions ---
  636. * These are for when you want to deal
  637. * with bits and don't care about type checking
  638. * Write a 0
  639. */
  640. void Write0( void );
  641. /**
  642. * --- Low level functions ---
  643. * These are for when you want to deal
  644. * with bits and don't care about type checking
  645. * Write a 1
  646. */
  647. void Write1( void );
  648. /**
  649. * --- Low level functions ---
  650. * These are for when you want to deal
  651. * with bits and don't care about type checking
  652. * Reads 1 bit and returns true if that bit is 1 and false if it is 0
  653. */
  654. bool ReadBit( void );
  655. /**
  656. * If we used the constructor version with copy data off, this
  657. * makes sure it is set to on and the data pointed to is copied.
  658. */
  659. void AssertCopyData( void );
  660. /**
  661. * Use this if you pass a pointer copy to the constructor
  662. * (_copyData==false) and want to overallocate to prevent
  663. * reallocation
  664. */
  665. void SetNumberOfBitsAllocated( const unsigned int lengthInBits );
  666.  
  667. private:
  668. /**
  669. * Assume the input source points to a native type, compress and write it.
  670. */
  671. void WriteCompressed( const unsigned char* input,
  672. const int size, const bool unsignedData );
  673.  
  674. /**
  675. * Assume the input source points to a compressed native type.
  676. * Decompress and read it.
  677. */
  678. bool ReadCompressed( unsigned char* output,
  679. const int size, const bool unsignedData );
  680.  
  681. /**
  682. * Reallocates (if necessary) in preparation of writing
  683. * numberOfBitsToWrite
  684. */
  685. void AddBitsAndReallocate( const int numberOfBitsToWrite );
  686.  
  687. /**
  688. * Number of bits currently used
  689. */
  690. int numberOfBitsUsed;
  691. /**
  692. * Number of bits currently allocated
  693. */
  694. int numberOfBitsAllocated;
  695. /**
  696. * Current readOffset
  697. */
  698. int readOffset;
  699. /**
  700. * array of byte storing the data. Points to stackData or if is bigger than that then is allocated
  701. */
  702. unsigned char *data;
  703. /**
  704. * true if the internal buffer is copy of the data passed to the
  705. * constructor
  706. */
  707. bool copyData;
  708.  
  709. unsigned char stackData[BITSTREAM_STACK_ALLOCATION_SIZE];
  710.  
  711. };
  712. }
  713.  
  714. #endif

BitSteam.cpp:

  1. // This should be on by default for speed. Turn it off if you actually need endian swapping
  2. #define __BITSTREAM_BIG_END
  3.  
  4. #include "BitStream.h"
  5. #include <math.h>
  6. #include <assert.h>
  7. #include <string.h>
  8. #ifdef _WIN32
  9. #include <stdlib.h>
  10. #include <memory.h>
  11. #include <stdio.h>
  12. #include <float.h>
  13. #else
  14. #define _copysign copysign
  15. #endif
  16.  
  17. #if defined ( __APPLE__ ) || defined ( __APPLE_CC__ )
  18. #include <malloc/malloc.h>
  19. #else
  20. #include <malloc.h>
  21. #include <string>
  22. #endif
  23.  
  24. #ifdef __BITSTREAM_BIG_END
  25. // Set up the read/write routines to produce Big-End network streams.
  26. #define B16_1 0
  27. #define B16_0 1
  28.  
  29. #define B32_3 0
  30. #define B32_2 1
  31. #define B32_1 2
  32. #define B32_0 3
  33.  
  34. #define B64_7 0
  35. #define B64_6 1
  36. #define B64_5 2
  37. #define B64_4 3
  38. #define B64_3 4
  39. #define B64_2 5
  40. #define B64_1 6
  41. #define B64_0 7
  42.  
  43. #else
  44. // Default to producing Little-End network streams.
  45. #define B16_1 1
  46. #define B16_0 0
  47.  
  48. #define B32_3 3
  49. #define B32_2 2
  50. #define B32_1 1
  51. #define B32_0 0
  52.  
  53. #define B64_7 7
  54. #define B64_6 6
  55. #define B64_5 5
  56. #define B64_4 4
  57. #define B64_3 3
  58. #define B64_2 2
  59. #define B64_1 1
  60. #define B64_0 0
  61. #endif
  62.  
  63. using namespace dhpnet;
  64.  
  65. BitStream::BitStream()
  66. {
  67. numberOfBitsUsed = ;
  68. //numberOfBitsAllocated = 32 * 8;
  69. numberOfBitsAllocated = BITSTREAM_STACK_ALLOCATION_SIZE * ;
  70. readOffset = ;
  71. //data = ( unsigned char* ) malloc( 32 );
  72. data = ( unsigned char* ) stackData;
  73.  
  74. #ifdef _DEBUG
  75. // assert( data );
  76. #endif
  77. //memset(data, 0, 32);
  78. copyData = true;
  79. }
  80.  
  81. BitStream::BitStream( int initialBytesToAllocate )
  82. {
  83. numberOfBitsUsed = ;
  84. readOffset = ;
  85. if (initialBytesToAllocate <= BITSTREAM_STACK_ALLOCATION_SIZE)
  86. {
  87. data = ( unsigned char* ) stackData;
  88. numberOfBitsAllocated = BITSTREAM_STACK_ALLOCATION_SIZE * ;
  89. }
  90. else
  91. {
  92. data = ( unsigned char* ) malloc( initialBytesToAllocate );
  93. numberOfBitsAllocated = initialBytesToAllocate << ;
  94. }
  95. #ifdef _DEBUG
  96. assert( data );
  97. #endif
  98. // memset(data, 0, initialBytesToAllocate);
  99. copyData = true;
  100. }
  101.  
  102. BitStream::BitStream(unsigned char* _data, unsigned int lengthInBytes, bool _copyData)
  103. {
  104. numberOfBitsUsed = lengthInBytes << ;
  105. readOffset = ;
  106. copyData = _copyData;
  107. numberOfBitsAllocated = lengthInBytes << ;
  108.  
  109. if ( copyData )
  110. {
  111. if ( lengthInBytes > )
  112. {
  113. if (lengthInBytes < BITSTREAM_STACK_ALLOCATION_SIZE)
  114. {
  115. data = ( unsigned char* ) stackData;
  116. numberOfBitsAllocated = BITSTREAM_STACK_ALLOCATION_SIZE << ;
  117. }
  118. else
  119. {
  120. data = ( unsigned char* ) malloc( lengthInBytes );
  121. }
  122. #ifdef _DEBUG
  123. assert( data );
  124. #endif
  125. memcpy( data, _data, lengthInBytes );
  126. }
  127. else
  128. data = ;
  129. }
  130. else
  131. {
  132. data = ( unsigned char* ) _data;
  133. numberOfBitsUsed = ;
  134. }
  135. }
  136. BitStream::BitStream(unsigned char* _data, unsigned int lengthInBytes, unsigned int datasize)
  137. {
  138. numberOfBitsUsed = datasize << ;
  139. readOffset = ;
  140. numberOfBitsAllocated = lengthInBytes << ;
  141. data = ( unsigned char* ) _data;
  142. copyData = false;
  143. }
  144. void BitStream::SetBuffer(char* _data, unsigned int lengthInBytes, unsigned int datasize)
  145. {
  146. numberOfBitsUsed = datasize << ;
  147. readOffset = ;
  148. numberOfBitsAllocated = lengthInBytes << ;
  149. data = ( unsigned char* ) _data;
  150. copyData = false;
  151. }
  152. void BitStream::ClearBuffer()
  153. {
  154. numberOfBitsUsed = ;
  155. readOffset = ;
  156. numberOfBitsAllocated = ;
  157. data = NULL;
  158. }
  159. // Use this if you pass a pointer copy to the constructor (_copyData==false) and want to overallocate to prevent reallocation
  160. void BitStream::SetNumberOfBitsAllocated( const unsigned int lengthInBits )
  161. {
  162. #ifdef _DEBUG
  163. assert( lengthInBits >= ( unsigned int ) numberOfBitsAllocated );
  164. #endif
  165. numberOfBitsAllocated = lengthInBits;
  166. }
  167.  
  168. BitStream::~BitStream()
  169. {
  170. if ( copyData && numberOfBitsAllocated > BITSTREAM_STACK_ALLOCATION_SIZE << )
  171. free( data ); // Use realloc and free so we are more efficient than delete and new for resizing
  172. }
  173.  
  174. void BitStream::Reset( void )
  175. {
  176. // Note: Do NOT reallocate memory because BitStream is used
  177. // in places to serialize/deserialize a buffer. Reallocation
  178. // is a dangerous operation (may result in leaks).
  179.  
  180. if ( numberOfBitsUsed > )
  181. {
  182. // memset(data, 0, BITS_TO_BYTES(numberOfBitsUsed));
  183. }
  184.  
  185. // Don't free memory here for speed efficiency
  186. //free(data); // Use realloc and free so we are more efficient than delete and new for resizing
  187. numberOfBitsUsed = ;
  188.  
  189. //numberOfBitsAllocated=8;
  190. readOffset = ;
  191.  
  192. //data=(unsigned char*)malloc(1);
  193. // if (numberOfBitsAllocated>0)
  194. // memset(data, 0, BITS_TO_BYTES(numberOfBitsAllocated));
  195. }
  196.  
  197. // Write the native types to the end of the buffer
  198. void BitStream::WriteBool( const bool input )
  199. {
  200. #ifdef TYPE_CHECKING
  201. unsigned char ID = ;
  202. WriteBits( ( unsigned char* ) & ID, sizeof(unsigned char) * , true );
  203. #endif
  204.  
  205. if ( input )
  206. WriteInt8();
  207. else
  208. WriteInt8();
  209. }
  210.  
  211. void BitStream::WriteUInt8( const uint8 input )
  212. {
  213. #ifdef TYPE_CHECKING
  214. unsigned char ID = ;
  215. WriteBits( ( unsigned char* ) & ID, sizeof(unsigned char) * , true );
  216. #endif
  217.  
  218. WriteBits( ( unsigned char* ) & input, sizeof( input ) * , true );
  219. }
  220.  
  221. void BitStream::WriteInt8( const int8 input )
  222. {
  223. #ifdef TYPE_CHECKING
  224. unsigned char ID = ;
  225. WriteBits( ( unsigned char* ) & ID, sizeof(unsigned char) * , true );
  226. #endif
  227.  
  228. WriteBits( ( unsigned char* ) & input, sizeof( input ) * , true );
  229. }
  230.  
  231. void BitStream::WriteUInt16( const uint16 input )
  232. {
  233. #ifdef TYPE_CHECKING
  234. unsigned char ID = ;
  235. WriteBits( ( unsigned char* ) & ID, sizeof(unsigned char) * , true );
  236. #endif
  237.  
  238. #ifdef __BITSTREAM_NATIVE_END
  239. WriteBits( ( unsigned char* ) & input, sizeof( input ) * , true );
  240. #else
  241. unsigned char uint16w[];
  242. uint16w[B16_1] = (input >> )&(0xff);
  243. uint16w[B16_0] = input&(0xff);
  244.  
  245. WriteBits( uint16w, sizeof( input ) * , true );
  246. #endif
  247. }
  248.  
  249. void BitStream::WriteInt16( const int16 input )
  250. {
  251. #ifdef TYPE_CHECKING
  252. unsigned char ID = ;
  253. WriteBits( ( unsigned char* ) & ID, sizeof(unsigned char) * , true );
  254. #endif
  255.  
  256. #ifdef __BITSTREAM_NATIVE_END
  257. WriteBits( ( unsigned char* ) & input, sizeof( input ) * , true );
  258. #else
  259. unsigned char int16w[];
  260. int16w[B16_1] = (input >> )&(0xff);
  261. int16w[B16_0] = input&(0xff);
  262.  
  263. WriteBits( int16w, sizeof( input ) * , true );
  264. #endif
  265. }
  266.  
  267. void BitStream::WriteUInt32( const uint32 input )
  268. {
  269. #ifdef TYPE_CHECKING
  270. unsigned char ID = ;
  271. WriteBits( ( unsigned char* ) & ID, sizeof(unsigned char) * , true );
  272. #endif
  273.  
  274. #ifdef __BITSTREAM_NATIVE_END
  275. WriteBits( ( unsigned char* ) & input, sizeof( input ) * , true );
  276. #else
  277. unsigned char uint32w[];
  278. uint32w[B32_3] = (input >> )&(0x000000ff);
  279. uint32w[B32_2] = (input >> )&(0x000000ff);
  280. uint32w[B32_1] = (input >> )&(0x000000ff);
  281. uint32w[B32_0] = (input)&(0x000000ff);
  282.  
  283. WriteBits( uint32w, sizeof( input ) * , true );
  284. #endif
  285. }
  286.  
  287. void BitStream::WriteInt32( const int32 input )
  288. {
  289. #ifdef TYPE_CHECKING
  290. unsigned char ID = ;
  291. WriteBits( ( unsigned char* ) & ID, sizeof(unsigned char) * , true );
  292. #endif
  293.  
  294. #ifdef __BITSTREAM_NATIVE_END
  295. WriteBits( ( unsigned char* ) & input, sizeof( input ) * , true );
  296. #else
  297. unsigned char int32w[];
  298. int32w[B32_3] = (input >> )&(0x000000ff);
  299. int32w[B32_2] = (input >> )&(0x000000ff);
  300. int32w[B32_1] = (input >> )&(0x000000ff);
  301. int32w[B32_0] = (input)&(0x000000ff);
  302.  
  303. WriteBits( int32w, sizeof( input ) * , true );
  304. #endif
  305. }
  306.  
  307. #ifdef HAS_INT64
  308. void BitStream::WriteUInt64( const uint64 input )
  309. {
  310. #ifdef TYPE_CHECKING
  311. unsigned char ID = ;
  312. WriteBits( ( unsigned char* ) & ID, sizeof(unsigned char) * , true );
  313. #endif
  314.  
  315. #ifdef __BITSTREAM_NATIVE_END
  316. WriteBits( ( unsigned char* ) & input, sizeof( input ) * , true );
  317. #else
  318. unsigned char uint64w[];
  319. uint64w[B64_7] = (input >> ) & 0xff;
  320. uint64w[B64_6] = (input >> ) & 0xff;
  321. uint64w[B64_5] = (input >> ) & 0xff;
  322. uint64w[B64_4] = (input >> ) & 0xff;
  323. uint64w[B64_3] = (input >> ) & 0xff;
  324. uint64w[B64_2] = (input >> ) & 0xff;
  325. uint64w[B64_1] = (input >> ) & 0xff;
  326. uint64w[B64_0] = input & 0xff;
  327.  
  328. WriteBits( uint64w, sizeof( input ) * , true );
  329. #endif
  330. }
  331.  
  332. void BitStream::WriteInt64( const int64 input )
  333. {
  334. #ifdef TYPE_CHECKING
  335. unsigned char ID = ;
  336. WriteBits( ( unsigned char* ) & ID, sizeof(unsigned char) * , true );
  337. #endif
  338.  
  339. #ifdef __BITSTREAM_NATIVE_END
  340. WriteBits( ( unsigned char* ) & input, sizeof( input ) * , true );
  341. #else
  342. unsigned char int64w[];
  343. int64w[B64_7] = (input >> ) & 0xff;
  344. int64w[B64_6] = (input >> ) & 0xff;
  345. int64w[B64_5] = (input >> ) & 0xff;
  346. int64w[B64_4] = (input >> ) & 0xff;
  347. int64w[B64_3] = (input >> ) & 0xff;
  348. int64w[B64_2] = (input >> ) & 0xff;
  349. int64w[B64_1] = (input >> ) & 0xff;
  350. int64w[B64_0] = input & 0xff;
  351.  
  352. WriteBits( int64w, sizeof( input ) * , true );
  353. #endif
  354. }
  355.  
  356. #endif
  357.  
  358. void BitStream::WriteFloat( const float input )
  359. {
  360. #ifdef TYPE_CHECKING
  361. unsigned char ID = ;
  362. WriteBits( ( unsigned char* ) & ID, sizeof(unsigned char) * , true );
  363. #endif
  364.  
  365. #ifndef __BITSTREAM_NATIVE_END
  366. unsigned int intval = *((unsigned int *)(&input));
  367. WriteUInt32(intval);
  368. #else
  369. WriteBits( ( unsigned char* ) & input, sizeof( input ) * , true );
  370. #endif
  371. }
  372.  
  373. void BitStream::WriteDouble( const double input )
  374. {
  375. #ifdef TYPE_CHECKING
  376. unsigned char ID = ;
  377. WriteBits( ( unsigned char* ) & ID, sizeof(unsigned char) * , true );
  378. #endif
  379.  
  380. #if defined ( __BITSTREAM_NATIVE_END ) || ( ! defined (HAS_INT64) )
  381. WriteBits( ( unsigned char* ) & input, sizeof( input ) * , true );
  382. #else
  383. uint64_t intval = *((uint64_t *)(&input));
  384. WriteUInt64(intval);
  385. #endif
  386. }
  387. // Write an array or casted stream
  388. void BitStream::WriteBytes( const char* input, const int numberOfBytes )
  389. {
  390. #ifdef TYPE_CHECKING
  391. unsigned char ID = ;
  392. WriteBits( ( unsigned char* ) & ID, sizeof(unsigned char) * , true );
  393. WriteBits( ( unsigned char* ) & numberOfBytes, sizeof( int ) * , true );
  394. #endif
  395.  
  396. WriteBits( ( unsigned char* ) input, numberOfBytes * , true );
  397. }
  398.  
  399. void BitStream::WriteStr(char input[])
  400. {
  401. unsigned short len = strlen(input);
  402. WriteUInt16(len);
  403. if(len > ){
  404. WriteBytes(input,len);
  405. }
  406. }
  407.  
  408. void BitStream::WriteStr(const std::string& input)
  409. {
  410. unsigned short len = input.size();
  411. WriteUInt16(len);
  412. if(len > ){
  413. WriteBytes(input.data(),len);
  414. }
  415. }
  416.  
  417. void BitStream::WriteBS( const BitStream *bitStream )
  418. {
  419. WriteBits(bitStream->GetData(), bitStream->GetWriteOffset(), false);
  420. }
  421.  
  422. // Write the native types with simple compression.
  423. // Best used with negatives and positives close to 0
  424. void BitStream::WriteCompUInt8( const uint8 input )
  425. {
  426. #ifdef TYPE_CHECKING
  427. unsigned char ID = ;
  428. WriteBits( ( unsigned char* ) & ID, sizeof(unsigned char) * , true );
  429. #endif
  430.  
  431. WriteCompressed( ( unsigned char* ) & input, sizeof( input ) * , true );
  432. }
  433.  
  434. void BitStream::WriteCompInt8( const int8 input )
  435. {
  436. #ifdef TYPE_CHECKING
  437. unsigned char ID = ;
  438. WriteBits( ( unsigned char* ) & ID, sizeof(unsigned char) * , true );
  439. #endif
  440.  
  441. WriteCompressed( ( unsigned char* ) & input, sizeof( input ) * , false );
  442. }
  443.  
  444. void BitStream::WriteCompUInt16( const uint16 input )
  445. {
  446. #ifdef TYPE_CHECKING
  447. unsigned char ID = ;
  448. WriteBits( ( unsigned char* ) & ID, sizeof(unsigned char) * , true );
  449. #endif
  450.  
  451. #ifdef __BITSTREAM_NATIVE_END
  452. WriteCompressed( ( unsigned char* ) & input, sizeof( input ) * , true );
  453. #else
  454. unsigned char uint16wc[];
  455. uint16wc[B16_1] = (input >> )&(0xff);
  456. uint16wc[B16_0] = input&(0xff);
  457.  
  458. WriteCompressed( uint16wc, sizeof( input ) * , true );
  459. #endif
  460. }
  461.  
  462. void BitStream::WriteCompInt16( const int16 input )
  463. {
  464. #ifdef TYPE_CHECKING
  465. unsigned char ID = ;
  466. WriteBits( ( unsigned char* ) & ID, sizeof(unsigned char) * , true );
  467. #endif
  468.  
  469. #ifdef __BITSTREAM_NATIVE_END
  470. WriteCompressed( ( unsigned char* ) & input, sizeof( input ) * , true );
  471. #else
  472. unsigned char int16wc[];
  473. int16wc[B16_1] = (input >> )&(0xff);
  474. int16wc[B16_0] = input&(0xff);
  475.  
  476. WriteCompressed( int16wc, sizeof( input ) * , false );
  477. #endif
  478. }
  479.  
  480. void BitStream::WriteCompUInt32( const uint32 input )
  481. {
  482. #ifdef TYPE_CHECKING
  483. unsigned char ID = ;
  484. WriteBits( ( unsigned char* ) & ID, sizeof(unsigned char) * , true );
  485. #endif
  486.  
  487. #ifdef __BITSTREAM_NATIVE_END
  488. WriteCompressed( ( unsigned char* ) & input, sizeof( input ) * , true );
  489. #else
  490. unsigned char uint32wc[];
  491. uint32wc[B32_3] = (input >> )&(0x000000ff);
  492. uint32wc[B32_2] = (input >> )&(0x000000ff);
  493. uint32wc[B32_1] = (input >> )&(0x000000ff);
  494. uint32wc[B32_0] = (input)&(0x000000ff);
  495.  
  496. WriteCompressed( uint32wc, sizeof( input ) * , true );
  497. #endif
  498. }
  499.  
  500. void BitStream::WriteCompInt32( const int32 input )
  501. {
  502. #ifdef TYPE_CHECKING
  503. unsigned char ID = ;
  504. WriteBits( ( unsigned char* ) & ID, sizeof(unsigned char) * , true );
  505. #endif
  506.  
  507. #ifdef __BITSTREAM_NATIVE_END
  508. WriteCompressed( ( unsigned char* ) & input, sizeof( input ) * , true );
  509. #else
  510. unsigned char int32wc[];
  511. int32wc[B32_3] = (input >> )&(0x000000ff);
  512. int32wc[B32_2] = (input >> )&(0x000000ff);
  513. int32wc[B32_1] = (input >> )&(0x000000ff);
  514. int32wc[B32_0] = (input)&(0x000000ff);
  515.  
  516. WriteCompressed( int32wc, sizeof( input ) * , false );
  517. #endif
  518. }
  519.  
  520. #ifdef HAS_INT64
  521. void BitStream::WriteCompUInt64( const uint64 input )
  522. {
  523. #ifdef TYPE_CHECKING
  524. unsigned char ID = ;
  525. WriteBits( ( unsigned char* ) & ID, sizeof(unsigned char) * , true );
  526. #endif
  527.  
  528. #ifdef __BITSTREAM_NATIVE_END
  529. WriteCompressed( ( unsigned char* ) & input, sizeof( input ) * , true );
  530. #else
  531. unsigned char uint64wc[];
  532. uint64wc[B64_7] = (input >> ) & 0xff;
  533. uint64wc[B64_6] = (input >> ) & 0xff;
  534. uint64wc[B64_5] = (input >> ) & 0xff;
  535. uint64wc[B64_4] = (input >> ) & 0xff;
  536. uint64wc[B64_3] = (input >> ) & 0xff;
  537. uint64wc[B64_2] = (input >> ) & 0xff;
  538. uint64wc[B64_1] = (input >> ) & 0xff;
  539. uint64wc[B64_0] = input & 0xff;
  540.  
  541. WriteCompressed( uint64wc, sizeof( input ) * , true );
  542. #endif
  543. }
  544.  
  545. void BitStream::WriteCompInt64( const int64 input )
  546. {
  547. #ifdef TYPE_CHECKING
  548. unsigned char ID = ;
  549. WriteBits( ( unsigned char* ) & ID, sizeof(unsigned char) * , true );
  550. #endif
  551.  
  552. #ifdef __BITSTREAM_NATIVE_END
  553. WriteCompressed( ( unsigned char* ) & input, sizeof( input ) * , true );
  554. #else
  555. unsigned char int64wc[];
  556. int64wc[B64_7] = (input >> ) & 0xff;
  557. int64wc[B64_6] = (input >> ) & 0xff;
  558. int64wc[B64_5] = (input >> ) & 0xff;
  559. int64wc[B64_4] = (input >> ) & 0xff;
  560. int64wc[B64_3] = (input >> ) & 0xff;
  561. int64wc[B64_2] = (input >> ) & 0xff;
  562. int64wc[B64_1] = (input >> ) & 0xff;
  563. int64wc[B64_0] = input & 0xff;
  564.  
  565. WriteCompressed( int64wc, sizeof( input ) * , false );
  566. #endif
  567. }
  568. #endif
  569.  
  570. void BitStream::WriteCompFloat( const float input )
  571. {
  572. #ifdef TYPE_CHECKING
  573. unsigned char ID = ;
  574. WriteBits( ( unsigned char* ) & ID, sizeof(unsigned char) * , true );
  575. #endif
  576.  
  577. // Not yet implemented (no compression)
  578. #if defined ( __BITSTREAM_NATIVE_END )
  579. WriteBits( ( unsigned char* ) &input, sizeof( input ) * , true );
  580. #else
  581. WriteFloat( input );
  582. #endif
  583. }
  584.  
  585. void BitStream::WriteCompDouble( const double input )
  586. {
  587. #ifdef TYPE_CHECKING
  588. unsigned char ID = ;
  589. WriteBits( ( unsigned char* ) & ID, sizeof(unsigned char) * , true );
  590. #endif
  591.  
  592. // Not yet implemented (no compression)
  593. #if defined ( __BITSTREAM_NATIVE_END )
  594. WriteBits( ( unsigned char* ) & input, sizeof( input ) * , true );
  595. #else
  596. WriteDouble( input );
  597. #endif
  598. }
  599.  
  600. void BitStream::WriteNormVector( float x, float y, float z )
  601. {
  602. #ifdef _DEBUG
  603. assert(x <= 1.01f && y <= 1.01f && z <= 1.01f && x >= -1.01f && y >= -1.01f && z >= -1.01f);
  604. #endif
  605. if (x>1.0f)
  606. x=1.0f;
  607. if (y>1.0f)
  608. y=1.0f;
  609. if (z>1.0f)
  610. z=1.0f;
  611. if (x<-1.0f)
  612. x=-1.0f;
  613. if (y<-1.0f)
  614. y=-1.0f;
  615. if (z<-1.0f)
  616. z=-1.0f;
  617.  
  618. WriteBool((bool) (x < 0.0f));
  619.  
  620. if (y==0.0f)
  621. WriteBool(true);
  622. else
  623. {
  624. WriteBool(false);
  625. WriteUInt16((unsigned short)((y+1.0f)*32767.5f));
  626. }
  627. if (z==0.0f)
  628. WriteBool(true);
  629. else
  630. {
  631. WriteBool(false);
  632. WriteUInt16((unsigned short)((z+1.0f)*32767.5f));
  633. }
  634. }
  635. void BitStream::WriteVector( float x, float y, float z )
  636. {
  637. float magnitude = sqrtf(x * x + y * y + z * z);
  638. WriteFloat(magnitude);
  639. if (magnitude > 0.0f)
  640. {
  641. WriteUInt16((unsigned short)((x/magnitude+1.0f)*32767.5f));
  642. WriteUInt16((unsigned short)((y/magnitude+1.0f)*32767.5f));
  643. WriteUInt16((unsigned short)((z/magnitude+1.0f)*32767.5f));
  644. }
  645. }
  646. void BitStream::WriteNormQuat( float w, float x, float y, float z)
  647. {
  648. WriteBool((bool)(w<0.0f));
  649. WriteBool((bool)(x<0.0f));
  650. WriteBool((bool)(y<0.0f));
  651. WriteBool((bool)(z<0.0f));
  652. WriteUInt16((unsigned short)(fabs(x)*65535.0));
  653. WriteUInt16((unsigned short)(fabs(y)*65535.0));
  654. WriteUInt16((unsigned short)(fabs(z)*65535.0));
  655. // Leave out w and calcuate it on the target
  656. }
  657. void BitStream::WriteOrthMatrix(
  658. float m00, float m01, float m02,
  659. float m10, float m11, float m12,
  660. float m20, float m21, float m22 )
  661. {
  662. double qw;
  663. double qx;
  664. double qy;
  665. double qz;
  666.  
  667. // Convert matrix to quat
  668. // http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/
  669. qw = sqrt( + m00 + m11 + m22 ) / ;
  670. qx = sqrt( + m00 - m11 - m22 ) / ;
  671. qy = sqrt( - m00 + m11 - m22 ) / ;
  672. qz = sqrt( - m00 - m11 + m22 ) / ;
  673. if (qw < 0.0) qw=0.0;
  674. if (qx < 0.0) qx=0.0;
  675. if (qy < 0.0) qy=0.0;
  676. if (qz < 0.0) qz=0.0;
  677. qx = _copysign( qx, m21 - m12 );
  678. qy = _copysign( qy, m02 - m20 );
  679. qz = _copysign( qz, m20 - m02 );
  680.  
  681. WriteNormQuat((float)qw,(float)qx,(float)qy,(float)qz);
  682. }
  683.  
  684. // Read the native types from the front of the buffer
  685. // Write the native types to the end of the buffer
  686. bool BitStream::ReadBool()
  687. {
  688. #ifdef TYPE_CHECKING
  689. unsigned char ID;
  690.  
  691. if ( ReadBits( ( unsigned char* ) & ID, sizeof(unsigned char) * ) == false )
  692. return false;
  693.  
  694. #ifdef _DEBUG
  695.  
  696. assert( ID == );
  697.  
  698. #endif
  699. return true;
  700. #endif
  701.  
  702. //assert(readOffset+1 <=numberOfBitsUsed); // If this assert is hit the stream wasn't long enough to read from
  703. if ( readOffset + > numberOfBitsUsed )
  704. return false;
  705.  
  706. //if (ReadBit()) // Check that bit
  707. if ( data[ readOffset >> ] & ( 0x80 >> ( readOffset++ % ) ) ) // Is it faster to just write it out here?
  708. return true;
  709.  
  710. return false;
  711. }
  712.  
  713. uint8 BitStream::ReadUInt8()
  714. {
  715. #ifdef TYPE_CHECKING
  716. unsigned char ID;
  717.  
  718. if ( ReadBits( ( unsigned char* ) & ID, sizeof(unsigned char) * ) == false )
  719. return ;
  720.  
  721. assert( ID == );
  722. return ID;
  723. #endif
  724. uint8 uint8r;
  725. if(ReadBits( ( unsigned char* ) & uint8r, sizeof( uint8r ) * )){
  726. return uint8r;
  727. }
  728. return ;
  729. }
  730.  
  731. int8 BitStream::ReadInt8()
  732. {
  733. #ifdef TYPE_CHECKING
  734. unsigned char ID;
  735.  
  736. if ( ReadBits( ( unsigned char* ) & ID, sizeof(unsigned char) * ) == false )
  737. return ;
  738.  
  739. assert( ID == );
  740. return ID;
  741. #endif
  742. int8 int8r;
  743. if(ReadBits( ( unsigned char* ) & int8r, sizeof( int8r ) * )) {
  744. return int8r;
  745. }
  746. return ;
  747. }
  748.  
  749. uint16 BitStream::ReadUInt16()
  750. {
  751. #ifdef TYPE_CHECKING
  752. unsigned char ID;
  753.  
  754. if ( ReadBits( ( unsigned char* ) & ID, sizeof(unsigned char) * ) == false )
  755. return ;
  756.  
  757. assert( ID == );
  758. return ID;
  759. #endif
  760. unsigned char uint16r[];
  761. #ifdef __BITSTREAM_NATIVE_END
  762. if(ReadBits( uint16r, sizeof( uint16_t ) * )){
  763. return *(uint16_t*)uint16r;
  764. }
  765. return ;
  766. #else
  767. if (ReadBits( uint16r, sizeof( uint16 ) * ) != true) return ;
  768. return (((uint16) uint16r[B16_1])<<)|((uint16)uint16r[B16_0]);
  769. #endif
  770. }
  771.  
  772. int16 BitStream::ReadInt16()
  773. {
  774. #ifdef TYPE_CHECKING
  775. unsigned char ID;
  776.  
  777. if ( ReadBits( ( unsigned char* ) & ID, sizeof(unsigned char) * ) == false )
  778. return ;
  779.  
  780. assert( ID == );
  781. return ID;
  782. #endif
  783. unsigned char int16r[];
  784. #ifdef __BITSTREAM_NATIVE_END
  785. if(ReadBits( int16r, sizeof( int16_t ) * )){
  786. return *(int16_t*)int16r;
  787. }
  788. return ;
  789. #else
  790. if (ReadBits( int16r, sizeof( int16 ) * ) != true) return ;
  791. return (((int16) int16r[B16_1])<<)|((int16)int16r[B16_0]);
  792. #endif
  793. }
  794.  
  795. uint32 BitStream::ReadUInt32()
  796. {
  797. #ifdef TYPE_CHECKING
  798. unsigned char ID;
  799.  
  800. if ( ReadBits( ( unsigned char* ) & ID, sizeof(unsigned char) * ) == false )
  801. return ;
  802.  
  803. assert( ID == );
  804. return ID;
  805. #endif
  806. unsigned char uint32r[];
  807. #ifdef __BITSTREAM_NATIVE_END
  808. if(ReadBits( uint32r, sizeof( uint32_t ) * )){
  809. return *(uint32_t*)uint32r;
  810. }
  811. return ;
  812. #else
  813. if(ReadBits( uint32r, sizeof( uint32 ) * ) != true)
  814. return ;
  815. return (((uint32) uint32r[B32_3])<<)|
  816. (((uint32) uint32r[B32_2])<<)|
  817. (((uint32) uint32r[B32_1])<<)|
  818. ((uint32) uint32r[B32_0]);
  819. #endif
  820. }
  821.  
  822. int32 BitStream::ReadInt32()
  823. {
  824. #ifdef TYPE_CHECKING
  825. unsigned char ID;
  826.  
  827. if ( ReadBits( ( unsigned char* ) & ID, sizeof(unsigned char) * ) == false )
  828. return ;
  829.  
  830. assert( ID == );
  831. return ID;
  832. #endif
  833. unsigned char int32r[];
  834. #ifdef __BITSTREAM_NATIVE_END
  835. if(ReadBits( int32r, sizeof( int32_t ) * )){
  836. return *(int32_t*)int32r;
  837. }
  838. return ;
  839. #else
  840. if(ReadBits( int32r, sizeof( int32 ) * ) != true)
  841. return ;
  842. return (((int32) int32r[B32_3])<<)|
  843. (((int32) int32r[B32_2])<<)|
  844. (((int32) int32r[B32_1])<<)|
  845. ((int32) int32r[B32_0]);
  846. #endif
  847. }
  848.  
  849. #ifdef HAS_INT64
  850. uint64 BitStream::ReadUInt64()
  851. {
  852. #ifdef TYPE_CHECKING
  853. unsigned char ID;
  854.  
  855. if ( ReadBits( ( unsigned char* ) & ID, sizeof(unsigned char) * ) == false )
  856. return ;
  857.  
  858. assert( ID == );
  859. return ID;
  860. #endif
  861. unsigned char uint64r[];
  862. #ifdef __BITSTREAM_NATIVE_END
  863. if(ReadBits( uint64r, sizeof( uint64_t ) * )){
  864. return *(uint64_t*)uint64r;
  865. }
  866. return ;
  867. #else
  868. if(ReadBits( uint64r, sizeof( uint64 ) * ) != true)
  869. return ;
  870. return (((uint64) uint64r[B64_7])<<)|(((uint64) uint64r[B64_6])<<)|
  871. (((uint64) uint64r[B64_5])<<)|(((uint64) uint64r[B64_4])<<)|
  872. (((uint64) uint64r[B64_3])<<)|(((uint64) uint64r[B64_2])<<)|
  873. (((uint64) uint64r[B64_1])<<)|((uint64) uint64r[B64_0]);
  874. #endif
  875. }
  876.  
  877. int64 BitStream::ReadInt64()
  878. {
  879. #ifdef TYPE_CHECKING
  880. unsigned char ID;
  881.  
  882. if ( ReadBits( ( unsigned char* ) & ID, sizeof(unsigned char) * ) == false )
  883. return ;
  884.  
  885. assert( ID == );
  886. return ID;
  887. #endif
  888. unsigned char int64r[];
  889. #ifdef __BITSTREAM_NATIVE_END
  890. if(ReadBits(int64r, sizeof( int64_t ) * )){
  891. return *(int64_t*)int64r;
  892. }
  893. return ;
  894. #else
  895. if(ReadBits( int64r, sizeof( int64_t ) * ) != true)
  896. return ;
  897. return (((uint64) int64r[B64_7])<<)|(((uint64) int64r[B64_6])<<)|
  898. (((uint64) int64r[B64_5])<<)|(((uint64) int64r[B64_4])<<)|
  899. (((uint64) int64r[B64_3])<<)|(((uint64) int64r[B64_2])<<)|
  900. (((uint64) int64r[B64_1])<<)|((uint64) int64r[B64_0]);
  901. #endif
  902. }
  903. #endif
  904.  
  905. float BitStream::ReadFloat()
  906. {
  907. #ifdef TYPE_CHECKING
  908. unsigned char ID;
  909.  
  910. if ( ReadBits( ( unsigned char* ) & ID, sizeof(unsigned char) * ) == false )
  911. return ;
  912.  
  913. assert( ID == );
  914. return ID;
  915. #endif
  916.  
  917. #ifdef __BITSTREAM_NATIVE_END
  918. static float floatr;
  919. if(ReadBits( ( unsigned char* ) & floatr, sizeof( floatr ) * )){
  920. return floatr;
  921. }
  922. return ;
  923. #else
  924. unsigned int val = ReadUInt32();
  925. return *((float *)(&val));
  926. #endif
  927. }
  928.  
  929. double BitStream::ReadDouble()
  930. {
  931. #ifdef TYPE_CHECKING
  932. unsigned char ID;
  933.  
  934. if ( ReadBits( ( unsigned char* ) & ID, sizeof(unsigned char) * ) == false )
  935. return ;
  936.  
  937. assert( ID == );
  938. return ID;
  939. #endif
  940.  
  941. #if defined ( __BITSTREAM_NATIVE_END ) || ( ! defined ( HAS_INT64 ) )
  942. static double doubler;
  943. if(ReadBits( ( unsigned char* ) & doubler, sizeof( doubler ) * )){
  944. return doubler;
  945. }
  946. return ;
  947. #else
  948. uint64_t val = ReadUInt64();
  949. return *((double *)(&val));
  950. #endif
  951. }
  952. // Read an array or casted stream
  953. bool BitStream::ReadBytes( char* output, const int numberOfBytes )
  954. {
  955. #ifdef TYPE_CHECKING
  956. unsigned char ID;
  957.  
  958. if ( ReadBits( ( unsigned char* ) & ID, sizeof(unsigned char) * ) == false )
  959. return false;
  960.  
  961. assert( ID == );
  962.  
  963. int NOB;
  964.  
  965. ReadBits( ( unsigned char* ) & NOB, sizeof( int ) * );
  966.  
  967. assert( NOB == numberOfBytes );
  968.  
  969. #endif
  970.  
  971. return ReadBits( ( unsigned char* ) output, numberOfBytes * );
  972. }
  973.  
  974. bool BitStream::ReadStr(char output[], int size){
  975. unsigned short len = ReadUInt16();
  976. len = (len+) > size?size:len;
  977. if(len > ){
  978. if(ReadBytes(output,len)){
  979. output[len] = '\0';
  980. return true;
  981. }
  982. }
  983. return false;
  984. }
  985.  
  986. std::string BitStream::ReadStr()
  987. {
  988. std::string strs ;
  989. unsigned short len = ReadUInt16();
  990.  
  991. if(len > ){
  992. char* str = new char[len+];
  993. if(ReadBytes(str,len)){
  994. str[len] = '\0';
  995. strs = str;
  996. delete[] str;
  997. return strs;
  998. }
  999. }
  1000.  
  1001. return std::string();
  1002. }
  1003.  
  1004. // Read the types you wrote with WriteCompressed
  1005. uint8 BitStream::ReadCompUInt8()
  1006. {
  1007. #ifdef TYPE_CHECKING
  1008. unsigned char ID;
  1009.  
  1010. if ( ReadBits( ( unsigned char* ) & ID, sizeof(unsigned char) * ) == false )
  1011. return ;
  1012.  
  1013. assert( ID == );
  1014. return ID;
  1015. #endif
  1016. uint8 uint8rc;
  1017. if(ReadCompressed( ( unsigned char* ) & uint8rc, sizeof( uint8rc ) * , true )){
  1018. return uint8rc;
  1019. }
  1020. return ;
  1021. }
  1022.  
  1023. int8 BitStream::ReadCompInt8()
  1024. {
  1025. #ifdef TYPE_CHECKING
  1026. unsigned char ID;
  1027.  
  1028. if ( ReadBits( ( unsigned char* ) & ID, sizeof(unsigned char) * ) == false )
  1029. return ;
  1030.  
  1031. assert( ID == );
  1032. return ID;
  1033. #endif
  1034. int8 int8rc;
  1035. if(ReadCompressed( ( unsigned char* ) & int8rc, sizeof( int8rc ) * , false )){
  1036. return int8rc;
  1037. }
  1038. return ;
  1039. }
  1040.  
  1041. uint16 BitStream::ReadCompUInt16()
  1042. {
  1043. #ifdef TYPE_CHECKING
  1044. unsigned char ID;
  1045.  
  1046. if ( ReadBits( ( unsigned char* ) & ID, sizeof(unsigned char) * ) == false )
  1047. return ;
  1048.  
  1049. assert( ID == );
  1050. return ID;
  1051. #endif
  1052. unsigned char uint16rc[];
  1053. #ifdef __BITSTREAM_NATIVE_END
  1054. if(ReadCompressed( uint16rc, sizeof( uint16_t ) * , true )){
  1055. return *(uint16_t*)uint16rc;
  1056. }
  1057. return ;
  1058. #else
  1059. if (ReadCompressed( uint16rc, sizeof( uint16 ) * , true ) != true) return ;
  1060. return (((uint16) uint16rc[B16_1])<<)|
  1061. ((uint16) uint16rc[B16_0]);
  1062. #endif
  1063. }
  1064.  
  1065. int16 BitStream::ReadCompInt16()
  1066. {
  1067. #ifdef TYPE_CHECKING
  1068. unsigned char ID;
  1069.  
  1070. if ( ReadBits( ( unsigned char* ) & ID, sizeof(unsigned char) * ) == false )
  1071. return ;
  1072.  
  1073. assert( ID == );
  1074. return ID;
  1075. #endif
  1076. unsigned char int16rc[];
  1077. #ifdef __BITSTREAM_NATIVE_END
  1078. if(ReadCompressed( int16rc, sizeof( int16_t ) * , true )){
  1079. return *(int16_t*)int16rc;
  1080. }
  1081. return ;
  1082. #else
  1083. if (ReadCompressed( int16rc, sizeof( int16 ) * , false ) != true) return ;
  1084. return (((uint16) int16rc[B16_1])<<)|((uint16)int16rc[B16_0]);
  1085. #endif
  1086. }
  1087.  
  1088. uint32 BitStream::ReadCompUInt32()
  1089. {
  1090. #ifdef TYPE_CHECKING
  1091. unsigned char ID;
  1092.  
  1093. if ( ReadBits( ( unsigned char* ) & ID, sizeof(unsigned char) * ) == false )
  1094. return ;
  1095.  
  1096. assert( ID == );
  1097. return ID;
  1098.  
  1099. #endif
  1100. unsigned char uint32rc[];
  1101. #ifdef __BITSTREAM_NATIVE_END
  1102. if(ReadCompressed( uint32rc, sizeof( uint32_t ) * , true )){
  1103. return *(uint32_t*)uint32rc;
  1104. }
  1105. return ;
  1106. #else
  1107. if(ReadCompressed( uint32rc, sizeof( uint32 ) * , true ) != true)
  1108. return ;
  1109. return (((uint32) uint32rc[B32_3])<<)|
  1110. (((uint32) uint32rc[B32_2])<<)|
  1111. (((uint32) uint32rc[B32_1])<<)|
  1112. ((uint32) uint32rc[B32_0]);
  1113. #endif
  1114. }
  1115.  
  1116. int32 BitStream::ReadCompInt32()
  1117. {
  1118. #ifdef TYPE_CHECKING
  1119. unsigned char ID;
  1120.  
  1121. if ( ReadBits( ( unsigned char* ) & ID, sizeof(unsigned char) * ) == false )
  1122. return ;
  1123.  
  1124. assert( ID == );
  1125. return ID;
  1126. #endif
  1127.  
  1128. unsigned char int32rc[];
  1129. #ifdef __BITSTREAM_NATIVE_END
  1130. if(ReadCompressed(int32rc, sizeof( int32_t ) * , true )){
  1131. return *(int32_t*)int32rc;
  1132. }
  1133. return ;
  1134. #else
  1135. if(ReadCompressed( int32rc, sizeof( int32 ) * , false ) != true)
  1136. return ;
  1137. return (((uint32) int32rc[B32_3])<<)|
  1138. (((uint32) int32rc[B32_2])<<)|
  1139. (((uint32) int32rc[B32_1])<<)|
  1140. ((uint32) int32rc[B32_0]);
  1141.  
  1142. #endif
  1143. }
  1144.  
  1145. #ifdef HAS_INT64
  1146. uint64_t BitStream::ReadCompUInt64()
  1147. {
  1148. #ifdef TYPE_CHECKING
  1149. unsigned char ID;
  1150.  
  1151. if ( ReadBits( ( unsigned char* ) & ID, sizeof(unsigned char) * ) == false )
  1152. return ;
  1153.  
  1154. assert( ID == );
  1155. return ID;
  1156. #endif
  1157.  
  1158. unsigned char uint64rc[];
  1159. #ifdef __BITSTREAM_NATIVE_END
  1160. if(ReadCompressed( uint64rc, sizeof( uint64_t ) * , true )){
  1161. return *(uint64_t*)uint64rc;
  1162. }
  1163. return ;
  1164. #else
  1165. if(ReadCompressed( uint64rc, sizeof( uint64_t ) * , true ) != true)
  1166. return ;
  1167. return (((uint64_t) uint64rc[B64_7])<<)|(((uint64_t) uint64rc[B64_6])<<)|
  1168. (((uint64_t) uint64rc[B64_5])<<)|(((uint64_t) uint64rc[B64_4])<<)|
  1169. (((uint64_t) uint64rc[B64_3])<<)|(((uint64_t) uint64rc[B64_2])<<)|
  1170. (((uint64_t) uint64rc[B64_1])<<)|((uint64_t) uint64rc[B64_0]);
  1171. #endif
  1172. }
  1173.  
  1174. int64_t BitStream::ReadCompInt64()
  1175. {
  1176. #ifdef TYPE_CHECKING
  1177. unsigned char ID;
  1178.  
  1179. if ( ReadBits( ( unsigned char* ) & ID, sizeof(unsigned char) * ) == false )
  1180. return ;
  1181.  
  1182. assert( ID == );
  1183. return ID;
  1184. #endif
  1185. unsigned char int64rc[];
  1186. #ifdef __BITSTREAM_NATIVE_END
  1187. if(ReadCompressed( int64rc, sizeof( int64_t ) * , true )){
  1188. return *(int64_t*)int64rc;
  1189. }
  1190. return ;
  1191. #else
  1192. if(ReadCompressed( int64rc, sizeof( int64_t ) * , false ) != true)
  1193. return ;
  1194. return (((uint64_t) int64rc[B64_7])<<)|(((uint64_t) int64rc[B64_6])<<)|
  1195. (((uint64_t) int64rc[B64_5])<<)|(((uint64_t) int64rc[B64_4])<<)|
  1196. (((uint64_t) int64rc[B64_3])<<)|(((uint64_t) int64rc[B64_2])<<)|
  1197. (((uint64_t) int64rc[B64_1])<<)|((uint64_t) int64rc[B64_0]);
  1198. #endif
  1199. }
  1200. #endif
  1201.  
  1202. float BitStream::ReadCompFloat()
  1203. {
  1204. #ifdef TYPE_CHECKING
  1205. unsigned char ID;
  1206.  
  1207. if ( ReadBits( ( unsigned char* ) & ID, sizeof(unsigned char) * ) == false )
  1208. return ;
  1209.  
  1210. assert( ID == );
  1211. return ID;
  1212. #endif
  1213.  
  1214. return ReadFloat();
  1215. }
  1216.  
  1217. double BitStream::ReadCompDouble()
  1218. {
  1219. #ifdef TYPE_CHECKING
  1220. unsigned char ID;
  1221.  
  1222. if ( ReadBits( ( unsigned char* ) & ID, sizeof(unsigned char) * ) == false )
  1223. return ;
  1224.  
  1225. assert( ID == );
  1226. return ID;
  1227. #endif
  1228.  
  1229. return ReadDouble();
  1230. }
  1231.  
  1232. bool BitStream::ReadNormVector( float &x, float &y, float &z )
  1233. {
  1234. unsigned short sy, sz;
  1235.  
  1236. bool xNeg = ReadBool();
  1237.  
  1238. bool yZero = ReadBool();
  1239. if (yZero)
  1240. y=0.0f;
  1241. else
  1242. {
  1243. sy = ReadUInt16();
  1244. y=((float)sy / 32767.5f - 1.0f);
  1245. }
  1246.  
  1247. bool zZero = ReadBool();
  1248. if (zZero)
  1249. z=0.0f;
  1250. else
  1251. {
  1252. sz = ReadUInt16();
  1253.  
  1254. z=((float)sz / 32767.5f - 1.0f);
  1255. }
  1256.  
  1257. x = sqrtf(1.0f - y*y - z*z);
  1258. if (xNeg)
  1259. x=-x;
  1260. return true;
  1261. }
  1262. bool BitStream::ReadVector( float &x, float &y, float &z )
  1263. {
  1264. unsigned short sx,sy,sz;
  1265.  
  1266. float magnitude = ReadFloat();
  1267.  
  1268. if (magnitude!=0.0f)
  1269. {
  1270. sx = ReadUInt16();
  1271. sy = ReadUInt16();
  1272. sz = ReadUInt16();
  1273.  
  1274. x=((float)sx / 32767.5f - 1.0f) * magnitude;
  1275. y=((float)sy / 32767.5f - 1.0f) * magnitude;
  1276. z=((float)sz / 32767.5f - 1.0f) * magnitude;
  1277. }
  1278. else
  1279. {
  1280. x=0.0f;
  1281. y=0.0f;
  1282. z=0.0f;
  1283. }
  1284. return true;
  1285. }
  1286. bool BitStream::ReadNormQuat( float &w, float &x, float &y, float &z)
  1287. {
  1288. bool cwNeg = ReadBool();
  1289. bool cxNeg = ReadBool();
  1290. bool cyNeg = ReadBool();
  1291. bool czNeg = ReadBool();
  1292. unsigned short cx = ReadUInt16();
  1293. unsigned short cy = ReadUInt16();
  1294. unsigned short cz = ReadUInt16();
  1295.  
  1296. // Calculate w from x,y,z
  1297. x=cx/65535.0f;
  1298. y=cy/65535.0f;
  1299. z=cz/65535.0f;
  1300. if (cxNeg) x=-x;
  1301. if (cyNeg) y=-y;
  1302. if (czNeg) z=-z;
  1303. w = sqrt(1.0f - x*x - y*y - z*z);
  1304. if (cwNeg)
  1305. w=-w;
  1306. return true;
  1307. }
  1308. bool BitStream::ReadOrthMatrix(
  1309. float &m00, float &m01, float &m02,
  1310. float &m10, float &m11, float &m12,
  1311. float &m20, float &m21, float &m22 )
  1312. {
  1313. float qw,qx,qy,qz;
  1314. if (!ReadNormQuat(qw,qx,qy,qz))
  1315. return false;
  1316.  
  1317. // Quat to orthogonal rotation matrix
  1318. // http://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToMatrix/index.htm
  1319. double sqw = (double)qw*(double)qw;
  1320. double sqx = (double)qx*(double)qx;
  1321. double sqy = (double)qy*(double)qy;
  1322. double sqz = (double)qz*(double)qz;
  1323. m00 = (float)(sqx - sqy - sqz + sqw); // since sqw + sqx + sqy + sqz =1
  1324. m11 = (float)(-sqx + sqy - sqz + sqw);
  1325. m22 = (float)(-sqx - sqy + sqz + sqw);
  1326.  
  1327. double tmp1 = (double)qx*(double)qy;
  1328. double tmp2 = (double)qz*(double)qw;
  1329. m10 = (float)(2.0 * (tmp1 + tmp2));
  1330. m01 = (float)(2.0 * (tmp1 - tmp2));
  1331.  
  1332. tmp1 = (double)qx*(double)qz;
  1333. tmp2 = (double)qy*(double)qw;
  1334. m20 =(float)(2.0 * (tmp1 - tmp2));
  1335. m02 = (float)(2.0 * (tmp1 + tmp2));
  1336. tmp1 = (double)qy*(double)qz;
  1337. tmp2 = (double)qx*(double)qw;
  1338. m21 = (float)(2.0 * (tmp1 + tmp2));
  1339. m12 = (float)(2.0 * (tmp1 - tmp2));
  1340.  
  1341. return true;
  1342. }
  1343.  
  1344. // Sets the read pointer back to the beginning of your data.
  1345. void BitStream::ResetReadPointer( void )
  1346. {
  1347. readOffset = ;
  1348. }
  1349.  
  1350. // Sets the write pointer back to the beginning of your data.
  1351. void BitStream::ResetWritePointer( void )
  1352. {
  1353. numberOfBitsUsed = ;
  1354. }
  1355.  
  1356. // Write a 0
  1357. void BitStream::Write0( void )
  1358. {
  1359. AddBitsAndReallocate( );
  1360.  
  1361. // New bytes need to be zeroed
  1362.  
  1363. if ( ( numberOfBitsUsed % ) == )
  1364. data[ numberOfBitsUsed >> ] = ;
  1365.  
  1366. numberOfBitsUsed++;
  1367. }
  1368.  
  1369. // Write a 1
  1370. void BitStream::Write1( void )
  1371. {
  1372. AddBitsAndReallocate( );
  1373.  
  1374. int numberOfBitsMod8 = numberOfBitsUsed % ;
  1375.  
  1376. if ( numberOfBitsMod8 == )
  1377. data[ numberOfBitsUsed >> ] = 0x80;
  1378. else
  1379. data[ numberOfBitsUsed >> ] |= 0x80 >> ( numberOfBitsMod8 ); // Set the bit to 1
  1380.  
  1381. numberOfBitsUsed++;
  1382. }
  1383.  
  1384. // Returns true if the next data read is a 1, false if it is a 0
  1385. bool BitStream::ReadBit( void )
  1386. {
  1387. #pragma warning( disable : 4800 )
  1388. return ( bool ) ( data[ readOffset >> ] & ( 0x80 >> ( readOffset++ % ) ) );
  1389. #pragma warning( default : 4800 )
  1390. }
  1391.  
  1392. // Align the bitstream to the byte boundary and then write the specified number of bits.
  1393. // This is faster than WriteBits but wastes the bits to do the alignment and requires you to call
  1394. // SetReadToByteAlignment at the corresponding read position
  1395. void BitStream::WriteAlignedBytes( const unsigned char* input,
  1396. const int numberOfBytesToWrite )
  1397. {
  1398. #ifdef _DEBUG
  1399. assert( numberOfBytesToWrite > );
  1400. #endif
  1401.  
  1402. AlignWriteToByteBoundary();
  1403. // Allocate enough memory to hold everything
  1404. AddBitsAndReallocate( numberOfBytesToWrite << );
  1405.  
  1406. // Write the data
  1407. memcpy( data + ( numberOfBitsUsed >> ), input, numberOfBytesToWrite );
  1408.  
  1409. numberOfBitsUsed += numberOfBytesToWrite << ;
  1410. }
  1411.  
  1412. // Read bits, starting at the next aligned bits. Note that the modulus 8 starting offset of the
  1413. // sequence must be the same as was used with WriteBits. This will be a problem with packet coalescence
  1414. // unless you byte align the coalesced packets.
  1415. bool BitStream::ReadAlignedBytes( unsigned char* output,
  1416. const int numberOfBytesToRead )
  1417. {
  1418. #ifdef _DEBUG
  1419. assert( numberOfBytesToRead > );
  1420. #endif
  1421.  
  1422. if ( numberOfBytesToRead <= )
  1423. return false;
  1424.  
  1425. // Byte align
  1426. AlignReadToByteBoundary();
  1427.  
  1428. if ( readOffset + ( numberOfBytesToRead << ) > numberOfBitsUsed )
  1429. return false;
  1430.  
  1431. // Write the data
  1432. memcpy( output, data + ( readOffset >> ), numberOfBytesToRead );
  1433.  
  1434. readOffset += numberOfBytesToRead << ;
  1435.  
  1436. return true;
  1437. }
  1438.  
  1439. // Align the next write and/or read to a byte boundary. This can be used to 'waste' bits to byte align for efficiency reasons
  1440. void BitStream::AlignWriteToByteBoundary( void )
  1441. {
  1442. if ( numberOfBitsUsed )
  1443. numberOfBitsUsed += - ( ( numberOfBitsUsed - ) % + );
  1444. }
  1445.  
  1446. // Align the next write and/or read to a byte boundary. This can be used to 'waste' bits to byte align for efficiency reasons
  1447. void BitStream::AlignReadToByteBoundary( void )
  1448. {
  1449. if ( readOffset )
  1450. readOffset += - ( ( readOffset - ) % + );
  1451. }
  1452.  
  1453. // Write numberToWrite bits from the input source
  1454. void BitStream::WriteBits( const unsigned char *input,
  1455. int numberOfBitsToWrite, const bool rightAlignedBits )
  1456. {
  1457. // if (numberOfBitsToWrite<=0)
  1458. // return;
  1459.  
  1460. AddBitsAndReallocate( numberOfBitsToWrite );
  1461. int offset = ;
  1462. unsigned char dataByte;
  1463. int numberOfBitsUsedMod8;
  1464.  
  1465. numberOfBitsUsedMod8 = numberOfBitsUsed % ;
  1466.  
  1467. // Faster to put the while at the top surprisingly enough
  1468. while ( numberOfBitsToWrite > )
  1469. //do
  1470. {
  1471. dataByte = *( input + offset );
  1472.  
  1473. if ( numberOfBitsToWrite < && rightAlignedBits ) // rightAlignedBits means in the case of a partial byte, the bits are aligned from the right (bit 0) rather than the left (as in the normal internal representation)
  1474. dataByte <<= - numberOfBitsToWrite; // shift left to get the bits on the left, as in our internal representation
  1475.  
  1476. // Writing to a new byte each time
  1477. if ( numberOfBitsUsedMod8 == )
  1478. * ( data + ( numberOfBitsUsed >> ) ) = dataByte;
  1479. else
  1480. {
  1481. // Copy over the new data.
  1482. *( data + ( numberOfBitsUsed >> ) ) |= dataByte >> ( numberOfBitsUsedMod8 ); // First half
  1483.  
  1484. if ( - ( numberOfBitsUsedMod8 ) < && - ( numberOfBitsUsedMod8 ) < numberOfBitsToWrite ) // If we didn't write it all out in the first half (8 - (numberOfBitsUsed%8) is the number we wrote in the first half)
  1485. {
  1486. *( data + ( numberOfBitsUsed >> ) + ) = (unsigned char) ( dataByte << ( - ( numberOfBitsUsedMod8 ) ) ); // Second half (overlaps byte boundary)
  1487. }
  1488. }
  1489.  
  1490. if ( numberOfBitsToWrite >= )
  1491. numberOfBitsUsed += ;
  1492. else
  1493. numberOfBitsUsed += numberOfBitsToWrite;
  1494.  
  1495. numberOfBitsToWrite -= ;
  1496.  
  1497. offset++;
  1498. }
  1499. // } while(numberOfBitsToWrite>0);
  1500. }
  1501.  
  1502. // Set the stream to some initial data. For internal use
  1503. void BitStream::SetData( const unsigned char* input, const int numberOfBits )
  1504. {
  1505. #ifdef _DEBUG
  1506. assert( numberOfBitsUsed == ); // Make sure the stream is clear
  1507. #endif
  1508.  
  1509. if ( numberOfBits <= )
  1510. return ;
  1511.  
  1512. AddBitsAndReallocate( numberOfBits );
  1513.  
  1514. memcpy( data, input, BITS_TO_BYTES( numberOfBits ) );
  1515.  
  1516. numberOfBitsUsed = numberOfBits;
  1517. }
  1518.  
  1519. // Assume the input source points to a native type, compress and write it
  1520. void BitStream::WriteCompressed( const unsigned char* input,
  1521. const int size, const bool unsignedData )
  1522. {
  1523. int currentByte = ( size >> ) - ; // PCs
  1524.  
  1525. unsigned char byteMatch;
  1526.  
  1527. if ( unsignedData )
  1528. {
  1529. byteMatch = ;
  1530. }
  1531.  
  1532. else
  1533. {
  1534. byteMatch = 0xFF;
  1535. }
  1536.  
  1537. // Write upper bytes with a single 1
  1538. // From high byte to low byte, if high byte is a byteMatch then write a 1 bit. Otherwise write a 0 bit and then write the remaining bytes
  1539. while ( currentByte > )
  1540. {
  1541. if ( input[ currentByte ] == byteMatch ) // If high byte is byteMatch (0 of 0xff) then it would have the same value shifted
  1542. {
  1543. bool b = true;
  1544. WriteBool( b );
  1545. }
  1546. else
  1547. {
  1548. // Write the remainder of the data after writing 0
  1549. bool b = false;
  1550. WriteBool( b );
  1551.  
  1552. WriteBits( input, ( currentByte + ) << , true );
  1553. // currentByte--;
  1554.  
  1555. return ;
  1556. }
  1557.  
  1558. currentByte--;
  1559. }
  1560.  
  1561. // If the upper half of the last byte is a 0 (positive) or 16 (negative) then write a 1 and the remaining 4 bits. Otherwise write a 0 and the 8 bites.
  1562. if ( ( unsignedData && ( ( *( input + currentByte ) ) & 0xF0 ) == 0x00 ) ||
  1563. ( unsignedData == false && ( ( *( input + currentByte ) ) & 0xF0 ) == 0xF0 ) )
  1564. {
  1565. bool b = true;
  1566. WriteBool( b );
  1567. WriteBits( input + currentByte, , true );
  1568. }
  1569.  
  1570. else
  1571. {
  1572. bool b = false;
  1573. WriteBool( b );
  1574. WriteBits( input + currentByte, , true );
  1575. }
  1576. }
  1577.  
  1578. // Read numberOfBitsToRead bits to the output source
  1579. // alignBitsToRight should be set to true to convert internal bitstream data to userdata
  1580. // It should be false if you used WriteBits with rightAlignedBits false
  1581. bool BitStream::ReadBits( unsigned char* output,
  1582. int numberOfBitsToRead, const bool alignBitsToRight )
  1583. {
  1584. #ifdef _DEBUG
  1585. assert( numberOfBitsToRead > );
  1586. #endif
  1587. // if (numberOfBitsToRead<=0)
  1588. // return false;
  1589.  
  1590. if ( readOffset + numberOfBitsToRead > numberOfBitsUsed )
  1591. return false;
  1592.  
  1593. int readOffsetMod8;
  1594.  
  1595. int offset = ;
  1596.  
  1597. memset( output, , BITS_TO_BYTES( numberOfBitsToRead ) );
  1598.  
  1599. readOffsetMod8 = readOffset % ;
  1600.  
  1601. // do
  1602. // Faster to put the while at the top surprisingly enough
  1603. while ( numberOfBitsToRead > )
  1604. {
  1605. *( output + offset ) |= *( data + ( readOffset >> ) ) << ( readOffsetMod8 ); // First half
  1606.  
  1607. if ( readOffsetMod8 > && numberOfBitsToRead > - ( readOffsetMod8 ) ) // If we have a second half, we didn't read enough bytes in the first half
  1608. *( output + offset ) |= *( data + ( readOffset >> ) + ) >> ( - ( readOffsetMod8 ) ); // Second half (overlaps byte boundary)
  1609.  
  1610. numberOfBitsToRead -= ;
  1611.  
  1612. if ( numberOfBitsToRead < ) // Reading a partial byte for the last byte, shift right so the data is aligned on the right
  1613. {
  1614.  
  1615. if ( alignBitsToRight )
  1616. * ( output + offset ) >>= -numberOfBitsToRead;
  1617.  
  1618. readOffset += + numberOfBitsToRead;
  1619. }
  1620. else
  1621. readOffset += ;
  1622.  
  1623. offset++;
  1624.  
  1625. }
  1626.  
  1627. //} while(numberOfBitsToRead>0);
  1628.  
  1629. return true;
  1630. }
  1631.  
  1632. // Assume the input source points to a compressed native type. Decompress and read it
  1633. bool BitStream::ReadCompressed( unsigned char* output,
  1634. const int size, const bool unsignedData )
  1635. {
  1636. int currentByte = ( size >> ) - ;
  1637.  
  1638. unsigned char byteMatch, halfByteMatch;
  1639.  
  1640. if ( unsignedData )
  1641. {
  1642. byteMatch = ;
  1643. halfByteMatch = ;
  1644. }
  1645.  
  1646. else
  1647. {
  1648. byteMatch = 0xFF;
  1649. halfByteMatch = 0xF0;
  1650. }
  1651.  
  1652. // Upper bytes are specified with a single 1 if they match byteMatch
  1653. // From high byte to low byte, if high byte is a byteMatch then write a 1 bit. Otherwise write a 0 bit and then write the remaining bytes
  1654. while ( currentByte > )
  1655. {
  1656. // If we read a 1 then the data is byteMatch.
  1657.  
  1658. bool b = ReadBool();
  1659.  
  1660. if ( b ) // Check that bit
  1661. {
  1662. output[ currentByte ] = byteMatch;
  1663. currentByte--;
  1664. }
  1665. else
  1666. {
  1667. // Read the rest of the bytes
  1668.  
  1669. if ( ReadBits( output, ( currentByte + ) << ) == false )
  1670. return false;
  1671.  
  1672. return true;
  1673. }
  1674. }
  1675.  
  1676. // All but the first bytes are byteMatch. If the upper half of the last byte is a 0 (positive) or 16 (negative) then what we read will be a 1 and the remaining 4 bits.
  1677. // Otherwise we read a 0 and the 8 bytes
  1678. //assert(readOffset+1 <=numberOfBitsUsed); // If this assert is hit the stream wasn't long enough to read from
  1679. if ( readOffset + > numberOfBitsUsed )
  1680. return false;
  1681.  
  1682. bool b = ReadBool();
  1683.  
  1684. if ( b ) // Check that bit
  1685. {
  1686.  
  1687. if ( ReadBits( output + currentByte, ) == false )
  1688. return false;
  1689.  
  1690. output[ currentByte ] |= halfByteMatch; // We have to set the high 4 bits since these are set to 0 by ReadBits
  1691. }
  1692. else
  1693. {
  1694. if ( ReadBits( output + currentByte, ) == false )
  1695. return false;
  1696. }
  1697.  
  1698. return true;
  1699. }
  1700.  
  1701. // Reallocates (if necessary) in preparation of writing numberOfBitsToWrite
  1702. void BitStream::AddBitsAndReallocate( const int numberOfBitsToWrite )
  1703. {
  1704. if ( numberOfBitsToWrite <= )
  1705. return;
  1706.  
  1707. int newNumberOfBitsAllocated = numberOfBitsToWrite + numberOfBitsUsed;
  1708.  
  1709. if ( numberOfBitsToWrite + numberOfBitsUsed > && ( ( numberOfBitsAllocated - ) >> ) < ( ( newNumberOfBitsAllocated - ) >> ) ) // If we need to allocate 1 or more new bytes
  1710. {
  1711. #ifdef _DEBUG
  1712. // If this assert hits then we need to specify true for the third parameter in the constructor
  1713. // It needs to reallocate to hold all the data and can't do it unless we allocated to begin with
  1714. assert( copyData == true );
  1715. #endif
  1716.  
  1717. // Less memory efficient but saves on news and deletes
  1718. newNumberOfBitsAllocated = ( numberOfBitsToWrite + numberOfBitsUsed ) * ;
  1719. // int newByteOffset = BITS_TO_BYTES( numberOfBitsAllocated );
  1720. // Use realloc and free so we are more efficient than delete and new for resizing
  1721. int amountToAllocate = BITS_TO_BYTES( newNumberOfBitsAllocated );
  1722. if (data==(unsigned char*)stackData)
  1723. {
  1724. if (amountToAllocate > BITSTREAM_STACK_ALLOCATION_SIZE)
  1725. {
  1726. data = ( unsigned char* ) malloc( amountToAllocate );
  1727.  
  1728. // need to copy the stack data over to our new memory area too
  1729. memcpy ((void *)data, (void *)stackData, BITS_TO_BYTES( numberOfBitsAllocated ));
  1730. }
  1731. }
  1732. else
  1733. {
  1734. data = ( unsigned char* ) realloc( data, amountToAllocate );
  1735. }
  1736.  
  1737. #ifdef _DEBUG
  1738. assert( data ); // Make sure realloc succeeded
  1739. #endif
  1740. // memset(data+newByteOffset, 0, ((newNumberOfBitsAllocated-1)>>3) - ((numberOfBitsAllocated-1)>>3)); // Set the new data block to 0
  1741. }
  1742.  
  1743. if ( newNumberOfBitsAllocated > numberOfBitsAllocated )
  1744. numberOfBitsAllocated = newNumberOfBitsAllocated;
  1745. }
  1746.  
  1747. // Should hit if reads didn't match writes
  1748. void BitStream::AssertStreamEmpty( void )
  1749. {
  1750. assert( readOffset == numberOfBitsUsed );
  1751. }
  1752.  
  1753. void BitStream::PrintBits( void ) const
  1754. {
  1755. if ( numberOfBitsUsed <= )
  1756. {
  1757. // printf( "No bits\n" );
  1758. return ;
  1759. }
  1760.  
  1761. for ( int counter = ; counter < BITS_TO_BYTES( numberOfBitsUsed ); counter++ )
  1762. {
  1763. int stop;
  1764.  
  1765. if ( counter == ( numberOfBitsUsed - ) >> )
  1766. stop = - ( ( ( numberOfBitsUsed - ) % ) + );
  1767. else
  1768. stop = ;
  1769.  
  1770. for ( int counter2 = ; counter2 >= stop; counter2-- )
  1771. {
  1772. if ( ( data[ counter ] >> counter2 ) & )
  1773. putchar( '' );
  1774. else
  1775. putchar( '' );
  1776. }
  1777.  
  1778. putchar( ' ' );
  1779. }
  1780.  
  1781. putchar( '\n' );
  1782. }
  1783.  
  1784. // Exposes the data for you to look at, like PrintBits does.
  1785. // Data will point to the stream. Returns the length in bits of the stream.
  1786. int BitStream::CopyData( unsigned char** _data ) const
  1787. {
  1788. #ifdef _DEBUG
  1789. assert( numberOfBitsUsed > );
  1790. #endif
  1791.  
  1792. *_data = new unsigned char [ BITS_TO_BYTES( numberOfBitsUsed ) ];
  1793. memcpy( *_data, data, sizeof(unsigned char) * ( BITS_TO_BYTES( numberOfBitsUsed ) ) );
  1794. return numberOfBitsUsed;
  1795. }
  1796.  
  1797. // Ignore data we don't intend to read
  1798. void BitStream::IgnoreBits( const int numberOfBits )
  1799. {
  1800. readOffset += numberOfBits;
  1801. }
  1802.  
  1803. // Move the write pointer to a position on the array. Dangerous if you don't know what you are doing!
  1804. void BitStream::SetWriteOffset( const int offset )
  1805. {
  1806. numberOfBitsUsed = offset;
  1807. }
  1808.  
  1809. // Returns the length in bits of the stream
  1810. int BitStream::GetWriteOffset( void ) const
  1811. {
  1812. return numberOfBitsUsed;
  1813. }
  1814.  
  1815. // Returns the length in bytes of the stream
  1816. int BitStream::GetNumberOfBytesUsed( void ) const
  1817. {
  1818. return BITS_TO_BYTES( numberOfBitsUsed );
  1819. }
  1820. int BitStream::GetNumberOfBytesRead(void)const
  1821. {
  1822. return BITS_TO_BYTES(readOffset);
  1823. }
  1824.  
  1825. // Move the read pointer to a position on the array.
  1826. void BitStream::SetReadOffset( const int offset )
  1827. {
  1828. readOffset = offset;
  1829. }
  1830. void BitStream::SetByteReadOffSet(const int offset)
  1831. {
  1832. readOffset = BYTES_TO_BITS(offset);
  1833. }
  1834. // Returns the number of bits into the stream that we have read
  1835. int BitStream::GetReadOffset( void ) const
  1836. {
  1837. return readOffset;
  1838. }
  1839.  
  1840. // Returns the number of bits left in the stream that haven't been read
  1841. int BitStream::GetNumberOfUnreadBits( void ) const
  1842. {
  1843. return numberOfBitsUsed - readOffset;
  1844. }
  1845.  
  1846. // Exposes the internal data
  1847. unsigned char* BitStream::GetData( void ) const
  1848. {
  1849. return data;
  1850. }
  1851.  
  1852. // If we used the constructor version with copy data off, this makes sure it is set to on and the data pointed to is copied.
  1853. void BitStream::AssertCopyData( void )
  1854. {
  1855. if ( copyData == false )
  1856. {
  1857. copyData = true;
  1858.  
  1859. if ( numberOfBitsAllocated > )
  1860. {
  1861. unsigned char * newdata = ( unsigned char* ) malloc( BITS_TO_BYTES( numberOfBitsAllocated ) );
  1862. #ifdef _DEBUG
  1863.  
  1864. assert( data );
  1865. #endif
  1866.  
  1867. memcpy( newdata, data, BITS_TO_BYTES( numberOfBitsAllocated ) );
  1868. data = newdata;
  1869. }
  1870.  
  1871. else
  1872. data = ;
  1873. }
  1874. }

C++ Bitstream类的更多相关文章

  1. Java类的继承与多态特性-入门笔记

    相信对于继承和多态的概念性我就不在怎么解释啦!不管你是.Net还是Java面向对象编程都是比不缺少一堂课~~Net如此Java亦也有同样的思想成分包含其中. 继承,多态,封装是Java面向对象的3大特 ...

  2. [转]unity3d 脚本参考-技术文档

    unity3d 脚本参考-技术文档 核心提示:一.脚本概览这是一个关于Unity内部脚本如何工作的简单概览.Unity内部的脚本,是通过附加自定义脚本对象到游戏物体组成的.在脚本对象内部不同志的函数被 ...

  3. Unity3D脚本中文系列教程(五)

    http://dong2008hong.blog.163.com/blog/static/4696882720140302848544/?suggestedreading&wumii Unit ...

  4. Unity多玩家网络游戏开发教程1章Unity带有网络功能

    Unity网络多玩家游戏开发教程第1章Unity自带网络功能 Unity拥有大量的第三方插件.专门提供了对网络功能的支持. 可是.大部分开发人员第一次接触到的还是Unity自带的网络功能.也就是大家常 ...

  5. 一日一点RakNet(3)--介绍

    介绍 安装 请参考Compiler Setup页,有什么问题在这块有解答.如果有额外的问题参考FAQ,http://www.jenkinssoftware.com,或者邮件联系我们.高级用户可以直接跳 ...

  6. Raknet是一个基于UDP网络传输协议的C++网络库(还有一些其它库,比如nanomsg,fastsocket等等)

    Raknet是一个基于UDP网络传输协议的C++网络库,允许程序员在他们自己的程序中实现高效的网络传输服务.通常情况下用于游戏,但也可以用于其它项目. Raknet有以下好处: 高性能 在同一台计算机 ...

  7. Unity3D脚本学习——运行时类

    AssetBundle 类,继承自Object.AssetBundles让你通过WWW类流式加载额外的资源并在运行时实例化它们.AssetBundles通过BuildPipeline.BuildAss ...

  8. C++ 可配置的类工厂

    项目中常用到工厂模式,工厂模式可以把创建对象的具体细节封装到Create函数中,减少重复代码,增强可读和可维护性.传统的工厂实现如下: class Widget { public: virtual i ...

  9. Android请求网络共通类——Hi_博客 Android App 开发笔记

    今天 ,来分享一下 ,一个博客App的开发过程,以前也没开发过这种类型App 的经验,求大神们轻点喷. 首先我们要创建一个Andriod 项目 因为要从网络请求数据所以我们先来一个请求网络的共通类. ...

随机推荐

  1. sql 触发器删除操作

    create trigger CheckDelete on 表 for delete as ) select @state=isnull(字段,'') from deleted if (@state& ...

  2. OBS MAC 系统开发(基于mac OS X 10.12)

    按照github 上的说明,安装配套软件,和跟踪需要的库 推荐使用homebrew 来安装各种依赖库. 安装Qt后,要配置系统变量 ,这个困扰本人很久:) 成功编译 cmake .. &&am ...

  3. vi学习总结

    1.模式 命令行模式:光标的移动.内容删除移动复制操作 插入模式:文字输入,即编辑状态 底行模式:文件保存或退出vi,设置编辑环境 2.基本操作 vi myfile,输入vi 文件名,,则进入vi. ...

  4. 浅谈servlet

    刚开始接触servlet的时候,其实不是太理解servlet的,后来经过慢慢摸爬滚打式的的学习,有了一点自己的理解. servlet的产生还要从Java和HTTP说起: Java的servletAPI ...

  5. Netty_UDP丢包解决

    程序背景 程序是Java编写,基于Netty框架写的客户端及服务端. 现象 客户端大数据量持续发UDP数据,作为UDP服务器出现了部分数据频繁丢失触发程序自身重传逻辑. 通过GC日志对比发现丢包的时间 ...

  6. NOIp 11.11/12

    最后一场比较正式的NOIp模拟赛,写一发小总结.题目没什么好说的,大部分很简单,先贴一下代码. 1111 T1 //string //by Cydiater //2016.11.11 #include ...

  7. Day10-线程进程

    什么是进程? 程序并不能单独运行,只有将程序装载到内存中,系统为它分配资源才能运行,而这种执行的程序就称之为进程.程序和进程的区别就在于:程序是指令的集合,它是进程运行的静态描述文本:进程是程序的一次 ...

  8. 面对bug和困难的心态

    遇到bug了? 作为程序员,会面对各种各样的bug,我们在编写代码的时候,也是生产bug的过程.在公司总会遇到老同事留下的代码,这些代码出现问题了该怎么办?最常见的想法就是, 老同事怎么考虑这么不周到 ...

  9. html5中画布和SVG的比较

    SVG是基于XML的图形语言,在DOM解析中其每个元素都是可以用的,这样就可以为SCG元素附加JavaScript事件处理器,实现更加丰富的效果. 在SVG中,每个被绘制的图形均被视为对象,如果SVG ...

  10. 深入理解Java中的String

    一.String类 想要了解一个类,最好的办法就是看这个类的实现源代码,来看一下String类的源码: public final class String implements java.io.Ser ...