tbuffer.c 10.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright (c) 2020 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

S
Shengliang Guan 已提交
16
#include "os.h"
17
#include "tbuffer.h"
weixin_48148422's avatar
weixin_48148422 已提交
18
#include "exception.h"
H
Haojun Liao 已提交
19
//#include "taoserror.h"
20

21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
typedef union Un4B {
  uint32_t ui;
  float     f;
} Un4B;
#if __STDC_VERSION__ >= 201112L
static_assert(sizeof(Un4B) == sizeof(uint32_t), "sizeof(Un4B) must equal to sizeof(uint32_t)");
static_assert(sizeof(Un4B) == sizeof(float), "sizeof(Un4B) must equal to sizeof(float)");
#endif

typedef union Un8B {
  uint64_t ull;
  double     d;
} Un8B;
#if __STDC_VERSION__ >= 201112L
static_assert(sizeof(Un8B) == sizeof(uint64_t), "sizeof(Un8B) must equal to sizeof(uint64_t)");
static_assert(sizeof(Un8B) == sizeof(double), "sizeof(Un8B) must equal to sizeof(double)");
#endif

39
////////////////////////////////////////////////////////////////////////////////
40
// reader functions
41

42 43
size_t tbufSkip(SBufferReader* buf, size_t size) {
  if( (buf->pos + size) > buf->size ) {
H
Haojun Liao 已提交
44
    THROW( -1 );
45 46
  }
  size_t old = buf->pos;
47
  buf->pos += size;
48 49 50
  return old;
}

weixin_48148422's avatar
weixin_48148422 已提交
51 52
const char* tbufRead( SBufferReader* buf, size_t size ) {
  const char* ret = buf->data + buf->pos;
53
  tbufSkip( buf, size );
54 55 56
  return ret;
}

57 58
void tbufReadToBuffer( SBufferReader* buf, void* dst, size_t size ) {
  assert( dst != NULL );
59
  // always using memcpy, leave optimization to compiler
60
  memcpy( dst, tbufRead(buf, size), size );
61 62
}

63
static size_t tbufReadLength( SBufferReader* buf ) {
weixin_48148422's avatar
weixin_48148422 已提交
64 65 66
  // maximum length is 65535, if larger length is required
  // this function and the corresponding write function need to be
  // revised.
67
  uint16_t l = tbufReadUint16( buf );
weixin_48148422's avatar
weixin_48148422 已提交
68 69 70
  return l;
}

71 72
const char* tbufReadString( SBufferReader* buf, size_t* len ) {
  size_t l = tbufReadLength( buf );
weixin_48148422's avatar
weixin_48148422 已提交
73
  const char* ret = buf->data + buf->pos;
74 75
  tbufSkip( buf, l + 1 );
  if( ret[l] != 0 ) {
H
Haojun Liao 已提交
76
    THROW( -1 );
77 78
  }
  if( len != NULL ) {
79 80 81 82 83
    *len = l;
  }
  return ret;
}

84 85 86 87
size_t tbufReadToString( SBufferReader* buf, char* dst, size_t size ) {
  assert( dst != NULL );
  size_t len;
  const char* str = tbufReadString( buf, &len );
weixin_48148422's avatar
weixin_48148422 已提交
88 89 90
  if (len >= size) {
    len = size - 1;
  }
91
  memcpy( dst, str, len );
92 93 94 95
  dst[len] = 0;
  return len;
}

96 97
const char* tbufReadBinary( SBufferReader* buf, size_t *len ) {
  size_t l = tbufReadLength( buf );
weixin_48148422's avatar
weixin_48148422 已提交
98
  const char* ret = buf->data + buf->pos;
99 100
  tbufSkip( buf, l );
  if( len != NULL ) {
weixin_48148422's avatar
weixin_48148422 已提交
101 102 103 104 105
    *len = l;
  }
  return ret;
}

