File: nsadec.cpp

package info (click to toggle)
onscripter 20120531-2
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 1,204 kB
  • sloc: cpp: 29,300; makefile: 81
file content (100 lines) | stat: -rw-r--r-- 3,059 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/* -*- C++ -*-
 * 
 *  nsadec.cpp - NSA archive decoder
 *
 *  Copyright (c) 2001-2010 Ogapee. All rights reserved.
 *
 *  ogapee@aqua.dti2.ne.jp
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include "NsaReader.h"

extern int errno;

int main( int argc, char **argv )
{
    NsaReader cNR;
    int nsa_offset = 0;
    unsigned long length;
    unsigned char *buffer;
    char file_name[256], dir_name[256];
    unsigned int i, j, count;
    int archive_type = BaseReader::ARCHIVE_TYPE_NSA;
    FILE *fp;
    struct stat file_stat;

    if ( argc >= 2 ){
        while ( argc > 2 ){
            if      ( !strcmp( argv[1], "-ns2" ) ) nsa_offset = 1;
            else if ( !strcmp( argv[1], "-ns3" ) ) nsa_offset = 2;
            argc--;
            argv++;
        }
    }
    if ( argc != 2 ){
        fprintf( stderr, "Usage: nsadec [-ns2] [-ns3] arc_file\n");
        exit(-1);
    }
    cNR.openForConvert( argv[1], archive_type, nsa_offset );
    count = cNR.getNumFiles();
    
    SarReader::FileInfo sFI;

    for ( i=0 ; i<count ; i++ ){
        sFI = cNR.getFileByIndex( i );
        length = cNR.getFileLength( sFI.name );
        buffer = new unsigned char[length];
        unsigned int len;
        if ( (len = cNR.getFile( sFI.name, buffer )) != length ){
            //fprintf( stderr, "file %s can't be retrieved\n", sFI.name );
            fprintf( stderr, "file %s is not fully retrieved %d %lu\n", sFI.name, len, length  );
            length = sFI.length;
            //continue;
        }

        strcpy( file_name, sFI.name );
        for ( j=0 ; j<strlen(file_name) ; j++ ){
            if ( file_name[j] == '\\' ){
                file_name[j] = '/';
                strncpy( dir_name, file_name, j );
                dir_name[j] = '\0';

                /* If the directory does'nt exist, create it */
                if ( stat ( dir_name, &file_stat ) == -1 && errno == ENOENT )
                    mkdir( dir_name, 00755 );
            }
        }
    
        if ( (fp = fopen( file_name, "wb" ) )){
            fwrite( buffer, 1, length, fp );
            fclose(fp);
        }
        else{
            printf("opening %s ... falied\n", file_name );
        }
        
        delete[] buffer;
    }
    
    exit(0);
}