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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
|
/*
* Copyright (c) 2020-2021 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
*
* This software is available to you under a choice of one of two
* licenses. You may choose to be licensed under the terms of the GNU
* General Public License (GPL) Version 2, available from the file
* COPYING in the main directory of this source tree, or the
* OpenIB.org BSD license below:
*
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
*
* - Redistributions of source code must retain the above
* copyright notice, this list of conditions and the following
* disclaimer.
*
* - Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* pldm_buff.cpp
*
* Created on: Feb 27, 2019
* Author: Samer Deeb
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <string>
#include <compatibility.h>
#include "pldm_buff.h"
PldmBuffer::PldmBuffer() : m_buff(NULL), m_pos(0), m_size(0) {}
PldmBuffer::~PldmBuffer()
{
reset();
}
void PldmBuffer::reset()
{
if (m_buff)
{
delete[] m_buff;
m_buff = NULL;
}
m_pos = 0;
m_size = 0;
}
bool PldmBuffer::loadFile(const std::string& fname)
{
// open the file:
FILE* fp;
fp = fopen(fname.c_str(), "rb");
if (!fp)
{
return false;
}
// Get file length
fseek(fp, 0, SEEK_END);
m_size = ftell(fp);
fseek(fp, 0, SEEK_SET);
// Allocate memory
if (m_buff)
{
delete[] m_buff;
m_buff = NULL;
}
m_buff = new u_int8_t[m_size + 1];
if (!m_buff)
{
return false;
}
// Read file contents into buffer
size_t read_size = fread(m_buff, m_size, 1, fp);
fclose(fp);
return (read_size == (size_t)m_size);
}
void PldmBuffer::read(u_int8_t& val)
{
val = *(m_buff + m_pos);
m_pos += sizeof(u_int8_t);
}
void PldmBuffer::read(u_int16_t& val)
{
val = *(u_int16_t*)(m_buff + m_pos);
m_pos += sizeof(u_int16_t);
val = __le16_to_cpu(val);
}
void PldmBuffer::read(u_int32_t& val)
{
val = *(u_int32_t*)(m_buff + m_pos);
m_pos += sizeof(u_int32_t);
val = __le32_to_cpu(val);
}
void PldmBuffer::read(std::string& str, size_t str_size)
{
u_int8_t* arr = new u_int8_t[str_size];
read(arr, str_size);
str = std::string(reinterpret_cast<char const*>(arr), str_size);
delete[] arr;
}
void PldmBuffer::read(u_int8_t* arr, size_t arr_size)
{
memcpy(arr, m_buff + m_pos, arr_size);
m_pos += arr_size;
}
int PldmBuffer::seek(long offset, int whence)
{
long new_pos = -1;
switch (whence)
{
case SEEK_SET:
new_pos = offset;
break;
case SEEK_CUR:
new_pos = m_pos + offset;
break;
case SEEK_END:
new_pos = m_size + offset;
break;
}
if (new_pos < 0 || new_pos > m_size)
{
return -1;
}
m_pos = new_pos;
return 0;
}
long PldmBuffer::tell()
{
return m_pos;
}
|