Skip to content

Commit

Permalink
Fix editor (Kanaduchi#160)
Browse files Browse the repository at this point in the history
* Fix codings

* Fix codings

* Fix codings

* Fix codings

* Fix codings

* Fix codings

* Fix codings

* Fix codings

* Fix codings

* Fix codings

* Fix codings

* Fix codings

* Fix codings

* Fix codings

* Fix codings

* Fix codings

* Fix codings

* Fix codings

* Fix codings

* Fix codings

* Fix codings

* Fix codings

* Fix disqus integration

* Add gitignore

* Add gitignore

* Preparing for gisqus

* Improve PQ26

* Add codings

* Add codings

* Add codings

* Fix

* Fix dataset editor

* Fix dataset editor

* Fix dataset editor
  • Loading branch information
Kanaduchi authored Feb 8, 2022
1 parent c957440 commit 304d203
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 27 deletions.
67 changes: 47 additions & 20 deletions docs/images/dataset.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
/**
* Convert big-endian hex to little-endian hex
* Convert big-endian hex to little-endian hex and vice-versa
*/
function setEndianConvert(value) {
let result = '';
if (value.length % 2) {
value = '0' + String(value);
}

const len = value.length / 2;
let str = '';
for (let i = 0; i < len; i++) {
const count = len - i - 1;
str += '' + value.substring(count * 2, count * 2 + 2);
for (let n = 0; n < value.length; n += 4) {
const str = value.substring(n, n + 4);
result += str.replace(/(..)(..)?/g, "$2$1");
}
return result;
}

str = str.toUpperCase();
return str;
/**
* Check if hex is little-endian. Example: 5216 = true; 1652 = false
*/
function isLittleEndian(value) {
if (value.length % 2) {
value = '0' + String(value);
}
let array = hexToDecimalArray(value.substring(0, 4));
if (array[0] === array[1]) {
return isLittleEndian(value.substring(4));
}
return array[0] > array[1];
}

/**
Expand Down Expand Up @@ -331,21 +341,28 @@ function doCreateParam(row, showDialog) {
let raw = cleanHexValue(document.getElementById('param_' + row).value);
if (raw) {
doCRC16Calc(row);
result = parseStringValueToHex(raw +
asciiToHex(document.getElementById('version_' + row).value.substring(0, 2)) +
document.getElementById("crc16_" + row).value);
result = raw + asciiToHex(document.getElementById('version_' + row).value.substring(0, 2)) +
document.getElementById("crc16_" + row).value;
if (document.getElementById('littleEndian_' + row).checked && !isLittleEndian(raw)) {
result = setEndianConvert(result);
}
if (showDialog) {
window.prompt("Parameter", result);
window.prompt("Parameter", parseStringValueToHex(result));
}
}
return result;
return parseStringValueToHex(result);
}

function doCRC16Calc(row) {
let raw = cleanHexValue(document.getElementById('param_' + row).value);
if (document.getElementById('version_' + row).value && raw) {
document.getElementById('crc16_' + row).value = CRC16_CCITT(hexToDecimalArray(
let hexValue = CRC16_CCITT(hexToDecimalArray(
raw + asciiToHex(document.getElementById('version_' + row).value.substring(0, 2)))).toUpperCase();
if (isLittleEndian(raw)) {
hexValue = CRC16_CCITT(hexToDecimalArray(
raw + setEndianConvert(asciiToHex(document.getElementById('version_' + row).value.substring(0, 2))))).toUpperCase()
}
document.getElementById('crc16_' + row).value = hexValue;
}
}

Expand All @@ -369,6 +386,7 @@ function addNewParam() {
html = "<td rowspan=\"1\" colspan=\"3\">";
html += "<textarea rows=\"5\" cols=\"25\" id=\"param_" + randomName + "\" style=\"width: 100%; font-family: Courier New, Courier, monospace;\"></textarea>";
html += "</td><td rowspan=\"1\" colspan=\"4\">";
html += "Bytes in little-endian format: <input type=\"checkbox\" id=\"littleEndian_" + randomName + "\"><br>";
html += "Version of hex: <input size=\"2\" maxlength=\"2\" id=\"version_" + randomName + "\" defaultValue=\"0\"><br>";
html += "CRC16 sum: <input size=\"10\" id=\"crc16_" + randomName + "\" defaultValue=\"0\" disabled> <button class=\"pure-material-button-contained\" style=\"min-width:10px\" type=\"button\" onclick=\"doCRC16Calc(" + randomName + ")\">⟳</button><br>";
html += "<button class=\"pure-material-button-contained\" style=\"min-width:10px\" type=\"button\" onclick=\"doCreateParam(" + randomName + ", true)\">Generate</button>";
Expand Down Expand Up @@ -432,15 +450,21 @@ function handleUpload() {
document.getElementById('fileName_' + randomName).defaultValue = a.getAttribute("FILENAME");
if (a.textContent.length !== 0) {
const raw = parseHexValueToString(a.textContent.trim());
document.getElementById("version_" + randomName).value = hexToAscii(raw.substring(raw.length - 8, raw.length - 4));
document.getElementById("crc16_" + randomName).value = raw.substring(raw.length - 4, raw.length);
if (isLittleEndian(raw)) {
document.getElementById('littleEndian_' + randomName).checked = true;
document.getElementById("version_" + randomName).value = hexToAscii(setEndianConvert(raw.substring(raw.length - 8, raw.length - 4)));
document.getElementById("crc16_" + randomName).value = setEndianConvert(raw.substring(raw.length - 4, raw.length));
} else {
document.getElementById("version_" + randomName).value = hexToAscii(raw.substring(raw.length - 8, raw.length - 4));
document.getElementById("crc16_" + randomName).value = raw.substring(raw.length - 4, raw.length);
}
document.getElementById("param_" + randomName).value = parseValue(raw.substring(0, raw.length - 8));
}
});

if (xmlDoc.querySelector('COMPRESSED_DATA') &&
xmlDoc.querySelector('COMPRESSED_DATA').getAttribute("BYTES_UNCOMPRESSED") != '0' &&
xmlDoc.querySelector('COMPRESSED_DATA').getAttribute("BYTES_COMPRESSED") != '0') {
xmlDoc.querySelector('COMPRESSED_DATA').getAttribute("BYTES_UNCOMPRESSED") !== '0' &&
xmlDoc.querySelector('COMPRESSED_DATA').getAttribute("BYTES_COMPRESSED") !== '0') {
document.getElementById("compressedData").style.display = "table";
document.getElementById('isCompressed').checked = true;
document.getElementById('content').value = xmlDoc.querySelector('COMPRESSED_DATA').getAttribute("CONTENT");
Expand Down Expand Up @@ -528,7 +552,10 @@ function doSimplify() {
document.getElementById('flashOffset_' + randomName).defaultValue = document.getElementById('parsedFlashOffset_' + rows[i].id).value;
document.getElementById('datasetName_' + randomName).defaultValue = document.getElementById('parsedDatasetName_' + rows[i].id).value;
document.getElementById("version_" + randomName).value = document.getElementById("parsedVersion_" + rows[i].id).value.substring(0, 2);
document.getElementById("param_" + randomName).value = parseValue(setEndianConvert(cleanHexValue(document.getElementById('parsedParam_' + rows[i].id).value)));
document.getElementById("param_" + randomName).value = document.getElementById('parsedParam_' + rows[i].id).value;
if (isLittleEndian(cleanHexValue(document.getElementById('parsedParam_' + rows[i].id).value))) {
document.getElementById('littleEndian_' + randomName).checked = true;
}
doCRC16Calc(randomName);
}
}
Expand Down
6 changes: 3 additions & 3 deletions docs/utils/datasetEditor.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ Editor possibilities:
- Migrate base64 data content into parameter sections (dataset simplification)
- Calculation of CRC32 (for decoded simplified data)
- Save dataset in simplified format
- Detect little-endian byte order

??? note "Experimental function of dataset simplification"
1 - Decode base64 value of <COMPRESSED_DATA> object. Value has structure of <SW-CNT>
2 - Cut first 8 bytes (version) and last 8 bytes (CRC-32 sum) of value of <DATEN> field
3 - Convert result value in little-endian format
4 - Save it in the corresponding <PARAMETER_DATA> object and calculate CRC-16 hash
2 - Cut first 8 bytes (version) and last 8 bytes (CRC-32 sum) of value of <DATEN> field
3 - Save it in the corresponding <PARAMETER_DATA> object and calculate CRC-16 hash

--8<-- "overrides/pages/datasets.en.html"
6 changes: 3 additions & 3 deletions docs/utils/datasetEditor.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ hide:
- Преобразование base64 набора данных в секции с параметрами (упрощение файла)
- Подсчет контрольных сумм (для дешифрованных значений)
- Сохранение файла в упрощенном формате
- Распознавание little-endian порядка байтов

??? note "Экспериментальная функция по упрощению зашифрованной параметрии"
1 - Расшифровываем base64 <COMPRESSED_DATA> объекта. Расшифрованное значение имеет набор параметров в виде структур <SW-CNT>
2 - У значения поля <DATEN> в каждой структуре <SW-CNT> отсекаем первые 8 байт (версия) и последние 8 байт (CRC-32 хэш)
3 - Полученное значение переворачиваем в формат little-endian
4 - Сохраняем его в соответствующее <PARAMETER_DATA> и калькулируем CRC-16 хэш
2 - У значения поля <DATEN> в каждой структуре <SW-CNT> отсекаем первые 8 байт (версия) и последние 8 байт (CRC-32 хэш)
3 - Сохраняем его в соответствующее <PARAMETER_DATA> и калькулируем CRC-16 хэш

--8<-- "overrides/pages/datasets.html"
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
mkdocs-material==8.1.10
mkdocs-print-site-plugin==2.3
mkdocs-static-i18n==0.31
mkdocs-static-i18n==0.42

0 comments on commit 304d203

Please sign in to comment.