📄 hex.cpp
字号:
/************************************************** Hex Encoder/Decoder Source File ** (C) 1999-2002 The Botan Project **************************************************/#include <botan/hex.h>namespace Botan {/************************************************** Hex_Encoder Constructor **************************************************/Hex_Encoder::Hex_Encoder(bool breaks, u32bit length, Case c) : casing(c), LINEBREAKS(breaks), LINELENGTH(length), in_buffer(32), out_buffer(2*in_buffer.size()) { counter = position = 0; if(LINEBREAKS && (LINELENGTH == 0)) throw Invalid_Argument("Hex_Encoder: Output line lengths cannot be 0"); }/************************************************** Hex Encoding Operation **************************************************/void Hex_Encoder::encode(byte in, byte out[2], Hex_Encoder::Case casing) { static const byte BIN_TO_HEX_UPPER[] = "0123456789ABCDEF"; static const byte BIN_TO_HEX_LOWER[] = "0123456789abcdef"; const byte* BIN_TO_HEX = ((casing == Uppercase) ? BIN_TO_HEX_UPPER : BIN_TO_HEX_LOWER); out[0] = BIN_TO_HEX[((in >> 4) & 0x0F)]; out[1] = BIN_TO_HEX[((in ) & 0x0F)]; }/************************************************** Encode and send a block **************************************************/void Hex_Encoder::encode_and_send(const byte block[], u32bit length) { for(u32bit j = 0; j != length; j++) encode(block[j], out_buffer + 2*j, casing); if(LINEBREAKS) { u32bit remaining = 2*length, offset = 0; while(remaining) { u32bit sent = std::min(LINELENGTH - counter, remaining); send(out_buffer + offset, sent); counter += sent; remaining -= sent; offset += sent; if(counter == LINELENGTH) { send('\n'); counter = 0; } } } else send(out_buffer, 2*length); }/************************************************** Convert some data into hex format **************************************************/void Hex_Encoder::write(const byte input[], u32bit length) { in_buffer.copy(position, input, length); if(position + length >= in_buffer.size()) { encode_and_send(in_buffer, in_buffer.size()); input += (in_buffer.size() - position); length -= (in_buffer.size() - position); while(length >= in_buffer.size()) { encode_and_send(input, in_buffer.size()); input += in_buffer.size(); length -= in_buffer.size(); } in_buffer.copy(input, length); position = 0; } position += length; }/************************************************** Flush buffers **************************************************/void Hex_Encoder::end_msg() { encode_and_send(in_buffer, position); if(counter && LINEBREAKS) send('\n'); counter = position = 0; }/************************************************** Hex_Decoder Constructor **************************************************/Hex_Decoder::Hex_Decoder() : in_buffer(32), out_buffer(in_buffer.size()/2) { position = 0; }/************************************************** Hex Decoding Operation **************************************************/byte Hex_Decoder::decode(const byte hex[2]) { return (byte)((HEX_TO_BIN[hex[0]] << 4) | HEX_TO_BIN[hex[1]]); }/************************************************** Decode and send a block **************************************************/void Hex_Decoder::decode_and_send(const byte block[], u32bit length) { for(u32bit j = 0; j != length / 2; j++) out_buffer[j] = decode(block + 2*j); send(out_buffer, length / 2); }/************************************************** Convert some data from hex format **************************************************/void Hex_Decoder::write(const byte input[], u32bit length) { for(u32bit j = 0; j != length; j++) { if(is_valid(input[j])) in_buffer[position++] = input[j]; if(position == in_buffer.size()) { decode_and_send(in_buffer, in_buffer.size()); position = 0; } } }/************************************************** Flush buffers **************************************************/void Hex_Decoder::end_msg() { decode_and_send(in_buffer, position); position = 0; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -