source: roaraudio/libroar/vio_misc.c @ 5248:0133acb5ae31

Last change on this file since 5248:0133acb5ae31 was 4876:06a2f29d0450, checked in by phi, 13 years ago

some updates to handle error values better with DSTR

File size: 5.2 KB
Line 
1//vio_misc.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2011
5 *
6 *  This file is part of libroar a part of RoarAudio,
7 *  a cross-platform sound system for both, home and professional use.
8 *  See README for details.
9 *
10 *  This file is free software; you can redistribute it and/or modify
11 *  it under the terms of the GNU General Public License version 3
12 *  as published by the Free Software Foundation.
13 *
14 *  libroar is distributed in the hope that it will be useful,
15 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 *  GNU General Public License for more details.
18 *
19 *  You should have received a copy of the GNU General Public License
20 *  along with this software; see the file COPYING.  If not, write to
21 *  the Free Software Foundation, 51 Franklin Street, Fifth Floor,
22 *  Boston, MA 02110-1301, USA.
23 *
24 *  NOTE for everyone want's to change something and send patches:
25 *  read README and HACKING! There a addition information on
26 *  the license of this document you need to read before you send
27 *  any patches.
28 *
29 *  NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc
30 *  or libpulse*:
31 *  The libs libroaresd, libroararts and libroarpulse link this lib
32 *  and are therefore GPL. Because of this it may be illigal to use
33 *  them with any software that uses libesd, libartsc or libpulse*.
34 */
35
36#include "libroar.h"
37
38static ssize_t roar_vio_misc_read    (struct roar_vio_calls * vio, void *buf, size_t count) {
39 struct roar_vio_misc * self = vio->inst;
40
41 if ( self->read == NULL )
42  return -1;
43
44 return self->read(buf, count, vio, self);
45}
46
47static ssize_t roar_vio_misc_write   (struct roar_vio_calls * vio, void *buf, size_t count) {
48 struct roar_vio_misc * self = vio->inst;
49
50 if ( self->write == NULL )
51  return -1;
52
53 return self->write(buf, count, vio, self);
54}
55
56static off_t   roar_vio_misc_lseek   (struct roar_vio_calls * vio, off_t offset, int whence) {
57 return 0;
58}
59
60static int     roar_vio_misc_nonblock(struct roar_vio_calls * vio, int state) {
61 struct roar_vio_misc * self = vio->inst;
62
63 if ( self->support_nonblocking )
64  return 0;
65
66 if ( state == ROAR_SOCKET_BLOCK )
67  return 0;
68
69 return -1;
70}
71
72static int     roar_vio_misc_sync    (struct roar_vio_calls * vio) {
73 return 0;
74}
75
76static int     roar_vio_misc_ctl     (struct roar_vio_calls * vio, int cmd, void * data) {
77 struct roar_vio_misc * self = vio->inst;
78
79 if ( vio == NULL || cmd == -1 )
80  return -1;
81
82 switch (cmd) {
83  case ROAR_VIO_CTL_GET_NAME:
84    if ( data == NULL )
85     return -1;
86
87    *(const char**)data = self->name;
88    return 0;
89   break;
90  case ROAR_VIO_CTL_SET_NOSYNC:
91    vio->sync = NULL;
92    return 0;
93   break;
94 }
95
96 return -1;
97}
98
99static int     roar_vio_misc_close   (struct roar_vio_calls * vio) {
100 return 0;
101}
102
103int     roar_vio_open_misc    (struct roar_vio_calls * calls, const struct roar_vio_misc * callbacks) {
104 if ( calls == NULL || callbacks == NULL )
105  return -1;
106
107 memset(calls, 0, sizeof(struct roar_vio_calls));
108 calls->read     = roar_vio_misc_read;
109 calls->write    = roar_vio_misc_write;
110 calls->lseek    = roar_vio_misc_lseek;
111 calls->nonblock = roar_vio_misc_nonblock;
112 calls->sync     = roar_vio_misc_sync;
113 calls->ctl      = roar_vio_misc_ctl;
114 calls->close    = roar_vio_misc_close;
115 calls->inst     = (void*)callbacks;
116
117 return 0;
118}
119
120static ssize_t roar_vio_misc_op_return_len (void * buf, size_t len, struct roar_vio_calls * vio, struct roar_vio_misc * callbacks) {
121 return len;
122}
123
124static ssize_t roar_vio_misc_op_return_zero(void * buf, size_t len, struct roar_vio_calls * vio, struct roar_vio_misc * callbacks) {
125 return 0;
126}
127
128static ssize_t roar_vio_misc_op_zero(void * buf, size_t len, struct roar_vio_calls * vio, struct roar_vio_misc * callbacks) {
129 memset(buf, 0, len);
130 return len;
131}
132
133static ssize_t roar_vio_misc_op_full(void * buf, size_t len, struct roar_vio_calls * vio, struct roar_vio_misc * callbacks)
134{
135 roar_err_set(ROAR_ERROR_NOSPC);
136 return -1;
137}
138
139static ssize_t roar_vio_misc_op_random_nonce(void * buf, size_t len, struct roar_vio_calls * vio, struct roar_vio_misc * callbacks) {
140 if ( roar_random_gen_nonce(buf, len) == -1 )
141  return -1;
142
143 return len;
144}
145
146static ssize_t roar_vio_misc_op_random_salt_nonce(void * buf, size_t len, struct roar_vio_calls * vio, struct roar_vio_misc * callbacks) {
147 if ( roar_random_salt_nonce(buf, len) == -1 )
148  return -1;
149
150 return len;
151}
152
153static const struct roar_vio_misc libroar_vio_miscs[] = {
154 {.name = "null", .support_nonblocking = 1, .read = roar_vio_misc_op_return_zero, .write = roar_vio_misc_op_return_len},
155 {.name = "full", .support_nonblocking = 1, .read = roar_vio_misc_op_return_zero, .write = roar_vio_misc_op_full},
156 {.name = "zero", .support_nonblocking = 1, .read = roar_vio_misc_op_zero,        .write = roar_vio_misc_op_return_len},
157 {.name = "nrandom", .support_nonblocking = 1,
158  .read = roar_vio_misc_op_random_nonce, .write = roar_vio_misc_op_random_salt_nonce}
159};
160
161int     roar_vio_open_misc_by_name(struct roar_vio_calls * calls, const char * name) {
162 size_t i;
163
164 if ( calls == NULL || name == NULL )
165  return -1;
166
167 for (i = 0; i < (sizeof(libroar_vio_miscs)/sizeof(*libroar_vio_miscs)); i++)
168  if ( !strcasecmp(name, libroar_vio_miscs[i].name) )
169   return roar_vio_open_misc(calls, &(libroar_vio_miscs[i]));
170
171 return -1;
172}
173
174//ll
Note: See TracBrowser for help on using the repository browser.