1dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project/*
2dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * Copyright (C) 2007 The Android Open Source Project
3dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project *
4dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License");
5dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * you may not use this file except in compliance with the License.
6dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * You may obtain a copy of the License at
7dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project *
8dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project *      http://www.apache.org/licenses/LICENSE-2.0
9dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project *
10dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * Unless required by applicable law or agreed to in writing, software
11dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS,
12dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * See the License for the specific language governing permissions and
14dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * limitations under the License.
15dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project */
16dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
17dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project/**
18dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * Byte buffer utilities.
19dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project */
20dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
21dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#ifndef __BUFFER_H
22dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#define __BUFFER_H
23dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
24dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#ifdef __cplusplus
25dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectextern "C" {
26dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#endif
27dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
28dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <stdlib.h>
29dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
30dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project/**
31dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * Byte buffer of known size. Keeps track of how much data has been read
32dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * into or written out of the buffer.
33dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project */
34dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projecttypedef struct {
35dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    /** Buffered data. */
36dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    char* data;
37dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
38dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    union {
39dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        /** For reading. # of bytes we expect. */
40dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        size_t expected;
41dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
42dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        /** For writing. # of bytes to write. */
43dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        size_t remaining;
44dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    };
45dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
46dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    /** Actual # of bytes in the buffer. */
47dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    size_t size;
48dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
49dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    /** Amount of memory allocated for this buffer. */
50dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    size_t capacity;
51dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project} Buffer;
52dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
53dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project/**
54dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * Returns true if all data has been read into the buffer.
55dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project */
56dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#define bufferReadComplete(buffer) (buffer->expected == buffer->size)
57dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
58dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project/**
59dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * Returns true if the buffer has been completely written.
60dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project */
61dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#define bufferWriteComplete(buffer) (buffer->remaining == 0)
62dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
63dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project/**
64dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * Creates a new buffer with the given initial capacity.
65dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project */
66dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source ProjectBuffer* bufferCreate(size_t initialCapacity);
67dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
68dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project/**
69dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * Wraps an existing byte array.
70dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project */
71dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source ProjectBuffer* bufferWrap(char* data, size_t capacity, size_t size);
72dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
73dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project/**
74dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * Frees and its data.
75dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project */
76dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectvoid bufferFree(Buffer* buffer);
77dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
78dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project/**
79dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * Prepares buffer to read 'expected' number of bytes. Expands capacity if
80dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * necessary. Returns 0 if successful or -1 if an error occurs allocating
81dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * memory.
82dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project */
83dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectint bufferPrepareForRead(Buffer* buffer, size_t expected);
84dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
85dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project/**
86dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * Reads some data into a buffer. Returns -1 in case of an error and sets
87dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * errno (see read()). Returns 0 for EOF. Updates buffer->size and returns
88dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * the new size after a succesful read.
89dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project *
90dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * Precondition: buffer->size < buffer->expected
91dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project */
92dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectssize_t bufferRead(Buffer* buffer, int fd);
93dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
94dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project/**
95dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * Prepares a buffer to be written out.
96dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project */
97dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectvoid bufferPrepareForWrite(Buffer* buffer);
98dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
99dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project/**
100dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * Writes data from buffer to the given fd. Returns -1 and sets errno in case
101dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * of an error. Updates buffer->remaining and returns the number of remaining
102dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * bytes to be written after a successful write.
103dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project *
104dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * Precondition: buffer->remaining > 0
105dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project */
106dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectssize_t bufferWrite(Buffer* buffer, int fd);
107dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
108dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#ifdef __cplusplus
109dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
110dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#endif
111dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
112dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#endif /* __BUFFER_H */
113