buffer.h 4.31 KB
Newer Older
1
2
3
4
5
/* buffer.h
 *
 * Buffering for sftp.
 */

Niels Möller's avatar
Niels Möller committed
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/* lsh, an implementation of the ssh protocol
 *
 * Copyright (C) 2001 Niels Mller
 *
 * 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
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */

24
25
26
#ifndef SFTP_BUFFER_H_INCLUDED
#define SFTP_BUFFER_H_INCLUDED

Niels Möller's avatar
Niels Möller committed
27
/* Basic configuration */
28

Niels Möller's avatar
Niels Möller committed
29
30
31
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
32

Niels Möller's avatar
Niels Möller committed
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
/* For off_t */
#include <sys/types.h>

#if SIZEOF_SHORT >= 4
# define UINT32 unsigned short
#elif SIZEOF_INT >= 4
# define UINT32 unsigned int
#elif SIZEOF_LONG >= 4
# define UINT32 unsigned long
#else
# error No suitable type found to use for UINT32
#endif /* UINT32 */
 
#if SIZEOF_SHORT >= 2
# define UINT16 unsigned short
#elif SIZEOF_INT >= 2
# define UINT16 unsigned int
#else
# error No suitable type found to use for UINT16
#endif  /* UINT16 */
 
#define UINT8 unsigned char

#if __GNUC__ && HAVE_GCC_ATTRIBUTE
# define NORETURN __attribute__ ((__noreturn__))
# define PRINTF_STYLE(f, a) __attribute__ ((__format__ (__printf__, f, a)))
# define UNUSED __attribute__ ((__unused__))
#else
# define NORETURN
# define PRINTF_STYLE(f, a)
# define UNUSED
#endif
65

Niels Möller's avatar
Niels Möller committed
66
67
68

/* Abstract input and output functions */
#include <time.h>
69

Niels Möller's avatar
Niels Möller committed
70
71
struct sftp_input;
struct sftp_output;
72

Niels Möller's avatar
Niels Möller committed
73
/* Input */
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88

/* Returns 1 of all was well, 0 on error, and -1 on EOF */
int
sftp_read_packet(struct sftp_input *i);

int
sftp_get_data(struct sftp_input *i, UINT32 length, UINT8 *data);

int
sftp_get_uint8(struct sftp_input *i, UINT8 *value);

int
sftp_get_uint32(struct sftp_input *i, UINT32 *value);

int
Niels Möller's avatar
Niels Möller committed
89
sftp_get_uint64(struct sftp_input *i, off_t *value);
90

Niels Möller's avatar
Niels Möller committed
91
92
/* Allocates storage. Caller must deallocate using
 * sftp_free_string. */
93
94
95
UINT8 *
sftp_get_string(struct sftp_input *i, UINT32 *length);

96
97
98
99
100
101
102
103
104
105
106
void
sftp_free_string(UINT8 *s);

/* Like sftp_get_string, but the data is deallocated automatically by
 * sftp_read_packet and sftp_input_clear_strings. */
UINT8 *
sftp_get_string_auto(struct sftp_input *i, UINT32 *length);

void
sftp_input_clear_strings(struct sftp_input *i);

107
108
109
110
int
sftp_get_eod(struct sftp_input *i);

/* Output */
Niels Möller's avatar
Niels Möller committed
111
112
113
114
115
116
117
118
119
120

void
sftp_set_msg(struct sftp_output *o, UINT8 msg);

void
sftp_set_id(struct sftp_output *o, UINT32 id);

int
sftp_write_packet(struct sftp_output *o);

121
void
122
123
sftp_put_data(struct sftp_output *o, UINT32 length, const UINT8 *data);

124
void
125
126
sftp_put_uint8(struct sftp_output *o, UINT8 value);

127
void
128
129
sftp_put_uint32(struct sftp_output *o, UINT32 value);

130
void
Niels Möller's avatar
Niels Möller committed
131
sftp_put_uint64(struct sftp_output *o, off_t value);
132

133
void
Niels Möller's avatar
Niels Möller committed
134
sftp_put_string(struct sftp_output *o, UINT32 length, const UINT8 *data);
135

136
137
138
/* Returns index. */
UINT32
sftp_put_reserve_length(struct sftp_output *o);
139

140
141
142
void
sftp_put_final_length(struct sftp_output *o,
		      UINT32 index);
143

144
145
146
147
148
void
sftp_put_length(struct sftp_output *o,
		UINT32 index,
		UINT32 length);

Niels Möller's avatar
Niels Möller committed
149
150
151
152
void
sftp_put_reset(struct sftp_output *o,
	       UINT32 index);

153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
UINT32
sftp_put_printf(struct sftp_output *o, const char *format, ...)
     PRINTF_STYLE(2,3);
     
UINT32
sftp_put_strftime(struct sftp_output *o, UINT32 size,
		  const char *format,
		  const struct tm *tm);

     
/* Constructed types. */

struct sftp_attrib
{
  UINT32 flags;
Niels Möller's avatar
Niels Möller committed
168
  off_t size;
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
  UINT32 uid;
  UINT32 gid;
  UINT32 permissions;

  /* NOTE: The representations of times is about to change. */
  UINT32 atime;
  UINT32 mtime;
};

void
sftp_clear_attrib(struct sftp_attrib *a);

int
sftp_get_attrib(struct sftp_input *i, struct sftp_attrib *a);

void
sftp_put_attrib(struct sftp_output *o, const struct sftp_attrib *a);

int
sftp_skip_extension(struct sftp_input *i);
189

Niels Möller's avatar
Niels Möller committed
190
191
192
193
194
195
196
197
198
199

/* Simple input and output objects based on FILE * */
#include <stdio.h>

struct sftp_input *
sftp_make_input(FILE *f);

struct sftp_output *
sftp_make_output(FILE *f);

200
201
202
int
sftp_packet_size(struct sftp_output* out);

Niels Möller's avatar
Niels Möller committed
203

204
#endif /* SFTP_BUFFER_H_INCLUDED */