コード例 #1
0
TEST(fpdf_parser_decode, HexDecode) {
  {
    // Empty src string.
    uint8_t* dest = nullptr;
    FX_DWORD dest_size;
    uint8_t src[] = "";
    EXPECT_EQ(0, HexDecode(src, 0, dest, dest_size));
    EXPECT_EQ(0, dest_size);
    EXPECT_EQ('\0', dest[0]);
    FX_Free(dest);
  }

  {
    // Regular conversion.
    uint8_t* dest = nullptr;
    FX_DWORD dest_size;
    uint8_t src[] = "12Ac>zzz";
    EXPECT_EQ(5, HexDecode(src, 8, dest, dest_size));
    EXPECT_EQ(2, dest_size);
    EXPECT_EQ(18, dest[0]);
    EXPECT_EQ(172, dest[1]);
    FX_Free(dest);
  }

  {
    // Non-multiple length.
    uint8_t* dest = nullptr;
    FX_DWORD dest_size;
    uint8_t src[] = "12A>zzz";
    EXPECT_EQ(4, HexDecode(src, 8, dest, dest_size));
    EXPECT_EQ(2, dest_size);
    EXPECT_EQ(18, dest[0]);
    EXPECT_EQ(160, dest[1]);
    FX_Free(dest);
  }

  {
    // Skips unknown characters.
    uint8_t* dest = nullptr;
    FX_DWORD dest_size;
    uint8_t src[] = "12tk  \tAc>zzz";
    EXPECT_EQ(10, HexDecode(src, 13, dest, dest_size));
    EXPECT_EQ(2, dest_size);
    EXPECT_EQ(18, dest[0]);
    EXPECT_EQ(172, dest[1]);
    FX_Free(dest);
  }
}
コード例 #2
0
ファイル: hexadecimal.cpp プロジェクト: d235j/Principia
void BM_DecodePi(benchmark::State& state) {  // NOLINT(runtime/references)
  bool correct = true;
  state.PauseTiming();
  std::vector<uint8_t> input_digits = Digits();
  std::vector<uint8_t> expected_bytes = Bytes();
  state.ResumeTiming();
  while (state.KeepRunning()) {
    HexDecode(&state, &correct, input_digits, expected_bytes);
  }
  std::stringstream ss;
  ss << correct;
  state.SetLabel(ss.str());
}
コード例 #3
0
FX_DWORD PDF_DecodeInlineStream(const uint8_t* src_buf,
                                FX_DWORD limit,
                                int width,
                                int height,
                                CFX_ByteString& decoder,
                                CPDF_Dictionary* pParam,
                                uint8_t*& dest_buf,
                                FX_DWORD& dest_size) {
  if (decoder == FX_BSTRC("CCITTFaxDecode") || decoder == FX_BSTRC("CCF")) {
    ICodec_ScanlineDecoder* pDecoder =
        FPDFAPI_CreateFaxDecoder(src_buf, limit, width, height, pParam);
    return _DecodeAllScanlines(pDecoder, dest_buf, dest_size);
  }
  if (decoder == FX_BSTRC("ASCII85Decode") || decoder == FX_BSTRC("A85")) {
    return A85Decode(src_buf, limit, dest_buf, dest_size);
  }
  if (decoder == FX_BSTRC("ASCIIHexDecode") || decoder == FX_BSTRC("AHx")) {
    return HexDecode(src_buf, limit, dest_buf, dest_size);
  }
  if (decoder == FX_BSTRC("FlateDecode") || decoder == FX_BSTRC("Fl")) {
    return FPDFAPI_FlateOrLZWDecode(FALSE, src_buf, limit, pParam, dest_size,
                                    dest_buf, dest_size);
  }
  if (decoder == FX_BSTRC("LZWDecode") || decoder == FX_BSTRC("LZW")) {
    return FPDFAPI_FlateOrLZWDecode(TRUE, src_buf, limit, pParam, 0, dest_buf,
                                    dest_size);
  }
  if (decoder == FX_BSTRC("DCTDecode") || decoder == FX_BSTRC("DCT")) {
    ICodec_ScanlineDecoder* pDecoder =
        CPDF_ModuleMgr::Get()->GetJpegModule()->CreateDecoder(
            src_buf, limit, width, height, 0,
            pParam ? pParam->GetInteger(FX_BSTRC("ColorTransform"), 1) : 1);
    return _DecodeAllScanlines(pDecoder, dest_buf, dest_size);
  }
  if (decoder == FX_BSTRC("RunLengthDecode") || decoder == FX_BSTRC("RL")) {
    return RunLengthDecode(src_buf, limit, dest_buf, dest_size);
  }
  dest_size = 0;
  dest_buf = 0;
  return (FX_DWORD)-1;
}
コード例 #4
0
ファイル: aj_sasl.c プロジェクト: bigzero/ajtcl
/*
 * Responder side of the SASL conversation
 */
