Commit 5134a58f authored by Martin Florian's avatar Martin Florian

Initial commit

parents
Beispiel zur Verwendung des asn1c
(0) asn1c kompilieren und installieren
-> http://lionet.info/asn1c
(1) ASN.1 Datei kompilieren:
$ asn1c -fnative-types rectangle.asn1
(2) Datei main.c ggf. mit konkreten Werten anpassen
(3) Binary erstellen, welches uns die BER (und XER) Kodierung liefert
$ rm converter-sample.c
$ cc -I. -o rencode *.c
(4) BER-Datei erstellen (in Datei "ber-sample")
$ ./rencode ber-sample
(5) Hexdump der Datei "ber-sample" ausgeben lassen
$ hexdump -C ber-sample
00000000 30 06 02 01 2a 02 01 17 |0...*...|
00000008
#include <stdio.h>
#include <sys/types.h>
#include <Rectangle.h> /* Rectangle ASN.1 type */
/*
* This is a custom function which writes the
* encoded output into some FILE stream.
*/
static int
write_out(const void *buffer, size_t size, void *app_key) {
FILE *out_fp = app_key;
size_t wrote;
wrote = fwrite(buffer, 1, size, out_fp);
return (wrote == size) ? 0 : -1;
}
int main(int ac, char **av) {
Rectangle_t *rectangle; /* Type to encode */
asn_enc_rval_t ec; /* Encoder return value */
/* Allocate the Rectangle_t */
rectangle = calloc(1, sizeof(Rectangle_t)); /* not malloc! */
if(!rectangle) {
perror("calloc() failed");
exit(71); /* better, EX_OSERR */
}
/*
* Initialize the Rectangle members
*/
/* height */
rectangle->height = 42;
/* width */
rectangle->width = 23;
/* BER encode the data if filename is given */
if(ac < 2) {
fprintf(stderr, "Specify filename for BER output\n");
} else {
const char *filename = av[1];
FILE *fp = fopen(filename, "wb"); /* for BER output */
if(!fp) {
perror(filename);
exit(71); /* better, EX_OSERR */
}
/* Encode the Rectangle type as BER (DER) */
ec = der_encode(&asn_DEF_Rectangle,
rectangle, write_out, fp);
fclose(fp);
if(ec.encoded == -1) {
fprintf(stderr,
"Could not encode Rectangle (at %s)\n",
ec.failed_type ? ec.failed_type->name : "unknown");
exit(65); /* better, EX_DATAERR */
} else {
fprintf(stderr, "Created %s with BER encoded Rectangle\n",
filename);
}
}
/* Also print the constructed Rectangle XER encoded (XML) */
xer_fprint(stdout, &asn_DEF_Rectangle, rectangle);
return 0; /* Encoding finished successfully */
}
RectangleModule1 DEFINITIONS ::=
BEGIN
Rectangle ::= SEQUENCE {
height INTEGER,
width INTEGER
}
END
Beispiel zur Verwendung von Google Protocol Buffers
(0) Protocol Buffers kompilieren und installieren
-> https://developers.google.com/protocol-buffers
(1) .proto Datei kompilieren:
$ protoc --cpp_out=. rectangle.proto
(2) Datei main.cc ggf. mit konkreten Werten anpassen
(3) Binary erstellen, welches uns die BER (und XER) Kodierung liefert
$ c++ *.cc -o rencode -lprotobuf
(4) BER-Datei erstellen (in Datei "protobuf")
$ ./rencode protobuf
(5) Hexdump der Datei "protobuf-sample" ausgeben lassen
$ hexdump -C protobuf-sample
00000000 08 2a 10 17 |.*..|
00000004
#include <iostream>
#include <fstream>
#include "rectangle.pb.h"
using namespace std;
int main(int argc, char ** argv) {
if(argc < 2) return -1;
fstream out(argv[1], ios::out | ios::binary | ios::trunc);
if(!out.is_open()) return -1;
Rectangle rect;
rect.set_height(42);
rect.set_width(23);
rect.SerializeToOstream(&out);
out.close();
return 0;
}
message Rectangle {
required int32 height = 1;
required int32 width = 2;
}
<Rectangle>
<height>42</height>
<width>23</width>
</Rectangle>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment