File: mbuff.c

package info (click to toggle)
libast 0.7-9
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 2,732 kB
  • sloc: ansic: 13,136; sh: 10,332; makefile: 78
file content (662 lines) | stat: -rw-r--r-- 19,768 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
/*
 * Copyright (C) 1997-2004, Michael Jennings
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies of the Software, its documentation and marketing & publicity
 * materials, and acknowledgment shall be given in the documentation, materials
 * and software packages that this Software was used.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

static const char __attribute__((unused)) cvs_ident[] = "$Id: mbuff.c,v 1.1 2004/12/15 00:00:22 mej Exp $";

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <libast_internal.h>

/* *INDENT-OFF* */
static SPIF_CONST_TYPE(mbuffclass) mb_class = {
    {
        SPIF_DECL_CLASSNAME(mbuff),
        (spif_func_t) spif_mbuff_new,
        (spif_func_t) spif_mbuff_init,
        (spif_func_t) spif_mbuff_done,
        (spif_func_t) spif_mbuff_del,
        (spif_func_t) spif_mbuff_show,
        (spif_func_t) spif_mbuff_comp,
        (spif_func_t) spif_mbuff_dup,
        (spif_func_t) spif_mbuff_type
    },
    (spif_func_t) spif_mbuff_new_from_ptr,
    (spif_func_t) spif_mbuff_new_from_buff,
    (spif_func_t) spif_mbuff_new_from_fp,
    (spif_func_t) spif_mbuff_new_from_fd,
    (spif_func_t) spif_mbuff_init_from_ptr,
    (spif_func_t) spif_mbuff_init_from_buff,
    (spif_func_t) spif_mbuff_init_from_fp,
    (spif_func_t) spif_mbuff_init_from_fd,
    (spif_func_t) spif_mbuff_append,
    (spif_func_t) spif_mbuff_append_from_ptr,
    (spif_func_t) spif_mbuff_clear,
    (spif_func_t) spif_mbuff_cmp,
    (spif_func_t) spif_mbuff_cmp_with_ptr,
    (spif_func_t) spif_mbuff_find,
    (spif_func_t) spif_mbuff_find_from_ptr,
    (spif_func_t) spif_mbuff_index,
    (spif_func_t) spif_mbuff_ncmp,
    (spif_func_t) spif_mbuff_ncmp_with_ptr,
    (spif_func_t) spif_mbuff_prepend,
    (spif_func_t) spif_mbuff_prepend_from_ptr,
    (spif_func_t) spif_mbuff_reverse,
    (spif_func_t) spif_mbuff_rindex,
    (spif_func_t) spif_mbuff_splice,
    (spif_func_t) spif_mbuff_splice_from_ptr,
    (spif_func_t) spif_mbuff_subbuff,
    (spif_func_t) spif_mbuff_subbuff_to_ptr,
    (spif_func_t) spif_mbuff_trim
};
SPIF_TYPE(class) SPIF_CLASS_VAR(mbuff) = SPIF_CAST(class) &mb_class;
SPIF_TYPE(mbuffclass) SPIF_MBUFFCLASS_VAR(mbuff) = &mb_class;
/* *INDENT-ON* */

spif_mbuff_t
spif_mbuff_new(void)
{
    spif_mbuff_t self;

    self = SPIF_ALLOC(mbuff);
    if (!spif_mbuff_init(self)) {
        SPIF_DEALLOC(self);
        self = SPIF_NULL_TYPE(mbuff);
    }
    return self;
}

spif_mbuff_t
spif_mbuff_new_from_ptr(spif_byteptr_t old, spif_memidx_t len)
{
    spif_mbuff_t self;

    self = SPIF_ALLOC(mbuff);
    if (!spif_mbuff_init_from_ptr(self, old, len)) {
        SPIF_DEALLOC(self);
        self = SPIF_NULL_TYPE(mbuff);
    }
    return self;
}

spif_mbuff_t
spif_mbuff_new_from_buff(spif_byteptr_t buff, spif_memidx_t len, spif_memidx_t size)
{
    spif_mbuff_t self;

    self = SPIF_ALLOC(mbuff);
    if (!spif_mbuff_init_from_buff(self, buff, len, size)) {
        SPIF_DEALLOC(self);
        self = SPIF_NULL_TYPE(mbuff);
    }
    return self;
}

spif_mbuff_t
spif_mbuff_new_from_fp(FILE * fp)
{
    spif_mbuff_t self;

    self = SPIF_ALLOC(mbuff);
    if (!spif_mbuff_init_from_fp(self, fp)) {
        SPIF_DEALLOC(self);
        self = SPIF_NULL_TYPE(mbuff);
    }
    return self;
}

spif_mbuff_t
spif_mbuff_new_from_fd(int fd)
{
    spif_mbuff_t self;

    self = SPIF_ALLOC(mbuff);
    if (!spif_mbuff_init_from_fd(self, fd)) {
        SPIF_DEALLOC(self);
        self = SPIF_NULL_TYPE(mbuff);
    }
    return self;
}

spif_bool_t
spif_mbuff_init(spif_mbuff_t self)
{
    ASSERT_RVAL(!SPIF_MBUFF_ISNULL(self), FALSE);
    /* ***NOT NEEDED*** spif_obj_init(SPIF_OBJ(self)); */
    spif_obj_set_class(SPIF_OBJ(self), SPIF_CLASS(SPIF_MBUFFCLASS_VAR(mbuff)));
    self->buff = SPIF_NULL_TYPE(byteptr);
    self->len = 0;
    self->size = 0;
    return TRUE;
}

spif_bool_t
spif_mbuff_init_from_ptr(spif_mbuff_t self, spif_byteptr_t old, spif_memidx_t len)
{
    ASSERT_RVAL(!SPIF_MBUFF_ISNULL(self), FALSE);
    REQUIRE_RVAL((old != SPIF_NULL_TYPE(byteptr)), spif_mbuff_init(self));
    /* ***NOT NEEDED*** spif_obj_init(SPIF_OBJ(self)); */
    spif_obj_set_class(SPIF_OBJ(self), SPIF_CLASS(SPIF_MBUFFCLASS_VAR(mbuff)));
    self->len = self->size = len;
    self->buff = SPIF_CAST(byteptr) MALLOC(self->size);
    memcpy(self->buff, old, self->len);
    return TRUE;
}

spif_bool_t
spif_mbuff_init_from_buff(spif_mbuff_t self, spif_byteptr_t buff, spif_memidx_t len, spif_memidx_t size)
{
    ASSERT_RVAL(!SPIF_MBUFF_ISNULL(self), FALSE);
    /* ***NOT NEEDED*** spif_obj_init(SPIF_OBJ(self)); */
    spif_obj_set_class(SPIF_OBJ(self), SPIF_CLASS(SPIF_MBUFFCLASS_VAR(mbuff)));
    if (buff != SPIF_NULL_TYPE(byteptr)) {
        self->len = len;
    } else {
        self->len = 0;
    }
    self->size = MAX(size, self->len);
    self->buff = SPIF_CAST(byteptr) MALLOC(self->size);
    if (buff != SPIF_NULL_TYPE(byteptr)) {
        memcpy(self->buff, buff, self->len);
    }
    return TRUE;
}

spif_bool_t
spif_mbuff_init_from_fp(spif_mbuff_t self, FILE *fp)
{
    spif_byteptr_t p;
    long file_pos;
    spif_memidx_t file_size;

    ASSERT_RVAL(!SPIF_MBUFF_ISNULL(self), FALSE);
    ASSERT_RVAL((fp != SPIF_NULL_TYPE_C(FILE *)), FALSE);
    /* ***NOT NEEDED*** spif_obj_init(SPIF_OBJ(self)); */
    spif_obj_set_class(SPIF_OBJ(self), SPIF_CLASS(SPIF_MBUFFCLASS_VAR(mbuff)));

    file_pos = ftell(fp);
    fseek(fp, 0L, SEEK_END);
    file_size = ftell(fp);
    fseek(fp, file_pos, SEEK_SET);
    LOWER_BOUND(file_size, 0);
    if (file_size == 0) {
        spif_mbuff_init(self);
        return FALSE;
    }
    self->len = self->size = file_size;
    self->buff = SPIF_CAST(byteptr) MALLOC(self->size);

    if (fread(p, file_size, 1, fp) < 1) {
        FREE(self->buff);
        return FALSE;
    }
    return TRUE;
}

