cli_read() was reading too many bytes.
[gd/samba/.git] / source3 / libsmb / clireadwrite.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 3.0
4    client file read/write routines
5    Copyright (C) Andrew Tridgell 1994-1998
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #define NO_SYSLOG
23
24 #include "includes.h"
25
26 /****************************************************************************
27 issue a single SMBread and don't wait for a reply
28 ****************************************************************************/
29
30 static BOOL cli_issue_read(struct cli_state *cli, int fnum, off_t offset, 
31                            size_t size, int i)
32 {
33         memset(cli->outbuf,'\0',smb_size);
34         memset(cli->inbuf,'\0',smb_size);
35
36         set_message(cli->outbuf,10,0,True);
37                 
38         CVAL(cli->outbuf,smb_com) = SMBreadX;
39         SSVAL(cli->outbuf,smb_tid,cli->cnum);
40         cli_setup_packet(cli);
41
42         CVAL(cli->outbuf,smb_vwv0) = 0xFF;
43         SSVAL(cli->outbuf,smb_vwv2,fnum);
44         SIVAL(cli->outbuf,smb_vwv3,offset);
45         SSVAL(cli->outbuf,smb_vwv5,size);
46         SSVAL(cli->outbuf,smb_vwv6,size);
47         SSVAL(cli->outbuf,smb_mid,cli->mid + i);
48
49         return cli_send_smb(cli);
50 }
51
52 /****************************************************************************
53   Read size bytes at offset offset using SMBreadX.
54 ****************************************************************************/
55
56 ssize_t cli_read(struct cli_state *cli, int fnum, char *buf, off_t offset, size_t size)
57 {
58         uint32 ecode;
59         uint8 eclass;
60         char *p;
61         int size2;
62         int readsize;
63         ssize_t total = 0;
64
65         if (size == 0) 
66                 return 0;
67
68         /*
69          * Set readsize to the maximum size we can handle in one readX,
70          * rounded down to a multiple of 1024.
71          */
72
73         readsize = (cli->max_xmit - (smb_size+32)) & ~1023;
74
75         while (total < size) {
76                 readsize = MIN(readsize, size-total);
77
78                 /* Issue a read and receive a reply */
79
80                 if (!cli_issue_read(cli, fnum, offset, readsize, 0))
81                         return -1;
82
83                 if (!cli_receive_smb(cli))
84                         return -1;
85
86                 /*
87                  * Check for error.  Because the client library doesn't support
88                  * STATUS32, we need to check for and ignore the more data error
89                  * for pipe support.
90                  */
91
92                 if (cli_error(cli, &eclass, &ecode, NULL) &&
93                         (eclass != ERRDOS && ecode != ERRmoredata)) {
94                         return -1;
95                 }
96
97                 size2 = SVAL(cli->inbuf, smb_vwv5);
98
99                 if (size2 > readsize) {
100                         DEBUG(5,("server returned more than we wanted!\n"));
101                         return -1;
102                 } else if (size2 < 0) {
103                         DEBUG(5,("read return < 0!\n"));
104                         return -1;
105                 }
106
107                 /* Copy data into buffer */
108
109                 p = smb_base(cli->inbuf) + SVAL(cli->inbuf,smb_vwv6);
110                 memcpy(buf + total, p, size2);
111
112                 total += size2;
113                 offset += size2;
114
115                 /*
116                  * If the server returned less than we asked for we're at EOF.
117                  */
118
119                 if (size2 < readsize)
120                         break;
121         }
122
123         return total;
124 }
125
126 /****************************************************************************
127 issue a single SMBwrite and don't wait for a reply
128 ****************************************************************************/
129
130 static BOOL cli_issue_write(struct cli_state *cli, int fnum, off_t offset, uint16 mode, char *buf,
131                             size_t size, int i)
132 {
133         char *p;
134
135         memset(cli->outbuf,'\0',smb_size);
136         memset(cli->inbuf,'\0',smb_size);
137
138         if (size > 0xFFFF)
139                 set_message(cli->outbuf,14,0,True);
140         else
141                 set_message(cli->outbuf,12,0,True);
142         
143         CVAL(cli->outbuf,smb_com) = SMBwriteX;
144         SSVAL(cli->outbuf,smb_tid,cli->cnum);
145         cli_setup_packet(cli);
146         
147         CVAL(cli->outbuf,smb_vwv0) = 0xFF;
148         SSVAL(cli->outbuf,smb_vwv2,fnum);
149
150         SIVAL(cli->outbuf,smb_vwv3,offset);
151         SIVAL(cli->outbuf,smb_vwv5,(mode & 0x0008) ? 0xFFFFFFFF : 0);
152         SSVAL(cli->outbuf,smb_vwv7,mode);
153
154         SSVAL(cli->outbuf,smb_vwv8,(mode & 0x0008) ? size : 0);
155         SSVAL(cli->outbuf,smb_vwv9,((size>>16)&1));
156         SSVAL(cli->outbuf,smb_vwv10,size);
157         SSVAL(cli->outbuf,smb_vwv11,
158               smb_buf(cli->outbuf) - smb_base(cli->outbuf));
159         
160         p = smb_base(cli->outbuf) + SVAL(cli->outbuf,smb_vwv11);
161         memcpy(p, buf, size);
162         cli_setup_bcc(cli, p+size);
163
164         SSVAL(cli->outbuf,smb_mid,cli->mid + i);
165         
166         show_msg(cli->outbuf);
167         return cli_send_smb(cli);
168 }
169
170 /****************************************************************************
171   write to a file
172   write_mode: 0x0001 disallow write cacheing
173               0x0002 return bytes remaining
174               0x0004 use raw named pipe protocol
175               0x0008 start of message mode named pipe protocol
176 ****************************************************************************/
177
178 ssize_t cli_write(struct cli_state *cli,
179                   int fnum, uint16 write_mode,
180                   char *buf, off_t offset, size_t size)
181 {
182         int bwritten = 0;
183         int issued = 0;
184         int received = 0;
185         int mpx = MAX(cli->max_mux-1, 1);
186         int block = (cli->max_xmit - (smb_size+32)) & ~1023;
187         int blocks = (size + (block-1)) / block;
188
189         while (received < blocks) {
190
191                 while ((issued - received < mpx) && (issued < blocks)) {
192                         int bsent = issued * block;
193                         int size1 = MIN(block, size - bsent);
194
195                         if (!cli_issue_write(cli, fnum, offset + bsent,
196                                         write_mode,
197                                         buf + bsent,
198                                         size1, issued))
199                                 return -1;
200                         issued++;
201                 }
202
203                 if (!cli_receive_smb(cli))
204                         return bwritten;
205
206                 received++;
207
208                 if (CVAL(cli->inbuf,smb_rcls) != 0)
209                         break;
210
211                 bwritten += SVAL(cli->inbuf, smb_vwv2);
212         }
213
214         while (received < issued && cli_receive_smb(cli))
215                 received++;
216         
217         return bwritten;
218 }
219
220 /****************************************************************************
221   write to a file using a SMBwrite and not bypassing 0 byte writes
222 ****************************************************************************/
223
224 ssize_t cli_smbwrite(struct cli_state *cli,
225                      int fnum, char *buf, off_t offset, size_t size1)
226 {
227         char *p;
228         ssize_t total = 0;
229
230         do {
231                 size_t size = MIN(size1, cli->max_xmit - 48);
232                 
233                 memset(cli->outbuf,'\0',smb_size);
234                 memset(cli->inbuf,'\0',smb_size);
235
236                 set_message(cli->outbuf,5, 0,True);
237
238                 CVAL(cli->outbuf,smb_com) = SMBwrite;
239                 SSVAL(cli->outbuf,smb_tid,cli->cnum);
240                 cli_setup_packet(cli);
241                 
242                 SSVAL(cli->outbuf,smb_vwv0,fnum);
243                 SSVAL(cli->outbuf,smb_vwv1,size);
244                 SIVAL(cli->outbuf,smb_vwv2,offset);
245                 SSVAL(cli->outbuf,smb_vwv4,0);
246                 
247                 p = smb_buf(cli->outbuf);
248                 *p++ = 1;
249                 SSVAL(p, 0, size); p += 2;
250                 memcpy(p, buf, size); p += size;
251
252                 cli_setup_bcc(cli, p);
253                 
254                 if (!cli_send_smb(cli))
255                         return -1;
256
257                 if (!cli_receive_smb(cli))
258                         return -1;
259                 
260                 if (CVAL(cli->inbuf,smb_rcls) != 0)
261                         return -1;
262
263                 size = SVAL(cli->inbuf,smb_vwv0);
264                 if (size == 0)
265                         break;
266
267                 size1 -= size;
268                 total += size;
269         } while (size1);
270
271         return total;
272 }