106 107 108 109 110
size_t tbufReadToBinary( SBufferReader* buf, void* dst, size_t size ) {
  assert( dst != NULL );
  size_t len;
  const char* data = tbufReadBinary( buf, &len );
  if( len >= size ) {
weixin_48148422's avatar
weixin_48148422 已提交
111 112
    len = size;
  }
113
  memcpy( dst, data, len );
weixin_48148422's avatar
weixin_48148422 已提交
114 115
  return len;
}
116

117 118 119 120 121
bool tbufReadBool( SBufferReader* buf ) {
  bool ret;
  tbufReadToBuffer( buf, &ret, sizeof(ret) );
  return ret;
}
122

123 124 125 126
char tbufReadChar( SBufferReader* buf ) {
  char ret;
  tbufReadToBuffer( buf, &ret, sizeof(ret) );
  return ret;
weixin_48148422's avatar
weixin_48148422 已提交
127 128
}

129 130 131 132
int8_t tbufReadInt8( SBufferReader* buf ) {
  int8_t ret;
  tbufReadToBuffer( buf, &ret, sizeof(ret) );
  return ret;
133 134
}

135 136 137 138
uint8_t tbufReadUint8( SBufferReader* buf ) {
  uint8_t ret;
  tbufReadToBuffer( buf, &ret, sizeof(ret) );
  return ret;
weixin_48148422's avatar
weixin_48148422 已提交
139 140
}

141 142 143 144 145
int16_t tbufReadInt16( SBufferReader* buf ) {
  int16_t ret;
  tbufReadToBuffer( buf, &ret, sizeof(ret) );
  if( buf->endian ) {
    return (int16_t)ntohs( ret );
146
  }
147 148
  return ret;
}
149

150 151 152 153 154 155
uint16_t tbufReadUint16( SBufferReader* buf ) {
  uint16_t ret;
  tbufReadToBuffer( buf, &ret, sizeof(ret) );
  if( buf->endian ) {
    return ntohs( ret );
  }
156
  return ret;
157 158
}

159 160 161 162 163 164 165
int32_t tbufReadInt32( SBufferReader* buf ) {
  int32_t ret;
  tbufReadToBuffer( buf, &ret, sizeof(ret) );
  if( buf->endian ) {
    return (int32_t)ntohl( ret );
  }
  return ret;
166 167
}

168 169 170 171 172 173 174
uint32_t tbufReadUint32( SBufferReader* buf ) {
  uint32_t ret;
  tbufReadToBuffer( buf, &ret, sizeof(ret) );
  if( buf->endian ) {
    return ntohl( ret );
  }
  return ret;
175 176
}

177 178 179 180 181 182 183
int64_t tbufReadInt64( SBufferReader* buf ) {
  int64_t ret;
  tbufReadToBuffer( buf, &ret, sizeof(ret) );
  if( buf->endian ) {
    return (int64_t)htobe64( ret ); // TODO: ntohll
  }
  return ret;
weixin_48148422's avatar
weixin_48148422 已提交
184 185
}

186 187 188 189 190 191 192
uint64_t tbufReadUint64( SBufferReader* buf ) {
  uint64_t ret;
  tbufReadToBuffer( buf, &ret, sizeof(ret) );
  if( buf->endian ) {
    return htobe64( ret ); // TODO: ntohll
  }
  return ret;
193 194
}

195
float tbufReadFloat( SBufferReader* buf ) {
196 197 198 199 200 201
  Un4B _un;
  tbufReadToBuffer( buf, &_un, sizeof(_un) );
  if( buf->endian ) {
    _un.ui = ntohl( _un.ui );
  }
  return _un.f;
202
}
weixin_48148422's avatar
weixin_48148422 已提交
203

204
double tbufReadDouble(SBufferReader* buf) {
205 206 207 208 209 210
  Un8B _un;
  tbufReadToBuffer( buf, &_un, sizeof(_un) );
  if( buf->endian ) {
    _un.ull = htobe64( _un.ull );
  }
  return _un.d;
weixin_48148422's avatar
weixin_48148422 已提交
211 212 213
}