spif_bool_t
spif_mbuff_init_from_fd(spif_mbuff_t self, int fd)
{
    spif_byteptr_t p;
    off_t file_pos;
    spif_memidx_t file_size;

    ASSERT_RVAL(!SPIF_MBUFF_ISNULL(self), FALSE);
    ASSERT_RVAL((fd >= 0), FALSE);
    /* ***NOT NEEDED*** spif_obj_init(SPIF_OBJ(self)); */
    spif_obj_set_class(SPIF_OBJ(self), SPIF_CLASS(SPIF_MBUFFCLASS_VAR(mbuff)));

    file_pos = lseek(fd, SPIF_CAST_C(off_t) 0, SEEK_CUR);
    file_size = SPIF_CAST(memidx) lseek(fd, SPIF_CAST_C(off_t) 0, SEEK_END);
    lseek(fd, file_pos, SEEK_SET);
    LOWER_BOUND(file_size, 0);
    if (file_size == 0) {
        spif_mbuff_init(self);
        return FALSE;
    }
    self->len = self->size = file_size;
    self->buff = SPIF_CAST(byteptr) MALLOC(self->size);

    if (read(fd, p, file_size) < 1) {
        FREE(self->buff);
        return FALSE;
    }
    return TRUE;
}

spif_bool_t
spif_mbuff_done(spif_mbuff_t self)
{
    ASSERT_RVAL(!SPIF_MBUFF_ISNULL(self), FALSE);
    if (self->size) {
        FREE(self->buff);
        self->len = 0;
        self->size = 0;
        self->buff = SPIF_NULL_TYPE(byteptr);
    }
    return TRUE;
}

spif_bool_t
spif_mbuff_del(spif_mbuff_t self)
{
    ASSERT_RVAL(!SPIF_MBUFF_ISNULL(self), FALSE);
    spif_mbuff_done(self);
    SPIF_DEALLOC(self);
    return TRUE;
}

spif_str_t
spif_mbuff_show(spif_mbuff_t self, spif_byteptr_t name, spif_str_t buff, size_t indent)
{
    spif_char_t tmp[4096];
    spif_memidx_t j;

    if (SPIF_MBUFF_ISNULL(self)) {
        SPIF_OBJ_SHOW_NULL(mbuff, name, buff, indent, tmp);
        return buff;
    }

    memset(tmp, ' ', indent);
    snprintf(SPIF_CHARPTR_C(tmp) + indent, sizeof(tmp) - indent,
             "(spif_mbuff_t) %s:  %10p (length %lu, size %lu) {\n",
             name, SPIF_CAST(ptr) self, SPIF_CAST(ulong) self->len, SPIF_CAST(ulong) self->size);
    if (SPIF_MBUFF_ISNULL(buff)) {
        buff = spif_str_new_from_ptr(tmp);
    } else {
        spif_str_append_from_ptr(buff, tmp);
    }

    memset(tmp, ' ', indent + 2);
    for (j = 0; j < self->len; j += 8) {
        spif_memidx_t k, l, len;

        snprintf(SPIF_CHARPTR_C(tmp) + indent + 2, sizeof(tmp) - indent - 2, "%0lx    ", SPIF_CAST(long) j);
        len = strlen(SPIF_CHARPTR_C(tmp));
        if (len + ((8 * 3) + 3 + 8 + 1) < sizeof(tmp)) {
            l = ((self->len - j < 8) ? (self->len - j) : (8));
            for (k = 0; k < l; k++) {
                sprintf(SPIF_CHARPTR_C(tmp) + len, "%02x ", self->buff[j + k]);
            }
            for (; k < 8; k++) {
                strcat(SPIF_CHARPTR_C(tmp) + len, "   ");
            }
            sprintf(SPIF_CHARPTR_C(tmp) + len, "%-8s\n", spiftool_safe_str(SPIF_CAST(charptr) (self->buff + j), l));
        }
        spif_str_append_from_ptr(buff, tmp);
    }

    snprintf(SPIF_CHARPTR_C(tmp) + indent, sizeof(tmp) - indent, "}\n");
    spif_str_append_from_ptr(buff, tmp);
    return buff;
}

