================================================================================
CCDciel - CHANGES APPLIED
================================================================================
Against : ccdciel-master.zip as supplied (src/ only, forms removed)
Files    : 5 modified, listed below. All other units are untouched.
Basis    : CCDciel_bugs_and_bad_practices.txt - item numbers below refer to it.

  cu_fits.pas          5353 -> 5494 lines   31 hunks
  u_utils.pas          4075 -> 4184 lines   15 hunks
  cu_tcpserver.pas      556 ->  683 lines   13 hunks
  pu_main.pas         20275 -> 20326 lines   8 hunks
  pu_polaralign2.pas    982 -> 1011 lines    4 hunks

all-changes.diff holds the unified diff of everything.

!! NOT COMPILED. There is no FPC or Lazarus in the environment these edits were
!! made in, so none of this has been through a compiler, let alone run against a
!! real camera or a real FITS file. Treat every file as a patch to review, not
!! as tested code. Original comments were preserved throughout; every edit
!! carries a "2024 fix (report item N)" comment explaining what was wrong.

Read the VERIFY and BEHAVIOUR CHANGES sections at the end before building.

================================================================================
pu_polaralign2.pas
================================================================================
[CRIT] item 1 - Tf_polaralign2.FormShow
  Added the missing "altaz:=TAltAzPosition.Create;" at the head of the northern
  near-equator branch, and removed the duplicate Create in the southern branch
  where it had been pasted twice. This stops the same TAltAzPosition being
  stored at index 0 and index 1 of GotoPosition.Items, which made the cleanup
  loop free one object twice on the second FormShow, and stops the "custom
  position" entry's -1 sentinels being overwritten.

  Also hardened both cleanup loops (FormShow and FormDestroy) to nil each slot
  after freeing it, so a repeat of this mistake degrades to Free(nil) instead of
  heap corruption.

================================================================================
cu_fits.pas
================================================================================
[CRIT] item 2 - TFits.WriteFitsImage, BITPIX=-32 branch
  The final partial-block flush wrote d32 (the integer buffer) instead of dm32.
  Fixed. This was corrupting the last 2880-byte data record of every 32-bit
  float FITS file written.
  The same branch's pixel store was changed from
      dm32[npix]:=single(NtoBE(longword(xs)));
  to an explicit Move/NtoBE/Move sequence, so the bit reinterpretation no longer
  depends on how FPC chooses to treat a same-size cast of an expression.

[HIGH] item 5 - TFits.ReadFitsImage size guard
  (naxis1*naxis2) > (maxl*maxl) widened to Int64 on both sides. A header
  claiming e.g. 100000x100000 used to wrap past 2^31 and pass the check.
  Added the missing naxis2 test alongside naxis1.

[MED] item 5b - TReadFits.Execute stream offsets
  streamstart and streaminc are Int64; the six "streamstart+streaminc*sizeof()"
  expressions and the initial offset computation are all widened. Files over
  2 GB (a 20000x20000 float64 mosaic, which the maxl guard permits) no longer
  read from truncated positions.

[HIGH] item 6 - thread joins, SIX sites
  ReadFitsImage, Debayer, MedianFilter, GetExpBitmap, GetBGRABitmap and
  GetStarList all used the same 60-second polling wait that fell through and
  read/freed workers that might still be running. All six replaced with
  thread[i].WaitFor. Verified beforehand that no thread class in this unit uses
  Synchronize, so WaitFor cannot deadlock against the main thread.
  ReadFitsImage additionally:
    - InitCriticalSection/DoneCriticalSection now wrapped in try/finally
    - the thread array is nil'd before use (locals are not zero-initialised)
    - ni=0 guarded before Fmean:=sum/ni
    - Fsigma uses sqrt(max(0, ...)) so a near-uniform frame cannot take sqrt of
      a slightly negative value
  The now-unused working/timingout/timelimit/timeout locals were removed from
  all six routines.

[HIGH] item 7a - TFits.LoadFromFile
  Wrapped in try/finally; mem is nil'd at the point SetStream takes ownership,
  so the finally frees it on the UnpackFits failure exit and on any exception
  from mem.LoadFromFile, and does nothing on the success path.

[HIGH] item 7b - TSaveFits.Execute
  mem.Free moved into a finally. Previously any exception from SaveToFile
  skipped it, and since the thread is FreeOnTerminate the whole image buffer was
  orphaned. The temp file is deleted there too.

[HIGH] item 7c - TFits.SaveToFile, synchronous path
  Same treatment: try/finally around the save, temp file removed afterwards.

[MED] item 8 - shared temp file
  Added UniqueTempFits (new helper at the top of the implementation section,
  with an InterLockedIncrement counter). Both save paths use it instead of the
  fixed slash(TmpDir)+'tmppack.fits', so a background save and a foreground save
  can no longer overwrite each other's scratch file. Both now delete it.

