util_st.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * Copyright (C) 2011-2018 Intel Corporation. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in
  12. * the documentation and/or other materials provided with the
  13. * distribution.
  14. * * Neither the name of Intel Corporation nor the names of its
  15. * contributors may be used to endorse or promote products derived
  16. * from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. */
  31. #include "util_st.h"
  32. #include "se_trace.h"
  33. #include <assert.h>
  34. #include <fstream>
  35. extern "C" bool write_data_to_file(const char *filename, std::ios_base::openmode mode,
  36. uint8_t *buf, size_t bsize, long offset)
  37. {
  38. if(filename == NULL || (buf == NULL && bsize != 0) || (buf != NULL && bsize == 0))
  39. return false;
  40. std::ofstream ofs(filename, mode);
  41. if(!ofs.good())
  42. {
  43. se_trace(SE_TRACE_ERROR, OPEN_FILE_ERROR, filename);
  44. return false;
  45. }
  46. ofs.seekp(offset, std::ios::beg);
  47. ofs.write(reinterpret_cast<char*>(buf), bsize);
  48. if(ofs.fail())
  49. {
  50. se_trace(SE_TRACE_ERROR, WRITE_FILE_ERROR, filename);
  51. return false;
  52. }
  53. return true;
  54. }
  55. extern "C" size_t get_file_size(const char *filename)
  56. {
  57. std::ifstream ifs(filename, std::ios::in | std::ios::binary);
  58. if(!ifs.good())
  59. {
  60. se_trace(SE_TRACE_ERROR, OPEN_FILE_ERROR, filename);
  61. return -1;
  62. }
  63. ifs.seekg(0, std::ios::end);
  64. size_t size = (size_t)ifs.tellg();
  65. return size;
  66. }
  67. extern "C" bool read_file_to_buf(const char *filename, uint8_t *buf, size_t bsize)
  68. {
  69. if(filename == NULL || buf == NULL || bsize == 0)
  70. return false;
  71. std::ifstream ifs(filename, std::ios::binary|std::ios::in);
  72. if(!ifs.good())
  73. {
  74. se_trace(SE_TRACE_ERROR, OPEN_FILE_ERROR, filename);
  75. return false;
  76. }
  77. ifs.read(reinterpret_cast<char *> (buf), bsize);
  78. if(ifs.fail())
  79. {
  80. return false;
  81. }
  82. return true;
  83. }
  84. extern "C" bool copy_file(const char *source_path, const char *dest_path)
  85. {
  86. std::ifstream ifs(source_path, std::ios::binary|std::ios::in);
  87. if(!ifs.good())
  88. {
  89. se_trace(SE_TRACE_ERROR, OPEN_FILE_ERROR, source_path);
  90. return false;
  91. }
  92. std::ofstream ofs(dest_path, std::ios::binary|std::ios::out);
  93. if(!ofs.good())
  94. {
  95. se_trace(SE_TRACE_ERROR, OPEN_FILE_ERROR, dest_path);
  96. return false;
  97. }
  98. ofs << ifs.rdbuf();
  99. return true;
  100. }