content: package com.r00t.response;
  
  import com.r00t.response.NCDataFileReader.NCDataInfo;
  import nc.vo.framework.rsa.DES;
  import nc.vo.framework.rsa.DESInputStream;
  
  import java.io.*;
  import java.util.zip.Inflater;
  import java.util.zip.InflaterInputStream;
  
  public class NCDataProcessor {
  
      /**
       * Process NC data (decrypt + decompress)
       */
      public static byte[] processNCData(NCDataInfo info) throws IOException {
          InputStream dataStream = new ByteArrayInputStream(info.rawData);
  
          try {
              // 1. Decrypt processing
              if (info.encrypted) {
                  dataStream = new SafeDESInputStream(dataStream, new DES(232L));
              }
  
              // 2. Decompress processing
              if (info.compressed) {
                  dataStream = new InflaterInputStream(dataStream, new Inflater());
              }
  
              // 3. Read processed data
              ByteArrayOutputStream baos = new ByteArrayOutputStream();
              byte[] buffer = new byte[1024];
              int bytesRead;
  
              while ((bytesRead = dataStream.read(buffer)) != -1) {
                  baos.write(buffer, 0, bytesRead);
              }
  
              return baos.toByteArray();
  
          } catch (EOFException e) {
              System.err.println("Encountered end of file during DES decryption, data may be incomplete");
              System.err.println("Original data length: " + info.rawData.length);
              System.err.println("First 16 bytes of data: " + bytesToHex(info.rawData, 0, 16));
              throw e;
          } finally {
              if (dataStream != null) {
                  try {
                      dataStream.close();
                  } catch (IOException e) {
                      // Ignore close exception
                  }
              }
          }
      }
  
      /**
       * Deserialize Java object
       */
      public static Object deserializeNCData(byte[] processedData) throws IOException, ClassNotFoundException {
          try (ByteArrayInputStream bais = new ByteArrayInputStream(processedData);
               ObjectInputStream ois = new ObjectInputStream(bais)) {
              return ois.readObject();
          }
      }
  
      /**
       * Safe DES input stream wrapper
       */
      private static class SafeDESInputStream extends InputStream {
          private DESInputStream desStream;
          private boolean hasError = false;
  
          public SafeDESInputStream(InputStream in, DES des) {
              this.desStream = new DESInputStream(in, des);
          }
  
          @Override
          public int read() throws IOException {
              if (hasError) {
                  return -1;
              }
  
              try {
                  return desStream.read();
              } catch (EOFException e) {
                  hasError = true;
                  return -1;
              }
          }
  
          @Override
          public int read(byte[] b, int off, int len) throws IOException {
              if (hasError) {
                  return -1;
              }
  
              try {
                  return desStream.read(b, off, len);
              } catch (EOFException e) {
                  hasError = true;
                  return -1;
              }
          }
  
          @Override
          public void close() throws IOException {
              if (desStream != null) {
                  desStream.close();
              }
          }
      }
  
      /**
       * Convert byte array to hexadecimal string
       */
      private static String bytesToHex(byte[] bytes, int offset, int length) {
          StringBuilder sb = new StringBuilder();
          for (int i = offset; i < Math.min(offset + length, bytes.length); i++) {
              sb.append(String.format("%02X ", bytes[i]));
          }
          return sb.toString();
      }
  }