Skip to content

Commit

Permalink
Further reduced indirection levels in y_inline_bcrypt.
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Cole committed Apr 24, 2024
1 parent a4ac714 commit d98391a
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 64 deletions.
87 changes: 41 additions & 46 deletions YSI_Extra/y_bcrypt/y_bcrypt_entry.inc
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ public _YSI_BCryptHashDone(hashid, arg2, arg3)
// V1.
new
prop[16];
// Secret internal hash ID.
_YSI_gLastHash = arg2,
format(prop, sizeof (prop), YSI_gscProp1, hashid),
extra = GetSVarInt(prop),
Expand Down Expand Up @@ -207,59 +208,53 @@ stock bool:BCrypt_Check(extra, const callback[], const password[], const hash[])
}
}

stock _BCrypt_Hash(extra, ptr, const password[], cost)
stock BCrypt_Hash(extra, const callback[], const password[], cost = 12)
{
++_YSI_gHashID;
switch (BCrypt_GetPlugin())
{
case BCRYPT_PLUGIN_VERSION_LASSIR:
{
return _YSI_BCryptHashA(password, cost, YSI_gscBCryptHashDone, YSI_gcIII, _YSI_gHashID, ptr, extra) ? _YSI_gHashID : 0;
}
case BCRYPT_PLUGIN_VERSION_SREYAS_1:
new ptr;
if (AMX_GetPublicPointer(0, ptr, callback, true))
{
if (_YSI_BCryptHashB(_YSI_gHashID, YSI_gscBCryptHashDone, password, cost))
switch (BCrypt_GetPlugin())
{
new
prop[16];
format(prop, sizeof (prop), YSI_gscProp1, _YSI_gHashID),
SetSVarInt(prop, extra),
format(prop, sizeof (prop), YSI_gscProp2, _YSI_gHashID),
SetSVarInt(prop, ptr);
return _YSI_gHashID;
case BCRYPT_PLUGIN_VERSION_LASSIR:
{
return _YSI_BCryptHashA(password, cost, YSI_gscBCryptHashDone, YSI_gcIII, _YSI_gHashID, ptr, extra) ? _YSI_gHashID : 0;
}
}
case BCRYPT_PLUGIN_VERSION_SREYAS_2:
{
if (_YSI_BCryptHashE(_YSI_gHashID, YSI_gscBCryptHashDone, password, cost, YSI_gcII, ptr, extra))
case BCRYPT_PLUGIN_VERSION_SREYAS_1:
{
new
prop[16];
format(prop, sizeof (prop), YSI_gscProp1, _YSI_gHashID),
SetSVarInt(prop, extra),
format(prop, sizeof (prop), YSI_gscProp2, _YSI_gHashID),
SetSVarInt(prop, ptr);
return _YSI_gHashID;
if (_YSI_BCryptHashB(_YSI_gHashID, YSI_gscBCryptHashDone, password, cost))
{
new
prop[16];
format(prop, sizeof (prop), YSI_gscProp1, _YSI_gHashID),
SetSVarInt(prop, extra),
format(prop, sizeof (prop), YSI_gscProp2, _YSI_gHashID),
SetSVarInt(prop, ptr);
return _YSI_gHashID;
}
}
case BCRYPT_PLUGIN_VERSION_SREYAS_2:
{
if (_YSI_BCryptHashE(_YSI_gHashID, YSI_gscBCryptHashDone, password, cost, YSI_gcII, ptr, extra))
{
new
prop[16];
format(prop, sizeof (prop), YSI_gscProp1, _YSI_gHashID),
SetSVarInt(prop, extra),
format(prop, sizeof (prop), YSI_gscProp2, _YSI_gHashID),
SetSVarInt(prop, ptr);
return _YSI_gHashID;
}
}
case BCRYPT_PLUGIN_VERSION_SREYAS_3, BCRYPT_PLUGIN_VERSION_SREYAS_4:
{
// May have extra data.
return _YSI_BCryptHashE(_YSI_gHashID, YSI_gscBCryptHashDone, password, cost, YSI_gcII, ptr, extra) ? _YSI_gHashID : 0;
}
default:
{
}
}
}
case BCRYPT_PLUGIN_VERSION_SREYAS_3, BCRYPT_PLUGIN_VERSION_SREYAS_4:
{
// May have extra data.
return _YSI_BCryptHashE(_YSI_gHashID, YSI_gscBCryptHashDone, password, cost, YSI_gcII, ptr, extra) ? _YSI_gHashID : 0;
}
default:
{
}
}
return 0;
}