static AJ_Status Response(AJ_SASL_Context* context, char* inStr, char* outStr, uint32_t outLen)
{
    AJ_Status status = AJ_OK;
    AJ_AuthResult result;
    const char* cmd;
    const char* rsp = outStr;

    if (context->state == AJ_SASL_SEND_AUTH_REQ) {
        cmd = CMD_REJECTED;
    } else {
        cmd = ParseCmd(&inStr);
    }
    /*
     * The REJECTED command is handled the same in all states
     */
    if (cmd == CMD_REJECTED) {
        context->nextMech++;
        context->mechanism = SelectAuth(context, "");
        if (!context->mechanism) {
            /*
             * No mechanism in common so authentication fails.
             */
            return AJ_ERR_SECURITY;
        }
        status = context->mechanism->Init(AJ_AUTH_RESPONDER, context->pwdFunc);
        /*
         * Initialization must succeed
         */
        if (status != AJ_OK) {
            return status;
        }
        result = context->mechanism->Response(NULL, outStr, outLen);
        if ((result == AJ_AUTH_STATUS_SUCCESS) || (result == AJ_AUTH_STATUS_CONTINUE)) {
            status = PrependStr(context->mechanism->name, outStr, outLen, TRUE);
            if (status == AJ_OK) {
                status = PrependStr(CMD_AUTH, outStr, outLen, FALSE);
            }
            if (status == AJ_OK) {
                status = AppendCRLF(outStr, outLen);
            }
            context->state = (result == AJ_AUTH_STATUS_SUCCESS) ? AJ_SASL_WAIT_FOR_OK : AJ_SASL_WAIT_FOR_DATA;
        } else {
            status = AJ_ERR_SECURITY;
        }
        return status;
    }

    switch (context->state) {
    case AJ_SASL_WAIT_FOR_DATA:
        if (cmd == CMD_DATA) {
            status = HexDecode(inStr);
            if (status == AJ_OK) {
                result = context->mechanism->Response(inStr, outStr, outLen);
                if (result == AJ_AUTH_STATUS_SUCCESS) {
                    status = PrependStr(CMD_DATA, outStr, outLen, TRUE);
                    context->state = AJ_SASL_WAIT_FOR_OK;
                } else if (result == AJ_AUTH_STATUS_ERROR) {
                    status = context->mechanism->Init(AJ_AUTH_RESPONDER, context->pwdFunc);
                    /*
                     * Initialization must succeed
                     */
                    if (status != AJ_OK) {
                        return status;
                    }
                    context->state = AJ_SASL_WAIT_FOR_REJECT;
                    rsp = CMD_CANCEL;
                } else if (result != AJ_AUTH_STATUS_CONTINUE) {
                    if (result == AJ_AUTH_STATUS_RETRY &&
                        context->mechList[context->nextMech + 1] != NULL) {
                        // Notify the challenger to give up on the current authentication mechanism and to be in WAIT_FOR_AUTH state
                        rsp = CMD_ERROR;
                    } else {
                        status = AJ_ERR_SECURITY;
                    }
                }
            }
            break;
        }
    /* Fallthrough */

    case AJ_SASL_WAIT_FOR_OK:
        if (cmd == CMD_OK) {
            AJ_GUID localGuid;
            AJ_GetLocalGUID(&localGuid);
            status = AJ_GUID_ToString(&localGuid, outStr, outLen);
            if (status == AJ_OK) {
                status = PrependStr(CMD_BEGIN, outStr, outLen, FALSE);
            }
            context->state = AJ_SASL_AUTHENTICATED;
        } else if (cmd == CMD_DATA) {
            rsp = CMD_CANCEL;
            context->state = AJ_SASL_WAIT_FOR_REJECT;
        } else if (cmd == CMD_ERROR) {
            rsp = CMD_CANCEL;
            context->state = AJ_SASL_WAIT_FOR_REJECT;
        } else {
            rsp = CMD_ERROR;
        }
        break;

    case AJ_SASL_WAIT_FOR_REJECT:
        status = AJ_ERR_SECURITY;
        break;

    default:
        status = AJ_ERR_UNEXPECTED;
    }

    if (status == AJ_OK) {
        if (rsp != outStr) {
            status = SetStr(rsp, outStr, outLen);
        }
    }
    if (status == AJ_OK) {
        status = AppendCRLF(outStr, outLen);
    }
    return status;
}
コード例 #5
0
ファイル: aj_sasl.c プロジェクト: bigzero/ajtcl
/*
 * Challenger side of the SASL conversation
 */
