1 /*
2  *
3  * Copyright 2015 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 #ifndef GRPC_IMPL_CODEGEN_BYTE_BUFFER_H
20 #define GRPC_IMPL_CODEGEN_BYTE_BUFFER_H
21 
22 #include <grpc/impl/codegen/port_platform.h>
23 
24 #include <grpc/impl/codegen/grpc_types.h>
25 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 
30 /** Returns a RAW byte buffer instance over the given slices (up to \a nslices).
31  *
32  * Increases the reference count for all \a slices processed. The user is
33  * responsible for invoking grpc_byte_buffer_destroy on the returned instance.*/
34 GRPCAPI grpc_byte_buffer* grpc_raw_byte_buffer_create(grpc_slice* slices,
35                                                       size_t nslices);
36 
37 /** Returns a *compressed* RAW byte buffer instance over the given slices (up to
38  * \a nslices). The \a compression argument defines the compression algorithm
39  * used to generate the data in \a slices.
40  *
41  * Increases the reference count for all \a slices processed. The user is
42  * responsible for invoking grpc_byte_buffer_destroy on the returned instance.*/
43 GRPCAPI grpc_byte_buffer* grpc_raw_compressed_byte_buffer_create(
44     grpc_slice* slices, size_t nslices, grpc_compression_algorithm compression);
45 
46 /** Copies input byte buffer \a bb.
47  *
48  * Increases the reference count of all the source slices. The user is
49  * responsible for calling grpc_byte_buffer_destroy over the returned copy. */
50 GRPCAPI grpc_byte_buffer* grpc_byte_buffer_copy(grpc_byte_buffer* bb);
51 
52 /** Returns the size of the given byte buffer, in bytes. */
53 GRPCAPI size_t grpc_byte_buffer_length(grpc_byte_buffer* bb);
54 
55 /** Destroys \a byte_buffer deallocating all its memory. */
56 GRPCAPI void grpc_byte_buffer_destroy(grpc_byte_buffer* byte_buffer);
57 
58 /** Reader for byte buffers. Iterates over slices in the byte buffer */
59 struct grpc_byte_buffer_reader;
60 typedef struct grpc_byte_buffer_reader grpc_byte_buffer_reader;
61 
62 /** Initialize \a reader to read over \a buffer.
63  * Returns 1 upon success, 0 otherwise. */
64 GRPCAPI int grpc_byte_buffer_reader_init(grpc_byte_buffer_reader* reader,
65                                          grpc_byte_buffer* buffer);
66 
67 /** Cleanup and destroy \a reader */
68 GRPCAPI void grpc_byte_buffer_reader_destroy(grpc_byte_buffer_reader* reader);
69 
70 /** Updates \a slice with the next piece of data from from \a reader and returns
71  * 1. Returns 0 at the end of the stream. Caller is responsible for calling
72  * grpc_slice_unref on the result. */
73 GRPCAPI int grpc_byte_buffer_reader_next(grpc_byte_buffer_reader* reader,
74                                          grpc_slice* slice);
75 
76 /** Merge all data from \a reader into single slice */
77 GRPCAPI grpc_slice
78 grpc_byte_buffer_reader_readall(grpc_byte_buffer_reader* reader);
79 
80 /** Returns a RAW byte buffer instance from the output of \a reader. */
81 GRPCAPI grpc_byte_buffer* grpc_raw_byte_buffer_from_reader(
82     grpc_byte_buffer_reader* reader);
83 
84 #ifdef __cplusplus
85 }
86 #endif
87 
88 #endif /* GRPC_IMPL_CODEGEN_BYTE_BUFFER_H */
89