step-02-rfc-to-bin.py 882 B

12345678910111213141516171819202122232425262728293031323334
  1. # Step 02 - parse RFC.
  2. #
  3. # Static dictionary is described in "Appendix A" section in a hexadecimal form.
  4. # This tool locates dictionary data in RFC and converts it to raw binary format.
  5. import re
  6. rfc_path = "rfc7932.txt"
  7. with open(rfc_path, "r") as rfc:
  8. lines = rfc.readlines()
  9. re_data_line = re.compile("^ [0-9a-f]{64}$")
  10. appendix_a_found = False
  11. dictionary = []
  12. for line in lines:
  13. if appendix_a_found:
  14. if re_data_line.match(line) is not None:
  15. data = line.strip()
  16. for i in range(32):
  17. dictionary.append(int(data[2 * i:2 * i + 2], 16))
  18. if len(dictionary) == 122784:
  19. break
  20. else:
  21. if line.startswith("Appendix A."):
  22. appendix_a_found = True
  23. bin_path = "dictionary.bin"
  24. with open(bin_path, "wb") as output:
  25. output.write(bytearray(dictionary))
  26. print("Parsed and saved " + str(len(dictionary)) + " bytes to " + bin_path)