private int MakeDecodeTable(long nsyms, long nbits, int[] length, int[] table) {
		int sym;
		long leaf;
		int bit_num = 1;
		long fill;
		long pos = 0; /* the current position in the decode table */
		long table_mask = (1 << (int)nbits);
		long bit_mask = table_mask >> 1; /* don't do 0 length codes */
		long next_symbol = bit_mask; /* base of allocation for long codes */

		/* fill entries for codes short enough for a direct mapping */
		while (bit_num <= nbits) {
			for (sym = 0; sym < nsyms; sym++) {
				if (length[sym] == bit_num) {
					leaf = pos;

					if ((pos += bit_mask) > table_mask) return 1; /* table overrun */

					/* fill all possible lookups of this symbol with the symbol itself */
					fill = bit_mask;
					while (fill-- > 0)
						table[(int)leaf++] = sym;
				}
			}
			bit_mask >>= 1;
			bit_num++;
		}

		/* if there are any codes longer than nbits */
		if (pos != table_mask) {
			/* clear the remainder of the table */
			for (sym = (int)pos; sym < table_mask; sym++)
				table[sym] = 0;

			/* give ourselves room for codes to grow by up to 16 more bits */
			pos <<= 16;
			table_mask <<= 16;
			bit_mask = 1 << 15;

			while (bit_num <= 16) {
				for (sym = 0; sym < nsyms; sym++) {
					if (length[sym] == bit_num) {
						leaf = pos >> 16;
						for (fill = 0; fill < bit_num - nbits; fill++) {
							/* if this path hasn't been taken yet, 'allocate' two entries */
							if (table[(int)leaf] == 0) {
								table[(int)(next_symbol << 1)] = 0;
								table[(int)((next_symbol << 1) + 1)] = 0;
								table[(int)leaf] = (int)(next_symbol++);
							}
							/* follow the path and select either left or right for next bit */
							leaf = (table[(int)leaf] << 1);
							if (((pos >> (int)(15 - fill)) & 1) == 1) leaf++;
						}
						table[(int)leaf] = sym;

						if ((pos += bit_mask) > table_mask) return 1;
					}
				}
				bit_mask >>= 1;
				bit_num++;
			}
		}

		/* full talbe? */
		if (pos == table_mask) return 0;

		/* either erroneous table, or all elements are 0 - let's find out. */
		for (sym = 0; sym < nsyms; sym++)
			if (length[sym] != 0) return 1;
		return 0;
	}