Blame data_blob.c

Packit 5f9837
/* 
Packit 5f9837
   Unix SMB/CIFS implementation.
Packit 5f9837
   Easy management of byte-length data
Packit 5f9837
   Copyright (C) Andrew Tridgell 2001
Packit 5f9837
   Copyright (C) Andrew Bartlett 2001
Packit 5f9837
   
Packit 5f9837
   This program is free software; you can redistribute it and/or modify
Packit 5f9837
   it under the terms of the GNU General Public License as published by
Packit 5f9837
   the Free Software Foundation; either version 3 of the License, or
Packit 5f9837
   (at your option) any later version.
Packit 5f9837
   
Packit 5f9837
   This program is distributed in the hope that it will be useful,
Packit 5f9837
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 5f9837
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 5f9837
   GNU General Public License for more details.
Packit 5f9837
   
Packit 5f9837
   You should have received a copy of the GNU General Public License
Packit 5f9837
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
Packit 5f9837
*/
Packit 5f9837
Packit 5f9837
#include <string.h>
Packit 5f9837
Packit 5f9837
#include "data_blob.h"
Packit 5f9837
Packit 5f9837
#ifndef ZERO_STRUCT
Packit 5f9837
#define ZERO_STRUCT(x) memset((char *)&(x), 0, sizeof(x))
Packit 5f9837
#endif
Packit 5f9837
Packit 5f9837
const DATA_BLOB data_blob_null = { NULL, 0 };
Packit 5f9837
Packit 5f9837
/**
Packit 5f9837
 * @file
Packit 5f9837
 * @brief Manipulation of arbitrary data blobs
Packit 5f9837
 **/
Packit 5f9837
Packit 5f9837
/**
Packit 5f9837
 construct a data blob, must be freed with data_blob_free()
Packit 5f9837
 you can pass NULL for p and get a blank data blob
Packit 5f9837
**/
Packit 5f9837
DATA_BLOB data_blob_named(const void *p, size_t length, const char *name)
Packit 5f9837
{
Packit 5f9837
	DATA_BLOB ret;
Packit 5f9837
Packit 5f9837
	if (p == NULL && length == 0) {
Packit 5f9837
		ZERO_STRUCT(ret);
Packit 5f9837
		return ret;
Packit 5f9837
	}
Packit 5f9837
Packit 5f9837
	if (p) {
Packit 5f9837
		ret.data = (uint8_t *)talloc_memdup(NULL, p, length);
Packit 5f9837
	} else {
Packit 5f9837
		ret.data = talloc_array(NULL, uint8_t, length);
Packit 5f9837
	}
Packit 5f9837
	if (ret.data == NULL) {
Packit 5f9837
		ret.length = 0;
Packit 5f9837
		return ret;
Packit 5f9837
	}
Packit 5f9837
	talloc_set_name_const(ret.data, name);
Packit 5f9837
	ret.length = length;
Packit 5f9837
	return ret;
Packit 5f9837
}
Packit 5f9837
Packit 5f9837
/**
Packit 5f9837
 construct a data blob, using supplied TALLOC_CTX
Packit 5f9837
**/
Packit 5f9837
DATA_BLOB data_blob_talloc_named(TALLOC_CTX *mem_ctx, const void *p, size_t length, const char *name)
Packit 5f9837
{
Packit 5f9837
	DATA_BLOB ret = data_blob_named(p, length, name);
Packit 5f9837
Packit 5f9837
	if (ret.data) {
Packit 5f9837
		talloc_steal(mem_ctx, ret.data);
Packit 5f9837
	}
Packit 5f9837
	return ret;
Packit 5f9837
}
Packit 5f9837
Packit 5f9837
/**
Packit 5f9837
free a data blob
Packit 5f9837
**/
Packit 5f9837
void data_blob_free(DATA_BLOB *d)
Packit 5f9837
{
Packit 5f9837
	if (d) {
Packit 5f9837
		talloc_free(d->data);
Packit 5f9837
		d->data = NULL;
Packit 5f9837
		d->length = 0;
Packit 5f9837
	}
Packit 5f9837
}
Packit 5f9837