| 49 | }; |
| 50 | |
| 51 | inline void CreateHeaderInfo(std::ifstream &ifStream, HeaderInfo &headerInfo) |
| 52 | { |
| 53 | // A Numpy header consists of: |
| 54 | // a magic string "x93NUMPY" |
| 55 | // 1 byte for the major version |
| 56 | // 1 byte for the minor version |
| 57 | // 2 or 4 bytes for the header length |
| 58 | // More info: https://numpy.org/devdocs/reference/generated/numpy.lib.format.html |
| 59 | char buffer[headerInfo.m_MagicStringLength + 2lu]; |
| 60 | ifStream.read(buffer, headerInfo.m_MagicStringLength + 2); |
| 61 | |
| 62 | if (!ifStream) |
| 63 | { |
| 64 | throw armnn::Exception( |
| 65 | fmt::format("Failed to create numpy header info at {}", |
| 66 | CHECK_LOCATION().AsString())); |
| 67 | } |
| 68 | // Verify that the numpy is in the valid format by checking for the magic string |
| 69 | int compare_result = ::memcmp(buffer, headerInfo.m_MagicString, headerInfo.m_MagicStringLength); |
| 70 | if (compare_result != 0) { |
| 71 | throw armnn::Exception(fmt::format("Numpy does not contain magic string. Can not parse invalid numpy {}", |
| 72 | CHECK_LOCATION().AsString())); |
| 73 | } |
| 74 | |
| 75 | headerInfo.m_MajorVersion = buffer[headerInfo.m_MagicStringLength]; |
| 76 | headerInfo.m_MinorVersion = buffer[headerInfo.m_MagicStringLength + 1]; |
| 77 | if(headerInfo.m_MajorVersion == 1 && headerInfo.m_MinorVersion == 0) |
| 78 | { |
| 79 | ifStream.read(headerInfo.m_HeaderLenBytes, 2); |
| 80 | // Header len is written in little endian, so we do a quick test |
| 81 | // to check the machines endianness |
| 82 | int i = 1; |
| 83 | if (*(reinterpret_cast<char *>(&i)) == 1) |
| 84 | { |
| 85 | headerInfo.m_HeaderLen = static_cast<unsigned>(headerInfo.m_HeaderLenBytes[0]) | |
| 86 | (static_cast<unsigned>(headerInfo.m_HeaderLenBytes[1] << 8)); |
| 87 | } |
| 88 | else |
| 89 | { |
| 90 | headerInfo.m_HeaderLen = static_cast<unsigned>(headerInfo.m_HeaderLenBytes[1]) | |
| 91 | (static_cast<unsigned>(headerInfo.m_HeaderLenBytes[0] << 8)); |
| 92 | } |
| 93 | } |
| 94 | else if (headerInfo.m_MajorVersion == 2 && headerInfo.m_MinorVersion == 0) |
| 95 | { |
| 96 | ifStream.read(headerInfo.m_HeaderLenBytes, 4); |
| 97 | // Header len is written in little endian, so we do a quick test |
| 98 | // to check the machines endianness |
| 99 | int i = 1; |
| 100 | if (*(reinterpret_cast<char *>(&i)) == 1) |
| 101 | { |
| 102 | headerInfo.m_HeaderLen = static_cast<unsigned>(headerInfo.m_HeaderLenBytes[0] << 0) | |
| 103 | static_cast<unsigned>(headerInfo.m_HeaderLenBytes[1] << 8) | |
| 104 | static_cast<unsigned>(headerInfo.m_HeaderLenBytes[2] << 16) | |
| 105 | static_cast<unsigned>(headerInfo.m_HeaderLenBytes[3] << 24); |
| 106 | } |
| 107 | else |
| 108 | { |
no test coverage detected