ホーム › Storage.Compression › Compress
Compress
関数圧縮器でデータを圧縮しバッファへ出力する。
シグネチャ
// Cabinet.dll
#include <windows.h>
BOOL Compress(
COMPRESSOR_HANDLE CompressorHandle,
const void* UncompressedData, // optional
UINT_PTR UncompressedDataSize,
void* CompressedBuffer, // optional
UINT_PTR CompressedBufferSize,
UINT_PTR* CompressedDataSize
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| CompressorHandle | COMPRESSOR_HANDLE | in | 圧縮処理に使用する圧縮ハンドル。 |
| UncompressedData | void* | inoptional | 圧縮元となる非圧縮データバッファへのポインタ。 |
| UncompressedDataSize | UINT_PTR | in | UncompressedDataのバイト単位のサイズ。 |
| CompressedBuffer | void* | outoptional | 圧縮結果を格納する出力バッファへのポインタ。サイズ算出時はNULL可。 |
| CompressedBufferSize | UINT_PTR | in | CompressedBufferのバイト単位の容量。 |
| CompressedDataSize | UINT_PTR* | out | 実際の圧縮後データサイズを受け取るUINT_PTRへのポインタ。出力用。 |
戻り値の型: BOOL
各言語での呼び出し定義
// Cabinet.dll
#include <windows.h>
BOOL Compress(
COMPRESSOR_HANDLE CompressorHandle,
const void* UncompressedData, // optional
UINT_PTR UncompressedDataSize,
void* CompressedBuffer, // optional
UINT_PTR CompressedBufferSize,
UINT_PTR* CompressedDataSize
);[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("Cabinet.dll", SetLastError = true, ExactSpelling = true)]
static extern bool Compress(
IntPtr CompressorHandle, // COMPRESSOR_HANDLE
IntPtr UncompressedData, // void* optional
UIntPtr UncompressedDataSize, // UINT_PTR
IntPtr CompressedBuffer, // void* optional, out
UIntPtr CompressedBufferSize, // UINT_PTR
out UIntPtr CompressedDataSize // UINT_PTR* out
);<DllImport("Cabinet.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function Compress(
CompressorHandle As IntPtr, ' COMPRESSOR_HANDLE
UncompressedData As IntPtr, ' void* optional
UncompressedDataSize As UIntPtr, ' UINT_PTR
CompressedBuffer As IntPtr, ' void* optional, out
CompressedBufferSize As UIntPtr, ' UINT_PTR
<Out> ByRef CompressedDataSize As UIntPtr ' UINT_PTR* out
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function' CompressorHandle : COMPRESSOR_HANDLE
' UncompressedData : void* optional
' UncompressedDataSize : UINT_PTR
' CompressedBuffer : void* optional, out
' CompressedBufferSize : UINT_PTR
' CompressedDataSize : UINT_PTR* out
Declare PtrSafe Function Compress Lib "cabinet" ( _
ByVal CompressorHandle As LongPtr, _
ByVal UncompressedData As LongPtr, _
ByVal UncompressedDataSize As LongPtr, _
ByVal CompressedBuffer As LongPtr, _
ByVal CompressedBufferSize As LongPtr, _
ByRef CompressedDataSize As LongPtr) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。import ctypes
from ctypes import wintypes
Compress = ctypes.windll.cabinet.Compress
Compress.restype = wintypes.BOOL
Compress.argtypes = [
wintypes.HANDLE, # CompressorHandle : COMPRESSOR_HANDLE
ctypes.POINTER(None), # UncompressedData : void* optional
ctypes.c_size_t, # UncompressedDataSize : UINT_PTR
ctypes.POINTER(None), # CompressedBuffer : void* optional, out
ctypes.c_size_t, # CompressedBufferSize : UINT_PTR
ctypes.POINTER(ctypes.c_size_t), # CompressedDataSize : UINT_PTR* out
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('Cabinet.dll')
Compress = Fiddle::Function.new(
lib['Compress'],
[
Fiddle::TYPE_VOIDP, # CompressorHandle : COMPRESSOR_HANDLE
Fiddle::TYPE_VOIDP, # UncompressedData : void* optional
Fiddle::TYPE_UINTPTR_T, # UncompressedDataSize : UINT_PTR
Fiddle::TYPE_VOIDP, # CompressedBuffer : void* optional, out
Fiddle::TYPE_UINTPTR_T, # CompressedBufferSize : UINT_PTR
Fiddle::TYPE_VOIDP, # CompressedDataSize : UINT_PTR* out
],
Fiddle::TYPE_INT)#[link(name = "cabinet")]
extern "system" {
fn Compress(
CompressorHandle: *mut core::ffi::c_void, // COMPRESSOR_HANDLE
UncompressedData: *const (), // void* optional
UncompressedDataSize: usize, // UINT_PTR
CompressedBuffer: *mut (), // void* optional, out
CompressedBufferSize: usize, // UINT_PTR
CompressedDataSize: *mut usize // UINT_PTR* out
) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.$sig = @"
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("Cabinet.dll", SetLastError = true)]
public static extern bool Compress(IntPtr CompressorHandle, IntPtr UncompressedData, UIntPtr UncompressedDataSize, IntPtr CompressedBuffer, UIntPtr CompressedBufferSize, out UIntPtr CompressedDataSize);
"@
$api = Add-Type -MemberDefinition $sig -Name 'Cabinet_Compress' -Namespace Win32 -PassThru
# $api::Compress(CompressorHandle, UncompressedData, UncompressedDataSize, CompressedBuffer, CompressedBufferSize, CompressedDataSize)#uselib "Cabinet.dll"
#func global Compress "Compress" sptr, sptr, sptr, sptr, sptr, sptr
; Compress CompressorHandle, UncompressedData, UncompressedDataSize, CompressedBuffer, CompressedBufferSize, varptr(CompressedDataSize) ; 戻り値は stat
; CompressorHandle : COMPRESSOR_HANDLE -> "sptr"
; UncompressedData : void* optional -> "sptr"
; UncompressedDataSize : UINT_PTR -> "sptr"
; CompressedBuffer : void* optional, out -> "sptr"
; CompressedBufferSize : UINT_PTR -> "sptr"
; CompressedDataSize : UINT_PTR* out -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。出力引数:
#uselib "Cabinet.dll" #cfunc global Compress "Compress" sptr, sptr, sptr, sptr, sptr, var ; res = Compress(CompressorHandle, UncompressedData, UncompressedDataSize, CompressedBuffer, CompressedBufferSize, CompressedDataSize) ; CompressorHandle : COMPRESSOR_HANDLE -> "sptr" ; UncompressedData : void* optional -> "sptr" ; UncompressedDataSize : UINT_PTR -> "sptr" ; CompressedBuffer : void* optional, out -> "sptr" ; CompressedBufferSize : UINT_PTR -> "sptr" ; CompressedDataSize : UINT_PTR* out -> "var" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。#uselib "Cabinet.dll" #cfunc global Compress "Compress" sptr, sptr, sptr, sptr, sptr, sptr ; res = Compress(CompressorHandle, UncompressedData, UncompressedDataSize, CompressedBuffer, CompressedBufferSize, varptr(CompressedDataSize)) ; CompressorHandle : COMPRESSOR_HANDLE -> "sptr" ; UncompressedData : void* optional -> "sptr" ; UncompressedDataSize : UINT_PTR -> "sptr" ; CompressedBuffer : void* optional, out -> "sptr" ; CompressedBufferSize : UINT_PTR -> "sptr" ; CompressedDataSize : UINT_PTR* out -> "sptr" ; ※出力/バッファ引数はポインタ方式(token=sptr / 呼び出しは varptr(変数))。
出力引数:
; BOOL Compress(COMPRESSOR_HANDLE CompressorHandle, void* UncompressedData, UINT_PTR UncompressedDataSize, void* CompressedBuffer, UINT_PTR CompressedBufferSize, UINT_PTR* CompressedDataSize) #uselib "Cabinet.dll" #cfunc global Compress "Compress" intptr, intptr, intptr, intptr, intptr, var ; res = Compress(CompressorHandle, UncompressedData, UncompressedDataSize, CompressedBuffer, CompressedBufferSize, CompressedDataSize) ; CompressorHandle : COMPRESSOR_HANDLE -> "intptr" ; UncompressedData : void* optional -> "intptr" ; UncompressedDataSize : UINT_PTR -> "intptr" ; CompressedBuffer : void* optional, out -> "intptr" ; CompressedBufferSize : UINT_PTR -> "intptr" ; CompressedDataSize : UINT_PTR* out -> "var" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。; BOOL Compress(COMPRESSOR_HANDLE CompressorHandle, void* UncompressedData, UINT_PTR UncompressedDataSize, void* CompressedBuffer, UINT_PTR CompressedBufferSize, UINT_PTR* CompressedDataSize) #uselib "Cabinet.dll" #cfunc global Compress "Compress" intptr, intptr, intptr, intptr, intptr, intptr ; res = Compress(CompressorHandle, UncompressedData, UncompressedDataSize, CompressedBuffer, CompressedBufferSize, varptr(CompressedDataSize)) ; CompressorHandle : COMPRESSOR_HANDLE -> "intptr" ; UncompressedData : void* optional -> "intptr" ; UncompressedDataSize : UINT_PTR -> "intptr" ; CompressedBuffer : void* optional, out -> "intptr" ; CompressedBufferSize : UINT_PTR -> "intptr" ; CompressedDataSize : UINT_PTR* out -> "intptr" ; ※出力/バッファ引数はポインタ方式(token=intptr / 呼び出しは varptr(変数))。
import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
cabinet = windows.NewLazySystemDLL("Cabinet.dll")
procCompress = cabinet.NewProc("Compress")
)
// CompressorHandle (COMPRESSOR_HANDLE), UncompressedData (void* optional), UncompressedDataSize (UINT_PTR), CompressedBuffer (void* optional, out), CompressedBufferSize (UINT_PTR), CompressedDataSize (UINT_PTR* out)
r1, _, err := procCompress.Call(
uintptr(CompressorHandle),
uintptr(UncompressedData),
uintptr(UncompressedDataSize),
uintptr(CompressedBuffer),
uintptr(CompressedBufferSize),
uintptr(CompressedDataSize),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // BOOLfunction Compress(
CompressorHandle: THandle; // COMPRESSOR_HANDLE
UncompressedData: Pointer; // void* optional
UncompressedDataSize: NativeUInt; // UINT_PTR
CompressedBuffer: Pointer; // void* optional, out
CompressedBufferSize: NativeUInt; // UINT_PTR
CompressedDataSize: Pointer // UINT_PTR* out
): BOOL; stdcall;
external 'Cabinet.dll' name 'Compress';result := DllCall("Cabinet\Compress"
, "Ptr", CompressorHandle ; COMPRESSOR_HANDLE
, "Ptr", UncompressedData ; void* optional
, "UPtr", UncompressedDataSize ; UINT_PTR
, "Ptr", CompressedBuffer ; void* optional, out
, "UPtr", CompressedBufferSize ; UINT_PTR
, "Ptr", CompressedDataSize ; UINT_PTR* out
, "Int") ; return: BOOL●Compress(CompressorHandle, UncompressedData, UncompressedDataSize, CompressedBuffer, CompressedBufferSize, CompressedDataSize) = DLL("Cabinet.dll", "bool Compress(void*, void*, int, void*, int, void*)")
# 呼び出し: Compress(CompressorHandle, UncompressedData, UncompressedDataSize, CompressedBuffer, CompressedBufferSize, CompressedDataSize)
# CompressorHandle : COMPRESSOR_HANDLE -> "void*"
# UncompressedData : void* optional -> "void*"
# UncompressedDataSize : UINT_PTR -> "int"
# CompressedBuffer : void* optional, out -> "void*"
# CompressedBufferSize : UINT_PTR -> "int"
# CompressedDataSize : UINT_PTR* out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。const std = @import("std");
extern "cabinet" fn Compress(
CompressorHandle: ?*anyopaque, // COMPRESSOR_HANDLE
UncompressedData: ?*anyopaque, // void* optional
UncompressedDataSize: usize, // UINT_PTR
CompressedBuffer: ?*anyopaque, // void* optional, out
CompressedBufferSize: usize, // UINT_PTR
CompressedDataSize: [*c]usize // UINT_PTR* out
) callconv(std.os.windows.WINAPI) i32;proc Compress(
CompressorHandle: pointer, # COMPRESSOR_HANDLE
UncompressedData: pointer, # void* optional
UncompressedDataSize: uint, # UINT_PTR
CompressedBuffer: pointer, # void* optional, out
CompressedBufferSize: uint, # UINT_PTR
CompressedDataSize: ptr uint # UINT_PTR* out
): int32 {.importc: "Compress", stdcall, dynlib: "Cabinet.dll".}pragma(lib, "cabinet");
extern(Windows)
int Compress(
void* CompressorHandle, // COMPRESSOR_HANDLE
void* UncompressedData, // void* optional
size_t UncompressedDataSize, // UINT_PTR
void* CompressedBuffer, // void* optional, out
size_t CompressedBufferSize, // UINT_PTR
size_t* CompressedDataSize // UINT_PTR* out
);ccall((:Compress, "Cabinet.dll"), stdcall, Int32,
(Ptr{Cvoid}, Ptr{Cvoid}, Csize_t, Ptr{Cvoid}, Csize_t, Ptr{Csize_t}),
CompressorHandle, UncompressedData, UncompressedDataSize, CompressedBuffer, CompressedBufferSize, CompressedDataSize)
# CompressorHandle : COMPRESSOR_HANDLE -> Ptr{Cvoid}
# UncompressedData : void* optional -> Ptr{Cvoid}
# UncompressedDataSize : UINT_PTR -> Csize_t
# CompressedBuffer : void* optional, out -> Ptr{Cvoid}
# CompressedBufferSize : UINT_PTR -> Csize_t
# CompressedDataSize : UINT_PTR* out -> Ptr{Csize_t}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。local ffi = require("ffi")
ffi.cdef[[
int32_t Compress(
void* CompressorHandle,
void* UncompressedData,
uintptr_t UncompressedDataSize,
void* CompressedBuffer,
uintptr_t CompressedBufferSize,
uintptr_t* CompressedDataSize);
]]
local cabinet = ffi.load("cabinet")
-- cabinet.Compress(CompressorHandle, UncompressedData, UncompressedDataSize, CompressedBuffer, CompressedBufferSize, CompressedDataSize)
-- CompressorHandle : COMPRESSOR_HANDLE
-- UncompressedData : void* optional
-- UncompressedDataSize : UINT_PTR
-- CompressedBuffer : void* optional, out
-- CompressedBufferSize : UINT_PTR
-- CompressedDataSize : UINT_PTR* out
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。const koffi = require('koffi');
const lib = koffi.load('Cabinet.dll');
const Compress = lib.func('__stdcall', 'Compress', 'int32_t', ['void *', 'void *', 'uintptr_t', 'void *', 'uintptr_t', 'uintptr_t *']);
// Compress(CompressorHandle, UncompressedData, UncompressedDataSize, CompressedBuffer, CompressedBufferSize, CompressedDataSize)
// CompressorHandle : COMPRESSOR_HANDLE -> 'void *'
// UncompressedData : void* optional -> 'void *'
// UncompressedDataSize : UINT_PTR -> 'uintptr_t'
// CompressedBuffer : void* optional, out -> 'void *'
// CompressedBufferSize : UINT_PTR -> 'uintptr_t'
// CompressedDataSize : UINT_PTR* out -> 'uintptr_t *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("Cabinet.dll", {
Compress: { parameters: ["pointer", "pointer", "usize", "pointer", "usize", "pointer"], result: "i32" },
});
// lib.symbols.Compress(CompressorHandle, UncompressedData, UncompressedDataSize, CompressedBuffer, CompressedBufferSize, CompressedDataSize)
// CompressorHandle : COMPRESSOR_HANDLE -> "pointer"
// UncompressedData : void* optional -> "pointer"
// UncompressedDataSize : UINT_PTR -> "usize"
// CompressedBuffer : void* optional, out -> "pointer"
// CompressedBufferSize : UINT_PTR -> "usize"
// CompressedDataSize : UINT_PTR* out -> "pointer"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
int32_t Compress(
void* CompressorHandle,
void* UncompressedData,
size_t UncompressedDataSize,
void* CompressedBuffer,
size_t CompressedBufferSize,
size_t* CompressedDataSize);
C, "Cabinet.dll");
// $ffi->Compress(CompressorHandle, UncompressedData, UncompressedDataSize, CompressedBuffer, CompressedBufferSize, CompressedDataSize);
// CompressorHandle : COMPRESSOR_HANDLE
// UncompressedData : void* optional
// UncompressedDataSize : UINT_PTR
// CompressedBuffer : void* optional, out
// CompressedBufferSize : UINT_PTR
// CompressedDataSize : UINT_PTR* out
// 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
// WINAPI(stdcall): x64 では呼出規約が統一されるため問題なし。x86 では __stdcall 対応のラッパが必要な場合あり。import com.sun.jna.*;
import com.sun.jna.ptr.*;
import com.sun.jna.win32.StdCallLibrary;
import com.sun.jna.win32.W32APIOptions;
public interface Cabinet extends StdCallLibrary {
Cabinet INSTANCE = Native.load("cabinet", Cabinet.class);
boolean Compress(
Pointer CompressorHandle, // COMPRESSOR_HANDLE
Pointer UncompressedData, // void* optional
long UncompressedDataSize, // UINT_PTR
Pointer CompressedBuffer, // void* optional, out
long CompressedBufferSize, // UINT_PTR
LongByReference CompressedDataSize // UINT_PTR* out
);
}@[Link("cabinet")]
lib LibCabinet
fun Compress = Compress(
CompressorHandle : Void*, # COMPRESSOR_HANDLE
UncompressedData : Void*, # void* optional
UncompressedDataSize : LibC::SizeT, # UINT_PTR
CompressedBuffer : Void*, # void* optional, out
CompressedBufferSize : LibC::SizeT, # UINT_PTR
CompressedDataSize : LibC::SizeT* # UINT_PTR* out
) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。import 'dart:ffi';
import 'package:ffi/ffi.dart';
typedef CompressNative = Int32 Function(Pointer<Void>, Pointer<Void>, UintPtr, Pointer<Void>, UintPtr, Pointer<UintPtr>);
typedef CompressDart = int Function(Pointer<Void>, Pointer<Void>, int, Pointer<Void>, int, Pointer<UintPtr>);
final Compress = DynamicLibrary.open('Cabinet.dll')
.lookupFunction<CompressNative, CompressDart>('Compress');
// CompressorHandle : COMPRESSOR_HANDLE -> Pointer<Void>
// UncompressedData : void* optional -> Pointer<Void>
// UncompressedDataSize : UINT_PTR -> UintPtr
// CompressedBuffer : void* optional, out -> Pointer<Void>
// CompressedBufferSize : UINT_PTR -> UintPtr
// CompressedDataSize : UINT_PTR* out -> Pointer<UintPtr>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function Compress(
CompressorHandle: THandle; // COMPRESSOR_HANDLE
UncompressedData: Pointer; // void* optional
UncompressedDataSize: NativeUInt; // UINT_PTR
CompressedBuffer: Pointer; // void* optional, out
CompressedBufferSize: NativeUInt; // UINT_PTR
CompressedDataSize: Pointer // UINT_PTR* out
): BOOL; stdcall;
external 'Cabinet.dll' name 'Compress';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "Compress"
c_Compress :: Ptr () -> Ptr () -> CUIntPtr -> Ptr () -> CUIntPtr -> Ptr CUIntPtr -> IO CInt
-- CompressorHandle : COMPRESSOR_HANDLE -> Ptr ()
-- UncompressedData : void* optional -> Ptr ()
-- UncompressedDataSize : UINT_PTR -> CUIntPtr
-- CompressedBuffer : void* optional, out -> Ptr ()
-- CompressedBufferSize : UINT_PTR -> CUIntPtr
-- CompressedDataSize : UINT_PTR* out -> Ptr CUIntPtr
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let compress =
foreign "Compress"
((ptr void) @-> (ptr void) @-> size_t @-> (ptr void) @-> size_t @-> (ptr size_t) @-> returning int32_t)
(* CompressorHandle : COMPRESSOR_HANDLE -> (ptr void) *)
(* UncompressedData : void* optional -> (ptr void) *)
(* UncompressedDataSize : UINT_PTR -> size_t *)
(* CompressedBuffer : void* optional, out -> (ptr void) *)
(* CompressedBufferSize : UINT_PTR -> size_t *)
(* CompressedDataSize : UINT_PTR* out -> (ptr size_t) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)(cffi:define-foreign-library cabinet (t "Cabinet.dll"))
(cffi:use-foreign-library cabinet)
(cffi:defcfun ("Compress" compress :convention :stdcall) :int32
(compressor-handle :pointer) ; COMPRESSOR_HANDLE
(uncompressed-data :pointer) ; void* optional
(uncompressed-data-size :uint64) ; UINT_PTR
(compressed-buffer :pointer) ; void* optional, out
(compressed-buffer-size :uint64) ; UINT_PTR
(compressed-data-size :pointer)) ; UINT_PTR* out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $Compress = Win32::API::More->new('Cabinet',
'BOOL Compress(HANDLE CompressorHandle, LPVOID UncompressedData, WPARAM UncompressedDataSize, LPVOID CompressedBuffer, WPARAM CompressedBufferSize, LPVOID CompressedDataSize)');
# my $ret = $Compress->Call($CompressorHandle, $UncompressedData, $UncompressedDataSize, $CompressedBuffer, $CompressedBufferSize, $CompressedDataSize);
# CompressorHandle : COMPRESSOR_HANDLE -> HANDLE
# UncompressedData : void* optional -> LPVOID
# UncompressedDataSize : UINT_PTR -> WPARAM
# CompressedBuffer : void* optional, out -> LPVOID
# CompressedBufferSize : UINT_PTR -> WPARAM
# CompressedDataSize : UINT_PTR* out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。