| 1 | /*
|
|---|
| 2 | Unix SMB/CIFS implementation.
|
|---|
| 3 | Easy management of byte-length data
|
|---|
| 4 | Copyright (C) Andrew Tridgell 2001
|
|---|
| 5 | Copyright (C) Andrew Bartlett 2001
|
|---|
| 6 |
|
|---|
| 7 | This program is free software; you can redistribute it and/or modify
|
|---|
| 8 | it under the terms of the GNU General Public License as published by
|
|---|
| 9 | the Free Software Foundation; either version 2 of the License, or
|
|---|
| 10 | (at your option) any later version.
|
|---|
| 11 |
|
|---|
| 12 | This program is distributed in the hope that it will be useful,
|
|---|
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 15 | GNU General Public License for more details.
|
|---|
| 16 |
|
|---|
| 17 | You should have received a copy of the GNU General Public License
|
|---|
| 18 | along with this program; if not, write to the Free Software
|
|---|
| 19 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|---|
| 20 | */
|
|---|
| 21 |
|
|---|
| 22 | #include "includes.h"
|
|---|
| 23 |
|
|---|
| 24 | /*******************************************************************
|
|---|
| 25 | Free() a data blob.
|
|---|
| 26 | *******************************************************************/
|
|---|
| 27 |
|
|---|
| 28 | static void free_data_blob(DATA_BLOB *d)
|
|---|
| 29 | {
|
|---|
| 30 | if ((d) && (d->free)) {
|
|---|
| 31 | SAFE_FREE(d->data);
|
|---|
| 32 | }
|
|---|
| 33 | }
|
|---|
| 34 |
|
|---|
| 35 | /*******************************************************************
|
|---|
| 36 | Construct a data blob, must be freed with data_blob_free().
|
|---|
| 37 | You can pass NULL for p and get a blank data blob
|
|---|
| 38 | *******************************************************************/
|
|---|
| 39 |
|
|---|
| 40 | DATA_BLOB data_blob(const void *p, size_t length)
|
|---|
| 41 | {
|
|---|
| 42 | DATA_BLOB ret;
|
|---|
| 43 |
|
|---|
| 44 | if (!length) {
|
|---|
| 45 | ZERO_STRUCT(ret);
|
|---|
| 46 | return ret;
|
|---|
| 47 | }
|
|---|
| 48 |
|
|---|
| 49 | if (p) {
|
|---|
| 50 | ret.data = (uint8 *)smb_xmemdup(p, length);
|
|---|
| 51 | } else {
|
|---|
| 52 | ret.data = SMB_XMALLOC_ARRAY(uint8, length);
|
|---|
| 53 | }
|
|---|
| 54 | ret.length = length;
|
|---|
| 55 | ret.free = free_data_blob;
|
|---|
| 56 | return ret;
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | /*******************************************************************
|
|---|
| 60 | Construct a data blob, using supplied TALLOC_CTX.
|
|---|
| 61 | *******************************************************************/
|
|---|
| 62 |
|
|---|
| 63 | DATA_BLOB data_blob_talloc(TALLOC_CTX *mem_ctx, const void *p, size_t length)
|
|---|
| 64 | {
|
|---|
| 65 | DATA_BLOB ret;
|
|---|
| 66 |
|
|---|
| 67 | if (!length) {
|
|---|
| 68 | ZERO_STRUCT(ret);
|
|---|
| 69 | return ret;
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | if (p) {
|
|---|
| 73 | ret.data = (uint8 *)TALLOC_MEMDUP(mem_ctx, p, length);
|
|---|
| 74 | if (ret.data == NULL)
|
|---|
| 75 | smb_panic("data_blob_talloc: TALLOC_MEMDUP failed.\n");
|
|---|
| 76 | } else {
|
|---|
| 77 | ret.data = (uint8 *)TALLOC(mem_ctx, length);
|
|---|
| 78 | if (ret.data == NULL)
|
|---|
| 79 | smb_panic("data_blob_talloc: talloc failed.\n");
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | ret.length = length;
|
|---|
| 83 | ret.free = NULL;
|
|---|
| 84 | return ret;
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | /*******************************************************************
|
|---|
| 88 | Free a data blob.
|
|---|
| 89 | *******************************************************************/
|
|---|
| 90 |
|
|---|
| 91 | void data_blob_free(DATA_BLOB *d)
|
|---|
| 92 | {
|
|---|
| 93 | if (d) {
|
|---|
| 94 | if (d->free) {
|
|---|
| 95 | (d->free)(d);
|
|---|
| 96 | }
|
|---|
| 97 | d->length = 0;
|
|---|
| 98 | }
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | /*******************************************************************
|
|---|
| 102 | Clear a DATA_BLOB's contents
|
|---|
| 103 | *******************************************************************/
|
|---|
| 104 |
|
|---|
| 105 | void data_blob_clear(DATA_BLOB *d)
|
|---|
| 106 | {
|
|---|
| 107 | if (d->data) {
|
|---|
| 108 | memset(d->data, 0, d->length);
|
|---|
| 109 | }
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | /*******************************************************************
|
|---|
| 113 | Free a data blob and clear its contents
|
|---|
| 114 | *******************************************************************/
|
|---|
| 115 |
|
|---|
| 116 | void data_blob_clear_free(DATA_BLOB *d)
|
|---|
| 117 | {
|
|---|
| 118 | data_blob_clear(d);
|
|---|
| 119 | data_blob_free(d);
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | /**
|
|---|
| 123 | useful for constructing data blobs in test suites, while
|
|---|
| 124 | avoiding const warnings
|
|---|
| 125 | **/
|
|---|
| 126 | DATA_BLOB data_blob_string_const(const char *str)
|
|---|
| 127 | {
|
|---|
| 128 | DATA_BLOB blob;
|
|---|
| 129 | blob.data = CONST_DISCARD(uint8 *, str);
|
|---|
| 130 | blob.length = strlen(str);
|
|---|
| 131 | blob.free = NULL;
|
|---|
| 132 | return blob;
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 | /**
|
|---|
| 136 | * Create a new data blob from const data
|
|---|
| 137 | */
|
|---|
| 138 | DATA_BLOB data_blob_const(const void *p, size_t length)
|
|---|
| 139 | {
|
|---|
| 140 | DATA_BLOB blob;
|
|---|
| 141 | blob.data = CONST_DISCARD(uint8 *, p);
|
|---|
| 142 | blob.length = length;
|
|---|
| 143 | blob.free = NULL;
|
|---|
| 144 | return blob;
|
|---|
| 145 | }
|
|---|