15.2.2 shared utf-16 encoder 
@constructor
@implements {Encoder}
@param {boolean} utf16_be True if big-endian, false if little-endian.
@param {{fatal: boolean}} options
966366
function UTF16Encoder(utf16_be, options) {
    var fatal = options.fatal;
    /**
     * @param {Stream} stream Input stream.
     * @param {number} code_point Next code point read from the stream.
     * @return {(number|!Array.<number>)} Byte(s) to emit.
     */
    this.handler = function(stream, code_point) {
      // 1. If code point is end-of-stream, return finished.
      if (code_point === end_of_stream)
        return finished;

      // 2. If code point is in the range U+0000 to U+FFFF, return the
      // sequence resulting of converting code point to bytes using
      // utf-16be encoder flag.
      if (inRange(code_point, 0x0000, 0xFFFF))
        return convertCodeUnitToBytes(code_point, utf16_be);

      // 3. Let lead be ((code point − 0x10000) >> 10) + 0xD800,
      // converted to bytes using utf-16be encoder flag.
      var lead = convertCodeUnitToBytes(
        ((code_point - 0x10000) >> 10) + 0xD800, utf16_be);

      // 4. Let trail be ((code point − 0x10000) & 0x3FF) + 0xDC00,
      // converted to bytes using utf-16be encoder flag.
      var trail = convertCodeUnitToBytes(
        ((code_point - 0x10000) & 0x3FF) + 0xDC00, utf16_be);

      // 5. Return a byte sequence of lead followed by trail.
      return lead.concat(trail);
    };
  }
219188
function ecc200encode(&$t, &$tl, $s, $sl, $e) {
        // start in ASCII encoding mode
        $enc = 'a';
        $tp = 0;
        $sp = 0;
        $p = 0;
        $v = 0;
        $w = 0;

        $oldErrorReporting = error_reporting(0);

        //does not work, but is said to be optional, doesn't work in the JS code either because of a typo
        //        if ($e['encodingLength'] < $sl)
        //        {
        //            die("Encoding string too short");
        //            return 0;
        //        }

        // do the encoding
        while ($sp < $sl && $tp < $tl) {
            // suggest new encoding
            $newenc = $enc;
            if ($tl - $tp <= 1 && ($enc == 'c' || $enc == 't') || $tl - $tp <= 2 && $enc == 'x')
                // auto revert to ASCII
                $enc = 'a';
            //$this->debug("encoding: " + e.encoding);
            //$this->debug("encoding[sp]: " + e.encoding[sp]);
            $newenc = strtolower($e['encoding'][$sp]);
            switch ($newenc) {
                // encode character
                case 'c':
                    // C40
                case 't':
                    // Text
                case 'x': {
                    // X12
                    $out = array_fill(0, 6, 0);
                    $p = 0;
                    $s2 = "!\"#$%&'()*+,-./:;<=>?@[\\]^_\232";
                    $s3 = "";
                    if ($newenc == 'c') {
                        $en = " 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
                        $s3 = "`abcdefghijklmnopqrstuvwxyz{|}~\177";
                    }
                    if ($newenc == 't') {
                        $en = " 0123456789abcdefghijklmnopqrstuvwxyz";
                        $s3 = "`ABCDEFGHIJKLMNOPQRSTUVWXYZ{|}~\177";
                    }
                    if ($newenc == 'x')
                        $en = " 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ\r*>";
                    do {
                        $c = $s{$sp++};
                        if ($c & 0x80) {
                            if ($newenc == 'x') {
                                die("Cannot encode char in X12: " . $c);
                                return 0;
                            }
                            $c = $c & 0x7f;
                            $out[$p++] = 1;
                            $out[$p++] = 30;
                        }
                        $w = strpos($en, $c);
                        if ($w !== false)
                            $out[$p++] = ($w + 3) % 40;
                        else {
                            if ($newenc == 'x') {
                                die("Cannot encode char in X12: " . $c);
                                return 0;
                            }
                            if (ord($c) < 32) {
                                // shift 1 XXX
                                $out[$p++] = 0;
                                $out[$p++] = $c;
                            } else {
                                $w = strpos($s2, $c);
                                if ($w !== false) {
                                    // shift 2
                                    $out[$p++] = 1;
                                    $out[$p++] = $w;
                                } else {
                                    $w = strpos($s3, $c);
                                    if ($w !== false) {
                                        $out[$p++] = 2;
                                        $out[$p++] = $w;
                                    } else {
                                        die("Should not happen. Could not encode: " . $c);
                                        return 0;
                                    }
                                }
                            }
                        }
                        if ($p == 2 && $tp + 2 == $tl && $sp == $sl)
                            // shift 1 $pad at end
                            $out[$p++] = 0;
                        while ($p >= 3) {
                            $v = $out[0] * 1600 + $out[1] * 40 + $out[2] + 1;
                            if ($enc != $newenc) {
                                if ($enc == 'c' || $enc == 't' || $enc == 'x')
                                    // escape C40/text/X12
                                    $t[$tp++] = 254;
                                elseif ($enc == 'x')
                                    // escape EDIFACT
                                    $t[$tp++] = 0x7C;
                                if ($newenc == 'c')
                                    $t[$tp++] = 230;
                                if ($newenc == 't')
                                    $t[$tp++] = 239;
                                if ($newenc == 'x')
                                    $t[$tp++] = 238;
                                $enc = $newenc;
                            }
                            $t[$tp++] = ($v >> 8);
                            $t[$tp++] = ($v & 0xFF);
                            $p -= 3;
                            $out[0] = $out[3];
                            $out[1] = $out[4];
                            $out[2] = $out[5];
                        }
                    } while ($p && $sp < $sl);
                }
                break;
                case 'e': {
                    // EDIFACT
                    $out = array_fill(0, 4, 0);
                    $p = 0;
                    if ($enc != $newenc) {
                        // can only be from C40/Text/X12
                        $t[$tp++] = 254;
                        $enc = 'a';
                    }
                    while ($sp < $sl && strtolower($e['encoding'][$sp]) == 'e' && $p < 4)
                        $out[$p++] = ord($s{$sp++});
                    if ($p < 4) {
                        $out[$p++] = 0x1F;
                        $enc = 'a';
                    }
                    // termination
                    $t[$tp] = (($s{0} & 0x3F) << 2);
                    $ttp = $tp++;
                    $t[$ttp] = $t[$ttp] | (($s{1} & 0x30) >> 4);
                    $t[$tp] = (($s{1} & 0x0F) << 4);
                    if ($p == 2)
                        $tp++;
                    else {
                        $ttp = $tp++;
                        $t[$ttp] = $t[$ttp] | (($s[2] & 0x3C) >> 2);
                        $t[$tp] = (($s{2} & 0x03) << 6);
                        $ttp = $tp++;
                        $t[$ttp] = $t[$ttp] | ($s{3} & 0x3F);
                    }
                }
                break;
                case 'a':
                    // ASCII
                    if ($enc != $newenc) {
                        if ($enc == 'c' || $enc == 't' || $enc == 'x')
                            // escape C40/text/X12
                            $t[$tp++] = 254;
                        else
                            // escape EDIFACT
                            $t[$tp++] = 0x7C;
                    }
                    $enc = 'a';
                    if ($sl - $sp >= 2 && $this->isDigit($s{$sp}) && $this->isDigit($s{$sp + 1})) {
                        $t[$tp++] = (ord($s{$sp}) - ord('0')) * 10 + ord($s{$sp + 1}) - ord('0') + 130;
                        $sp += 2;
                    } elseif (ord($s{$sp}) > 127) {
                        $t[$tp++] = 235;
                        $t[$tp++] = ord($s{$sp++}) - 127;
                    } else
                        $t[$tp++] = ord($s{$sp++}) + 1;
                    break;
                case 'b': {
                    // Binary
                    // how much to encode
                    $l = 0;
                    if ($encoding) {
                        for ($p = $sp; $p < $sl && strtolower($e['encoding'][$p]) == 'b'; $p++)
                            $l++;
                    }
                    // base256
                    $t[$tp++] = 231;
                    if ($l < 250)
                        $t[$tp++] = $l;
                    else {
                        $t[$tp++] = 249 + floor(l / 250);
                        $t[$tp++] = ($l % 250);
                    }
                    while ($l-- && $tp < $tl) {
                        // see annex H
                        $t[$tp] = ord($s{$sp++}) + ((($tp + 1) * 149) % 255) + 1;
                        $tp++;
                    }
                    // reverse to ASCII at end
                    $enc = 'a';
                }
                break;
                default:
                    die("Unknown encoding: " . $newenc);
                    // failed
                    return 0;
            }
        }
        if ($tp < $tl && $enc != 'a') {
            if ($enc == 'c' || $enc == 'x' || $enc == 't')
                // escape X12/C40/Text
                $t[$tp++] = 254;
            else
                // escape EDIFACT
                $t[$tp++] = 0x7C;
        }
        if ($tp < $tl)
            // $pad
            $t[$tp++] = 129;
        while ($tp < $tl) {
            // more $padding
            // see Annex H
            $v = 129 + ((($tp + 1) * 149) % 253) + 1;
            if ($v > 254)
                $v -= 254;
            $t[$tp++] = $v;
        }

        error_reporting($oldErrorReporting);

        if ($tp > $tl || $sp < $sl) // did not fit
            return 0;
        // OK
        return $tp;
    }
150079
function amf0decString(buf) {
    var sLen = buf.readUInt16BE(1);
    return { len: 3 + sLen, value: buf.toString('utf8', 3, 3 + sLen) }
}
156680
function Decoder(options) {
  options = options || {};
  Transform.call(this, {});

  this._reset();

  // maximum length for bulk strings (512MB)
  this.maxlen = options.maxlen || 536870912;

  // create String instances for simple string replies
  this.simple = options.simple !== undefined ? options.simple : false;

  // return buffers for bulk strings
  this.buffers = options.return_buffers || options.buffers;

  // pass RESP messasges untouched
  this.raw = options.raw !== undefined ? options.raw : false;

  /* istanbul ignore next: optional dependency */
  if(hiredis) {
    this.reader = new hiredis.Reader({return_buffers: this.buffers});
  }

}
978872
function encodeIREF( encoder, id ) {
	var hi = (id & 0xF0000) >> 16,
		lo = (id & 0xFFFF)

	// Write opcode splitted inti 8-bit and 16-bit 
	encoder.log(LOG.IREF, "iref="+id);
	encoder.stream8.write( pack1b( PRIM_OP.REF | hi, false ) );
	encoder.stream16.write( pack2b( lo, false ) );
	encoder.counters.op_iref+=3;

}
1156839
function(input, options) {
		var mode;
		if (options && options.mode) {
			mode = options.mode.toLowerCase();
		}
		// “An error mode […] is either `replacement` (default) or `fatal` for a
		// decoder.”
		if (mode != 'replacement' && mode != 'fatal') {
			mode = 'replacement';
		}
		var length = input.length;
		var index = -1;
		var byteValue;
		var pointer;
		var result = '';
		while (++index < length) {
			byteValue = input.charCodeAt(index);
			// “If `byte` is in the range `0x00` to `0x7F`, return a code point whose
			// value is `byte`.”
			if (byteValue >= 0x00 && byteValue <= 0x7F) {
				result += stringFromCharCode(byteValue);
				continue;
			}
			// “Let `code point` be the index code point for `byte − 0x80` in index
			// `single-byte`.”
			pointer = byteValue - 0x80;
			if (hasOwnProperty.call(INDEX_BY_POINTER, pointer)) {
				// “Return a code point whose value is `code point`.”
				result += INDEX_BY_POINTER[pointer];
			} else {
				// “If `code point` is `null`, return `error`.”
				result += error(null, mode);
			}
		}
		return result;
	}
