status_t DenybbleizeData(const String & nybbleizedText, ByteBuffer & retBuf) { const uint32 numBytes = nybbleizedText.Length(); if ((numBytes%2)!=0) { LogTime(MUSCLE_LOG_ERROR, "DenybblizeData: Nybblized text [%s] has an odd length; that shouldn't ever happen!\n", nybbleizedText()); return B_ERROR; } if (retBuf.SetNumBytes(numBytes/2, false) != B_NO_ERROR) return B_ERROR; uint8 * b = retBuf.GetBuffer(); for (uint32 i=0; i<numBytes; i+=2) { const char c1 = nybbleizedText[i+0]; const char c2 = nybbleizedText[i+1]; if ((muscleInRange(c1, 'A', 'P') == false)||(muscleInRange(c2, 'A', 'P') == false)) { LogTime(MUSCLE_LOG_ERROR, "DenybblizeData: Nybblized text [%s] contains characters other than A through P!\n", nybbleizedText()); return B_ERROR; } *b++ = (uint8) (((c1-'A')<<0)|((c2-'A')<<4)); } return B_NO_ERROR; }
// This program exercises the ParseFile() function. int main(int argc, char ** argv) { if (argc < 2) { printf("Usage: parsefile <filename> [filename] [...]\n"); return 5; } else { for (int i=1; i<argc; i++) { printf("TESTING ParseFile() with a FILE pointer for file [%s]\n", argv[i]); { FILE * fpIn = muscleFopen(argv[i], "r"); if (fpIn) { Message msg; if (ParseFile(fpIn, msg) == B_NO_ERROR) { LogTime(MUSCLE_LOG_INFO, "Parsed contents of file [%s]:\n", argv[i]); msg.PrintToStream(); printf("\n"); const String s = UnparseFile(msg); printf(" UnparseFile(msg) output is below: -------------\n[%s]", s()); } else LogTime(MUSCLE_LOG_ERROR, "Error parsing file [%s]\n", argv[i]); fclose(fpIn); } else LogTime(MUSCLE_LOG_ERROR, "Unable to open file [%s]\n", argv[i]); } printf("\n\nTESTING ParseFile() with a String for file [%s]\n", argv[i]); { FILE * fpIn = muscleFopen(argv[i], "r"); if (fpIn) { FileDataIO fdio(fpIn); const int64 fileLen = fdio.GetLength(); ByteBuffer bb; if ((fileLen >= 0)&&(bb.SetNumBytes((uint32)fileLen, false) == B_NO_ERROR)&&(fdio.ReadFully(bb.GetBuffer(), fileLen) == fileLen)) { const String s((const char *) bb.GetBuffer(), bb.GetNumBytes()); Message msg; if (ParseFile(s, msg) == B_NO_ERROR) { LogTime(MUSCLE_LOG_INFO, "Parsed contents of file [%s]:\n", argv[i]); msg.PrintToStream(); printf("\n"); const String s = UnparseFile(msg); printf(" UnparseFile(msg) output is below: -------------\n[%s]", s()); } else LogTime(MUSCLE_LOG_ERROR, "Error parsing file [%s]\n", argv[i]); } else LogTime(MUSCLE_LOG_ERROR, "Unable to read file [%s]\n", argv[i]); } else LogTime(MUSCLE_LOG_ERROR, "Unable to open file [%s]\n", argv[i]); } } return 0; } }
String HexBytesToString(const ByteBuffer & bb) { return HexBytesToString(bb.GetBuffer(), bb.GetNumBytes()); }
String DenybbleizeString(const String & ns) { ByteBuffer outBuf; return (DenybbleizeData(ns, outBuf) == B_NO_ERROR) ? String((const char *) outBuf.GetBuffer(), outBuf.GetNumBytes()) : String(); }
status_t NybbleizeData(const ByteBuffer & buf, String & retString) { return NybbleizeData(buf.GetBuffer(), buf.GetNumBytes(), retString); }