codingstatemachine.py 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. ######################## BEGIN LICENSE BLOCK ########################
  2. # The Original Code is mozilla.org code.
  3. #
  4. # The Initial Developer of the Original Code is
  5. # Netscape Communications Corporation.
  6. # Portions created by the Initial Developer are Copyright (C) 1998
  7. # the Initial Developer. All Rights Reserved.
  8. #
  9. # Contributor(s):
  10. # Mark Pilgrim - port to Python
  11. #
  12. # This library is free software; you can redistribute it and/or
  13. # modify it under the terms of the GNU Lesser General Public
  14. # License as published by the Free Software Foundation; either
  15. # version 2.1 of the License, or (at your option) any later version.
  16. #
  17. # This library is distributed in the hope that it will be useful,
  18. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  20. # Lesser General Public License for more details.
  21. #
  22. # You should have received a copy of the GNU Lesser General Public
  23. # License along with this library; if not, write to the Free Software
  24. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  25. # 02110-1301 USA
  26. ######################### END LICENSE BLOCK #########################
  27. import logging
  28. from .enums import MachineState
  29. class CodingStateMachine(object):
  30. """
  31. A state machine to verify a byte sequence for a particular encoding. For
  32. each byte the detector receives, it will feed that byte to every active
  33. state machine available, one byte at a time. The state machine changes its
  34. state based on its previous state and the byte it receives. There are 3
  35. states in a state machine that are of interest to an auto-detector:
  36. START state: This is the state to start with, or a legal byte sequence
  37. (i.e. a valid code point) for character has been identified.
  38. ME state: This indicates that the state machine identified a byte sequence
  39. that is specific to the charset it is designed for and that
  40. there is no other possible encoding which can contain this byte
  41. sequence. This will to lead to an immediate positive answer for
  42. the detector.
  43. ERROR state: This indicates the state machine identified an illegal byte
  44. sequence for that encoding. This will lead to an immediate
  45. negative answer for this encoding. Detector will exclude this
  46. encoding from consideration from here on.
  47. """
  48. def __init__(self, sm):
  49. self._model = sm
  50. self._curr_byte_pos = 0
  51. self._curr_char_len = 0
  52. self._curr_state = None
  53. self.logger = logging.getLogger(__name__)
  54. self.reset()
  55. def reset(self):
  56. self._curr_state = MachineState.START
  57. def next_state(self, c):
  58. # for each byte we get its class
  59. # if it is first byte, we also get byte length
  60. byte_class = self._model['class_table'][c]
  61. if self._curr_state == MachineState.START:
  62. self._curr_byte_pos = 0
  63. self._curr_char_len = self._model['char_len_table'][byte_class]
  64. # from byte's class and state_table, we get its next state
  65. curr_state = (self._curr_state * self._model['class_factor']
  66. + byte_class)
  67. self._curr_state = self._model['state_table'][curr_state]
  68. self._curr_byte_pos += 1
  69. return self._curr_state
  70. def get_current_charlen(self):
  71. return self._curr_char_len
  72. def get_coding_state_machine(self):
  73. return self._model['name']
  74. @property
  75. def language(self):
  76. return self._model['language']