lib/data_blob.c

説明を見る。
00001 /* 
00002    Unix SMB/CIFS implementation.
00003    Easy management of byte-length data
00004    Copyright (C) Andrew Tridgell 2001
00005    Copyright (C) Andrew Bartlett 2001
00006    
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 2 of the License, or
00010    (at your option) any later version.
00011    
00012    This program is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016    
00017    You should have received a copy of the GNU General Public License
00018    along with this program; if not, write to the Free Software
00019    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00020 */
00021 
00022 #include "includes.h"
00023 
00024 /*******************************************************************
00025  Free() a data blob.
00026 *******************************************************************/
00027 
00028 static void free_data_blob(DATA_BLOB *d)
00029 {
00030         if ((d) && (d->free)) {
00031                 SAFE_FREE(d->data);
00032         }
00033 }
00034 
00035 /*******************************************************************
00036  Construct a data blob, must be freed with data_blob_free().
00037  You can pass NULL for p and get a blank data blob
00038 *******************************************************************/
00039 
00040 DATA_BLOB data_blob(const void *p, size_t length)
00041 {
00042         DATA_BLOB ret;
00043 
00044         if (!length) {
00045                 ZERO_STRUCT(ret);
00046                 return ret;
00047         }
00048 
00049         if (p) {
00050                 ret.data = (uint8 *)smb_xmemdup(p, length);
00051         } else {
00052                 ret.data = SMB_XMALLOC_ARRAY(uint8, length);
00053         }
00054         ret.length = length;
00055         ret.free = free_data_blob;
00056         return ret;
00057 }
00058 
00059 /*******************************************************************
00060  Construct a data blob, using supplied TALLOC_CTX.
00061 *******************************************************************/
00062 
00063 DATA_BLOB data_blob_talloc(TALLOC_CTX *mem_ctx, const void *p, size_t length)
00064 {
00065         DATA_BLOB ret;
00066 
00067         if (!length) {
00068                 ZERO_STRUCT(ret);
00069                 return ret;
00070         }
00071 
00072         if (p) {
00073                 ret.data = (uint8 *)TALLOC_MEMDUP(mem_ctx, p, length);
00074                 if (ret.data == NULL)
00075                         smb_panic("data_blob_talloc: TALLOC_MEMDUP failed.\n");
00076         } else {
00077                 ret.data = (uint8 *)TALLOC(mem_ctx, length);
00078                 if (ret.data == NULL)
00079                         smb_panic("data_blob_talloc: talloc failed.\n");
00080         }
00081 
00082         ret.length = length;
00083         ret.free = NULL;
00084         return ret;
00085 }
00086 
00087 /*******************************************************************
00088  Free a data blob.
00089 *******************************************************************/
00090 
00091 void data_blob_free(DATA_BLOB *d)
00092 {
00093         if (d) {
00094                 if (d->free) {
00095                         (d->free)(d);
00096                 }
00097                 d->length = 0;
00098         }
00099 }
00100 
00101 /*******************************************************************
00102  Clear a DATA_BLOB's contents
00103 *******************************************************************/
00104 
00105 void data_blob_clear(DATA_BLOB *d)
00106 {
00107         if (d->data) {
00108                 memset(d->data, 0, d->length);
00109         }
00110 }
00111 
00112 /*******************************************************************
00113  Free a data blob and clear its contents
00114 *******************************************************************/
00115 
00116 void data_blob_clear_free(DATA_BLOB *d)
00117 {
00118         data_blob_clear(d);
00119         data_blob_free(d);
00120 }
00121 
00122 /**
00123   useful for constructing data blobs in test suites, while
00124   avoiding const warnings
00125 **/
00126 DATA_BLOB data_blob_string_const(const char *str)
00127 {
00128         DATA_BLOB blob;
00129         blob.data = CONST_DISCARD(uint8 *, str);
00130         blob.length = strlen(str);
00131         blob.free = NULL;
00132         return blob;
00133 }
00134 
00135 /**
00136  * Create a new data blob from const data 
00137  */
00138 DATA_BLOB data_blob_const(const void *p, size_t length)
00139 {
00140         DATA_BLOB blob;
00141         blob.data = CONST_DISCARD(uint8 *, p);
00142         blob.length = length;
00143         blob.free = NULL;
00144         return blob;
00145 }

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