summaryrefslogtreecommitdiff
path: root/encode.c
blob: acf24b72b67857807e1f8245d394df69149906d4 (plain)
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#include <stdlib.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>

#include "gob.h"
#include "encode.h"

static int sNextTypeId = 65;

unsigned long long flip_unsigned_long_long(unsigned long long ull) {
  ull = ((ull >> 8)  & 0x00FF00FF00FF00FF) | ((ull & 0x00FF00FF00FF00FF) << 8  );
  ull = ((ull >> 16) & 0x0000FFFF0000FFFF) | ((ull & 0x0000FFFF0000FFFF) << 16 );
  ull = ((ull >> 32)                     ) | ((ull                     ) << 32 );
  return ull;
}

int gob_allocate_type_id() {
  return sNextTypeId++;
}

// a return value of buf_size or more means that output
// was truncated.
int gob_encode_unsigned_long_long(char *buf, size_t buf_size, unsigned long long ull) {
  if (ull < 128) {
    if (buf_size >= 1) {
      *buf = (char)ull;
      return 1;
    }
  }
  unsigned char *ull_ptr = (char*)&ull;
  unsigned char *end_ptr = ull_ptr + (sizeof(unsigned long long)-1);
  char *write_ptr = buf + 1;
  int bytes_to_write = 1;
  int seen_first_bit = 0;
  // high byte first
  while (end_ptr >= ull_ptr) {
    if (*end_ptr != 0 || seen_first_bit) {
      seen_first_bit = 1;
      bytes_to_write++;
      if (bytes_to_write <= buf_size) {
	*write_ptr = *end_ptr;
	write_ptr++;
      }
    }
    end_ptr--;
  }
  if (buf_size >= 1) {
    *buf = -1*((char)bytes_to_write-1); // byte count omits first byte
  }
  return bytes_to_write;
}

int gob_encode_unsigned_int(char *buf, size_t buf_size, unsigned int i) {
  return gob_encode_unsigned_long_long(buf, buf_size, (unsigned long long)i);
}

int gob_encode_long_long(char *buf, size_t buf_size, long long i) {
  unsigned long long u;
  if (i < 0) {
    u = (~i << 1) | 1;	// complement i, bit 0 is 1
  } else {
    u = (i << 1);	// do not complement i, bit 0 is 0
  }
  return gob_encode_unsigned_long_long(buf, buf_size, u);
}

int gob_encode_int(char *buf, size_t buf_size, int i) {
  unsigned int u;
  if (i < 0) {
    u = (~i << 1) | 1;	// complement i, bit 0 is 1
  } else {
    u = (i << 1);	// do not complement i, bit 0 is 0
  }
  return gob_encode_unsigned_int(buf, buf_size, u);
}

int gob_encode_boolean(char *buf, size_t buf_size, int b) {
  return gob_encode_unsigned_long_long(buf, buf_size, b != 0);
}

int gob_encode_double(char *buf, size_t buf_size, double d) {
  unsigned long long ull = 0;
  memcpy((char*)&ull, &d, sizeof(double));
  unsigned long long rev_ull = flip_unsigned_long_long(ull);  

  return gob_encode_unsigned_long_long(buf, buf_size, rev_ull);
}

int gob_encode_string(char *buf, size_t buf_size, const char *s) {
  size_t len = strlen(s);
  int encoded_len_size = gob_encode_unsigned_int(buf, buf_size, len);
  buf += encoded_len_size;
  buf_size -= encoded_len_size;
  if (buf_size > 0) {
    strncpy(buf, s, buf_size);
  }
  return len + encoded_len_size;
}

int gob_start_type_definition(char *buf, size_t buf_size, int id, int type) {
  int total_size = 0;
  int num_bytes = 0;
  char *write_ptr = buf;

  num_bytes = gob_start_struct(write_ptr, buf_size);
  write_ptr += num_bytes;
  buf_size -= num_bytes;
  total_size += num_bytes;
  
  num_bytes = gob_encode_int(write_ptr, buf_size, -1*id);
  write_ptr += num_bytes;
  buf_size -= num_bytes;
  total_size += num_bytes;
  
  int type_delta = 0;
  switch (type) {
  case GOB_ARRAYTYPE_ID:
    type_delta = 1;
    break;
  case GOB_SLICETYPE_ID:
    type_delta = 2;
    break;
  case GOB_STRUCTTYPE_ID:
  default:
    type_delta = 3;
    break;
  case GOB_MAPTYPE_ID:
    type_delta = 4;
    break;
  }

  num_bytes = gob_encode_unsigned_int(write_ptr, buf_size, type_delta);
  write_ptr += num_bytes;
  buf_size -= num_bytes;
  total_size += num_bytes;

  return total_size;
}

int gob_end_type_definition(char *buf, size_t buf_size) {
  // just the end of the wireType struct
  return gob_end_struct(buf, buf_size);
}