////////////////////////////////////////////////////////////////////////////////
214
// writer functions
weixin_48148422's avatar
weixin_48148422 已提交
215

216
void tbufCloseWriter( SBufferWriter* buf ) {
H
Haojun Liao 已提交
217 218
  tfree(buf->data);
//  (*buf->allocator)( buf->data, 0 );  // potential memory leak.
219 220 221
  buf->data = NULL;
  buf->pos = 0;
  buf->size = 0;
weixin_48148422's avatar
weixin_48148422 已提交
222 223
}

224 225 226 227 228 229 230
void tbufEnsureCapacity( SBufferWriter* buf, size_t size ) {
  size += buf->pos;
  if( size > buf->size ) {
    size_t nsize = size + buf->size;
    char* data = (*buf->allocator)( buf->data, nsize );
    // TODO: the exception should be thrown by the allocator function
    if( data == NULL ) {
H
Haojun Liao 已提交
231
      THROW( -1 );
232 233 234 235
    }
    buf->data = data;
    buf->size = nsize;
  }
weixin_48148422's avatar
weixin_48148422 已提交
236 237
}

238 239 240 241 242
size_t tbufReserve( SBufferWriter* buf, size_t size ) {
  tbufEnsureCapacity( buf, size );
  size_t old = buf->pos;
  buf->pos += size;
  return old;
weixin_48148422's avatar
weixin_48148422 已提交
243 244
}

245 246 247 248 249 250 251
char* tbufGetData( SBufferWriter* buf, bool takeOver ) {
  char* ret = buf->data;
  if( takeOver ) {
    buf->pos = 0;
    buf->size = 0;
    buf->data = NULL;
  }
weixin_48148422's avatar
weixin_48148422 已提交
252 253 254
  return ret;
}

255 256 257 258 259
void tbufWrite( SBufferWriter* buf, const void* data, size_t size ) {
  assert( data != NULL );
  tbufEnsureCapacity( buf, size );
  memcpy( buf->data + buf->pos, data, size );
  buf->pos += size;
weixin_48148422's avatar
weixin_48148422 已提交
260 261
}

262 263 264 265 266 267
void tbufWriteAt( SBufferWriter* buf, size_t pos, const void* data, size_t size ) {
  assert( data != NULL );
  // this function can only be called to fill the gap on previous writes,
  // so 'pos + size <= buf->pos' must be true
  assert( pos + size <= buf->pos );
  memcpy( buf->data + pos, data, size );
weixin_48148422's avatar
weixin_48148422 已提交
268 269
}

270 271 272 273 274 275
static void tbufWriteLength( SBufferWriter* buf, size_t len ) {
  // maximum length is 65535, if larger length is required
  // this function and the corresponding read function need to be
  // revised.
  assert( len <= 0xffff );
  tbufWriteUint16( buf, (uint16_t)len );
weixin_48148422's avatar
weixin_48148422 已提交
276 277
}

278 279 280 281
void tbufWriteStringLen( SBufferWriter* buf, const char* str, size_t len ) {
  tbufWriteLength( buf, len );
  tbufWrite( buf, str, len );
  tbufWriteChar( buf, '\0' );
weixin_48148422's avatar
weixin_48148422 已提交
282 283
}

284 285
void tbufWriteString( SBufferWriter* buf, const char* str ) {
  tbufWriteStringLen( buf, str, strlen(str) );
weixin_48148422's avatar
weixin_48148422 已提交
286 287
}

288 289 290
void tbufWriteBinary( SBufferWriter* buf, const void* data, size_t len ) {
  tbufWriteLength( buf, len );
  tbufWrite( buf, data, len );
weixin_48148422's avatar
weixin_48148422 已提交
291 292
}

293 294
void tbufWriteBool( SBufferWriter* buf, bool data ) {
  tbufWrite( buf, &data, sizeof(data) );
weixin_48148422's avatar
weixin_48148422 已提交
295 296
}

