Integer overflow attack

#include <stdio.h>

// https://stackoverflow.com/questions/111928/is-there-a-printf-converter-to-print-in-binary-format
// Assumes little endian
void print_bits(size_t const size, void const * const ptr) {
    unsigned char *b = (unsigned char*) ptr;
    unsigned char byte;
    int i, j;
    for (i = size-1; i >= 0; i--) {
        for (j = 7; j >= 0; j--) {
            byte = (b[i] >> j) & 1;
            printf("%u", byte);
        }
    }
    printf("\n");
}


int main() {
	unsigned char a = 255;
	unsigned short b = 511;
	print_bits(sizeof(a), &a);
	print_bits(sizeof(a), &b);
	unsigned char c = (unsigned char) b;
	print_bits(sizeof(c), &c);
	if (a == c) {
		printf("Integer overflow attack!\n");
	} else {
		printf("All is fine\n");
	}
	return 0;
}

*Update java example

long x = 50000L; // 64 bits
long y = new BigInteger("2").pow(64).add(BigInteger.valueOf(x)).longValue();
System.out.printf("x(%d) = y(%d) = %b", x, y, x == y);

MariaDB Insert Large Dataset (Java)

EntityManager entityManagerA = entityManagerFactoryA.createEntityManager();
QPayment payment = QPayment.payment;
JPAQueryFactory qf = new JPAQueryFactory(entityManagerA);
JPAQuery<PaymentResponse> query = qf.from(payment)
        .groupBy(payment.refnum)
        .select(
                Projections.bean(
                        PaymentResponse.class,
                        payment.refnum.as(PaymentResponse.PaymentResponse_.REFNUM),
                        payment.idtrx.as(PaymentResponse.PaymentResponse_.IDTRX),
                        payment.jenisTransaksi.as(PaymentResponse.PaymentResponse_.PRODUK),
                        payment.tglbayar.as(PaymentResponse.PaymentResponse_.TANGGAL_BAYAR),
                        payment.rptag.castToNum(Long.class).add(payment.rpadmin.castToNum(Long.class)).sum().stringValue().as(PaymentResponse.PaymentResponse_.AMOUNT)
                )
        ).where(payment.tglbayar.between(from, to).and(payment.kodebank.in(banks)))
        .orderBy(payment.tglbayar.desc());

String start = DateTimes.format(from);
String end = DateTimes.format(to);
String file = System.getProperty("user.dir") + "/" + start + "-" + end + "-" + System.currentTimeMillis() + ".csv";
LocalDateTime now = DateTimes.now();
log.info("Started...");
log.info("Writing to file: {}", file);
final AtomicLong total = new AtomicLong();
try (RandomAccessFile writer = new RandomAccessFile(file, "rw");
     FileChannel channel = writer.getChannel()) {
    final ByteBuffer buff = ByteBuffer.allocate(65535);
    query.stream().forEach(paymentResponse -> {
        buff.clear();
        String line = String.format("%s;%s;%s;%s;%d;%s\n", paymentResponse.getAmount(), DateTimes.format(paymentResponse.getTanggalBayar()), paymentResponse.getIdtrx(), paymentResponse.getProduk(), pullPlnId, paymentResponse.getRefnum());
        buff.put(line.getBytes(StandardCharsets.UTF_8));
        buff.flip();
        try {
            channel.write(buff);
            total.incrementAndGet();
        } catch (IOException e) {
            // do something
        }
    });
} catch (FileNotFoundException e) {
    // do something
} catch (IOException e) {
    // do something
} finally {
    entityManagerA.close();
}

EntityManager entityManagerB = entityManagerFactoryB.createEntityManager();

Session session = entityManagerB.unwrap(Session.class);
session.doWork(connection -> {
    String baQuery = "LOAD DATA LOCAL INFILE ? INTO TABLE my_dst_table FIELDS TERMINATED BY ';' LINES TERMINATED BY '\n'";
    PreparedStatement preparedStatement = connection.prepareStatement(baQuery);
    preparedStatement.setString(1, file);
    boolean execute = preparedStatement.execute();
    System.out.println("EXECUTED: " + execute);
});
long between = ChronoUnit.MINUTES.between(now, DateTimes.now());
log.info("Done: takes {} minutes.", between);
entityManagerB.close();

*) Inserting more then 400k data takes less the a minutes.

Refs:

https://mariadb.com/kb/en/how-to-quickly-insert-data-into-mariadb/

https://mariadb.com/kb/en/load-data-infile/

https://mariadb.com/kb/en/unsafe-statements-for-statement-based-replication/

Prime Factor

Simple prime factorization

import math

def prime_factor(num):
    factors = []
    while num % 2 == 0:
        factors.append(2)
        num = num / 2
    for i in range(3, int(math.sqrt(num)) + 1, 2): # 3, 5, 7, ...
        while num % i == 0:
            factors.append(i)
            num = num / i
    if num > 2:
        factors.append(num)
    return factors


num = 1000003 + 1
factors = prime_factor(num)

if len(factors) == 1:
    print(str(num) + " is prime")
else:
    print(str(num) + " = multiply(" + str(factors) + ")")