00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef ISC_BUFFER_H
00019 #define ISC_BUFFER_H 1
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108 #include <isc/lang.h>
00109 #include <isc/magic.h>
00110 #include <isc/types.h>
00111
00112
00113
00114
00115
00116
00117
00118 ISC_LANG_BEGINDECLS
00119
00120
00121
00122
00123
00124 #define ISC_BUFFER_MAGIC 0x42756621U
00125 #define ISC_BUFFER_VALID(b) ISC_MAGIC_VALID(b, ISC_BUFFER_MAGIC)
00126
00127
00128
00129
00130
00131
00132
00133 #define ISC_BUFFER_INCR 2048
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146 #define isc_buffer_base(b) ((void *)(b)->base)
00147 #define isc_buffer_current(b) \
00148 ((void *)((unsigned char *)(b)->base + (b)->current))
00149 #define isc_buffer_active(b) \
00150 ((void *)((unsigned char *)(b)->base + (b)->active))
00151 #define isc_buffer_used(b) \
00152 ((void *)((unsigned char *)(b)->base + (b)->used))
00153 #define isc_buffer_length(b) ((b)->length)
00154
00155
00156
00157
00158
00159
00160 #define isc_buffer_usedlength(b) ((b)->used)
00161 #define isc_buffer_consumedlength(b) ((b)->current)
00162 #define isc_buffer_remaininglength(b) ((b)->used - (b)->current)
00163 #define isc_buffer_activelength(b) ((b)->active - (b)->current)
00164 #define isc_buffer_availablelength(b) ((b)->length - (b)->used)
00165
00166
00167
00168
00169
00170
00171
00172
00173 struct isc_buffer {
00174 unsigned int magic;
00175 void *base;
00176
00177
00178 unsigned int length;
00179 unsigned int used;
00180 unsigned int current;
00181 unsigned int active;
00182
00183
00184 ISC_LINK(isc_buffer_t) link;
00185
00186 isc_mem_t *mctx;
00187 };
00188
00189
00190
00191
00192
00193 isc_result_t
00194 isc_buffer_allocate(isc_mem_t *mctx, isc_buffer_t **dynbuffer,
00195 unsigned int length);
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213 isc_result_t
00214 isc_buffer_reallocate(isc_buffer_t **dynbuffer, unsigned int length);
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236 isc_result_t
00237 isc_buffer_reserve(isc_buffer_t **dynbuffer, unsigned int size);
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257 void
00258 isc_buffer_free(isc_buffer_t **dynbuffer);
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273 void
00274 isc__buffer_init(isc_buffer_t *b, void *base, unsigned int length);
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286 void
00287 isc__buffer_initnull(isc_buffer_t *b);
00288
00289
00290
00291
00292 void
00293 isc_buffer_reinit(isc_buffer_t *b, void *base, unsigned int length);
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306 void
00307 isc__buffer_invalidate(isc_buffer_t *b);
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319 void
00320 isc__buffer_region(isc_buffer_t *b, isc_region_t *r);
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331 void
00332 isc__buffer_usedregion(isc_buffer_t *b, isc_region_t *r);
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343 void
00344 isc__buffer_availableregion(isc_buffer_t *b, isc_region_t *r);
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355 void
00356 isc__buffer_add(isc_buffer_t *b, unsigned int n);
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368 void
00369 isc__buffer_subtract(isc_buffer_t *b, unsigned int n);
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381 void
00382 isc__buffer_clear(isc_buffer_t *b);
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396 void
00397 isc__buffer_consumedregion(isc_buffer_t *b, isc_region_t *r);
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408 void
00409 isc__buffer_remainingregion(isc_buffer_t *b, isc_region_t *r);
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420 void
00421 isc__buffer_activeregion(isc_buffer_t *b, isc_region_t *r);
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432 void
00433 isc__buffer_setactive(isc_buffer_t *b, unsigned int n);
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444 void
00445 isc__buffer_first(isc_buffer_t *b);
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459 void
00460 isc__buffer_forward(isc_buffer_t *b, unsigned int n);
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472 void
00473 isc__buffer_back(isc_buffer_t *b, unsigned int n);
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485 void
00486 isc_buffer_compact(isc_buffer_t *b);
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505 isc_uint8_t
00506 isc_buffer_getuint8(isc_buffer_t *b);
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525 void
00526 isc__buffer_putuint8(isc_buffer_t *b, isc_uint8_t val);
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537
00538
00539 isc_uint16_t
00540 isc_buffer_getuint16(isc_buffer_t *b);
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554
00555
00556
00557
00558
00559
00560 void
00561 isc__buffer_putuint16(isc_buffer_t *b, isc_uint16_t val);
00562
00563
00564
00565
00566
00567
00568
00569
00570
00571
00572
00573
00574
00575 isc_uint32_t
00576 isc_buffer_getuint32(isc_buffer_t *b);
00577
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588
00589
00590
00591
00592
00593
00594
00595
00596 void
00597 isc__buffer_putuint32(isc_buffer_t *b, isc_uint32_t val);
00598
00599
00600
00601
00602
00603
00604
00605
00606
00607
00608
00609
00610
00611 isc_uint64_t
00612 isc_buffer_getuint48(isc_buffer_t *b);
00613
00614
00615
00616
00617
00618
00619
00620
00621
00622
00623
00624
00625
00626
00627
00628
00629
00630
00631
00632 void
00633 isc__buffer_putuint48(isc_buffer_t *b, isc_uint64_t val);
00634
00635
00636
00637
00638
00639
00640
00641
00642
00643
00644
00645
00646
00647 void
00648 isc__buffer_putuint24(isc_buffer_t *b, isc_uint32_t val);
00649
00650
00651
00652
00653
00654
00655
00656
00657
00658
00659
00660
00661
00662 void
00663 isc__buffer_putmem(isc_buffer_t *b, const unsigned char *base,
00664 unsigned int length);
00665
00666
00667
00668
00669
00670
00671
00672
00673
00674
00675 void
00676 isc__buffer_putstr(isc_buffer_t *b, const char *source);
00677
00678
00679
00680
00681
00682
00683
00684
00685
00686
00687
00688 isc_result_t
00689 isc_buffer_copyregion(isc_buffer_t *b, const isc_region_t *r);
00690
00691
00692
00693
00694
00695
00696
00697
00698
00699
00700
00701
00702
00703
00704
00705 ISC_LANG_ENDDECLS
00706
00707
00708
00709
00710
00711
00712
00713
00714
00715
00716
00717
00718
00719
00720
00721
00722
00723
00724
00725
00726
00727
00728
00729
00730
00731 #define ISC__BUFFER_INIT(_b, _base, _length) \
00732 do { \
00733 (_b)->base = _base; \
00734 (_b)->length = (_length); \
00735 (_b)->used = 0; \
00736 (_b)->current = 0; \
00737 (_b)->active = 0; \
00738 (_b)->mctx = NULL; \
00739 ISC_LINK_INIT(_b, link); \
00740 (_b)->magic = ISC_BUFFER_MAGIC; \
00741 } while (0)
00742
00743 #define ISC__BUFFER_INITNULL(_b) ISC__BUFFER_INIT(_b, NULL, 0)
00744
00745 #define ISC__BUFFER_INVALIDATE(_b) \
00746 do { \
00747 (_b)->magic = 0; \
00748 (_b)->base = NULL; \
00749 (_b)->length = 0; \
00750 (_b)->used = 0; \
00751 (_b)->current = 0; \
00752 (_b)->active = 0; \
00753 } while (0)
00754
00755 #define ISC__BUFFER_REGION(_b, _r) \
00756 do { \
00757 (_r)->base = (_b)->base; \
00758 (_r)->length = (_b)->length; \
00759 } while (0)
00760
00761 #define ISC__BUFFER_USEDREGION(_b, _r) \
00762 do { \
00763 (_r)->base = (_b)->base; \
00764 (_r)->length = (_b)->used; \
00765 } while (0)
00766
00767 #define ISC__BUFFER_AVAILABLEREGION(_b, _r) \
00768 do { \
00769 (_r)->base = isc_buffer_used(_b); \
00770 (_r)->length = isc_buffer_availablelength(_b); \
00771 } while (0)
00772
00773 #define ISC__BUFFER_ADD(_b, _n) \
00774 do { \
00775 (_b)->used += (_n); \
00776 } while (0)
00777
00778 #define ISC__BUFFER_SUBTRACT(_b, _n) \
00779 do { \
00780 (_b)->used -= (_n); \
00781 if ((_b)->current > (_b)->used) \
00782 (_b)->current = (_b)->used; \
00783 if ((_b)->active > (_b)->used) \
00784 (_b)->active = (_b)->used; \
00785 } while (0)
00786
00787 #define ISC__BUFFER_CLEAR(_b) \
00788 do { \
00789 (_b)->used = 0; \
00790 (_b)->current = 0; \
00791 (_b)->active = 0; \
00792 } while (0)
00793
00794 #define ISC__BUFFER_CONSUMEDREGION(_b, _r) \
00795 do { \
00796 (_r)->base = (_b)->base; \
00797 (_r)->length = (_b)->current; \
00798 } while (0)
00799
00800 #define ISC__BUFFER_REMAININGREGION(_b, _r) \
00801 do { \
00802 (_r)->base = isc_buffer_current(_b); \
00803 (_r)->length = isc_buffer_remaininglength(_b); \
00804 } while (0)
00805
00806 #define ISC__BUFFER_ACTIVEREGION(_b, _r) \
00807 do { \
00808 if ((_b)->current < (_b)->active) { \
00809 (_r)->base = isc_buffer_current(_b); \
00810 (_r)->length = isc_buffer_activelength(_b); \
00811 } else { \
00812 (_r)->base = NULL; \
00813 (_r)->length = 0; \
00814 } \
00815 } while (0)
00816
00817 #define ISC__BUFFER_SETACTIVE(_b, _n) \
00818 do { \
00819 (_b)->active = (_b)->current + (_n); \
00820 } while (0)
00821
00822 #define ISC__BUFFER_FIRST(_b) \
00823 do { \
00824 (_b)->current = 0; \
00825 } while (0)
00826
00827 #define ISC__BUFFER_FORWARD(_b, _n) \
00828 do { \
00829 (_b)->current += (_n); \
00830 } while (0)
00831
00832 #define ISC__BUFFER_BACK(_b, _n) \
00833 do { \
00834 (_b)->current -= (_n); \
00835 } while (0)
00836
00837 #define ISC__BUFFER_PUTMEM(_b, _base, _length) \
00838 do { \
00839 memmove(isc_buffer_used(_b), (_base), (_length)); \
00840 (_b)->used += (_length); \
00841 } while (0)
00842
00843 #define ISC__BUFFER_PUTSTR(_b, _source) \
00844 do { \
00845 unsigned int _length; \
00846 unsigned char *_cp; \
00847 _length = strlen(_source); \
00848 _cp = isc_buffer_used(_b); \
00849 memmove(_cp, (_source), _length); \
00850 (_b)->used += (_length); \
00851 } while (0)
00852
00853 #define ISC__BUFFER_PUTUINT8(_b, _val) \
00854 do { \
00855 unsigned char *_cp; \
00856 isc_uint8_t _val2 = (_val); \
00857 _cp = isc_buffer_used(_b); \
00858 (_b)->used++; \
00859 _cp[0] = _val2 & 0x00ff; \
00860 } while (0)
00861
00862 #define ISC__BUFFER_PUTUINT16(_b, _val) \
00863 do { \
00864 unsigned char *_cp; \
00865 isc_uint16_t _val2 = (_val); \
00866 _cp = isc_buffer_used(_b); \
00867 (_b)->used += 2; \
00868 _cp[0] = (unsigned char)((_val2 & 0xff00U) >> 8); \
00869 _cp[1] = (unsigned char)(_val2 & 0x00ffU); \
00870 } while (0)
00871
00872 #define ISC__BUFFER_PUTUINT24(_b, _val) \
00873 do { \
00874 unsigned char *_cp; \
00875 isc_uint32_t _val2 = (_val); \
00876 _cp = isc_buffer_used(_b); \
00877 (_b)->used += 3; \
00878 _cp[0] = (unsigned char)((_val2 & 0xff0000U) >> 16); \
00879 _cp[1] = (unsigned char)((_val2 & 0xff00U) >> 8); \
00880 _cp[2] = (unsigned char)(_val2 & 0x00ffU); \
00881 } while (0)
00882
00883 #define ISC__BUFFER_PUTUINT32(_b, _val) \
00884 do { \
00885 unsigned char *_cp; \
00886 isc_uint32_t _val2 = (_val); \
00887 _cp = isc_buffer_used(_b); \
00888 (_b)->used += 4; \
00889 _cp[0] = (unsigned char)((_val2 & 0xff000000) >> 24); \
00890 _cp[1] = (unsigned char)((_val2 & 0x00ff0000) >> 16); \
00891 _cp[2] = (unsigned char)((_val2 & 0x0000ff00) >> 8); \
00892 _cp[3] = (unsigned char)((_val2 & 0x000000ff)); \
00893 } while (0)
00894
00895 #if defined(ISC_BUFFER_USEINLINE)
00896 #define isc_buffer_init ISC__BUFFER_INIT
00897 #define isc_buffer_initnull ISC__BUFFER_INITNULL
00898 #define isc_buffer_invalidate ISC__BUFFER_INVALIDATE
00899 #define isc_buffer_region ISC__BUFFER_REGION
00900 #define isc_buffer_usedregion ISC__BUFFER_USEDREGION
00901 #define isc_buffer_availableregion ISC__BUFFER_AVAILABLEREGION
00902 #define isc_buffer_add ISC__BUFFER_ADD
00903 #define isc_buffer_subtract ISC__BUFFER_SUBTRACT
00904 #define isc_buffer_clear ISC__BUFFER_CLEAR
00905 #define isc_buffer_consumedregion ISC__BUFFER_CONSUMEDREGION
00906 #define isc_buffer_remainingregion ISC__BUFFER_REMAININGREGION
00907 #define isc_buffer_activeregion ISC__BUFFER_ACTIVEREGION
00908 #define isc_buffer_setactive ISC__BUFFER_SETACTIVE
00909 #define isc_buffer_first ISC__BUFFER_FIRST
00910 #define isc_buffer_forward ISC__BUFFER_FORWARD
00911 #define isc_buffer_back ISC__BUFFER_BACK
00912 #define isc_buffer_putmem ISC__BUFFER_PUTMEM
00913 #define isc_buffer_putstr ISC__BUFFER_PUTSTR
00914 #define isc_buffer_putuint8 ISC__BUFFER_PUTUINT8
00915 #define isc_buffer_putuint16 ISC__BUFFER_PUTUINT16
00916 #define isc_buffer_putuint24 ISC__BUFFER_PUTUINT24
00917 #define isc_buffer_putuint32 ISC__BUFFER_PUTUINT32
00918 #else
00919 #define isc_buffer_init isc__buffer_init
00920 #define isc_buffer_initnull isc__buffer_initnull
00921 #define isc_buffer_invalidate isc__buffer_invalidate
00922 #define isc_buffer_region isc__buffer_region
00923 #define isc_buffer_usedregion isc__buffer_usedregion
00924 #define isc_buffer_availableregion isc__buffer_availableregion
00925 #define isc_buffer_add isc__buffer_add
00926 #define isc_buffer_subtract isc__buffer_subtract
00927 #define isc_buffer_clear isc__buffer_clear
00928 #define isc_buffer_consumedregion isc__buffer_consumedregion
00929 #define isc_buffer_remainingregion isc__buffer_remainingregion
00930 #define isc_buffer_activeregion isc__buffer_activeregion
00931 #define isc_buffer_setactive isc__buffer_setactive
00932 #define isc_buffer_first isc__buffer_first
00933 #define isc_buffer_forward isc__buffer_forward
00934 #define isc_buffer_back isc__buffer_back
00935 #define isc_buffer_putmem isc__buffer_putmem
00936 #define isc_buffer_putstr isc__buffer_putstr
00937 #define isc_buffer_putuint8 isc__buffer_putuint8
00938 #define isc_buffer_putuint16 isc__buffer_putuint16
00939 #define isc_buffer_putuint24 isc__buffer_putuint24
00940 #define isc_buffer_putuint32 isc__buffer_putuint32
00941 #endif
00942
00943 #define isc_buffer_constinit(_b, _d, _l) \
00944 do { \
00945 union { void *_var; const void *_const; } _deconst; \
00946 _deconst._const = (_d); \
00947 isc_buffer_init((_b), _deconst._var, (_l)); \
00948 } while (0)
00949
00950
00951
00952
00953 #define isc_buffer_putuint48 isc__buffer_putuint48
00954
00955 #endif