|
| 1 | +using System; |
| 2 | +using System.Runtime.InteropServices; |
| 3 | + |
| 4 | +namespace IrbImgFormat |
| 5 | +{ |
| 6 | + class BufferReader |
| 7 | + { |
| 8 | + private static Logging logging = new Logging("BufferReader"); |
| 9 | + |
| 10 | + /// <summary> |
| 11 | + /// Union to cast a 4-byte int to a float |
| 12 | + /// </summary> |
| 13 | + [StructLayout(LayoutKind.Explicit)] |
| 14 | + private struct BinaryConvertIntToFloat |
| 15 | + { |
| 16 | + [FieldOffset(0)] |
| 17 | + public float toFloat; |
| 18 | + [FieldOffset(0)] |
| 19 | + public int toInt; |
| 20 | + } |
| 21 | + |
| 22 | + |
| 23 | + |
| 24 | + /// <summary> |
| 25 | + /// Union to cast a 8 byte long into a double |
| 26 | + /// </summary> |
| 27 | + [StructLayout(LayoutKind.Explicit)] |
| 28 | + private struct BinaryConvertLongToDouble |
| 29 | + { |
| 30 | + [FieldOffset(0)] |
| 31 | + public double toDouble; |
| 32 | + [FieldOffset(0)] |
| 33 | + public long toLong; |
| 34 | + } |
| 35 | + |
| 36 | + |
| 37 | + byte[] data; |
| 38 | + int offset = 0; |
| 39 | + int dataLength = 0; |
| 40 | + |
| 41 | + public BufferReader(byte[] data) |
| 42 | + { |
| 43 | + this.data = data; |
| 44 | + dataLength = (data != null) ? data.Length : 0; |
| 45 | + } |
| 46 | + |
| 47 | + /// <summary> |
| 48 | + /// is the buffer Pointer at the end of the buffer |
| 49 | + /// </summary> |
| 50 | + public bool Eof |
| 51 | + { |
| 52 | + get |
| 53 | + { |
| 54 | + return ((offset < 0) || (offset >= dataLength) || (data == null)); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + /// <summary> |
| 59 | + /// Return the lenght of the buffer |
| 60 | + /// </summary> |
| 61 | + public int Length |
| 62 | + { |
| 63 | + get |
| 64 | + { |
| 65 | + return data.Length; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + /// <summary> |
| 70 | + /// Read a string from the the buffer |
| 71 | + /// </summary> |
| 72 | + public string ReadStr(int length, int offset) |
| 73 | + { |
| 74 | + if (length < 0) length = 0; |
| 75 | + if (offset > -1) this.offset = offset; |
| 76 | + |
| 77 | + string outVal = ""; |
| 78 | + |
| 79 | + |
| 80 | + if (this.offset < dataLength) |
| 81 | + { |
| 82 | + if ((this.offset + length) > dataLength) |
| 83 | + { |
| 84 | + length = dataLength - this.offset; |
| 85 | + } |
| 86 | + |
| 87 | + outVal = System.Text.Encoding.Default.GetString(data, this.offset, length); |
| 88 | + } |
| 89 | + else |
| 90 | + { |
| 91 | + if (length > 0) |
| 92 | + { |
| 93 | + logging.addWarning("ReadStr:EOF!"); |
| 94 | + length = 0; |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + |
| 99 | + if (length > 0) this.offset = this.offset + length; |
| 100 | + |
| 101 | + return outVal; |
| 102 | + } |
| 103 | + |
| 104 | + |
| 105 | + /// <summary> |
| 106 | + /// read a NULL terminated string from buffer |
| 107 | + /// </summary> |
| 108 | + public string ReadNullTerminatedString(int offset, int size) |
| 109 | + { |
| 110 | + string s = this.ReadStr(size, offset); |
| 111 | + |
| 112 | + int pos = s.IndexOf('\0'); |
| 113 | + |
| 114 | + if (pos > 0) |
| 115 | + { |
| 116 | + return s.Substring(0, pos); |
| 117 | + } |
| 118 | + return s; |
| 119 | + } |
| 120 | + |
| 121 | + |
| 122 | + /// <summary> |
| 123 | + /// Read 8 byte big ending long from buffer |
| 124 | + /// </summary> |
| 125 | + public long ReadLongBE(int offset = -1) |
| 126 | + { |
| 127 | + if (offset > -1) this.offset = offset; |
| 128 | + |
| 129 | + if (this.offset < dataLength) |
| 130 | + { |
| 131 | + if ((this.offset + 8) > dataLength) |
| 132 | + { |
| 133 | + this.offset = dataLength; |
| 134 | + return 0; |
| 135 | + } |
| 136 | + |
| 137 | + |
| 138 | + byte[] d = data; |
| 139 | + int i = this.offset; |
| 140 | + |
| 141 | + byte[] bytes = { d[i + 0], d[i + 1], d[i + 2], d[i + 3], d[i + 4], d[i + 5], d[i + 6], d[i + 7] }; |
| 142 | + |
| 143 | + return BitConverter.ToInt64(bytes, 0); |
| 144 | + } |
| 145 | + return 0; |
| 146 | + |
| 147 | + |
| 148 | + } |
| 149 | + |
| 150 | + /// <summary> |
| 151 | + /// Read one byte from buffer |
| 152 | + /// </summary> |
| 153 | + public int ReadByte(int offset = -1) |
| 154 | + { |
| 155 | + if (offset > -1) this.offset = offset; |
| 156 | + |
| 157 | + if (this.offset >= dataLength) |
| 158 | + { |
| 159 | + return 0; |
| 160 | + } |
| 161 | + |
| 162 | + if ((this.offset + 1) > dataLength) |
| 163 | + { |
| 164 | + this.offset = dataLength; |
| 165 | + return 0; |
| 166 | + } |
| 167 | + |
| 168 | + |
| 169 | + var result = data[this.offset]; |
| 170 | + this.offset++; |
| 171 | + return result; |
| 172 | + |
| 173 | + |
| 174 | + } |
| 175 | + |
| 176 | + |
| 177 | + /// <summary> |
| 178 | + /// Read big ending word (2 Bytes) from buffer |
| 179 | + /// </summary> |
| 180 | + /// <param name="offset"></param> |
| 181 | + /// <returns></returns> |
| 182 | + public int ReadWordBE(int offset = -1) |
| 183 | + { |
| 184 | + if (offset > -1) |
| 185 | + { |
| 186 | + this.offset = offset; |
| 187 | + } |
| 188 | + |
| 189 | + |
| 190 | + if (this.offset >= dataLength) |
| 191 | + { |
| 192 | + return 0; |
| 193 | + } |
| 194 | + |
| 195 | + if ((this.offset + 2) > dataLength) |
| 196 | + { |
| 197 | + this.offset = dataLength; |
| 198 | + return 0; |
| 199 | + } |
| 200 | + |
| 201 | + |
| 202 | + var result = (int)data[this.offset] + (data[this.offset + 1] << 8); |
| 203 | + |
| 204 | + this.offset += 2; |
| 205 | + return result; |
| 206 | + |
| 207 | + |
| 208 | + } |
| 209 | + |
| 210 | + |
| 211 | + |
| 212 | + /// <summary> |
| 213 | + /// Read big-ending int from buffer |
| 214 | + /// </summary> |
| 215 | + public int ReadIntBE(int offset = -1) |
| 216 | + { |
| 217 | + if (offset > -1) |
| 218 | + { |
| 219 | + this.offset = offset; |
| 220 | + } |
| 221 | + |
| 222 | + |
| 223 | + if (this.offset >= dataLength) |
| 224 | + { |
| 225 | + return 0; |
| 226 | + } |
| 227 | + |
| 228 | + if ((this.offset + 4) > dataLength) |
| 229 | + { |
| 230 | + this.offset = dataLength; |
| 231 | + return 0; |
| 232 | + } |
| 233 | + |
| 234 | + |
| 235 | + var result = (int)data[this.offset] + (data[this.offset + 1] << 8) + (data[this.offset + 2] << 16) + (data[this.offset + 3] << 24); |
| 236 | + |
| 237 | + this.offset += 4; |
| 238 | + return result; |
| 239 | + } |
| 240 | + |
| 241 | + |
| 242 | + /// <summary> |
| 243 | + /// read double from buffer |
| 244 | + /// </summary> |
| 245 | + public double ReadDoubleBE(int length = 8, int offset = -1) |
| 246 | + { |
| 247 | + BinaryConvertLongToDouble converterLongDouble; |
| 248 | + converterLongDouble.toDouble = 0.0; /// need to be set to avoid compiler errors |
| 249 | + |
| 250 | + converterLongDouble.toLong = ReadLongBE(offset); |
| 251 | + return converterLongDouble.toDouble; |
| 252 | + } |
| 253 | + |
| 254 | + |
| 255 | + /// <summary> |
| 256 | + /// read float from buffer |
| 257 | + /// </summary> |
| 258 | + public float ReadSingleBE(int offset = -1) |
| 259 | + { |
| 260 | + BinaryConvertIntToFloat converterIntFloat; |
| 261 | + converterIntFloat.toFloat = 0.0f; /// need to be set to avoid compiler errors |
| 262 | + |
| 263 | + converterIntFloat.toInt = ReadIntBE(offset); |
| 264 | + return converterIntFloat.toFloat; |
| 265 | + } |
| 266 | + |
| 267 | + } |
| 268 | +} |
0 commit comments