Back to index

php5  5.3.10
Classes | Defines | Typedefs | Functions
mbfilter.h File Reference
#include "mbfl_defs.h"
#include "mbfl_consts.h"
#include "mbfl_allocators.h"
#include "mbfl_encoding.h"
#include "mbfl_language.h"
#include "mbfl_string.h"
#include "mbfl_convert.h"
#include "mbfl_ident.h"

Go to the source code of this file.

Classes

struct  _mbfl_buffer_converter
struct  _mbfl_encoding_detector

Defines

#define MBFL_VERSION_MAJOR   1
#define MBFL_VERSION_MINOR   1
#define MBFL_VERSION_TEENY   0
#define MBFL_OUTPUTFILTER_ILLEGAL_MODE_NONE   0
#define MBFL_OUTPUTFILTER_ILLEGAL_MODE_CHAR   1
#define MBFL_OUTPUTFILTER_ILLEGAL_MODE_LONG   2
#define MBFL_OUTPUTFILTER_ILLEGAL_MODE_ENTITY   3

Typedefs

typedef struct _mbfl_buffer_converter
typedef struct _mbfl_encoding_detector

Functions

MBFLAPI mbfl_buffer_converter * mbfl_buffer_converter_new (enum mbfl_no_encoding from, enum mbfl_no_encoding to, int buf_initsz)
MBFLAPI void mbfl_buffer_converter_delete (mbfl_buffer_converter *convd)
MBFLAPI void mbfl_buffer_converter_reset (mbfl_buffer_converter *convd)
MBFLAPI int mbfl_buffer_converter_illegal_mode (mbfl_buffer_converter *convd, int mode)
MBFLAPI int mbfl_buffer_converter_illegal_substchar (mbfl_buffer_converter *convd, int substchar)
MBFLAPI int mbfl_buffer_converter_strncat (mbfl_buffer_converter *convd, const unsigned char *p, int n)
MBFLAPI int mbfl_buffer_converter_feed (mbfl_buffer_converter *convd, mbfl_string *string)
MBFLAPI int mbfl_buffer_converter_flush (mbfl_buffer_converter *convd)
MBFLAPI mbfl_stringmbfl_buffer_converter_getbuffer (mbfl_buffer_converter *convd, mbfl_string *result)
MBFLAPI mbfl_stringmbfl_buffer_converter_result (mbfl_buffer_converter *convd, mbfl_string *result)
MBFLAPI mbfl_stringmbfl_buffer_converter_feed_result (mbfl_buffer_converter *convd, mbfl_string *string, mbfl_string *result)
MBFLAPI int mbfl_buffer_illegalchars (mbfl_buffer_converter *convd)
MBFLAPI mbfl_encoding_detector * mbfl_encoding_detector_new (enum mbfl_no_encoding *elist, int elistsz, int strict)
MBFLAPI void mbfl_encoding_detector_delete (mbfl_encoding_detector *identd)
MBFLAPI int mbfl_encoding_detector_feed (mbfl_encoding_detector *identd, mbfl_string *string)
MBFLAPI enum mbfl_no_encoding mbfl_encoding_detector_judge (mbfl_encoding_detector *identd)
MBFLAPI mbfl_stringmbfl_convert_encoding (mbfl_string *string, mbfl_string *result, enum mbfl_no_encoding toenc)
MBFLAPI const mbfl_encodingmbfl_identify_encoding (mbfl_string *string, enum mbfl_no_encoding *elist, int elistsz, int strict)
MBFLAPI const char * mbfl_identify_encoding_name (mbfl_string *string, enum mbfl_no_encoding *elist, int elistsz, int strict)
MBFLAPI enum mbfl_no_encoding mbfl_identify_encoding_no (mbfl_string *string, enum mbfl_no_encoding *elist, int elistsz, int strict)
MBFLAPI int mbfl_strlen (mbfl_string *string)
MBFLAPI int mbfl_oddlen (mbfl_string *string)
MBFLAPI int mbfl_strpos (mbfl_string *haystack, mbfl_string *needle, int offset, int reverse)
MBFLAPI int mbfl_substr_count (mbfl_string *haystack, mbfl_string *needle)
MBFLAPI mbfl_stringmbfl_substr (mbfl_string *string, mbfl_string *result, int from, int length)
MBFLAPI mbfl_stringmbfl_strcut (mbfl_string *string, mbfl_string *result, int from, int length)
MBFLAPI int mbfl_strwidth (mbfl_string *string)
MBFLAPI mbfl_stringmbfl_strimwidth (mbfl_string *string, mbfl_string *marker, mbfl_string *result, int from, int width)
MBFLAPI struct
mime_header_encoder_data
mime_header_encoder_new (enum mbfl_no_encoding incode, enum mbfl_no_encoding outcode, enum mbfl_no_encoding encoding)
MBFLAPI void mime_header_encoder_delete (struct mime_header_encoder_data *pe)
MBFLAPI int mime_header_encoder_feed (int c, struct mime_header_encoder_data *pe)
MBFLAPI mbfl_stringmime_header_encoder_result (struct mime_header_encoder_data *pe, mbfl_string *result)
MBFLAPI mbfl_stringmbfl_mime_header_encode (mbfl_string *string, mbfl_string *result, enum mbfl_no_encoding outcode, enum mbfl_no_encoding encoding, const char *linefeed, int indent)
MBFLAPI struct
mime_header_decoder_data
mime_header_decoder_new (enum mbfl_no_encoding outcode)
MBFLAPI void mime_header_decoder_delete (struct mime_header_decoder_data *pd)
MBFLAPI int mime_header_decoder_feed (int c, struct mime_header_decoder_data *pd)
MBFLAPI mbfl_stringmime_header_decoder_result (struct mime_header_decoder_data *pd, mbfl_string *result)
MBFLAPI mbfl_stringmbfl_mime_header_decode (mbfl_string *string, mbfl_string *result, enum mbfl_no_encoding outcode)
MBFLAPI mbfl_stringmbfl_html_numeric_entity (mbfl_string *string, mbfl_string *result, int *convmap, int mapsize, int type)
MBFLAPI mbfl_stringmbfl_ja_jp_hantozen (mbfl_string *string, mbfl_string *result, int mode)

Class Documentation

struct _mbfl_buffer_converter

Definition at line 121 of file mbfilter.h.

Collaboration diagram for _mbfl_buffer_converter:
Class Members
mbfl_memory_device device
mbfl_convert_filter * filter1
mbfl_convert_filter * filter2
const mbfl_encoding * from
const mbfl_encoding * to
struct _mbfl_encoding_detector

Definition at line 147 of file mbfilter.h.

Class Members
mbfl_identify_filter ** filter_list
int filter_list_size
int strict

Define Documentation

Definition at line 112 of file mbfilter.h.

Definition at line 114 of file mbfilter.h.

Definition at line 113 of file mbfilter.h.

Definition at line 111 of file mbfilter.h.

#define MBFL_VERSION_MAJOR   1

Definition at line 104 of file mbfilter.h.

#define MBFL_VERSION_MINOR   1

Definition at line 105 of file mbfilter.h.

#define MBFL_VERSION_TEENY   0

Definition at line 106 of file mbfilter.h.


Typedef Documentation

typedef struct _mbfl_buffer_converter

Definition at line 119 of file mbfilter.h.

typedef struct _mbfl_encoding_detector

Definition at line 145 of file mbfilter.h.


Function Documentation

MBFLAPI void mbfl_buffer_converter_delete ( mbfl_buffer_converter *  convd)

Definition at line 177 of file mbfilter.c.

{
       if (convd != NULL) {
              if (convd->filter1) {
                     mbfl_convert_filter_delete(convd->filter1);
              }
              if (convd->filter2) {
                     mbfl_convert_filter_delete(convd->filter2);
              }
              mbfl_memory_device_clear(&convd->device);
              mbfl_free((void*)convd);
       }
}

Here is the call graph for this function:

Here is the caller graph for this function:

MBFLAPI int mbfl_buffer_converter_feed ( mbfl_buffer_converter *  convd,
mbfl_string string 
)

Definition at line 252 of file mbfilter.c.

