__main__.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #!/usr/bin/env python3
  2. # -*- coding: utf8 -*-
  3. # CRAP5 - 5-bit Compact Representation of Alphabetical Patterns
  4. # Copyright © 2023 Nichlas Severinsen
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. import re
  19. import sys
  20. import string
  21. import argparse
  22. import textwrap
  23. import crap5
  24. if __name__ == 'crap5.__main__':
  25. parser = argparse.ArgumentParser('CRAP5 encoder/decoder')
  26. parser.add_argument('-d', '--decode', action='store_true', help='Decode data')
  27. parser.add_argument('-i', '--infile', type=argparse.FileType('rb'), default=(None if sys.stdin.isatty() else sys.stdin.buffer.raw))
  28. parser.add_argument('-o', '--outfile', type=argparse.FileType('wb'), default=sys.stdout.buffer)
  29. parser.add_argument('-u', '--uppercase', action='store_true', help='Print decoded data as uppercase')
  30. parser.add_argument('-x', '--hex', action='store_true', help='Print encoded data as hex')
  31. parser.add_argument('-b', '--bin', action='store_true', help='Print encoded data as binary')
  32. args = parser.parse_args()
  33. if not args.infile:
  34. print('No input given through either STDIN or input file. Do --help to see options. Nothing left to do. Terminating.')
  35. sys.exit()
  36. if args.decode:
  37. for decoded in crap5.decode(args.infile):
  38. if args.uppercase:
  39. sys.stdout.buffer.write(decoded)
  40. else:
  41. sys.stdout.buffer.write(decoded.lower())
  42. sys.stdout.buffer.write(b'\n')
  43. else:
  44. try:
  45. if args.bin:
  46. bitdict = {x.to_bytes(1, 'big'): '{0:08b}'.format(x).encode('utf8') for x in range(0,crap5.BASE_BINARY**crap5.BYTE_LENGTH)}
  47. for byte in crap5.encode(args.infile):
  48. if args.bin:
  49. sys.stdout.buffer.write(bitdict[byte] + b' ')
  50. elif args.hex:
  51. sys.stdout.buffer.write(byte.hex().encode('utf8'))
  52. else:
  53. sys.stdout.buffer.write(byte)
  54. sys.stdout.buffer.write(b'\n')
  55. except KeyError as err:
  56. sys.stdout.buffer.write(b'\n')
  57. sys.stdout.flush()
  58. sys.stderr.buffer.write(b"ERROR: Unknown character %s. Please sanitize your input. Terminating.\n" % str(err).encode('utf8'))
  59. sys.exit(1)
  60. sys.exit()