Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
A
asn1c
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
asn1c
Commits
e4589f94
Commit
e4589f94
authored
Jul 25, 2017
by
Lev Walkin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add XER equivalence check
parent
b8933edb
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
188 additions
and
0 deletions
+188
-0
skeletons/xer_encoder.c
skeletons/xer_encoder.c
+166
-0
skeletons/xer_encoder.h
skeletons/xer_encoder.h
+22
-0
No files found.
skeletons/xer_encoder.c
View file @
e4589f94
...
...
@@ -65,3 +65,169 @@ xer_fprint(FILE *stream, asn_TYPE_descriptor_t *td, void *sptr) {
return
fflush
(
stream
);
}
struct
xer_buffer
{
char
*
buffer
;
size_t
buffer_size
;
size_t
allocated_size
;
};
static
int
xer__buffer_append
(
const
void
*
buffer
,
size_t
size
,
void
*
app_key
)
{
struct
xer_buffer
*
xb
=
app_key
;
while
(
xb
->
buffer_size
+
size
+
1
>
xb
->
allocated_size
)
{
size_t
new_size
=
2
*
(
xb
->
allocated_size
?
xb
->
allocated_size
:
64
);
char
*
new_buf
=
MALLOC
(
new_size
);
if
(
!
new_buf
)
return
-
1
;
memcpy
(
new_buf
,
xb
->
buffer
,
xb
->
buffer_size
);
FREEMEM
(
xb
->
buffer
);
xb
->
buffer
=
new_buf
;
xb
->
allocated_size
=
new_size
;
}
memcpy
(
xb
->
buffer
+
xb
->
buffer_size
,
buffer
,
size
);
xb
->
buffer_size
+=
size
;
xb
->
buffer
[
xb
->
buffer_size
]
=
'\0'
;
return
0
;
}
enum
xer_equivalence_e
xer_equivalent
(
struct
asn_TYPE_descriptor_s
*
td
,
void
*
struct1
,
void
*
struct2
,
FILE
*
opt_debug_stream
)
{
struct
xer_buffer
xb1
=
{
0
,
0
,
0
};
struct
xer_buffer
xb2
=
{
0
,
0
,
0
};
asn_enc_rval_t
e1
,
e2
;
asn_dec_rval_t
rval
;
void
*
sptr
=
NULL
;
if
(
!
td
||
!
struct1
||
!
struct2
)
{
if
(
opt_debug_stream
)
{
if
(
!
td
)
fprintf
(
opt_debug_stream
,
"Type descriptor missing
\n
"
);
if
(
!
struct1
)
fprintf
(
opt_debug_stream
,
"Structure 1 missing
\n
"
);
if
(
!
struct2
)
fprintf
(
opt_debug_stream
,
"Structure 2 missing
\n
"
);
}
return
XEQ_FAILURE
;
}
e1
=
xer_encode
(
td
,
struct1
,
XER_F_BASIC
,
xer__buffer_append
,
&
xb1
);
if
(
e1
.
encoded
==
-
1
)
{
if
(
opt_debug_stream
)
{
fprintf
(
stderr
,
"XER Encoding of %s failed
\n
"
,
td
->
name
);
}
FREEMEM
(
xb1
.
buffer
);
return
XEQ_ENCODE1_FAILED
;
}
e2
=
xer_encode
(
td
,
struct2
,
XER_F_BASIC
,
xer__buffer_append
,
&
xb2
);
if
(
e2
.
encoded
==
-
1
)
{
if
(
opt_debug_stream
)
{
fprintf
(
stderr
,
"XER Encoding of %s failed
\n
"
,
td
->
name
);
}
FREEMEM
(
xb1
.
buffer
);
FREEMEM
(
xb2
.
buffer
);
return
XEQ_ENCODE1_FAILED
;
}
if
(
xb1
.
buffer_size
!=
xb2
.
buffer_size
||
memcmp
(
xb1
.
buffer
,
xb2
.
buffer
,
xb1
.
buffer_size
)
!=
0
)
{
if
(
opt_debug_stream
)
{
fprintf
(
opt_debug_stream
,
"Structures XER-encoded into different byte streams:
\n
=== "
"Structure 1 ===
\n
%s
\n
=== Structure 2 ===
\n
%s
\n
"
,
xb1
.
buffer
,
xb2
.
buffer
);
}
FREEMEM
(
xb1
.
buffer
);
FREEMEM
(
xb2
.
buffer
);
return
XEQ_DIFFERENT
;
}
else
{
if
(
opt_debug_stream
)
{
fprintf
(
opt_debug_stream
,
"Both structures encoded into the same XER byte stream "
"of size %zu:
\n
%s"
,
xb1
.
buffer_size
,
xb1
.
buffer
);
}
}
rval
=
xer_decode
(
NULL
,
td
,
(
void
**
)
&
sptr
,
xb1
.
buffer
,
xb1
.
buffer_size
);
switch
(
rval
.
code
)
{
case
RC_OK
:
break
;
case
RC_WMORE
:
if
(
opt_debug_stream
)
{
fprintf
(
opt_debug_stream
,
"Structure %s XER decode unexpectedly requires "
"more data:
\n
%s
\n
"
,
td
->
name
,
xb1
.
buffer
);
}
/* Fall through */
case
RC_FAIL
:
default:
if
(
opt_debug_stream
)
{
fprintf
(
opt_debug_stream
,
"Structure %s XER decoding resulted in failure.
\n
"
,
td
->
name
);
}
ASN_STRUCT_FREE
(
*
td
,
sptr
);
FREEMEM
(
xb1
.
buffer
);
FREEMEM
(
xb2
.
buffer
);
return
XEQ_DECODE_FAILED
;
}
if
(
rval
.
consumed
!=
xb1
.
buffer_size
&&
((
rval
.
consumed
>
xb1
.
buffer_size
)
||
xer_whitespace_span
(
xb1
.
buffer
+
rval
.
consumed
,
xb1
.
buffer_size
-
rval
.
consumed
)
!=
(
xb1
.
buffer_size
-
rval
.
consumed
)))
{
if
(
opt_debug_stream
)
{
fprintf
(
opt_debug_stream
,
"Round-trip decode of %s required less bytes (%zu) than "
"encoded (%zu)
\n
"
,
td
->
name
,
rval
.
consumed
,
xb1
.
buffer_size
);
}
ASN_STRUCT_FREE
(
*
td
,
sptr
);
FREEMEM
(
xb1
.
buffer
);
FREEMEM
(
xb2
.
buffer
);
return
XEQ_ROUND_TRIP_FAILED
;
}
/*
* Reuse xb2 to encode newly decoded structure.
*/
FREEMEM
(
xb2
.
buffer
);
memset
(
&
xb2
,
0
,
sizeof
(
xb2
));
e2
=
xer_encode
(
td
,
sptr
,
XER_F_BASIC
,
xer__buffer_append
,
&
xb2
);
if
(
e2
.
encoded
==
-
1
)
{
if
(
opt_debug_stream
)
{
fprintf
(
stderr
,
"XER Encoding of round-trip decode of %s failed
\n
"
,
td
->
name
);
}
ASN_STRUCT_FREE
(
*
td
,
sptr
);
FREEMEM
(
xb1
.
buffer
);
FREEMEM
(
xb2
.
buffer
);
return
XEQ_ROUND_TRIP_FAILED
;
}
ASN_STRUCT_FREE
(
*
td
,
sptr
);
sptr
=
0
;
if
(
xb1
.
buffer_size
!=
xb2
.
buffer_size
||
memcmp
(
xb1
.
buffer
,
xb2
.
buffer
,
xb1
.
buffer_size
)
!=
0
)
{
if
(
opt_debug_stream
)
{
fprintf
(
opt_debug_stream
,
"XER Encoding of round-trip decode of %s resulted in "
"different byte stream:
\n
"
"=== Original ===
\n
%s
\n
"
"=== Round-tripped ===
\n
%s
\n
"
,
xb1
.
buffer
,
xb2
.
buffer
,
td
->
name
);
}
FREEMEM
(
xb1
.
buffer
);
FREEMEM
(
xb2
.
buffer
);
return
XEQ_ROUND_TRIP_FAILED
;
}
return
XEQ_SUCCESS
;
}
skeletons/xer_encoder.h
View file @
e4589f94
...
...
@@ -30,6 +30,28 @@ asn_enc_rval_t xer_encode(struct asn_TYPE_descriptor_s *type_descriptor,
void
*
app_key
/* Arbitrary callback argument */
);
/*
* A helper function that uses XER encoding/decoding to verify that:
* - Both structures encode into the same BASIC XER.
* - Both resulting XER byte streams can be decoded back.
* - Both decoded structures encode into the same BASIC XER (round-trip).
* All of this verifies equivalence between structures and a round-trip.
* ARGUMENTS:
* (opt_debug_stream) - If specified, prints ongoing details.
*/
enum
xer_equivalence_e
{
XEQ_SUCCESS
,
/* The only completely positive return value */
XEQ_FAILURE
,
/* General failure */
XEQ_ENCODE1_FAILED
,
/* First sructure XER encoding failed */
XEQ_ENCODE2_FAILED
,
/* Second structure XER encoding failed */
XEQ_DIFFERENT
,
/* Structures encoded into different XER */
XEQ_DECODE_FAILED
,
/* Decode of the XER data failed */
XEQ_ROUND_TRIP_FAILED
/* Bad round-trip */
};
enum
xer_equivalence_e
xer_equivalent
(
struct
asn_TYPE_descriptor_s
*
type_descriptor
,
void
*
struct1
,
void
*
struct2
,
FILE
*
opt_debug_stream
);
/*
* The variant of the above function which dumps the BASIC-XER (XER_F_BASIC)
* output into the chosen file pointer.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment