123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625 |
- #include <stdio.h>
- #include <errno.h>
- #include <stdlib.h>
- #include <string.h>
- #include "jam.h"
- #include "structrw.h"
- int JAM_ReadMsgHeader( s_JamBase* Base_PS,
- uint32_t MsgNo_I,
- s_JamMsgHeader* Header_PS,
- s_JamSubPacket** SubfieldPack_PPS )
- {
- s_JamIndex Index_S;
- if ( !Base_PS || !Header_PS )
- return JAM_BAD_PARAM;
-
- if ( fseek( Base_PS->IdxFile_PS, MsgNo_I * sizeof( s_JamIndex ), SEEK_SET ) ) {
- Base_PS->Errno_I = errno;
- return JAM_IO_ERROR;
- }
-
- if ( 1 > freadjamindex(Base_PS->IdxFile_PS,&Index_S) ) {
- Base_PS->Errno_I = errno;
- return JAM_IO_ERROR;
- }
-
- if(Index_S.HdrOffset == 0xffffffff && Index_S.UserCRC == 0xffffffff)
- {
- return JAM_NO_MESSAGE;
- }
-
- if ( fseek( Base_PS->HdrFile_PS, Index_S.HdrOffset, SEEK_SET ) ) {
- Base_PS->Errno_I = errno;
- return JAM_IO_ERROR;
- }
-
- if ( 1 > freadjammsgheader(Base_PS->HdrFile_PS,Header_PS) ) {
- Base_PS->Errno_I = errno;
- return JAM_IO_ERROR;
- }
-
- if ( SubfieldPack_PPS && Header_PS->SubfieldLen ) {
- s_JamSubPacket* SubPacket_PS;
- s_JamSubfield Subfield_S;
- char* Buf_PC;
- char* Ptr_PC;
- char* Roof_PC;
- int BufSize_I = Header_PS->SubfieldLen;
- Buf_PC = (void*) malloc( BufSize_I );
- if ( !Buf_PC )
- return JAM_NO_MEMORY;
-
- if ( 1 > fread( Buf_PC, BufSize_I, 1, Base_PS->HdrFile_PS ) ) {
- Base_PS->Errno_I = errno;
- free (Buf_PC);
- return JAM_IO_ERROR;
- }
- SubPacket_PS = JAM_NewSubPacket();
- if ( !SubPacket_PS ) {
- free (Buf_PC);
- return JAM_NO_MEMORY;
- }
- Roof_PC = Buf_PC + BufSize_I;
-
- for ( Ptr_PC = Buf_PC;
- Ptr_PC < Roof_PC;
- Ptr_PC += Subfield_S.DatLen + SIZE_JAMSAVESUBFIELD ) {
- int Status_I;
- getjamsubfield(Ptr_PC,&Subfield_S);
- if((char *)Subfield_S.Buffer + Subfield_S.DatLen > Roof_PC) {
- JAM_DelSubPacket( SubPacket_PS );
- free (Buf_PC);
- return JAM_CORRUPT_MSG;
- }
- Status_I = JAM_PutSubfield( SubPacket_PS, &Subfield_S );
- if ( Status_I ) {
- JAM_DelSubPacket( SubPacket_PS );
- free (Buf_PC);
- return Status_I;
- }
- }
- free( Buf_PC );
- *SubfieldPack_PPS = SubPacket_PS;
- }
- else
- if ( SubfieldPack_PPS )
-
-
- *SubfieldPack_PPS = JAM_NewSubPacket();
- return 0;
- }
- int JAM_ReadMsgText( s_JamBase* Base_PS,
- uint32_t Offset_I,
- uint32_t Length_I,
- char* Buffer_PC )
- {
- if ( !Base_PS || !Buffer_PC )
- return JAM_BAD_PARAM;
- if ( !Length_I )
- return 0;
- if ( fseek( Base_PS->TxtFile_PS, Offset_I, SEEK_SET ) ) {
- Base_PS->Errno_I = errno;
- return JAM_IO_ERROR;
- }
- if ( 1 > fread( Buffer_PC, Length_I, 1, Base_PS->TxtFile_PS ) ) {
- Base_PS->Errno_I = errno;
- return JAM_IO_ERROR;
- }
- return 0;
- }
- int JAM_ChangeMsgHeader( s_JamBase* Base_PS,
- uint32_t MsgNo_I,
- s_JamMsgHeader* Header_PS )
- {
- s_JamBaseHeader BaseHeader_S;
- s_JamMsgHeader OldHeader_S;
- s_JamIndex Index_S;
- int Status_I;
- if ( !Base_PS )
- return JAM_BAD_PARAM;
- if ( !Base_PS->Locked_I )
- return JAM_NOT_LOCKED;
-
- Status_I = JAM_ReadMBHeader( Base_PS, &BaseHeader_S );
- if ( Status_I )
- return Status_I;
-
- if ( fseek( Base_PS->IdxFile_PS, MsgNo_I * sizeof( s_JamIndex ),
- SEEK_SET ) ) {
- Base_PS->Errno_I = errno;
- return JAM_IO_ERROR;
- }
-
- if ( 1 > freadjamindex(Base_PS->IdxFile_PS,&Index_S) ) {
- Base_PS->Errno_I = errno;
- return JAM_IO_ERROR;
- }
-
- if ( fseek( Base_PS->HdrFile_PS, Index_S.HdrOffset, SEEK_SET ) ) {
- Base_PS->Errno_I = errno;
- return JAM_IO_ERROR;
- }
-
- if ( 1 > freadjammsgheader( Base_PS->HdrFile_PS, &OldHeader_S ) ) {
- Base_PS->Errno_I = errno;
- return JAM_IO_ERROR;
- }
-
- if ( fseek( Base_PS->HdrFile_PS, Index_S.HdrOffset, SEEK_SET ) ) {
- Base_PS->Errno_I = errno;
- return JAM_IO_ERROR;
- }
-
- if ( 1 > fwritejammsgheader(Base_PS->HdrFile_PS,Header_PS) ) {
- Base_PS->Errno_I = errno;
- return JAM_IO_ERROR;
- }
- if( ( Header_PS->Attribute & JAM_MSG_DELETED ) && !(OldHeader_S.Attribute & JAM_MSG_DELETED) ) {
-
- BaseHeader_S.ActiveMsgs--;
- }
- Status_I = JAM_WriteMBHeader( Base_PS, &BaseHeader_S );
- if ( Status_I )
- return Status_I;
- return 0;
- }
- int JAM_AddMessage( s_JamBase* Base_PS,
- s_JamMsgHeader* Header_PS,
- s_JamSubPacket* SubPack_PS,
- char* Text_PC,
- uint32_t TextLen_I )
- {
- s_JamBaseHeader BaseHeader_S;
- s_JamIndex Index_S;
- long Offset_I;
- int Status_I;
- uint32_t TotLen_I;
- if ( !Base_PS )
- return JAM_BAD_PARAM;
- if ( !Base_PS->Locked_I )
- return JAM_NOT_LOCKED;
-
- Status_I = JAM_ReadMBHeader( Base_PS, &BaseHeader_S );
- if ( Status_I )
- return Status_I;
-
- Header_PS->TxtOffset = 0;
- Header_PS->TxtLen = 0;
- if(Text_PC && TextLen_I!=0)
- {
-
- if ( fseek( Base_PS->TxtFile_PS, 0, SEEK_END ) ) {
- Base_PS->Errno_I = errno;
- return JAM_IO_ERROR;
- }
-
- Offset_I = ftell( Base_PS->TxtFile_PS );
- if ( Offset_I == -1 ) {
- Base_PS->Errno_I = errno;
- return JAM_IO_ERROR;
- }
- Header_PS->TxtOffset = Offset_I;
- Header_PS->TxtLen = TextLen_I;
-
- if ( 1 > fwrite( Text_PC, TextLen_I, 1, Base_PS->TxtFile_PS ) ) {
- Base_PS->Errno_I = errno;
- return JAM_IO_ERROR;
- }
- }
-
-
- if ( fseek( Base_PS->HdrFile_PS, 0, SEEK_END ) ) {
- Base_PS->Errno_I = errno;
- return JAM_IO_ERROR;
- }
-
- TotLen_I = 0;
- if ( SubPack_PS ) {
- s_JamSubfield* Subfield_PS;
- for ( Subfield_PS = JAM_GetSubfield( SubPack_PS ); Subfield_PS;
- Subfield_PS = JAM_GetSubfield( NULL ) )
- TotLen_I += sizeof( s_JamSaveSubfield ) + Subfield_PS->DatLen;
- }
- Header_PS->SubfieldLen = TotLen_I;
-
- if ( fseek( Base_PS->IdxFile_PS, 0, SEEK_END ) ) {
- Base_PS->Errno_I = errno;
- return JAM_IO_ERROR;
- }
-
- Offset_I = ftell( Base_PS->IdxFile_PS );
- if ( Offset_I == -1 ) {
- Base_PS->Errno_I = errno;
- return JAM_IO_ERROR;
- }
-
- Header_PS->MsgNum = Offset_I / sizeof( s_JamIndex ) +
- BaseHeader_S.BaseMsgNum;
- memcpy( Header_PS->Signature, HEADERSIGNATURE, 4 );
- Header_PS->Revision = CURRENTREVLEV;
-
- if ( fseek( Base_PS->HdrFile_PS, 0, SEEK_END ) ) {
- Base_PS->Errno_I = errno;
- return JAM_IO_ERROR;
- }
-
- Offset_I = ftell( Base_PS->HdrFile_PS );
- if ( Offset_I == -1 ) {
- Base_PS->Errno_I = errno;
- return JAM_IO_ERROR;
- }
- Index_S.HdrOffset = Offset_I;
-
- if ( 1 > fwritejammsgheader(Base_PS->HdrFile_PS,Header_PS) ) {
- Base_PS->Errno_I = errno;
- return JAM_IO_ERROR;
- }
-
- if ( SubPack_PS ) {
- s_JamSubfield* Subfield_PS;
- char User_AC[101];
-
- User_AC[0] = 0;
- for ( Subfield_PS = JAM_GetSubfield( SubPack_PS ); Subfield_PS;
- Subfield_PS = JAM_GetSubfield( NULL ) ) {
-
- if ( 1 > fwritejamsavesubfield(Base_PS->HdrFile_PS,(s_JamSaveSubfield *)Subfield_PS) ) {
- Base_PS->Errno_I = errno;
- return JAM_IO_ERROR;
- }
-
- if(Subfield_PS->DatLen) {
- if ( 1 > fwrite( Subfield_PS->Buffer, Subfield_PS->DatLen,
- 1, Base_PS->HdrFile_PS ) ) {
- Base_PS->Errno_I = errno;
- return JAM_IO_ERROR;
- }
- }
-
- if ( Subfield_PS->LoID == JAMSFLD_RECVRNAME ) {
- memcpy( User_AC, Subfield_PS->Buffer, Subfield_PS->DatLen );
- User_AC[ Subfield_PS->DatLen ] = 0;
- }
- }
-
- if ( User_AC[0] )
- Index_S.UserCRC = JAM_Crc32( User_AC, strlen( User_AC ) );
- else
- Index_S.UserCRC = JAM_NO_CRC;
- }
- else
-
- Index_S.UserCRC = JAM_NO_CRC;
-
-
- if ( 1 > fwritejamindex(Base_PS->IdxFile_PS,&Index_S) ) {
- Base_PS->Errno_I = errno;
- return JAM_IO_ERROR;
- }
- if(!(Header_PS->Attribute & JAM_MSG_DELETED))
- BaseHeader_S.ActiveMsgs++;
-
- Status_I = JAM_WriteMBHeader( Base_PS, &BaseHeader_S );
- if ( Status_I )
- return Status_I;
- return 0;
- }
- int JAM_AddEmptyMessage( s_JamBase* Base_PS)
- {
- s_JamIndex Index_S;
- if ( !Base_PS )
- return JAM_BAD_PARAM;
- if ( !Base_PS->Locked_I )
- return JAM_NOT_LOCKED;
-
- if ( fseek( Base_PS->IdxFile_PS, 0, SEEK_END ) ) {
- Base_PS->Errno_I = errno;
- return JAM_IO_ERROR;
- }
-
- Index_S.HdrOffset = 0xffffffff;
- Index_S.UserCRC = 0xffffffff;
-
- if ( 1 > fwritejamindex(Base_PS->IdxFile_PS,&Index_S) ) {
- Base_PS->Errno_I = errno;
- return JAM_IO_ERROR;
- }
- return 0;
- }
- int JAM_DeleteMessage( s_JamBase* Base_PS,
- uint32_t MsgNo_I )
- {
- s_JamBaseHeader BaseHeader_S;
- s_JamMsgHeader Header_S;
- s_JamIndex Index_S;
- int Status_I;
- uint32_t OldAttribute_I;
- if ( !Base_PS )
- return JAM_BAD_PARAM;
- if ( !Base_PS->Locked_I )
- return JAM_NOT_LOCKED;
-
- Status_I = JAM_ReadMBHeader( Base_PS, &BaseHeader_S );
- if ( Status_I )
- return Status_I;
-
- if ( fseek( Base_PS->IdxFile_PS, MsgNo_I * sizeof( s_JamIndex ), SEEK_SET ) ) {
- Base_PS->Errno_I = errno;
- return JAM_IO_ERROR;
- }
-
- if ( 1 > freadjamindex( Base_PS->IdxFile_PS, &Index_S ) ) {
- Base_PS->Errno_I = errno;
- return JAM_IO_ERROR;
- }
-
- if(Index_S.HdrOffset == 0xffffffff && Index_S.UserCRC == 0xffffffff)
- {
- return JAM_NO_MESSAGE;
- }
-
- if ( fseek( Base_PS->HdrFile_PS, Index_S.HdrOffset, SEEK_SET ) ) {
- Base_PS->Errno_I = errno;
- return JAM_IO_ERROR;
- }
-
- if ( 1 > freadjammsgheader( Base_PS->HdrFile_PS, &Header_S ) ) {
- Base_PS->Errno_I = errno;
- return JAM_IO_ERROR;
- }
- OldAttribute_I = Header_S.Attribute;
- Header_S.Attribute |= JAM_MSG_DELETED;
-
- if ( fseek( Base_PS->HdrFile_PS, Index_S.HdrOffset, SEEK_SET ) ) {
- Base_PS->Errno_I = errno;
- return JAM_IO_ERROR;
- }
-
- if ( 1 > fwritejammsgheader( Base_PS->HdrFile_PS, &Header_S ) ) {
- Base_PS->Errno_I = errno;
- return JAM_IO_ERROR;
- }
-
- if ( fseek( Base_PS->IdxFile_PS, MsgNo_I * sizeof( s_JamIndex ), SEEK_SET ) ) {
- Base_PS->Errno_I = errno;
- return JAM_IO_ERROR;
- }
- Index_S.HdrOffset = 0xffffffff;
- Index_S.UserCRC = 0xffffffff;
-
- if ( 1 > fwritejamindex(Base_PS->IdxFile_PS,&Index_S) ) {
- Base_PS->Errno_I = errno;
- return JAM_IO_ERROR;
- }
- if(!(OldAttribute_I & JAM_MSG_DELETED))
- BaseHeader_S.ActiveMsgs--;
-
- Status_I = JAM_WriteMBHeader( Base_PS, &BaseHeader_S );
- if ( Status_I )
- return Status_I;
- return 0;
- }
- int JAM_Errno( s_JamBase* Base_PS )
- {
- return Base_PS->Errno_I;
- }
- int JAM_ClearMsgHeader( s_JamMsgHeader* Header_PS )
- {
- if (!Header_PS)
- return JAM_BAD_PARAM;
- memset( Header_PS, 0, sizeof( s_JamMsgHeader ) );
- memcpy( Header_PS->Signature, HEADERSIGNATURE, 4 );
- Header_PS->Revision = CURRENTREVLEV;
- Header_PS->MsgIdCRC = JAM_NO_CRC;
- Header_PS->ReplyCRC = JAM_NO_CRC;
- Header_PS->PasswordCRC = JAM_NO_CRC;
- return 0;
- }
|