torture: copychunk test suite improvements
[Samba/gbeck.git] / librpc / ndr / ndr_dnsp.c
blobfcb623ad82228ba38bed340bc00fea00843e4359
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 (i != count-1) {
60 newlen++; /* for the '.' */
62 ret = talloc_realloc(ndr->current_mem_ctx, ret, char, newlen);
63 if (!ret) {
64 return ndr_pull_error(ndr, NDR_ERR_ALLOC, "Failed to pull dnsp_name");
66 NDR_CHECK(ndr_pull_bytes(ndr, (uint8_t *)&ret[total_len-1], sublen));
67 if (i != count-1) {
68 ret[newlen-2] = '.';
70 ret[newlen-1] = 0;
71 total_len = newlen;
73 NDR_CHECK(ndr_pull_uint8(ndr, ndr_flags, &termination));
74 if (termination != 0) {
75 return ndr_pull_error(ndr, NDR_ERR_ALLOC, "Failed to pull dnsp_name - not NUL terminated");
77 if (ndr->offset > raw_offset + len) {
78 return ndr_pull_error(ndr, NDR_ERR_ALLOC, "Failed to pull dnsp_name - overrun by %u bytes",
79 ndr->offset - (raw_offset + len));
81 /* there could be additional pad bytes */
82 while (ndr->offset < raw_offset + len) {
83 uint8_t pad;
84 NDR_CHECK(ndr_pull_uint8(ndr, ndr_flags, &pad));
86 (*name) = ret;
87 return NDR_ERR_SUCCESS;
90 enum ndr_err_code ndr_push_dnsp_name(struct ndr_push *ndr, int ndr_flags, const char *name)
92 int count, total_len, i;
94 /* count the dots */
95 for (count=i=0; name[i]; i++) {
96 if (name[i] == '.') count++;
98 total_len = strlen(name) + 1;
100 /* cope with names ending in '.' */
101 if (name[strlen(name)-1] != '.') {
102 total_len++;
103 count++;
105 if (total_len > 255 || count > 255) {
106 return ndr_push_error(ndr, NDR_ERR_BUFSIZE,
107 "dns_name of length %d larger than 255", total_len);
109 NDR_CHECK(ndr_push_uint8(ndr, ndr_flags, (uint8_t)total_len));
110 NDR_CHECK(ndr_push_uint8(ndr, ndr_flags, (uint8_t)count));
111 for (i=0; i<count; i++) {
112 const char *p = strchr(name, '.');
113 size_t sublen = p?(p-name):strlen(name);
114 NDR_CHECK(ndr_push_uint8(ndr, ndr_flags, (uint8_t)sublen));
115 NDR_CHECK(ndr_push_bytes(ndr, (const uint8_t *)name, sublen));
116 name += sublen + 1;
118 NDR_CHECK(ndr_push_uint8(ndr, ndr_flags, 0));
120 return NDR_ERR_SUCCESS;
124 print a dnsp_string
126 _PUBLIC_ void ndr_print_dnsp_string(struct ndr_print *ndr, const char *name,
127 const char *dns_string)
129 ndr->print(ndr, "%-25s: %s", name, dns_string);
133 pull a dnsp_string
135 _PUBLIC_ enum ndr_err_code ndr_pull_dnsp_string(struct ndr_pull *ndr, int ndr_flags, const char **string)
137 uint8_t len;
138 uint32_t total_len;
139 char *ret;
141 NDR_CHECK(ndr_pull_uint8(ndr, ndr_flags, &len));
143 ret = talloc_strdup(ndr->current_mem_ctx, "");
144 if (!ret) {
145 return ndr_pull_error(ndr, NDR_ERR_ALLOC, "Failed to pull dnsp_string");
147 total_len = 1;
148 ret = talloc_zero_array(ndr->current_mem_ctx, char, len+1);
149 if (!ret) {
150 return ndr_pull_error(ndr, NDR_ERR_ALLOC, "Failed to pull dnsp_string");
152 NDR_CHECK(ndr_pull_bytes(ndr, (uint8_t *)&ret[total_len-1], len));
153 total_len = len;
155 (*string) = ret;
156 NDR_PULL_ALIGN(ndr, 1);
157 return NDR_ERR_SUCCESS;
160 enum ndr_err_code ndr_push_dnsp_string(struct ndr_push *ndr, int ndr_flags, const char *string)
162 int total_len;
163 total_len = strlen(string);
164 if (total_len > 255) {
165 return ndr_push_error(ndr, NDR_ERR_BUFSIZE,
166 "dns_name of length %d larger than 255", total_len);
168 NDR_CHECK(ndr_push_uint8(ndr, ndr_flags, (uint8_t)total_len));
169 NDR_CHECK(ndr_push_bytes(ndr, (const uint8_t *)string, total_len));
171 return NDR_ERR_SUCCESS;
175 * print a dnsp_string_list
177 _PUBLIC_ void ndr_print_dnsp_string_list(struct ndr_print *ndr, const char *name,
178 const struct dnsp_string_list *list)
180 uint32_t i;
182 ndr->no_newline = true;
183 for (i=0; i<ndr->depth; i++) {
184 ndr->print(ndr, " ");
186 ndr->print(ndr, "%-25s:", name);
187 for (i=0; i<list->count; i++) {
188 ndr->print(ndr, " \"%s\"", list->str[i]);
190 ndr->print(ndr, "\n");
191 ndr->no_newline = false;
195 * pull a dnsp_string_list
197 _PUBLIC_ enum ndr_err_code ndr_pull_dnsp_string_list(struct ndr_pull *ndr, int ndr_flags, struct dnsp_string_list *list)
199 list->count = 0;
200 list->str = talloc_array(ndr->current_mem_ctx, const char *,
201 list->count);
202 if (! list->str) {
203 return ndr_pull_error(ndr, NDR_ERR_ALLOC, "Failed to pull dnsp_string_list");
206 while (ndr->offset < ndr->data_size) {
207 list->str = talloc_realloc(ndr->current_mem_ctx, list->str,
208 const char *, list->count+1);
209 if (! list->str) {
210 return ndr_pull_error(ndr, NDR_ERR_ALLOC, "Failed to pull dnsp_string_list");
212 NDR_CHECK(ndr_pull_dnsp_string(ndr, ndr_flags, &list->str[list->count]));
213 list->count++;
216 return NDR_ERR_SUCCESS;
219 enum ndr_err_code ndr_push_dnsp_string_list(struct ndr_push *ndr, int ndr_flags, const struct dnsp_string_list *list)
221 uint8_t i;
223 for (i=0; i<list->count; i++) {
224 NDR_CHECK(ndr_push_dnsp_string(ndr, ndr_flags, list->str[i]));
226 return NDR_ERR_SUCCESS;