-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
topic_tok.c
118 lines (95 loc) · 2.33 KB
/
topic_tok.c
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
/*
Copyright (c) 2010-2019 Roger Light <[email protected]>
All rights reserved. This program and the accompanying materials
are made available under the terms of the Eclipse Public License 2.0
and Eclipse Distribution License v1.0 which accompany this distribution.
The Eclipse Public License is available at
https://www.eclipse.org/legal/epl-2.0/
and the Eclipse Distribution License is available at
http:https://www.eclipse.org/org/documents/edl-v10.php.
SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
Contributors:
Roger Light - initial implementation and documentation.
*/
#include "config.h"
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include "mosquitto_broker_internal.h"
#include "memory_mosq.h"
#include "mqtt_protocol.h"
#include "util_mosq.h"
#include "utlist.h"
static char *strtok_hier(char *str, char **saveptr)
{
char *c;
if(str != NULL){
*saveptr = str;
}
if(*saveptr == NULL){
return NULL;
}
c = strchr(*saveptr, '/');
if(c){
str = *saveptr;
*saveptr = c+1;
c[0] = '\0';
}else if(*saveptr){
/* No match, but surplus string */
str = *saveptr;
*saveptr = NULL;
}
return str;
}
int sub__topic_tokenise(const char *subtopic, char **local_sub, char ***topics, const char **sharename)
{
char *saveptr = NULL;
char *token;
int count;
int topic_index = 0;
int i;
size_t len;
len = strlen(subtopic);
if(len == 0){
return MOSQ_ERR_INVAL;
}
*local_sub = mosquitto__strdup(subtopic);
if((*local_sub) == NULL) return MOSQ_ERR_NOMEM;
count = 0;
saveptr = *local_sub;
while(saveptr){
saveptr = strchr(&saveptr[1], '/');
count++;
}
*topics = mosquitto__calloc((size_t)(count+3) /* 3=$shared,sharename,NULL */, sizeof(char *));
if((*topics) == NULL){
mosquitto__free(*local_sub);
return MOSQ_ERR_NOMEM;
}
if((*local_sub)[0] != '$'){
(*topics)[topic_index] = "";
topic_index++;
}
token = strtok_hier((*local_sub), &saveptr);
while(token){
(*topics)[topic_index] = token;
topic_index++;
token = strtok_hier(NULL, &saveptr);
}
if(!strcmp((*topics)[0], "$share")){
if(count < 2){
mosquitto__free(*local_sub);
mosquitto__free(*topics);
return MOSQ_ERR_PROTOCOL;
}
if(sharename){
(*sharename) = (*topics)[1];
}
for(i=1; i<count-1; i++){
(*topics)[i] = (*topics)[i+1];
}
(*topics)[0] = "";
(*topics)[count-1] = NULL;
}
return MOSQ_ERR_SUCCESS;
}