105 lines
2.7 KiB
TypeScript
105 lines
2.7 KiB
TypeScript
import { calculateCrc, calculateCrcChunks, formatCrc, type CrcParameters } from '@/lib/crc'
|
|
import { parseHex } from '@/lib/bytes'
|
|
|
|
export const MAX_INLINE_CRC_DATA_BYTES = 1024 * 1024
|
|
export const MAX_INLINE_CRC_INPUT_CHARS = 6 * 1024 * 1024
|
|
|
|
export interface CrcWorkerBlock {
|
|
startAddress: number
|
|
data: Uint8Array
|
|
}
|
|
|
|
export interface CrcWorkerParseRequest {
|
|
type: 'parse'
|
|
id: number
|
|
file: File
|
|
}
|
|
|
|
export interface CrcWorkerCalculateRequest {
|
|
type: 'calculate'
|
|
id: number
|
|
parameters: CrcParameters
|
|
}
|
|
|
|
export interface CrcWorkerCalculateHexRequest {
|
|
type: 'calculate-hex'
|
|
id: number
|
|
input: string
|
|
parameters: CrcParameters
|
|
}
|
|
|
|
export type CrcWorkerRequest = CrcWorkerParseRequest | CrcWorkerCalculateRequest | CrcWorkerCalculateHexRequest
|
|
|
|
export interface CrcBlockResult {
|
|
startAddress: number
|
|
endAddress: number
|
|
length: number
|
|
crc: string
|
|
}
|
|
|
|
type CrcWorkerParsedResponse = {
|
|
type: 'parsed'
|
|
id: number
|
|
format: 'Intel HEX' | 'BIN'
|
|
totalBytes: number
|
|
blockCount: number
|
|
warnings: string[]
|
|
}
|
|
|
|
type CrcWorkerCalculatedResponse = {
|
|
type: 'calculated'
|
|
id: number
|
|
combinedResult: string
|
|
blockResults: CrcBlockResult[]
|
|
}
|
|
|
|
type CrcWorkerCalculatedHexResponse = {
|
|
type: 'calculated-hex'
|
|
id: number
|
|
combinedResult: string
|
|
totalBytes: number
|
|
}
|
|
|
|
type CrcWorkerErrorResponse<TOperation extends CrcWorkerRequest['type']> = {
|
|
type: 'error'
|
|
id: number
|
|
operation: TOperation
|
|
error: string
|
|
}
|
|
|
|
export type CrcFileWorkerResponse = CrcWorkerParsedResponse
|
|
| CrcWorkerCalculatedResponse
|
|
| CrcWorkerErrorResponse<'parse' | 'calculate'>
|
|
|
|
export type CrcHexWorkerResponse = CrcWorkerCalculatedHexResponse
|
|
| CrcWorkerErrorResponse<'calculate-hex'>
|
|
|
|
export type CrcWorkerResponse = CrcFileWorkerResponse | CrcHexWorkerResponse
|
|
|
|
export function calculateCrcHexInput(input: string, parameters: CrcParameters) {
|
|
const data = parseHex(input, {
|
|
maxBytes: MAX_INLINE_CRC_DATA_BYTES,
|
|
maxInputChars: MAX_INLINE_CRC_INPUT_CHARS,
|
|
limitLabel: 'Hex 数据',
|
|
})
|
|
if (data.length === 0) throw new Error('请输入 Hex 数据')
|
|
return {
|
|
combinedResult: formatCrc(calculateCrc(data, parameters), parameters.width),
|
|
totalBytes: data.length,
|
|
}
|
|
}
|
|
|
|
export function calculateCrcReport(blocks: CrcWorkerBlock[], parameters: CrcParameters) {
|
|
const combinedValue = calculateCrcChunks(blocks.map((block) => block.data), parameters)
|
|
const combinedResult = formatCrc(combinedValue, parameters.width)
|
|
const blockResults = blocks.map((block) => ({
|
|
startAddress: block.startAddress,
|
|
endAddress: block.startAddress + block.data.length - 1,
|
|
length: block.data.length,
|
|
crc: blocks.length === 1
|
|
? combinedResult
|
|
: formatCrc(calculateCrc(block.data, parameters), parameters.width),
|
|
}))
|
|
return { combinedResult, blockResults }
|
|
}
|