Пример #1
0
def encode(pos, length, dic, buf, consts):
  if length < consts['min']: # not worth encoding, just dump the buffer
    bits = [consts['uncoded']]
    length = consts['max']
    bits.extend(buf[:consts['max']])
  else: # encode the buffer as a match
    bits = [consts['coded']]
    length = min(length, consts['max'])
    bits.extend(dic[pos:length])
  bitstream.write(bits)
  return length
Пример #2
0
def decompress(dic, consts):
  bitstream.start(sys.argv[2], sys.argv[3])
  buf = ['filler']
  while buf != []:
    flag = bitstream.read(1)
    if flag == [47]: #unencoded
      buf = bitstream.read(consts['max'])
    else: # encoded
      break
    # slide the window
    del dic[:len(buf)]
    dic.extend(buf)
    bitstream.write(buf)