summaryrefslogtreecommitdiff
path: root/src/libio/mpu-cookie.c
blob: 9a41bf61e0ba6e332e42235ed901eae96f2eb8ea (plain)
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
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
/***************************************************************
  MPU-COOKIE.C

       This file contains source code of functions for
       user-defined cookie stream operations.

       PART OF : MPUIO - library .

       USAGE   : Internal and external .

       NOTE    : Cookie callbacks transfer external raw bytes.
                 Text I/O uses UTF-8 at the cookie boundary.

       Copyright (C) 2000 - 2026  by Andrey V.Kosteltsev.
       All Rights Reserved.
 ***************************************************************/

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

#include <libmpuio.h>
#include <mpu-symbols.h>
#include <mpu-libio.h>


/***************************************************************
  Return the private callback descriptor attached to STREAM.
 ***************************************************************/
static __mpu_IO_cookie_file *__mpu_IO_cookie_data( mpu_FILE *stream )
{
  return( (__mpu_IO_cookie_file *)stream->_cookie );

} /* End of __mpu_IO_cookie_data() */


/***************************************************************
  Invoke the cookie read callback, retrying EINTR reports.
 ***************************************************************/
static ssize_t __mpu_IO_cookie_read( mpu_FILE *stream, void *buf,
                                     size_t size )
{
  __mpu_IO_cookie_file *cf = __mpu_IO_cookie_data( stream );
  ssize_t               ret;

  if( cf == (__mpu_IO_cookie_file *)0 ||
      cf->io.read == (mpu_cookie_read_function_t *)0 )
  {
    errno = EBADF;
    return( -1 );
  }

  do
  {
    ret = (*cf->io.read)( cf->user_cookie, (char *)buf, size );
  }
  while( ret < 0 && errno == EINTR );

  if( ret > 0 && (size_t)ret > size )
  {
    errno = EIO;
    return( -1 );
  }

  return( ret );

} /* End of __mpu_IO_cookie_read() */


/***************************************************************
  Invoke the cookie write callback until SIZE bytes are written.
 ***************************************************************/
static size_t __mpu_IO_cookie_write_bytes( mpu_FILE *stream,
                                           const unsigned char *buf,
                                           size_t size, int *complete )
{
  __mpu_IO_cookie_file *cf = __mpu_IO_cookie_data( stream );
  size_t                done = 0;

  *complete = 0;

  if( cf == (__mpu_IO_cookie_file *)0 ||
      cf->io.write == (mpu_cookie_write_function_t *)0 )
  {
    errno = EBADF;
    stream->_flags |= __MPU_IO_ERR_SEEN;
    return( 0 );
  }

  while( done < size )
  {
    ssize_t  n;

    do
    {
      n = (*cf->io.write)( cf->user_cookie,
                           (const char *)buf + done, size - done );
    }
    while( n < 0 && errno == EINTR );

    if( n < 0 )
    {
      stream->_flags |= __MPU_IO_ERR_SEEN;
      return( done );
    }

    if( n == 0 || (size_t)n > size - done )
    {
      errno = EIO;
      stream->_flags |= __MPU_IO_ERR_SEEN;
      return( done );
    }

    done += (size_t)n;
  }

  *complete = 1;
  return( done );

} /* End of __mpu_IO_cookie_write_bytes() */


/***************************************************************
  Reposition the underlying cookie by invoking its seek callback.
 ***************************************************************/
static off_t __mpu_IO_cookie_call_seek( mpu_FILE *stream, off_t offset,
                                        int whence )
{
  __mpu_IO_cookie_file *cf  = __mpu_IO_cookie_data( stream );
  off_t                 pos = offset;

  if( cf == (__mpu_IO_cookie_file *)0 ||
      cf->io.seek == (mpu_cookie_seek_function_t *)0 )
  {
    errno = ESPIPE;
    return( (off_t)-1 );
  }

  if( (*cf->io.seek)( cf->user_cookie, &pos, whence ) != 0 )
    return( (off_t)-1 );

  return( pos );

} /* End of __mpu_IO_cookie_call_seek() */


/***************************************************************
  Synchronize unread buffered bytes with a seekable cookie.
 ***************************************************************/
static int __mpu_IO_cookie_sync_input_unlocked( mpu_FILE *stream )
{
  off_t  unread = 0;

  if( !(stream->_flags & __MPU_IO_READING) )
    return( 0 );

  if( stream->_read_ptr != (unsigned char *)0 &&
      stream->_read_end != (unsigned char *)0 )
    unread = (off_t)(stream->_read_end - stream->_read_ptr);

  if( unread != 0 &&
      __mpu_IO_cookie_call_seek( stream, -unread, MPU_SEEK_CUR ) == (off_t)-1 )
  {
    stream->_flags |= __MPU_IO_ERR_SEEN;
    return( mpu_EOF );
  }

  stream->_read_base = stream->_buf_base;
  stream->_read_ptr  = stream->_buf_base;
  stream->_read_end  = stream->_buf_base;

  return( 0 );

} /* End of __mpu_IO_cookie_sync_input_unlocked() */


/***************************************************************
  Flush buffered cookie output or synchronize buffered input.
 ***************************************************************/
static int __mpu_IO_cookie_flush_unlocked( mpu_FILE *stream )
{
  size_t  size;

  if( stream->_flags & __MPU_IO_WRITING )
  {
    size = 0;
    if( stream->_write_base != (unsigned char *)0 &&
        stream->_write_ptr != (unsigned char *)0 )
      size = (size_t)(stream->_write_ptr - stream->_write_base);

    if( size != 0 )
    {
      size_t  done;
      int     complete;

      done = __mpu_IO_cookie_write_bytes( stream, stream->_write_base,
                                          size, &complete );
      if( !complete )
      {
        if( done != 0 && done < size )
          memmove( stream->_write_base, stream->_write_base + done, size - done );

        stream->_write_ptr = stream->_write_base + (size - done);
        stream->_write_end = stream->_buf_end;
        return( mpu_EOF );
      }
    }

    stream->_write_base = stream->_buf_base;
    stream->_write_ptr  = stream->_buf_base;
    stream->_write_end  = stream->_buf_end;
  }
  else if( stream->_flags & __MPU_IO_READING )
  {
    if( __mpu_IO_cookie_sync_input_unlocked( stream ) == mpu_EOF )
      return( mpu_EOF );
  }

  return( 0 );

} /* End of __mpu_IO_cookie_flush_unlocked() */


/***************************************************************
  Switch a cookie stream to input mode.
 ***************************************************************/
static int __mpu_IO_cookie_switch_to_read( mpu_FILE *stream )
{
  if( stream->_flags & __MPU_IO_NO_READS )
  {
    errno = EBADF;
    stream->_flags |= __MPU_IO_ERR_SEEN;
    return( mpu_EOF );
  }

  if( stream->_flags & __MPU_IO_WRITING )
  {
    if( __mpu_IO_cookie_flush_unlocked( stream ) == mpu_EOF )
      return( mpu_EOF );
    stream->_flags &= ~__MPU_IO_WRITING;
  }

  if( __mpu_IO_doallocbuf( stream ) == mpu_EOF )
    return( mpu_EOF );

  if( !(stream->_flags & __MPU_IO_READING) )
  {
    stream->_flags |= __MPU_IO_READING;
    stream->_read_base = stream->_buf_base;
    stream->_read_ptr  = stream->_buf_base;
    stream->_read_end  = stream->_buf_base;
  }

  return( 0 );

} /* End of __mpu_IO_cookie_switch_to_read() */


/***************************************************************
  Switch a cookie stream to output mode.
 ***************************************************************/
static int __mpu_IO_cookie_switch_to_write( mpu_FILE *stream )
{
  if( stream->_flags & __MPU_IO_NO_WRITES )
  {
    errno = EBADF;
    stream->_flags |= __MPU_IO_ERR_SEEN;
    return( mpu_EOF );
  }

  if( stream->_flags & __MPU_IO_READING )
  {
    if( __mpu_IO_cookie_sync_input_unlocked( stream ) == mpu_EOF )
      return( mpu_EOF );
    stream->_flags &= ~__MPU_IO_READING;
  }

  if( __mpu_IO_doallocbuf( stream ) == mpu_EOF )
    return( mpu_EOF );

  if( !(stream->_flags & __MPU_IO_WRITING) )
  {
    if( (stream->_flags & __MPU_IO_APPEND) &&
        __mpu_IO_cookie_call_seek( stream, (off_t)0,
                                   MPU_SEEK_END ) == (off_t)-1 )
    {
      stream->_flags |= __MPU_IO_ERR_SEEN;
      return( mpu_EOF );
    }

    stream->_flags |= __MPU_IO_WRITING;
    stream->_write_base = stream->_buf_base;
    stream->_write_ptr  = stream->_buf_base;
    stream->_write_end  = stream->_buf_end;
  }

  return( 0 );

} /* End of __mpu_IO_cookie_switch_to_write() */


/***************************************************************
  Refill a cookie input buffer and return the next byte.
 ***************************************************************/
static int __mpu_IO_cookie_underflow_unlocked( mpu_FILE *stream )
{
  ssize_t  n;

  if( __mpu_IO_cookie_switch_to_read( stream ) == mpu_EOF )
    return( mpu_EOF );

  if( stream->_read_ptr < stream->_read_end )
    return( (int)*stream->_read_ptr );

  n = __mpu_IO_cookie_read( stream, stream->_buf_base,
                            (size_t)(stream->_buf_end - stream->_buf_base) );
  if( n < 0 )
  {
    stream->_flags |= __MPU_IO_ERR_SEEN;
    return( mpu_EOF );
  }

  if( n == 0 )
  {
    stream->_flags |= __MPU_IO_EOF_SEEN;
    return( mpu_EOF );
  }

  stream->_flags &= ~__MPU_IO_EOF_SEEN;
  stream->_read_base = stream->_buf_base;
  stream->_read_ptr  = stream->_buf_base;
  stream->_read_end  = stream->_buf_base + n;

  return( (int)*stream->_read_ptr );

} /* End of __mpu_IO_cookie_underflow_unlocked() */


/***************************************************************
  Flush cookie output and optionally append one byte.
 ***************************************************************/
static int __mpu_IO_cookie_overflow_unlocked( mpu_FILE *stream, int c )
{
  unsigned char  ch;

  if( __mpu_IO_cookie_switch_to_write( stream ) == mpu_EOF )
    return( mpu_EOF );

  if( stream->_write_ptr != stream->_write_base &&
      __mpu_IO_cookie_flush_unlocked( stream ) == mpu_EOF )
    return( mpu_EOF );

  if( c == mpu_EOF )
    return( 0 );

  ch = (unsigned char)c;

  if( stream->_flags & __MPU_IO_UNBUFFERED )
  {
    int  complete;

    return( __mpu_IO_cookie_write_bytes( stream, &ch, 1, &complete ) == 1 &&
            complete ? c : mpu_EOF );
  }

  *stream->_write_ptr++ = ch;

  if( (stream->_flags & __MPU_IO_LINE_BUF) && ch == '\n' &&
      __mpu_IO_cookie_flush_unlocked( stream ) == mpu_EOF )
    return( mpu_EOF );

  return( c );

} /* End of __mpu_IO_cookie_overflow_unlocked() */


/***************************************************************
  Synchronize buffered cookie state.
 ***************************************************************/
static int __mpu_IO_cookie_sync_unlocked( mpu_FILE *stream )
{
  return( __mpu_IO_cookie_flush_unlocked( stream ) );

} /* End of __mpu_IO_cookie_sync_unlocked() */


/***************************************************************
  Read one raw byte from a cookie stream.
 ***************************************************************/
static int __mpu_IO_cookie_get_byte_unlocked( mpu_FILE *stream )
{
  int  c;

  if( __mpu_IO_cookie_switch_to_read( stream ) == mpu_EOF )
    return( mpu_EOF );

  if( stream->_read_ptr >= stream->_read_end )
  {
    c = __mpu_IO_cookie_underflow_unlocked( stream );
    if( c == mpu_EOF ) return( mpu_EOF );
  }

  return( (int)*stream->_read_ptr++ );

} /* End of __mpu_IO_cookie_get_byte_unlocked() */


/***************************************************************
  Write one raw byte to a cookie stream.
 ***************************************************************/
static int __mpu_IO_cookie_put_byte_unlocked( unsigned char c,
                                              mpu_FILE *stream )
{
  if( __mpu_IO_cookie_switch_to_write( stream ) == mpu_EOF )
    return( mpu_EOF );

  if( stream->_flags & __MPU_IO_UNBUFFERED )
    return( __mpu_IO_cookie_overflow_unlocked( stream, (int)c ) );

  if( stream->_write_ptr == stream->_write_end &&
      __mpu_IO_cookie_overflow_unlocked( stream, mpu_EOF ) == mpu_EOF )
    return( mpu_EOF );

  *stream->_write_ptr++ = c;

  if( (stream->_flags & __MPU_IO_LINE_BUF) && c == '\n' &&
      __mpu_IO_cookie_overflow_unlocked( stream, mpu_EOF ) == mpu_EOF )
    return( mpu_EOF );

  return( (int)c );

} /* End of __mpu_IO_cookie_put_byte_unlocked() */


/***************************************************************
  Read SIZE raw bytes through a cookie callback.
 ***************************************************************/
static size_t __mpu_IO_cookie_xsgetn_unlocked( mpu_FILE *stream,
                                               void *data, size_t size )
{
  unsigned char  *p    = (unsigned char *)data;
  size_t          done = 0;

  if( __mpu_IO_cookie_switch_to_read( stream ) == mpu_EOF )
    return( 0 );

  while( done < size )
  {
    size_t  avail = (size_t)(stream->_read_end - stream->_read_ptr);
    size_t  want;

    if( avail != 0 )
    {
      want = size - done;
      if( want > avail ) want = avail;
      memcpy( p + done, stream->_read_ptr, want );
      stream->_read_ptr += want;
      done += want;
      continue;
    }

    want = size - done;
    if( want >= (size_t)(stream->_buf_end - stream->_buf_base) )
    {
      ssize_t  n = __mpu_IO_cookie_read( stream, p + done, want );

      if( n < 0 )
      {
        stream->_flags |= __MPU_IO_ERR_SEEN;
        break;
      }
      if( n == 0 )
      {
        stream->_flags |= __MPU_IO_EOF_SEEN;
        break;
      }
      done += (size_t)n;
      continue;
    }

    {
      ssize_t  n = __mpu_IO_cookie_read( stream, stream->_buf_base,
                         (size_t)(stream->_buf_end - stream->_buf_base) );

      if( n < 0 )
      {
        stream->_flags |= __MPU_IO_ERR_SEEN;
        break;
      }
      if( n == 0 )
      {
        stream->_flags |= __MPU_IO_EOF_SEEN;
        break;
      }
      stream->_read_base = stream->_buf_base;
      stream->_read_ptr  = stream->_buf_base;
      stream->_read_end  = stream->_buf_base + n;
    }
  }

  return( done );

} /* End of __mpu_IO_cookie_xsgetn_unlocked() */


/***************************************************************
  Write SIZE raw bytes through a cookie callback.
 ***************************************************************/
static size_t __mpu_IO_cookie_xsputn_unlocked( mpu_FILE *stream,
                                               const void *data, size_t size )
{
  const unsigned char  *p    = (const unsigned char *)data;
  size_t                done = 0;

  if( __mpu_IO_cookie_switch_to_write( stream ) == mpu_EOF )
    return( 0 );

  if( stream->_flags & (__MPU_IO_UNBUFFERED | __MPU_IO_LINE_BUF) )
  {
    while( done < size )
    {
      if( __mpu_IO_cookie_put_byte_unlocked( p[done], stream ) == mpu_EOF )
        break;
      ++done;
    }
    return( done );
  }

  while( done < size )
  {
    size_t  avail = (size_t)(stream->_write_end - stream->_write_ptr);
    size_t  want;

    if( avail != 0 )
    {
      want = size - done;
      if( want > avail ) want = avail;
      memcpy( stream->_write_ptr, p + done, want );
      stream->_write_ptr += want;
      done += want;
      if( done == size ) break;
    }

    if( stream->_write_ptr == stream->_write_end &&
        __mpu_IO_cookie_flush_unlocked( stream ) == mpu_EOF )
      break;

    want = size - done;
    if( want >= (size_t)(stream->_buf_end - stream->_buf_base) )
    {
      size_t  n;
      int     complete;

      n = __mpu_IO_cookie_write_bytes( stream, p + done, want, &complete );
      done += n;
      if( !complete ) break;
    }
  }

  return( done );

} /* End of __mpu_IO_cookie_xsputn_unlocked() */


/***************************************************************
  Finish a cookie stream.  Output is flushed; buffered input is
  discarded because close must work for non-seekable cookies.
 ***************************************************************/
static int __mpu_IO_cookie_finish_unlocked( mpu_FILE *stream )
{
  if( stream->_flags & __MPU_IO_WRITING )
    return( __mpu_IO_cookie_flush_unlocked( stream ) );

  stream->_read_base = stream->_buf_base;
  stream->_read_ptr  = stream->_buf_base;
  stream->_read_end  = stream->_buf_base;
  stream->_unget_count = 0;

  return( 0 );

} /* End of __mpu_IO_cookie_finish_unlocked() */


/***************************************************************
  Configure buffering for a cookie stream.
 ***************************************************************/
static int __mpu_IO_cookie_setbuf_unlocked( mpu_FILE *stream,
                                            unsigned char *buf,
                                            int mode, size_t size )
{
  unsigned char  *newbuf = buf;
  unsigned char  *oldbuf;
  int             newbuf_owned = 0;

  if( mode != MPU_IOFBF && mode != MPU_IOLBF && mode != MPU_IONBF )
  {
    errno = EINVAL;
    return( -1 );
  }
  if( mode != MPU_IONBF && size == 0 )
  {
    errno = EINVAL;
    return( -1 );
  }

  /* Allocate replacement storage before touching the current cookie
     buffering state.  A failed allocation leaves that state usable. */
  if( mode == MPU_IONBF )
  {
    size = 1;
    newbuf = (unsigned char *)malloc( 1 );
    if( newbuf == (unsigned char *)0 )
    {
      stream->_flags |= __MPU_IO_ERR_SEEN;
      return( -1 );
    }
    newbuf_owned = 1;
  }
  else if( newbuf == (unsigned char *)0 )
  {
    newbuf = (unsigned char *)malloc( size );
    if( newbuf == (unsigned char *)0 )
    {
      stream->_flags |= __MPU_IO_ERR_SEEN;
      return( -1 );
    }
    newbuf_owned = 1;
  }

  if( __mpu_IO_cookie_sync_unlocked( stream ) == mpu_EOF )
  {
    if( newbuf_owned )
      free( newbuf );
    return( -1 );
  }

  oldbuf = stream->_buf_base;
  if( oldbuf != (unsigned char *)0 &&
      !(stream->_flags & __MPU_IO_USER_BUF) )
    free( oldbuf );

  stream->_buf_base = newbuf;
  stream->_buf_end = newbuf + size;
  stream->_read_base = (unsigned char *)0;
  stream->_read_ptr = (unsigned char *)0;
  stream->_read_end = (unsigned char *)0;
  stream->_write_base = (unsigned char *)0;
  stream->_write_ptr = (unsigned char *)0;
  stream->_write_end = (unsigned char *)0;

  stream->_flags &= ~(__MPU_IO_USER_BUF | __MPU_IO_LINE_BUF |
                      __MPU_IO_UNBUFFERED | __MPU_IO_READING |
                      __MPU_IO_WRITING);

  if( mode == MPU_IONBF )
    stream->_flags |= __MPU_IO_UNBUFFERED;
  else if( !newbuf_owned )
    stream->_flags |= __MPU_IO_USER_BUF;

  if( mode == MPU_IOLBF )
    stream->_flags |= __MPU_IO_LINE_BUF;

  return( 0 );

} /* End of __mpu_IO_cookie_setbuf_unlocked() */


/***************************************************************
  Seek a cookie stream and reset all buffered state.
 ***************************************************************/
static int __mpu_IO_cookie_seek_unlocked( mpu_FILE *stream,
                                          off_t offset, int whence )
{
  off_t  pos;

  if( stream->_flags & __MPU_IO_WRITING )
  {
    if( __mpu_IO_cookie_flush_unlocked( stream ) == mpu_EOF )
      return( -1 );
  }
  else if( stream->_flags & __MPU_IO_READING )
  {
    /* Discard buffered input: the requested offset is interpreted from
       the logical position by seekoff() when WHENCE is SEEK_CUR. */
    stream->_read_base = stream->_buf_base;
    stream->_read_ptr  = stream->_buf_base;
    stream->_read_end  = stream->_buf_base;
  }

  pos = __mpu_IO_cookie_call_seek( stream, offset, whence );
  if( pos == (off_t)-1 )
  {
    stream->_flags |= __MPU_IO_ERR_SEEN;
    return( -1 );
  }

  stream->_unget_count = 0;
  stream->_flags &= ~(__MPU_IO_READING | __MPU_IO_WRITING |
                      __MPU_IO_EOF_SEEN);
  stream->_read_base = stream->_buf_base;
  stream->_read_ptr = stream->_buf_base;
  stream->_read_end = stream->_buf_base;
  stream->_write_base = stream->_buf_base;
  stream->_write_ptr = stream->_buf_base;
  stream->_write_end = stream->_buf_end;

  return( 0 );

} /* End of __mpu_IO_cookie_seek_unlocked() */


