Skip to content

Commit

Permalink
Add string compression functions (#3020)
Browse files Browse the repository at this point in the history
* Add compression functions

* Add throw for invalid decompression

* Add size limit
  • Loading branch information
Denneisk authored Apr 5, 2024
1 parent b172171 commit bc614de
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
20 changes: 20 additions & 0 deletions lua/entities/gmod_wire_expression2/core/string.lua
Original file line number Diff line number Diff line change
Expand Up @@ -500,3 +500,23 @@ e2function number string:unicodeLength(number startPos, number endPos)
end
return -1
end

--[[******************************************************************************]]--
__e2setcost(10)

local compress = util.Compress
local decompress = util.Decompress

e2function string compress(string plaintext)
local len = #plaintext
if len > 32768 then return self:throw("Input string is too long!", "") end
self.prf = self.prf + len * 0.1
return compress(plaintext)
end

e2function string decompress(string compressed)
local len = #compressed
if len > 32768 then return self:throw("Input string is too long!", "") end
self.prf = self.prf + len * 0.5
return decompress(compressed) or self:throw("Invalid input for decompression!", "")
end
2 changes: 2 additions & 0 deletions lua/wire/client/e2descriptions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@ E2Helper.Descriptions["matchFirst(s:s)"] = "runs string.match(S, S2) and returns
E2Helper.Descriptions["matchFirst(s:sn)"] = "runs string.match(S, S2, N) and returns the first match or an empty string if the match failed"
E2Helper.Descriptions["gmatch(s:s)"] = "runs string.gmatch(S, S2) and returns the captures in arrays in a table"
E2Helper.Descriptions["gmatch(s:sn)"] = "runs string.gmatch(S, S2, N) and returns the captures in arrays in a table"
E2Helper.Descriptions["compress(s)"] = "Compresses the input string using LZMA compression. See decompress(string)"
E2Helper.Descriptions["decompress(s)"] = "Decompresses an LZMA-compressed string. See compress(string)"

-- Entity/Player
E2Helper.Descriptions["entity(n)"] = "Gets the entity associated with the id"
Expand Down

0 comments on commit bc614de

Please sign in to comment.