Commit 48966d5e authored by Nathan/Eilisha Shiraini's avatar Nathan/Eilisha Shiraini
Browse files

Created data encoding and preparing function

parent 51418980
from . import encoder
from . import reedsolomon
from . import sizes
def prepareData(data, forceLayers = None, forceCompact = None):
encoded = encoder.Encoder().encode(data, asBits = True)
if isinstance(forceLayers, int):
wordsize = sizes.WORD_SIZE_FOR_LAYERS[forceLayers]
stuffed = list(reedsolomon.towords(reedsolomon.stuff(encoded, wordsize), wordsize))
layers = forceLayers
compact = bool(forceCompact) if forceCompact is not None else (forceLayers < 5 and len(stuffed) < sizes.COMPACT_WORD_COUNT[forceLayers])
if (compact and len(stuffed) > sizes.COMPACT_WORD_COUNT[forceLayers]) or ((not compact) and len(stuffed) > sizes.FULL_WORD_COUNT[forceLayers]):
raise ValueError("Data too large")
else:
for i in range(1, 33):
wordsize = sizes.WORD_SIZE_FOR_LAYERS[i]
stuffed = list(reedsolomon.towords(reedsolomon.stuff(encoded, wordsize), wordsize))
if forceCompact is not None:
if forceCompact and len(stuffed) <= sizes.COMPACT_WORD_COUNT[i]:
compact = True
layers = i
break
elif len(stuffed) <= sizes.FULL_WORD_COUNT[i]:
compact = False
layers = i
break
else:
if i < 5 and len(stuffed) <= sizes.COMPACT_WORD_COUNT[i]:
compact = True
layers = i
break
elif len(stuffed) <= sizes.FULL_WORD_COUNT[i]:
compact = False
layers = i
break
else:
raise ValueError("Data too large")
wordcount = (len(stuffed), (sizes.COMPACT_WORD_COUNT[layers] if compact else sizes.FULL_WORD_COUNT[layers]) - len(stuffed))
rscode = sizes.GALLOIS_FIELD_FOR_LAYERS[layers].gen_rs_code(stuffed, wordcount[1])
result = list(reedsolomon.fromwords(stuffed + rscode, wordsize))
return result, layers, compact, wordcount
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment