Switch RDFF to 2-clause BSD license.
[ardour.git] / libs / ardour / rdff.h
1 /*
2   RDFF - RDF in RIFF
3   Copyright 2011 David Robillard <http://drobilla.net>
4
5   Redistribution and use in source and binary forms, with or without
6   modification, are permitted provided that the following conditions are met:
7
8   1. Redistributions of source code must retain the above copyright notice,
9      this list of conditions and the following disclaimer.
10
11   2. Redistributions in binary form must reproduce the above copyright
12      notice, this list of conditions and the following disclaimer in the
13      documentation and/or other materials provided with the distribution.
14
15   THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
16   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
17   AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
18   AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
19   OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
24   THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27 #ifndef RDFF_RDFF_H
28 #define RDFF_RDFF_H
29
30 #include <stdbool.h>
31 #include <stdint.h>
32
33 #ifdef __GNUC__
34 #    define PACKED __attribute__((__packed__))
35 #else
36 #    define PACKED
37 #endif
38
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
42
43 typedef struct _RDFF* RDFF;
44
45 typedef enum {
46         RDFF_STATUS_OK            = 0,
47         RDFF_STATUS_UNKNOWN_ERROR = 1,
48         RDFF_STATUS_EOF           = 2,
49         RDFF_STATUS_CORRUPT       = 3
50 } RDFFStatus;
51
52 /**
53    Generic RIFF chunk header.
54 */
55 typedef struct {
56         char     type[4];
57         uint32_t size;
58         char     data[];
59 } PACKED RDFFChunk;
60
61 /**
62    Body of a URID chunk.
63 */
64 typedef struct {
65         uint32_t id;
66         char     uri[];
67 } PACKED RDFFURIChunk;
68
69 /**
70    Body of a KVAL chunk.
71 */
72 typedef struct {
73         uint32_t key;
74         uint32_t type;
75         uint32_t size;
76         char     value[];
77 } PACKED RDFFValueChunk;
78
79 /**
80    Open/Create a new RDFF file.
81 */
82 RDFF
83 rdff_open(const char* path, bool write);
84
85 /**
86    Write a URI ID to @a file.
87 */
88 RDFFStatus
89 rdff_write_uri(RDFF        file,
90                uint32_t    id,
91                const char* uri,
92                uint32_t    len);
93
94 /**
95    Write a key/value record to @a file.
96 */
97 RDFFStatus
98 rdff_write_value(RDFF        file,
99                  uint32_t    key,
100                  const void* value,
101                  uint32_t    size,
102                  uint32_t    type);
103
104 /**
105    Read a chunk from @a file.
106
107    @param buf MUST point to an RDFFChunk dynamically allocated with malloc.
108    The @a size field (i.e. (*buf)->size) MUST be set to the amount of available
109    memory in the chunk (not including the header). If this is insufficient,
110    *buf will be resized using realloc.
111 */
112 RDFFStatus
113 rdff_read_chunk(RDFF        file,
114                 RDFFChunk** buf);
115
116 /**
117    Close @a file.
118    After this call, @a file is invalid.
119 */
120 void
121 rdff_close(RDFF file);
122
123 #ifdef __cplusplus
124 } /* extern "C" */
125 #endif
126
127 #endif /* RDFF_RDFF_H */