static AJ_Status Challenge(AJ_SASL_Context* context, char* inStr, char* outStr, uint32_t outLen)
{
    AJ_Status status = AJ_OK;
    AJ_AuthResult result;
    const char* rsp = outStr;
    const char* cmd = ParseCmd(&inStr);

    /*
     * The ERROR command is handled the same in all states.
     */
    if (cmd == CMD_ERROR || ((cmd == CMD_CANCEL) && (context->state != AJ_SASL_WAIT_FOR_AUTH))) {
        status = Rejected(context, outStr, outLen);
        if (status == AJ_OK) {
            status = AppendCRLF(outStr, outLen);
        }
        context->state = AJ_SASL_WAIT_FOR_AUTH;
        return status;
    }

    switch (context->state) {
    case AJ_SASL_WAIT_FOR_AUTH:
        if (cmd == CMD_AUTH) {
            context->mechanism = MatchMechanism(context, &inStr);
            if (!context->mechanism) {
                result = AJ_AUTH_STATUS_RETRY;
                status = Rejected(context, outStr, outLen);
                break;
            } else {
                status = context->mechanism->Init(AJ_AUTH_CHALLENGER, context->pwdFunc);
                /*
                 * Initialization must succeed
                 */
                if (status != AJ_OK) {
                    break;
                }
            }
            /*
             * Data following an AUTH command is handled sames as DATA command
             */
            if ((*inStr) || (strcmp(context->mechanism->name, "ANONYMOUS") == 0)) {
                cmd = CMD_DATA;
            } else {
                break;
            }
        }
    /* Falling through */

    case AJ_SASL_WAIT_FOR_DATA:
        if (cmd == CMD_DATA) {
            if (strcmp(context->mechanism->name, "ANONYMOUS") != 0) {
                status = HexDecode(inStr);
            }
            if (status == AJ_OK) {
                result = context->mechanism->Challenge(inStr, outStr, outLen);
                if (result == AJ_AUTH_STATUS_SUCCESS) {
                    AJ_GUID localGuid;
                    AJ_GetLocalGUID(&localGuid);
                    status = AJ_GUID_ToString(&localGuid, outStr, outLen);
                    if (status == AJ_OK) {
                        status = PrependStr(CMD_OK, outStr, outLen, FALSE);
                    }
                    context->state = AJ_SASL_WAIT_FOR_BEGIN;
                } else if (result == AJ_AUTH_STATUS_CONTINUE) {
                    status = PrependStr(CMD_DATA, outStr, outLen, TRUE);
                    context->state = AJ_SASL_WAIT_FOR_DATA;
                } else if (result == AJ_AUTH_STATUS_RETRY) {
                    status = Rejected(context, outStr, outLen);
                } else if (result == AJ_AUTH_STATUS_FAILURE) {
                    status = AJ_ERR_SECURITY;
                } else {
                    rsp = CMD_ERROR;
                }
            }
        } else if (cmd == CMD_BEGIN) {
            status = AJ_ERR_SECURITY;
        } else {
            rsp = CMD_ERROR;
        }
        break;

    case AJ_SASL_WAIT_FOR_BEGIN:
        if (cmd == CMD_BEGIN) {
            context->state = AJ_SASL_AUTHENTICATED;
        } else {
            rsp = CMD_ERROR;
        }
        break;

    default:
        status = AJ_ERR_UNEXPECTED;
    }

    if (status == AJ_OK) {
        if (rsp != outStr) {
            status = SetStr(rsp, outStr, outLen);
        }
    }

    /* The Challenger does not send out any SASL message once the state is AJ_SASL_AUTHENTICATED
       i.e after the BEGIN command is received. So we should not append the CRLF. This results
       in unwanted bytes being put in the tx buffer which gets sent out to the Responder. */
    if ((status == AJ_OK) && (context->state != AJ_SASL_AUTHENTICATED)) {
        status = AppendCRLF(outStr, outLen);
    }
    return status;
}
コード例 #6
0
ファイル: test.cpp プロジェクト: acat/emule
int main(int argc, char *argv[])
#endif
{
#ifdef _CRTDBG_LEAK_CHECK_DF
	// Turn on leak-checking
	int tempflag = _CrtSetDbgFlag( _CRTDBG_REPORT_FLAG );
	tempflag |= _CRTDBG_LEAK_CHECK_DF;
	_CrtSetDbgFlag( tempflag );
#endif

#if defined(__MWERKS__) && defined(macintosh)
	argc = ccommand(&argv);
#endif

	try
	{
		std::string command, executableName, edcFilename;

		if (argc < 2)
			command = 'h';
		else
			command = argv[1];

		if (FIPS_140_2_ComplianceEnabled())
		{
			edcFilename = "edc.dat";

#ifdef CRYPTOPP_WIN32_AVAILABLE
			TCHAR filename[MAX_PATH];
			GetModuleFileName(GetModuleHandle(NULL), filename, sizeof(filename));
			executableName = filename;
			std::string::size_type pos = executableName.rfind('\\');
			if (pos != std::string::npos)
				edcFilename = executableName.substr(0, pos+1) + edcFilename;
#else
			executableName = argv[0];
#endif

			if (command.substr(0, 4) != "fips")
			{
				byte expectedModuleDigest[SHA1::DIGESTSIZE];
				FileSource(edcFilename.c_str(), true, new HexDecoder(new ArraySink(expectedModuleDigest, sizeof(expectedModuleDigest))));

				DoPowerUpSelfTest(executableName.c_str(), expectedModuleDigest);
			}
		}

		switch (command[0])
		{
		case 'g':
		  {
			char seed[1024], privFilename[128], pubFilename[128];
			unsigned int keyLength;

			cout << "Key length in bits: ";
			cin >> keyLength;

			cout << "\nSave private key to file: ";
			cin >> privFilename;

			cout << "\nSave public key to file: ";
			cin >> pubFilename;

			cout << "\nRandom Seed: ";
			ws(cin);
			cin.getline(seed, 1024);

			GenerateRSAKey(keyLength, privFilename, pubFilename, seed);
			return 0;
		  }
		case 'r':
		  {
			switch (argv[1][1])
			{
			case 's':
				RSASignFile(argv[2], argv[3], argv[4]);
				return 0;
			case 'v':
			  {
				bool verified = RSAVerifyFile(argv[2], argv[3], argv[4]);
				cout << (verified ? "valid signature" : "invalid signature") << endl;
				return 0;
			  }
			default:
			  {
				char privFilename[128], pubFilename[128];
				char seed[1024], message[1024];

				cout << "Private key file: ";
				cin >> privFilename;

				cout << "\nPublic key file: ";
				cin >> pubFilename;

				cout << "\nRandom Seed: ";
				ws(cin);
				cin.getline(seed, 1024);

				cout << "\nMessage: ";
				cin.getline(message, 1024);

				string ciphertext = RSAEncryptString(pubFilename, seed, message);
				cout << "\nCiphertext: " << ciphertext << endl;

				string decrypted = RSADecryptString(privFilename, ciphertext.c_str());
				cout << "\nDecrypted: " << decrypted << endl;

				return 0;
			  }
			}
		  }
		case 'm':
			DigestFile(argv[2]);
			return 0;
		case 't':
		  {
			if (command == "tv")
			{
				return !RunTestDataFile(argv[2]);
			}
			// VC60 workaround: use char array instead of std::string to workaround MSVC's getline bug
			char passPhrase[MAX_PHRASE_LENGTH], plaintext[1024];

			cout << "Passphrase: ";
			cin.getline(passPhrase, MAX_PHRASE_LENGTH);

			cout << "\nPlaintext: ";
			cin.getline(plaintext, 1024);

			string ciphertext = EncryptString(plaintext, passPhrase);
			cout << "\nCiphertext: " << ciphertext << endl;

			string decrypted = DecryptString(ciphertext.c_str(), passPhrase);
			cout << "\nDecrypted: " << decrypted << endl;

			return 0;
		  }
		case 'e':
		case 'd':
			if (command == "e64")
				Base64Encode(argv[2], argv[3]);
			else if (command == "d64")
				Base64Decode(argv[2], argv[3]);
			else if (command == "e16")
				HexEncode(argv[2], argv[3]);
			else if (command == "d16")
				HexDecode(argv[2], argv[3]);
			else
			{
				char passPhrase[MAX_PHRASE_LENGTH];
				cout << "Passphrase: ";
				cin.getline(passPhrase, MAX_PHRASE_LENGTH);
				if (command == "e")
					EncryptFile(argv[2], argv[3], passPhrase);
				else
					DecryptFile(argv[2], argv[3], passPhrase);
			}
			return 0;
		case 's':
			if (argv[1][1] == 's')
			{
				char seed[1024];
				cout << "\nRandom Seed: ";
				ws(cin);
				cin.getline(seed, 1024);
				SecretShareFile(atoi(argv[2]), atoi(argv[3]), argv[4], seed);
			}
			else
				SecretRecoverFile(argc-3, argv[2], argv+3);
			return 0;
		case 'i':
			if (argv[1][1] == 'd')
				InformationDisperseFile(atoi(argv[2]), atoi(argv[3]), argv[4]);
			else
				InformationRecoverFile(argc-3, argv[2], argv+3);
			return 0;
		case 'v':
			return !Validate(argc>2 ? atoi(argv[2]) : 0, argv[1][1] == 'v', argc>3 ? argv[3] : NULL);
		case 'b':
			if (argc<3)
				BenchMarkAll();
			else
				BenchMarkAll((float)atof(argv[2]));
			return 0;
		case 'z':
			GzipFile(argv[3], argv[4], argv[2][0]-'0');
			return 0;
		case 'u':
			GunzipFile(argv[2], argv[3]);
			return 0;
		case 'f':
			if (command == "fips")
				FIPS140_SampleApplication(executableName.c_str(), edcFilename.c_str());
			else if (command == "fips-rand")
				FIPS140_GenerateRandomFiles();
			else if (command == "ft")
				ForwardTcpPort(argv[2], argv[3], argv[4]);
			return 0;
		case 'a':
			if (AdhocTest)
				return (*AdhocTest)(argc, argv);
			else
				return 0;
		default:
			FileSource usage("usage.dat", true, new FileSink(cout));
			return 1;
		}
	}
	catch(CryptoPP::Exception &e)
	{
		cout << "\nCryptoPP::Exception caught: " << e.what() << endl;
		return -1;
	}
	catch(std::exception &e)
	{
		cout << "\nstd::exception caught: " << e.what() << endl;
		return -2;
	}
}