+static int utf8_fill( hb_work_private_t * pv )
+{
+ int bytes, conversion = 0;
+ size_t out_size;
+
+ /* Align utf8 data to beginning of the buffer so that we can
+ * fill the buffer to its maximum */
+ memmove( pv->utf8_buf, pv->utf8_buf + pv->utf8_pos, pv->utf8_end - pv->utf8_pos );
+ pv->utf8_end -= pv->utf8_pos;
+ pv->utf8_pos = 0;
+ out_size = 2048 - pv->utf8_end;
+ while( out_size )
+ {
+ char *p, *q;
+ size_t in_size, retval;
+
+ if( pv->end == pv->pos )
+ {
+ bytes = fread( pv->buf, 1, 1024, pv->file );
+ pv->pos = 0;
+ pv->end = bytes;
+ if( bytes == 0 )
+ {
+ if( conversion )
+ return 1;
+ else
+ return 0;
+ }
+ }
+
+ p = pv->buf + pv->pos;
+ q = pv->utf8_buf + pv->utf8_end;
+ in_size = pv->end - pv->pos;
+
+ retval = iconv( pv->iconv_context, &p, &in_size, &q, &out_size);
+ if( q != pv->utf8_buf + pv->utf8_pos )
+ conversion = 1;
+
+ pv->utf8_end = q - pv->utf8_buf;
+ pv->pos = p - pv->buf;
+
+ if( ( retval == -1 ) && ( errno == EINVAL ) )
+ {
+ /* Incomplete multibyte sequence, read more data */
+ memmove( pv->buf, p, pv->end - pv->pos );
+ pv->end -= pv->pos;
+ pv->pos = 0;
+ bytes = fread( pv->buf + pv->end, 1, 1024 - pv->end, pv->file );
+ if( bytes == 0 )
+ {
+ if( !conversion )
+ return 0;
+ else
+ return 1;
+ }
+ pv->end += bytes;
+ } else if ( ( retval == -1 ) && ( errno == EILSEQ ) )
+ {
+ hb_error( "Invalid byte for codeset in input, discard byte" );
+ /* Try the next byte of the input */
+ pv->pos++;
+ } else if ( ( retval == -1 ) && ( errno == E2BIG ) )
+ {
+ /* buffer full */
+ return conversion;
+ }
+ }
+ return 1;
+}
+
+static int get_line( hb_work_private_t * pv, char *buf, int size )
+{
+ int i;
+ char c;
+
+ /* Find newline in converted UTF-8 buffer */
+ for( i = 0; i < size - 1; i++ )
+ {
+ if( pv->utf8_pos >= pv->utf8_end )
+ {
+ if( !utf8_fill( pv ) )
+ {
+ if( i )
+ return 1;
+ else
+ return 0;
+ }
+ }
+ c = pv->utf8_buf[pv->utf8_pos++];
+ if( c == '\n' )
+ {
+ buf[i] = '\n';
+ buf[i+1] = '\0';
+ return 1;
+ }
+ buf[i] = c;
+ }
+ buf[0] = '\0';
+ return 1;
+}
+