Пример #1
0
void Value::doConvert(Type newType) {
    switch (type) {
    case TYPE_I32:
        type = newType;
        switch (newType) {
        case TYPE_I8:  constant.i8  =  S08(constant.i32); return;
        case TYPE_I16: constant.i16 = S16(constant.i32); return;
        case TYPE_I32: constant.i32 = S32(constant.i32); return;
        case TYPE_I64: constant.i64 = S64(constant.i32); return;
        case TYPE_F32: constant.f32 = F32(constant.i32); return;
        case TYPE_F64: constant.f64 = F64(constant.i32); return;
        default:
            assert_always("Unimplemented case");
            return;
        }
    case TYPE_I64:
        type = newType;
        switch (newType) {
        case TYPE_I8:  constant.i8  =  S08(constant.i64); return;
        case TYPE_I16: constant.i16 = S16(constant.i64); return;
        case TYPE_I32: constant.i32 = S32(constant.i64); return;
        case TYPE_I64: constant.i64 = S64(constant.i64); return;
        case TYPE_F32: constant.f32 = F32(constant.i64); return;
        case TYPE_F64: constant.f64 = F64(constant.i64); return;
        default:
            assert_always("Unimplemented case");
            return;
        }
    case TYPE_F32:
        type = newType;
        switch (newType) {
        case TYPE_I8:  constant.i8  =  S08(constant.f32); return;
        case TYPE_I16: constant.i16 = S16(constant.f32); return;
        case TYPE_I32: constant.i32 = S32(constant.f32); return;
        case TYPE_I64: constant.i64 = S64(constant.f32); return;
        case TYPE_F32: constant.f32 = F32(constant.f32); return;
        case TYPE_F64: constant.f64 = F64(constant.f32); return;
        default:
            assert_always("Unimplemented case");
            return;
        }
    case TYPE_F64:
        type = newType;
        switch (newType) {
        case TYPE_I8:  constant.i8  =  S08(constant.f64); return;
        case TYPE_I16: constant.i16 = S16(constant.f64); return;
        case TYPE_I32: constant.i32 = S32(constant.f64); return;
        case TYPE_I64: constant.i64 = S64(constant.f64); return;
        case TYPE_F32: constant.f32 = F32(constant.f64); return;
        case TYPE_F64: constant.f64 = F64(constant.f64); return;
        default:
            assert_always("Unimplemented case");
            return;
        }
    default:
        assert_always("Unimplemented case");
        return;
    }
}
Пример #2
0
inline bool TblIndexInsertValid(JNIEnv* env, T* pTable, jlong columnIndex, jlong rowIndex)
{
    if (!TblColIndexValid(env, pTable, columnIndex))
        return false;
    bool rowErr = realm::util::int_greater_than(rowIndex, pTable->size()+1);
    if (rowErr) {
        TR_ERR("rowIndex %" PRId64 " > %" PRId64 " - invalid!", S64(rowIndex), S64(pTable->size()))
        ThrowException(env, IndexOutOfBounds,
            "rowIndex " + num_to_string(rowIndex) +
            " > available rows " + num_to_string(pTable->size()) + ".");
    }
    return !rowErr;
}
Пример #3
0
inline bool ColIndexValid(JNIEnv* env, T* pTable, jlong columnIndex)
{
    if (columnIndex < 0) {
        ThrowException(env, IndexOutOfBounds, "columnIndex is less than 0.");
        return false;
    }
    bool colErr = realm::util::int_greater_than_or_equal(columnIndex, pTable->get_column_count());
    if (colErr) {
        TR_ERR("columnIndex %" PRId64 " > %" PRId64 " - invalid!", S64(columnIndex), S64(pTable->get_column_count()))
        ThrowException(env, IndexOutOfBounds, "columnIndex > available columns.");
    }
    return !colErr;
}
 ENTER()
 try {
     LOG("iterations: %ld", S64(iterations))
     LOG("generators: %ld", S64(generators))
     Pi *pi = new Pi(static_cast<long>(iterations), static_cast<long>(generators));
     return reinterpret_cast<jlong>(pi);
 }
 catch (std::exception& e) {
     LOG("Exception in %s", __FUNCTION__)
     jclass jExceptionClass = env->FindClass("java/lang/RuntimeException");
     std::ostringstream message;
     message << "Allocating native class Pi failed: " << e.what();
     env->ThrowNew(jExceptionClass, message.str().c_str());
     env->DeleteLocalRef(jExceptionClass);
 }
Пример #5
0
bool RowIndexesValid(JNIEnv* env, T* pTable, jlong startIndex, jlong endIndex, jlong range)
{
    size_t maxIndex = pTable->size();
    if (endIndex == -1)
        endIndex = maxIndex;
    if (startIndex < 0) {
        TR_ERR("startIndex %" PRId64 " < 0 - invalid!", S64(startIndex))
        ThrowException(env, IndexOutOfBounds, "startIndex < 0.");
        return false;
    }
    if (realm::util::int_greater_than(startIndex, maxIndex)) {
        TR_ERR("startIndex %" PRId64 " > %" PRId64 " - invalid!", S64(startIndex), S64(maxIndex))
        ThrowException(env, IndexOutOfBounds, "startIndex > available rows.");
        return false;
    }

    if (realm::util::int_greater_than(endIndex, maxIndex)) {
        TR_ERR("endIndex %" PRId64 " > %" PRId64 " - invalid!", S64(endIndex), S64(maxIndex))
        ThrowException(env, IndexOutOfBounds, "endIndex > available rows.");
        return false;
    }
    if (startIndex > endIndex) {
        TR_ERR("startIndex %" PRId64 " > endIndex %" PRId64 " - invalid!", S64(startIndex), S64(endIndex))
        ThrowException(env, IndexOutOfBounds, "startIndex > endIndex.");
        return false;
    }

    if (range != -1 && range < 0) {
        TR_ERR("range %" PRId64 " < 0 - invalid!", S64(range))
        ThrowException(env, IndexOutOfBounds, "range < 0.");
        return false;
    }

    return true;
}
Пример #6
0
inline bool RowIndexValid(JNIEnv* env, T* pTable, jlong rowIndex, bool offset=false)
{
    if (rowIndex < 0) {
        ThrowException(env, IndexOutOfBounds, "rowIndex is less than 0.");
        return false;
    }
    size_t size = pTable->size();
    if (size > 0 && offset)
        size -= 1;
    bool rowErr = realm::util::int_greater_than_or_equal(rowIndex, size);
    if (rowErr) {
        TR_ERR("rowIndex %" PRId64 " > %" PRId64 " - invalid!", S64(rowIndex), S64(size))
        ThrowException(env, IndexOutOfBounds,
            "rowIndex > available rows: " +
            num_to_string(rowIndex) + " > " + num_to_string(size));
    }
    return !rowErr;
}
Пример #7
0
bool Time::set(S32 year, S32 month, S32 day, S32 hour, S32 minute, S32 second, S32 microsecond)
{
   second += microsecond / 100000;
   microsecond %= 100000;
   minute += second / 60;
   second %= 60;
   hour += minute / 60;
   minute %= 60;
   S32 carryDays = hour / 24;
   hour %= 24;

   bool leapYear = _isLeapYear(year);

   year -= 1;     // all the next operations need (year-1) so do it ahead of time
   S32 gregorian = 365 * year             // number of days since the epoch
                   + (year/4)             // add Julian leap year days
                   - (year/100)           // subtract century leap years
                   + (year/400)           // add gregorian 400 year leap adjustment
                   + ((367*month-362)/12) // days in prior months
                   + day                  // add days
                   + carryDays;           // add days from time overflow/underflow

   // make days in this year adjustment if leap year
   if (leapYear)
   {
      if (month > 2)
         gregorian -= 1;
   }
   else
   {
      if (month > 2)
         gregorian -= 2;
   }

   _time  = S64(gregorian) * OneDay;
   _time += S64((hour * OneHour) +
                (minute * OneMinute) +
                (second * OneSecond) +
                microsecond);

   return true;
}
Пример #8
0
bool CIniFile::sectionComment(const string &section, const string &comment, bool const create)
{
   S32 sectionId = findSection(section);

   if(sectionId == noID) 
   {
      if(create)
         sectionId = S64(addSection(section));
      else
         return false;

      // I think this should never happen -CE 1/8/2011
      // addsection only returns noID if section exists
      // if section exists, then it would have been found at top of function

      //if(sectionId == noID)
      //   sectionId = findSection(section);

      if(sectionId == noID)     
         return false;
   }

   return sectionComment(sectionId, comment);
}
Пример #9
0
static inline INT64 SC64(INT64 x) { return x & S64(0x0000ffffffffffff); }
Пример #10
0
static inline INT64 SX64(INT64 x) { return (x & S64(0x0000800000000000)) ? x | S64(0xffff000000000000) : x & S64(0x0000ffffffffffff); }
Пример #11
0
 *
 *   es5510.c - Ensoniq ES5510 (ESP) emulation
 *   by Christian Brunschen
 *
 ***************************************************************************/

