src/ASN1Writer.h
author František Kučera <franta-hg@frantovo.cz>
Sat, 30 Mar 2019 01:12:42 +0100
branchv_0
changeset 0 f4f9cdf7ed59
child 1 e9b4f3a9e436
permissions -rw-r--r--
project and code skeleton: text output
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
 * Relational pipes
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
 * Copyright © 2018 František Kučera (Frantovo.cz, GlobalCode.info)
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
 * the Free Software Foundation, either version 3 of the License, or
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 * (at your option) any later version.
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
 *
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * This program is distributed in the hope that it will be useful,
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 * GNU General Public License for more details.
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
 *
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
 */
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
#pragma once
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
#include <iostream>
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
#include <sstream>
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
#include <string>
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
#include <vector>
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
#include <codecvt>
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
#include <locale>
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
#include <relpipe/reader/typedefs.h>
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
#include <relpipe/cli/RelpipeCLIException.h>
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
using namespace std;
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
namespace relpipe {
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
namespace out {
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
namespace asn1 {
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
using namespace relpipe::reader;
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
/**
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
 * A simple library for writing ASN.1 BER encoded files.
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
 */
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
class ASN1Writer {
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
private:
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
	ostream& output;
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
	wstring_convert<codecvt_utf8<wchar_t>> convertor; // only UTF8String is supported
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
	int sequenceLevel = 0;
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
	void xxx_indent() {
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
		// FIXME: remove
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
		for (int i = 0; i < sequenceLevel; i++) output << "  ";
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
	}
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    52
public:
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    53
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    54
	ASN1Writer(std::ostream& output) : output(output) {
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    55
	}
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    56
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    57
	virtual ~ASN1Writer() {
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    58
		if (sequenceLevel) output << "Unable to close ASN1Writer because there are not ended sequences." << endl; // FIXME: better error handling
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    59
		output.flush();
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    60
	}
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    61
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    62
	void writeStartSequence() {
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    63
		xxx_indent();
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    64
		output << "sequence start" << endl;
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    65
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    66
		sequenceLevel++;
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    67
	}
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    68
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    69
	void writeEndSequence() {
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    70
		if (sequenceLevel == 0) throw cli::RelpipeCLIException(L"Unable to writeEndSequence() if not sequence is currently started.", cli::CLI::EXIT_CODE_UNEXPECTED_ERROR); // should not happen, error in the program
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    71
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    72
		sequenceLevel--;
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    73
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    74
		xxx_indent();
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    75
		output << "sequence end" << endl;
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    76
	}
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    77
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    78
	void writeBoolean(const boolean_t& value) {
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    79
		xxx_indent();
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    80
		output << "boolean" << (value ? "true" : "false") << endl;
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    81
	}
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    82
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    83
	void writeInteger(const integer_t& value) {
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    84
		xxx_indent();
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    85
		output << "integer: " << value << endl;
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    86
	}
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    87
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    88
	void writeString(const string_t& value) {
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    89
		xxx_indent();
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    90
		output << "string: " << convertor.to_bytes(value) << endl;
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    91
	}
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    92
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    93
};
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    94
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    95
}
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    96
}
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    97
}