* fix to display correct form information in REG_BINARY information
[Samba.git] / source / libsmb / clireadwrite.c
blob756a6cce2f9ef9bf10cdad11670ed470b69fccef
1 /*
2 Unix SMB/CIFS implementation.
3 client file read/write routines
4 Copyright (C) Andrew Tridgell 1994-1998
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #define NO_SYSLOG
23 #include "includes.h"
25 /****************************************************************************
26 Issue a single SMBread and don't wait for a reply.
27 ****************************************************************************/
29 static BOOL cli_issue_read(struct cli_state *cli, int fnum, off_t offset,
30 size_t size, int i)
32 memset(cli->outbuf,'\0',smb_size);
33 memset(cli->inbuf,'\0',smb_size);
35 set_message(cli->outbuf,10,0,True);
37 SCVAL(cli->outbuf,smb_com,SMBreadX);
38 SSVAL(cli->outbuf,smb_tid,cli->cnum);
39 cli_setup_packet(cli);
41 SCVAL(cli->outbuf,smb_vwv0,0xFF);
42 SSVAL(cli->outbuf,smb_vwv2,fnum);
43 SIVAL(cli->outbuf,smb_vwv3,offset);
44 SSVAL(cli->outbuf,smb_vwv5,size);
45 SSVAL(cli->outbuf,smb_vwv6,size);
46 SSVAL(cli->outbuf,smb_mid,cli->mid + i);
48 return cli_send_smb(cli);
51 /****************************************************************************
52 Read size bytes at offset offset using SMBreadX.
53 ****************************************************************************/
55 ssize_t cli_read(struct cli_state *cli, int fnum, char *buf, off_t offset, size_t size)
57 char *p;
58 int size2;
59 int readsize;
60 ssize_t total = 0;
62 if (size == 0)
63 return 0;
66 * Set readsize to the maximum size we can handle in one readX,
67 * rounded down to a multiple of 1024.
70 readsize = (cli->max_xmit - (smb_size+32)) & ~1023;
72 while (total < size) {
73 readsize = MIN(readsize, size-total);
75 /* Issue a read and receive a reply */
77 if (!cli_issue_read(cli, fnum, offset, readsize, 0))
78 return -1;
80 if (!cli_receive_smb(cli))
81 return -1;
83 /* Check for error. Make sure to check for DOS and NT
84 errors. */
86 if (cli_is_error(cli)) {
87 NTSTATUS status = NT_STATUS_OK;
88 uint8 eclass = 0;
89 uint32 ecode = 0;
91 if (cli_is_nt_error(cli))
92 status = cli_nt_error(cli);
93 else
94 cli_dos_error(cli, &eclass, &ecode);
96 if ((eclass == ERRDOS && ecode == ERRmoredata) ||
97 NT_STATUS_V(status) == NT_STATUS_V(STATUS_MORE_ENTRIES))
98 return -1;
101 size2 = SVAL(cli->inbuf, smb_vwv5);
103 if (size2 > readsize) {
104 DEBUG(5,("server returned more than we wanted!\n"));
105 return -1;
106 } else if (size2 < 0) {
107 DEBUG(5,("read return < 0!\n"));
108 return -1;
111 /* Copy data into buffer */
113 p = smb_base(cli->inbuf) + SVAL(cli->inbuf,smb_vwv6);
114 memcpy(buf + total, p, size2);
116 total += size2;
117 offset += size2;
120 * If the server returned less than we asked for we're at EOF.
123 if (size2 < readsize)
124 break;
127 return total;
130 #if 0 /* relies on client_recieve_smb(), now a static in libsmb/clientgen.c */
132 /* This call is INCOMPATIBLE with SMB signing. If you remove the #if 0
133 you must fix ensure you don't attempt to sign the packets - data
134 *will* be currupted */
136 /****************************************************************************
137 Issue a single SMBreadraw and don't wait for a reply.
138 ****************************************************************************/
140 static BOOL cli_issue_readraw(struct cli_state *cli, int fnum, off_t offset,
141 size_t size, int i)
144 if (!cli->sign_info.use_smb_signing) {
145 DEBUG(0, ("Cannot use readraw and SMB Signing\n"));
146 return False;
149 memset(cli->outbuf,'\0',smb_size);
150 memset(cli->inbuf,'\0',smb_size);
152 set_message(cli->outbuf,10,0,True);
154 SCVAL(cli->outbuf,smb_com,SMBreadbraw);
155 SSVAL(cli->outbuf,smb_tid,cli->cnum);
156 cli_setup_packet(cli);
158 SSVAL(cli->outbuf,smb_vwv0,fnum);
159 SIVAL(cli->outbuf,smb_vwv1,offset);
160 SSVAL(cli->outbuf,smb_vwv2,size);
161 SSVAL(cli->outbuf,smb_vwv3,size);
162 SSVAL(cli->outbuf,smb_mid,cli->mid + i);
164 return cli_send_smb(cli);
167 /****************************************************************************
168 Tester for the readraw call.
169 ****************************************************************************/
171 ssize_t cli_readraw(struct cli_state *cli, int fnum, char *buf, off_t offset, size_t size)
173 char *p;
174 int size2;
175 size_t readsize;
176 ssize_t total = 0;
178 if (size == 0)
179 return 0;
182 * Set readsize to the maximum size we can handle in one readraw.
185 readsize = 0xFFFF;
187 while (total < size) {
188 readsize = MIN(readsize, size-total);
190 /* Issue a read and receive a reply */
192 if (!cli_issue_readraw(cli, fnum, offset, readsize, 0))
193 return -1;
195 if (!client_receive_smb(cli->fd, cli->inbuf, cli->timeout))
196 return -1;
198 size2 = smb_len(cli->inbuf);
200 if (size2 > readsize) {
201 DEBUG(5,("server returned more than we wanted!\n"));
202 return -1;
203 } else if (size2 < 0) {
204 DEBUG(5,("read return < 0!\n"));
205 return -1;
208 /* Copy data into buffer */
210 if (size2) {
211 p = cli->inbuf + 4;
212 memcpy(buf + total, p, size2);
215 total += size2;
216 offset += size2;
219 * If the server returned less than we asked for we're at EOF.
222 if (size2 < readsize)
223 break;
226 return total;
228 #endif
229 /****************************************************************************
230 issue a single SMBwrite and don't wait for a reply
231 ****************************************************************************/
233 static BOOL cli_issue_write(struct cli_state *cli, int fnum, off_t offset, uint16 mode, char *buf,
234 size_t size, int i)
236 char *p;
238 if (size > cli->bufsize) {
239 cli->outbuf = realloc(cli->outbuf, size + 1024);
240 cli->inbuf = realloc(cli->inbuf, size + 1024);
241 if (cli->outbuf == NULL || cli->inbuf == NULL)
242 return False;
243 cli->bufsize = size + 1024;
246 memset(cli->outbuf,'\0',smb_size);
247 memset(cli->inbuf,'\0',smb_size);
249 if (size > 0xFFFF)
250 set_message(cli->outbuf,14,0,True);
251 else
252 set_message(cli->outbuf,12,0,True);
254 SCVAL(cli->outbuf,smb_com,SMBwriteX);
255 SSVAL(cli->outbuf,smb_tid,cli->cnum);
256 cli_setup_packet(cli);
258 SCVAL(cli->outbuf,smb_vwv0,0xFF);
259 SSVAL(cli->outbuf,smb_vwv2,fnum);
261 SIVAL(cli->outbuf,smb_vwv3,offset);
262 SIVAL(cli->outbuf,smb_vwv5,(mode & 0x0008) ? 0xFFFFFFFF : 0);
263 SSVAL(cli->outbuf,smb_vwv7,mode);
265 SSVAL(cli->outbuf,smb_vwv8,(mode & 0x0008) ? size : 0);
266 SSVAL(cli->outbuf,smb_vwv9,((size>>16)&1));
267 SSVAL(cli->outbuf,smb_vwv10,size);
268 SSVAL(cli->outbuf,smb_vwv11,
269 smb_buf(cli->outbuf) - smb_base(cli->outbuf));
271 p = smb_base(cli->outbuf) + SVAL(cli->outbuf,smb_vwv11);
272 memcpy(p, buf, size);
273 cli_setup_bcc(cli, p+size);
275 SSVAL(cli->outbuf,smb_mid,cli->mid + i);
277 show_msg(cli->outbuf);
278 return cli_send_smb(cli);
281 /****************************************************************************
282 write to a file
283 write_mode: 0x0001 disallow write cacheing
284 0x0002 return bytes remaining
285 0x0004 use raw named pipe protocol
286 0x0008 start of message mode named pipe protocol
287 ****************************************************************************/
289 ssize_t cli_write(struct cli_state *cli,
290 int fnum, uint16 write_mode,
291 char *buf, off_t offset, size_t size)
293 int bwritten = 0;
294 int issued = 0;
295 int received = 0;
296 int mpx = MAX(cli->max_mux-1, 1);
297 int block = (cli->max_xmit - (smb_size+32)) & ~1023;
298 int blocks = (size + (block-1)) / block;
300 while (received < blocks) {
302 while ((issued - received < mpx) && (issued < blocks)) {
303 int bsent = issued * block;
304 int size1 = MIN(block, size - bsent);
306 if (!cli_issue_write(cli, fnum, offset + bsent,
307 write_mode,
308 buf + bsent,
309 size1, issued))
310 return -1;
311 issued++;
314 if (!cli_receive_smb(cli))
315 return bwritten;
317 received++;
319 if (cli_is_error(cli))
320 break;
322 bwritten += SVAL(cli->inbuf, smb_vwv2);
323 bwritten += (((int)(SVAL(cli->inbuf, smb_vwv4)))>>16);
326 while (received < issued && cli_receive_smb(cli))
327 received++;
329 return bwritten;
332 /****************************************************************************
333 write to a file using a SMBwrite and not bypassing 0 byte writes
334 ****************************************************************************/
336 ssize_t cli_smbwrite(struct cli_state *cli,
337 int fnum, char *buf, off_t offset, size_t size1)
339 char *p;
340 ssize_t total = 0;
342 do {
343 size_t size = MIN(size1, cli->max_xmit - 48);
345 memset(cli->outbuf,'\0',smb_size);
346 memset(cli->inbuf,'\0',smb_size);
348 set_message(cli->outbuf,5, 0,True);
350 SCVAL(cli->outbuf,smb_com,SMBwrite);
351 SSVAL(cli->outbuf,smb_tid,cli->cnum);
352 cli_setup_packet(cli);
354 SSVAL(cli->outbuf,smb_vwv0,fnum);
355 SSVAL(cli->outbuf,smb_vwv1,size);
356 SIVAL(cli->outbuf,smb_vwv2,offset);
357 SSVAL(cli->outbuf,smb_vwv4,0);
359 p = smb_buf(cli->outbuf);
360 *p++ = 1;
361 SSVAL(p, 0, size); p += 2;
362 memcpy(p, buf, size); p += size;
364 cli_setup_bcc(cli, p);
366 if (!cli_send_smb(cli))
367 return -1;
369 if (!cli_receive_smb(cli))
370 return -1;
372 if (cli_is_error(cli))
373 return -1;
375 size = SVAL(cli->inbuf,smb_vwv0);
376 if (size == 0)
377 break;
379 size1 -= size;
380 total += size;
381 offset += size;
383 } while (size1);
385 return total;