Implement and test ASN.1 BER visitors
v5 cover Implement and test ASN.1 BER visitors
This patch series implements ASN.1 BER visitors for encoding and decoding of data into byte streams.
This is the last post of this series with some issues fixed, the visitor_optional implemented and all possible tests in test-visitor-serialization passing.
Stefan
Stefan Berger (8):
Move some contents of savevm.c to qemu-file.c 3 new file wrappers
QEMUSizedBuffer
QAPI: add type_sized_buffer
ASN.1 output visitor
ASN.1 input visitor
Extend test-visitor-serialization with ASN.1 visitor(s) ASN.1 specific test cases
configure | 2 +- include/migration/qemu-file.h | 22 + include/qapi/ber-input-visitor.h | 30 + include/qapi/ber-output-visitor.h | 32 + include/qapi/ber.h | 113 ++++ include/qapi/visitor-impl.h | 3 + include/qapi/visitor.h | 3 + include/qemu-common.h | 14 + qapi/Makefile.objs | 1 + qapi/ber-common.c | 86 +++ qapi/ber-common.h | 29 + qapi/ber-input-visitor.c | 1153 ++++++++++++++++++++++++++++++++++ qapi/ber-output-visitor.c | 705 +++++++++++++++++++++ qapi/qapi-visit-core.c | 8 + savevm.c | 690 -------------------- tests/Makefile | 15 +- tests/test-ber-visitor.c | 894 ++++++++++++++++++++++++++ tests/test-visitor-serialization.c | 78 +++ util/Makefile.objs | 1 + util/qemu-file.c | 1214 ++++++++++++++++++++++++++++++++++++ 20 files changed, 4401 insertions(+), 692 deletions(-) create mode 100644 include/qapi/ber-input-visitor.h create mode 100644 include/qapi/ber-output-visitor.h create mode 100644 include/qapi/ber.h create mode 100644 qapi/ber-common.c create mode 100644 qapi/ber-common.h create mode 100644 qapi/ber-input-visitor.c create mode 100644 qapi/ber-output-visitor.c create mode 100644 tests/test-ber-visitor.c create mode 100644 util/qemu-file.c
Signature · 1 line
1.7.11.7
PATCH v5 1/8 Move some contents of savevm.c to qemu-file.c
This patch reorganizes qemu file operations to be in their own source file instead of being lumped in savevm.c. Besides being more logical for maintenance it also makes it easier for future users of the file functions to add tests.
--- include/migration/qemu-file.h | 6 + savevm.c | 690 ---------------------------------------- util/Makefile.objs | 1 + util/qemu-file.c | 715 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 722 insertions(+), 690 deletions(-) create mode 100644 util/qemu-file.c
include/migration/qemu-file.h +6 -0
@@ -98,6 +98,12 @@ void qemu_file_reset_rate_limit(QEMUFile *f);void qemu_file_set_rate_limit (QEMUFile *f,int64_t new_rate);int64_t qemu_file_get_rate_limit (QEMUFile *f);int qemu_file_get_error (QEMUFile *f); +void qemu_file_set_error (QEMUFile *f,int ret); +QEMUFile *qemu_fopen_bdrv (BlockDriverState *bs,int is_writable); +int qemu_peek_buffer (QEMUFile *f,uint8_t *buf,int size,size_t offset); +int qemu_peek_byte (QEMUFile *f,int offset); +void qemu_file_skip (QEMUFile *f,int size); +void qemu_fflush (QEMUFile *f);static inline void qemu_put_be64s (QEMUFile *f,const uint64_t *pv) {
savevm.c +0 -690
696 lines not on this page · raw message
util/Makefile.objs +1 -0
@@ -10,3 +10,4 @@ util-obj-$(CONFIG_POSIX) += compatfd.o util-obj-y += iov.o aes.o qemu-config.o qemu-sockets.o uri.o notify.o util-obj-y += qemu-option.o qemu-progress.o util-obj-y += hexdump.o +util-obj-y += qemu-file.o
util/qemu-file.c new file +715 -0
715 lines not on this page · raw message
Signature · 1 line
1.7.11.7
PATCH v5 2/8 3 new file wrappers
In reply to Stefan BergerAdd 3 very short file wrapper functions to make code that follows more readable.
--- include/migration/qemu-file.h | 3 +++ util/qemu-file.c | 30 ++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+)
include/migration/qemu-file.h +3 -0
@@ -68,6 +68,9 @@ int qemu_fclose(QEMUFile *f);int64_t qemu_ftell (QEMUFile *f);void qemu_put_buffer (QEMUFile *f,const uint8_t *buf,int size);void qemu_put_byte (QEMUFile *f,int v); +int qemu_read_bytes (QEMUFile *f,uint8_t *buf,int size); +int qemu_peek_bytes (QEMUFile *f,uint8_t *buf,int size,size_t offset); +int qemu_write_bytes (QEMUFile *f,const uint8_t *buf,int size);static inline void qemu_put_ubyte (QEMUFile *f,unsigned int v) {
util/qemu-file.c +30 -0
@@ -713,3 +713,33 @@ uint64_t qemu_get_be64(QEMUFile *f)return v; } +int qemu_read_bytes (QEMUFile *f,uint8_t *buf,int size) +{ +if (qemu_file_get_error (f)) { +return -1 ; + } +return qemu_get_buffer (f, buf, size); +} + +int qemu_peek_bytes (QEMUFile *f,uint8_t *buf,int size,size_t offset) +{ +if (qemu_file_get_error (f)) { +return -1 ; + } +return qemu_peek_buffer (f, buf, size, offset); +} + +int qemu_write_bytes (QEMUFile *f,const uint8_t *buf,int size) +{ +if (qemu_file_get_error (f)) { +return -1 ; + } + +qemu_put_buffer (f, buf, size); + +if (qemu_file_get_error (f)) { +return -1 ; + } + +return size; +}
Signature · 1 line
1.7.11.7
PATCH v5 3/8 QEMUSizedBuffer
In reply to Stefan BergerUsing the QEMUFile interface, this patch adds support functions for operating on in-memory sized buffers that can be written to or read from.
--- include/migration/qemu-file.h | 13 ++ include/qemu-common.h | 12 ++ util/qemu-file.c | 411 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 436 insertions(+)
include/migration/qemu-file.h +13 -0
@@ -24,6 +24,8 @@#ifndef QEMU_FILE_H #define QEMU_FILE_H 1 +#include <stdint.h> +/* This function writes a chunk of data to a file at the given position. * The pos argument can be ignored if the file is only being used for * streaming. The handler should try to write all of the data it can.
@@ -58,6 +60,15 @@ typedef struct QEMUFileOps {QEMUFileGetFD *get_fd; } QEMUFileOps; +struct QEMUSizedBuffer { +struct iovec *iov; +size_t n_iov; +size_t size;/* total allocated size in all iov's */ +size_t used;/* number of used bytes */ +}; + +typedef struct QEMUSizedBuffer QEMUSizedBuffer ; +QEMUFile *qemu_fopen_ops (void *opaque,const QEMUFileOps *ops);QEMUFile *qemu_fopen (const char *filename,const char *mode);QEMUFile *qemu_fdopen (int fd,const char *mode);
@@ -71,6 +82,8 @@ void qemu_put_byte(QEMUFile *f, int v);int qemu_read_bytes (QEMUFile *f,uint8_t *buf,int size);int qemu_peek_bytes (QEMUFile *f,uint8_t *buf,int size,size_t offset);int qemu_write_bytes (QEMUFile *f,const uint8_t *buf,int size); +QEMUFile *qemu_bufopen (const char *mode,QEMUSizedBuffer *input); +const QEMUSizedBuffer *qemu_buf_get (QEMUFile *f);static inline void qemu_put_ubyte (QEMUFile *f,unsigned int v) {
include/qemu-common.h +12 -0
@@ -448,4 +448,16 @@ int uleb128_decode_small(const uint8_t *in, uint32_t *n);void hexdump (const char *buf,FILE *fp,const char *prefix,size_t size); +/* QEMU Sized Buffer */ +#include "include/migration/qemu-file.h" +QEMUSizedBuffer *qsb_create (const uint8_t *buffer,size_t len); +QEMUSizedBuffer *qsb_clone (const QEMUSizedBuffer *); +void qsb_free (QEMUSizedBuffer *); +size_t qsb_set_length (QEMUSizedBuffer *qsb,size_t length); +size_t qsb_get_length (const QEMUSizedBuffer *qsb); +ssize_t qsb_get_buffer (const QEMUSizedBuffer *,off_t start,size_t count, +uint8_t **buf); +ssize_t qsb_write_at (QEMUSizedBuffer *qsb,const uint8_t *buf, +off_t pos,size_t count); +#endif
util/qemu-file.c +411 -0
414 lines not on this page · raw message
Signature · 1 line
1.7.11.7
PATCH v5 4/8 QAPI: add type_sized_buffer
In reply to Stefan BergerAdd a sized buffer interface to qapi for serializing and deserializing of u8[], u16[], u32[] and u64[] with proper handling of endianess.
--- include/qapi/visitor-impl.h | 3 +++ include/qapi/visitor.h | 3 +++ qapi/qapi-visit-core.c | 8 ++++++++ 3 files changed, 14 insertions(+)
include/qapi/visitor-impl.h +3 -0
@@ -34,6 +34,9 @@ struct Visitorvoid (*type_str)(Visitor *v,char **obj,const char *name,Error **errp);void (*type_number)(Visitor *v,double *obj,const char *name,Error **errp); +void (*type_sized_buffer)(Visitor *v,void **obj,const char *name, +size_t elem_count,size_t elem_size, +Error **errp);/* May be NULL */ void (*start_optional)(Visitor *v, bool *present, const char *name,
include/qapi/visitor.h +3 -0
@@ -51,5 +51,8 @@ void visit_type_size(Visitor *v, uint64_t *obj, const char *name, Error **errp);void visit_type_bool (Visitor *v,bool *obj,const char *name,Error **errp);void visit_type_str (Visitor *v,char **obj,const char *name,Error **errp);void visit_type_number (Visitor *v,double *obj,const char *name,Error **errp); +void visit_type_sized_buffer (Visitor *v,void **obj,const char *name, +size_t elem_counter,size_t elem_size, +Error **errp);#endif
qapi/qapi-visit-core.c +8 -0
@@ -313,3 +313,11 @@ void input_type_enum(Visitor *v, int *obj, const char *strings[],g_free (enum_str); *obj = value; } + +void visit_type_sized_buffer (Visitor *v,void **obj,const char *name, +size_t elem_count,size_t elem_size,Error **errp) +{ +if (!error_is_set (errp)) { + v->type_sized_buffer (v, obj, name, elem_count, elem_size, errp); + } +}
Signature · 1 line
1.7.11.7
PATCH v5 5/8 ASN.1 output visitor
In reply to Stefan BergerImplement an output visitor for ASN.1 BER and CER encoding.
--- configure | 2 +- include/qapi/ber-output-visitor.h | 32 ++ include/qapi/ber.h | 113 ++++++ include/qemu-common.h | 2 + qapi/Makefile.objs | 1 + qapi/ber-common.c | 86 +++++ qapi/ber-common.h | 29 ++ qapi/ber-output-visitor.c | 705 ++++++++++++++++++++++++++++++++++++++ util/qemu-file.c | 58 ++++ 9 files changed, 1027 insertions(+), 1 deletion(-) create mode 100644 include/qapi/ber-output-visitor.h create mode 100644 include/qapi/ber.h create mode 100644 qapi/ber-common.c create mode 100644 qapi/ber-common.h create mode 100644 qapi/ber-output-visitor.c
configure +1 -1
@@ -2844,7 +2844,7 @@ fi # Do we need libm cat > $TMPC << EOF #include <math.h> -int main(void) { return isnan(sin(0.0)); } +int main(void) { return isnan(nan("NAN")); } EOF if compile_prog "" "" ; then :
include/qapi/ber-output-visitor.h new file +32 -0
@@ -0,0 +1,32 @@ +/* +* BER Output Visitor header +* +* Copyright IBM, Corp. 2013 +* +* Authors: +* Anthony Liguori <[email protected]> +* Stefan Berger <[email protected]> +* +* This work is licensed under the terms of the GNU LGPL, version 2.1 or later. +* See the COPYING.LIB file in the top-level directory. +* +*/ + +#ifndef BER_OUTPUT_VISITOR_H +#define BER_OUTPUT_VISITOR_H + +#include "qapi/visitor.h" +#include "qapi/ber.h" + +typedef struct BEROutputVisitor BEROutputVisitor ; + +BEROutputVisitor *ber_output_visitor_new (QEMUFile *, +BERLengthEncoding le); +void ber_output_visitor_cleanup (BEROutputVisitor *v); + +Visitor *ber_output_get_visitor (BEROutputVisitor *v); + +BERLengthEncoding ber_output_set_length_encoding (BEROutputVisitor *v, +BERLengthEncoding le); + +#endif
include/qapi/ber.h new file +113 -0
113 lines not on this page · raw message
include/qemu-common.h +2 -0
@@ -459,5 +459,7 @@ ssize_t qsb_get_buffer(const QEMUSizedBuffer *, off_t start, size_t count, uint8_t **buf);ssize_t qsb_write_at (QEMUSizedBuffer *qsb,const uint8_t *buf,off_t pos,size_t count); +ssize_t qsb_qfile_write (const QEMUSizedBuffer *qsb,QEMUFile *file, +off_t start,size_t count);#endif
qapi/Makefile.objs +1 -0
@@ -3,3 +3,4 @@ util-obj-y += qmp-output-visitor.o qmp-registry.o qmp-dispatch.o util-obj-y += string-input-visitor.o string-output-visitor.o util-obj-y += opts-visitor.o +util-obj-y += ber-common.o ber-output-visitor.o
qapi/ber-common.c new file +86 -0
86 lines not on this page · raw message
qapi/ber-common.h new file +29 -0
@@ -0,0 +1,29 @@ +/* +* BER Visitor -- common code +* +* Copyright IBM, Corp. 2013 +* +* Authors: +* Stefan Berger <[email protected]> +* +* This work is licensed under the terms of the GNU LGPL, version 2.1 or later. +* See the COPYING.LIB file in the top-level directory. +* +*/ +#ifndef __QAPI_BER_COMMON_H__ +#define __QAPI_BER_COMMON_H__ + +#include <stdint.h> + +#include "qemu/compiler.h" + +struct ieee754_buffer { +uint8_t type; +uint8_t length; +uint8_t first; +uint16_t exponent; +uint32_t mant_hi; +uint32_t mant_lo; +}QEMU_PACKED ; + +#endif /* __QAPI_BER_COMMON_H__ */
qapi/ber-output-visitor.c new file +705 -0
705 lines not on this page · raw message
util/qemu-file.c +58 -0
64 lines not on this page · raw message
Signature · 1 line
1.7.11.7
PATCH v5 6/8 ASN.1 input visitor
In reply to Stefan BergerImplement an input visitor for ASN.1 BER and CER encoding.
--- include/qapi/ber-input-visitor.h | 30 + qapi/Makefile.objs | 2 +- qapi/ber-input-visitor.c | 1153 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 1184 insertions(+), 1 deletion(-) create mode 100644 include/qapi/ber-input-visitor.h create mode 100644 qapi/ber-input-visitor.c
include/qapi/ber-input-visitor.h new file +30 -0
@@ -0,0 +1,30 @@ +/* +* BER Input Visitor header +* +* Copyright IBM, Corp. 2013 +* +* Authors: +* Anthony Liguori <[email protected]> +* Stefan Berger <[email protected]> +* +* This work is licensed under the terms of the GNU LGPL, version 2.1 or later. +* See the COPYING.LIB file in the top-level directory. +* +*/ + +#ifndef BER_INPUT_VISITOR_H +#define BER_INPUT_VISITOR_H + +#include "qapi/visitor.h" + +typedef struct BERInputVisitor BERInputVisitor ; + +BERInputVisitor *ber_input_visitor_new (QEMUFile *, +uint64_t max_allowd_buffer_size); +void ber_input_visitor_cleanup (BERInputVisitor *v); +uint64_t ber_input_get_parser_position (BERInputVisitor *v); +uint64_t ber_input_get_largest_needed_buffer (BERInputVisitor *v); + +Visitor *ber_input_get_visitor (BERInputVisitor *v); + +#endif
qapi/Makefile.objs +1 -1
@@ -3,4 +3,4 @@ util-obj-y += qmp-output-visitor.o qmp-registry.o qmp-dispatch.o util-obj-y += string-input-visitor.o string-output-visitor.o util-obj-y += opts-visitor.o -util-obj-y += ber-common.o ber-output-visitor.o +util-obj-y += ber-common.o ber-output-visitor.o ber-input-visitor.o
qapi/ber-input-visitor.c new file +1153 -0
1,153 lines not on this page · raw message
Signature · 1 line
1.7.11.7
PATCH v5 7/8 Extend test-visitor-serialization with ASN.1 visitor(s)
In reply to Stefan BergerAdd BER visitor hooks to test-visitor-serialization
--- tests/Makefile | 2 +- tests/test-visitor-serialization.c | 78 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 1 deletion(-)
tests/Makefile +1 -1
@@ -124,7 +124,7 @@ tests/test-qmp-output-visitor$(EXESUF): tests/test-qmp-output-visitor.o $(test-q tests/test-qmp-input-visitor$(EXESUF): tests/test-qmp-input-visitor.o $(test-qapi-obj-y) libqemuutil.a libqemustub.a tests/test-qmp-input-strict$(EXESUF): tests/test-qmp-input-strict.o $(test-qapi-obj-y) libqemuutil.a libqemustub.a tests/test-qmp-commands$(EXESUF): tests/test-qmp-commands.o tests/test-qmp-marshal.o $(test-qapi-obj-y) qapi-types.o qapi-visit.o libqemuutil.a libqemustub.a -tests/test-visitor-serialization$(EXESUF): tests/test-visitor-serialization.o $(test-qapi-obj-y) libqemuutil.a libqemustub.a +tests/test-visitor-serialization$(EXESUF): tests/test-visitor-serialization.o $(test-qapi-obj-y) $(block-obj-y) libqemuutil.a libqemustub.a tests/test-mul64$(EXESUF): tests/test-mul64.o libqemuutil.a
tests/test-visitor-serialization.c +78 -0
96 lines not on this page · raw message
Signature · 1 line
1.7.11.7
PATCH v5 8/8 ASN.1 specific test cases
In reply to Stefan BergerBER visitor tests give us some assurance that the BER visitor code works, and also end up by extension helping out on our code coverage of the filesystem tests.
After the output visitor invocation the resulting buffer is compared against a known byte stream -- this will lock the implementation into producing specific byte arrays.
--- tests/Makefile | 13 + tests/test-ber-visitor.c | 894 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 907 insertions(+) create mode 100644 tests/test-ber-visitor.c
tests/Makefile +13 -0
@@ -23,6 +23,10 @@ check-unit-y += tests/test-string-input-visitor$(EXESUF) gcov-files-test-string-input-visitor-y = qapi/string-input-visitor.c check-unit-y += tests/test-string-output-visitor$(EXESUF) gcov-files-test-string-output-visitor-y = qapi/string-output-visitor.c +check-unit-y += tests/test-ber-visitor$(EXESUF) +gcov-files-test-vistor-y = qapi/ber-input-visitor.c + + check-unit-y += tests/test-coroutine$(EXESUF) ifeq ($(CONFIG_WIN32),y) gcov-files-test-coroutine-y = coroutine-win32.c
@@ -134,6 +138,15 @@ tests/fdc-test$(EXESUF): tests/fdc-test.o tests/hd-geo-test$(EXESUF): tests/hd-geo-test.o tests/tmp105-test$(EXESUF): tests/tmp105-test.o +tests/test-ber-visitor.o : QEMU_CFLAGS += -I include/qapi + +tests/test-ber-visitor.o: \ + $(addprefix include/qapi/, ber.h ber-input-visitor.h ber-output-visitor.h) \ + $(addprefix qapi/, ber-common.c ber-input-visitor.c ber-output-visitor.c) +tests/test-ber-visitor$(EXESUF): tests/test-ber-visitor.o $(tools-obj-y) \ + qapi/ber-output-visitor.o qapi/ber-input-visitor.o \ + qapi/ber-common.o $(block-obj-y) libqemuutil.a libqemustub.a + # QTest rules TARGETS=$(patsubst %-softmmu,%, $(filter %-softmmu,$(TARGET_DIRS)))
tests/test-ber-visitor.c new file +894 -0
894 lines not on this page · raw message
Signature · 1 line
1.7.11.7
Quoted 5 lines · Stefan Berger
This patch series implements ASN.1 BER visitors for encoding and decoding of data into byte streams.
This is the last post of this series with some issues fixed, the
visitor_optionalimplemented and all possible tests in test-visitor-serialization passing.
The series applies to f7d42093a5.
I won't need it anymore for what I had planned to use it for.
Stefan