/*
** Copyright (c) 2026 Warren Young
**
** This program is free software; you can redistribute it and/or
** modify it under the terms of the Simplified BSD License (also
** known as the "2-Clause License" or "FreeBSD License".)
**
** This program is distributed in the hope that it will be useful,
** but without any warranty; without even the implied warranty of
** merchantability or fitness for a particular purpose.
**
** Author contact information:
**   https://fossil-scm.org/forum/ (user wyoung)
**
*******************************************************************************
**
** Canonical caseless filename comparison for case-sensitive off.
** Tables in casefold.h are generated by tools/casefold.tcl from the
** Unicode character database.
*/
#include "config.h"
#include "casefold.h"
#include <string.h>

/*
** Tables for filename_nocase_cmp().  Canonical decompositions and case
** folds come from the Unicode character database.  Fullwidth Latin
** letters fold to fullwidth letters, not to ASCII.
*/
typedef struct FnMap FnMap;
struct FnMap {
  unsigned int cp;
  unsigned short off;
  unsigned char n;
};
typedef struct FnCcc FnCcc;
struct FnCcc {
  unsigned int cp;
  unsigned char ccc;
};
#include "../src/casefold.h"

static int fn_find_map(const FnMap *a, int n, unsigned int cp){
  int lo = 0, hi = n-1;
  while( lo<=hi ){
    int mid = (lo+hi)/2;
    if( a[mid].cp<cp ) lo = mid+1;
    else if( a[mid].cp>cp ) hi = mid-1;
    else return mid;
  }
  return -1;
}

static int fn_find_ccc(unsigned int cp){
  int lo = 0, hi = count(aFnCcc)-1;
  while( lo<=hi ){
    int mid = (lo+hi)/2;
    if( aFnCcc[mid].cp<cp ) lo = mid+1;
    else if( aFnCcc[mid].cp>cp ) hi = mid-1;
    else return aFnCcc[mid].ccc;
  }
  return 0;
}

static int fn_hangul(unsigned int cp, unsigned int *out){
  unsigned int si, t;
  if( cp<0xAC00 || cp>0xD7A3 ) return 0;
  si = cp - 0xAC00;
  out[0] = 0x1100 + si/588;
  out[1] = 0x1161 + (si%588)/28;
  t = 0x11A7 + si%28;
  if( t==0x11A7 ) return 2;
  out[2] = t;
  return 3;
}

static int *fn_push(int *a, int *pn, int *pnAlloc, int cp){
  if( *pn>=*pnAlloc ){
    *pnAlloc = *pnAlloc ? (*pnAlloc)*2 : 32;
    a = fossil_realloc(a, (size_t)(*pnAlloc)*sizeof(int));
  }
  a[(*pn)++] = cp;
  return a;
}

static int *fn_emit_decomp(int *a, int *pn, int *pnAlloc, unsigned int cp){
  unsigned int h[3];
  int i, k, n;
  n = fn_hangul(cp, h);
  if( n ){
    for(k=0; k<n; k++) a = fn_push(a, pn, pnAlloc, (int)h[k]);
    return a;
  }
  i = fn_find_map(aFnNfdMap, count(aFnNfdMap), cp);
  if( i>=0 ){
    for(k=0; k<aFnNfdMap[i].n; k++){
      a = fn_push(a, pn, pnAlloc, (int)aFnNfdSeq[aFnNfdMap[i].off+k]);
    }
    return a;
  }
  return fn_push(a, pn, pnAlloc, (int)cp);
}

static void fn_reorder(int *a, int n){
  int done;
  do{
    int i;
    done = 1;
    for(i=1; i<n; i++){
      int cb = fn_find_ccc((unsigned int)a[i]);
      int ca = fn_find_ccc((unsigned int)a[i-1]);
      if( cb && ca>cb ){
        int t = a[i];
        a[i] = a[i-1];
        a[i-1] = t;
        done = 0;
      }
    }
  }while( !done );
}

