Several minor huffman-related cleanups

- In jbig2_context.cpp, rename nTemp to temp as requested in
  https://pdfium-review.googlesource.com/c/pdfium/+/156810
- Move HuffmanAssignCode() from jbig2_context.cpp to
  jbig2_huffman_table.cpp, as it's only used there after recent
  changes (pure code move, no behavior change, no edits)
- Move JBig2HuffmanCode to jbig2_huffman_table.h as it's no
  longer needed in jbig2_context.cpp after the previous bullet
- Add noexcept to CJBig2_HuffmanTable default ctor, move ctor,
  and move assignment operator

No behavior change.

Bug: 555145149
Change-Id: I5c832e2e0fb7e1b0c3782082a0440818ca092ace
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/157190
Reviewed-by: Tom Sepez <tsepez@chromium.org>
Commit-Queue: Nico Weber <thakis@chromium.org>
Reviewed-by: Nico Weber <thakis@google.com>
diff --git a/core/fxcodec/jbig2/jbig2_context.cpp b/core/fxcodec/jbig2/jbig2_context.cpp
index 04ad02d..98121cc 100644
--- a/core/fxcodec/jbig2/jbig2_context.cpp
+++ b/core/fxcodec/jbig2/jbig2_context.cpp
@@ -1237,25 +1237,25 @@
     if (runcode_decoder.DecodeAValue(&runcode_table, &runcode) != 0) {
       return CJBig2_HuffmanTable();
     }
-    uint32_t nTemp;
+    uint32_t temp;
     if (runcode < 32) {
       SBSYMCODES[i] = runcode;
       run = 0;
     } else if (runcode == 32) {
-      if (stream_->readNBits(2, &nTemp) != 0) {
+      if (stream_->readNBits(2, &temp) != 0) {
         return CJBig2_HuffmanTable();
       }
-      run = nTemp + 3;
+      run = temp + 3;
     } else if (runcode == 33) {
-      if (stream_->readNBits(3, &nTemp) != 0) {
+      if (stream_->readNBits(3, &temp) != 0) {
         return CJBig2_HuffmanTable();
       }
-      run = nTemp + 3;
+      run = temp + 3;
     } else if (runcode == 34) {
-      if (stream_->readNBits(7, &nTemp) != 0) {
+      if (stream_->readNBits(7, &temp) != 0) {
         return CJBig2_HuffmanTable();
       }
-      run = nTemp + 11;
+      run = temp + 11;
     }
     if (run > 0) {
       if (i + run > (int)SBNUMSYMS) {
@@ -1284,34 +1284,3 @@
   }
   return huffman_tables_[idx].get();
 }
-
-// static
-bool CJBig2_Context::HuffmanAssignCode(
-    pdfium::span<JBig2HuffmanCode> symcodes) {
-  int lenmax = 0;
-  for (const auto& symcode : symcodes) {
-    lenmax = std::max(symcode.codelen, lenmax);
-  }
-  std::vector<int> lencounts(lenmax + 1);
-  std::vector<int> firstcodes(lenmax + 1);
-  for (const auto& symcode : symcodes) {
-    ++lencounts[symcode.codelen];
-  }
-  lencounts[0] = 0;
-  for (int i = 1; i <= lenmax; ++i) {
-    FX_SAFE_INT32 shifted = firstcodes[i - 1];
-    shifted += lencounts[i - 1];
-    shifted <<= 1;
-    if (!shifted.IsValid()) {
-      return false;
-    }
-    firstcodes[i] = shifted.ValueOrDie();
-    int curcode = firstcodes[i];
-    for (auto& symcode : symcodes) {
-      if (symcode.codelen == i) {
-        symcode.code = curcode++;
-      }
-    }
-  }
-  return true;
-}
diff --git a/core/fxcodec/jbig2/jbig2_context.h b/core/fxcodec/jbig2/jbig2_context.h
index 71d91e1..07f0777 100644
--- a/core/fxcodec/jbig2/jbig2_context.h
+++ b/core/fxcodec/jbig2/jbig2_context.h
@@ -39,8 +39,6 @@
 
   ~CJBig2_Context();
 
-  static bool HuffmanAssignCode(pdfium::span<JBig2HuffmanCode> symcodes);
-
   bool GetFirstPage(pdfium::span<uint8_t> pBuf,
                     int32_t width,
                     int32_t height,
diff --git a/core/fxcodec/jbig2/jbig2_define.h b/core/fxcodec/jbig2/jbig2_define.h
index f2796f5..d705460 100644
--- a/core/fxcodec/jbig2/jbig2_define.h
+++ b/core/fxcodec/jbig2/jbig2_define.h
@@ -17,11 +17,6 @@
   uint8_t flags;
 };
 
-struct JBig2HuffmanCode {
-  int32_t codelen;
-  int32_t code;
-};
-
 constexpr int32_t kJBig2OOB = 1;
 
 constexpr int32_t kJBig2MaxReferredSegmentCount = 64;
diff --git a/core/fxcodec/jbig2/jbig2_huffman_table.cpp b/core/fxcodec/jbig2/jbig2_huffman_table.cpp
index f69f369..264c7ab7 100644
--- a/core/fxcodec/jbig2/jbig2_huffman_table.cpp
+++ b/core/fxcodec/jbig2/jbig2_huffman_table.cpp
@@ -6,6 +6,7 @@
 
 #include "core/fxcodec/jbig2/jbig2_huffman_table.h"
 
+#include <algorithm>
 #include <array>
 #include <iterator>
 #include <limits>
@@ -118,6 +119,35 @@
                   std::size(kHuffmanTables),
               "kNumHuffmanTables must be equal to the size of kHuffmanTables");
 
+bool HuffmanAssignCode(pdfium::span<JBig2HuffmanCode> symcodes) {
+  int lenmax = 0;
+  for (const auto& symcode : symcodes) {
+    lenmax = std::max(symcode.codelen, lenmax);
+  }
+  std::vector<int> lencounts(lenmax + 1);
+  std::vector<int> firstcodes(lenmax + 1);
+  for (const auto& symcode : symcodes) {
+    ++lencounts[symcode.codelen];
+  }
+  lencounts[0] = 0;
+  for (int i = 1; i <= lenmax; ++i) {
+    FX_SAFE_INT32 shifted = firstcodes[i - 1];
+    shifted += lencounts[i - 1];
+    shifted <<= 1;
+    if (!shifted.IsValid()) {
+      return false;
+    }
+    firstcodes[i] = shifted.ValueOrDie();
+    int curcode = firstcodes[i];
+    for (auto& symcode : symcodes) {
+      if (symcode.codelen == i) {
+        symcode.code = curcode++;
+      }
+    }
+  }
+  return true;
+}
+
 }  // namespace
 
 CJBig2_HuffmanTable::CJBig2_HuffmanTable(size_t idx) {
@@ -144,10 +174,11 @@
   ok_ = ParseFromTable({.HTOOB = false, .lines = lines});
 }
 
-CJBig2_HuffmanTable::CJBig2_HuffmanTable() = default;
-CJBig2_HuffmanTable::CJBig2_HuffmanTable(CJBig2_HuffmanTable&&) = default;
-CJBig2_HuffmanTable& CJBig2_HuffmanTable::operator=(CJBig2_HuffmanTable&&) =
+CJBig2_HuffmanTable::CJBig2_HuffmanTable() noexcept = default;
+CJBig2_HuffmanTable::CJBig2_HuffmanTable(CJBig2_HuffmanTable&&) noexcept =
     default;
+CJBig2_HuffmanTable& CJBig2_HuffmanTable::operator=(
+    CJBig2_HuffmanTable&&) noexcept = default;
 CJBig2_HuffmanTable::~CJBig2_HuffmanTable() = default;
 
 bool CJBig2_HuffmanTable::ParseFromTable(const HuffmanTable& table) {
@@ -163,7 +194,7 @@
     RANGELOW[i] = line.RANGELOW;
     ++i;
   }
-  return CJBig2_Context::HuffmanAssignCode(CODES);
+  return HuffmanAssignCode(CODES);
 }
 
 bool CJBig2_HuffmanTable::ParseFromCodedBuffer(CJBig2_BitStream* pStream) {
@@ -237,7 +268,7 @@
     ++NTEMP;
   }
 
