📄 pipe_rw.cpp
字号:
/************************************************** Pipe Reading/Writing Source File ** (C) 1999-2002 The Botan Project **************************************************/#include <botan/pipe.h>#include <botan/secqueue.h>namespace Botan {/************************************************** Write into a Pipe **************************************************/SecureQueue* Pipe::get_message(const std::string& func_name, u32bit msg) const { if(msg >= messages.size()) throw Invalid_Message_Number(func_name, msg); if(messages[msg]) return messages[msg]; else throw Internal_Error("Pipe:get_message: got NULL for message #" + to_string(msg)); }/************************************************** Write into a Pipe **************************************************/void Pipe::write(const byte input[], u32bit length) { if(!locked) throw Exception("Cannot write to a Pipe while it is unlocked"); pipe->write(input, length); }/************************************************** Write a string into a Pipe **************************************************/void Pipe::write(const std::string& str) { write((const byte*)str.c_str(), str.size()); }/************************************************** Read some data from the pipe **************************************************/u32bit Pipe::read(byte output[], u32bit length, u32bit msg) { msg = ((msg != DEFAULT_MESSAGE) ? msg : default_msg()); SecureQueue* msg_queue = get_message("read", msg); if(msg_queue) return msg_queue->read(output, length); else return 0; }/************************************************** Read a single byte from the pipe **************************************************/u32bit Pipe::read(byte& out, u32bit msg) { return read(&out, 1, msg); }/************************************************** Return all data in the pipe **************************************************/SecureVector<byte> Pipe::read_all(u32bit msg) { msg = ((msg != DEFAULT_MESSAGE) ? msg : default_msg()); SecureVector<byte> buffer(remaining(msg)); read(msg, buffer, buffer.size()); return buffer; }/************************************************** Return all data in the pipe as a string **************************************************/std::string Pipe::read_all_as_string(u32bit msg) { msg = ((msg != DEFAULT_MESSAGE) ? msg : default_msg()); SecureBuffer<byte, DEFAULT_BUFFERSIZE> buffer; std::string str; while(remaining(msg)) { u32bit got = read(msg, buffer, buffer.size()); str.append((char*)buffer.ptr(), got); } return str; }/************************************************** Find out how many bytes are ready to read **************************************************/u32bit Pipe::remaining(u32bit msg) const { msg = ((msg != DEFAULT_MESSAGE) ? msg : default_msg()); SecureQueue* msg_queue = get_message("remaining", msg); if(msg_queue) return msg_queue->size(); else return 0; }/************************************************** Peek at some data in the pipe **************************************************/u32bit Pipe::peek(byte output[], u32bit length, u32bit msg) { msg = ((msg != DEFAULT_MESSAGE) ? msg : default_msg()); SecureQueue* msg_queue = get_message("peek", msg); if(msg_queue) return msg_queue->peek(output, length); else return 0; }/************************************************** Peek at a byte in the pipe **************************************************/u32bit Pipe::peek(byte& out, u32bit msg) { return peek(&out, 1, msg); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -