C Configuration
Add the C compiler block to asn1.avn:
compilers {
{
id "asncc",
language "c",
options {
output-folder "c",
mapping {
integer automatic,
size-of-int 4,
size-of-long 8,
size-of-xlong 8,
real automatic
},
using-module-name-as-type-prefix false,
custom-type-prefix "",
dont-generate-names false,
generate {
constraints false,
sample-test false,
frees false,
equals false,
prints false
}
}
}
}
Integration with Make
Generated C output goes to MyProject/build/c/. A typical Makefile looks like:
ASNRT = /path/to/c/asnrt
CFLAGS = -I$(ASNRT)/include -Ibuild/c
LDFLAGS = -L$(ASNRT) -lasnrt
SRCS = $(wildcard build/c/*.c) main.c
OBJS = $(SRCS:.c=.o)
test: $(OBJS)
\t$(CC) -o $@ $^ $(LDFLAGS)
clean:
\trm -f $(OBJS) test
Generated types provide encode_TypeName and decode_TypeName functions accepting an encoding rule constant (BASIC_ENCODING_RULES, CANONICAL_ENCODING_RULES, UNALIGNED_PACKED_ENCODING_RULES, ALIGNED_PACKED_ENCODING_RULES, OCTET_ENCODING_RULES, XML_ENCODING_RULES, JER).
Code Example
#include "stdio.h"
#include "GetRequest.h"
#include "AcceptTypes_standards.h"
int main(void) {
struct GetRequest req = {0};
AsnBuffer* buffer = NULL;
Bits* standards = NULL;
int rc = 0;
req.header_only = true;
req.lock = false;
req.url = "www.asnlab.org";
req.timestamp.year = 2012;
req.timestamp.month = 12;
req.timestamp.day = 21;
req.timestamp.hour = 12;
req.timestamp.minute = 12;
req.timestamp.second = 21;
req.timestamp.hour_offset = 0;
req.timestamp.minute_offset = 0;
req.timestamp.millisec = 0;
standards = ASN_ALLOC(1, Bits);
standards->length = 4;
standards->bytes = ASN_ALLOC(1, char);
standards->bytes[0] = 0;
setBit(standards, acceptTypes_standards_html);
setBit(standards, acceptTypes_standards_plain_text);
req.accept_types.standards = standards;
char rules[] = { BASIC_ENCODING_RULES, UNALIGNED_PACKED_ENCODING_RULES };
for (unsigned int rule_index = 0; rule_index < sizeof(rules); ++rule_index) {
char encoding_rules = rules[rule_index];
buffer = alloc_buffer(1024, true, encoding_rules);
rc = encode_object(buffer, &req, &GETREQUEST_TYPE);
if (rc == 0) {
for (unsigned long i = 0; i < buffer->limit; i++)
printf("%02X ", (unsigned char) buffer->array[i]);
putchar('\n');
}
free_buffer(buffer);
}
asn_free(standards->bytes);
asn_free(standards);
return 0;
}
C++ Configuration
Add the C++ compiler block to asn1.avn:
compilers {
{
id "asncpp",
language "c++",
options {
output-folder "c++",
mapping {
integer automatic,
real automatic,
choice union,
size-of-int 2,
size-of-long 4,
size-of-xlong 8
},
cpp-standard cpp17,
nullable-member shared-pointer,
using-module-name-as-namespace true,
namespace "",
precompiled-header "",
generate {
compressed-codes false,
constraints false,
sample-test false,
copy-constructor false,
clone false,
frees true,
equals false,
prints false
}
}
}
}
Integration with Make
Generated C++ output goes to MyProject/build/c++/. A typical Makefile looks like:
CXX = g++
ASNRT = /path/to/cpp/asnrt
CXXFLAGS = -std=c++17 -I$(ASNRT)/include -Ibuild/c++
LDFLAGS = -L$(ASNRT) -lasnrt
SRCS = $(wildcard build/c++/*.cpp) main.cpp
OBJS = $(SRCS:.cpp=.o)
test: $(OBJS)
\t$(CXX) -o $@ $^ $(LDFLAGS)
clean:
\trm -f $(OBJS) test
Generated types provide encode and decode methods accepting an encoding rule constant. Use encode_object / decode_object free functions for non-member encoding.
Code Example
#include "GetRequest.h"
#include
#include
using namespace std;
using namespace asnrt;
using namespace myhttp;
int main(void) {
AsnBuffer* buffer = alloc_buffer(1024, true, BASIC_ENCODING_RULES);
GetRequest* getRequest = new GetRequest();
getRequest->header_only = true;
getRequest->lock = false;
getRequest->accept_types.standards = new bitstring();
getRequest->accept_types.standards->push_back(true);
getRequest->accept_types.standards->push_back(true);
getRequest->accept_types.standards->push_back(false);
getRequest->accept_types.standards->push_back(false);
getRequest->accept_types.others = NULL;
getRequest->url = "www.asnlab.org";
getRequest->timestamp.fromUTCTime(2012, 12, 21, 12, 12, 21, 0);
encode_object(buffer, getRequest, GetRequest::TYPE, &GetRequest::CONVERTER);
octetstring octets;
buffer_content(buffer, &octets);
for (unsigned long i = 0; i < octets.length(); i++) {
cout << setfill('0') << setw(2) << hex << uppercase
<< (0xFF & octets.at(i)) << " ";
}
cout << endl;
delete getRequest;
free_buffer(buffer);
return 0;
}
Java Configuration
Add the Java compiler block to asn1.avn:
compilers {
{
id "asnjc",
language "java",
options {
output-folder "java",
mapping {
integer automatic,
enumerated automatic,
real double,
list automatic
},
lower-case-package true,
package-prefix "com.example",
generate {
sample-test false,
setters-getters false,
clones false,
equals true,
prints false
}
}
}
}
Integration with Maven/Gradle
Generated Java output goes to MyProject/build/java/ with packages derived from module names. Add the asnrt JAR to your classpath.
Generated types provide TYPE and CONV static fields. Call TYPE.encode() and TYPE.decode() with a Buffer and converter. Supported encoding rules: BASIC_ENCODING_RULES, CANONICAL_ENCODING_RULES, UNALIGNED_PACKED_ENCODING_RULES, ALIGNED_PACKED_ENCODING_RULES, OCTET_ENCODING_RULES, XML_ENCODING_RULES, JSON_ENCODING_RULES.
Code Example
import java.util.Calendar;
import java.util.TimeZone;
import org.asnlab.asndt.runtime.conv.EncodingRules;
import org.asnlab.asndt.runtime.type.Buffer;
import myhttp.AcceptTypes;
import myhttp.AcceptTypes_standards;
import myhttp.GetRequest;
public class TestMyHttp {
public static void main(String[] args) throws Exception {
GetRequest getRequest = new GetRequest();
getRequest.header_only = true;
getRequest.lock = false;
getRequest.accept_types = new AcceptTypes();
getRequest.accept_types.standards = new AcceptTypes_standards(new byte[1], (byte) 4);
getRequest.accept_types.standards.setHtml();
getRequest.accept_types.standards.setPlain_text();
getRequest.url = "www.asnlab.org";
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
cal.set(2012, Calendar.DECEMBER, 21, 12, 12, 21);
cal.set(Calendar.MILLISECOND, 0);
getRequest.timestamp = cal.getTime();
for (byte encodingRules : new byte[] {
EncodingRules.BASIC_ENCODING_RULES,
EncodingRules.UNALIGNED_PACKED_ENCODING_RULES }) {
Buffer buffer = Buffer.allocate(1024, encodingRules);
GetRequest.TYPE.encode(getRequest, buffer, GetRequest.CONV);
byte[] bs = buffer.array();
for (byte b : bs) {
System.out.printf("%02X ", b & 0xFF);
}
System.out.println();
}
}
}
C# Configuration
Add the C# compiler block to asn1.avn:
compilers {
{
id "asncsc",
language "c#",
options {
output-folder "c#",
mapping {
integer automatic,
real double
},
lower-case-namespace true,
namespace-prefix "MyCompany.Protocols",
generate {
constraints false,
sample-test false,
properties false,
clones false,
equals true,
prints false,
hash-code true
}
}
}
}
Integration with .NET
Generated C# output goes to MyProject/build/c#/. Add the ASN.1 C# runtime library to your .NET project references. Generated types provide TYPE and CONV static fields. Call TYPE.Encode() and TYPE.Decode() with an AsnBuffer and converter.
Code Example
using System;
using System.Collections;
using System.IO;
using myhttp;
using org.asnlab.asndt.asnrt.conv;
using org.asnlab.asndt.asnrt.type;
class TestMyHttp {
static void Main() {
Directory.SetCurrentDirectory(AppContext.BaseDirectory);
GetRequest getRequest = new GetRequest();
getRequest.header_only = true;
getRequest.lock_ = false;
getRequest.accept_types = new AcceptTypes();
BitArray standards = new BitArray(4);
standards[AcceptTypes_standards.html] = true;
standards[AcceptTypes_standards.plain_text] = true;
getRequest.accept_types.standards = standards;
getRequest.url = "www.asnlab.org";
getRequest.timestamp = new DateTime(2012, 12, 21, 12, 12, 21, DateTimeKind.Utc);
foreach (byte encodingRules in new byte[] {
EncodingRules.BASIC_ENCODING_RULES,
EncodingRules.UNALIGNED_PACKED_ENCODING_RULES }) {
AsnBuffer buffer = AsnBuffer.Allocate(1024, encodingRules);
GetRequest.TYPE.Encode(getRequest, buffer, GetRequest.CONV);
foreach (byte b in buffer.Array()) {
Console.Write("{0:X2} ", b);
}
Console.WriteLine();
}
}
}
Go Configuration
Add the Go compiler block to asn1.avn:
compilers {
{
id "asngc",
language "go",
options {
output-folder "go",
mapping {
integer automatic-int64,
real automatic
},
generate {
constraints false,
sample-test false,
clones false,
equals true,
prints false
}
}
}
}
Integration with Go Modules
Generated Go output goes to MyProject/build/go/. Import the generated Go package and the Go runtime library in your module. Obtain the module runtime via GetRuntime(), then use runtime.Encode() and runtime.Decode() with a buffer.
Code Example
package main
import (
"fmt"
"time"
"myhttp-test/myhttp"
"github.com/asnlab/asnrt"
)
func main() {
req := &myhttp.GetRequest{
Header_only: true,
Lock: false,
Accept_types: myhttp.AcceptTypes{
Standards: myhttp.NewAcceptTypes_standards(4),
},
Url: "www.asnlab.org",
Timestamp: time.Date(2012, 12, 21, 12, 12, 21, 0, time.UTC),
}
req.Accept_types.Standards.SetHtml(true)
req.Accept_types.Standards.SetPlain_text(true)
runtime := myhttp.GetRuntime()
for _, encodingRules := range []byte{
asnrt.BASIC_ENCODING_RULES,
asnrt.UNALIGNED_PACKED_ENCODING_RULES,
} {
buffer, err := runtime.Allocate(1024, encodingRules)
if err != nil {
panic(err)
}
if err := runtime.Encode(buffer, req, myhttp.GETREQUEST_TYPE); err != nil {
panic(err)
}
for _, b := range buffer.Array() {
fmt.Printf("%02X ", b)
}
fmt.Println()
}
}