Fix sha1 support when no binary dependencies available

Thu, 23 Mar 2023 19:06:04 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Thu, 23 Mar 2023 19:06:04 +0000
changeset 486
6416ea3fff86
parent 485
c9a144591649
child 487
fd5f48a0f122

Fix sha1 support when no binary dependencies available

libs/bit.lua file | annotate | diff | comparison | revisions
squishy file | annotate | diff | comparison | revisions
util/sha1.lua file | annotate | diff | comparison | revisions
--- a/libs/bit.lua	Thu Mar 23 18:56:32 2023 +0000
+++ b/libs/bit.lua	Thu Mar 23 19:06:04 2023 +0000
@@ -12,7 +12,6 @@
 local setmetatable = setmetatable;
 local error = error;
 local tostring = tostring;
-local print = print;
 
 local xor_map = {[0]=0;[1]=1;[2]=2;[3]=3;[4]=4;[5]=5;[6]=6;[7]=7;[8]=8;[9]=9;[10]=10;[11]=11;[12]=12;[13]=13;[14]=14;[15]=15;[16]=1;[17]=0;[18]=3;[19]=2;[20]=5;[21]=4;[22]=7;[23]=6;[24]=9;[25]=8;[26]=11;[27]=10;[28]=13;[29]=12;[30]=15;[31]=14;[32]=2;[33]=3;[34]=0;[35]=1;[36]=6;[37]=7;[38]=4;[39]=5;[40]=10;[41]=11;[42]=8;[43]=9;[44]=14;[45]=15;[46]=12;[47]=13;[48]=3;[49]=2;[50]=1;[51]=0;[52]=7;[53]=6;[54]=5;[55]=4;[56]=11;[57]=10;[58]=9;[59]=8;[60]=15;[61]=14;[62]=13;[63]=12;[64]=4;[65]=5;[66]=6;[67]=7;[68]=0;[69]=1;[70]=2;[71]=3;[72]=12;[73]=13;[74]=14;[75]=15;[76]=8;[77]=9;[78]=10;[79]=11;[80]=5;[81]=4;[82]=7;[83]=6;[84]=1;[85]=0;[86]=3;[87]=2;[88]=13;[89]=12;[90]=15;[91]=14;[92]=9;[93]=8;[94]=11;[95]=10;[96]=6;[97]=7;[98]=4;[99]=5;[100]=2;[101]=3;[102]=0;[103]=1;[104]=14;[105]=15;[106]=12;[107]=13;[108]=10;[109]=11;[110]=8;[111]=9;[112]=7;[113]=6;[114]=5;[115]=4;[116]=3;[117]=2;[118]=1;[119]=0;[120]=15;[121]=14;[122]=13;[123]=12;[124]=11;[125]=10;[126]=9;[127]=8;[128]=8;[129]=9;[130]=10;[131]=11;[132]=12;[133]=13;[134]=14;[135]=15;[136]=0;[137]=1;[138]=2;[139]=3;[140]=4;[141]=5;[142]=6;[143]=7;[144]=9;[145]=8;[146]=11;[147]=10;[148]=13;[149]=12;[150]=15;[151]=14;[152]=1;[153]=0;[154]=3;[155]=2;[156]=5;[157]=4;[158]=7;[159]=6;[160]=10;[161]=11;[162]=8;[163]=9;[164]=14;[165]=15;[166]=12;[167]=13;[168]=2;[169]=3;[170]=0;[171]=1;[172]=6;[173]=7;[174]=4;[175]=5;[176]=11;[177]=10;[178]=9;[179]=8;[180]=15;[181]=14;[182]=13;[183]=12;[184]=3;[185]=2;[186]=1;[187]=0;[188]=7;[189]=6;[190]=5;[191]=4;[192]=12;[193]=13;[194]=14;[195]=15;[196]=8;[197]=9;[198]=10;[199]=11;[200]=4;[201]=5;[202]=6;[203]=7;[204]=0;[205]=1;[206]=2;[207]=3;[208]=13;[209]=12;[210]=15;[211]=14;[212]=9;[213]=8;[214]=11;[215]=10;[216]=5;[217]=4;[218]=7;[219]=6;[220]=1;[221]=0;[222]=3;[223]=2;[224]=14;[225]=15;[226]=12;[227]=13;[228]=10;[229]=11;[230]=8;[231]=9;[232]=6;[233]=7;[234]=4;[235]=5;[236]=2;[237]=3;[238]=0;[239]=1;[240]=15;[241]=14;[242]=13;[243]=12;[244]=11;[245]=10;[246]=9;[247]=8;[248]=7;[249]=6;[250]=5;[251]=4;[252]=3;[253]=2;[254]=1;[255]=0;};
 local or_map = {[0]=0;[1]=1;[2]=2;[3]=3;[4]=4;[5]=5;[6]=6;[7]=7;[8]=8;[9]=9;[10]=10;[11]=11;[12]=12;[13]=13;[14]=14;[15]=15;[16]=1;[17]=1;[18]=3;[19]=3;[20]=5;[21]=5;[22]=7;[23]=7;[24]=9;[25]=9;[26]=11;[27]=11;[28]=13;[29]=13;[30]=15;[31]=15;[32]=2;[33]=3;[34]=2;[35]=3;[36]=6;[37]=7;[38]=6;[39]=7;[40]=10;[41]=11;[42]=10;[43]=11;[44]=14;[45]=15;[46]=14;[47]=15;[48]=3;[49]=3;[50]=3;[51]=3;[52]=7;[53]=7;[54]=7;[55]=7;[56]=11;[57]=11;[58]=11;[59]=11;[60]=15;[61]=15;[62]=15;[63]=15;[64]=4;[65]=5;[66]=6;[67]=7;[68]=4;[69]=5;[70]=6;[71]=7;[72]=12;[73]=13;[74]=14;[75]=15;[76]=12;[77]=13;[78]=14;[79]=15;[80]=5;[81]=5;[82]=7;[83]=7;[84]=5;[85]=5;[86]=7;[87]=7;[88]=13;[89]=13;[90]=15;[91]=15;[92]=13;[93]=13;[94]=15;[95]=15;[96]=6;[97]=7;[98]=6;[99]=7;[100]=6;[101]=7;[102]=6;[103]=7;[104]=14;[105]=15;[106]=14;[107]=15;[108]=14;[109]=15;[110]=14;[111]=15;[112]=7;[113]=7;[114]=7;[115]=7;[116]=7;[117]=7;[118]=7;[119]=7;[120]=15;[121]=15;[122]=15;[123]=15;[124]=15;[125]=15;[126]=15;[127]=15;[128]=8;[129]=9;[130]=10;[131]=11;[132]=12;[133]=13;[134]=14;[135]=15;[136]=8;[137]=9;[138]=10;[139]=11;[140]=12;[141]=13;[142]=14;[143]=15;[144]=9;[145]=9;[146]=11;[147]=11;[148]=13;[149]=13;[150]=15;[151]=15;[152]=9;[153]=9;[154]=11;[155]=11;[156]=13;[157]=13;[158]=15;[159]=15;[160]=10;[161]=11;[162]=10;[163]=11;[164]=14;[165]=15;[166]=14;[167]=15;[168]=10;[169]=11;[170]=10;[171]=11;[172]=14;[173]=15;[174]=14;[175]=15;[176]=11;[177]=11;[178]=11;[179]=11;[180]=15;[181]=15;[182]=15;[183]=15;[184]=11;[185]=11;[186]=11;[187]=11;[188]=15;[189]=15;[190]=15;[191]=15;[192]=12;[193]=13;[194]=14;[195]=15;[196]=12;[197]=13;[198]=14;[199]=15;[200]=12;[201]=13;[202]=14;[203]=15;[204]=12;[205]=13;[206]=14;[207]=15;[208]=13;[209]=13;[210]=15;[211]=15;[212]=13;[213]=13;[214]=15;[215]=15;[216]=13;[217]=13;[218]=15;[219]=15;[220]=13;[221]=13;[222]=15;[223]=15;[224]=14;[225]=15;[226]=14;[227]=15;[228]=14;[229]=15;[230]=14;[231]=15;[232]=14;[233]=15;[234]=14;[235]=15;[236]=14;[237]=15;[238]=14;[239]=15;[240]=15;[241]=15;[242]=15;[243]=15;[244]=15;[245]=15;[246]=15;[247]=15;[248]=15;[249]=15;[250]=15;[251]=15;[252]=15;[253]=15;[254]=15;[255]=15;};
@@ -25,8 +24,6 @@
 local lshift1carry_map = {[0]=0;[1]=0;[2]=0;[3]=0;[4]=0;[5]=0;[6]=0;[7]=0;[8]=1;[9]=1;[10]=1;[11]=1;[12]=1;[13]=1;[14]=1;[15]=1;};
 local arshift1carry_map = {[0]=0;[1]=0;[2]=0;[3]=0;[4]=0;[5]=0;[6]=0;[7]=0;[8]=8;[9]=8;[10]=8;[11]=8;[12]=8;[13]=8;[14]=8;[15]=8;};
 
-module "bit"
-
 local bit_mt = {__tostring = function(t) return ("%x%x%x%x%x%x%x%x"):format(t[1],t[2],t[3],t[4],t[5],t[6],t[7],t[8]); end};
 local function do_bop(a, b, op)
 	return setmetatable({
@@ -53,11 +50,11 @@
 	}, bit_mt);
 end
 
-function bxor(a, b) return do_bop(a, b, xor_map); end
-function bor(a, b) return do_bop(a, b, or_map); end
-function band(a, b) return do_bop(a, b, and_map); end
+local function bxor(a, b) return do_bop(a, b, xor_map); end
+local function bor(a, b) return do_bop(a, b, or_map); end
+local function band(a, b) return do_bop(a, b, and_map); end
 
-function bnot(a) return do_uop(a, not_map); end
+local function bnot(a) return do_uop(a, not_map); end
 local function _rshift1(t)
 	local carry = 0;
 	for i=1,8 do
@@ -66,7 +63,7 @@
 		t[i] = t_i;
 	end
 end
-function rshift(a, i)
+local function rshift(a, i)
 	local t = {a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8]};
 	for n = 1,i do _rshift1(t); end
 	return setmetatable(t, bit_mt);
@@ -79,7 +76,7 @@
 		t[i] = t_i;
 	end
 end
-function arshift(a, i)
+local function arshift(a, i)
 	local t = {a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8]};
 	for n = 1,i do _arshift1(t); end
 	return setmetatable(t, bit_mt);
@@ -92,7 +89,7 @@
 		t[i] = t_i;
 	end
 end
-function lshift(a, i)
+local function lshift(a, i)
 	local t = {a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8]};
 	for n = 1,i do _lshift1(t); end
 	return setmetatable(t, bit_mt);
@@ -131,15 +128,16 @@
 	end;
 end
 
-bxor = wrap2(bxor);
-bor = wrap2(bor);
-band = wrap2(band);
-bnot = wrap1(bnot);
-lshift = wrap1(lshift);
-rshift = wrap1(rshift);
-arshift = wrap1(arshift);
-cast = wrap1(_cast);
+
+return {
+	bits = 32;
 
-bits = 32;
-
-return _M;
+	bxor = wrap2(bxor);
+	bor = wrap2(bor);
+	band = wrap2(band);
+	bnot = wrap1(bnot);
+	lshift = wrap1(lshift);
+	rshift = wrap1(rshift);
+	arshift = wrap1(arshift);
+	cast = wrap1(_cast);
+};
--- a/squishy	Thu Mar 23 18:56:32 2023 +0000
+++ b/squishy	Thu Mar 23 19:06:04 2023 +0000
@@ -3,7 +3,6 @@
 -- Verse-specific versions of libraries
 Module "util.encodings"		"libs/encodings.lua"
 Module "util.hashes"		"libs/hashes.lua"
-Module "util.sha1"		"util/sha1.lua"
 Module "lib.adhoc"              "libs/adhoc.lib.lua"
 Module "util.table" "libs/table.lua"
 
@@ -14,6 +13,9 @@
 	AutoFetchURL(GetOption("prosody").."/?")
 end
 
+Module "util.sha1"		"util/sha1.lua"
+Module "util.bit"               "libs/bit.lua"
+
 Module "util.stanza"		"util/stanza.lua"
 Module "util.timer"		"util/timer.lua"
 Module "util.termcolours"	"util/termcolours.lua"
--- a/util/sha1.lua	Thu Mar 23 18:56:32 2023 +0000
+++ b/util/sha1.lua	Thu Mar 23 19:06:04 2023 +0000
@@ -13,7 +13,7 @@
 local strbyte = string.byte
 local strsub  = string.sub
 local floor   = math.floor
-local bit     = require "bit"
+local bit     = bit32 or require "util.bit"
 local bnot    = bit.bnot
 local band    = bit.band
 local bor     = bit.bor
@@ -138,8 +138,9 @@
 	end
 end
 
-_G.sha1 = {sha1 = sha1};
-return _G.sha1;
+return {
+	sha1 = sha1;
+};
 
 -------------------------------------------------
 -------------------------------------------------

mercurial