PageRenderTime 49ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/qwaqvm/platforms/Cross/third-party/resiprocate-src-1.6/p2p/s2c/s2c/s2c_native.cxx

http://openqwaq.googlecode.com/
C++ | 200 lines | 127 code | 44 blank | 29 comment | 6 complexity | 32b53250dc37bf32d166fa34d0fc2bb9 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, LGPL-2.0, BSD-3-Clause
  1. /**
  2. s2c_native.cxx
  3. Copyright (C) 2008, RTFM, Inc.
  4. All Rights Reserved.
  5. ekr@rtfm.com Fri May 2 17:04:55 2008
  6. */
  7. #include <assert.h>
  8. #include "rutil/Data.hxx"
  9. #include "rutil/DataStream.hxx"
  10. #include "rutil/ParseException.hxx"
  11. #include "s2c_native.hxx"
  12. void s2c::encode_uintX(std::ostream& out, const unsigned int bits, const u_int64 value)
  13. {
  14. int size;
  15. assert((bits%8)==0);
  16. assert(bits<=64);
  17. size=bits/8;
  18. switch(size){
  19. case 8:
  20. out.put(((value>>56)&0xff));
  21. case 7:
  22. out.put(((value>>48)&0xff));
  23. case 6:
  24. out.put(((value>>40)&0xff));
  25. case 5:
  26. out.put(((value>>32)&0xff));
  27. case 4:
  28. out.put(((value>>24)&0xff));
  29. case 3:
  30. out.put(((value>>16)&0xff));
  31. case 2:
  32. out.put(((value>>8)&0xff));
  33. case 1:
  34. out.put(value&0xff);
  35. break;
  36. default:
  37. assert(1==0);
  38. }
  39. }
  40. void s2c::decode_uintX(std::istream& in, const unsigned int bits, u_char &value)
  41. {
  42. int size;
  43. int c;
  44. assert(bits<=8);
  45. size=bits/8;
  46. value=0;
  47. while(size--){
  48. value <<=8;
  49. c=in.get();
  50. if(c==EOF)
  51. throw resip::ParseException("Unexpected end of encoding","",__FILE__,__LINE__);
  52. value |= c;
  53. }
  54. }
  55. /*void s2c::decode_uintX(std::istream& in, const unsigned int bits, u_int8 &value)
  56. {
  57. int size;
  58. int c;
  59. assert(bits==8);
  60. size=bits/8;
  61. value=0;
  62. while(size--){
  63. value <<=8;
  64. c=in->get();
  65. value |= c;
  66. }
  67. }
  68. */
  69. void s2c::decode_uintX(std::istream& in, const unsigned int bits, u_int16 &value)
  70. {
  71. int size;
  72. int c;
  73. assert(bits<=16);
  74. size=bits/8;
  75. value=0;
  76. while(size--){
  77. value <<=8;
  78. c=in.get();
  79. if(c==EOF)
  80. throw resip::ParseException("Unexpected end of encoding","",__FILE__,__LINE__);
  81. value |= c;
  82. }
  83. }
  84. void s2c::decode_uintX(std::istream& in, const unsigned int bits, u_int32 &value)
  85. {
  86. int size;
  87. int c;
  88. assert(bits<=32);
  89. size=bits/8;
  90. value=0;
  91. while(size--){
  92. value <<=8;
  93. c=in.get();
  94. if(c==EOF)
  95. throw resip::ParseException("Unexpected end of encoding","",__FILE__,__LINE__);
  96. value |= c;
  97. }
  98. }
  99. void s2c::decode_uintX(std::istream& in, const unsigned int bits, u_int64 &value)
  100. {
  101. int size;
  102. int c;
  103. assert(bits<=64);
  104. size=bits/8;
  105. value=0;
  106. while(size--){
  107. value <<=8;
  108. c=in.get();
  109. if(c==EOF)
  110. throw resip::ParseException("Unexpected end of encoding","",__FILE__,__LINE__);
  111. value |= c;
  112. }
  113. }
  114. void s2c::do_indent(std::ostream& out, int indent)
  115. {
  116. while(indent--) out << ' ';
  117. }
  118. // This is really clumsy, but I don't understand rutil
  119. // TODO: !ekr! cleanup
  120. void s2c::read_varray1(std::istream& in, unsigned int lenlen, resip::Data &buf)
  121. {
  122. u_int64 len=0;
  123. int c;
  124. // First read the length
  125. assert(lenlen<=8);
  126. while(lenlen--){
  127. len<<=8;
  128. c=in.get();
  129. if(c==EOF)
  130. throw resip::ParseException("Unexpected end of encoding","",__FILE__,__LINE__);
  131. len|=c;
  132. }
  133. resip::DataStream out(buf);
  134. while(len--){
  135. c=in.get();
  136. if(c==EOF)
  137. throw resip::ParseException("Unexpected end of encoding","",__FILE__,__LINE__);
  138. out.put(c);
  139. }
  140. out.flush();
  141. }
  142. s2c::PDU::~PDU()
  143. {
  144. }
  145. std::ostream&
  146. operator<<(std::ostream& strm, const s2c::PDU& pdu)
  147. {
  148. pdu.print(strm);
  149. return strm;
  150. }