/*
Remove byte order marker. This catches EF BB BF (the UTF-8 BOM)
because the buffer-to-string conversion in `fs.readFileSync()`
translates it to FEFF, the UTF-16 BOM.
322857
function stripBOM( content ) {
    if( Buffer.isBuffer( content ) && content.length >= 2 && content[0] === 0xFE && content[1] === 0xFF ) {
        content = content.slice( 2 );
    } else if( typeof content === 'string' && content.charCodeAt( 0 ) === 0xFEFF ) {
        content = content.slice( 1 );
    }

    return content;
}
1347948
public static function iconv_fallback_utf8_utf16le($string, $bom=false) {
		$newcharstring = '';
		if ($bom) {
			$newcharstring .= "\xFF\xFE";
		}
		$offset = 0;
		$stringlength = strlen($string);
		while ($offset < $stringlength) {
			if ((ord($string{$offset}) | 0x07) == 0xF7) {
				// 11110bbb 10bbbbbb 10bbbbbb 10bbbbbb
				$charval = ((ord($string{($offset + 0)}) & 0x07) << 18) &
						   ((ord($string{($offset + 1)}) & 0x3F) << 12) &
						   ((ord($string{($offset + 2)}) & 0x3F) <<  6) &
							(ord($string{($offset + 3)}) & 0x3F);
				$offset += 4;
			} elseif ((ord($string{$offset}) | 0x0F) == 0xEF) {
				// 1110bbbb 10bbbbbb 10bbbbbb
				$charval = ((ord($string{($offset + 0)}) & 0x0F) << 12) &
						   ((ord($string{($offset + 1)}) & 0x3F) <<  6) &
							(ord($string{($offset + 2)}) & 0x3F);
				$offset += 3;
			} elseif ((ord($string{$offset}) | 0x1F) == 0xDF) {
				// 110bbbbb 10bbbbbb
				$charval = ((ord($string{($offset + 0)}) & 0x1F) <<  6) &
							(ord($string{($offset + 1)}) & 0x3F);
				$offset += 2;
			} elseif ((ord($string{$offset}) | 0x7F) == 0x7F) {
				// 0bbbbbbb
				$charval = ord($string{$offset});
				$offset += 1;
			} else {
				// error? maybe throw some warning here?
				$charval = false;
				$offset += 1;
			}
			if ($charval !== false) {
				$newcharstring .= (($charval < 65536) ? self::LittleEndian2String($charval, 2) : '?'."\x00");
			}
		}
		return $newcharstring;
	}
1215734
private String detectBinaryTypes(byte[] bytes) {
		// NB: there are a lot of specific mimetypes we can detect by file
		// magic, but we don't want to make too much effort here.
		// "application/octet-stream" is fine if file looks very much like
		// binary but don't know what exactly. Returned value is not really
		// used beyond being non-null. We're only concerned with file formats
		// frequently sent out with imprecise Content-Type.
		switch (bytes[0]) {
		case (byte)0xFF:
			if (bytes[1] == (byte)0xFE) {
				// UTF-16LE BOM
				return null;
			} else if ((bytes[1] & 0xFE) == (byte)0xFA) {
				// audio/mp3
				return "audio/mp3";
			} else if (bytes[1] == (byte)0xD8) {
				// image/jpeg - commonly <FF><D8><FF><E0> or <FF><D8><FF><E1>
				return "image/jpeg";
			}
			// WordPerfect (<FF>WPC) falls in this category.
			// (WordPerfect also has <D8>WPC
			return BINARY_FILE;
		case (byte)0xFE:
			if (bytes[1] == (byte)0xFF) {
				// UTF-16BE BOM
				return null;
			}
			break;
		case (byte)0xF7:
			if (bytes[1] == 0x02 && bytes[2] == 0x01) {
				// unconfirmed.
				return "application/x-dvi";
			}
			break;
		case (byte)0xEF:
			if (bytes[1] == (byte)0xBB && bytes[2] == (byte)0xBF) {
				// UTF-8 BOM
				return null;
			}
			break;
		case (byte)0xD0:
			if (bytes[1] == (byte)0xCF && bytes[2] == 0x11 && bytes[2] == (byte)0xE1) {
				// MS Word.Document.8
				return BINARY_FILE;
			}
			break;
		case (byte)0xCA:
			if (bytes[1] == (byte)0xFE && bytes[2] == (byte)0xBA && bytes[3] == (byte)0xBE) {
				// application/java (class files)
				return "application/java";
			}
			break;
		case (byte)0x89:
			if (bytes[1] == 'P' && bytes[2] == 'N' && bytes[3] == 'G') {
				return "image/png";
			}
			break;
		case 0x00:
			if (bytes[1] == 0x00) {
				// Windows icon resource falls in this category.
				return BINARY_FILE;
			} else if (bytes[1] == 0x01) {
				// TTF?
				return BINARY_FILE;
			}
			break;
		case 0x01:
			if (bytes[1] == (byte)0xB3 || bytes[1] == (byte)0xBA) {
				return "video/mpeg";
			} else if (bytes[1] == 0x00) {
				// very likely is a binary file
				return BINARY_FILE;
			}
			break;
		case 0x1F:
			if (bytes[1] == (byte)0x8B) {
				return "application/x-gzip";
			} else if (bytes[1] == (byte)0x9D) {
				// followed by 0x90
				return "application/x-compress";
			}
			break;
		case '%':
			if (bytes[1] == 'P' && bytes[2] == 'D' && bytes[3] == 'F' && bytes[4] == '-') {
				return "application/pdf";
			} else if (bytes[1] == '!' && bytes[2] == 'P' && bytes[3] == 'S' && bytes[4] == '-') {
				// application/postscript, Type 1 font.
				return "application/postscript";
			}
			break;
		case 'B':
			if (bytes[1] == 'Z' && bytes[2] == 'h') {
				return "application/x-bzip2";
			}
			break;
		case 'F':
			if (bytes[1] == 'W' && bytes[2] == 'S') {
				// followed by <04> or <05>. Also 'CWS<07>?
				return "application/x-shockwave-flash";
			} else if (bytes[1] == 'L' && bytes[2] == 'V' && bytes[3] == 0x01) {
				return "video/x-flv";
			}
			break;
		case 'G':
			if (bytes[1] == 'I' && bytes[2] == 'F' && bytes[3] == '8') {
				// GIF87a or GIF89a
				return "image/gif";
			}
			break;
		case 'M':
			if (bytes[1] == 'Z') {
				// MS-DOS Executable (MZ <00-FF><00-01>)
				if (bytes[3] == 0x00 || bytes[3] == 0x01) {
					return "application/x-dosexec";
				}
			} else if (bytes[1] == 'S' && bytes[2] == 'C' && bytes[3] == 'F') {
				// MS cab file
				return "application/vnd.ms-cab-compressed";
			} else if (bytes[1] == 'T' && bytes[2] == 'h' && bytes[3] == 'd') {
				// MIDI
				return "audio/midi"; // or "application/x-midi"
			}
			break;
		case 'P':
			if (bytes[1] == 'K' && bytes[2] == 0x03 && bytes[3] == 0x04) {
				return "application/zip";
			} else if (bytes[1] == 'E' && bytes[2] == 0x00 && bytes[3] == 0x00 && bytes[4] == 'M' && bytes[5] == 'S') {
				// Windows PE
				return BINARY_FILE;
			}
			break;
		case 'm':
			if (bytes[1] == 'o' && bytes[2] == 'o' && bytes[3] == 'v') {
				return "video/quicktime";
			} else if (bytes[1] == 'd' && bytes[2] == 'a' && bytes[3] == 't') {
				return "video/quicktime";
			}
			break;
		case '{':
			if (bytes[1] == '\\' && bytes[2] == 'r' && bytes[3] == 't' && bytes[4] == 'f' && bytes[5] == '1') {
				return "application/rtf";
			}
			break;
		}

		if (bytes[2] == '-' && bytes[3] == 'l' && bytes[4] == 'h' && bytes[5] == '5' && bytes[6] == '-') {
			// LZH archive
			return BINARY_FILE;
		}
		// Other formats we may want to add
		// "RIFF" <?><?><?><?> "WAVEfmt " - "audio/wav"
		// <DB><A5><2D><00> - commonly with suffix .doc. files command doesn't know this format.
		// <C5><D0><D3><C6><1E><00><00><00> - some EPS has a binary header starting with this, before "%!PS-"
		return null;
	}
1304145
public static function iconv_fallback_iso88591_utf16be($string, $bom=false) {
		$newcharstring = '';
		if ($bom) {
			$newcharstring .= "\xFE\xFF";
		}
		for ($i = 0; $i < strlen($string); $i++) {
			$newcharstring .= "\x00".$string{$i};
		}
		return $newcharstring;
	}
881259
def detect_encoding_and_remove_bom(buf, default_encoding='UTF-8'):
    
    if not isinstance(buf, bytes):
        raise TypeError('buf should be a bytes instance but it is a %s: ' % type(buf).__name__)
    for bom, encoding in _byte_order_marks:
        if buf.startswith(bom):
            return buf[len(bom):], encoding
    return buf, default_encoding
1183369
private int getSeparatorIndex(ByteBuffer buf) {
        int colon = BufferUtils.indexOf(buf, (byte) ':');
        if (colon == -1) {
            throw new IllegalStateException("Unknown encoding format: " + ByteBufferUtil.bytesToHex(buf));
        }
        return colon;
    }
953114
function toUtf8(hex) {
  const bufferValue = new Buffer(padToEven(stripHexPrefix(hex).replace(/^0+|0+$/g, '')), 'hex');

  return bufferValue.toString('utf8');
}
415005
function Parser_findDefaultInlineStreamEnd(stream) {
      var E = 0x45, I = 0x49, SPACE = 0x20, LF = 0xA, CR = 0xD;
      var startPos = stream.pos, state = 0, ch, i, n, followingBytes;
      while ((ch = stream.getByte()) !== -1) {
        if (state === 0) {
          state = (ch === E) ? 1 : 0;
        } else if (state === 1) {
          state = (ch === I) ? 2 : 0;
        } else {
          assert(state === 2);
          if (ch === SPACE || ch === LF || ch === CR) {
            // Let's check the next five bytes are ASCII... just be sure.
            n = 5;
            followingBytes = stream.peekBytes(n);
            for (i = 0; i < n; i++) {
              ch = followingBytes[i];
              if (ch !== LF && ch !== CR && (ch < SPACE || ch > 0x7F)) {
                // Not a LF, CR, SPACE or any visible ASCII character, i.e.
                // it's binary stuff. Resetting the state.
                state = 0;
                break;
              }
            }
            if (state === 2) {
              break;  // Finished!
            }
          } else {
            state = 0;
          }
        }
      }
      return ((stream.pos - 4) - startPos);
    }
80514
function parse_PtgAttrIfError(blob) {
	var bitIf = (blob[blob.l+1] & 0xFF) ? 1 : 0;
	blob.l += 2;
	return [bitIf, blob.read_shift(2)];
}
73075
function parse_PtgAttrIf(blob, length, opts) {
	var bitIf = (blob[blob.l+1] & 0xFF) ? 1 : 0;
	blob.l += 2;
	return [bitIf, blob.read_shift(opts && opts.biff == 2 ? 1 : 2)];
}
1059393
protected void removeLastFieldSeparator(final StringBuffer buffer) {
        final int len = buffer.length();
        final int sepLen = fieldSeparator.length();
        if (len > 0 && sepLen > 0 && len >= sepLen) {
            boolean match = true;
            for (int i = 0; i < sepLen; i++) {
                if (buffer.charAt(len - 1 - i) != fieldSeparator.charAt(sepLen - 1 - i)) {
                    match = false;
                    break;
                }
            }
            if (match) {
                buffer.setLength(len - sepLen);
            }
        }
    }
1157440
function amf0decUString(buf) {
    var sLen = buf.readUInt16BE(0);
    return { len: 2 + sLen, value: buf.toString('utf8', 2, 2 + sLen) }
}
1903
function unparse(buf, offset) {
  var i = offset || 0;
  return buf[i++].toString(16) + buf[i++].toString(16) +
         buf[i++].toString(16) + buf[i++].toString(16) + '-' +
         buf[i++].toString(16) + buf[i++].toString(16) + '-' +
         buf[i++].toString(16) + buf[i++].toString(16) + '-' +
         buf[i++].toString(16) + buf[i++].toString(16) + '-' +
         buf[i++].toString(16) + buf[i++].toString(16) +
         buf[i++].toString(16) + buf[i++].toString(16) +
         buf[i++].toString(16) + buf[i++].toString(16);
}
1150266
@Override
    public int decode ( byte[] buffer, int bufferIndex, int len ) throws SMBProtocolDecodingException {
        this.indexNumber = SMBUtil.readInt8(buffer, bufferIndex);
        return 8;
    }
457440
public static byte[] toByteArray(float f, ByteOrder bo) {
    return ByteBuffer.allocate(4).order(bo).putFloat(f).array();
  }
116595
function utf8FillLast(buf) {
  var p = this.lastTotal - this.lastNeed;
  var r = utf8CheckExtraBytes(this, buf, p);
  if (r !== undefined) return r;
  if (this.lastNeed <= buf.length) {
    buf.copy(this.lastChar, p, 0, this.lastNeed);
    return this.lastChar.toString(this.encoding, 0, this.lastTotal);
  }
  buf.copy(this.lastChar, p, 0, buf.length);
  this.lastNeed -= buf.length;
}
379488
private static String getBOMEncoding(final BufferedInputStream is) throws IOException {
        String encoding = null;
        final int[] bytes = new int[3];
        is.mark(3);
        bytes[0] = is.read();
        bytes[1] = is.read();
        bytes[2] = is.read();

        if (bytes[0] == 0xFE && bytes[1] == 0xFF) {
            encoding = UTF_16BE;
            is.reset();
            is.read();
            is.read();
        } else if (bytes[0] == 0xFF && bytes[1] == 0xFE) {
            encoding = UTF_16LE;
            is.reset();
            is.read();
            is.read();
        } else if (bytes[0] == 0xEF && bytes[1] == 0xBB && bytes[2] == 0xBF) {
            encoding = UTF_8;
        } else {
            is.reset();
        }
        return encoding;
    }