diff --git a/computer_networks/lab/lab4/BitStuffing.cpp b/computer_networks/lab/lab4/BitStuffing.cpp new file mode 100644 index 0000000..15ed84e --- /dev/null +++ b/computer_networks/lab/lab4/BitStuffing.cpp @@ -0,0 +1,55 @@ +/** + * /mnt/z/my_git/sixth-semester/computer_networks/lab/lab4/BitStuffing.cpp + * @file BitStuffing.cpp + * @author Avinal Kumar + * @since February 17, 2021 + * + * Bit Stuffing Implementation in CPP + */ + +#include +#include + +std::string bit_stuff(std::string const data) { + std::string stuffed = ""; + size_t last_pos = 0; + while (true) { + size_t one = data.find("11111", last_pos); + if (one != std::string::npos) { + stuffed += data.substr(last_pos, one - last_pos + 5); + stuffed += '0'; + last_pos = one + 5; + } else { + break; + } + } + stuffed += data.substr(last_pos); + return stuffed; +} + +std::string bit_unstuff(std::string const data) { + std::string unstuff = ""; + size_t last_pos = 0; + while (true) { + size_t one = data.find("11111", last_pos); + if (one != std::string::npos) { + unstuff += data.substr(last_pos, one - last_pos + 5); + last_pos = one + 6; + } else { + break; + } + } + unstuff += data.substr(last_pos); + return unstuff; +} + +int main(int argc, char const *argv[]) { + std::string data = ""; + std::cout << "Enter data stream consisting of 0 and 1 only:" << std::endl; + std::cin >> data; + std::string stuff = bit_stuff(data); + std::cout << "Data after stuffing bits: " << stuff << std::endl; + std::cout << "Data after unstuffing: " << bit_unstuff(stuff) << std::endl; + std::cout << std::endl; + return 0; +} diff --git a/computer_networks/lab/lab4/ByteStuffing.cpp b/computer_networks/lab/lab4/ByteStuffing.cpp new file mode 100644 index 0000000..2a022dd --- /dev/null +++ b/computer_networks/lab/lab4/ByteStuffing.cpp @@ -0,0 +1,61 @@ +/** + * /mnt/z/my_git/sixth-semester/computer_networks/lab/lab4/ByteStuffing.cpp + * @file ByteStuffing.cpp + * @author Avinal Kumar + * @since February 17, 2021 + * + * Byte Stuffing Implementation in C++ + */ + +#include + +std::string byte_stuff(std::string const data, std::string const escape) { + std::string stuffed = "0x"; + size_t last_pos = 2; + while (true) { + size_t esc = data.find(escape, last_pos); + if (esc != std::string::npos) { + stuffed += data.substr(last_pos, esc - last_pos); + stuffed += "5D"; + stuffed += escape; + last_pos = esc + 2; + } else { + break; + } + } + stuffed += data.substr(last_pos); + return stuffed; +} + +std::string byte_unstuff(std::string const data) { + std::string unstuff = "0x"; + size_t last_pos = 2; + while (true) { + size_t esc = data.find("5D", last_pos); + if (esc != std::string::npos) { + unstuff += data.substr(last_pos, esc - last_pos); + unstuff += data.substr(esc + 2, 2); + last_pos = esc + 4; + } else { + break; + } + } + unstuff += data.substr(last_pos); + return unstuff; +} + +int main(int argc, char const *argv[]) { + std::string data = ""; + std::cout << "Enter hexadecimal data string starting with 0x: " << std::endl; + std::cin >> data; + std::cout << "Flag to escape: "; + std::string escape = ""; + std::cin >> escape; + std::string stuff = byte_stuff(data, "5D"); + stuff = byte_stuff(stuff, escape); + std::cout << "Data after Byte stuffing: " << stuff << std::endl; + std::string unstuff = byte_unstuff(stuff); + std::cout << "Data after unstuffing: " << unstuff << std::endl; + std::cout << std::endl; + return 0; +}