summaryrefslogtreecommitdiffstats
path: root/lib/libshout-idjc/src/queue.c
blob: a5274a05bb1c5816e4117008a00002da8d81b452 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/* -*- c-basic-offset: 8; -*- */
/* queue.c: Implementation data queue logic.
 *
 *  Copyright (C) 2002-2004 the Icecast team <team@icecast.org>,
 *  Copyright (C) 2012-2015 Philipp "ph3-der-loewe" Schafft <lion@lion.leolix.org>
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public
 *  License along with this library; if not, write to the Free
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * $Id$
 */

#ifdef HAVE_CONFIG_H
#   include <config.h>
#endif

#include <stdlib.h>
#include <string.h>
#include <stdio.h>

#include <shoutidjc/shout.h>
#include "shout_private.h"

/* queue data in pages of SHOUT_BUFSIZE bytes */
int shout_queue_data(shout_queue_t *queue, const unsigned char *data, size_t len)
{
    shout_buf_t *buf;
    size_t       plen;

	if (!len)
        return SHOUTERR_SUCCESS;

    if (!queue->len) {
        queue->head = calloc(1, sizeof(shout_buf_t));
		if (!queue->head)
            return SHOUTERR_MALLOC;
    }

    for (buf = queue->head; buf->next; buf = buf->next) ;

    /* Maybe any added data should be freed if we hit a malloc error?
     * Otherwise it'd be impossible to tell where to start requeueing.
     * (As if anyone ever tried to recover from a malloc error.) */
    while (len > 0) {
        if (buf->len == SHOUT_BUFSIZE) {
            buf->next = calloc(1, sizeof(shout_buf_t));
			if (!buf->next)
                return SHOUTERR_MALLOC;
            buf->next->prev = buf;
            buf = buf->next;
        }

        plen = len > SHOUT_BUFSIZE - buf->len ? SHOUT_BUFSIZE - buf->len : len;
        memcpy(buf->data + buf->len, data, plen);
        buf->len += plen;
        data += plen;
        len -= plen;
        queue->len += plen;
    }
    return SHOUTERR_SUCCESS;
}

int shout_queue_str(shout_t *self, const char *str)
{
    return shout_queue_data(&self->wqueue, (const unsigned char*)str, strlen(str));
}

/* this should be shared with sock_write. Create libicecommon. */
int shout_queue_printf(shout_t *self, const char *fmt, ...)
{
    char        buffer[1024];
    char       *buf;
    va_list     ap, ap_retry;
    int         len;

    buf = buffer;

    va_start(ap, fmt);
    va_copy(ap_retry, ap);

    len = vsnprintf(buf, sizeof(buffer), fmt, ap);

    self->error = SHOUTERR_SUCCESS;
    if (len > 0) {
		if ((size_t)len < sizeof(buffer)) {
            shout_queue_data(&self->wqueue, (unsigned char*)buf, len);
        } else {
            buf = malloc(++len);
            if (buf) {
                len = vsnprintf(buf, len, fmt, ap_retry);
                shout_queue_data(&self->wqueue, (unsigned char*)buf, len);
                free(buf);
			} else {
                self->error = SHOUTERR_MALLOC;
            }
        }
    }

    va_end(ap_retry);
    va_end(ap);

    return self->error;
}

void shout_queue_free(shout_queue_t *queue)
{
    shout_buf_t *prev;

    while (queue->head) {
        prev = queue->head;
        queue->head = queue->head->next;
        free(prev);
    }
    queue->len = 0;
}

/* collect nodes of a queue into a single buffer */
ssize_t shout_queue_collect(shout_buf_t *queue, char **buf)
{
    shout_buf_t *node;
    size_t       pos = 0;
    size_t       len = 0;

    for (node = queue; node; node = node->next)
        len += node->len;

    if (!(*buf = malloc(len)))
        return SHOUTERR_MALLOC;

    for (node = queue; node; node = node->next) {
        memcpy(*buf + pos, node->data, node->len);
        pos += node->len;
    }

    return len;
}