|
| 1 | +local C14n = {} |
| 2 | + |
| 3 | +local libxml2 = require("xmlua.libxml2") |
| 4 | +local ffi = require("ffi") |
| 5 | + |
| 6 | + |
| 7 | + |
| 8 | +local C14N_MODES = { |
| 9 | + C14N_1_0 = ffi.C.XML_C14N_1_0, -- Original C14N 1.0 spec |
| 10 | + C14N_EXCLUSIVE_1_0 = ffi.C.XML_C14N_EXCLUSIVE_1_0, -- Exclusive C14N 1.0 spec |
| 11 | + C14N_1_1 = ffi.C.XML_C14N_1_1, -- C14N 1.1 spec |
| 12 | +} |
| 13 | + |
| 14 | + |
| 15 | + |
| 16 | +local C14N_MODES_LOOKUP = {} -- lookup by name or number, returns the number |
| 17 | +for name, number in pairs(C14N_MODES) do |
| 18 | + C14N_MODES_LOOKUP[name] = number |
| 19 | + C14N_MODES_LOOKUP[number] = number |
| 20 | +end |
| 21 | + |
| 22 | + |
| 23 | + |
| 24 | +-- list can be a string (space separated), an array of strings, or nil |
| 25 | +local function getNamespacePrefixArray(list) |
| 26 | + local list = list or {} |
| 27 | + |
| 28 | + if type(list) == "string" then |
| 29 | + -- list is a string, assume it is the space separated PrefixList attribute, split it |
| 30 | + local list_str = list |
| 31 | + list = {} |
| 32 | + list_str:gsub("([^%s]+)", function(cap) list[#list+1] = cap end) |
| 33 | + end |
| 34 | + |
| 35 | + if #list == 0 then |
| 36 | + return nil |
| 37 | + end |
| 38 | + |
| 39 | + local result = ffi.new('xmlChar*[?]', #list+1) |
| 40 | + local refs = {} |
| 41 | + for i, prefix in ipairs(list) do |
| 42 | + local c_ns = ffi.new("unsigned char[?]", #prefix+1, prefix) |
| 43 | + ffi.copy(c_ns, prefix) |
| 44 | + result[i-1] = c_ns |
| 45 | + refs[i] = c_ns -- hold on to refs to prevent GC while in use |
| 46 | + end |
| 47 | + result[#list] = nil |
| 48 | + |
| 49 | + return ffi.gc(result, function(ptr) |
| 50 | + refs = nil -- release references, so they can be GC'ed |
| 51 | + end) |
| 52 | +end |
| 53 | + |
| 54 | + |
| 55 | + |
| 56 | +local function getNodesList(nodes) |
| 57 | + if (not nodes) or #nodes == 0 then |
| 58 | + return nil |
| 59 | + end |
| 60 | + |
| 61 | + local nodeTab = ffi.new("xmlNodePtr[?]", #nodes) |
| 62 | + for i = 1, #nodes do |
| 63 | + nodeTab[i - 1] = nodes[i] -- FFI side is 0 indexed |
| 64 | + end |
| 65 | + |
| 66 | + local set = ffi.new("xmlNodeSet") |
| 67 | + set.nodeNr = #nodes |
| 68 | + set.nodeMax = #nodes |
| 69 | + set.nodeTab = nodeTab |
| 70 | + |
| 71 | + return set |
| 72 | +end |
| 73 | + |
| 74 | + |
| 75 | + |
| 76 | +--- Canonicalise an xmlDocument or set of elements. |
| 77 | +-- @param self xmlDoc from which to canonicalize elements |
| 78 | +-- @param nodes list of nodes to include when canonicalizing, if 'nil' entire doc will be canonicalized |
| 79 | +-- @param mode any of C14N_1_0, C14N_EXCLUSIVE_1_0 (default), C14N_1_1 |
| 80 | +-- @param inclusive_ns_prefixes array, or space-separated string, of namespace prefixes to include |
| 81 | +-- @param with_comments if thruthy, comments will be included (default: false) |
| 82 | +-- @return string containing canonicalized xml |
| 83 | +function C14n:c14n(nodes, mode, inclusive_ns_prefixes, with_comments) |
| 84 | + if mode == nil then -- default is exclusive 1.0 |
| 85 | + mode = "C14N_EXCLUSIVE_1_0" |
| 86 | + end |
| 87 | + with_comments = with_comments and 1 or 0 -- default = not including comments |
| 88 | + |
| 89 | + mode = assert(C14N_MODES_LOOKUP[mode], "mode must be a valid C14N mode constant") |
| 90 | + local prefixes = getNamespacePrefixArray(inclusive_ns_prefixes) |
| 91 | + local nodeSet = getNodesList(nodes) |
| 92 | + local buffer = libxml2.xmlBufferCreate() |
| 93 | + local output_buffer = libxml2.xmlOutputBufferCreate(buffer) |
| 94 | + |
| 95 | + local success = libxml2.xmlC14NDocSaveTo(self.document, nodeSet, mode, |
| 96 | + prefixes, with_comments, output_buffer) |
| 97 | + |
| 98 | + if success < 0 then |
| 99 | + return nil, "failed to generate C14N string" |
| 100 | + end |
| 101 | + return libxml2.xmlBufferGetContent(buffer) |
| 102 | +end |
| 103 | + |
| 104 | + |
| 105 | +return C14n |
0 commit comments