Passwordfile library  3.1.4
C++ library to read/write passwords from/to encrypted files
field.cpp
Go to the documentation of this file.
1 #include "./field.h"
2 #include "./parsingexception.h"
3 
4 #include <c++utilities/io/binaryreader.h>
5 #include <c++utilities/io/binarywriter.h>
6 
7 using namespace std;
8 using namespace IoUtilities;
9 using namespace ConversionUtilities;
10 
11 namespace Io {
12 
23 Field::Field(AccountEntry *tiedAccount, const string &name, const string &value)
24  : m_name(name)
25  , m_value(value)
26  , m_type(FieldType::Normal)
27  , m_tiedAccount(tiedAccount)
28 {
29 }
30 
37 {
38  BinaryReader reader(&stream);
39  int version = reader.readByte();
40  if (version == 0x0 || version == 0x1) {
41  m_name = reader.readLengthPrefixedString();
42  m_value = reader.readLengthPrefixedString();
43  byte type = reader.readByte();
44  if (isValidType(type)) {
45  m_type = static_cast<FieldType>(type);
46  } else {
47  throw ParsingException("Field type is not supported.");
48  }
49  if (version == 0x1) { // version 0x1 has an extended header
50  uint16 extendedHeaderSize = reader.readUInt16BE();
51  // currently there's nothing to read here
52  m_extendedData = reader.readString(extendedHeaderSize);
53  }
54  m_tiedAccount = tiedAccount;
55  } else {
56  throw ParsingException("Field version is not supported.");
57  }
58 }
59 
63 void Field::make(ostream &stream) const
64 {
65  BinaryWriter writer(&stream);
66  writer.writeByte(m_extendedData.empty() ? 0x0 : 0x1); // version
67  writer.writeLengthPrefixedString(m_name);
68  writer.writeLengthPrefixedString(m_value);
69  writer.writeByte(static_cast<byte>(m_type));
70  if (!m_extendedData.empty()) {
71  writer.writeUInt16BE(m_extendedData.size());
72  writer.writeString(m_extendedData);
73  }
74 }
75 }
static bool isValidType(int number)
Returns whether the specified number is a valid field type.
Definition: field.h:108
STL namespace.
void make(std::ostream &stream) const
Serializes the current instance to the specified stream.
Definition: field.cpp:63
Contains all IO related classes.
FieldType type() const
Returns the type.
Definition: field.h:84
The exception that is thrown when a parsing error occurs.
Definition: entry.h:145
Field(AccountEntry *tiedAccount, const std::string &name=std::string(), const std::string &value=std::string())
Constructs a new account entry for the specified account with the specified name and value...
Definition: field.cpp:23
std::string m_extendedData
Definition: field.h:38
FieldType
Definition: field.h:11
The exception that is thrown when a parsing error occurs.
AccountEntry * tiedAccount() const
Returns the tied account.
Definition: field.h:100