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
  
#pike __REAL_VERSION__ 
#pragma strict_types 
 
//! String buffer with the possibility to read and write data 
//! as they would be formatted in structs. 
 
inherit Stdio.Buffer; 
 
this_program add_hstring(string(8bit)|Stdio.Buffer str, int sz) 
{ 
  return [object(this_program)]::add_hstring(str,sz); 
} 
 
//! Create a new buffer, optionally initialized with the 
//! value @[s]. 
void create(void|string(0..255) s) 
{ 
  if( s && strlen(s) ) 
    ::create(s); 
  else 
    ::create(); 
  set_error_mode(.BufferError); 
} 
 
//! Adds the data @[s] verbatim to the end of the buffer. 
this_program add_data(string(0..255) s) 
{ 
  return [object(this_program)]add(s); 
} 
 
//! Appends an unsigned integer in network order to the buffer. 
//! 
//!  @param i 
//!    Unsigned integer to append. 
//!  @param len 
//!    Length of integer in bytes. 
this_program put_uint(int i, int(0..) len) 
{ 
  return [object(this_program)]add_int(i,len); 
} 
 
//! 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. 
this_program put_bignum(Gmp.mpz i, int(0..)|void len_width) 
{ 
  return [object(this_program)]add_hstring(i->digits(256),len_width||2); 
} 
 
//! Appends the fix sized string @[s] to the buffer. 
this_program put_fix_string(string(0..255) s) 
{ 
  return [object(this_program)]add(s); 
} 
 
//! Appends an array of unsigned integers of width @[item_size] 
//! to the buffer. 
this_program put_fix_uint_array(array(int) data, int(0..) item_size) 
{ 
  return [object(this_program)]add_ints(data,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 in bytes. 
this_program put_var_uint_array(array(int) data, int(0..) item_size, int(0..) len) 
{ 
  add_int(sizeof(data)*item_size, len ); 
  return [object(this_program)]add_ints(data,item_size); 
} 
 
//! Appends an array of variable length strings with @[item_size] 
//! bytes hollerith coding, prefixed by a @[len] bytes large integer 
//! declaring the total size of the array in bytes. 
this_program put_var_string_array(array(string(8bit)) data, int(0..) item_size, int(0..) len) 
{ 
  Stdio.Buffer sub = Stdio.Buffer(); 
  foreach(data, string(8bit) s) 
    sub->add_hstring(s, item_size); 
  add_int(sizeof(sub),len); 
  return [object(this_program)]add(sub); 
} 
 
//! Reads an unsigned integer from the buffer. 
int(0..) get_uint(int len) 
{ 
  return read_int(len); 
} 
 
//! Reads a fixed sized string of length @[len] from the buffer. 
string(0..255) get_fix_string(int len) 
{ 
  return read(len); 
} 
 
//! Reads a string written by @[put_var_string] from the buffer. 
string(0..255) get_var_string(int len) 
{ 
  return read_hstring(len); 
} 
 
//! Reads a bignum written by @[put_bignum] from the buffer. 
Gmp.mpz get_bignum(int|void len) 
{ 
  return Gmp.mpz(read_hstring(len||2),256); 
} 
 
//! Get the remaining data from the buffer and clears the buffer. 
string(0..255) get_rest() 
{ 
  return read(); 
} 
 
//! 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) 
{ 
  return read_ints(size, item_size); 
} 
 
//! 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) 
{ 
  int size = read_int(len); 
  int elems = size/item_size; 
  if( elems*item_size != size ) 
    throw(.BufferError("Impossible uint array length value.\n")); 
  return read_ints(elems, item_size); 
} 
 
//! Returns one if there is any more data to read. 
int(0..1) is_empty() 
{ 
  return !sizeof(this); 
}