bool Stream::GetAll(void *data, int size) { if(Get(data, size) != size) { LoadError(); return false; } return true; }
bool Stream::GetAll(Huge& h, size_t size) { if(Get(h, size) != size) { LoadError(); return false; } return true; }
inline node Parser::RequireChild(node n, const char * name) { node child = n.child(name); if (! child) throw LoadError(std::string("Missing required element: \"") + name + "\""); return Visit(child); }
int Stream::_Get8() { int c = Get(); if(c < 0) { LoadError(); return -1; } return c; }
int Stream::Get16be() { byte h[2]; int q; h[1] = Get(); h[0] = q = Get(); if(q < 0) { LoadError(); return -1; } return *(word *)h; }
Ref<Object> Sound::Loader::load_from_data (const Ptr<IData> data, const ILoader * loader) { Shared<Buffer> buffer = data->buffer(); Mimetype mt = buffer->mimetype(); if (mt == AUDIO_XWAV) { return load_wave_data(data); } else { throw LoadError("Could not load audio data"); } }
int Stream::Get32be() { byte h[4]; int q; h[3] = Get(); h[2] = Get(); h[1] = Get(); h[0] = q = Get(); if(q < 0) { LoadError(); return -1; } return *(int32 *)h; }
String Stream::GetAll(int size) { String result; if(size < 4 * 1024*1024) result = Get(size); else { Huge h; Get(h, size); result = h.Get(); } if(result.GetCount() != size) { LoadError(); result = String::GetVoid(); } return result; }
int64 Stream::Get64be() { byte h[8]; int q; h[7] = Get(); h[6] = Get(); h[5] = Get(); h[4] = Get(); h[3] = Get(); h[2] = Get(); h[1] = Get(); h[0] = q = Get(); if(q < 0) { LoadError(); return -1; } return *(int64 *)h; }
void Texture::init(const char *file, GLint internalformat, GLenum format, const Parameter ¶ms) { glGenTextures(1, &m_texture); glBindTexture(GL_TEXTURE_2D, m_texture); { params.apply(); int texWidth, texHeight; unsigned char *image = SOIL_load_image(file, &texWidth, &texHeight, nullptr, SOIL_LOAD_RGB); if (image == nullptr) { throw LoadError("[" + std::string(file) + "] : failed to load"); } glTexImage2D(GL_TEXTURE_2D, 0, internalformat, texWidth, texHeight, 0, format, GL_UNSIGNED_BYTE, image); glGenerateMipmap(GL_TEXTURE_2D); SOIL_free_image_data(image); } glBindTexture(GL_TEXTURE_2D, 0); }
int Stream::GetUtf8() { int code = Get(); if(code <= 0) { LoadError(); return -1; } if(code < 0x80) return code; else if(code < 0xC2) return -1; else if(code < 0xE0) { if(IsEof()) { LoadError(); return -1; } return ((code - 0xC0) << 6) + Get() - 0x80; } else if(code < 0xF0) { int c0 = Get(); int c1 = Get(); if(c1 < 0) { LoadError(); return -1; } return ((code - 0xE0) << 12) + ((c0 - 0x80) << 6) + c1 - 0x80; } else if(code < 0xF8) { int c0 = Get(); int c1 = Get(); int c2 = Get(); if(c2 < 0) { LoadError(); return -1; } return ((code - 0xf0) << 18) + ((c0 - 0x80) << 12) + ((c1 - 0x80) << 6) + c2 - 0x80; } else if(code < 0xFC) { int c0 = Get(); int c1 = Get(); int c2 = Get(); int c3 = Get(); if(c3 < 0) { LoadError(); return -1; } return ((code - 0xF8) << 24) + ((c0 - 0x80) << 18) + ((c1 - 0x80) << 12) + ((c2 - 0x80) << 6) + c3 - 0x80; } else if(code < 0xFE) { int c0 = Get(); int c1 = Get(); int c2 = Get(); int c3 = Get(); int c4 = Get(); if(c4 < 0) { LoadError(); return -1; } return ((code - 0xFC) << 30) + ((c0 - 0x80) << 24) + ((c1 - 0x80) << 18) + ((c2 - 0x80) << 12) + ((c3 - 0x80) << 6) + c4 - 0x80; } else { LoadError(); return -1; } }
static Ref<Sound> load_wave_data (const Ptr<IData> data) { DecoderT decoder = NULL; Shared<Buffer> buffer = data->buffer(); std::size_t i = 4; uint32_t chunk_length; int32_t magic; i += buffer->read(i, chunk_length, LITTLE); i += buffer->read(i, magic, BIG); if (magic != 'WAVE') throw LoadError("Could not load WAV data"); bool found_header; uint16_t audio_format, channel_count, block_align, bits_per_sample; uint32_t sample_frequency, byte_rate; while (i < buffer->size()) { i += buffer->read(i, magic, BIG); i += buffer->read(i, chunk_length, LITTLE); if (magic == 'fmt ') { // Decode header found_header = true; i += buffer->read(i, audio_format, LITTLE); i += buffer->read(i, channel_count, LITTLE); i += buffer->read(i, sample_frequency, LITTLE); i += buffer->read(i, byte_rate, LITTLE); i += buffer->read(i, block_align, LITTLE); i += buffer->read(i, bits_per_sample, LITTLE); i += chunk_length - 16; if (audio_format == 1) { if (bits_per_sample == 8) // Copy samples verbatim. decoder = decode_linear_codec; else // Use PCM16 decoder - will pass through if endian doesn't need to be converted. decoder = decode_pcm16_codec; } else if (audio_format == 7) { //bits_per_sample *= 2; //decoder = decode_ulaw_codec; throw LoadError("Unsupported WAV encoding (ULaw)"); } else { throw LoadError("Unsupported WAV encoding (Unknown)"); } } else if (magic == 'data') { if (!found_header) throw LoadError("Corrupt or truncated data"); StaticBuffer sample_data(&(*buffer)[i], chunk_length); DREAM_ASSERT(decoder != NULL); return decoder(&sample_data, channel_count, bits_per_sample, sample_frequency); } else { // Unknown header i += chunk_length; } } throw LoadError("Corrupt or truncated data"); }
void LoadPuz(Puzzle * puz, const std::string & filename, void * /* dummy */) { std::ifstream stream(filename.c_str(), std::ios::in | std::ios::binary); if (stream.fail()) throw FileError(filename); istream_wrapper f(stream); const unsigned short c_primary = f.ReadShort(); if (strcmp(f.ReadString(12).c_str(), "ACROSS&DOWN") != 0) throw FileTypeError("puz"); const unsigned short c_cib = f.ReadShort(); unsigned char c_masked[8]; f.ReadCharArray(c_masked, 8); // Version is "[major].[minor]\0" // We can read puzzles of 1.[anything] std::string versionstr = f.ReadString(4); if (versionstr[0] != '1' || ! isdigit(versionstr[2])) throw LoadError("Unknown puz version."); const unsigned short version = 10 + versionstr[2] - 0x30; f.Skip(2); // 1 unknown short const unsigned short c_grid = f.ReadShort(); f.Skip(2 * 6); // 6 noise shorts const unsigned char width = f.ReadChar(); const unsigned char height = f.ReadChar(); const unsigned short num_clues = f.ReadShort(); const unsigned short grid_type = f.ReadShort(); const unsigned short grid_flag = f.ReadShort(); puz->GetGrid().SetCksum(c_grid); puz->GetGrid().SetType(grid_type); puz->GetGrid().SetFlag(grid_flag); puz->GetGrid().SetSize(width, height); // Read user text and solution std::string solution = f.ReadString(width * height); std::string text = f.ReadString(width * height); // Set the grid's solution and text std::string::iterator sol_it = solution.begin(); std::string::iterator text_it = text.begin(); for (Square * square = puz->GetGrid().First(); square != NULL; square = square->Next()) { // Solution if (*sol_it == '.' || *sol_it == ':' && puz->IsDiagramless()) square->SetSolution(puz::Square::Black); else if (*sol_it == '-') square->SetSolution(puz::Square::Blank); else square->SetSolution(decode_puz(std::string(1, *sol_it))); ++sol_it; // Text if (square->IsBlack() && ! puz->IsDiagramless()) square->SetText(puz::Square::Black); else if (*text_it == '-' || *text_it == 0) square->SetText(puz::Square::Blank); else if (puz->IsDiagramless() && (*text_it == '.' || *text_it == ':')) { // Black squares in a diagramless puzzle. if (*text_it == '.') square->SetText(puz::Square::Black); else if (*text_it == ':') square->SetText(puz::Square::Blank); } else { square->SetText(decode_puz(std::string(1, *text_it))); if (islower(*text_it)) square->AddFlag(FLAG_PENCIL); } ++text_it; } assert(sol_it == solution.end() && text_it == text.end()); puz->NumberGrid(); // General puzzle info puz->SetTitle(decode_puz(f.ReadString())); puz->SetAuthor(decode_puz(f.ReadString())); puz->SetCopyright(decode_puz(f.ReadString())); // Clues std::vector<string_t> clues; clues.reserve(num_clues); // Save unaltered clues for the checksums std::vector<std::string> cksum_clues; cksum_clues.reserve(num_clues); for (size_t i = 0; i < num_clues; ++i) { cksum_clues.push_back(f.ReadString()); clues.push_back(escape_xml(decode_puz(cksum_clues.back()))); } puz->SetAllClues(clues); // Notes std::string notes = f.ReadString(); puz->SetNotes(escape_xml(decode_puz(notes))); puz->SetOk(true); // Try to load the extra sections (i.e. GEXT, LTIM, etc). try { LoadSections(puz, f); } catch (std::ios::failure &) { // EOF here doesn't matter. } // Don't even bother with the checksums, since we check the validity // of the puzzle anyways }