2 Unix SMB/CIFS implementation.
4 provide access to data blobs
6 Copyright (C) Andrew Tridgell 2005
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.
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.
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/>.
23 #include "scripting/ejs/smbcalls.h"
24 #include "lib/appweb/ejs/ejs.h"
25 #include "librpc/gen_ndr/winreg.h"
28 create a data blob object from a ejs array of integers
30 static int ejs_blobFromArray(MprVarHandle eid, int argc, struct MprVar **argv)
32 struct MprVar *array, *v;
37 ejsSetErrorMsg(eid, "blobFromArray invalid arguments");
42 v = mprGetProperty(array, "length", NULL);
48 blob = data_blob_talloc(mprMemCtx(), NULL, length);
49 if (length != 0 && blob.data == NULL) {
53 for (i=0;i<length;i++) {
56 mprItoa(i, idx, sizeof(idx));
57 vs = mprGetProperty(array, idx, NULL);
61 blob.data[i] = mprVarToNumber(vs);
64 mpr_Return(eid, mprDataBlob(blob));
68 mpr_Return(eid, mprCreateUndefinedVar());
73 create a ejs array of integers from a data blob
75 static int ejs_blobToArray(MprVarHandle eid, int argc, struct MprVar **argv)
82 ejsSetErrorMsg(eid, "blobToArray invalid arguments");
85 blob = mprToDataBlob(argv[0]);
90 array = mprArray("array");
92 for (i=0;i<blob->length;i++) {
93 mprAddArray(&array, i, mprCreateNumberVar(blob->data[i]));
95 mpr_Return(eid, array);
99 mpr_Return(eid, mprCreateUndefinedVar());
105 compare two data blobs
107 static int ejs_blobCompare(MprVarHandle eid, int argc, struct MprVar **argv)
109 DATA_BLOB *blob1, *blob2;
113 ejsSetErrorMsg(eid, "blobCompare invalid arguments");
117 blob1 = mprToDataBlob(argv[0]);
118 blob2 = mprToDataBlob(argv[1]);
120 if (blob1 == blob2) {
124 if (blob1 == NULL || blob2 == NULL) {
129 if (blob1->length != blob2->length) {
134 if (memcmp(blob1->data, blob2->data, blob1->length) != 0) {
141 mpr_Return(eid, mprCreateBoolVar(ret));
147 convert a blob in winreg format to a mpr variable
150 v = data.regToVar(blob, type);
152 static int ejs_regToVar(MprVarHandle eid, int argc, struct MprVar **argv)
155 enum winreg_Type type;
159 ejsSetErrorMsg(eid, "regToVar invalid arguments");
163 blob = mprToDataBlob(argv[0]);
164 type = mprToInt(argv[1]);
167 ejsSetErrorMsg(eid, "regToVar null data");
173 v = mprCreateUndefinedVar();
177 case REG_EXPAND_SZ: {
180 len = convert_string_talloc(mprMemCtx(), lp_iconv_convenience(mprLpCtx()), CH_UTF16, CH_UNIX,
181 blob->data, blob->length, (void **)&s);
183 ejsSetErrorMsg(eid, "regToVar invalid REG_SZ string");
192 if (blob->length != 4) {
193 ejsSetErrorMsg(eid, "regToVar invalid REG_DWORD length %ld", (long)blob->length);
196 v = mprCreateNumberVar(IVAL(blob->data, 0));
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);
205 v = mprCreateNumberVar(RIVAL(blob->data, 0));
210 if (blob->length != 8) {
211 ejsSetErrorMsg(eid, "regToVar invalid REG_QWORD length %ld", (long)blob->length);
214 v = mprCreateNumberVar(BVAL(blob->data, 0));
220 const char **list = NULL;
221 while (b.length > 0) {
224 size_t slen = utf16_len_n(b.data, b.length);
225 if (slen == 2 && b.length == 2 && SVAL(b.data, 0) == 0) {
228 len = convert_string_talloc(mprMemCtx(), lp_iconv_convenience(mprLpCtx()), CH_UTF16, CH_UNIX,
229 b.data, slen, (void **)&s);
231 ejsSetErrorMsg(eid, "regToVar invalid REG_MULTI_SZ string");
234 list = str_list_add(list, s);
236 talloc_steal(mprMemCtx(), list);
240 v = mprList("REG_MULTI_SZ", list);
246 case REG_FULL_RESOURCE_DESCRIPTOR:
247 case REG_RESOURCE_LIST:
249 case REG_RESOURCE_REQUIREMENTS_LIST:
251 return ejs_blobToArray(eid, 1, argv);
254 ejsSetErrorMsg(eid, "regToVar invalid type %d", type);
263 initialise datablob ejs subsystem
265 static int ejs_datablob_init(MprVarHandle eid, int argc, struct MprVar **argv)
267 struct MprVar *obj = mprInitObject(eid, "datablob", argc, argv);
269 mprSetCFunction(obj, "blobFromArray", ejs_blobFromArray);
270 mprSetCFunction(obj, "blobToArray", ejs_blobToArray);
271 mprSetCFunction(obj, "blobCompare", ejs_blobCompare);
272 mprSetCFunction(obj, "regToVar", ejs_regToVar);
278 setup C functions that be called from ejs
280 NTSTATUS smb_setup_ejs_datablob(void)
282 ejsDefineCFunction(-1, "datablob_init", ejs_datablob_init, NULL, MPR_VAR_SCRIPT_HANDLE);