From 520d4be5c6b8136f61811c9227515fec8937c709 Mon Sep 17 00:00:00 2001
From: vincentlu <t1341870251@gmail.com>
Date: 星期四, 07 五月 2026 10:22:56 +0800
Subject: [PATCH] #latent_code

---
 zy-acs-common/src/main/java/com/zy/acs/common/utils/QrCodeCodecSupport.java |   31 ++++++++++++++++++++++++-------
 1 files changed, 24 insertions(+), 7 deletions(-)

diff --git a/zy-acs-common/src/main/java/com/zy/acs/common/utils/QrCodeCodecSupport.java b/zy-acs-common/src/main/java/com/zy/acs/common/utils/QrCodeCodecSupport.java
index 1fef3be..f3a18b6 100644
--- a/zy-acs-common/src/main/java/com/zy/acs/common/utils/QrCodeCodecSupport.java
+++ b/zy-acs-common/src/main/java/com/zy/acs/common/utils/QrCodeCodecSupport.java
@@ -1,6 +1,7 @@
 package com.zy.acs.common.utils;
 
 import com.zy.acs.framework.common.Cools;
+import lombok.extern.slf4j.Slf4j;
 
 import java.math.BigInteger;
 import java.nio.charset.Charset;
@@ -11,6 +12,7 @@
 /**
  * 鍦扮爜缂栬В鐮佽鍒欙紝鎸夎繘绋嬬骇閰嶇疆鐢熸晥銆�
  */
+@Slf4j
 public final class QrCodeCodecSupport {
 
     private static volatile CodecConfig config = CodecConfig.defaultConfig();
@@ -31,7 +33,7 @@
         if (config.mode == Mode.NUMERIC) {
             return normalizeNumericValue(value, config.bytes, config.displayLength);
         }
-        return normalizeStringValue(value, config.bytes, config.charset);
+        return normalizeStringValue(value, config.bytes, config.displayLength, config.charset);
     }
 
     public static String decode(byte[] bytes, int pos) {
@@ -69,8 +71,11 @@
     }
 
     private static byte[] toFixedStringBytes(String qrCode, int size, Charset charset) {
-        String value = normalizeStringValue(qrCode, size, charset);
-        return value.getBytes(charset);
+        String value = normalizeStringValue(qrCode, size, config.displayLength, charset);
+        byte[] raw = value.getBytes(charset);
+        byte[] result = new byte[size];
+        System.arraycopy(raw, 0, result, 0, raw.length);
+        return result;
     }
 
     private static String normalizeNumericValue(String qrCode, int size, int displayLength) {
@@ -86,11 +91,15 @@
         return Utils.zeroFill(numeric.toString(), displayLength);
     }
 
-    private static String normalizeStringValue(String qrCode, int size, Charset charset) {
+    private static String normalizeStringValue(String qrCode, int size, int displayLength, Charset charset) {
         String value = requireNonEmpty(qrCode);
         byte[] raw = value.getBytes(charset);
-        if (raw.length != size) {
-            throw new IllegalArgumentException("qrCode byte length must be " + size + ": " + qrCode);
+        if (raw.length > size) {
+            log.error("qrCode exceeds configured byte size: " + qrCode);
+//            throw new IllegalArgumentException("qrCode byte length exceeds protocol size " + size + ": " + qrCode);
+        }
+        if (displayLength > 0 && raw.length != displayLength) {
+//            throw new IllegalArgumentException("qrCode byte length must be " + displayLength + ": " + qrCode);
         }
         return value;
     }
@@ -157,7 +166,15 @@
         static CodecConfig of(String mode, Integer bytes, Integer displayLength, String charsetName) {
             Mode resolvedMode = Mode.of(mode);
             int resolvedBytes = bytes == null || bytes <= 0 ? 4 : bytes;
-            int resolvedDisplayLength = displayLength == null || displayLength <= 0 ? 8 : displayLength;
+            int resolvedDisplayLength;
+            if (displayLength == null || displayLength <= 0) {
+                resolvedDisplayLength = resolvedMode == Mode.STRING ? resolvedBytes : 8;
+            } else {
+                resolvedDisplayLength = displayLength;
+            }
+            if (resolvedMode == Mode.STRING && resolvedDisplayLength > resolvedBytes) {
+                throw new IllegalArgumentException("qrCode display length exceeds protocol size");
+            }
             Charset resolvedCharset = charsetName == null || charsetName.trim().isEmpty()
                     ? StandardCharsets.US_ASCII
                     : Charset.forName(charsetName.trim());

--
Gitblit v1.9.1