~ubuntu-branches/ubuntu/saucy/resiprocate/saucy-proposed

« back to all changes in this revision

Viewing changes to p2p/s2c/s2c/s2c_native.cxx

  • Committer: Package Import Robot
  • Author(s): Daniel Pocock
  • Date: 2012-05-17 19:29:59 UTC
  • Revision ID: package-import@ubuntu.com-20120517192959-vv00m77isztdy64q
Tags: upstream-1.8.2
ImportĀ upstreamĀ versionĀ 1.8.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
   s2c_native.cxx
 
3
 
 
4
   Copyright (C) 2008, RTFM, Inc.
 
5
   All Rights Reserved.
 
6
 
 
7
   ekr@rtfm.com  Fri May  2 17:04:55 2008
 
8
*/
 
9
 
 
10
 
 
11
 
 
12
#include <assert.h>
 
13
#include "rutil/Data.hxx"
 
14
#include "rutil/DataStream.hxx"
 
15
#include "rutil/ParseException.hxx"
 
16
#include "s2c_native.hxx"
 
17
 
 
18
void s2c::encode_uintX(std::ostream& out, const unsigned int bits, const u_int64 value)
 
19
  {
 
20
    int size;
 
21
 
 
22
    assert((bits%8)==0);
 
23
    assert(bits<=64);
 
24
 
 
25
    size=bits/8;
 
26
    
 
27
    switch(size){
 
28
      case 8:
 
29
        out.put(((value>>56)&0xff));
 
30
      case 7:
 
31
        out.put(((value>>48)&0xff));
 
32
      case 6:
 
33
        out.put(((value>>40)&0xff));
 
34
      case 5:
 
35
        out.put(((value>>32)&0xff));
 
36
      case 4:
 
37
        out.put(((value>>24)&0xff));
 
38
      case 3:
 
39
        out.put(((value>>16)&0xff));
 
40
      case 2:
 
41
        out.put(((value>>8)&0xff));
 
42
      case 1:
 
43
        out.put(value&0xff);
 
44
        break;
 
45
      default:
 
46
        assert(1==0);
 
47
    }
 
48
  }
 
49
  
 
50
 
 
51
void s2c::decode_uintX(std::istream& in, const unsigned int bits, u_char &value)
 
52
  {
 
53
    int size;
 
54
    int c;      
 
55
    
 
56
    assert(bits<=8);
 
57
 
 
58
    size=bits/8;
 
59
    
 
60
    value=0;
 
61
 
 
62
    while(size--){
 
63
      value <<=8;
 
64
      c=in.get();
 
65
     
 
66
      if(c==EOF)
 
67
        throw resip::ParseException("Unexpected end of encoding","",__FILE__,__LINE__);
 
68
      value |= c;
 
69
    }
 
70
  }
 
71
 
 
72
 
 
73
/*void s2c::decode_uintX(std::istream& in, const unsigned int bits, u_int8 &value)
 
74
  {
 
75
    int size;
 
76
    int c;      
 
77
    
 
78
    assert(bits==8);
 
79
 
 
80
    size=bits/8;
 
81
    
 
82
    value=0;
 
83
 
 
84
    while(size--){
 
85
      value <<=8;
 
86
      c=in->get();
 
87
      value |= c;
 
88
    }
 
89
  }
 
90
*/
 
91
void s2c::decode_uintX(std::istream& in, const unsigned int bits, u_int16 &value)
 
92
  {
 
93
    int size;
 
94
    int c;
 
95
    
 
96
    assert(bits<=16);
 
97
 
 
98
    size=bits/8;
 
99
    
 
100
    value=0;
 
101
 
 
102
    while(size--){
 
103
      value <<=8;
 
104
      c=in.get();
 
105
      if(c==EOF)
 
106
        throw resip::ParseException("Unexpected end of encoding","",__FILE__,__LINE__);
 
107
 
 
108
 
 
109
      value |= c;
 
110
    }
 
111
  }
 
112
 
 
113
void s2c::decode_uintX(std::istream& in, const unsigned int bits, u_int32 &value)
 
114
  {
 
115
    int size;
 
116
    int c;
 
117
    
 
118
    assert(bits<=32);
 
119
 
 
120
    size=bits/8;
 
121
    
 
122
    value=0;
 
123
 
 
124
    while(size--){
 
125
      value <<=8;
 
126
      
 
127
      c=in.get();
 
128
      if(c==EOF)
 
129
        throw resip::ParseException("Unexpected end of encoding","",__FILE__,__LINE__);
 
130
 
 
131
      value |= c;
 
132
    }
 
133
  }
 
134
 
 
135
void s2c::decode_uintX(std::istream& in, const unsigned int bits, u_int64 &value)
 
136
  {
 
137
    int size;
 
138
    int c;
 
139
    
 
140
    assert(bits<=64);
 
141
 
 
142
    size=bits/8;
 
143
    
 
144
    value=0;
 
145
 
 
146
    while(size--){
 
147
      value <<=8;
 
148
      c=in.get();
 
149
      if(c==EOF)
 
150
        throw resip::ParseException("Unexpected end of encoding","",__FILE__,__LINE__);
 
151
      value |= c;
 
152
    }
 
153
  }
 
154
    
 
155
void s2c::do_indent(std::ostream& out, int indent)
 
156
 {
 
157
   while(indent--)  out  << ' ';
 
158
 }
 
159
 
 
160
// This is really clumsy, but I don't understand rutil
 
161
// TODO: !ekr! cleanup
 
162
void s2c::read_varray1(std::istream& in, unsigned int lenlen, resip::Data &buf)
 
163
  {
 
164
    u_int64 len=0;
 
165
    int c;
 
166
    
 
167
    // First read the length
 
168
    assert(lenlen<=8);
 
169
    while(lenlen--){
 
170
      len<<=8;
 
171
      c=in.get();
 
172
      if(c==EOF)
 
173
        throw resip::ParseException("Unexpected end of encoding","",__FILE__,__LINE__);
 
174
      len|=c;
 
175
    }
 
176
    
 
177
    resip::DataStream out(buf);
 
178
    
 
179
    while(len--){
 
180
      c=in.get();
 
181
      if(c==EOF)
 
182
        throw resip::ParseException("Unexpected end of encoding","",__FILE__,__LINE__);
 
183
 
 
184
      out.put(c);
 
185
    }
 
186
    
 
187
    out.flush();
 
188
  }
 
189
 
 
190
s2c::PDU::~PDU()
 
191
{
 
192
}
 
193
 
 
194
std::ostream& 
 
195
operator<<(std::ostream& strm, const s2c::PDU& pdu)
 
196
{
 
197
   pdu.print(strm);
 
198
   return strm;
 
199
}
 
200