OpenEuler树莓派基础实验(无树莓派)

2021/11/11 6:09:56

本文主要是介绍OpenEuler树莓派基础实验(无树莓派),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

OpenSSL编译安装

1.去OpenSSL官网下载最新版本OpenSSL 1.1.1l的源码openssl-1.1.lk.tar.gz,然后把代码上传到openEuler云服务器中。


2.建立两个文件夹,分别放置OpenSSL的源码和安装路径,记住pwd运行的结果

3.解压源代码到rocopensslsrc文件夹:

4.查看安装后的版本,确定是最新安装的1.1.1l版

OpenSSL编程

简单测试

编写一个测试代码test_openssl.c:

#include <stdio.h>
#include <openssl/evp.h>

int main(){
	
	OpenSSL_add_all_algorithms();
	
	return 0;
}

然后编译运行:

BASE64算法

BASE64的测试代码testbase64.c :

#include <stdio.h>
#include <string.h>
#include <openssl/evp.h>
#include <openssl/x509.h>

//Base64编码
void tEVP_Encode()
{
	EVP_ENCODE_CTX *ctx;
ctx = EVP_ENCODE_CTX_new();	//EVP编码结构体
	unsigned char in[1024];			//输入数据缓冲区
	int inl;						//输入数据长度
	char out[2048]={0};				//输出数据缓冲区
	int outl;						//输出数据长度
	FILE *infp;						//输入文件句柄
	FILE *outfp;					//输出文件句柄

	infp = fopen("test.dat","rb");//打开待编码的文件
	if(infp == NULL)
	{
		printf("Open File \"Test.dat\"  for Read Err.\n");
		return;
	}
	
	outfp = fopen("test.txt","w");//打开编码后保存的文件
	if(outfp == NULL)
	{
		printf("Open File \"test.txt\" For Write Err.\n");
		return;
	}
	EVP_EncodeInit(ctx);//Base64编码初始化
	printf("文件\"Test.dat\" Base64编码后为:\n");
	//循环读取原文,并调用EVP_EncodeUpdate计算Base64编码
	while(1)
	{
		inl = fread(in,1,1024,infp);
		if(inl <= 0)
			break;
		EVP_EncodeUpdate(ctx,out,&outl,in,inl);//编码
		fwrite(out,1,outl,outfp);//输出编码结果到文件
		printf("%s",out);
	} 
	EVP_EncodeFinal(ctx,out,&outl);//完成编码,输出最后的数据。
	fwrite(out,1,outl,outfp);
	printf("%s",out);
	fclose(infp);
	fclose(outfp);	
	printf("对文件\"Test.dat\" Base64编码完成,保存到\"test.txt\"文件.\n\n\n");
}

//Base64解码
void tEVP_Decode()
{
	EVP_ENCODE_CTX *ctx;
ctx = EVP_ENCODE_CTX_new();			//EVP编码结构体
	char in[1024];					//输入数据缓冲区
	int inl;						//输入数据长度
	unsigned char out[1024];		//输出数据缓冲区
	int outl;						//输出数据长度
	FILE *infp;						//输入文件句柄
	FILE *outfp;					//输出文件句柄
	
	infp = fopen("test.txt","r");//打开待解码的文件
	if(infp == NULL)
	{
		printf("Open File \"Test.txt\"  for Read Err.\n");
		return;
	}
	outfp = fopen("test-1.dat","wb");//打开解码后保存的文件
	if(outfp == NULL)
	{
		printf("Open File \"test-1.txt\" For Write Err.\n");
		return;
	}
	EVP_DecodeInit(ctx);//Base64解码初始化
	printf("开始对文件\"Test.txt\" Base64解码...\n\n");
	//循环读取原文,并调用EVP_DecodeUpdate进行Base64解码
	while(1)
	{
		inl = fread(in,1,1024,infp);
		if(inl <= 0)
			break;
		EVP_DecodeUpdate(ctx,out,&outl,in,inl);//Base64解码
		fwrite(out,1,outl,outfp);//输出到文件
	} 
	EVP_DecodeFinal(ctx,out,&outl);//完成解码,输出最后的数据。
	fwrite(out,1,outl,outfp);
	fclose(infp);
	fclose(outfp);	
	printf("对文件\"Test.txt\" Base64解码完成,保存为\"test-1.dat\"\n\n\n");
	
}
 
int main()
{
 
	tEVP_Encode();
	tEVP_Decode();
	
	return 0;
}

编译运行:

测试中的用到一个test.dat 是个二进制文件

实验

实验3-2:汇编语言练习——查找最大数

.section .data
.align 3
my_data:
	.quad 1
	.quad 2
	.quad 5
	.quad 8
	.quad 10
	.quad 12

my_data_count:
	.quad 6

.align 3
print_data:
	.string "big data: %d\n"

.section .text
.globl main
main:
	stp x29, x30, [sp, -16]!

	ldr x0, =my_data
	ldr x1, my_data_count

	add x4, x0, #40

	mov x3, xzr
1:
	ldr x2, [x0], #8
	cmp x2, x3
	csel x3, x2, x3, hi

	cmp x0, x4
	b.ls 1b

	ldr x0, =print_data
	mov x1, x3

	bl printf

	ldp x29, x30, [sp], 16
	ret

编译运行:

调试:

实验3-3:汇编语言练习——通过C语言调用汇编函数

compare.S文件如下:

.section .text 
.globl compare_data 
compare_data: 
	cmp x0, x1 
	csel x0, x0, x1, hi 
	ret       

main.c文件如下:

#include <stdio.h> 
extern int compare_data(int a, int b); 
int main() 
{ 
	int val; 
	val = compare_data(5, 6); 
	printf("big data: %d\n", val); 
}

编译运行:

实验3-4:汇编语言练习——通过汇编语言调用C函数

compare.c文件如下。

int compare_data(int a, int b) 
{ 
	return (a >= b) ? a : b; 
} 

main.S文件如下。

.section .data 
.align 3 

print_data: 
	.string "big data: %d\n" 

.section .text 
.globl main 
main: 
	stp x29, x30, [sp, -16]! 
	mov x0, #6 
	mov x1, #5 
	bl compare_data 
	mov x1, x0 
	ldr x0, =print_data 
	bl printf 
	ldp  x29, x30, [sp], 16
	ret 

编译运行:

实验3-5:汇编语言练习——GCC内联汇编

#include <stdio.h>
static int compare_data(int a, int b)
{ 
	int val;
	asm volatile (
		"cmp %1, %2\n"
		 "csel %0, %1, %2, hi\n"
		 : "+r" (val)
		 : "r" (a), "r" (b)
 		: "memory");
	return val;
} 
int main()
{ 
	int val;
	val = compare_data(5, 6);
 printf("big data: %d\n", val);
	val = compare_data(6, 4);
	printf("big data: %d\n", val);
}

编译运行:



这篇关于OpenEuler树莓派基础实验(无树莓派)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程