Changeset 16765 in vbox for trunk/src/VBox/Runtime
- Timestamp:
- Feb 14, 2009 8:49:30 AM (16 years ago)
- Location:
- trunk/src/VBox/Runtime
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Runtime/common/string/base64.cpp
r16762 r16765 447 447 * may contain an invalid Base64 string. 448 448 * 449 * @param pvData The data to encode. 450 * @param cbData The number of bytes to encode. 449 451 * @param pszBuf Where to put the Base64 string. 450 452 * @param cbBuf The size of the output buffer, including the terminator. 451 * @param pvData The data to encode. 452 * @param cbData The number of bytes to encode. 453 */ 454 RTDECL(int) RTBase64Encode(char *pszBuf, size_t cbBuf, const void *pvData, size_t cbData) 453 * @param pcchActual The actual number of characters returned. 454 */ 455 RTDECL(int) RTBase64Encode(const void *pvData, size_t cbData, char *pszBuf, size_t cbBuf, size_t *pcchActual) 455 456 { 456 457 /** @todo implement RTBase64Encode. */ -
trunk/src/VBox/Runtime/testcase/tstBase64.cpp
r16764 r16765 41 41 int main() 42 42 { 43 int cErrors =0; 43 int cErrors = 0; 44 char szOut[0x10000]; 45 size_t cchOut = 0; 44 46 RTR3Init(); 47 45 48 46 49 /* 47 50 * Test 0. 48 51 */ 49 static const char s_szText0[] = "Hey"; 50 static const char s_szEnc0[] = "SGV5"; 51 size_t cchOut0 = 0; 52 char szOut0[sizeof(s_szText0)]; 53 int rc = RTBase64Decode(&s_szEnc0[0], szOut0, sizeof(szOut0), &cchOut0, NULL); 54 if (RT_FAILURE(rc)) 52 static const struct 55 53 { 56 RTPrintf("tstBase64: FAILURE - RTBase64Decode s_szEnc0 -> %Rrc\n", rc); 57 cErrors++; 58 } 59 else if (cchOut0 != sizeof(s_szText0) - 1) 54 const char *pszText; 55 size_t cchText; 56 const char *pszEnc; 57 size_t cchEnc; 58 } g_aTests[] = 60 59 { 61 RTPrintf("tstBase64: FAILURE - RTBase64Decode returned %zu bytes, expected %zu.\n", 62 cchOut0, sizeof(s_szText0) - 1); 63 cErrors++; 64 } 65 else if (memcmp(szOut0, s_szText0, cchOut0)) 60 #define TEST_ENTRY(szText, szEnc) { szText, sizeof(szText) - 1, szEnc, sizeof(szEnc) - 1 } 61 TEST_ENTRY("Hey", "SGV5"), 62 TEST_ENTRY("Base64", "QmFzZTY0"), 63 TEST_ENTRY("Call me Ishmael.", "Q2FsbCBtZSBJc2htYWVsLg==") 64 #undef TEST_ENTRY 65 }; 66 67 for (unsigned i = 0; i < RT_ELEMENTS(g_aTests); i++) 66 68 { 67 RTPrintf("tstBase64: FAILURE - RTBase64Decode returned:\n%.*s\nexpected:\n%s\n", 68 (int)cchOut0, szOut0, s_szText0); 69 cErrors++; 70 } 69 int rc = RTBase64Decode(g_aTests[i].pszEnc, szOut, g_aTests[i].cchText, &cchOut, NULL); 70 if (RT_FAILURE(rc)) 71 { 72 RTPrintf("tstBase64: FAILURE - #%u: RTBase64Decode -> %Rrc\n", i, rc); 73 cErrors++; 74 } 75 else if (cchOut != g_aTests[i].cchText) 76 { 77 RTPrintf("tstBase64: FAILURE - #%u: RTBase64Decode returned %zu bytes, expected %zu.\n", 78 i, cchOut, g_aTests[i].cchText); 79 cErrors++; 80 } 81 else if (memcmp(szOut, g_aTests[i].pszText, cchOut)) 82 { 83 RTPrintf("tstBase64: FAILURE - #%u: RTBase64Decode returned:\n%.*s\nexpected:\n%s\n", 84 i, (int)cchOut, szOut, g_aTests[i].pszText); 85 cErrors++; 86 } 71 87 72 cchOut0 = RTBase64DecodedSize(s_szEnc0, NULL); 73 if (cchOut0 != sizeof(s_szText0) - 1) 74 { 75 RTPrintf("tstBase64: FAILURE - RTBase64DecodedSize returned %zu bytes, expected %zu.\n", 76 cchOut0, sizeof(s_szText0) - 1); 77 cErrors++; 88 cchOut = RTBase64DecodedSize(g_aTests[i].pszEnc, NULL); 89 if (cchOut != g_aTests[i].cchText) 90 { 91 RTPrintf("tstBase64: FAILURE - #%u: RTBase64DecodedSize returned %zu bytes, expected %zu.\n", 92 i, cchOut, g_aTests[i].cchText); 93 cErrors++; 94 } 95 96 rc = RTBase64Encode(g_aTests[i].pszText, g_aTests[i].cchText, szOut, g_aTests[i].cchEnc + 1, &cchOut); 97 if (RT_FAILURE(rc)) 98 { 99 RTPrintf("tstBase64: FAILURE - #%u: RTBase64Encode -> %Rrc\n", i, rc); 100 cErrors++; 101 } 102 else if (cchOut != g_aTests[i].cchEnc) 103 { 104 RTPrintf("tstBase64: FAILURE - #%u: RTBase64Encode returned %zu bytes, expected %zu.\n", 105 i, cchOut, g_aTests[i].cchEnc); 106 cErrors++; 107 } 108 else if (memcmp(szOut, g_aTests[i].pszEnc, cchOut + 1)) 109 { 110 RTPrintf("tstBase64: FAILURE - #%u: RTBase64Encode returned:\n%*s\nexpected:\n%s\n", 111 i, szOut, g_aTests[i].pszText); 112 cErrors++; 113 } 114 115 cchOut = RTBase64EncodedLength(g_aTests[i].cchText); 116 if (cchOut != g_aTests[i].cchEnc) 117 { 118 RTPrintf("tstBase64: FAILURE - #%u: RTBase64EncodedLength returned %zu bytes, expected %zu.\n", 119 i, cchOut, g_aTests[i].cchEnc); 120 cErrors++; 121 } 122 78 123 } 79 124 80 125 /* 81 * Test 1. 82 */ 83 static const char s_szText1[] = "Call me Ishmael."; 84 static const char s_szEnc1[] = "Q2FsbCBtZSBJc2htYWVsLg=="; 85 size_t cchOut1 = 0; 86 char szOut1[sizeof(s_szText1)]; 87 rc = RTBase64Decode(&s_szEnc1[0], szOut1, sizeof(szOut1), &cchOut1, NULL); 88 if (RT_FAILURE(rc)) 89 { 90 RTPrintf("tstBase64: FAILURE - RTBase64Decode s_szEnc1 -> %Rrc\n", rc); 91 cErrors++; 92 } 93 else if (cchOut1 != sizeof(s_szText1) - 1) 94 { 95 RTPrintf("tstBase64: FAILURE - RTBase64Decode returned %zu bytes, expected %zu.\n", 96 cchOut1, sizeof(s_szText1) - 1); 97 cErrors++; 98 } 99 else if (memcmp(szOut1, s_szText1, cchOut1)) 100 { 101 RTPrintf("tstBase64: FAILURE - RTBase64Decode returned:\n%.*s\nexpected:\n%s\n", 102 (int)cchOut1, szOut1, s_szText1); 103 cErrors++; 104 } 105 106 cchOut1 = RTBase64DecodedSize(s_szEnc1, NULL); 107 if (cchOut1 != sizeof(s_szText1) - 1) 108 { 109 RTPrintf("tstBase64: FAILURE - RTBase64DecodedSize returned %zu bytes, expected %zu.\n", 110 cchOut1, sizeof(s_szText1) - 1); 111 cErrors++; 112 } 113 114 /* 115 * Test 2. 126 * Bigger Test. 116 127 */ 117 128 static const char s_szText2[] = … … 128 139 " ZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4=\n \n"; 129 140 130 size_t cchOut2 = 0; 131 char szOut2[sizeof(s_szText2)]; 132 rc = RTBase64Decode(&s_szEnc2[0], szOut2, sizeof(szOut2), &cchOut2, NULL); 141 int rc = RTBase64Decode(&s_szEnc2[0], szOut, sizeof(s_szText2), &cchOut, NULL); 133 142 if (RT_FAILURE(rc)) 134 143 { … … 136 145 cErrors++; 137 146 } 138 else if (cchOut 2!= sizeof(s_szText2) - 1)147 else if (cchOut != sizeof(s_szText2) - 1) 139 148 { 140 149 RTPrintf("tstBase64: FAILURE - RTBase64Decode returned %zu bytes, expected %zu.\n", 141 cchOut 2, sizeof(s_szText2) - 1);150 cchOut, sizeof(s_szText2) - 1); 142 151 cErrors++; 143 152 } 144 else if (memcmp(szOut 2, s_szText2, cchOut2))153 else if (memcmp(szOut, s_szText2, cchOut)) 145 154 { 146 155 RTPrintf("tstBase64: FAILURE - RTBase64Decode returned:\n%.*s\nexpected:\n%s\n", 147 (int)cchOut 2, szOut2, s_szText2);156 (int)cchOut, szOut, s_szText2); 148 157 cErrors++; 149 158 } 150 159 151 cchOut 2= RTBase64DecodedSize(s_szEnc2, NULL);152 if (cchOut 2!= sizeof(s_szText2) - 1)160 cchOut = RTBase64DecodedSize(s_szEnc2, NULL); 161 if (cchOut != sizeof(s_szText2) - 1) 153 162 { 154 163 RTPrintf("tstBase64: FAILURE - RTBase64DecodedSize returned %zu bytes, expected %zu.\n", 155 cchOut 2, sizeof(s_szText2) - 1);164 cchOut, sizeof(s_szText2) - 1); 156 165 cErrors++; 157 166 }
Note:
See TracChangeset
for help on using the changeset viewer.