src/lib/ASN1ContentHandler.h
branchv_0
changeset 21 705036445672
parent 20 fac034e3e867
child 22 9b6f86760384
equal deleted inserted replaced
20:fac034e3e867 21:705036445672
    50 		UniversalString,
    50 		UniversalString,
    51 		CharacterString,
    51 		CharacterString,
    52 		BMPString,
    52 		BMPString,
    53 	};
    53 	};
    54 
    54 
       
    55 	enum class DateTimeType : uint64_t {
       
    56 		UTCTime,
       
    57 		GeneralizedTime,
       
    58 		Time,
       
    59 		Date,
       
    60 		TimeOfDay,
       
    61 		DateTime,
       
    62 		Duration,
       
    63 		// TODO: review date/time types
       
    64 	};
       
    65 
    55 	class Integer {
    66 	class Integer {
    56 	private:
    67 	private:
    57 		// TODO: use std::string (of octets, not ASCII) instead of std::vector?
    68 		// TODO: use std::string (of octets, not ASCII) instead of std::vector?
    58 		// TODO: use this class as BigInteger across Relational pipes?
    69 		// TODO: use this class as BigInteger across Relational pipes?
    59 		std::vector<uint8_t> data;
    70 		std::vector<uint8_t> data;
   165 			return result.str();
   176 			return result.str();
   166 		}
   177 		}
   167 
   178 
   168 	};
   179 	};
   169 
   180 
       
   181 	class DateTime {
       
   182 	public:
       
   183 
       
   184 		enum class Precision {
       
   185 			Year,
       
   186 			Month,
       
   187 			Day,
       
   188 			Hour,
       
   189 			Minute,
       
   190 			Second,
       
   191 			Milisecond,
       
   192 			Nanosecond
       
   193 		};
       
   194 
       
   195 		// TODO: timezone (in minutes or 1/4 hours)
       
   196 
       
   197 		Precision precision = Precision::Second;
       
   198 		int32_t year = 1970;
       
   199 		int8_t month = 1;
       
   200 		int8_t day = 1;
       
   201 		int8_t hour = 0;
       
   202 		int8_t minute = 0;
       
   203 		int8_t second = 0;
       
   204 		// TODO: ms/ns
       
   205 
       
   206 		virtual ~DateTime() {
       
   207 		}
       
   208 
       
   209 		const std::string toString() const {
       
   210 			std::stringstream result;
       
   211 			result << std::setfill('0');
       
   212 			result << std::setw(4) << (int) year;
       
   213 			result << "-" << std::setw(2) << (int) month;
       
   214 			result << "-" << std::setw(2) << (int) day;
       
   215 			result << "T" << std::setw(2) << (int) hour;
       
   216 			result << ":" << std::setw(2) << (int) minute;
       
   217 			result << ":" << std::setw(2) << (int) second;
       
   218 			result << "+00:00"; // TODO: timezone
       
   219 			return result.str();
       
   220 		}
       
   221 	};
       
   222 
   170 	virtual ~ASN1ContentHandler() = default;
   223 	virtual ~ASN1ContentHandler() = default;
   171 
   224 
   172 	// TODO: more metadata, support OID decoding and ASN.1 modules (schema), probably through a plug-in
   225 	// TODO: more metadata, support OID decoding and ASN.1 modules (schema), probably through a plug-in
       
   226 	// TODO: support also extension extractor plug-ins? (could decode some opaque structures like octet strings and replace them with nested elements) e.g. subjectAltName in https://datatracker.ietf.org/doc/html/rfc5280#section-4.2.1.6
   173 
   227 
   174 	virtual void writeStreamStart() = 0;
   228 	virtual void writeStreamStart() = 0;
   175 	virtual void writeStreamEnd() = 0;
   229 	virtual void writeStreamEnd() = 0;
   176 
   230 
   177 	virtual void writeCollectionStart(CollectionType type) = 0;
   231 	virtual void writeCollectionStart(CollectionType type) = 0;
   179 	virtual void writeBoolean(bool value) = 0;
   233 	virtual void writeBoolean(bool value) = 0;
   180 	virtual void writeNull() = 0;
   234 	virtual void writeNull() = 0;
   181 	virtual void writeInteger(Integer value) = 0;
   235 	virtual void writeInteger(Integer value) = 0;
   182 	virtual void writeString(StringType type, std::string value) = 0;
   236 	virtual void writeString(StringType type, std::string value) = 0;
   183 	virtual void writeOID(ObjectIdentifier value) = 0;
   237 	virtual void writeOID(ObjectIdentifier value) = 0;
       
   238 	virtual void writeDateTime(DateTimeType type, DateTime value) = 0;
   184 	// Object descriptor
   239 	// Object descriptor
   185 	// virtual void writeReal(float value) = 0;
   240 	// virtual void writeReal(float value) = 0;
   186 	// Enumerated
   241 	// Enumerated
   187 	// Embedded PVD
   242 	// Embedded PVD
   188 	// Relative OID
   243 	// Relative OID
   189 	// TIME
       
   190 	// UTC time
       
   191 	// Generalized time
       
   192 	// Date
       
   193 	// Time of day
       
   194 	// Date-time
       
   195 	// Duration
       
   196 	// OID-IRI
   244 	// OID-IRI
   197 	// Relative OID-IRI
   245 	// Relative OID-IRI
   198 
   246 
   199 };
   247 };
   200 
   248 
   243 
   291 
   244 	void writeOID(ObjectIdentifier value) override {
   292 	void writeOID(ObjectIdentifier value) override {
   245 		handler->writeOID(value);
   293 		handler->writeOID(value);
   246 	}
   294 	}
   247 
   295 
       
   296 	void writeDateTime(DateTimeType type, DateTime value) override {
       
   297 		handler->writeDateTime(type, value);
       
   298 	}
   248 
   299 
   249 #undef handler
   300 #undef handler
   250 
   301 
   251 };
   302 };
   252 
   303