r26570: - Trim size of the swig-generated Python bindings by removing a bunch of...
[samba.git] / source4 / scripting / ejs / smbcalls_data.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    provide access to data blobs
5
6    Copyright (C) Andrew Tridgell 2005
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "scripting/ejs/smbcalls.h"
24 #include "lib/appweb/ejs/ejs.h"
25 #include "librpc/gen_ndr/winreg.h"
26
27 /*
28   create a data blob object from a ejs array of integers
29 */
30 static int ejs_blobFromArray(MprVarHandle eid, int argc, struct MprVar **argv)
31 {
32         struct MprVar *array, *v;
33         unsigned length, i;
34         DATA_BLOB blob;
35
36         if (argc != 1) {
37                 ejsSetErrorMsg(eid, "blobFromArray invalid arguments");
38                 return -1;              
39         }
40         array = argv[0];
41
42         v = mprGetProperty(array, "length", NULL);
43         if (v == NULL) {
44                 goto failed;
45         }
46         length = mprToInt(v);
47
48         blob = data_blob_talloc(mprMemCtx(), NULL, length);
49         if (length != 0 && blob.data == NULL) {
50                 goto failed;
51         }
52
53         for (i=0;i<length;i++) {
54                 struct MprVar *vs;
55                 char idx[16];
56                 mprItoa(i, idx, sizeof(idx));           
57                 vs = mprGetProperty(array, idx, NULL);
58                 if (vs == NULL) {
59                         goto failed;
60                 }
61                 blob.data[i] = mprVarToNumber(vs);
62         }
63
64         mpr_Return(eid, mprDataBlob(blob));
65         return 0;
66
67 failed:
68         mpr_Return(eid, mprCreateUndefinedVar());
69         return 0;
70 }
71
72 /*
73   create a ejs array of integers from a data blob
74 */
75 static int ejs_blobToArray(MprVarHandle eid, int argc, struct MprVar **argv)
76 {
77         DATA_BLOB *blob;
78         struct MprVar array;
79         int i;
80
81         if (argc != 1) {
82                 ejsSetErrorMsg(eid, "blobToArray invalid arguments");
83                 return -1;              
84         }
85         blob = mprToDataBlob(argv[0]);
86         if (blob == NULL) {
87                 goto failed;
88         }
89
90         array = mprArray("array");
91         
92         for (i=0;i<blob->length;i++) {
93                 mprAddArray(&array, i, mprCreateNumberVar(blob->data[i]));
94         }
95         mpr_Return(eid, array);
96         return 0;
97
98 failed:
99         mpr_Return(eid, mprCreateUndefinedVar());
100         return 0;
101 }
102
103
104 /*
105   compare two data blobs
106 */
107 static int ejs_blobCompare(MprVarHandle eid, int argc, struct MprVar **argv)
108 {
109         DATA_BLOB *blob1, *blob2;
110         bool ret = false;
111
112         if (argc != 2) {
113                 ejsSetErrorMsg(eid, "blobCompare invalid arguments");
114                 return -1;              
115         }
116         
117         blob1 = mprToDataBlob(argv[0]);
118         blob2 = mprToDataBlob(argv[1]);
119
120         if (blob1 == blob2) {
121                 ret = true;
122                 goto done;
123         }
124         if (blob1 == NULL || blob2 == NULL) {
125                 ret = false;
126                 goto done;
127         }
128
129         if (blob1->length != blob2->length) {
130                 ret = false;
131                 goto done;
132         }
133
134         if (memcmp(blob1->data, blob2->data, blob1->length) != 0) {
135                 ret = false;
136                 goto done;
137         }
138         ret = true;
139
140 done:
141         mpr_Return(eid, mprCreateBoolVar(ret));
142         return 0;
143 }
144
145
146 /*
147   convert a blob in winreg format to a mpr variable
148   
149   usage:
150      v = data.regToVar(blob, type);
151 */
152 static int ejs_regToVar(MprVarHandle eid, int argc, struct MprVar **argv)
153 {
154         DATA_BLOB *blob;
155         enum winreg_Type type;
156         struct MprVar v;
157
158         if (argc != 2) {
159                 ejsSetErrorMsg(eid, "regToVar invalid arguments");
160                 return -1;              
161         }
162         
163         blob = mprToDataBlob(argv[0]);
164         type = mprToInt(argv[1]);
165
166         if (blob == NULL) {
167                 ejsSetErrorMsg(eid, "regToVar null data");
168                 return -1;
169         }
170
171         switch (type) {
172         case REG_NONE:
173                 v = mprCreateUndefinedVar();
174                 break;
175
176         case REG_SZ:
177         case REG_EXPAND_SZ: {
178                 char *s;
179                 ssize_t len;
180                 len = convert_string_talloc(mprMemCtx(), lp_iconv_convenience(global_loadparm), CH_UTF16, CH_UNIX, 
181                                             blob->data, blob->length, (void **)&s);
182                 if (len == -1) {
183                         ejsSetErrorMsg(eid, "regToVar invalid REG_SZ string");
184                         return -1;
185                 }
186                 v = mprString(s);
187                 talloc_free(s);
188                 break;
189         }
190
191         case REG_DWORD: {
192                 if (blob->length != 4) {
193                         ejsSetErrorMsg(eid, "regToVar invalid REG_DWORD length %ld", (long)blob->length);
194                         return -1;
195                 }
196                 v = mprCreateNumberVar(IVAL(blob->data, 0));
197                 break;
198         }
199
200         case REG_DWORD_BIG_ENDIAN: {
201                 if (blob->length != 4) {
202                         ejsSetErrorMsg(eid, "regToVar invalid REG_DWORD_BIG_ENDIAN length %ld", (long)blob->length);
203                         return -1;
204                 }
205                 v = mprCreateNumberVar(RIVAL(blob->data, 0));
206                 break;
207         }
208
209         case REG_QWORD: {
210                 if (blob->length != 8) {
211                         ejsSetErrorMsg(eid, "regToVar invalid REG_QWORD length %ld", (long)blob->length);
212                         return -1;
213                 }
214                 v = mprCreateNumberVar(BVAL(blob->data, 0));
215                 break;
216         }
217
218         case REG_MULTI_SZ: {
219                 DATA_BLOB b = *blob;
220                 const char **list = NULL;
221                 while (b.length > 0) {
222                         char *s;
223                         ssize_t len;
224                         size_t slen = utf16_len_n(b.data, b.length);
225                         if (slen == 2 && b.length == 2 && SVAL(b.data, 0) == 0) {
226                                 break;
227                         }
228                         len = convert_string_talloc(mprMemCtx(), lp_iconv_convenience(global_loadparm), CH_UTF16, CH_UNIX, 
229                                                     b.data, slen, (void **)&s);
230                         if (len == -1) {
231                                 ejsSetErrorMsg(eid, "regToVar invalid REG_MULTI_SZ string");
232                                 return -1;
233                         }
234                         list = str_list_add(list, s);
235                         talloc_free(s);
236                         talloc_steal(mprMemCtx(), list);
237                         b.data += slen;
238                         b.length -= slen;
239                 }
240                 v = mprList("REG_MULTI_SZ", list);
241                 talloc_free(list);
242                 break;
243         }
244                 
245
246         case REG_FULL_RESOURCE_DESCRIPTOR:
247         case REG_RESOURCE_LIST:
248         case REG_BINARY:
249         case REG_RESOURCE_REQUIREMENTS_LIST:
250         case REG_LINK:
251                 return ejs_blobToArray(eid, 1, argv);
252
253         default:
254                 ejsSetErrorMsg(eid, "regToVar invalid type %d", type);
255                 return -1;              
256         }
257         
258         mpr_Return(eid, v);
259         return 0;
260 }
261
262 /*
263   initialise datablob ejs subsystem
264 */
265 static int ejs_datablob_init(MprVarHandle eid, int argc, struct MprVar **argv)
266 {
267         struct MprVar *obj = mprInitObject(eid, "datablob", argc, argv);
268
269         mprSetCFunction(obj, "blobFromArray", ejs_blobFromArray);
270         mprSetCFunction(obj, "blobToArray", ejs_blobToArray);
271         mprSetCFunction(obj, "blobCompare", ejs_blobCompare);
272         mprSetCFunction(obj, "regToVar", ejs_regToVar);
273
274         return 0;
275 }
276
277 /*
278   setup C functions that be called from ejs
279 */
280 NTSTATUS smb_setup_ejs_datablob(void)
281 {
282         ejsDefineCFunction(-1, "datablob_init", ejs_datablob_init, NULL, MPR_VAR_SCRIPT_HANDLE);
283         return NT_STATUS_OK;
284 }