2010-10-12 15:56:41 +02:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-06-22 15:19:32 +01:00
|
|
|
/*
|
|
|
|
* This file is part of the LibreOffice project.
|
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*
|
|
|
|
* This file incorporates work covered by the following license notice:
|
|
|
|
*
|
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed
|
|
|
|
* with this work for additional information regarding copyright
|
|
|
|
* ownership. The ASF licenses this file to you under the Apache
|
|
|
|
* License, Version 2.0 (the "License"); you may not use this file
|
|
|
|
* except in compliance with the License. You may obtain a copy of
|
|
|
|
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
|
|
|
|
*/
|
2003-10-15 13:44:37 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
2013-05-08 15:47:24 +02:00
|
|
|
|
2003-10-15 13:44:37 +00:00
|
|
|
#ifdef WIN32
|
|
|
|
# include <io.h>
|
|
|
|
#else
|
|
|
|
# include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2012-03-02 17:01:52 +01:00
|
|
|
#include <osl/diagnose.h>
|
|
|
|
|
2003-10-15 13:44:37 +00:00
|
|
|
#include "hwplib.h"
|
|
|
|
#include "hgzip.h"
|
|
|
|
#include "hiodev.h"
|
|
|
|
#include "hwpfile.h"
|
|
|
|
#include "hstream.h"
|
|
|
|
|
|
|
|
const int BUFSIZE = 1024;
|
|
|
|
static uchar rBuf[BUFSIZE];
|
|
|
|
|
|
|
|
// HIODev abstract class
|
|
|
|
HIODev::HIODev()
|
|
|
|
{
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
HIODev::~HIODev()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void HIODev::init()
|
|
|
|
{
|
|
|
|
compressed = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int HIODev::read1b(void *ptr, int nmemb)
|
|
|
|
{
|
|
|
|
uchar *p = (uchar *) ptr;
|
|
|
|
int ii;
|
|
|
|
|
|
|
|
if (state())
|
|
|
|
return -1;
|
|
|
|
for (ii = 0; ii < nmemb; ii++)
|
|
|
|
{
|
2015-02-25 10:50:59 +00:00
|
|
|
if (!read1b(p[ii]))
|
|
|
|
break;
|
2003-10-15 13:44:37 +00:00
|
|
|
if (state())
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return ii;
|
|
|
|
}
|
|
|
|
|
|
|
|
int HIODev::read2b(void *ptr, int nmemb)
|
|
|
|
{
|
|
|
|
ushort *p = (ushort *) ptr;
|
|
|
|
int ii;
|
|
|
|
|
|
|
|
if (state())
|
|
|
|
return -1;
|
|
|
|
for (ii = 0; ii < nmemb; ii++)
|
|
|
|
{
|
2015-02-25 10:50:59 +00:00
|
|
|
if (!read2b(p[ii]))
|
|
|
|
break;
|
2003-10-15 13:44:37 +00:00
|
|
|
if (state())
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return ii;
|
|
|
|
}
|
|
|
|
|
|
|
|
int HIODev::read4b(void *ptr, int nmemb)
|
|
|
|
{
|
2015-02-25 10:50:59 +00:00
|
|
|
uint *p = (uint *) ptr;
|
2003-10-15 13:44:37 +00:00
|
|
|
int ii;
|
|
|
|
|
|
|
|
if (state())
|
|
|
|
return -1;
|
|
|
|
for (ii = 0; ii < nmemb; ii++)
|
|
|
|
{
|
2015-02-25 10:50:59 +00:00
|
|
|
if (!read4b(p[ii]))
|
|
|
|
break;
|
2003-10-15 13:44:37 +00:00
|
|
|
if (state())
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return ii;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// hfileiodev class
|
2014-07-03 09:10:17 +02:00
|
|
|
HStreamIODev::HStreamIODev(HStream * stream):_stream(stream)
|
2003-10-15 13:44:37 +00:00
|
|
|
{
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
HStreamIODev::~HStreamIODev()
|
|
|
|
{
|
|
|
|
close();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void HStreamIODev::init()
|
|
|
|
{
|
|
|
|
_gzfp = NULL;
|
|
|
|
compressed = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool HStreamIODev::open()
|
|
|
|
{
|
2014-07-03 09:10:17 +02:00
|
|
|
if (!(_stream->available()))
|
2003-10-15 13:44:37 +00:00
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void HStreamIODev::flush(void)
|
|
|
|
{
|
|
|
|
if (_gzfp)
|
|
|
|
gz_flush(_gzfp, Z_FINISH);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void HStreamIODev::close(void)
|
|
|
|
{
|
2014-04-19 21:09:51 +02:00
|
|
|
/* 플러시한 후 닫는다. */
|
2003-10-15 13:44:37 +00:00
|
|
|
this->flush();
|
|
|
|
if (_gzfp)
|
2014-07-03 09:17:53 +02:00
|
|
|
gz_close(_gzfp);
|
2003-10-15 13:44:37 +00:00
|
|
|
_gzfp = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int HStreamIODev::state(void) const
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-19 21:09:51 +02:00
|
|
|
/* zlib 관련 부분 */
|
2003-10-15 13:44:37 +00:00
|
|
|
bool HStreamIODev::setCompressed(bool flag)
|
|
|
|
{
|
|
|
|
compressed = flag;
|
|
|
|
if (flag == true)
|
2014-07-03 09:10:17 +02:00
|
|
|
return 0 != (_gzfp = gz_open(*_stream));
|
2003-10-15 13:44:37 +00:00
|
|
|
else if (_gzfp)
|
|
|
|
{
|
|
|
|
gz_flush(_gzfp, Z_FINISH);
|
|
|
|
gz_close(_gzfp);
|
|
|
|
_gzfp = 0;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// IO routines
|
|
|
|
|
|
|
|
#define GZREAD(ptr,len) (_gzfp?gz_read(_gzfp,ptr,len):0)
|
|
|
|
|
2015-02-25 10:50:59 +00:00
|
|
|
bool HStreamIODev::read1b(unsigned char &out)
|
2003-10-15 13:44:37 +00:00
|
|
|
{
|
2014-07-03 09:10:17 +02:00
|
|
|
int res = (compressed) ? GZREAD(rBuf, 1) : _stream->readBytes(rBuf, 1);
|
2003-10-15 13:44:37 +00:00
|
|
|
|
2015-02-25 10:50:59 +00:00
|
|
|
if (res < 1)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
out = (unsigned char)rBuf[0];
|
|
|
|
return true;
|
2003-10-15 13:44:37 +00:00
|
|
|
}
|
|
|
|
|
2015-02-25 10:50:59 +00:00
|
|
|
bool HStreamIODev::read1b(char &out)
|
|
|
|
{
|
|
|
|
unsigned char tmp8;
|
|
|
|
if (!read1b(tmp8))
|
|
|
|
return false;
|
|
|
|
out = tmp8;
|
|
|
|
return true;
|
|
|
|
}
|
2003-10-15 13:44:37 +00:00
|
|
|
|
2015-02-25 10:50:59 +00:00
|
|
|
bool HStreamIODev::read2b(unsigned short &out)
|
2003-10-15 13:44:37 +00:00
|
|
|
{
|
2014-07-03 09:10:17 +02:00
|
|
|
int res = (compressed) ? GZREAD(rBuf, 2) : _stream->readBytes(rBuf, 2);
|
2003-10-15 13:44:37 +00:00
|
|
|
|
2015-02-25 10:50:59 +00:00
|
|
|
if (res < 2)
|
|
|
|
return false;
|
2003-10-15 13:44:37 +00:00
|
|
|
|
2015-02-25 10:50:59 +00:00
|
|
|
out = ((unsigned char) rBuf[1] << 8 | (unsigned char) rBuf[0]);
|
|
|
|
return true;
|
|
|
|
}
|
2003-10-15 13:44:37 +00:00
|
|
|
|
2015-02-25 10:50:59 +00:00
|
|
|
bool HStreamIODev::read4b(unsigned int &out)
|
2003-10-15 13:44:37 +00:00
|
|
|
{
|
2014-07-03 09:10:17 +02:00
|
|
|
int res = (compressed) ? GZREAD(rBuf, 4) : _stream->readBytes(rBuf, 4);
|
2003-10-15 13:44:37 +00:00
|
|
|
|
2015-02-25 10:50:59 +00:00
|
|
|
if (res < 4)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
out = ((unsigned char) rBuf[3] << 24 | (unsigned char) rBuf[2] << 16 |
|
|
|
|
(unsigned char) rBuf[1] << 8 | (unsigned char) rBuf[0]);
|
|
|
|
return true;
|
2003-10-15 13:44:37 +00:00
|
|
|
}
|
|
|
|
|
2015-02-25 10:50:59 +00:00
|
|
|
bool HStreamIODev::read4b(int &out)
|
|
|
|
{
|
|
|
|
unsigned int tmp32;
|
|
|
|
if (!read4b(tmp32))
|
|
|
|
return false;
|
|
|
|
out = tmp32;
|
|
|
|
return true;
|
|
|
|
}
|
2003-10-15 13:44:37 +00:00
|
|
|
|
|
|
|
int HStreamIODev::readBlock(void *ptr, int size)
|
|
|
|
{
|
|
|
|
int count =
|
2014-07-03 09:10:17 +02:00
|
|
|
(compressed) ? GZREAD(ptr, size) : _stream->readBytes((byte *) ptr,
|
2003-10-15 13:44:37 +00:00
|
|
|
|
|
|
|
size);
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
int HStreamIODev::skipBlock(int size)
|
|
|
|
{
|
|
|
|
if (compressed){
|
|
|
|
if( size <= BUFSIZE )
|
|
|
|
return GZREAD(rBuf, size);
|
|
|
|
else{
|
|
|
|
int remain = size;
|
|
|
|
while(remain){
|
|
|
|
if( remain > BUFSIZE )
|
|
|
|
remain -= GZREAD(rBuf, BUFSIZE);
|
|
|
|
else{
|
|
|
|
remain -= GZREAD(rBuf, remain);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return size - remain;
|
|
|
|
}
|
|
|
|
}
|
2014-07-03 09:10:17 +02:00
|
|
|
return _stream->skipBytes(size);
|
2003-10-15 13:44:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
HMemIODev::HMemIODev(char *s, int len)
|
|
|
|
{
|
|
|
|
init();
|
2015-01-17 18:47:35 +01:00
|
|
|
ptr = reinterpret_cast<uchar *>(s);
|
2003-10-15 13:44:37 +00:00
|
|
|
length = len;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
HMemIODev::~HMemIODev()
|
|
|
|
{
|
|
|
|
close();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void HMemIODev::init()
|
|
|
|
{
|
|
|
|
ptr = 0;
|
|
|
|
length = 0;
|
|
|
|
pos = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool HMemIODev::open()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void HMemIODev::flush(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void HMemIODev::close(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int HMemIODev::state(void) const
|
|
|
|
{
|
|
|
|
if (pos <= length)
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-06-19 23:53:55 +00:00
|
|
|
bool HMemIODev::setCompressed(bool )
|
2003-10-15 13:44:37 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-02-25 10:50:59 +00:00
|
|
|
bool HMemIODev::read1b(unsigned char &out)
|
2003-10-15 13:44:37 +00:00
|
|
|
{
|
|
|
|
if (pos <= length)
|
2015-02-25 10:50:59 +00:00
|
|
|
{
|
|
|
|
out = ptr[pos++];
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2003-10-15 13:44:37 +00:00
|
|
|
}
|
|
|
|
|
2015-02-25 10:50:59 +00:00
|
|
|
bool HMemIODev::read1b(char &out)
|
|
|
|
{
|
|
|
|
unsigned char tmp8;
|
|
|
|
if (!read1b(tmp8))
|
|
|
|
return false;
|
|
|
|
out = tmp8;
|
|
|
|
return true;
|
|
|
|
}
|
2003-10-15 13:44:37 +00:00
|
|
|
|
2015-02-25 10:50:59 +00:00
|
|
|
bool HMemIODev::read2b(unsigned short &out)
|
2003-10-15 13:44:37 +00:00
|
|
|
{
|
|
|
|
pos += 2;
|
|
|
|
if (pos <= length)
|
2015-02-25 10:50:59 +00:00
|
|
|
{
|
|
|
|
out = ptr[pos - 1] << 8 | ptr[pos - 2];
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2003-10-15 13:44:37 +00:00
|
|
|
}
|
|
|
|
|
2015-02-25 10:50:59 +00:00
|
|
|
bool HMemIODev::read4b(unsigned int &out)
|
2003-10-15 13:44:37 +00:00
|
|
|
{
|
|
|
|
pos += 4;
|
|
|
|
if (pos <= length)
|
2015-02-25 10:50:59 +00:00
|
|
|
{
|
|
|
|
out = static_cast<unsigned int>(ptr[pos - 1] << 24 | ptr[pos - 2] << 16 |
|
|
|
|
ptr[pos - 3] << 8 | ptr[pos - 4]);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2003-10-15 13:44:37 +00:00
|
|
|
}
|
|
|
|
|
2015-02-25 10:50:59 +00:00
|
|
|
bool HMemIODev::read4b(int &out)
|
|
|
|
{
|
|
|
|
unsigned int tmp32;
|
|
|
|
if (!read4b(tmp32))
|
|
|
|
return false;
|
|
|
|
out = tmp32;
|
|
|
|
return true;
|
|
|
|
}
|
2003-10-15 13:44:37 +00:00
|
|
|
|
|
|
|
int HMemIODev::readBlock(void *p, int size)
|
|
|
|
{
|
|
|
|
if (length < pos + size)
|
|
|
|
size = length - pos;
|
|
|
|
memcpy(p, ptr + pos, size);
|
|
|
|
pos += size;
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
int HMemIODev::skipBlock(int size)
|
|
|
|
{
|
|
|
|
if (length < pos + size)
|
|
|
|
return 0;
|
|
|
|
pos += size;
|
|
|
|
return size;
|
|
|
|
}
|
2010-10-12 15:56:41 +02:00
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|