Skip to content

yunkya2/iconv_mini

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

iconv_mini

A lightweight iconv library specialized for Shift_JIS ↔ UTF-8 character encoding conversion.

Overview

iconv_mini is a minimal, efficient implementation of character encoding conversion specifically designed for converting between Shift_JIS (Japanese character encoding) and UTF-8. This library provides a compact alternative to the standard iconv library when only Shift_JIS ↔ UTF-8 conversion is needed.

Features

  • Lightweight: Minimal memory footprint and binary size
  • Specialized: Optimized for Shift_JIS ↔ UTF-8 conversion only
  • Fast: Direct lookup table implementation for efficient conversion
  • Configurable: Compile-time options to include only needed conversion direction
  • Standards compliant: Compatible with standard Shift_JIS and UTF-8 encodings

API Reference

The library provides two main conversion functions:

Shift_JIS to UTF-8 Conversion

int iconv_s2u(char **src_buf, size_t *src_len,
              char **dst_buf, size_t *dst_len);

Converts Shift_JIS encoded text to UTF-8.

Parameters:

  • src_buf: Pointer to source buffer pointer (Shift_JIS input)
  • src_len: Pointer to source buffer length
  • dst_buf: Pointer to destination buffer pointer (UTF-8 output)
  • dst_len: Pointer to destination buffer length

Returns:

  • 0: Success
  • -1: Error (invalid input, insufficient buffer space, etc.)

UTF-8 to Shift_JIS Conversion

int iconv_u2s(char **src_buf, size_t *src_len,
              char **dst_buf, size_t *dst_len);

Converts UTF-8 encoded text to Shift_JIS.

Parameters:

  • src_buf: Pointer to source buffer pointer (UTF-8 input)
  • src_len: Pointer to source buffer length
  • dst_buf: Pointer to destination buffer pointer (Shift_JIS output)
  • dst_len: Pointer to destination buffer length

Returns:

  • 0: Success
  • -1: Error (invalid input, insufficient buffer space, etc.)

Usage Example

#include "iconv_mini.h"
#include <stdio.h>
#include <string.h>

int main() {
    char src[] = "こんにちは"; // UTF-8 input
    char dst[256];
    char *src_ptr = src;
    char *dst_ptr = dst;
    size_t src_len = strlen(src);
    size_t dst_len = sizeof(dst);
    
    // Convert UTF-8 to Shift_JIS
    if (iconv_u2s(&src_ptr, &src_len, &dst_ptr, &dst_len) == 0) {
        printf("Conversion successful\n");
    } else {
        printf("Conversion failed\n");
    }
    
    return 0;
}

Compilation Options

The library supports compile-time configuration to reduce size when only one conversion direction is needed:

  • ICONV_ONLY_U2S: Include only UTF-8 to Shift_JIS conversion
  • ICONV_ONLY_S2U: Include only Shift_JIS to UTF-8 conversion

Example:

gcc -DICONV_ONLY_U2S -c iconv_mini.c

Building

Prerequisites

  • C compiler (GCC, Clang, etc.)
  • Standard C library

Building the Library

# Compile the library
gcc -c iconv_mini.c -o iconv_mini.o

# Link with your application
gcc your_app.c iconv_mini.o -o your_app

Building the Conversion Table Generator

The library includes tools to generate the conversion tables:

# Build the C-based table generator
make createtable

# Or use the Python version
python3 createtable.py > iconv_table.h

Files

  • iconv_mini.h: Main header file with function declarations
  • iconv_mini.c: Core conversion implementation
  • iconv_table.h: Pre-generated conversion lookup tables
  • createtable.c: C program to generate conversion tables
  • createtable.py: Python script to generate conversion tables
  • Makefile: Build configuration

Character Support

This library supports the standard Shift_JIS character set including:

  • ASCII characters (0x00-0x7F)
  • Half-width Katakana (0xA1-0xDF)
  • JIS X 0208 characters (Kanji, Hiragana, Katakana, symbols)
  • Does not support JIS X 0213 plane 2 characters

License

Copyright (c) 2023,2025 Yuichi Nakamura (@yunkya2)

This project is licensed under the MIT License - see the source files for details.

Applications

This library is particularly useful for:

  • Embedded systems with limited memory
  • Legacy system integration
  • File format converters
  • Network protocols handling Japanese text
  • Any application requiring efficient Shift_JIS ↔ UTF-8 conversion

Limitations

  • Only supports Shift_JIS ↔ UTF-8 conversion (no other encodings)
  • No support for JIS X 0213 plane 2 characters
  • No locale-specific handling
  • Error handling is basic (returns -1 on any error)

About

A lightweight library for converting text between Shift_JIS and UTF-8 encodings

Resources

Stars

Watchers

Forks

Languages