ubuntu安裝asn1c編譯器
RectangleTest DEFINITIONS ::= BEGIN Rectangle ::= SEQUENCE { height INTEGER, -- Height of the rectangle width INTEGER -- Width of the rectangle } END
2、對rectangle.asn1進(jìn)行編譯:asn1c -fnative-types rectangle.asn1 ,執(zhí)行完畢后會在該目錄下生成跟編碼相關(guān)的c語言文件
#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 */ rectangle->height = 42; /* any random value */ rectangle->width = 23; /* any random value */ /* 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 */}
Specify filename for BER output<Rectangle><height>42</height><width>23</width></Rectangle>
*博客內(nèi)容為網(wǎng)友個人發(fā)布,僅代表博主個人觀點,如有侵權(quán)請聯(lián)系工作人員刪除。