4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
25 * Copyright 2013 Nexenta Systems, Inc. All rights reserved.
29 * Legacy encode/decode routines for door clients and servers.
32 #if !defined(_KERNEL) && !defined(_FAKE_KERNEL)
37 #include <sys/types.h>
38 #include <sys/sunddi.h>
39 #include <sys/errno.h>
42 #include <smbsrv/wintypes.h>
43 #include <smbsrv/smb_share.h>
44 #include <smbsrv/smb_door.h>
45 #include <smbsrv/alloc.h>
46 #include <smbsrv/smbinfo.h>
49 smb_dr_decode_start(char *ptr
, int size
)
51 smb_dr_ctx_t
*ctx
= MEM_MALLOC("CommonDoor", sizeof (smb_dr_ctx_t
));
53 ctx
->start_ptr
= ctx
->ptr
= ptr
;
54 ctx
->end_ptr
= ptr
+ size
;
61 smb_dr_decode_finish(smb_dr_ctx_t
*ctx
)
63 int status
= ctx
->status
;
64 if (status
== 0 && ctx
->ptr
!= ctx
->end_ptr
)
67 MEM_FREE("CommonDoor", ctx
);
72 smb_dr_encode_start(char *ptr
, int size
)
74 smb_dr_ctx_t
*ctx
= MEM_MALLOC("CommonDoor", sizeof (smb_dr_ctx_t
));
76 ctx
->start_ptr
= ctx
->ptr
= ptr
;
77 ctx
->end_ptr
= ptr
+ size
;
84 smb_dr_encode_finish(smb_dr_ctx_t
*ctx
, unsigned int *used
)
86 int status
= ctx
->status
;
88 if (ctx
->ptr
< ctx
->end_ptr
) {
89 /*LINTED E_PTRDIFF_OVERFLOW*/
90 *used
= ctx
->ptr
- ctx
->start_ptr
;
96 MEM_FREE("CommonDoor", ctx
);
101 smb_dr_get_dword(smb_dr_ctx_t
*ctx
)
104 if (ctx
->status
== 0) {
105 if (ctx
->ptr
+ sizeof (DWORD
) <= ctx
->end_ptr
) {
106 (void) memcpy(&num
, ctx
->ptr
, sizeof (DWORD
));
107 ctx
->ptr
+= sizeof (DWORD
);
109 ctx
->status
= ENOSPC
;
116 smb_dr_get_int32(smb_dr_ctx_t
*ctx
)
119 if (ctx
->status
== 0) {
120 if (ctx
->ptr
+ sizeof (int32_t) <= ctx
->end_ptr
) {
121 (void) memcpy(&num
, ctx
->ptr
, sizeof (int32_t));
122 ctx
->ptr
+= sizeof (int32_t);
124 ctx
->status
= ENOSPC
;
131 smb_dr_get_uint32(smb_dr_ctx_t
*ctx
)
133 return ((uint32_t)smb_dr_get_int32(ctx
));
137 smb_dr_get_string(smb_dr_ctx_t
*ctx
)
140 int len
= smb_dr_get_int32(ctx
);
142 if (ctx
->status
== 0) {
146 if (ctx
->ptr
+ len
<= ctx
->end_ptr
) {
147 buf
= MEM_MALLOC("CommonDoor", len
+1);
150 (void) strcpy(buf
, "");
152 (void) memcpy(buf
, ctx
->ptr
, len
);
157 #if !defined(_KERNEL) && !defined(_FAKE_KERNEL)
160 ctx
->status
= ENOMEM
;
164 ctx
->status
= ENOSPC
;
171 smb_dr_put_dword(smb_dr_ctx_t
*ctx
, DWORD num
)
173 if (ctx
->status
== 0) {
174 if (ctx
->ptr
+ sizeof (DWORD
) <= ctx
->end_ptr
) {
175 (void) memcpy(ctx
->ptr
, &num
, sizeof (DWORD
));
176 ctx
->ptr
+= sizeof (DWORD
);
178 ctx
->status
= ENOSPC
;
184 smb_dr_put_int32(smb_dr_ctx_t
*ctx
, int32_t num
)
186 if (ctx
->status
== 0) {
187 if (ctx
->ptr
+ sizeof (int32_t) <= ctx
->end_ptr
) {
188 (void) memcpy(ctx
->ptr
, &num
, sizeof (int32_t));
189 ctx
->ptr
+= sizeof (int32_t);
191 ctx
->status
= ENOSPC
;
197 smb_dr_put_uint32(smb_dr_ctx_t
*ctx
, uint32_t num
)
199 smb_dr_put_int32(ctx
, (int32_t)num
);
203 smb_dr_put_string(smb_dr_ctx_t
*ctx
, const char *buf
)
212 if (ctx
->status
== 0) {
213 smb_dr_put_int32(ctx
, len
);
217 if (ctx
->ptr
+ len
<= ctx
->end_ptr
) {
218 (void) memcpy(ctx
->ptr
, buf
, len
);
221 ctx
->status
= ENOSPC
;
227 smb_dr_free_string(char *buf
)
230 MEM_FREE("CommonDoor", buf
);
234 smb_dr_get_int64(smb_dr_ctx_t
*ctx
)
237 if (ctx
->status
== 0) {
238 if (ctx
->ptr
+ sizeof (int64_t) <= ctx
->end_ptr
) {
239 (void) memcpy(&num
, ctx
->ptr
, sizeof (int64_t));
240 ctx
->ptr
+= sizeof (int64_t);
242 ctx
->status
= ENOSPC
;
249 smb_dr_get_uint64(smb_dr_ctx_t
*ctx
)
251 return ((uint64_t)smb_dr_get_int64(ctx
));
256 smb_dr_put_int64(smb_dr_ctx_t
*ctx
, int64_t num
)
258 if (ctx
->status
== 0) {
259 if (ctx
->ptr
+ sizeof (int64_t) <= ctx
->end_ptr
) {
260 (void) memcpy(ctx
->ptr
, &num
, sizeof (int64_t));
261 ctx
->ptr
+= sizeof (int64_t);
263 ctx
->status
= ENOSPC
;
269 smb_dr_put_uint64(smb_dr_ctx_t
*ctx
, uint64_t num
)
271 smb_dr_put_int64(ctx
, (int64_t)num
);
275 smb_dr_put_short(smb_dr_ctx_t
*ctx
, short num
)
277 if (ctx
->status
== 0) {
278 if (ctx
->ptr
+ sizeof (short) <= ctx
->end_ptr
) {
279 (void) memcpy(ctx
->ptr
, &num
, sizeof (short));
280 ctx
->ptr
+= sizeof (short);
282 ctx
->status
= ENOSPC
;
288 smb_dr_get_short(smb_dr_ctx_t
*ctx
)
291 if (ctx
->status
== 0) {
292 if (ctx
->ptr
+ sizeof (short) <= ctx
->end_ptr
) {
293 (void) memcpy(&num
, ctx
->ptr
, sizeof (short));
294 ctx
->ptr
+= sizeof (short);
296 ctx
->status
= ENOSPC
;
303 smb_dr_put_ushort(smb_dr_ctx_t
*ctx
, unsigned short num
)
305 smb_dr_put_short(ctx
, (short)num
);
309 smb_dr_get_ushort(smb_dr_ctx_t
*ctx
)
311 return ((unsigned short)smb_dr_get_short(ctx
));
315 smb_dr_put_word(smb_dr_ctx_t
*ctx
, WORD num
)
317 smb_dr_put_ushort(ctx
, num
);
321 smb_dr_get_word(smb_dr_ctx_t
*ctx
)
323 return (smb_dr_get_ushort(ctx
));
327 smb_dr_put_BYTE(smb_dr_ctx_t
*ctx
, BYTE byte
)
329 if (ctx
->status
== 0) {
330 if (ctx
->ptr
+ sizeof (BYTE
) <= ctx
->end_ptr
) {
331 (void) memcpy(ctx
->ptr
, &byte
, sizeof (BYTE
));
332 ctx
->ptr
+= sizeof (BYTE
);
334 ctx
->status
= ENOSPC
;
340 smb_dr_get_BYTE(smb_dr_ctx_t
*ctx
)
343 if (ctx
->status
== 0) {
344 if (ctx
->ptr
+ sizeof (BYTE
) <= ctx
->end_ptr
) {
345 (void) memcpy(&byte
, ctx
->ptr
, sizeof (BYTE
));
346 ctx
->ptr
+= sizeof (BYTE
);
348 ctx
->status
= ENOSPC
;
355 smb_dr_put_buf(smb_dr_ctx_t
*ctx
, unsigned char *start
, int len
)
357 smb_dr_put_int32(ctx
, len
);
358 if (ctx
->status
== 0) {
359 if (ctx
->ptr
+ len
<= ctx
->end_ptr
) {
360 (void) memcpy(ctx
->ptr
, start
, len
);
363 ctx
->status
= ENOSPC
;
369 smb_dr_get_buf(smb_dr_ctx_t
*ctx
, unsigned char *buf
, int bufsize
)
376 len
= smb_dr_get_int32(ctx
);
377 if (ctx
->status
== 0) {
379 ctx
->status
= ENOSPC
;
383 if (ctx
->ptr
+ len
<= ctx
->end_ptr
) {
384 (void) memcpy(buf
, ctx
->ptr
, len
);
387 ctx
->status
= ENOSPC
;
396 smb_dr_get_share(smb_dr_ctx_t
*ctx
, smb_share_t
*si
)
398 if (ctx
->status
== 0) {
399 if (smb_dr_get_int32(ctx
)) {
400 (void) memcpy(si
, ctx
->ptr
, sizeof (smb_share_t
));
401 ctx
->ptr
+= sizeof (smb_share_t
);
403 bzero(si
, sizeof (smb_share_t
));
406 bzero(si
, sizeof (smb_share_t
));
411 smb_dr_put_share(smb_dr_ctx_t
*ctx
, smb_share_t
*si
)
414 smb_dr_put_int32(ctx
, 1);
415 if (ctx
->ptr
+ sizeof (smb_share_t
) <= ctx
->end_ptr
) {
416 (void) memcpy(ctx
->ptr
, si
, sizeof (smb_share_t
));
417 ctx
->ptr
+= sizeof (smb_share_t
);
419 ctx
->status
= ENOSPC
;
422 smb_dr_put_int32(ctx
, 0);