spif_cmp_t
spif_mbuff_comp(spif_mbuff_t self, spif_mbuff_t other)
{
    return spif_mbuff_cmp(self, other);
}

spif_mbuff_t
spif_mbuff_dup(spif_mbuff_t self)
{
    spif_mbuff_t tmp;

    ASSERT_RVAL(!SPIF_MBUFF_ISNULL(self), SPIF_NULL_TYPE(mbuff));
    tmp = SPIF_ALLOC(mbuff);
    memcpy(tmp, self, SPIF_SIZEOF_TYPE(mbuff));
    tmp->buff = SPIF_CAST(byteptr) MALLOC(self->size);
    memcpy(tmp->buff, self->buff, self->size);
    tmp->len = self->len;
    tmp->size = self->size;
    return tmp;
}

spif_classname_t
spif_mbuff_type(spif_mbuff_t self)
{
    ASSERT_RVAL(!SPIF_MBUFF_ISNULL(self), SPIF_CAST(classname) SPIF_NULLSTR_TYPE(classname));
    return SPIF_OBJ_CLASSNAME(self);
}

spif_bool_t
spif_mbuff_append(spif_mbuff_t self, spif_mbuff_t other)
{
    ASSERT_RVAL(!SPIF_MBUFF_ISNULL(self), FALSE);
    REQUIRE_RVAL(!SPIF_MBUFF_ISNULL(other), FALSE);
    if (other->size && other->len) {
        self->size += other->size;
        self->buff = SPIF_CAST(byteptr) REALLOC(self->buff, self->size);
        memcpy(self->buff + self->len, SPIF_MBUFF_BUFF(other), other->len);
        self->len += other->len;
    }
    return TRUE;
}

spif_bool_t
spif_mbuff_append_from_ptr(spif_mbuff_t self, spif_byteptr_t other, spif_memidx_t len)
{
    ASSERT_RVAL(!SPIF_MBUFF_ISNULL(self), FALSE);
    REQUIRE_RVAL((other != SPIF_NULL_TYPE(byteptr)), FALSE);
    if (len) {
        self->size += len;
        self->buff = SPIF_CAST(byteptr) REALLOC(self->buff, self->size);
        memcpy(self->buff + self->len, other, len);
        self->len += len;
    }
    return TRUE;
}

spif_bool_t
spif_mbuff_clear(spif_mbuff_t self, spif_uint8_t c)
{
    ASSERT_RVAL(!SPIF_MBUFF_ISNULL(self), FALSE);
    memset(self->buff, c, self->len);
    return TRUE;
}

spif_cmp_t
spif_mbuff_cmp(spif_mbuff_t self, spif_mbuff_t other)
{
    int c;

    SPIF_OBJ_COMP_CHECK_NULL(self, other);
    c = memcmp(SPIF_MBUFF_BUFF(self), SPIF_MBUFF_BUFF(other), MIN(self->len, other->len));
    return SPIF_CMP_FROM_INT(c);
}

spif_cmp_t
spif_mbuff_cmp_with_ptr(spif_mbuff_t self, spif_byteptr_t other, spif_memidx_t len)
{
    int c;

    SPIF_OBJ_COMP_CHECK_NULL(self, other);
    c = memcmp(SPIF_MBUFF_BUFF(self), other, len);
    return SPIF_CMP_FROM_INT(c);
}

