| 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
| 2 | /* |
| 3 | * This file is a part of the libzmf project. |
| 4 | * |
| 5 | * This Source Code Form is subject to the terms of the Mozilla Public |
| 6 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 7 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 8 | */ |
| 9 | |
| 10 | #include "libzmf_utils.h" |
| 11 | |
| 12 | #include <unicode/ucnv.h> |
| 13 | #include <unicode/utypes.h> |
| 14 | |
| 15 | #ifdef DEBUG |
| 16 | #include <cstdarg> |
| 17 | #include <cstdio> |
| 18 | #endif |
| 19 | |
| 20 | #include <cstring> |
| 21 | #include <memory> |
| 22 | |
| 23 | #include <boost/math/constants/constants.hpp> |
| 24 | |
| 25 | namespace libzmf |
| 26 | { |
| 27 | |
| 28 | namespace |
| 29 | { |
| 30 | |
| 31 | void checkStream(const RVNGInputStreamPtr &input) |
| 32 | { |
| 33 | if (!input || input->isEnd()) |
| 34 | throw EndOfStreamException(); |
| 35 | } |
| 36 | |
| 37 | struct SeekFailedException {}; |
| 38 | |
| 39 | static void _appendUCS4(librevenge::RVNGString &text, unsigned ucs4Character) |
| 40 | { |
| 41 | unsigned char first; |
| 42 | int len; |
| 43 | if (ucs4Character < 0x80) |
| 44 | { |
| 45 | first = 0; |
| 46 | len = 1; |
| 47 | } |
| 48 | else if (ucs4Character < 0x800) |
| 49 | { |
| 50 | first = 0xc0; |
| 51 | len = 2; |
| 52 | } |
| 53 | else if (ucs4Character < 0x10000) |
| 54 | { |
| 55 | first = 0xe0; |
| 56 | len = 3; |
| 57 | } |
| 58 | else if (ucs4Character < 0x200000) |
| 59 | { |
| 60 | first = 0xf0; |
| 61 | len = 4; |
| 62 | } |
| 63 | else if (ucs4Character < 0x4000000) |
| 64 | { |
| 65 | first = 0xf8; |
| 66 | len = 5; |
| 67 | } |
| 68 | else |
| 69 | { |
| 70 | first = 0xfc; |
| 71 | len = 6; |
| 72 | } |
| 73 | |
| 74 | char outbuf[7] = { 0 }; |
| 75 | int i; |
| 76 | for (i = len - 1; i > 0; --i) |
| 77 | { |
| 78 | outbuf[i] = char((ucs4Character & 0x3f) | 0x80); |
| 79 | ucs4Character >>= 6; |
| 80 | } |
| 81 | outbuf[0] = char((ucs4Character & 0xff) | first); |
| 82 | outbuf[len] = '\0'; |
| 83 | |
| 84 | text.append(outbuf); |
| 85 | } |
| 86 | |
| 87 | } |
| 88 | |
| 89 | uint8_t readU8(const RVNGInputStreamPtr &input, bool /* bigEndian */) |
| 90 | { |
| 91 | checkStream(input); |
| 92 | |
| 93 | unsigned long numBytesRead; |
| 94 | uint8_t const *p = input->read(sizeof(uint8_t), numBytesRead); |
| 95 | |
| 96 | if (p && numBytesRead == sizeof(uint8_t)) |
| 97 | return *(uint8_t const *)(p); |
| 98 | throw EndOfStreamException(); |
| 99 | } |
| 100 | |
| 101 | uint16_t readU16(const RVNGInputStreamPtr &input, bool bigEndian) |
| 102 | { |
| 103 | checkStream(input); |
| 104 | |
| 105 | unsigned long numBytesRead; |
| 106 | uint8_t const *p = input->read(sizeof(uint16_t), numBytesRead); |
| 107 | |
| 108 | if (p && numBytesRead == sizeof(uint16_t)) |
| 109 | { |
| 110 | if (bigEndian) |
| 111 | return static_cast<uint16_t>((uint16_t)p[1]|((uint16_t)p[0]<<8)); |
| 112 | return static_cast<uint16_t>((uint16_t)p[0]|((uint16_t)p[1]<<8)); |
| 113 | } |
| 114 | throw EndOfStreamException(); |
| 115 | } |
| 116 | |
| 117 | uint32_t readU32(const RVNGInputStreamPtr &input, bool bigEndian) |
| 118 | { |
| 119 | checkStream(input); |
| 120 | |
| 121 | unsigned long numBytesRead; |
| 122 | uint8_t const *p = input->read(sizeof(uint32_t), numBytesRead); |
| 123 | |
| 124 | if (p && numBytesRead == sizeof(uint32_t)) |
| 125 | { |
| 126 | if (bigEndian) |
| 127 | return (uint32_t)p[3]|((uint32_t)p[2]<<8)|((uint32_t)p[1]<<16)|((uint32_t)p[0]<<24); |
| 128 | return (uint32_t)p[0]|((uint32_t)p[1]<<8)|((uint32_t)p[2]<<16)|((uint32_t)p[3]<<24); |
| 129 | } |
| 130 | throw EndOfStreamException(); |
| 131 | } |
| 132 | |
| 133 | uint64_t readU64(const RVNGInputStreamPtr &input, bool bigEndian) |
| 134 | { |
| 135 | checkStream(input); |
| 136 | |
| 137 | unsigned long numBytesRead; |
| 138 | uint8_t const *p = input->read(sizeof(uint64_t), numBytesRead); |
| 139 | |
| 140 | if (p && numBytesRead == sizeof(uint64_t)) |
| 141 | { |
| 142 | if (bigEndian) |
| 143 | return (uint64_t)p[7]|((uint64_t)p[6]<<8)|((uint64_t)p[5]<<16)|((uint64_t)p[4]<<24)|((uint64_t)p[3]<<32)|((uint64_t)p[2]<<40)|((uint64_t)p[1]<<48)|((uint64_t)p[0]<<56); |
| 144 | return (uint64_t)p[0]|((uint64_t)p[1]<<8)|((uint64_t)p[2]<<16)|((uint64_t)p[3]<<24)|((uint64_t)p[4]<<32)|((uint64_t)p[5]<<40)|((uint64_t)p[6]<<48)|((uint64_t)p[7]<<56); |
| 145 | } |
| 146 | throw EndOfStreamException(); |
| 147 | } |
| 148 | |
| 149 | int32_t readS32(const RVNGInputStreamPtr &input, bool bigEndian) |
| 150 | { |
| 151 | return (int32_t) readU32(input, bigEndian); |
| 152 | } |
| 153 | |
| 154 | float readFloat(const RVNGInputStreamPtr &input, bool bigEndian) |
| 155 | { |
| 156 | uint32_t u = readU32(input, bigEndian); |
| 157 | float f; |
| 158 | std::memcpy(&f, &u, sizeof(f)); |
| 159 | return f; |
| 160 | } |
| 161 | |
| 162 | const unsigned char *readNBytes(const RVNGInputStreamPtr &input, const unsigned long numBytes) |
| 163 | { |
| 164 | checkStream(input); |
| 165 | |
| 166 | unsigned long readBytes = 0; |
| 167 | const unsigned char *const s = input->read(numBytes, readBytes); |
| 168 | |
| 169 | if (numBytes != readBytes) |
| 170 | throw EndOfStreamException(); |
| 171 | |
| 172 | return s; |
| 173 | } |
| 174 | |
| 175 | void skip(const RVNGInputStreamPtr &input, unsigned long numBytes) |
| 176 | { |
| 177 | checkStream(input); |
| 178 | |
| 179 | seekRelative(input, static_cast<long>(numBytes)); |
| 180 | } |
| 181 | |
| 182 | void seek(const RVNGInputStreamPtr &input, const unsigned long pos) |
| 183 | { |
| 184 | if (!input) |
| 185 | throw EndOfStreamException(); |
| 186 | if (0 != input->seek(static_cast<long>(pos), librevenge::RVNG_SEEK_SET)) |
| 187 | throw SeekFailedException(); |
| 188 | } |
| 189 | |
| 190 | void seekRelative(const RVNGInputStreamPtr &input, const long pos) |
| 191 | { |
| 192 | if (!input) |
| 193 | throw EndOfStreamException(); |
| 194 | if (0 != input->seek(pos, librevenge::RVNG_SEEK_CUR)) |
| 195 | throw SeekFailedException(); |
| 196 | } |
| 197 | |
| 198 | unsigned long getLength(const RVNGInputStreamPtr &input) |
| 199 | { |
| 200 | checkStream(input); |
| 201 | |
| 202 | const long begin = input->tell(); |
| 203 | |
| 204 | if (input->seek(0, librevenge::RVNG_SEEK_END) != 0) |
| 205 | { |
| 206 | // RVNG_SEEK_END does not work. Use the harder way. |
| 207 | while (!input->isEnd()) |
| 208 | readU8(input); |
| 209 | } |
| 210 | long end = input->tell(); |
| 211 | if (end < begin) |
| 212 | throw SeekFailedException(); |
| 213 | |
| 214 | seek(input, begin); |
| 215 | |
| 216 | return static_cast<unsigned long>(end - begin); |
| 217 | } |
| 218 | |
| 219 | void appendCharacters(librevenge::RVNGString &text, const unsigned char *characters, uint32_t size, |
| 220 | const char *encoding) |
| 221 | { |
| 222 | if (size == 0) |
| 223 | { |
| 224 | ZMF_DEBUG_MSG(("Attempt to append 0 characters!")); |
| 225 | return; |
| 226 | } |
| 227 | |
| 228 | UErrorCode status = U_ZERO_ERROR; |
| 229 | std::unique_ptr<UConverter, void(*)(UConverter *)> conv(ucnv_open(encoding, &status), ucnv_close); |
| 230 | if (U_SUCCESS(status)) |
| 231 | { |
| 232 | // ICU documentation claims that character-by-character processing is faster "for small amounts of data" and "'normal' charsets" |
| 233 | // (in any case, it is more convenient :) ) |
| 234 | const char *src = (const char *)&characters[0]; |
| 235 | const char *srcLimit = (const char *)src + size; |
| 236 | while (src < srcLimit) |
| 237 | { |
| 238 | uint32_t ucs4Character = (uint32_t)ucnv_getNextUChar(conv.get(), &src, srcLimit, &status); |
| 239 | if (U_SUCCESS(status)) |
| 240 | { |
| 241 | _appendUCS4(text, ucs4Character); |
| 242 | } |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | void writeU16(librevenge::RVNGBinaryData &buffer, const int value) |
| 248 | { |
| 249 | buffer.append((unsigned char)(value & 0xFF)); |
| 250 | buffer.append((unsigned char)((value >> 8) & 0xFF)); |
| 251 | } |
| 252 | |
| 253 | void writeU32(librevenge::RVNGBinaryData &buffer, const int value) |
| 254 | { |
| 255 | buffer.append((unsigned char)(value & 0xFF)); |
| 256 | buffer.append((unsigned char)((value >> 8) & 0xFF)); |
| 257 | buffer.append((unsigned char)((value >> 16) & 0xFF)); |
| 258 | buffer.append((unsigned char)((value >> 24) & 0xFF)); |
| 259 | } |
| 260 | |
| 261 | double rad2deg(double value) |
| 262 | { |
| 263 | using namespace boost::math::double_constants; |
| 264 | |
| 265 | return normalizeAngle(value) * radian; |
| 266 | } |
| 267 | |
| 268 | double normalizeAngle(double radAngle) |
| 269 | { |
| 270 | using namespace boost::math::double_constants; |
| 271 | radAngle = std::fmod(radAngle, two_pi); |
| 272 | if (radAngle < 0) |
| 273 | radAngle += two_pi; |
| 274 | return radAngle; |
| 275 | } |
| 276 | |
| 277 | #ifdef DEBUG |
| 278 | void debugPrint(const char *format, ...) |
| 279 | { |
| 280 | va_list args; |
| 281 | va_start(args, format); |
| 282 | std::vfprintf(stderr, format, args); |
| 283 | va_end(args); |
| 284 | } |
| 285 | #endif |
| 286 | |
| 287 | EndOfStreamException::EndOfStreamException() |
| 288 | { |
| 289 | } |
| 290 | |
| 291 | } |
| 292 | |
| 293 | /* vim:set shiftwidth=2 softtabstop=2 expandtab: */ |