The best way of learning any language or notation is to read some examples of it. This example is adapted from http://www.w3.org/Protocols/HTTP-NG/asn1.html, it's a simplified form of the the GET request from FHTTP.
For our example, suppose that we need to employ the following ASN.1 module MyHTTP.asn:
MyHTTP DEFINITIONS AUTOMATIC TAGS ::= BEGIN GetRequest ::= SEQUENCE { header-only BOOLEAN, lock BOOLEAN, accept-types AcceptTypes, url Url, ..., timestamp GeneralizedTime } AcceptTypes ::= SET { standards BIT STRING { html(0), plain-text(1), gif(2), jpeg(3) } (SIZE(4)) OPTIONAL, others SEQUENCE OF VisibleString (SIZE(4)) OPTIONAL } Url ::= VisibleString (FROM("a".."z"|"A".."Z"|"0".."9"|"./-_~%#")) myRequest GetRequest ::= { header-only TRUE, lock FALSE, accept-types { standards { html, plain-text } }, url "www.asnlab.org", timestamp "20100202020202Z" } END
1. Select File > New > Project...
2. Under ASN.1 category, select ASN.1 Project, then click Next.
3. For project name, enter 'MyHTTP' and click Finish.
4. ASN.1 files may now be added to the project by copying to the source folder, or creating ASN.1 files from scratch by
5. Select File > New > Other...
6. Under the ASN.1 category, select ASN.1 Module, then click Next.
7. For module name, enter 'MyHTTP' and click Finish.
8. Enter the above content in the opening editor.
9. Save the ASN.1 file, then the compiled Java class files will be automatically generated.
10. Create a Java project test, and add the ASN.1 Java Runtime to the build path.
11. Copy the generated Java class files into proper package ("MyHTTP" in this case) folder in the project.
12. Create a TestMyHttp.java file in the project with the following contents:
import java.io.ByteArrayOutputStream; import java.util.Calendar; import MyHTTP.AcceptTypes; import MyHTTP.GetRequest; import MyHTTP.Standards; 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 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(); cal.set(Calendar.YEAR, 2010); cal.set(Calendar.MONTH, Calendar.FEBRUARY); cal.set(Calendar.DAY_OF_MONTH, 2); cal.set(Calendar.HOUR_OF_DAY, 2); cal.set(Calendar.MINUTE, 2); cal.set(Calendar.SECOND, 2); cal.set(Calendar.MILLISECOND, 0); cal.set(Calendar.ZONE_OFFSET, 0); getRequest.timestamp = cal.getTime(); ByteArrayOutputStream bos=new ByteArrayOutputStream(); getRequest.ber_encode(bos); byte[] bs=bos.toByteArray(); for(int i=0; i<bs.length; i++) { System.out.printf("%02X ", bs[i] & 0xFF); } } }
13. Compile the project if the Build automatically is turn off.
14. Right click the file TestMyHttp.java and select Run as > Java Application:
30 2D 80 01 FF 81 01 00 A2 04 80 02 04 C0 83 0E 77 77 77 2E 61 73 6E 6C 61 62 2E 63 6F 6D 84 0F 32 30 31 30 30 32 30 32 30 32 30 32 30 32 5A
Here's the explanation of encoded bits and bytes in BER:
0x30 -- [0011|0000], [UNIVERSAL, CONSTRUCTED, 16(SEQUENCE)] - GetRequest 0x2D -- [0010|1101], length 45 0x80 -- [1000|0000], [CONTEXT, PRIMITIVE, 0(BOOLEAN)] GetRequest.header_only 0x01 -- [0000|0001], length 1 0xFF -- [0000|1111], value TRUE 0x81 -- [1000|0001], [CONTEXT, PRIMITIVE, 1(BOOLEAN)] GetRequest.lock 0x01 -- [0000|0001], length 1 0x00 -- [0000|0000], value FALSE 0xA2 -- [1010|0010], [CONTEXT, CONSTRUCTED, 2(SET)] - GetRequest.accept_types 0x04 -- [0000|0100], length 4 0x80 -- [1000|0000], [CONTEXT, PRIMITIVE, 0(BIT STRING)] AcceptTypes.standards 0x02 -- [0000|0010], length 2 0x04 -- [0000|0100], 4 unused bits 0xC0 -- [1100|0000], {html, plaint_text} 0x83 -- [1000|0011], [CONTEXT, PRIMITIVE, 3(VisibleString)] GetRequest.url 0x0E -- [0000|1100], length 14 0x77 0x77 0x77 0x2E 0x61 0x73 0x6E 0x6C 0x61 0x62 0x2E 0x63 0x6F 0x6D -- www.asnlab.org 0x84 -- [1000|0011], [CONTEXT, PRIMITIVE, 4(GeneralizedTime)] GetRequest.timestamp 0x0F -- [0000|1100], length 15 0x32 0x30 0x31 0x30 0x30 0x32 0x30 0x32 0x30 0x32 0x30 0x32 0x30 0x32 0x5A -- 20100202020202Z