Software /
code /
prosody
Annotate
util-src/strbitop.c @ 13587:fdb2e0568cf8
mod_authz_internal: Make 'prosody:guest' default role for all unknown JIDs
This fixes an issue where e.g. remote users or even other users on the server
were unable to list MUC rooms.
We want to define a permission to list MUC rooms, but we want it to be
available to everyone by default (the traditional behaviour).
prosody:guest is the lowest role we have. I ran a quick check and it isn't
really used for anything right now that would be concerning.
It was originally designed for anonymous logins. I think it's safe to treat
remote JIDs as equivalent, since we have no trust relationship with anonymous
users either.
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Tue, 07 Jan 2025 14:41:32 +0000 |
parent | 13429:6cdc6923d65a |
rev | line source |
---|---|
11163
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
1 /* |
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
2 * This project is MIT licensed. Please see the |
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
3 * COPYING file in the source package for more information. |
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
4 * |
11172
712b2e6a09d9
Back out 6dde2c9fa272: Doesn't work on Lua 5.1
Kim Alvefur <zash@zash.se>
parents:
11171
diff
changeset
|
5 * Copyright (C) 2016 Kim Alvefur |
11163
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
6 */ |
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
7 |
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
8 #include <lua.h> |
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
9 #include <lauxlib.h> |
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
10 |
13429
6cdc6923d65a
util.strbitop: Add common_prefix_bits() method
Matthew Wild <mwild1@gmail.com>
parents:
12976
diff
changeset
|
11 #include <sys/param.h> |
6cdc6923d65a
util.strbitop: Add common_prefix_bits() method
Matthew Wild <mwild1@gmail.com>
parents:
12976
diff
changeset
|
12 #include <limits.h> |
11163
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
13 |
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
14 /* TODO Deduplicate code somehow */ |
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
15 |
12469
2b3adaa6d38e
util.strbitop: Reduce scope of functions
Kim Alvefur <zash@zash.se>
parents:
11175
diff
changeset
|
16 static int strop_and(lua_State *L) { |
11163
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
17 luaL_Buffer buf; |
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
18 size_t a, b, i; |
11167 | 19 const char *str_a = luaL_checklstring(L, 1, &a); |
20 const char *str_b = luaL_checklstring(L, 2, &b); | |
11163
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
21 |
11175
235537247aa3
Back out changeset 2c1583bb0e0f
Kim Alvefur <zash@zash.se>
parents:
11172
diff
changeset
|
22 luaL_buffinit(L, &buf); |
235537247aa3
Back out changeset 2c1583bb0e0f
Kim Alvefur <zash@zash.se>
parents:
11172
diff
changeset
|
23 |
11163
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
24 if(a == 0 || b == 0) { |
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
25 lua_settop(L, 1); |
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
26 return 1; |
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
27 } |
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
28 |
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
29 for(i = 0; i < a; i++) { |
11172
712b2e6a09d9
Back out 6dde2c9fa272: Doesn't work on Lua 5.1
Kim Alvefur <zash@zash.se>
parents:
11171
diff
changeset
|
30 luaL_addchar(&buf, str_a[i] & str_b[i % b]); |
11163
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
31 } |
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
32 |
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
33 luaL_pushresult(&buf); |
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
34 return 1; |
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
35 } |
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
36 |
12469
2b3adaa6d38e
util.strbitop: Reduce scope of functions
Kim Alvefur <zash@zash.se>
parents:
11175
diff
changeset
|
37 static int strop_or(lua_State *L) { |
11163
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
38 luaL_Buffer buf; |
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
39 size_t a, b, i; |
11167 | 40 const char *str_a = luaL_checklstring(L, 1, &a); |
41 const char *str_b = luaL_checklstring(L, 2, &b); | |
11163
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
42 |
11175
235537247aa3
Back out changeset 2c1583bb0e0f
Kim Alvefur <zash@zash.se>
parents:
11172
diff
changeset
|
43 luaL_buffinit(L, &buf); |
235537247aa3
Back out changeset 2c1583bb0e0f
Kim Alvefur <zash@zash.se>
parents:
11172
diff
changeset
|
44 |
11163
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
45 if(a == 0 || b == 0) { |
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
46 lua_settop(L, 1); |
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
47 return 1; |
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
48 } |
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
49 |
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
50 for(i = 0; i < a; i++) { |
11172
712b2e6a09d9
Back out 6dde2c9fa272: Doesn't work on Lua 5.1
Kim Alvefur <zash@zash.se>
parents:
11171
diff
changeset
|
51 luaL_addchar(&buf, str_a[i] | str_b[i % b]); |
11163
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
52 } |
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
53 |
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
54 luaL_pushresult(&buf); |
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
55 return 1; |
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
56 } |
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
57 |
12469
2b3adaa6d38e
util.strbitop: Reduce scope of functions
Kim Alvefur <zash@zash.se>
parents:
11175
diff
changeset
|
58 static int strop_xor(lua_State *L) { |
11163
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
59 luaL_Buffer buf; |
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
60 size_t a, b, i; |
11167 | 61 const char *str_a = luaL_checklstring(L, 1, &a); |
62 const char *str_b = luaL_checklstring(L, 2, &b); | |
11163
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
63 |
11172
712b2e6a09d9
Back out 6dde2c9fa272: Doesn't work on Lua 5.1
Kim Alvefur <zash@zash.se>
parents:
11171
diff
changeset
|
64 luaL_buffinit(L, &buf); |
712b2e6a09d9
Back out 6dde2c9fa272: Doesn't work on Lua 5.1
Kim Alvefur <zash@zash.se>
parents:
11171
diff
changeset
|
65 |
11163
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
66 if(a == 0 || b == 0) { |
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
67 lua_settop(L, 1); |
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
68 return 1; |
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
69 } |
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
70 |
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
71 for(i = 0; i < a; i++) { |
11172
712b2e6a09d9
Back out 6dde2c9fa272: Doesn't work on Lua 5.1
Kim Alvefur <zash@zash.se>
parents:
11171
diff
changeset
|
72 luaL_addchar(&buf, str_a[i] ^ str_b[i % b]); |
11163
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
73 } |
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
74 |
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
75 luaL_pushresult(&buf); |
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
76 return 1; |
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
77 } |
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
78 |
13429
6cdc6923d65a
util.strbitop: Add common_prefix_bits() method
Matthew Wild <mwild1@gmail.com>
parents:
12976
diff
changeset
|
79 unsigned int clz(unsigned char c) { |
6cdc6923d65a
util.strbitop: Add common_prefix_bits() method
Matthew Wild <mwild1@gmail.com>
parents:
12976
diff
changeset
|
80 #if __GNUC__ |
6cdc6923d65a
util.strbitop: Add common_prefix_bits() method
Matthew Wild <mwild1@gmail.com>
parents:
12976
diff
changeset
|
81 return __builtin_clz((unsigned int) c) - ((sizeof(int)-1)*CHAR_BIT); |
6cdc6923d65a
util.strbitop: Add common_prefix_bits() method
Matthew Wild <mwild1@gmail.com>
parents:
12976
diff
changeset
|
82 #else |
6cdc6923d65a
util.strbitop: Add common_prefix_bits() method
Matthew Wild <mwild1@gmail.com>
parents:
12976
diff
changeset
|
83 if(c & 0x80) return 0; |
6cdc6923d65a
util.strbitop: Add common_prefix_bits() method
Matthew Wild <mwild1@gmail.com>
parents:
12976
diff
changeset
|
84 if(c & 0x40) return 1; |
6cdc6923d65a
util.strbitop: Add common_prefix_bits() method
Matthew Wild <mwild1@gmail.com>
parents:
12976
diff
changeset
|
85 if(c & 0x20) return 2; |
6cdc6923d65a
util.strbitop: Add common_prefix_bits() method
Matthew Wild <mwild1@gmail.com>
parents:
12976
diff
changeset
|
86 if(c & 0x10) return 3; |
6cdc6923d65a
util.strbitop: Add common_prefix_bits() method
Matthew Wild <mwild1@gmail.com>
parents:
12976
diff
changeset
|
87 if(c & 0x08) return 4; |
6cdc6923d65a
util.strbitop: Add common_prefix_bits() method
Matthew Wild <mwild1@gmail.com>
parents:
12976
diff
changeset
|
88 if(c & 0x04) return 5; |
6cdc6923d65a
util.strbitop: Add common_prefix_bits() method
Matthew Wild <mwild1@gmail.com>
parents:
12976
diff
changeset
|
89 if(c & 0x02) return 6; |
6cdc6923d65a
util.strbitop: Add common_prefix_bits() method
Matthew Wild <mwild1@gmail.com>
parents:
12976
diff
changeset
|
90 if(c & 0x01) return 7; |
6cdc6923d65a
util.strbitop: Add common_prefix_bits() method
Matthew Wild <mwild1@gmail.com>
parents:
12976
diff
changeset
|
91 return 8; |
6cdc6923d65a
util.strbitop: Add common_prefix_bits() method
Matthew Wild <mwild1@gmail.com>
parents:
12976
diff
changeset
|
92 #endif |
6cdc6923d65a
util.strbitop: Add common_prefix_bits() method
Matthew Wild <mwild1@gmail.com>
parents:
12976
diff
changeset
|
93 } |
6cdc6923d65a
util.strbitop: Add common_prefix_bits() method
Matthew Wild <mwild1@gmail.com>
parents:
12976
diff
changeset
|
94 |
6cdc6923d65a
util.strbitop: Add common_prefix_bits() method
Matthew Wild <mwild1@gmail.com>
parents:
12976
diff
changeset
|
95 LUA_API int strop_common_prefix_bits(lua_State *L) { |
6cdc6923d65a
util.strbitop: Add common_prefix_bits() method
Matthew Wild <mwild1@gmail.com>
parents:
12976
diff
changeset
|
96 size_t a, b, i; |
6cdc6923d65a
util.strbitop: Add common_prefix_bits() method
Matthew Wild <mwild1@gmail.com>
parents:
12976
diff
changeset
|
97 const char *str_a = luaL_checklstring(L, 1, &a); |
6cdc6923d65a
util.strbitop: Add common_prefix_bits() method
Matthew Wild <mwild1@gmail.com>
parents:
12976
diff
changeset
|
98 const char *str_b = luaL_checklstring(L, 2, &b); |
6cdc6923d65a
util.strbitop: Add common_prefix_bits() method
Matthew Wild <mwild1@gmail.com>
parents:
12976
diff
changeset
|
99 |
6cdc6923d65a
util.strbitop: Add common_prefix_bits() method
Matthew Wild <mwild1@gmail.com>
parents:
12976
diff
changeset
|
100 size_t min_len = MIN(a, b); |
6cdc6923d65a
util.strbitop: Add common_prefix_bits() method
Matthew Wild <mwild1@gmail.com>
parents:
12976
diff
changeset
|
101 |
6cdc6923d65a
util.strbitop: Add common_prefix_bits() method
Matthew Wild <mwild1@gmail.com>
parents:
12976
diff
changeset
|
102 for(i=0; i<min_len; i++) { |
6cdc6923d65a
util.strbitop: Add common_prefix_bits() method
Matthew Wild <mwild1@gmail.com>
parents:
12976
diff
changeset
|
103 if(str_a[i] != str_b[i]) { |
6cdc6923d65a
util.strbitop: Add common_prefix_bits() method
Matthew Wild <mwild1@gmail.com>
parents:
12976
diff
changeset
|
104 lua_pushinteger(L, i*8 + (clz(str_a[i] ^ str_b[i]))); |
6cdc6923d65a
util.strbitop: Add common_prefix_bits() method
Matthew Wild <mwild1@gmail.com>
parents:
12976
diff
changeset
|
105 return 1; |
6cdc6923d65a
util.strbitop: Add common_prefix_bits() method
Matthew Wild <mwild1@gmail.com>
parents:
12976
diff
changeset
|
106 } |
6cdc6923d65a
util.strbitop: Add common_prefix_bits() method
Matthew Wild <mwild1@gmail.com>
parents:
12976
diff
changeset
|
107 } |
6cdc6923d65a
util.strbitop: Add common_prefix_bits() method
Matthew Wild <mwild1@gmail.com>
parents:
12976
diff
changeset
|
108 |
6cdc6923d65a
util.strbitop: Add common_prefix_bits() method
Matthew Wild <mwild1@gmail.com>
parents:
12976
diff
changeset
|
109 lua_pushinteger(L, i*8); |
6cdc6923d65a
util.strbitop: Add common_prefix_bits() method
Matthew Wild <mwild1@gmail.com>
parents:
12976
diff
changeset
|
110 return 1; |
6cdc6923d65a
util.strbitop: Add common_prefix_bits() method
Matthew Wild <mwild1@gmail.com>
parents:
12976
diff
changeset
|
111 } |
6cdc6923d65a
util.strbitop: Add common_prefix_bits() method
Matthew Wild <mwild1@gmail.com>
parents:
12976
diff
changeset
|
112 |
12976
a187600ec7d6
util: Add compat for prosody module name change to C sources
Kim Alvefur <zash@zash.se>
parents:
12575
diff
changeset
|
113 LUA_API int luaopen_prosody_util_strbitop(lua_State *L) { |
11163
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
114 luaL_Reg exports[] = { |
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
115 { "sand", strop_and }, |
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
116 { "sor", strop_or }, |
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
117 { "sxor", strop_xor }, |
13429
6cdc6923d65a
util.strbitop: Add common_prefix_bits() method
Matthew Wild <mwild1@gmail.com>
parents:
12976
diff
changeset
|
118 { "common_prefix_bits", strop_common_prefix_bits }, |
11163
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
119 { NULL, NULL } |
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
120 }; |
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
121 |
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
122 lua_newtable(L); |
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
123 luaL_setfuncs(L, exports, 0); |
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
124 return 1; |
37a6a535343e
util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
125 } |
12976
a187600ec7d6
util: Add compat for prosody module name change to C sources
Kim Alvefur <zash@zash.se>
parents:
12575
diff
changeset
|
126 |
a187600ec7d6
util: Add compat for prosody module name change to C sources
Kim Alvefur <zash@zash.se>
parents:
12575
diff
changeset
|
127 LUA_API int luaopen_util_strbitop(lua_State *L) { |
a187600ec7d6
util: Add compat for prosody module name change to C sources
Kim Alvefur <zash@zash.se>
parents:
12575
diff
changeset
|
128 return luaopen_prosody_util_strbitop(L); |
a187600ec7d6
util: Add compat for prosody module name change to C sources
Kim Alvefur <zash@zash.se>
parents:
12575
diff
changeset
|
129 } |