format.c 5.05 KB
Newer Older
Niels Möller's avatar
Niels Möller committed
1
2
3
4
/* format.c
 *
 */

Niels Möller's avatar
Niels Möller committed
5
6
#include <assert.h>
#include <string.h>
Niels Möller's avatar
Niels Möller committed
7

Niels Möller's avatar
Niels Möller committed
8
9
10
11
12
#include "format.h"
#include "werror.h"
#include "xalloc.h"

struct lsh_string *ssh_format(char *format, ...)
Niels Möller's avatar
Niels Möller committed
13
14
15
{
  va_list args;
  UINT32 length;
Niels Möller's avatar
Niels Möller committed
16
  struct lsh_string *packet;
Niels Möller's avatar
Niels Möller committed
17
18
19
20
21

  va_start(args, format);
  length = ssh_vformat_length(format, args);
  va_end(args);

Niels Möller's avatar
Niels Möller committed
22
  packet = lsh_string_alloc(length);
Niels Möller's avatar
Niels Möller committed
23
24
25
26
27
28
29

  va_start(args, format);
  ssh_vformat(format, packet->data, args);
  va_end(args);

  return packet;
}
Niels Möller's avatar
Niels Möller committed
30
 
Niels Möller's avatar
Niels Möller committed
31
UINT32 ssh_vformat_length(char *f, va_list args)
Niels Möller's avatar
Niels Möller committed
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
{
  UINT32 length = 0;

  while(*f)
    {
      if (*f == '%')
	{
	  int literal = 0;
	  f++;
	  if (*f == 'l')
	    {
	      literal = 1;
	      f++;
	    }
	  switch(*f)
	    {
	    default:
	      fatal("ssh_vformat_length: bad format string");
	      break;

	    case 'c':
	      (void) va_arg(args, UINT8);
	      /* Fall through */
	    case '%':
	      f++;
	      length++;
	      break;

	    case 'i':
	      (void) va_arg(args, UINT32);
	      f++;
	      length += 4;
	      break;

	    case 's':
	      {
Niels Möller's avatar
Niels Möller committed
68
		length += va_arg(args, UINT32); /* String length */
Niels Möller's avatar
Niels Möller committed
69

Niels Möller's avatar
Niels Möller committed
70
		(void) va_arg(args, UINT8 *);    /* data */
Niels Möller's avatar
Niels Möller committed
71
72
73
74
75
76

		f++;
		
		if (!literal)
		  length += 4;
	      }
Niels Möller's avatar
Niels Möller committed
77
78
79
80
81
82
83
84
	      break;
	    case 'S':
	      length += va_arg(args, struct lsh_string *)->length;
	      f++;

	      if (!literal)
		length += 4;
	      
Niels Möller's avatar
Niels Möller committed
85
	      break;
Niels Möller's avatar
Niels Möller committed
86
87
88
89
90
91
92
	    case 'z':
	      length += strlen(va_arg(args, char*));
	      f++;

	      if (!literal)
		length += 4;
	      break;
Niels Möller's avatar
Niels Möller committed
93
94
95
96
97
98
99
100
101
	    case 'r':
	      length += va_arg(args, struct lsh_string *)->length;
	      (void) va_arg(args, UINT8 **);    /* pointer */

	      f++;

	      if (!literal)
		length += 4;
	      break;
Niels Möller's avatar
Niels Möller committed
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
	    case 'a':
	      {
		int atom = va_arg(args, int);

		assert(atom);

		length += get_atom_length(atom);

		if (!literal)
		  length += 4;
		f++;
	      }
	    break;
	    case 'A':
	      {
		int atom;

Niels Möller's avatar
Niels Möller committed
119
		while ( (atom = va_arg(args, int)) )
Niels Möller's avatar
Niels Möller committed
120
121
122
123
124
125
126
127
128
129
130
131
		  length += get_atom_length(atom) + 1;

		/* One ','-character less than the number of atoms */
		length--;
		
		if (!literal)
		  length += 4;
		f++;
	      }
	    break;
	    case 'n':
	      {
Niels Möller's avatar
Niels Möller committed
132
		bignum *n = va_arg(args, bignum *);
Niels Möller's avatar
Niels Möller committed
133
134

		/* Calculate length of written number */
Niels Möller's avatar
Niels Möller committed
135
		length += bignum_format_length(n);
Niels Möller's avatar
Niels Möller committed
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152

		if (!literal)
		  length += 4;
		f++;
	      }
	    break;
	    }
	}
      else
	{
	  length++;
	  f++;
	}
    }
  return length;
}

