gen_qstr.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #
  2. # This file is part of the MicroPython project, http://micropython.org/
  3. #
  4. # The MIT License (MIT)
  5. #
  6. # Copyright (c) 2017 SummerGift <zhangyuan@rt-thread.com>
  7. #
  8. # Permission is hereby granted, free of charge, to any person obtaining a copy
  9. # of this software and associated documentation files (the "Software"), to deal
  10. # in the Software without restriction, including without limitation the rights
  11. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. # copies of the Software, and to permit persons to whom the Software is
  13. # furnished to do so, subject to the following conditions:
  14. #
  15. # The above copyright notice and this permission notice shall be included in
  16. # all copies or substantial portions of the Software.
  17. #
  18. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. # THE SOFTWARE.
  25. #
  26. from __future__ import print_function
  27. import re
  28. import sys
  29. import platform
  30. if platform.python_version_tuple()[0] == '2':
  31. bytes_cons = lambda val, enc=None: bytearray(val)
  32. from htmlentitydefs import codepoint2name
  33. elif platform.python_version_tuple()[0] == '3':
  34. bytes_cons = bytes
  35. from html.entities import codepoint2name
  36. # end compatibility code
  37. # this must match the equivalent function in qstr.c
  38. def compute_hash(qstr, bytes_hash):
  39. hash = 5381
  40. for b in qstr:
  41. hash = (hash * 33) ^ b
  42. # Make sure that valid hash is never zero, zero means "hash not computed"
  43. return (hash & ((1 << (8 * bytes_hash)) - 1)) or 1
  44. def make_bytes(cfg_bytes_len, cfg_bytes_hash, qstr):
  45. #print("qstr:",qstr)
  46. #print("cfg_bytes_hash:",cfg_bytes_hash)
  47. #print("cfg_bytes_len:",cfg_bytes_len)
  48. qbytes = bytes_cons(qstr, 'utf8')
  49. #print("qbytes:",qbytes)
  50. qlen = len(qbytes)
  51. qhash = compute_hash(qbytes, cfg_bytes_hash)
  52. #print("qlen:",qlen)
  53. #print("cfg_bytes_hash:",cfg_bytes_hash)
  54. #print("qhash:",qhash)
  55. if all(32 <= ord(c) <= 126 and c != '\\' and c != '"' for c in qstr):
  56. # qstr is all printable ASCII so render it as-is (for easier debugging)
  57. qdata = qstr
  58. else:
  59. # qstr contains non-printable codes so render entire thing as hex pairs
  60. qdata = ''.join(('\\x%02x' % b) for b in qbytes)
  61. if qlen >= (1 << (8 * cfg_bytes_len)):
  62. print('qstr is too long:', qstr)
  63. assert False
  64. qlen_str = ('\\x%02x' * cfg_bytes_len) % tuple(((qlen >> (8 * i)) & 0xff) for i in range(cfg_bytes_len))
  65. qhash_str = ('\\x%02x' * cfg_bytes_hash) % tuple(((qhash >> (8 * i)) & 0xff) for i in range(cfg_bytes_hash))
  66. return 'QDEF(MP_QSTR_%s, (const byte*)"%s%s" "%s")' % (qdata,qhash_str, qlen_str, qdata)
  67. if __name__ == "__main__":
  68. print ("This program will gen qstr for micropython:")
  69. for arg in sys.argv:
  70. if arg == 'gen_qstr.py':
  71. continue
  72. qstr_computer = make_bytes(1, 1, arg)
  73. print (qstr_computer)