diff options
| author | 童宗振 <[email protected]> | 2024-05-17 12:14:14 +0000 |
|---|---|---|
| committer | 童宗振 <[email protected]> | 2024-05-17 12:14:14 +0000 |
| commit | adfdd8368973fabbf1580edf9df21471c8c5ae04 (patch) | |
| tree | f6eae280eaef7b4d71ecb1e69f7b7a70ce6bee69 | |
| parent | 7796d0bb8b7d4ea204869e004f6a12c49d0f3d6d (diff) | |
| parent | 0f00aa537cd251ae2c7e45c88871d489ae9ffcf5 (diff) | |
Merge branch 'special_characters_for_bpf' into 'master'v0.1.11-20240517
(TSG-21096)Special characters for bpf
See merge request tsg/dp_telemetry_app!38
| -rw-r--r-- | doc/ToolUtil.java | 172 | ||||
| -rw-r--r-- | include/common.h | 18 | ||||
| -rw-r--r-- | src/maat.c | 2 |
3 files changed, 189 insertions, 3 deletions
diff --git a/doc/ToolUtil.java b/doc/ToolUtil.java new file mode 100644 index 0000000..edc3987 --- /dev/null +++ b/doc/ToolUtil.java @@ -0,0 +1,172 @@ +/**
+ * Copyright (c) 2015-2016, Chill Zhuang 庄骞 ([email protected]).
+ * <p>
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package cn.nis.ntc.utils;
+
+
+import cn.hutool.core.collection.CollectionUtil;
+import cn.hutool.core.util.ObjectUtil;
+import cn.hutool.core.util.StrUtil;
+import cn.hutool.log.Log;
+import cn.nis.ntc.utils.bean.LocationPage;
+import cn.nis.ntc.utils.exception.NtcException;
+import com.baomidou.mybatisplus.annotation.TableField;
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
+import com.csvreader.CsvReader;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import jakarta.servlet.http.HttpServletRequest;
+import jakarta.validation.ValidationException;
+import org.apache.commons.beanutils.ConvertUtils;
+import org.eclipse.collections.impl.list.fixed.ArrayAdapter;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Array;
+import java.lang.reflect.Field;
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Proxy;
+import java.math.BigDecimal;
+import java.net.URISyntaxException;
+import java.net.URLDecoder;
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
+import java.security.SecureRandom;
+import java.util.*;
+import java.util.Map.Entry;
+import java.util.function.*;
+import java.util.regex.Pattern;
+import java.util.stream.Collectors;
+
+import static cn.nis.ntc.utils.DPs.log;
+
+
+/**
+ * 高频方法集合类
+ */
+public class ToolUtil {
+
+ private static final Log logger = Log.get();
+
+ private static final Pattern POINT_PATTERN = Pattern.compile("(\\s)(\\.)(\\s)|(\\.)(\\s)|(\\s)(\\.)");
+
+ private static final Pattern POINT_PATTERN_DEEP = Pattern.compile("(\\.)");
+
+ private static final Pattern humpPattern = Pattern.compile("[A-Z]");
+
+ /**
+ * 入库字段\处理查询入库
+ *
+ * @param cfgKeywords
+ * @return
+ */
+ public static String strGetEscape(String cfgKeywords) {
+ if (StringUtils.isNotEmpty(cfgKeywords)) {
+ cfgKeywords = cfgKeywords.trim();// 首先去掉空白符号
+ // 不能输入不可见字符
+ if (containsInvisibleChar(cfgKeywords)) {
+ throw new NtcException(Code.InvisibleChar.getCode(), Code.InvisibleChar.getDesc() + ": " + cfgKeywords);
+ }
+ // \\ 需要第一个替换
+// String[] fbsArr = { "\\","$","(",")","*","+",".","?","^","|","'","%","&"};
+ cfgKeywords = cfgKeywords.replace("\\", "\\\\")
+ .replace("&", "\\&")
+ .replace(" ", "\\b");
+ }
+ return cfgKeywords;
+ }
+
+ /**
+ * 反转义特殊字符 出库
+ *
+ * @param cfgKeywords
+ * @return
+ */
+ public static String strUnEscape(String cfgKeywords, Boolean... flag) {
+ if (StringUtils.isNotEmpty(cfgKeywords)) {
+ // 不转译特殊字符
+ cfgKeywords = cfgKeywords.trim();// 首先去掉首尾空格
+ if (flag.length > 0 && flag[0]) {
+ /**因为入库前空格要转换成了\b;但如果输入的是\b而不是空格,入库后就是\\b,这时在反转时不需要处理,
+ * 在查询的时候要将\b转反换回空格,需要对\\b提前处理,否则会误转
+ * * split本身对\需要转义,最终\\b写成了8个\
+ */
+ String[] cfgKeywordAry = cfgKeywords.split("\\\\\\\\b", -1);
+// String tempKeywords = "";
+ StringBuilder tempKeywords = new StringBuilder();
+ for (String str : cfgKeywordAry) {
+ str = str.replace("\\b", " ");
+// tempKeywords+=str+"\\\\b";
+ tempKeywords.append(str);
+ tempKeywords.append("\\\\b");
+ }
+ if (StringUtils.endsWith(tempKeywords.toString(), "\\\\b")) {
+ cfgKeywords = tempKeywords.substring(0, tempKeywords.lastIndexOf("\\\\b"));
+ }
+ cfgKeywords = cfgKeywords.indexOf("\\\\b") == -1 ? cfgKeywords.replace("\\b", " ") : cfgKeywords;
+ } else {
+ String[] tempStr = cfgKeywords.split("\\\\\\\\b", -1);
+// String tempKwd = "";
+ StringBuilder tempKwd = new StringBuilder();
+ for (String str : tempStr) {
+ str = str.replace("\\b", "\b");
+// tempKwd += str+"\\\\b";
+ tempKwd.append(str);
+ tempKwd.append("\\\\b");
+ }
+ if (StringUtils.endsWith(tempKwd.toString(), "\\\\b")) {
+ cfgKeywords = tempKwd.substring(0, tempKwd.lastIndexOf("\\\\b"));
+ }
+ char[] chars = cfgKeywords.toCharArray();
+ StringBuilder sb = new StringBuilder();
+ for (char aChar : chars) {
+ if (aChar == '\b') {
+ sb.append(" ");
+ } else {
+ sb.append(aChar);
+ }
+ }
+ cfgKeywords = sb.toString();
+
+ }
+ cfgKeywords = cfgKeywords.replace("\\\\", "\\");
+ cfgKeywords = cfgKeywords.replace("\\&", "&");
+ }
+ return cfgKeywords;
+ }
+
+ /**
+ * 字符串是否包含不可见字符
+ * 包含:true
+ * 不包含:false
+ *
+ * @param content
+ * @return Boolean
+ */
+ public static Boolean containsInvisibleChar(String content) {
+ if (content != null && content.length() > 0) {
+ char[] contentCharArr = content.toCharArray();
+ for (int i = 0; i < contentCharArr.length; i++) {
+ if ((contentCharArr[i] <= 0x1F) || contentCharArr[i] == 0x7F) {
+ return true;
+ }
+ }
+ return false;
+ }
+ return false;
+ }
+}
diff --git a/include/common.h b/include/common.h index 613d337..e108dda 100644 --- a/include/common.h +++ b/include/common.h @@ -94,17 +94,31 @@ static inline char * paths_combine(const char * restrict prog_absolute_path, con return resolve_path; } -static inline void backspace_remove(const char * input, char * output) +// Special character conversion adapted to geedge CM. +// unescape only for bpf. +// Full string escaping is confusing and difficult to read. +// pcap-filter support backslash (\). but no '\b' in bpf. +static inline void bpf_str_unescape_for_cm(const char * input, char * output) { int i, j; for (i = 0, j = 0; i < strlen(input) - 1; i++) { - if (input[i] == '\\' && input[i + 1] == 'b') + if (input[i] == '\\' && input[i + 1] == '\\') + { + i += 1; + output[j++] = '\\'; + } + else if (input[i] == '\\' && input[i + 1] == 'b') { i += 1; output[j++] = ' '; } + else if (input[i] == '\\' && input[i + 1] == '&') + { + i += 1; + output[j++] = '&'; + } else { output[j++] = input[i]; @@ -219,7 +219,7 @@ device_group_or_date_center_match_end: goto end; } - backspace_remove(bpf_expr, job_desc->bpf_expr); + bpf_str_unescape_for_cm(bpf_expr, job_desc->bpf_expr); traffic_json = cJSON_Parse(traffic_link_id); if (traffic_json == NULL || !cJSON_IsArray(traffic_json)) |
