{"version":3,"file":"mode-ctr-gladman.mjs","names":[],"sources":["../src/mode-ctr-gladman.ts"],"sourcesContent":["import {\n  BlockCipherMode,\n} from './cipher-core';\n\nconst incWord = (word: number): number => {\n  let _word = word;\n\n  if (((word >> 24) & 0xff) === 0xff) { // overflow\n    let b1 = (word >> 16) & 0xff;\n    let b2 = (word >> 8) & 0xff;\n    let b3 = word & 0xff;\n\n    if (b1 === 0xff) { // overflow b1\n      b1 = 0;\n      if (b2 === 0xff) {\n        b2 = 0;\n        if (b3 === 0xff) {\n          b3 = 0;\n        } else {\n          b3 += 1;\n        }\n      } else {\n        b2 += 1;\n      }\n    } else {\n      b1 += 1;\n    }\n\n    _word = 0;\n    _word += (b1 << 16);\n    _word += (b2 << 8);\n    _word += b3;\n  } else {\n    _word += (0x01 << 24);\n  }\n  return _word;\n};\n\nconst incCounter = (counter: number[]): number[] => {\n  const _counter = counter;\n  _counter[0] = incWord(_counter[0]);\n\n  if (_counter[0] === 0) {\n    // encr_data in fileenc.c from  Dr Brian Gladman's counts only with DWORD j < 8\n    _counter[1] = incWord(_counter[1]);\n  }\n  return _counter;\n};\n\n/**\n * CTRGladman Encryptor/Decryptor (same operation)\n */\nclass CTRGladmanMode extends BlockCipherMode {\n  /** Counter for CTR Gladman mode */\n  _counter?: number[];\n\n  processBlock(words: number[], offset: number): void {\n    const _words = words;\n\n    // Shortcuts\n    const cipher = this._cipher;\n    const blockSize = cipher.blockSize!;\n    const iv = this._iv;\n    let counter = this._counter;\n\n    // Generate keystream\n    if (iv) {\n      this._counter = iv.slice(0);\n      counter = this._counter;\n\n      // Remove IV for subsequent blocks\n      this._iv = undefined;\n    }\n\n    incCounter(counter!);\n\n    const keystream = counter!.slice(0);\n    cipher.encryptBlock!(keystream, 0);\n\n    // Encrypt\n    for (let i = 0; i < blockSize; i += 1) {\n      _words[offset + i] ^= keystream[i];\n    }\n  }\n}\n\n/** @preserve\n * Counter block mode compatible with  Dr Brian Gladman fileenc.c\n * derived from CTR mode\n * Jan Hruby jhruby.web@gmail.com\n */\nexport class CTRGladman extends BlockCipherMode {\n  /** Counter for CTR Gladman mode */\n  _counter?: number[];\n\n  static readonly Encryptor = CTRGladmanMode;\n  static readonly Decryptor = CTRGladmanMode;\n}"],"mappings":";;;AAIA,MAAM,WAAW,SAAyB;CACxC,IAAI,QAAQ;AAEZ,MAAM,QAAQ,KAAM,SAAU,KAAM;EAClC,IAAI,KAAM,QAAQ,KAAM;EACxB,IAAI,KAAM,QAAQ,IAAK;EACvB,IAAI,KAAK,OAAO;AAEhB,MAAI,OAAO,KAAM;AACf,QAAK;AACL,OAAI,OAAO,KAAM;AACf,SAAK;AACL,QAAI,OAAO,IACT,MAAK;QAEL,OAAM;GAET,MACC,OAAM;EAET,MACC,OAAM;AAGR,UAAQ;AACR,WAAU,MAAM;AAChB,WAAU,MAAM;AAChB,WAAS;CACV,MACC,UAAU,KAAQ;AAEpB,QAAO;AACR;AAED,MAAM,cAAc,YAAgC;CAClD,MAAM,WAAW;AACjB,UAAS,KAAK,QAAQ,SAAS;AAE/B,KAAI,SAAS,OAAO,EAElB,UAAS,KAAK,QAAQ,SAAS;AAEjC,QAAO;AACR;;;;AAKD,IAAM,iBAAN,cAA6B,gBAAgB;;CAE3C;CAEA,aAAa,OAAiB,QAAsB;EAClD,MAAM,SAAS;EAGf,MAAM,SAAS,KAAK;EACpB,MAAM,YAAY,OAAO;EACzB,MAAM,KAAK,KAAK;EAChB,IAAI,UAAU,KAAK;AAGnB,MAAI,IAAI;AACN,QAAK,WAAW,GAAG,MAAM;AACzB,aAAU,KAAK;AAGf,QAAK,MAAM;EACZ;AAED,aAAW;EAEX,MAAM,YAAY,QAAS,MAAM;AACjC,SAAO,aAAc,WAAW;AAGhC,OAAK,IAAI,IAAI,GAAG,IAAI,WAAW,KAAK,EAClC,QAAO,SAAS,MAAM,UAAU;CAEnC;AACF;;;;;;AAOD,IAAa,aAAb,cAAgC,gBAAgB;;CAE9C;CAEA,OAAgB,YAAY;CAC5B,OAAgB,YAAY;AAC7B"}