Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions puremagic.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
-- Licensed under the MIT license.


function basename(path)
local function basename(path)
local basename_match = path:match('[/\\]([^/\\]+)$')
if basename_match then
return basename_match, nil
Expand All @@ -13,7 +13,7 @@ function basename(path)
end


function extension(path)
local function extension(path)
path = path:lower()
local tar_match = path:match('%.(tar%.[^.]+)$')
if tar_match then
Expand All @@ -32,7 +32,7 @@ function extension(path)
end


function in_table(value, list)
local function in_table(value, list)
for i=1, #list do
if list[i] == value then
return true
Expand All @@ -42,7 +42,7 @@ function in_table(value, list)
end


function string_to_bit_table(chars)
local function string_to_bit_table(chars)
local output = {}
for char in chars:gmatch('.') do
local num = string.byte(char)
Expand All @@ -59,7 +59,7 @@ function string_to_bit_table(chars)
end


function bit_table_to_string(bits)
local function bit_table_to_string(bits)
local output = {}
for i = 1, #bits do
local num = tonumber(table.concat(bits[i]), 2)
Expand All @@ -69,7 +69,7 @@ function bit_table_to_string(bits)
end


function bitwise_and(a, b)
local function bitwise_and(a, b)
local a_bytes = string_to_bit_table(a)
local b_bytes = string_to_bit_table(b)

Expand All @@ -91,7 +91,7 @@ end


-- Unpack a little endian byte string into an integer
function unpack_le(chars)
local function unpack_le(chars)
local bit_table = string_to_bit_table(chars)
-- Merge the bits into a string of 1s and 0s
local result = {}
Expand All @@ -103,7 +103,7 @@ end


-- Unpack a big endian byte string into an integer
function unpack_be(chars)
local function unpack_be(chars)
local bit_table = string_to_bit_table(chars)
-- Merge the bits into a string of 1s and 0s
for i=1, #bit_table do
Expand All @@ -115,7 +115,7 @@ end

-- Takes the first 4-8k of an EBML file and identifies if it is matroska or webm
-- and it it contains just video or just audio.
function ebml_parse(content)
local function ebml_parse(content)
local position = 1
local length = #content

Expand Down Expand Up @@ -198,7 +198,7 @@ end
-- Parses a section of an EBML document, returning the EBML ID at the beginning,
-- plus the value as a table with child EBML IDs as keys and the number of
-- bytes from the content that contained the ID and value
function ebml_parse_section(content)
local function ebml_parse_section(content)
local ebml_id, element_length, used_bytes = ebml_id_and_length(content)

-- Don't parse the segment since it is the whole file!
Expand Down Expand Up @@ -262,7 +262,7 @@ end

-- Should accept 12+ bytes, will return the ebml id, the data length and the
-- number of bytes that were used to hold those values.
function ebml_id_and_length(chars)
local function ebml_id_and_length(chars)
-- The ID is encoded the same way as the length, however, we don't want
-- to remove the length bits from the ID value or intepret it as an
-- unsigned int since all of the documentation online references the IDs in
Expand All @@ -279,7 +279,7 @@ end

-- Should accept 8+ bytes, will return the data length plus the number of bytes
-- that were used to hold the data length.
function ebml_length(chars)
local function ebml_length(chars)
-- We substring chars to ensure we don't build a huge table we don't need
local bit_tables = string_to_bit_table(chars:sub(1, 8))

Expand All @@ -303,7 +303,7 @@ function ebml_length(chars)
end


function binary_tests(content, ext)
local function binary_tests(content, ext)
local length = #content
local _1_8 = content:sub(1, 8)
local _1_7 = content:sub(1, 7)
Expand Down Expand Up @@ -600,7 +600,7 @@ function binary_tests(content, ext)
end


function text_tests(content)
local function text_tests(content)
local lower_content = content:lower()

if content:find('^%%!PS-Adobe') then
Expand Down Expand Up @@ -677,7 +677,7 @@ local ext_map = {
xml = 'application/xml'
}

function ext_tests(ext)
local function ext_tests(ext)
local mimetype = ext_map[ext]
if mimetype then
return mimetype
Expand Down