// ASN.1 related methods void Person::serialize(ENetPacket* buf) { ASNPerson p; p.name = name.c_str(); p.age = age; AsnBuf abuf; const size_t dataSize = 1024; char data[dataSize]; size_t encodedLen; abuf.Init (data, dataSize); abuf.ResetInWriteRvsMode(); AsnLen len = p.BEncPdu ( abuf, encodedLen ); #ifndef ASN_USE_FILE ofstream outputFile; outputFile.open ("pr.ber"); abuf.ResetInReadMode(); for (; encodedLen > 0; encodedLen--) outputFile.put (abuf.GetByte()); #else # error "Char Asn Encoding not implemented" #endif }
main (int argc, char *argv[]) { ofstream outputFile; AsnBuf outputBuf; size_t encodedLen; const size_t dataSize = 1024; char data[dataSize]; ChildInformation *ciPtr; PersonnelRecord pr; // build internal value of a PersonnelRecord pr.name = new Name; pr.name->givenName = "John"; // this calls pr.name->givenName.Set ("John"); pr.name->initial = "E"; pr.name->familyName = "Smith"; pr.title.Set ("The Big Cheese"); pr.employeeNumber = 99999; pr.dateOfHire.Set ("19820104"); pr.nameOfSpouse = new Name; pr.nameOfSpouse->givenName.Set ("Mary"); pr.nameOfSpouse->initial.Set ("L"); pr.nameOfSpouse->familyName.Set ("Smith"); pr.children = new PersonnelRecordSeqOf; ciPtr = pr.children->Append(); ciPtr->name = new Name; ciPtr->name->givenName.Set ("James"); ciPtr->name->initial.Set ("R"); ciPtr->name->familyName.Set ("Smith"); ciPtr->dateOfBirth.Set ("19570310"); ciPtr = pr.children->Append(); ciPtr->name = new Name; ciPtr->name->givenName.Set ("Lisa"); ciPtr->name->initial.Set ("M"); ciPtr->name->familyName.Set ("Smith"); ciPtr->dateOfBirth.Set ("19610621"); // set up buffer for writing to outputBuf.Init (data, dataSize); outputBuf.ResetInWriteRvsMode(); // encode the internal value we just build into the buffer if (!pr.BEncPdu (outputBuf, encodedLen)) cout << "failed encoding AnyTestType value" << endl; // open file to hold the BER value outputFile.open ("pr.ber"); if (!outputFile) { perror ("ofstream::open"); exit (1); } // copy the BER value from the buffer to the file outputBuf.ResetInReadMode(); for (; encodedLen > 0; encodedLen--) outputFile.put (outputBuf.GetByte()); cout << "Wrote the following BER PersonnelRecord value to pr.ber." << endl; cout << "Test it with \"def\" and \"indef\"." << endl; cout << pr << endl; return 0; }
main (int argc, char *argv[]) { ofstream outputFile; AsnBuf outputBuf; size_t encodedLen; size_t dataSize = 1024; #if HAVE_VARIABLE_SIZED_AUTOMATIC_ARRAYS char data[dataSize]; #else char *data = new char[dataSize]; if (!data) return 1; #endif /* HAVE_VARIABLE_SIZED_AUTOMATIC_ARRAYS */ AnyTestType att; TSeq1 ts1; TSeq2 ts2; AttrValue1 *atv1ptr; AttrValue2 *atv2ptr; AsnInt intVal; AsnBool boolVal; AsnOcts octsVal ("Hi Mom"); OctsId octsIdVal = octsVal; AsnBits bitsVal; BitsId bitsIdVal (9); AsnReal realVal; // READ THIS!!! // you must be really careful when setting the // "value" field and "id" fields in an // ANY/ANY DEFINED BY type because "value" is a // "AsnType*" and will accept any // pointer value. It will even encode // the wrong value without complaining if you // set "value" to the wrong object. atv1ptr = att.intMap.Append(); atv1ptr->id = intId; intVal = -99; atv1ptr->anyDefBy.value = &intVal; atv1ptr = att.intMap.Append(); atv1ptr->id = boolId; boolVal = true; atv1ptr->anyDefBy.value = &boolVal; atv1ptr = att.intMap.Append(); atv1ptr->id = octsId; atv1ptr->anyDefBy.value = &octsIdVal; atv1ptr = att.intMap.Append(); atv1ptr->id = bitsId; bitsIdVal.SetBit (0); bitsIdVal.ClrBit (1); bitsIdVal.SetBit (2); bitsIdVal.ClrBit (3); bitsIdVal.SetBit (4); bitsIdVal.ClrBit (5); bitsIdVal.SetBit (6); bitsIdVal.ClrBit (7); bitsIdVal.SetBit (8); bitsIdVal.ClrBit (9); atv1ptr->anyDefBy.value = &bitsIdVal; atv1ptr = att.intMap.Append(); atv1ptr->id = realId; realVal = 108.3838; atv1ptr->anyDefBy.value = &realVal; // now do TSeq2 with same vals but use OID as identifier atv2ptr = att.oidMap.Append(); atv2ptr->id = intOid; atv2ptr->anyDefBy.value = &intVal; atv2ptr = att.oidMap.Append(); atv2ptr->id = boolOid; atv2ptr->anyDefBy.value = &boolVal; atv2ptr = att.oidMap.Append(); atv2ptr->id = octsOid; atv2ptr->anyDefBy.value = &octsVal; atv2ptr = att.oidMap.Append(); atv2ptr->id = bitsOid; bitsVal = bitsIdVal; // copy bits atv2ptr->anyDefBy.value = &bitsVal; atv2ptr = att.oidMap.Append(); atv2ptr->id = realOid; atv2ptr->anyDefBy.value = &realVal; outputBuf.Init (data, dataSize); outputBuf.ResetInWriteRvsMode(); if (!att.BEncPdu (outputBuf, encodedLen)) cout << "failed encoding AnyTestType value" << endl; outputFile.open ("att.ber"); if (!outputFile) { perror ("ofstream::open"); exit (1); } outputBuf.ResetInReadMode(); for ( ; encodedLen > 0; encodedLen--) outputFile.put (outputBuf.GetByte()); cout << "Wrote the following BER AnyTestType value to att.ber." << endl; cout << "Test it with \"def\" and \"indef\"" << endl; cout << att << endl; return 0; }
main (int argc, char *argv[]) { AsnBuf inputBuf; AsnBuf outputBuf; size_t encodedLen; size_t decodedLen; size_t dataSize; ifstream dataFile; PersonnelRecord pr; if (argc != 2) { cerr << "Usage: " << argv[0] << " <BER data file name>" << endl; cerr << " Decodes the given PersonnelRecord BER data file" << endl; cerr << " and re-encodes it to stdout" << endl; exit (1); } // open the data file dataFile.open (argv[1]); if (!dataFile) { perror ("ifstream::open"); exit (1); } // get size of the data file file dataFile.seekg (0, ios::end); dataSize = dataFile.tellg(); dataFile.seekg (0); // read data from file into contiguous block for a buffer #if HAVE_VARIABLE_SIZED_AUTOMATIC_ARRAYS char data[dataSize]; #else char *data = new char[dataSize]; if (!data) return 1; #endif /* HAVE_VARIABLE_SIZED_AUTOMATIC_ARRAYS */ dataFile.read (data, dataSize); dataFile.close(); // // put the BER data read from the file // into buffer format, ready for reading from the // beginning // inputBuf.InstallData (data, dataSize); if (!pr.BDecPdu (inputBuf, decodedLen)) { cerr << "--- ERROR - Decode routines failed, exiting..." << endl; exit (1); } cerr << "decodedValue PersonnelRecord ::= " << pr << endl << endl; // // allocate a new buffer set up for writing to // #if HAVE_VARIABLE_SIZED_AUTOMATIC_ARRAYS char outputData[dataSize + 512]; #else char *outputData = new char[dataSize + 512]; if (!outputData) return 1; #endif /* HAVE_VARIABLE_SIZED_AUTOMATIC_ARRAYS */ outputBuf.Init (outputData, dataSize+512); outputBuf.ResetInWriteRvsMode(); if (!pr.BEncPdu (outputBuf, encodedLen)) { cerr << "--- ERROR - Encode routines failed" << endl; } // write the BER value to cout outputBuf.ResetInReadMode(); for (; encodedLen > 0; encodedLen--) cout.put (outputBuf.GetByte()); return 0; }