format.h 3.57 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
struct lsh_string *ssh_format(const char *format, ...);
80
uint32_t ssh_format_length(const char *format, ...);
81
82
void ssh_format_write(const char *format,
		      struct lsh_string *buffer, uint32_t pos, ...);
83

84
uint32_t ssh_vformat_length(const char *format, va_list args);
85
86
87
88
89
90
void ssh_vformat_write(const char *format,
		       struct lsh_string *buffer, uint32_t pos, va_list args);

void
format_hex_string(struct lsh_string *buffer, uint32_t pos,
		  uint32_t length, const uint8_t *data);
Niels Möller's avatar
Niels Möller committed
91

92
     
93
/* Short cuts */
Niels Möller's avatar
Niels Möller committed
94
95
#define lsh_string_dup(s) (ssh_format("%lS", (s)))

96
#define make_string(s) (ssh_format("%lz", (s)))
97

98
unsigned format_size_in_decimal(uint32_t n);
99
100
101
void
format_decimal(struct lsh_string *buffer, uint32_t pos,
	       uint32_t length, uint32_t n);
102

103
104
/* FIXME: These functions don't really belong here */

105

Niels Möller's avatar
Niels Möller committed
106
struct lsh_string *
107
lsh_string_colonize(const struct lsh_string *s, int every, int freeflag);
108

Niels Möller's avatar
Niels Möller committed
109
struct lsh_string *
110
lsh_string_bubblebabble(const struct lsh_string *s, int freeflag);
111

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