aboutsummaryrefslogtreecommitdiff
path: root/src/nidsstream.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/nidsstream.cpp')
-rw-r--r--src/nidsstream.cpp65
1 files changed, 47 insertions, 18 deletions
diff --git a/src/nidsstream.cpp b/src/nidsstream.cpp
index 4f692c6..fff73f6 100644
--- a/src/nidsstream.cpp
+++ b/src/nidsstream.cpp
@@ -45,15 +45,27 @@ void Bu::NidsStream::close()
45 45
46size_t Bu::NidsStream::read( void *pBuf, size_t nBytes ) 46size_t Bu::NidsStream::read( void *pBuf, size_t nBytes )
47{ 47{
48 if( uPos%uBlockSize+nBytes < uBlockSize ) 48 if( nBytes == 0 )
49 return 0;
50 if( nBytes + uPos > uSize )
51 nBytes = uSize - uPos;
52 if( (uPos%uBlockSize)+nBytes < uBlockSize )
49 { 53 {
50 size_t iRead = nBytes; 54 size_t iRead = nBytes;
51 if( iRead > pCurBlock->uBytesUsed-(uPos%uBlockSize) ) 55 if( iRead > pCurBlock->uBytesUsed-(uPos%uBlockSize) )
52 iRead = pCurBlock->uBytesUsed-(uPos%uBlockSize); 56 iRead = pCurBlock->uBytesUsed-(uPos%uBlockSize);
53 memcpy( pBuf, pCurBlock->pData+(uPos%uBlockSize), iRead ); 57 memcpy( pBuf, pCurBlock->pData+(uPos%uBlockSize), iRead );
58 //printf("buffill: %ub, %u-%u/%u -> %d-%d/%d (a:%u)",
59 // iRead, uPos, uPos+iRead-1, uSize, 0, iRead-1, nBytes, uCurBlock );
54 uPos += iRead; 60 uPos += iRead;
55 //printf("a: block %u = %ub (%ub total)\n", 61 //printf(" -- %u\n", uPos%uBlockSize );
56 // uCurBlock, pCurBlock->uBytesUsed, uSize ); 62 //printf("ra: block %u = %ub:%u (%ub total)\n",
63 // uCurBlock, uPos, nBytes, uSize );
64
65 // This can't happen, if we're right on a boundery, it goes to the
66 // other case
67 //if( uPos%uBlockSize == 0 )
68 // uCurBlock = rNids.getNextBlock( uCurBlock, pCurBlock, false );
57 return iRead; 69 return iRead;
58 } 70 }
59 else 71 else
@@ -68,15 +80,18 @@ size_t Bu::NidsStream::read( void *pBuf, size_t nBytes )
68 iRead = pCurBlock->uBytesUsed-(uPos%uBlockSize); 80 iRead = pCurBlock->uBytesUsed-(uPos%uBlockSize);
69 memcpy( ((char *)pBuf)+nTotal, 81 memcpy( ((char *)pBuf)+nTotal,
70 pCurBlock->pData+(uPos%uBlockSize), iRead ); 82 pCurBlock->pData+(uPos%uBlockSize), iRead );
83 //printf("buffill: %ub, %u-%u/%u -> %d-%d/%d (b:%u)\n",
84 // iRead, uPos, uPos+iRead-1, uSize,
85 // nTotal, nTotal+nBytes-1, nBytes, uCurBlock );
71 uPos += iRead; 86 uPos += iRead;
72 nBytes -= iRead; 87 nBytes -= iRead;
73 nTotal += iRead; 88 nTotal += iRead;
74 //printf("r: block %u = %ub/%ub (%ub total)\n", 89 //printf("rb: block %u = %ub:%u (%ub total)\n",
75 // uCurBlock, iRead, pCurBlock->uBytesUsed, uSize ); 90 // uCurBlock, uPos, iRead, uSize );
76 if( nBytes == 0 || uPos == uSize ) 91 if( uPos%uBlockSize == 0 )
77 return nTotal;
78 if( nTotal%uBlockSize == 0 )
79 uCurBlock = rNids.getNextBlock( uCurBlock, pCurBlock, false ); 92 uCurBlock = rNids.getNextBlock( uCurBlock, pCurBlock, false );
93 if( nBytes == 0 || uPos >= uSize )
94 return nTotal;
80 } 95 }
81 } 96 }
82 return 0; 97 return 0;
@@ -84,15 +99,26 @@ size_t Bu::NidsStream::read( void *pBuf, size_t nBytes )
84 99
85size_t Bu::NidsStream::write( const void *pBuf, size_t nBytes ) 100size_t Bu::NidsStream::write( const void *pBuf, size_t nBytes )
86{ 101{
87 if( uPos%uBlockSize+nBytes < uBlockSize ) 102 if( nBytes == 0 )
103 return 0;
104/* if( pCurBlock->uBytesUsed >= uBlockSize )
88 { 105 {
106 // We're at the end of our current block, allocate another before we do
107 // anything.
108 uCurBlock = rNids.getNextBlock( uCurBlock, pCurBlock );
109 } */
110 if( (uPos%uBlockSize)+nBytes < uBlockSize )
111 {
112 //printf("wa: %u:%u:%u:%u -> ", uPos, uPos%uBlockSize, uSize, pCurBlock->uBytesUsed );
89 memcpy( pCurBlock->pData+(uPos%uBlockSize), pBuf, nBytes ); 113 memcpy( pCurBlock->pData+(uPos%uBlockSize), pBuf, nBytes );
90 pCurBlock->uBytesUsed += nBytes; 114 if( (uPos%uBlockSize)+nBytes > pCurBlock->uBytesUsed )
115 pCurBlock->uBytesUsed = (uPos%uBlockSize)+nBytes;
91 rNids.setBlock( uCurBlock, pCurBlock ); 116 rNids.setBlock( uCurBlock, pCurBlock );
92 uPos += nBytes; 117 uPos += nBytes;
93 uSize += nBytes; 118 if( uPos > uSize )
94 //printf("a: block %u = %ub (%ub total)\n", 119 uSize = uPos;
95 // uCurBlock, pCurBlock->uBytesUsed, uSize ); 120 //printf("block %u = %ub (%ub total) %d:%u\n",
121 // uCurBlock, pCurBlock->uBytesUsed, uSize, nBytes, uPos );
96 return nBytes; 122 return nBytes;
97 } 123 }
98 else 124 else
@@ -101,22 +127,25 @@ size_t Bu::NidsStream::write( const void *pBuf, size_t nBytes )
101 for(;;) 127 for(;;)
102 { 128 {
103 uint32_t uNow = uBlockSize-(uPos%uBlockSize); 129 uint32_t uNow = uBlockSize-(uPos%uBlockSize);
130 //printf("uNow: %u (%u-(%u%%%u)) %d req\n", uNow, uBlockSize, uPos, uBlockSize, nBytes );
104 if( nBytes < uNow ) 131 if( nBytes < uNow )
105 uNow = nBytes; 132 uNow = nBytes;
106 memcpy( pCurBlock->pData+(uPos%uBlockSize), 133 memcpy( pCurBlock->pData+(uPos%uBlockSize),
107 &((char *)pBuf)[nTotal], uNow ); 134 &((char *)pBuf)[nTotal], uNow );
108 pCurBlock->uBytesUsed += uNow; 135 if( (uPos%uBlockSize)+uNow > pCurBlock->uBytesUsed )
136 pCurBlock->uBytesUsed = (uPos%uBlockSize)+uNow;
109 rNids.setBlock( uCurBlock, pCurBlock ); 137 rNids.setBlock( uCurBlock, pCurBlock );
110 uSize += uNow;
111 uPos += uNow; 138 uPos += uNow;
139 if( uPos > uSize )
140 uSize = uPos;
112 nTotal += uNow; 141 nTotal += uNow;
113 nBytes -= uNow; 142 nBytes -= uNow;
114 //printf("b: block %u = %ub (%ub total)\n", 143 //printf("wb: block %u = %ub (%ub total)\n",
115 // uCurBlock, pCurBlock->uBytesUsed, uSize ); 144 // uCurBlock, pCurBlock->uBytesUsed, uSize );
116 if( nBytes == 0 )
117 return nTotal;
118 if( pCurBlock->uBytesUsed == uBlockSize ) 145 if( pCurBlock->uBytesUsed == uBlockSize )
119 uCurBlock = rNids.getNextBlock( uCurBlock, pCurBlock ); 146 uCurBlock = rNids.getNextBlock( uCurBlock, pCurBlock );
147 if( nBytes == 0 )
148 return nTotal;
120 } 149 }
121 } 150 }
122} 151}