[MMDEVAPI] Sync with Wine Staging 3.3. CORE-14434
[reactos.git] / dll / np / nfs / options.c
1 /* NFSv4.1 client for Windows
2 * Copyright © 2012 The Regents of the University of Michigan
3 *
4 * Olga Kornievskaia <aglo@umich.edu>
5 * Casey Bodley <cbodley@umich.edu>
6 *
7 * This library is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as published by
9 * the Free Software Foundation; either version 2.1 of the License, or (at
10 * your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful, but
13 * without any warranty; without even the implied warranty of merchantability
14 * or fitness for a particular purpose. See the GNU Lesser General Public
15 * License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this library; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 */
21
22 #include <windows.h>
23 #include "options.h"
24
25
26 DWORD InitializeConnectionInfo(
27 IN OUT PCONNECTION_INFO Connection,
28 IN PMOUNT_OPTION_BUFFER Options,
29 OUT LPWSTR *ConnectionName)
30 {
31 DWORD result = WN_SUCCESS;
32 SIZE_T size;
33
34 /* verify that this is a mount options buffer */
35 if (Options &&
36 Options->Zero == 0 &&
37 Options->Secret == MOUNT_OPTION_BUFFER_SECRET)
38 {
39 Connection->Options = Options;
40 size = MAX_CONNECTION_BUFFER_SIZE(Options->Length);
41 }
42 else
43 {
44 Connection->Options = NULL;
45 size = MAX_CONNECTION_BUFFER_SIZE(0);
46 }
47
48 Connection->Buffer = LocalAlloc(LMEM_ZEROINIT, size);
49 if (Connection->Buffer)
50 *ConnectionName = (LPWSTR)Connection->Buffer->Buffer;
51 else
52 result = WN_OUT_OF_MEMORY;
53
54 return result;
55 }
56
57 #ifdef __REACTOS__
58 FORCEINLINE SIZE_T ConnectionBufferSize(
59 #else
60 static FORCEINLINE SIZE_T ConnectionBufferSize(
61 #endif
62 IN PCONNECTION_BUFFER Buffer)
63 {
64 return sizeof(USHORT) + sizeof(USHORT) + sizeof(ULONG) +
65 Buffer->NameLength + Buffer->EaPadding + Buffer->EaLength;
66 }
67
68 void MarshalConnectionInfo(
69 IN OUT PCONNECTION_INFO Connection)
70 {
71 PCONNECTION_BUFFER Buffer = Connection->Buffer;
72 LPWSTR ConnectionName = (LPWSTR)Buffer->Buffer;
73
74 Buffer->NameLength = (USHORT)(wcslen(ConnectionName) + 1) * sizeof(WCHAR);
75
76 /* copy the EaBuffer after the end of ConnectionName */
77 if (Connection->Options && Connection->Options->Length)
78 {
79 PBYTE ptr = Buffer->Buffer + Buffer->NameLength;
80 /* add padding so EaBuffer starts on a ULONG boundary */
81 Buffer->EaPadding = (USHORT)
82 (sizeof(ULONG) - (SIZE_T)ptr % sizeof(ULONG)) % sizeof(ULONG);
83 Buffer->EaLength = Connection->Options->Length;
84 ptr += Buffer->EaPadding;
85
86 RtlCopyMemory(ptr, Connection->Options->Buffer, Buffer->EaLength);
87 }
88
89 Connection->BufferSize = (ULONG)ConnectionBufferSize(Buffer);
90 }
91
92 void FreeConnectionInfo(
93 IN PCONNECTION_INFO Connection)
94 {
95 if (Connection->Buffer)
96 {
97 LocalFree(Connection->Buffer);
98 Connection->Buffer = NULL;
99 }
100 Connection->Options = NULL;
101 Connection->BufferSize = 0;
102 }