增加 CRC Hex 输入并修复依赖漏洞
This commit is contained in:
+49
-5
@@ -1,4 +1,8 @@
|
||||
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
|
||||
@@ -17,7 +21,14 @@ export interface CrcWorkerCalculateRequest {
|
||||
parameters: CrcParameters
|
||||
}
|
||||
|
||||
export type CrcWorkerRequest = CrcWorkerParseRequest | CrcWorkerCalculateRequest
|
||||
export interface CrcWorkerCalculateHexRequest {
|
||||
type: 'calculate-hex'
|
||||
id: number
|
||||
input: string
|
||||
parameters: CrcParameters
|
||||
}
|
||||
|
||||
export type CrcWorkerRequest = CrcWorkerParseRequest | CrcWorkerCalculateRequest | CrcWorkerCalculateHexRequest
|
||||
|
||||
export interface CrcBlockResult {
|
||||
startAddress: number
|
||||
@@ -26,25 +37,58 @@ export interface CrcBlockResult {
|
||||
crc: string
|
||||
}
|
||||
|
||||
export type CrcWorkerResponse = {
|
||||
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: CrcWorkerRequest['type']
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user