-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMPAStream.cpp
67 lines (52 loc) · 1.97 KB
/
MPAStream.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// GNU LESSER GENERAL PUBLIC LICENSE
// Version 3, 29 June 2007
//
// Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
//
// Everyone is permitted to copy and distribute verbatim copies of this license
// document, but changing it is not allowed.
//
// This version of the GNU Lesser General Public License incorporates the terms
// and conditions of version 3 of the GNU General Public License, supplemented
// by the additional permissions listed below.
#include "stdafx.h"
#include "MPAStream.h"
#include "MPAException.h"
// constructor
CMPAStream::CMPAStream(LPCTSTR file_name) {
assert(file_name);
// save filename
m_szFile = strdup(file_name);
}
CMPAStream::~CMPAStream() { free(m_szFile); }
unsigned CMPAStream::ReadLEValue(unsigned num_bytes, unsigned& offset,
bool move_offset) const {
assert(num_bytes > 0);
assert(num_bytes <= 4); // max 4 byte
const unsigned char* buffer{ReadBytes(num_bytes, offset, move_offset)};
// little endian extract (least significant byte first) (will work on little
// and big-endian computers)
unsigned num_bytes_shifts = 0U;
unsigned result = 0U;
for (unsigned n = 0; n < num_bytes; n++) {
result |= buffer[n] << 8U * num_bytes_shifts++;
}
return result;
}
// convert from big endian to native format (Intel=little endian) and return as
// unsigned (32bit)
unsigned CMPAStream::ReadBEValue(unsigned num_bytes, unsigned& offset,
bool move_offset) const {
assert(num_bytes > 0);
assert(num_bytes <= 4U); // max 4 byte
const unsigned char* buffer{ReadBytes(num_bytes, offset, move_offset)};
// big endian extract (most significant byte first) (will work on little and
// big-endian computers)
unsigned num_bytes_shifts{num_bytes - 1U};
unsigned result = 0;
for (unsigned n = 0; n < num_bytes; n++) {
// the bit shift will do the correct byte order for you
result |= buffer[n] << 8U * num_bytes_shifts--;
}
return result;
}