LCOV - code coverage report
Current view: top level - libreoffice/hwpfilter/source - hiodev.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 76 154 49.4 %
Date: 2012-12-17 Functions: 19 35 54.3 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : #include <stdio.h>
      21             : #include <errno.h>
      22             : // DVO: add zlib/ prefix
      23             : #ifdef SYSTEM_ZLIB
      24             : #include <zlib.h>
      25             : #else
      26             : #include <zlib/zlib.h>
      27             : #endif
      28             : #ifdef WIN32
      29             : # include <io.h>
      30             : #else
      31             : # include <unistd.h>
      32             : #endif
      33             : 
      34             : #include <osl/diagnose.h>
      35             : 
      36             : #include "hwplib.h"
      37             : #include "hgzip.h"
      38             : #include "hiodev.h"
      39             : #include "hwpfile.h"
      40             : #include "hstream.h"
      41             : 
      42             : const int BUFSIZE = 1024;
      43             : static uchar rBuf[BUFSIZE];
      44             : 
      45             : // HIODev abstract class
      46           4 : HIODev::HIODev()
      47             : {
      48           4 :     init();
      49           4 : }
      50             : 
      51             : 
      52           4 : HIODev::~HIODev()
      53             : {
      54           4 : }
      55             : 
      56             : 
      57           4 : void HIODev::init()
      58             : {
      59           4 :     compressed = false;
      60           4 : }
      61             : 
      62             : 
      63        2822 : int HIODev::read1b(void *ptr, int nmemb)
      64             : {
      65        2822 :     uchar *p = (uchar *) ptr;
      66             :     int ii;
      67             : 
      68        2822 :     if (state())
      69           0 :         return -1;
      70        6550 :     for (ii = 0; ii < nmemb; ii++)
      71             :     {
      72        3728 :         p[ii] = sal::static_int_cast<uchar>(read1b());
      73        3728 :         if (state())
      74           0 :             break;
      75             :     }
      76        2822 :     return ii;
      77             : }
      78             : 
      79             : 
      80          64 : int HIODev::read2b(void *ptr, int nmemb)
      81             : {
      82          64 :     ushort *p = (ushort *) ptr;
      83             :     int ii;
      84             : 
      85          64 :     if (state())
      86           0 :         return -1;
      87        1118 :     for (ii = 0; ii < nmemb; ii++)
      88             :     {
      89        1054 :         p[ii] = sal::static_int_cast<uchar>(read2b());
      90        1054 :         if (state())
      91           0 :             break;
      92             :     }
      93          64 :     return ii;
      94             : }
      95             : 
      96             : 
      97           6 : int HIODev::read4b(void *ptr, int nmemb)
      98             : {
      99           6 :     ulong *p = (ulong *) ptr;
     100             :     int ii;
     101             : 
     102           6 :     if (state())
     103           0 :         return -1;
     104          12 :     for (ii = 0; ii < nmemb; ii++)
     105             :     {
     106           6 :         p[ii] = read4b();
     107           6 :         if (state())
     108           0 :             break;
     109             :     }
     110           6 :     return ii;
     111             : }
     112             : 
     113             : 
     114             : // hfileiodev class
     115           4 : HStreamIODev::HStreamIODev(HStream & stream):_stream(stream)
     116             : {
     117           4 :     init();
     118           4 : }
     119             : 
     120             : 
     121          12 : HStreamIODev::~HStreamIODev()
     122             : {
     123           4 :     close();
     124           8 : }
     125             : 
     126             : 
     127           4 : void HStreamIODev::init()
     128             : {
     129           4 :     _gzfp = NULL;
     130           4 :     compressed = false;
     131           4 : }
     132             : 
     133             : 
     134           4 : bool HStreamIODev::open()
     135             : {
     136           4 :     if (!(_stream.available()))
     137           0 :         return false;
     138           4 :     return true;
     139             : }
     140             : 
     141             : 
     142           4 : void HStreamIODev::flush(void)
     143             : {
     144           4 :     if (_gzfp)
     145           2 :         gz_flush(_gzfp, Z_FINISH);
     146           4 : }
     147             : 
     148             : 
     149           4 : void HStreamIODev::close(void)
     150             : {
     151             : /* Ç÷¯½ÃÇÑ ÈÄ ´Ý´Â´Ù. */
     152           4 :     this->flush();
     153           4 :     if (_gzfp)
     154           2 :         gz_close(_gzfp);                          /* gz_close() calls stream_closeInput() */
     155             :     else
     156           2 :         _stream.closeInput();
     157           4 :     _gzfp = NULL;
     158           4 : }
     159             : 
     160             : 
     161        7680 : int HStreamIODev::state(void) const
     162             : {
     163        7680 :     return 0;
     164             : }
     165             : 
     166             : 
     167             : /* zlib °ü·Ã ºÎºÐ */
     168           2 : bool HStreamIODev::setCompressed(bool flag)
     169             : {
     170           2 :     compressed = flag;
     171           2 :     if (flag == true)
     172           2 :         return 0 != (_gzfp = gz_open(_stream));
     173           0 :     else if (_gzfp)
     174             :     {
     175           0 :         gz_flush(_gzfp, Z_FINISH);
     176           0 :         gz_close(_gzfp);
     177           0 :         _gzfp = 0;
     178             :     }
     179           0 :     return true;
     180             : }
     181             : 
     182             : 
     183             : // IO routines
     184             : 
     185             : #define GZREAD(ptr,len) (_gzfp?gz_read(_gzfp,ptr,len):0)
     186             : 
     187        3728 : int HStreamIODev::read1b()
     188             : {
     189        3728 :     int res = (compressed) ? GZREAD(rBuf, 1) : _stream.readBytes(rBuf, 1);
     190             : 
     191        3728 :     if (res <= 0)
     192           0 :         return -1;
     193             :     else
     194        3728 :         return (unsigned char) rBuf[0];
     195             : }
     196             : 
     197             : 
     198        2590 : int HStreamIODev::read2b()
     199             : {
     200        2590 :     int res = (compressed) ? GZREAD(rBuf, 2) : _stream.readBytes(rBuf, 2);
     201             : 
     202        2590 :     if (res <= 0)
     203           0 :         return -1;
     204             :     else
     205        2590 :         return ((unsigned char) rBuf[1] << 8 | (unsigned char) rBuf[0]);
     206             : }
     207             : 
     208             : 
     209          10 : long HStreamIODev::read4b()
     210             : {
     211          10 :     int res = (compressed) ? GZREAD(rBuf, 4) : _stream.readBytes(rBuf, 4);
     212             : 
     213          10 :     if (res <= 0)
     214           0 :         return -1;
     215             :     else
     216          20 :         return ((unsigned char) rBuf[3] << 24 | (unsigned char) rBuf[2] << 16 |
     217          20 :             (unsigned char) rBuf[1] << 8 | (unsigned char) rBuf[0]);
     218             : }
     219             : 
     220             : 
     221        1434 : int HStreamIODev::readBlock(void *ptr, int size)
     222             : {
     223             :     int count =
     224        1430 :         (compressed) ? GZREAD(ptr, size) : _stream.readBytes((byte *) ptr,
     225             : 
     226        2864 :         size);
     227             : 
     228        1434 :     return count;
     229             : }
     230             : 
     231             : 
     232           0 : int HStreamIODev::skipBlock(int size)
     233             : {
     234           0 :     if (compressed){
     235           0 :           if( size <= BUFSIZE )
     236           0 :                 return GZREAD(rBuf, size);
     237             :           else{
     238           0 :                 int remain = size;
     239           0 :                 while(remain){
     240           0 :                      if( remain > BUFSIZE )
     241           0 :                           remain -= GZREAD(rBuf, BUFSIZE);
     242             :                      else{
     243           0 :                           remain -= GZREAD(rBuf, remain);
     244           0 :                           break;
     245             :                      }
     246             :                 }
     247           0 :                 return size - remain;
     248             :           }
     249             :      }
     250           0 :     return _stream.skipBytes(size);
     251             : }
     252             : 
     253             : 
     254           0 : HMemIODev::HMemIODev(char *s, int len)
     255             : {
     256           0 :     init();
     257           0 :     ptr = (uchar *) s;
     258           0 :     length = len;
     259           0 : }
     260             : 
     261             : 
     262           0 : HMemIODev::~HMemIODev()
     263             : {
     264           0 :     close();
     265           0 : }
     266             : 
     267             : 
     268           0 : void HMemIODev::init()
     269             : {
     270           0 :     ptr = 0;
     271           0 :     length = 0;
     272           0 :     pos = 0;
     273           0 : }
     274             : 
     275             : 
     276           0 : bool HMemIODev::open()
     277             : {
     278           0 :     return true;
     279             : }
     280             : 
     281             : 
     282           0 : void HMemIODev::flush(void)
     283             : {
     284           0 : }
     285             : 
     286             : 
     287           0 : void HMemIODev::close(void)
     288             : {
     289           0 : }
     290             : 
     291             : 
     292           0 : int HMemIODev::state(void) const
     293             : {
     294           0 :     if (pos <= length)
     295           0 :         return 0;
     296             :     else
     297           0 :         return -1;
     298             : }
     299             : 
     300             : 
     301           0 : bool HMemIODev::setCompressed(bool )
     302             : {
     303           0 :     return false;
     304             : }
     305             : 
     306             : 
     307           0 : int HMemIODev::read1b()
     308             : {
     309           0 :     if (pos <= length)
     310           0 :          return ptr[pos++];
     311             :      else
     312           0 :          return 0;
     313             : }
     314             : 
     315             : 
     316           0 : int HMemIODev::read2b()
     317             : {
     318           0 :     pos += 2;
     319           0 :     if (pos <= length)
     320           0 :          return ptr[pos - 1] << 8 | ptr[pos - 2];
     321             :      else
     322           0 :          return 0;
     323             : }
     324             : 
     325             : 
     326           0 : long HMemIODev::read4b()
     327             : {
     328           0 :     pos += 4;
     329           0 :     if (pos <= length)
     330           0 :          return DWORD(ptr[pos - 1] << 24 | ptr[pos - 2] << 16 |
     331           0 :         ptr[pos - 3] << 8 | ptr[pos - 4]);
     332             :      else
     333           0 :          return 0;
     334             : }
     335             : 
     336             : 
     337           0 : int HMemIODev::readBlock(void *p, int size)
     338             : {
     339           0 :     if (length < pos + size)
     340           0 :         size = length - pos;
     341           0 :     memcpy(p, ptr + pos, size);
     342           0 :     pos += size;
     343           0 :     return size;
     344             : }
     345             : 
     346             : 
     347           0 : int HMemIODev::skipBlock(int size)
     348             : {
     349           0 :     if (length < pos + size)
     350           0 :         return 0;
     351           0 :     pos += size;
     352           0 :     return size;
     353             : }
     354             : 
     355             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10