代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
C
】
RLE编码
作者:
李海俊
/ 发布于
2011/10/9
/
634
<div>?/*---rle.c--------------------------------------------------------- * Decode & Encode a file for RLE. * * Entry: RLEEncode(IN unsigned char *infile,// input file buffer * IN unsigned long inlen, // input length * IN unsigned char *outfile, // output file buffer * IN&OUT unsigned long *outlen, // output length * IN unsigned char sentinel1, // default sentinel * IN unsigned char sentinel2 // changed sentinel * ); * * RLEEncode() returns: * -1: output buffer too small * 0: success * * Entry: RLEDecode(IN unsigned char *infile, // input file buffer * IN unsigned long inlen, // input length * IN unsigned char *outfile, // output file length * IN&OUT unsigned long *outlen // output length * ); * * RLEDecode() returns: * 0: success * 1: outlen too small, and return outlen was needed; * *---------------------------------------------------------------*/ #include <windows.h> #include <stdio.h></div> <div>int RLEEncode( unsigned char *infile, unsigned long inlen, unsigned char *outfile, unsigned long *outlen, unsigned char sentinel1, unsigned char sentinel2) { unsigned char sentinel, prev_char; unsigned long i, j; unsigned short count; sentinel = sentinel1;</div> <div> i = 0; j = 0; count = 1; outfile[ j++ ] = 'R'; outfile[ j++ ] = 'L'; outfile[ j++ ] = 'E'; outfile[ j++ ] = sentinel1; outfile[ j++ ] = sentinel2; *(unsigned long *)&outfile[ j ] = inlen; j += sizeof( unsigned long );</div> <div> while ( i <= inlen ) { if ( j > *outlen ) { return -1; } count = 1; prev_char = infile[ i++ ]; if ( prev_char == sentinel ) { outfile[ j++ ] = sentinel; outfile[ j++ ] = 0x00; if ( sentinel == sentinel1 ) { outfile[ j++ ] = sentinel2; sentinel = sentinel2; } else { outfile[ j++ ] = sentinel1; sentinel = sentinel1; } } while ( prev_char == infile[ i ] ) { i++; count++; if ( i > inlen ) { break; } if ( count == 255 ) { break; } } switch ( count ) { case 1: outfile[ j++ ] = prev_char; break; case 2: outfile[ j++ ] = prev_char; outfile[ j++ ] = prev_char; break; default: outfile[ j++ ] = sentinel; outfile[ j++ ] = count; outfile[ j++ ] = prev_char; break; } } *outlen = j; return 0; } <div>int RLEDecode( unsigned char *infile, unsigned long inlen, unsigned char *outfile, unsigned long *outlen ) { unsigned long i, j, len; unsigned char default_sentinel, change_sentinel, prev_char; unsigned short count; i = 0; j = 0; if ( infile[ i++ ] == 'R' &&\ infile[i++] == 'L' && \ infile[i++] == 'E') { default_sentinel = infile[ i++ ]; change_sentinel = infile[ i++ ]; len = *( unsigned long * )&infile[ i ]; i += sizeof ( unsigned long ); if ( *outlen < len ) { *outlen = len; return 1; } } else { return 2; } while ( i < inlen ) { prev_char = infile[ i++ ]; count = 1; if ( prev_char == default_sentinel ) { if ( infile[ i ] == 0x00 ) { i++; if ( change_sentinel == infile[ i ] ) { change_sentinel = default_sentinel; default_sentinel = infile[ i++ ]; continue; } else { // error } } else { count = infile[ i ]; i++; prev_char = infile[ i++ ]; } } do { outfile[ j++ ] = prev_char; } while ( --count ); } return 0; } <div>int main ( int argc, char *argv[] ) { char *szinfile = "C:\\1.bin"; char *szoutfile = "C:\\2.bin"; HANDLE hinfile, houtfile; HANDLE hmapinfile; unsigned char *lpinfile, *lpoutfile; unsigned long dwinfile_size = 0, dwoutfile_size = 0, dwwrite = 0, save_size = 0;</div> <div>hinfile = CreateFileA ( szinfile, GENERIC_READ, FILE_SHARE_READ,\ 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0 );</div> <div>dwinfile_size = GetFileSize ( hinfile, NULL );</div> <div>hmapinfile = CreateFileMapping ( hinfile, NULL, PAGE_READONLY, \ 0, 0, NULL );</div> <div>lpinfile = ( unsigned char * ) MapViewOfFile ( hmapinfile, FILE_MAP_READ, 0, 0, 0 );</div> <div>RLEDecode ( lpinfile, dwinfile_size, 0 ,&dwoutfile_size ); //save_size = dwoutfile_size = dwinfile_size * 2;</div> <div>lpoutfile = ( unsigned char * ) VirtualAlloc ( NULL, dwoutfile_size, \ MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE );</div> <div>//RLEEncode ( lpinfile, dwinfile_size, lpoutfile, &dwoutfile_size, 'A', 'B' ); RLEDecode ( lpinfile, dwinfile_size, lpoutfile, &dwoutfile_size );</div> <div>houtfile = CreateFileA ( szoutfile, GENERIC_WRITE, FILE_SHARE_READ, \ 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0 );</div> <div>WriteFile ( houtfile, lpoutfile, dwoutfile_size, &dwwrite, NULL ); VirtualFree ( lpoutfile, save_size, MEM_DECOMMIT );</div> <div>CloseHandle ( houtfile );</div> <div>UnmapViewOfFile ( lpinfile ); CloseHandle ( hmapinfile ); CloseHandle ( hinfile ); return 0; }
试试其它关键字
RLE编码
同语言下
.
获取手机通讯录 iOS去除数字以外的所有字符
.
异步加载音乐等资源
.
交通罚单管理系统
.
freemark实现,简单的替换
.
计算斐波那契数列
.
base64解码 包括解码长度
.
图像显示
.
冒泡排序
.
输入十进制数,输出指定进制
.
链式栈
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
李海俊
贡献的其它代码
(
1
)
.
RLE编码
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3