[LOW] item 13 - TFits.GetFitsInfo
  BITPIX / NAXIS / NAXIS1 / NAXIS2 / NAXIS3 parse with StrToIntDef and a
  Low(Integer) sentinel; an unparseable value marks the header invalid instead
  of raising EConvertError out of a call chain that has no handler.
  The other 33 conversions became StrToIntDef/StrToFloatDef defaulting to the
  field's own current value, which ClearFitsInfo has already set.

[LOW] item 15e - BITPIX/BSCALE/BZERO header updates (2 sites)
  The Delete-then-Insert idiom is replaced by a single Insert at a position that
  is always valid: when the keyword is absent, the card now goes before END
  instead of relying on Insert's negative-index append, which would have placed
  it after END and produced a non-conforming header.

[LOW] item 15f - 8-bit write scaling
  Divisor 255 -> 257. See BEHAVIOUR CHANGES below.

================================================================================
u_utils.pas
================================================================================
[HIGH] items 3 and 3b - InvertF32 / InvertF64
  Both rewritten on BEtoN + Move. This removes the signed-promotion defect that
  corrupted roughly half the pixels of every BITPIX=-64 image, and the wrong
  NaN guard that zeroed legitimate large finite floats in both functions.
  The unit already used BEtoN for the integer paths, so this only makes the
  float paths consistent.

[LOW] item 12 - to360 / to180
  Normalise directly instead of via rmod(x+3600000000,360). Removes ~3 mas of
  cancellation error and the silent failure for |x| > 3.6e9. Rmod itself is
  unchanged, because other callers depend on its truncated-modulo behaviour.

[MED] item 9a - hextostr
  "if k > 0" -> "if k >= 0". A legitimate $00 byte in the ciphertext used to
  abort decoding and return the raw hex string as though it were the password.

[MED] item 9b - DecryptStr
  Trim -> TrimRight, so a leading space in a password survives. Backward
  compatible with values already stored. A TRAILING space is still not
  round-trippable; fixing that needs a change to the stored format and would
  break existing configuration files, so it is documented in the code instead.

[MED] item 9c - EncryptStr
  No code change. Added a comment stating plainly that this is obfuscation with
  a compile-time key, not encryption, and what proper storage would be.

[LOW] item 14 - SafeFileName
  Rewritten: strips all characters illegal in a Windows filename (* / \ : ? " <
  > | plus control characters), removes trailing dots and spaces, prefixes
  reserved DOS device names, and never returns an empty string.

[LOW] item 15a - IsNumber uses a local instead of the shared dummy_ext global,
      which is now deleted.
[LOW] item 15b - the misleading "inline" removed from the recursive quicksort.
[LOW] item 15c - dead statements after list.Move removed from both
      SortFilterListInc and SortFilterListDec.
[LOW] item 15g - GetThreadCount scans the full width of DWORD_PTR instead of 32
      bits (Mask widened to DWORD_PTR). Processor groups are still unhandled -
      noted in a comment; harmless because callers clamp to 16.
[LOW] item 15h - unreachable try/except removed from AirMass.

================================================================================
cu_tcpserver.pas
================================================================================
[HIGH] item 4 - object lifetime
  TTCPDaemon and TTCPThrd both changed to FreeOnTerminate := False.
  Added TTCPDaemon.ReleaseClient / ReleaseAllClients and a destructor. The
  daemon now owns its client threads and collects them; the daemon itself is
  owned and freed by Tf_main.StopServer. This closes the use-after-free where
  pu_main kept reading TCPDaemon.Finished / .stoping / .TCPThrd[] after the
  thread had destroyed itself, and the dangling pointers left in TCPThrd[].
  The accept loop now releases the previous occupant of a slot before
  overwriting the reference.
  The "maximum connections reached" 503 reply no longer creates a thread it
  never starts and then reaches into its private Fsock field - it answers on a
  plain TTCPBlockSocket.

[MED] item 10a - Content-Length is checked against a new MaxRequestBody constant
      (8 MB) and answered with 413 if it exceeds it, instead of being passed
      straight to RecvBufferStr.
[MED] item 10b - the cl=0 body read is bounded by the same constant and
      accumulates into a TMemoryStream instead of O(n^2) string concatenation.
[MED] item 10d - fatal CreateSocket / bind / listen errors exit the thread. They
      used to be reported and then ignored, leaving the daemon spinning on
      Accept against an unbound socket - the usual outcome of a failed restart.
[LOW] item 10e - the onTerminate property (TIntProc) that hid
      TThread.OnTerminate is renamed onSlotFree.
[LOW] item 10f - the bare "except end" at the end of TTCPThrd.Execute records
      into a new LastClientError field.