Niels Möller's avatar
Niels Möller committed
153
void ssh_vformat(char *f, UINT8 *buffer, va_list args)
Niels Möller's avatar
Niels Möller committed
154
{
Niels Möller's avatar
Niels Möller committed
155
  while(*f)
Niels Möller's avatar
Niels Möller committed
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
    {
      if (*f == '%')
	{
	  int literal = 0;
	  f++;
	  if (*f == 'l')
	    {
	      literal = 1;
	      f++;
	    }
	  switch(*f)
	    {
	    default:
	      fatal("ssh_vformat: bad format string");
	      break;

	    case 'c':
	      *buffer++ = va_arg(args, UINT8);
	      f++;
	      break;
	    case '%':
	      *buffer++ = '%';
	      f++;
	      break;

	    case 'i':
	      {
		UINT32 i = va_arg(args, UINT32);
		WRITE_UINT32(buffer, i);
		buffer += 4;
		f++;
	      }
	    break;
	    case 's':
	      {
Niels Möller's avatar
Niels Möller committed
191
192
		UINT32 length = va_arg(args, UINT32);
		UINT8 *data = va_arg(args, UINT8 *);
Niels Möller's avatar
Niels Möller committed
193
194
195
196
197
198
199
200
201
202
203
204

		if (!literal)
		  {
		    WRITE_UINT32(buffer, length);
		    buffer += 4;
		  }

		memcpy(buffer, data, length);
		buffer += length;
		f++;
	      }
	    break;
Niels Möller's avatar
Niels Möller committed
205
206
207
208
209
210
211
212
213
214
215
216
217
	    case 'S':
	      {
		struct lsh_string *s = va_arg(args, struct lsh_string *);

		if (!literal)
		  {
		    WRITE_UINT32(buffer, s->length);
		    buffer += 4;
		  }

		memcpy(buffer, s->data, s->length);
		buffer += s->length;
		f++;
Niels Möller's avatar
Niels Möller committed
218
		break;
Niels Möller's avatar
Niels Möller committed
219
	      }
Niels Möller's avatar
Niels Möller committed
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
	    case 'z':
	      {
		char *s = va_arg(args, char *);
		UINT32 length = strlen(s);
		if (!literal)
		  {
		    WRITE_UINT32(buffer, length);
		    buffer += 4;
		  }

		memcpy(buffer, s, length);
		buffer += length;
		f++;
		break;
	      }
Niels Möller's avatar
Niels Möller committed
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
	    case 'r':
	      {
		UINT32 length = va_arg(args, UINT32);
		UINT8 **p = va_arg(args, UINT8 **);

		if (!literal)
		  {
		    WRITE_UINT32(buffer, length);
		    buffer += 4;
		  }

		if (p)
		  *p = buffer;
		buffer += length;
		f++;
	      }
	    
Niels Möller's avatar
Niels Möller committed
252
253
	    case 'a':
	      {
Niels Möller's avatar
Niels Möller committed
254
		UINT32 length;
Niels Möller's avatar
Niels Möller committed
255
		int atom = va_arg(args, int);
Niels Möller's avatar
Niels Möller committed
256
		
Niels Möller's avatar
Niels Möller committed
257
258
		assert(atom);

Niels Möller's avatar
Niels Möller committed
259
		length = get_atom_length(atom);
Niels Möller's avatar
Niels Möller committed
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

		if (!literal)
		  {
		    WRITE_UINT32(buffer, length);
		    buffer += 4;
		  }

		memcpy(buffer, get_atom_name(atom), length);
		buffer += length;
		f++;
	      }
	    break;
	    case 'A':
	      {
		int atom;
		UINT8 *start = buffer; /* Where to store the length */
		
		if (!literal)
		  buffer += 4;

		atom = va_arg(args, int);
		if (atom)
		  {
		    UINT32 length = get_atom_length(atom);
		    memcpy(buffer, get_atom_name(atom), length);
		    buffer += length;

Niels Möller's avatar
Niels Möller committed
287
		    while ( (atom = va_arg(args, int)) )
Niels Möller's avatar
Niels Möller committed
288
289
290
291
292
293
294
295
296
297
		      {
			*buffer++ = ',';
			length = get_atom_length(atom);
			memcpy(buffer, get_atom_name(atom), length);
			buffer += length;
		      }
		  }
				
		if (!literal)
		  {
Niels Möller's avatar
Niels Möller committed
298
		    UINT32 total = buffer - start - 4;
Niels Möller's avatar
Niels Möller committed
299
300
301
302
303
304
305
		    WRITE_UINT32(start, total);
		  }
		f++;
	      }
	    break;
	    case 'n':
	      {
Niels Möller's avatar
Niels Möller committed
306
		bignum *n = va_arg(args, bignum *);
Niels Möller's avatar
Niels Möller committed
307
308
309
		UINT32 length = bignum_format(n, buffer);

		buffer += length;
Niels Möller's avatar
Niels Möller committed
310
311
312
313
314
315
316
317
318
319
320
321
322
323

		if (!literal)
		  {
		    WRITE_UINT32(buffer, length);
		    buffer += 4;
		  }

		f++;
	      }
	    break;
	    }
	}
      *buffer++ = *f++;
    }
Niels Möller's avatar
Niels Möller committed
324
}