summaryrefslogtreecommitdiffstats
path: root/source4/libcli/clireadwrite.c
blob: afffeadeff88ecc810a1b6672bb17427d67d0bac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/* 
   Unix SMB/CIFS implementation.
   client file read/write routines
   Copyright (C) Andrew Tridgell 1994-1998
   Copyright (C) James Myers 2003
   
   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., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "includes.h"
#include "libcli/raw/libcliraw.h"

/****************************************************************************
  Read size bytes at offset offset using SMBreadX.
****************************************************************************/
ssize_t smbcli_read(struct smbcli_tree *tree, int fnum, void *_buf, off_t offset, 
		 size_t size)
{
	uint8_t *buf = _buf;
	union smb_read parms;
	int readsize;
	ssize_t total = 0;
	
	if (size == 0) {
		return 0;
	}

	parms.readx.level = RAW_READ_READX;
	parms.readx.in.file.fnum = fnum;

	/*
	 * Set readsize to the maximum size we can handle in one readX,
	 * rounded down to a multiple of 1024.
	 */
	readsize = (tree->session->transport->negotiate.max_xmit - (MIN_SMB_SIZE+32));
	if (readsize > 0xFFFF) readsize = 0xFFFF;

	while (total < size) {
		NTSTATUS status;

		readsize = MIN(readsize, size-total);

		parms.readx.in.offset    = offset;
		parms.readx.in.mincnt    = readsize;
		parms.readx.in.maxcnt    = readsize;
		parms.readx.in.remaining = size - total;
		parms.readx.in.read_for_execute = False;
		parms.readx.out.data     = buf + total;
		
		status = smb_raw_read(tree, &parms);
		
		if (!NT_STATUS_IS_OK(status)) {
			return -1;
		}

		total += parms.readx.out.nread;
		offset += parms.readx.out.nread;

		/* If the server returned less than we asked for we're at EOF */
		if (parms.readx.out.nread < readsize)
			break;
	}

	return total;
}


/****************************************************************************
  write to a file
  write_mode: 0x0001 disallow write cacheing
              0x0002 return bytes remaining
              0x0004 use raw named pipe protocol
              0x0008 start of message mode named pipe protocol
****************************************************************************/
ssize_t smbcli_write(struct smbcli_tree *tree,
		     int fnum, uint16_t write_mode,
		     const void *_buf, off_t offset, size_t size)
{
	const uint8_t *buf = _buf;
	union smb_write parms;
	int block = (tree->session->transport->negotiate.max_xmit - (MIN_SMB_SIZE+32));
	ssize_t total = 0;

	if (size == 0) {
		return 0;
	}

	if (block > 0xFFFF) block = 0xFFFF;


	parms.writex.level = RAW_WRITE_WRITEX;
	parms.writex.in.file.fnum = fnum;
	parms.writex.in.wmode = write_mode;
	parms.writex.in.remaining = 0;
	
	while (total < size) {
		NTSTATUS status;

		block = MIN(block, size - total);

		parms.writex.in.offset = offset;
		parms.writex.in.count = block;
		parms.writex.in.data = buf;

		status = smb_raw_write(tree, &parms);

		if (!NT_STATUS_IS_OK(status)) {
			return -1;
		}

		offset += parms.writex.out.nwritten;
		total += parms.writex.out.nwritten;
		buf += parms.writex.out.nwritten;
	}

	return total;
}

/****************************************************************************
  write to a file using a SMBwrite and not bypassing 0 byte writes
****************************************************************************/
ssize_t smbcli_smbwrite(struct smbcli_tree *tree,
		     int fnum, const void *_buf, off_t offset, size_t size1)
{
	const uint8_t *buf = _buf;
	union smb_write parms;
	ssize_t total = 0;

	parms.write.level = RAW_WRITE_WRITE;
	parms.write.in.remaining = 0;
	
	do {
		size_t size = MIN(size1, tree->session->transport->negotiate.max_xmit - 48);
		if (size > 0xFFFF) size = 0xFFFF;
		
		parms.write.in.file.fnum = fnum;
		parms.write.in.offset = offset;
		parms.write.in.count = size;
		parms.write.in.data = buf + total;

		if (NT_STATUS_IS_ERR(smb_raw_write(tree, &parms)))
			return -1;

		size = parms.write.out.nwritten;
		if (size == 0)
			break;

		size1 -= size;
		total += size;
		offset += size;
	} while (size1);

	return total;
}