CS144 lab1

news/2025/2/24 1:51:56/文章来源:https://www.cnblogs.com/liyishui2003/p/18733203

lab1

这次作业主要完成两个类 StreamReassembler 和 ByteStream。我的思路是每读入数据就O(n)扫一遍,用map维护每个index对应的数据,用一个指针指向当前需要写入值的位置,不断向后移动。原本没深刻认识到overleap的含义,记录的是整串string,然而测试数据相当于把一个字符串随机剁碎,字符串反而不好维护。

这是第三个版本,能过94%,15/16。没过的因为lab0的历史遗留问题,连接CS144官网时会connection reset by peer。

stream_reassembler.hh

#ifndef SPONGE_LIBSPONGE_STREAM_REASSEMBLER_HH
#define SPONGE_LIBSPONGE_STREAM_REASSEMBLER_HH#include "byte_stream.hh"#include <cstdint>
#include <climits>
#include <string>
#include <set>
#include <map>using std::string;
using std::map;//! \brief A class that assembles a series of excerpts from a byte stream (possibly out of order,
//! possibly overlapping) into an in-order byte stream.
class StreamReassembler {private:// Your code here -- add private members as necessary.size_t _capacity;    //!< The maximum number of bytesmap<size_t,char>store;ByteStream _output;  //!< The reassembled in-order byte streamsize_t reassembleByte = 0;size_t unAssemble = 0;int lastIndex = -1;public://! \brief Construct a `StreamReassembler` that will store up to `capacity` bytes.//! \note This capacity limits both the bytes that have been reassembled,//! and those that have not yet been reassembled.StreamReassembler();StreamReassembler(const size_t capacity);//! \brief Receives a substring and writes any newly contiguous bytes into the stream.//!//! If accepting all the data would overflow the `capacity` of this//! `StreamReassembler`, then only the part of the data that fits will be//! accepted. If the substring is only partially accepted, then the `eof`//! will be disregarded.//!//! \param data the string being added//! \param index the index of the first byte in `data`//! \param eof whether or not this segment ends with the end of the streamvoid push_substring(const std::string &data, const uint64_t index, const bool eof);//! \name Access the reassembled byte stream//!@{const ByteStream &stream_out() const { return _output; }ByteStream &stream_out() { return _output; }//!@}//! The number of bytes in the substrings stored but not yet reassembled//!//! \note If the byte at a particular index has been submitted twice, it//! should only be counted once for the purpose of this function.size_t unassembled_bytes() const;//! \brief Is the internal state empty (other than the output stream)?//! \returns `true` if no substrings are waiting to be assembledbool empty() const;
};#endif  // SPONGE_LIBSPONGE_STREAM_REASSEMBLER_HH

stream_reassembler.cc

