1da6c28aamw/*
2da6c28aamw * CDDL HEADER START
3da6c28aamw *
4da6c28aamw * The contents of this file are subject to the terms of the
5da6c28aamw * Common Development and Distribution License (the "License").
6da6c28aamw * You may not use this file except in compliance with the License.
7da6c28aamw *
8da6c28aamw * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9da6c28aamw * or http://www.opensolaris.org/os/licensing.
10da6c28aamw * See the License for the specific language governing permissions
11da6c28aamw * and limitations under the License.
12da6c28aamw *
13da6c28aamw * When distributing Covered Code, include this CDDL HEADER in each
14da6c28aamw * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15da6c28aamw * If applicable, add the following below this CDDL HEADER, with the
16da6c28aamw * fields enclosed by brackets "[]" replaced with your own identifying
17da6c28aamw * information: Portions Copyright [yyyy] [name of copyright owner]
18da6c28aamw *
19da6c28aamw * CDDL HEADER END
20da6c28aamw */
21da6c28aamw/*
22bbf6f00Jordan Brown * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23da6c28aamw * Use is subject to license terms.
24b819ceaGordon Ross *
25b819ceaGordon Ross * Copyright 2014 Nexenta Systems, Inc.  All rights reserved.
26da6c28aamw */
27da6c28aamw
28b819ceaGordon Ross#if defined(_KERNEL) || defined(_FAKE_KERNEL)
29da6c28aamw#include <sys/types.h>
30da6c28aamw#include <sys/sunddi.h>
31da6c28aamw#else
32da6c28aamw#include <string.h>
33da6c28aamw#endif
34da6c28aamw#include <smbsrv/string.h>
35da6c28aamw#include <smbsrv/netbios.h>
36da6c28aamw
37da6c28aamw/*
38da6c28aamw * Routines than support name compression.
39da6c28aamw *
40da6c28aamw *   The NetBIOS name representation in all NetBIOS packets (for NAME,
41da6c28aamw *   SESSION, and DATAGRAM services) is defined in the Domain Name
42da6c28aamw *   Service RFC 883[3] as "compressed" name messages.  This format is
43da6c28aamw *   called "second-level encoding" in the section entitled
44da6c28aamw *   "Representation of NetBIOS Names" in the Concepts and Methods
45da6c28aamw *   document.
46da6c28aamw *
47da6c28aamw *   For ease of description, the first two paragraphs from page 31,
48da6c28aamw *   the section titled "Domain name representation and compression",
49da6c28aamw *   of RFC 883 are replicated here:
50da6c28aamw *
51da6c28aamw *        Domain names messages are expressed in terms of a sequence
52da6c28aamw *        of labels.  Each label is represented as a one octet length
53da6c28aamw *        field followed by that number of octets.  Since every domain
54da6c28aamw *        name ends with the null label of the root, a compressed
55da6c28aamw *        domain name is terminated by a length byte of zero.  The
56da6c28aamw *        high order two bits of the length field must be zero, and
57da6c28aamw *        the remaining six bits of the length field limit the label
58da6c28aamw *        to 63 octets or less.
59da6c28aamw *
60da6c28aamw *        To simplify implementations, the total length of label
61da6c28aamw *        octets and label length octets that make up a domain name is
62da6c28aamw *        restricted to 255 octets or less.
63da6c28aamw *
64da6c28aamw *   The following is the uncompressed representation of the NetBIOS name
65da6c28aamw *   "FRED ", which is the 4 ASCII characters, F, R, E, D, followed by 12
66da6c28aamw *   space characters (0x20).  This name has the SCOPE_ID: "NETBIOS.COM"
67da6c28aamw *
68da6c28aamw *           EGFCEFEECACACACACACACACACACACACA.NETBIOS.COM
69da6c28aamw *
70da6c28aamw *   This uncompressed representation of names is called "first-level
71da6c28aamw *   encoding" in the section entitled "Representation of NetBIOS Names"
72da6c28aamw *   in the Concepts and Methods document.
73da6c28aamw *
74da6c28aamw *   The following is a pictographic representation of the compressed
75da6c28aamw *   representation of the previous uncompressed Domain Name
76da6c28aamw *   representation.
77da6c28aamw *
78da6c28aamw *                        1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
79da6c28aamw *    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
80da6c28aamw *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
81da6c28aamw *   |      0x20     |    E (0x45)   |    G (0x47)   |    F (0x46)   |
82da6c28aamw *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
83da6c28aamw *   |    C (0x43)   |    E (0x45)   |    F (0x46)   |    E (0x45)   |
84da6c28aamw *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
85da6c28aamw *   |    E (0x45)   |    C (0x43)   |    A (0x41)   |    C (0x43)   |
86da6c28aamw *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
87da6c28aamw *   |    A (0x41)   |    C (0x43)   |    A (0x41)   |    C (0x43)   |
88da6c28aamw *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
89da6c28aamw *   |    A (0x41)   |    C (0x43)   |    A (0x41)   |    C (0x43)   |
90da6c28aamw *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
91da6c28aamw *   |    A (0x41)   |    C (0x43)   |    A (0x41)   |    C (0x43)   |
92da6c28aamw *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
93da6c28aamw *   |    A (0x41)   |    C (0x43)   |    A (0x41)   |    C (0x43)   |
94da6c28aamw *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
95da6c28aamw *   |    A (0x41)   |    C (0x43)   |    A (0x41)   |    C (0x43)   |
96da6c28aamw *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
97da6c28aamw *   |    A (0X41)   |      0x07     |    N (0x4E)   |    E (0x45)   |
98da6c28aamw *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
99da6c28aamw *   |    T (0x54)   |    B (0x42)   |    I (0x49)   |    O (0x4F)   |
100da6c28aamw *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
101da6c28aamw *   |    S (0x53)   |      0x03     |    C (0x43)   |    O (0x4F)   |
102da6c28aamw *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
103da6c28aamw *   |    M (0x4D)   |      0x00     |
104da6c28aamw *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
105da6c28aamw *
106da6c28aamw *   Each section of a domain name is called a label [7 (page 31)].  A
107da6c28aamw *   label can be a maximum of 63 bytes.  The first byte of a label in
108da6c28aamw *   compressed representation is the number of bytes in the label.  For
109da6c28aamw *   the above example, the first 0x20 is the number of bytes in the
110da6c28aamw *   left-most label, EGFCEFEECACACACACACACACACACACACA, of the domain
111da6c28aamw *   name.  The bytes following the label length count are the characters
112da6c28aamw *   of the label.  The following labels are in sequence after the first
113da6c28aamw *   label, which is the encoded NetBIOS name, until a zero (0x00) length
114da6c28aamw *   count.  The zero length count represents the root label, which is
115da6c28aamw *   always null.
116da6c28aamw *
117da6c28aamw *   A label length count is actually a 6-bit field in the label length
118da6c28aamw *   field.  The most significant 2 bits of the field, bits 7 and 6, are
119da6c28aamw *   flags allowing an escape from the above compressed representation.
120da6c28aamw *   If bits 7 and 6 are both set (11), the following 14 bits are an
121da6c28aamw *   offset pointer into the full message to the actual label string from
122da6c28aamw *   another domain name that belongs in this name.  This label pointer
123da6c28aamw *   allows for a further compression of a domain name in a packet.
124da6c28aamw *
125da6c28aamw *   NetBIOS implementations can only use label string pointers in Name
126da6c28aamw *   Service packets.  They cannot be used in Session or Datagram Service
127da6c28aamw *   packets.
128da6c28aamw *
129da6c28aamw *   The other two possible values for bits 7 and 6 (01 and 10) of a label
130da6c28aamw *   length field are reserved for future use by RFC 883[2 (page 32)].
131da6c28aamw *
132da6c28aamw *   Note that the first octet of a compressed name must contain one of
133da6c28aamw *   the following bit patterns.  (An "x" indicates a bit whose value may
134da6c28aamw *   be either 0 or 1.):
135da6c28aamw *
136da6c28aamw *           00100000 -  Netbios name, length must be 32 (decimal)
137da6c28aamw *           11xxxxxx -  Label string pointer
138da6c28aamw *           10xxxxxx -  Reserved
139da6c28aamw *           01xxxxxx -  Reserved
140da6c28aamw */
141da6c28aamw
142da6c28aamw/*
143da6c28aamw * netbios_first_level_name_encode
144da6c28aamw *
145da6c28aamw * Put test description here.
146da6c28aamw *
147da6c28aamw * Inputs:
148da6c28aamw *	char *	in	-> Name to encode
149da6c28aamw *	char *	out	-> Buffer to encode into.
150da6c28aamw *	int	length	-> # of bytes to encode.
151da6c28aamw *
152da6c28aamw * Returns:
153da6c28aamw *	Nothing
154da6c28aamw */
155da6c28aamwint
156da6c28aamwnetbios_first_level_name_encode(unsigned char *name, unsigned char *scope,
157da6c28aamw    unsigned char *out, int max_out)
158da6c28aamw{
159da6c28aamw	unsigned char	ch, len;
160da6c28aamw	unsigned char	 *in;
161da6c28aamw	unsigned char	 *lp;
162da6c28aamw	unsigned char	 *op = out;
163da6c28aamw
164