576029
function ISO2022JPEncoder(options) {
    var fatal = options.fatal;
    // iso-2022-jp's encoder has an associated iso-2022-jp encoder
    // state which is one of ASCII, Roman, and jis0208 (initially
    // ASCII).
    /** @enum */
    var states = {
      ASCII: 0,
      Roman: 1,
      jis0208: 2
    };
    var /** @type {number} */ iso2022jp_state = states.ASCII;
    /**
     * @param {Stream} stream Input stream.
     * @param {number} code_point Next code point read from the stream.
     * @return {(number|!Array.<number>)} Byte(s) to emit.
     */
    this.handler = function(stream, code_point) {
      // 1. If code point is end-of-stream and iso-2022-jp encoder
      // state is not ASCII, prepend code point to stream, set
      // iso-2022-jp encoder state to ASCII, and return three bytes
      // 0x1B 0x28 0x42.
      if (code_point === end_of_stream &&
          iso2022jp_state !== states.ASCII) {
        stream.prepend(code_point);
        iso2022jp_state = states.ASCII;
        return [0x1B, 0x28, 0x42];
      }

      // 2. If code point is end-of-stream and iso-2022-jp encoder
      // state is ASCII, return finished.
      if (code_point === end_of_stream && iso2022jp_state === states.ASCII)
        return finished;

      // 3. If ISO-2022-JP encoder state is ASCII or Roman, and code
      // point is U+000E, U+000F, or U+001B, return error with U+FFFD.
      if ((iso2022jp_state === states.ASCII ||
           iso2022jp_state === states.Roman) &&
          (code_point === 0x000E || code_point === 0x000F ||
           code_point === 0x001B)) {
        return encoderError(0xFFFD);
      }

      // 4. If iso-2022-jp encoder state is ASCII and code point is an
      // ASCII code point, return a byte whose value is code point.
      if (iso2022jp_state === states.ASCII &&
          isASCIICodePoint(code_point))
        return code_point;

      // 5. If iso-2022-jp encoder state is Roman and code point is an
      // ASCII code point, excluding U+005C and U+007E, or is U+00A5
      // or U+203E, run these substeps:
      if (iso2022jp_state === states.Roman &&
          ((isASCIICodePoint(code_point) &&
           code_point !== 0x005C && code_point !== 0x007E) ||
          (code_point == 0x00A5 || code_point == 0x203E))) {

        // 1. If code point is an ASCII code point, return a byte
        // whose value is code point.
        if (isASCIICodePoint(code_point))
          return code_point;

        // 2. If code point is U+00A5, return byte 0x5C.
        if (code_point === 0x00A5)
          return 0x5C;

        // 3. If code point is U+203E, return byte 0x7E.
        if (code_point === 0x203E)
          return 0x7E;
      }

      // 6. If code point is an ASCII code point, and iso-2022-jp
      // encoder state is not ASCII, prepend code point to stream, set
      // iso-2022-jp encoder state to ASCII, and return three bytes
      // 0x1B 0x28 0x42.
      if (isASCIICodePoint(code_point) &&
          iso2022jp_state !== states.ASCII) {
        stream.prepend(code_point);
        iso2022jp_state = states.ASCII;
        return [0x1B, 0x28, 0x42];
      }

      // 7. If code point is either U+00A5 or U+203E, and iso-2022-jp
      // encoder state is not Roman, prepend code point to stream, set
      // iso-2022-jp encoder state to Roman, and return three bytes
      // 0x1B 0x28 0x4A.
      if ((code_point === 0x00A5 || code_point === 0x203E) &&
          iso2022jp_state !== states.Roman) {
        stream.prepend(code_point);
        iso2022jp_state = states.Roman;
        return [0x1B, 0x28, 0x4A];
      }

      // 8. If code point is U+2212, set it to U+FF0D.
      if (code_point === 0x2212)
        code_point = 0xFF0D;

      // 9. Let pointer be the index pointer for code point in index
      // jis0208.
      var pointer = indexPointerFor(code_point, index('jis0208'));

      // 10. If pointer is null, return error with code point.
      if (pointer === null)
        return encoderError(code_point);

      // 11. If iso-2022-jp encoder state is not jis0208, prepend code
      // point to stream, set iso-2022-jp encoder state to jis0208,
      // and return three bytes 0x1B 0x24 0x42.
      if (iso2022jp_state !== states.jis0208) {
        stream.prepend(code_point);
        iso2022jp_state = states.jis0208;
        return [0x1B, 0x24, 0x42];
      }

      // 12. Let lead be floor(pointer / 94) + 0x21.
      var lead = floor(pointer / 94) + 0x21;

      // 13. Let trail be pointer % 94 + 0x21.
      var trail = pointer % 94 + 0x21;

      // 14. Return two bytes whose values are lead and trail.
      return [lead, trail];
    };
  }
