range type_name() { // TODO: support other compilers etc. char const *name = typeid(T).name(); if (*name == 'N') { ++name; int length; do { length = *name - '0'; ++name; while (*name >= '0' && *name <= '9') { length *= 10; length += *name - '0'; ++name; } name += length; } while (*name != 'E'); return range(name - length, name); } else { while (*name >= '0' && *name <= '9') ++name; return to_range(name); } }
void new_buffer_(){ if (map_dest){ wview = map_dest->view_wr(ext_heap::handle(zstream.total_out, zstream.total_out + K_CompressedViewSize)); if (wview.get<io::write_view>().address().size() != K_CompressedViewSize) AIO_THROW(deflate_exception)("Failed to request write view"); auto addr = wview.get<io::write_view>().address(); zstream.next_out = (Bytef*)addr.begin(); zstream.avail_out = uInt(addr.size()); } else{ crc = crc32(to_range(buf), crc); stream_dest->write(to_range(buf)); buf.resize(K_CompressedViewSize); zstream.next_out = (Bytef*)buf.begin(); zstream.avail_out = uInt(buf.size()); } }
uint32_t crc32(io::reader& src, uint32_t crc /* = crc32_init()*/){ buffer<byte> bin; bin.resize(K_BufferSize); while (src.readable()){ auto rest = src.read(to_range(bin)); crc = ::crc32(crc, (const Bytef*)bin.begin(), (uInt)(bin.size() - rest.size())); } return crc; }
static void set_material_values(t_env *e, char *pt1, char *pt2) { t_split_string values; values = ft_nstrsplit(pt2, ' '); if (!ft_strcmp(pt1, "NAME")) { ft_strdel(&e->material[e->materials]->name); e->material[e->materials]->name = ft_strdup(values.strings[0]); } else if (!ft_strcmp(pt1, "DIFFUSE")) e->material[e->materials]->diff = get_colour(e, values); else if (!ft_strcmp(pt1, "SPECULAR")) e->material[e->materials]->spec = get_colour(e, values); else if (!ft_strcmp(pt1, "REFLECT")) e->material[e->materials]->reflect = to_range(ft_atod(values.strings[0]), 0.0, 1.0); else if (!ft_strcmp(pt1, "REFRACT")) e->material[e->materials]->refract = to_range(ft_atod(values.strings[0]), 0.0, 1.0); else if (!ft_strcmp(pt1, "IOR")) e->material[e->materials]->ior = ft_atod(values.strings[0]); ft_free_split(&values); }
void new_input_(){ if (map_src){ long_size_t rest_in_size = map_src->size() - zstream.total_in; if (rest_in_size > 0){ typedef ext_heap::handle handle; handle hin(zstream.total_in, zstream.total_in + std::min(K_UncompressedViewSize, rest_in_size)); rview = map_src->view_rd(hin); auto bin = rview.get<io::read_view>().address(); zstream.avail_in = uInt(bin.size()); zstream.next_in = (Bytef*)bin.begin(); crc = crc32(bin, crc); } } else if(stream_src->readable()){ buf.resize(K_UncompressedViewSize); auto res = stream_src->read(to_range(buf)); buf.resize(buf.size() - res.size()); zstream.avail_in = uInt(buf.size()); zstream.next_in = (Bytef*)buf.begin(); } }
void reader_writer::sync(){ AIO_PRE_CONDITION(valid()); if (!m_imp->dirty) return; bool zip64_enabled = false; long_size_t cd_size = 0; for (auto & i : m_imp->items){ file_header& h = i; cd_size += K_fix_part_of_central_header + h.name.str().size() + h.comments.size(); auto need_zip64 = h.compressed_size >= uint32_t(-1) || h.uncompressed_size >= uint32_t(-1) || h.relative_offset_ >= uint32_t(-1); zip64_enabled = zip64_enabled || need_zip64; if (need_zip64) cd_size += h.extra.size(); } if (m_imp->items.size() >= uint16_t(-1) || cd_size >= uint32_t(-1) || m_imp->end_of_last >= uint32_t(-1)) zip64_enabled = true; uint16_t num_entries = m_imp->items.size() >= uint16_t(-1) ? uint16_t(-1) : uint16_t(m_imp->items.size()); auto cd_and_end_size = cd_size + K_sizeof_cd_end; if (zip64_enabled) cd_and_end_size += K_sizeof_zip64_end_cd + K_sizeof_zip64_locator; auto view = m_imp->archive.get<io::write_map>().view_wr(ext_heap::handle(m_imp->end_of_last, m_imp->end_of_last + cd_and_end_size)); auto address = view.get<io::write_view>().address(); io::fixed_buffer_io bout(address); auto saver = io::exchange::as_sink(bout); for (auto & i : m_imp->items){ file_header& h = i; auto need_zip64 = h.compressed_size >= uint32_t(-1) || h.uncompressed_size >= uint32_t(-1) || h.relative_offset_ >= uint32_t(-1); saver & K_sig_central_file_header & h.creator_version & h.reader_version & h.flags & h.method & h.modified_time & h.modified_date & h.crc32; if (need_zip64) saver & uint32_t(-1) & uint32_t(-1); else saver & uint32_t(h.compressed_size) & uint32_t(h.uncompressed_size); saver & uint16_t(h.name.str().size()) & uint16_t(need_zip64 ? h.extra.size() : 0) & uint16_t(h.comments.size()) & uint16_t(0) //disk number start & uint16_t(0) //internal file attributes & h.external_attrs; if (need_zip64) saver & uint32_t(-1); else saver & uint32_t(h.relative_offset_); bout.write(make_range(reinterpret_cast<const byte*>(h.name.str().begin()) , reinterpret_cast<const byte*>(h.name.str().end()))); if (need_zip64) bout.write(to_range(h.extra)); bout.write(make_range(reinterpret_cast<const byte*>(h.comments.begin()) , reinterpret_cast<const byte*>(h.comments.end()))); } if (zip64_enabled){ saver & K_sig_zip64_end_cd & uint64_t(cd_size) & uint16_t(20) //version made by & uint16_t(20) //version needed to extract & uint32_t(0) //number of this disk & uint32_t(0) //number of the disk with the start of the central directory & uint64_t(m_imp->items.size()) //total number of entries in the central directory on this disk & uint64_t(m_imp->items.size()) //total number of entries in the central directory & uint64_t(cd_size) //size of the central directory & uint64_t(m_imp->end_of_last); //offset of start of central directory with respect to the starting disk number saver & K_sig_zip64_end_of_cd_locator & uint32_t(0) //number of the disk with the start of the zip64 end of central directory & uint64_t(cd_size + m_imp->end_of_last) //relative offset of the zip64 end of central directory record & uint32_t(1); //total number of disks } saver & K_sig_end_central_dir_signature & uint16_t(0) //number of this disk & uint16_t(0) //number of the disk with the start of the central directory & num_entries //total number of entries in the central directory on this disk & num_entries //total number of entries in the central directory & (cd_size >= uint32_t(-1)? uint32_t(-1) : uint32_t(cd_size)) //size of the central directory & (m_imp->end_of_last >= uint32_t(-1)? uint32_t(-1) : uint32_t(m_imp->end_of_last)) //offset of start of central directory with respect to the starting disk number & uint16_t(0); //.ZIP file comment length m_imp->archive.get<io::write_map>().sync(); m_imp->dirty = false; }
const file_header* append_(IoType& ar, const file_header& h_, file_type type, zip_package_writer_imp * m_imp){ file_header h = h_; AIO_PRE_CONDITION(m_imp); AIO_PRE_CONDITION(h.method == cm_deflate || h.method == cm_store); AIO_PRE_CONDITION(h.name.str().size() < uint16_t(-1) ); AIO_PRE_CONDITION(h.comments.size() < uint16_t(-1) ); if (m_imp->exist_(h.name)) return 0; auto header_size = K_fix_part_of_local_header + h.name.str().size() + K_sizeof_zip64_extra_field; auto dest_map = io::decorate<io::tail_archive , io::tail_read_map_p , io::tail_write_map_p >(m_imp->archive, m_imp->end_of_last + header_size); m_imp->dirty = true; if (h.method == cm_deflate){ if (type == ft_raw){ deflate_writer d_writer(dest_map); iref<io::writer> dest(d_writer); io::copy_data(ar, dest.get<io::writer>()); d_writer.finish(); h.uncompressed_size = d_writer.uncompressed_size(); h.compressed_size = d_writer.size(); h.crc32 = d_writer.crc32(); } else {// type == ft_defalted io::copy_data(ar, dest_map); } } else { // == cm_store auto size = io::copy_data(ar, dest_map); h.uncompressed_size = size; h.compressed_size = size; h.crc32 = crc32(dest_map); } setDateTime(h); h.relative_offset_ = m_imp->end_of_last; bool zip64_enabled = (h.uncompressed_size >= uint32_t(-1) || h.compressed_size >= uint32_t(-1) || h.relative_offset_ >= uint32_t(-1) ); { // fill extra data for zip64 io::buffer_out bout(h.extra); auto saver = io::exchange::as_sink(bout); save(saver, uint16_t(K_z64_extra_id)); // save(saver, uint16_t(28)); //sizeof extra data save(saver, uint64_t(h.uncompressed_size)); save(saver, uint64_t(h.compressed_size)); save(saver, uint64_t(h.relative_offset_)); save(saver, uint32_t(0)); //Disk Start Number } auto hview = m_imp->archive.get<io::write_map>().view_wr(ext_heap::handle(m_imp->end_of_last , m_imp->end_of_last + header_size)); io::fixed_buffer_io bout(hview.get<io::write_view>().address()); auto saver = io::exchange::as_sink(bout); saver & K_sig_local_eader; saver & h.reader_version & h.flags & h.method & h.modified_time & h.modified_date & h.crc32; if (zip64_enabled) saver & uint32_t(-1) & uint32_t(-1); else saver & uint32_t(h.compressed_size) & uint32_t(h.uncompressed_size); saver & uint16_t(h.name.str().size()) & uint16_t(h.extra.size()); bout.write(make_range(reinterpret_cast<const byte*>(h.name.str().begin()) , reinterpret_cast<const byte*>(h.name.str().end()))); bout.write(to_range(h.extra)); h.local_header_size_ = header_size; m_imp->items.push_back(h); m_imp->end_of_last += header_size + h.compressed_size; return &m_imp->items.back(); }