Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 1 | /*- |
| 2 | * Copyright (c) 2003, 2004 Lev Walkin <vlm@lionet.info>. All rights reserved. |
| 3 | * Redistribution and modifications are permitted subject to BSD license. |
| 4 | */ |
| 5 | #include <asn_internal.h> |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 6 | #include <stdio.h> |
| 7 | #include <assert.h> |
| 8 | #include <errno.h> |
| 9 | |
| 10 | /* |
| 11 | * The XER encoder of any type. May be invoked by the application. |
| 12 | */ |
| 13 | asn_enc_rval_t |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 14 | xer_encode(asn_TYPE_descriptor_t *td, void *sptr, |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 15 | enum xer_encoder_flags_e xer_flags, |
| 16 | asn_app_consume_bytes_f *cb, void *app_key) { |
| 17 | asn_enc_rval_t er, tmper; |
| 18 | const char *mname; |
| 19 | size_t mlen; |
| 20 | int xcan = (xer_flags & XER_F_CANONICAL) ? 1 : 2; |
| 21 | |
Lev Walkin | 942fd08 | 2004-10-03 09:13:02 +0000 | [diff] [blame] | 22 | if(!td || !sptr) goto cb_failed; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 23 | |
Lev Walkin | dc06f6b | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 24 | mname = td->xml_tag; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 25 | mlen = strlen(mname); |
| 26 | |
| 27 | _ASN_CALLBACK3("<", 1, mname, mlen, ">", 1); |
| 28 | |
| 29 | tmper = td->xer_encoder(td, sptr, 1, xer_flags, cb, app_key); |
| 30 | if(tmper.encoded == -1) return tmper; |
| 31 | |
Lev Walkin | 942fd08 | 2004-10-03 09:13:02 +0000 | [diff] [blame] | 32 | _ASN_CALLBACK3("</", 2, mname, mlen, ">\n", xcan); |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 33 | |
Lev Walkin | 942fd08 | 2004-10-03 09:13:02 +0000 | [diff] [blame] | 34 | er.encoded = 4 + xcan + (2 * mlen) + tmper.encoded; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 35 | |
| 36 | return er; |
Lev Walkin | 942fd08 | 2004-10-03 09:13:02 +0000 | [diff] [blame] | 37 | cb_failed: |
| 38 | _ASN_ENCODE_FAILED; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 39 | } |
| 40 | |
Lev Walkin | cc6a910 | 2004-09-23 22:06:26 +0000 | [diff] [blame] | 41 | /* |
Lev Walkin | 11c3e17 | 2004-09-24 21:00:50 +0000 | [diff] [blame] | 42 | * This is a helper function for xer_fprint, which directs all incoming data |
Lev Walkin | cc6a910 | 2004-09-23 22:06:26 +0000 | [diff] [blame] | 43 | * into the provided file descriptor. |
| 44 | */ |
| 45 | static int |
| 46 | xer__print2fp(const void *buffer, size_t size, void *app_key) { |
| 47 | FILE *stream = (FILE *)app_key; |
| 48 | |
| 49 | if(fwrite(buffer, 1, size, stream) != size) |
| 50 | return -1; |
| 51 | |
| 52 | return 0; |
| 53 | } |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 54 | |
| 55 | int |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 56 | xer_fprint(FILE *stream, asn_TYPE_descriptor_t *td, void *sptr) { |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 57 | asn_enc_rval_t er; |
| 58 | |
| 59 | if(!stream) stream = stdout; |
| 60 | if(!td || !sptr) |
| 61 | return -1; |
| 62 | |
Lev Walkin | cc6a910 | 2004-09-23 22:06:26 +0000 | [diff] [blame] | 63 | er = xer_encode(td, sptr, XER_F_BASIC, xer__print2fp, stream); |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 64 | if(er.encoded == -1) |
| 65 | return -1; |
| 66 | |
| 67 | return fflush(stream); |
| 68 | } |