#include "stream_reassembler.hh"
#include <iostream>
// Dummy implementation of a stream reassembler.// For Lab 1, please replace with a real implementation that passes the
// automated checks run by `make check_lab1`.// You will need to add private members to the class declaration in `stream_reassembler.hh`template <typename... Targs>
void DUMMY_CODE(Targs &&... /* unused */) {}using namespace std;/*
size_t _capacity;    //!< The maximum number of bytesset<dataNode>s;map<size_t,int>submited;ByteStream _output;  //!< The reassembled in-order byte streamsize_t reassembleByte = 0;bool isEof = false;
*/
StreamReassembler::StreamReassembler(const size_t capacity) : _capacity(capacity), store(), _output(capacity) {// 构造函数体可以为空,因为成员变量都已在初始化列表中初始化}//! \details This function accepts a substring (aka a segment) of bytes,
//! possibly out-of-order, from the logical stream, and assembles any newly
//! contiguous substrings and writes them into the output stream in order.void StreamReassembler::push_substring(const string &data, const size_t index, const bool eof) {size_t len = data.length();for(size_t i = 0;i < len;i++){if(store.find(index+i) != store.end() ) {}else store[index+i] = data[i],unAssemble++;}if(eof == true) lastIndex = index + len;while(store.find(reassembleByte) != store.end()){_output.write(std::string(1,store[reassembleByte]));unAssemble--;reassembleByte++;}if(lastIndex != -1 && static_cast<int>(reassembleByte) == lastIndex ){_output.end_input();}}size_t StreamReassembler::unassembled_bytes() const { return unAssemble; }// myTodo
bool StreamReassembler::empty() const { return unAssemble == 0; }

byte_stream.hh

#ifndef SPONGE_LIBSPONGE_BYTE_STREAM_HH
#define SPONGE_LIBSPONGE_BYTE_STREAM_HH#include <cstddef>
#include <cstdint>
#include <deque>
#include <list>
#include <string>
#include <utility>
#include <deque>
//! \brief An in-order byte stream.//! Bytes are written on the "input" side and read from the "output"
//! side.  The byte stream is finite: the writer can end the input,
//! and then no more bytes can be written.
class ByteStream {private:// Your code here -- add private members as necessary.size_t capacity;std::deque<char> buffer;size_t writtenByte = 0;size_t readByte = 0;bool endInput = false;bool _error{};  //!< Flag indicating that the stream suffered an error.public://! Construct a stream with room for `capacity` bytes.ByteStream(const size_t capacity);//! \name "Input" interface for the writer//!@{//! Write a string of bytes into the stream. Write as many//! as will fit, and return how many were written.//! \returns the number of bytes accepted into the streamsize_t write(const std::string &data);//! \returns the number of additional bytes that the stream has space forsize_t remaining_capacity() const;//! Signal that the byte stream has reached its endingvoid end_input();//! Indicate that the stream suffered an error.void set_error() { _error = true; }//!@}//! \name "Output" interface for the reader//!@{//! Peek at next "len" bytes of the stream//! \returns a stringstd::string peek_output(const size_t len) const;//! Remove bytes from the buffervoid pop_output(const size_t len);//! Read (i.e., copy and then pop) the next "len" bytes of the stream//! \returns a vector of bytes readstd::string read(const size_t len) {const auto ret = peek_output(len);pop_output(len);return ret;}//! \returns `true` if the stream input has endedbool input_ended() const;//! \returns `true` if the stream has suffered an errorbool error() const { return _error; }//! \returns the maximum amount that can currently be read from the streamsize_t buffer_size() const;//! \returns `true` if the buffer is emptybool buffer_empty() const;//! \returns `true` if the output has reached the endingbool eof() const;//!@}//! \name General accounting//!@{//! Total number of bytes writtensize_t bytes_written() const;//! Total number of bytes poppedsize_t bytes_read() const;//!@}
};#endif  // SPONGE_LIBSPONGE_BYTE_STREAM_HH

byte_stream.cc

#include "byte_stream.hh"#include <algorithm>
#include <iterator>
#include <stdexcept>
#include <iostream>
// Dummy implementation of a flow-controlled in-memory byte stream.// For Lab 0, please replace with a real implementation that passes the
// automated checks run by `make check_lab0`.// You will need to add private members to the class declaration in `byte_stream.hh`template <typename... Targs>
void DUMMY_CODE(Targs &&... /* unused */) {}using namespace std;ByteStream::ByteStream(const size_t Capacity):capacity(Capacity),buffer(){ }size_t ByteStream::write(const string &data) {size_t len = data.length();size_t bufferLeft = capacity - buffer.size();size_t i;for(i = 0;i < len && bufferLeft;i++,bufferLeft--){writtenByte++;buffer.push_back(data[i]);}return i;
}//! \param[in] len bytes will be copied from the output side of the buffer
string ByteStream::peek_output(const size_t len) const {size_t i;std::string s = "";for(i = 0;i < min(len,buffer.size());i++){s.push_back(buffer[i]);}return s;
}//! \param[in] len bytes will be removed from the output side of the buffer
void ByteStream::pop_output(const size_t len) { size_t i;for(i = 0;i < len;i++){readByte++;buffer.pop_front();}}void ByteStream::end_input() { endInput = true; }bool ByteStream::input_ended() const { return endInput; }size_t ByteStream::buffer_size() const { return buffer.size(); }bool ByteStream::buffer_empty() const { return buffer.empty(); }bool ByteStream::eof() const { return endInput && buffer.empty(); }size_t ByteStream::bytes_written() const { return writtenByte; }size_t ByteStream::bytes_read() const { return readByte; }size_t ByteStream::remaining_capacity() const { return capacity - buffer.size(); }

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/888646.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

uniapp+h5---进行混合开发

uniapp和h5进行混合开发,最近在接手开发微信小程序,在技术选型的时候,拟采用uniapp+h5进行混合开发。 想必为啥要使用uniapp开发,就不用多说了?就是为了进行跨平台开发。如果使用微信小程序的开发方式,开发出来的应用就只能在微信小程序使用,但是使用uniapp就可以多端开…

dokcer-compose方式部署 mongo集群

声明:本人在单台机器上部署的mongo机器, mongo版本为8.0, 这玩意居然部署了一天,终于搞好了, 希望对后面想要部署的人有帮助 先创建一个mongodb目录, 后续的配置,数据都存放在这个目录中: 1. 创建mongo-secrets目录,mkdir mongo-secrets 2. 创建keyfile文件, openssl…

两个终端小玩具:Yazi和elinks

1. 终端文件管理器yazi 可以很方便地查找文件,跳转,并且很容易看出文件 1.1 下载安装 参考官方地址:鸭子官方安装手册安装rust编译环境curl --proto =https --tlsv1.2 -sSf https://sh.rustup.rs | sh rustup update编译yazigit clone https://github.com/sxyazi/yazi.git c…

两个终端小玩具

1. 终端浏览器 1.1 m3w 1.2 elinks 2. 终端文件管理器yazi 可以很方便地查找文件,跳转,并且很容易看出文件 2.1 下载安装 参考官方地址:鸭子官方安装手册安装rust编译环境curl --proto =https --tlsv1.2 -sSf https://sh.rustup.rs | sh rustup update编译yazigit clone htt…

用大模型DeepSeek分析一篇小公司创业失败的文章,失败的原因有哪些?

第一步:把原文保存为Doc格式文档 原本我想直接让 DeepSeek 分析链接的文章,但是两次写提示词后让 DeepSeek 深度分析,都失败了,它说无法直接访问,所以就无法直接分析原文内容。但还是基于搜索的结果进行普遍原因分析。 于是就把原文直接保存为 Doc,上传到 DeepSeek 让它分…

Markdowm学习

标题 二级标题 三级标题 四级标题 五级标题 六级标题 (#*n+空格+标题内容+回车——n级标题 注:此方法最多为六级标题) 字体样式 Hello World (两边加**为粗体) Hello World (两边加*为斜体) Hello World (两边加***为粗体+斜体) Hello World (两边加~~为删除线) 引用h…

区块链模型原理入门学习2——细化模型

以上描述中,存在一些非生产情景的理想化设定。比如:1.没有设计谜题难度平衡 2.没有加入加密校验数字签证 3.没有设计个人钱包 4.广播问题【敬畏能量 敬畏自然】

upload-labs/Pass-12 白名单检测 - %00 截断 GET

save_path 为客户端向服务器端传递的额外信息,可能用此参数指定上传文件的保存目录将save_path 更改为../upload/test.php%00 filename=test.png,filename 的后缀名需要满足白名单 路径和文件名组合在一起会变成../upload/test.php%00test.png , 后缀名满足白名单那么文件就…

upload-labs/Pass-13 白名单检测 - %00 截断 POST

POST 数据包的路径在请求体中在请求体中添加文件名称test.php, 但是不能像GET请求的数据包一样直接添加%00 了,我们需要直接在hex 中将标记修改为00;同时记得把文件名修改为可以上传的后缀重放数据包即可成功绕过上传

upload-labs/Pass-14 Pass-15 图片码绕过

copy .\test.png/b+.\test.php/a kb.png /b:以二进制模式 读取文件 .\test.png /b:以二进制读取test.png /a:以 ASCII 文本模式 读取文件,遇到第一个 EOF(文件结束符,如 0x1A)时停止读取。 .\as.php /a:以 ASCII 文本模式 读取as.php +:表示合并操作,将多个文件内容拼…

upload-labs/Pass-07 黑名单检测 -空格绕过

代码中没有对文件左右两侧去除空格,在文件名后面添加空格不影响文件执行,因此可以绕过

upload-labs/Pass-06 黑名单检测 - 后缀大小写绕过

利用Windows对大小写不敏感的特性。代码中没有对文件大小写做归一,更改文件后缀名大小写可以绕过黑名单