aboutsummaryrefslogtreecommitdiff
path: root/src/nidsstream.cpp
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2008-10-08 16:17:26 +0000
committerMike Buland <eichlan@xagasoft.com>2008-10-08 16:17:26 +0000
commit3f1c8998166466245aee2860197fb4908e55f1a2 (patch)
treeb6d148679bbd87125f03cb723d5b59969b90c733 /src/nidsstream.cpp
parent5883662909051e99093514483c32e2539a3cf850 (diff)
downloadlibbu++-3f1c8998166466245aee2860197fb4908e55f1a2.tar.gz
libbu++-3f1c8998166466245aee2860197fb4908e55f1a2.tar.bz2
libbu++-3f1c8998166466245aee2860197fb4908e55f1a2.tar.xz
libbu++-3f1c8998166466245aee2860197fb4908e55f1a2.zip
Ok...corrected a problem with new block allocation in nids, and it no longer
goes into an infinite loop while doing certain kinds of read. Also, it zeros out new blocks to make things easier to cope with in the hex editor, it'll probably also compress better. I also fixed Bu::MemBuf so that you can now write to arbitrary places mid-stream.
Diffstat (limited to '')
-rw-r--r--src/nidsstream.cpp40
1 files changed, 28 insertions, 12 deletions
diff --git a/src/nidsstream.cpp b/src/nidsstream.cpp
index 740dc1f..4f692c6 100644
--- a/src/nidsstream.cpp
+++ b/src/nidsstream.cpp
@@ -9,7 +9,7 @@ Bu::NidsStream::NidsStream( Nids &rNids, uint32_t uStream ) :
9 uBlockSize( rNids.iBlockSize-sizeof(Nids::Block) ), 9 uBlockSize( rNids.iBlockSize-sizeof(Nids::Block) ),
10 uPos( 0 ) 10 uPos( 0 )
11{ 11{
12 printf("NidsStream::allocated\n"); 12 //printf("NidsStream::allocated\n");
13 pCurBlock = rNids.newBlock(); 13 pCurBlock = rNids.newBlock();
14 rNids.getBlock( uStream, pCurBlock ); 14 rNids.getBlock( uStream, pCurBlock );
15 uSize = pCurBlock->uBytesUsed; 15 uSize = pCurBlock->uBytesUsed;
@@ -34,6 +34,7 @@ Bu::NidsStream::NidsStream( const Bu::NidsStream &rSrc ) :
34 34
35Bu::NidsStream::~NidsStream() 35Bu::NidsStream::~NidsStream()
36{ 36{
37 //printf("Destroying stream?\n");
37 rNids.updateStreamSize( uStream, uSize ); 38 rNids.updateStreamSize( uStream, uSize );
38 rNids.deleteBlock( pCurBlock ); 39 rNids.deleteBlock( pCurBlock );
39} 40}
@@ -47,20 +48,35 @@ size_t Bu::NidsStream::read( void *pBuf, size_t nBytes )
47 if( uPos%uBlockSize+nBytes < uBlockSize ) 48 if( uPos%uBlockSize+nBytes < uBlockSize )
48 { 49 {
49 size_t iRead = nBytes; 50 size_t iRead = nBytes;
50 if( iRead > pCurBlock->uBytesUsed ) 51 if( iRead > pCurBlock->uBytesUsed-(uPos%uBlockSize) )
51 iRead = pCurBlock->uBytesUsed; 52 iRead = pCurBlock->uBytesUsed-(uPos%uBlockSize);
52 memcpy( pBuf, pCurBlock->pData+(uPos%uBlockSize), iRead ); 53 memcpy( pBuf, pCurBlock->pData+(uPos%uBlockSize), iRead );
53 uPos += nBytes; 54 uPos += iRead;
54 printf("a: block %u = %ub (%ub total)\n", 55 //printf("a: block %u = %ub (%ub total)\n",
55 uCurBlock, pCurBlock->uBytesUsed, uSize ); 56 // uCurBlock, pCurBlock->uBytesUsed, uSize );
56 return iRead; 57 return iRead;
57 } 58 }
58 else 59 else
59 { 60 {
60 //size_t iTotal = 0; 61 size_t nTotal = 0;
61 for(;;) 62 for(;;)
62 { 63 {
63 64 uint32_t iRead = nBytes;
65 if( iRead > uBlockSize-(uPos%uBlockSize) )
66 iRead = uBlockSize-(uPos%uBlockSize);
67 if( iRead > pCurBlock->uBytesUsed-(uPos%uBlockSize) )
68 iRead = pCurBlock->uBytesUsed-(uPos%uBlockSize);
69 memcpy( ((char *)pBuf)+nTotal,
70 pCurBlock->pData+(uPos%uBlockSize), iRead );
71 uPos += iRead;
72 nBytes -= iRead;
73 nTotal += iRead;
74 //printf("r: block %u = %ub/%ub (%ub total)\n",
75 // uCurBlock, iRead, pCurBlock->uBytesUsed, uSize );
76 if( nBytes == 0 || uPos == uSize )
77 return nTotal;
78 if( nTotal%uBlockSize == 0 )
79 uCurBlock = rNids.getNextBlock( uCurBlock, pCurBlock, false );
64 } 80 }
65 } 81 }
66 return 0; 82 return 0;
@@ -75,8 +91,8 @@ size_t Bu::NidsStream::write( const void *pBuf, size_t nBytes )
75 rNids.setBlock( uCurBlock, pCurBlock ); 91 rNids.setBlock( uCurBlock, pCurBlock );
76 uPos += nBytes; 92 uPos += nBytes;
77 uSize += nBytes; 93 uSize += nBytes;
78 printf("a: block %u = %ub (%ub total)\n", 94 //printf("a: block %u = %ub (%ub total)\n",
79 uCurBlock, pCurBlock->uBytesUsed, uSize ); 95 // uCurBlock, pCurBlock->uBytesUsed, uSize );
80 return nBytes; 96 return nBytes;
81 } 97 }
82 else 98 else
@@ -95,8 +111,8 @@ size_t Bu::NidsStream::write( const void *pBuf, size_t nBytes )
95 uPos += uNow; 111 uPos += uNow;
96 nTotal += uNow; 112 nTotal += uNow;
97 nBytes -= uNow; 113 nBytes -= uNow;
98 printf("b: block %u = %ub (%ub total)\n", 114 //printf("b: block %u = %ub (%ub total)\n",
99 uCurBlock, pCurBlock->uBytesUsed, uSize ); 115 // uCurBlock, pCurBlock->uBytesUsed, uSize );
100 if( nBytes == 0 ) 116 if( nBytes == 0 )
101 return nTotal; 117 return nTotal;
102 if( pCurBlock->uBytesUsed == uBlockSize ) 118 if( pCurBlock->uBytesUsed == uBlockSize )