VERSION: Bump version up to 4.8.0rc1...
[Samba.git] / librpc / ndr / ndr_dnsp.c
blob974ff5ebff2c7f1f96fa20d0e5378ad9a5c3c37b
1 /*
2 Unix SMB/CIFS implementation.
4 Manually parsed structures found in DNSP
6 Copyright (C) Andrew Tridgell 2010
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "includes.h"
23 #include "librpc/gen_ndr/ndr_dnsp.h"
26 print a dnsp_name
28 _PUBLIC_ void ndr_print_dnsp_name(struct ndr_print *ndr, const char *name,
29 const char *dns_name)
31 ndr->print(ndr, "%-25s: %s", name, dns_name);
35 pull a dnsp_name
37 _PUBLIC_ enum ndr_err_code ndr_pull_dnsp_name(struct ndr_pull *ndr, int ndr_flags, const char **name)
39 uint8_t len, count, termination;
40 int i;
41 uint32_t total_len, raw_offset;
42 char *ret;
44 NDR_CHECK(ndr_pull_uint8(ndr, ndr_flags, &len));
45 NDR_CHECK(ndr_pull_uint8(ndr, ndr_flags, &count));
47 raw_offset = ndr->offset;
49 ret = talloc_strdup(ndr->current_mem_ctx, "");
50 if (!ret) {
51 return ndr_pull_error(ndr, NDR_ERR_ALLOC, "Failed to pull dnsp_name");
53 total_len = 1;
55 for (i=0; i<count; i++) {
56 uint8_t sublen, newlen;
57 NDR_CHECK(ndr_pull_uint8(ndr, ndr_flags, &sublen));
58 newlen = total_len + sublen;
59 if (newlen < total_len) {
60 return ndr_pull_error(ndr, NDR_ERR_RANGE,
61 "Failed to pull dnsp_name");
63 if (i != count-1) {
64 if (newlen == UINT8_MAX) {
65 return ndr_pull_error(
66 ndr, NDR_ERR_RANGE,
67 "Failed to pull dnsp_name");
69 newlen++; /* for the '.' */
71 ret = talloc_realloc(ndr->current_mem_ctx, ret, char, newlen);
72 if (!ret) {
73 return ndr_pull_error(ndr, NDR_ERR_ALLOC, "Failed to pull dnsp_name");
75 NDR_CHECK(ndr_pull_bytes(ndr, (uint8_t *)&ret[total_len-1], sublen));
76 if (i != count-1) {
77 ret[newlen-2] = '.';
79 ret[newlen-1] = 0;
80 total_len = newlen;
82 NDR_CHECK(ndr_pull_uint8(ndr, ndr_flags, &termination));
83 if (termination != 0) {
84 return ndr_pull_error(ndr, NDR_ERR_ALLOC, "Failed to pull dnsp_name - not NUL terminated");
86 if (ndr->offset > raw_offset + len) {
87 return ndr_pull_error(ndr, NDR_ERR_ALLOC, "Failed to pull dnsp_name - overrun by %u bytes",
88 ndr->offset - (raw_offset + len));
90 /* there could be additional pad bytes */
91 while (ndr->offset < raw_offset + len) {
92 uint8_t pad;
93 NDR_CHECK(ndr_pull_uint8(ndr, ndr_flags, &pad));
95 (*name) = ret;
96 return NDR_ERR_SUCCESS;
99 enum ndr_err_code ndr_push_dnsp_name(struct ndr_push *ndr, int ndr_flags, const char *name)
101 int count, total_len, i;
103 /* count the dots */
104 for (count=i=0; name[i]; i++) {
105 if (name[i] == '.') count++;
107 total_len = strlen(name) + 1;
109 /* cope with names ending in '.' */
110 if (name[strlen(name)-1] != '.') {
111 total_len++;
112 count++;
114 if (total_len > 255 || count > 255) {
115 return ndr_push_error(ndr, NDR_ERR_BUFSIZE,
116 "dns_name of length %d larger than 255", total_len);
118 NDR_CHECK(ndr_push_uint8(ndr, ndr_flags, (uint8_t)total_len));
119 NDR_CHECK(ndr_push_uint8(ndr, ndr_flags, (uint8_t)count));
120 for (i=0; i<count; i++) {
121 const char *p = strchr(name, '.');
122 size_t sublen = p?(p-name):strlen(name);
123 NDR_CHECK(ndr_push_uint8(ndr, ndr_flags, (uint8_t)sublen));
124 NDR_CHECK(ndr_push_bytes(ndr, (const uint8_t *)name, sublen));
125 name += sublen + 1;
127 NDR_CHECK(ndr_push_uint8(ndr, ndr_flags, 0));
129 return NDR_ERR_SUCCESS;
133 print a dnsp_string
135 _PUBLIC_ void ndr_print_dnsp_string(struct ndr_print *ndr, const char *name,
136 const char *dns_string)
138 ndr->print(ndr, "%-25s: %s", name, dns_string);
142 pull a dnsp_string
144 _PUBLIC_ enum ndr_err_code ndr_pull_dnsp_string(struct ndr_pull *ndr, int ndr_flags, const char **string)
146 uint8_t len;
147 char *ret;
149 NDR_CHECK(ndr_pull_uint8(ndr, ndr_flags, &len));
151 ret = talloc_zero_array(ndr->current_mem_ctx, char, len + 1);
152 if (!ret) {
153 return ndr_pull_error(ndr, NDR_ERR_ALLOC, "Failed to pull dnsp_string");
155 NDR_CHECK(ndr_pull_bytes(ndr, (uint8_t *)ret, len));
157 (*string) = ret;
158 NDR_PULL_ALIGN(ndr, 1);
159 return NDR_ERR_SUCCESS;
162 enum ndr_err_code ndr_push_dnsp_string(struct ndr_push *ndr, int ndr_flags, const char *string)
164 int total_len;
165 total_len = strlen(string);
166 if (total_len > 255) {
167 return ndr_push_error(ndr, NDR_ERR_BUFSIZE,
168 "dns_name of length %d larger than 255", total_len);
170 NDR_CHECK(ndr_push_uint8(ndr, ndr_flags, (uint8_t)total_len));
171 NDR_CHECK(ndr_push_bytes(ndr, (const uint8_t *)string, total_len));
173 return NDR_ERR_SUCCESS;
177 * print a dnsp_string_list
179 _PUBLIC_ void ndr_print_dnsp_string_list(struct ndr_print *ndr, const char *name,
180 const struct dnsp_string_list *list)
182 uint32_t i;
184 ndr->no_newline = true;
185 for (i=0; i<ndr->depth; i++) {
186 ndr->print(ndr, " ");
188 ndr->print(ndr, "%-25s:", name);
189 for (i=0; i<list->count; i++) {
190 ndr->print(ndr, " \"%s\"", list->str[i]);
192 ndr->print(ndr, "\n");
193 ndr->no_newline = false;
197 * pull a dnsp_string_list
199 _PUBLIC_ enum ndr_err_code ndr_pull_dnsp_string_list(struct ndr_pull *ndr, int ndr_flags, struct dnsp_string_list *list)
201 list->count = 0;
202 list->str = talloc_array(ndr->current_mem_ctx, const char *,
203 list->count);
204 if (! list->str) {
205 return ndr_pull_error(ndr, NDR_ERR_ALLOC, "Failed to pull dnsp_string_list");
208 while (ndr->offset < ndr->data_size) {
209 list->str = talloc_realloc(ndr->current_mem_ctx, list->str,
210 const char *, list->count+1);
211 if (! list->str) {
212 return ndr_pull_error(ndr, NDR_ERR_ALLOC, "Failed to pull dnsp_string_list");
214 NDR_CHECK(ndr_pull_dnsp_string(ndr, ndr_flags, &list->str[list->count]));
215 list->count++;
218 return NDR_ERR_SUCCESS;
221 enum ndr_err_code ndr_push_dnsp_string_list(struct ndr_push *ndr, int ndr_flags, const struct dnsp_string_list *list)
223 uint8_t i;
225 for (i=0; i<list->count; i++) {
226 NDR_CHECK(ndr_push_dnsp_string(ndr, ndr_flags, list->str[i]));
228 return NDR_ERR_SUCCESS;
231 enum ndr_err_code ndr_dnsp_string_list_copy(TALLOC_CTX *mem_ctx,
232 const struct dnsp_string_list *src,
233 struct dnsp_string_list *dst)
235 size_t i;
237 dst->count = 0;
238 dst->str = talloc_zero_array(mem_ctx, const char *, src->count);
239 if (dst->str == NULL) {
240 return NDR_ERR_ALLOC;
243 for (i = 0; i < src->count; i++) {
244 dst->str[i] = talloc_strdup(dst->str, src->str[i]);
245 if (dst->str[i] == NULL) {
246 TALLOC_FREE(dst->str);
247 return NDR_ERR_ALLOC;
251 dst->count = src->count;
252 return NDR_ERR_SUCCESS;