Skip to content

Instantly share code, notes, and snippets.

@giuseppeM99
Created July 3, 2020 00:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save giuseppeM99/3cc99ace023eded6c7ba7ff0b3b7e405 to your computer and use it in GitHub Desktop.
Save giuseppeM99/3cc99ace023eded6c7ba7ff0b3b7e405 to your computer and use it in GitHub Desktop.
Lua telegram unpack util
--[[
BSD 2-Clause License
Copyright (c) 2020, Giuseppe Marino
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
--]]
local b = require 'base64'
local unpack = string.unpack
local byte = string.byte
local char = string.char
local WEB_LOCATION_FLAG = 1 << 24
local FILE_REFERENCE_FLAG = 1 << 25
local THUMBNAIL = 0x00;
local PROFILE_PHOTO = 0x01;
local PHOTO = 0x02;
local VOICE = 0x03;
local VIDEO = 0x04;
local DOCUMENT = 0x05;
local ENCRYPTED = 0x06;
local TEMP = 0x07;
local STICKER = 0x08;
local AUDIO = 0x09;
local GIF = 0x0A;
local ENCRYPTED_THUMBNAIL = 0x0B;
local WALLPAPER = 0x0C;
local VIDEO_NOTE = 0x0D;
local SECURE_RAW = 0x0F;
local SECURE = 0x10;
local BACKGROUND = 0x11;
local SIZE = 0x12;
local function rleDecode(input)
local new = '';
local last = '';
local null = string.char(0);
local len = #input
for i = 1, len do
local cur = input:sub(i, i)
if (last == null) then
new = new .. string.rep(last, string.byte(cur));
last = '';
else
new = new ..last;
last = cur;
end
end
return new .. last;
end
local function posmod(a, b)
local resto = a % b;
return resto < 0 and (resto + math.abs(b)) or resto;
end
local function decodeFileID(file_id)
local file_info = rleDecode(b.from_b64(file_id))
local file_type = unpack('<b', file_info:sub(1, 1))
local file_flags = unpack('<i', char(0) .. file_info:sub(2, 4))
local version = byte(file_info:sub(-1))
local subVersion = version == 4 and byte(file_info:sub(-2, -1)) or 0
local dc_id = unpack('<i', file_info:sub(5,8))
file_info = file_info:sub(9,-1)
local file_reference = nil
if not ((file_flags & FILE_REFERENCE_FLAG) == 0) then
local frlen = byte(file_info:sub(1,1))
local pad = 0
file_info = char(0) .. file_info:sub(2, -1)
if frlen == 254 then
frlen = unpack('<i', file_info)
pad = math.abs(-frlen % 4)
else
pad = math.abs(frlen % -4)
end
file_reference = file_info:sub(1, frlen)
file_info = file_info:sub(frlen+pad+1, -1)
end
local infos = {
type = file_type,
dc = dc_id,
version = version..'.'..subVersion,
flags = file_flags,
}
if file_reference then
infos.file_reference = b.to_b64(file_reference)
end
if file_type == TYPE_THUMBNAIL or file_type == TYPE_PROFILE_PHOTO or file_type == TYPE_PHOTO then
local id, access_hash, volume_id, secret, robastrana, local_id = unpack('<llllii', file_info)
infos.id = id
infos.access_hash = access_hash
infos.volume_id = volume_id
infos.secret = secret
infos.robastrana = robastrana
infos.local_id = local_id
else
local id, access_hash = unpack('<ll', file_info)
infos.id = id
infos.access_hash = access_hash
end
if file_type == TYPE_STICKER then
infos.creator_id = infos.id >> 32
end
return infos
end
local function decodeInlineMessageId(inline_message_id)
local decoded = b.from_b64url(inline_message_id)
local dc_id, message_id, chat_id, access_hash = unpack('<iiil', decoded)
return {
dc = dc_id,
message_id = message_id,
chat_id = chat_id,
access_hash = access_hash
}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment