当前位置: 首页 > article >正文

Electron使用WebAssembly实现CRC-32 常用标准校验

Electron使用WebAssembly实现CRC-32 常用标准校验

将C/C++语言代码,经由WebAssembly编译为库函数,可以在JS语言环境进行调用。这里介绍在Electron工具环境使用WebAssembly调用CRC-32 常用标准格式校验的方式。

CRC-32 常用标准校验函数WebAssembly源文件

C语言实现CRC-32 常用标准格式校验的介绍见:《C语言标准CRC-32校验函数》

选择上面介绍文章中的uint32_t PY_CRC_32_M(uint8_t *di, uint32_t len)校验函数,建立一个新文件PY_CRC_32_M.cc:

#ifndef EM_PORT_API
#	if defined(__EMSCRIPTEN__)
#		include <emscripten.h>
#		if defined(__cplusplus)
#			define EM_PORT_API(rettype) extern "C" rettype EMSCRIPTEN_KEEPALIVE
#		else
#			define EM_PORT_API(rettype) rettype EMSCRIPTEN_KEEPALIVE
#		endif
#	else
#		if defined(__cplusplus)
#			define EM_PORT_API(rettype) extern "C" rettype
#		else
#			define EM_PORT_API(rettype) rettype
#		endif
#	endif
#endif

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>

EM_PORT_API(void *) mymalloc(uint32_t size) {
	return malloc(size);
}

EM_PORT_API(void) myfree(void * ptr) {
	 free(ptr);
}

EM_PORT_API(uint32_t) PY_CRC_32_M(uint8_t *di, uint32_t len)
{
    uint32_t crc_poly = 0xEDB88320;  //Inversion bit sequence of 0x04C11DB7
	uint32_t data_t = 0xFFFFFFFF; //initial value

	for(uint32_t i=0; i<len; i++)
	{
		data_t ^=  di[i];
        for (int8_t j = 8; j > 0; --j)
        {
        	data_t = (data_t >> 1) ^ ((data_t & 1)? crc_poly: 0);
        }
	}
	return data_t ^ 0xFFFFFFFF;
}



这个文件有三个函数导出,前两个是获取和释放内存的函数,后一个就是CRC-32 常用标准校验函数的导出。

将这个文件进行WebAssembly编译,就会得到两个库文件:
在这里插入图片描述

将这几个文件拷贝到后面建立的Electron工程目录,再进行调用。

Electron调用WebAssembly CRC-32 常用标准函数演示源文件

下载Electron的Hello World!例程,并实现正常运行:
在这里插入图片描述

然后将前面的3个WebAssembly相关文件,放到例程根目录。再引入一个jQuery库。编写index.html文件如下:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <!-- https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP -->
    <link href="./styles.css" rel="stylesheet">
    <title>Hello World!</title>
         <script>
              window.$ = window.jQuery = require('./js/jquery-3.3.1.min.js');
      </script>
  </head>
      <script src="PY_CRC_32_M.js"></script>
      <script src="./mainprocess.js"></script>  
  <body>
    <h1>Hello World!</h1>
    We are using Node.js <span id="node-version"></span>,
    Chromium <span id="chrome-version"></span>,
    and Electron <span id="electron-version"></span>.

    <!-- You can also require other files to run in this process -->
    <script src="./renderer.js"></script>
  </body>
</html>



主要修改部分为引入了jQuery,引入了PY_CRC_32_M.js以及引入了mainprocess.js,mainprocess.js是在例程根目录下新建的工程文件,内容如下:

// This file is required by the index.html file and will
// be executed in the renderer process for that window.
// All of the Node.js APIs are available in this process.

//增加当前运行状态和当前运行进程/函数信息,控制不产生误触发
window.name="mainwindow";   

$(document).ready(function(){
    
      Module.onRuntimeInitialized = function() {
           console.log(Module);
    }
    
    setTimeout(function(){
      var count = 8;
      var ptr = Module._mymalloc(count);
      for (var i = 0; i < count; i++){
          Module.HEAP8[ptr + i] = 1+i;
      }
      console.log(Module._PY_CRC_32_M(ptr, count));
      Module._myfree(ptr);
    
    },2000);   //Delay is a must for Module initialized! 
  
 });
 
 
 process.on('uncaughtException', function (){
 
});
 

mainprocess.js实现了WebAssembly库文件的导入和使用,Module._mymalloc用于申请内存空间,Module._myfree用于释放内存空间,Module.HEAP8[ptr + i] = 1+i;用于给申请到的内存空间从1开始赋值,这里堆空间为8个字节,因此赋值从1到8。Module._PY_CRC_32_M(ptr, count)则进行CRC-32 常用标准校验函数的调用,提供了内存指针和要校验的字节数量。

整个Electron工程环境的文件如下所示:
在这里插入图片描述

Electron调用WebAssembly CRC-32 常用标准函数演示效果

通过在控制台输入 npm start执行Electron工程,打开console显示:
在这里插入图片描述

1070237893是打印出的CRC校验结果,十六进制值为0x3FCA88C5 ,通过在线工具比较验证:

在这里插入图片描述

Electron使用WebAssembly实现CRC-32 常用标准校验演示工程下载

Electron Demo工程下载,包含已编译后的WebAssembly库文件:
在这里插入图片描述

–End–


http://www.kler.cn/a/580643.html

相关文章:

  • Hcaptcha验证码自动识别方案详解
  • 台风信息查询API:数据赋能,守护安全
  • 每天一道算法题【蓝桥杯】【使用最小花费爬楼梯】
  • linux设置pem免密登录和密码登录
  • 【算法day3】寻找两个正序数组的中位数
  • 精选一百道备赛蓝桥杯——2.K倍区间
  • 解释 TypeScript 中的类型系统,如何定义和使用类型?
  • Go语言中位清除运算符的应用场景
  • Linux 内核自定义协议族开发:从 “No buffer space available“ 错误到解决方案
  • php虚拟站点提示No input file specified时的问题及权限处理方法
  • P8662 [蓝桥杯 2018 省 AB] 全球变暖--DFS
  • 【洛谷DFS算法】P1123取数游戏
  • 09 HarmonyOS NEXT 仿uv-ui Tag组件开发教程系列(三)
  • React基础之项目创建
  • 在线json转ArkTs-Harmonyos
  • C 语言数据结构(二):顺序表和链表
  • 项目管理工具 Maven
  • docker学习使用教程
  • 航空发动机叶片检测-三维扫描技术重构精密制造质量体系
  • MQTT(Message Queuing Telemetry Transport,消息队列遥测传输协议)