{
       int n;
       unsigned char *p;
       mbfl_convert_filter *filter;
       int (*filter_function)(int c, mbfl_convert_filter *filter);

       if (convd == NULL || string == NULL) {
              return -1;
       }
       mbfl_memory_device_realloc(&convd->device, convd->device.pos + string->len, string->len/4);
       /* feed data */
       n = string->len;
       p = string->val;
       filter = convd->filter1;
       if (filter != NULL) {
              filter_function = filter->filter_function;
              while (n > 0) {
                     if ((*filter_function)(*p++, filter) < 0) {
                            return -1;
                     }
                     n--;
              }
       }

       return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

MBFLAPI mbfl_string* mbfl_buffer_converter_feed_result ( mbfl_buffer_converter *  convd,
mbfl_string string,
mbfl_string result 
)

Definition at line 322 of file mbfilter.c.

{
       if (convd == NULL || string == NULL || result == NULL) {
              return NULL;
       }
       mbfl_buffer_converter_feed(convd, string);
       if (convd->filter1 != NULL) {
              mbfl_convert_filter_flush(convd->filter1);
       }
       if (convd->filter2 != NULL) {
              mbfl_convert_filter_flush(convd->filter2);
       }
       result->no_encoding = convd->to->no_encoding;
       return mbfl_memory_device_result(&convd->device, result);
}

Here is the call graph for this function:

Here is the caller graph for this function:

MBFLAPI int mbfl_buffer_converter_flush ( mbfl_buffer_converter *  convd)

Definition at line 281 of file mbfilter.c.

{
       if (convd == NULL) {
              return -1;
       }

       if (convd->filter1 != NULL) {
              mbfl_convert_filter_flush(convd->filter1);
       }
       if (convd->filter2 != NULL) {
              mbfl_convert_filter_flush(convd->filter2);
       }

       return 0;
}

Here is the call graph for this function:

MBFLAPI mbfl_string* mbfl_buffer_converter_getbuffer ( mbfl_buffer_converter *  convd,
mbfl_string result 
)

Definition at line 298 of file mbfilter.c.

{
       if (convd != NULL && result != NULL && convd->device.buffer != NULL) {
              result->no_encoding = convd->to->no_encoding;
              result->val = convd->device.buffer;
              result->len = convd->device.pos;
       } else {
              result = NULL;
       }

       return result;
}
MBFLAPI int mbfl_buffer_converter_illegal_mode ( mbfl_buffer_converter *  convd,
int  mode 
)

Definition at line 198 of file mbfilter.c.

{
       if (convd != NULL) {
              if (convd->filter2 != NULL) {
                     convd->filter2->illegal_mode = mode;
              } else if (convd->filter1 != NULL) {
                     convd->filter1->illegal_mode = mode;
              } else {
                     return 0;
              }
       }

       return 1;
}
MBFLAPI int mbfl_buffer_converter_illegal_substchar ( mbfl_buffer_converter *  convd,
int  substchar 
)

Definition at line 214 of file mbfilter.c.

{
       if (convd != NULL) {
              if (convd->filter2 != NULL) {
                     convd->filter2->illegal_substchar = substchar;
              } else if (convd->filter1 != NULL) {
                     convd->filter1->illegal_substchar = substchar;
              } else {
                     return 0;
              }
       }

       return 1;
}
MBFLAPI mbfl_buffer_converter* mbfl_buffer_converter_new ( enum mbfl_no_encoding  from,
enum mbfl_no_encoding  to,
int  buf_initsz 
)

Definition at line 126 of file mbfilter.c.

{
       mbfl_buffer_converter *convd;

       /* allocate */
       convd = (mbfl_buffer_converter*)mbfl_malloc(sizeof (mbfl_buffer_converter));
       if (convd == NULL) {
              return NULL;
       }

       /* initialize */
       convd->from = mbfl_no2encoding(from);
       convd->to = mbfl_no2encoding(to);
       if (convd->from == NULL) {
              convd->from = &mbfl_encoding_pass;
       }
       if (convd->to == NULL) {
              convd->to = &mbfl_encoding_pass;
       }

       /* create convert filter */
       convd->filter1 = NULL;
       convd->filter2 = NULL;
       if (mbfl_convert_filter_get_vtbl(convd->from->no_encoding, convd->to->no_encoding) != NULL) {
              convd->filter1 = mbfl_convert_filter_new(convd->from->no_encoding, convd->to->no_encoding, mbfl_memory_device_output, NULL, &convd->device);
       } else {
              convd->filter2 = mbfl_convert_filter_new(mbfl_no_encoding_wchar, convd->to->no_encoding, mbfl_memory_device_output, NULL, &convd->device);
              if (convd->filter2 != NULL) {
                     convd->filter1 = mbfl_convert_filter_new(convd->from->no_encoding,
                                   mbfl_no_encoding_wchar,
                                   (int (*)(int, void*))convd->filter2->filter_function,
                                   (int (*)(void*))convd->filter2->filter_flush,
                                   convd->filter2);
                     if (convd->filter1 == NULL) {
                            mbfl_convert_filter_delete(convd->filter2);
                     }
              }
       }
       if (convd->filter1 == NULL) {
              return NULL;
       }

       mbfl_memory_device_init(&convd->device, buf_initsz, buf_initsz/4);

       return convd;
}

Here is the call graph for this function:

Here is the caller graph for this function:

MBFLAPI void mbfl_buffer_converter_reset ( mbfl_buffer_converter *  convd)

Definition at line 192 of file mbfilter.c.

{
       mbfl_memory_device_reset(&convd->device);
}

Here is the call graph for this function:

MBFLAPI mbfl_string* mbfl_buffer_converter_result ( mbfl_buffer_converter *  convd,
mbfl_string result 
)

Definition at line 312 of file mbfilter.c.

{
       if (convd == NULL || result == NULL) {
              return NULL;
       }
       result->no_encoding = convd->to->no_encoding;
       return mbfl_memory_device_result(&convd->device, result);
}

Here is the call graph for this function:

MBFLAPI int mbfl_buffer_converter_strncat ( mbfl_buffer_converter *  convd,
const unsigned char *  p,
int  n 
)

Definition at line 230 of file mbfilter.c.

{
       mbfl_convert_filter *filter;
       int (*filter_function)(int c, mbfl_convert_filter *filter);

       if (convd != NULL && p != NULL) {
              filter = convd->filter1;
              if (filter != NULL) {
                     filter_function = filter->filter_function;
                     while (n > 0) {
                            if ((*filter_function)(*p++, filter) < 0) {
                                   break;
                            }
                            n--;
                     }
              }
       }

       return n;
}
MBFLAPI int mbfl_buffer_illegalchars ( mbfl_buffer_converter *  convd)

Definition at line 339 of file mbfilter.c.

{
       int num_illegalchars = 0;

       if (convd == NULL) {
              return 0;
       }

       if (convd->filter1 != NULL) {
              num_illegalchars += convd->filter1->num_illegalchar;
       }

       if (convd->filter2 != NULL) {
              num_illegalchars += convd->filter2->num_illegalchar;
       }

       return (num_illegalchars);
}

Definition at line 498 of file mbfilter.c.

{
       int n;
       unsigned char *p;
       const mbfl_encoding *encoding;
       mbfl_memory_device device;
       mbfl_convert_filter *filter1;
       mbfl_convert_filter *filter2;

       /* initialize */
       encoding = mbfl_no2encoding(toenc);
       if (encoding == NULL || string == NULL || result == NULL) {
              return NULL;
       }

       filter1 = NULL;
       filter2 = NULL;
       if (mbfl_convert_filter_get_vtbl(string->no_encoding, toenc) != NULL) {
              filter1 = mbfl_convert_filter_new(string->no_encoding, toenc, mbfl_memory_device_output, 0, &device);
       } else {
              filter2 = mbfl_convert_filter_new(mbfl_no_encoding_wchar, toenc, mbfl_memory_device_output, 0, &device);
              if (filter2 != NULL) {
                     filter1 = mbfl_convert_filter_new(string->no_encoding, mbfl_no_encoding_wchar, (int (*)(int, void*))filter2->filter_function, NULL, filter2);
                     if (filter1 == NULL) {
                            mbfl_convert_filter_delete(filter2);
                     }
              }
       }
       if (filter1 == NULL) {
              return NULL;
       }

       if (filter2 != NULL) {
              filter2->illegal_mode = MBFL_OUTPUTFILTER_ILLEGAL_MODE_CHAR;
              filter2->illegal_substchar = 0x3f;        /* '?' */
       }

       mbfl_memory_device_init(&device, string->len, (string->len >> 2) + 8);

       /* feed data */
       n = string->len;
       p = string->val;
       if (p != NULL) {
              while (n > 0) {
                     if ((*filter1->filter_function)(*p++, filter1) < 0) {
                            break;
                     }
                     n--;
              }
       }

       mbfl_convert_filter_flush(filter1);
       mbfl_convert_filter_delete(filter1);
       if (filter2 != NULL) {
              mbfl_convert_filter_flush(filter2);
              mbfl_convert_filter_delete(filter2);
       }

       return mbfl_memory_device_result(&device, result);
}

Here is the call graph for this function:

Here is the caller graph for this function:

MBFLAPI void mbfl_encoding_detector_delete ( mbfl_encoding_detector *  identd)

Definition at line 404 of file mbfilter.c.

{
       int i;

       if (identd != NULL) {
              if (identd->filter_list != NULL) {
                     i = identd->filter_list_size;
                     while (i > 0) {
                            i--;
                            mbfl_identify_filter_delete(identd->filter_list[i]);
                     }
                     mbfl_free((void *)identd->filter_list);
              }
              mbfl_free((void *)identd);
       }
}

Here is the call graph for this function:

MBFLAPI int mbfl_encoding_detector_feed ( mbfl_encoding_detector *  identd,
mbfl_string string 
)

Definition at line 422 of file mbfilter.c.

{
       int i, n, num, bad, res;
       unsigned char *p;
       mbfl_identify_filter *filter;

       res = 0;
       /* feed data */
       if (identd != NULL && string != NULL && string->val != NULL) {
              num = identd->filter_list_size;
              n = string->len;
              p = string->val;
              bad = 0;
              while (n > 0) {
                     for (i = 0; i < num; i++) {
                            filter = identd->filter_list[i];
                            if (!filter->flag) {
                                   (*filter->filter_function)(*p, filter);
                                   if (filter->flag) {
                                          bad++;
                                   }
                            }
                     }
                     if ((num - 1) <= bad) {
                            res = 1;
                            break;
                     }
                     p++;
                     n--;
              }
       }

       return res;
}
MBFLAPI enum mbfl_no_encoding mbfl_encoding_detector_judge ( mbfl_encoding_detector *  identd)

Definition at line 457 of file mbfilter.c.

{
       mbfl_identify_filter *filter;
       enum mbfl_no_encoding encoding;
       int n;

       /* judge */
       encoding = mbfl_no_encoding_invalid;
       if (identd != NULL) {
              n = identd->filter_list_size - 1;
              while (n >= 0) {
                     filter = identd->filter_list[n];
                     if (!filter->flag) {
                            if (!identd->strict || !filter->status) {
                                   encoding = filter->encoding->no_encoding;
                            }
                     }
                     n--;
              }
 
              /* fallback judge */
              if (encoding ==      mbfl_no_encoding_invalid) {
                     n = identd->filter_list_size - 1;
                     while (n >= 0) {
                            filter = identd->filter_list[n];
                            if (!filter->flag) {
                                   encoding = filter->encoding->no_encoding;
                            }
                            n--;
                     }
              }
       }

       return encoding;
}
MBFLAPI mbfl_encoding_detector* mbfl_encoding_detector_new ( enum mbfl_no_encoding elist,
int  elistsz,
int  strict 
)

Definition at line 362 of file mbfilter.c.

{
       mbfl_encoding_detector *identd;

       int i, num;
       mbfl_identify_filter *filter;

       if (elist == NULL || elistsz <= 0) {
              return NULL;
       }

       /* allocate */
       identd = (mbfl_encoding_detector*)mbfl_malloc(sizeof(mbfl_encoding_detector));
       if (identd == NULL) {
              return NULL;
       }
       identd->filter_list = (mbfl_identify_filter **)mbfl_calloc(elistsz, sizeof(mbfl_identify_filter *));
       if (identd->filter_list == NULL) {
              mbfl_free(identd);
              return NULL;
       }

       /* create filters */
       i = 0;
       num = 0;
       while (i < elistsz) {
              filter = mbfl_identify_filter_new(elist[i]);
              if (filter != NULL) {
                     identd->filter_list[num] = filter;
                     num++;
              }
              i++;
       }
       identd->filter_list_size = num;

       /* set strict flag */
       identd->strict = strict;

       return identd;
}

Here is the call graph for this function:

MBFLAPI mbfl_string* mbfl_html_numeric_entity ( mbfl_string string,
mbfl_string result,
int convmap,
int  mapsize,
int  type 
)

Definition at line 2753 of file mbfilter.c.

{
       struct collector_htmlnumericentity_data pc;
       mbfl_memory_device device;
       mbfl_convert_filter *encoder;
       int n;
       unsigned char *p;

       if (string == NULL || result == NULL) {
              return NULL;
       }
       mbfl_string_init(result);
       result->no_language = string->no_language;
       result->no_encoding = string->no_encoding;
       mbfl_memory_device_init(&device, string->len, 0);

       /* output code filter */
       pc.decoder = mbfl_convert_filter_new(
           mbfl_no_encoding_wchar,
           string->no_encoding,
           mbfl_memory_device_output, 0, &device);
       /* wchar filter */
       if (type == 0) {
              encoder = mbfl_convert_filter_new(
                  string->no_encoding,
                  mbfl_no_encoding_wchar,
                  collector_encode_htmlnumericentity, 0, &pc);
       } else {
              encoder = mbfl_convert_filter_new(
                  string->no_encoding,
                  mbfl_no_encoding_wchar,
                  collector_decode_htmlnumericentity, 
                     (int (*)(void*))mbfl_filt_decode_htmlnumericentity_flush, &pc);
       }
       if (pc.decoder == NULL || encoder == NULL) {
              mbfl_convert_filter_delete(encoder);
              mbfl_convert_filter_delete(pc.decoder);
              return NULL;
       }
       pc.status = 0;
       pc.cache = 0;
       pc.digit = 0;
       pc.convmap = convmap;
       pc.mapsize = mapsize;

       /* feed data */
       p = string->val;
       n = string->len;
       if (p != NULL) {
              while (n > 0) {
                     if ((*encoder->filter_function)(*p++, encoder) < 0) {
                            break;
                     }
                     n--;
              }
       }
       mbfl_convert_filter_flush(encoder);
       mbfl_convert_filter_flush(pc.decoder);
       result = mbfl_memory_device_result(&device, result);
       mbfl_convert_filter_delete(encoder);
       mbfl_convert_filter_delete(pc.decoder);

       return result;
}

Here is the call graph for this function:

MBFLAPI const mbfl_encoding* mbfl_identify_encoding ( mbfl_string string,
enum mbfl_no_encoding elist,
int  elistsz,
int  strict 
)

Definition at line 567 of file mbfilter.c.

{
       int i, n, num, bad;
       unsigned char *p;
       mbfl_identify_filter *flist, *filter;
       const mbfl_encoding *encoding;

       /* flist is an array of mbfl_identify_filter instances */
       flist = (mbfl_identify_filter *)mbfl_calloc(elistsz, sizeof(mbfl_identify_filter));
       if (flist == NULL) {
              return NULL;
       }

       num = 0;
       if (elist != NULL) {
              for (i = 0; i < elistsz; i++) {
                     if (!mbfl_identify_filter_init(&flist[num], elist[i])) {
                            num++;
                     }
              }
       }

       /* feed data */
       n = string->len;
       p = string->val;

       if (p != NULL) {
              bad = 0;
              while (n > 0) {
                     for (i = 0; i < num; i++) {
                            filter = &flist[i];
                            if (!filter->flag) {
                                   (*filter->filter_function)(*p, filter);
                                   if (filter->flag) {
                                          bad++;
                                   }
                            }
                     }
                     if ((num - 1) <= bad && !strict) {
                            break;
                     }
                     p++;
                     n--;
              }
       }

       /* judge */
       encoding = NULL;

       for (i = 0; i < num; i++) {
              filter = &flist[i];
              if (!filter->flag) {
                     if (strict && filter->status) {
                            continue;
                     }
                     encoding = filter->encoding;
                     break;
              }
       }

       /* fall-back judge */
       if (!encoding) {
              for (i = 0; i < num; i++) {
                     filter = &flist[i];
                     if (!filter->flag && (!strict || !filter->status)) {
                            encoding = filter->encoding;
                            break;
                     }
              }
       }
 
       /* cleanup */
       /* dtors should be called in reverse order */
       i = num; while (--i >= 0) {
              mbfl_identify_filter_cleanup(&flist[i]);
       }

       mbfl_free((void *)flist);

       return encoding;
}

Here is the call graph for this function:

Here is the caller graph for this function:

MBFLAPI const char* mbfl_identify_encoding_name ( mbfl_string string,
enum mbfl_no_encoding elist,
int  elistsz,
int  strict 
)

Definition at line 650 of file mbfilter.c.

{
       const mbfl_encoding *encoding;

       encoding = mbfl_identify_encoding(string, elist, elistsz, strict);
       if (encoding != NULL &&
           encoding->no_encoding > mbfl_no_encoding_charset_min &&
           encoding->no_encoding < mbfl_no_encoding_charset_max) {
              return encoding->name;
       } else {
              return NULL;
       }
}

Here is the call graph for this function:

MBFLAPI enum mbfl_no_encoding mbfl_identify_encoding_no ( mbfl_string string,
enum mbfl_no_encoding elist,
int  elistsz,
int  strict 
)

Definition at line 665 of file mbfilter.c.

{
       const mbfl_encoding *encoding;

       encoding = mbfl_identify_encoding(string, elist, elistsz, strict);
       if (encoding != NULL &&
           encoding->no_encoding > mbfl_no_encoding_charset_min &&
           encoding->no_encoding < mbfl_no_encoding_charset_max) {
              return encoding->no_encoding;
       } else {
              return mbfl_no_encoding_invalid;
       }
}

Here is the call graph for this function:

MBFLAPI mbfl_string* mbfl_ja_jp_hantozen ( mbfl_string string,
mbfl_string result,
int  mode 
)

Definition at line 1841 of file mbfilter.c.

{
       int n;
       unsigned char *p;
       const mbfl_encoding *encoding;
       mbfl_memory_device device;
       mbfl_convert_filter *decoder = NULL;
       mbfl_convert_filter *encoder = NULL;
       mbfl_convert_filter *tl_filter = NULL;
       mbfl_convert_filter *next_filter = NULL;
       mbfl_filt_tl_jisx0201_jisx0208_param *param = NULL;

       /* validate parameters */
       if (string == NULL || result == NULL) {
              return NULL;
       }

       encoding = mbfl_no2encoding(string->no_encoding);
       if (encoding == NULL) {
              return NULL;
       }

       mbfl_memory_device_init(&device, string->len, 0);
       mbfl_string_init(result);

       result->no_language = string->no_language;
       result->no_encoding = string->no_encoding;

       decoder = mbfl_convert_filter_new(
              mbfl_no_encoding_wchar,
              string->no_encoding,
              mbfl_memory_device_output, 0, &device);
       if (decoder == NULL) {
              goto out;
       }
       next_filter = decoder;

       param =
              (mbfl_filt_tl_jisx0201_jisx0208_param *)mbfl_malloc(sizeof(mbfl_filt_tl_jisx0201_jisx0208_param));
       if (param == NULL) {
              goto out;
       }

       param->mode = mode;

       tl_filter = mbfl_convert_filter_new2(
              &vtbl_tl_jisx0201_jisx0208,
              (int(*)(int, void*))next_filter->filter_function,
              (int(*)(void*))next_filter->filter_flush,
              next_filter);
       if (tl_filter == NULL) {
              mbfl_free(param);
              goto out;
       }

       tl_filter->opaque = param;
       next_filter = tl_filter;

       encoder = mbfl_convert_filter_new(
              string->no_encoding,
              mbfl_no_encoding_wchar,
              (int(*)(int, void*))next_filter->filter_function,
              (int(*)(void*))next_filter->filter_flush,
              next_filter); 
       if (encoder == NULL) {
              goto out;
       }

       /* feed data */
       p = string->val;
       n = string->len;
       if (p != NULL) {
              while (n > 0) {
                     if ((*encoder->filter_function)(*p++, encoder) < 0) {
                            break;
                     }
                     n--;
              }
       }

       mbfl_convert_filter_flush(encoder);
       result = mbfl_memory_device_result(&device, result);
out:
       if (tl_filter != NULL) {
              if (tl_filter->opaque != NULL) {
                     mbfl_free(tl_filter->opaque);
              }
              mbfl_convert_filter_delete(tl_filter);
       }

       if (decoder != NULL) {
              mbfl_convert_filter_delete(decoder);
       }

       if (encoder != NULL) {
              mbfl_convert_filter_delete(encoder);
       }

       return result;
}

Here is the call graph for this function:

Here is the caller graph for this function:

MBFLAPI mbfl_string* mbfl_mime_header_decode ( mbfl_string string,
mbfl_string result,
enum mbfl_no_encoding  outcode 
)

Definition at line 2520 of file mbfilter.c.

{
       int n;
       unsigned char *p;
       struct mime_header_decoder_data *pd;

       mbfl_string_init(result);
       result->no_language = string->no_language;
       result->no_encoding = outcode;

       pd = mime_header_decoder_new(outcode);
       if (pd == NULL) {
              return NULL;
       }

       /* feed data */
       n = string->len;
       p = string->val;
       while (n > 0) {
              mime_header_decoder_collector(*p++, pd);
              n--;
       }

       result = mime_header_decoder_result(pd, result);
       mime_header_decoder_delete(pd);

       return result;
}

Here is the call graph for this function:

MBFLAPI mbfl_string* mbfl_mime_header_encode ( mbfl_string string,
mbfl_string result,
enum mbfl_no_encoding  outcode,
enum mbfl_no_encoding  encoding,
const char *  linefeed,
int  indent 
)

Definition at line 2220 of file mbfilter.c.

{
       int n;
       unsigned char *p;
       struct mime_header_encoder_data *pe;

       mbfl_string_init(result);
       result->no_language = string->no_language;
       result->no_encoding = mbfl_no_encoding_ascii;

       pe = mime_header_encoder_new(string->no_encoding, outcode, encoding);
       if (pe == NULL) {
              return NULL;
       }

       if (linefeed != NULL) {
              n = 0;
              while (*linefeed && n < 8) {
                     pe->lwsp[n++] = *linefeed++;
              }
              pe->lwsp[n++] = 0x20;
              pe->lwsp[n] = '\0';
              pe->lwsplen = n;
       }
       if (indent > 0 && indent < 74) {
              pe->firstindent = indent;
       }

       n = string->len;
       p = string->val;
       while (n > 0) {
              (*pe->conv1_filter->filter_function)(*p++, pe->conv1_filter);
              n--;
       }

       result = mime_header_encoder_result(pe, result);
       mime_header_encoder_delete(pe);

       return result;
}

Here is the call graph for this function:

Definition at line 815 of file mbfilter.c.

{
       int len, n, m, k;
       unsigned char *p;
       const unsigned char *mbtab;
       const mbfl_encoding *encoding;


       if (string == NULL) {
              return -1;
       }
       encoding = mbfl_no2encoding(string->no_encoding);
       if (encoding == NULL) {
              return -1;
       }

       len = 0;
       if (encoding->flag & MBFL_ENCTYPE_SBCS) {
              return 0;
       } else if (encoding->flag & (MBFL_ENCTYPE_WCS2BE | MBFL_ENCTYPE_WCS2LE)) {
              return len % 2;
       } else if (encoding->flag & (MBFL_ENCTYPE_WCS4BE | MBFL_ENCTYPE_WCS4LE)) {
              return len % 4;
       } else if (encoding->mblen_table != NULL) {
              mbtab = encoding->mblen_table;
              n = 0;
              p = string->val;
              k = string->len;
              /* count */
              if (p != NULL) {
                     while (n < k) {
                            m = mbtab[*p];
                            n += m;
                            p += m;
                     };
              }
              return n-k;
       } else {
              /* how can i do ? */
              return 0;
       }
       /* NOT REACHED */
}

Here is the call graph for this function:

MBFLAPI mbfl_string* mbfl_strcut ( mbfl_string string,
mbfl_string result,
int  from,
int  length 
)

Definition at line 1339 of file mbfilter.c.

{
       const mbfl_encoding *encoding;
       mbfl_memory_device device;

       /* validate the parameters */
       if (string == NULL || string->val == NULL || result == NULL) {
              return NULL;
       }

       if (from < 0 || length < 0) {
              return NULL;
       }

       if (from >= string->len) {
              from = string->len;
       }

       encoding = mbfl_no2encoding(string->no_encoding);
       if (encoding == NULL) {
              return NULL;
       }

       mbfl_string_init(result);
       result->no_language = string->no_language;
       result->no_encoding = string->no_encoding;

       if ((encoding->flag & (MBFL_ENCTYPE_SBCS
                            | MBFL_ENCTYPE_WCS2BE
                            | MBFL_ENCTYPE_WCS2LE
                            | MBFL_ENCTYPE_WCS4BE
                            | MBFL_ENCTYPE_WCS4LE))
                     || encoding->mblen_table != NULL) {
              const unsigned char *start = NULL;
              const unsigned char *end = NULL;
              unsigned char *w;
              unsigned int sz;

              if (encoding->flag & (MBFL_ENCTYPE_WCS2BE | MBFL_ENCTYPE_WCS2LE)) {
                     from &= -2;

                     if (from + length >= string->len) {
                            length = string->len - from;
                     }

                     start = string->val + from;
                     end   = start + (length & -2);
              } else if (encoding->flag & (MBFL_ENCTYPE_WCS4BE | MBFL_ENCTYPE_WCS4LE)) {
                     from &= -4;

                     if (from + length >= string->len) {
                            length = string->len - from;
                     }

                     start = string->val + from;
                     end   = start + (length & -4);
              } else if ((encoding->flag & MBFL_ENCTYPE_SBCS)) {
                     if (from + length >= string->len) {
                            length = string->len - from;
                     }

                     start = string->val + from;
                     end = start + length;
              } else if (encoding->mblen_table != NULL) {
                     const unsigned char *mbtab = encoding->mblen_table;
                     const unsigned char *p, *q;
                     int m;

                     /* search start position */
                     for (m = 0, p = string->val, q = p + from;
                                   p < q; p += (m = mbtab[*p]));

                     if (p > q) {
                            p -= m;
                     }

                     start = p;

                     /* search end position */
                     if ((start - string->val) + length >= (int)string->len) {
                            end = string->val + string->len;
                     } else {
                            for (q = p + length; p < q; p += (m = mbtab[*p]));

                            if (p > q) {
                                   p -= m;
                            }
                            end = p;
                     }
              } else {
                     /* never reached */
                     return NULL;
              }

              /* allocate memory and copy string */
              sz = end - start;
              if ((w = (unsigned char*)mbfl_calloc(sz + 8,
                            sizeof(unsigned char))) == NULL) {
                     return NULL;
              }

              memcpy(w, start, sz);
              w[sz] = '\0';
              w[sz + 1] = '\0';
              w[sz + 2] = '\0';
              w[sz + 3] = '\0';

              result->val = w;
              result->len = sz;
       } else {
              mbfl_convert_filter *encoder     = NULL;
              mbfl_convert_filter *decoder     = NULL;
              const unsigned char *p, *q, *r;
              struct {
                     mbfl_convert_filter encoder;
                     mbfl_convert_filter decoder;
                     const unsigned char *p;
                     int pos;
              } bk, _bk;

              /* output code filter */
              if (!(decoder = mbfl_convert_filter_new(
                            mbfl_no_encoding_wchar,
                            string->no_encoding,
                            mbfl_memory_device_output, 0, &device))) {
                     return NULL;
              }

              /* wchar filter */
              if (!(encoder = mbfl_convert_filter_new(
                            string->no_encoding,
                            mbfl_no_encoding_wchar,
                            mbfl_filter_output_null,
                            NULL, NULL))) {
                     mbfl_convert_filter_delete(decoder);
                     return NULL;
              }

              mbfl_memory_device_init(&device, length + 8, 0);

              p = string->val;

              /* search start position */
              for (q = string->val + from; p < q; p++) {
                     (*encoder->filter_function)(*p, encoder);
              }

              /* switch the drain direction */
              encoder->output_function = (int(*)(int,void *))decoder->filter_function;
              encoder->flush_function = (int(*)(void *))decoder->filter_flush;
              encoder->data = decoder;

              q = string->val + string->len;

              /* save the encoder, decoder state and the pointer */
              mbfl_convert_filter_copy(decoder, &_bk.decoder);
              mbfl_convert_filter_copy(encoder, &_bk.encoder);
              _bk.p = p;
              _bk.pos = device.pos;

              if (length > q - p) {
                     length = q - p;
              }

              if (length >= 20) {
                     /* output a little shorter than "length" */
                     /* XXX: the constant "20" was determined purely on the heuristics. */
                     for (r = p + length - 20; p < r; p++) {
                            (*encoder->filter_function)(*p, encoder);
                     }

                     /* if the offset of the resulting string exceeds the length,
                      * then restore the state */
                     if (device.pos > length) {
                            p = _bk.p;
                            device.pos = _bk.pos;
                            decoder->filter_dtor(decoder);
                            encoder->filter_dtor(encoder);
                            mbfl_convert_filter_copy(&_bk.decoder, decoder);
                            mbfl_convert_filter_copy(&_bk.encoder, encoder);
                            bk = _bk;
                     } else {
                            /* save the encoder, decoder state and the pointer */
                            mbfl_convert_filter_copy(decoder, &bk.decoder);
                            mbfl_convert_filter_copy(encoder, &bk.encoder);
                            bk.p = p;
                            bk.pos = device.pos;

                            /* flush the stream */
                            (*encoder->filter_flush)(encoder);

                            /* if the offset of the resulting string exceeds the length,
                             * then restore the state */
                            if (device.pos > length) {
                                   bk.decoder.filter_dtor(&bk.decoder);
                                   bk.encoder.filter_dtor(&bk.encoder);

                                   p = _bk.p;
                                   device.pos = _bk.pos;
                                   decoder->filter_dtor(decoder);
                                   encoder->filter_dtor(encoder);
                                   mbfl_convert_filter_copy(&_bk.decoder, decoder);
                                   mbfl_convert_filter_copy(&_bk.encoder, encoder);
                                   bk = _bk;
                            } else {
                                   _bk.decoder.filter_dtor(&_bk.decoder);
                                   _bk.encoder.filter_dtor(&_bk.encoder);

                                   p = bk.p;
                                   device.pos = bk.pos;
                                   decoder->filter_dtor(decoder);
                                   encoder->filter_dtor(encoder);
                                   mbfl_convert_filter_copy(&bk.decoder, decoder);
                                   mbfl_convert_filter_copy(&bk.encoder, encoder);
                            }
                     }
              } else {
                     bk = _bk;
              }

              /* detect end position */
              while (p < q) {
                     (*encoder->filter_function)(*p, encoder);

                     if (device.pos > length) {
                            /* restore filter */
                            p = bk.p;
                            device.pos = bk.pos;
                            decoder->filter_dtor(decoder);
                            encoder->filter_dtor(encoder);
                            mbfl_convert_filter_copy(&bk.decoder, decoder);
                            mbfl_convert_filter_copy(&bk.encoder, encoder);
                            break;
                     }

                     p++;

                     /* backup current state */
                     mbfl_convert_filter_copy(decoder, &_bk.decoder);
                     mbfl_convert_filter_copy(encoder, &_bk.encoder);
                     _bk.pos = device.pos;
                     _bk.p = p;

                     (*encoder->filter_flush)(encoder);

                     if (device.pos > length) {
                            _bk.decoder.filter_dtor(&_bk.decoder);
                            _bk.encoder.filter_dtor(&_bk.encoder);

                            /* restore filter */
                            p = bk.p;
                            device.pos = bk.pos;
                            decoder->filter_dtor(decoder);
                            encoder->filter_dtor(encoder);
                            mbfl_convert_filter_copy(&bk.decoder, decoder);
                            mbfl_convert_filter_copy(&bk.encoder, encoder);
                            break;
                     }

                     bk.decoder.filter_dtor(&bk.decoder);
                     bk.encoder.filter_dtor(&bk.encoder);

                     p = _bk.p;
                     device.pos = _bk.pos;
                     decoder->filter_dtor(decoder);
                     encoder->filter_dtor(encoder);
                     mbfl_convert_filter_copy(&_bk.decoder, decoder);
                     mbfl_convert_filter_copy(&_bk.encoder, encoder);

                     bk = _bk;
              }

              (*encoder->filter_flush)(encoder);

              bk.decoder.filter_dtor(&bk.decoder);
              bk.encoder.filter_dtor(&bk.encoder);

              result = mbfl_memory_device_result(&device, result);

              mbfl_convert_filter_delete(encoder);
              mbfl_convert_filter_delete(decoder);
       }

       return result;
}

Here is the call graph for this function:

Here is the caller graph for this function:

MBFLAPI mbfl_string* mbfl_strimwidth ( mbfl_string string,
mbfl_string marker,
mbfl_string result,
int  from,
int  width 
)

Definition at line 1740 of file mbfilter.c.

{
       struct collector_strimwidth_data pc;
       mbfl_convert_filter *encoder;
       int n, mkwidth;
       unsigned char *p;

       if (string == NULL || result == NULL) {
              return NULL;
       }
       mbfl_string_init(result);
       result->no_language = string->no_language;
       result->no_encoding = string->no_encoding;
       mbfl_memory_device_init(&pc.device, width, 0);

       /* output code filter */
       pc.decoder = mbfl_convert_filter_new(
           mbfl_no_encoding_wchar,
           string->no_encoding,
           mbfl_memory_device_output, 0, &pc.device);
       pc.decoder_backup = mbfl_convert_filter_new(
           mbfl_no_encoding_wchar,
           string->no_encoding,
           mbfl_memory_device_output, 0, &pc.device);
       /* wchar filter */
       encoder = mbfl_convert_filter_new(
           string->no_encoding,
           mbfl_no_encoding_wchar,
           collector_strimwidth, 0, &pc);
       if (pc.decoder == NULL || pc.decoder_backup == NULL || encoder == NULL) {
              mbfl_convert_filter_delete(encoder);
              mbfl_convert_filter_delete(pc.decoder);
              mbfl_convert_filter_delete(pc.decoder_backup);
              return NULL;
       }
       mkwidth = 0;
       if (marker) {
              mkwidth = mbfl_strwidth(marker);
       }
       pc.from = from;
       pc.width = width - mkwidth;
       pc.outwidth = 0;
       pc.outchar = 0;
       pc.status = 0;
       pc.endpos = 0;

       /* feed data */
       p = string->val;
       n = string->len;
       if (p != NULL) {
              while (n > 0) {
                     n--;
                     if ((*encoder->filter_function)(*p++, encoder) < 0) {
                            break;
                     }
              }
              mbfl_convert_filter_flush(encoder);
              if (pc.status != 0 && mkwidth > 0) {
                     pc.width += mkwidth;
                     while (n > 0) {
                            if ((*encoder->filter_function)(*p++, encoder) < 0) {
                                   break;
                            }
                            n--;
                     }
                     mbfl_convert_filter_flush(encoder);
                     if (pc.status != 1) {
                            pc.status = 10;
                            pc.device.pos = pc.endpos;
                            mbfl_convert_filter_copy(pc.decoder_backup, pc.decoder);
                            mbfl_convert_filter_reset(encoder, marker->no_encoding, mbfl_no_encoding_wchar);
                            p = marker->val;
                            n = marker->len;
                            while (n > 0) {
                                   if ((*encoder->filter_function)(*p++, encoder) < 0) {
                                          break;
                                   }
                                   n--;
                            }
                            mbfl_convert_filter_flush(encoder);
                     }
              } else if (pc.status != 0) {
                     pc.device.pos = pc.endpos;
                     mbfl_convert_filter_copy(pc.decoder_backup, pc.decoder);
              }
              mbfl_convert_filter_flush(pc.decoder);
       }
       result = mbfl_memory_device_result(&pc.device, result);
       mbfl_convert_filter_delete(encoder);
       mbfl_convert_filter_delete(pc.decoder);
       mbfl_convert_filter_delete(pc.decoder_backup);

       return result;
}

Here is the call graph for this function:

Definition at line 691 of file mbfilter.c.

{
       int len, n, m, k;
       unsigned char *p;
       const unsigned char *mbtab;
       const mbfl_encoding *encoding;

       encoding = mbfl_no2encoding(string->no_encoding);
       if (encoding == NULL || string == NULL) {
              return -1;
       }

       len = 0;
       if (encoding->flag & MBFL_ENCTYPE_SBCS) {
              len = string->len;
       } else if (encoding->flag & (MBFL_ENCTYPE_WCS2BE | MBFL_ENCTYPE_WCS2LE)) {
              len = string->len/2;
       } else if (encoding->flag & (MBFL_ENCTYPE_WCS4BE | MBFL_ENCTYPE_WCS4LE)) {
              len = string->len/4;
       } else if (encoding->mblen_table != NULL) {
              mbtab = encoding->mblen_table;
              n = 0;
              p = string->val;
              k = string->len;
              /* count */
              if (p != NULL) {
                     while (n < k) {
                            m = mbtab[*p];
                            n += m;
                            p += m;
                            len++;
                     };
              }
       } else {
              /* wchar filter */
              mbfl_convert_filter *filter = mbfl_convert_filter_new(
                string->no_encoding, 
                mbfl_no_encoding_wchar,
                filter_count_output, 0, &len);
              if (filter == NULL) {
                     return -1;
              }
              /* count */
              n = string->len;
              p = string->val;
              if (p != NULL) {
                     while (n > 0) {
                            (*filter->filter_function)(*p++, filter);
                            n--;
                     }
              }
              mbfl_convert_filter_delete(filter);
       }

       return len;
}

Here is the call graph for this function:

MBFLAPI int mbfl_strpos ( mbfl_string haystack,
mbfl_string needle,
int  offset,
int  reverse 
)

Definition at line 860 of file mbfilter.c.

{
       int result;
       mbfl_string _haystack_u8, _needle_u8;
       const mbfl_string *haystack_u8, *needle_u8;
       const unsigned char *u8_tbl;

       if (haystack == NULL || haystack->val == NULL || needle == NULL || needle->val == NULL) {
              return -8;
       }

       {
              const mbfl_encoding *u8_enc;
              u8_enc = mbfl_no2encoding(mbfl_no_encoding_utf8);
              if (u8_enc == NULL || u8_enc->mblen_table == NULL) {
                     return -8;
              }
              u8_tbl = u8_enc->mblen_table;
       }

       if (haystack->no_encoding != mbfl_no_encoding_utf8) {
              mbfl_string_init(&_haystack_u8);
              haystack_u8 = mbfl_convert_encoding(haystack, &_haystack_u8, mbfl_no_encoding_utf8);
              if (haystack_u8 == NULL) {
                     result = -4;
                     goto out;
              }
       } else {
              haystack_u8 = haystack;
       }

       if (needle->no_encoding != mbfl_no_encoding_utf8) {
              mbfl_string_init(&_needle_u8);
              needle_u8 = mbfl_convert_encoding(needle, &_needle_u8, mbfl_no_encoding_utf8);
              if (needle_u8 == NULL) {
                     result = -4;
                     goto out;
              }
       } else {
              needle_u8 = needle;
       }

       if (needle_u8->len < 1) {
              result = -8;
              goto out;
       }

       result = -1;
       if (haystack_u8->len < needle_u8->len) {
              goto out;
       }

       if (!reverse) {
              unsigned int jtbl[1 << (sizeof(unsigned char) * 8)];
              unsigned int needle_u8_len = needle_u8->len;
              unsigned int i;
              const unsigned char *p, *q, *e;
              const unsigned char *haystack_u8_val = haystack_u8->val,
                                  *needle_u8_val = needle_u8->val;
              for (i = 0; i < sizeof(jtbl) / sizeof(*jtbl); ++i) {
                     jtbl[i] = needle_u8_len + 1;
              }
              for (i = 0; i < needle_u8_len - 1; ++i) {
                     jtbl[needle_u8_val[i]] = needle_u8_len - i;
              }
              e = haystack_u8_val + haystack_u8->len;
              p = haystack_u8_val;
              while (--offset >= 0) {
                     if (p >= e) {
                            result = -16;
                            goto out;
                     }
                     p += u8_tbl[*p];
              }
              p += needle_u8_len;
              if (p > e) {
                     goto out;
              }
              while (p <= e) {
                     const unsigned char *pv = p;
                     q = needle_u8_val + needle_u8_len;
                     for (;;) {
                            if (q == needle_u8_val) {
                                   result = 0;
                                   while (p > haystack_u8_val) {
                                          unsigned char c = *--p;
                                          if (c < 0x80) {
                                                 ++result;
                                          } else if ((c & 0xc0) != 0x80) {
                                                 ++result;
                                          }      
                                   }
                                   goto out;
                            }
                            if (*--q != *--p) {
                                   break;
                            }
                     }
                     p += jtbl[*p];
                     if (p <= pv) {
                            p = pv + 1;
                     }
              }
       } else {
              unsigned int jtbl[1 << (sizeof(unsigned char) * 8)];
              unsigned int needle_u8_len = needle_u8->len, needle_len = 0;
              unsigned int i;
              const unsigned char *p, *e, *q, *qe;
              const unsigned char *haystack_u8_val = haystack_u8->val,
                                  *needle_u8_val = needle_u8->val;
              for (i = 0; i < sizeof(jtbl) / sizeof(*jtbl); ++i) {
                     jtbl[i] = needle_u8_len;
              }
              for (i = needle_u8_len - 1; i > 0; --i) {
                     unsigned char c = needle_u8_val[i];
                     jtbl[c] = i;
                     if (c < 0x80) {
                            ++needle_len;
                     } else if ((c & 0xc0) != 0x80) {
                            ++needle_len;
                     }
              }
              {
                     unsigned char c = needle_u8_val[0];
                     if (c < 0x80) {
                            ++needle_len;
                     } else if ((c & 0xc0) != 0x80) {
                            ++needle_len;
                     }
              }
              e = haystack_u8_val;
              p = e + haystack_u8->len;
              qe = needle_u8_val + needle_u8_len;
              if (offset < 0) {
                     if (-offset > needle_len) {
                            offset += needle_len; 
                            while (offset < 0) {
                                   unsigned char c;
                                   if (p <= e) {
                                          result = -16;
                                          goto out;
                                   }
                                   c = *(--p);
                                   if (c < 0x80) {
                                          ++offset;
                                   } else if ((c & 0xc0) != 0x80) {
                                          ++offset;
                                   }
                            }
                     }
              } else {
                     const unsigned char *ee = haystack_u8_val + haystack_u8->len;
                     while (--offset >= 0) {
                            if (e >= ee) {
                                   result = -16;
                                   goto out;
                            }
                            e += u8_tbl[*e];
                     }
              }
              if (p < e + needle_u8_len) {
                     goto out;
              }
              p -= needle_u8_len;
              while (p >= e) {
                     const unsigned char *pv = p;
                     q = needle_u8_val;
                     for (;;) {
                            if (q == qe) {
                                   result = 0;
                                   p -= needle_u8_len;
                                   while (p > haystack_u8_val) {
                                          unsigned char c = *--p;
                                          if (c < 0x80) {
                                                 ++result;
                                          } else if ((c & 0xc0) != 0x80) {
                                                 ++result;
                                          }      
                                   }
                                   goto out;
                            }
                            if (*q != *p) {
                                   break;
                            }
                            ++p, ++q;
                     }
                     p -= jtbl[*p];
                     if (p >= pv) {
                            p = pv - 1;
                     }
              }
       }
out:
       if (haystack_u8 == &_haystack_u8) {
              mbfl_string_clear(&_haystack_u8);
       }
       if (needle_u8 == &_needle_u8) {
              mbfl_string_clear(&_needle_u8);
       }
       return result;
}

Here is the call graph for this function:

Definition at line 1658 of file mbfilter.c.

{
       int len, n;
       unsigned char *p;
       mbfl_convert_filter *filter;

       len = 0;
       if (string->len > 0 && string->val != NULL) {
              /* wchar filter */
              filter = mbfl_convert_filter_new(
                  string->no_encoding,
                  mbfl_no_encoding_wchar,
                  filter_count_width, 0, &len);
              if (filter == NULL) {
                     mbfl_convert_filter_delete(filter);
                     return -1;
              }

              /* feed data */
              p = string->val;
              n = string->len;
              while (n > 0) {
                     (*filter->filter_function)(*p++, filter);
                     n--;
              }

              mbfl_convert_filter_flush(filter);
              mbfl_convert_filter_delete(filter);
       }

       return len;
}

Here is the call graph for this function:

Here is the caller graph for this function:

MBFLAPI mbfl_string* mbfl_substr ( mbfl_string string,
mbfl_string result,
int  from,
int  length 
)

Definition at line 1181 of file mbfilter.c.

{
       const mbfl_encoding *encoding;
       int n, m, k, len, start, end;
       unsigned char *p, *w;
       const unsigned char *mbtab;

       encoding = mbfl_no2encoding(string->no_encoding);
       if (encoding == NULL || string == NULL || result == NULL) {
              return NULL;
       }
       mbfl_string_init(result);
       result->no_language = string->no_language;
       result->no_encoding = string->no_encoding;

       if ((encoding->flag & (MBFL_ENCTYPE_SBCS | MBFL_ENCTYPE_WCS2BE | MBFL_ENCTYPE_WCS2LE | MBFL_ENCTYPE_WCS4BE | MBFL_ENCTYPE_WCS4LE)) ||
          encoding->mblen_table != NULL) {
              len = string->len;
              start = from;
              end = from + length;
              if (encoding->flag & (MBFL_ENCTYPE_WCS2BE | MBFL_ENCTYPE_WCS2LE)) {
                     start *= 2;
                     end = start + length*2;
              } else if (encoding->flag & (MBFL_ENCTYPE_WCS4BE | MBFL_ENCTYPE_WCS4LE)) {
                     start *= 4;
                     end = start + length*4;
              } else if (encoding->mblen_table != NULL) {
                     mbtab = encoding->mblen_table;
                     start = 0;
                     end = 0;
                     n = 0;
                     k = 0;
                     p = string->val;
                     if (p != NULL) {
                            /* search start position */
                            while (k <= from) {
                                   start = n;
                                   if (n >= len) {
                                          break;
                                   }
                                   m = mbtab[*p];
                                   n += m;
                                   p += m;
                                   k++;
                            }
                            /* detect end position */
                            k = 0;
                            end = start;
                            while (k < length) {
                                   end = n;
                                   if (n >= len) {
                                          break;
                                   }
                                   m = mbtab[*p];
                                   n += m;
                                   p += m;
                                   k++;
                            }
                     }
              }

              if (start > len) {
                     start = len;
              }
              if (start < 0) {
                     start = 0;
              }
              if (end > len) {
                     end = len;
              }
              if (end < 0) {
                     end = 0;
              }
              if (start > end) {
                     start = end;
              }

              /* allocate memory and copy */
              n = end - start;
              result->len = 0;
              result->val = w = (unsigned char*)mbfl_malloc((n + 8)*sizeof(unsigned char));
              if (w != NULL) {
                     p = string->val;
                     if (p != NULL) {
                            p += start;
                            result->len = n;
                            while (n > 0) {
                                   *w++ = *p++;
                                   n--;
                            }
                     }
                     *w++ = '\0';
                     *w++ = '\0';
                     *w++ = '\0';
                     *w = '\0';
              } else {
                     result = NULL;
              }
       } else {
              mbfl_memory_device device;
              struct collector_substr_data pc;
              mbfl_convert_filter *decoder;
              mbfl_convert_filter *encoder;

              mbfl_memory_device_init(&device, length + 1, 0);
              mbfl_string_init(result);
              result->no_language = string->no_language;
              result->no_encoding = string->no_encoding;
              /* output code filter */
              decoder = mbfl_convert_filter_new(
                  mbfl_no_encoding_wchar,
                  string->no_encoding,
                  mbfl_memory_device_output, 0, &device);
              /* wchar filter */
              encoder = mbfl_convert_filter_new(
                  string->no_encoding,
                  mbfl_no_encoding_wchar,
                  collector_substr, 0, &pc);
              if (decoder == NULL || encoder == NULL) {
                     mbfl_convert_filter_delete(encoder);
                     mbfl_convert_filter_delete(decoder);
                     return NULL;
              }
              pc.next_filter = decoder;
              pc.start = from;
              pc.stop = from + length;
              pc.output = 0;

              /* feed data */
              p = string->val;
              n = string->len;
              if (p != NULL) {
                     while (n > 0) {
                            if ((*encoder->filter_function)(*p++, encoder) < 0) {
                                   break;
                            }
                            n--;
                     }
              }

              mbfl_convert_filter_flush(encoder);
              mbfl_convert_filter_flush(decoder);
              result = mbfl_memory_device_result(&device, result);
              mbfl_convert_filter_delete(encoder);
              mbfl_convert_filter_delete(decoder);
       }

       return result;
}

Here is the call graph for this function:

MBFLAPI int mbfl_substr_count ( mbfl_string haystack,
mbfl_string needle 
)

Definition at line 1071 of file mbfilter.c.

{
       int n, result = 0;
       unsigned char *p;
       mbfl_convert_filter *filter;
       struct collector_strpos_data pc;

       if (haystack == NULL || needle == NULL) {
              return -8;
       }
       /* needle is converted into wchar */
       mbfl_wchar_device_init(&pc.needle);
       filter = mbfl_convert_filter_new(
         needle->no_encoding,
         mbfl_no_encoding_wchar,
         mbfl_wchar_device_output, 0, &pc.needle);
       if (filter == NULL) {
              return -4;
       }
       p = needle->val;
       n = needle->len;
       if (p != NULL) {
              while (n > 0) {
                     if ((*filter->filter_function)(*p++, filter) < 0) {
                            break;
                     }
                     n--;
              }
       }
       mbfl_convert_filter_flush(filter);
       mbfl_convert_filter_delete(filter);
       pc.needle_len = pc.needle.pos;
       if (pc.needle.buffer == NULL) {
              return -4;
       }
       if (pc.needle_len <= 0) {
              mbfl_wchar_device_clear(&pc.needle);
              return -2;
       }
       /* initialize filter and collector data */
       filter = mbfl_convert_filter_new(
         haystack->no_encoding,
         mbfl_no_encoding_wchar,
         collector_strpos, 0, &pc);
       if (filter == NULL) {
              mbfl_wchar_device_clear(&pc.needle);
              return -4;
       }
       pc.start = 0;
       pc.output = 0;
       pc.needle_pos = 0;
       pc.found_pos = 0;
       pc.matched_pos = -1;

       /* feed data */
       p = haystack->val;
       n = haystack->len;
       if (p != NULL) {
              while (n > 0) {
                     if ((*filter->filter_function)(*p++, filter) < 0) {
                            pc.matched_pos = -4;
                            break;
                     }
                     if (pc.matched_pos >= 0) {
                            ++result;
                            pc.matched_pos = -1;
                            pc.needle_pos = 0;
                     }
                     n--;
              }
       }
       mbfl_convert_filter_flush(filter);
       mbfl_convert_filter_delete(filter);
       mbfl_wchar_device_clear(&pc.needle);

       return result;
}

Here is the call graph for this function:

Definition at line 2501 of file mbfilter.c.

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 2514 of file mbfilter.c.

{
       return mime_header_decoder_collector(c, pd);
}

Here is the call graph for this function:

Definition at line 2470 of file mbfilter.c.

{
       struct mime_header_decoder_data *pd;

       pd = (struct mime_header_decoder_data*)mbfl_malloc(sizeof(struct mime_header_decoder_data));
       if (pd == NULL) {
              return NULL;
       }

       mbfl_memory_device_init(&pd->outdev, 0, 0);
       mbfl_memory_device_init(&pd->tmpdev, 0, 0);
       pd->cspos = 0;
       pd->status = 0;
       pd->encoding = mbfl_no_encoding_pass;
       pd->incode = mbfl_no_encoding_ascii;
       pd->outcode = outcode;
       /* charset convert filter */
       pd->conv2_filter = mbfl_convert_filter_new(mbfl_no_encoding_wchar, pd->outcode, mbfl_memory_device_output, 0, &pd->outdev);
       pd->conv1_filter = mbfl_convert_filter_new(pd->incode, mbfl_no_encoding_wchar, mbfl_filter_output_pipe, 0, pd->conv2_filter);
       /* decode filter */
       pd->deco_filter = mbfl_convert_filter_new(pd->encoding, mbfl_no_encoding_8bit, mbfl_filter_output_pipe, 0, pd->conv1_filter);

       if (pd->conv1_filter == NULL || pd->conv2_filter == NULL || pd->deco_filter == NULL) {
              mime_header_decoder_delete(pd);
              return NULL;
       }

       return pd;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 2444 of file mbfilter.c.

{
       switch (pd->status) {
       case 1:
       case 2:
       case 3:
       case 4:
       case 7:
       case 8:
       case 9:
              mbfl_convert_filter_devcat(pd->conv1_filter, &pd->tmpdev);
              break;
       case 5:
       case 6:
              (*pd->deco_filter->filter_flush)(pd->deco_filter);
              (*pd->conv1_filter->filter_flush)(pd->conv1_filter);
              break;
       }
       (*pd->conv2_filter->filter_flush)(pd->conv2_filter);
       mbfl_memory_device_reset(&pd->tmpdev);
       pd->status = 0;

       return mbfl_memory_device_result(&pd->outdev, result);
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 2214 of file mbfilter.c.

{
       return (*pe->conv1_filter->filter_function)(c, pe->conv1_filter);
}
MBFLAPI struct mime_header_encoder_data* mime_header_encoder_new ( enum mbfl_no_encoding  incode,
enum mbfl_no_encoding  outcode,
enum mbfl_no_encoding  encoding 
) [read]

Definition at line 2108 of file mbfilter.c.

{
       int n;
       const char *s;
       const mbfl_encoding *outencoding;
       struct mime_header_encoder_data *pe;

       /* get output encoding and check MIME charset name */
       outencoding = mbfl_no2encoding(outcode);
       if (outencoding == NULL || outencoding->mime_name == NULL || outencoding->mime_name[0] == '\0') {
              return NULL;
       }

       pe = (struct mime_header_encoder_data*)mbfl_malloc(sizeof(struct mime_header_encoder_data));
       if (pe == NULL) {
              return NULL;
       }

       mbfl_memory_device_init(&pe->outdev, 0, 0);
       mbfl_memory_device_init(&pe->tmpdev, 0, 0);
       pe->prevpos = 0;
       pe->linehead = 0;
       pe->firstindent = 0;
       pe->status1 = 0;
       pe->status2 = 0;

       /* make the encoding description string  exp. "=?ISO-2022-JP?B?" */
       n = 0;
       pe->encname[n++] = 0x3d;
       pe->encname[n++] = 0x3f;
       s = outencoding->mime_name;
       while (*s) {
              pe->encname[n++] = *s++;
       }
       pe->encname[n++] = 0x3f;
       if (transenc == mbfl_no_encoding_qprint) {
              pe->encname[n++] = 0x51;
       } else {
              pe->encname[n++] = 0x42;
              transenc = mbfl_no_encoding_base64;
       }
       pe->encname[n++] = 0x3f;
       pe->encname[n] = '\0';
       pe->encnamelen = n;

       n = 0;
       pe->lwsp[n++] = 0x0d;
       pe->lwsp[n++] = 0x0a;
       pe->lwsp[n++] = 0x20;
       pe->lwsp[n] = '\0';
       pe->lwsplen = n;

       /* transfer encode filter */
       pe->encod_filter = mbfl_convert_filter_new(outcode, transenc, mbfl_memory_device_output, 0, &(pe->outdev));
       pe->encod_filter_backup = mbfl_convert_filter_new(outcode, transenc, mbfl_memory_device_output, 0, &(pe->outdev));

       /* Output code filter */
       pe->conv2_filter = mbfl_convert_filter_new(mbfl_no_encoding_wchar, outcode, mbfl_filter_output_pipe, 0, pe->encod_filter);
       pe->conv2_filter_backup = mbfl_convert_filter_new(mbfl_no_encoding_wchar, outcode, mbfl_filter_output_pipe, 0, pe->encod_filter);

       /* encoded block filter */
       pe->block_filter = mbfl_convert_filter_new(mbfl_no_encoding_wchar, mbfl_no_encoding_wchar, mime_header_encoder_block_collector, 0, pe);

       /* Input code filter */
       pe->conv1_filter = mbfl_convert_filter_new(incode, mbfl_no_encoding_wchar, mime_header_encoder_collector, 0, pe);

       if (pe->encod_filter == NULL ||
           pe->encod_filter_backup == NULL ||
           pe->conv2_filter == NULL ||
           pe->conv2_filter_backup == NULL ||
           pe->conv1_filter == NULL) {
              mime_header_encoder_delete(pe);
              return NULL;
       }

       if (transenc == mbfl_no_encoding_qprint) {
              pe->encod_filter->status |= MBFL_QPRINT_STS_MIME_HEADER;
              pe->encod_filter_backup->status |= MBFL_QPRINT_STS_MIME_HEADER;
       } else {
              pe->encod_filter->status |= MBFL_BASE64_STS_MIME_HEADER;
              pe->encod_filter_backup->status |= MBFL_BASE64_STS_MIME_HEADER;
       }

       return pe;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 2082 of file mbfilter.c.

{
       if (pe->status1 >= 10) {
              (*pe->conv2_filter->filter_flush)(pe->conv2_filter);
              (*pe->encod_filter->filter_flush)(pe->encod_filter);
              mbfl_memory_device_strncat(&pe->outdev, "\x3f\x3d", 2);        /* ?= */
       } else if (pe->tmpdev.pos > 0) {
              if (pe->outdev.pos > 0) {
                     if ((pe->outdev.pos - pe->linehead + pe->tmpdev.pos) > 74) {
                            mbfl_memory_device_strncat(&pe->outdev, pe->lwsp, pe->lwsplen);
                     } else {
                            mbfl_memory_device_output(0x20, &pe->outdev);
                     }
              }
              mbfl_memory_device_devcat(&pe->outdev, &pe->tmpdev);
       }
       mbfl_memory_device_reset(&pe->tmpdev);
       pe->prevpos = 0;
       pe->linehead = 0;
       pe->status1 = 0;
       pe->status2 = 0;

       return mbfl_memory_device_result(&pe->outdev, result);
}

Here is the call graph for this function:

Here is the caller graph for this function: