-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrc.html
More file actions
133 lines (108 loc) · 4.98 KB
/
Copy pathcrc.html
File metadata and controls
133 lines (108 loc) · 4.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Devialet Expert Pro CRC Calculator</title>
<style>
body { font-family: sans-serif; margin: 2em; max-width: 800px; margin: auto; }
.box { display: flex; flex-direction: column; width: 100%; margin-top: 1em; }
textarea { width: 100%; height: 300px; margin-top: 0.5em; box-sizing: border-box; font-family: monospace; }
button { margin-top: 1em; padding: 0.5em 1em; font-size: 1em; }
#controls { margin-top: 1em; }
</style>
</head>
<body>
<h1>Devialet Expert Pro CRC Calculator</h1>
<p>Upload your configuration file. Click recalculate and download the file with the correct CRC.</p>
<p>Disclaimer: Use this tool at your own risk.</p>
<div id="controls">
<label for="fileInput">Upload File:</label>
<input type="file" id="fileInput">
</div>
<div class="box">
<h2>Input File</h2>
<textarea id="inputFileContent" placeholder="Upload a file..."></textarea>
</div>
<button id="recalculateButton">Recalculate Checksum</button>
<div class="box">
<h2>Output File</h2>
<textarea id="outputFileContent" readonly></textarea>
<button id="downloadButton" style="display: none; align-self: flex-start;">Download Output File</button>
</div>
<script>
document.getElementById('fileInput').addEventListener('change', handleFileSelect, false);
document.getElementById('recalculateButton').addEventListener('click', runCalculation, false);
document.getElementById('downloadButton').addEventListener('click', downloadOutputFile, false);
let outputContent = '';
let originalFilename = 'recalculated_file.txt'; // Default filename
function handleFileSelect(event) {
const file = event.target.files[0];
if (!file) return;
originalFilename = file.name;
const reader = new FileReader();
reader.onload = function(e) {
document.getElementById('inputFileContent').value = e.target.result;
};
reader.readAsText(file);
}
function runCalculation() {
const contentsFromTextarea = document.getElementById('inputFileContent').value;
if (!contentsFromTextarea) {
alert("Input box is empty.");
return;
}
// **FIX:** Normalize all line endings to CRLF (\r\n) to ensure consistency.
// This replaces any standalone \n or \r\n with \r\n.
const contents = contentsFromTextarea.replace(/\r?\n/g, '\r\n');
if (contents.length < 7 || !contents.toUpperCase().startsWith('CRC')) {
alert('The input text must start with "CRCxxxx" (e.g., CRC0000).');
return;
}
// The data to be hashed is everything AFTER the first 7 bytes ('CRCxxxx')
const dataToHash = contents.substring(7);
// Calculate the new CRC on the CRLF-normalized data
const crc = crc16_ccitt_false(dataToHash);
const crcHex = crc.toString(16).toUpperCase().padStart(4, '0');
const newCrcLine = 'CRC' + crcHex;
// Find the end of the first line to get the rest of the file content
const firstLineEndIndex = contents.indexOf('\r\n');
let restOfContent = '';
if (firstLineEndIndex !== -1) {
// If a CRLF is found, the rest of the content starts from there
restOfContent = contents.substring(firstLineEndIndex);
}
// Reconstruct the final output with the new CRC
outputContent = newCrcLine + restOfContent;
// Update the UI
document.getElementById('outputFileContent').value = outputContent;
document.getElementById('downloadButton').style.display = 'block';
}
function downloadOutputFile() {
// The outputContent string already has the correct CRLF line endings
const blob = new Blob([outputContent], { type: 'text/plain' });
const link = document.createElement('a');
link.href = URL.createObjectURL(blob);
link.download = originalFilename;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
function crc16_ccitt_false(str) {
let crc = 0xFFFF; // Initial value
const polynomial = 0x1021;
for (let i = 0; i < str.length; i++) {
crc ^= str.charCodeAt(i) << 8;
for (let j = 0; j < 8; j++) {
if ((crc & 0x8000) !== 0) {
crc = (crc << 1) ^ polynomial;
} else {
crc <<= 1;
}
}
}
return crc & 0xFFFF;
}
</script>
</body>
</html>