Add a new header file for functions in lib/util/util.c.
[obnox/samba/samba-obnox.git] / librpc / ndr / libndr.h
index 8070c3cb56f24b78a1d1d41cc18c8073f141c1e7..c6116ed8119fadcb2ee7f286dfc1310fba3dff5d 100644 (file)
 #define __LIBNDR_H__
 
 #include <talloc.h>
-#include <sys/time.h>
-#include "../lib/util/samba_util.h" /* for discard_const */
+#include "../lib/util/memory.h" /* for discard_const */
+#include "../lib/util/byteorder.h"
+#include "../lib/util/data_blob.h"
+#include "../lib/util/time.h"
 #include "../lib/util/charset/charset.h"
 
 /*
@@ -123,6 +125,14 @@ struct ndr_print {
 #define LIBNDR_FLAG_STR_RAW8           (1<<13)
 #define LIBNDR_STRING_FLAGS            (0x7FFC)
 
+/*
+ * don't debug NDR_ERR_BUFSIZE failures,
+ * as the available buffer might be incomplete.
+ *
+ * return NDR_ERR_INCOMPLETE_BUFFER instead.
+ */
+#define LIBNDR_FLAG_INCOMPLETE_BUFFER (1<<16)
+
 /*
  * This lets ndr_pull_subcontext_end() return
  * NDR_ERR_UNREAD_BYTES.
@@ -206,7 +216,8 @@ enum ndr_err_code {
        NDR_ERR_INVALID_POINTER,
        NDR_ERR_UNREAD_BYTES,
        NDR_ERR_NDR64,
-       NDR_ERR_FLAGS
+       NDR_ERR_FLAGS,
+       NDR_ERR_INCOMPLETE_BUFFER
 };
 
 #define NDR_ERR_CODE_IS_SUCCESS(x) (x == NDR_ERR_SUCCESS)
@@ -265,6 +276,11 @@ enum ndr_compression_alg {
 
 #define NDR_PULL_NEED_BYTES(ndr, n) do { \
        if (unlikely((n) > ndr->data_size || ndr->offset + (n) > ndr->data_size)) { \
+               if (ndr->flags & LIBNDR_FLAG_INCOMPLETE_BUFFER) { \
+                       uint32_t _available = ndr->data_size - ndr->offset; \
+                       uint32_t _missing = n - _available; \
+                       ndr->relative_highest_offset = _missing; \
+               } \
                return ndr_pull_error(ndr, NDR_ERR_BUFSIZE, "Pull bytes %u (%s)", (unsigned)n, __location__); \
        } \
 } while(0)
@@ -281,6 +297,10 @@ enum ndr_compression_alg {
                ndr->offset = (ndr->offset + (n-1)) & ~(n-1); \
        } \
        if (unlikely(ndr->offset > ndr->data_size)) {                   \
+               if (ndr->flags & LIBNDR_FLAG_INCOMPLETE_BUFFER) { \
+                       uint32_t _missing = ndr->offset - ndr->data_size; \
+                       ndr->relative_highest_offset = _missing; \
+               } \
                return ndr_pull_error(ndr, NDR_ERR_BUFSIZE, "Pull align %u", (unsigned)n); \
        } \
 } while(0)
@@ -440,6 +460,8 @@ size_t ndr_size_dom_sid0(const struct dom_sid *sid, int flags);
 void ndr_print_GUID(struct ndr_print *ndr, const char *name, const struct GUID *guid);
 void ndr_print_sockaddr_storage(struct ndr_print *ndr, const char *name, const struct sockaddr_storage *ss);
 bool ndr_syntax_id_equal(const struct ndr_syntax_id *i1, const struct ndr_syntax_id *i2); 
+char *ndr_syntax_id_to_string(TALLOC_CTX *mem_ctx, const struct ndr_syntax_id *id);
+bool ndr_syntax_id_from_string(const char *s, struct ndr_syntax_id *id);
 enum ndr_err_code ndr_push_struct_blob(DATA_BLOB *blob, TALLOC_CTX *mem_ctx, const void *p, ndr_push_flags_fn_t fn);
 enum ndr_err_code ndr_push_union_blob(DATA_BLOB *blob, TALLOC_CTX *mem_ctx, void *p, uint32_t level, ndr_push_flags_fn_t fn);
 size_t ndr_size_struct(const void *p, int flags, ndr_push_flags_fn_t push);
@@ -462,6 +484,8 @@ enum ndr_err_code ndr_pull_relative_ptr2(struct ndr_pull *ndr, const void *p);
 enum ndr_err_code ndr_pull_relative_ptr_short(struct ndr_pull *ndr, uint16_t *v);
 size_t ndr_align_size(uint32_t offset, size_t n);
 struct ndr_pull *ndr_pull_init_blob(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx);
+enum ndr_err_code ndr_pull_append(struct ndr_pull *ndr, DATA_BLOB *blob);
+enum ndr_err_code ndr_pull_pop(struct ndr_pull *ndr);
 enum ndr_err_code ndr_pull_advance(struct ndr_pull *ndr, uint32_t size);
 struct ndr_push *ndr_push_init_ctx(TALLOC_CTX *mem_ctx);
 DATA_BLOB ndr_push_blob(struct ndr_push *ndr);
@@ -534,6 +558,11 @@ enum ndr_err_code ndr_push_ ## name(struct ndr_push *ndr, int ndr_flags, type v)
 enum ndr_err_code ndr_pull_ ## name(struct ndr_pull *ndr, int ndr_flags, type *v); \
 void ndr_print_ ## name(struct ndr_print *ndr, const char *var_name, type v); 
 
+#define NDR_SCALAR_PTR_PROTO(name, type) \
+enum ndr_err_code ndr_push_ ## name(struct ndr_push *ndr, int ndr_flags, const type *v); \
+enum ndr_err_code ndr_pull_ ## name(struct ndr_pull *ndr, int ndr_flags, type **v); \
+void ndr_print_ ## name(struct ndr_print *ndr, const char *var_name, const type *v);
+
 #define NDR_BUFFER_PROTO(name, type) \
 enum ndr_err_code ndr_push_ ## name(struct ndr_push *ndr, int ndr_flags, const type *v); \
 enum ndr_err_code ndr_pull_ ## name(struct ndr_pull *ndr, int ndr_flags, type *v); \
@@ -558,6 +587,7 @@ NDR_SCALAR_PROTO(uid_t, uid_t)
 NDR_SCALAR_PROTO(gid_t, gid_t)
 NDR_SCALAR_PROTO(NTSTATUS, NTSTATUS)
 NDR_SCALAR_PROTO(WERROR, WERROR)
+NDR_SCALAR_PROTO(HRESULT, HRESULT)
 NDR_SCALAR_PROTO(NTTIME, NTTIME)
 NDR_SCALAR_PROTO(NTTIME_1sec, NTTIME)
 NDR_SCALAR_PROTO(NTTIME_hyper, NTTIME)
@@ -631,6 +661,12 @@ char *GUID_string2(TALLOC_CTX *mem_ctx, const struct GUID *guid);
 char *GUID_hexstring(TALLOC_CTX *mem_ctx, const struct GUID *guid);
 struct GUID GUID_random(void);
 
+/* Format is "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x" */
+ /* 32 chars + 4 ' ' + \0 + 2 for adding {}  */
+struct GUID_txt_buf { char buf[39]; };
+_PUBLIC_ char* GUID_buf_string(const struct GUID *guid,
+                              struct GUID_txt_buf *dst);
+
 _PUBLIC_ enum ndr_err_code ndr_pull_enum_uint8(struct ndr_pull *ndr, int ndr_flags, uint8_t *v);
 _PUBLIC_ enum ndr_err_code ndr_pull_enum_uint16(struct ndr_pull *ndr, int ndr_flags, uint16_t *v);
 _PUBLIC_ enum ndr_err_code ndr_pull_enum_uint32(struct ndr_pull *ndr, int ndr_flags, uint32_t *v);