1 #include "alutInternal.h"
2 #include <string.h>
3 
4 struct OutputStream_struct
5 {
6   char *data;
7   char *current;
8   size_t maximumLength;
9 };
10 
11 /****************************************************************************
12  * The functions below know the internal OutputStream representation.
13  ****************************************************************************/
14 
15 OutputStream *
_alutOutputStreamConstruct(size_t maximumLength)16 _alutOutputStreamConstruct (size_t maximumLength)
17 {
18   OutputStream *stream = (OutputStream *) _alutMalloc (sizeof (OutputStream));
19   if (stream == NULL)
20     {
21       return NULL;
22     }
23   stream->data = _alutMalloc (maximumLength);
24   if (stream->data == NULL)
25     {
26       free (stream);
27       return NULL;
28     }
29   stream->current = stream->data;
30   stream->maximumLength = maximumLength;
31   return stream;
32 }
33 
34 ALboolean
_alutOutputStreamDestroy(OutputStream * stream)35 _alutOutputStreamDestroy (OutputStream *stream)
36 {
37   free (stream->data);
38   free (stream);
39   return AL_TRUE;
40 }
41 
42 void *
_alutOutputStreamGetData(OutputStream * stream)43 _alutOutputStreamGetData (OutputStream *stream)
44 {
45   return stream->data;
46 }
47 
48 size_t
_alutOutputStreamGetLength(OutputStream * stream)49 _alutOutputStreamGetLength (OutputStream *stream)
50 {
51   return stream->current - stream->data;
52 }
53 
54 static ALboolean
streamWrite(OutputStream * stream,const void * ptr,size_t numBytesToWrite)55 streamWrite (OutputStream *stream, const void *ptr, size_t numBytesToWrite)
56 {
57   size_t remainingLength =
58     stream->maximumLength - _alutOutputStreamGetLength (stream);
59   if (remainingLength < numBytesToWrite)
60     {
61       /* this should never happen within our library */
62       _alutSetError (ALUT_ERROR_IO_ERROR);
63       return AL_FALSE;
64     }
65   memcpy (stream->current, ptr, numBytesToWrite);
66   stream->current += numBytesToWrite;
67   return AL_TRUE;
68 }
69 
70 /****************************************************************************
71  * The utility functions below do not know the internal OutputStream
72  * representation.
73  ****************************************************************************/
74 
75 ALboolean
_alutOutputStreamWriteInt16BE(OutputStream * stream,Int16BigEndian value)76 _alutOutputStreamWriteInt16BE (OutputStream *stream, Int16BigEndian value)
77 {
78   unsigned char buf[2];
79   buf[0] = (unsigned char) (value >> 8);
80   buf[1] = (unsigned char) value;
81   return streamWrite (stream, buf, 2);
82 }
83 
84 ALboolean
_alutOutputStreamWriteInt32BE(OutputStream * stream,Int32BigEndian value)85 _alutOutputStreamWriteInt32BE (OutputStream *stream, Int32BigEndian value)
86 {
87   unsigned char buf[4];
88   buf[0] = (unsigned char) (value >> 24);
89   buf[1] = (unsigned char) (value >> 16);
90   buf[2] = (unsigned char) (value >> 8);
91   buf[3] = (unsigned char) value;
92   return streamWrite (stream, buf, 4);
93 }
94