#include #include #include #include "mm_jsapi.h" /** * convert URI to file path and return it. * caller must free it. */ static unsigned short *uri2path(const unsigned short *uri) { size_t len; unsigned short *path; size_t i; if (_tcsncmp(uri, TEXT("file:///"), 8) != 0) return NULL; len = _tcslen(uri); path = malloc(len * sizeof(TCHAR)); _stprintf_s(path, len, TEXT("\\\\?\\%s"), uri + 8); for (i = 0; i < len; ++i) { if (path[i] == '|') { path[i] = ':'; } else if (path[i] == '/') { path[i] = '\\'; } } return path; } /** * copy */ static JSBool copy_inner(const unsigned short *fileURI, const unsigned short *copyURI) { unsigned short *filepath, *copypath; JSBool rv; rv = JS_FALSE; filepath = uri2path(fileURI); copypath = uri2path(copyURI); if (filepath && copypath) { if (CopyFile(filepath, copypath, FALSE)) { rv = JS_TRUE; } } free(filepath); free(copypath); return rv; } /** * decode encoded string using decode URI js function and return it. * caller must free return value. */ static unsigned short *decodeURI(JSContext *cx, JSObject *obj, unsigned short *encoded) { size_t js_len; unsigned short *js, *decoded; jsval decoded_jsval; unsigned int len; decoded = NULL; // size in words. js_len = _tcslen(encoded) + 100; js = malloc(js_len * sizeof(TCHAR)); _stprintf_s(js, js_len, TEXT("decodeURI(\"%s\")"), encoded); if (JS_ExecuteScript(cx, obj, js, 0, &decoded_jsval) != JS_FALSE) { decoded = JS_ValueToString(cx, decoded_jsval, &len); } free(js); return decoded; } /** * return fileURI argument. * caller must free return value. */ static unsigned short *get_fileURI(JSContext *cx, JSObject *obj, jsval *argv) { unsigned int len; unsigned short *uri, *encoded_uri; uri = NULL; encoded_uri = JS_ValueToString(cx, argv[0], &len); if (encoded_uri) { uri = decodeURI(cx, obj, encoded_uri); } return uri; } /** * return copyURI argument. * caller must free return value. */ static unsigned short *get_copyURI(JSContext *cx, JSObject *obj, jsval *argv) { unsigned int len; unsigned short *uri, *encoded_uri; uri = NULL; encoded_uri = JS_ValueToString(cx, argv[1], &len); if (encoded_uri) { uri = decodeURI(cx, obj, encoded_uri); } return uri; } /** * Copy file. * TrueFLfile.copy(fileURI, copyURI) */ JSBool copy(JSContext *cx, JSObject *obj, unsigned int argc, jsval *argv, jsval *rval) { unsigned short *src, *dest; if (argc != 2) return JS_FALSE; src = get_fileURI(cx, obj, argv); dest = get_copyURI(cx, obj, argv); if (src && dest) { *rval = JS_BooleanToValue(copy_inner(src, dest)); } else { *rval = JS_BooleanToValue(JS_FALSE); } return JS_TRUE; } // MM_STATE is a macro that expands to some definitions that are // needed in order interact with Dreamweaver. This macro must be // defined exactly once in your library MM_STATE // Flash calls MM_Init when your library is loaded void MM_Init() { // file copy function JS_DefineFunction(_T("copy"), copy, 2); }