int gob_start_struct_type(char *buf, size_t buf_size, const char *name, int id) {
  int total_size = 0;
  int num_bytes = 0;
  char *write_ptr = buf;

  num_bytes = gob_start_struct(write_ptr, buf_size);
  write_ptr += num_bytes;
  buf_size -= num_bytes;
  total_size += num_bytes;
  
  num_bytes = gob_encode_unsigned_int(write_ptr, buf_size, 1);
  write_ptr += num_bytes;
  buf_size -= num_bytes;
  total_size += num_bytes;
  
  num_bytes = gob_encode_common_type(write_ptr, buf_size, name, id);
  write_ptr += num_bytes;
  buf_size -= num_bytes;
  total_size += num_bytes;
  
  return total_size;
}

int gob_end_struct_type(char *buf, size_t buf_size) {
  // just the end of the wireType struct
  return gob_end_struct(buf, buf_size);
}

int gob_encode_string_int_helper(char *buf, size_t buf_size, const char *name, int id) {
  int total_size = 0;
  int num_bytes = 0;
  char *write_ptr = buf;

  num_bytes = gob_start_struct(write_ptr, buf_size);
  write_ptr += num_bytes;
  buf_size -= num_bytes;
  total_size += num_bytes;

  int fieldDelta = 1;
  if (name == NULL || *name == '\0') { // in go, null and "" are the same
    fieldDelta ++;
  } else {
    num_bytes = gob_encode_unsigned_int(write_ptr, buf_size, fieldDelta);
    write_ptr += num_bytes;
    buf_size -= num_bytes;
    total_size += num_bytes;
    num_bytes = gob_encode_string(write_ptr, buf_size, name);
    write_ptr += num_bytes;
    buf_size -= num_bytes;
    total_size += num_bytes;
  }
  if (id != 0) {
    num_bytes = gob_encode_unsigned_int(write_ptr, buf_size, fieldDelta);
    write_ptr += num_bytes;
    buf_size -= num_bytes;
    total_size += num_bytes;
    num_bytes = gob_encode_int(write_ptr, buf_size, id);
    write_ptr += num_bytes;
    buf_size -= num_bytes;
    total_size += num_bytes;
  }

  num_bytes = gob_end_struct(write_ptr, buf_size);
  write_ptr += num_bytes;
  buf_size -= num_bytes;
  total_size += num_bytes;
  
  return total_size;
}

int gob_encode_common_type(char *buf, size_t buf_size, const char *name, int id) {
  return gob_encode_string_int_helper(buf, buf_size, name, id);
}

int gob_encode_field_type(char *buf, size_t buf_size, const char *name, int id) {
  return gob_encode_string_int_helper(buf, buf_size, name, id);
}

int gob_encode_array_type(char *buf, size_t buf_size, const char *name, int id, int elem_type, int len) {
  int total_size = 0;
  char *write_ptr = buf;
  int num_bytes = gob_start_struct(write_ptr, buf_size);
  write_ptr += num_bytes;
  buf_size -= num_bytes;
  total_size += num_bytes;

  num_bytes = gob_encode_unsigned_int(write_ptr, buf_size, 1);
  write_ptr += num_bytes;
  buf_size -= num_bytes;
  total_size += num_bytes;

  num_bytes = gob_encode_common_type(write_ptr, buf_size, name, id);
  write_ptr += num_bytes;
  buf_size -= num_bytes;
  total_size += num_bytes;
  
  int field_delta = 1;
  if (elem_type == 0) {
    field_delta++;
  } else {
    num_bytes = gob_encode_unsigned_int(write_ptr, buf_size, field_delta);
    write_ptr += num_bytes;
    buf_size -= num_bytes;
    total_size += num_bytes;
    num_bytes = gob_encode_int(write_ptr, buf_size, elem_type);
    write_ptr += num_bytes;
    buf_size -= num_bytes;
    total_size += num_bytes;
  }

  if (len == 0) {
    field_delta++;
  } else {
    num_bytes = gob_encode_unsigned_int(write_ptr, buf_size, field_delta);
    write_ptr += num_bytes;
    buf_size -= num_bytes;
    total_size += num_bytes;
    num_bytes = gob_encode_int(write_ptr, buf_size, len);
    write_ptr += num_bytes;
    buf_size -= num_bytes;
    total_size += num_bytes;
  }

  num_bytes = gob_end_struct(write_ptr, buf_size);
  write_ptr += num_bytes;
  buf_size -= num_bytes;
  total_size += num_bytes;
  
  return total_size;
}

int gob_encode_slice_type(char *buf, size_t buf_size, const char *name, int id, int elem_type) {
  // array type will not output len if it is 0
  return gob_encode_array_type(buf, buf_size, name, id, elem_type, 0);
}

int gob_start_array(char *buf, size_t buf_size, size_t size) {
  return gob_encode_unsigned_int(buf, buf_size, size);
}

int gob_end_array(char *buf, size_t buf_size) {
  return 0;
}

int gob_start_slice(char *buf, size_t buf_size, size_t size) {
  return gob_encode_unsigned_int(buf, buf_size, size);
}

int gob_end_slice(char *buf, size_t buf_size) {
  return 0;
}

int gob_start_struct(char *buf, size_t buf_size) {
  return 0;
}

int gob_end_struct(char *buf, size_t buf_size) {
  if (buf_size >= 1) {
    *buf = '\0';
  }
  return 1;
}