Skip to content

Commit

Permalink
update size estimate accuracy when seperator option applied
Browse files Browse the repository at this point in the history
  • Loading branch information
tp7309 committed Jul 23, 2017
1 parent c67ee88 commit 6f56b8d
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
out.dict
out.dict.1
out.dict.2
README.rst
coverage_html/

# Byte-compiled / optimized / DLL files
__pycache__/
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ generated password displayed line by line in `OUTPUT`. It is recommended to use

**[]{m:n:r}**
```
when repeatMode is `?`, [123]{1,2} -> 1 2 3 12 13 21 23 31 32
when repeatMode is `*`, [123]{1,2} -> 1 2 3 11 12 13 21 22 23 31 32 33
when repeatMode is `?`, [123]{1,2:?} -> 1 2 3 12 13 21 23 31 32
when repeatMode is `*`, [123]{1,2:*} -> 1 2 3 11 12 13 21 22 23 31 32 33
```

**[]{m:n}**
Expand Down
4 changes: 2 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ Repeat mode

::

when repeatMode is `?`, [123]{1,2} -> 1 2 3 12 13 21 23 31 32
when repeatMode is `*`, [123]{1,2} -> 1 2 3 11 12 13 21 22 23 31 32 33
when repeatMode is `?`, [123]{1,2:?} -> 1 2 3 12 13 21 23 31 32
when repeatMode is `*`, [123]{1,2:*} -> 1 2 3 11 12 13 21 22 23 31 32 33

**[]{minLength:maxLength}** default use ``global_repeat_mode`` option.

Expand Down
4 changes: 2 additions & 2 deletions README_zh_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ Options:

**[]{m:n:r}** 重复m次到n次,重复模式(r)支持`?``*`
```
repeatMode为 `?`, [123]{1,2} -> 1 2 3 12 13 21 23 31 32
repeatMode为 `*`, [123]{1,2} -> 1 2 3 11 12 13 21 22 23 31 32 33
repeatMode为 `?`, [123]{1,2:?} -> 1 2 3 12 13 21 23 31 32
repeatMode为 `*`, [123]{1,2:*} -> 1 2 3 11 12 13 21 22 23 31 32 33
```

**[]{m:n}** 重复m次到n次,重复模式未定义时采用`global_repeat_mode`选项定义的值。
Expand Down
1 change: 1 addition & 0 deletions tests/test_TTPassGen.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ def test_dict_mark_charset_rule(self):

def test_word_seperator(self):
self.assertEquals(go('$0[abc]?', seperator=' '), 1)
self.assertEquals(go('$0[abc]?', seperator='-------------------------\n'), 24)


def test_multi_dict_mark_charset_rule(self):
Expand Down
16 changes: 9 additions & 7 deletions ttpassgen/ttpassgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class DictRule(object):
def __init__(self, order, dictPath):
self.order = order;
self.dictPath = dictPath;

#Rule class end

class WordProductor(object):
def __init__(self, countList, sizeList, productors):
Expand All @@ -114,14 +114,13 @@ def prod(cls, iterable):
def totalCount(self):
return self.prod(self.countList)

def totalSize(self):
def totalSize(self, sep=os.linesep):
total = 0
tCount = self.totalCount()
for i, size in enumerate(self.sizeList):
total += size * tCount / self.countList[i]
total += tCount * len(os.linesep)
total += tCount * len(sep)
return total
#Rule class end


def prettySize(size_bytes):
Expand Down Expand Up @@ -164,16 +163,19 @@ def getCharsetRuleResultDataSize(rule):
size += count * wordLength
return count, size


def getDictRuleResultDataSize(rule):
sumLines = 0
sepLen = 1
with open(rule.dictPath, 'r') as f:
bufferSize = 1024 * 4
readFunc = f.read #loop optimization
chunk = readFunc(bufferSize)
if '\r\n' in chunk: sepLen = 2
while chunk:
sumLines += chunk.count('\n')
chunk = readFunc(bufferSize)
linSeperatorCount = len(os.linesep) * sumLines
linSeperatorCount = sepLen * sumLines
return sumLines, (os.path.getsize(rule.dictPath) - linSeperatorCount)


Expand Down Expand Up @@ -334,7 +336,7 @@ def productCombinationWords(result, rules, dictCacheLimit, partSize, appendMode,
productor = generateWordProductor(rules, dictCacheLimit)
result[1] = int(productor.totalCount())
result[0] = 1
estimatedSize = prettySize(productor.totalSize())
estimatedSize = prettySize(productor.totalSize(sep=seperator))
print(("estimated size: %s, generate dict...")%(estimatedSize))

if not os.path.exists(os.path.abspath(os.path.join(output, os.path.pardir))):
Expand Down Expand Up @@ -477,4 +479,4 @@ def cli(mode, dictlist, rule, dict_cache, global_repeat_mode, part_size, append_

if __name__ == "__main__":
cli()
# cli.main(['-d', 'tests/in.dict', '-r', '[?l]{5}', out.dict'])
# cli.main(['-d', '../tests/in.dict', '-r', '[?l]{5}', 'out.dict'])

0 comments on commit 6f56b8d

Please sign in to comment.