/***************************************************************
  Return the logical external-byte position of a cookie stream.
 ***************************************************************/
static off_t __mpu_IO_cookie_tell_unlocked( mpu_FILE *stream )
{
  off_t  pos;

  pos = __mpu_IO_cookie_call_seek( stream, (off_t)0, MPU_SEEK_CUR );
  if( pos == (off_t)-1 )
  {
    stream->_flags |= __MPU_IO_ERR_SEEN;
    return( (off_t)-1 );
  }

  if( stream->_flags & __MPU_IO_READING )
  {
    unsigned int  i;

    if( stream->_read_ptr != (unsigned char *)0 &&
        stream->_read_end != (unsigned char *)0 )
      pos -= (off_t)(stream->_read_end - stream->_read_ptr);

    for( i = 0; i < stream->_unget_count; ++i )
    {
      uint32_t u = (uint32_t)stream->_unget_chars[i];
      pos -= (off_t)(u < 0x80U ? 1 : (u < 0x800U ? 2 : 3));
    }
  }
  else if( stream->_flags & __MPU_IO_WRITING )
  {
    if( stream->_write_base != (unsigned char *)0 &&
        stream->_write_ptr != (unsigned char *)0 )
      pos += (off_t)(stream->_write_ptr - stream->_write_base);
  }

  return( pos );

} /* End of __mpu_IO_cookie_tell_unlocked() */


/***************************************************************
  Seek relative to the logical cookie stream position.
 ***************************************************************/
static off_t __mpu_IO_cookie_seekoff_unlocked( mpu_FILE *stream,
                                               off_t offset, int whence )
{
  off_t  base;

  if( whence == MPU_SEEK_CUR )
  {
    base = __mpu_IO_cookie_tell_unlocked( stream );
    if( base == (off_t)-1 ) return( (off_t)-1 );

    if( (offset > 0 && base > (off_t)(INT64_MAX - offset)) ||
        (offset < 0 && base < (off_t)(INT64_MIN - offset)) )
    {
      errno = EOVERFLOW;
      stream->_flags |= __MPU_IO_ERR_SEEN;
      return( (off_t)-1 );
    }
    offset = base + offset;
    whence = MPU_SEEK_SET;
  }

  if( __mpu_IO_cookie_seek_unlocked( stream, offset, whence ) != 0 )
    return( (off_t)-1 );

  return( __mpu_IO_cookie_tell_unlocked( stream ) );

} /* End of __mpu_IO_cookie_seekoff_unlocked() */


/***************************************************************
  Seek a cookie stream to an absolute position.
 ***************************************************************/
static off_t __mpu_IO_cookie_seekpos_unlocked( mpu_FILE *stream,
                                               off_t position )
{
  return( __mpu_IO_cookie_seekoff_unlocked( stream, position,
                                            MPU_SEEK_SET ) );

} /* End of __mpu_IO_cookie_seekpos_unlocked() */


/***************************************************************
  Invoke the user close callback and release private cookie state.
 ***************************************************************/
static int __mpu_IO_cookie_close_unlocked( mpu_FILE *stream )
{
  __mpu_IO_cookie_file *cf  = __mpu_IO_cookie_data( stream );
  int                   ret = 0;

  if( cf != (__mpu_IO_cookie_file *)0 &&
      cf->io.close != (mpu_cookie_close_function_t *)0 )
    ret = (*cf->io.close)( cf->user_cookie );

  free( cf );
  stream->_cookie = (void *)0;

  if( ret != 0 )
  {
    stream->_flags |= __MPU_IO_ERR_SEEN;
    return( mpu_EOF );
  }

  return( 0 );

} /* End of __mpu_IO_cookie_close_unlocked() */


