/** * Reads a chunk from the input stream. Returns the number of bytes * recovered from the input stream, zero on end of file or -1 in case * of BASE32 format error. */ STATIC int BASE32_ReadChunk(Base32Decode * decode, I8u chunk[4]) { /* the index where the padding begins */ int end = ((decode->flags & FLAG_PAD) ? 0 : -1); int n = 0; while (n < DECODE_CHUNK_SIZE) { int nextChar = FILE_Getc(decode->in); if (nextChar < 0) { break; } else { I8u k; Char c = (Char)nextChar; /* count this character */ decode->nread++; if ((c & (~0x7f)) != 0) { decode->flags |= FLAG_ERROR; return -1; } /* ignore whitespaces */ if (IsSpace(c)) { continue; } if (c == '=') { if (end < 0) end = n; decode->flags |= FLAG_PAD; ASSERT(decode->decodeMap[(int)c] == 0); k = 0; } else { if (decode->flags & FLAG_PAD) { /* junk after the padding */ decode->flags |= FLAG_ERROR; return -1; } /* run it through the decoding map */ k = decode->decodeMap[(int)c]; if (k == 0xff) { decode->flags |= FLAG_ERROR; return -1; } } chunk[n++] = k; } } if (n < DECODE_CHUNK_SIZE) { int k = n; while (k < DECODE_CHUNK_SIZE) chunk[k++] = 0; } if (end >= 0) { return end; } else { return n; } }
/** * The same thing as above but for 64-bit integers. Copy/pasted for * efficiency reasons. */ Bool FILE_ReadMultiByte64(File * in, I64u * result) { I64u value = 0; int i; for (i=0; i<MAX_MULTI_INT_SIZE_64; i++) { int c = FILE_Getc(in); if (c < 0) { return False; } else { value = (value << 7) | (c & 0x7f); if (!(c & 0x80)) { if (result) *result = value; return True; } } } /* stream corrupted - too many continuation bytes */ return False; }