Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

添加BCC/CRC/LRC校验功能 #199

Merged
merged 7 commits into from
Jan 18, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
bcc crc lrc
  • Loading branch information
baiy committed Jan 18, 2023
commit 130616eb14b6b6709c011cec7006f83bfd889b09
7 changes: 7 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,12 @@
"build": "pnpm run initialize && pnpm --filter ctool-core run build",
"release": "node release.js && pnpm run build && pnpm -r run platform-release",
"only-release": "node release.js && pnpm -r run platform-release"
},
"pnpm": {
"peerDependencyRules": {
"ignoreMissing": [
"buffer"
]
}
}
}
1 change: 1 addition & 0 deletions packages/ctool-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"bignumber.js": "^9.1.1",
"chardet": "^1.5.1",
"chinese-simple2traditional": "^1.1.0",
"crc": "^4.2.0",
"cron-parser": "^4.7.1",
"cronstrue": "^2.21.0",
"crypto-js": "^4.1.1",
Expand Down
55 changes: 33 additions & 22 deletions packages/ctool-core/src/tools/dataValidation/Bcc.vue
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<template>
<HeightResize v-slot="{height}" v-row="'10-14'">
<TextInput v-model="action.current.input" upload="file" :height="height" :allow="['text','hex']"/>
<TextInput v-model="action.current.input" upload="file" :height="height"/>
<Align direction="vertical">
<template v-for="key in ['Hex','Dec','Oct','Bin']">
<Textarea
:model-value="output[key]"
:model-value="getResult(key)"
:height="(height - 15) / 4"
:placeholder="`${$t('main_ui_output')} ${key}`"
:copy="key"
Expand All @@ -17,33 +17,44 @@
<script lang="ts" setup>
import {initialize, useAction} from "@/store/action";
import {createTextInput} from "@/components/text";
import {Bcc} from "./util";
import {bcc, result} from "./util";
import {watch} from "vue";

const action = useAction(await initialize({
input: createTextInput('hex', ""),
}, {paste: false}))
let output = $ref<null | number>(null)
let error = $ref('')

const output = $computed(() => {
if (action.current.input.text.isEmpty()) {
return {
Hex: "",
Dec: "",
Oct: "",
Bin: "",
Count: "",
}
watch(() => {
return {
text: action.current.input.text
}

const handle = new Bcc(action.current.input.text)
if (!handle.isError()){
}, ({text}) => {
error = ""
output = null
if (text.isError()) {
error = text.toString()
return;
}
if (text.isEmpty()) {
return;
}
try {
output = bcc(text)
action.save()
} catch (e) {
error = $error(e)
}
return {
Hex: handle.isError() ? handle.error : handle.hex,
Dec: handle.isError() ? handle.error : handle.dec,
Oct: handle.isError() ? handle.error : handle.oct,
Bin: handle.isError() ? handle.error : handle.bin,
Count: handle.count,
}, {immediate: true, deep: true})

const getResult = (type: string) => {
if (error !== "") {
return error
}
if (output === null) {
return ""
}
})
return result(output, type)
}
</script>
63 changes: 62 additions & 1 deletion packages/ctool-core/src/tools/dataValidation/Crc.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,68 @@
<template>

<HeightResize v-slot="{height}" v-row="'10-14'">
<TextInput v-model="action.current.input" upload="file" :height="height">
<Align>
<Select :size="'small'" :options="crcTypeLists" v-model="action.current.type"/>
<HelpTip link="https://www.npmjs.com/package/crc" />
</Align>
</TextInput>
<Align direction="vertical">
<template v-for="key in ['Hex','Dec','Oct','Bin']">
<Textarea
:model-value="getResult(key)"
:height="(height - 15) / 4"
:placeholder="`${$t('main_ui_output')} ${key}`"
:copy="key"
/>
</template>
</Align>
</HeightResize>
</template>

<script lang="ts" setup>
import {initialize, useAction} from "@/store/action";
import {createTextInput} from "@/components/text";
import {crc, result, crcTypeLists, CrcType} from "./util";
import {watch} from "vue";
import Input from "@/components/text/input";

const action = useAction(await initialize<{ input: Input, type: CrcType }>({
input: createTextInput('hex', ""),
type: "crc32",
}, {paste: false}))
let output = $ref<null | number>(null)
let error = $ref('')

watch(() => {
return {
text: action.current.input.text,
type:action.current.type
}
}, async ({text,type}) => {
error = ""
output = null
if (text.isError()) {
error = text.toString()
return;
}
if (text.isEmpty()) {
return;
}
try {
output = await crc(text, type)
action.save()
} catch (e) {
error = $error(e)
}
}, {immediate: true, deep: true})

const getResult = (type: string) => {
if (error !== "") {
return error
}
if (output === null) {
return ""
}
return result(output, type)
}
</script>
55 changes: 54 additions & 1 deletion packages/ctool-core/src/tools/dataValidation/Lrc.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,60 @@
<template>

<HeightResize v-slot="{height}" v-row="'10-14'">
<TextInput v-model="action.current.input" upload="file" :height="height"/>
<Align direction="vertical">
<template v-for="key in ['Hex','Dec','Oct','Bin']">
<Textarea
:model-value="getResult(key)"
:height="(height - 15) / 4"
:placeholder="`${$t('main_ui_output')} ${key}`"
:copy="key"
/>
</template>
</Align>
</HeightResize>
</template>

<script lang="ts" setup>
import {initialize, useAction} from "@/store/action";
import {createTextInput} from "@/components/text";
import {lrc, result} from "./util";
import {watch} from "vue";

const action = useAction(await initialize({
input: createTextInput('hex', ""),
}, {paste: false}))
let output = $ref<null | number>(null)
let error = $ref('')

watch(() => {
return {
text: action.current.input.text
}
}, ({text}) => {
error = ""
output = null
if (text.isError()) {
error = text.toString()
return;
}
if (text.isEmpty()) {
return;
}
try {
output = lrc(text)
action.save()
} catch (e) {
error = $error(e)
}
}, {immediate: true, deep: true})

const getResult = (type: string) => {
if (error !== "") {
return error
}
if (output === null) {
return ""
}
return result(output, type)
}
</script>
77 changes: 34 additions & 43 deletions packages/ctool-core/src/tools/dataValidation/util.ts
Original file line number Diff line number Diff line change
@@ -1,53 +1,44 @@
import Text from "@/helper/text";
import {padStart} from "lodash";
import radix from "@/helper/radix";
import {padStart} from "lodash";
import Text from "@/helper/text";
import crcHandle from 'crc';

// 算法来源: http://www.ip33.com/bcc.html
export class Bcc {
private _dec: number = 0
private _count: number = 0
private readonly _error: string = ""

constructor(input: Text) {
if (input.isError()){
this._error = input.toString()
return;
}
if (!input.isAscii()) {
this._error = "Input Error"
return;
}
input.toHexArray().forEach(item => {
this._dec = this._dec ^ parseInt(item, 16);
this._count++;
})
}

isError() {
return this._error !== ""
}
export type CrcType = keyof typeof crcHandle;
export const crcTypeLists = Object.keys(crcHandle) as CrcType[]

get error() {
return this._error
}
export const bcc = (text: Text) => {
let result = 0
text.toHexArray().forEach(item => {
result ^= parseInt(item, 16);
})
return result;
}

get dec() {
return `${this._dec}`
}
export const lrc = (text: Text) => {
let result = 0
text.toHexArray().forEach(item => {
result += parseInt(item, 16);
})
return 256 - (result % 256);
}

get oct() {
return radix(this._dec, 10, 8)
}
export const crc = async (text: Text, type: CrcType) => {
const handle = await import('crc');
return handle[type](text.toBuffer())
}

get hex() {
return padStart(radix(this._dec, 10, 16), 2, "0")
export const result = (value: number, type: string) => {
type = type.toLowerCase()
if (type === "oct") {
return radix(value, 10, 8)
}

get bin() {
return padStart(radix(this._dec, 10, 2), 8, "0")
if (type === "hex") {
const temp = radix(value, 10, 16)
return padStart(temp, Math.ceil(temp.length / 2) * 2, "0")
}

get count() {
return `${this._count} Bytes`;
if (type === "bin") {
const temp = radix(value, 10, 2)
return padStart(temp, Math.ceil(temp.length / 8) * 8, "0")
}
return `${value}`
}
12 changes: 12 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.