295021
def to_utf8
            detect_encoding

            utf16 = self.encoding.to_utf16be(self.value)
            utf16.slice!(0, Encoding::UTF16BE::BOM.size)

            utf16.encode("utf-8", "utf-16be")
        end
189398
function( input, type, charset ) {

    type = ( type || 'Q' ).toUpperCase()
    charset = ( charset || 'utf-8' ).toUpperCase()

    if( type === 'B' ) {
      input = this.encodeBase64( input, charset )
    }

    input = this.encodeQP( input, true )
    input = [ charset, type, input ].join( '?' )

    return '=?' + input + '?='

  }
969344
function encodeBool(value) {
    let buffer = new ByteBuffer();
    if (value === null) {
        return buffer;
    }
    if (typeof value === 'string') {
        return buffer.writeVarint32(value.toLowerCase() === 'false' ? 0 : Boolean(value));
    }
    return buffer.writeVarint32(value ? 1 : 0);
}
839785
function (value, encoding) {
    encoding = (typeof encoding === 'undefined') ? 'utf8' : encoding
    var returnValue = value
    if (!Buffer.isBuffer(returnValue)) {
      if (value === null) {
        returnValue = ''
      } else if (typeof value === 'object') {
        returnValue = module.exports.objectToJson(value)
      } else if (typeof value !== 'string') {
        returnValue = '' + value
      }
      returnValue = Buffer.from(returnValue, encoding)
    }
    return returnValue
  }
1018610
function decode(data) {

    var languageCodeLength = (data[0] & 0x3F), // 6 LSBs
        languageCode = data.slice(1, 1 + languageCodeLength),
        utf16 = (data[0] & 0x80) !== 0; // assuming UTF-16BE

    // TODO need to deal with UTF in the future
    // console.log("lang " + languageCode + (utf16 ? " utf16" : " utf8"));

    return util.bytesToString(data.slice(languageCodeLength + 1));
}
1212614
function __deserializeUInt16(buffer, start, options) {
	let end = start + 2;
	let dataView = new DataView(buffer);
	let data = dataView.getUint16(start, true);
	return { anchor: end, value:options.use_native_types ? data : UInt16.from(data) };
}
1137167
function EUCKRDecoder(options) {
  var fatal = options.fatal;
  var /** @type {number} */ euckr_lead = 0x00;
  /**
   * @param {ByteInputStream} byte_pointer The byte stream to decode.
   * @return {?number} The next code point decoded, or null if not enough
   *     data exists in the input stream to decode a complete code point.
   */
  this.decode = function(byte_pointer) {
    var bite = byte_pointer.get();
    if (bite === EOF_byte && euckr_lead === 0) {
      return EOF_code_point;
    }
    if (bite === EOF_byte && euckr_lead !== 0) {
      euckr_lead = 0x00;
      return decoderError(fatal);
    }
    byte_pointer.offset(1);
    if (euckr_lead !== 0x00) {
      var lead = euckr_lead;
      var pointer = null;
      euckr_lead = 0x00;

      if (inRange(lead, 0x81, 0xC6)) {
        var temp = (26 + 26 + 126) * (lead - 0x81);
        if (inRange(bite, 0x41, 0x5A)) {
          pointer = temp + bite - 0x41;
        } else if (inRange(bite, 0x61, 0x7A)) {
          pointer = temp + 26 + bite - 0x61;
        } else if (inRange(bite, 0x81, 0xFE)) {
          pointer = temp + 26 + 26 + bite - 0x81;
        }
      }

      if (inRange(lead, 0xC7, 0xFD) && inRange(bite, 0xA1, 0xFE)) {
        pointer = (26 + 26 + 126) * (0xC7 - 0x81) + (lead - 0xC7) * 94 +
            (bite - 0xA1);
      }

      var code_point = (pointer === null) ? null :
          indexCodePointFor(pointer, indexes['euc-kr']);
      if (pointer === null) {
        byte_pointer.offset(-1);
      }
      if (code_point === null) {
        return decoderError(fatal);
      }
      return code_point;
    }

    if (inRange(bite, 0x00, 0x7F)) {
      return bite;
    }

    if (inRange(bite, 0x81, 0xFD)) {
      euckr_lead = bite;
      return null;
    }

    return decoderError(fatal);
  };
}
301782
function convertCodeUnitToBytes(code_unit, utf16be) {
    // 1. Let byte1 be code unit >> 8.
    var byte1 = code_unit >> 8;

    // 2. Let byte2 be code unit & 0x00FF.
    var byte2 = code_unit & 0x00FF;

    // 3. Then return the bytes in order:
        // utf-16be flag is set: byte1, then byte2.
    if (utf16be)
      return [byte1, byte2];
    // utf-16be flag is unset: byte2, then byte1.
    return [byte2, byte1];
  }
325124
function amf0encString(str) {
    var buf = new Buffer(3);
    buf.writeUInt8(0x02, 0);
    buf.writeUInt16BE(str.length, 1);
    return Buffer.concat([buf, new Buffer(str, 'utf8')]);
}
331893
function str2rstr_utf8(input) {
    var output = "";
    var i = -1;
    var x, y;

    while (++i < input.length) {
        /* Decode utf-16 surrogate pairs */
        x = input.charCodeAt(i);
        y = i + 1 < input.length ? input.charCodeAt(i + 1) : 0;
        if (0xD800 <= x && x <= 0xDBFF && 0xDC00 <= y && y <= 0xDFFF) {
            x = 0x10000 + ((x & 0x03FF) << 10) + (y & 0x03FF);
            i++;
        }

        /* Encode output as utf-8 */
        if (x <= 0x7F)
            output += String.fromCharCode(x);
        else if (x <= 0x7FF)
            output += String.fromCharCode(0xC0 | ((x >>> 6 ) & 0x1F),
                0x80 | ( x & 0x3F));
        else if (x <= 0xFFFF)
            output += String.fromCharCode(0xE0 | ((x >>> 12) & 0x0F),
                0x80 | ((x >>> 6 ) & 0x3F),
                0x80 | ( x & 0x3F));
        else if (x <= 0x1FFFFF)
            output += String.fromCharCode(0xF0 | ((x >>> 18) & 0x07),
                0x80 | ((x >>> 12) & 0x3F),
                0x80 | ((x >>> 6 ) & 0x3F),
                0x80 | ( x & 0x3F));
    }
    return output;
}