const struct __mpu_IO_jump_t  __mpu_IO_cookie_jumps =
{
  .finish    = __mpu_IO_cookie_finish_unlocked,
  .underflow = __mpu_IO_cookie_underflow_unlocked,
  .overflow  = __mpu_IO_cookie_overflow_unlocked,
  .sync      = __mpu_IO_cookie_sync_unlocked,
  .setbuf    = __mpu_IO_cookie_setbuf_unlocked,
  .seekoff   = __mpu_IO_cookie_seekoff_unlocked,
  .seekpos   = __mpu_IO_cookie_seekpos_unlocked,
  .flush     = __mpu_IO_cookie_flush_unlocked,
  .close     = __mpu_IO_cookie_close_unlocked,
  .seek      = __mpu_IO_cookie_seek_unlocked,
  .tell      = __mpu_IO_cookie_tell_unlocked,
  .get_byte  = __mpu_IO_cookie_get_byte_unlocked,
  .put_byte  = __mpu_IO_cookie_put_byte_unlocked,
  .xsgetn    = __mpu_IO_cookie_xsgetn_unlocked,
  .xsputn    = __mpu_IO_cookie_xsputn_unlocked,
  .get_char  = __mpu_UTF8_decode_unlocked,
  .put_char  = __mpu_UTF8_encode_unlocked
};


/***************************************************************
  Open an arbitrary byte-oriented callback object as an MPU
  stream.  Text functions decode/encode UTF-8 at this boundary.
 ***************************************************************/
mpu_FILE *mpu_fopencookie( void *cookie, const char *mode,
                           mpu_cookie_io_functions_t io_functions )
{
  struct __mpu_IO_FILE_plus *plus;
  __mpu_IO_cookie_file      *cf;
  mpu_FILE                  *stream;
  unsigned int               flags;
  int                        oflags;

  if( __mpu_IO_parse_mode( mode, &flags, &oflags, 0 ) < 0 )
    return( (mpu_FILE *)0 );
  (void)oflags;

  if( (!(flags & __MPU_IO_NO_READS) &&
        io_functions.read == (mpu_cookie_read_function_t *)0) ||
      (!(flags & __MPU_IO_NO_WRITES) &&
        io_functions.write == (mpu_cookie_write_function_t *)0) ||
      ((flags & __MPU_IO_APPEND) &&
        io_functions.seek == (mpu_cookie_seek_function_t *)0) )
  {
    errno = EINVAL;
    return( (mpu_FILE *)0 );
  }

  plus = (struct __mpu_IO_FILE_plus *)calloc( 1, sizeof(*plus) );
  if( plus == (struct __mpu_IO_FILE_plus *)0 )
    return( (mpu_FILE *)0 );

  cf = (__mpu_IO_cookie_file *)calloc( 1, sizeof(*cf) );
  if( cf == (__mpu_IO_cookie_file *)0 )
  {
    free( plus );
    return( (mpu_FILE *)0 );
  }

  stream = &plus->file;
  plus->vtable = &__mpu_IO_cookie_jumps;
  cf->user_cookie = cookie;
  cf->io = io_functions;

  stream->_magic = __MPU_IO_MAGIC;
  stream->_flags = flags | __MPU_IO_COOKIE;
  stream->_fileno = -1;
  stream->_cookie = cf;

  {
    int  lock_error = __mpu_IO_lock_init( &stream->_lock );

    if( lock_error != 0 )
    {
      free( cf );
      free( plus );
      errno = lock_error;
      return( (mpu_FILE *)0 );
    }
  }

  if( (flags & __MPU_IO_APPEND) &&
      io_functions.seek != (mpu_cookie_seek_function_t *)0 )
  {
    off_t  pos = (off_t)0;

    if( (*io_functions.seek)( cookie, &pos, MPU_SEEK_END ) != 0 )
    {
      int  saved_errno = errno;
      pthread_mutex_destroy( &stream->_lock );
      free( cf );
      free( plus );
      errno = saved_errno;
      return( (mpu_FILE *)0 );
    }
  }

  __mpu_IO_link_in( stream );
  return( stream );

} /* End of mpu_fopencookie() */


/***************************************************************
  Hide internal symbols:
 ***************************************************************/

__mpu_hidden_decl(__mpu_IO_cookie_jumps);

/*
  End of hide internal symbols.
 ***************************************************************/

/* End of mpu-cookie.c */