Skip to content

Commit c415006

Browse files
committed
Initial commit
1 parent aab0c43 commit c415006

File tree

4 files changed

+187
-5
lines changed

4 files changed

+187
-5
lines changed

src/m3u8.c

+3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "readlines.h"
1515
#include "m3u8httpclient.h"
1616
#include "hex.h"
17+
#include "sutils.h"
1718
#include "guess_uri.h"
1819

1920
static const enum M3U8VAttrType M3U8_VATTR_TYPES[] = {
@@ -2348,6 +2349,8 @@ int m3u8_parse(struct M3U8Playlist* const playlist, const char* const s) {
23482349
memcpy(attr_name, start, size);
23492350
attr_name[size] = '\0';
23502351

2352+
strip_whitespaces(attr_name);
2353+
23512354
if (!namesafe(attr_name)) {
23522355
err = M3U8ERR_ATTRIBUTE_INVALID_NAME;
23532356
goto end;

src/python-kai.c

+128-5
Original file line numberDiff line numberDiff line change
@@ -982,6 +982,9 @@ static PyObject* topy(
982982
PyObject* subtitles = NULL;
983983
PyObject* closed_captions = NULL;
984984
PyObject* size = NULL;
985+
PyObject* version = NULL;
986+
PyObject* value = NULL;
987+
PyObject* data_id = NULL;
985988

986989
PyObject* result = NULL;
987990

@@ -996,6 +999,128 @@ static PyObject* topy(
996999
}
9971000

9981001
switch (item->type) {
1002+
case M3U8_STREAM_SESSION_DATA: {
1003+
const struct M3U8SessionData* const session_data = item->item;
1004+
1005+
/* Data ID */
1006+
Py_XDECREF(data_id);
1007+
1008+
data_id = get_string(session_data->data_id);
1009+
1010+
if (data_id == NULL) {
1011+
return NULL;
1012+
}
1013+
1014+
/* Value */
1015+
Py_XDECREF(value);
1016+
1017+
value = get_string(session_data->value);
1018+
1019+
if (value == NULL) {
1020+
return NULL;
1021+
}
1022+
1023+
/* URI */
1024+
Py_XDECREF(uri);
1025+
1026+
err = m3u8uri_resolve(base_uri, session_data->uri, &resolved_uri);
1027+
1028+
uri = get_string((err == M3U8ERR_SUCCESS) ? resolved_uri : session_data->uri);
1029+
1030+
if (uri == NULL) {
1031+
return NULL;
1032+
}
1033+
1034+
/* Language */
1035+
Py_XDECREF(language);
1036+
1037+
language = get_string(session_data->language);
1038+
1039+
if (language == NULL) {
1040+
return NULL;
1041+
}
1042+
1043+
/* Tag */
1044+
Py_XDECREF(tag);
1045+
1046+
tag = PyLong_FromLongLong((uintptr_t) session_data->tag);
1047+
1048+
if (tag == NULL) {
1049+
return NULL;
1050+
}
1051+
1052+
Py_XDECREF(args);
1053+
1054+
args = Py_BuildValue(
1055+
"(OOOOO)",
1056+
data_id,
1057+
value,
1058+
uri,
1059+
language,
1060+
tag
1061+
);
1062+
1063+
if (args == NULL) {
1064+
return NULL;
1065+
}
1066+
1067+
Py_XDECREF(class);
1068+
1069+
class = PyObject_GetAttrString(module, "M3U8SessionData");
1070+
1071+
if (class == NULL) {
1072+
return NULL;
1073+
}
1074+
1075+
result = PyObject_Call(class, args, NULL);
1076+
1077+
break;
1078+
}
1079+
case M3U8_STREAM_VERSION: {
1080+
const struct M3U8Version* const ver = item->item;
1081+
1082+
/* Version */
1083+
Py_XDECREF(version);
1084+
1085+
version = PyLong_FromLongLong(ver->version);
1086+
1087+
if (version == NULL) {
1088+
return NULL;
1089+
}
1090+
1091+
/* Tag */
1092+
Py_XDECREF(tag);
1093+
1094+
tag = PyLong_FromLongLong((uintptr_t) ver->tag);
1095+
1096+
if (tag == NULL) {
1097+
return NULL;
1098+
}
1099+
1100+
Py_XDECREF(args);
1101+
1102+
args = Py_BuildValue(
1103+
"(OO)",
1104+
version,
1105+
tag
1106+
);
1107+
1108+
if (args == NULL) {
1109+
return NULL;
1110+
}
1111+
1112+
Py_XDECREF(class);
1113+
1114+
class = PyObject_GetAttrString(module, "M3U8Version");
1115+
1116+
if (class == NULL) {
1117+
return NULL;
1118+
}
1119+
1120+
result = PyObject_Call(class, args, NULL);
1121+
1122+
break;
1123+
}
9991124
case M3U8_STREAM_MEDIA: {
10001125
const struct M3U8Media* const media = item->item;
10011126

@@ -1479,11 +1604,9 @@ static PyObject* _m3u8stream_getstream(
14791604
const struct M3U8StreamItem* const item = &stream->items[index];
14801605

14811606
switch (item->type) {
1482-
case M3U8_STREAM_MEDIA: {
1483-
kitem = topy(stream, item);
1484-
PyList_Append(items, kitem);
1485-
break;
1486-
}
1607+
case M3U8_STREAM_SESSION_DATA:
1608+
case M3U8_STREAM_MEDIA:
1609+
case M3U8_STREAM_VERSION:
14871610
case M3U8_STREAM_VARIANT_STREAM: {
14881611
kitem = topy(stream, item);
14891612
PyList_Append(items, kitem);

src/sutils.c

+54
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,57 @@ size_t uintptrlen(const uintptr_t value) {
8484
return size;
8585

8686
}
87+
88+
char* strip_whitespaces(char* const s) {
89+
90+
size_t size = 0;
91+
92+
const char* start = s;
93+
const char* end = strchr(s, '\0');
94+
95+
while (start != end) {
96+
const char ch = *start;
97+
98+
if (!isspace(ch)) {
99+
break;
100+
}
101+
102+
start++;
103+
}
104+
105+
if (start != end) {
106+
end--;
107+
}
108+
109+
while (end != start) {
110+
const unsigned char ch = *end;
111+
112+
if (!isspace(ch)) {
113+
break;
114+
}
115+
116+
end--;
117+
}
118+
119+
if (*end != '\0') {
120+
end++;
121+
}
122+
123+
size = (size_t) (start - s);
124+
125+
if (size != 0) {
126+
size = (size_t) (end - start);
127+
memmove(s, start, size);
128+
s[size] = '\0';
129+
}
130+
131+
return s;
132+
133+
}
134+
135+
int main() {
136+
char s[] = "VALUE";
137+
strip_whitespaces(s);
138+
puts(s);
139+
}
140+

src/sutils.h

+2
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,6 @@ size_t uintlen(const biguint_t value);
1010
size_t intptrlen(const intptr_t value);
1111
size_t uintptrlen(const uintptr_t value);
1212

13+
char* strip_whitespaces(char* const s);
14+
1315
#endif

0 commit comments

Comments
 (0)