#include <cstdio>
#include "emu.h"
#include "debugger.h"
#include "es5510.h"
#include "cpu/m68000/m68000.h"

static const INT32 MIN_24 = -(1 << 23);
static const INT32 MAX_24 = (1 << 23) - 1;

static const INT64 MIN_48 = -(S64(1) << 47);
static const INT64 MAX_48 = (S64(1) << 47) - 1;

#define SIGN_BIT_24 (0x00800000)
#define GET_SIGN_BIT_24(x) ((x) & SIGN_BIT_24)
#define IS_NEGATIVE(x) (((x) & SIGN_BIT_24) != 0)

#define CARRY_OUT_24 (0x01000000)

static inline INT32 SX(INT32 x) { return IS_NEGATIVE(x) ? x | 0xff000000 : x & 0x00ffffff; }
static inline INT32 SC(INT32 x) { return x & 0x00ffffff; }
static inline INT64 SX64(INT64 x) { return (x & S64(0x0000800000000000)) ? x | S64(0xffff000000000000) : x & S64(0x0000ffffffffffff); }
static inline INT64 SC64(INT64 x) { return x & S64(0x0000ffffffffffff); }

#define VERBOSE 0
#define VERBOSE_EXEC 0
Пример #12
0
void test(int M, int N, int O, int P, int Q, int R)
{
  /* Scattering iterators. */
  int p1, p3, p5;
  /* Original iterators. */
  int i, j, k;
  if (M == 1) {
    S1() ;
    S2() ;
    S3() ;
    S4() ;
    S5() ;
    S6() ;
    S7() ;
    S8() ;
    S9() ;
    S10() ;
    S11() ;
    S12() ;
    S13() ;
    S14() ;
    S15() ;
    S16() ;
    S17() ;
    S18() ;
    S19() ;
    S20() ;
    S21() ;
    S22() ;
    S23() ;
    S24() ;
    S25() ;
    S26() ;
    S27() ;
  }
  if (M == 1) {
    for (p1=1;p1<=N;p1++) {
      for (p3=1;p3<=N;p3++) {
        S28(p1,p3) ;
        S29(p1,p3) ;
        S30(p1,p3) ;
      }
      S31(p1) ;
    }
  }
  if (M == 1) {
    S32() ;
    S33() ;
    S34() ;
  }
  if ((M == 1) && (O <= 1)) {
    S35() ;
  }
  if (M == 1) {
    S36() ;
    S37() ;
  }
  if ((M == 1) && (N >= 1) && (Q >= 1) && (R >= 1)) {
    for (p1=2;p1<=P;p1++) {
      S38(p1) ;
      S39(p1) ;
      for (p3=1;p3<=Q;p3++) {
        for (p5=1;p5<=R;p5++) {
          S40(p1,p3,p5) ;
          S41(p1,p3,p5) ;
          S42(p1,p3,p5) ;
          S43(p1,p3,p5) ;
        }
      }
      for (p3=1;p3<=Q;p3++) {
        S44(p1,p3) ;
        S45(p1,p3) ;
        S46(p1,p3) ;
        S47(p1,p3) ;
      }
      for (p3=1;p3<=R;p3++) {
        S48(p1,p3) ;
        S49(p1,p3) ;
        S50(p1,p3) ;
        S51(p1,p3) ;
      }
      S52(p1) ;
      S53(p1) ;
      S54(p1) ;
      S55(p1) ;
      S56(p1) ;
      S57(p1) ;
      S58(p1) ;
      for (p3=1;p3<=Q;p3++) {
        for (p5=1;p5<=R;p5++) {
          S59(p1,p3,p5) ;
          S60(p1,p3,p5) ;
          S61(p1,p3,p5) ;
        }
      }
      for (p3=1;p3<=Q;p3++) {
        S62(p1,p3) ;
        S63(p1,p3) ;
        S64(p1,p3) ;
      }
      for (p3=1;p3<=R;p3++) {
        S65(p1,p3) ;
        S66(p1,p3) ;
        S67(p1,p3) ;
      }
      S68(p1) ;
      S69(p1) ;
      S70(p1) ;
      S71(p1) ;
      S72(p1) ;
      S73(p1) ;
      S74(p1) ;
      S75(p1) ;
      S76(p1) ;
      S77(p1) ;
      S78(p1) ;
      S79(p1) ;
      S80(p1) ;
      S81(p1) ;
      S82(p1) ;
      S83(p1) ;
      S84(p1) ;
      S85(p1) ;
      S86(p1) ;
      S87(p1) ;
      S88(p1) ;
      S89(p1) ;
      S90(p1) ;
      S91(p1) ;
      S92(p1) ;
      S93(p1) ;
      S94(p1) ;
      for (p3=1;p3<=N;p3++) {
        for (p5=1;p5<=N;p5++) {
          S95(p1,p3,p5) ;
          S96(p1,p3,p5) ;
          S97(p1,p3,p5) ;
        }
        S98(p1,p3) ;
      }
      S99(p1) ;
      S100(p1) ;
      S101(p1) ;
      for (p3=1;p3<=Q;p3++) {
        for (p5=1;p5<=R;p5++) {
          S102(p1,p3,p5) ;
          S103(p1,p3,p5) ;
          S104(p1,p3,p5) ;
          S105(p1,p3,p5) ;
          S106(p1,p3,p5) ;
          S107(p1,p3,p5) ;
        }
      }
      for (p3=1;p3<=Q;p3++) {
        S108(p1,p3) ;
        S109(p1,p3) ;
        S110(p1,p3) ;
        S111(p1,p3) ;
        S112(p1,p3) ;
        S113(p1,p3) ;
      }
      for (p3=1;p3<=R;p3++) {
        S114(p1,p3) ;
        S115(p1,p3) ;
        S116(p1,p3) ;
        S117(p1,p3) ;
        S118(p1,p3) ;
        S119(p1,p3) ;
      }
      S120(p1) ;
      S121(p1) ;
      S122(p1) ;
      S123(p1) ;
      S124(p1) ;
      S125(p1) ;
    }
  }
  if ((M == 1) && (N <= 0) && (Q >= 1) && (R >= 1)) {
    for (p1=2;p1<=P;p1++) {
      S38(p1) ;
      S39(p1) ;
      for (p3=1;p3<=Q;p3++) {
        for (p5=1;p5<=R;p5++) {
          S40(p1,p3,p5) ;
          S41(p1,p3,p5) ;
          S42(p1,p3,p5) ;
          S43(p1,p3,p5) ;
        }
      }
      for (p3=1;p3<=Q;p3++) {
        S44(p1,p3) ;
        S45(p1,p3) ;
        S46(p1,p3) ;
        S47(p1,p3) ;
      }
      for (p3=1;p3<=R;p3++) {
        S48(p1,p3) ;
        S49(p1,p3) ;
        S50(p1,p3) ;
        S51(p1,p3) ;
      }
      S52(p1) ;
      S53(p1) ;
      S54(p1) ;
      S55(p1) ;
      S56(p1) ;
      S57(p1) ;
      S58(p1) ;
      for (p3=1;p3<=Q;p3++) {
        for (p5=1;p5<=R;p5++) {
          S59(p1,p3,p5) ;
          S60(p1,p3,p5) ;
          S61(p1,p3,p5) ;
        }
      }
      for (p3=1;p3<=Q;p3++) {
        S62(p1,p3) ;
        S63(p1,p3) ;
        S64(p1,p3) ;
      }
      for (p3=1;p3<=R;p3++) {
        S65(p1,p3) ;
        S66(p1,p3) ;
        S67(p1,p3) ;
      }
      S68(p1) ;
      S69(p1) ;
      S70(p1) ;
      S71(p1) ;
      S72(p1) ;
      S73(p1) ;
      S74(p1) ;
      S75(p1) ;
      S76(p1) ;
      S77(p1) ;
      S78(p1) ;
      S79(p1) ;
      S80(p1) ;
      S81(p1) ;
      S82(p1) ;
      S83(p1) ;
      S84(p1) ;
      S85(p1) ;
      S86(p1) ;
      S87(p1) ;
      S88(p1) ;
      S89(p1) ;
      S90(p1) ;
      S91(p1) ;
      S92(p1) ;
      S93(p1) ;
      S94(p1) ;
      S99(p1) ;
      S100(p1) ;
      S101(p1) ;
      for (p3=1;p3<=Q;p3++) {
        for (p5=1;p5<=R;p5++) {
          S102(p1,p3,p5) ;
          S103(p1,p3,p5) ;
          S104(p1,p3,p5) ;
          S105(p1,p3,p5) ;
          S106(p1,p3,p5) ;
          S107(p1,p3,p5) ;
        }
      }
      for (p3=1;p3<=Q;p3++) {
        S108(p1,p3) ;
        S109(p1,p3) ;
        S110(p1,p3) ;
        S111(p1,p3) ;
        S112(p1,p3) ;
        S113(p1,p3) ;
      }
      for (p3=1;p3<=R;p3++) {
        S114(p1,p3) ;
        S115(p1,p3) ;
        S116(p1,p3) ;
        S117(p1,p3) ;
        S118(p1,p3) ;
        S119(p1,p3) ;
      }
      S120(p1) ;
      S121(p1) ;
      S122(p1) ;
      S123(p1) ;
      S124(p1) ;
      S125(p1) ;
    }
  }
  if ((M == 1) && (N >= 1) && (Q <= 0) && (R >= 1)) {
    for (p1=2;p1<=P;p1++) {
      S38(p1) ;
      S39(p1) ;
      for (p3=1;p3<=R;p3++) {
        S48(p1,p3) ;
        S49(p1,p3) ;
        S50(p1,p3) ;
        S51(p1,p3) ;
      }
      S52(p1) ;
      S53(p1) ;
      S54(p1) ;
      S55(p1) ;
      S56(p1) ;
      S57(p1) ;
      S58(p1) ;
      for (p3=1;p3<=R;p3++) {
        S65(p1,p3) ;
        S66(p1,p3) ;
        S67(p1,p3) ;
      }
      S68(p1) ;
      S69(p1) ;
      S70(p1) ;
      S71(p1) ;
      S72(p1) ;
      S73(p1) ;
      S74(p1) ;
      S75(p1) ;
      S76(p1) ;
      S77(p1) ;
      S78(p1) ;
      S79(p1) ;
      S80(p1) ;
      S81(p1) ;
      S82(p1) ;
      S83(p1) ;
      S84(p1) ;
      S85(p1) ;
      S86(p1) ;
      S87(p1) ;
      S88(p1) ;
      S89(p1) ;
      S90(p1) ;
      S91(p1) ;
      S92(p1) ;
      S93(p1) ;
      S94(p1) ;
      for (p3=1;p3<=N;p3++) {
        for (p5=1;p5<=N;p5++) {
          S95(p1,p3,p5) ;
          S96(p1,p3,p5) ;
          S97(p1,p3,p5) ;
        }
        S98(p1,p3) ;
      }
      S99(p1) ;
      S100(p1) ;
      S101(p1) ;
      for (p3=1;p3<=R;p3++) {
        S114(p1,p3) ;
        S115(p1,p3) ;
        S116(p1,p3) ;
        S117(p1,p3) ;
        S118(p1,p3) ;
        S119(p1,p3) ;
      }
      S120(p1) ;
      S121(p1) ;
      S122(p1) ;
      S123(p1) ;
      S124(p1) ;
      S125(p1) ;
    }
  }
  if ((M == 1) && (N <= 0) && (Q <= 0) && (R >= 1)) {
    for (p1=2;p1<=P;p1++) {
      S38(p1) ;
      S39(p1) ;
      for (p3=1;p3<=R;p3++) {
        S48(p1,p3) ;
        S49(p1,p3) ;
        S50(p1,p3) ;
        S51(p1,p3) ;
      }
      S52(p1) ;
      S53(p1) ;
      S54(p1) ;
      S55(p1) ;
      S56(p1) ;
      S57(p1) ;
      S58(p1) ;
      for (p3=1;p3<=R;p3++) {
        S65(p1,p3) ;
        S66(p1,p3) ;
        S67(p1,p3) ;
      }
      S68(p1) ;
      S69(p1) ;
      S70(p1) ;
      S71(p1) ;
      S72(p1) ;
      S73(p1) ;
      S74(p1) ;
      S75(p1) ;
      S76(p1) ;
      S77(p1) ;
      S78(p1) ;
      S79(p1) ;
      S80(p1) ;
      S81(p1) ;
      S82(p1) ;
      S83(p1) ;
      S84(p1) ;
      S85(p1) ;
      S86(p1) ;
      S87(p1) ;
      S88(p1) ;
      S89(p1) ;
      S90(p1) ;
      S91(p1) ;
      S92(p1) ;
      S93(p1) ;
      S94(p1) ;
      S99(p1) ;
      S100(p1) ;
      S101(p1) ;
      for (p3=1;p3<=R;p3++) {
        S114(p1,p3) ;
        S115(p1,p3) ;
        S116(p1,p3) ;
        S117(p1,p3) ;
        S118(p1,p3) ;
        S119(p1,p3) ;
      }
      S120(p1) ;
      S121(p1) ;
      S122(p1) ;
      S123(p1) ;
      S124(p1) ;
      S125(p1) ;
    }
  }
  if ((M == 1) && (N >= 1) && (Q <= 0) && (R <= 0)) {
    for (p1=2;p1<=P;p1++) {
      S38(p1) ;
      S39(p1) ;
      S52(p1) ;
      S53(p1) ;
      S54(p1) ;
      S55(p1) ;
      S56(p1) ;
      S57(p1) ;
      S58(p1) ;
      S68(p1) ;
      S69(p1) ;
      S70(p1) ;
      S71(p1) ;
      S72(p1) ;
      S73(p1) ;
      S74(p1) ;
      S75(p1) ;
      S76(p1) ;
      S77(p1) ;
      S78(p1) ;
      S79(p1) ;
      S80(p1) ;
      S81(p1) ;
      S82(p1) ;
      S83(p1) ;
      S84(p1) ;
      S85(p1) ;
      S86(p1) ;
      S87(p1) ;
      S88(p1) ;
      S89(p1) ;
      S90(p1) ;
      S91(p1) ;
      S92(p1) ;
      S93(p1) ;
      S94(p1) ;
      for (p3=1;p3<=N;p3++) {
        for (p5=1;p5<=N;p5++) {
          S95(p1,p3,p5) ;
          S96(p1,p3,p5) ;
          S97(p1,p3,p5) ;
        }
        S98(p1,p3) ;
      }
      S99(p1) ;
      S100(p1) ;
      S101(p1) ;
      S120(p1) ;
      S121(p1) ;
      S122(p1) ;
      S123(p1) ;
      S124(p1) ;
      S125(p1) ;
    }
  }
  if ((M == 1) && (N <= 0) && (Q <= 0) && (R <= 0)) {
    for (p1=2;p1<=P;p1++) {
      S38(p1) ;
      S39(p1) ;
      S52(p1) ;
      S53(p1) ;
      S54(p1) ;
      S55(p1) ;
      S56(p1) ;
      S57(p1) ;
      S58(p1) ;
      S68(p1) ;
      S69(p1) ;
      S70(p1) ;
      S71(p1) ;
      S72(p1) ;
      S73(p1) ;
      S74(p1) ;
      S75(p1) ;
      S76(p1) ;
      S77(p1) ;
      S78(p1) ;
      S79(p1) ;
      S80(p1) ;
      S81(p1) ;
      S82(p1) ;
      S83(p1) ;
      S84(p1) ;
      S85(p1) ;
      S86(p1) ;
      S87(p1) ;
      S88(p1) ;
      S89(p1) ;
      S90(p1) ;
      S91(p1) ;
      S92(p1) ;
      S93(p1) ;
      S94(p1) ;
      S99(p1) ;
      S100(p1) ;
      S101(p1) ;
      S120(p1) ;
      S121(p1) ;
      S122(p1) ;
      S123(p1) ;
      S124(p1) ;
      S125(p1) ;
    }
  }
  if ((M == 1) && (N >= 1) && (Q >= 1) && (R <= 0)) {
    for (p1=2;p1<=P;p1++) {
      S38(p1) ;
      S39(p1) ;
      for (p3=1;p3<=Q;p3++) {
        S44(p1,p3) ;
        S45(p1,p3) ;
        S46(p1,p3) ;
        S47(p1,p3) ;
      }
      S52(p1) ;
      S53(p1) ;
      S54(p1) ;
      S55(p1) ;
      S56(p1) ;
      S57(p1) ;
      S58(p1) ;
      for (p3=1;p3<=Q;p3++) {
        S62(p1,p3) ;
        S63(p1,p3) ;
        S64(p1,p3) ;
      }
      S68(p1) ;
      S69(p1) ;
      S70(p1) ;
      S71(p1) ;
      S72(p1) ;
      S73(p1) ;
      S74(p1) ;
      S75(p1) ;
      S76(p1) ;
      S77(p1) ;
      S78(p1) ;
      S79(p1) ;
      S80(p1) ;
      S81(p1) ;
      S82(p1) ;
      S83(p1) ;
      S84(p1) ;
      S85(p1) ;
      S86(p1) ;
      S87(p1) ;
      S88(p1) ;
      S89(p1) ;
      S90(p1) ;
      S91(p1) ;
      S92(p1) ;
      S93(p1) ;
      S94(p1) ;
      for (p3=1;p3<=N;p3++) {
        for (p5=1;p5<=N;p5++) {
          S95(p1,p3,p5) ;
          S96(p1,p3,p5) ;
          S97(p1,p3,p5) ;
        }
        S98(p1,p3) ;
      }
      S99(p1) ;
      S100(p1) ;
      S101(p1) ;
      for (p3=1;p3<=Q;p3++) {
        S108(p1,p3) ;
        S109(p1,p3) ;
        S110(p1,p3) ;
        S111(p1,p3) ;
        S112(p1,p3) ;
        S113(p1,p3) ;
      }
      S120(p1) ;
      S121(p1) ;
      S122(p1) ;
      S123(p1) ;
      S124(p1) ;
      S125(p1) ;
    }
  }
  if ((M == 1) && (N <= 0) && (Q >= 1) && (R <= 0)) {
    for (p1=2;p1<=P;p1++) {
      S38(p1) ;
      S39(p1) ;
      for (p3=1;p3<=Q;p3++) {
        S44(p1,p3) ;
        S45(p1,p3) ;
        S46(p1,p3) ;
        S47(p1,p3) ;
      }
      S52(p1) ;
      S53(p1) ;
      S54(p1) ;
      S55(p1) ;
      S56(p1) ;
      S57(p1) ;
      S58(p1) ;
      for (p3=1;p3<=Q;p3++) {
        S62(p1,p3) ;
        S63(p1,p3) ;
        S64(p1,p3) ;
      }
      S68(p1) ;
      S69(p1) ;
      S70(p1) ;
      S71(p1) ;
      S72(p1) ;
      S73(p1) ;
      S74(p1) ;
      S75(p1) ;
      S76(p1) ;
      S77(p1) ;
      S78(p1) ;
      S79(p1) ;
      S80(p1) ;
      S81(p1) ;
      S82(p1) ;
      S83(p1) ;
      S84(p1) ;
      S85(p1) ;
      S86(p1) ;
      S87(p1) ;
      S88(p1) ;
      S89(p1) ;
      S90(p1) ;
      S91(p1) ;
      S92(p1) ;
      S93(p1) ;
      S94(p1) ;
      S99(p1) ;
      S100(p1) ;
      S101(p1) ;
      for (p3=1;p3<=Q;p3++) {
        S108(p1,p3) ;
        S109(p1,p3) ;
        S110(p1,p3) ;
        S111(p1,p3) ;
        S112(p1,p3) ;
        S113(p1,p3) ;
      }
      S120(p1) ;
      S121(p1) ;
      S122(p1) ;
      S123(p1) ;
      S124(p1) ;
      S125(p1) ;
    }
  }
}
Пример #13
0
int main()
{
	u8 *buf = NULL, *newContentBuf = NULL;
	FILE *f = fopen("title.cia","rb");
	if(!f)
	{
		puts("Unable to open title.cia!");
		goto end;
	}
	fseek(f,0,SEEK_END);
	size_t fsize = ftell(f);
	fseek(f,0,SEEK_SET);
	buf = malloc(fsize);
	if(!buf)
	{
		printf("Unable to allocate %i bytes!\n",fsize);
		goto end;
	}
	fread(buf,1,fsize,f);
	fclose(f);
	f = NULL;
	u32 certsize = A64(*(u32*)(buf+0x8)), tiksize=A64(*(u32*)(buf+0xC)), tmdsize=A64(*(u32*)(buf+0x10)),
		metasize=A64(*(u32*)(buf+0x14)), contentsize=A64(*(u32*)(buf+0x18));
	printf("Cert Size:%x, Tik Size: %x, TMD Size: %x\nMeta Size: %x, Total Content Size:%x\n",
		certsize,tiksize,tmdsize,metasize,contentsize);
	u32 tmdstart = 0x2040+certsize+tiksize;
	printf("TMD Start: %x\n",tmdstart);
	u32 sigtype = S32(*(u32*)(buf+tmdstart));
	u32 tmdsigsize = 0x100;

	if(sigtype != RSA_2048_SHA256)
	{
		printf("Signature Type not supported:0x%08x\n",sigtype);
		goto end;
	}

	u32 tmdhdr = A64(tmdstart+4+tmdsigsize);
	u32 tmdverpos = tmdhdr+0x40;
	int tmdver = *(buf+tmdverpos);
	if(tmdver != 1)
	{
		printf("Unknown tmd ver:%i\n",tmdver);
		goto end;
	}
	printf("Title ID: %016I64x\n",S64(*(u64*)(buf+tmdhdr+0x4C)));
	u32 numcontentpos = tmdhdr+0x9E;
	u16 numcontents = S16(*(u16*)(buf+numcontentpos));
	printf("Num Contents: %i\n",numcontents);
	u32 infoshapos = numcontentpos+6; //has sha1 of 0x900 inforecords
	u32 inforecords = infoshapos+0x20; //has sha1 of each chunkrecord
	f = fopen("content.bin","rb");
	fseek(f,0,SEEK_END);
	size_t newContentSize = ftell(f);
	fseek(f,0,SEEK_SET);
	newContentBuf = malloc(newContentSize);
	fread(newContentBuf,newContentSize,1,f);
	fclose(f);
	memcpy(buf+0x18,&newContentSize,4);
	printf("New Content Size: %x\n",A64(*(u32*)(buf+0x18)));
	u32 chunkrecords = inforecords+(0x40*0x24); //has sha1 of each content
	u32 chunkrecordsSize = chunkrecords+(numcontents*0x30);
	u32 content = A64(chunkrecordsSize);
	printf("Content starts at 0x%x\n",content);
	int i;
	for(i = 0; i < numcontents; i++)
	{
		*(u64*)(buf+(chunkrecords+(i*0x30))+0x8) = S64(newContentSize);
		u64 thisContentSize = S64(*(u64*)(buf+(chunkrecords+(i*0x30))+0x8));
		printf("Content %i Size: %I64x\n", 
			S16(*(u16*)(buf+(chunkrecords+(i*0x30))+0x4)),
			thisContentSize);
		sha2(newContentBuf,(u32)thisContentSize,buf+(chunkrecords+(i*0x30))+0x10,0);
	}
	sha2(buf+chunkrecords,0x30,buf+inforecords+4,0);
	sha2(buf+inforecords,(0x40*0x24),buf+infoshapos,0);
	printf("Signing TMD, RsaSignVerify=%i\n",
		RsaSignVerify(buf+tmdstart+0x140,0xC4,buf+tmdstart+4,mod,priv_exp,RSA_2048_SHA256,CTR_RSA_SIGN));
	f = fopen("titleNew.cia","wb");
	fwrite(buf,content,1,f);
	fwrite(newContentBuf,newContentSize,1,f);
	if(metasize > 0)
	{
		u32 metastart = 0x2040+certsize+tiksize+tmdsize+contentsize;
		fwrite(buf+metastart,metasize,1,f);
	}
	fclose(f);
	puts("Wrote titleNew.cia");
end:
	if(buf) free(buf);
	if(f) fclose(f);
	return 0;
}
Пример #14
0
int main(int argc, char *argv[])
{
    /*Initialize the catalog locks*/
    MT_lock_init(&dataLock);
    MT_cond_init(&mainCond);
    MT_cond_init(&writeTCond);
    MT_cond_init(&readCond);

    char* file_name_in = 0;
    char* file_name_out = 0;
    char separator_sign = ' ';
    char* parse_string = "xyz";
    char* buffer;
    char printstring[256];
    LASReaderH reader = NULL;
    LASHeaderH header = NULL;
    LASPointH p = NULL;
    FILE** files_out = NULL;
    int len, j;
    int64_t mortonkey = 0;
    unsigned int index = 0;
    int num_files_in = 0, num_files_out = 0, num_files, num_of_entries=0, check = 0, num_read_threads = DEFAULT_NUM_READ_THREADS;
    int i;
    pthread_t *writeThreads = NULL;
    pthread_t *readThreads = NULL;
    struct readThreadArgs *dataRead = NULL;
    boolean input_file = FALSE;
    int64_t global_offset_x = 0;
    int64_t global_offset_y = 0;
    double scale_x;
    double scale_y;


    if (argc == 1) {
        usage();
        exit(0);
    }

    /*Allocate space for input files*/
    files_name_in = (char**) malloc(sizeof(char*)*DEFAULT_NUM_INPUT_FILES);

    for (i = 1; i < argc; i++)
    {
        if (    strcmp(argv[i],"-h") == 0 ||
                strcmp(argv[i],"--help") == 0
           )
        {
            usage();
            exit(0);
        }
        else if (   strcmp(argv[i],"-v") == 0 ||
                strcmp(argv[i],"--verbose") == 0
                )
        {
            verbose = TRUE;
        }
        else if ( strcmp(argv[i],"--num_read_threads") == 0)
        {
            num_read_threads = atoi(argv[++i]);
        }
        else if (   strcmp(argv[i],"-s") == 0 ||
                strcmp(argv[i],"--skip_invalid") == 0
                )
        {
            skip_invalid = TRUE;
        }
        else if (   strcmp(argv[i], "--parse") == 0 ||
                strcmp(argv[i], "-parse") == 0
                )
        {
            i++;
            parse_string = argv[i];
        }
        else if (   strcmp(argv[i], "--moffset") == 0 ||
                strcmp(argv[i], "-moffset") == 0
                )
        {
            i++;
            buffer = strtok (argv[i], ",");
            j = 0;
            while (buffer) {
                if (j == 0) {
                    global_offset_x = S64(buffer);
                }
                else if (j == 1) {
                    global_offset_y = S64(buffer);
                }
                j++;
                buffer = strtok (NULL, ",");
                while (buffer && *buffer == '\040')
                    buffer++;
            }
            if (j != 2){
                fprintf(stderr, "Only two int64_t are required in moffset option!\n");
                exit(1);
            }

        }
        else if (   strcmp(argv[i], "--check") == 0 ||
                strcmp(argv[i], "-check") == 0
                )
        {
            i++;
            check = 1;
            buffer = strtok (argv[i], ",");
            j = 0;
            while (buffer) {
                if (j == 0) {
                    sscanf(buffer, "%lf", &scale_x);
                }
                else if (j == 1) {
                    sscanf(buffer, "%lf", &scale_y);
                }
                j++;
                buffer = strtok (NULL, ",");
                while (buffer && *buffer == '\040')
                    buffer++;
            }
            if (j != 2){
                fprintf(stderr, "Only two doubles are required in moffset option!\n");
                exit(1);
            }
        }
        else if (   strcmp(argv[i],"--input") == 0  ||
                strcmp(argv[i],"-input") == 0   ||
                strcmp(argv[i],"-i") == 0       ||
                strcmp(argv[i],"-in") == 0
                )
        {
            i++;
            files_name_in[num_files_in++] = argv[i];

            if (num_files_in % DEFAULT_NUM_INPUT_FILES)
                files_name_in = (char**) realloc(files_name_in, (num_files_in*2)*sizeof(char*));
        }
        else if (strcmp(argv[i],"--file") == 0  ||
                strcmp(argv[i],"-file") == 0   ||
                strcmp(argv[i],"-f") == 0
                )
        {
            i++;
            int read;
            char line_buffer[BUFSIZ];
            FILE* in = NULL;

            in = fopen(argv[i], "r");
            if (!in) {
                fprintf(stderr, "ERROR: the path for file containing the input files is invalid %s\n", argv[i]);
                exit(1);
            }
            while (fgets(line_buffer, sizeof(line_buffer), in)) {
                line_buffer[strlen(line_buffer)-1]='\0';
                files_name_in[num_files_in++] = strdup(line_buffer);

                if (num_files_in % DEFAULT_NUM_INPUT_FILES)
                    files_name_in = (char**) realloc(files_name_in, (num_files_in*2)*sizeof(char*));
            }
            fclose(in);
            input_file = TRUE;
        }
        else if ((num_files_in != 0) && num_files_out == 0)
        {
            file_name_out = argv[i];
            num_files_out++;
        }
        else
        {
            fprintf(stderr, "ERROR: unknown argument '%s'\n",argv[i]);
            usage();
            exit(1);
        }
    } /* end looping through argc/argv */
    num_of_entries = strlen(parse_string);

    if (num_files_in == 0)
    {
        LASError_Print("No input filename was specified");
        usage();
        exit(1);
    }
    num_files = num_files_in;

    /*Entries metadata*/
    i = 0;
    for (;;)
    {
        switch (parse_string[i])
        {
                /* // the morton code on xy */
            case 'k':
                entries[i] = ENTRY_k;
                entriesType[i] = sizeof(int64_t);
	            /*Changes for Oscar's new Morton code function*/
                //entriesFunc[i] = (void*)morton2D_encode;
                entriesFunc[i] = (void*)morton2D_encodeOscar;
                break;
                /* // the x coordinate  double*/
            case 'x':
                entries[i] = ENTRY_x;
                entriesType[i] = sizeof(double);
                entriesFunc[i] = (void*)LASPoint_GetX;
                break;
                /* // the y coordinate double*/
            case 'y':
                entries[i] = ENTRY_y;
                entriesType[i] = sizeof(double);
                entriesFunc[i] = (void*)LASPoint_GetY;
                break;
                /* // the z coordinate double*/
            case 'z':
                entries[i] = ENTRY_z;
                entriesType[i] = sizeof(double);
                entriesFunc[i] = (void*)LASPoint_GetZ;
                break;
                /* // the X coordinate decimal*/
            case 'X':
                entries[i] = ENTRY_X;
                entriesType[i] = sizeof(int);
                entriesFunc[i] = (void*)LASPoint_GetX;
                break;
                /* // the y coordinate decimal*/
            case 'Y':
                entries[i] = ENTRY_Y;
                entriesType[i] = sizeof(int);
                entriesFunc[i] = (void*)LASPoint_GetY;
                break;
                /* // the z coordinate decimal*/
            case 'Z':
                entries[i] = ENTRY_Z;
                entriesType[i] = sizeof(int);
                entriesFunc[i] = (void*)LASPoint_GetZ;
                break;
                /* // the gps-time */
            case 't':
                entries[i] = ENTRY_t;
                entriesType[i] = sizeof(double);
                entriesFunc[i] = (void*)LASPoint_GetTime;
                break;
                /* // the intensity */
            case 'i':
                entries[i] = ENTRY_i;
                entriesType[i] = sizeof(int);
                entriesFunc[i] = (void*)LASPoint_GetIntensity;
                break;
                /* the scan angle */
            case 'a':
                entries[i] = ENTRY_a;
                entriesType[i] = sizeof(int);
                entriesFunc[i] = (void*)LASPoint_GetScanAngleRank;
                break;
                /* the number of the return */
            case 'r':
                entries[i] = ENTRY_r;
                entriesType[i] = sizeof(int);
                entriesFunc[i] = (void*)LASPoint_GetReturnNumber;
                break;
                /* the classification */
            case 'c':
                entries[i] = ENTRY_c;
                entriesType[i] = sizeof(int);
                entriesFunc[i] = (void*)LASPoint_GetClassification;
                break;
                /* the user data */
            case 'u':
                entries[i] = ENTRY_u;
                entriesType[i] = sizeof(int);
                entriesFunc[i] = (void*)LASPoint_GetUserData;
                break;
                /* the number of returns of given pulse */
            case 'n':
                entries[i] = ENTRY_n;
                entriesType[i] = sizeof(int);
                entriesFunc[i] = (void*)LASPoint_GetNumberOfReturns;
                break;
                /* the red channel color */
            case 'R':
                entries[i] = ENTRY_R;
                entriesType[i] = sizeof(int);
                entriesFunc[i] = (void*)LASColor_GetRed;
                break;
                /* the green channel color */
            case 'G':
                entries[i] = ENTRY_G;
                entriesType[i] = sizeof(int);
                entriesFunc[i] = (void*)LASColor_GetGreen;
                break;
                /* the blue channel color */
            case 'B':
                entries[i] = ENTRY_B;
                entriesType[i] = sizeof(int);
                entriesFunc[i] = (void*)LASColor_GetBlue;
                break;
            case 'M':
                entries[i] = ENTRY_M;
                entriesType[i] = sizeof(unsigned int);
                break;
            case 'p':
                entries[i] = ENTRY_p;
                entriesType[i] = sizeof(int);
                entriesFunc[i] = (void*)LASPoint_GetPointSourceId;
                break;
                /* the edge of flight line flag */
            case 'e':
                entries[i] = ENTRY_e;
                entriesType[i] = sizeof(int);
                entriesFunc[i] = (void*)LASPoint_GetFlightLineEdge;
                break;
                /* the direction of scan flag */
            case 'd':
                entries[i] = ENTRY_d;
                entriesType[i] = sizeof(int);
                entriesFunc[i] = (void*)LASPoint_GetScanDirection;
                break;
        }
        i++;
        if (parse_string[i] == 0)
        {
            break;
        }
    }

    /*Prepare the output files*/
    if (file_name_out == NULL)
    {
        len = (int)strlen(file_name_in);
        file_name_out = LASCopyString(file_name_in);
        if (file_name_out[len-3] == '.' && file_name_out[len-2] == 'g' && file_name_out[len-1] == 'z')
        {
            len = len - 4;
        }
        while (len > 0 && file_name_out[len] != '.')
        {
            len--;
        }
        file_name_out[len] = '\0';
    }
    char *str = malloc(sizeof(char)*(strlen(file_name_out)+12));
    files_out = (FILE**) malloc(sizeof(FILE*)*num_of_entries);
    for (i = 0; i < num_of_entries; i++) {
        sprintf(str, "%s_col_%c.dat", file_name_out, parse_string[i]);
        if(doesFileExist(str)) {
            remove(str);
        }

        files_out[i] = fopen(str, "wb");

        if (files_out[i] == 0) {
            LASError_Print("Could not open file for write");
            usage();
            exit(1);
        }
    }
    free(str);

    /*Initialize structures for the reading threads*/
    //data = (struct writeT**) malloc(num_read_threads*sizeof(struct writeT*)); //Malloc is more efficient than calloc
    data = (struct writeT**) calloc(num_read_threads, sizeof(struct writeT*));

    dataRead = (struct readThreadArgs*) malloc(sizeof(struct readThreadArgs)*num_read_threads);
    /* Launch read Threads */
    stop = 0;
    readThreads = (pthread_t*) malloc(sizeof(pthread_t)*num_read_threads);
    for (i=0; i < num_read_threads; i++) {
        dataRead[i].id = i;
        dataRead[i].num_read_threads = num_read_threads;
        dataRead[i].num_of_entries = num_of_entries;
        dataRead[i].check = check;
        dataRead[i].global_offset_x = global_offset_x;
        dataRead[i].global_offset_y = global_offset_y;
        dataRead[i].scale_x = scale_x;
        dataRead[i].scale_y = scale_y;
        pthread_create(&readThreads[i], NULL, readFile, (void*)dataRead);
    }

    int writeIndex = 0;
    writeThreads = (pthread_t*) malloc(sizeof(pthread_t)*num_of_entries);

    /* Launch Threads */
    struct writeThreadArgs *dataWrite = (struct writeThreadArgs *) malloc(sizeof(struct writeThreadArgs) *num_of_entries);
    for (i = 0; i < num_of_entries; i++) {
        dataWrite[i].id = i;
        dataWrite[i].out = files_out[i];
        pthread_create(&writeThreads[i], NULL, writeFile, (void*)(&dataWrite[i]));
    }
    sleep(1);
    //Do we need to comment this one out!?
    int done = 0;
    while (num_files) {
        /*Obtain lock over data to get the pointer*/
        MT_set_lock(&dataLock);
        dataWriteT = data[writeIndex];
        while (dataWriteT == NULL) {
            /*Sleep and wait for data to be read*/
            MT_cond_wait(&mainCond,&dataLock);
            dataWriteT = data[writeIndex];
        }
        data[writeIndex] = NULL;
        //Release the lock

        /*Tell the write threads there is new data*/
        pthread_cond_broadcast(&writeTCond);

        /*Tell the read threads there is a new buf empty*/
        pthread_cond_broadcast(&readCond);
        MT_unset_lock(&dataLock);

        /*Keep looping*/
        writeIndex++;
        writeIndex = (writeIndex % num_read_threads);

        MT_set_lock(&dataLock);
        while (done == 0) {
            /*Sleep and wait for data to be read*/
            MT_cond_wait(&mainCond,&dataLock);
            done = 1;
            for (i = 0; i < num_of_entries; i++) {
                if (dataWriteT[i].values != NULL) {
                    done = 0;
                    break;
                }
            }
        }
        num_files--;
        if (verbose)
            printf("Files to go %d\n", num_files);
        free(dataWriteT);
        dataWriteT = NULL;
        done = 0;
        MT_unset_lock(&dataLock);
    }

    /*Tell the write threads to exit*/
    MT_set_lock(&dataLock);
    stop = 1;
    pthread_cond_broadcast(&writeTCond);
    MT_unset_lock(&dataLock);

    /* Wait for Threads to Finish */
    for (i=0; i<num_of_entries; i++) {
        pthread_join(writeThreads[i], NULL);
    }
    free(dataWrite);
    free(writeThreads);

    MT_cond_destroy(&readCond);
    MT_cond_destroy(&writeTCond);
    MT_cond_destroy(&mainCond);
    MT_lock_destroy(&dataLock);

    for (i = 0; i < num_of_entries; i++) {
        fflush(files_out[i]);
        if (verbose)
            printf("close file %d\n", i);
        fsync(files_out[i]);
        fclose(files_out[i]);
    }
    free(files_out);
    if (input_file) {
        for (i=0 ; i < num_files_in; i++)
            free(files_name_in[i]);

        free(files_name_in);
    }

    free(dataRead);

    if (readThreads)
        free(readThreads);

    return 0;
}
Пример #15
0
void
WriteGatheredData(const char *pathname, VolumeObjects_t *vop)
{
	int fd;
	gzFile outf;
	struct HFSInfoHeader hdr = { 0 };
	HFSDataObject *objs = NULL, *op;
	ExtentList_t *ep;
	int i;

	hdr.version = S32(kHFSInfoHeaderVersion);
	hdr.deviceBlockSize = S32((uint32_t)vop->devp->blockSize);
	hdr.rawDeviceSize = S64(vop->devp->size);
	hdr.objectCount = S32(vop->count);
	hdr.size = S32(sizeof(hdr) + sizeof(HFSDataObject) * vop->count);

	objs = malloc(sizeof(HFSDataObject) * vop->count);
	if (objs == NULL) {
		warn("Unable to allocate space for data objects (%zu bytes)", sizeof(HFSDataObject)* vop->count);
		goto done;
	}

	op = objs;
	for (ep = vop->list;
	     ep;
	     ep = ep->next) {
		int i;
		for (i = 0; i < ep->count; i++) {
			op->offset = S64(ep->extents[i].base);
			op->size = S64(ep->extents[i].length);
			op++;
		}
	}

	fd = open(pathname, O_WRONLY | O_CREAT | O_TRUNC, 0666);
	if (fd == -1) {
		warn("cannot create gather file %s", pathname);
		goto done;
	}
	outf = gzdopen(fd, "wb");
	if (outf == NULL) {
		warn("Cannot create gz descriptor from file %s", pathname);
		close(fd);
		goto done;
	}

	gzwrite(outf, &hdr, sizeof(hdr));
	gzwrite(outf, objs, sizeof(HFSDataObject) * vop->count);

	int count = 0;
	for (ep = vop->list;
	     ep;
	     ep = ep->next) {
		int i;
		for (i = 0; i < ep->count; i++) {
			if (verbose)
				fprintf(stderr, "Writing extent <%lld, %lld>\n", ep->extents[i].base, ep->extents[i].length);
			if (WriteExtent(outf, vop->devp, ep->extents[i].base, ep->extents[i].length) == -1) {
				if (verbose)
					fprintf(stderr, "\tWrite failed\n");
				break;
			}
			count++;
		}
	}
	gzclose(outf);
	if (count != vop->count)
		fprintf(stderr, "WHOAH!  we're short by %zd objects!\n", vop->count - count);


done:
	if (objs)
		free(objs);
	return;

}
Пример #16
0
int main(int argc, char *argv[])
{
    int i;
    int j;
    char* buffer;
    int use_stdout = FALSE;
    int skip_invalid = FALSE;
    int num_entries = 0;

    int verbose = FALSE;
    char* file_name_in = 0;
    char* file_name_out = 0;
    char separator_sign = ' ';
    char* parse_string = "xyz";

    int64_t global_offset_x = 0;
    int64_t global_offset_y = 0;
    int check = FALSE;
    double scale_x;
    double scale_y;

    LASReaderH reader = NULL;
    LASHeaderH header = NULL;
    LASPointH p = NULL;
    FILE* file_out;
    int len;

    unsigned int index = 0;
    if (argc == 1) {
        usage();
        exit(0);
    }

    for (i = 1; i < argc; i++)
    {
        if (    strcmp(argv[i],"-h") == 0 ||
                strcmp(argv[i],"-help") == 0 ||
                strcmp(argv[i],"--help") == 0
           )
        {
            usage();
            exit(0);
        }
        else if (   strcmp(argv[i],"-v") == 0 ||
                strcmp(argv[i],"--verbose") == 0
                )
        {
            verbose = TRUE;
        }
        else if (   strcmp(argv[i],"-s") == 0 ||
                strcmp(argv[i],"--skip_invalid") == 0
                )
        {
            skip_invalid = TRUE;
        }
        else if (   strcmp(argv[i], "--parse") == 0 ||
                    strcmp(argv[i], "-parse") == 0
                )
        {
            i++;
            parse_string = argv[i];
        }
        else if (   strcmp(argv[i], "--moffset") == 0 ||
                    strcmp(argv[i], "-moffset") == 0
                )
        {
            i++;
            buffer = strtok (argv[i], ",");
            j = 0;
            while (buffer) {
                if (j == 0) {
                    global_offset_x = S64(buffer);
                }
                else if (j == 1) {
                    global_offset_y = S64(buffer);
                }
                j++;
                buffer = strtok (NULL, ",");
                while (buffer && *buffer == '\040')
                    buffer++;
            }
            if (j != 2){
                fprintf(stderr, "Only two int64_t are required in moffset option!\n");
                exit(1);
            }

        }
        else if (   strcmp(argv[i], "--check") == 0 ||
                    strcmp(argv[i], "-check") == 0
                )
        {
            i++;
            check = TRUE;
            buffer = strtok (argv[i], ",");
            j = 0;
            while (buffer) {
                if (j == 0) {
                    sscanf(buffer, "%lf", &scale_x);
                }
                else if (j == 1) {
                    sscanf(buffer, "%lf", &scale_y);
                }
                j++;
                buffer = strtok (NULL, ",");
                while (buffer && *buffer == '\040')
                    buffer++;
            }
            if (j != 2){
                fprintf(stderr, "Only two doubles are required in moffset option!\n");
                exit(1);
            }
        }
        else if (   strcmp(argv[i], "--stdout") == 0
                )
        {
            use_stdout = TRUE;
        }
        else if (   strcmp(argv[i],"--input") == 0  ||
                strcmp(argv[i],"-input") == 0   ||
                strcmp(argv[i],"-i") == 0       ||
                strcmp(argv[i],"-in") == 0
                )
        {
            i++;
            file_name_in = argv[i];
        }
        else if (   strcmp(argv[i],"--output") == 0  ||
                    strcmp(argv[i],"--out") == 0     ||
                    strcmp(argv[i],"-out") == 0     ||
                    strcmp(argv[i],"-o") == 0
                )
        {
            i++;
            file_name_out = argv[i];
        }
        else if (file_name_in == 0 && file_name_out == 0)
        {
            file_name_in = argv[i];
        }
        else if (file_name_in && file_name_out == 0)
        {
            file_name_out = argv[i];
        }
        else
        {
            fprintf(stderr, "ERROR: unknown argument '%s'\n",argv[i]);
            usage();
            exit(1);
        }
    } /* end looping through argc/argv */
    num_entries = strlen(parse_string);

    if (use_stdout == TRUE && file_name_out){
      LASError_Print("If an output file is specified, --stdout must not be used!");
      exit(1);
    }

    reader = LASReader_Create(file_name_in);
    if (!reader) {
        LASError_Print("Unable to read file");
        exit(1);
    }

    header = LASReader_GetHeader(reader);
    if (!header) {
        LASError_Print("Unable to fetch header for file");
        exit(1);
    }

    if (use_stdout)
    {
        file_out = stdout;
    }
    else
    {
        if (file_name_out == NULL)
        {
            if (file_name_in == NULL)
            {
                LASError_Print("No input filename was specified");
                usage();
                exit(1);
            }

            len = (int)strlen(file_name_in);
            file_name_out = LASCopyString(file_name_in);
            if (file_name_out[len-3] == '.' && file_name_out[len-2] == 'g' && file_name_out[len-1] == 'z')
            {
                len = len - 4;
            }
            while (len > 0 && file_name_out[len] != '.')
            {
                len--;
            }
            file_name_out[len] = '\0';
        }
        file_out = fopen(file_name_out, "wb");
    }

    if (file_out == 0)
    {
        LASError_Print("Could not open file for write");
        usage();
        exit(1);
    }

    if (verbose)
    {
        print_header(stderr, header, file_name_in);
    }

    // Compute factors to add to X and Y and check sanity of generated codes
    double file_scale_x = LASHeader_GetScaleX(header);
    double file_scale_y = LASHeader_GetScaleY(header);

    if (check)
    {
        // Check specified scales are like in the LAS file
        if (fabs(scale_x - file_scale_x) > TOLERANCE){
        fprintf(stderr, "ERROR: x scale in input file (%lf) does not match specified x scale (%lf)\n",file_scale_x, scale_x);
            exit(1);
        }
        if (fabs(scale_y - file_scale_y) > TOLERANCE){
            fprintf(stderr, "ERROR: y scale in input file (%lf) does not match specified y scale (%lf)\n",file_scale_y, scale_y);
            exit(1);
        }
        /* Check that the extent of the file (taking into account the global offset)
         * is within 0,2^31 */
        double check_min_x = 1.0 + LASHeader_GetMinX(header) - (((double) global_offset_x) * scale_x);
        if (check_min_x < TOLERANCE) {
            fprintf(stderr, "ERROR: Specied X global offset is too large. (MinX - (GlobalX*ScaleX)) < 0\n");
            exit(1);
        }
        double check_min_y = 1.0 + LASHeader_GetMinY(header) - (((double) global_offset_y) * scale_y);
        if (check_min_y < TOLERANCE) {
            fprintf(stderr, "ERROR: Specied Y global offset is too large. (MinY - (GlobalY*ScaleY)) < 0\n");
            exit(1);
        }
        double check_max_x = LASHeader_GetMaxX(header) - (((double) global_offset_x) * scale_x);
        if (check_max_x > (MAX_INT_31 * scale_x)) {
            fprintf(stderr, "ERROR: Specied X global offset is too small. (MaxX - (GlobalX*ScaleX)) > (2^31)*ScaleX\n");
            exit(1);
        }
        double check_max_y = LASHeader_GetMaxY(header) - (((double) global_offset_y) * scale_y);
        if (check_max_y > (MAX_INT_31 * scale_y)) {
            fprintf(stderr, "ERROR: Specied Y global offset is too small. (MaxY - (GlobalY*ScaleY)) > (2^31)*ScaleY\n");
            exit(1);
        }
    }


    /*Write Postgres header*/
    struct postHeader pgHeader;
    pgHeader.s = "PGCOPY\n\377\r\n\0";
    int i1T = 0, i2T = 0;
    pgHeader.i1 = htonl(i1T);
    pgHeader.i2 = htonl(i2T);
    fwrite(pgHeader.s, 11, 1, file_out);
    fwrite(&pgHeader.i1, sizeof(uint32_t), 1, file_out);
    fwrite(&pgHeader.i2, sizeof(uint32_t), 1, file_out);

    /* declaration for morton*/
    uint32_t rawx = 0;
    uint32_t rawy = 0;
    uint64_t mortonkey = 0;

    /* scaled offsets to add for the morton encoding */
    int64_t factorX =  ((int64_t) (LASHeader_GetOffsetX(header) / file_scale_x)) - global_offset_x;
    int64_t factorY =  ((int64_t) (LASHeader_GetOffsetY(header) / file_scale_y)) - global_offset_y;

    p = LASReader_GetNextPoint(reader);
    while (p)
    {
        if (skip_invalid && !LASPoint_IsValid(p)) {
            if (verbose) {
                LASError_Print("Skipping writing invalid point...");
            }
            p = LASReader_GetNextPoint(reader);
            index -=1;
            continue;
        }
        struct postRow pgRow;
        uint32_t size;
        uint16_t hT = num_entries;
        pgRow.h = htons(hT);
        fwrite(& pgRow.h, 2, 1, file_out);
        size = sizeof(double);
        pgRow.vardSize = htonl(size);
        size = sizeof(uint32_t);
        pgRow.varSize = htonl(size);

        i = 0;
        for (;;)
        {
            LASColorH color = LASPoint_GetColor(p);
            double vard;
            int var;
            unsigned long long int vardL, varL;

            switch (parse_string[i])
               {
                    /* // the morton code on xy */
                case 'k':
                    rawx = (uint32_t) (((int64_t) LASPoint_GetRawX(p)) + factorX);
                    rawy = (uint32_t) (((int64_t) LASPoint_GetRawY(p)) + factorY);
                    mortonkey = EncodeMorton2D(rawx,rawy);
                    varL = htobe64(mortonkey);
                    fwrite(&pgRow.vardSize, sizeof(uint32_t), 1, file_out);
                    fwrite(&varL, sizeof(uint64_t), 1, file_out);
                    break;
                    /* // the x coordinate */
                case 'x':
                    vard = LASPoint_GetX(p);
                    fwrite(&pgRow.vardSize, sizeof(uint32_t), 1, file_out);
                    vardL = bigEndian_double(vard);
                    fwrite(&vardL, sizeof(double), 1, file_out);
                    break;
                    /* // the y coordinate */
                case 'y':
                    vard = LASPoint_GetY(p);
                    fwrite(&pgRow.vardSize, sizeof(uint32_t), 1, file_out);
                    vardL = bigEndian_double(vard);
                    fwrite(&vardL, sizeof(double), 1, file_out);
                    break;
                    /* // the z coordinate */
                case 'z':
                    vard = LASPoint_GetZ(p);
                    fwrite(&pgRow.vardSize, sizeof(uint32_t), 1, file_out);
                    vardL = bigEndian_double(vard);
                    fwrite(&vardL, sizeof(double), 1, file_out);
                    break;
                    /* // the gps-time */
                case 't':
                    vard = LASPoint_GetTime(p);
                    fwrite(&pgRow.vardSize, sizeof(uint32_t), 1, file_out);
                    vardL = bigEndian_double(vard);
                    fwrite(&vardL, sizeof(double), 1, file_out);
                    break;
                    /* // the intensity */
                case 'i':
                    var = LASPoint_GetIntensity(p);
                    fwrite(&pgRow.varSize, sizeof(uint32_t), 1, file_out);
                    varL = htonl(var);
                    fwrite(&varL, sizeof(uint32_t), 1, file_out);
                    break;
                    /* the scan angle */
                case 'a':
                    var = LASPoint_GetScanAngleRank(p);
                    fwrite(&pgRow.varSize, sizeof(uint32_t), 1, file_out);
                    varL = htonl(var);
                    fwrite(&varL, sizeof(uint32_t), 1, file_out);
                    break;
                    /* the number of the return */
                case 'r':
                    var = LASPoint_GetReturnNumber(p);
                    fwrite(&pgRow.varSize, sizeof(uint32_t), 1, file_out);
                    varL = htonl(var);
                    fwrite(&varL, sizeof(uint32_t), 1, file_out);
                    break;
                    /* the classification */
                case 'c':
                    var = LASPoint_GetClassification(p);
                    fwrite(&pgRow.varSize, sizeof(uint32_t), 1, file_out);
                    varL = htonl(var);
                    fwrite(&varL, sizeof(uint32_t), 1, file_out);
                    break;
                    /* the user data */
                case 'u':
                    var = LASPoint_GetUserData(p);
                    fwrite(&pgRow.varSize, sizeof(uint32_t), 1, file_out);
                    varL = htonl(var);
                    fwrite(&varL, sizeof(uint32_t), 1, file_out);
                    break;
                    /* the number of returns of given pulse */
                case 'n':
                    var = LASPoint_GetNumberOfReturns(p);
                    fwrite(&pgRow.varSize, sizeof(uint32_t), 1, file_out);
                    varL = htonl(var);
                    fwrite(&varL, sizeof(uint32_t), 1, file_out);
                    break;
                    /* the red channel color */
                case 'R':
                    var = LASColor_GetRed(color);
                    fwrite(&pgRow.varSize, sizeof(uint32_t), 1, file_out);
                    varL = htonl(var);
                    fwrite(&varL, sizeof(uint32_t), 1, file_out);
                    break;
                    /* the green channel color */
                case 'G':
                    var = LASColor_GetGreen(color);
                    fwrite(&pgRow.varSize, sizeof(uint32_t), 1, file_out);
                    varL = htonl(var);
                    fwrite(&varL, sizeof(uint32_t), 1, file_out);
                    break;
                    /* the blue channel color */
                case 'B':
                    var = LASColor_GetBlue(color);
                    fwrite(&pgRow.varSize, sizeof(uint32_t), 1, file_out);
                    varL = htonl(var);
                    fwrite(&varL, sizeof(uint32_t), 1, file_out);
                    break;
                case 'M':
                    var = index;
                    fwrite(&pgRow.varSize, sizeof(uint32_t), 1, file_out);
                    varL = htonl(var);
                    fwrite(&varL, sizeof(uint32_t), 1, file_out);
                    break;
                case 'p':
                    var = LASPoint_GetPointSourceId(p);
                    fwrite(&pgRow.varSize, sizeof(uint32_t), 1, file_out);
                    varL = htonl(var);
                    fwrite(&varL, sizeof(uint32_t), 1, file_out);
                    break;
                    /* the edge of flight line flag */
                case 'e':
                    var = LASPoint_GetFlightLineEdge(p);
                    fwrite(&pgRow.varSize, sizeof(uint32_t), 1, file_out);
                    varL = htonl(var);
                    fwrite(&varL, sizeof(uint32_t), 1, file_out);
                    break;
                    /* the direction of scan flag */
                case 'd':
                    var = LASPoint_GetScanDirection(p);
                    fwrite(&pgRow.varSize, sizeof(uint32_t), 1, file_out);
                    varL = htonl(var);
                    fwrite(&varL, sizeof(uint32_t), 1, file_out);
                    break;
            }
            i++;
            if (!parse_string[i])
            {
                break;
            }
            LASColor_Destroy(color);
        }
        p = LASReader_GetNextPoint(reader);
        index +=1;
    }
    short endT = -1;
    short end = htons(endT);
    fwrite(&end, sizeof(end), 1, file_out);

    fflush(file_out);
    fclose(file_out);

    LASReader_Destroy(reader);
    LASHeader_Destroy(header);
    return 0;
}