76 #include "../../utilities/utilities.h"
80 static const char* cpMakeFileName(
char* cpBuffer,
const char* cpBase,
const char* cpDivider,
const char* cpName){
81 strcpy(cpBuffer, cpBase);
82 strcat(cpBuffer, cpDivider);
83 strcat(cpBuffer, cpName);
87 static char s_caBuf[5*PATH_MAX];
89 static char* s_cpDescription =
90 "Illustrate the construction and use of the data conversion utility object.";
92 static char* s_cppCases[] = {
93 "Display application information.",
94 "Compare the conversion object results with the Linux iconv command-line application.",
95 "Get the raw, 32-bit decoded data.",
96 "Encode raw, 32-bit data.",
97 "Add base64 encoding and decoding.",
99 static long int s_iCaseCount = (
long int)(
sizeof(s_cppCases) /
sizeof(s_cppCases[0]));
101 static int iHelp(
void){
104 printf(
"description: %s\n", s_cpDescription);
105 printf(
" usage: ex-api arg\n");
106 printf(
" arg = n, 1 <= n <= %ld\n", s_iCaseCount);
107 printf(
" execute case number n\n");
108 printf(
" arg = anthing else\n");
109 printf(
" print this help screen\n");
111 for(; i < s_iCaseCount; i++){
112 printf(
"case %ld %s\n", (i + 1), s_cppCases[i]);
134 int iReturn = EXIT_SUCCESS;
135 static void* vpMem = NULL;
136 static void* vpConv = NULL;
138 aint uiFileSize = 1024;
140 { cpMakeFileName(s_caBuf, SOURCE_DIR,
"/../input/",
"data8"), NULL, 0,
UTF_8},
141 { cpMakeFileName(&s_caBuf[PATH_MAX], SOURCE_DIR,
"/../input/",
"data16le"), NULL, 0,
UTF_16LE},
142 { cpMakeFileName(&s_caBuf[2*PATH_MAX], SOURCE_DIR,
"/../input/",
"data16be"), NULL, 0,
UTF_16BE},
143 { cpMakeFileName(&s_caBuf[3*PATH_MAX], SOURCE_DIR,
"/../input/",
"data32le"), NULL, 0,
UTF_32LE},
146 aint uiFileCount = (
aint)(
sizeof(saFiles) /
sizeof(saFiles[0]));
159 "The Linux command \"iconv\" has been used to convert a simple array of data spanning\n"
160 "the full range of Unicode characters. The files and formats are:\n"
162 " - UTF-16LE, data16le\n"
163 " - UTF-16BE, data16be\n"
164 " - UTF-32LE, data32le\n"
165 " - UTF-32BE, data32be\n"
167 "This example case uses the utilities conv object to do all possible conversions\n"
168 "and compare the converted data to the Linux file data.\n\n";
169 printf(
"\n%s", cpHeader);
170 for(ui = 0; ui < uiFileCount; ui++){
171 spFileIn = &saFiles[ui];
173 spFileIn->
uiBytes = uiFileSize;
175 if(spFileIn->
uiBytes > uiFileSize){
176 snprintf(caBuf, uiFileSize,
"buffer not big enough for input file %s", spFileIn->
cpName);
182 printf(
"Make all possible comparisons doing the encoding and decoding steps separately.\n");
183 for(ui = 0; ui < uiFileCount; ui++){
184 spFileIn = &saFiles[ui];
189 for(uj = 0; uj < uiFileCount; uj++){
190 spFileOut = &saFiles[uj];
191 memset(&sDst, 0,
sizeof(sDst));
195 snprintf(caBuf, uiFileSize,
"source (%s), destination (%s) conversion lengths not the same",
200 snprintf(caBuf, uiFileSize,
"source (%s), destination (%s) conversion comparison failed",
204 printf(
"conversion (%s) -> (%s) successful\n",
209 printf(
"\nMake all possible comparisons encoding and decoding in a single step.\n");
210 for(ui = 0; ui < uiFileCount; ui++){
211 spFileIn = &saFiles[ui];
215 for(uj = 0; uj < uiFileCount; uj++){
216 spFileOut = &saFiles[uj];
217 memset(&sDst, 0,
sizeof(sDst));
221 snprintf(caBuf, uiFileSize,
"source (%s), destination (%s) conversion lengths not the same",
226 snprintf(caBuf, uiFileSize,
"source (%s), destination (%s) conversion comparison failed",
230 printf(
"conversion (%s) -> (%s) successful\n",
238 iReturn = EXIT_FAILURE;
248 int iReturn = EXIT_SUCCESS;
249 static void* vpMem = NULL;
250 static void* vpConv = NULL;
252 aint uiFileSize = 1024;
253 uint32_t uiaGetCodePoints[1024];
255 uint32_t* uipCodePoints = &uiaGetCodePoints[0];
257 { cpMakeFileName(s_caBuf, SOURCE_DIR,
"/../input/",
"data8"), NULL, 0,
UTF_8},
258 { cpMakeFileName(&s_caBuf[PATH_MAX], SOURCE_DIR,
"/../input/",
"data16le"), NULL, 0,
UTF_16LE},
259 { cpMakeFileName(&s_caBuf[2*PATH_MAX], SOURCE_DIR,
"/../input/",
"data16be"), NULL, 0,
UTF_16BE},
260 { cpMakeFileName(&s_caBuf[3*PATH_MAX], SOURCE_DIR,
"/../input/",
"data32le"), NULL, 0,
UTF_32LE},
263 aint uiFileCount = (
aint)(
sizeof(saFiles) /
sizeof(saFiles[0]));
265 uint32_t uiaOriginalData[] = {
266 0, 126, 127, 128, 0xff, 0x1ff, 0x1fff, 0xd7ff, 0xe000, 0xffff, 0x10ffff
268 aint uiOriginalLength = (
aint)(
sizeof(uiaOriginalData) /
sizeof(uiaOriginalData[0]));
279 "The Linux command \"iconv\" has been used to convert a simple array of data spanning\n"
280 "the full range of Unicode characters. The files and formats are:\n"
282 " - UTF-16LE, data16le\n"
283 " - UTF-16BE, data16be\n"
284 " - UTF-32LE, data32le\n"
285 " - UTF-32BE, data32be\n"
287 "This example case uses the utilities conv object to decode the files and compare the\n"
288 "decoded data to the original data used to create the files.\n\n";
289 printf(
"\n%s", cpHeader);
290 printf(
"Original Code Points\n");
291 for(ui = 0; ui < uiOriginalLength; ui++){
292 printf(
"0x%06X ", uiaOriginalData[ui]);
296 for(ui = 0; ui < uiFileCount; ui++){
297 spFileIn = &saFiles[ui];
299 spFileIn->
uiBytes = uiFileSize;
301 if(spFileIn->
uiBytes > uiFileSize){
302 snprintf(caBuf, uiFileSize,
"buffer not big enough for input file %s", spFileIn->
cpName);
308 printf(
"\nDecode all files, get decoded code points and compare to original.\n");
309 for(ui = 0; ui < uiFileCount; ui++){
310 spFileIn = &saFiles[ui];
315 uiLength = uiFileSize;
317 if(uiLength > (uint32_t)uiFileSize){
318 XTHROW(&e,
"Code point buffer too small for converted data.");
320 if(uiLength != uiOriginalLength){
321 snprintf(caBuf, uiFileSize,
"source (%s), converted code points length incorrect",
325 if(memcmp(uipCodePoints, uiaOriginalData, (uiLength *
sizeof(*uipCodePoints))) != 0){
326 snprintf(caBuf, uiFileSize,
"source (%s), conversion failed",
330 printf(
"conversion (%s) successful\n",
338 iReturn = EXIT_FAILURE;
347 int iReturn = EXIT_SUCCESS;
348 static void* vpMem = NULL;
349 static void* vpConv = NULL;
350 static void* vpFmt = NULL;
352 aint uiFileSize = 1024;
354 { cpMakeFileName(s_caBuf, SOURCE_DIR,
"/../input/",
"data8"), NULL, 0,
UTF_8},
355 { cpMakeFileName(&s_caBuf[PATH_MAX], SOURCE_DIR,
"/../input/",
"data16le"), NULL, 0,
UTF_16LE},
356 { cpMakeFileName(&s_caBuf[2*PATH_MAX], SOURCE_DIR,
"/../input/",
"data16be"), NULL, 0,
UTF_16BE},
357 { cpMakeFileName(&s_caBuf[3*PATH_MAX], SOURCE_DIR,
"/../input/",
"data32le"), NULL, 0,
UTF_32LE},
360 aint uiFileCount = (
aint)(
sizeof(saFiles) /
sizeof(saFiles[0]));
362 uint32_t uiaOriginalData[] = {
363 0, 126, 127, 128, 0xff, 0x1ff, 0x1fff, 0xd7ff, 0xe000, 0xffff, 0x10ffff
365 aint uiOriginalLength = (
aint)(
sizeof(uiaOriginalData) /
sizeof(uiaOriginalData[0]));
377 "The Linux command \"iconv\" has been used to convert a simple array of data spanning\n"
378 "the full range of Unicode characters. The files and formats are:\n"
380 " - UTF-16LE, data16le\n"
381 " - UTF-16BE, data16be\n"
382 " - UTF-32LE, data32le\n"
383 " - UTF-32BE, data32be\n"
385 "This example case uses the utilities conv object endocde the original data\n"
386 "and compare the results to the files.\n\n";
387 printf(
"\n%s", cpHeader);
388 printf(
"Original Code Points\n");
389 for(ui = 0; ui < uiOriginalLength; ui++){
390 printf(
"0x%06X ", uiaOriginalData[ui]);
395 for(ui = 0; ui < uiFileCount; ui++){
396 spFileIn = &saFiles[ui];
398 spFileIn->
uiBytes = uiFileSize;
400 if(spFileIn->
uiBytes > uiFileSize){
401 snprintf(caBuf, uiFileSize,
"buffer not big enough for input file %s", spFileIn->
cpName);
407 printf(
"\nCompare encoded original data to Linux iconv files.\n");
408 for(ui = 0; ui < uiFileCount; ui++){
409 spFileIn = &saFiles[ui];
410 memset(&sDst, 0,
sizeof(sDst));
415 snprintf(caBuf, uiFileSize,
"%s conversion lengths not the same", spFileIn->
cpName);
419 snprintf(caBuf, uiFileSize,
"%s conversion data not the same", spFileIn->
cpName);
422 printf(
"%s encoding successful\n", spFileIn->
cpName);
429 iReturn = EXIT_FAILURE;
438 static void vRemoveLineEnds(uint8_t* ucpData,
aint uiLen, uint8_t* ucpBuf,
aint* uipBufLen){
442 for(; ui < uiLen; ui++){
443 uint8_t ucChar = ucpData[ui];
444 if(ucChar == 9 || ucChar == 13){
447 ucpBuf[uiLength] = ucChar;
450 *uipBufLen = uiLength;
452 static int iBase64() {
453 int iReturn = EXIT_SUCCESS;
454 static void* vpMem = NULL;
455 static void* vpConv = NULL;
456 static void* vpFmt = NULL;
458 aint uiBufSize = 1024;
459 uint8_t *ucpRand, *ucpRand64, *ucpRandBuf1, *ucpRandBuf2;
460 aint uiRandLen, uiRand64Len, uiBuf1Len, uiBuf2Len;
461 const char* cpRandFile = cpMakeFileName(&s_caBuf[0], SOURCE_DIR,
"/../input/",
"rand512");
462 const char* cpRand64File = cpMakeFileName(&s_caBuf[PATH_MAX], SOURCE_DIR,
"/../input/",
"rand512b64");
475 "The Linux command \"base64\" has been used to convert a file of 512 random bytes\n"
476 "to base64 format. The files are \"rand512\" and \"rand512b64\", respectively.\n"
478 "This example case uses the utilities conv object to do a base64 encoding of \"rand512\"\n"
479 "and compare to \"rand512b64\".\n"
480 "Note that the set of 256 8-bit bytes constitute ISO-8859-1 encoding.\n"
481 "ISO_8859_1, LATIN1 and BINARY are used by the conv object as aliases.\n\n";
482 printf(
"\n%s", cpHeader);
485 ucpRand = (uint8_t*)
vpMemAlloc(vpMem, uiBufSize);
486 ucpRand64 = (uint8_t*)
vpMemAlloc(vpMem, uiBufSize);
487 ucpRandBuf1 = (uint8_t*)
vpMemAlloc(vpMem, uiBufSize);
488 ucpRandBuf2 = (uint8_t*)
vpMemAlloc(vpMem, uiBufSize);
489 uiRandLen = uiBufSize;
493 if(uiRandLen > uiBufSize){
494 snprintf(caBuf, uiBufSize,
"buffer not big enough for input file %s", cpRandFile);
497 uiRand64Len = uiBufSize;
499 if(uiRand64Len > uiBufSize){
500 snprintf(caBuf, uiBufSize,
"buffer not big enough for input file %s", cpRand64File);
508 memset(&sDst, 0,
sizeof(sDst));
512 XTHROW(&e,
"buffer not big enough for input converted data");
514 vRemoveLineEnds(ucpRand64, uiRand64Len, ucpRandBuf1, &uiBuf1Len);
516 if(uiBuf1Len != uiBuf2Len){
517 XTHROW(&e,
"converted data not correct length");
519 if(memcmp(ucpRandBuf1, ucpRandBuf2, uiBuf1Len) != 0){
520 XTHROW(&e,
"converted data does not match file data");
522 printf(
"base64 conversion success\n");
525 printf(
"\nThe default base64 file is has line feed line breaks at 76 characters.\n");
526 printf(
"The line length and line ending can be configured with the conv utility.\n");
528 printf(
"\nDefault conversion.\n");
531 printf(
"%s\n", (
char*)ucpRandBuf1);
533 printf(
"Conversion with 100 characters per line.\n");
535 memset(&sDst, 0,
sizeof(sDst));
539 XTHROW(&e,
"buffer not big enough for input converted data");
543 printf(
"%s\n", (
char*)ucpRandBuf1);
545 printf(
"Conversion with 50 characters per line.\n");
547 memset(&sDst, 0,
sizeof(sDst));
551 XTHROW(&e,
"buffer not big enough for input converted data");
555 printf(
"%s\n", (
char*)ucpRandBuf1);
560 iReturn = EXIT_FAILURE;
576 int main(
int argc,
char **argv) {
579 iCase = atol(argv[1]);
581 if((iCase > 0) && (iCase <= s_iCaseCount)){
582 printf(
"%s\n", s_cppCases[iCase -1]);