static int fn_utf8_next(const unsigned char *z, int n, int *pi){
  int i0, i;
  unsigned int c, u;
  i0 = *pi;
  i = i0;
  if( i>=n ) return -1;
  c = z[i++];
  if( c<0x80 ){
    *pi = i;
    return (int)c;
  }
  if( (c&0xE0)==0xC0 && i<n && (z[i]&0xC0)==0x80 ){
    u = ((c&0x1F)<<6) | (z[i]&0x3F);
    if( u>=0x80 ){
      *pi = i+1;
      return (int)u;
    }
  }else if( (c&0xF0)==0xE0 && i+1<n && (z[i]&0xC0)==0x80
         && (z[i+1]&0xC0)==0x80 ){
    u = ((c&0x0F)<<12) | ((z[i]&0x3F)<<6) | (z[i+1]&0x3F);
    if( u>=0x800 && (u<0xD800 || u>0xDFFF) ){
      *pi = i+2;
      return (int)u;
    }
  }else if( (c&0xF8)==0xF0 && i+2<n && (z[i]&0xC0)==0x80
         && (z[i+1]&0xC0)==0x80 && (z[i+2]&0xC0)==0x80 ){
    u = ((c&0x07)<<18) | ((z[i]&0x3F)<<12) | ((z[i+1]&0x3F)<<6)
        | (z[i+2]&0x3F);
    if( u>=0x10000 && u<=0x10FFFF ){
      *pi = i+3;
      return (int)u;
    }
  }
  *pi = i0+1;
  return (int)z[i0];
}

static int *fn_key(const char *z, int n, int *pn){
  const unsigned char *uz = (const unsigned char *)(z ? z : "");
  int *dec = 0, *out = 0;
  int nDec = 0, nDecAlloc = 0, nOut = 0, nOutAlloc = 0;
  int i, k;
  if( n<0 ) n = (int)strlen((const char *)uz);
  i = 0;
  while( i<n ){
    int cp = fn_utf8_next(uz, n, &i);
    if( cp<0 ) break;
    dec = fn_emit_decomp(dec, &nDec, &nDecAlloc, (unsigned int)cp);
  }
  fn_reorder(dec, nDec);
  for(k=0; k<nDec; k++){
    unsigned int cp = (unsigned int)dec[k];
    int m = fn_find_map(aFnFoldMap, count(aFnFoldMap), cp);
    int j;
    if( m<0 ){
      out = fn_push(out, &nOut, &nOutAlloc, (int)cp);
    }else{
      for(j=0; j<aFnFoldMap[m].n; j++){
        out = fn_push(out, &nOut, &nOutAlloc,
                      (int)aFnFoldSeq[aFnFoldMap[m].off+j]);
      }
    }
  }
  fn_reorder(out, nOut);
  fossil_free(dec);
  *pn = nOut;
  return out;
}

static int fn_cmp_cps(const int *a, int na, const int *b, int nb){
  int i, n;
  n = na<nb ? na : nb;
  for(i=0; i<n; i++){
    if( a[i]!=b[i] ) return a[i]<b[i] ? -1 : 1;
  }
  if( na==nb ) return 0;
  return na<nb ? -1 : 1;
}

/*
** Compare two filenames for case-sensitive off.  Zero means the same
** file: equal after canonical decomposition and case folding.  nA or
** nB below zero selects a NUL-terminated string.  Fullwidth Latin
** letters fold inside the fullwidth block, so they stay distinct from
** ASCII.
*/
int filename_nocase_cmp(const char *zA, int nA, const char *zB, int nB){
  int *a, *b, na = 0, nb = 0, rc;
  if( zA==0 ){ zA = ""; nA = 0; }
  if( zB==0 ){ zB = ""; nB = 0; }
  a = fn_key(zA, nA, &na);
  b = fn_key(zB, nB, &nb);
  rc = fn_cmp_cps(a, na, b, nb);
  fossil_free(a);
  fossil_free(b);
  return rc;
}
