source: roaraudio/libroar/vio_misc.c @ 4824:bd21c2bc0214

Last change on this file since 4824:bd21c2bc0214 was 4824:bd21c2bc0214, checked in by phi, 13 years ago

Added VIOs and DSTR elements: null:, zero:, nrandom:

File size: 4.6 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 return -1;
78}
79
80static int     roar_vio_misc_close   (struct roar_vio_calls * vio) {
81 return 0;
82}
83
84int     roar_vio_open_misc    (struct roar_vio_calls * calls, const struct roar_vio_misc * callbacks) {
85 if ( calls == NULL || callbacks == NULL )
86  return -1;
87
88 memset(calls, 0, sizeof(struct roar_vio_calls));
89 calls->read     = roar_vio_misc_read;
90 calls->write    = roar_vio_misc_write;
91 calls->lseek    = roar_vio_misc_lseek;
92 calls->nonblock = roar_vio_misc_nonblock;
93 calls->sync     = roar_vio_misc_sync;
94 calls->ctl      = roar_vio_misc_ctl;
95 calls->close    = roar_vio_misc_close;
96 calls->inst     = (void*)callbacks;
97
98 return 0;
99}
100
101static ssize_t roar_vio_misc_op_return_len (void * buf, size_t len, struct roar_vio_calls * vio, struct roar_vio_misc * callbacks) {
102 return len;
103}
104
105static ssize_t roar_vio_misc_op_return_zero(void * buf, size_t len, struct roar_vio_calls * vio, struct roar_vio_misc * callbacks) {
106 return 0;
107}
108
109static ssize_t roar_vio_misc_op_zero(void * buf, size_t len, struct roar_vio_calls * vio, struct roar_vio_misc * callbacks) {
110 memset(buf, 0, len);
111 return len;
112}
113
114static ssize_t roar_vio_misc_op_random_nonce(void * buf, size_t len, struct roar_vio_calls * vio, struct roar_vio_misc * callbacks) {
115 if ( roar_random_gen_nonce(buf, len) == -1 )
116  return -1;
117
118 return len;
119}
120
121static ssize_t roar_vio_misc_op_random_salt_nonce(void * buf, size_t len, struct roar_vio_calls * vio, struct roar_vio_misc * callbacks) {
122 if ( roar_random_salt_nonce(buf, len) == -1 )
123  return -1;
124
125 return len;
126}
127
128static const struct roar_vio_misc libroar_vio_miscs[] = {
129 {.name = "null", .support_nonblocking = 1, .read = roar_vio_misc_op_return_zero, .write = roar_vio_misc_op_return_len},
130 {.name = "zero", .support_nonblocking = 1, .read = roar_vio_misc_op_zero,        .write = roar_vio_misc_op_return_len},
131 {.name = "nrandom", .support_nonblocking = 1,
132  .read = roar_vio_misc_op_random_nonce, .write = roar_vio_misc_op_random_salt_nonce}
133};
134
135int     roar_vio_open_misc_by_name(struct roar_vio_calls * calls, const char * name) {
136 size_t i;
137
138 if ( calls == NULL || name == NULL )
139  return -1;
140
141 for (i = 0; i < (sizeof(libroar_vio_miscs)/sizeof(*libroar_vio_miscs)); i++)
142  if ( !strcasecmp(name, libroar_vio_miscs[i].name) )
143   return roar_vio_open_misc(calls, &(libroar_vio_miscs[i]));
144
145 return -1;
146}
147
148//ll
Note: See TracBrowser for help on using the repository browser.