Examples of use of the asn1--asn1c, Javaasn1comilper, and Protobuf of the "Cryptography Project"

Source: Internet
Author: User
Tags delete cache git clone

1 using ASN1C to implement encoding

Reference URL: http://lengxuezhixuan.blog.chinaunix.net/uid-28765492-id-3765759.html

PS: This URL is based on the English instruction document in the downloaded asn1c .

* Problems encountered:

(1) Header file Reference: The header file is placed under the Vc/include, the. h file can be used include<>. For the. h file under this project, use the include ""

(2) If the CPP and the C file coexist, there will be a problem with the precompiled header incompatibility, you can set all the source file properties to remove the precompiled header, or adjust all the code to CPP or C

****************************************************************************************************

I do the experiment is to use C code, with Java decoding: The result of the code is output directly to the TXT file, then the use of Java decoding to achieve the different languages of the serialization of data usage (in fact, the ASN file is used, C encoding only need to use the ASN file to generate the series of C files The decoder will not need the ASN any more.)

PS: After a complex, a variety of error types can not find, wasted one hours to find asn-0.9.21 type definition name and the Web is not the same (to go to Sketon inside), good pit. The structure name you design when you encode name is name_t! by typedef

=============================================================

2 using Javaasn1comilper decoding ( refer to the example of the Javaasn1comilper Guidance document )

(1) Testutil is an example, you can not (the structure of the output, you can copy directly); Testparser can also not, need to delete another inherited its class

(2) Create a class for the elements in the structure you have designed, and inherit the corresponding type: Create a class width for rectangle content (inherit Asn1integer)

Package COM.CHAOSINMOTION.ASN1;


public class width extends Asn1integer {

Public width () {//default is 5

Super (5);

}

Public width (Long value) {//Set size

Super (value);

}

}

(3) Create the class of the structure you designed: Create the class rectangle. You must add elements to the structure with super.addelement, otherwise there is nothing in the structure

Package COM.CHAOSINMOTION.ASN1;

public class Berrectangle extends Sequence {

Public Asn1integer length=new width ();

Public Asn1integer width=new width ();

Public Berrectangle () {

Super ();

SetUp ();//You must use AddElement to add content to sequence, otherwise it will not be initialized

}

protected void SetUp () {

Super.addelement (length);

Super.addelement (width);

}

}

(4) Implement the application: Just press the document!

public static void Main (string[] args) {

berrectangle test=new berrectangle ();//Create Object

Test.width.setValue (23);//Initialize Object

Test.length.setValue (42);

//Encode an object

bytearrayoutputstream stream=new bytearrayoutputstream ();//Must Have

beroutputstream out=new Beroutputstream (stream);//Must Have

try {

Test.encode (out);//Must Have

Printdata (Stream.tobytearray ()); Testutil structured output method of//copy

} catch (IOException e) {

TODO auto-generated Catch block

SYSTEM.OUT.PRINTLN ("error");

E.printstacktrace ();

}

Decoding

bytearrayinputstream destream=new bytearrayinputstream (Stream.tobytearray ());

berinputstream in=new berinputstream (Destream);

berrectangle test2=new berrectangle ();//must have, create an entity

try {

Test2.decode (in);//must have, in is actually a coded string of binary, so it can also be read from the file; decode in and place in Test2

System.out.println (test2);//Direct output can be

} catch (IOException e) {

TODO auto-generated Catch block

E.printstacktrace ();

}

System.out.println ("over");

}


=============================================================

3 PROTOBUF Implementation ASN1

First, PROTOBUF installation (System environment: windows)

(Refer to Protobuf source readme.md, no adjustment)

1 Installing CMake

Website: https://cmake.org/download/

I directly download the installer:)

2 Installing Git

Website: Https://git-scm.com/download/win

Similarly, directly download the binary version of, click &finish:)

3 Installing PROTOBUF

3.1 Follow the instructions of the Readme

3.1.1 Setting the use path for CMake and Git

I want to install Protobuf in "f:\ new Folder", set path is setting TEMP environment variable

F:

CD new Folder

Set path=%path%; " C:\Program Files (x86) \cmake\bin "

Set path=%path%; " C:\Program Files\git\cmd "

3.1.2 Get protobuf Source: I downloaded the master version of the

git clone-b master Https://github.com/google/protobuf.git

3.1.3 Get Gmock (anyway protobuf readme says so)

CD Protobuf

git clone-b release-1.7.0 https://github.com/google/googlemock.git gmock

CD Gmock

git clone-b release-1.7.0 https://github.com/google/googletest.git gtest

3.1.4 using CMake to make sln (can be graphical or command-line-my compiler is VS2010)

(1) I use the graphical version of CMake to generate the SLN used by the IDE