spif_memidx_t
spif_mbuff_find(spif_mbuff_t self, spif_mbuff_t other)
{
    spif_byteptr_t tmp;

    ASSERT_RVAL(!SPIF_MBUFF_ISNULL(self), (SPIF_CAST(memidx) -1));
    REQUIRE_RVAL(!SPIF_MBUFF_ISNULL(other), (SPIF_CAST(memidx) -1));
    tmp = SPIF_CAST(byteptr) memmem(SPIF_MBUFF_BUFF(self), self->len, SPIF_MBUFF_BUFF(other), other->len);
    if (tmp) {
        return SPIF_CAST(memidx) (SPIF_CAST(long) tmp - SPIF_CAST(long) (SPIF_MBUFF_BUFF(self)));
    } else {
        return SPIF_CAST(memidx) (self->len);
    }
}

spif_memidx_t
spif_mbuff_find_from_ptr(spif_mbuff_t self, spif_byteptr_t other, spif_memidx_t len)
{
    spif_byteptr_t tmp;

    ASSERT_RVAL(!SPIF_MBUFF_ISNULL(self), (SPIF_CAST(memidx) -1));
    REQUIRE_RVAL((other != SPIF_NULL_TYPE(byteptr)), (SPIF_CAST(memidx) -1));
    tmp = SPIF_CAST(byteptr) memmem(SPIF_MBUFF_BUFF(self), self->len, other, len);
    if (tmp) {
        return SPIF_CAST(memidx) (SPIF_CAST(long) tmp - SPIF_CAST(long) (SPIF_MBUFF_BUFF(self)));
    } else {
        return SPIF_CAST(memidx) (self->len);
    }
}

spif_memidx_t
spif_mbuff_index(spif_mbuff_t self, spif_uint8_t c)
{
    spif_byteptr_t tmp;
    spif_memidx_t i;

    ASSERT_RVAL(!SPIF_MBUFF_ISNULL(self), (SPIF_CAST(memidx) -1));
    for (tmp = self->buff, i = 0; (*tmp & (~c)) && (i < self->len); i++, tmp++);
    return SPIF_CAST(memidx) (SPIF_CAST(long) tmp - SPIF_CAST(long) self->buff);
}

spif_cmp_t
spif_mbuff_ncmp(spif_mbuff_t self, spif_mbuff_t other, spif_memidx_t cnt)
{
    int c;

    SPIF_OBJ_COMP_CHECK_NULL(self, other);
    if (cnt > self->len || cnt > other->len) {
        cnt = MIN(self->len, other->len);
    }
    c = memcmp(SPIF_MBUFF_BUFF(self), SPIF_MBUFF_BUFF(other), cnt);
    return SPIF_CMP_FROM_INT(c);
}

spif_cmp_t
spif_mbuff_ncmp_with_ptr(spif_mbuff_t self, spif_byteptr_t other, spif_memidx_t cnt)
{
    return spif_mbuff_cmp_with_ptr(self, other, cnt);
}

spif_bool_t
spif_mbuff_prepend(spif_mbuff_t self, spif_mbuff_t other)
{
    ASSERT_RVAL(!SPIF_MBUFF_ISNULL(self), FALSE);
    REQUIRE_RVAL(!SPIF_MBUFF_ISNULL(other), FALSE);
    if (other->size && other->len) {
        self->size += other->size;
        self->buff = SPIF_CAST(byteptr) REALLOC(self->buff, self->size);
        memmove(self->buff + other->len, self->buff, self->len);
        memcpy(self->buff, SPIF_MBUFF_BUFF(other), other->len);
        self->len += other->len;
    }
    return TRUE;
}

