1
  
2
  
3
  
4
  
5
  
6
  
7
  
8
  
9
  
10
  
11
  
12
  
13
  
14
  
15
  
16
  
17
  
18
  
19
  
20
  
21
  
22
  
23
  
24
  
25
  
26
  
27
  
28
  
29
  
30
  
31
  
32
  
33
  
34
  
35
  
36
  
37
  
38
  
39
  
40
  
41
  
42
  
43
  
44
  
45
  
46
  
47
  
48
  
49
  
50
  
51
  
52
  
53
  
54
  
55
  
56
  
57
  
58
  
59
  
60
  
61
  
62
  
63
  
64
  
65
  
66
  
67
  
68
  
69
  
70
  
71
  
72
  
73
  
74
  
75
  
76
  
77
  
78
  
79
  
80
  
81
  
82
  
83
  
84
  
85
  
86
  
87
  
88
  
89
  
90
  
91
  
92
  
93
  
94
  
95
  
96
  
97
  
98
  
99
  
100
  
101
  
102
  
103
  
104
  
105
  
106
  
107
  
108
  
109
  
110
  
111
  
112
  
113
  
114
  
115
  
116
  
117
  
118
  
119
  
120
  
121
  
122
  
123
  
124
  
125
  
126
  
127
  
128
  
129
  
130
  
131
  
132
  
133
  
134
  
135
  
136
  
137
  
138
  
139
  
140
  
141
  
142
  
143
  
144
  
145
  
146
  
147
  
148
  
149
  
150
  
151
  
152
  
153
  
154
  
155
  
156
  
157
  
158
  
159
  
160
  
161
  
162
  
163
  
164
  
165
  
166
  
167
  
168
  
169
  
170
  
171
  
172
  
173
  
174
  
175
  
176
  
177
  
178
  
179
  
180
  
181
  
182
  
183
  
184
  
#pike __REAL_VERSION__ 
#pragma strict_types 
 
//! Various Abstract Data Types. 
 
#if constant(_ADT) 
inherit _ADT; 
#endif /* _ADT */ 
 
constant List = __builtin.List; 
 
// Internal stuff for ADT.Struct 
protected int item_counter; 
int get_item_id() { return item_counter++; } 
 
//! String buffer with the possibility to read and write data 
//! as they would be formatted in structs. 
class struct { 
 
  string(0..255) buffer; 
  int index; 
 
  //! Create a new buffer, optionally initialized with the 
  //! value @[s]. 
  void create(void|string(0..255) s) 
  { 
    buffer = s || ""; 
    index = 0; 
  } 
 
  //! Trims the buffer to only contain the data after the 
  //! read pointer and returns the contents of the buffer. 
  string(0..255) contents() 
  { 
    buffer = buffer[index..]; 
    index = 0; 
    return buffer; 
  } 
 
  //! Adds the data @[s] verbatim to the end of the buffer. 
  void add_data(string(0..255) s) 
  { 
    buffer += s; 
  } 
 
  //! Return all the data in the buffer and empties it. 
  string(0..255) pop_data() 
  { 
    string(0..255) res = buffer; 
    create(); 
    return res; 
  } 
 
  //! Appends an unsigned integer in network order to the buffer. 
  //! 
  //!  @param i 
  //!    Unsigned integer to append. 
  //!  @param len 
  //!    Length of integer in bytes. 
  void put_uint(int i, int(0..) len) 
  { 
    if (i<0) 
      error("Negative argument.\n"); 
    add_data([string(0..255)]sprintf("%*c", len, i)); 
  } 
 
  //! Appends a variable stringĀ @[s] preceded with an unsigned integer 
  //! of the size @[len_width] declaring the length of the string. The 
  //! string @[s] should be 8 bits wide. 
  void put_var_string(string(0..255) s, int(0..) len_width) 
  { 
    add_data([string(0..255)]sprintf("%*H", len_width, s)); 
  } 
 
  //! Appends a bignum @[i] as a variable string preceded with an 
  //! unsigned integer of the size @[len_width] declaring the length 
  //! of the string. @[len_width] defaults to 2. 
  void put_bignum(Gmp.mpz i, int(0..)|void len_width) 
  { 
    if (i<0) 
      error("Negative argument.\n"); 
    put_var_string(i->digits(256), len_width || 2); 
  } 
 
  //! Appends the fix sized string @[s] to the buffer. 
  void put_fix_string(string(0..255) s) 
  { 
    add_data(s); 
  } 
 
  //! Appends an array of unsigned integers of width @[item_size] 
  //! to the buffer. 
  void put_fix_uint_array(array(int) data, int(0..) item_size) 
  { 
    foreach(data, int i) 
      put_uint(i, item_size); 
  } 
 
  //! Appends an array of unsigned integers of width @[item_size] 
  //! to the buffer, preceded with an unsigned integer @[len] declaring 
  //! the size of the array. 
  void put_var_uint_array(array(int) data, int(0..) item_size, int(0..) len) 
  { 
    put_uint(sizeof(data), len); 
    put_fix_uint_array(data, item_size); 
  } 
 
  //! Reads an unsigned integer from the buffer. 
  int(0..) get_uint(int len) 
  { 
    int(0..) i; 
    if ( (sizeof(buffer) - index) < len) 
      error("No data.\n"); 
    sscanf(buffer, "%*" + (string) index +"s%" + (string) len + "c", i); 
    index += len; 
    return i; 
  } 
 
  //! Reads a fixed sized string of length @[len] from the buffer. 
  string(0..255) get_fix_string(int len) 
  { 
    if ((sizeof(buffer) - index) < len) 
      error("No data\n"); 
 
    string(0..255) res = buffer[index .. index + len - 1]; 
    index += len; 
    return res; 
  } 
 
  //! Reads a string written by @[put_var_string] from the buffer. 
  string(0..255) get_var_string(int len) 
  { 
    return get_fix_string(get_uint(len)); 
  } 
 
  //! Reads a bignum written by @[put_bignum] from the buffer. 
  Gmp.mpz get_bignum(int|void len) 
  { 
    return Gmp.mpz(get_var_string(len || 2), 256); 
  } 
 
  //! Get the remaining data from the buffer and clears the buffer. 
  string(0..255) get_rest() 
  { 
    string(0..255) s = buffer[index..]; 
    create(); 
    return s; 
  } 
 
  //! Reads an array of integers as written by @[put_fix_uint_array] 
  //! from the buffer. 
  array(int) get_fix_uint_array(int item_size, int size) 
  { 
    array(int) res = allocate(size); 
    for(int i = 0; i<size; i++) 
      res[i] = get_uint(item_size); 
    return res; 
  } 
 
  //! Reads an array of integers as written by @[put_var_uint_array] 
  //! from the buffer. 
  array(int) get_var_uint_array(int item_size, int len) 
  { 
    return get_fix_uint_array(item_size, get_uint(len)); 
  } 
 
  //! Returns one of there is any more data to read. 
  int(0..1) is_empty() 
  { 
    return (index == sizeof(buffer)); 
  } 
 
  protected int(0..) _sizeof() 
  { 
    return [int(0..)](sizeof(buffer)-index); 
  } 
 
  protected string _sprintf(int t) 
  { 
    return t=='O' && sprintf("%O(%O)", this_program, _sizeof()); 
  } 
}