librpc/ndr/ndr_misc.c

説明を見る。
00001 /* 
00002    Unix SMB/CIFS implementation.
00003 
00004    UUID/GUID/policy_handle functions
00005 
00006    Copyright (C) Theodore Ts'o               1996, 1997,
00007    Copyright (C) Jim McDonough                     2002.
00008    Copyright (C) Andrew Tridgell                   2003.
00009    Copyright (C) Stefan (metze) Metzmacher         2004.
00010    
00011    This program is free software; you can redistribute it and/or modify
00012    it under the terms of the GNU General Public License as published by
00013    the Free Software Foundation; either version 2 of the License, or
00014    (at your option) any later version.
00015    
00016    This program is distributed in the hope that it will be useful,
00017    but WITHOUT ANY WARRANTY; without even the implied warranty of
00018    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019    GNU General Public License for more details.
00020    
00021    You should have received a copy of the GNU General Public License
00022    along with this program; if not, write to the Free Software
00023    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00024 */
00025 
00026 #include "includes.h"
00027 
00028 NTSTATUS ndr_push_GUID(struct ndr_push *ndr, int ndr_flags, const struct GUID *r)
00029 {
00030         if (ndr_flags & NDR_SCALARS) {
00031                 NDR_CHECK(ndr_push_align(ndr, 4));
00032                 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, r->time_low));
00033                 NDR_CHECK(ndr_push_uint16(ndr, NDR_SCALARS, r->time_mid));
00034                 NDR_CHECK(ndr_push_uint16(ndr, NDR_SCALARS, r->time_hi_and_version));
00035                 NDR_CHECK(ndr_push_array_uint8(ndr, NDR_SCALARS, r->clock_seq, 2));
00036                 NDR_CHECK(ndr_push_array_uint8(ndr, NDR_SCALARS, r->node, 6));
00037         }
00038         if (ndr_flags & NDR_BUFFERS) {
00039         }
00040         return NT_STATUS_OK;
00041 }
00042 
00043 NTSTATUS ndr_pull_GUID(struct ndr_pull *ndr, int ndr_flags, struct GUID *r)
00044 {
00045         if (ndr_flags & NDR_SCALARS) {
00046                 NDR_CHECK(ndr_pull_align(ndr, 4));
00047                 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &r->time_low));
00048                 NDR_CHECK(ndr_pull_uint16(ndr, NDR_SCALARS, &r->time_mid));
00049                 NDR_CHECK(ndr_pull_uint16(ndr, NDR_SCALARS, &r->time_hi_and_version));
00050                 NDR_CHECK(ndr_pull_array_uint8(ndr, NDR_SCALARS, r->clock_seq, 2));
00051                 NDR_CHECK(ndr_pull_array_uint8(ndr, NDR_SCALARS, r->node, 6));
00052         }
00053         if (ndr_flags & NDR_BUFFERS) {
00054         }
00055         return NT_STATUS_OK;
00056 }
00057 
00058 size_t ndr_size_GUID(const struct GUID *r, int flags)
00059 {
00060         return ndr_size_struct(r, flags, (ndr_push_flags_fn_t)ndr_push_GUID);
00061 }
00062 
00063 /**
00064   build a GUID from a string
00065 */
00066 NTSTATUS GUID_from_string(const char *s, struct GUID *guid)
00067 {
00068         NTSTATUS status = NT_STATUS_INVALID_PARAMETER;
00069         uint32_t time_low;
00070         uint32_t time_mid, time_hi_and_version;
00071         uint32_t clock_seq[2];
00072         uint32_t node[6];
00073         int i;
00074 
00075         if (s == NULL) {
00076                 return NT_STATUS_INVALID_PARAMETER;
00077         }
00078 
00079         if (11 == sscanf(s, "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
00080                          &time_low, &time_mid, &time_hi_and_version, 
00081                          &clock_seq[0], &clock_seq[1],
00082                          &node[0], &node[1], &node[2], &node[3], &node[4], &node[5])) {
00083                 status = NT_STATUS_OK;
00084         } else if (11 == sscanf(s, "{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
00085                                 &time_low, &time_mid, &time_hi_and_version, 
00086                                 &clock_seq[0], &clock_seq[1],
00087                                 &node[0], &node[1], &node[2], &node[3], &node[4], &node[5])) {
00088                 status = NT_STATUS_OK;
00089         }
00090 
00091         if (!NT_STATUS_IS_OK(status)) {
00092                 return status;
00093         }
00094 
00095         guid->time_low = time_low;
00096         guid->time_mid = time_mid;
00097         guid->time_hi_and_version = time_hi_and_version;
00098         guid->clock_seq[0] = clock_seq[0];
00099         guid->clock_seq[1] = clock_seq[1];
00100         for (i=0;i<6;i++) {
00101                 guid->node[i] = node[i];
00102         }
00103 
00104         return NT_STATUS_OK;
00105 }
00106 
00107 /**
00108  * generate a random GUID
00109  */
00110 struct GUID GUID_random(void)
00111 {
00112         struct GUID guid;
00113 
00114         generate_random_buffer((uint8_t *)&guid, sizeof(guid));
00115         guid.clock_seq[0] = (guid.clock_seq[0] & 0x3F) | 0x80;
00116         guid.time_hi_and_version = (guid.time_hi_and_version & 0x0FFF) | 0x4000;
00117 
00118         return guid;
00119 }
00120 
00121 /**
00122  * generate an empty GUID 
00123  */
00124 struct GUID GUID_zero(void)
00125 {
00126         struct GUID guid;
00127 
00128         ZERO_STRUCT(guid);
00129 
00130         return guid;
00131 }
00132 
00133 /**
00134  * see if a range of memory is all zero. A NULL pointer is considered
00135  * to be all zero 
00136  */
00137 BOOL all_zero(const uint8_t *ptr, size_t size)
00138 {
00139         int i;
00140         if (!ptr) return True;
00141         for (i=0;i<size;i++) {
00142                 if (ptr[i]) return False;
00143         }
00144         return True;
00145 }
00146 
00147 
00148 BOOL GUID_all_zero(const struct GUID *u)
00149 {
00150         if (u->time_low != 0 ||
00151             u->time_mid != 0 ||
00152             u->time_hi_and_version != 0 ||
00153             u->clock_seq[0] != 0 ||
00154             u->clock_seq[1] != 0 ||
00155             !all_zero(u->node, 6)) {
00156                 return False;
00157         }
00158         return True;
00159 }
00160 
00161 BOOL GUID_equal(const struct GUID *u1, const struct GUID *u2)
00162 {
00163         if (u1->time_low != u2->time_low ||
00164             u1->time_mid != u2->time_mid ||
00165             u1->time_hi_and_version != u2->time_hi_and_version ||
00166             u1->clock_seq[0] != u2->clock_seq[0] ||
00167             u1->clock_seq[1] != u2->clock_seq[1] ||
00168             memcmp(u1->node, u2->node, 6) != 0) {
00169                 return False;
00170         }
00171         return True;
00172 }
00173 
00174 /**
00175   its useful to be able to display these in debugging messages
00176 */
00177 char *GUID_string(TALLOC_CTX *mem_ctx, const struct GUID *guid)
00178 {
00179         return talloc_asprintf(mem_ctx, 
00180                                "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
00181                                guid->time_low, guid->time_mid,
00182                                guid->time_hi_and_version,
00183                                guid->clock_seq[0],
00184                                guid->clock_seq[1],
00185                                guid->node[0], guid->node[1],
00186                                guid->node[2], guid->node[3],
00187                                guid->node[4], guid->node[5]);
00188 }
00189 
00190 char *GUID_string2(TALLOC_CTX *mem_ctx, const struct GUID *guid)
00191 {
00192         char *ret, *s = GUID_string(mem_ctx, guid);
00193         ret = talloc_asprintf(mem_ctx, "{%s}", s);
00194         talloc_free(s);
00195         return ret;
00196 }
00197 
00198 void ndr_print_GUID(struct ndr_print *ndr, const char *name, const struct GUID *guid)
00199 {
00200         ndr->print(ndr, "%-25s: %s", name, GUID_string(ndr, guid));
00201 }
00202 
00203 BOOL policy_handle_empty(struct policy_handle *h) 
00204 {
00205         return (h->handle_type == 0 && GUID_all_zero(&h->uuid));
00206 }
00207 
00208 NTSTATUS ndr_push_policy_handle(struct ndr_push *ndr, int ndr_flags, const struct policy_handle *r)
00209 {
00210         if (ndr_flags & NDR_SCALARS) {
00211                 NDR_CHECK(ndr_push_align(ndr, 4));
00212                 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, r->handle_type));
00213                 NDR_CHECK(ndr_push_GUID(ndr, NDR_SCALARS, &r->uuid));
00214         }
00215         if (ndr_flags & NDR_BUFFERS) {
00216         }
00217         return NT_STATUS_OK;
00218 }
00219 
00220 NTSTATUS ndr_pull_policy_handle(struct ndr_pull *ndr, int ndr_flags, struct policy_handle *r)
00221 {
00222         if (ndr_flags & NDR_SCALARS) {
00223                 NDR_CHECK(ndr_pull_align(ndr, 4));
00224                 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &r->handle_type));
00225                 NDR_CHECK(ndr_pull_GUID(ndr, NDR_SCALARS, &r->uuid));
00226         }
00227         if (ndr_flags & NDR_BUFFERS) {
00228         }
00229         return NT_STATUS_OK;
00230 }
00231 
00232 void ndr_print_policy_handle(struct ndr_print *ndr, const char *name, const struct policy_handle *r)
00233 {
00234         ndr_print_struct(ndr, name, "policy_handle");
00235         ndr->depth++;
00236         ndr_print_uint32(ndr, "handle_type", r->handle_type);
00237         ndr_print_GUID(ndr, "uuid", &r->uuid);
00238         ndr->depth--;
00239 }
00240 
00241 NTSTATUS ndr_push_server_id(struct ndr_push *ndr, int ndr_flags, const struct server_id *r)
00242 {
00243         if (ndr_flags & NDR_SCALARS) {
00244                 NDR_CHECK(ndr_push_align(ndr, 4));
00245                 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS,
00246                                           (uint32_t)r->id.pid));
00247         }
00248         if (ndr_flags & NDR_BUFFERS) {
00249         }
00250         return NT_STATUS_OK;
00251 }
00252 
00253 NTSTATUS ndr_pull_server_id(struct ndr_pull *ndr, int ndr_flags, struct server_id *r)
00254 {
00255         if (ndr_flags & NDR_SCALARS) {
00256                 uint32_t pid;
00257                 NDR_CHECK(ndr_pull_align(ndr, 4));
00258                 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &pid));
00259                 r->id.pid = (pid_t)pid;
00260         }
00261         if (ndr_flags & NDR_BUFFERS) {
00262         }
00263         return NT_STATUS_OK;
00264 }
00265 
00266 void ndr_print_server_id(struct ndr_print *ndr, const char *name, const struct server_id *r)
00267 {
00268         ndr_print_struct(ndr, name, "server_id");
00269         ndr->depth++;
00270         ndr_print_uint32(ndr, "id", (uint32_t)r->id.pid);
00271         ndr->depth--;
00272 }

Sambaに対してSat Aug 29 21:23:04 2009に生成されました。  doxygen 1.4.7