spif_bool_t
spif_mbuff_prepend_from_ptr(spif_mbuff_t self, spif_byteptr_t other, spif_memidx_t len)
{
    ASSERT_RVAL(!SPIF_MBUFF_ISNULL(self), FALSE);
    REQUIRE_RVAL((other != SPIF_NULL_TYPE(byteptr)), FALSE);
    if (len) {
        self->size += len;
        self->buff = SPIF_CAST(byteptr) REALLOC(self->buff, self->size);
        memmove(self->buff + len, self->buff, self->len);
        memcpy(self->buff, other, len);
        self->len += len;
    }
    return TRUE;
}

spif_bool_t
spif_mbuff_reverse(spif_mbuff_t self)
{
    spif_byteptr_t tmp = self->buff;
    int i, j;

    ASSERT_RVAL(!SPIF_MBUFF_ISNULL(self), FALSE);
    REQUIRE_RVAL(self->buff != SPIF_NULL_TYPE(byteptr), FALSE);

    for (j = 0, i = self->len - 1; i > j; i--, j++) {
        SWAP(tmp[j], tmp[i]);
    }
    return TRUE;
}

spif_memidx_t
spif_mbuff_rindex(spif_mbuff_t self, spif_uint8_t c)
{
    spif_byteptr_t tmp;
    spif_memidx_t i;

    ASSERT_RVAL(!SPIF_MBUFF_ISNULL(self), (SPIF_CAST(memidx) -1));
    for (tmp = self->buff + self->len, i = 0; (*tmp & (~c)) && (i < self->len); i++, tmp--);

    if ((tmp == self->buff) && (*tmp != c)) {
        return SPIF_CAST(memidx) (self->len);
    } else {
        return SPIF_CAST(memidx) (SPIF_CAST(long) tmp - SPIF_CAST(long) self->buff);
    }
}

spif_bool_t
spif_mbuff_splice(spif_mbuff_t self, spif_memidx_t idx, spif_memidx_t cnt, spif_mbuff_t other)
{
    spif_byteptr_t tmp, ptmp;
    spif_memidx_t newsize;

    ASSERT_RVAL(!SPIF_MBUFF_ISNULL(self), FALSE);
    if (idx < 0) {
        idx = self->len + idx;
    }
    REQUIRE_RVAL(idx >= 0, FALSE);
    REQUIRE_RVAL(idx < self->len, FALSE);
    if (cnt < 0) {
        cnt = idx + self->len + cnt;
    }
    REQUIRE_RVAL(cnt >= 0, FALSE);
    REQUIRE_RVAL(cnt <= (self->len - idx), FALSE);

    newsize = self->len + ((SPIF_MBUFF_ISNULL(other)) ? (0) : (other->len)) - cnt + 1;
    ptmp = tmp = SPIF_CAST(byteptr) MALLOC(newsize);
    if (idx > 0) {
        memcpy(tmp, self->buff, idx);
        ptmp += idx;
    }
    if (!SPIF_MBUFF_ISNULL(other)) {
        memcpy(ptmp, other->buff, other->len);
        ptmp += other->len;
    }
    memcpy(ptmp, self->buff + idx + cnt, self->len - idx - cnt + 1);
    if (self->size < newsize) {
        self->buff = SPIF_CAST(byteptr) REALLOC(self->buff, newsize);
        self->size = newsize;
    }
    self->len = newsize;
    memcpy(self->buff, tmp, newsize);
    FREE(tmp);
    return TRUE;
}

