FromHex.mjs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /**
  2. * @author n1474335 [n1474335@gmail.com]
  3. * @copyright Crown Copyright 2016
  4. * @license Apache-2.0
  5. */
  6. import Operation from "../Operation.mjs";
  7. import {fromHex, FROM_HEX_DELIM_OPTIONS} from "../lib/Hex.mjs";
  8. import Utils from "../Utils.mjs";
  9. /**
  10. * From Hex operation
  11. */
  12. class FromHex extends Operation {
  13. /**
  14. * FromHex constructor
  15. */
  16. constructor() {
  17. super();
  18. this.name = "From Hex";
  19. this.module = "Default";
  20. this.description = "Converts a hexadecimal byte string back into its raw value.<br><br>e.g. <code>ce 93 ce b5 ce b9 ce ac 20 cf 83 ce bf cf 85 0a</code> becomes the UTF-8 encoded string <code>Γειά σου</code>";
  21. this.infoURL = "https://wikipedia.org/wiki/Hexadecimal";
  22. this.inputType = "string";
  23. this.outputType = "byteArray";
  24. this.args = [
  25. {
  26. name: "Delimiter",
  27. type: "option",
  28. value: FROM_HEX_DELIM_OPTIONS
  29. }
  30. ];
  31. this.checks = {
  32. input: {
  33. regex: [
  34. {
  35. match: "^(?:[\\dA-F]{2})+$",
  36. flags: "i",
  37. args: ["None"]
  38. },
  39. {
  40. match: "^[\\dA-F]{2}(?: [\\dA-F]{2})*$",
  41. flags: "i",
  42. args: ["Space"]
  43. },
  44. {
  45. match: "^[\\dA-F]{2}(?:,[\\dA-F]{2})*$",
  46. flags: "i",
  47. args: ["Comma"]
  48. },
  49. {
  50. match: "^[\\dA-F]{2}(?:;[\\dA-F]{2})*$",
  51. flags: "i",
  52. args: ["Semi-colon"]
  53. },
  54. {
  55. match: "^[\\dA-F]{2}(?::[\\dA-F]{2})*$",
  56. flags: "i",
  57. args: ["Colon"]
  58. },
  59. {
  60. match: "^[\\dA-F]{2}(?:\\n[\\dA-F]{2})*$",
  61. flags: "i",
  62. args: ["Line feed"]
  63. },
  64. {
  65. match: "^[\\dA-F]{2}(?:\\r\\n[\\dA-F]{2})*$",
  66. flags: "i",
  67. args: ["CRLF"]
  68. },
  69. {
  70. match: "^[\\dA-F]{2}(?:0x[\\dA-F]{2})*$",
  71. flags: "i",
  72. args: ["0x"]
  73. },
  74. {
  75. match: "^[\\dA-F]{2}(?:\\\\x[\\dA-F]{2})*$",
  76. flags: "i",
  77. args: ["\\x"]
  78. }
  79. ]
  80. }
  81. };
  82. }
  83. /**
  84. * @param {string} input
  85. * @param {Object[]} args
  86. * @returns {byteArray}
  87. */
  88. run(input, args) {
  89. const delim = args[0] || "Auto";
  90. return fromHex(input, delim, 2);
  91. }
  92. /**
  93. * Highlight to Hex
  94. *
  95. * @param {Object[]} pos
  96. * @param {number} pos[].start
  97. * @param {number} pos[].end
  98. * @param {Object[]} args
  99. * @returns {Object[]} pos
  100. */
  101. highlight(pos, args) {
  102. if (args[0] === "Auto") return false;
  103. const delim = Utils.charRep(args[0] || "Space"),
  104. len = delim === "\r\n" ? 1 : delim.length,
  105. width = len + 2;
  106. // 0x and \x are added to the beginning if they are selected, so increment the positions accordingly
  107. if (delim === "0x" || delim === "\\x") {
  108. if (pos[0].start > 1) pos[0].start -= 2;
  109. else pos[0].start = 0;
  110. if (pos[0].end > 1) pos[0].end -= 2;
  111. else pos[0].end = 0;
  112. }
  113. pos[0].start = pos[0].start === 0 ? 0 : Math.round(pos[0].start / width);
  114. pos[0].end = pos[0].end === 0 ? 0 : Math.ceil(pos[0].end / width);
  115. return pos;
  116. }
  117. /**
  118. * Highlight from Hex
  119. *
  120. * @param {Object[]} pos
  121. * @param {number} pos[].start
  122. * @param {number} pos[].end
  123. * @param {Object[]} args
  124. * @returns {Object[]} pos
  125. */
  126. highlightReverse(pos, args) {
  127. const delim = Utils.charRep(args[0] || "Space"),
  128. len = delim === "\r\n" ? 1 : delim.length;
  129. pos[0].start = pos[0].start * (2 + len);
  130. pos[0].end = pos[0].end * (2 + len) - len;
  131. // 0x and \x are added to the beginning if they are selected, so increment the positions accordingly
  132. if (delim === "0x" || delim === "\\x") {
  133. pos[0].start += 2;
  134. pos[0].end += 2;
  135. }
  136. return pos;
  137. }
  138. }
  139. export default FromHex;