if (pktin.maxlen < packetlen) { pktin.maxlen = packetlen; pktin.data = (pktin.data == NULL ? malloc(packetlen+APIEXTRA) : realloc(pktin.data, packetlen+APIEXTRA));
if (pktin.maxlen < packetlen+maclen) { pktin.maxlen = packetlen+maclen; pktin.data = (pktin.data == NULL ? malloc(pktin.maxlen+APIEXTRA) : realloc(pktin.data, pktin.maxlen+APIEXTRA));
static int ssh2_rdpkt(unsigned char **data, int *datalen){    static long len, pad, payload, packetlen, maclen;    static int i;    static int cipherblk;    static unsigned long incoming_sequence = 0;    crBegin;next_packet:    pktin.type = 0;    pktin.length = 0;    if (cipher)        cipherblk = cipher->blksize;    else        cipherblk = 8;    if (cipherblk < 8)        cipherblk = 8;    if (pktin.maxlen < cipherblk) {	pktin.maxlen = cipherblk;	pktin.data = (pktin.data == NULL ? malloc(cipherblk+APIEXTRA) :	              realloc(pktin.data, cipherblk+APIEXTRA));	if (!pktin.data)	    fatalbox("Out of memory");    }    /*     * Acquire and decrypt the first block of the packet. This will     * contain the length and padding details.     */     for (i = len = 0; i < cipherblk; i++) {	while ((*datalen) == 0)	    crReturn(cipherblk-i);	pktin.data[i] = *(*data)++;        (*datalen)--;    }#ifdef FWHACK    if (!memcmp(pktin.data, "Remo", 4)) {/* "Remo"te server has closed ... */        /* FIXME */    }#endif    if (sccipher)        sccipher->decrypt(pktin.data, cipherblk);    /*     * Now get the length and padding figures.     */    len = GET_32BIT(pktin.data);    pad = pktin.data[4];    /*     * This enables us to deduce the payload length.     */    payload = len - pad - 1;    pktin.length = payload + 5;    /*     * So now we can work out the total packet length.     */    packetlen = len + 4;    maclen = scmac ? scmac->len : 0;    /*     * Adjust memory allocation if packet is too big.     */    if (pktin.maxlen < packetlen) {	pktin.maxlen = packetlen;	pktin.data = (pktin.data == NULL ? malloc(packetlen+APIEXTRA) :	              realloc(pktin.data, packetlen+APIEXTRA));	if (!pktin.data)	    fatalbox("Out of memory");    }    /*     * Read and decrypt the remainder of the packet.     */    for (i = cipherblk; i < packetlen + maclen; i++) {	while ((*datalen) == 0)	    crReturn(packetlen + maclen - i);	pktin.data[i] = *(*data)++;        (*datalen)--;    }    /* Decrypt everything _except_ the MAC. */    if (sccipher)        sccipher->decrypt(pktin.data + cipherblk, packetlen - cipherblk);#if 0    debug(("Got packet len=%d pad=%d\r\n", len, pad));    for (i = 0; i < packetlen; i++)        debug(("  %02x", (unsigned char)pktin.data[i]));    debug(("\r\n"));#endif    /*     * Check the MAC.     */    if (scmac && !scmac->verify(pktin.data, len+4, incoming_sequence))	fatalbox("Incorrect MAC received on packet");    incoming_sequence++;               /* whether or not we MACed */    pktin.savedpos = 6;    pktin.type = pktin.data[5];    if (pktin.type == SSH2_MSG_IGNORE || pktin.type == SSH2_MSG_DEBUG)        goto next_packet;              /* FIXME: print DEBUG message */    crFinish(0);}