4 * Copyright (c) Freescale Semiconductor, Inc. All rights reserved.
5 * See included license file for license details.
9 #include "EndianUtilities.h"
11 using namespace elftosb;
14 * Parses a string in the form "xxx.xxx.xxx" (where x is a digit) into
15 * three version fields for major, minor, and revision. The output is
16 * right aligned BCD in host-natural byte order.
18 * \param versionString String containing the version.
20 void version_t::set(const std::string & versionString)
22 size_t length = versionString.size();
33 // set initial versions to 0s
38 VersionParseState parseState = kVersionStateNone;
40 for (; index < length && !done; ++index)
42 char c = versionString[index];
48 case kVersionStateNone:
49 parseState = kVersionStateMajor;
52 case kVersionStateMajor:
53 case kVersionStateMinor:
54 case kVersionStateRevision:
55 version = (version << 4) | (c - '0');
63 case kVersionStateNone:
64 parseState = kVersionStateNone;
66 case kVersionStateMajor:
69 parseState = kVersionStateMinor;
71 case kVersionStateMinor:
74 parseState = kVersionStateRevision;
76 case kVersionStateRevision:
87 case kVersionStateNone:
88 parseState = kVersionStateNone;
90 case kVersionStateMajor:
94 case kVersionStateMinor:
98 case kVersionStateRevision:
108 case kVersionStateMajor:
111 case kVersionStateMinor:
114 case kVersionStateRevision:
115 m_revision = version;
123 //! \brief Converts host endian BCD version values to the equivalent big-endian BCD values.
125 //! The output is a half-word. And BCD is inherently big-endian, or byte ordered, if
126 //! you prefer to think of it that way. So for little endian systems, we need to convert
127 //! the output half-word in reverse byte order. When it is written to disk or a
128 //! buffer it will come out big endian.
131 //! - The input is BCD in host endian format, so 0x1234. Written to a file, this would
132 //! come out as 0x34 0x12, reverse of what we want.
133 //! - The desired BCD output is the two bytes 0x12 0x34.
134 //! - So the function's uint16_t result must be 0x3412 on a little-endian host.
136 //! On big endian hosts, we don't have to worry about byte swapping.
137 void version_t::fixByteOrder()
139 m_major = ENDIAN_HOST_TO_BIG_U16(m_major);
140 m_minor = ENDIAN_HOST_TO_BIG_U16(m_minor);
141 m_revision = ENDIAN_HOST_TO_BIG_U16(m_revision);