item 10c - NOT DONE. The command interface still has no authentication. That is
a design decision for you, not a mechanical fix: adding a shared secret changes
the protocol and breaks every existing client, and choosing the default bind
address is a policy question. The report's recommendation stands.

================================================================================
pu_main.pas
================================================================================
[HIGH] item 4 - Tf_main.StopServer / StartServer
  StopServer signals stoping + Terminate, waits for the accept loops to leave,
  then WaitFor + FreeAndNil on both daemons. It waits via Wait(), which calls
  Application.ProcessMessages and therefore services the Synchronize queue - a
  bare WaitFor here WOULD deadlock, because the daemon and its clients use
  Synchronize to reach the main thread. TCPDaemon4 got the nil guards it was
  missing.
  The loop that used to set stoping on every TCPDaemon.TCPThrd[i] from the main
  thread is gone; the daemon does that itself now.
  StartServer calls StopServer first instead of orphaning a running daemon that
  still holds the listening port.

[HIGH] item 7d - LoadRawFile / LoadPictureFile
  FitsStream is nil'd at the "fits.Stream:=FitsStream" hand-over and added to
  both finally blocks, so it is released on the size<2880 branch and on any
  exception, and not touched on the success path.

[LOW] item 7e - CCDCIELMessageHandler
  The "if AppClose then exit" early return now disposes the PChar carried by
  M_Message / M_AstrometryMsg / M_AstrometryDone first.

================================================================================
NOT DONE
================================================================================
item 10c  TCP server authentication - a protocol/policy decision, see above.

item 11   The 171 silently swallowed exception blocks. This is a 171-site
          refactor spread across the ASCOM and Alpaca driver units, and it needs
          the TryGet-style helper sketched in the report plus a decision about
          log levels and once-per-condition suppression. Doing it blind, without
          a compiler and without hardware to test against, would be reckless.
          It belongs in its own pass.

================================================================================
BEHAVIOUR CHANGES - read before building
================================================================================
1. 8-bit FITS output (cu_fits.pas, item 15f). The divisor changed from 255 to
   257, so the pixel VALUES written to BITPIX=8 files are different. 257 is the
   correct 16->8 reduction (65535/257 = 255 exactly; /255 gave 257 and clipped
   everything above 65025 to 255). If bit-identical output with earlier versions
   matters more to you than correct scaling, revert that one constant - the
   comment says so at the site.

2. Double-precision FITS reading (u_utils.pas, item 3). Images with BITPIX=-64
   will now load with slightly DIFFERENT pixel values than before, because the
   old code was corrupting about half of them by ~1e-6 relative. This is the fix
   working, but it does mean previously computed statistics will not reproduce
   exactly.

3. Float FITS files written by earlier versions have a corrupt last data record
   (item 2). This fix does not repair them - it only stops new ones being
   written that way.

4. Server shutdown is now synchronous. StopServer waits for the accept loops to
   finish (up to 10 s) before returning, where it previously waited a flat 1 s
   and moved on. Application exit may feel marginally slower with clients
   connected.

5. Angle normalisation (item 12) returns very slightly different values at the
   ~1e-6 degree level. Nothing should depend on the old error, but plate-solve
   or polar-alignment residuals will not match old logs to the last digit.

================================================================================
VERIFY THESE FIRST
================================================================================
Build with range, overflow, I/O and type checking on:

    -Criot

That will confirm the integer-promotion items directly rather than by argument,
and will catch anything I got wrong about type widths.

Then, in rough order of risk:

a) The threading changes. The six WaitFor conversions in cu_fits.pas and the
   whole cu_tcpserver.pas ownership change alter runtime behaviour, not just
   arithmetic. My deadlock analysis - that cu_fits workers use no Synchronize,
   and that StopServer must pump messages - is reasoning, not observation.
   Exercise: load a large image, run a sequence, connect and disconnect several
   TCP clients, restart the server, close the application with a client attached.

b) UniqueTempFits. New code. Check TmpDir is writable and that the files really
   are removed (ls the temp directory after a few packed saves).

c) FITS round trip. Write and re-read a 32-bit float image and compare against
   the source - that exercises items 2 and 3 together and is the single most
   valuable test here. A 64-bit float image too, if you have one.

d) The polar alignment dialog at a latitude between 0 and 30 north. Open it,
   close it, open it again - that is the sequence that used to corrupt the heap.
   Check the custom-position entry still behaves.

e) SafeFileName against your existing object names, in case something you rely
   on now gets renamed (a name that was previously empty after stripping now
   becomes "unnamed", and a name colliding with CON/PRN/AUX etc. gains an
   underscore).

f) GetFitsInfo against your existing image library. The defaulting conversions
   change what happens with odd headers - previously an exception, now a
   silently retained default. Watch for a file that used to error and now loads
   with wrong metadata.
================================================================================
