From 9ce69b6bc34655e437feccd89354730c91e5677b Mon Sep 17 00:00:00 2001 From: Pixel Date: Sat, 19 Dec 2009 17:44:13 -0800 Subject: Adding a first stub for binary ops, and fixing hexviewer's ability to destroy its input when going too far in the file. --- dalos-binaryops.lua | 127 ++++++++++++++++++++++++++++++++++++++++++++++++++++ dalos-hexview.lua | 2 + dalos.lua | 1 + 3 files changed, 130 insertions(+) create mode 100644 dalos-binaryops.lua diff --git a/dalos-binaryops.lua b/dalos-binaryops.lua new file mode 100644 index 0000000..db6a39e --- /dev/null +++ b/dalos-binaryops.lua @@ -0,0 +1,127 @@ +-- bit.tobit bit.tohex bit.bnot bit.band bit.bor bit.bxor +-- bit.lshift bit.rshift bit.arshift bit.rol bit.ror bit.bswap + +dalosp.binaryops = { + operations = { + XOR = 1, + AND = 2, + OR = 3, + ADD = 4, + SUB = 5, + }, + + opnames = { + "xor", + "and", + "or", + "add", + "sub", + }, + + configure = function (self) +-- local accept = iup.GetParam + end, + + input_change = function (self, ind) + local h1 = self:get_linked_input(1) + local h2 = self:get_linked_input(1) + local op = self.extra.op or dalosp.binaryops.XOR + if h1 and h2 then + local obj = { + h1 = h1, + h2 = h2, + op = op, + maximize = self.extra.maximize, + offset = 0, + size = self.extra.maximize and math.max(h1:getsize(), h2:getsize()) or math.min(h1:getsize(), h2:getsize()), + canread = function (self) return true end, + canwrite = function (self) return false end, + canseek = function (self) return true end, + canwatch = function (self) return false end, + getname = function (self) return end, + tell = function (self) return self.offset end, + getsize = function (self) return self.size end, + getmodif = function (self) return 0 end, + flush = function (self) return true end, + seek = function (self, offset, wheel) + if wheel == SEEK_SET then + self.offset = offset + elseif wheel == SEEK_CUR then + self.offset = self.offset + offset + elseif wheel == SEEK_END then + self.offset = self.size + offset + else + error "Unknown wheel" + end + if self.offset < 0 then self.offset = 0 end + if self.offset >= self.size then self.offset = self.size end + return self.offset + end, + read = function (self, dummy, count) + count = math.min(count, self.size - self.offset) + + if count == 0 then + if self.got_eof then self.lh:close() end + self.got_eof = true + return 0 + end + + self.got_eof = false + + self.h1:seek(self.offset) + self.h2:seek(self.offset) + +---- maximizing work to do here... + local r1, t1 = self.h1:read(count) + local r2, t2 = self.h2:read(count) + local r = math.min(r1, r2) + self.offset = self.offset + r + if r == 0 then self.got_eof = true return 0 end + local t = {} + local op + if self.op == dalosp.binaryops.operations.XOR then + op = bit.bxor + elseif self.op == dalosp.binaryops.operations.AND then + op = bit.band + elseif self.op == dalosp.binaryops.operations.OR then + op = bit.bor + elseif self.op == dalosp.binaryops.operations.ADD then + op = function(a, b) return a + b end + elseif self.op == dalosp.binaryops.operations.SUB then + op = function(a, b) return a - b end + end + for i = 0, r - 1 do + t[i] = bit.band(op(t1[i], t2[i]), 255) + end + return r, t + end, + } + local newh = HandleLua(obj) + obj.lh = newh + self:set_houtput(newh) + else + self:set_houtput(nil) + end + end, + + draw = function (self, cv, x, y, w, h) + dalosp.object.default_draw(self, cv, x, y, w, h) + -- add status display + end, + + create = function (d, tab) + tab.ninputs = 2 + tab.noutputs = 1 + tab.otype = dalos.objtype.LUA_FILTER + tab.configure = dalosp.binaryops.configure + tab.input_change = dalosp.binaryops.input_change + tab.default_name = "Binary Ops" + tab.draw = dalosp.binaryops.draw + local extra = { } + + local obj = dalos.object(d, tab, extra) + end, +} + +dalos.binaryops = dalosp.binaryops.create +dalos:register_obj("Binary Ops", dalos.binaryops) \ No newline at end of file diff --git a/dalos-hexview.lua b/dalos-hexview.lua index 7e46ed6..b085f12 100644 --- a/dalos-hexview.lua +++ b/dalos-hexview.lua @@ -67,6 +67,8 @@ dalosp.hexview = { else error "Unknown wheel" end + if self.offset < 0 then self.offset = 0 end + if self.offset >= self.size then self.offset = self.size end self.h:seek(self.offset + self.origin) return self.offset end, diff --git a/dalos.lua b/dalos.lua index fe94973..d5dfe85 100644 --- a/dalos.lua +++ b/dalos.lua @@ -663,6 +663,7 @@ dalos.objtype = { ---------------- load "dalos-hexview.lua" +load "dalos-binaryops.lua" ---------------- -- cgit v1.2.3