📄 trans.cpp
字号:
// trans.cc// code for trans.h// copyright SafeTP Development Group, Inc., 2000 Terms of use are as specified in license.txt#include "trans.h" // this module#include "datablok.h" // DataBlock#include "exc.h" // xassert, xBase#include <limits.h> // INT_MAX#include <stdlib.h> // rand, srand, RAND_MAX#include <time.h> // time#include <stdio.h> // printf// ------------------ Trans -------------Trans::~Trans(){}int Trans::minInputSize() const{ return 0;}int Trans::maxInputSize() const{ return INT_MAX;}int Trans::minOutputSize(int inputSize) const{ return inputSize;}int Trans::maxOutputSize(int inputSize) const{ return inputSize;}// identity transformvoid Trans::trans(DataBlock &/*data*/){}// just call trans(block), but verify length guaranteesvoid Trans::checkingTrans(DataBlock &data){ // record initial data length int inlen = data.getDataLen(); // do the transformation trans(data); // get final data length int outlen = data.getDataLen(); // confirm the transformation conforms to its advertised // length specs xassert(minOutputSize(inlen) <= outlen && outlen <= maxOutputSize(inlen)); // debugging, for trimming length estimates: //printf("inlen=%d minout=%d out=%d maxout=%d\n", // inlen, minOutputSize(inlen), outlen, maxOutputSize(inlen));}// ----------------- TransPair -------------------STATICDEF void TransPair::initRandom(){ srand(time(0));}bool TransPair::testBuffer(byte const *data, int length, bool echo){ // simply filter out tests of lengths this thing can't handle if (!( encoder.minInputSize() <= length && length <= encoder.maxInputSize() )) { return true; } try { // determine the size of the block as the maximum of all // the sizes involved (original, encoded, and the result // of decoding (which normally ~= original)) int encodedLen = encoder.maxOutputSize(length); int decodedEncodedLen = decoder.maxOutputSize(encodedLen); int blockLen = mymax(length, mymax(encodedLen, decodedEncodedLen)); // create the block DataBlock block(blockLen); // enter the data we have block.setFromBlock(data, length); // copy it somewhere (only used when there is a failure) DataBlock initialData(block); // encode it encoder.checkingTrans(block); // make a copy of this, too DataBlock encodedData(block); // decode it decoder.checkingTrans(block); // compare to what we started with bool same = (initialData == block); // print something if we failed to produce the same thing, or the // caller wanted something printed regardless if (!same || echo) { if (!same) { // distinguish the failure case printf("failure report:\n"); } // print the three relevant data blocks initialData.print("initial data"); encodedData.print("encoded data"); block.print("decoded data"); if (!same) { // throw an exception to terminate tests xassert(!"transform-then-inverse failed to be identity"); } } return true; } catch (xBase &x) { // make a data block we can print DataBlock block(data, mymin(length, 16)); block.print(stringb("exceptioning block, real length = " << length)); printf("reported exception: %s\n", x.why()); return false; }}bool TransPair::test(int randIters, bool echo){ bool ok = true; #define CK if (!ok) { return false; } // test the cases that have actually exposed bugs in the past CK ok &= testBuffer((byte*)"\x86", 1, echo); CK ok &= testBuffer((byte*)"\x18\xBD", 2, echo); CK ok &= testBuffer((byte*)"pwd", 3, echo); CK ok &= testBuffer((byte*)"pwd", 4, echo); CK ok &= testBuffer((byte*)"pwd\r\n", 5, echo); CK ok &= testBuffer((byte*)"pwd\r\n", 6, echo); // test some specific cases CK ok &= testBuffer((byte*)NULL /*can't access!*/, 0, echo); // empty buffer CK ok &= testBuffer((byte*)"x" /*potential access*/, 0, echo); // empty buffer CK ok &= testBuffer((byte*)"\0\0\0\0\0\0\0\0\0\0", 10, echo); // all nulls CK ok &= testBuffer((byte*)"\xFF\xFF\xFF\xFF\xFF", 5, echo); // all 0xFF CK ok &= testBuffer((byte*)"\0abcdef", 7, echo); // starts with null CK ok &= testBuffer((byte*)"abc\0def", 7, echo); // null in middle CK ok &= testBuffer((byte*)"abcdef\0", 7, echo); // null at end // a big one with simple structure { int const big = 100000; byte *data = new byte[big]; for(int i=0; i<big; i++) { data[i] = (byte)(i & 0xFF); } CK ok &= testBuffer(data, big, echo); delete[] data; } // test some small, randomly-generated cases for(int j=0; j<randIters; j++) { // pick size int length = rand() % 200; // allocate byte *data = new byte[length]; // fill in randomly int i; for(i=0; i<length; i++) { data[i] = (byte)rand(); } // test it CK ok &= testBuffer(data, length, echo); // also test the first 10 lengths to make sure we have the // short cases exhaustively covered for(i=0; i<mymin(10,length); i++) { CK ok &= testBuffer(data, i, echo); } // deallocate delete[] data; } return ok;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -