|
| 1 | +--[[ |
| 2 | +$module enigmaxml |
| 3 | +
|
| 4 | +EnigmaXML is the underlying file format of a Finale `.musx` file. It is undocumented |
| 5 | +by MakeMusic and must be extracted from the `.musx` file. There is an effort to document |
| 6 | +it underway at the [EnigmaXML Documentation](https://github.com/Project-Attacca/enigmaxml-documentation) |
| 7 | +repository. |
| 8 | +]] -- |
| 9 | +local enigmaxml = {} |
| 10 | + |
| 11 | +local utils = require("library.utils") |
| 12 | +local client = require("library.client") |
| 13 | +local ziputils = require("library.ziputils") |
| 14 | + |
| 15 | +-- symmetrical encryption/decryption function for EnigmaXML |
| 16 | +local function crypt_enigmaxml_buffer(buffer) |
| 17 | + -- do not use <const> because this library must be loadable by Lua 5.2 (JW Lua) |
| 18 | + local INITIAL_STATE = 0x28006D45 -- this value was determined empirically |
| 19 | + local state = INITIAL_STATE |
| 20 | + local result = {} |
| 21 | + |
| 22 | + for i = 1, #buffer do |
| 23 | + -- BSD rand() |
| 24 | + if (i - 1) % 0x20000 == 0 then |
| 25 | + state = INITIAL_STATE |
| 26 | + end |
| 27 | + state = (state * 0x41c64e6d + 0x3039) & 0xFFFFFFFF -- Simulate 32-bit overflow |
| 28 | + local upper = state >> 16 |
| 29 | + local c = upper + math.floor(upper / 255) |
| 30 | + |
| 31 | + local byte = string.byte(buffer, i) |
| 32 | + byte = byte ~ (c & 0xFF) -- XOR operation on the byte |
| 33 | + |
| 34 | + table.insert(result, string.char(byte)) |
| 35 | + end |
| 36 | + |
| 37 | + return table.concat(result) |
| 38 | +end |
| 39 | + |
| 40 | +--[[ |
| 41 | +% extract_enigmaxml |
| 42 | +
|
| 43 | +This function extracts the EnigmaXML buffer from a `.musx` file. Note that it does not work with Finale's |
| 44 | +older `.mus` format. |
| 45 | +
|
| 46 | +@ filepath (string) utf8-encoded file path to a `.musx` file. |
| 47 | +: (string) buffer of EnigmaXml data extracted from the `.musx`. (The xml declaration specifies the encoding, but expect it to be utf8.) |
| 48 | +]] |
| 49 | +function enigmaxml.extract_enigmaxml(filepath) |
| 50 | + local not_supported_message |
| 51 | + if finenv.TrustedMode == finenv.TrustedModeType.UNTRUSTED then |
| 52 | + not_supported_message = "enigmaxml.extract_enigmaxml must run in Trusted mode." |
| 53 | + elseif not finaleplugin.ExecuteExternalCode then |
| 54 | + not_supported_message = "enigmaxml.extract_enigmaxml must have finaleplugin.ExecuteExternalCode set to true." |
| 55 | + end |
| 56 | + if not_supported_message then |
| 57 | + error(not_supported_message, 2) |
| 58 | + end |
| 59 | + local _, _, extension = utils.split_file_path(filepath) |
| 60 | + if extension ~= ".musx" then |
| 61 | + error(filepath .. " is not a .musx file.", 2) |
| 62 | + end |
| 63 | + |
| 64 | + -- Steps to extract: |
| 65 | + -- Unzip the `.musx` (which is a `.zip` archive in disguise) |
| 66 | + -- Run the `score.dat` file through `crypt_enigmaxml_buffer` to get a gzip archive of the EnigmaXML file. |
| 67 | + -- Gunzip the extracted EnigmaXML gzip archive into a string and return it. |
| 68 | + |
| 69 | + local os_filepath = client.encode_with_client_codepage(filepath) |
| 70 | + local output_dir, zipcommand = ziputils.calc_temp_output_path(os_filepath) |
| 71 | + if not client.execute(zipcommand) then |
| 72 | + error(zipcommand .. " failed") |
| 73 | + end |
| 74 | + |
| 75 | + -- do not use <close> because this library must be loadable by Lua 5.2 (JW Lua) |
| 76 | + local file = io.open(output_dir .. "/score.dat", "rb") |
| 77 | + if not file then |
| 78 | + error("unable to read " .. output_dir .. "/score.dat") |
| 79 | + end |
| 80 | + local buffer = file:read("*all") |
| 81 | + file:close() |
| 82 | + |
| 83 | + local delcommand = ziputils.calc_rmdir_command(output_dir) |
| 84 | + client.execute(delcommand) |
| 85 | + |
| 86 | + buffer = crypt_enigmaxml_buffer(buffer) |
| 87 | + if ziputils.calc_is_gzip(buffer) then |
| 88 | + local gzip_path = ziputils.calc_temp_output_path() |
| 89 | + local gzip_file = io.open(gzip_path, "wb") |
| 90 | + if not gzip_file then |
| 91 | + error("unable to create " .. gzip_file) |
| 92 | + end |
| 93 | + gzip_file:write(buffer) |
| 94 | + gzip_file:close() |
| 95 | + local gunzip_command = ziputils.calc_gunzip_command(gzip_path) |
| 96 | + buffer = client.execute(gunzip_command) |
| 97 | + client.execute(ziputils.calc_delete_file_command(gzip_path)) |
| 98 | + if not buffer or buffer == "" then |
| 99 | + error(gunzip_command .. " failed") |
| 100 | + end |
| 101 | + end |
| 102 | + |
| 103 | + return buffer |
| 104 | +end |
| 105 | + |
| 106 | +return enigmaxml |
0 commit comments