📄 base64t.c
字号:
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- *//* * The contents of this file are subject to the Mozilla Public * License Version 1.1 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of * the License at http://www.mozilla.org/MPL/ * * Software distributed under the License is distributed on an "AS * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or * implied. See the License for the specific language governing * rights and limitations under the License. * * The Original Code is the Netscape Portable Runtime (NSPR). * * The Initial Developer of the Original Code is Netscape * Communications Corporation. Portions created by Netscape are * Copyright (C) 1998-2000 Netscape Communications Corporation. All * Rights Reserved. * * Contributor(s): * * Alternatively, the contents of this file may be used under the * terms of the GNU General Public License Version 2 or later (the * "GPL"), in which case the provisions of the GPL are applicable * instead of those above. If you wish to allow use of your * version of this file only under the terms of the GPL and not to * allow others to use your version of this file under the MPL, * indicate your decision by deleting the provisions above and * replace them with the notice and other provisions required by * the GPL. If you do not delete the provisions above, a recipient * may use your version of this file under either the MPL or the * GPL. */#include "plbase64.h"#include "plstr.h"#include "nspr.h"#include <stdio.h>static unsigned char *base = (unsigned char *)"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";/* PL_Base64Encode, single characters */PRBool test_001(void){ PRUint32 a, b; unsigned char plain[ 4 ]; unsigned char cypher[ 5 ]; char result[ 8 ]; char *rv; printf("Test 001 (PL_Base64Encode, single characters) ..."); fflush(stdout); plain[1] = plain[2] = plain[3] = (unsigned char)0; cypher[2] = cypher[3] = (unsigned char)'='; cypher[4] = (unsigned char)0; for( a = 0; a < 64; a++ ) { cypher[0] = base[a]; for( b = 0; b < 4; b++ ) { plain[0] = (unsigned char)(a * 4 + b); cypher[1] = base[(b * 16)]; rv = PL_Base64Encode((char *)plain, 1, result); if( rv != result ) { printf("FAIL\n\t(%d, %d): return value\n", a, b); return PR_FALSE; } if( 0 != PL_strncmp((char *)cypher, result, 4) ) { printf("FAIL\n\t(%d, %d): expected \"%s,\" got \"%.4s.\"\n", a, b, cypher, result); return PR_FALSE; } } } printf("PASS\n"); return PR_TRUE;}/* PL_Base64Encode, double characters */PRBool test_002(void){ PRUint32 a, b, c, d; unsigned char plain[ 4 ]; unsigned char cypher[ 5 ]; char result[ 8 ]; char *rv; printf("Test 002 (PL_Base64Encode, double characters) ..."); fflush(stdout); plain[2] = plain[3] = (unsigned char)0; cypher[3] = (unsigned char)'='; cypher[4] = (unsigned char)0; for( a = 0; a < 64; a++ ) { cypher[0] = base[a]; for( b = 0; b < 4; b++ ) { plain[0] = (a*4) + b; for( c = 0; c < 16; c++ ) { cypher[1] = base[b*16 + c]; for( d = 0; d < 16; d++ ) { plain[1] = c*16 + d; cypher[2] = base[d*4]; rv = PL_Base64Encode((char *)plain, 2, result); if( rv != result ) { printf("FAIL\n\t(%d, %d, %d, %d): return value\n", a, b, c, d); return PR_FALSE; } if( 0 != PL_strncmp((char *)cypher, result, 4) ) { printf("FAIL\n\t(%d, %d, %d, %d): expected \"%s,\" got \"%.4s.\"\n", a, b, c, d, cypher, result); return PR_FALSE; } } } } } printf("PASS\n"); return PR_TRUE;}/* PL_Base64Encode, triple characters */PRBool test_003(void){ PRUint32 a, b, c, d, e, f; unsigned char plain[ 4 ]; unsigned char cypher[ 5 ]; char result[ 8 ]; char *rv; printf("Test 003 (PL_Base64Encode, triple characters) ..."); fflush(stdout); cypher[4] = (unsigned char)0; for( a = 0; a < 64; a++ ) { cypher[0] = base[a]; for( b = 0; b < 4; b++ ) { plain[0] = (a*4) + b; for( c = 0; c < 16; c++ ) { cypher[1] = base[b*16 + c]; for( d = 0; d < 16; d++ ) { plain[1] = c*16 + d; for( e = 0; e < 4; e++ ) { cypher[2] = base[d*4 + e]; for( f = 0; f < 64; f++ ) { plain[2] = e * 64 + f; cypher[3] = base[f]; rv = PL_Base64Encode((char *)plain, 3, result); if( rv != result ) { printf("FAIL\n\t(%d, %d, %d, %d, %d, %d): return value\n", a, b, c, d, e, f); return PR_FALSE; } if( 0 != PL_strncmp((char *)cypher, result, 4) ) { printf("FAIL\n\t(%d, %d, %d, %d, %d, %d): expected \"%s,\" got \"%.4s.\"\n", a, b, c, d, e, f, cypher, result); return PR_FALSE; } } } } } } } printf("PASS\n"); return PR_TRUE;} static struct { const char *plaintext; const char *cyphertext; } array[] = { /* Cyphertexts generated with uuenview 0.5.13 */ { " ", "IA==" }, { ".", "Lg==" }, { "/", "Lw==" }, { "C", "Qw==" }, { "H", "SA==" }, { "S", "Uw==" }, { "^", "Xg==" }, { "a", "YQ==" }, { "o", "bw==" }, { "t", "dA==" }, { "AB", "QUI=" }, { "AH", "QUg=" }, { "AQ", "QVE=" }, { "BD", "QkQ=" }, { "CR", "Q1I=" }, { "CS", "Q1M=" }, { "DB", "REI=" }, { "DC", "REM=" }, { "EK", "RUs=" }, { "ET", "RVQ=" }, { "IM", "SU0=" }, { "JR", "SlI=" }, { "LO", "TE8=" }, { "LW", "TFc=" }, { "ML", "TUw=" }, { "SB", "U0I=" }, { "TO", "VE8=" }, { "VS", "VlM=" }, { "WP", "V1A=" }, /* legitimate two-letter words */ { "ad", "YWQ=" }, { "ah", "YWg=" }, { "am", "YW0=" }, { "an", "YW4=" }, { "as", "YXM=" }, { "at", "YXQ=" }, { "ax", "YXg=" }, { "be", "YmU=" }, { "by", "Ynk=" }, { "do", "ZG8=" }, { "go", "Z28=" }, { "he", "aGU=" }, { "hi", "aGk=" }, { "if", "aWY=" }, { "in", "aW4=" }, { "is", "aXM=" }, { "it", "aXQ=" }, { "me", "bWU=" }, { "my", "bXk=" }, { "no", "bm8=" }, { "of", "b2Y=" }, { "on", "b24=" }, { "or", "b3I=" }, { "ox", "b3g=" }, { "so", "c28=" }, { "to", "dG8=" }, { "up", "dXA=" }, { "us", "dXM=" }, { "we", "d2U=" }, /* all three-letter entries in /usr/dict/words */ { "1st", "MXN0" }, { "2nd", "Mm5k" }, { "3rd", "M3Jk" }, { "4th", "NHRo" }, { "5th", "NXRo" }, { "6th", "NnRo" }, { "7th", "N3Ro" }, { "8th", "OHRo" }, { "9th", "OXRo" }, { "AAA", "QUFB" }, { "AAU", "QUFV" }, { "ABA", "QUJB" }, { "abc", "YWJj" }, { "Abe", "QWJl" }, { "Abo", "QWJv" }, { "ace", "YWNl" }, { "ACM", "QUNN" }, { "ACS", "QUNT" }, { "act", "YWN0" }, { "Ada", "QWRh" }, { "add", "YWRk" }, { "ado", "YWRv" }, { "aft", "YWZ0" }, { "age", "YWdl" }, { "ago", "YWdv" }, { "aid", "YWlk" }, { "ail", "YWls" }, { "aim", "YWlt" }, { "air", "YWly" }, { "ala", "YWxh" }, { "alb", "YWxi" }, { "ale", "YWxl" }, { "Ali", "QWxp" }, { "all", "YWxs" }, { "alp", "YWxw" }, { "A&M", "QSZN" },
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -