format.h 3.55 KB
Newer Older
Niels Möller's avatar
Niels Möller committed
1
2
3
/* format.h
 *
 * Create a packet from a format string and arguments.
4
 *
5
 */
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

/* lsh, an implementation of the ssh protocol
 *
 * Copyright (C) 1998 Niels Möller
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
J.H.M. Dassen's avatar
J.H.M. Dassen committed
23
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
Niels Möller's avatar
Niels Möller committed
24
25
26
27
28
29
30
 */

#ifndef LSH_FORMAT_H_INCLUDED
#define LSH_FORMAT_H_INCLUDED

#include <stdarg.h>

Niels Möller's avatar
Niels Möller committed
31
32
#include "atoms.h"

Niels Möller's avatar
Niels Möller committed
33
34
35
36
37
38
/* Format strings can contain the following %-specifications:
 *
 * %%  Insert a %-sign
 *
 * %c  Insert an 8-bit character
 *
39
40
 * %i  Insert a 32-bit integer, in network byte order
 *
Niels Möller's avatar
Niels Möller committed
41
42
 * %s  Insert a string, given by a length and a pointer.
 *
Niels Möller's avatar
Niels Möller committed
43
44
 * %S  Insert a string, given as a struct lsh_string pointer.
 *
Niels Möller's avatar
Niels Möller committed
45
46
 * %z  Insert a string, using a null-terminated argument.
 *
Niels Möller's avatar
Niels Möller committed
47
48
 * %r  Reserves space in the string, first argument is the length, and
 *     the start position is stored into the second argument, a uint32_t *.
Niels Möller's avatar
Niels Möller committed
49
 *
Niels Möller's avatar
Niels Möller committed
50
51
 * %a  Insert a string containing one atom.
 *
52
53
 * %A  Insert a string containing a list of atoms. The input is an
 *     int_list object. Zero elements are allowed and ignored.
54
55
56
 *
 * %X  Insert a string containing a list of atoms. The corresponding
 *     argument sublist should be terminated with a zero. (Not used)
Niels Möller's avatar
Niels Möller committed
57
58
59
 *
 * %n  Insert a string containing a bignum.
 *
Niels Möller's avatar
Niels Möller committed
60
 * There are also some valid modifiers:
Niels Möller's avatar
Niels Möller committed
61
62
 *
 * "l" (as in literal). It is applicable to the s, a, A, n and r
63
64
 * specifiers, and outputs strings *without* a length field.
 *
65
66
 * "d" (as in decimal). For integers, convert the integer to decimal
 * digits. For strings, format the input string using sexp syntax;
67
68
 * i.e. prefixed with the length in decimal.
 *
69
70
71
72
 * "x" (as in heXadecimal). For strings, format each character as two
 * hexadecimal digits. Does not currently mean any thing for numbers.
 * Note that this modifier is orthogonal to the decimal modifier.
 * 
Niels Möller's avatar
Niels Möller committed
73
 * "f" (as in free). Frees the input string after it has been copied.
74
 * Applicable to %S only.
75
76
 *
 * "u" (as in unsigned). Used with bignums, to use unsigned-only
77
 * number format. */
Niels Möller's avatar
Niels Möller committed
78

79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#if DEBUG_ALLOC && __GNUC__

struct lsh_string *
ssh_format_clue(const char *clue, const char *format, ...);

#define ssh_format(format, ...) \
ssh_format_clue(__FILE__ ":" STRING_LINE, format, ## __VA_ARGS__)

#else /* !DEBUG_ALLOC */

struct lsh_string *
ssh_format(const char *format, ...);

#endif /* !DEBUG_ALLOC */

94
uint32_t ssh_format_length(const char *format, ...);
Niels Möller's avatar
Niels Möller committed
95
96
void
ssh_format_write(const char *format,
97
		      struct lsh_string *buffer, uint32_t pos, ...);
98

Niels Möller's avatar
Niels Möller committed
99
100
uint32_t
ssh_vformat_length(const char *format, va_list args);
101
102

void
Niels Möller's avatar
Niels Möller committed
103
104
ssh_vformat_write(const char *format,
		       struct lsh_string *buffer, uint32_t pos, va_list args);
Niels Möller's avatar
Niels Möller committed
105

106
     
Niels Möller's avatar
Niels Möller committed
107
/* Short cut */
108
#define make_string(s) (ssh_format("%lz", (s)))
109

Niels Möller's avatar
Niels Möller committed
110
111
unsigned
format_size_in_decimal(uint32_t n);
112

113

114
115
116
117
118
119
120
121
122
/* Helper functions for formatting particular ssh messages */
struct lsh_string *
format_disconnect(int code, const char *msg, 
		  const char *language);

struct lsh_string *
format_unimplemented(uint32_t seqno);


Niels Möller's avatar
Niels Möller committed
123
#endif /* LSH_FORMAT_H_INCLUDED */