From f85198c60f02149de39a8fc01d48f39cf29fbb1e Mon Sep 17 00:00:00 2001 From: Tarquin Winot Date: Tue, 10 Jan 2017 23:37:58 +0100 Subject: [PATCH] Replace old-style C conversions in entropy estimator (#150) --- src/zxcvbn/zxcvbn.cpp | 25 +++++++++++++------------ src/zxcvbn/zxcvbn.h | 2 +- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/src/zxcvbn/zxcvbn.cpp b/src/zxcvbn/zxcvbn.cpp index 5e5678332..25cbe5440 100644 --- a/src/zxcvbn/zxcvbn.cpp +++ b/src/zxcvbn/zxcvbn.cpp @@ -1,4 +1,4 @@ -/********************************************************************************** +/********************************************************************************** * C implementation of the zxcvbn password strength estimation method. * Copyright (c) 2015, Tony Evans * All rights reserved. @@ -228,12 +228,13 @@ static void AddMatchRepeats(ZxcMatch_t **Result, ZxcMatch_t *Match, const uint8_ while(MaxLen >= (Len * RepeatCount)) { - if (strncmp((const char *)Passwd, (const char *)Rpt, Len) == 0) + if (strncmp(reinterpret_cast(Passwd), + reinterpret_cast(Rpt), Len) == 0) { /* Found a repeat */ ZxcMatch_t *p = AllocMatch(); p->Entrpy = Match->Entrpy + log(RepeatCount); - p->Type = (ZxcTypeMatch_t)(Match->Type + MULTIPLE_MATCH); + p->Type = static_cast(Match->Type + MULTIPLE_MATCH); p->Length = Len * RepeatCount; p->Begin = Match->Begin; AddResult(Result, p, MaxLen); @@ -617,7 +618,7 @@ static void DictionaryEntropy(ZxcMatch_t *m, DictMatchInfo_t *Extra, const uint8 e += d; } /* Add entropy due to word's rank */ - e += log((double)Extra->Rank); + e += log(static_cast(Extra->Rank)); m->Entrpy = e; } @@ -794,7 +795,7 @@ static void UserMatch(ZxcMatch_t **Result, const char *Words[], const uint8_t *P int Caps = 0; int Lowers = 0; int Leets = 0; - const uint8_t *Wrd = (const uint8_t *)(Words[Rank]); + const uint8_t *Wrd = reinterpret_cast(Words[Rank]); const uint8_t *Pwd = Passwd; memset(Extra.Leeted, 0, sizeof Extra.Leeted); memset(Extra.UnLeet, 0, sizeof Extra.UnLeet); @@ -1169,7 +1170,7 @@ static void SpatialMatch(ZxcMatch_t **Result, const uint8_t *Passwd, int Start, int i, j, s; double Degree, Entropy; ZxcMatch_t *p; - Degree = (k->NumNear-1) - (double)k->NumBlank / (double)k->NumKeys; + Degree = (k->NumNear-1) - static_cast(k->NumBlank) / static_cast(k->NumKeys); s = k->NumKeys; if (k->Shifts) s *= 2; @@ -1405,13 +1406,13 @@ static void RepeatMatch(ZxcMatch_t **Result, const uint8_t *Passwd, int Start, i int RepeatCount = 2; while(MaxLen >= (Len * RepeatCount)) { - if (strncmp((const char *)Passwd, (const char *)Rpt, Len) == 0) + if (strncmp(reinterpret_cast(Passwd), reinterpret_cast(Rpt), Len) == 0) { /* Found a repeat */ int c = Cardinality(Passwd, Len); ZxcMatch_t *p = AllocMatch(); - p->Entrpy = log((double)c) * Len + log(RepeatCount); - p->Type = (ZxcTypeMatch_t)(BRUTE_MATCH + MULTIPLE_MATCH); + p->Entrpy = log(static_cast(c)) * Len + log(RepeatCount); + p->Type = static_cast(BRUTE_MATCH + MULTIPLE_MATCH); p->Length = Len * RepeatCount; p->Begin = Start; AddResult(Result, p, MaxLen); @@ -1527,7 +1528,7 @@ static void SequenceMatch(ZxcMatch_t **Result, const uint8_t *Passwd, int Start, p->Type = SEQUENCE_MATCH; p->Begin = Start; p->Length = i; - p->Entrpy = e + log((double)i); + p->Entrpy = e + log(static_cast(i)); AddMatchRepeats(Result, p, Pwd, MaxLen); AddResult(Result, p, MaxLen); } @@ -1578,13 +1579,13 @@ double ZxcvbnMatch(const char *Pwd, const char *UserDict[], ZxcMatch_t **Info) Node_t *Np; double e; int Len = strlen(Pwd); - const uint8_t *Passwd = (const uint8_t *)Pwd; + const uint8_t *Passwd = reinterpret_cast(Pwd); uint8_t *RevPwd; /* Create the paths */ Node_t *Nodes = MallocFn(Node_t, Len+1); memset(Nodes, 0, (Len+1) * sizeof *Nodes); i = Cardinality(Passwd, Len); - e = log((double)i); + e = log(static_cast(i)); /* Do matching for all parts of the password */ for(i = 0; i < Len; ++i) diff --git a/src/zxcvbn/zxcvbn.h b/src/zxcvbn/zxcvbn.h index 796d6b47b..2d3ec52c1 100644 --- a/src/zxcvbn/zxcvbn.h +++ b/src/zxcvbn/zxcvbn.h @@ -1,4 +1,4 @@ -#ifndef ZXCVBN_H_F98183CE2A01_INCLUDED +#ifndef ZXCVBN_H_F98183CE2A01_INCLUDED #define ZXCVBN_H_F98183CE2A01_INCLUDED /********************************************************************************** * C implementation of the zxcvbn password strength estimation method.