FromHex.mjs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. {
  33. pattern: "^(?:[\\dA-F]{2})+$",
  34. flags: "i",
  35. args: ["None"]
  36. },
  37. {
  38. pattern: "^[\\dA-F]{2}(?: [\\dA-F]{2})*$",
  39. flags: "i",
  40. args: ["Space"]
  41. },
  42. {
  43. pattern: "^[\\dA-F]{2}(?:,[\\dA-F]{2})*$",
  44. flags: "i",
  45. args: ["Comma"]
  46. },
  47. {
  48. pattern: "^[\\dA-F]{2}(?:;[\\dA-F]{2})*$",
  49. flags: "i",
  50. args: ["Semi-colon"]
  51. },
  52. {
  53. pattern: "^[\\dA-F]{2}(?::[\\dA-F]{2})*$",
  54. flags: "i",
  55. args: ["Colon"]
  56. },
  57. {
  58. pattern: "^[\\dA-F]{2}(?:\\n[\\dA-F]{2})*$",
  59. flags: "i",
  60. args: ["Line feed"]
  61. },
  62. {
  63. pattern: "^[\\dA-F]{2}(?:\\r\\n[\\dA-F]{2})*$",
  64. flags: "i",
  65. args: ["CRLF"]
  66. },
  67. {
  68. pattern: "^(?:0x[\\dA-F]{2})+$",
  69. flags: "i",
  70. args: ["0x"]
  71. },
  72. {
  73. pattern: "^0x[\\dA-F]{2}(?:,0x[\\dA-F]{2})*$",
  74. flags: "i",
  75. args: ["0x with comma"]
  76. },
  77. {
  78. pattern: "^(?:\\\\x[\\dA-F]{2})+$",
  79. flags: "i",
  80. args: ["\\x"]
  81. }
  82. ];
  83. }
  84. /**
  85. * @param {string} input
  86. * @param {Object[]} args
  87. * @returns {byteArray}
  88. */
  89. run(input, args) {
  90. const delim = args[0] || "Auto";
  91. return fromHex(input, delim, 2);
  92. }
  93. /**
  94. * Highlight to Hex
  95. *
  96. * @param {Object[]} pos
  97. * @param {number} pos[].start
  98. * @param {number} pos[].end
  99. * @param {Object[]} args
  100. * @returns {Object[]} pos
  101. */
  102. highlight(pos, args) {
  103. if (args[0] === "Auto") return false;
  104. const delim = Utils.charRep(args[0] || "Space"),
  105. len = delim === "\r\n" ? 1 : delim.length,
  106. width = len + 2;
  107. // 0x and \x are added to the beginning if they are selected, so increment the positions accordingly
  108. if (delim === "0x" || delim === "\\x") {
  109. if (pos[0].start > 1) pos[0].start -= 2;
  110. else pos[0].start = 0;
  111. if (pos[0].end > 1) pos[0].end -= 2;
  112. else pos[0].end = 0;
  113. }
  114. pos[0].start = pos[0].start === 0 ? 0 : Math.round(pos[0].start / width);
  115. pos[0].end = pos[0].end === 0 ? 0 : Math.ceil(pos[0].end / width);
  116. return pos;
  117. }
  118. /**
  119. * Highlight from Hex
  120. *
  121. * @param {Object[]} pos
  122. * @param {number} pos[].start
  123. * @param {number} pos[].end
  124. * @param {Object[]} args
  125. * @returns {Object[]} pos
  126. */
  127. highlightReverse(pos, args) {
  128. const delim = Utils.charRep(args[0] || "Space"),
  129. len = delim === "\r\n" ? 1 : delim.length;
  130. pos[0].start = pos[0].start * (2 + len);
  131. pos[0].end = pos[0].end * (2 + len) - len;
  132. // 0x and \x are added to the beginning if they are selected, so increment the positions accordingly
  133. if (delim === "0x" || delim === "\\x") {
  134. pos[0].start += 2;
  135. pos[0].end += 2;
  136. }
  137. return pos;
  138. }
  139. }
  140. export default FromHex;