Source is Protobuf/cmake;dst is Protobuf/cmake/build/debug (Build/debug is his own path, in order to do a good job of deleting).

Click Delete cache of file before generate, then Configue, then generate.

When you're done, you can find Protobuf.sln in Debug.

(2) The command line generates the SLN:

The statements are as follows:

Cmake-g "Visual Studio ten Win64" ^

-dcmake_build_type=debug ^

-dcmake_install_prefix=. /.. /.. /.. /install ^

.. /.. ^

-dcmake_cxx_compiler= "F:/program Files (x86)/microsoft Visual Studio 10.0/vc/bin/cl.exe" ^

-dcmake_c_compiler= "F:/program Files (x86)/microsoft Visual Studio 10.0/vc/bin/cl.exe"

My IDE is VS2010 Win64 (you can use CMake--help to view the type of IDE supported by CMake); The build type is debug (regardless of file name);

The last two lines are the absolute paths of the C and cxx compiler addresses, which are the cl.exe in the IDE folder;

(If the hint Mspdb100.dll can not find, need to path=%path%; F:\Program Files (x86) \microsoft Visual Studio 10.0\Common7\IDE)

Note: When entering the path, be sure to use the/(CMake syntax) and "" (because the space is segmented, so there are spaces in the path to use "" package).

3.1.5 compile, you can use the IDE to open the Protobuf.sln Direct build solution (because the command line is not good, so the novice uses the IDE)

3.1.6 Install, open Protobuf.sln

(1) First build the entire solution

(2) build three: Libprotobuf,libprotoc,libprotobuf-lite,protoc; the results will be in debug in the Engineering directory (Protoc.exe Libprotobuf-lite.lib Libprotobufd.lib libprotocd.lib)

Ps:readme let build install, but I only use the master version is successful once, after the failure, do not know how to do, but do not affect the application is not the tube (lazy)

3.2 According to the online tutorial is the same, just do not git, directly to the website to download; URL: https://developers.google.com/protocol-buffers/docs/downloads.

The version used in the Internet has vsproject directory, so can be directly 3.1.5, but I downloaded the version there is no ready-made vsproject, need to build, so cmake, gmock or to install, just omitted 2 and 3.1.2

=============================================================

Application of Protobuf (win&&c++)

1 Writing proto Files

I made up a simple example, the specific grammar of what can go online to find.

Message rectangle{

Required int32 height = 1;

Required Int32 width = 2;

}

2 generating. cc and. h files

cmd command: protoc-i=c:\users\chris\desktop\--cpp_out=c:\1 C:\Users\chris\Desktop\rectangle.proto

Description

C:\Users\chris\Desktop\ is the directory where my proto files are located

C:\1 is the target directory

C:\Users\chris\Desktop\rectangle.proto is the absolute path to the proto file

3 Creating a Project

3.1 Adding support files for Protobuf

Libprotobuf-lite.lib Libprotobufd.lib generated when Protobuf/src/google and compiled source Libprotocd.lib

The header file uses <>, so you need to add Google to the Vc/include directory of the compiler (I don't know the meaning of <>, try to change <> to "", but always fail and finally give up)

Add the Libprotobuf-lite.lib libprotobufd.lib libprotocd.lib to the project directory, using an absolute path to load (there are other ways, of course, myself.) Lazy

3.2 Put the previously generated. CC and. h in

3.3 Write the main function to apply the rectangle

#include "Rectangle.pb.h"

#include <iostream>

#include <string>

using namespace Std;

#pragma comment (lib, "C:\\users\\chris\\desktop\\protodemo\\protodemo\\libprotocd.lib")

#pragma comment (lib, "C:\\users\\chris\\desktop\\protodemo\\protodemo\\libprotobufd.lib")

#pragma comment (lib, "C:\\users\\chris\\desktop\\protodemo\\protodemo\\libprotobufd-lite.lib")

int main (int,char**) {

Rectangle Test1,test2;

string data;

Initialize Test1

Test1.set_height (42);

Test1.set_width (23);

Test1. Serializetostring (&data);//encode the test1 and put the result in data


Test2. parsefromstring (data);//decode the sequence data and assign the result to Test2


cout<<data<< "\ n" << "test2\n hieght:" <<test2.height () << "\ n width:" << Test2.width () << "\ n";

return 0;

}

PS: The version of the header file, Lib, and Protoc used to generate the. cc and. h are consistent; the Propertis->c/c++->code generation of the created project is changed to MTD

=============================================================


This article is from the "12746634" blog, please be sure to keep this source http://12756634.blog.51cto.com/12746634/1942056

Examples of use of the asn1--asn1c, Javaasn1comilper, and Protobuf of the "Cryptography Project"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.