61 #define FMT_BUFLEN (128+MAX_INDENT)
62 #define FILE_END ((uint64_t)-1)
65 static const void* s_vpMagicNumber = (
void*)
"format";
69 #include "../library/lib.h"
102 static char* cpNext(fmt* spCtx);
103 static void vFmtBuf8Hex(fmt* spCtx, uint64_t uiChars);
104 static void vFmtBuf8Hex2(fmt* spCtx, uint64_t uiChars);
105 static void vFmtBuf8Ascii(fmt* spCtx, uint64_t uiChars);
106 static void vFmtBuf8Canonical(fmt* spCtx, uint64_t uiChars);
107 static void vFmtBufUnicode(fmt* spCtx, uint64_t uiChars);
108 static uint64_t uiFillBytes(fmt* spCtx);
109 static uint64_t uiFillFile(fmt* spCtx);
110 static uint64_t uiFillUnicode(fmt* spCtx);
111 static void vReset(fmt* spCtx);
123 memset((
void*) spCtx, 0,
sizeof(fmt));
127 spCtx->vpMem =
vpMem;
128 spCtx->spException = spEx;
130 return (
void*) spCtx;
148 fmt* spCtx = (fmt*) vpCtx;
149 if (spCtx->vpValidate == s_vpMagicNumber) {
151 fclose(spCtx->spFile);
153 void*
vpMem = spCtx->vpMem;
154 memset(vpCtx, 0,
sizeof(fmt));
168 fmt* spCtx = (fmt*) vpCtx;
169 if (vpCtx && (spCtx->vpValidate == s_vpMagicNumber)) {
182 fmt* spCtx = (fmt*) vpCtx;
183 if (vpCtx && (spCtx->vpValidate == s_vpMagicNumber)) {
211 int iStyle, uint64_t uiOffset, uint64_t uiLimit) {
212 fmt* spCtx = (fmt*) vpCtx;
213 if (vpCtx && (spCtx->vpValidate == s_vpMagicNumber)) {
214 if(ucpBytes == NULL){
215 XTHROW(spCtx->spException,
"ucpBytes: input cannot be NULL");
218 XTHROW(spCtx->spException,
"uiLength: input length must be > 0");
222 spCtx->pfnFmt = vFmtBuf8Hex;
226 spCtx->pfnFmt = vFmtBuf8Hex2;
230 spCtx->pfnFmt = vFmtBuf8Ascii;
234 spCtx->pfnFmt = vFmtBuf8Canonical;
237 spCtx->pfnFill = uiFillBytes;
239 spCtx->ucpChars8 = ucpBytes;
241 spCtx->uiDisplayOffset = (uiOffset > uiLength) ? uiLength : uiOffset;
243 spCtx->uiNextOffset = spCtx->uiDisplayOffset;
245 spCtx->uiDisplayEnd = uiLength;
247 spCtx->uiDisplayEnd = spCtx->uiDisplayOffset + uiLimit;
248 if (spCtx->uiDisplayEnd > uiLength) {
249 spCtx->uiDisplayEnd = uiLength;
252 return cpNext(spCtx);
271 const char*
cpFmtFirstFile(
void* vpCtx,
const char* cpFileName,
int iStyle, uint64_t uiOffset, uint64_t uiLimit) {
272 char* cpReturn = NULL;
273 fmt* spCtx = (fmt*) vpCtx;
274 if (vpCtx && (spCtx->vpValidate == s_vpMagicNumber)) {
275 if(cpFileName == NULL || (cpFileName[0] == 0)){
276 XTHROW(spCtx->spException,
"cpFileName: cannot be NULL or empty");
278 char caBuf[PATH_MAX + 128];
279 size_t uiBufSize = PATH_MAX + 128;
281 spCtx->spFile = fopen(cpFileName,
"rb");
282 if (!spCtx->spFile) {
283 snprintf(caBuf, uiBufSize,
"can't open file: %s", cpFileName);
284 XTHROW(spCtx->spException, caBuf);
287 spCtx->pfnFmt = vFmtBuf8Hex;
291 spCtx->pfnFmt = vFmtBuf8Hex2;
295 spCtx->pfnFmt = vFmtBuf8Ascii;
299 spCtx->pfnFmt = vFmtBuf8Canonical;
302 spCtx->pfnFill = uiFillFile;
305 spCtx->uiNextOffset = 0L;
308 while(spCtx->uiNextOffset < uiOffset){
309 iChar = fgetc(spCtx->spFile);
313 spCtx->uiNextOffset++;
316 spCtx->uiDisplayOffset = spCtx->uiNextOffset;
319 spCtx->uiDisplayEnd = spCtx->uiDisplayOffset + uiLimit;
321 return cpNext(spCtx);
341 const char*
cpFmtFirstUnicode(
void* vpCtx,
const uint32_t* uipChars, uint64_t uiLength, uint64_t uiOffset,
343 char* cpReturn = NULL;
344 fmt* spCtx = (fmt*) vpCtx;
345 if (vpCtx && (spCtx->vpValidate == s_vpMagicNumber)) {
348 if(uipChars == NULL){
349 XTHROW(spCtx->spException,
"uipChars: input cannot be NULL");
352 XTHROW(spCtx->spException,
"uiLength: input length must be > 0");
354 spCtx->pfnFmt = vFmtBufUnicode;
355 spCtx->pfnFill = uiFillUnicode;
358 spCtx->uipChars32 = uipChars;
360 spCtx->uiDisplayOffset = (uiOffset > uiLength) ? uiLength : uiOffset;
363 spCtx->uiDisplayEnd = uiLength;
365 spCtx->uiDisplayEnd = spCtx->uiDisplayOffset + uiLimit;
366 if (spCtx->uiDisplayEnd > uiLength) {
367 spCtx->uiDisplayEnd = uiLength;
370 spCtx->uiNextOffset = spCtx->uiDisplayOffset;
371 cpReturn = cpNext(spCtx);
387 const char* cpReturn = NULL;
388 fmt* spCtx = (fmt*) vpCtx;
389 if (vpCtx && (spCtx->vpValidate == s_vpMagicNumber)) {
393 cpReturn = cpNext(spCtx);
402 static char* cpNext(fmt* spCtx) {
403 uint64_t uiChars = spCtx->pfnFill(spCtx);
404 spCtx->pfnFmt(spCtx, uiChars);
408 return spCtx->cpFmtBuf;
411 static uint64_t uiFillBytes(fmt* spCtx) {
413 spCtx->uiLastOffset = spCtx->uiNextOffset;
414 while ((uiDst < spCtx->
uiFillLineLen) && (spCtx->uiNextOffset < spCtx->uiDisplayEnd)) {
415 spCtx->ucpBuf8[uiDst++] = spCtx->ucpChars8[spCtx->uiNextOffset++];
420 static uint64_t uiFillFile(fmt* spCtx) {
423 spCtx->uiLastOffset = spCtx->uiNextOffset;
424 while ((uiDst < spCtx->
uiFillLineLen) && (spCtx->uiNextOffset < spCtx->uiDisplayEnd)) {
425 iChar = fgetc(spCtx->spFile);
429 spCtx->uiNextOffset++;
430 spCtx->ucpBuf8[uiDst++] = (uint8_t) iChar;
435 static uint64_t uiFillUnicode(fmt* spCtx) {
437 spCtx->uiLastOffset = spCtx->uiNextOffset;
438 while ((uiDst < spCtx->
uiFillLineLen) && (spCtx->uiNextOffset < spCtx->uiDisplayEnd)) {
439 spCtx->uipBuf32[uiDst++] = spCtx->uipChars32[spCtx->uiNextOffset++];
444 static void vFmtBufUnicode(fmt* spCtx, uint64_t uiChars) {
447 if (spCtx->iIndent > 0) {
448 for (; n < spCtx->iIndent; n++) {
449 spCtx->cpFmtBuf[n] = 32;
452 n += sprintf(&spCtx->cpFmtBuf[n],
"%08"PRIxMAX
" ", spCtx->uiLastOffset);
454 for (; ui < uiChars; ui++) {
456 n += sprintf(&spCtx->cpFmtBuf[n],
" ");
458 n += sprintf(&spCtx->cpFmtBuf[n],
" %06X", spCtx->uipBuf32[ui]);
463 n += sprintf(&spCtx->cpFmtBuf[n],
" ");
465 n += sprintf(&spCtx->cpFmtBuf[n],
" ");
468 n += sprintf(&spCtx->cpFmtBuf[n],
" |");
469 for (ui = 0; ui < uiChars; ui++) {
470 if ((spCtx->uipBuf32[ui] >= 32) && (spCtx->uipBuf32[ui] <= 126)) {
471 n += sprintf(&spCtx->cpFmtBuf[n],
"%c", (
char) spCtx->uipBuf32[ui]);
473 n += sprintf(&spCtx->cpFmtBuf[n],
".");
476 n += sprintf(&spCtx->cpFmtBuf[n],
"|");
478 spCtx->cpFmtBuf[n++] = 10;
479 spCtx->cpFmtBuf[n] = 0;
482 static void vFmtBuf8Hex(fmt* spCtx, uint64_t uiChars) {
485 if (spCtx->iIndent > 0) {
486 for (; n < spCtx->iIndent; n++) {
487 spCtx->cpFmtBuf[n] = 32;
490 n += sprintf(&spCtx->cpFmtBuf[n],
"%08"PRIxMAX
" ", spCtx->uiLastOffset);
492 for (ui = 0; ui < uiChars; ui++) {
494 n += sprintf(&spCtx->cpFmtBuf[n],
" ");
496 n += sprintf(&spCtx->cpFmtBuf[n],
" %02x", spCtx->ucpBuf8[ui]);
499 spCtx->cpFmtBuf[n++] = 10;
500 spCtx->cpFmtBuf[n] = 0;
503 static void vFmtBuf8Hex2(fmt* spCtx, uint64_t uiChars) {
509 uint16_t* uip16 = (uint16_t*)spCtx->ucpBuf8;
510 if (spCtx->iIndent > 0) {
511 for (; n < spCtx->iIndent; n++) {
512 spCtx->cpFmtBuf[n] = 32;
515 n += sprintf(&spCtx->cpFmtBuf[n],
"%08"PRIxMAX
"", spCtx->uiLastOffset);
519 ui16 = (uint16_t)spCtx->ucpBuf8[uiChars - 1];
522 for (ui = 0; ui < uiCount; ui += 2) {
523 n += sprintf(&spCtx->cpFmtBuf[n],
" %04x", uip16[j++]);
525 if(uiCount < uiChars){
526 n += sprintf(&spCtx->cpFmtBuf[n],
" %04x", ui16);
529 spCtx->cpFmtBuf[n++] = 10;
530 spCtx->cpFmtBuf[n] = 0;
533 static void vFmtBuf8Ascii(fmt* spCtx, uint64_t uiChars) {
537 if (spCtx->iIndent > 0) {
538 for (; n < spCtx->iIndent; n++) {
539 spCtx->cpFmtBuf[n] = 32;
542 n += sprintf(&spCtx->cpFmtBuf[n],
"%08"PRIxMAX
" ", spCtx->uiLastOffset);
544 for (; ui < uiChars; ui++) {
545 iChar = (int)spCtx->ucpBuf8[ui];
547 n += sprintf(&spCtx->cpFmtBuf[n],
" \\t");
548 }
else if(iChar == 10){
549 n += sprintf(&spCtx->cpFmtBuf[n],
" \\n");
550 }
else if(iChar == 13){
551 n += sprintf(&spCtx->cpFmtBuf[n],
" \\r");
552 }
else if(iChar >= 32 && iChar <= 126){
553 n += sprintf(&spCtx->cpFmtBuf[n],
" %c", (
char)iChar);
555 n += sprintf(&spCtx->cpFmtBuf[n],
" %03d", iChar);
559 spCtx->cpFmtBuf[n++] = 10;
560 spCtx->cpFmtBuf[n] = 0;
563 static void vFmtBuf8Canonical(fmt* spCtx, uint64_t uiChars) {
566 if (spCtx->iIndent > 0) {
567 for (; n < spCtx->iIndent; n++) {
568 spCtx->cpFmtBuf[n] = 32;
571 n += sprintf(&spCtx->cpFmtBuf[n],
"%08"PRIxMAX
" ", spCtx->uiLastOffset);
573 for (; ui < uiChars; ui++) {
575 n += sprintf(&spCtx->cpFmtBuf[n],
" ");
577 n += sprintf(&spCtx->cpFmtBuf[n],
" %02x", spCtx->ucpBuf8[ui]);
582 n += sprintf(&spCtx->cpFmtBuf[n],
" ");
584 n += sprintf(&spCtx->cpFmtBuf[n],
" ");
587 n += sprintf(&spCtx->cpFmtBuf[n],
" |");
588 for (ui = 0; ui < uiChars; ui++) {
589 if ((spCtx->ucpBuf8[ui] >= 32) && (spCtx->ucpBuf8[ui] <= 126)) {
590 n += sprintf(&spCtx->cpFmtBuf[n],
"%c", (
char) spCtx->ucpBuf8[ui]);
592 n += sprintf(&spCtx->cpFmtBuf[n],
".");
595 n += sprintf(&spCtx->cpFmtBuf[n],
"|");
597 spCtx->cpFmtBuf[n++] = 10;
598 spCtx->cpFmtBuf[n] = 0;
601 static void vReset(fmt* spCtx) {
603 fclose(spCtx->spFile);
604 spCtx->spFile = NULL;
606 spCtx->ucpChars8 = NULL;
607 spCtx->uipChars32 = NULL;
608 spCtx->uiDisplayOffset = 0;
609 spCtx->uiDisplayEnd = 0;
610 spCtx->uiNextOffset = 0;