297 298
void tbufWriteBoolAt( SBufferWriter* buf, size_t pos, bool data ) {
  tbufWriteAt( buf, pos, &data, sizeof(data) );
weixin_48148422's avatar
weixin_48148422 已提交
299 300
}

301 302
void tbufWriteChar( SBufferWriter* buf, char data ) {
  tbufWrite( buf, &data, sizeof(data) );
weixin_48148422's avatar
weixin_48148422 已提交
303 304
}

305 306
void tbufWriteCharAt( SBufferWriter* buf, size_t pos, char data ) {
  tbufWriteAt( buf, pos, &data, sizeof(data) );
weixin_48148422's avatar
weixin_48148422 已提交
307 308
}

309 310
void tbufWriteInt8( SBufferWriter* buf, int8_t data ) {
  tbufWrite( buf, &data, sizeof(data) );
weixin_48148422's avatar
weixin_48148422 已提交
311 312
}

313 314
void tbufWriteInt8At( SBufferWriter* buf, size_t pos, int8_t data ) {
  tbufWriteAt( buf, pos, &data, sizeof(data) );
weixin_48148422's avatar
weixin_48148422 已提交
315 316
}

317 318
void tbufWriteUint8( SBufferWriter* buf, uint8_t data ) {
  tbufWrite( buf, &data, sizeof(data) );
weixin_48148422's avatar
weixin_48148422 已提交
319 320
}

321 322
void tbufWriteUint8At( SBufferWriter* buf, size_t pos, uint8_t data ) {
  tbufWriteAt( buf, pos, &data, sizeof(data) );
weixin_48148422's avatar
weixin_48148422 已提交
323 324
}

325 326 327
void tbufWriteInt16( SBufferWriter* buf, int16_t data ) {
  if( buf->endian ) {
    data = (int16_t)htons( data );
weixin_48148422's avatar
weixin_48148422 已提交
328
  }
329
  tbufWrite( buf, &data, sizeof(data) );
weixin_48148422's avatar
weixin_48148422 已提交
330 331
}

332 333 334
void tbufWriteInt16At( SBufferWriter* buf, size_t pos, int16_t data ) {
  if( buf->endian ) {
    data = (int16_t)htons( data );
weixin_48148422's avatar
weixin_48148422 已提交
335
  }
336
  tbufWriteAt( buf, pos, &data, sizeof(data) );
weixin_48148422's avatar
weixin_48148422 已提交
337 338
}

339 340 341
void tbufWriteUint16( SBufferWriter* buf, uint16_t data ) {
  if( buf->endian ) {
    data = htons( data );
weixin_48148422's avatar
weixin_48148422 已提交
342
  }
343
  tbufWrite( buf, &data, sizeof(data) );
weixin_48148422's avatar
weixin_48148422 已提交
344 345
}

346 347 348
void tbufWriteUint16At( SBufferWriter* buf, size_t pos, uint16_t data ) {
  if( buf->endian ) {
    data = htons( data );
weixin_48148422's avatar
weixin_48148422 已提交
349
  }
350
  tbufWriteAt( buf, pos, &data, sizeof(data) );
weixin_48148422's avatar
weixin_48148422 已提交
351 352
}

353 354 355
void tbufWriteInt32( SBufferWriter* buf, int32_t data ) {
  if( buf->endian ) {
    data = (int32_t)htonl( data );
weixin_48148422's avatar
weixin_48148422 已提交
356
  }
357
  tbufWrite( buf, &data, sizeof(data) );
weixin_48148422's avatar
weixin_48148422 已提交
358 359
}

360 361 362
void tbufWriteInt32At( SBufferWriter* buf, size_t pos, int32_t data ) {
  if( buf->endian ) {
    data = (int32_t)htonl( data );
weixin_48148422's avatar
weixin_48148422 已提交
363
  }
364
  tbufWriteAt( buf, pos, &data, sizeof(data) );
weixin_48148422's avatar
weixin_48148422 已提交
365 366
}