-  return CJBig2_Context::HuffmanAssignCode(pdfium::span(CODES).first(NTEMP));
+  return HuffmanAssignCode(pdfium::span(CODES).first(NTEMP));
 }
 
 void CJBig2_HuffmanTable::ExtendBuffers(bool increment) {
diff --git a/core/fxcodec/jbig2/jbig2_huffman_table.h b/core/fxcodec/jbig2/jbig2_huffman_table.h
index a526634..47ddad1 100644
--- a/core/fxcodec/jbig2/jbig2_huffman_table.h
+++ b/core/fxcodec/jbig2/jbig2_huffman_table.h
@@ -17,6 +17,11 @@
 
 class CJBig2_BitStream;
 
+struct JBig2HuffmanCode {
+  int32_t codelen;
+  int32_t code;
+};
+
 struct JBig2TableLine {
   uint8_t PREFLEN;
   uint8_t RANGELEN;
@@ -43,9 +48,9 @@
   // prefix_lengths[i] to i.
   explicit CJBig2_HuffmanTable(pdfium::span<uint8_t> prefix_lengths);
 
-  CJBig2_HuffmanTable();
-  CJBig2_HuffmanTable(CJBig2_HuffmanTable&&);
-  CJBig2_HuffmanTable& operator=(CJBig2_HuffmanTable&&);
+  CJBig2_HuffmanTable() noexcept;
+  CJBig2_HuffmanTable(CJBig2_HuffmanTable&&) noexcept;
+  CJBig2_HuffmanTable& operator=(CJBig2_HuffmanTable&&) noexcept;
   ~CJBig2_HuffmanTable();
 
   bool IsHTOOB() const { return HTOOB; }