spif_bool_t
spif_mbuff_splice_from_ptr(spif_mbuff_t self, spif_memidx_t idx, spif_memidx_t cnt, spif_byteptr_t other, spif_memidx_t len)
{
    spif_byteptr_t tmp, ptmp;
    spif_memidx_t newsize;

    ASSERT_RVAL(!SPIF_MBUFF_ISNULL(self), FALSE);
    if (SPIF_PTR_ISNULL(other)) {
        len = 0;
    }
    if (idx < 0) {
        idx = self->len + idx;
    }
    REQUIRE_RVAL(idx >= 0, FALSE);
    REQUIRE_RVAL(idx < self->len, FALSE);
    if (cnt < 0) {
        cnt = idx + self->len + cnt;
    }
    REQUIRE_RVAL(cnt >= 0, FALSE);
    REQUIRE_RVAL(cnt <= (self->len - idx), FALSE);

    newsize = self->len + len - cnt;
    ptmp = tmp = SPIF_CAST(byteptr) MALLOC(newsize);
    if (idx > 0) {
        memcpy(tmp, self->buff, idx);
        ptmp += idx;
    }
    if (len) {
        memcpy(ptmp, other, len);
        ptmp += len;
    }
    memcpy(ptmp, self->buff + idx + cnt, self->len - idx - cnt);
    if (self->size < newsize) {
        self->buff = SPIF_CAST(byteptr) REALLOC(self->buff, newsize);
        self->size = newsize;
    }
    self->len = newsize;
    memcpy(self->buff, tmp, newsize);
    FREE(tmp);
    return TRUE;
}

spif_mbuff_t
spif_mbuff_subbuff(spif_mbuff_t self, spif_memidx_t idx, spif_memidx_t cnt)
{
    ASSERT_RVAL(!SPIF_MBUFF_ISNULL(self), SPIF_NULL_TYPE(mbuff));
    if (idx < 0) {
        idx = self->len + idx;
    }
    REQUIRE_RVAL(idx >= 0, SPIF_NULL_TYPE(mbuff));
    REQUIRE_RVAL(idx < self->len, SPIF_NULL_TYPE(mbuff));
    if (cnt <= 0) {
        cnt = self->len - idx + cnt;
    }
    REQUIRE_RVAL(cnt >= 0, SPIF_NULL_TYPE(mbuff));
    UPPER_BOUND(cnt, self->len - idx);
    return spif_mbuff_new_from_buff(self->buff + idx, cnt, cnt);
}

spif_byteptr_t
spif_mbuff_subbuff_to_ptr(spif_mbuff_t self, spif_memidx_t idx, spif_memidx_t cnt)
{
    spif_byteptr_t newmbuff;

    ASSERT_RVAL(!SPIF_MBUFF_ISNULL(self), SPIF_NULL_TYPE(byteptr));
    if (idx < 0) {
        idx = self->len + idx;
    }
    REQUIRE_RVAL(idx >= 0, SPIF_NULL_TYPE(byteptr));
    REQUIRE_RVAL(idx < self->len, SPIF_NULL_TYPE(byteptr));
    if (cnt <= 0) {
        cnt = self->len - idx + cnt;
    }
    REQUIRE_RVAL(cnt >= 0, SPIF_NULL_TYPE(byteptr));
    UPPER_BOUND(cnt, self->len - idx);

    newmbuff = SPIF_CAST(byteptr) MALLOC(cnt + 1);
    memcpy(newmbuff, SPIF_MBUFF_BUFF(self) + idx, cnt);
    newmbuff[cnt] = 0;
    return newmbuff;
}

spif_bool_t
spif_mbuff_trim(spif_mbuff_t self)
{
    spif_byteptr_t start, end;

    ASSERT_RVAL(!SPIF_MBUFF_ISNULL(self), FALSE);
    start = self->buff;
    end = self->buff + self->len - 1;
    for (; isspace((spif_uchar_t) (*start)) && (start < end); start++);
    for (; isspace((spif_uchar_t) (*end)) && (start < end); end--);
    if (start > end) {
        return spif_mbuff_done(self);
    }
    *(++end) = 0;
    self->len = (spif_memidx_t) (end - start);
    self->size = self->len + 1;
    memmove(self->buff, start, self->size);
    self->buff = SPIF_CAST(byteptr) REALLOC(self->buff, self->size);
    return TRUE;
}

SPIF_DEFINE_PROPERTY_FUNC_C(mbuff, spif_memidx_t, size)
SPIF_DEFINE_PROPERTY_FUNC_C(mbuff, spif_memidx_t, len)