367 368 369
void tbufWriteUint32( SBufferWriter* buf, uint32_t data ) {
  if( buf->endian ) {
    data = htonl( data );
weixin_48148422's avatar
weixin_48148422 已提交
370
  }
371
  tbufWrite( buf, &data, sizeof(data) );
weixin_48148422's avatar
weixin_48148422 已提交
372 373
}

374 375 376
void tbufWriteUint32At( SBufferWriter* buf, size_t pos, uint32_t data ) {
  if( buf->endian ) {
    data = htonl( data );
weixin_48148422's avatar
weixin_48148422 已提交
377
  }
378
  tbufWriteAt( buf, pos, &data, sizeof(data) );
weixin_48148422's avatar
weixin_48148422 已提交
379 380
}

381 382 383
void tbufWriteInt64( SBufferWriter* buf, int64_t data ) {
  if( buf->endian ) {
    data = (int64_t)htobe64( data );
weixin_48148422's avatar
weixin_48148422 已提交
384
  }
385
  tbufWrite( buf, &data, sizeof(data) );
weixin_48148422's avatar
weixin_48148422 已提交
386 387
}

388 389 390
void tbufWriteInt64At( SBufferWriter* buf, size_t pos, int64_t data ) {
  if( buf->endian ) {
    data = (int64_t)htobe64( data );
weixin_48148422's avatar
weixin_48148422 已提交
391
  }
392
  tbufWriteAt( buf, pos, &data, sizeof(data) );
weixin_48148422's avatar
weixin_48148422 已提交
393 394
}

395 396 397
void tbufWriteUint64( SBufferWriter* buf, uint64_t data ) {
  if( buf->endian ) {
    data = htobe64( data );
weixin_48148422's avatar
weixin_48148422 已提交
398
  }
399
  tbufWrite( buf, &data, sizeof(data) );
weixin_48148422's avatar
weixin_48148422 已提交
400 401
}

402 403 404
void tbufWriteUint64At( SBufferWriter* buf, size_t pos, uint64_t data ) {
  if( buf->endian ) {
    data = htobe64( data );
weixin_48148422's avatar
weixin_48148422 已提交
405
  }
406
  tbufWriteAt( buf, pos, &data, sizeof(data) );
weixin_48148422's avatar
weixin_48148422 已提交
407 408
}

409
void tbufWriteFloat( SBufferWriter* buf, float data ) {
410 411 412 413 414 415
  Un4B _un;
  _un.f = data;
  if( buf->endian ) {
    _un.ui = htonl( _un.ui );
  }
  tbufWrite( buf, &_un, sizeof(_un) );
weixin_48148422's avatar
weixin_48148422 已提交
416 417
}

418
void tbufWriteFloatAt( SBufferWriter* buf, size_t pos, float data ) {
419 420 421 422 423 424
  Un4B _un;
  _un.f = data;
  if( buf->endian ) {
    _un.ui = htonl( _un.ui );
  }
  tbufWriteAt( buf, pos, &_un, sizeof(_un) );
weixin_48148422's avatar
weixin_48148422 已提交
425 426
}

427
void tbufWriteDouble( SBufferWriter* buf, double data ) {
428 429 430 431 432 433
  Un8B _un;
  _un.d = data;
  if( buf->endian ) {
    _un.ull = htobe64( _un.ull );
  }
  tbufWrite( buf, &_un, sizeof(_un) );
weixin_48148422's avatar
weixin_48148422 已提交
434 435
}

436
void tbufWriteDoubleAt( SBufferWriter* buf, size_t pos, double data ) {
437 438 439 440 441 442
  Un8B _un;
  _un.d = data;
  if( buf->endian ) {
    _un.ull = htobe64( _un.ull );
  }
  tbufWriteAt( buf, pos, &_un, sizeof(_un) );
weixin_48148422's avatar
weixin_48148422 已提交
443
}