stock BCrypt_Hash(extra, const callback[], const password[], cost = 12)
{
new ptr;
if (AMX_GetPublicPointer(0, ptr, callback, true))
{
return _BCrypt_Hash(extra, ptr, password, cost);
}
return 0;
}
Expand Down
56 changes: 38 additions & 18 deletions YSI_Extra/y_inline_bcrypt/y_inline_bcrypt_entry.inc
Original file line number Diff line number Diff line change
Expand Up @@ -78,30 +78,43 @@ Optional plugins:

#include "..\..\YSI_Core\y_utils"
#include "..\..\YSI_Server\y_thirdpartyinclude\y3_indirection"
#include "..\y_bcrypt\y_bcrypt_entry"
#include "..\y_bcrypt\y_bcrypt_natives"

static stock const
YSI_gsc_Inline_BCryptCheckDone[] = "_Inline_BCryptCheckDone";
YSI_gsc_Inline_BCryptCheckDone[] = "_Inline_BCryptCheckDone",
YSI_gsc_Inline_BCryptHashDone[] = "_Inline_BCryptHashDone";

forward _Inline_BCryptCheckDone(Func:cb<i>, success);
forward _Inline_BCryptHashDone(Func:cb<si>, arg2);

public _Inline_BCryptCheckDone(Func:cb<i>, success)
{
if (success == -1)
{
success = _YSI_BCryptIsEqualA();
}
success = @.cb(success);
@.cb(success);
Indirect_Release(cb);
return success;
}

static stock _Inline_BCryptHashDone(Func:cb<si>, hashid)
public _Inline_BCryptHashDone(Func:cb<si>, arg2)
{
new hash[61];
// If the plugin is anything other than `1` or `2` this can't be called
// so there's no need for extra checks.
BCrypt_GetHash(hash);
new
hash[61],
hashid = Indirect_GetMeta(cb);
if (numargs() == 2)
{
// Secret internal hash ID.
_YSI_BCryptGetHashB(arg2, hash, sizeof (hash));
}
else if (BCrypt_GetPlugin() == BCRYPT_PLUGIN_VERSION_LASSIR)
{
_YSI_BCryptGetHashA(hash);
}
else
{
_YSI_BCryptGetHashE(hash, sizeof (hash));
}
if (Indirect_GetTag(cb) == _:tagof (F@_@si:))
{
Indirect_Call(_:cb, tagof (F@_@si:), hash, hashid);
Expand All @@ -111,11 +124,8 @@ static stock _Inline_BCryptHashDone(Func:cb<si>, hashid)
Indirect_Call(_:cb, tagof (F@_@s:), hash);
}
Indirect_Release(cb);
return hashid;
}

#define CALL@_Inline_BCryptHashDone%8() _Inline_BCryptHashDone%8(Func:0<si>, 0)

stock bool:BCrypt_CheckInline(const password[], const hash[], Func:cb<i>)
{
switch (BCrypt_GetPlugin())
Expand All @@ -132,13 +142,23 @@ stock bool:BCrypt_CheckInline(const password[], const hash[], Func:cb<i>)

stock BCrypt_HashInline(const password[], cost = 12, {F@_@s, F@_@si}:cb, tag = tagof (cb))
{
cost = _BCrypt_Hash(_:cb, _:__addressof(_Inline_BCryptHashDone), password, cost);
if (cost)
++_YSI_gHashID;
Indirect_SetTag(cb, tag);
Indirect_SetMeta(cb, _YSI_gHashID);
switch (BCrypt_GetPlugin())
{
Indirect_SetTag(cb, tag);
Indirect_Claim(cb);
case BCRYPT_PLUGIN_VERSION_LASSIR:
{
return _YSI_BCryptHashA(password, cost, YSI_gsc_Inline_BCryptHashDone, YSI_gcI, _:cb) ? _YSI_gHashID : 0;
}
case BCRYPT_PLUGIN_VERSION_SREYAS_1, BCRYPT_PLUGIN_VERSION_SREYAS_2, BCRYPT_PLUGIN_VERSION_SREYAS_3, BCRYPT_PLUGIN_VERSION_SREYAS_4:
{
return _YSI_BCryptHashB(_:cb, YSI_gsc_Inline_BCryptHashDone, password, cost) ? _YSI_gHashID : 0;
}
default:
{
return 0;
}
}
// Return the hashid.
return cost;
}

0 comments on commit d98391a

Please sign in to comment.